lcs.c 46.2 KB
Newer Older
1 2 3 4 5
/*
 *  linux/drivers/s390/net/lcs.c
 *
 *  Linux for S/390 Lan Channel Station Network Driver
 *
6 7 8 9 10 11 12 13
 *  Copyright (C)  1999-2001 IBM Deutschland Entwicklung GmbH,
 *			     IBM Corporation
 *    Author(s): Original Code written by
 *			  DJ Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
 *		 Rewritten by
 *			  Frank Pavlic (pavlic@de.ibm.com) and
 *		 	  Martin Schwidefsky <schwidefsky@de.ibm.com>
 *
14
 *    $Revision: 1.42 $	 $Date: 2002/12/09 13:55:28 $
15 16 17 18 19 20 21 22
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
24 25 26 27 28 29 30 31 32
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/version.h>
#include <linux/module.h>
33
#include <linux/if.h>
34 35 36 37 38 39 40
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/trdevice.h>
#include <linux/fddidevice.h>
#include <linux/inetdevice.h>
#include <linux/in.h>
#include <linux/igmp.h>
41 42
#include <net/arp.h>
#include <net/ip.h>
43 44

#include <asm/debug.h>
45 46 47 48
#include <asm/idals.h>
#include <asm/timex.h>
#include <linux/device.h>
#include <asm/ccwgroup.h>
49

50 51
#include "lcs.h"
#include "cu3088.h"
52 53


54 55 56
#if !defined(CONFIG_NET_ETHERNET) && \
    !defined(CONFIG_TR) && !defined(CONFIG_FDDI)
#error Cannot compile lcs.c without some net devices switched on.
57 58
#endif

59 60
/**
 * initialization string for output
61
 */
62
#define VERSION_LCS_C  "$Revision: 1.42 $"
63

64
static const char *version="LCS driver ("VERSION_LCS_C "/" VERSION_LCS_H ")";
65

66 67
/**
 * Some prototypes.
68
 */
69 70 71
static void lcs_tasklet(unsigned long);
static void lcs_start_kernel_thread(struct lcs_card *card);
static void lcs_get_frames_cb(struct lcs_channel *, struct lcs_buffer *);
72

73 74
/**
 * Debug Facility Stuff
75
 */
76 77
static debug_info_t *lcs_dbf_setup;
static debug_info_t *lcs_dbf_trace;
78

79 80
/**
 *  LCS Debug Facility functions
81
 */
82 83 84 85 86 87 88
static void
lcs_unregister_debug_facility(void)
{
	if (lcs_dbf_setup)
		debug_unregister(lcs_dbf_setup);
	if (lcs_dbf_trace)
		debug_unregister(lcs_dbf_trace);
89 90
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
static int
lcs_register_debug_facility(void)
{
	lcs_dbf_setup = debug_register("lcs_setup", 1, 1, 8);
	lcs_dbf_trace = debug_register("lcs_trace", 1, 2, 8);
	if (lcs_dbf_setup == NULL || lcs_dbf_trace == NULL) {
		PRINT_ERR("Not enough memory for debug facility.\n");
		lcs_unregister_debug_facility();
		return -ENOMEM;
	}
	debug_register_view(lcs_dbf_setup, &debug_hex_ascii_view);
	debug_set_level(lcs_dbf_setup, 5);
	debug_register_view(lcs_dbf_trace, &debug_hex_ascii_view);
	debug_set_level(lcs_dbf_trace, 3);
	return 0;
106 107
}

108 109 110 111 112
/**
 * Allocate io buffers.
 */
static int
lcs_alloc_channel(struct lcs_channel *channel)
113
{
114
	int cnt;
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	LCS_DBF_TEXT(3, setup, "ichalloc");
	for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
		/* alloc memory fo iobuffer */
		channel->iob[cnt].data = (void *)
			kmalloc(LCS_IOBUFFERSIZE, GFP_DMA | GFP_KERNEL);
		if (channel->iob[cnt].data == NULL)
			break;
		memset(channel->iob[cnt].data, 0, LCS_IOBUFFERSIZE);
		channel->iob[cnt].state = BUF_STATE_EMPTY;
	}
	if (cnt < LCS_NUM_BUFFS) {
		/* Not all io buffers could be allocated. */
		LCS_DBF_TEXT(3, setup, "echalloc");
		while (cnt-- > 0)
			kfree(channel->iob[cnt].data);
		return -ENOMEM;
132
	}
133
	return 0;
134 135
}

136 137 138
/**
 * Free io buffers.
 */
139
static void
140
lcs_free_channel(struct lcs_channel *channel)
141
{
142
	int cnt;
143

144 145 146
	LCS_DBF_TEXT(3, setup, "ichfree");
	for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++)
		kfree(channel->iob[cnt].data);
147 148
}

149 150 151 152 153
/**
 * LCS alloc memory for card and channels
 */
static struct lcs_card *
lcs_alloc_card(void)
154
{
155
	struct lcs_card *card;
156

157 158 159 160 161 162 163
	LCS_DBF_TEXT(3, setup, "alloclcs");
	card = kmalloc(sizeof(struct lcs_card), GFP_KERNEL | GFP_DMA);
	if (card == NULL)
		return NULL;
	memset(card, 0, sizeof(struct lcs_card));
	card->lan_type = LCS_FRAME_TYPE_AUTO;
	return card;
164 165
}

166 167 168 169 170
/**
 * LCS free memory for card and channels.
 */
static void
lcs_free_card(struct lcs_card *card)
171
{
172 173
	LCS_DBF_TEXT(2, setup, "remcard");
	kfree(card);
174 175
}

176 177
/*
 * Setup read channel.
178
 */
179 180
static void
lcs_setup_read_ccws(struct lcs_card *card)
181
{
182
	int cnt;
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	LCS_DBF_TEXT(2, setup, "ireadccw");
	/* Setup read ccws. */
	memset(card->read.ccws, 0, sizeof (struct ccw1) * (LCS_NUM_BUFFS + 1));
	for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
		card->read.ccws[cnt].cmd_code = LCS_CCW_READ;
		card->read.ccws[cnt].count = LCS_IOBUFFERSIZE;
		card->read.ccws[cnt].flags =
			CCW_FLAG_CC | CCW_FLAG_SLI | CCW_FLAG_PCI;
		/*
		 * Note: we have allocated the buffer with GFP_DMA, so
		 * we do not need to do set_normalized_cda.
		 */
		card->read.ccws[cnt].cda =
			(__u32) __pa(card->read.iob[cnt].data);
		((struct lcs_header *)
		 card->read.iob[cnt].data)->offset = LCS_ILLEGAL_OFFSET;
		card->read.iob[cnt].callback = lcs_get_frames_cb;
		card->read.iob[cnt].state = BUF_STATE_READY;
		card->read.iob[cnt].count = LCS_IOBUFFERSIZE;
	}
	card->read.ccws[0].flags &= ~CCW_FLAG_PCI;
	card->read.ccws[LCS_NUM_BUFFS - 1].flags &= ~CCW_FLAG_PCI;
	card->read.ccws[LCS_NUM_BUFFS - 1].flags |= CCW_FLAG_SUSPEND;
	/* Last ccw is a tic (transfer in channel). */
	card->read.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
	card->read.ccws[LCS_NUM_BUFFS].cda =
		(__u32) __pa(card->read.ccws);
	/* Setg initial state of the read channel. */
	card->read.state = CH_STATE_INIT;

	card->read.io_idx = 0;
	card->read.buf_idx = 0;
}
217

218 219
static int
lcs_setup_read(struct lcs_card *card)
220
{
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	int rc;

	LCS_DBF_TEXT(3, setup, "readirq");
	/* Allocate io buffers for the read channel. */
	rc = lcs_alloc_channel(&card->read);
	if (rc){
		LCS_DBF_TEXT(3, setup, "iccwerr");
		return rc;
	}
	lcs_setup_read_ccws(card);
	/* Initialize read channel tasklet. */
	card->read.irq_tasklet.data = (unsigned long) &card->read;
	card->read.irq_tasklet.func = lcs_tasklet;
	/* Initialize waitqueue. */
	init_waitqueue_head(&card->read.wait_q);
	return 0;
237 238
}

