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 (fpavlic@de.ibm.com) and
12 * Martin Schwidefsky <schwidefsky@de.ibm.com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <linux/module.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/trdevice.h>
34 #include <linux/fddidevice.h>
35 #include <linux/inetdevice.h>
37 #include <linux/igmp.h>
38 #include <linux/delay.h>
42 #include <asm/debug.h>
43 #include <asm/idals.h>
44 #include <asm/timex.h>
45 #include <linux/device.h>
46 #include <asm/ccwgroup.h>
52 #if !defined(CONFIG_NET_ETHERNET) && \
53 !defined(CONFIG_TR) && !defined(CONFIG_FDDI)
54 #error Cannot compile lcs.c without some net devices switched on.
58 * initialization string for output
61 static char version[] __initdata = "LCS driver";
62 static char debug_buffer[255];
67 static void lcs_tasklet(unsigned long);
68 static void lcs_start_kernel_thread(struct lcs_card *card);
69 static void lcs_get_frames_cb(struct lcs_channel *, struct lcs_buffer *);
70 static int lcs_send_delipm(struct lcs_card *, struct lcs_ipm_list *);
73 * Debug Facility Stuff
75 static debug_info_t *lcs_dbf_setup;
76 static debug_info_t *lcs_dbf_trace;
79 * LCS Debug Facility functions
82 lcs_unregister_debug_facility(void)
85 debug_unregister(lcs_dbf_setup);
87 debug_unregister(lcs_dbf_trace);
91 lcs_register_debug_facility(void)
93 lcs_dbf_setup = debug_register("lcs_setup", 2, 1, 8);
94 lcs_dbf_trace = debug_register("lcs_trace", 2, 2, 8);
95 if (lcs_dbf_setup == NULL || lcs_dbf_trace == NULL) {
96 PRINT_ERR("Not enough memory for debug facility.\n");
97 lcs_unregister_debug_facility();
100 debug_register_view(lcs_dbf_setup, &debug_hex_ascii_view);
101 debug_set_level(lcs_dbf_setup, 4);
102 debug_register_view(lcs_dbf_trace, &debug_hex_ascii_view);
103 debug_set_level(lcs_dbf_trace, 4);
108 * Allocate io buffers.
111 lcs_alloc_channel(struct lcs_channel *channel)
115 LCS_DBF_TEXT(2, setup, "ichalloc");
116 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
117 /* alloc memory fo iobuffer */
118 channel->iob[cnt].data = (void *)
119 kmalloc(LCS_IOBUFFERSIZE, GFP_DMA | GFP_KERNEL);
120 if (channel->iob[cnt].data == NULL)
122 memset(channel->iob[cnt].data, 0, LCS_IOBUFFERSIZE);
123 channel->iob[cnt].state = BUF_STATE_EMPTY;
125 if (cnt < LCS_NUM_BUFFS) {
126 /* Not all io buffers could be allocated. */
127 LCS_DBF_TEXT(2, setup, "echalloc");
129 kfree(channel->iob[cnt].data);
139 lcs_free_channel(struct lcs_channel *channel)
143 LCS_DBF_TEXT(2, setup, "ichfree");
144 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
145 kfree(channel->iob[cnt].data);
146 channel->iob[cnt].data = NULL;
154 lcs_cleanup_channel(struct lcs_channel *channel)
156 LCS_DBF_TEXT(3, setup, "cleanch");
157 /* Kill write channel tasklets. */
158 tasklet_kill(&channel->irq_tasklet);
159 /* Free channel buffers. */
160 lcs_free_channel(channel);
164 * LCS free memory for card and channels.
167 lcs_free_card(struct lcs_card *card)
169 LCS_DBF_TEXT(2, setup, "remcard");
170 LCS_DBF_HEX(2, setup, &card, sizeof(void*));
175 * LCS alloc memory for card and channels
177 static struct lcs_card *
180 struct lcs_card *card;
183 LCS_DBF_TEXT(2, setup, "alloclcs");
185 card = kmalloc(sizeof(struct lcs_card), GFP_KERNEL | GFP_DMA);
188 memset(card, 0, sizeof(struct lcs_card));
189 card->lan_type = LCS_FRAME_TYPE_AUTO;
191 card->lancmd_timeout = LCS_LANCMD_TIMEOUT_DEFAULT;
192 /* Allocate io buffers for the read channel. */
193 rc = lcs_alloc_channel(&card->read);
195 LCS_DBF_TEXT(2, setup, "iccwerr");
199 /* Allocate io buffers for the write channel. */
200 rc = lcs_alloc_channel(&card->write);
202 LCS_DBF_TEXT(2, setup, "iccwerr");
203 lcs_cleanup_channel(&card->read);
208 #ifdef CONFIG_IP_MULTICAST
209 INIT_LIST_HEAD(&card->ipm_list);
211 LCS_DBF_HEX(2, setup, &card, sizeof(void*));
216 * Setup read channel.
219 lcs_setup_read_ccws(struct lcs_card *card)
223 LCS_DBF_TEXT(2, setup, "ireadccw");
224 /* Setup read ccws. */
225 memset(card->read.ccws, 0, sizeof (struct ccw1) * (LCS_NUM_BUFFS + 1));
226 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
227 card->read.ccws[cnt].cmd_code = LCS_CCW_READ;
228 card->read.ccws[cnt].count = LCS_IOBUFFERSIZE;
229 card->read.ccws[cnt].flags =
230 CCW_FLAG_CC | CCW_FLAG_SLI | CCW_FLAG_PCI;
232 * Note: we have allocated the buffer with GFP_DMA, so
233 * we do not need to do set_normalized_cda.
235 card->read.ccws[cnt].cda =
236 (__u32) __pa(card->read.iob[cnt].data);
237 ((struct lcs_header *)
238 card->read.iob[cnt].data)->offset = LCS_ILLEGAL_OFFSET;
239 card->read.iob[cnt].callback = lcs_get_frames_cb;
240 card->read.iob[cnt].state = BUF_STATE_READY;
241 card->read.iob[cnt].count = LCS_IOBUFFERSIZE;
243 card->read.ccws[0].flags &= ~CCW_FLAG_PCI;
244 card->read.ccws[LCS_NUM_BUFFS - 1].flags &= ~CCW_FLAG_PCI;
245 card->read.ccws[LCS_NUM_BUFFS - 1].flags |= CCW_FLAG_SUSPEND;
246 /* Last ccw is a tic (transfer in channel). */
247 card->read.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
248 card->read.ccws[LCS_NUM_BUFFS].cda =
249 (__u32) __pa(card->read.ccws);
250 /* Setg initial state of the read channel. */
251 card->read.state = CH_STATE_INIT;
253 card->read.io_idx = 0;
254 card->read.buf_idx = 0;
258 lcs_setup_read(struct lcs_card *card)
260 LCS_DBF_TEXT(3, setup, "initread");
262 lcs_setup_read_ccws(card);
263 /* Initialize read channel tasklet. */
264 card->read.irq_tasklet.data = (unsigned long) &card->read;
265 card->read.irq_tasklet.func = lcs_tasklet;
266 /* Initialize waitqueue. */
267 init_waitqueue_head(&card->read.wait_q);
271 * Setup write channel.
274 lcs_setup_write_ccws(struct lcs_card *card)
278 LCS_DBF_TEXT(3, setup, "iwritccw");
279 /* Setup write ccws. */
280 memset(card->write.ccws, 0, sizeof(struct ccw1) * LCS_NUM_BUFFS + 1);
281 for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
282 card->write.ccws[cnt].cmd_code = LCS_CCW_WRITE;
283 card->write.ccws[cnt].count = 0;
284 card->write.ccws[cnt].flags =
285 CCW_FLAG_SUSPEND | CCW_FLAG_CC | CCW_FLAG_SLI;
287 * Note: we have allocated the buffer with GFP_DMA, so
288 * we do not need to do set_normalized_cda.
290 card->write.ccws[cnt].cda =
291 (__u32) __pa(card->write.iob[cnt].data);
293 /* Last ccw is a tic (transfer in channel). */
294 card->write.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
295 card->write.ccws[LCS_NUM_BUFFS].cda =
296 (__u32) __pa(card->write.ccws);
297 /* Set initial state of the write channel. */
298 card->read.state = CH_STATE_INIT;
300 card->write.io_idx = 0;
301 card->write.buf_idx = 0;
305 lcs_setup_write(struct lcs_card *card)
307 LCS_DBF_TEXT(3, setup, "initwrit");
309 lcs_setup_write_ccws(card);
310 /* Initialize write channel tasklet. */
311 card->write.irq_tasklet.data = (unsigned long) &card->write;
312 card->write.irq_tasklet.func = lcs_tasklet;
313 /* Initialize waitqueue. */
314 init_waitqueue_head(&card->write.wait_q);
318 lcs_set_allowed_threads(struct lcs_card *card, unsigned long threads)
322 spin_lock_irqsave(&card->mask_lock, flags);
323 card->thread_allowed_mask = threads;
324 spin_unlock_irqrestore(&card->mask_lock, flags);
325 wake_up(&card->wait_q);
328 lcs_threads_running(struct lcs_card *card, unsigned long threads)
333 spin_lock_irqsave(&card->mask_lock, flags);
334 rc = (card->thread_running_mask & threads);
335 spin_unlock_irqrestore(&card->mask_lock, flags);
340 lcs_wait_for_threads(struct lcs_card *card, unsigned long threads)
342 return wait_event_interruptible(card->wait_q,
343 lcs_threads_running(card, threads) == 0);
347 lcs_set_thread_start_bit(struct lcs_card *card, unsigned long thread)
351 spin_lock_irqsave(&card->mask_lock, flags);
352 if ( !(card->thread_allowed_mask & thread) ||
353 (card->thread_start_mask & thread) ) {
354 spin_unlock_irqrestore(&card->mask_lock, flags);
357 card->thread_start_mask |= thread;
358 spin_unlock_irqrestore(&card->mask_lock, flags);
363 lcs_clear_thread_running_bit(struct lcs_card *card, unsigned long thread)
367 spin_lock_irqsave(&card->mask_lock, flags);
368 card->thread_running_mask &= ~thread;
369 spin_unlock_irqrestore(&card->mask_lock, flags);
370 wake_up(&card->wait_q);
374 __lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
379 spin_lock_irqsave(&card->mask_lock, flags);
380 if (card->thread_start_mask & thread){
381 if ((card->thread_allowed_mask & thread) &&
382 !(card->thread_running_mask & thread)){
384 card->thread_start_mask &= ~thread;
385 card->thread_running_mask |= thread;
389 spin_unlock_irqrestore(&card->mask_lock, flags);
394 lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
397 wait_event(card->wait_q,
398 (rc = __lcs_do_run_thread(card, thread)) >= 0);
403 lcs_do_start_thread(struct lcs_card *card, unsigned long thread)
408 spin_lock_irqsave(&card->mask_lock, flags);
409 LCS_DBF_TEXT_(4, trace, " %02x%02x%02x",
410 (u8) card->thread_start_mask,
411 (u8) card->thread_allowed_mask,
412 (u8) card->thread_running_mask);
413 rc = (card->thread_start_mask & thread);
414 spin_unlock_irqrestore(&card->mask_lock, flags);
419 * Initialize channels,card and state machines.
422 lcs_setup_card(struct lcs_card *card)
424 LCS_DBF_TEXT(2, setup, "initcard");
425 LCS_DBF_HEX(2, setup, &card, sizeof(void*));
427 lcs_setup_read(card);
428 lcs_setup_write(card);
429 /* Set cards initial state. */
430 card->state = DEV_STATE_DOWN;
431 card->tx_buffer = NULL;
432 card->tx_emitted = 0;
434 /* Initialize kernel thread task used for LGW commands. */
435 INIT_WORK(&card->kernel_thread_starter,
436 (void *)lcs_start_kernel_thread,card);
437 card->thread_start_mask = 0;
438 card->thread_allowed_mask = 0;
439 card->thread_running_mask = 0;
440 init_waitqueue_head(&card->wait_q);
441 spin_lock_init(&card->lock);
442 spin_lock_init(&card->ipm_lock);
443 spin_lock_init(&card->mask_lock);
444 #ifdef CONFIG_IP_MULTICAST
445 INIT_LIST_HEAD(&card->ipm_list);
447 INIT_LIST_HEAD(&card->lancmd_waiters);
451 lcs_clear_multicast_list(struct lcs_card *card)
453 #ifdef CONFIG_IP_MULTICAST
454 struct lcs_ipm_list *ipm;
457 /* Free multicast list. */
458 LCS_DBF_TEXT(3, setup, "clmclist");
459 spin_lock_irqsave(&card->ipm_lock, flags);
460 while (!list_empty(&card->ipm_list)){
461 ipm = list_entry(card->ipm_list.next,
462 struct lcs_ipm_list, list);
463 list_del(&ipm->list);
464 if (ipm->ipm_state != LCS_IPM_STATE_SET_REQUIRED){
465 spin_unlock_irqrestore(&card->ipm_lock, flags);
466 lcs_send_delipm(card, ipm);
467 spin_lock_irqsave(&card->ipm_lock, flags);
471 spin_unlock_irqrestore(&card->ipm_lock, flags);
475 * Cleanup channels,card and state machines.
478 lcs_cleanup_card(struct lcs_card *card)
481 LCS_DBF_TEXT(3, setup, "cleancrd");
482 LCS_DBF_HEX(2,setup,&card,sizeof(void*));
484 if (card->dev != NULL)
485 free_netdev(card->dev);
486 /* Cleanup channels. */
487 lcs_cleanup_channel(&card->write);
488 lcs_cleanup_channel(&card->read);
495 lcs_start_channel(struct lcs_channel *channel)
500 LCS_DBF_TEXT_(4,trace,"ssch%s", channel->ccwdev->dev.bus_id);
501 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
502 rc = ccw_device_start(channel->ccwdev,
503 channel->ccws + channel->io_idx, 0, 0,
504 DOIO_DENY_PREFETCH | DOIO_ALLOW_SUSPEND);
506 channel->state = CH_STATE_RUNNING;
507 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
509 LCS_DBF_TEXT_(4,trace,"essh%s", channel->ccwdev->dev.bus_id);
510 PRINT_ERR("Error in starting channel, rc=%d!\n", rc);
516 lcs_clear_channel(struct lcs_channel *channel)
521 LCS_DBF_TEXT(4,trace,"clearch");
522 LCS_DBF_TEXT_(4,trace,"%s", channel->ccwdev->dev.bus_id);
523 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
524 rc = ccw_device_clear(channel->ccwdev, (addr_t) channel);
525 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
527 LCS_DBF_TEXT_(4,trace,"ecsc%s", channel->ccwdev->dev.bus_id);
530 wait_event(channel->wait_q, (channel->state == CH_STATE_CLEARED));
531 channel->state = CH_STATE_STOPPED;
540 lcs_stop_channel(struct lcs_channel *channel)
545 if (channel->state == CH_STATE_STOPPED)
547 LCS_DBF_TEXT(4,trace,"haltsch");
548 LCS_DBF_TEXT_(4,trace,"%s", channel->ccwdev->dev.bus_id);
549 channel->state = CH_STATE_INIT;
550 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
551 rc = ccw_device_halt(channel->ccwdev, (addr_t) channel);
552 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
554 LCS_DBF_TEXT_(4,trace,"ehsc%s", channel->ccwdev->dev.bus_id);
557 /* Asynchronous halt initialted. Wait for its completion. */
558 wait_event(channel->wait_q, (channel->state == CH_STATE_HALTED));
559 lcs_clear_channel(channel);
564 * start read and write channel
567 lcs_start_channels(struct lcs_card *card)
571 LCS_DBF_TEXT(2, trace, "chstart");
572 /* start read channel */
573 rc = lcs_start_channel(&card->read);
576 /* start write channel */
577 rc = lcs_start_channel(&card->write);
579 lcs_stop_channel(&card->read);
584 * stop read and write channel
587 lcs_stop_channels(struct lcs_card *card)
589 LCS_DBF_TEXT(2, trace, "chhalt");
590 lcs_stop_channel(&card->read);
591 lcs_stop_channel(&card->write);
598 static struct lcs_buffer *
599 __lcs_get_buffer(struct lcs_channel *channel)
603 LCS_DBF_TEXT(5, trace, "_getbuff");
604 index = channel->io_idx;
606 if (channel->iob[index].state == BUF_STATE_EMPTY) {
607 channel->iob[index].state = BUF_STATE_LOCKED;
608 return channel->iob + index;
610 index = (index + 1) & (LCS_NUM_BUFFS - 1);
611 } while (index != channel->io_idx);
615 static struct lcs_buffer *
616 lcs_get_buffer(struct lcs_channel *channel)
618 struct lcs_buffer *buffer;
621 LCS_DBF_TEXT(5, trace, "getbuff");
622 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
623 buffer = __lcs_get_buffer(channel);
624 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
629 * Resume channel program if the channel is suspended.
632 __lcs_resume_channel(struct lcs_channel *channel)
636 if (channel->state != CH_STATE_SUSPENDED)
638 if (channel->ccws[channel->io_idx].flags & CCW_FLAG_SUSPEND)
640 LCS_DBF_TEXT_(5, trace, "rsch%s", channel->ccwdev->dev.bus_id);
641 rc = ccw_device_resume(channel->ccwdev);
643 LCS_DBF_TEXT_(4, trace, "ersc%s", channel->ccwdev->dev.bus_id);
644 PRINT_ERR("Error in lcs_resume_channel: rc=%d\n",rc);
646 channel->state = CH_STATE_RUNNING;
652 * Make a buffer ready for processing.
655 __lcs_ready_buffer_bits(struct lcs_channel *channel, int index)
659 LCS_DBF_TEXT(5, trace, "rdybits");
660 prev = (index - 1) & (LCS_NUM_BUFFS - 1);
661 next = (index + 1) & (LCS_NUM_BUFFS - 1);
662 /* Check if we may clear the suspend bit of this buffer. */
663 if (channel->ccws[next].flags & CCW_FLAG_SUSPEND) {
664 /* Check if we have to set the PCI bit. */
665 if (!(channel->ccws[prev].flags & CCW_FLAG_SUSPEND))
666 /* Suspend bit of the previous buffer is not set. */
667 channel->ccws[index].flags |= CCW_FLAG_PCI;
668 /* Suspend bit of the next buffer is set. */
669 channel->ccws[index].flags &= ~CCW_FLAG_SUSPEND;
674 lcs_ready_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
679 LCS_DBF_TEXT(5, trace, "rdybuff");
680 if (buffer->state != BUF_STATE_LOCKED &&
681 buffer->state != BUF_STATE_PROCESSED)
683 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
684 buffer->state = BUF_STATE_READY;
685 index = buffer - channel->iob;
687 channel->ccws[index].count = buffer->count;
688 /* Check relevant PCI/suspend bits. */
689 __lcs_ready_buffer_bits(channel, index);
690 rc = __lcs_resume_channel(channel);
691 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
696 * Mark the buffer as processed. Take care of the suspend bit
697 * of the previous buffer. This function is called from
698 * interrupt context, so the lock must not be taken.
701 __lcs_processed_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
703 int index, prev, next;
705 LCS_DBF_TEXT(5, trace, "prcsbuff");
706 if (buffer->state != BUF_STATE_READY)
708 buffer->state = BUF_STATE_PROCESSED;
709 index = buffer - channel->iob;
710 prev = (index - 1) & (LCS_NUM_BUFFS - 1);
711 next = (index + 1) & (LCS_NUM_BUFFS - 1);
712 /* Set the suspend bit and clear the PCI bit of this buffer. */
713 channel->ccws[index].flags |= CCW_FLAG_SUSPEND;
714 channel->ccws[index].flags &= ~CCW_FLAG_PCI;
715 /* Check the suspend bit of the previous buffer. */
716 if (channel->iob[prev].state == BUF_STATE_READY) {
718 * Previous buffer is in state ready. It might have
719 * happened in lcs_ready_buffer that the suspend bit
720 * has not been cleared to avoid an endless loop.
723 __lcs_ready_buffer_bits(channel, prev);
725 /* Clear PCI bit of next buffer. */
726 channel->ccws[next].flags &= ~CCW_FLAG_PCI;
727 return __lcs_resume_channel(channel);
731 * Put a processed buffer back to state empty.
734 lcs_release_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
738 LCS_DBF_TEXT(5, trace, "relbuff");
739 if (buffer->state != BUF_STATE_LOCKED &&
740 buffer->state != BUF_STATE_PROCESSED)
742 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
743 buffer->state = BUF_STATE_EMPTY;
744 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
748 * Get buffer for a lan command.
750 static struct lcs_buffer *
751 lcs_get_lancmd(struct lcs_card *card, int count)
753 struct lcs_buffer *buffer;
756 LCS_DBF_TEXT(4, trace, "getlncmd");
757 /* Get buffer and wait if none is available. */
758 wait_event(card->write.wait_q,
759 ((buffer = lcs_get_buffer(&card->write)) != NULL));
760 count += sizeof(struct lcs_header);
761 *(__u16 *)(buffer->data + count) = 0;
762 buffer->count = count + sizeof(__u16);
763 buffer->callback = lcs_release_buffer;
764 cmd = (struct lcs_cmd *) buffer->data;
766 cmd->type = LCS_FRAME_TYPE_CONTROL;
773 lcs_get_reply(struct lcs_reply *reply)
775 WARN_ON(atomic_read(&reply->refcnt) <= 0);
776 atomic_inc(&reply->refcnt);
780 lcs_put_reply(struct lcs_reply *reply)
782 WARN_ON(atomic_read(&reply->refcnt) <= 0);
783 if (atomic_dec_and_test(&reply->refcnt)) {
789 static struct lcs_reply *
790 lcs_alloc_reply(struct lcs_cmd *cmd)
792 struct lcs_reply *reply;
794 LCS_DBF_TEXT(4, trace, "getreply");
796 reply = kmalloc(sizeof(struct lcs_reply), GFP_ATOMIC);
799 memset(reply,0,sizeof(struct lcs_reply));
800 atomic_set(&reply->refcnt,1);
801 reply->sequence_no = cmd->sequence_no;
804 init_waitqueue_head(&reply->wait_q);
810 * Notifier function for lancmd replies. Called from read irq.
813 lcs_notify_lancmd_waiters(struct lcs_card *card, struct lcs_cmd *cmd)
815 struct list_head *l, *n;
816 struct lcs_reply *reply;
818 LCS_DBF_TEXT(4, trace, "notiwait");
819 spin_lock(&card->lock);
820 list_for_each_safe(l, n, &card->lancmd_waiters) {
821 reply = list_entry(l, struct lcs_reply, list);
822 if (reply->sequence_no == cmd->sequence_no) {
823 lcs_get_reply(reply);
824 list_del_init(&reply->list);
825 if (reply->callback != NULL)
826 reply->callback(card, cmd);
828 reply->rc = cmd->return_code;
829 wake_up(&reply->wait_q);
830 lcs_put_reply(reply);
834 spin_unlock(&card->lock);
838 * Emit buffer of a lan comand.
841 lcs_lancmd_timeout(unsigned long data)
843 struct lcs_reply *reply, *list_reply, *r;
846 LCS_DBF_TEXT(4, trace, "timeout");
847 reply = (struct lcs_reply *) data;
848 spin_lock_irqsave(&reply->card->lock, flags);
849 list_for_each_entry_safe(list_reply, r,
850 &reply->card->lancmd_waiters,list) {
851 if (reply == list_reply) {
852 lcs_get_reply(reply);
853 list_del_init(&reply->list);
854 spin_unlock_irqrestore(&reply->card->lock, flags);
857 wake_up(&reply->wait_q);
858 lcs_put_reply(reply);
862 spin_unlock_irqrestore(&reply->card->lock, flags);
866 lcs_send_lancmd(struct lcs_card *card, struct lcs_buffer *buffer,
867 void (*reply_callback)(struct lcs_card *, struct lcs_cmd *))
869 struct lcs_reply *reply;
871 struct timer_list timer;
875 LCS_DBF_TEXT(4, trace, "sendcmd");
876 cmd = (struct lcs_cmd *) buffer->data;
877 cmd->return_code = 0;
878 cmd->sequence_no = card->sequence_no++;
879 reply = lcs_alloc_reply(cmd);
882 reply->callback = reply_callback;
884 spin_lock_irqsave(&card->lock, flags);
885 list_add_tail(&reply->list, &card->lancmd_waiters);
886 spin_unlock_irqrestore(&card->lock, flags);
888 buffer->callback = lcs_release_buffer;
889 rc = lcs_ready_buffer(&card->write, buffer);
893 timer.function = lcs_lancmd_timeout;
894 timer.data = (unsigned long) reply;
895 timer.expires = jiffies + HZ*card->lancmd_timeout;
897 wait_event(reply->wait_q, reply->received);
898 del_timer_sync(&timer);
899 LCS_DBF_TEXT_(4, trace, "rc:%d",reply->rc);
901 lcs_put_reply(reply);
902 return rc ? -EIO : 0;
906 * LCS startup command
909 lcs_send_startup(struct lcs_card *card, __u8 initiator)
911 struct lcs_buffer *buffer;
914 LCS_DBF_TEXT(2, trace, "startup");
915 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
916 cmd = (struct lcs_cmd *) buffer->data;
917 cmd->cmd_code = LCS_CMD_STARTUP;
918 cmd->initiator = initiator;
919 cmd->cmd.lcs_startup.buff_size = LCS_IOBUFFERSIZE;
920 return lcs_send_lancmd(card, buffer, NULL);
924 * LCS shutdown command
927 lcs_send_shutdown(struct lcs_card *card)
929 struct lcs_buffer *buffer;
932 LCS_DBF_TEXT(2, trace, "shutdown");
933 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
934 cmd = (struct lcs_cmd *) buffer->data;
935 cmd->cmd_code = LCS_CMD_SHUTDOWN;
936 cmd->initiator = LCS_INITIATOR_TCPIP;
937 return lcs_send_lancmd(card, buffer, NULL);
941 * LCS lanstat command
944 __lcs_lanstat_cb(struct lcs_card *card, struct lcs_cmd *cmd)
946 LCS_DBF_TEXT(2, trace, "statcb");
947 memcpy(card->mac, cmd->cmd.lcs_lanstat_cmd.mac_addr, LCS_MAC_LENGTH);
951 lcs_send_lanstat(struct lcs_card *card)
953 struct lcs_buffer *buffer;
956 LCS_DBF_TEXT(2,trace, "cmdstat");
957 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
958 cmd = (struct lcs_cmd *) buffer->data;
959 /* Setup lanstat command. */
960 cmd->cmd_code = LCS_CMD_LANSTAT;
961 cmd->initiator = LCS_INITIATOR_TCPIP;
962 cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
963 cmd->cmd.lcs_std_cmd.portno = card->portno;
964 return lcs_send_lancmd(card, buffer, __lcs_lanstat_cb);
968 * send stoplan command
971 lcs_send_stoplan(struct lcs_card *card, __u8 initiator)
973 struct lcs_buffer *buffer;
976 LCS_DBF_TEXT(2, trace, "cmdstpln");
977 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
978 cmd = (struct lcs_cmd *) buffer->data;
979 cmd->cmd_code = LCS_CMD_STOPLAN;
980 cmd->initiator = initiator;
981 cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
982 cmd->cmd.lcs_std_cmd.portno = card->portno;
983 return lcs_send_lancmd(card, buffer, NULL);
987 * send startlan command
990 __lcs_send_startlan_cb(struct lcs_card *card, struct lcs_cmd *cmd)
992 LCS_DBF_TEXT(2, trace, "srtlancb");
993 card->lan_type = cmd->cmd.lcs_std_cmd.lan_type;
994 card->portno = cmd->cmd.lcs_std_cmd.portno;
998 lcs_send_startlan(struct lcs_card *card, __u8 initiator)
1000 struct lcs_buffer *buffer;
1001 struct lcs_cmd *cmd;
1003 LCS_DBF_TEXT(2, trace, "cmdstaln");
1004 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1005 cmd = (struct lcs_cmd *) buffer->data;
1006 cmd->cmd_code = LCS_CMD_STARTLAN;
1007 cmd->initiator = initiator;
1008 cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
1009 cmd->cmd.lcs_std_cmd.portno = card->portno;
1010 return lcs_send_lancmd(card, buffer, __lcs_send_startlan_cb);
1013 #ifdef CONFIG_IP_MULTICAST
1015 * send setipm command (Multicast)
1018 lcs_send_setipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1020 struct lcs_buffer *buffer;
1021 struct lcs_cmd *cmd;
1023 LCS_DBF_TEXT(2, trace, "cmdsetim");
1024 buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1025 cmd = (struct lcs_cmd *) buffer->data;
1026 cmd->cmd_code = LCS_CMD_SETIPM;
1027 cmd->initiator = LCS_INITIATOR_TCPIP;
1028 cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1029 cmd->cmd.lcs_qipassist.portno = card->portno;
1030 cmd->cmd.lcs_qipassist.version = 4;
1031 cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1032 memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1033 &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1034 LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1035 return lcs_send_lancmd(card, buffer, NULL);
1039 * send delipm command (Multicast)
1042 lcs_send_delipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
1044 struct lcs_buffer *buffer;
1045 struct lcs_cmd *cmd;
1047 LCS_DBF_TEXT(2, trace, "cmddelim");
1048 buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
1049 cmd = (struct lcs_cmd *) buffer->data;
1050 cmd->cmd_code = LCS_CMD_DELIPM;
1051 cmd->initiator = LCS_INITIATOR_TCPIP;
1052 cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1053 cmd->cmd.lcs_qipassist.portno = card->portno;
1054 cmd->cmd.lcs_qipassist.version = 4;
1055 cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1056 memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
1057 &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
1058 LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
1059 return lcs_send_lancmd(card, buffer, NULL);
1063 * check if multicast is supported by LCS
1066 __lcs_check_multicast_cb(struct lcs_card *card, struct lcs_cmd *cmd)
1068 LCS_DBF_TEXT(2, trace, "chkmccb");
1069 card->ip_assists_supported =
1070 cmd->cmd.lcs_qipassist.ip_assists_supported;
1071 card->ip_assists_enabled =
1072 cmd->cmd.lcs_qipassist.ip_assists_enabled;
1076 lcs_check_multicast_support(struct lcs_card *card)
1078 struct lcs_buffer *buffer;
1079 struct lcs_cmd *cmd;
1082 LCS_DBF_TEXT(2, trace, "cmdqipa");
1083 /* Send query ipassist. */
1084 buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
1085 cmd = (struct lcs_cmd *) buffer->data;
1086 cmd->cmd_code = LCS_CMD_QIPASSIST;
1087 cmd->initiator = LCS_INITIATOR_TCPIP;
1088 cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
1089 cmd->cmd.lcs_qipassist.portno = card->portno;
1090 cmd->cmd.lcs_qipassist.version = 4;
1091 cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
1092 rc = lcs_send_lancmd(card, buffer, __lcs_check_multicast_cb);
1094 PRINT_ERR("Query IPAssist failed. Assuming unsupported!\n");
1097 if (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT)
1103 * set or del multicast address on LCS card
1106 lcs_fix_multicast_list(struct lcs_card *card)
1108 struct list_head failed_list;
1109 struct lcs_ipm_list *ipm, *tmp;
1110 unsigned long flags;
1113 LCS_DBF_TEXT(4,trace, "fixipm");
1114 INIT_LIST_HEAD(&failed_list);
1115 spin_lock_irqsave(&card->ipm_lock, flags);
1117 list_for_each_entry_safe(ipm, tmp, &card->ipm_list, list){
1118 switch (ipm->ipm_state) {
1119 case LCS_IPM_STATE_SET_REQUIRED:
1120 /* del from ipm_list so noone else can tamper with
1122 list_del_init(&ipm->list);
1123 spin_unlock_irqrestore(&card->ipm_lock, flags);
1124 rc = lcs_send_setipm(card, ipm);
1125 spin_lock_irqsave(&card->ipm_lock, flags);
1127 PRINT_INFO("Adding multicast address failed."
1128 "Table possibly full!\n");
1129 /* store ipm in failed list -> will be added
1130 * to ipm_list again, so a retry will be done
1131 * during the next call of this function */
1132 list_add_tail(&ipm->list, &failed_list);
1134 ipm->ipm_state = LCS_IPM_STATE_ON_CARD;
1135 /* re-insert into ipm_list */
1136 list_add_tail(&ipm->list, &card->ipm_list);
1139 case LCS_IPM_STATE_DEL_REQUIRED:
1140 list_del(&ipm->list);
1141 spin_unlock_irqrestore(&card->ipm_lock, flags);
1142 lcs_send_delipm(card, ipm);
1143 spin_lock_irqsave(&card->ipm_lock, flags);
1146 case LCS_IPM_STATE_ON_CARD:
1150 /* re-insert all entries from the failed_list into ipm_list */
1151 list_for_each_entry_safe(ipm, tmp, &failed_list, list) {
1152 list_del_init(&ipm->list);
1153 list_add_tail(&ipm->list, &card->ipm_list);
1155 spin_unlock_irqrestore(&card->ipm_lock, flags);
1156 if (card->state == DEV_STATE_UP)
1157 netif_wake_queue(card->dev);
1161 * get mac address for the relevant Multicast address
1164 lcs_get_mac_for_ipm(__u32 ipm, char *mac, struct net_device *dev)
1166 LCS_DBF_TEXT(4,trace, "getmac");
1167 if (dev->type == ARPHRD_IEEE802_TR)
1168 ip_tr_mc_map(ipm, mac);
1170 ip_eth_mc_map(ipm, mac);
1174 * function called by net device to handle multicast address relevant things
1177 lcs_remove_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1179 struct ip_mc_list *im4;
1180 struct list_head *l;
1181 struct lcs_ipm_list *ipm;
1182 unsigned long flags;
1183 char buf[MAX_ADDR_LEN];
1185 LCS_DBF_TEXT(4, trace, "remmclst");
1186 spin_lock_irqsave(&card->ipm_lock, flags);
1187 list_for_each(l, &card->ipm_list) {
1188 ipm = list_entry(l, struct lcs_ipm_list, list);
1189 for (im4 = in4_dev->mc_list; im4 != NULL; im4 = im4->next) {
1190 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1191 if ( (ipm->ipm.ip_addr == im4->multiaddr) &&
1192 (memcmp(buf, &ipm->ipm.mac_addr,
1193 LCS_MAC_LENGTH) == 0) )
1197 ipm->ipm_state = LCS_IPM_STATE_DEL_REQUIRED;
1199 spin_unlock_irqrestore(&card->ipm_lock, flags);
1202 static inline struct lcs_ipm_list *
1203 lcs_check_addr_entry(struct lcs_card *card, struct ip_mc_list *im4, char *buf)
1205 struct lcs_ipm_list *tmp, *ipm = NULL;
1206 struct list_head *l;
1207 unsigned long flags;
1209 LCS_DBF_TEXT(4, trace, "chkmcent");
1210 spin_lock_irqsave(&card->ipm_lock, flags);
1211 list_for_each(l, &card->ipm_list) {
1212 tmp = list_entry(l, struct lcs_ipm_list, list);
1213 if ( (tmp->ipm.ip_addr == im4->multiaddr) &&
1214 (memcmp(buf, &tmp->ipm.mac_addr,
1215 LCS_MAC_LENGTH) == 0) ) {
1220 spin_unlock_irqrestore(&card->ipm_lock, flags);
1225 lcs_set_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
1228 struct ip_mc_list *im4;
1229 struct lcs_ipm_list *ipm;
1230 char buf[MAX_ADDR_LEN];
1231 unsigned long flags;
1233 LCS_DBF_TEXT(4, trace, "setmclst");
1234 for (im4 = in4_dev->mc_list; im4; im4 = im4->next) {
1235 lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
1236 ipm = lcs_check_addr_entry(card, im4, buf);
1238 continue; /* Address already in list. */
1239 ipm = (struct lcs_ipm_list *)
1240 kmalloc(sizeof(struct lcs_ipm_list), GFP_ATOMIC);
1242 PRINT_INFO("Not enough memory to add "
1243 "new multicast entry!\n");
1246 memset(ipm, 0, sizeof(struct lcs_ipm_list));
1247 memcpy(&ipm->ipm.mac_addr, buf, LCS_MAC_LENGTH);
1248 ipm->ipm.ip_addr = im4->multiaddr;
1249 ipm->ipm_state = LCS_IPM_STATE_SET_REQUIRED;
1250 spin_lock_irqsave(&card->ipm_lock, flags);
1251 list_add(&ipm->list, &card->ipm_list);
1252 spin_unlock_irqrestore(&card->ipm_lock, flags);
1257 lcs_register_mc_addresses(void *data)
1259 struct lcs_card *card;
1260 struct in_device *in4_dev;
1262 card = (struct lcs_card *) data;
1263 daemonize("regipm");
1265 if (!lcs_do_run_thread(card, LCS_SET_MC_THREAD))
1267 LCS_DBF_TEXT(4, trace, "regmulti");
1269 in4_dev = in_dev_get(card->dev);
1270 if (in4_dev == NULL)
1272 read_lock(&in4_dev->mc_list_lock);
1273 lcs_remove_mc_addresses(card,in4_dev);
1274 lcs_set_mc_addresses(card, in4_dev);
1275 read_unlock(&in4_dev->mc_list_lock);
1276 in_dev_put(in4_dev);
1278 lcs_fix_multicast_list(card);
1280 lcs_clear_thread_running_bit(card, LCS_SET_MC_THREAD);
1284 * function called by net device to
1285 * handle multicast address relevant things
1288 lcs_set_multicast_list(struct net_device *dev)
1290 struct lcs_card *card;
1292 LCS_DBF_TEXT(4, trace, "setmulti");
1293 card = (struct lcs_card *) dev->priv;
1295 if (!lcs_set_thread_start_bit(card, LCS_SET_MC_THREAD)) {
1296 schedule_work(&card->kernel_thread_starter);
1300 #endif /* CONFIG_IP_MULTICAST */
1303 lcs_check_irb_error(struct ccw_device *cdev, struct irb *irb)
1308 switch (PTR_ERR(irb)) {
1310 PRINT_WARN("i/o-error on device %s\n", cdev->dev.bus_id);
1311 LCS_DBF_TEXT(2, trace, "ckirberr");
1312 LCS_DBF_TEXT_(2, trace, " rc%d", -EIO);
1315 PRINT_WARN("timeout on device %s\n", cdev->dev.bus_id);
1316 LCS_DBF_TEXT(2, trace, "ckirberr");
1317 LCS_DBF_TEXT_(2, trace, " rc%d", -ETIMEDOUT);
1320 PRINT_WARN("unknown error %ld on device %s\n", PTR_ERR(irb),
1322 LCS_DBF_TEXT(2, trace, "ckirberr");
1323 LCS_DBF_TEXT(2, trace, " rc???");
1325 return PTR_ERR(irb);
1330 * IRQ Handler for LCS channels
1333 lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1335 struct lcs_card *card;
1336 struct lcs_channel *channel;
1339 if (lcs_check_irb_error(cdev, irb))
1342 card = CARD_FROM_DEV(cdev);
1343 if (card->read.ccwdev == cdev)
1344 channel = &card->read;
1346 channel = &card->write;
1348 LCS_DBF_TEXT_(5, trace, "Rint%s",cdev->dev.bus_id);
1349 LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.cstat, irb->scsw.dstat);
1350 LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.fctl, irb->scsw.actl);
1352 /* How far in the ccw chain have we processed? */
1353 if ((channel->state != CH_STATE_INIT) &&
1354 (irb->scsw.fctl & SCSW_FCTL_START_FUNC)) {
1355 index = (struct ccw1 *) __va((addr_t) irb->scsw.cpa)
1357 if ((irb->scsw.actl & SCSW_ACTL_SUSPENDED) ||
1358 (irb->scsw.cstat | SCHN_STAT_PCI))
1359 /* Bloody io subsystem tells us lies about cpa... */
1360 index = (index - 1) & (LCS_NUM_BUFFS - 1);
1361 while (channel->io_idx != index) {
1362 __lcs_processed_buffer(channel,
1363 channel->iob + channel->io_idx);
1365 (channel->io_idx + 1) & (LCS_NUM_BUFFS - 1);
1369 if ((irb->scsw.dstat & DEV_STAT_DEV_END) ||
1370 (irb->scsw.dstat & DEV_STAT_CHN_END) ||
1371 (irb->scsw.dstat & DEV_STAT_UNIT_CHECK))
1372 /* Mark channel as stopped. */
1373 channel->state = CH_STATE_STOPPED;
1374 else if (irb->scsw.actl & SCSW_ACTL_SUSPENDED)
1375 /* CCW execution stopped on a suspend bit. */
1376 channel->state = CH_STATE_SUSPENDED;
1378 if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1379 if (irb->scsw.cc != 0) {
1380 ccw_device_halt(channel->ccwdev, (addr_t) channel);
1383 /* The channel has been stopped by halt_IO. */
1384 channel->state = CH_STATE_HALTED;
1387 if (irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
1388 channel->state = CH_STATE_CLEARED;
1390 /* Do the rest in the tasklet. */
1391 tasklet_schedule(&channel->irq_tasklet);
1395 * Tasklet for IRQ handler
1398 lcs_tasklet(unsigned long data)
1400 unsigned long flags;
1401 struct lcs_channel *channel;
1402 struct lcs_buffer *iob;
1406 channel = (struct lcs_channel *) data;
1407 LCS_DBF_TEXT_(5, trace, "tlet%s",channel->ccwdev->dev.bus_id);
1409 /* Check for processed buffers. */
1411 buf_idx = channel->buf_idx;
1412 while (iob[buf_idx].state == BUF_STATE_PROCESSED) {
1413 /* Do the callback thing. */
1414 if (iob[buf_idx].callback != NULL)
1415 iob[buf_idx].callback(channel, iob + buf_idx);
1416 buf_idx = (buf_idx + 1) & (LCS_NUM_BUFFS - 1);
1418 channel->buf_idx = buf_idx;
1420 if (channel->state == CH_STATE_STOPPED)
1421 // FIXME: what if rc != 0 ??
1422 rc = lcs_start_channel(channel);
1423 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1424 if (channel->state == CH_STATE_SUSPENDED &&
1425 channel->iob[channel->io_idx].state == BUF_STATE_READY) {
1426 // FIXME: what if rc != 0 ??
1427 rc = __lcs_resume_channel(channel);
1429 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1431 /* Something happened on the channel. Wake up waiters. */
1432 wake_up(&channel->wait_q);
1436 * Finish current tx buffer and make it ready for transmit.
1439 __lcs_emit_txbuffer(struct lcs_card *card)
1441 LCS_DBF_TEXT(5, trace, "emittx");
1442 *(__u16 *)(card->tx_buffer->data + card->tx_buffer->count) = 0;
1443 card->tx_buffer->count += 2;
1444 lcs_ready_buffer(&card->write, card->tx_buffer);
1445 card->tx_buffer = NULL;
1450 * Callback for finished tx buffers.
1453 lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1455 struct lcs_card *card;
1457 LCS_DBF_TEXT(5, trace, "txbuffcb");
1458 /* Put buffer back to pool. */
1459 lcs_release_buffer(channel, buffer);
1460 card = (struct lcs_card *)
1461 ((char *) channel - offsetof(struct lcs_card, write));
1462 spin_lock(&card->lock);
1464 if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1466 * Last running tx buffer has finished. Submit partially
1467 * filled current buffer.
1469 __lcs_emit_txbuffer(card);
1470 spin_unlock(&card->lock);
1474 * Packet transmit function called by network stack
1477 __lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
1478 struct net_device *dev)
1480 struct lcs_header *header;
1482 LCS_DBF_TEXT(5, trace, "hardxmit");
1484 card->stats.tx_dropped++;
1485 card->stats.tx_errors++;
1488 if (card->state != DEV_STATE_UP) {
1490 card->stats.tx_dropped++;
1491 card->stats.tx_errors++;
1492 card->stats.tx_carrier_errors++;
1495 if (netif_queue_stopped(dev) ) {
1496 card->stats.tx_dropped++;
1499 if (card->tx_buffer != NULL &&
1500 card->tx_buffer->count + sizeof(struct lcs_header) +
1501 skb->len + sizeof(u16) > LCS_IOBUFFERSIZE)
1502 /* skb too big for current tx buffer. */
1503 __lcs_emit_txbuffer(card);
1504 if (card->tx_buffer == NULL) {
1505 /* Get new tx buffer */
1506 card->tx_buffer = lcs_get_buffer(&card->write);
1507 if (card->tx_buffer == NULL) {
1508 card->stats.tx_dropped++;
1511 card->tx_buffer->callback = lcs_txbuffer_cb;
1512 card->tx_buffer->count = 0;
1514 header = (struct lcs_header *)
1515 (card->tx_buffer->data + card->tx_buffer->count);
1516 card->tx_buffer->count += skb->len + sizeof(struct lcs_header);
1517 header->offset = card->tx_buffer->count;
1518 header->type = card->lan_type;
1519 header->slot = card->portno;
1520 memcpy(header + 1, skb->data, skb->len);
1521 card->stats.tx_bytes += skb->len;
1522 card->stats.tx_packets++;
1524 if (card->tx_emitted <= 0)
1525 /* If this is the first tx buffer emit it immediately. */
1526 __lcs_emit_txbuffer(card);
1531 lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
1533 struct lcs_card *card;
1536 LCS_DBF_TEXT(5, trace, "pktxmit");
1537 card = (struct lcs_card *) dev->priv;
1538 spin_lock(&card->lock);
1539 rc = __lcs_start_xmit(card, skb, dev);
1540 spin_unlock(&card->lock);
1545 * send startlan and lanstat command to make LCS device ready
1548 lcs_startlan_auto(struct lcs_card *card)
1552 LCS_DBF_TEXT(2, trace, "strtauto");
1553 #ifdef CONFIG_NET_ETHERNET
1554 card->lan_type = LCS_FRAME_TYPE_ENET;
1555 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1561 card->lan_type = LCS_FRAME_TYPE_TR;
1562 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1567 card->lan_type = LCS_FRAME_TYPE_FDDI;
1568 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1576 lcs_startlan(struct lcs_card *card)
1580 LCS_DBF_TEXT(2, trace, "startlan");
1582 if (card->portno != LCS_INVALID_PORT_NO) {
1583 if (card->lan_type == LCS_FRAME_TYPE_AUTO)
1584 rc = lcs_startlan_auto(card);
1586 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1588 for (i = 0; i <= 16; i++) {
1590 if (card->lan_type != LCS_FRAME_TYPE_AUTO)
1591 rc = lcs_send_startlan(card,
1592 LCS_INITIATOR_TCPIP);
1594 /* autodetecting lan type */
1595 rc = lcs_startlan_auto(card);
1601 return lcs_send_lanstat(card);
1606 * LCS detect function
1607 * setup channels and make them I/O ready
1610 lcs_detect(struct lcs_card *card)
1614 LCS_DBF_TEXT(2, setup, "lcsdetct");
1615 /* start/reset card */
1617 netif_stop_queue(card->dev);
1618 rc = lcs_stop_channels(card);
1620 rc = lcs_start_channels(card);
1622 rc = lcs_send_startup(card, LCS_INITIATOR_TCPIP);
1624 rc = lcs_startlan(card);
1628 card->state = DEV_STATE_UP;
1630 card->state = DEV_STATE_DOWN;
1631 card->write.state = CH_STATE_INIT;
1632 card->read.state = CH_STATE_INIT;
1641 lcs_resetcard(struct lcs_card *card)
1645 LCS_DBF_TEXT(2, trace, "rescard");
1646 for (retries = 0; retries < 10; retries++) {
1647 if (lcs_detect(card) == 0) {
1648 netif_wake_queue(card->dev);
1649 card->state = DEV_STATE_UP;
1650 PRINT_INFO("LCS device %s successfully restarted!\n",
1656 PRINT_ERR("Error in Reseting LCS card!\n");
1665 lcs_stopcard(struct lcs_card *card)
1669 LCS_DBF_TEXT(3, setup, "stopcard");
1671 if (card->read.state != CH_STATE_STOPPED &&
1672 card->write.state != CH_STATE_STOPPED &&
1673 card->state == DEV_STATE_UP) {
1674 lcs_clear_multicast_list(card);
1675 rc = lcs_send_stoplan(card,LCS_INITIATOR_TCPIP);
1676 rc = lcs_send_shutdown(card);
1678 rc = lcs_stop_channels(card);
1679 card->state = DEV_STATE_DOWN;
1685 * LGW initiated commands
1688 lcs_lgw_startlan_thread(void *data)
1690 struct lcs_card *card;
1692 card = (struct lcs_card *) data;
1693 daemonize("lgwstpln");
1695 if (!lcs_do_run_thread(card, LCS_STARTLAN_THREAD))
1697 LCS_DBF_TEXT(4, trace, "lgwstpln");
1699 netif_stop_queue(card->dev);
1700 if (lcs_startlan(card) == 0) {
1701 netif_wake_queue(card->dev);
1702 card->state = DEV_STATE_UP;
1703 PRINT_INFO("LCS Startlan for device %s succeeded!\n",
1707 PRINT_ERR("LCS Startlan for device %s failed!\n",
1709 lcs_clear_thread_running_bit(card, LCS_STARTLAN_THREAD);
1714 * Send startup command initiated by Lan Gateway
1717 lcs_lgw_startup_thread(void *data)
1721 struct lcs_card *card;
1723 card = (struct lcs_card *) data;
1724 daemonize("lgwstaln");
1726 if (!lcs_do_run_thread(card, LCS_STARTUP_THREAD))
1728 LCS_DBF_TEXT(4, trace, "lgwstaln");
1730 netif_stop_queue(card->dev);
1731 rc = lcs_send_startup(card, LCS_INITIATOR_LGW);
1733 PRINT_ERR("Startup for LCS device %s initiated " \
1734 "by LGW failed!\nReseting card ...\n",
1736 /* do a card reset */
1737 rc = lcs_resetcard(card);
1741 rc = lcs_startlan(card);
1743 netif_wake_queue(card->dev);
1744 card->state = DEV_STATE_UP;
1748 PRINT_INFO("LCS Startup for device %s succeeded!\n",
1751 PRINT_ERR("LCS Startup for device %s failed!\n",
1753 lcs_clear_thread_running_bit(card, LCS_STARTUP_THREAD);
1759 * send stoplan command initiated by Lan Gateway
1762 lcs_lgw_stoplan_thread(void *data)
1764 struct lcs_card *card;
1767 card = (struct lcs_card *) data;
1768 daemonize("lgwstop");
1770 if (!lcs_do_run_thread(card, LCS_STOPLAN_THREAD))
1772 LCS_DBF_TEXT(4, trace, "lgwstop");
1774 netif_stop_queue(card->dev);
1775 if (lcs_send_stoplan(card, LCS_INITIATOR_LGW) == 0)
1776 PRINT_INFO("Stoplan for %s initiated by LGW succeeded!\n",
1779 PRINT_ERR("Stoplan %s initiated by LGW failed!\n",
1781 /*Try to reset the card, stop it on failure */
1782 rc = lcs_resetcard(card);
1784 rc = lcs_stopcard(card);
1785 lcs_clear_thread_running_bit(card, LCS_STOPLAN_THREAD);
1790 * Kernel Thread helper functions for LGW initiated commands
1793 lcs_start_kernel_thread(struct lcs_card *card)
1795 LCS_DBF_TEXT(5, trace, "krnthrd");
1796 if (lcs_do_start_thread(card, LCS_STARTUP_THREAD))
1797 kernel_thread(lcs_lgw_startup_thread, (void *) card, SIGCHLD);
1798 if (lcs_do_start_thread(card, LCS_STARTLAN_THREAD))
1799 kernel_thread(lcs_lgw_startlan_thread, (void *) card, SIGCHLD);
1800 if (lcs_do_start_thread(card, LCS_STOPLAN_THREAD))
1801 kernel_thread(lcs_lgw_stoplan_thread, (void *) card, SIGCHLD);
1802 #ifdef CONFIG_IP_MULTICAST
1803 if (lcs_do_start_thread(card, LCS_SET_MC_THREAD))
1804 kernel_thread(lcs_register_mc_addresses, (void *) card, SIGCHLD);
1809 * Process control frames.
1812 lcs_get_control(struct lcs_card *card, struct lcs_cmd *cmd)
1814 LCS_DBF_TEXT(5, trace, "getctrl");
1815 if (cmd->initiator == LCS_INITIATOR_LGW) {
1816 switch(cmd->cmd_code) {
1817 case LCS_CMD_STARTUP:
1818 if (!lcs_set_thread_start_bit(card,
1819 LCS_STARTUP_THREAD))
1820 schedule_work(&card->kernel_thread_starter);
1822 case LCS_CMD_STARTLAN:
1823 if (!lcs_set_thread_start_bit(card,
1824 LCS_STARTLAN_THREAD))
1825 schedule_work(&card->kernel_thread_starter);
1827 case LCS_CMD_STOPLAN:
1828 if (!lcs_set_thread_start_bit(card,
1829 LCS_STOPLAN_THREAD))
1830 schedule_work(&card->kernel_thread_starter);
1833 PRINT_INFO("UNRECOGNIZED LGW COMMAND\n");
1837 lcs_notify_lancmd_waiters(card, cmd);
1841 * Unpack network packet.
1844 lcs_get_skb(struct lcs_card *card, char *skb_data, unsigned int skb_len)
1846 struct sk_buff *skb;
1848 LCS_DBF_TEXT(5, trace, "getskb");
1849 if (card->dev == NULL ||
1850 card->state != DEV_STATE_UP)
1851 /* The card isn't up. Ignore the packet. */
1854 skb = dev_alloc_skb(skb_len);
1856 PRINT_ERR("LCS: alloc_skb failed for device=%s\n",
1858 card->stats.rx_dropped++;
1861 skb->dev = card->dev;
1862 memcpy(skb_put(skb, skb_len), skb_data, skb_len);
1863 skb->protocol = card->lan_type_trans(skb, card->dev);
1864 card->stats.rx_bytes += skb_len;
1865 card->stats.rx_packets++;
1866 *((__u32 *)skb->cb) = ++card->pkt_seq;
1871 * LCS main routine to get packets and lancmd replies from the buffers
1874 lcs_get_frames_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1876 struct lcs_card *card;
1877 struct lcs_header *lcs_hdr;
1880 LCS_DBF_TEXT(5, trace, "lcsgtpkt");
1881 lcs_hdr = (struct lcs_header *) buffer->data;
1882 if (lcs_hdr->offset == LCS_ILLEGAL_OFFSET) {
1883 LCS_DBF_TEXT(4, trace, "-eiogpkt");
1886 card = (struct lcs_card *)
1887 ((char *) channel - offsetof(struct lcs_card, read));
1889 while (lcs_hdr->offset != 0) {
1890 if (lcs_hdr->offset <= 0 ||
1891 lcs_hdr->offset > LCS_IOBUFFERSIZE ||
1892 lcs_hdr->offset < offset) {
1893 /* Offset invalid. */
1894 card->stats.rx_length_errors++;
1895 card->stats.rx_errors++;
1898 /* What kind of frame is it? */
1899 if (lcs_hdr->type == LCS_FRAME_TYPE_CONTROL)
1900 /* Control frame. */
1901 lcs_get_control(card, (struct lcs_cmd *) lcs_hdr);
1902 else if (lcs_hdr->type == LCS_FRAME_TYPE_ENET ||
1903 lcs_hdr->type == LCS_FRAME_TYPE_TR ||
1904 lcs_hdr->type == LCS_FRAME_TYPE_FDDI)
1905 /* Normal network packet. */
1906 lcs_get_skb(card, (char *)(lcs_hdr + 1),
1907 lcs_hdr->offset - offset -
1908 sizeof(struct lcs_header));
1910 /* Unknown frame type. */
1911 ; // FIXME: error message ?
1912 /* Proceed to next frame. */
1913 offset = lcs_hdr->offset;
1914 lcs_hdr->offset = LCS_ILLEGAL_OFFSET;
1915 lcs_hdr = (struct lcs_header *) (buffer->data + offset);
1917 /* The buffer is now empty. Make it ready again. */
1918 lcs_ready_buffer(&card->read, buffer);
1922 * get network statistics for ifconfig and other user programs
1924 static struct net_device_stats *
1925 lcs_getstats(struct net_device *dev)
1927 struct lcs_card *card;
1929 LCS_DBF_TEXT(4, trace, "netstats");
1930 card = (struct lcs_card *) dev->priv;
1931 return &card->stats;
1936 * This function will be called by user doing ifconfig xxx down
1939 lcs_stop_device(struct net_device *dev)
1941 struct lcs_card *card;
1944 LCS_DBF_TEXT(2, trace, "stopdev");
1945 card = (struct lcs_card *) dev->priv;
1946 netif_stop_queue(dev);
1947 dev->flags &= ~IFF_UP;
1948 rc = lcs_stopcard(card);
1950 PRINT_ERR("Try it again!\n ");
1955 * start lcs device and make it runnable
1956 * This function will be called by user doing ifconfig xxx up
1959 lcs_open_device(struct net_device *dev)
1961 struct lcs_card *card;
1964 LCS_DBF_TEXT(2, trace, "opendev");
1965 card = (struct lcs_card *) dev->priv;
1966 /* initialize statistics */
1967 rc = lcs_detect(card);
1969 PRINT_ERR("LCS:Error in opening device!\n");
1972 dev->flags |= IFF_UP;
1973 netif_wake_queue(dev);
1974 card->state = DEV_STATE_UP;
1980 * show function for portno called by cat or similar things
1983 lcs_portno_show (struct device *dev, struct device_attribute *attr, char *buf)
1985 struct lcs_card *card;
1987 card = (struct lcs_card *)dev->driver_data;
1992 return sprintf(buf, "%d\n", card->portno);
1996 * store the value which is piped to file portno
1999 lcs_portno_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2001 struct lcs_card *card;
2004 card = (struct lcs_card *)dev->driver_data;
2009 sscanf(buf, "%u", &value);
2010 /* TODO: sanity checks */
2011 card->portno = value;
2017 static DEVICE_ATTR(portno, 0644, lcs_portno_show, lcs_portno_store);
2020 lcs_type_show(struct device *dev, struct device_attribute *attr, char *buf)
2022 struct ccwgroup_device *cgdev;
2024 cgdev = to_ccwgroupdev(dev);
2028 return sprintf(buf, "%s\n", cu3088_type[cgdev->cdev[0]->id.driver_info]);
2031 static DEVICE_ATTR(type, 0444, lcs_type_show, NULL);
2034 lcs_timeout_show(struct device *dev, struct device_attribute *attr, char *buf)
2036 struct lcs_card *card;
2038 card = (struct lcs_card *)dev->driver_data;
2040 return card ? sprintf(buf, "%u\n", card->lancmd_timeout) : 0;
2044 lcs_timeout_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2046 struct lcs_card *card;
2049 card = (struct lcs_card *)dev->driver_data;
2054 sscanf(buf, "%u", &value);
2055 /* TODO: sanity checks */
2056 card->lancmd_timeout = value;
2062 DEVICE_ATTR(lancmd_timeout, 0644, lcs_timeout_show, lcs_timeout_store);
2064 static struct attribute * lcs_attrs[] = {
2065 &dev_attr_portno.attr,
2066 &dev_attr_type.attr,
2067 &dev_attr_lancmd_timeout.attr,
2071 static struct attribute_group lcs_attr_group = {
2076 * lcs_probe_device is called on establishing a new ccwgroup_device.
2079 lcs_probe_device(struct ccwgroup_device *ccwgdev)
2081 struct lcs_card *card;
2084 if (!get_device(&ccwgdev->dev))
2087 LCS_DBF_TEXT(2, setup, "add_dev");
2088 card = lcs_alloc_card();
2090 PRINT_ERR("Allocation of lcs card failed\n");
2091 put_device(&ccwgdev->dev);
2094 ret = sysfs_create_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2096 PRINT_ERR("Creating attributes failed");
2097 lcs_free_card(card);
2098 put_device(&ccwgdev->dev);
2101 ccwgdev->dev.driver_data = card;
2102 ccwgdev->cdev[0]->handler = lcs_irq;
2103 ccwgdev->cdev[1]->handler = lcs_irq;
2108 lcs_register_netdev(struct ccwgroup_device *ccwgdev)
2110 struct lcs_card *card;
2112 LCS_DBF_TEXT(2, setup, "regnetdv");
2113 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2114 if (card->dev->reg_state != NETREG_UNINITIALIZED)
2116 SET_NETDEV_DEV(card->dev, &ccwgdev->dev);
2117 return register_netdev(card->dev);
2121 * lcs_new_device will be called by setting the group device online.
2125 lcs_new_device(struct ccwgroup_device *ccwgdev)
2127 struct lcs_card *card;
2128 struct net_device *dev=NULL;
2129 enum lcs_dev_states recover_state;
2132 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2136 LCS_DBF_TEXT(2, setup, "newdev");
2137 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2138 card->read.ccwdev = ccwgdev->cdev[0];
2139 card->write.ccwdev = ccwgdev->cdev[1];
2141 recover_state = card->state;
2142 ccw_device_set_online(card->read.ccwdev);
2143 ccw_device_set_online(card->write.ccwdev);
2145 LCS_DBF_TEXT(3, setup, "lcsnewdv");
2147 lcs_setup_card(card);
2148 rc = lcs_detect(card);
2150 LCS_DBF_TEXT(2, setup, "dtctfail");
2151 PRINT_WARN("Detection of LCS card failed with return code "
2152 "%d (0x%x)\n", rc, rc);
2157 LCS_DBF_TEXT(2, setup, "samedev");
2158 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2161 switch (card->lan_type) {
2162 #ifdef CONFIG_NET_ETHERNET
2163 case LCS_FRAME_TYPE_ENET:
2164 card->lan_type_trans = eth_type_trans;
2165 dev = alloc_etherdev(0);
2169 case LCS_FRAME_TYPE_TR:
2170 card->lan_type_trans = tr_type_trans;
2171 dev = alloc_trdev(0);
2175 case LCS_FRAME_TYPE_FDDI:
2176 card->lan_type_trans = fddi_type_trans;
2177 dev = alloc_fddidev(0);
2181 LCS_DBF_TEXT(3, setup, "errinit");
2182 PRINT_ERR("LCS: Initialization failed\n");
2183 PRINT_ERR("LCS: No device found!\n");
2189 card->dev->priv = card;
2190 card->dev->open = lcs_open_device;
2191 card->dev->stop = lcs_stop_device;
2192 card->dev->hard_start_xmit = lcs_start_xmit;
2193 card->dev->get_stats = lcs_getstats;
2194 SET_MODULE_OWNER(dev);
2195 memcpy(card->dev->dev_addr, card->mac, LCS_MAC_LENGTH);
2196 #ifdef CONFIG_IP_MULTICAST
2197 if (!lcs_check_multicast_support(card))
2198 card->dev->set_multicast_list = lcs_set_multicast_list;
2201 lcs_set_allowed_threads(card,0xffffffff);
2202 if (recover_state == DEV_STATE_RECOVER) {
2203 lcs_set_multicast_list(card->dev);
2204 card->dev->flags |= IFF_UP;
2205 netif_wake_queue(card->dev);
2206 card->state = DEV_STATE_UP;
2211 if (lcs_register_netdev(ccwgdev) != 0)
2214 /* Print out supported assists: IPv6 */
2215 PRINT_INFO("LCS device %s %s IPv6 support\n", card->dev->name,
2216 (card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
2217 "with" : "without");
2218 /* Print out supported assist: Multicast */
2219 PRINT_INFO("LCS device %s %s Multicast support\n", card->dev->name,
2220 (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
2221 "with" : "without");
2225 ccw_device_set_offline(card->read.ccwdev);
2226 ccw_device_set_offline(card->write.ccwdev);
2231 * lcs_shutdown_device, called when setting the group device offline.
2234 lcs_shutdown_device(struct ccwgroup_device *ccwgdev)
2236 struct lcs_card *card;
2237 enum lcs_dev_states recover_state;
2240 LCS_DBF_TEXT(3, setup, "shtdndev");
2241 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2244 lcs_set_allowed_threads(card, 0);
2245 if (lcs_wait_for_threads(card, LCS_SET_MC_THREAD))
2246 return -ERESTARTSYS;
2247 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2248 recover_state = card->state;
2250 ret = lcs_stop_device(card->dev);
2251 ret = ccw_device_set_offline(card->read.ccwdev);
2252 ret = ccw_device_set_offline(card->write.ccwdev);
2253 if (recover_state == DEV_STATE_UP) {
2254 card->state = DEV_STATE_RECOVER;
2262 * lcs_remove_device, free buffers and card
2265 lcs_remove_device(struct ccwgroup_device *ccwgdev)
2267 struct lcs_card *card;
2269 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2273 PRINT_INFO("Removing lcs group device ....\n");
2274 LCS_DBF_TEXT(3, setup, "remdev");
2275 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2276 if (ccwgdev->state == CCWGROUP_ONLINE) {
2277 lcs_shutdown_device(ccwgdev);
2280 unregister_netdev(card->dev);
2281 sysfs_remove_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2282 lcs_cleanup_card(card);
2283 lcs_free_card(card);
2284 put_device(&ccwgdev->dev);
2288 * LCS ccwgroup driver registration
2290 static struct ccwgroup_driver lcs_group_driver = {
2291 .owner = THIS_MODULE,
2294 .driver_id = 0xD3C3E2,
2295 .probe = lcs_probe_device,
2296 .remove = lcs_remove_device,
2297 .set_online = lcs_new_device,
2298 .set_offline = lcs_shutdown_device,
2302 * LCS Module/Kernel initialization function
2305 __init lcs_init_module(void)
2309 PRINT_INFO("Loading %s\n",version);
2310 rc = lcs_register_debug_facility();
2311 LCS_DBF_TEXT(0, setup, "lcsinit");
2313 PRINT_ERR("Initialization failed\n");
2317 rc = register_cu3088_discipline(&lcs_group_driver);
2319 PRINT_ERR("Initialization failed\n");
2328 * LCS module cleanup function
2331 __exit lcs_cleanup_module(void)
2333 PRINT_INFO("Terminating lcs module.\n");
2334 LCS_DBF_TEXT(0, trace, "cleanup");
2335 unregister_cu3088_discipline(&lcs_group_driver);
2336 lcs_unregister_debug_facility();
2339 module_init(lcs_init_module);
2340 module_exit(lcs_cleanup_module);
2342 MODULE_AUTHOR("Frank Pavlic <fpavlic@de.ibm.com>");
2343 MODULE_LICENSE("GPL");