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.98 $ $Date: 2005/04/18 13:41:29 $
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.98 $"
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 if (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT)
1107 * set or del multicast address on LCS card
1110 lcs_fix_multicast_list(struct lcs_card *card)
1112 struct list_head failed_list;
1113 struct lcs_ipm_list *ipm, *tmp;
1114 unsigned long flags;
1117 LCS_DBF_TEXT(4,trace, "fixipm");
1118 INIT_LIST_HEAD(&failed_list);
1119 spin_lock_irqsave(&card->ipm_lock, flags);
1121 list_for_each_entry_safe(ipm, tmp, &card->ipm_list, list){
1122 switch (ipm->ipm_state) {
1123 case LCS_IPM_STATE_SET_REQUIRED:
1124 /* del from ipm_list so noone else can tamper with
1126 list_del_init(&ipm->list);
1127 spin_unlock_irqrestore(&card->ipm_lock, flags);
1128 rc = lcs_send_setipm(card, ipm);
1129 spin_lock_irqsave(&card->ipm_lock, flags);
1131 PRINT_INFO("Adding multicast address failed."
1132 "Table possibly full!\n");
1133 /* store ipm in failed list -> will be added
1134 * to ipm_list again, so a retry will be done
1135 * during the next call of this function */
1136 list_add_tail(&ipm->list, &failed_list);
1138 ipm->ipm_state = LCS_IPM_STATE_ON_CARD;
1139 /* re-insert into ipm_list */
1140 list_add_tail(&ipm->list, &card->ipm_list);
1143 case LCS_IPM_STATE_DEL_REQUIRED:
1144 list_del(&ipm->list);
1145 spin_unlock_irqrestore(&card->ipm_lock, flags);
1146 lcs_send_delipm(card, ipm);
1147 spin_lock_irqsave(&card->ipm_lock, flags);
1150 case LCS_IPM_STATE_ON_CARD:
1154 /* re-insert all entries from the failed_list into ipm_list */
1155 list_for_each_entry_safe(ipm, tmp, &failed_list, list) {
1156 list_del_init(&ipm->list);
1157 list_add_tail(&ipm->list, &card->ipm_list);
1159 spin_unlock_irqrestore(&card->ipm_lock, flags);
1160 if (card->state == DEV_STATE_UP)
1161 netif_wake_queue(card->dev);
1165 * get mac address for the relevant Multicast address
1168 lcs_get_mac_for_ipm(__u32 ipm, char *mac, struct net_device *dev)
1170 LCS_DBF_TEXT(4,trace, "getmac");
1171 if (dev->type == ARPHRD_IEEE802_TR)
1172 ip_tr_mc_map(ipm, mac);
1174 ip_eth_mc_map(ipm, mac);
1178 * function called by net device to handle multicast address relevant things
1181 lcs_remove_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1183 struct ip_mc_list *im4;
1184 struct list_head *l;
1185 struct lcs_ipm_list *ipm;
1186 unsigned long flags;
1187 char buf[MAX_ADDR_LEN];
1189 LCS_DBF_TEXT(4, trace, "remmclst");
1190 spin_lock_irqsave(&card->ipm_lock, flags);
1191 list_for_each(l, &card->ipm_list) {
1192 ipm = list_entry(l, struct lcs_ipm_list, list);
1193 for (im4 = in4_dev->mc_list; im4 != NULL; im4 = im4->next) {
1194 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1195 if ( (ipm->ipm.ip_addr == im4->multiaddr) &&
1196 (memcmp(buf, &ipm->ipm.mac_addr,
1197 LCS_MAC_LENGTH) == 0) )
1201 ipm->ipm_state = LCS_IPM_STATE_DEL_REQUIRED;
1203 spin_unlock_irqrestore(&card->ipm_lock, flags);
1206 static inline struct lcs_ipm_list *
1207 lcs_check_addr_entry(struct lcs_card *card, struct ip_mc_list *im4, char *buf)
1209 struct lcs_ipm_list *tmp, *ipm = NULL;
1210 struct list_head *l;
1211 unsigned long flags;
1213 LCS_DBF_TEXT(4, trace, "chkmcent");
1214 spin_lock_irqsave(&card->ipm_lock, flags);
1215 list_for_each(l, &card->ipm_list) {
1216 tmp = list_entry(l, struct lcs_ipm_list, list);
1217 if ( (tmp->ipm.ip_addr == im4->multiaddr) &&
1218 (memcmp(buf, &tmp->ipm.mac_addr,
1219 LCS_MAC_LENGTH) == 0) ) {
1224 spin_unlock_irqrestore(&card->ipm_lock, flags);
1229 lcs_set_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1232 struct ip_mc_list *im4;
1233 struct lcs_ipm_list *ipm;
1234 char buf[MAX_ADDR_LEN];
1235 unsigned long flags;
1237 LCS_DBF_TEXT(4, trace, "setmclst");
1238 for (im4 = in4_dev->mc_list; im4; im4 = im4->next) {
1239 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1240 ipm = lcs_check_addr_entry(card, im4, buf);
1242 continue; /* Address already in list. */
1243 ipm = (struct lcs_ipm_list *)
1244 kmalloc(sizeof(struct lcs_ipm_list), GFP_ATOMIC);
1246 PRINT_INFO("Not enough memory to add "
1247 "new multicast entry!\n");
1250 memset(ipm, 0, sizeof(struct lcs_ipm_list));
1251 memcpy(&ipm->ipm.mac_addr, buf, LCS_MAC_LENGTH);
1252 ipm->ipm.ip_addr = im4->multiaddr;
1253 ipm->ipm_state = LCS_IPM_STATE_SET_REQUIRED;
1254 spin_lock_irqsave(&card->ipm_lock, flags);
1255 list_add(&ipm->list, &card->ipm_list);
1256 spin_unlock_irqrestore(&card->ipm_lock, flags);
1261 lcs_register_mc_addresses(void *data)
1263 struct lcs_card *card;
1264 struct in_device *in4_dev;
1266 card = (struct lcs_card *) data;
1267 daemonize("regipm");
1269 if (!lcs_do_run_thread(card, LCS_SET_MC_THREAD))
1271 LCS_DBF_TEXT(4, trace, "regmulti");
1273 in4_dev = in_dev_get(card->dev);
1274 if (in4_dev == NULL)
1276 read_lock(&in4_dev->mc_list_lock);
1277 lcs_remove_mc_addresses(card,in4_dev);
1278 lcs_set_mc_addresses(card, in4_dev);
1279 read_unlock(&in4_dev->mc_list_lock);
1280 in_dev_put(in4_dev);
1282 lcs_fix_multicast_list(card);
1284 lcs_clear_thread_running_bit(card, LCS_SET_MC_THREAD);
1288 * function called by net device to
1289 * handle multicast address relevant things
1292 lcs_set_multicast_list(struct net_device *dev)
1294 struct lcs_card *card;
1296 LCS_DBF_TEXT(4, trace, "setmulti");
1297 card = (struct lcs_card *) dev->priv;
1299 if (!lcs_set_thread_start_bit(card, LCS_SET_MC_THREAD)) {
1300 schedule_work(&card->kernel_thread_starter);
1304 #endif /* CONFIG_IP_MULTICAST */
1307 lcs_check_irb_error(struct ccw_device *cdev, struct irb *irb)
1312 switch (PTR_ERR(irb)) {
1314 PRINT_WARN("i/o-error on device %s\n", cdev->dev.bus_id);
1315 LCS_DBF_TEXT(2, trace, "ckirberr");
1316 LCS_DBF_TEXT_(2, trace, " rc%d", -EIO);
1319 PRINT_WARN("timeout on device %s\n", cdev->dev.bus_id);
1320 LCS_DBF_TEXT(2, trace, "ckirberr");
1321 LCS_DBF_TEXT_(2, trace, " rc%d", -ETIMEDOUT);
1324 PRINT_WARN("unknown error %ld on device %s\n", PTR_ERR(irb),
1326 LCS_DBF_TEXT(2, trace, "ckirberr");
1327 LCS_DBF_TEXT(2, trace, " rc???");
1329 return PTR_ERR(irb);
1334 * IRQ Handler for LCS channels
1337 lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1339 struct lcs_card *card;
1340 struct lcs_channel *channel;
1343 if (lcs_check_irb_error(cdev, irb))
1346 card = CARD_FROM_DEV(cdev);
1347 if (card->read.ccwdev == cdev)
1348 channel = &card->read;
1350 channel = &card->write;
1352 LCS_DBF_TEXT_(5, trace, "Rint%s",cdev->dev.bus_id);
1353 LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.cstat, irb->scsw.dstat);
1354 LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.fctl, irb->scsw.actl);
1356 /* How far in the ccw chain have we processed? */
1357 if ((channel->state != CH_STATE_INIT) &&
1358 (irb->scsw.fctl & SCSW_FCTL_START_FUNC)) {
1359 index = (struct ccw1 *) __va((addr_t) irb->scsw.cpa)
1361 if ((irb->scsw.actl & SCSW_ACTL_SUSPENDED) ||
1362 (irb->scsw.cstat | SCHN_STAT_PCI))
1363 /* Bloody io subsystem tells us lies about cpa... */
1364 index = (index - 1) & (LCS_NUM_BUFFS - 1);
1365 while (channel->io_idx != index) {
1366 __lcs_processed_buffer(channel,
1367 channel->iob + channel->io_idx);
1369 (channel->io_idx + 1) & (LCS_NUM_BUFFS - 1);
1373 if ((irb->scsw.dstat & DEV_STAT_DEV_END) ||
1374 (irb->scsw.dstat & DEV_STAT_CHN_END) ||
1375 (irb->scsw.dstat & DEV_STAT_UNIT_CHECK))
1376 /* Mark channel as stopped. */
1377 channel->state = CH_STATE_STOPPED;
1378 else if (irb->scsw.actl & SCSW_ACTL_SUSPENDED)
1379 /* CCW execution stopped on a suspend bit. */
1380 channel->state = CH_STATE_SUSPENDED;
1382 if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1383 if (irb->scsw.cc != 0) {
1384 ccw_device_halt(channel->ccwdev, (addr_t) channel);
1387 /* The channel has been stopped by halt_IO. */
1388 channel->state = CH_STATE_HALTED;
1391 if (irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
1392 channel->state = CH_STATE_CLEARED;
1394 /* Do the rest in the tasklet. */
1395 tasklet_schedule(&channel->irq_tasklet);
1399 * Tasklet for IRQ handler
1402 lcs_tasklet(unsigned long data)
1404 unsigned long flags;
1405 struct lcs_channel *channel;
1406 struct lcs_buffer *iob;
1410 channel = (struct lcs_channel *) data;
1411 LCS_DBF_TEXT_(5, trace, "tlet%s",channel->ccwdev->dev.bus_id);
1413 /* Check for processed buffers. */
1415 buf_idx = channel->buf_idx;
1416 while (iob[buf_idx].state == BUF_STATE_PROCESSED) {
1417 /* Do the callback thing. */
1418 if (iob[buf_idx].callback != NULL)
1419 iob[buf_idx].callback(channel, iob + buf_idx);
1420 buf_idx = (buf_idx + 1) & (LCS_NUM_BUFFS - 1);
1422 channel->buf_idx = buf_idx;
1424 if (channel->state == CH_STATE_STOPPED)
1425 // FIXME: what if rc != 0 ??
1426 rc = lcs_start_channel(channel);
1427 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1428 if (channel->state == CH_STATE_SUSPENDED &&
1429 channel->iob[channel->io_idx].state == BUF_STATE_READY) {
1430 // FIXME: what if rc != 0 ??
1431 rc = __lcs_resume_channel(channel);
1433 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1435 /* Something happened on the channel. Wake up waiters. */
1436 wake_up(&channel->wait_q);
1440 * Finish current tx buffer and make it ready for transmit.
1443 __lcs_emit_txbuffer(struct lcs_card *card)
1445 LCS_DBF_TEXT(5, trace, "emittx");
1446 *(__u16 *)(card->tx_buffer->data + card->tx_buffer->count) = 0;
1447 card->tx_buffer->count += 2;
1448 lcs_ready_buffer(&card->write, card->tx_buffer);
1449 card->tx_buffer = NULL;
1454 * Callback for finished tx buffers.
1457 lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1459 struct lcs_card *card;
1461 LCS_DBF_TEXT(5, trace, "txbuffcb");
1462 /* Put buffer back to pool. */
1463 lcs_release_buffer(channel, buffer);
1464 card = (struct lcs_card *)
1465 ((char *) channel - offsetof(struct lcs_card, write));
1466 spin_lock(&card->lock);
1468 if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1470 * Last running tx buffer has finished. Submit partially
1471 * filled current buffer.
1473 __lcs_emit_txbuffer(card);
1474 spin_unlock(&card->lock);
1478 * Packet transmit function called by network stack
1481 __lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
1482 struct net_device *dev)
1484 struct lcs_header *header;
1486 LCS_DBF_TEXT(5, trace, "hardxmit");
1488 card->stats.tx_dropped++;
1489 card->stats.tx_errors++;
1492 if (card->state != DEV_STATE_UP) {
1494 card->stats.tx_dropped++;
1495 card->stats.tx_errors++;
1496 card->stats.tx_carrier_errors++;
1499 if (netif_queue_stopped(dev) ) {
1500 card->stats.tx_dropped++;
1503 if (card->tx_buffer != NULL &&
1504 card->tx_buffer->count + sizeof(struct lcs_header) +
1505 skb->len + sizeof(u16) > LCS_IOBUFFERSIZE)
1506 /* skb too big for current tx buffer. */
1507 __lcs_emit_txbuffer(card);
1508 if (card->tx_buffer == NULL) {
1509 /* Get new tx buffer */
1510 card->tx_buffer = lcs_get_buffer(&card->write);
1511 if (card->tx_buffer == NULL) {
1512 card->stats.tx_dropped++;
1515 card->tx_buffer->callback = lcs_txbuffer_cb;
1516 card->tx_buffer->count = 0;
1518 header = (struct lcs_header *)
1519 (card->tx_buffer->data + card->tx_buffer->count);
1520 card->tx_buffer->count += skb->len + sizeof(struct lcs_header);
1521 header->offset = card->tx_buffer->count;
1522 header->type = card->lan_type;
1523 header->slot = card->portno;
1524 memcpy(header + 1, skb->data, skb->len);
1525 card->stats.tx_bytes += skb->len;
1526 card->stats.tx_packets++;
1528 if (card->tx_emitted <= 0)
1529 /* If this is the first tx buffer emit it immediately. */
1530 __lcs_emit_txbuffer(card);
1535 lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
1537 struct lcs_card *card;
1540 LCS_DBF_TEXT(5, trace, "pktxmit");
1541 card = (struct lcs_card *) dev->priv;
1542 spin_lock(&card->lock);
1543 rc = __lcs_start_xmit(card, skb, dev);
1544 spin_unlock(&card->lock);
1549 * send startlan and lanstat command to make LCS device ready
1552 lcs_startlan_auto(struct lcs_card *card)
1556 LCS_DBF_TEXT(2, trace, "strtauto");
1557 #ifdef CONFIG_NET_ETHERNET
1558 card->lan_type = LCS_FRAME_TYPE_ENET;
1559 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1565 card->lan_type = LCS_FRAME_TYPE_TR;
1566 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1571 card->lan_type = LCS_FRAME_TYPE_FDDI;
1572 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1580 lcs_startlan(struct lcs_card *card)
1584 LCS_DBF_TEXT(2, trace, "startlan");
1586 if (card->portno != LCS_INVALID_PORT_NO) {
1587 if (card->lan_type == LCS_FRAME_TYPE_AUTO)
1588 rc = lcs_startlan_auto(card);
1590 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1592 for (i = 0; i <= 16; i++) {
1594 if (card->lan_type != LCS_FRAME_TYPE_AUTO)
1595 rc = lcs_send_startlan(card,
1596 LCS_INITIATOR_TCPIP);
1598 /* autodetecting lan type */
1599 rc = lcs_startlan_auto(card);
1605 return lcs_send_lanstat(card);
1610 * LCS detect function
1611 * setup channels and make them I/O ready
1614 lcs_detect(struct lcs_card *card)
1618 LCS_DBF_TEXT(2, setup, "lcsdetct");
1619 /* start/reset card */
1621 netif_stop_queue(card->dev);
1622 rc = lcs_stop_channels(card);
1624 rc = lcs_start_channels(card);
1626 rc = lcs_send_startup(card, LCS_INITIATOR_TCPIP);
1628 rc = lcs_startlan(card);
1632 card->state = DEV_STATE_UP;
1634 card->state = DEV_STATE_DOWN;
1635 card->write.state = CH_STATE_INIT;
1636 card->read.state = CH_STATE_INIT;
1645 lcs_resetcard(struct lcs_card *card)
1649 LCS_DBF_TEXT(2, trace, "rescard");
1650 for (retries = 0; retries < 10; retries++) {
1651 if (lcs_detect(card) == 0) {
1652 netif_wake_queue(card->dev);
1653 card->state = DEV_STATE_UP;
1654 PRINT_INFO("LCS device %s successfully restarted!\n",
1660 PRINT_ERR("Error in Reseting LCS card!\n");
1669 lcs_stopcard(struct lcs_card *card)
1673 LCS_DBF_TEXT(3, setup, "stopcard");
1675 if (card->read.state != CH_STATE_STOPPED &&
1676 card->write.state != CH_STATE_STOPPED &&
1677 card->state == DEV_STATE_UP) {
1678 lcs_clear_multicast_list(card);
1679 rc = lcs_send_stoplan(card,LCS_INITIATOR_TCPIP);
1680 rc = lcs_send_shutdown(card);
1682 rc = lcs_stop_channels(card);
1683 card->state = DEV_STATE_DOWN;
1689 * LGW initiated commands
1692 lcs_lgw_startlan_thread(void *data)
1694 struct lcs_card *card;
1696 card = (struct lcs_card *) data;
1697 daemonize("lgwstpln");
1699 if (!lcs_do_run_thread(card, LCS_STARTLAN_THREAD))
1701 LCS_DBF_TEXT(4, trace, "lgwstpln");
1703 netif_stop_queue(card->dev);
1704 if (lcs_startlan(card) == 0) {
1705 netif_wake_queue(card->dev);
1706 card->state = DEV_STATE_UP;
1707 PRINT_INFO("LCS Startlan for device %s succeeded!\n",
1711 PRINT_ERR("LCS Startlan for device %s failed!\n",
1713 lcs_clear_thread_running_bit(card, LCS_STARTLAN_THREAD);
1718 * Send startup command initiated by Lan Gateway
1721 lcs_lgw_startup_thread(void *data)
1725 struct lcs_card *card;
1727 card = (struct lcs_card *) data;
1728 daemonize("lgwstaln");
1730 if (!lcs_do_run_thread(card, LCS_STARTUP_THREAD))
1732 LCS_DBF_TEXT(4, trace, "lgwstaln");
1734 netif_stop_queue(card->dev);
1735 rc = lcs_send_startup(card, LCS_INITIATOR_LGW);
1737 PRINT_ERR("Startup for LCS device %s initiated " \
1738 "by LGW failed!\nReseting card ...\n",
1740 /* do a card reset */
1741 rc = lcs_resetcard(card);
1745 rc = lcs_startlan(card);
1747 netif_wake_queue(card->dev);
1748 card->state = DEV_STATE_UP;
1752 PRINT_INFO("LCS Startup for device %s succeeded!\n",
1755 PRINT_ERR("LCS Startup for device %s failed!\n",
1757 lcs_clear_thread_running_bit(card, LCS_STARTUP_THREAD);
1763 * send stoplan command initiated by Lan Gateway
1766 lcs_lgw_stoplan_thread(void *data)
1768 struct lcs_card *card;
1771 card = (struct lcs_card *) data;
1772 daemonize("lgwstop");
1774 if (!lcs_do_run_thread(card, LCS_STOPLAN_THREAD))
1776 LCS_DBF_TEXT(4, trace, "lgwstop");
1778 netif_stop_queue(card->dev);
1779 if (lcs_send_stoplan(card, LCS_INITIATOR_LGW) == 0)
1780 PRINT_INFO("Stoplan for %s initiated by LGW succeeded!\n",
1783 PRINT_ERR("Stoplan %s initiated by LGW failed!\n",
1785 /*Try to reset the card, stop it on failure */
1786 rc = lcs_resetcard(card);
1788 rc = lcs_stopcard(card);
1789 lcs_clear_thread_running_bit(card, LCS_STOPLAN_THREAD);
1794 * Kernel Thread helper functions for LGW initiated commands
1797 lcs_start_kernel_thread(struct lcs_card *card)
1799 LCS_DBF_TEXT(5, trace, "krnthrd");
1800 if (lcs_do_start_thread(card, LCS_STARTUP_THREAD))
1801 kernel_thread(lcs_lgw_startup_thread, (void *) card, SIGCHLD);
1802 if (lcs_do_start_thread(card, LCS_STARTLAN_THREAD))
1803 kernel_thread(lcs_lgw_startlan_thread, (void *) card, SIGCHLD);
1804 if (lcs_do_start_thread(card, LCS_STOPLAN_THREAD))
1805 kernel_thread(lcs_lgw_stoplan_thread, (void *) card, SIGCHLD);
1806 #ifdef CONFIG_IP_MULTICAST
1807 if (lcs_do_start_thread(card, LCS_SET_MC_THREAD))
1808 kernel_thread(lcs_register_mc_addresses, (void *) card, SIGCHLD);
1813 * Process control frames.
1816 lcs_get_control(struct lcs_card *card, struct lcs_cmd *cmd)
1818 LCS_DBF_TEXT(5, trace, "getctrl");
1819 if (cmd->initiator == LCS_INITIATOR_LGW) {
1820 switch(cmd->cmd_code) {
1821 case LCS_CMD_STARTUP:
1822 if (!lcs_set_thread_start_bit(card,
1823 LCS_STARTUP_THREAD))
1824 schedule_work(&card->kernel_thread_starter);
1826 case LCS_CMD_STARTLAN:
1827 if (!lcs_set_thread_start_bit(card,
1828 LCS_STARTLAN_THREAD))
1829 schedule_work(&card->kernel_thread_starter);
1831 case LCS_CMD_STOPLAN:
1832 if (!lcs_set_thread_start_bit(card,
1833 LCS_STOPLAN_THREAD))
1834 schedule_work(&card->kernel_thread_starter);
1837 PRINT_INFO("UNRECOGNIZED LGW COMMAND\n");
1841 lcs_notify_lancmd_waiters(card, cmd);
1845 * Unpack network packet.
1848 lcs_get_skb(struct lcs_card *card, char *skb_data, unsigned int skb_len)
1850 struct sk_buff *skb;
1852 LCS_DBF_TEXT(5, trace, "getskb");
1853 if (card->dev == NULL ||
1854 card->state != DEV_STATE_UP)
1855 /* The card isn't up. Ignore the packet. */
1858 skb = dev_alloc_skb(skb_len);
1860 PRINT_ERR("LCS: alloc_skb failed for device=%s\n",
1862 card->stats.rx_dropped++;
1865 skb->dev = card->dev;
1866 memcpy(skb_put(skb, skb_len), skb_data, skb_len);
1867 skb->protocol = card->lan_type_trans(skb, card->dev);
1868 card->stats.rx_bytes += skb_len;
1869 card->stats.rx_packets++;
1870 *((__u32 *)skb->cb) = ++card->pkt_seq;
1875 * LCS main routine to get packets and lancmd replies from the buffers
1878 lcs_get_frames_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1880 struct lcs_card *card;
1881 struct lcs_header *lcs_hdr;
1884 LCS_DBF_TEXT(5, trace, "lcsgtpkt");
1885 lcs_hdr = (struct lcs_header *) buffer->data;
1886 if (lcs_hdr->offset == LCS_ILLEGAL_OFFSET) {
1887 LCS_DBF_TEXT(4, trace, "-eiogpkt");
1890 card = (struct lcs_card *)
1891 ((char *) channel - offsetof(struct lcs_card, read));
1893 while (lcs_hdr->offset != 0) {
1894 if (lcs_hdr->offset <= 0 ||
1895 lcs_hdr->offset > LCS_IOBUFFERSIZE ||
1896 lcs_hdr->offset < offset) {
1897 /* Offset invalid. */
1898 card->stats.rx_length_errors++;
1899 card->stats.rx_errors++;
1902 /* What kind of frame is it? */
1903 if (lcs_hdr->type == LCS_FRAME_TYPE_CONTROL)
1904 /* Control frame. */
1905 lcs_get_control(card, (struct lcs_cmd *) lcs_hdr);
1906 else if (lcs_hdr->type == LCS_FRAME_TYPE_ENET ||
1907 lcs_hdr->type == LCS_FRAME_TYPE_TR ||
1908 lcs_hdr->type == LCS_FRAME_TYPE_FDDI)
1909 /* Normal network packet. */
1910 lcs_get_skb(card, (char *)(lcs_hdr + 1),
1911 lcs_hdr->offset - offset -
1912 sizeof(struct lcs_header));
1914 /* Unknown frame type. */
1915 ; // FIXME: error message ?
1916 /* Proceed to next frame. */
1917 offset = lcs_hdr->offset;
1918 lcs_hdr->offset = LCS_ILLEGAL_OFFSET;
1919 lcs_hdr = (struct lcs_header *) (buffer->data + offset);
1921 /* The buffer is now empty. Make it ready again. */
1922 lcs_ready_buffer(&card->read, buffer);
1926 * get network statistics for ifconfig and other user programs
1928 static struct net_device_stats *
1929 lcs_getstats(struct net_device *dev)
1931 struct lcs_card *card;
1933 LCS_DBF_TEXT(4, trace, "netstats");
1934 card = (struct lcs_card *) dev->priv;
1935 return &card->stats;
1940 * This function will be called by user doing ifconfig xxx down
1943 lcs_stop_device(struct net_device *dev)
1945 struct lcs_card *card;
1948 LCS_DBF_TEXT(2, trace, "stopdev");
1949 card = (struct lcs_card *) dev->priv;
1950 netif_stop_queue(dev);
1951 dev->flags &= ~IFF_UP;
1952 rc = lcs_stopcard(card);
1954 PRINT_ERR("Try it again!\n ");
1959 * start lcs device and make it runnable
1960 * This function will be called by user doing ifconfig xxx up
1963 lcs_open_device(struct net_device *dev)
1965 struct lcs_card *card;
1968 LCS_DBF_TEXT(2, trace, "opendev");
1969 card = (struct lcs_card *) dev->priv;
1970 /* initialize statistics */
1971 rc = lcs_detect(card);
1973 PRINT_ERR("LCS:Error in opening device!\n");
1976 dev->flags |= IFF_UP;
1977 netif_wake_queue(dev);
1978 card->state = DEV_STATE_UP;
1984 * show function for portno called by cat or similar things
1987 lcs_portno_show (struct device *dev, char *buf)
1989 struct lcs_card *card;
1991 card = (struct lcs_card *)dev->driver_data;
1996 return sprintf(buf, "%d\n", card->portno);
2000 * store the value which is piped to file portno
2003 lcs_portno_store (struct device *dev, const char *buf, size_t count)
2005 struct lcs_card *card;
2008 card = (struct lcs_card *)dev->driver_data;
2013 sscanf(buf, "%u", &value);
2014 /* TODO: sanity checks */
2015 card->portno = value;
2021 static DEVICE_ATTR(portno, 0644, lcs_portno_show, lcs_portno_store);
2024 lcs_type_show(struct device *dev, char *buf)
2026 struct ccwgroup_device *cgdev;
2028 cgdev = to_ccwgroupdev(dev);
2032 return sprintf(buf, "%s\n", cu3088_type[cgdev->cdev[0]->id.driver_info]);
2035 static DEVICE_ATTR(type, 0444, lcs_type_show, NULL);
2038 lcs_timeout_show(struct device *dev, char *buf)
2040 struct lcs_card *card;
2042 card = (struct lcs_card *)dev->driver_data;
2044 return card ? sprintf(buf, "%u\n", card->lancmd_timeout) : 0;
2048 lcs_timeout_store (struct device *dev, const char *buf, size_t count)
2050 struct lcs_card *card;
2053 card = (struct lcs_card *)dev->driver_data;
2058 sscanf(buf, "%u", &value);
2059 /* TODO: sanity checks */
2060 card->lancmd_timeout = value;
2066 DEVICE_ATTR(lancmd_timeout, 0644, lcs_timeout_show, lcs_timeout_store);
2068 static struct attribute * lcs_attrs[] = {
2069 &dev_attr_portno.attr,
2070 &dev_attr_type.attr,
2071 &dev_attr_lancmd_timeout.attr,
2075 static struct attribute_group lcs_attr_group = {
2080 * lcs_probe_device is called on establishing a new ccwgroup_device.
2083 lcs_probe_device(struct ccwgroup_device *ccwgdev)
2085 struct lcs_card *card;
2088 if (!get_device(&ccwgdev->dev))
2091 LCS_DBF_TEXT(2, setup, "add_dev");
2092 card = lcs_alloc_card();
2094 PRINT_ERR("Allocation of lcs card failed\n");
2095 put_device(&ccwgdev->dev);
2098 ret = sysfs_create_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2100 PRINT_ERR("Creating attributes failed");
2101 lcs_free_card(card);
2102 put_device(&ccwgdev->dev);
2105 ccwgdev->dev.driver_data = card;
2106 ccwgdev->cdev[0]->handler = lcs_irq;
2107 ccwgdev->cdev[1]->handler = lcs_irq;
2112 lcs_register_netdev(struct ccwgroup_device *ccwgdev)
2114 struct lcs_card *card;
2116 LCS_DBF_TEXT(2, setup, "regnetdv");
2117 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2118 if (card->dev->reg_state != NETREG_UNINITIALIZED)
2120 SET_NETDEV_DEV(card->dev, &ccwgdev->dev);
2121 return register_netdev(card->dev);
2125 * lcs_new_device will be called by setting the group device online.
2129 lcs_new_device(struct ccwgroup_device *ccwgdev)
2131 struct lcs_card *card;
2132 struct net_device *dev=NULL;
2133 enum lcs_dev_states recover_state;
2136 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2140 LCS_DBF_TEXT(2, setup, "newdev");
2141 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2142 card->read.ccwdev = ccwgdev->cdev[0];
2143 card->write.ccwdev = ccwgdev->cdev[1];
2145 recover_state = card->state;
2146 ccw_device_set_online(card->read.ccwdev);
2147 ccw_device_set_online(card->write.ccwdev);
2149 LCS_DBF_TEXT(3, setup, "lcsnewdv");
2151 lcs_setup_card(card);
2152 rc = lcs_detect(card);
2154 LCS_DBF_TEXT(2, setup, "dtctfail");
2155 PRINT_WARN("Detection of LCS card failed with return code "
2156 "%d (0x%x)\n", rc, rc);
2161 LCS_DBF_TEXT(2, setup, "samedev");
2162 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2165 switch (card->lan_type) {
2166 #ifdef CONFIG_NET_ETHERNET
2167 case LCS_FRAME_TYPE_ENET:
2168 card->lan_type_trans = eth_type_trans;
2169 dev = alloc_etherdev(0);
2173 case LCS_FRAME_TYPE_TR:
2174 card->lan_type_trans = tr_type_trans;
2175 dev = alloc_trdev(0);
2179 case LCS_FRAME_TYPE_FDDI:
2180 card->lan_type_trans = fddi_type_trans;
2181 dev = alloc_fddidev(0);
2185 LCS_DBF_TEXT(3, setup, "errinit");
2186 PRINT_ERR("LCS: Initialization failed\n");
2187 PRINT_ERR("LCS: No device found!\n");
2193 card->dev->priv = card;
2194 card->dev->open = lcs_open_device;
2195 card->dev->stop = lcs_stop_device;
2196 card->dev->hard_start_xmit = lcs_start_xmit;
2197 card->dev->get_stats = lcs_getstats;
2198 SET_MODULE_OWNER(dev);
2199 memcpy(card->dev->dev_addr, card->mac, LCS_MAC_LENGTH);
2200 #ifdef CONFIG_IP_MULTICAST
2201 if (!lcs_check_multicast_support(card))
2202 card->dev->set_multicast_list = lcs_set_multicast_list;
2205 lcs_set_allowed_threads(card,0xffffffff);
2206 if (recover_state == DEV_STATE_RECOVER) {
2207 lcs_set_multicast_list(card->dev);
2208 card->dev->flags |= IFF_UP;
2209 netif_wake_queue(card->dev);
2210 card->state = DEV_STATE_UP;
2215 if (lcs_register_netdev(ccwgdev) != 0)
2218 /* Print out supported assists: IPv6 */
2219 PRINT_INFO("LCS device %s %s IPv6 support\n", card->dev->name,
2220 (card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
2221 "with" : "without");
2222 /* Print out supported assist: Multicast */
2223 PRINT_INFO("LCS device %s %s Multicast support\n", card->dev->name,
2224 (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
2225 "with" : "without");
2229 ccw_device_set_offline(card->read.ccwdev);
2230 ccw_device_set_offline(card->write.ccwdev);
2235 * lcs_shutdown_device, called when setting the group device offline.
2238 lcs_shutdown_device(struct ccwgroup_device *ccwgdev)
2240 struct lcs_card *card;
2241 enum lcs_dev_states recover_state;
2244 LCS_DBF_TEXT(3, setup, "shtdndev");
2245 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2248 lcs_set_allowed_threads(card, 0);
2249 if (lcs_wait_for_threads(card, LCS_SET_MC_THREAD))
2250 return -ERESTARTSYS;
2251 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2252 recover_state = card->state;
2254 ret = lcs_stop_device(card->dev);
2255 ret = ccw_device_set_offline(card->read.ccwdev);
2256 ret = ccw_device_set_offline(card->write.ccwdev);
2257 if (recover_state == DEV_STATE_UP) {
2258 card->state = DEV_STATE_RECOVER;
2266 * lcs_remove_device, free buffers and card
2269 lcs_remove_device(struct ccwgroup_device *ccwgdev)
2271 struct lcs_card *card;
2273 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2277 PRINT_INFO("Removing lcs group device ....\n");
2278 LCS_DBF_TEXT(3, setup, "remdev");
2279 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2280 if (ccwgdev->state == CCWGROUP_ONLINE) {
2281 lcs_shutdown_device(ccwgdev);
2284 unregister_netdev(card->dev);
2285 sysfs_remove_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2286 lcs_cleanup_card(card);
2287 lcs_free_card(card);
2288 put_device(&ccwgdev->dev);
2292 * LCS ccwgroup driver registration
2294 static struct ccwgroup_driver lcs_group_driver = {
2295 .owner = THIS_MODULE,
2298 .driver_id = 0xD3C3E2,
2299 .probe = lcs_probe_device,
2300 .remove = lcs_remove_device,
2301 .set_online = lcs_new_device,
2302 .set_offline = lcs_shutdown_device,
2306 * LCS Module/Kernel initialization function
2309 __init lcs_init_module(void)
2313 PRINT_INFO("Loading %s\n",version);
2314 rc = lcs_register_debug_facility();
2315 LCS_DBF_TEXT(0, setup, "lcsinit");
2317 PRINT_ERR("Initialization failed\n");
2321 rc = register_cu3088_discipline(&lcs_group_driver);
2323 PRINT_ERR("Initialization failed\n");
2332 * LCS module cleanup function
2335 __exit lcs_cleanup_module(void)
2337 PRINT_INFO("Terminating lcs module.\n");
2338 LCS_DBF_TEXT(0, trace, "cleanup");
2339 unregister_cu3088_discipline(&lcs_group_driver);
2340 lcs_unregister_debug_facility();
2343 module_init(lcs_init_module);
2344 module_exit(lcs_cleanup_module);
2346 MODULE_AUTHOR("Frank Pavlic <pavlic@de.ibm.com>");
2347 MODULE_LICENSE("GPL");