239 240 241 242 243
/*
 * Setup write channel.
 */
static void
lcs_setup_write_ccws(struct lcs_card *card)
244
{
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	int cnt;

	LCS_DBF_TEXT(2, setup, "iwritccw");
	/* Setup write ccws. */
	memset(card->write.ccws, 0, sizeof(struct ccw1) * LCS_NUM_BUFFS + 1);
	for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
		card->write.ccws[cnt].cmd_code = LCS_CCW_WRITE;
		card->write.ccws[cnt].count = 0;
		card->write.ccws[cnt].flags =
			CCW_FLAG_SUSPEND | CCW_FLAG_CC | CCW_FLAG_SLI;
		/*
		 * Note: we have allocated the buffer with GFP_DMA, so
		 * we do not need to do set_normalized_cda.
		 */
		card->write.ccws[cnt].cda =
			(__u32) __pa(card->write.iob[cnt].data);
	}
	/* Last ccw is a tic (transfer in channel). */
	card->write.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
	card->write.ccws[LCS_NUM_BUFFS].cda =
		(__u32) __pa(card->write.ccws);
	/* Set initial state of the write channel. */
	card->read.state = CH_STATE_INIT;
268

269 270
	card->write.io_idx = 0;
	card->write.buf_idx = 0;
271 272 273
}

static int
274
lcs_setup_write(struct lcs_card *card)
275
{
276
	int rc;
277

278 279 280 281 282 283
	LCS_DBF_TEXT(3, setup, "writeirq");
	/* Allocate io buffers for the write channel. */
	rc = lcs_alloc_channel(&card->write);
	if (rc) {
		LCS_DBF_TEXT(3, setup, "iccwerr");
		return rc;
284
	}
285 286 287 288 289 290 291
	lcs_setup_write_ccws(card);
	/* Initialize write channel tasklet. */
	card->write.irq_tasklet.data = (unsigned long) &card->write;
	card->write.irq_tasklet.func = lcs_tasklet;
	/* Initialize waitqueue. */
	init_waitqueue_head(&card->write.wait_q);
	return 0;
292 293
}

294 295 296
/*
 * Cleanup channel.
 */
297
static void
298
lcs_cleanup_channel(struct lcs_channel *channel)
299
{
300 301 302 303 304 305
	LCS_DBF_TEXT(3, setup, "cleanch");
	/* Kill write channel tasklets. */
	tasklet_kill(&channel->irq_tasklet);
	/* Free channel buffers. */
	lcs_free_channel(channel);
}
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
/**
 * Initialize channels,card and state machines.
 */
static int
lcs_setup_card(struct lcs_card *card)
{
	int rc;

	LCS_DBF_TEXT(3, setup, "initcard");

	rc = lcs_setup_read(card);
	if (rc) {
		PRINT_ERR("Could not initialize read channel\n");
		return rc;
	}
	rc = lcs_setup_write(card);
	if (rc) {
		PRINT_ERR("Could not initialize write channel\n");
		lcs_cleanup_channel(&card->read);
		return rc;
327
	}
328 329 330 331 332 333 334 335 336 337 338 339 340
	/* Set cards initial state. */
	card->state = DEV_STATE_DOWN;
	card->tx_buffer = NULL;
	card->tx_emitted = 0;

	/* Initialize kernel thread task used for LGW commands. */
	INIT_WORK(&card->kernel_thread_starter,
		  (void *)lcs_start_kernel_thread,card);
	card->thread_mask = 0;
	spin_lock_init(&card->lock);
	INIT_LIST_HEAD(&card->ipm_list);
	INIT_LIST_HEAD(&card->lancmd_waiters);
	return 0;
341 342
}

343 344 345
/**
 * Cleanup channels,card and state machines.
 */
346
static void
347
lcs_cleanup_card(struct lcs_card *card)
348
{
349 350
	struct list_head *l, *n;
	struct lcs_ipm_list *ipm_list;
351

352 353 354 355 356 357 358
	LCS_DBF_TEXT(3, setup, "cleancrd");
#ifdef	CONFIG_IP_MULTICAST
	/* Free multicast list. */
	list_for_each_safe(l, n, &card->ipm_list) {
		ipm_list = list_entry(l, struct lcs_ipm_list, list);
		list_del(&ipm_list->list);
		kfree(ipm_list);
359
	}
360 361 362 363
#endif
	/* Cleanup channels. */
	lcs_cleanup_channel(&card->write);
	lcs_cleanup_channel(&card->read);
364 365
}

366 367 368 369 370
/**
 * Start channel.
 */
static int
lcs_start_channel(struct lcs_channel *channel)
371
{
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
	char dbf_text[15];
	unsigned long flags;
	int rc;

	sprintf(dbf_text,"ssch%s", channel->ccwdev->dev.bus_id);
	LCS_DBF_TEXT(4, trace, dbf_text);
	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
	rc = ccw_device_start(channel->ccwdev,
			      channel->ccws + channel->io_idx, 0, 0,
			      DOIO_DENY_PREFETCH | DOIO_ALLOW_SUSPEND);
	if (rc == 0)
		channel->state = CH_STATE_RUNNING;
	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
	if (rc) {
		sprintf(dbf_text,"essc%s", channel->ccwdev->dev.bus_id);
		LCS_DBF_TEXT(4, trace, dbf_text);
		PRINT_ERR("Error in starting channel, rc=%d!\n", rc);
	}
	return rc;
391 392
}

393 394
/**
 * Stop channel.
395
 */
396 397
static int
lcs_stop_channel(struct lcs_channel *channel)
398
{
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
	char dbf_text[15];
	unsigned long flags;
	int rc;

	if (channel->state == CH_STATE_STOPPED)
		return 0;
	sprintf(dbf_text,"hsch%s", channel->ccwdev->dev.bus_id);
	LCS_DBF_TEXT(4, trace, dbf_text);
	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
	rc = ccw_device_halt(channel->ccwdev, (addr_t) channel);
	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
	if (rc) {
		sprintf(dbf_text,"ehsc%s", channel->ccwdev->dev.bus_id);
		LCS_DBF_TEXT(4, trace, dbf_text);
		return rc;
414
	}
415 416 417
	/* Asynchronous halt initialted. Wait for its completion. */
	wait_event(channel->wait_q, (channel->state == CH_STATE_HALTED));
	return 0;
418 419
}

420 421
/**
 * start read and write channel
422 423
 */
static int
424
lcs_start_channels(struct lcs_card *card)
425
{
426 427 428 429 430 431 432 433 434 435 436 437
	int rc;

	LCS_DBF_TEXT(2, trace, "chstart");
	/* start read channel */
	rc = lcs_start_channel(&card->read);
	if (rc)
		return rc;
	/* start write channel */
	rc = lcs_start_channel(&card->write);
	if (rc)
		lcs_stop_channel(&card->read);
	return rc;
438 439
}

440 441 442 443 444
/**
 * stop read and write channel
 */
static int
lcs_stop_channels(struct lcs_card *card)
445
{
446 447 448 449
	LCS_DBF_TEXT(2, trace, "chhalt");
	lcs_stop_channel(&card->read);
	lcs_stop_channel(&card->write);
	return 0;
450 451
}

452 453 454 455 456
/**
 * Get empty buffer.
 */
static struct lcs_buffer *
__lcs_get_buffer(struct lcs_channel *channel)
457
{
458 459 460 461 462 463 464
	int index;

	index = channel->io_idx;
	do {
		if (channel->iob[index].state == BUF_STATE_EMPTY) {
			channel->iob[index].state = BUF_STATE_LOCKED;
			return channel->iob + index;
465
		}
466 467 468
		index = (index + 1) & (LCS_NUM_BUFFS - 1);
	} while (index != channel->io_idx);
	return NULL;
469 470
}

471 472
static struct lcs_buffer *
lcs_get_buffer(struct lcs_channel *channel)
473
{
474 475 476 477 478 479 480
	struct lcs_buffer *buffer;
	unsigned long flags;

	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
	buffer = __lcs_get_buffer(channel);
	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
	return buffer;
481 482
}

483 484 485
/**
 * Resume channel program if the channel is suspended.
 */
