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.99 $ $Date: 2005/05/11 08:10:17 $
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.99 $"
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", 2, 1, 8);
97 lcs_dbf_trace = debug_register("lcs_trace", 2, 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 kfree(channel->iob[cnt].data);
149 channel->iob[cnt].data = NULL;
157 lcs_cleanup_channel(struct lcs_channel *channel)
159 LCS_DBF_TEXT(3, setup, "cleanch");
160 /* Kill write channel tasklets. */
161 tasklet_kill(&channel->irq_tasklet);
162 /* Free channel buffers. */
163 lcs_free_channel(channel);
167 * LCS free memory for card and channels.
170 lcs_free_card(struct lcs_card *card)
172 LCS_DBF_TEXT(2, setup, "remcard");
173 LCS_DBF_HEX(2, setup, &card, sizeof(void*));
178 * LCS alloc memory for card and channels
180 static struct lcs_card *
183 struct lcs_card *card;
186 LCS_DBF_TEXT(2, setup, "alloclcs");
188 card = kmalloc(sizeof(struct lcs_card), GFP_KERNEL | GFP_DMA);
191 memset(card, 0, sizeof(struct lcs_card));
192 card->lan_type = LCS_FRAME_TYPE_AUTO;
194 card->lancmd_timeout = LCS_LANCMD_TIMEOUT_DEFAULT;
195 /* Allocate io buffers for the read channel. */
196 rc = lcs_alloc_channel(&card->read);
198 LCS_DBF_TEXT(2, setup, "iccwerr");
202 /* Allocate io buffers for the write channel. */
203 rc = lcs_alloc_channel(&card->write);
205 LCS_DBF_TEXT(2, setup, "iccwerr");
206 lcs_cleanup_channel(&card->read);
211 #ifdef CONFIG_IP_MULTICAST
212 INIT_LIST_HEAD(&card->ipm_list);
214 LCS_DBF_HEX(2, setup, &card, sizeof(void*));
219 * Setup read channel.
222 lcs_setup_read_ccws(struct lcs_card *card)
226 LCS_DBF_TEXT(2, setup, "ireadccw");
227 /* Setup read ccws. */
228 memset(card->read.ccws, 0, sizeof (struct ccw1) * (LCS_NUM_BUFFS + 1));
229 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
230 card->read.ccws[cnt].cmd_code = LCS_CCW_READ;
231 card->read.ccws[cnt].count = LCS_IOBUFFERSIZE;
232 card->read.ccws[cnt].flags =
233 CCW_FLAG_CC | CCW_FLAG_SLI | CCW_FLAG_PCI;
235 * Note: we have allocated the buffer with GFP_DMA, so
236 * we do not need to do set_normalized_cda.
238 card->read.ccws[cnt].cda =
239 (__u32) __pa(card->read.iob[cnt].data);
240 ((struct lcs_header *)
241 card->read.iob[cnt].data)->offset = LCS_ILLEGAL_OFFSET;
242 card->read.iob[cnt].callback = lcs_get_frames_cb;
243 card->read.iob[cnt].state = BUF_STATE_READY;
244 card->read.iob[cnt].count = LCS_IOBUFFERSIZE;
246 card->read.ccws[0].flags &= ~CCW_FLAG_PCI;
247 card->read.ccws[LCS_NUM_BUFFS - 1].flags &= ~CCW_FLAG_PCI;
248 card->read.ccws[LCS_NUM_BUFFS - 1].flags |= CCW_FLAG_SUSPEND;
249 /* Last ccw is a tic (transfer in channel). */
250 card->read.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
251 card->read.ccws[LCS_NUM_BUFFS].cda =
252 (__u32) __pa(card->read.ccws);
253 /* Setg initial state of the read channel. */
254 card->read.state = CH_STATE_INIT;
256 card->read.io_idx = 0;
257 card->read.buf_idx = 0;
261 lcs_setup_read(struct lcs_card *card)
263 LCS_DBF_TEXT(3, setup, "initread");
265 lcs_setup_read_ccws(card);
266 /* Initialize read channel tasklet. */
267 card->read.irq_tasklet.data = (unsigned long) &card->read;
268 card->read.irq_tasklet.func = lcs_tasklet;
269 /* Initialize waitqueue. */
270 init_waitqueue_head(&card->read.wait_q);
274 * Setup write channel.
277 lcs_setup_write_ccws(struct lcs_card *card)
281 LCS_DBF_TEXT(3, setup, "iwritccw");
282 /* Setup write ccws. */
283 memset(card->write.ccws, 0, sizeof(struct ccw1) * LCS_NUM_BUFFS + 1);
284 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
285 card->write.ccws[cnt].cmd_code = LCS_CCW_WRITE;
286 card->write.ccws[cnt].count = 0;
287 card->write.ccws[cnt].flags =
288 CCW_FLAG_SUSPEND | CCW_FLAG_CC | CCW_FLAG_SLI;
290 * Note: we have allocated the buffer with GFP_DMA, so
291 * we do not need to do set_normalized_cda.
293 card->write.ccws[cnt].cda =
294 (__u32) __pa(card->write.iob[cnt].data);
296 /* Last ccw is a tic (transfer in channel). */
297 card->write.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
298 card->write.ccws[LCS_NUM_BUFFS].cda =
299 (__u32) __pa(card->write.ccws);
300 /* Set initial state of the write channel. */
301 card->read.state = CH_STATE_INIT;
303 card->write.io_idx = 0;
304 card->write.buf_idx = 0;
308 lcs_setup_write(struct lcs_card *card)
310 LCS_DBF_TEXT(3, setup, "initwrit");
312 lcs_setup_write_ccws(card);
313 /* Initialize write channel tasklet. */
314 card->write.irq_tasklet.data = (unsigned long) &card->write;
315 card->write.irq_tasklet.func = lcs_tasklet;
316 /* Initialize waitqueue. */
317 init_waitqueue_head(&card->write.wait_q);
321 lcs_set_allowed_threads(struct lcs_card *card, unsigned long threads)
325 spin_lock_irqsave(&card->mask_lock, flags);
326 card->thread_allowed_mask = threads;
327 spin_unlock_irqrestore(&card->mask_lock, flags);
328 wake_up(&card->wait_q);
331 lcs_threads_running(struct lcs_card *card, unsigned long threads)
336 spin_lock_irqsave(&card->mask_lock, flags);
337 rc = (card->thread_running_mask & threads);
338 spin_unlock_irqrestore(&card->mask_lock, flags);
343 lcs_wait_for_threads(struct lcs_card *card, unsigned long threads)
345 return wait_event_interruptible(card->wait_q,
346 lcs_threads_running(card, threads) == 0);
350 lcs_set_thread_start_bit(struct lcs_card *card, unsigned long thread)
354 spin_lock_irqsave(&card->mask_lock, flags);
355 if ( !(card->thread_allowed_mask & thread) ||
356 (card->thread_start_mask & thread) ) {
357 spin_unlock_irqrestore(&card->mask_lock, flags);
360 card->thread_start_mask |= thread;
361 spin_unlock_irqrestore(&card->mask_lock, flags);
366 lcs_clear_thread_running_bit(struct lcs_card *card, unsigned long thread)
370 spin_lock_irqsave(&card->mask_lock, flags);
371 card->thread_running_mask &= ~thread;
372 spin_unlock_irqrestore(&card->mask_lock, flags);
373 wake_up(&card->wait_q);
377 __lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
382 spin_lock_irqsave(&card->mask_lock, flags);
383 if (card->thread_start_mask & thread){
384 if ((card->thread_allowed_mask & thread) &&
385 !(card->thread_running_mask & thread)){
387 card->thread_start_mask &= ~thread;
388 card->thread_running_mask |= thread;
392 spin_unlock_irqrestore(&card->mask_lock, flags);
397 lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
400 wait_event(card->wait_q,
401 (rc = __lcs_do_run_thread(card, thread)) >= 0);
406 lcs_do_start_thread(struct lcs_card *card, unsigned long thread)
411 spin_lock_irqsave(&card->mask_lock, flags);
412 LCS_DBF_TEXT_(4, trace, " %02x%02x%02x",
413 (u8) card->thread_start_mask,
414 (u8) card->thread_allowed_mask,
415 (u8) card->thread_running_mask);
416 rc = (card->thread_start_mask & thread);
417 spin_unlock_irqrestore(&card->mask_lock, flags);
422 * Initialize channels,card and state machines.
425 lcs_setup_card(struct lcs_card *card)
427 LCS_DBF_TEXT(2, setup, "initcard");
428 LCS_DBF_HEX(2, setup, &card, sizeof(void*));
430 lcs_setup_read(card);
431 lcs_setup_write(card);
432 /* Set cards initial state. */
433 card->state = DEV_STATE_DOWN;
434 card->tx_buffer = NULL;
435 card->tx_emitted = 0;
437 /* Initialize kernel thread task used for LGW commands. */
438 INIT_WORK(&card->kernel_thread_starter,
439 (void *)lcs_start_kernel_thread,card);
440 card->thread_start_mask = 0;
441 card->thread_allowed_mask = 0;
442 card->thread_running_mask = 0;
443 init_waitqueue_head(&card->wait_q);
444 spin_lock_init(&card->lock);
445 spin_lock_init(&card->ipm_lock);
446 spin_lock_init(&card->mask_lock);
447 #ifdef CONFIG_IP_MULTICAST
448 INIT_LIST_HEAD(&card->ipm_list);
450 INIT_LIST_HEAD(&card->lancmd_waiters);
454 lcs_clear_multicast_list(struct lcs_card *card)
456 #ifdef CONFIG_IP_MULTICAST
457 struct lcs_ipm_list *ipm;
460 /* Free multicast list. */
461 LCS_DBF_TEXT(3, setup, "clmclist");
462 spin_lock_irqsave(&card->ipm_lock, flags);
463 while (!list_empty(&card->ipm_list)){
464 ipm = list_entry(card->ipm_list.next,
465 struct lcs_ipm_list, list);
466 list_del(&ipm->list);
467 if (ipm->ipm_state != LCS_IPM_STATE_SET_REQUIRED){
468 spin_unlock_irqrestore(&card->ipm_lock, flags);
469 lcs_send_delipm(card, ipm);
470 spin_lock_irqsave(&card->ipm_lock, flags);
474 spin_unlock_irqrestore(&card->ipm_lock, flags);
478 * Cleanup channels,card and state machines.
481 lcs_cleanup_card(struct lcs_card *card)
484 LCS_DBF_TEXT(3, setup, "cleancrd");
485 LCS_DBF_HEX(2,setup,&card,sizeof(void*));
487 if (card->dev != NULL)
488 free_netdev(card->dev);
489 /* Cleanup channels. */
490 lcs_cleanup_channel(&card->write);
491 lcs_cleanup_channel(&card->read);
498 lcs_start_channel(struct lcs_channel *channel)
503 LCS_DBF_TEXT_(4,trace,"ssch%s", channel->ccwdev->dev.bus_id);
504 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
505 rc = ccw_device_start(channel->ccwdev,
506 channel->ccws + channel->io_idx, 0, 0,
507 DOIO_DENY_PREFETCH | DOIO_ALLOW_SUSPEND);
509 channel->state = CH_STATE_RUNNING;
510 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
512 LCS_DBF_TEXT_(4,trace,"essh%s", channel->ccwdev->dev.bus_id);
513 PRINT_ERR("Error in starting channel, rc=%d!\n", rc);
519 lcs_clear_channel(struct lcs_channel *channel)
524 LCS_DBF_TEXT(4,trace,"clearch");
525 LCS_DBF_TEXT_(4,trace,"%s", channel->ccwdev->dev.bus_id);
526 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
527 rc = ccw_device_clear(channel->ccwdev, (addr_t) channel);
528 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
530 LCS_DBF_TEXT_(4,trace,"ecsc%s", channel->ccwdev->dev.bus_id);
533 wait_event(channel->wait_q, (channel->state == CH_STATE_CLEARED));
534 channel->state = CH_STATE_STOPPED;
543 lcs_stop_channel(struct lcs_channel *channel)
548 if (channel->state == CH_STATE_STOPPED)
550 LCS_DBF_TEXT(4,trace,"haltsch");
551 LCS_DBF_TEXT_(4,trace,"%s", channel->ccwdev->dev.bus_id);
552 channel->state = CH_STATE_INIT;
553 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
554 rc = ccw_device_halt(channel->ccwdev, (addr_t) channel);
555 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
557 LCS_DBF_TEXT_(4,trace,"ehsc%s", channel->ccwdev->dev.bus_id);
560 /* Asynchronous halt initialted. Wait for its completion. */
561 wait_event(channel->wait_q, (channel->state == CH_STATE_HALTED));
562 lcs_clear_channel(channel);
567 * start read and write channel
570 lcs_start_channels(struct lcs_card *card)
574 LCS_DBF_TEXT(2, trace, "chstart");
575 /* start read channel */
576 rc = lcs_start_channel(&card->read);
579 /* start write channel */
580 rc = lcs_start_channel(&card->write);
582 lcs_stop_channel(&card->read);
587 * stop read and write channel
590 lcs_stop_channels(struct lcs_card *card)
592 LCS_DBF_TEXT(2, trace, "chhalt");
593 lcs_stop_channel(&card->read);
594 lcs_stop_channel(&card->write);
601 static struct lcs_buffer *
602 __lcs_get_buffer(struct lcs_channel *channel)
606 LCS_DBF_TEXT(5, trace, "_getbuff");
607 index = channel->io_idx;
609 if (channel->iob[index].state == BUF_STATE_EMPTY) {
610 channel->iob[index].state = BUF_STATE_LOCKED;
611 return channel->iob + index;
613 index = (index + 1) & (LCS_NUM_BUFFS - 1);
614 } while (index != channel->io_idx);
618 static struct lcs_buffer *
619 lcs_get_buffer(struct lcs_channel *channel)
621 struct lcs_buffer *buffer;
624 LCS_DBF_TEXT(5, trace, "getbuff");
625 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
626 buffer = __lcs_get_buffer(channel);
627 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
632 * Resume channel program if the channel is suspended.
635 __lcs_resume_channel(struct lcs_channel *channel)
639 if (channel->state != CH_STATE_SUSPENDED)
641 if (channel->ccws[channel->io_idx].flags & CCW_FLAG_SUSPEND)
643 LCS_DBF_TEXT_(5, trace, "rsch%s", channel->ccwdev->dev.bus_id);
644 rc = ccw_device_resume(channel->ccwdev);
646 LCS_DBF_TEXT_(4, trace, "ersc%s", channel->ccwdev->dev.bus_id);
647 PRINT_ERR("Error in lcs_resume_channel: rc=%d\n",rc);
649 channel->state = CH_STATE_RUNNING;
655 * Make a buffer ready for processing.
658 __lcs_ready_buffer_bits(struct lcs_channel *channel, int index)
662 LCS_DBF_TEXT(5, trace, "rdybits");
663 prev = (index - 1) & (LCS_NUM_BUFFS - 1);
664 next = (index + 1) & (LCS_NUM_BUFFS - 1);
665 /* Check if we may clear the suspend bit of this buffer. */
666 if (channel->ccws[next].flags & CCW_FLAG_SUSPEND) {
667 /* Check if we have to set the PCI bit. */
668 if (!(channel->ccws[prev].flags & CCW_FLAG_SUSPEND))
669 /* Suspend bit of the previous buffer is not set. */
670 channel->ccws[index].flags |= CCW_FLAG_PCI;
671 /* Suspend bit of the next buffer is set. */
672 channel->ccws[index].flags &= ~CCW_FLAG_SUSPEND;
677 lcs_ready_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
682 LCS_DBF_TEXT(5, trace, "rdybuff");
683 if (buffer->state != BUF_STATE_LOCKED &&
684 buffer->state != BUF_STATE_PROCESSED)
686 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
687 buffer->state = BUF_STATE_READY;
688 index = buffer - channel->iob;
690 channel->ccws[index].count = buffer->count;
691 /* Check relevant PCI/suspend bits. */
692 __lcs_ready_buffer_bits(channel, index);
693 rc = __lcs_resume_channel(channel);
694 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
699 * Mark the buffer as processed. Take care of the suspend bit
700 * of the previous buffer. This function is called from
701 * interrupt context, so the lock must not be taken.
704 __lcs_processed_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
706 int index, prev, next;
708 LCS_DBF_TEXT(5, trace, "prcsbuff");
709 if (buffer->state != BUF_STATE_READY)
711 buffer->state = BUF_STATE_PROCESSED;
712 index = buffer - channel->iob;
713 prev = (index - 1) & (LCS_NUM_BUFFS - 1);
714 next = (index + 1) & (LCS_NUM_BUFFS - 1);
715 /* Set the suspend bit and clear the PCI bit of this buffer. */
716 channel->ccws[index].flags |= CCW_FLAG_SUSPEND;
717 channel->ccws[index].flags &= ~CCW_FLAG_PCI;
718 /* Check the suspend bit of the previous buffer. */
719 if (channel->iob[prev].state == BUF_STATE_READY) {
721 * Previous buffer is in state ready. It might have
722 * happened in lcs_ready_buffer that the suspend bit
723 * has not been cleared to avoid an endless loop.
726 __lcs_ready_buffer_bits(channel, prev);
728 /* Clear PCI bit of next buffer. */
729 channel->ccws[next].flags &= ~CCW_FLAG_PCI;
730 return __lcs_resume_channel(channel);
734 * Put a processed buffer back to state empty.
737 lcs_release_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
741 LCS_DBF_TEXT(5, trace, "relbuff");
742 if (buffer->state != BUF_STATE_LOCKED &&
743 buffer->state != BUF_STATE_PROCESSED)
745 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
746 buffer->state = BUF_STATE_EMPTY;
747 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
751 * Get buffer for a lan command.
753 static struct lcs_buffer *
754 lcs_get_lancmd(struct lcs_card *card, int count)
756 struct lcs_buffer *buffer;
759 LCS_DBF_TEXT(4, trace, "getlncmd");
760 /* Get buffer and wait if none is available. */
761 wait_event(card->write.wait_q,
762 ((buffer = lcs_get_buffer(&card->write)) != NULL));
763 count += sizeof(struct lcs_header);
764 *(__u16 *)(buffer->data + count) = 0;
765 buffer->count = count + sizeof(__u16);
766 buffer->callback = lcs_release_buffer;
767 cmd = (struct lcs_cmd *) buffer->data;
769 cmd->type = LCS_FRAME_TYPE_CONTROL;
776 lcs_get_reply(struct lcs_reply *reply)
778 WARN_ON(atomic_read(&reply->refcnt) <= 0);
779 atomic_inc(&reply->refcnt);
783 lcs_put_reply(struct lcs_reply *reply)
785 WARN_ON(atomic_read(&reply->refcnt) <= 0);
786 if (atomic_dec_and_test(&reply->refcnt)) {
792 static struct lcs_reply *
793 lcs_alloc_reply(struct lcs_cmd *cmd)
795 struct lcs_reply *reply;
797 LCS_DBF_TEXT(4, trace, "getreply");
799 reply = kmalloc(sizeof(struct lcs_reply), GFP_ATOMIC);
802 memset(reply,0,sizeof(struct lcs_reply));
803 atomic_set(&reply->refcnt,1);
804 reply->sequence_no = cmd->sequence_no;
807 init_waitqueue_head(&reply->wait_q);
813 * Notifier function for lancmd replies. Called from read irq.
816 lcs_notify_lancmd_waiters(struct lcs_card *card, struct lcs_cmd *cmd)
818 struct list_head *l, *n;
819 struct lcs_reply *reply;
821 LCS_DBF_TEXT(4, trace, "notiwait");
822 spin_lock(&card->lock);
823 list_for_each_safe(l, n, &card->lancmd_waiters) {
824 reply = list_entry(l, struct lcs_reply, list);
825 if (reply->sequence_no == cmd->sequence_no) {
826 lcs_get_reply(reply);
827 list_del_init(&reply->list);
828 if (reply->callback != NULL)
829 reply->callback(card, cmd);
831 reply->rc = cmd->return_code;
832 wake_up(&reply->wait_q);
833 lcs_put_reply(reply);
837 spin_unlock(&card->lock);
841 * Emit buffer of a lan comand.
844 lcs_lancmd_timeout(unsigned long data)
846 struct lcs_reply *reply, *list_reply, *r;
849 LCS_DBF_TEXT(4, trace, "timeout");
850 reply = (struct lcs_reply *) data;
851 spin_lock_irqsave(&reply->card->lock, flags);
852 list_for_each_entry_safe(list_reply, r,
853 &reply->card->lancmd_waiters,list) {
854 if (reply == list_reply) {
855 lcs_get_reply(reply);
856 list_del_init(&reply->list);
857 spin_unlock_irqrestore(&reply->card->lock, flags);
860 wake_up(&reply->wait_q);
861 lcs_put_reply(reply);
865 spin_unlock_irqrestore(&reply->card->lock, flags);
869 lcs_send_lancmd(struct lcs_card *card, struct lcs_buffer *buffer,
870 void (*reply_callback)(struct lcs_card *, struct lcs_cmd *))
872 struct lcs_reply *reply;
874 struct timer_list timer;
878 LCS_DBF_TEXT(4, trace, "sendcmd");
879 cmd = (struct lcs_cmd *) buffer->data;
880 cmd->return_code = 0;
881 cmd->sequence_no = card->sequence_no++;
882 reply = lcs_alloc_reply(cmd);
885 reply->callback = reply_callback;
887 spin_lock_irqsave(&card->lock, flags);
888 list_add_tail(&reply->list, &card->lancmd_waiters);
889 spin_unlock_irqrestore(&card->lock, flags);
891 buffer->callback = lcs_release_buffer;
892 rc = lcs_ready_buffer(&card->write, buffer);
896 timer.function = lcs_lancmd_timeout;
897 timer.data = (unsigned long) reply;
898 timer.expires = jiffies + HZ*card->lancmd_timeout;
900 wait_event(reply->wait_q, reply->received);
901 del_timer_sync(&timer);
902 LCS_DBF_TEXT_(4, trace, "rc:%d",reply->rc);
904 lcs_put_reply(reply);
905 return rc ? -EIO : 0;
909 * LCS startup command
912 lcs_send_startup(struct lcs_card *card, __u8 initiator)
914 struct lcs_buffer *buffer;
917 LCS_DBF_TEXT(2, trace, "startup");
918 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
919 cmd = (struct lcs_cmd *) buffer->data;
920 cmd->cmd_code = LCS_CMD_STARTUP;
921 cmd->initiator = initiator;
922 cmd->cmd.lcs_startup.buff_size = LCS_IOBUFFERSIZE;
923 return lcs_send_lancmd(card, buffer, NULL);
927 * LCS shutdown command
930 lcs_send_shutdown(struct lcs_card *card)
932 struct lcs_buffer *buffer;
935 LCS_DBF_TEXT(2, trace, "shutdown");
936 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
937 cmd = (struct lcs_cmd *) buffer->data;
938 cmd->cmd_code = LCS_CMD_SHUTDOWN;
939 cmd->initiator = LCS_INITIATOR_TCPIP;
940 return lcs_send_lancmd(card, buffer, NULL);
944 * LCS lanstat command
947 __lcs_lanstat_cb(struct lcs_card *card, struct lcs_cmd *cmd)
949 LCS_DBF_TEXT(2, trace, "statcb");
950 memcpy(card->mac, cmd->cmd.lcs_lanstat_cmd.mac_addr, LCS_MAC_LENGTH);
954 lcs_send_lanstat(struct lcs_card *card)
956 struct lcs_buffer *buffer;
959 LCS_DBF_TEXT(2,trace, "cmdstat");
960 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
961 cmd = (struct lcs_cmd *) buffer->data;
962 /* Setup lanstat command. */
963 cmd->cmd_code = LCS_CMD_LANSTAT;
964 cmd->initiator = LCS_INITIATOR_TCPIP;
965 cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
966 cmd->cmd.lcs_std_cmd.portno = card->portno;
967 return lcs_send_lancmd(card, buffer, __lcs_lanstat_cb);
971 * send stoplan command
974 lcs_send_stoplan(struct lcs_card *card, __u8 initiator)
976 struct lcs_buffer *buffer;
979 LCS_DBF_TEXT(2, trace, "cmdstpln");
980 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
981 cmd = (struct lcs_cmd *) buffer->data;
982 cmd->cmd_code = LCS_CMD_STOPLAN;
983 cmd->initiator = initiator;
984 cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
985 cmd->cmd.lcs_std_cmd.portno = card->portno;
986 return lcs_send_lancmd(card, buffer, NULL);
990 * send startlan command
993 __lcs_send_startlan_cb(struct lcs_card *card, struct lcs_cmd *cmd)
995 LCS_DBF_TEXT(2, trace, "srtlancb");
996 card->lan_type = cmd->cmd.lcs_std_cmd.lan_type;
997 card->portno = cmd->cmd.lcs_std_cmd.portno;
1001 lcs_send_startlan(struct lcs_card *card, __u8 initiator)
1003 struct lcs_buffer *buffer;
1004 struct lcs_cmd *cmd;
1006 LCS_DBF_TEXT(2, trace, "cmdstaln");
1007 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1008 cmd = (struct lcs_cmd *) buffer->data;
1009 cmd->cmd_code = LCS_CMD_STARTLAN;
1010 cmd->initiator = initiator;
1011 cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
1012 cmd->cmd.lcs_std_cmd.portno = card->portno;
1013 return lcs_send_lancmd(card, buffer, __lcs_send_startlan_cb);
1016 #ifdef CONFIG_IP_MULTICAST
1018 * send setipm command (Multicast)
1021 lcs_send_setipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1023 struct lcs_buffer *buffer;
1024 struct lcs_cmd *cmd;
1026 LCS_DBF_TEXT(2, trace, "cmdsetim");
1027 buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1028 cmd = (struct lcs_cmd *) buffer->data;
1029 cmd->cmd_code = LCS_CMD_SETIPM;
1030 cmd->initiator = LCS_INITIATOR_TCPIP;
1031 cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1032 cmd->cmd.lcs_qipassist.portno = card->portno;
1033 cmd->cmd.lcs_qipassist.version = 4;
1034 cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1035 memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1036 &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1037 LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1038 return lcs_send_lancmd(card, buffer, NULL);
1042 * send delipm command (Multicast)
1045 lcs_send_delipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1047 struct lcs_buffer *buffer;
1048 struct lcs_cmd *cmd;
1050 LCS_DBF_TEXT(2, trace, "cmddelim");
1051 buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1052 cmd = (struct lcs_cmd *) buffer->data;
1053 cmd->cmd_code = LCS_CMD_DELIPM;
1054 cmd->initiator = LCS_INITIATOR_TCPIP;
1055 cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1056 cmd->cmd.lcs_qipassist.portno = card->portno;
1057 cmd->cmd.lcs_qipassist.version = 4;
1058 cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1059 memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1060 &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1061 LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1062 return lcs_send_lancmd(card, buffer, NULL);
1066 * check if multicast is supported by LCS
1069 __lcs_check_multicast_cb(struct lcs_card *card, struct lcs_cmd *cmd)
1071 LCS_DBF_TEXT(2, trace, "chkmccb");
1072 card->ip_assists_supported =
1073 cmd->cmd.lcs_qipassist.ip_assists_supported;
1074 card->ip_assists_enabled =
1075 cmd->cmd.lcs_qipassist.ip_assists_enabled;
1079 lcs_check_multicast_support(struct lcs_card *card)
1081 struct lcs_buffer *buffer;
1082 struct lcs_cmd *cmd;
1085 LCS_DBF_TEXT(2, trace, "cmdqipa");
1086 /* Send query ipassist. */
1087 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1088 cmd = (struct lcs_cmd *) buffer->data;
1089 cmd->cmd_code = LCS_CMD_QIPASSIST;
1090 cmd->initiator = LCS_INITIATOR_TCPIP;
1091 cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1092 cmd->cmd.lcs_qipassist.portno = card->portno;
1093 cmd->cmd.lcs_qipassist.version = 4;
1094 cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1095 rc = lcs_send_lancmd(card, buffer, __lcs_check_multicast_cb);
1097 PRINT_ERR("Query IPAssist failed. Assuming unsupported!\n");
1100 if (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT)
1106 * set or del multicast address on LCS card
1109 lcs_fix_multicast_list(struct lcs_card *card)
1111 struct list_head failed_list;
1112 struct lcs_ipm_list *ipm, *tmp;
1113 unsigned long flags;
1116 LCS_DBF_TEXT(4,trace, "fixipm");
1117 INIT_LIST_HEAD(&failed_list);
1118 spin_lock_irqsave(&card->ipm_lock, flags);
1120 list_for_each_entry_safe(ipm, tmp, &card->ipm_list, list){
1121 switch (ipm->ipm_state) {
1122 case LCS_IPM_STATE_SET_REQUIRED:
1123 /* del from ipm_list so noone else can tamper with
1125 list_del_init(&ipm->list);
1126 spin_unlock_irqrestore(&card->ipm_lock, flags);
1127 rc = lcs_send_setipm(card, ipm);
1128 spin_lock_irqsave(&card->ipm_lock, flags);
1130 PRINT_INFO("Adding multicast address failed."
1131 "Table possibly full!\n");
1132 /* store ipm in failed list -> will be added
1133 * to ipm_list again, so a retry will be done
1134 * during the next call of this function */
1135 list_add_tail(&ipm->list, &failed_list);
1137 ipm->ipm_state = LCS_IPM_STATE_ON_CARD;
1138 /* re-insert into ipm_list */
1139 list_add_tail(&ipm->list, &card->ipm_list);
1142 case LCS_IPM_STATE_DEL_REQUIRED:
1143 list_del(&ipm->list);
1144 spin_unlock_irqrestore(&card->ipm_lock, flags);
1145 lcs_send_delipm(card, ipm);
1146 spin_lock_irqsave(&card->ipm_lock, flags);
1149 case LCS_IPM_STATE_ON_CARD:
1153 /* re-insert all entries from the failed_list into ipm_list */
1154 list_for_each_entry_safe(ipm, tmp, &failed_list, list) {
1155 list_del_init(&ipm->list);
1156 list_add_tail(&ipm->list, &card->ipm_list);
1158 spin_unlock_irqrestore(&card->ipm_lock, flags);
1159 if (card->state == DEV_STATE_UP)
1160 netif_wake_queue(card->dev);
1164 * get mac address for the relevant Multicast address
1167 lcs_get_mac_for_ipm(__u32 ipm, char *mac, struct net_device *dev)
1169 LCS_DBF_TEXT(4,trace, "getmac");
1170 if (dev->type == ARPHRD_IEEE802_TR)
1171 ip_tr_mc_map(ipm, mac);
1173 ip_eth_mc_map(ipm, mac);
1177 * function called by net device to handle multicast address relevant things
1180 lcs_remove_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1182 struct ip_mc_list *im4;
1183 struct list_head *l;
1184 struct lcs_ipm_list *ipm;
1185 unsigned long flags;
1186 char buf[MAX_ADDR_LEN];
1188 LCS_DBF_TEXT(4, trace, "remmclst");
1189 spin_lock_irqsave(&card->ipm_lock, flags);
1190 list_for_each(l, &card->ipm_list) {
1191 ipm = list_entry(l, struct lcs_ipm_list, list);
1192 for (im4 = in4_dev->mc_list; im4 != NULL; im4 = im4->next) {
1193 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1194 if ( (ipm->ipm.ip_addr == im4->multiaddr) &&
1195 (memcmp(buf, &ipm->ipm.mac_addr,
1196 LCS_MAC_LENGTH) == 0) )
1200 ipm->ipm_state = LCS_IPM_STATE_DEL_REQUIRED;
1202 spin_unlock_irqrestore(&card->ipm_lock, flags);
1205 static inline struct lcs_ipm_list *
1206 lcs_check_addr_entry(struct lcs_card *card, struct ip_mc_list *im4, char *buf)
1208 struct lcs_ipm_list *tmp, *ipm = NULL;
1209 struct list_head *l;
1210 unsigned long flags;
1212 LCS_DBF_TEXT(4, trace, "chkmcent");
1213 spin_lock_irqsave(&card->ipm_lock, flags);
1214 list_for_each(l, &card->ipm_list) {
1215 tmp = list_entry(l, struct lcs_ipm_list, list);
1216 if ( (tmp->ipm.ip_addr == im4->multiaddr) &&
1217 (memcmp(buf, &tmp->ipm.mac_addr,
1218 LCS_MAC_LENGTH) == 0) ) {
1223 spin_unlock_irqrestore(&card->ipm_lock, flags);
1228 lcs_set_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1231 struct ip_mc_list *im4;
1232 struct lcs_ipm_list *ipm;
1233 char buf[MAX_ADDR_LEN];
1234 unsigned long flags;
1236 LCS_DBF_TEXT(4, trace, "setmclst");
1237 for (im4 = in4_dev->mc_list; im4; im4 = im4->next) {
1238 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1239 ipm = lcs_check_addr_entry(card, im4, buf);
1241 continue; /* Address already in list. */
1242 ipm = (struct lcs_ipm_list *)
1243 kmalloc(sizeof(struct lcs_ipm_list), GFP_ATOMIC);
1245 PRINT_INFO("Not enough memory to add "
1246 "new multicast entry!\n");
1249 memset(ipm, 0, sizeof(struct lcs_ipm_list));
1250 memcpy(&ipm->ipm.mac_addr, buf, LCS_MAC_LENGTH);
1251 ipm->ipm.ip_addr = im4->multiaddr;
1252 ipm->ipm_state = LCS_IPM_STATE_SET_REQUIRED;
1253 spin_lock_irqsave(&card->ipm_lock, flags);
1254 list_add(&ipm->list, &card->ipm_list);
1255 spin_unlock_irqrestore(&card->ipm_lock, flags);
1260 lcs_register_mc_addresses(void *data)
1262 struct lcs_card *card;
1263 struct in_device *in4_dev;
1265 card = (struct lcs_card *) data;
1266 daemonize("regipm");
1268 if (!lcs_do_run_thread(card, LCS_SET_MC_THREAD))
1270 LCS_DBF_TEXT(4, trace, "regmulti");
1272 in4_dev = in_dev_get(card->dev);
1273 if (in4_dev == NULL)
1275 read_lock(&in4_dev->mc_list_lock);
1276 lcs_remove_mc_addresses(card,in4_dev);
1277 lcs_set_mc_addresses(card, in4_dev);
1278 read_unlock(&in4_dev->mc_list_lock);
1279 in_dev_put(in4_dev);
1281 lcs_fix_multicast_list(card);
1283 lcs_clear_thread_running_bit(card, LCS_SET_MC_THREAD);
1287 * function called by net device to
1288 * handle multicast address relevant things
1291 lcs_set_multicast_list(struct net_device *dev)
1293 struct lcs_card *card;
1295 LCS_DBF_TEXT(4, trace, "setmulti");
1296 card = (struct lcs_card *) dev->priv;
1298 if (!lcs_set_thread_start_bit(card, LCS_SET_MC_THREAD)) {
1299 schedule_work(&card->kernel_thread_starter);
1303 #endif /* CONFIG_IP_MULTICAST */
1306 lcs_check_irb_error(struct ccw_device *cdev, struct irb *irb)
1311 switch (PTR_ERR(irb)) {
1313 PRINT_WARN("i/o-error on device %s\n", cdev->dev.bus_id);
1314 LCS_DBF_TEXT(2, trace, "ckirberr");
1315 LCS_DBF_TEXT_(2, trace, " rc%d", -EIO);
1318 PRINT_WARN("timeout on device %s\n", cdev->dev.bus_id);
1319 LCS_DBF_TEXT(2, trace, "ckirberr");
1320 LCS_DBF_TEXT_(2, trace, " rc%d", -ETIMEDOUT);
1323 PRINT_WARN("unknown error %ld on device %s\n", PTR_ERR(irb),
1325 LCS_DBF_TEXT(2, trace, "ckirberr");
1326 LCS_DBF_TEXT(2, trace, " rc???");
1328 return PTR_ERR(irb);
1333 * IRQ Handler for LCS channels
1336 lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1338 struct lcs_card *card;
1339 struct lcs_channel *channel;
1342 if (lcs_check_irb_error(cdev, irb))
1345 card = CARD_FROM_DEV(cdev);
1346 if (card->read.ccwdev == cdev)
1347 channel = &card->read;
1349 channel = &card->write;
1351 LCS_DBF_TEXT_(5, trace, "Rint%s",cdev->dev.bus_id);
1352 LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.cstat, irb->scsw.dstat);
1353 LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.fctl, irb->scsw.actl);
1355 /* How far in the ccw chain have we processed? */
1356 if ((channel->state != CH_STATE_INIT) &&
1357 (irb->scsw.fctl & SCSW_FCTL_START_FUNC)) {
1358 index = (struct ccw1 *) __va((addr_t) irb->scsw.cpa)
1360 if ((irb->scsw.actl & SCSW_ACTL_SUSPENDED) ||
1361 (irb->scsw.cstat | SCHN_STAT_PCI))
1362 /* Bloody io subsystem tells us lies about cpa... */
1363 index = (index - 1) & (LCS_NUM_BUFFS - 1);
1364 while (channel->io_idx != index) {
1365 __lcs_processed_buffer(channel,
1366 channel->iob + channel->io_idx);
1368 (channel->io_idx + 1) & (LCS_NUM_BUFFS - 1);
1372 if ((irb->scsw.dstat & DEV_STAT_DEV_END) ||
1373 (irb->scsw.dstat & DEV_STAT_CHN_END) ||
1374 (irb->scsw.dstat & DEV_STAT_UNIT_CHECK))
1375 /* Mark channel as stopped. */
1376 channel->state = CH_STATE_STOPPED;
1377 else if (irb->scsw.actl & SCSW_ACTL_SUSPENDED)
1378 /* CCW execution stopped on a suspend bit. */
1379 channel->state = CH_STATE_SUSPENDED;
1381 if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1382 if (irb->scsw.cc != 0) {
1383 ccw_device_halt(channel->ccwdev, (addr_t) channel);
1386 /* The channel has been stopped by halt_IO. */
1387 channel->state = CH_STATE_HALTED;
1390 if (irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
1391 channel->state = CH_STATE_CLEARED;
1393 /* Do the rest in the tasklet. */
1394 tasklet_schedule(&channel->irq_tasklet);
1398 * Tasklet for IRQ handler
1401 lcs_tasklet(unsigned long data)
1403 unsigned long flags;
1404 struct lcs_channel *channel;
1405 struct lcs_buffer *iob;
1409 channel = (struct lcs_channel *) data;
1410 LCS_DBF_TEXT_(5, trace, "tlet%s",channel->ccwdev->dev.bus_id);
1412 /* Check for processed buffers. */
1414 buf_idx = channel->buf_idx;
1415 while (iob[buf_idx].state == BUF_STATE_PROCESSED) {
1416 /* Do the callback thing. */
1417 if (iob[buf_idx].callback != NULL)
1418 iob[buf_idx].callback(channel, iob + buf_idx);
1419 buf_idx = (buf_idx + 1) & (LCS_NUM_BUFFS - 1);
1421 channel->buf_idx = buf_idx;
1423 if (channel->state == CH_STATE_STOPPED)
1424 // FIXME: what if rc != 0 ??
1425 rc = lcs_start_channel(channel);
1426 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1427 if (channel->state == CH_STATE_SUSPENDED &&
1428 channel->iob[channel->io_idx].state == BUF_STATE_READY) {
1429 // FIXME: what if rc != 0 ??
1430 rc = __lcs_resume_channel(channel);
1432 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1434 /* Something happened on the channel. Wake up waiters. */
1435 wake_up(&channel->wait_q);
1439 * Finish current tx buffer and make it ready for transmit.
1442 __lcs_emit_txbuffer(struct lcs_card *card)
1444 LCS_DBF_TEXT(5, trace, "emittx");
1445 *(__u16 *)(card->tx_buffer->data + card->tx_buffer->count) = 0;
1446 card->tx_buffer->count += 2;
1447 lcs_ready_buffer(&card->write, card->tx_buffer);
1448 card->tx_buffer = NULL;
1453 * Callback for finished tx buffers.
1456 lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1458 struct lcs_card *card;
1460 LCS_DBF_TEXT(5, trace, "txbuffcb");
1461 /* Put buffer back to pool. */
1462 lcs_release_buffer(channel, buffer);
1463 card = (struct lcs_card *)
1464 ((char *) channel - offsetof(struct lcs_card, write));
1465 spin_lock(&card->lock);
1467 if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1469 * Last running tx buffer has finished. Submit partially
1470 * filled current buffer.
1472 __lcs_emit_txbuffer(card);
1473 spin_unlock(&card->lock);
1477 * Packet transmit function called by network stack
1480 __lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
1481 struct net_device *dev)
1483 struct lcs_header *header;
1485 LCS_DBF_TEXT(5, trace, "hardxmit");
1487 card->stats.tx_dropped++;
1488 card->stats.tx_errors++;
1491 if (card->state != DEV_STATE_UP) {
1493 card->stats.tx_dropped++;
1494 card->stats.tx_errors++;
1495 card->stats.tx_carrier_errors++;
1498 if (netif_queue_stopped(dev) ) {
1499 card->stats.tx_dropped++;
1502 if (card->tx_buffer != NULL &&
1503 card->tx_buffer->count + sizeof(struct lcs_header) +
1504 skb->len + sizeof(u16) > LCS_IOBUFFERSIZE)
1505 /* skb too big for current tx buffer. */
1506 __lcs_emit_txbuffer(card);
1507 if (card->tx_buffer == NULL) {
1508 /* Get new tx buffer */
1509 card->tx_buffer = lcs_get_buffer(&card->write);
1510 if (card->tx_buffer == NULL) {
1511 card->stats.tx_dropped++;
1514 card->tx_buffer->callback = lcs_txbuffer_cb;
1515 card->tx_buffer->count = 0;
1517 header = (struct lcs_header *)
1518 (card->tx_buffer->data + card->tx_buffer->count);
1519 card->tx_buffer->count += skb->len + sizeof(struct lcs_header);
1520 header->offset = card->tx_buffer->count;
1521 header->type = card->lan_type;
1522 header->slot = card->portno;
1523 memcpy(header + 1, skb->data, skb->len);
1524 card->stats.tx_bytes += skb->len;
1525 card->stats.tx_packets++;
1527 if (card->tx_emitted <= 0)
1528 /* If this is the first tx buffer emit it immediately. */
1529 __lcs_emit_txbuffer(card);
1534 lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
1536 struct lcs_card *card;
1539 LCS_DBF_TEXT(5, trace, "pktxmit");
1540 card = (struct lcs_card *) dev->priv;
1541 spin_lock(&card->lock);
1542 rc = __lcs_start_xmit(card, skb, dev);
1543 spin_unlock(&card->lock);
1548 * send startlan and lanstat command to make LCS device ready
1551 lcs_startlan_auto(struct lcs_card *card)
1555 LCS_DBF_TEXT(2, trace, "strtauto");
1556 #ifdef CONFIG_NET_ETHERNET
1557 card->lan_type = LCS_FRAME_TYPE_ENET;
1558 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1564 card->lan_type = LCS_FRAME_TYPE_TR;
1565 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1570 card->lan_type = LCS_FRAME_TYPE_FDDI;
1571 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1579 lcs_startlan(struct lcs_card *card)
1583 LCS_DBF_TEXT(2, trace, "startlan");
1585 if (card->portno != LCS_INVALID_PORT_NO) {
1586 if (card->lan_type == LCS_FRAME_TYPE_AUTO)
1587 rc = lcs_startlan_auto(card);
1589 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1591 for (i = 0; i <= 16; i++) {
1593 if (card->lan_type != LCS_FRAME_TYPE_AUTO)
1594 rc = lcs_send_startlan(card,
1595 LCS_INITIATOR_TCPIP);
1597 /* autodetecting lan type */
1598 rc = lcs_startlan_auto(card);
1604 return lcs_send_lanstat(card);
1609 * LCS detect function
1610 * setup channels and make them I/O ready
1613 lcs_detect(struct lcs_card *card)
1617 LCS_DBF_TEXT(2, setup, "lcsdetct");
1618 /* start/reset card */
1620 netif_stop_queue(card->dev);
1621 rc = lcs_stop_channels(card);
1623 rc = lcs_start_channels(card);
1625 rc = lcs_send_startup(card, LCS_INITIATOR_TCPIP);
1627 rc = lcs_startlan(card);
1631 card->state = DEV_STATE_UP;
1633 card->state = DEV_STATE_DOWN;
1634 card->write.state = CH_STATE_INIT;
1635 card->read.state = CH_STATE_INIT;
1644 lcs_resetcard(struct lcs_card *card)
1648 LCS_DBF_TEXT(2, trace, "rescard");
1649 for (retries = 0; retries < 10; retries++) {
1650 if (lcs_detect(card) == 0) {
1651 netif_wake_queue(card->dev);
1652 card->state = DEV_STATE_UP;
1653 PRINT_INFO("LCS device %s successfully restarted!\n",
1659 PRINT_ERR("Error in Reseting LCS card!\n");
1668 lcs_stopcard(struct lcs_card *card)
1672 LCS_DBF_TEXT(3, setup, "stopcard");
1674 if (card->read.state != CH_STATE_STOPPED &&
1675 card->write.state != CH_STATE_STOPPED &&
1676 card->state == DEV_STATE_UP) {
1677 lcs_clear_multicast_list(card);
1678 rc = lcs_send_stoplan(card,LCS_INITIATOR_TCPIP);
1679 rc = lcs_send_shutdown(card);
1681 rc = lcs_stop_channels(card);
1682 card->state = DEV_STATE_DOWN;
1688 * LGW initiated commands
1691 lcs_lgw_startlan_thread(void *data)
1693 struct lcs_card *card;
1695 card = (struct lcs_card *) data;
1696 daemonize("lgwstpln");
1698 if (!lcs_do_run_thread(card, LCS_STARTLAN_THREAD))
1700 LCS_DBF_TEXT(4, trace, "lgwstpln");
1702 netif_stop_queue(card->dev);
1703 if (lcs_startlan(card) == 0) {
1704 netif_wake_queue(card->dev);
1705 card->state = DEV_STATE_UP;
1706 PRINT_INFO("LCS Startlan for device %s succeeded!\n",
1710 PRINT_ERR("LCS Startlan for device %s failed!\n",
1712 lcs_clear_thread_running_bit(card, LCS_STARTLAN_THREAD);
1717 * Send startup command initiated by Lan Gateway
1720 lcs_lgw_startup_thread(void *data)
1724 struct lcs_card *card;
1726 card = (struct lcs_card *) data;
1727 daemonize("lgwstaln");
1729 if (!lcs_do_run_thread(card, LCS_STARTUP_THREAD))
1731 LCS_DBF_TEXT(4, trace, "lgwstaln");
1733 netif_stop_queue(card->dev);
1734 rc = lcs_send_startup(card, LCS_INITIATOR_LGW);
1736 PRINT_ERR("Startup for LCS device %s initiated " \
1737 "by LGW failed!\nReseting card ...\n",
1739 /* do a card reset */
1740 rc = lcs_resetcard(card);
1744 rc = lcs_startlan(card);
1746 netif_wake_queue(card->dev);
1747 card->state = DEV_STATE_UP;
1751 PRINT_INFO("LCS Startup for device %s succeeded!\n",
1754 PRINT_ERR("LCS Startup for device %s failed!\n",
1756 lcs_clear_thread_running_bit(card, LCS_STARTUP_THREAD);
1762 * send stoplan command initiated by Lan Gateway
1765 lcs_lgw_stoplan_thread(void *data)
1767 struct lcs_card *card;
1770 card = (struct lcs_card *) data;
1771 daemonize("lgwstop");
1773 if (!lcs_do_run_thread(card, LCS_STOPLAN_THREAD))
1775 LCS_DBF_TEXT(4, trace, "lgwstop");
1777 netif_stop_queue(card->dev);
1778 if (lcs_send_stoplan(card, LCS_INITIATOR_LGW) == 0)
1779 PRINT_INFO("Stoplan for %s initiated by LGW succeeded!\n",
1782 PRINT_ERR("Stoplan %s initiated by LGW failed!\n",
1784 /*Try to reset the card, stop it on failure */
1785 rc = lcs_resetcard(card);
1787 rc = lcs_stopcard(card);
1788 lcs_clear_thread_running_bit(card, LCS_STOPLAN_THREAD);
1793 * Kernel Thread helper functions for LGW initiated commands
1796 lcs_start_kernel_thread(struct lcs_card *card)
1798 LCS_DBF_TEXT(5, trace, "krnthrd");
1799 if (lcs_do_start_thread(card, LCS_STARTUP_THREAD))
1800 kernel_thread(lcs_lgw_startup_thread, (void *) card, SIGCHLD);
1801 if (lcs_do_start_thread(card, LCS_STARTLAN_THREAD))
1802 kernel_thread(lcs_lgw_startlan_thread, (void *) card, SIGCHLD);
1803 if (lcs_do_start_thread(card, LCS_STOPLAN_THREAD))
1804 kernel_thread(lcs_lgw_stoplan_thread, (void *) card, SIGCHLD);
1805 #ifdef CONFIG_IP_MULTICAST
1806 if (lcs_do_start_thread(card, LCS_SET_MC_THREAD))
1807 kernel_thread(lcs_register_mc_addresses, (void *) card, SIGCHLD);
1812 * Process control frames.
1815 lcs_get_control(struct lcs_card *card, struct lcs_cmd *cmd)
1817 LCS_DBF_TEXT(5, trace, "getctrl");
1818 if (cmd->initiator == LCS_INITIATOR_LGW) {
1819 switch(cmd->cmd_code) {
1820 case LCS_CMD_STARTUP:
1821 if (!lcs_set_thread_start_bit(card,
1822 LCS_STARTUP_THREAD))
1823 schedule_work(&card->kernel_thread_starter);
1825 case LCS_CMD_STARTLAN:
1826 if (!lcs_set_thread_start_bit(card,
1827 LCS_STARTLAN_THREAD))
1828 schedule_work(&card->kernel_thread_starter);
1830 case LCS_CMD_STOPLAN:
1831 if (!lcs_set_thread_start_bit(card,
1832 LCS_STOPLAN_THREAD))
1833 schedule_work(&card->kernel_thread_starter);
1836 PRINT_INFO("UNRECOGNIZED LGW COMMAND\n");
1840 lcs_notify_lancmd_waiters(card, cmd);
1844 * Unpack network packet.
1847 lcs_get_skb(struct lcs_card *card, char *skb_data, unsigned int skb_len)
1849 struct sk_buff *skb;
1851 LCS_DBF_TEXT(5, trace, "getskb");
1852 if (card->dev == NULL ||
1853 card->state != DEV_STATE_UP)
1854 /* The card isn't up. Ignore the packet. */
1857 skb = dev_alloc_skb(skb_len);
1859 PRINT_ERR("LCS: alloc_skb failed for device=%s\n",
1861 card->stats.rx_dropped++;
1864 skb->dev = card->dev;
1865 memcpy(skb_put(skb, skb_len), skb_data, skb_len);
1866 skb->protocol = card->lan_type_trans(skb, card->dev);
1867 card->stats.rx_bytes += skb_len;
1868 card->stats.rx_packets++;
1869 *((__u32 *)skb->cb) = ++card->pkt_seq;
1874 * LCS main routine to get packets and lancmd replies from the buffers
1877 lcs_get_frames_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1879 struct lcs_card *card;
1880 struct lcs_header *lcs_hdr;
1883 LCS_DBF_TEXT(5, trace, "lcsgtpkt");
1884 lcs_hdr = (struct lcs_header *) buffer->data;
1885 if (lcs_hdr->offset == LCS_ILLEGAL_OFFSET) {
1886 LCS_DBF_TEXT(4, trace, "-eiogpkt");
1889 card = (struct lcs_card *)
1890 ((char *) channel - offsetof(struct lcs_card, read));
1892 while (lcs_hdr->offset != 0) {
1893 if (lcs_hdr->offset <= 0 ||
1894 lcs_hdr->offset > LCS_IOBUFFERSIZE ||
1895 lcs_hdr->offset < offset) {
1896 /* Offset invalid. */
1897 card->stats.rx_length_errors++;
1898 card->stats.rx_errors++;
1901 /* What kind of frame is it? */
1902 if (lcs_hdr->type == LCS_FRAME_TYPE_CONTROL)
1903 /* Control frame. */
1904 lcs_get_control(card, (struct lcs_cmd *) lcs_hdr);
1905 else if (lcs_hdr->type == LCS_FRAME_TYPE_ENET ||
1906 lcs_hdr->type == LCS_FRAME_TYPE_TR ||
1907 lcs_hdr->type == LCS_FRAME_TYPE_FDDI)
1908 /* Normal network packet. */
1909 lcs_get_skb(card, (char *)(lcs_hdr + 1),
1910 lcs_hdr->offset - offset -
1911 sizeof(struct lcs_header));
1913 /* Unknown frame type. */
1914 ; // FIXME: error message ?
1915 /* Proceed to next frame. */
1916 offset = lcs_hdr->offset;
1917 lcs_hdr->offset = LCS_ILLEGAL_OFFSET;
1918 lcs_hdr = (struct lcs_header *) (buffer->data + offset);
1920 /* The buffer is now empty. Make it ready again. */
1921 lcs_ready_buffer(&card->read, buffer);
1925 * get network statistics for ifconfig and other user programs
1927 static struct net_device_stats *
1928 lcs_getstats(struct net_device *dev)
1930 struct lcs_card *card;
1932 LCS_DBF_TEXT(4, trace, "netstats");
1933 card = (struct lcs_card *) dev->priv;
1934 return &card->stats;
1939 * This function will be called by user doing ifconfig xxx down
1942 lcs_stop_device(struct net_device *dev)
1944 struct lcs_card *card;
1947 LCS_DBF_TEXT(2, trace, "stopdev");
1948 card = (struct lcs_card *) dev->priv;
1949 netif_stop_queue(dev);
1950 dev->flags &= ~IFF_UP;
1951 rc = lcs_stopcard(card);
1953 PRINT_ERR("Try it again!\n ");
1958 * start lcs device and make it runnable
1959 * This function will be called by user doing ifconfig xxx up
1962 lcs_open_device(struct net_device *dev)
1964 struct lcs_card *card;
1967 LCS_DBF_TEXT(2, trace, "opendev");
1968 card = (struct lcs_card *) dev->priv;
1969 /* initialize statistics */
1970 rc = lcs_detect(card);
1972 PRINT_ERR("LCS:Error in opening device!\n");
1975 dev->flags |= IFF_UP;
1976 netif_wake_queue(dev);
1977 card->state = DEV_STATE_UP;
1983 * show function for portno called by cat or similar things
1986 lcs_portno_show (struct device *dev, struct device_attribute *attr, char *buf)
1988 struct lcs_card *card;
1990 card = (struct lcs_card *)dev->driver_data;
1995 return sprintf(buf, "%d\n", card->portno);
1999 * store the value which is piped to file portno
2002 lcs_portno_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2004 struct lcs_card *card;
2007 card = (struct lcs_card *)dev->driver_data;
2012 sscanf(buf, "%u", &value);
2013 /* TODO: sanity checks */
2014 card->portno = value;
2020 static DEVICE_ATTR(portno, 0644, lcs_portno_show, lcs_portno_store);
2023 lcs_type_show(struct device *dev, struct device_attribute *attr, char *buf)
2025 struct ccwgroup_device *cgdev;
2027 cgdev = to_ccwgroupdev(dev);
2031 return sprintf(buf, "%s\n", cu3088_type[cgdev->cdev[0]->id.driver_info]);
2034 static DEVICE_ATTR(type, 0444, lcs_type_show, NULL);
2037 lcs_timeout_show(struct device *dev, struct device_attribute *attr, char *buf)
2039 struct lcs_card *card;
2041 card = (struct lcs_card *)dev->driver_data;
2043 return card ? sprintf(buf, "%u\n", card->lancmd_timeout) : 0;
2047 lcs_timeout_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2049 struct lcs_card *card;
2052 card = (struct lcs_card *)dev->driver_data;
2057 sscanf(buf, "%u", &value);
2058 /* TODO: sanity checks */
2059 card->lancmd_timeout = value;
2065 DEVICE_ATTR(lancmd_timeout, 0644, lcs_timeout_show, lcs_timeout_store);
2067 static struct attribute * lcs_attrs[] = {
2068 &dev_attr_portno.attr,
2069 &dev_attr_type.attr,
2070 &dev_attr_lancmd_timeout.attr,
2074 static struct attribute_group lcs_attr_group = {
2079 * lcs_probe_device is called on establishing a new ccwgroup_device.
2082 lcs_probe_device(struct ccwgroup_device *ccwgdev)
2084 struct lcs_card *card;
2087 if (!get_device(&ccwgdev->dev))
2090 LCS_DBF_TEXT(2, setup, "add_dev");
2091 card = lcs_alloc_card();
2093 PRINT_ERR("Allocation of lcs card failed\n");
2094 put_device(&ccwgdev->dev);
2097 ret = sysfs_create_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2099 PRINT_ERR("Creating attributes failed");
2100 lcs_free_card(card);
2101 put_device(&ccwgdev->dev);
2104 ccwgdev->dev.driver_data = card;
2105 ccwgdev->cdev[0]->handler = lcs_irq;
2106 ccwgdev->cdev[1]->handler = lcs_irq;
2111 lcs_register_netdev(struct ccwgroup_device *ccwgdev)
2113 struct lcs_card *card;
2115 LCS_DBF_TEXT(2, setup, "regnetdv");
2116 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2117 if (card->dev->reg_state != NETREG_UNINITIALIZED)
2119 SET_NETDEV_DEV(card->dev, &ccwgdev->dev);
2120 return register_netdev(card->dev);
2124 * lcs_new_device will be called by setting the group device online.
2128 lcs_new_device(struct ccwgroup_device *ccwgdev)
2130 struct lcs_card *card;
2131 struct net_device *dev=NULL;
2132 enum lcs_dev_states recover_state;
2135 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2139 LCS_DBF_TEXT(2, setup, "newdev");
2140 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2141 card->read.ccwdev = ccwgdev->cdev[0];
2142 card->write.ccwdev = ccwgdev->cdev[1];
2144 recover_state = card->state;
2145 ccw_device_set_online(card->read.ccwdev);
2146 ccw_device_set_online(card->write.ccwdev);
2148 LCS_DBF_TEXT(3, setup, "lcsnewdv");
2150 lcs_setup_card(card);
2151 rc = lcs_detect(card);
2153 LCS_DBF_TEXT(2, setup, "dtctfail");
2154 PRINT_WARN("Detection of LCS card failed with return code "
2155 "%d (0x%x)\n", rc, rc);
2160 LCS_DBF_TEXT(2, setup, "samedev");
2161 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2164 switch (card->lan_type) {
2165 #ifdef CONFIG_NET_ETHERNET
2166 case LCS_FRAME_TYPE_ENET:
2167 card->lan_type_trans = eth_type_trans;
2168 dev = alloc_etherdev(0);
2172 case LCS_FRAME_TYPE_TR:
2173 card->lan_type_trans = tr_type_trans;
2174 dev = alloc_trdev(0);
2178 case LCS_FRAME_TYPE_FDDI:
2179 card->lan_type_trans = fddi_type_trans;
2180 dev = alloc_fddidev(0);
2184 LCS_DBF_TEXT(3, setup, "errinit");
2185 PRINT_ERR("LCS: Initialization failed\n");
2186 PRINT_ERR("LCS: No device found!\n");
2192 card->dev->priv = card;
2193 card->dev->open = lcs_open_device;
2194 card->dev->stop = lcs_stop_device;
2195 card->dev->hard_start_xmit = lcs_start_xmit;
2196 card->dev->get_stats = lcs_getstats;
2197 SET_MODULE_OWNER(dev);
2198 memcpy(card->dev->dev_addr, card->mac, LCS_MAC_LENGTH);
2199 #ifdef CONFIG_IP_MULTICAST
2200 if (!lcs_check_multicast_support(card))
2201 card->dev->set_multicast_list = lcs_set_multicast_list;
2204 lcs_set_allowed_threads(card,0xffffffff);
2205 if (recover_state == DEV_STATE_RECOVER) {
2206 lcs_set_multicast_list(card->dev);
2207 card->dev->flags |= IFF_UP;
2208 netif_wake_queue(card->dev);
2209 card->state = DEV_STATE_UP;
2214 if (lcs_register_netdev(ccwgdev) != 0)
2217 /* Print out supported assists: IPv6 */
2218 PRINT_INFO("LCS device %s %s IPv6 support\n", card->dev->name,
2219 (card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
2220 "with" : "without");
2221 /* Print out supported assist: Multicast */
2222 PRINT_INFO("LCS device %s %s Multicast support\n", card->dev->name,
2223 (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
2224 "with" : "without");
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");