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, 2);
102 debug_register_view(lcs_dbf_trace, &debug_hex_ascii_view);
103 debug_set_level(lcs_dbf_trace, 2);
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);
1299 #endif /* CONFIG_IP_MULTICAST */
1302 lcs_check_irb_error(struct ccw_device *cdev, struct irb *irb)
1307 switch (PTR_ERR(irb)) {
1309 PRINT_WARN("i/o-error on device %s\n", cdev->dev.bus_id);
1310 LCS_DBF_TEXT(2, trace, "ckirberr");
1311 LCS_DBF_TEXT_(2, trace, " rc%d", -EIO);
1314 PRINT_WARN("timeout on device %s\n", cdev->dev.bus_id);
1315 LCS_DBF_TEXT(2, trace, "ckirberr");
1316 LCS_DBF_TEXT_(2, trace, " rc%d", -ETIMEDOUT);
1319 PRINT_WARN("unknown error %ld on device %s\n", PTR_ERR(irb),
1321 LCS_DBF_TEXT(2, trace, "ckirberr");
1322 LCS_DBF_TEXT(2, trace, " rc???");
1324 return PTR_ERR(irb);
1329 * IRQ Handler for LCS channels
1332 lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1334 struct lcs_card *card;
1335 struct lcs_channel *channel;
1338 if (lcs_check_irb_error(cdev, irb))
1341 card = CARD_FROM_DEV(cdev);
1342 if (card->read.ccwdev == cdev)
1343 channel = &card->read;
1345 channel = &card->write;
1347 LCS_DBF_TEXT_(5, trace, "Rint%s",cdev->dev.bus_id);
1348 LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.cstat, irb->scsw.dstat);
1349 LCS_DBF_TEXT_(5, trace, "%4x%4x",irb->scsw.fctl, irb->scsw.actl);
1351 /* How far in the ccw chain have we processed? */
1352 if ((channel->state != CH_STATE_INIT) &&
1353 (irb->scsw.fctl & SCSW_FCTL_START_FUNC)) {
1354 index = (struct ccw1 *) __va((addr_t) irb->scsw.cpa)
1356 if ((irb->scsw.actl & SCSW_ACTL_SUSPENDED) ||
1357 (irb->scsw.cstat | SCHN_STAT_PCI))
1358 /* Bloody io subsystem tells us lies about cpa... */
1359 index = (index - 1) & (LCS_NUM_BUFFS - 1);
1360 while (channel->io_idx != index) {
1361 __lcs_processed_buffer(channel,
1362 channel->iob + channel->io_idx);
1364 (channel->io_idx + 1) & (LCS_NUM_BUFFS - 1);
1368 if ((irb->scsw.dstat & DEV_STAT_DEV_END) ||
1369 (irb->scsw.dstat & DEV_STAT_CHN_END) ||
1370 (irb->scsw.dstat & DEV_STAT_UNIT_CHECK))
1371 /* Mark channel as stopped. */
1372 channel->state = CH_STATE_STOPPED;
1373 else if (irb->scsw.actl & SCSW_ACTL_SUSPENDED)
1374 /* CCW execution stopped on a suspend bit. */
1375 channel->state = CH_STATE_SUSPENDED;
1377 if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC) {
1378 if (irb->scsw.cc != 0) {
1379 ccw_device_halt(channel->ccwdev, (addr_t) channel);
1382 /* The channel has been stopped by halt_IO. */
1383 channel->state = CH_STATE_HALTED;
1386 if (irb->scsw.fctl & SCSW_FCTL_CLEAR_FUNC) {
1387 channel->state = CH_STATE_CLEARED;
1389 /* Do the rest in the tasklet. */
1390 tasklet_schedule(&channel->irq_tasklet);
1394 * Tasklet for IRQ handler
1397 lcs_tasklet(unsigned long data)
1399 unsigned long flags;
1400 struct lcs_channel *channel;
1401 struct lcs_buffer *iob;
1405 channel = (struct lcs_channel *) data;
1406 LCS_DBF_TEXT_(5, trace, "tlet%s",channel->ccwdev->dev.bus_id);
1408 /* Check for processed buffers. */
1410 buf_idx = channel->buf_idx;
1411 while (iob[buf_idx].state == BUF_STATE_PROCESSED) {
1412 /* Do the callback thing. */
1413 if (iob[buf_idx].callback != NULL)
1414 iob[buf_idx].callback(channel, iob + buf_idx);
1415 buf_idx = (buf_idx + 1) & (LCS_NUM_BUFFS - 1);
1417 channel->buf_idx = buf_idx;
1419 if (channel->state == CH_STATE_STOPPED)
1420 // FIXME: what if rc != 0 ??
1421 rc = lcs_start_channel(channel);
1422 spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1423 if (channel->state == CH_STATE_SUSPENDED &&
1424 channel->iob[channel->io_idx].state == BUF_STATE_READY) {
1425 // FIXME: what if rc != 0 ??
1426 rc = __lcs_resume_channel(channel);
1428 spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1430 /* Something happened on the channel. Wake up waiters. */
1431 wake_up(&channel->wait_q);
1435 * Finish current tx buffer and make it ready for transmit.
1438 __lcs_emit_txbuffer(struct lcs_card *card)
1440 LCS_DBF_TEXT(5, trace, "emittx");
1441 *(__u16 *)(card->tx_buffer->data + card->tx_buffer->count) = 0;
1442 card->tx_buffer->count += 2;
1443 lcs_ready_buffer(&card->write, card->tx_buffer);
1444 card->tx_buffer = NULL;
1449 * Callback for finished tx buffers.
1452 lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1454 struct lcs_card *card;
1456 LCS_DBF_TEXT(5, trace, "txbuffcb");
1457 /* Put buffer back to pool. */
1458 lcs_release_buffer(channel, buffer);
1459 card = (struct lcs_card *)
1460 ((char *) channel - offsetof(struct lcs_card, write));
1461 if (netif_queue_stopped(card->dev))
1462 netif_wake_queue(card->dev);
1463 spin_lock(&card->lock);
1465 if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1467 * Last running tx buffer has finished. Submit partially
1468 * filled current buffer.
1470 __lcs_emit_txbuffer(card);
1471 spin_unlock(&card->lock);
1475 * Packet transmit function called by network stack
1478 __lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
1479 struct net_device *dev)
1481 struct lcs_header *header;
1484 LCS_DBF_TEXT(5, trace, "hardxmit");
1486 card->stats.tx_dropped++;
1487 card->stats.tx_errors++;
1490 if (card->state != DEV_STATE_UP) {
1492 card->stats.tx_dropped++;
1493 card->stats.tx_errors++;
1494 card->stats.tx_carrier_errors++;
1497 netif_stop_queue(card->dev);
1498 spin_lock(&card->lock);
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++;
1512 card->tx_buffer->callback = lcs_txbuffer_cb;
1513 card->tx_buffer->count = 0;
1515 header = (struct lcs_header *)
1516 (card->tx_buffer->data + card->tx_buffer->count);
1517 card->tx_buffer->count += skb->len + sizeof(struct lcs_header);
1518 header->offset = card->tx_buffer->count;
1519 header->type = card->lan_type;
1520 header->slot = card->portno;
1521 memcpy(header + 1, skb->data, skb->len);
1522 spin_unlock(&card->lock);
1523 card->stats.tx_bytes += skb->len;
1524 card->stats.tx_packets++;
1526 netif_wake_queue(card->dev);
1527 spin_lock(&card->lock);
1528 if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
1529 /* If this is the first tx buffer emit it immediately. */
1530 __lcs_emit_txbuffer(card);
1532 spin_unlock(&card->lock);
1537 lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
1539 struct lcs_card *card;
1542 LCS_DBF_TEXT(5, trace, "pktxmit");
1543 card = (struct lcs_card *) dev->priv;
1544 rc = __lcs_start_xmit(card, skb, dev);
1549 * send startlan and lanstat command to make LCS device ready
1552 lcs_startlan_auto(struct lcs_card *card)
1556 LCS_DBF_TEXT(2, trace, "strtauto");
1557 #ifdef CONFIG_NET_ETHERNET
1558 card->lan_type = LCS_FRAME_TYPE_ENET;
1559 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1565 card->lan_type = LCS_FRAME_TYPE_TR;
1566 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1571 card->lan_type = LCS_FRAME_TYPE_FDDI;
1572 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1580 lcs_startlan(struct lcs_card *card)
1584 LCS_DBF_TEXT(2, trace, "startlan");
1586 if (card->portno != LCS_INVALID_PORT_NO) {
1587 if (card->lan_type == LCS_FRAME_TYPE_AUTO)
1588 rc = lcs_startlan_auto(card);
1590 rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
1592 for (i = 0; i <= 16; i++) {
1594 if (card->lan_type != LCS_FRAME_TYPE_AUTO)
1595 rc = lcs_send_startlan(card,
1596 LCS_INITIATOR_TCPIP);
1598 /* autodetecting lan type */
1599 rc = lcs_startlan_auto(card);
1605 return lcs_send_lanstat(card);
1610 * LCS detect function
1611 * setup channels and make them I/O ready
1614 lcs_detect(struct lcs_card *card)
1618 LCS_DBF_TEXT(2, setup, "lcsdetct");
1619 /* start/reset card */
1621 netif_stop_queue(card->dev);
1622 rc = lcs_stop_channels(card);
1624 rc = lcs_start_channels(card);
1626 rc = lcs_send_startup(card, LCS_INITIATOR_TCPIP);
1628 rc = lcs_startlan(card);
1632 card->state = DEV_STATE_UP;
1634 card->state = DEV_STATE_DOWN;
1635 card->write.state = CH_STATE_INIT;
1636 card->read.state = CH_STATE_INIT;
1645 lcs_resetcard(struct lcs_card *card)
1649 LCS_DBF_TEXT(2, trace, "rescard");
1650 for (retries = 0; retries < 10; retries++) {
1651 if (lcs_detect(card) == 0) {
1652 netif_wake_queue(card->dev);
1653 card->state = DEV_STATE_UP;
1654 PRINT_INFO("LCS device %s successfully restarted!\n",
1660 PRINT_ERR("Error in Reseting LCS card!\n");
1669 lcs_stopcard(struct lcs_card *card)
1673 LCS_DBF_TEXT(3, setup, "stopcard");
1675 if (card->read.state != CH_STATE_STOPPED &&
1676 card->write.state != CH_STATE_STOPPED &&
1677 card->state == DEV_STATE_UP) {
1678 lcs_clear_multicast_list(card);
1679 rc = lcs_send_stoplan(card,LCS_INITIATOR_TCPIP);
1680 rc = lcs_send_shutdown(card);
1682 rc = lcs_stop_channels(card);
1683 card->state = DEV_STATE_DOWN;
1689 * LGW initiated commands
1692 lcs_lgw_startlan_thread(void *data)
1694 struct lcs_card *card;
1696 card = (struct lcs_card *) data;
1697 daemonize("lgwstpln");
1699 if (!lcs_do_run_thread(card, LCS_STARTLAN_THREAD))
1701 LCS_DBF_TEXT(4, trace, "lgwstpln");
1703 netif_stop_queue(card->dev);
1704 if (lcs_startlan(card) == 0) {
1705 netif_wake_queue(card->dev);
1706 card->state = DEV_STATE_UP;
1707 PRINT_INFO("LCS Startlan for device %s succeeded!\n",
1711 PRINT_ERR("LCS Startlan for device %s failed!\n",
1713 lcs_clear_thread_running_bit(card, LCS_STARTLAN_THREAD);
1718 * Send startup command initiated by Lan Gateway
1721 lcs_lgw_startup_thread(void *data)
1725 struct lcs_card *card;
1727 card = (struct lcs_card *) data;
1728 daemonize("lgwstaln");
1730 if (!lcs_do_run_thread(card, LCS_STARTUP_THREAD))
1732 LCS_DBF_TEXT(4, trace, "lgwstaln");
1734 netif_stop_queue(card->dev);
1735 rc = lcs_send_startup(card, LCS_INITIATOR_LGW);
1737 PRINT_ERR("Startup for LCS device %s initiated " \
1738 "by LGW failed!\nReseting card ...\n",
1740 /* do a card reset */
1741 rc = lcs_resetcard(card);
1745 rc = lcs_startlan(card);
1747 netif_wake_queue(card->dev);
1748 card->state = DEV_STATE_UP;
1752 PRINT_INFO("LCS Startup for device %s succeeded!\n",
1755 PRINT_ERR("LCS Startup for device %s failed!\n",
1757 lcs_clear_thread_running_bit(card, LCS_STARTUP_THREAD);
1763 * send stoplan command initiated by Lan Gateway
1766 lcs_lgw_stoplan_thread(void *data)
1768 struct lcs_card *card;
1771 card = (struct lcs_card *) data;
1772 daemonize("lgwstop");
1774 if (!lcs_do_run_thread(card, LCS_STOPLAN_THREAD))
1776 LCS_DBF_TEXT(4, trace, "lgwstop");
1778 netif_stop_queue(card->dev);
1779 if (lcs_send_stoplan(card, LCS_INITIATOR_LGW) == 0)
1780 PRINT_INFO("Stoplan for %s initiated by LGW succeeded!\n",
1783 PRINT_ERR("Stoplan %s initiated by LGW failed!\n",
1785 /*Try to reset the card, stop it on failure */
1786 rc = lcs_resetcard(card);
1788 rc = lcs_stopcard(card);
1789 lcs_clear_thread_running_bit(card, LCS_STOPLAN_THREAD);
1794 * Kernel Thread helper functions for LGW initiated commands
1797 lcs_start_kernel_thread(struct lcs_card *card)
1799 LCS_DBF_TEXT(5, trace, "krnthrd");
1800 if (lcs_do_start_thread(card, LCS_STARTUP_THREAD))
1801 kernel_thread(lcs_lgw_startup_thread, (void *) card, SIGCHLD);
1802 if (lcs_do_start_thread(card, LCS_STARTLAN_THREAD))
1803 kernel_thread(lcs_lgw_startlan_thread, (void *) card, SIGCHLD);
1804 if (lcs_do_start_thread(card, LCS_STOPLAN_THREAD))
1805 kernel_thread(lcs_lgw_stoplan_thread, (void *) card, SIGCHLD);
1806 #ifdef CONFIG_IP_MULTICAST
1807 if (lcs_do_start_thread(card, LCS_SET_MC_THREAD))
1808 kernel_thread(lcs_register_mc_addresses, (void *) card, SIGCHLD);
1813 * Process control frames.
1816 lcs_get_control(struct lcs_card *card, struct lcs_cmd *cmd)
1818 LCS_DBF_TEXT(5, trace, "getctrl");
1819 if (cmd->initiator == LCS_INITIATOR_LGW) {
1820 switch(cmd->cmd_code) {
1821 case LCS_CMD_STARTUP:
1822 if (!lcs_set_thread_start_bit(card,
1823 LCS_STARTUP_THREAD))
1824 schedule_work(&card->kernel_thread_starter);
1826 case LCS_CMD_STARTLAN:
1827 if (!lcs_set_thread_start_bit(card,
1828 LCS_STARTLAN_THREAD))
1829 schedule_work(&card->kernel_thread_starter);
1831 case LCS_CMD_STOPLAN:
1832 if (!lcs_set_thread_start_bit(card,
1833 LCS_STOPLAN_THREAD))
1834 schedule_work(&card->kernel_thread_starter);
1837 PRINT_INFO("UNRECOGNIZED LGW COMMAND\n");
1841 lcs_notify_lancmd_waiters(card, cmd);
1845 * Unpack network packet.
1848 lcs_get_skb(struct lcs_card *card, char *skb_data, unsigned int skb_len)
1850 struct sk_buff *skb;
1852 LCS_DBF_TEXT(5, trace, "getskb");
1853 if (card->dev == NULL ||
1854 card->state != DEV_STATE_UP)
1855 /* The card isn't up. Ignore the packet. */
1858 skb = dev_alloc_skb(skb_len);
1860 PRINT_ERR("LCS: alloc_skb failed for device=%s\n",
1862 card->stats.rx_dropped++;
1865 skb->dev = card->dev;
1866 memcpy(skb_put(skb, skb_len), skb_data, skb_len);
1867 skb->protocol = card->lan_type_trans(skb, card->dev);
1868 card->stats.rx_bytes += skb_len;
1869 card->stats.rx_packets++;
1870 *((__u32 *)skb->cb) = ++card->pkt_seq;
1875 * LCS main routine to get packets and lancmd replies from the buffers
1878 lcs_get_frames_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1880 struct lcs_card *card;
1881 struct lcs_header *lcs_hdr;
1884 LCS_DBF_TEXT(5, trace, "lcsgtpkt");
1885 lcs_hdr = (struct lcs_header *) buffer->data;
1886 if (lcs_hdr->offset == LCS_ILLEGAL_OFFSET) {
1887 LCS_DBF_TEXT(4, trace, "-eiogpkt");
1890 card = (struct lcs_card *)
1891 ((char *) channel - offsetof(struct lcs_card, read));
1893 while (lcs_hdr->offset != 0) {
1894 if (lcs_hdr->offset <= 0 ||
1895 lcs_hdr->offset > LCS_IOBUFFERSIZE ||
1896 lcs_hdr->offset < offset) {
1897 /* Offset invalid. */
1898 card->stats.rx_length_errors++;
1899 card->stats.rx_errors++;
1902 /* What kind of frame is it? */
1903 if (lcs_hdr->type == LCS_FRAME_TYPE_CONTROL)
1904 /* Control frame. */
1905 lcs_get_control(card, (struct lcs_cmd *) lcs_hdr);
1906 else if (lcs_hdr->type == LCS_FRAME_TYPE_ENET ||
1907 lcs_hdr->type == LCS_FRAME_TYPE_TR ||
1908 lcs_hdr->type == LCS_FRAME_TYPE_FDDI)
1909 /* Normal network packet. */
1910 lcs_get_skb(card, (char *)(lcs_hdr + 1),
1911 lcs_hdr->offset - offset -
1912 sizeof(struct lcs_header));
1914 /* Unknown frame type. */
1915 ; // FIXME: error message ?
1916 /* Proceed to next frame. */
1917 offset = lcs_hdr->offset;
1918 lcs_hdr->offset = LCS_ILLEGAL_OFFSET;
1919 lcs_hdr = (struct lcs_header *) (buffer->data + offset);
1921 /* The buffer is now empty. Make it ready again. */
1922 lcs_ready_buffer(&card->read, buffer);
1926 * get network statistics for ifconfig and other user programs
1928 static struct net_device_stats *
1929 lcs_getstats(struct net_device *dev)
1931 struct lcs_card *card;
1933 LCS_DBF_TEXT(4, trace, "netstats");
1934 card = (struct lcs_card *) dev->priv;
1935 return &card->stats;
1940 * This function will be called by user doing ifconfig xxx down
1943 lcs_stop_device(struct net_device *dev)
1945 struct lcs_card *card;
1948 LCS_DBF_TEXT(2, trace, "stopdev");
1949 card = (struct lcs_card *) dev->priv;
1950 netif_stop_queue(dev);
1951 dev->flags &= ~IFF_UP;
1952 rc = lcs_stopcard(card);
1954 PRINT_ERR("Try it again!\n ");
1959 * start lcs device and make it runnable
1960 * This function will be called by user doing ifconfig xxx up
1963 lcs_open_device(struct net_device *dev)
1965 struct lcs_card *card;
1968 LCS_DBF_TEXT(2, trace, "opendev");
1969 card = (struct lcs_card *) dev->priv;
1970 /* initialize statistics */
1971 rc = lcs_detect(card);
1973 PRINT_ERR("LCS:Error in opening device!\n");
1976 dev->flags |= IFF_UP;
1977 netif_wake_queue(dev);
1978 card->state = DEV_STATE_UP;
1984 * show function for portno called by cat or similar things
1987 lcs_portno_show (struct device *dev, struct device_attribute *attr, char *buf)
1989 struct lcs_card *card;
1991 card = (struct lcs_card *)dev->driver_data;
1996 return sprintf(buf, "%d\n", card->portno);
2000 * store the value which is piped to file portno
2003 lcs_portno_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2005 struct lcs_card *card;
2008 card = (struct lcs_card *)dev->driver_data;
2013 sscanf(buf, "%u", &value);
2014 /* TODO: sanity checks */
2015 card->portno = value;
2021 static DEVICE_ATTR(portno, 0644, lcs_portno_show, lcs_portno_store);
2024 lcs_type_show(struct device *dev, struct device_attribute *attr, char *buf)
2026 struct ccwgroup_device *cgdev;
2028 cgdev = to_ccwgroupdev(dev);
2032 return sprintf(buf, "%s\n", cu3088_type[cgdev->cdev[0]->id.driver_info]);
2035 static DEVICE_ATTR(type, 0444, lcs_type_show, NULL);
2038 lcs_timeout_show(struct device *dev, struct device_attribute *attr, char *buf)
2040 struct lcs_card *card;
2042 card = (struct lcs_card *)dev->driver_data;
2044 return card ? sprintf(buf, "%u\n", card->lancmd_timeout) : 0;
2048 lcs_timeout_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2050 struct lcs_card *card;
2053 card = (struct lcs_card *)dev->driver_data;
2058 sscanf(buf, "%u", &value);
2059 /* TODO: sanity checks */
2060 card->lancmd_timeout = value;
2066 DEVICE_ATTR(lancmd_timeout, 0644, lcs_timeout_show, lcs_timeout_store);
2068 static struct attribute * lcs_attrs[] = {
2069 &dev_attr_portno.attr,
2070 &dev_attr_type.attr,
2071 &dev_attr_lancmd_timeout.attr,
2075 static struct attribute_group lcs_attr_group = {
2080 * lcs_probe_device is called on establishing a new ccwgroup_device.
2083 lcs_probe_device(struct ccwgroup_device *ccwgdev)
2085 struct lcs_card *card;
2088 if (!get_device(&ccwgdev->dev))
2091 LCS_DBF_TEXT(2, setup, "add_dev");
2092 card = lcs_alloc_card();
2094 PRINT_ERR("Allocation of lcs card failed\n");
2095 put_device(&ccwgdev->dev);
2098 ret = sysfs_create_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2100 PRINT_ERR("Creating attributes failed");
2101 lcs_free_card(card);
2102 put_device(&ccwgdev->dev);
2105 ccwgdev->dev.driver_data = card;
2106 ccwgdev->cdev[0]->handler = lcs_irq;
2107 ccwgdev->cdev[1]->handler = lcs_irq;
2112 lcs_register_netdev(struct ccwgroup_device *ccwgdev)
2114 struct lcs_card *card;
2116 LCS_DBF_TEXT(2, setup, "regnetdv");
2117 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2118 if (card->dev->reg_state != NETREG_UNINITIALIZED)
2120 SET_NETDEV_DEV(card->dev, &ccwgdev->dev);
2121 return register_netdev(card->dev);
2125 * lcs_new_device will be called by setting the group device online.
2129 lcs_new_device(struct ccwgroup_device *ccwgdev)
2131 struct lcs_card *card;
2132 struct net_device *dev=NULL;
2133 enum lcs_dev_states recover_state;
2136 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2140 LCS_DBF_TEXT(2, setup, "newdev");
2141 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2142 card->read.ccwdev = ccwgdev->cdev[0];
2143 card->write.ccwdev = ccwgdev->cdev[1];
2145 recover_state = card->state;
2146 ccw_device_set_online(card->read.ccwdev);
2147 ccw_device_set_online(card->write.ccwdev);
2149 LCS_DBF_TEXT(3, setup, "lcsnewdv");
2151 lcs_setup_card(card);
2152 rc = lcs_detect(card);
2154 LCS_DBF_TEXT(2, setup, "dtctfail");
2155 PRINT_WARN("Detection of LCS card failed with return code "
2156 "%d (0x%x)\n", rc, rc);
2161 LCS_DBF_TEXT(2, setup, "samedev");
2162 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2165 switch (card->lan_type) {
2166 #ifdef CONFIG_NET_ETHERNET
2167 case LCS_FRAME_TYPE_ENET:
2168 card->lan_type_trans = eth_type_trans;
2169 dev = alloc_etherdev(0);
2173 case LCS_FRAME_TYPE_TR:
2174 card->lan_type_trans = tr_type_trans;
2175 dev = alloc_trdev(0);
2179 case LCS_FRAME_TYPE_FDDI:
2180 card->lan_type_trans = fddi_type_trans;
2181 dev = alloc_fddidev(0);
2185 LCS_DBF_TEXT(3, setup, "errinit");
2186 PRINT_ERR("LCS: Initialization failed\n");
2187 PRINT_ERR("LCS: No device found!\n");
2193 card->dev->priv = card;
2194 card->dev->open = lcs_open_device;
2195 card->dev->stop = lcs_stop_device;
2196 card->dev->hard_start_xmit = lcs_start_xmit;
2197 card->dev->get_stats = lcs_getstats;
2198 SET_MODULE_OWNER(dev);
2199 memcpy(card->dev->dev_addr, card->mac, LCS_MAC_LENGTH);
2200 #ifdef CONFIG_IP_MULTICAST
2201 if (!lcs_check_multicast_support(card))
2202 card->dev->set_multicast_list = lcs_set_multicast_list;
2205 lcs_set_allowed_threads(card,0xffffffff);
2206 if (recover_state == DEV_STATE_RECOVER) {
2207 lcs_set_multicast_list(card->dev);
2208 card->dev->flags |= IFF_UP;
2209 netif_wake_queue(card->dev);
2210 card->state = DEV_STATE_UP;
2215 if (lcs_register_netdev(ccwgdev) != 0)
2218 /* Print out supported assists: IPv6 */
2219 PRINT_INFO("LCS device %s %s IPv6 support\n", card->dev->name,
2220 (card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
2221 "with" : "without");
2222 /* Print out supported assist: Multicast */
2223 PRINT_INFO("LCS device %s %s Multicast support\n", card->dev->name,
2224 (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
2225 "with" : "without");
2229 ccw_device_set_offline(card->read.ccwdev);
2230 ccw_device_set_offline(card->write.ccwdev);
2235 * lcs_shutdown_device, called when setting the group device offline.
2238 lcs_shutdown_device(struct ccwgroup_device *ccwgdev)
2240 struct lcs_card *card;
2241 enum lcs_dev_states recover_state;
2244 LCS_DBF_TEXT(3, setup, "shtdndev");
2245 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2248 lcs_set_allowed_threads(card, 0);
2249 if (lcs_wait_for_threads(card, LCS_SET_MC_THREAD))
2250 return -ERESTARTSYS;
2251 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2252 recover_state = card->state;
2254 ret = lcs_stop_device(card->dev);
2255 ret = ccw_device_set_offline(card->read.ccwdev);
2256 ret = ccw_device_set_offline(card->write.ccwdev);
2257 if (recover_state == DEV_STATE_UP) {
2258 card->state = DEV_STATE_RECOVER;
2266 * lcs_remove_device, free buffers and card
2269 lcs_remove_device(struct ccwgroup_device *ccwgdev)
2271 struct lcs_card *card;
2273 card = (struct lcs_card *)ccwgdev->dev.driver_data;
2277 PRINT_INFO("Removing lcs group device ....\n");
2278 LCS_DBF_TEXT(3, setup, "remdev");
2279 LCS_DBF_HEX(3, setup, &card, sizeof(void*));
2280 if (ccwgdev->state == CCWGROUP_ONLINE) {
2281 lcs_shutdown_device(ccwgdev);
2284 unregister_netdev(card->dev);
2285 sysfs_remove_group(&ccwgdev->dev.kobj, &lcs_attr_group);
2286 lcs_cleanup_card(card);
2287 lcs_free_card(card);
2288 put_device(&ccwgdev->dev);
2292 * LCS ccwgroup driver registration
2294 static struct ccwgroup_driver lcs_group_driver = {
2295 .owner = THIS_MODULE,
2298 .driver_id = 0xD3C3E2,
2299 .probe = lcs_probe_device,
2300 .remove = lcs_remove_device,
2301 .set_online = lcs_new_device,
2302 .set_offline = lcs_shutdown_device,
2306 * LCS Module/Kernel initialization function
2309 __init lcs_init_module(void)
2313 PRINT_INFO("Loading %s\n",version);
2314 rc = lcs_register_debug_facility();
2315 LCS_DBF_TEXT(0, setup, "lcsinit");
2317 PRINT_ERR("Initialization failed\n");
2321 rc = register_cu3088_discipline(&lcs_group_driver);
2323 PRINT_ERR("Initialization failed\n");
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 <fpavlic@de.ibm.com>");
2346 MODULE_LICENSE("GPL");