486
static int
487
__lcs_resume_channel(struct lcs_channel *channel)
488
{
489 490
	char dbf_text[15];
	int rc;
491

492 493 494 495 496 497 498 499 500 501 502 503 504 505
	if (channel->state != CH_STATE_SUSPENDED)
		return 0;
	if (channel->ccws[channel->io_idx].flags & CCW_FLAG_SUSPEND)
		return 0;
	sprintf(dbf_text,"rsch%s", channel->ccwdev->dev.bus_id);
	LCS_DBF_TEXT(4, trace, dbf_text);
	rc = ccw_device_resume(channel->ccwdev);
	if (rc) {
		sprintf(dbf_text,"ersc%s", channel->ccwdev->dev.bus_id);
		LCS_DBF_TEXT(4, trace, dbf_text);
		PRINT_ERR("Error in lcs_resume_channel: rc=%d\n",rc);
	} else
		channel->state = CH_STATE_RUNNING;
	return rc;
506

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
}

/**
 * Make a buffer ready for processing.
 */
static inline void
__lcs_ready_buffer_bits(struct lcs_channel *channel, int index)
{
	int prev, next;

	prev = (index - 1) & (LCS_NUM_BUFFS - 1);
	next = (index + 1) & (LCS_NUM_BUFFS - 1);
	/* Check if we may clear the suspend bit of this buffer. */
	if (channel->ccws[next].flags & CCW_FLAG_SUSPEND) {
		/* Check if we have to set the PCI bit. */
		if (!(channel->ccws[prev].flags & CCW_FLAG_SUSPEND))
			/* Suspend bit of the previous buffer is not set. */
			channel->ccws[index].flags |= CCW_FLAG_PCI;
		/* Suspend bit of the next buffer is set. */
		channel->ccws[index].flags &= ~CCW_FLAG_SUSPEND;
527
	}
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
}

static int
lcs_ready_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
{
	unsigned long flags;
	int index, rc;

	if (buffer->state != BUF_STATE_LOCKED &&
	    buffer->state != BUF_STATE_PROCESSED)
		BUG();
	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
	buffer->state = BUF_STATE_READY;
	index = buffer - channel->iob;
	/* Set length. */
	channel->ccws[index].count = buffer->count;
	/* Check relevant PCI/suspend bits. */
	__lcs_ready_buffer_bits(channel, index);
	rc = __lcs_resume_channel(channel);
	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
	return rc;
}
550

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
/**
 * Mark the buffer as processed. Take care of the suspend bit
 * of the previous buffer. This function is called from
 * interrupt context, so the lock must not be taken.
 */
static int
__lcs_processed_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
{
	int index, prevprev, prev, next;

	if (buffer->state != BUF_STATE_READY)
		BUG();
	buffer->state = BUF_STATE_PROCESSED;
	index = buffer - channel->iob;
	prevprev = (index - 1) & (LCS_NUM_BUFFS - 1);
	prev = (index - 1) & (LCS_NUM_BUFFS - 1);
	next = (index + 1) & (LCS_NUM_BUFFS - 1);
	/* Set the suspend bit and clear the PCI bit of this buffer. */
	channel->ccws[index].flags |= CCW_FLAG_SUSPEND;
	channel->ccws[index].flags &= ~CCW_FLAG_PCI;
	/* Check the suspend bit of the previous buffer. */
	if (channel->iob[prev].state == BUF_STATE_READY) {
		/*
		 * Previous buffer is in state ready. It might have
		 * happened in lcs_ready_buffer that the suspend bit
		 * has not been cleared to avoid an endless loop.
		 * Do it now.
		 */
		__lcs_ready_buffer_bits(channel, prev);
	}
	/* Clear PCI bit of next buffer. */
	channel->ccws[next].flags &= ~CCW_FLAG_PCI;
	return __lcs_resume_channel(channel);
584 585
}

586 587
/**
 * Put a processed buffer back to state empty.
588
 */
589 590
static void
lcs_release_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
591
{
592
	unsigned long flags;
593

594 595 596 597 598 599 600
	if (buffer->state != BUF_STATE_LOCKED &&
	    buffer->state != BUF_STATE_PROCESSED)
		BUG();
	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
	buffer->state = BUF_STATE_EMPTY;
	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
}
601

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
/**
 * Get buffer for a lan command.
 */
static struct lcs_buffer *
lcs_get_lancmd(struct lcs_card *card, int count)
{
	struct lcs_buffer *buffer;
	struct lcs_cmd *cmd;

	/* Get buffer and wait if none is available. */
	wait_event(card->write.wait_q,
		   ((buffer = lcs_get_buffer(&card->write)) != NULL));
	count += sizeof(struct lcs_header);
	*(__u16 *)(buffer->data + count) = 0;
	buffer->count = count + sizeof(__u16);
	buffer->callback = lcs_release_buffer;
	cmd = (struct lcs_cmd *) buffer->data;
	cmd->offset = count;
	cmd->type = LCS_FRAME_TYPE_CONTROL;
	cmd->slot = 0;
	return buffer;
}

/**
 * Notifier function for lancmd replies. Called from read irq.
 */
static void
lcs_notify_lancmd_waiters(struct lcs_card *card, struct lcs_cmd *cmd)
{
	struct list_head *l, *n;
	struct lcs_reply *reply;

	spin_lock(&card->lock);
	list_for_each_safe(l, n, &card->lancmd_waiters) {
		reply = list_entry(l, struct lcs_reply, list);
		if (reply->sequence_no == cmd->sequence_no) {
			list_del(&reply->list);
			if (reply->callback != NULL)
				reply->callback(card, cmd);
			reply->received = 1;
			reply->rc = cmd->return_code;
			wake_up(&reply->wait_q);
			break;
		}
646
	}
647
	spin_unlock(&card->lock);
648 649
}

650 651 652 653 654
/**
 * Emit buffer of a lan comand.
 */
void
lcs_lancmd_timeout(unsigned long data)
655
{
656 657 658 659 660 661 662
	struct lcs_reply *reply;

	reply = (struct lcs_reply *) data;
	list_del(&reply->list);
	reply->received = 1;
	reply->rc = -ETIME;
	wake_up(&reply->wait_q);
663 664 665
}

static int
666 667
lcs_send_lancmd(struct lcs_card *card, struct lcs_buffer *buffer,
		void (*reply_callback)(struct lcs_card *, struct lcs_cmd *))
668
{
669 670 671
	struct lcs_reply reply;
	struct lcs_cmd *cmd;
	struct timer_list timer;
672 673
	int rc;

674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
	cmd = (struct lcs_cmd *) buffer->data;
	cmd->sequence_no = ++card->sequence_no;
	cmd->return_code = 0;
	reply.sequence_no = cmd->sequence_no;
	reply.callback = reply_callback;
	reply.received = 0;
	reply.rc = 0;
	init_waitqueue_head(&reply.wait_q);
	spin_lock(&card->lock);
	list_add_tail(&reply.list, &card->lancmd_waiters);
	spin_unlock(&card->lock);
	buffer->callback = lcs_release_buffer;
	rc = lcs_ready_buffer(&card->write, buffer);
	if (rc)
		return rc;
	init_timer(&timer);
	timer.function = lcs_lancmd_timeout;
	timer.data = (unsigned long) &reply;
	timer.expires = jiffies + HZ*5;
	add_timer(&timer);
	wait_event(reply.wait_q, reply.received);
	del_timer(&timer);
	return reply.rc ? -EIO : 0;
}

/**
 * LCS startup command
 */
static int
lcs_send_startup(struct lcs_card *card, __u8 initiator)
{
	struct lcs_buffer *buffer;
	struct lcs_cmd *cmd;
707

708 709 710 711 712 713 714
	LCS_DBF_TEXT(2, trace, "startup");
	buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
	cmd = (struct lcs_cmd *) buffer->data;
	cmd->cmd_code = LCS_CMD_STARTUP;
	cmd->initiator = initiator;
	cmd->cmd.lcs_startup.buff_size = LCS_IOBUFFERSIZE;
	return lcs_send_lancmd(card, buffer, NULL);
715 716
}

717 718
/**
 * LCS shutdown command
719 720
 */
static int
721
lcs_send_shutdown(struct lcs_card *card)
722
{
723 724
	struct lcs_buffer *buffer;
	struct lcs_cmd *cmd;
725

726 727 728 729 730 731 732
	LCS_DBF_TEXT(2, trace, "shutdown");
	buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
	cmd = (struct lcs_cmd *) buffer->data;
	cmd->cmd_code = LCS_CMD_SHUTDOWN;
	cmd->initiator = LCS_INITIATOR_TCPIP;
	return lcs_send_lancmd(card, buffer, NULL);
}
733

734 735 736
/**
 * LCS lanstat command
 */
737
static void
738
__lcs_lanstat_cb(struct lcs_card *card, struct lcs_cmd *cmd)
739
{
740
	memcpy(card->mac, cmd->cmd.lcs_lanstat_cmd.mac_addr, LCS_MAC_LENGTH);
741 742 743
}

static int
744
lcs_send_lanstat(struct lcs_card *card)
745
{
746 747 748 749 750 751 752 753 754 755 756 757
	struct lcs_buffer *buffer;
	struct lcs_cmd *cmd;

	LCS_DBF_TEXT(2, trace, "cmdstat");
	buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
	cmd = (struct lcs_cmd *) buffer->data;
	/* Setup lanstat command. */
	cmd->cmd_code = LCS_CMD_LANSTAT;
	cmd->initiator = LCS_INITIATOR_TCPIP;
	cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
	cmd->cmd.lcs_std_cmd.portno = card->portno;
	return lcs_send_lancmd(card, buffer, __lcs_lanstat_cb);
758 759
}

760 761 762 763 764
/**
 * send stoplan command
 */
static int
lcs_send_stoplan(struct lcs_card *card, __u8 initiator)
765
{
766 767 768 769 770 771 772 773 774 775 776
	struct lcs_buffer *buffer;
	struct lcs_cmd *cmd;

	LCS_DBF_TEXT(2, trace, "cmdstpln");
	buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
	cmd = (struct lcs_cmd *) buffer->data;
	cmd->cmd_code = LCS_CMD_STOPLAN;
	cmd->initiator = initiator;
	cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
	cmd->cmd.lcs_std_cmd.portno = card->portno;
	return lcs_send_lancmd(card, buffer, NULL);
777 778
}

779 780 781 782 783
/**
 * send startlan command
 */
static void
__lcs_send_startlan_cb(struct lcs_card *card, struct lcs_cmd *cmd)
784
{
785 786
	card->lan_type = cmd->cmd.lcs_std_cmd.lan_type;
	card->portno = cmd->cmd.lcs_std_cmd.portno;
787 788
}

789 790
static int
lcs_send_startlan(struct lcs_card *card, __u8 initiator)
791
{
792 793 794 795 796 797 798 799 800 801 802
	struct lcs_buffer *buffer;
	struct lcs_cmd *cmd;

	LCS_DBF_TEXT(2, trace, "cmdstpln");
	buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
	cmd = (struct lcs_cmd *) buffer->data;
	cmd->cmd_code = LCS_CMD_STARTLAN;
	cmd->initiator = initiator;
	cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
	cmd->cmd.lcs_std_cmd.portno = card->portno;
	return lcs_send_lancmd(card, buffer, __lcs_send_startlan_cb);
803 804
}

805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
#ifdef CONFIG_IP_MULTICAST
/**
 * send setipm command (Multicast)
 */
static int
lcs_send_setipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
{
	struct lcs_buffer *buffer;
	struct lcs_cmd *cmd;

	LCS_DBF_TEXT(2, trace, "cmdsetim");
	buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
	cmd = (struct lcs_cmd *) buffer->data;
	cmd->cmd_code = LCS_CMD_SETIPM;
	cmd->initiator = LCS_INITIATOR_TCPIP;
	cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
	cmd->cmd.lcs_qipassist.portno = card->portno;
	cmd->cmd.lcs_qipassist.version = 4;
	cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
	memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
	       &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
	return lcs_send_lancmd(card, buffer, NULL);
}

/**
 * send delipm command (Multicast)
 */
static int
lcs_send_delipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
{
	struct lcs_buffer *buffer;
	struct lcs_cmd *cmd;

	LCS_DBF_TEXT(2, trace, "cmddelim");
	buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
	cmd = (struct lcs_cmd *) buffer->data;
	cmd->cmd_code = LCS_CMD_DELIPM;
	cmd->initiator = LCS_INITIATOR_TCPIP;
	cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
	cmd->cmd.lcs_qipassist.portno = card->portno;
	cmd->cmd.lcs_qipassist.version = 4;
	cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
	memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
	       &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
	return lcs_send_lancmd(card, buffer, NULL);
}

/**
 * check if multicast is supported by LCS
 */
855
static void
856
__lcs_check_multicast_cb(struct lcs_card *card, struct lcs_cmd *cmd)
857
{
858 859 860 861
	card->ip_assists_supported =
		cmd->cmd.lcs_qipassist.ip_assists_supported;
	card->ip_assists_enabled =
		cmd->cmd.lcs_qipassist.ip_assists_enabled;
862 863 864
}

static int
865
lcs_check_multicast_support(struct lcs_card *card)
866
{
867 868 869
	struct lcs_buffer *buffer;
	struct lcs_cmd *cmd;
	int rc;
870

871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
	LCS_DBF_TEXT(2, trace, "cmdqipa");
	/* Send query ipassist. */
	buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
	cmd = (struct lcs_cmd *) buffer->data;
	cmd->cmd_code = LCS_CMD_QIPASSIST;
	cmd->initiator = LCS_INITIATOR_TCPIP;
	cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
	cmd->cmd.lcs_qipassist.portno = card->portno;
	cmd->cmd.lcs_qipassist.version = 4;
	cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
	rc = lcs_send_lancmd(card, buffer, __lcs_check_multicast_cb);
	if (rc != 0) {
		PRINT_ERR("Query IPAssist failed. Assuming unsupported!\n");
		return -EOPNOTSUPP;
	}
	/* Print out supported assists: IPv6 */
	PRINT_INFO("LCS device %s %s IPv6 support\n", card->dev->name,
		   (card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
		   "with" : "without");
	/* Print out supported assist: Multicast */
	PRINT_INFO("LCS device %s %s Multicast support\n", card->dev->name,
		   (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
		   "with" : "without");
	if (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT)
		return 0;
	return -EOPNOTSUPP;
897 898
}

899 900
/**
 * set or del multicast address on LCS card
901 902
 */
static int
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
lcs_fix_multicast_list(void *data)
{
	struct list_head *l, *n;
	struct lcs_ipm_list *ipm;
	struct lcs_card *card;

	card = (struct lcs_card *) data;

	daemonize();
	LCS_DBF_TEXT(5, trace, "fixipm");
	spin_lock(&card->lock);
	list_for_each_safe(l, n, &card->ipm_list) {
		ipm = list_entry(l, struct lcs_ipm_list, list);
		switch (ipm->ipm_state) {
		case LCS_IPM_STATE_SET_REQUIRED:
			if (lcs_send_setipm(card, ipm))
				PRINT_INFO("Adding multicast address failed."
					   "Table possibly full!\n");
			else
				ipm->ipm_state = LCS_IPM_STATE_ON_CARD;
			break;
		case LCS_IPM_STATE_DEL_REQUIRED:
			lcs_send_delipm(card, ipm);
			list_del(&ipm->list);
			kfree(ipm);
			break;
		case LCS_IPM_STATE_ON_CARD:
			break;
		}
	}
	if (card->state == DEV_STATE_UP)
		netif_wake_queue(card->dev);
	spin_unlock(&card->lock);
	return 0;
}
938

939 940 941 942 943 944 945 946
/**
 * get mac address for the relevant Multicast address
 */
static void
lcs_get_mac_for_ipm(__u32 ipm, char *mac, struct net_device *dev)
{
	if (dev->type == ARPHRD_IEEE802_TR)
		ip_tr_mc_map(ipm, mac);
947
	else
948
		ip_eth_mc_map(ipm, mac);
949 950
}

951 952 953
/**
 * function called by net device to handle multicast address relevant things
 */
954
static void
955
lcs_set_multicast_list(struct net_device *dev)
956
{
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	char buf[MAX_ADDR_LEN];
	struct list_head *l;
	struct ip_mc_list *im4;
	struct in_device *in4_dev;
	struct lcs_ipm_list *ipm, *tmp;
	struct lcs_card *card;

	LCS_DBF_TEXT(5, trace, "setmulti");
	in4_dev = in_dev_get(dev);
	if (in4_dev == NULL)
		return;
	read_lock(&in4_dev->lock);
	card = (struct lcs_card *) dev->priv;
	spin_lock(&card->lock);
	/* Check for multicast addresses to be removed. */
	list_for_each(l, &card->ipm_list) {
		ipm = list_entry(l, struct lcs_ipm_list, list);
		for (im4 = in4_dev->mc_list; im4 != NULL; im4 = im4->next) {
			lcs_get_mac_for_ipm(im4->multiaddr, buf, dev);
			if (memcmp(buf, &ipm->ipm.mac_addr,
				   LCS_MAC_LENGTH) == 0 &&
			    ipm->ipm.ip_addr == im4->multiaddr)
				break;
		}
		if (im4 == NULL)
			ipm->ipm_state = LCS_IPM_STATE_DEL_REQUIRED;
	}
	/* Check for multicast addresses to be added. */
	for (im4 = in4_dev->mc_list; im4; im4 = im4->next) {
		lcs_get_mac_for_ipm(im4->multiaddr, buf, dev);
		ipm = NULL;
		list_for_each(l, &card->ipm_list) {
			tmp = list_entry(l, struct lcs_ipm_list, list);
			if (memcmp(buf, &tmp->ipm.mac_addr,
				   LCS_MAC_LENGTH) == 0 &&
			    tmp->ipm.ip_addr == im4->multiaddr) {
				ipm = tmp;
				break;
			}
		}
		if (ipm != NULL)
			continue;	/* Address already in list. */
		ipm = (struct lcs_ipm_list *)
			kmalloc(sizeof(struct lcs_ipm_list), GFP_ATOMIC);
		if (ipm == NULL) {
			PRINT_INFO("Not enough memory to add "
				   "new multicast entry!\n");
			break;
		}
		memset(ipm, 0, sizeof(struct lcs_ipm_list));
		memcpy(&ipm->ipm.mac_addr, buf, LCS_MAC_LENGTH);
		ipm->ipm.ip_addr = im4->multiaddr;
		ipm->ipm_state = LCS_IPM_STATE_SET_REQUIRED;
		list_add(&ipm->list, &card->ipm_list);
	}
	spin_unlock(&card->lock);
	read_unlock(&in4_dev->lock);
	set_bit(3, &card->thread_mask);
	schedule_work(&card->kernel_thread_starter);
1016 1017
}

1018 1019 1020 1021 1022
#endif /* CONFIG_IP_MULTICAST */

/**
 * IRQ Handler for LCS channels
 */
1023
static void
1024
lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
1025
{
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	char dbf_text[15];
	struct lcs_card *card;
	struct lcs_channel *channel;
	int index;

	card = (struct lcs_card *)cdev->dev.driver_data;
	if (card->read.ccwdev == cdev)
		channel = &card->read;
	else
		channel = &card->write;

	sprintf(dbf_text, "Rint%s", cdev->dev.bus_id);
	LCS_DBF_TEXT(5, trace, dbf_text);
	sprintf(dbf_text, "%4x%4x", irb->scsw.cstat, irb->scsw.dstat);
	LCS_DBF_TEXT(5, trace, dbf_text);

	/* How far in the ccw chain have we processed? */
	if (channel->state != CH_STATE_INIT) {
		index = (struct ccw1 *) __va((addr_t) irb->scsw.cpa) 
			- channel->ccws;
		if ((irb->scsw.actl & SCSW_ACTL_SUSPENDED) ||
		    (irb->scsw.cstat | SCHN_STAT_PCI))
			/* Bloody io subsystem tells us lies about cpa... */
			index = (index - 1) & (LCS_NUM_BUFFS - 1);
		while (channel->io_idx != index) {
			__lcs_processed_buffer(channel,
					       channel->iob + channel->io_idx);
			channel->io_idx =
				(channel->io_idx + 1) & (LCS_NUM_BUFFS - 1);
		}
1056
	}
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072

	if ((irb->scsw.dstat & DEV_STAT_DEV_END) ||
	    (irb->scsw.dstat & DEV_STAT_CHN_END) ||
	    (irb->scsw.dstat & DEV_STAT_UNIT_CHECK))
		/* Mark channel as stopped. */
		channel->state = CH_STATE_STOPPED;
	else if (irb->scsw.actl & SCSW_ACTL_SUSPENDED)
		/* CCW execution stopped on a suspend bit. */
		channel->state = CH_STATE_SUSPENDED;

	if (irb->scsw.fctl & SCSW_FCTL_HALT_FUNC)
		/* The channel has been stopped by halt_IO. */
		channel->state = CH_STATE_HALTED;

	/* Do the rest in the tasklet. */
	tasklet_schedule(&channel->irq_tasklet);
1073 1074
}

1075 1076 1077 1078 1079
/**
 * Tasklet for IRQ handler
 */
static void
lcs_tasklet(unsigned long data)
1080
{
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
	char dbf_text[15];
	unsigned long flags;
	struct lcs_channel *channel;
	struct lcs_buffer *iob;
	int buf_idx, io_idx;
	int rc;

	channel = (struct lcs_channel *) data;
	sprintf(dbf_text, "tlet%s", channel->ccwdev->dev.bus_id);
	LCS_DBF_TEXT(5, trace, dbf_text);
1091

1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
	/* Check for processed buffers. */
	iob = channel->iob;
	buf_idx = channel->buf_idx;
	io_idx = channel->io_idx;
	while (buf_idx != io_idx &&
	       iob[buf_idx].state == BUF_STATE_PROCESSED) {
		/* Do the callback thing. */
		if (iob[buf_idx].callback != NULL)
			iob[buf_idx].callback(channel, iob + buf_idx);
		buf_idx = (buf_idx + 1) & (LCS_NUM_BUFFS - 1);
	}
	channel->buf_idx = buf_idx;

	if (channel->state == CH_STATE_STOPPED)
		// FIXME: what if rc != 0 ??
		rc = lcs_start_channel(channel);
	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
	if (channel->state == CH_STATE_SUSPENDED &&
	    channel->iob[channel->io_idx].state == BUF_STATE_READY) {
		// FIXME: what if rc != 0 ??
		rc = __lcs_resume_channel(channel);
	}
	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
1115

1116 1117
	/* Something happened on the channel. Wake up waiters. */
	wake_up(&channel->wait_q);
1118 1119
}

1120 1121 1122
/**
 * Finish current tx buffer and make it ready for transmit.
 */
1123
static void
1124
__lcs_emit_txbuffer(struct lcs_card *card)
1125
{
1126 1127 1128 1129 1130
	*(__u16 *)(card->tx_buffer->data + card->tx_buffer->count) = 0;
	card->tx_buffer->count += 2;
	lcs_ready_buffer(&card->write, card->tx_buffer);
	card->tx_buffer = NULL;
	card->tx_emitted++;
1131 1132
}

1133 1134 1135 1136 1137
/**
 * Callback for finished tx buffers.
 */
static void
lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1138
{
1139
	struct lcs_card *card;
1140

1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
	/* Put buffer back to pool. */
	lcs_release_buffer(channel, buffer);
	card = (struct lcs_card *)
		((char *) channel - offsetof(struct lcs_card, write));
	spin_lock(&card->lock);
	card->tx_emitted--;
	if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
		/*
		 * Last running tx buffer has finished. Submit partially
		 * filled current buffer.
		 */
		__lcs_emit_txbuffer(card);
	spin_unlock(&card->lock);
1154 1155
}

1156 1157 1158
/**
 * Packet transmit function called by network stack
 */
1159
static int
1160 1161
__lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
		 struct net_device *dev)
1162
{
1163
	struct lcs_header *header;
1164

1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
	if (skb == NULL) {
		card->stats.tx_dropped++;
		card->stats.tx_errors++;
		return -EIO;
	}
	if (card->state != DEV_STATE_UP) {
		dst_link_failure(skb);
		dev_kfree_skb(skb);
		card->stats.tx_dropped++;
		card->stats.tx_errors++;
		card->stats.tx_carrier_errors++;
		return 0;
	}
	if (netif_queue_stopped(dev) ) {
		card->stats.tx_dropped++;
		return -EBUSY;
	}
	if (card->tx_buffer != NULL &&
	    card->tx_buffer->count + sizeof(struct lcs_header) +
	    skb->len + sizeof(u16) > LCS_IOBUFFERSIZE)
		/* skb too big for current tx buffer. */
		__lcs_emit_txbuffer(card);
	if (card->tx_buffer == NULL) {
		/* Get new tx buffer */
		card->tx_buffer = lcs_get_buffer(&card->write);
		if (card->tx_buffer == NULL) {
			netif_stop_queue(dev);
			card->stats.tx_dropped++;
			return -EBUSY;
		}
		card->tx_buffer->callback = lcs_txbuffer_cb;
		card->tx_buffer->count = 0;
	}
	header = (struct lcs_header *)
		(card->tx_buffer->data + card->tx_buffer->count);
	card->tx_buffer->count += skb->len + sizeof(struct lcs_header);
	header->offset = card->tx_buffer->count;
	header->type = card->lan_type;
	header->slot = card->portno;
	memcpy(header + 1, skb->data, skb->len);
	card->stats.tx_bytes += skb->len;
	card->stats.tx_packets++;
	dev_kfree_skb(skb);
	if (card->tx_emitted <= 0)
		/* If this is the first tx buffer emit it immediatly. */
		__lcs_emit_txbuffer(card);
	return 0;
1212 1213 1214
}

static int
1215
lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
1216
{
1217 1218
	struct lcs_card *card;
	int rc;
1219

1220 1221 1222 1223 1224 1225
	LCS_DBF_TEXT(5, trace, "pktxmit");
	card = (struct lcs_card *) dev->priv;
	spin_lock(&card->lock);
	rc = __lcs_start_xmit(card, skb, dev);
	spin_unlock(&card->lock);
	return rc;
1226 1227
}

1228 1229 1230 1231 1232
/**
 * send startlan and lanstat command to make LCS device ready
 */
static int
lcs_startlan_auto(struct lcs_card *card)
1233
{
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
	int rc;

#ifdef CONFIG_NET_ETHERNET
	card->lan_type = LCS_FRAME_TYPE_ENET;
	rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
	if (rc == 0)
		return 0;

#endif
#ifdef CONFIG_TR
	card->lan_type = LCS_FRAME_TYPE_TR;
	rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
	if (rc == 0)
		return 0;
#endif
#ifdef CONFIG_FDDI
	card->lan_type = LCS_FRAME_TYPE_FDDI;
	rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
	if (rc == 0)
		return 0;
#endif
	return -EIO;
1256 1257 1258
}

static int
1259
lcs_startlan(struct lcs_card *card)
1260
{
1261
	int rc, i;
1262

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	LCS_DBF_TEXT(2, trace, "startlan");
	rc = 0;
	if (card->portno != LCS_INVALID_PORT_NO) {
		if (card->lan_type == LCS_FRAME_TYPE_AUTO)
			rc = lcs_startlan_auto(card);
		else
			rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
	} else {
                for (i = 0; i <= card->max_port_no; i++) {
                        card->portno = i;
                        if (card->lan_type != LCS_FRAME_TYPE_AUTO)
                                rc = lcs_send_startlan(card,
                                                       LCS_INITIATOR_TCPIP);
                        else
                                /* autodetecting lan type */
                                rc = lcs_startlan_auto(card);
                        if (rc == 0)
                                break;
                }
        }
	if (rc == 0)
		return lcs_send_lanstat(card);
	return rc;
1286 1287
}

1288 1289 1290 1291
/**
 * LCS detect function
 * setup channels and make them I/O ready
 */
1292
static int
1293
lcs_detect(struct lcs_card *card)
1294
{
1295
	int rc;
1296

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
	LCS_DBF_TEXT(3, setup," lcsdetct");
	/* start/reset card */
	if (card->dev)
		netif_stop_queue(card->dev);
	card->write.state = CH_STATE_INIT;
	card->read.state = CH_STATE_INIT;
	rc = lcs_stop_channels(card);
	if (rc == 0) {
		rc = lcs_start_channels(card);
		if (rc == 0) {
			rc = lcs_send_startup(card, LCS_INITIATOR_TCPIP);
			if (rc == 0)
				rc = lcs_startlan(card);
		}
	}
	if (rc == 0) {
		card->state = DEV_STATE_UP;
	} else {
		card->state = DEV_STATE_DOWN;
		card->write.state = CH_STATE_INIT;
		card->read.state =  CH_STATE_INIT;
	}
	return rc;
1320 1321
}

1322 1323 1324
/**
 * reset card
 */
1325
static int
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
lcs_resetcard(struct lcs_card *card)
{
	int retries;

	LCS_DBF_TEXT(4, trace, "rescard");
	for (retries = 0; retries < 10; retries++) {
		if (lcs_detect(card) == 0) {
			netif_wake_queue(card->dev);
			card->state = DEV_STATE_UP;
			PRINT_INFO("LCS device %s successfully restarted!\n",
				   card->dev->name);
			return 0;
1338 1339 1340
		}
		schedule_timeout(3 * HZ);
	}
1341 1342
	PRINT_ERR("Error in Reseting LCS card!\n");
	return -EIO;
1343 1344
}

1345 1346 1347 1348 1349
/**
 * LCS Stop card
 */
static int
lcs_stopcard(struct lcs_card *card)
1350
{
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
	int rc;

	LCS_DBF_TEXT(3, setup, "stopcard");
	if (card->read.state != CH_STATE_STOPPED &&
	    card->write.state != CH_STATE_STOPPED &&
	    card->state == DEV_STATE_UP)
		rc = lcs_send_stoplan(card,LCS_INITIATOR_TCPIP);
	rc = lcs_send_shutdown(card);
	rc = lcs_stop_channels(card);
	card->state = DEV_STATE_DOWN;
	return rc;
1362 1363
}

1364 1365 1366
/**
 * LGW initiated commands
 */
1367
static int
1368
lcs_lgw_startlan_thread(void *data)
1369
{
1370
	struct lcs_card *card;
1371

1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
	card = (struct lcs_card *) data;
	daemonize();
	LCS_DBF_TEXT(4, trace, "lgwstpln");
	if (card->dev)
		netif_stop_queue(card->dev);
	if (lcs_startlan(card) == 0) {
		netif_wake_queue(card->dev);
		card->state = DEV_STATE_UP;
		PRINT_INFO("LCS Startlan for device %s succeeded!\n",
			   card->dev->name);

	} else
		PRINT_ERR("LCS Startlan for device %s failed!\n",
			  card->dev->name);
	return 0;
1387 1388
}

1389 1390 1391
/**
 * Send startup command initiated by Lan Gateway
 */
1392
static int
1393
lcs_lgw_startup_thread(void *data)
1394
{
1395
	int rc;
1396

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
	struct lcs_card *card;

	card = (struct lcs_card *) data;
	daemonize();
	LCS_DBF_TEXT(4, trace, "lgwstpln");
	if (card->dev)
		netif_stop_queue(card->dev);
	rc = lcs_send_startup(card, LCS_INITIATOR_LGW);
	if (rc != 0) {
		PRINT_ERR("Startup for LCS device %s initiated " \
			  "by LGW failed!\nReseting card ...\n",
			  card->dev->name);
		/* do a card reset */
		rc = lcs_resetcard(card);
		if (rc == 0)
			goto Done;
1413
	}
1414 1415 1416 1417
	rc = lcs_startlan(card);
	if (rc == 0) {
		netif_wake_queue(card->dev);
		card->state = DEV_STATE_UP;
1418
	}
1419 1420 1421 1422 1423 1424 1425 1426 1427
Done:
	if (rc == 0)
		PRINT_INFO("LCS Startup for device %s succeeded!\n",
			   card->dev->name);
	else
		PRINT_ERR("LCS Startup for device %s failed!\n",
			  card->dev->name);
	return 0;
}
1428

1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448

/**
 * send stoplan command initiated by Lan Gateway
 */
static int
lcs_lgw_stoplan_thread(void *data)
{
	struct lcs_card *card;

	card = (struct lcs_card *) data;
	daemonize();
	LCS_DBF_TEXT(4, trace, "lgwstop");
	if (card->dev)
		netif_stop_queue(card->dev);
	if (lcs_send_stoplan(card, LCS_INITIATOR_LGW) == 0)
		PRINT_INFO("Stoplan for %s initiated by LGW succeeded!\n",
			   card->dev->name);
	else
		PRINT_ERR("Stoplan %s initiated by LGW failed!\n",
			  card->dev->name);
1449 1450 1451
	return 0;
}

1452 1453
/**
 * Kernel Thread helper functions for LGW initiated commands
1454
 */
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
static void
lcs_start_kernel_thread(struct lcs_card *card)
{
	LCS_DBF_TEXT(5, trace, "krnthrd");
	if (test_and_clear_bit(0, &card->thread_mask))
		kernel_thread(lcs_lgw_startup_thread, (void *) card, SIGCHLD);
	if (test_and_clear_bit(1, &card->thread_mask))
		kernel_thread(lcs_lgw_startlan_thread, (void *) card, SIGCHLD);
	if (test_and_clear_bit(2, &card->thread_mask))
		kernel_thread(lcs_lgw_stoplan_thread, (void *) card, SIGCHLD);
	if (test_and_clear_bit(3, &card->thread_mask))
		kernel_thread(lcs_fix_multicast_list, (void *) card, SIGCHLD);
}
1468

1469 1470 1471
/**
 * Process control frames.
 */
1472
static void
1473
lcs_get_control(struct lcs_card *card, struct lcs_cmd *cmd)
1474
{
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
	if (cmd->initiator == LCS_INITIATOR_LGW) {
		switch(cmd->cmd_code) {
		case LCS_CMD_STARTUP:
			set_bit(0, &card->thread_mask);
			schedule_work(&card->kernel_thread_starter);
			break;
		case LCS_CMD_STARTLAN:
			set_bit(1, &card->thread_mask);
			schedule_work(&card->kernel_thread_starter);
			break;
		case LCS_CMD_STOPLAN:
			set_bit(2, &card->thread_mask);
			schedule_work(&card->kernel_thread_starter);
1488 1489
			break;
		default:
1490
			PRINT_INFO("UNRECOGNIZED LGW COMMAND\n");
1491 1492
			break;
		}
1493 1494 1495
	} else
		lcs_notify_lancmd_waiters(card, cmd);
}
1496

1497 1498
/**
 * Unpack network packet.
1499
 */
1500 1501
static void
lcs_get_skb(struct lcs_card *card, char *skb_data, unsigned int skb_len)
1502
{
1503
	struct sk_buff *skb;
1504

1505 1506 1507 1508
	if (card->dev == NULL ||
	    card->state != DEV_STATE_UP)
		/* The card isn't up. Ignore the packet. */
		return;
1509

1510 1511 1512 1513 1514 1515
	skb = dev_alloc_skb(skb_len);
	if (skb == NULL) {
		PRINT_ERR("LCS: alloc_skb failed for device=%s\n",
			  card->dev->name);
		card->stats.rx_dropped++;
		return;
1516
	}
1517 1518 1519 1520 1521 1522
	skb->dev = card->dev;
	memcpy(skb_put(skb, skb_len), skb_data, skb_len);
	skb->protocol =	card->lan_type_trans(skb, card->dev);
	card->stats.rx_bytes += skb_len;
	card->stats.rx_packets++;
	netif_rx(skb);
1523 1524
}

1525 1526 1527
/**
 * LCS main routine to get packets and lancmd replies from the buffers
 */
1528
static void
1529
lcs_get_frames_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
1530
{
1531 1532 1533
	struct lcs_card *card;
	struct lcs_header *lcs_hdr;
	__u16 offset;
1534

1535 1536 1537 1538 1539
	LCS_DBF_TEXT(5, trace, "lcsgtpkt");
	lcs_hdr = (struct lcs_header *) buffer->data;
	if (lcs_hdr->offset == LCS_ILLEGAL_OFFSET) {
		LCS_DBF_TEXT(4, trace, "-eiogpkt");
		return;
1540
	}
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
	card = (struct lcs_card *)
		((char *) channel - offsetof(struct lcs_card, read));
	offset = 0;
	while (lcs_hdr->offset != 0) {
		if (lcs_hdr->offset <= 0 ||
		    lcs_hdr->offset > LCS_IOBUFFERSIZE ||
		    lcs_hdr->offset < offset) {
			/* Offset invalid. */
			card->stats.rx_length_errors++;
			card->stats.rx_errors++;
			return;
1552
		}
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
		/* What kind of frame is it? */
		if (lcs_hdr->type == LCS_FRAME_TYPE_CONTROL)
			/* Control frame. */
			lcs_get_control(card, (struct lcs_cmd *) lcs_hdr);
		else if (lcs_hdr->type == LCS_FRAME_TYPE_ENET ||
			 lcs_hdr->type == LCS_FRAME_TYPE_TR ||
			 lcs_hdr->type == LCS_FRAME_TYPE_FDDI)
			/* Normal network packet. */
			lcs_get_skb(card, (char *)(lcs_hdr + 1),
				    lcs_hdr->offset - offset -
				    sizeof(struct lcs_header));
		else
			/* Unknown frame type. */
			; // FIXME: error message ?
		/* Proceed to next frame. */
		offset = lcs_hdr->offset;
		lcs_hdr->offset = LCS_ILLEGAL_OFFSET;
		lcs_hdr = (struct lcs_header *) (buffer->data + offset);
1571
	}
1572 1573
	/* The buffer is now empty. Make it ready again. */
	lcs_ready_buffer(&card->read, buffer);
1574 1575
}

1576 1577 1578
/**
 * get network statistics for ifconfig and other user programs
 */
1579
static struct net_device_stats *
1580
lcs_getstats(struct net_device *dev)
1581
{
1582
	struct lcs_card *card;
1583

1584 1585 1586
	LCS_DBF_TEXT(4, trace, "netstats");
	card = (struct lcs_card *) dev->priv;
	return &card->stats;
1587 1588
}

1589 1590 1591
/**
 * stop lcs device
 * This function will be called by user doing ifconfig xxx down
1592
 */
1593
static int
1594
lcs_stop_device(struct net_device *dev)
1595
{
1596
	struct lcs_card *card;
1597 1598
	int rc;

1599 1600 1601 1602 1603 1604 1605 1606
	LCS_DBF_TEXT(2, trace, "stopdev");
	card   = (struct lcs_card *) dev->priv;
	netif_stop_queue(dev);
	// FIXME: really free the net_device here ?!?
	kfree(card->dev);
	rc = lcs_stopcard(card);
	if (rc)
		PRINT_ERR("Try it again!\n ");
1607
	MOD_DEC_USE_COUNT;
1608
	return rc;
1609 1610
}

1611 1612 1613
/**
 * start lcs device and make it runnable
 * This function will be called by user doing ifconfig xxx up
1614
 */
1615
static int
1616
lcs_open_device(struct net_device *dev)
1617
{
1618 1619
	struct lcs_card *card;
	int rc;
1620

1621 1622 1623 1624 1625 1626
	LCS_DBF_TEXT(2, trace, "opendev");
	card = (struct lcs_card *) dev->priv;
	/* initialize statistics */
	rc = lcs_detect(card);
	if (rc) {
		PRINT_ERR("LCS:Error in opening device!\n");
1627 1628 1629

	} else {
		MOD_INC_USE_COUNT;
1630 1631
		netif_wake_queue(dev);
		card->state = DEV_STATE_UP;
1632 1633 1634 1635
	}
	return rc;
}

1636 1637 1638 1639 1640 1641
/**
 * show function for portno called by cat or similar things
 */
static ssize_t
lcs_portno_show (struct device *dev, char *buf, size_t count,
		 loff_t off)
1642
{
1643
        struct lcs_card *card;
1644

1645
	card = (struct lcs_card *)dev->driver_data;
1646

1647 1648
        if (off || !card)
                return 0;
1649

1650
        return snprintf(buf, count, "%d\n", card->portno);
1651 1652
}

1653 1654
/**
 * store the value which is piped to file portno
1655
 */
1656 1657 1658
static ssize_t
lcs_portno_store (struct device *dev, const char *buf, size_t count,
		  loff_t off)
1659
{
1660 1661
        struct lcs_card *card;
        int value;
1662

1663
	card = (struct lcs_card *)dev->driver_data;
1664

1665 1666
        if (off || !card)
                return 0;
1667

1668 1669 1670
        sscanf(buf, "%u", &value);
        /* TODO: sanity checks */
        card->portno = value;
1671

1672
        return count;
1673 1674 1675

}

1676
static DEVICE_ATTR(portno, 0644, lcs_portno_show, lcs_portno_store);
1677

1678 1679 1680
/**
 * lcs_probe_device is called on establishing a new ccwgroup_device.
 */
1681
static int
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
lcs_probe_device(struct ccwgroup_device *ccwgdev)
{
	struct lcs_card *card;
	int ret;

	if (!get_device(&ccwgdev->dev))
		return -ENODEV;

	LCS_DBF_TEXT(3, setup, "add_dev");
        card = lcs_alloc_card();
        if (!card) {
                PRINT_ERR("Allocation of lcs card failed\n");
		put_device(&ccwgdev->dev);
                return -ENOMEM;
        }
	ret = device_create_file(&ccwgdev->dev, &dev_attr_portno);
	if (ret) {
                PRINT_ERR("Creating attributes failed");
		lcs_free_card(card);
		put_device(&ccwgdev->dev);
		return ret;
        }
	ccwgdev->dev.driver_data = card;
	snprintf(ccwgdev->dev.name, DEVICE_NAME_SIZE, "%s",
		 cu3088_type[ccwgdev->cdev[0]->id.driver_info]);
	ccwgdev->cdev[0]->dev.driver_data = card;
	ccwgdev->cdev[0]->handler = lcs_irq;
	ccwgdev->cdev[1]->dev.driver_data = card;
	ccwgdev->cdev[1]->handler = lcs_irq;
        return 0;
}

/**
 * lcs_new_device will be called by setting the group device online.
 */
1717
static int
1718
lcs_new_device(struct ccwgroup_device *ccwgdev)
1719
{
1720 1721 1722
	struct  lcs_card *card;
	struct net_device *dev;
	int rc;
1723

1724 1725 1726
	card = (struct lcs_card *)ccwgdev->dev.driver_data;
	if (!card)
		return -ENODEV;
1727

1728 1729
	card->read.ccwdev  = ccwgdev->cdev[0];
	card->write.ccwdev = ccwgdev->cdev[1];
1730

1731 1732
	ccw_device_set_online(card->read.ccwdev);
	ccw_device_set_online(card->write.ccwdev);
1733

1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
	LCS_DBF_TEXT(3, setup, "lcsnewdv");
	rc = lcs_setup_card(card);
	if (rc) {
		LCS_DBF_TEXT(3, setup, "errinit");
		PRINT_ERR("LCS card Initialization failed\n");
		lcs_free_card(card);
		return rc;
	}

	rc = lcs_detect(card);
	if (rc) {
		lcs_stopcard(card);
		lcs_cleanup_card(card);
		lcs_free_card(card);
		return -ENODEV;
	}
	switch (card->lan_type) {
1751
#ifdef CONFIG_NET_ETHERNET
1752 1753 1754 1755
	case LCS_FRAME_TYPE_ENET:
		card->lan_type_trans = eth_type_trans;
		dev = init_etherdev(NULL,0);
		break;
1756 1757
#endif
#ifdef CONFIG_TR
1758 1759 1760 1761
	case LCS_FRAME_TYPE_TR:
		card->lan_type_trans = tr_type_trans;
		dev = init_trdev(NULL,0);
		break;
1762
#endif
1763 1764 1765 1766 1767
#ifdef CONFIG_FDDI
	case LCS_FRAME_TYPE_FDDI:
		card->lan_type_trans = fddi_type_trans;
		dev = init_fddidev(NULL,0);
		break;
1768
#endif
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
	default:
		LCS_DBF_TEXT(3, setup, "errinit");
		PRINT_ERR("LCS: Initialization failed\n");
		PRINT_ERR("LCS: No device found!\n");
		lcs_cleanup_channel(&card->read);
		lcs_cleanup_channel(&card->write);
		lcs_free_card(card);
		return -ENODEV;
	}
	memcpy(dev->dev_addr, card->mac, LCS_MAC_LENGTH);
	card->dev = dev;
	dev->priv = card;
	dev->open = lcs_open_device;
	dev->stop = lcs_stop_device;
	dev->hard_start_xmit = lcs_start_xmit;
1784
#ifdef CONFIG_IP_MULTICAST
1785 1786
	if (lcs_check_multicast_support(card))
		dev->set_multicast_list = lcs_set_multicast_list;
1787
#endif
1788 1789 1790 1791
	dev->get_stats = lcs_getstats;
	netif_stop_queue(dev);
	lcs_stopcard(card);
	return 0;
1792 1793
}

1794 1795
/**
 * lcs_shutdown_device, called when setting the group device offline.
1796
 */
1797
static int
1798
lcs_shutdown_device(struct ccwgroup_device *ccwgdev)
1799
{
1800
	struct lcs_card *card;
1801

1802 1803 1804 1805
	LCS_DBF_TEXT(3, setup, "shtdndev");
	card = (struct lcs_card *)ccwgdev->dev.driver_data;
	if (!card)
		return -ENODEV;
1806

1807
	return lcs_stop_device(card->dev);
1808 1809
}

1810 1811 1812
/**
 * lcs_remove_device, free buffers and card
 */
1813
static int
1814
lcs_remove_device(struct ccwgroup_device *ccwgdev)
1815
{
1816
	struct lcs_card *card;
1817

1818 1819 1820 1821 1822 1823 1824 1825
	LCS_DBF_TEXT(3, setup, "remdev");
	card = (struct lcs_card *)ccwgdev->dev.driver_data;
	if (!card)
		return 0;
	lcs_cleanup_card(card);
	lcs_free_card(card);
	put_device(&ccwgdev->dev);
	return 0;
1826 1827
}

1828 1829
/**
 * LCS ccwgroup driver registration
1830
 */
1831
static struct ccwgroup_driver lcs_group_driver = {
1832 1833 1834 1835 1836 1837 1838 1839
	.name        = "lcs",
	.max_slaves  = 2,
	.driver_id   = 0xD3C3E2,
	.probe       = lcs_probe_device,
	.remove      = lcs_remove_device,
	.set_online  = lcs_new_device,
	.set_offline = lcs_shutdown_device,
};
1840

1841 1842 1843
/**
 *  LCS Module/Kernel initialization function
 */
1844
static int
1845
__init lcs_init_module(void)
1846
{
1847
	int rc;
1848

1849 1850 1851
	LCS_DBF_TEXT(0, setup, "lcsinit");
	PRINT_INFO("Loading %s\n",version);
	rc = lcs_register_debug_facility();
1852
	if (rc) {
1853 1854
		PRINT_ERR("Initialization failed\n");
		return rc;
1855 1856
	}

1857 1858 1859 1860
	rc = register_cu3088_discipline(&lcs_group_driver);
	if (rc) {
		PRINT_ERR("Initialization failed\n");
		return rc;
1861 1862
	}

1863
	return 0;
1864 1865 1866
}


1867 1868 1869
/**
 *  LCS module cleanup function
 */
1870
static void
1871
__exit lcs_cleanup_module(void)
1872
{
1873 1874 1875 1876
	PRINT_INFO("Terminating lcs module.\n");
	LCS_DBF_TEXT(0, trace, "cleanup");
	unregister_cu3088_discipline(&lcs_group_driver);
	lcs_unregister_debug_facility();
1877 1878
}

1879 1880
module_init(lcs_init_module);
module_exit(lcs_cleanup_module);
1881

1882 1883
MODULE_AUTHOR("Frank Pavlic <pavlic@de.ibm.com>");
MODULE_LICENSE("GPL");
1884