lcs.c 62.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *  Linux for S/390 Lan Channel Station Network Driver
 *
4 5 6 7 8 9
 *  Copyright IBM Corp. 1999, 2009
 *  Author(s): Original Code written by
 *			DJ Barrow <djbarrow@de.ibm.com,barrow_dj@yahoo.com>
 *	       Rewritten by
 *			Frank Pavlic <fpavlic@de.ibm.com> and
 *			Martin Schwidefsky <schwidefsky@de.ibm.com>
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * 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.
 */

26 27 28
#define KMSG_COMPONENT		"lcs"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37
#include <linux/module.h>
#include <linux/if.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/fddidevice.h>
#include <linux/inetdevice.h>
#include <linux/in.h>
#include <linux/igmp.h>
#include <linux/delay.h>
38
#include <linux/kthread.h>
39
#include <linux/slab.h>
L
Linus Torvalds 已提交
40 41 42 43 44 45 46 47 48 49 50 51
#include <net/arp.h>
#include <net/ip.h>

#include <asm/debug.h>
#include <asm/idals.h>
#include <asm/timex.h>
#include <linux/device.h>
#include <asm/ccwgroup.h>

#include "lcs.h"


52
#if !defined(CONFIG_ETHERNET) && !defined(CONFIG_FDDI)
L
Linus Torvalds 已提交
53 54 55 56 57 58 59
#error Cannot compile lcs.c without some net devices switched on.
#endif

/**
 * initialization string for output
 */

60
static char version[] __initdata = "LCS driver";
61 62 63 64 65

/**
  * the root device for lcs group devices
  */
static struct device *lcs_root_dev;
L
Linus Torvalds 已提交
66 67 68 69 70

/**
 * Some prototypes.
 */
static void lcs_tasklet(unsigned long);
71
static void lcs_start_kernel_thread(struct work_struct *);
L
Linus Torvalds 已提交
72
static void lcs_get_frames_cb(struct lcs_channel *, struct lcs_buffer *);
73
#ifdef CONFIG_IP_MULTICAST
L
Linus Torvalds 已提交
74
static int lcs_send_delipm(struct lcs_card *, struct lcs_ipm_list *);
75
#endif /* CONFIG_IP_MULTICAST */
76
static int lcs_recovery(void *ptr);
L
Linus Torvalds 已提交
77 78 79 80

/**
 * Debug Facility Stuff
 */
81
static char debug_buffer[255];
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89 90
static debug_info_t *lcs_dbf_setup;
static debug_info_t *lcs_dbf_trace;

/**
 *  LCS Debug Facility functions
 */
static void
lcs_unregister_debug_facility(void)
{
91 92
	debug_unregister(lcs_dbf_setup);
	debug_unregister(lcs_dbf_trace);
L
Linus Torvalds 已提交
93 94 95 96 97
}

static int
lcs_register_debug_facility(void)
{
98
	lcs_dbf_setup = debug_register("lcs_setup", 2, 1, 8);
99
	lcs_dbf_trace = debug_register("lcs_trace", 4, 1, 8);
L
Linus Torvalds 已提交
100
	if (lcs_dbf_setup == NULL || lcs_dbf_trace == NULL) {
101
		pr_err("Not enough memory for debug facility.\n");
L
Linus Torvalds 已提交
102 103 104 105
		lcs_unregister_debug_facility();
		return -ENOMEM;
	}
	debug_register_view(lcs_dbf_setup, &debug_hex_ascii_view);
106
	debug_set_level(lcs_dbf_setup, 2);
L
Linus Torvalds 已提交
107
	debug_register_view(lcs_dbf_trace, &debug_hex_ascii_view);
108
	debug_set_level(lcs_dbf_trace, 2);
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122
	return 0;
}

/**
 * Allocate io buffers.
 */
static int
lcs_alloc_channel(struct lcs_channel *channel)
{
	int cnt;

	LCS_DBF_TEXT(2, setup, "ichalloc");
	for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
		/* alloc memory fo iobuffer */
123 124
		channel->iob[cnt].data =
			kzalloc(LCS_IOBUFFERSIZE, GFP_DMA | GFP_KERNEL);
L
Linus Torvalds 已提交
125 126
		if (channel->iob[cnt].data == NULL)
			break;
127
		channel->iob[cnt].state = LCS_BUF_STATE_EMPTY;
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	}
	if (cnt < LCS_NUM_BUFFS) {
		/* Not all io buffers could be allocated. */
		LCS_DBF_TEXT(2, setup, "echalloc");
		while (cnt-- > 0)
			kfree(channel->iob[cnt].data);
		return -ENOMEM;
	}
	return 0;
}

/**
 * Free io buffers.
 */
static void
lcs_free_channel(struct lcs_channel *channel)
{
	int cnt;

	LCS_DBF_TEXT(2, setup, "ichfree");
	for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
J
Jesper Juhl 已提交
149
		kfree(channel->iob[cnt].data);
L
Linus Torvalds 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		channel->iob[cnt].data = NULL;
	}
}

/*
 * Cleanup channel.
 */
static void
lcs_cleanup_channel(struct lcs_channel *channel)
{
	LCS_DBF_TEXT(3, setup, "cleanch");
	/* Kill write channel tasklets. */
	tasklet_kill(&channel->irq_tasklet);
	/* Free channel buffers. */
	lcs_free_channel(channel);
}

/**
 * LCS free memory for card and channels.
 */
static void
lcs_free_card(struct lcs_card *card)
{
	LCS_DBF_TEXT(2, setup, "remcard");
	LCS_DBF_HEX(2, setup, &card, sizeof(void*));
	kfree(card);
}

/**
 * LCS alloc memory for card and channels
 */
static struct lcs_card *
lcs_alloc_card(void)
{
	struct lcs_card *card;
	int rc;

	LCS_DBF_TEXT(2, setup, "alloclcs");

189
	card = kzalloc(sizeof(struct lcs_card), GFP_KERNEL | GFP_DMA);
L
Linus Torvalds 已提交
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	if (card == NULL)
		return NULL;
	card->lan_type = LCS_FRAME_TYPE_AUTO;
	card->pkt_seq = 0;
	card->lancmd_timeout = LCS_LANCMD_TIMEOUT_DEFAULT;
	/* Allocate io buffers for the read channel. */
	rc = lcs_alloc_channel(&card->read);
	if (rc){
		LCS_DBF_TEXT(2, setup, "iccwerr");
		lcs_free_card(card);
		return NULL;
	}
	/* Allocate io buffers for the write channel. */
	rc = lcs_alloc_channel(&card->write);
	if (rc) {
		LCS_DBF_TEXT(2, setup, "iccwerr");
		lcs_cleanup_channel(&card->read);
		lcs_free_card(card);
		return NULL;
	}

#ifdef CONFIG_IP_MULTICAST
	INIT_LIST_HEAD(&card->ipm_list);
#endif
	LCS_DBF_HEX(2, setup, &card, sizeof(void*));
	return card;
}

/*
 * Setup read channel.
 */
static void
lcs_setup_read_ccws(struct lcs_card *card)
{
	int cnt;

	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;
243
		card->read.iob[cnt].state = LCS_BUF_STATE_READY;
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251 252 253
		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. */
254
	card->read.state = LCS_CH_STATE_INIT;
L
Linus Torvalds 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

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

static void
lcs_setup_read(struct lcs_card *card)
{
	LCS_DBF_TEXT(3, setup, "initread");

	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);
}

/*
 * Setup write channel.
 */
static void
lcs_setup_write_ccws(struct lcs_card *card)
{
	int cnt;

	LCS_DBF_TEXT(3, setup, "iwritccw");
	/* Setup write ccws. */
S
Sebastian Ott 已提交
283
	memset(card->write.ccws, 0, sizeof(struct ccw1) * (LCS_NUM_BUFFS + 1));
L
Linus Torvalds 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	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. */
301
	card->read.state = LCS_CH_STATE_INIT;
L
Linus Torvalds 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

	card->write.io_idx = 0;
	card->write.buf_idx = 0;
}

static void
lcs_setup_write(struct lcs_card *card)
{
	LCS_DBF_TEXT(3, setup, "initwrit");

	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);
}

static void
lcs_set_allowed_threads(struct lcs_card *card, unsigned long threads)
{
	unsigned long flags;

	spin_lock_irqsave(&card->mask_lock, flags);
	card->thread_allowed_mask = threads;
	spin_unlock_irqrestore(&card->mask_lock, flags);
	wake_up(&card->wait_q);
}
J
Julian Wiedmann 已提交
330
static int lcs_threads_running(struct lcs_card *card, unsigned long threads)
L
Linus Torvalds 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
{
        unsigned long flags;
        int rc = 0;

	spin_lock_irqsave(&card->mask_lock, flags);
        rc = (card->thread_running_mask & threads);
	spin_unlock_irqrestore(&card->mask_lock, flags);
        return rc;
}

static int
lcs_wait_for_threads(struct lcs_card *card, unsigned long threads)
{
        return wait_event_interruptible(card->wait_q,
                        lcs_threads_running(card, threads) == 0);
}

J
Julian Wiedmann 已提交
348
static int lcs_set_thread_start_bit(struct lcs_card *card, unsigned long thread)
L
Linus Torvalds 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
{
        unsigned long flags;

	spin_lock_irqsave(&card->mask_lock, flags);
        if ( !(card->thread_allowed_mask & thread) ||
              (card->thread_start_mask & thread) ) {
                spin_unlock_irqrestore(&card->mask_lock, flags);
                return -EPERM;
        }
        card->thread_start_mask |= thread;
	spin_unlock_irqrestore(&card->mask_lock, flags);
        return 0;
}

static void
lcs_clear_thread_running_bit(struct lcs_card *card, unsigned long thread)
{
        unsigned long flags;

	spin_lock_irqsave(&card->mask_lock, flags);
        card->thread_running_mask &= ~thread;
	spin_unlock_irqrestore(&card->mask_lock, flags);
        wake_up(&card->wait_q);
}

J
Julian Wiedmann 已提交
374
static int __lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
{
        unsigned long flags;
        int rc = 0;

	spin_lock_irqsave(&card->mask_lock, flags);
        if (card->thread_start_mask & thread){
                if ((card->thread_allowed_mask & thread) &&
                    !(card->thread_running_mask & thread)){
                        rc = 1;
                        card->thread_start_mask &= ~thread;
                        card->thread_running_mask |= thread;
                } else
                        rc = -EPERM;
        }
	spin_unlock_irqrestore(&card->mask_lock, flags);
        return rc;
}

static int
lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
{
        int rc = 0;
        wait_event(card->wait_q,
                   (rc = __lcs_do_run_thread(card, thread)) >= 0);
        return rc;
}

static int
lcs_do_start_thread(struct lcs_card *card, unsigned long thread)
{
        unsigned long flags;
        int rc = 0;

	spin_lock_irqsave(&card->mask_lock, flags);
        LCS_DBF_TEXT_(4, trace, "  %02x%02x%02x",
                        (u8) card->thread_start_mask,
                        (u8) card->thread_allowed_mask,
                        (u8) card->thread_running_mask);
        rc = (card->thread_start_mask & thread);
	spin_unlock_irqrestore(&card->mask_lock, flags);
        return rc;
}

/**
 * Initialize channels,card and state machines.
 */
static void
lcs_setup_card(struct lcs_card *card)
{
	LCS_DBF_TEXT(2, setup, "initcard");
	LCS_DBF_HEX(2, setup, &card, sizeof(void*));

	lcs_setup_read(card);
	lcs_setup_write(card);
	/* Set cards initial state. */
	card->state = DEV_STATE_DOWN;
	card->tx_buffer = NULL;
	card->tx_emitted = 0;

	init_waitqueue_head(&card->wait_q);
	spin_lock_init(&card->lock);
	spin_lock_init(&card->ipm_lock);
	spin_lock_init(&card->mask_lock);
#ifdef CONFIG_IP_MULTICAST
	INIT_LIST_HEAD(&card->ipm_list);
#endif
	INIT_LIST_HEAD(&card->lancmd_waiters);
}

J
Julian Wiedmann 已提交
444
static void lcs_clear_multicast_list(struct lcs_card *card)
L
Linus Torvalds 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
{
#ifdef	CONFIG_IP_MULTICAST
	struct lcs_ipm_list *ipm;
	unsigned long flags;

	/* Free multicast list. */
	LCS_DBF_TEXT(3, setup, "clmclist");
	spin_lock_irqsave(&card->ipm_lock, flags);
	while (!list_empty(&card->ipm_list)){
		ipm = list_entry(card->ipm_list.next,
				 struct lcs_ipm_list, list);
		list_del(&ipm->list);
		if (ipm->ipm_state != LCS_IPM_STATE_SET_REQUIRED){
			spin_unlock_irqrestore(&card->ipm_lock, flags);
			lcs_send_delipm(card, ipm);
			spin_lock_irqsave(&card->ipm_lock, flags);
		}
		kfree(ipm);
	}
	spin_unlock_irqrestore(&card->ipm_lock, flags);
#endif
}
/**
 * Cleanup channels,card and state machines.
 */
static void
lcs_cleanup_card(struct lcs_card *card)
{

	LCS_DBF_TEXT(3, setup, "cleancrd");
	LCS_DBF_HEX(2,setup,&card,sizeof(void*));

	if (card->dev != NULL)
		free_netdev(card->dev);
	/* Cleanup channels. */
	lcs_cleanup_channel(&card->write);
	lcs_cleanup_channel(&card->read);
}

/**
 * Start channel.
 */
static int
lcs_start_channel(struct lcs_channel *channel)
{
	unsigned long flags;
	int rc;

493
	LCS_DBF_TEXT_(4, trace,"ssch%s", dev_name(&channel->ccwdev->dev));
L
Linus Torvalds 已提交
494 495 496 497 498
	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)
499
		channel->state = LCS_CH_STATE_RUNNING;
L
Linus Torvalds 已提交
500 501
	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
	if (rc) {
502 503
		LCS_DBF_TEXT_(4,trace,"essh%s",
			      dev_name(&channel->ccwdev->dev));
504 505 506
		dev_err(&channel->ccwdev->dev,
			"Starting an LCS device resulted in an error,"
			" rc=%d!\n", rc);
L
Linus Torvalds 已提交
507 508 509 510 511 512 513 514 515 516 517
	}
	return rc;
}

static int
lcs_clear_channel(struct lcs_channel *channel)
{
	unsigned long flags;
	int rc;

	LCS_DBF_TEXT(4,trace,"clearch");
518
	LCS_DBF_TEXT_(4, trace, "%s", dev_name(&channel->ccwdev->dev));
L
Linus Torvalds 已提交
519 520 521 522
	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
	rc = ccw_device_clear(channel->ccwdev, (addr_t) channel);
	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
	if (rc) {
523 524
		LCS_DBF_TEXT_(4, trace, "ecsc%s",
			      dev_name(&channel->ccwdev->dev));
L
Linus Torvalds 已提交
525 526
		return rc;
	}
527 528
	wait_event(channel->wait_q, (channel->state == LCS_CH_STATE_CLEARED));
	channel->state = LCS_CH_STATE_STOPPED;
L
Linus Torvalds 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541
	return rc;
}


/**
 * Stop channel.
 */
static int
lcs_stop_channel(struct lcs_channel *channel)
{
	unsigned long flags;
	int rc;

542
	if (channel->state == LCS_CH_STATE_STOPPED)
L
Linus Torvalds 已提交
543 544
		return 0;
	LCS_DBF_TEXT(4,trace,"haltsch");
545
	LCS_DBF_TEXT_(4, trace, "%s", dev_name(&channel->ccwdev->dev));
546
	channel->state = LCS_CH_STATE_INIT;
L
Linus Torvalds 已提交
547 548 549 550
	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) {
551 552
		LCS_DBF_TEXT_(4, trace, "ehsc%s",
			      dev_name(&channel->ccwdev->dev));
L
Linus Torvalds 已提交
553 554 555
		return rc;
	}
	/* Asynchronous halt initialted. Wait for its completion. */
556
	wait_event(channel->wait_q, (channel->state == LCS_CH_STATE_HALTED));
L
Linus Torvalds 已提交
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 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	lcs_clear_channel(channel);
	return 0;
}

/**
 * start read and write channel
 */
static int
lcs_start_channels(struct lcs_card *card)
{
	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;
}

/**
 * stop read and write channel
 */
static int
lcs_stop_channels(struct lcs_card *card)
{
	LCS_DBF_TEXT(2, trace, "chhalt");
	lcs_stop_channel(&card->read);
	lcs_stop_channel(&card->write);
	return 0;
}

/**
 * Get empty buffer.
 */
static struct lcs_buffer *
__lcs_get_buffer(struct lcs_channel *channel)
{
	int index;

	LCS_DBF_TEXT(5, trace, "_getbuff");
	index = channel->io_idx;
	do {
604 605
		if (channel->iob[index].state == LCS_BUF_STATE_EMPTY) {
			channel->iob[index].state = LCS_BUF_STATE_LOCKED;
L
Linus Torvalds 已提交
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
			return channel->iob + index;
		}
		index = (index + 1) & (LCS_NUM_BUFFS - 1);
	} while (index != channel->io_idx);
	return NULL;
}

static struct lcs_buffer *
lcs_get_buffer(struct lcs_channel *channel)
{
	struct lcs_buffer *buffer;
	unsigned long flags;

	LCS_DBF_TEXT(5, trace, "getbuff");
	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;
}

/**
 * Resume channel program if the channel is suspended.
 */
static int
__lcs_resume_channel(struct lcs_channel *channel)
{
	int rc;

634
	if (channel->state != LCS_CH_STATE_SUSPENDED)
L
Linus Torvalds 已提交
635 636 637
		return 0;
	if (channel->ccws[channel->io_idx].flags & CCW_FLAG_SUSPEND)
		return 0;
638
	LCS_DBF_TEXT_(5, trace, "rsch%s", dev_name(&channel->ccwdev->dev));
L
Linus Torvalds 已提交
639 640
	rc = ccw_device_resume(channel->ccwdev);
	if (rc) {
641 642
		LCS_DBF_TEXT_(4, trace, "ersc%s",
			      dev_name(&channel->ccwdev->dev));
643 644 645
		dev_err(&channel->ccwdev->dev,
			"Sending data from the LCS device to the LAN failed"
			" with rc=%d\n",rc);
L
Linus Torvalds 已提交
646
	} else
647
		channel->state = LCS_CH_STATE_RUNNING;
L
Linus Torvalds 已提交
648 649 650 651 652 653 654
	return rc;

}

/**
 * Make a buffer ready for processing.
 */
J
Julian Wiedmann 已提交
655
static void __lcs_ready_buffer_bits(struct lcs_channel *channel, int index)
L
Linus Torvalds 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
{
	int prev, next;

	LCS_DBF_TEXT(5, trace, "rdybits");
	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;
	}
}

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

	LCS_DBF_TEXT(5, trace, "rdybuff");
680 681
	BUG_ON(buffer->state != LCS_BUF_STATE_LOCKED &&
	       buffer->state != LCS_BUF_STATE_PROCESSED);
L
Linus Torvalds 已提交
682
	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
683
	buffer->state = LCS_BUF_STATE_READY;
L
Linus Torvalds 已提交
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
	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;
}

/**
 * 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, prev, next;

	LCS_DBF_TEXT(5, trace, "prcsbuff");
705 706
	BUG_ON(buffer->state != LCS_BUF_STATE_READY);
	buffer->state = LCS_BUF_STATE_PROCESSED;
L
Linus Torvalds 已提交
707 708 709 710 711 712 713
	index = buffer - channel->iob;
	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. */
714
	if (channel->iob[prev].state == LCS_BUF_STATE_READY) {
L
Linus Torvalds 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
		/*
		 * 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);
}

/**
 * Put a processed buffer back to state empty.
 */
static void
lcs_release_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
{
	unsigned long flags;

	LCS_DBF_TEXT(5, trace, "relbuff");
737 738
	BUG_ON(buffer->state != LCS_BUF_STATE_LOCKED &&
	       buffer->state != LCS_BUF_STATE_PROCESSED);
L
Linus Torvalds 已提交
739
	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
740
	buffer->state = LCS_BUF_STATE_EMPTY;
L
Linus Torvalds 已提交
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
}

/**
 * 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;

	LCS_DBF_TEXT(4, trace, "getlncmd");
	/* 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;
}


static void
lcs_get_reply(struct lcs_reply *reply)
{
	WARN_ON(atomic_read(&reply->refcnt) <= 0);
	atomic_inc(&reply->refcnt);
}

static void
lcs_put_reply(struct lcs_reply *reply)
{
        WARN_ON(atomic_read(&reply->refcnt) <= 0);
        if (atomic_dec_and_test(&reply->refcnt)) {
		kfree(reply);
	}

}

static struct lcs_reply *
lcs_alloc_reply(struct lcs_cmd *cmd)
{
	struct lcs_reply *reply;

	LCS_DBF_TEXT(4, trace, "getreply");

793
	reply = kzalloc(sizeof(struct lcs_reply), GFP_ATOMIC);
L
Linus Torvalds 已提交
794 795 796 797 798 799 800 801 802 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
	if (!reply)
		return NULL;
	atomic_set(&reply->refcnt,1);
	reply->sequence_no = cmd->sequence_no;
	reply->received = 0;
	reply->rc = 0;
	init_waitqueue_head(&reply->wait_q);

	return reply;
}

/**
 * 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;

	LCS_DBF_TEXT(4, trace, "notiwait");
	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) {
			lcs_get_reply(reply);
			list_del_init(&reply->list);
			if (reply->callback != NULL)
				reply->callback(card, cmd);
			reply->received = 1;
			reply->rc = cmd->return_code;
			wake_up(&reply->wait_q);
			lcs_put_reply(reply);
			break;
		}
	}
	spin_unlock(&card->lock);
}

/**
834
 * Emit buffer of a lan command.
L
Linus Torvalds 已提交
835
 */
836
static void
837
lcs_lancmd_timeout(struct timer_list *t)
L
Linus Torvalds 已提交
838
{
839 840
	struct lcs_reply *reply = from_timer(reply, t, timer);
	struct lcs_reply *list_reply, *r;
L
Linus Torvalds 已提交
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
	unsigned long flags;

	LCS_DBF_TEXT(4, trace, "timeout");
	spin_lock_irqsave(&reply->card->lock, flags);
	list_for_each_entry_safe(list_reply, r,
				 &reply->card->lancmd_waiters,list) {
		if (reply == list_reply) {
			lcs_get_reply(reply);
			list_del_init(&reply->list);
			spin_unlock_irqrestore(&reply->card->lock, flags);
			reply->received = 1;
			reply->rc = -ETIME;
			wake_up(&reply->wait_q);
			lcs_put_reply(reply);
			return;
		}
	}
	spin_unlock_irqrestore(&reply->card->lock, flags);
}

static int
lcs_send_lancmd(struct lcs_card *card, struct lcs_buffer *buffer,
		void (*reply_callback)(struct lcs_card *, struct lcs_cmd *))
{
	struct lcs_reply *reply;
	struct lcs_cmd *cmd;
	unsigned long flags;
	int rc;

	LCS_DBF_TEXT(4, trace, "sendcmd");
	cmd = (struct lcs_cmd *) buffer->data;
	cmd->return_code = 0;
	cmd->sequence_no = card->sequence_no++;
	reply = lcs_alloc_reply(cmd);
	if (!reply)
		return -ENOMEM;
	reply->callback = reply_callback;
	reply->card = card;
	spin_lock_irqsave(&card->lock, flags);
	list_add_tail(&reply->list, &card->lancmd_waiters);
	spin_unlock_irqrestore(&card->lock, flags);

	buffer->callback = lcs_release_buffer;
	rc = lcs_ready_buffer(&card->write, buffer);
	if (rc)
		return rc;
887 888
	timer_setup(&reply->timer, lcs_lancmd_timeout, 0);
	mod_timer(&reply->timer, jiffies + HZ * card->lancmd_timeout);
L
Linus Torvalds 已提交
889
	wait_event(reply->wait_q, reply->received);
890
	del_timer_sync(&reply->timer);
L
Linus Torvalds 已提交
891 892 893 894 895 896 897 898 899 900 901 902 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 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 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 1016 1017 1018 1019 1020 1021 1022 1023 1024 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 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
	LCS_DBF_TEXT_(4, trace, "rc:%d",reply->rc);
	rc = reply->rc;
	lcs_put_reply(reply);
	return 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;

	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);
}

/**
 * LCS shutdown command
 */
static int
lcs_send_shutdown(struct lcs_card *card)
{
	struct lcs_buffer *buffer;
	struct lcs_cmd *cmd;

	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);
}

/**
 * LCS lanstat command
 */
static void
__lcs_lanstat_cb(struct lcs_card *card, struct lcs_cmd *cmd)
{
	LCS_DBF_TEXT(2, trace, "statcb");
	memcpy(card->mac, cmd->cmd.lcs_lanstat_cmd.mac_addr, LCS_MAC_LENGTH);
}

static int
lcs_send_lanstat(struct lcs_card *card)
{
	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);
}

/**
 * send stoplan command
 */
static int
lcs_send_stoplan(struct lcs_card *card, __u8 initiator)
{
	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);
}

/**
 * send startlan command
 */
static void
__lcs_send_startlan_cb(struct lcs_card *card, struct lcs_cmd *cmd)
{
	LCS_DBF_TEXT(2, trace, "srtlancb");
	card->lan_type = cmd->cmd.lcs_std_cmd.lan_type;
	card->portno = cmd->cmd.lcs_std_cmd.portno;
}

static int
lcs_send_startlan(struct lcs_card *card, __u8 initiator)
{
	struct lcs_buffer *buffer;
	struct lcs_cmd *cmd;

	LCS_DBF_TEXT(2, trace, "cmdstaln");
	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);
}

#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));
	LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
	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));
	LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
	return lcs_send_lancmd(card, buffer, NULL);
}

/**
 * check if multicast is supported by LCS
 */
static void
__lcs_check_multicast_cb(struct lcs_card *card, struct lcs_cmd *cmd)
{
	LCS_DBF_TEXT(2, trace, "chkmccb");
	card->ip_assists_supported =
		cmd->cmd.lcs_qipassist.ip_assists_supported;
	card->ip_assists_enabled =
		cmd->cmd.lcs_qipassist.ip_assists_enabled;
}

static int
lcs_check_multicast_support(struct lcs_card *card)
{
	struct lcs_buffer *buffer;
	struct lcs_cmd *cmd;
	int rc;

	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) {
1086
		pr_err("Query IPAssist failed. Assuming unsupported!\n");
L
Linus Torvalds 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
		return -EOPNOTSUPP;
	}
	if (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT)
		return 0;
	return -EOPNOTSUPP;
}

/**
 * set or del multicast address on LCS card
 */
static void
lcs_fix_multicast_list(struct lcs_card *card)
{
	struct list_head failed_list;
	struct lcs_ipm_list *ipm, *tmp;
	unsigned long flags;
	int rc;

	LCS_DBF_TEXT(4,trace, "fixipm");
	INIT_LIST_HEAD(&failed_list);
	spin_lock_irqsave(&card->ipm_lock, flags);
list_modified:
	list_for_each_entry_safe(ipm, tmp, &card->ipm_list, list){
		switch (ipm->ipm_state) {
		case LCS_IPM_STATE_SET_REQUIRED:
L
Lucas De Marchi 已提交
1112
			/* del from ipm_list so no one else can tamper with
L
Linus Torvalds 已提交
1113 1114 1115 1116 1117 1118
			 * this entry */
			list_del_init(&ipm->list);
			spin_unlock_irqrestore(&card->ipm_lock, flags);
			rc = lcs_send_setipm(card, ipm);
			spin_lock_irqsave(&card->ipm_lock, flags);
			if (rc) {
1119 1120
				pr_info("Adding multicast address failed."
					" Table possibly full!\n");
L
Linus Torvalds 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
				/* store ipm in failed list -> will be added
				 * to ipm_list again, so a retry will be done
				 * during the next call of this function */
				list_add_tail(&ipm->list, &failed_list);
			} else {
				ipm->ipm_state = LCS_IPM_STATE_ON_CARD;
				/* re-insert into ipm_list */
				list_add_tail(&ipm->list, &card->ipm_list);
			}
			goto list_modified;
		case LCS_IPM_STATE_DEL_REQUIRED:
			list_del(&ipm->list);
			spin_unlock_irqrestore(&card->ipm_lock, flags);
			lcs_send_delipm(card, ipm);
			spin_lock_irqsave(&card->ipm_lock, flags);
			kfree(ipm);
			goto list_modified;
		case LCS_IPM_STATE_ON_CARD:
			break;
		}
	}
	/* re-insert all entries from the failed_list into ipm_list */
A
Akinobu Mita 已提交
1143 1144 1145
	list_for_each_entry_safe(ipm, tmp, &failed_list, list)
		list_move_tail(&ipm->list, &card->ipm_list);

L
Linus Torvalds 已提交
1146 1147 1148 1149 1150 1151 1152
	spin_unlock_irqrestore(&card->ipm_lock, flags);
}

/**
 * get mac address for the relevant Multicast address
 */
static void
1153
lcs_get_mac_for_ipm(__be32 ipm, char *mac, struct net_device *dev)
L
Linus Torvalds 已提交
1154 1155
{
	LCS_DBF_TEXT(4,trace, "getmac");
1156
	ip_eth_mc_map(ipm, mac);
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161
}

/**
 * function called by net device to handle multicast address relevant things
 */
J
Julian Wiedmann 已提交
1162 1163
static void lcs_remove_mc_addresses(struct lcs_card *card,
				    struct in_device *in4_dev)
L
Linus Torvalds 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
{
	struct ip_mc_list *im4;
	struct list_head *l;
	struct lcs_ipm_list *ipm;
	unsigned long flags;
	char buf[MAX_ADDR_LEN];

	LCS_DBF_TEXT(4, trace, "remmclst");
	spin_lock_irqsave(&card->ipm_lock, flags);
	list_for_each(l, &card->ipm_list) {
		ipm = list_entry(l, struct lcs_ipm_list, list);
S
Sachin Sant 已提交
1175 1176
		for (im4 = rcu_dereference(in4_dev->mc_list);
		     im4 != NULL; im4 = rcu_dereference(im4->next_rcu)) {
L
Linus Torvalds 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
			lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
			if ( (ipm->ipm.ip_addr == im4->multiaddr) &&
			     (memcmp(buf, &ipm->ipm.mac_addr,
				     LCS_MAC_LENGTH) == 0) )
				break;
		}
		if (im4 == NULL)
			ipm->ipm_state = LCS_IPM_STATE_DEL_REQUIRED;
	}
	spin_unlock_irqrestore(&card->ipm_lock, flags);
}

J
Julian Wiedmann 已提交
1189 1190 1191
static struct lcs_ipm_list *lcs_check_addr_entry(struct lcs_card *card,
						 struct ip_mc_list *im4,
						 char *buf)
L
Linus Torvalds 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
{
	struct lcs_ipm_list *tmp, *ipm = NULL;
	struct list_head *l;
	unsigned long flags;

	LCS_DBF_TEXT(4, trace, "chkmcent");
	spin_lock_irqsave(&card->ipm_lock, flags);
	list_for_each(l, &card->ipm_list) {
		tmp = list_entry(l, struct lcs_ipm_list, list);
		if ( (tmp->ipm.ip_addr == im4->multiaddr) &&
		     (memcmp(buf, &tmp->ipm.mac_addr,
			     LCS_MAC_LENGTH) == 0) ) {
			ipm = tmp;
			break;
		}
	}
	spin_unlock_irqrestore(&card->ipm_lock, flags);
	return ipm;
}

J
Julian Wiedmann 已提交
1212 1213
static void lcs_set_mc_addresses(struct lcs_card *card,
				 struct in_device *in4_dev)
L
Linus Torvalds 已提交
1214 1215 1216 1217 1218 1219 1220 1221
{

	struct ip_mc_list *im4;
	struct lcs_ipm_list *ipm;
	char buf[MAX_ADDR_LEN];
	unsigned long flags;

	LCS_DBF_TEXT(4, trace, "setmclst");
S
Sachin Sant 已提交
1222 1223
	for (im4 = rcu_dereference(in4_dev->mc_list); im4 != NULL;
	     im4 = rcu_dereference(im4->next_rcu)) {
L
Linus Torvalds 已提交
1224 1225 1226 1227
		lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
		ipm = lcs_check_addr_entry(card, im4, buf);
		if (ipm != NULL)
			continue;	/* Address already in list. */
1228
		ipm = kzalloc(sizeof(struct lcs_ipm_list), GFP_ATOMIC);
L
Linus Torvalds 已提交
1229
		if (ipm == NULL) {
1230 1231
			pr_info("Not enough memory to add"
				" new multicast entry!\n");
L
Linus Torvalds 已提交
1232 1233 1234 1235 1236 1237
			break;
		}
		memcpy(&ipm->ipm.mac_addr, buf, LCS_MAC_LENGTH);
		ipm->ipm.ip_addr = im4->multiaddr;
		ipm->ipm_state = LCS_IPM_STATE_SET_REQUIRED;
		spin_lock_irqsave(&card->ipm_lock, flags);
1238
		LCS_DBF_HEX(2,trace,&ipm->ipm.ip_addr,4);
L
Linus Torvalds 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
		list_add(&ipm->list, &card->ipm_list);
		spin_unlock_irqrestore(&card->ipm_lock, flags);
	}
}

static int
lcs_register_mc_addresses(void *data)
{
	struct lcs_card *card;
	struct in_device *in4_dev;

	card = (struct lcs_card *) data;

	if (!lcs_do_run_thread(card, LCS_SET_MC_THREAD))
		return 0;
	LCS_DBF_TEXT(4, trace, "regmulti");

	in4_dev = in_dev_get(card->dev);
	if (in4_dev == NULL)
		goto out;
S
Sachin Sant 已提交
1259
	rcu_read_lock();
L
Linus Torvalds 已提交
1260 1261
	lcs_remove_mc_addresses(card,in4_dev);
	lcs_set_mc_addresses(card, in4_dev);
S
Sachin Sant 已提交
1262
	rcu_read_unlock();
L
Linus Torvalds 已提交
1263 1264
	in_dev_put(in4_dev);

1265 1266 1267
	netif_carrier_off(card->dev);
	netif_tx_disable(card->dev);
	wait_event(card->write.wait_q,
1268
			(card->write.state != LCS_CH_STATE_RUNNING));
L
Linus Torvalds 已提交
1269
	lcs_fix_multicast_list(card);
1270 1271 1272 1273
	if (card->state == DEV_STATE_UP) {
		netif_carrier_on(card->dev);
		netif_wake_queue(card->dev);
	}
L
Linus Torvalds 已提交
1274 1275 1276 1277
out:
	lcs_clear_thread_running_bit(card, LCS_SET_MC_THREAD);
	return 0;
}
1278 1279
#endif /* CONFIG_IP_MULTICAST */

L
Linus Torvalds 已提交
1280 1281 1282 1283 1284 1285 1286
/**
 * function called by net device to
 * handle multicast address relevant things
 */
static void
lcs_set_multicast_list(struct net_device *dev)
{
1287
#ifdef CONFIG_IP_MULTICAST
L
Linus Torvalds 已提交
1288 1289 1290
        struct lcs_card *card;

        LCS_DBF_TEXT(4, trace, "setmulti");
1291
        card = (struct lcs_card *) dev->ml_priv;
L
Linus Torvalds 已提交
1292

1293
        if (!lcs_set_thread_start_bit(card, LCS_SET_MC_THREAD))
L
Linus Torvalds 已提交
1294 1295
		schedule_work(&card->kernel_thread_starter);
#endif /* CONFIG_IP_MULTICAST */
1296
}
L
Linus Torvalds 已提交
1297 1298 1299 1300 1301 1302 1303 1304 1305

static long
lcs_check_irb_error(struct ccw_device *cdev, struct irb *irb)
{
	if (!IS_ERR(irb))
		return 0;

	switch (PTR_ERR(irb)) {
	case -EIO:
1306 1307
		dev_warn(&cdev->dev,
			"An I/O-error occurred on the LCS device\n");
L
Linus Torvalds 已提交
1308 1309 1310 1311
		LCS_DBF_TEXT(2, trace, "ckirberr");
		LCS_DBF_TEXT_(2, trace, "  rc%d", -EIO);
		break;
	case -ETIMEDOUT:
1312 1313
		dev_warn(&cdev->dev,
			"A command timed out on the LCS device\n");
L
Linus Torvalds 已提交
1314 1315 1316 1317
		LCS_DBF_TEXT(2, trace, "ckirberr");
		LCS_DBF_TEXT_(2, trace, "  rc%d", -ETIMEDOUT);
		break;
	default:
1318 1319 1320
		dev_warn(&cdev->dev,
			"An error occurred on the LCS device, rc=%ld\n",
			PTR_ERR(irb));
L
Linus Torvalds 已提交
1321 1322 1323 1324 1325 1326
		LCS_DBF_TEXT(2, trace, "ckirberr");
		LCS_DBF_TEXT(2, trace, "  rc???");
	}
	return PTR_ERR(irb);
}

1327 1328 1329 1330 1331 1332 1333
static int
lcs_get_problem(struct ccw_device *cdev, struct irb *irb)
{
	int dstat, cstat;
	char *sense;

	sense = (char *) irb->ecw;
1334 1335
	cstat = irb->scsw.cmd.cstat;
	dstat = irb->scsw.cmd.dstat;
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366

	if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
		     SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
		     SCHN_STAT_PROT_CHECK   | SCHN_STAT_PROG_CHECK)) {
		LCS_DBF_TEXT(2, trace, "CGENCHK");
		return 1;
	}
	if (dstat & DEV_STAT_UNIT_CHECK) {
		if (sense[LCS_SENSE_BYTE_1] &
		    LCS_SENSE_RESETTING_EVENT) {
			LCS_DBF_TEXT(2, trace, "REVIND");
			return 1;
		}
		if (sense[LCS_SENSE_BYTE_0] &
		    LCS_SENSE_CMD_REJECT) {
			LCS_DBF_TEXT(2, trace, "CMDREJ");
			return 0;
		}
		if ((!sense[LCS_SENSE_BYTE_0]) &&
		    (!sense[LCS_SENSE_BYTE_1]) &&
		    (!sense[LCS_SENSE_BYTE_2]) &&
		    (!sense[LCS_SENSE_BYTE_3])) {
			LCS_DBF_TEXT(2, trace, "ZEROSEN");
			return 0;
		}
		LCS_DBF_TEXT(2, trace, "DGENCHK");
		return 1;
	}
	return 0;
}

1367
static void
1368 1369 1370 1371 1372 1373
lcs_schedule_recovery(struct lcs_card *card)
{
	LCS_DBF_TEXT(2, trace, "startrec");
	if (!lcs_set_thread_start_bit(card, LCS_RECOVERY_THREAD))
		schedule_work(&card->kernel_thread_starter);
}
L
Linus Torvalds 已提交
1374 1375 1376 1377 1378 1379 1380 1381 1382

/**
 * IRQ Handler for LCS channels
 */
static void
lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
{
	struct lcs_card *card;
	struct lcs_channel *channel;
1383 1384
	int rc, index;
	int cstat, dstat;
L
Linus Torvalds 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394

	if (lcs_check_irb_error(cdev, irb))
		return;

	card = CARD_FROM_DEV(cdev);
	if (card->read.ccwdev == cdev)
		channel = &card->read;
	else
		channel = &card->write;

1395 1396
	cstat = irb->scsw.cmd.cstat;
	dstat = irb->scsw.cmd.dstat;
1397
	LCS_DBF_TEXT_(5, trace, "Rint%s", dev_name(&cdev->dev));
1398 1399 1400 1401
	LCS_DBF_TEXT_(5, trace, "%4x%4x", irb->scsw.cmd.cstat,
		      irb->scsw.cmd.dstat);
	LCS_DBF_TEXT_(5, trace, "%4x%4x", irb->scsw.cmd.fctl,
		      irb->scsw.cmd.actl);
L
Linus Torvalds 已提交
1402

1403 1404 1405
	/* Check for channel and device errors presented */
	rc = lcs_get_problem(cdev, irb);
	if (rc || (dstat & DEV_STAT_UNIT_EXCEP)) {
1406 1407 1408 1409
		dev_warn(&cdev->dev,
			"The LCS device stopped because of an error,"
			" dstat=0x%X, cstat=0x%X \n",
			    dstat, cstat);
1410
		if (rc) {
1411
			channel->state = LCS_CH_STATE_ERROR;
1412 1413
		}
	}
1414 1415 1416 1417 1418
	if (channel->state == LCS_CH_STATE_ERROR) {
		lcs_schedule_recovery(card);
		wake_up(&card->wait_q);
		return;
	}
L
Linus Torvalds 已提交
1419
	/* How far in the ccw chain have we processed? */
1420
	if ((channel->state != LCS_CH_STATE_INIT) &&
1421 1422
	    (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
	    (irb->scsw.cmd.cpa != 0)) {
1423
		index = (struct ccw1 *) __va((addr_t) irb->scsw.cmd.cpa)
L
Linus Torvalds 已提交
1424
			- channel->ccws;
1425 1426
		if ((irb->scsw.cmd.actl & SCSW_ACTL_SUSPENDED) ||
		    (irb->scsw.cmd.cstat & SCHN_STAT_PCI))
L
Linus Torvalds 已提交
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
			/* 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);
		}
	}

1437 1438 1439
	if ((irb->scsw.cmd.dstat & DEV_STAT_DEV_END) ||
	    (irb->scsw.cmd.dstat & DEV_STAT_CHN_END) ||
	    (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK))
L
Linus Torvalds 已提交
1440
		/* Mark channel as stopped. */
1441
		channel->state = LCS_CH_STATE_STOPPED;
1442
	else if (irb->scsw.cmd.actl & SCSW_ACTL_SUSPENDED)
L
Linus Torvalds 已提交
1443
		/* CCW execution stopped on a suspend bit. */
1444
		channel->state = LCS_CH_STATE_SUSPENDED;
1445 1446
	if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
		if (irb->scsw.cmd.cc != 0) {
L
Linus Torvalds 已提交
1447 1448 1449 1450
			ccw_device_halt(channel->ccwdev, (addr_t) channel);
			return;
		}
		/* The channel has been stopped by halt_IO. */
1451
		channel->state = LCS_CH_STATE_HALTED;
L
Linus Torvalds 已提交
1452
	}
1453
	if (irb->scsw.cmd.fctl & SCSW_FCTL_CLEAR_FUNC)
1454
		channel->state = LCS_CH_STATE_CLEARED;
L
Linus Torvalds 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
	/* Do the rest in the tasklet. */
	tasklet_schedule(&channel->irq_tasklet);
}

/**
 * Tasklet for IRQ handler
 */
static void
lcs_tasklet(unsigned long data)
{
	unsigned long flags;
	struct lcs_channel *channel;
	struct lcs_buffer *iob;
	int buf_idx;

	channel = (struct lcs_channel *) data;
1471
	LCS_DBF_TEXT_(5, trace, "tlet%s", dev_name(&channel->ccwdev->dev));
L
Linus Torvalds 已提交
1472 1473 1474 1475

	/* Check for processed buffers. */
	iob = channel->iob;
	buf_idx = channel->buf_idx;
1476
	while (iob[buf_idx].state == LCS_BUF_STATE_PROCESSED) {
L
Linus Torvalds 已提交
1477 1478 1479 1480 1481 1482 1483
		/* 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;

1484
	if (channel->state == LCS_CH_STATE_STOPPED)
H
Heiko Carstens 已提交
1485
		lcs_start_channel(channel);
L
Linus Torvalds 已提交
1486
	spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
1487
	if (channel->state == LCS_CH_STATE_SUSPENDED &&
H
Heiko Carstens 已提交
1488 1489
	    channel->iob[channel->io_idx].state == LCS_BUF_STATE_READY)
		__lcs_resume_channel(channel);
L
Linus Torvalds 已提交
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
	spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);

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

/**
 * Finish current tx buffer and make it ready for transmit.
 */
static void
__lcs_emit_txbuffer(struct lcs_card *card)
{
	LCS_DBF_TEXT(5, trace, "emittx");
	*(__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++;
}

/**
 * Callback for finished tx buffers.
 */
static void
lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
{
	struct lcs_card *card;

	LCS_DBF_TEXT(5, trace, "txbuffcb");
	/* Put buffer back to pool. */
	lcs_release_buffer(channel, buffer);
1521
	card = container_of(channel, struct lcs_card, write);
1522
	if (netif_queue_stopped(card->dev) && netif_carrier_ok(card->dev))
1523
		netif_wake_queue(card->dev);
L
Linus Torvalds 已提交
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
	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);
}

/**
 * Packet transmit function called by network stack
 */
static int
__lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
		 struct net_device *dev)
{
	struct lcs_header *header;
1543
	int rc = NETDEV_TX_OK;
L
Linus Torvalds 已提交
1544 1545 1546 1547 1548

	LCS_DBF_TEXT(5, trace, "hardxmit");
	if (skb == NULL) {
		card->stats.tx_dropped++;
		card->stats.tx_errors++;
1549
		return NETDEV_TX_OK;
L
Linus Torvalds 已提交
1550 1551 1552 1553 1554 1555
	}
	if (card->state != DEV_STATE_UP) {
		dev_kfree_skb(skb);
		card->stats.tx_dropped++;
		card->stats.tx_errors++;
		card->stats.tx_carrier_errors++;
1556
		return NETDEV_TX_OK;
L
Linus Torvalds 已提交
1557
	}
1558 1559
	if (skb->protocol == htons(ETH_P_IPV6)) {
		dev_kfree_skb(skb);
1560
		return NETDEV_TX_OK;
1561
	}
1562 1563
	netif_stop_queue(card->dev);
	spin_lock(&card->lock);
L
Linus Torvalds 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
	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) {
			card->stats.tx_dropped++;
1574
			rc = NETDEV_TX_BUSY;
1575
			goto out;
L
Linus Torvalds 已提交
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
		}
		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;
1586
	skb_copy_from_linear_data(skb, header + 1, skb->len);
1587
	spin_unlock(&card->lock);
L
Linus Torvalds 已提交
1588 1589 1590
	card->stats.tx_bytes += skb->len;
	card->stats.tx_packets++;
	dev_kfree_skb(skb);
1591 1592 1593
	netif_wake_queue(card->dev);
	spin_lock(&card->lock);
	if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
L
Linus Torvalds 已提交
1594 1595
		/* If this is the first tx buffer emit it immediately. */
		__lcs_emit_txbuffer(card);
1596 1597 1598
out:
	spin_unlock(&card->lock);
	return rc;
L
Linus Torvalds 已提交
1599 1600 1601 1602 1603 1604 1605 1606 1607
}

static int
lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct lcs_card *card;
	int rc;

	LCS_DBF_TEXT(5, trace, "pktxmit");
1608
	card = (struct lcs_card *) dev->ml_priv;
L
Linus Torvalds 已提交
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
	rc = __lcs_start_xmit(card, skb, dev);
	return rc;
}

/**
 * send startlan and lanstat command to make LCS device ready
 */
static int
lcs_startlan_auto(struct lcs_card *card)
{
	int rc;

	LCS_DBF_TEXT(2, trace, "strtauto");
1622
#ifdef CONFIG_ETHERNET
L
Linus Torvalds 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
	card->lan_type = LCS_FRAME_TYPE_ENET;
	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;
}

static int
lcs_startlan(struct lcs_card *card)
{
	int rc, i;

	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 <= 16; 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;
}

/**
 * LCS detect function
 * setup channels and make them I/O ready
 */
static int
lcs_detect(struct lcs_card *card)
{
	int rc = 0;

	LCS_DBF_TEXT(2, setup, "lcsdetct");
	/* start/reset card */
	if (card->dev)
		netif_stop_queue(card->dev);
	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;
1694 1695
		card->write.state = LCS_CH_STATE_INIT;
		card->read.state =  LCS_CH_STATE_INIT;
L
Linus Torvalds 已提交
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
	}
	return rc;
}

/**
 * LCS Stop card
 */
static int
lcs_stopcard(struct lcs_card *card)
{
	int rc;

	LCS_DBF_TEXT(3, setup, "stopcard");

1710 1711
	if (card->read.state != LCS_CH_STATE_STOPPED &&
	    card->write.state != LCS_CH_STATE_STOPPED &&
1712 1713
	    card->read.state != LCS_CH_STATE_ERROR &&
	    card->write.state != LCS_CH_STATE_ERROR &&
L
Linus Torvalds 已提交
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
	    card->state == DEV_STATE_UP) {
		lcs_clear_multicast_list(card);
		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;
}

/**
 * Kernel Thread helper functions for LGW initiated commands
 */
static void
1729
lcs_start_kernel_thread(struct work_struct *work)
L
Linus Torvalds 已提交
1730
{
1731
	struct lcs_card *card = container_of(work, struct lcs_card, kernel_thread_starter);
L
Linus Torvalds 已提交
1732
	LCS_DBF_TEXT(5, trace, "krnthrd");
1733
	if (lcs_do_start_thread(card, LCS_RECOVERY_THREAD))
1734
		kthread_run(lcs_recovery, card, "lcs_recover");
L
Linus Torvalds 已提交
1735 1736
#ifdef CONFIG_IP_MULTICAST
	if (lcs_do_start_thread(card, LCS_SET_MC_THREAD))
1737
		kthread_run(lcs_register_mc_addresses, card, "regipm");
L
Linus Torvalds 已提交
1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751
#endif
}

/**
 * Process control frames.
 */
static void
lcs_get_control(struct lcs_card *card, struct lcs_cmd *cmd)
{
	LCS_DBF_TEXT(5, trace, "getctrl");
	if (cmd->initiator == LCS_INITIATOR_LGW) {
		switch(cmd->cmd_code) {
		case LCS_CMD_STARTUP:
		case LCS_CMD_STARTLAN:
1752
			lcs_schedule_recovery(card);
L
Linus Torvalds 已提交
1753 1754
			break;
		case LCS_CMD_STOPLAN:
1755 1756
			pr_warn("Stoplan for %s initiated by LGW\n",
				card->dev->name);
1757 1758
			if (card->dev)
				netif_carrier_off(card->dev);
L
Linus Torvalds 已提交
1759 1760
			break;
		default:
1761
			LCS_DBF_TEXT(5, trace, "noLGWcmd");
L
Linus Torvalds 已提交
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
			break;
		}
	} else
		lcs_notify_lancmd_waiters(card, cmd);
}

/**
 * Unpack network packet.
 */
static void
lcs_get_skb(struct lcs_card *card, char *skb_data, unsigned int skb_len)
{
	struct sk_buff *skb;

	LCS_DBF_TEXT(5, trace, "getskb");
	if (card->dev == NULL ||
	    card->state != DEV_STATE_UP)
		/* The card isn't up. Ignore the packet. */
		return;

	skb = dev_alloc_skb(skb_len);
	if (skb == NULL) {
1784 1785
		dev_err(&card->dev->dev,
			" Allocating a socket buffer to interface %s failed\n",
L
Linus Torvalds 已提交
1786 1787 1788 1789
			  card->dev->name);
		card->stats.rx_dropped++;
		return;
	}
1790
	skb_put_data(skb, skb_data, skb_len);
L
Linus Torvalds 已提交
1791 1792 1793
	skb->protocol =	card->lan_type_trans(skb, card->dev);
	card->stats.rx_bytes += skb_len;
	card->stats.rx_packets++;
1794 1795
	if (skb->protocol == htons(ETH_P_802_2))
		*((__u32 *)skb->cb) = ++card->pkt_seq;
L
Linus Torvalds 已提交
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
	netif_rx(skb);
}

/**
 * LCS main routine to get packets and lancmd replies from the buffers
 */
static void
lcs_get_frames_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
{
	struct lcs_card *card;
	struct lcs_header *lcs_hdr;
	__u16 offset;

	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;
	}
1815
	card = container_of(channel, struct lcs_card, read);
L
Linus Torvalds 已提交
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
	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;
		}
		/* 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);
	}
	/* The buffer is now empty. Make it ready again. */
	lcs_ready_buffer(&card->read, buffer);
}

/**
 * get network statistics for ifconfig and other user programs
 */
static struct net_device_stats *
lcs_getstats(struct net_device *dev)
{
	struct lcs_card *card;

	LCS_DBF_TEXT(4, trace, "netstats");
1858
	card = (struct lcs_card *) dev->ml_priv;
L
Linus Torvalds 已提交
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
	return &card->stats;
}

/**
 * stop lcs device
 * This function will be called by user doing ifconfig xxx down
 */
static int
lcs_stop_device(struct net_device *dev)
{
	struct lcs_card *card;
	int rc;

	LCS_DBF_TEXT(2, trace, "stopdev");
1873
	card   = (struct lcs_card *) dev->ml_priv;
1874 1875
	netif_carrier_off(dev);
	netif_tx_disable(dev);
L
Linus Torvalds 已提交
1876
	dev->flags &= ~IFF_UP;
1877
	wait_event(card->write.wait_q,
1878
		(card->write.state != LCS_CH_STATE_RUNNING));
L
Linus Torvalds 已提交
1879 1880
	rc = lcs_stopcard(card);
	if (rc)
1881
		dev_err(&card->dev->dev,
1882
			" Shutting down the LCS device failed\n");
L
Linus Torvalds 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
	return rc;
}

/**
 * start lcs device and make it runnable
 * This function will be called by user doing ifconfig xxx up
 */
static int
lcs_open_device(struct net_device *dev)
{
	struct lcs_card *card;
	int rc;

	LCS_DBF_TEXT(2, trace, "opendev");
1897
	card = (struct lcs_card *) dev->ml_priv;
L
Linus Torvalds 已提交
1898 1899 1900
	/* initialize statistics */
	rc = lcs_detect(card);
	if (rc) {
1901
		pr_err("Error in opening device!\n");
L
Linus Torvalds 已提交
1902 1903 1904

	} else {
		dev->flags |= IFF_UP;
1905
		netif_carrier_on(dev);
L
Linus Torvalds 已提交
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
		netif_wake_queue(dev);
		card->state = DEV_STATE_UP;
	}
	return rc;
}

/**
 * show function for portno called by cat or similar things
 */
static ssize_t
1916
lcs_portno_show (struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1917 1918 1919
{
        struct lcs_card *card;

1920
	card = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931

        if (!card)
                return 0;

        return sprintf(buf, "%d\n", card->portno);
}

/**
 * store the value which is piped to file portno
 */
static ssize_t
1932
lcs_portno_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
1933 1934
{
        struct lcs_card *card;
1935 1936
	int rc;
	s16 value;
L
Linus Torvalds 已提交
1937

1938
	card = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
1939 1940 1941 1942

        if (!card)
                return 0;

1943 1944
	rc = kstrtos16(buf, 0, &value);
	if (rc)
1945
		return -EINVAL;
L
Linus Torvalds 已提交
1946 1947 1948 1949 1950 1951 1952 1953 1954
        /* TODO: sanity checks */
        card->portno = value;

        return count;

}

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

1955
static const char *lcs_type[] = {
1956 1957 1958 1959 1960 1961 1962 1963
	"not a channel",
	"2216 parallel",
	"2216 channel",
	"OSA LCS card",
	"unknown channel type",
	"unsupported channel type",
};

L
Linus Torvalds 已提交
1964
static ssize_t
1965
lcs_type_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1966 1967 1968 1969 1970 1971 1972
{
	struct ccwgroup_device *cgdev;

	cgdev = to_ccwgroupdev(dev);
	if (!cgdev)
		return -ENODEV;

1973
	return sprintf(buf, "%s\n", lcs_type[cgdev->cdev[0]->id.driver_info]);
L
Linus Torvalds 已提交
1974 1975 1976 1977 1978
}

static DEVICE_ATTR(type, 0444, lcs_type_show, NULL);

static ssize_t
1979
lcs_timeout_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1980 1981 1982
{
	struct lcs_card *card;

1983
	card = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
1984 1985 1986 1987 1988

	return card ? sprintf(buf, "%u\n", card->lancmd_timeout) : 0;
}

static ssize_t
1989
lcs_timeout_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
1990 1991
{
        struct lcs_card *card;
1992 1993
	unsigned int value;
	int rc;
L
Linus Torvalds 已提交
1994

1995
	card = dev_get_drvdata(dev);
L
Linus Torvalds 已提交
1996 1997 1998 1999

        if (!card)
                return 0;

2000 2001
	rc = kstrtouint(buf, 0, &value);
	if (rc)
2002
		return -EINVAL;
L
Linus Torvalds 已提交
2003 2004 2005 2006 2007 2008 2009
        /* TODO: sanity checks */
        card->lancmd_timeout = value;

        return count;

}

2010
static DEVICE_ATTR(lancmd_timeout, 0644, lcs_timeout_show, lcs_timeout_store);
L
Linus Torvalds 已提交
2011

2012
static ssize_t
2013
lcs_dev_recover_store(struct device *dev, struct device_attribute *attr,
2014 2015
		      const char *buf, size_t count)
{
2016
	struct lcs_card *card = dev_get_drvdata(dev);
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
	char *tmp;
	int i;

	if (!card)
		return -EINVAL;
	if (card->state != DEV_STATE_UP)
		return -EPERM;
	i = simple_strtoul(buf, &tmp, 16);
	if (i == 1)
		lcs_schedule_recovery(card);
	return count;
}

static DEVICE_ATTR(recover, 0200, NULL, lcs_dev_recover_store);

L
Linus Torvalds 已提交
2032 2033 2034 2035
static struct attribute * lcs_attrs[] = {
	&dev_attr_portno.attr,
	&dev_attr_type.attr,
	&dev_attr_lancmd_timeout.attr,
2036
	&dev_attr_recover.attr,
L
Linus Torvalds 已提交
2037 2038 2039 2040 2041
	NULL,
};
static struct attribute_group lcs_attr_group = {
	.attrs = lcs_attrs,
};
2042 2043 2044 2045 2046 2047 2048 2049
static const struct attribute_group *lcs_attr_groups[] = {
	&lcs_attr_group,
	NULL,
};
static const struct device_type lcs_devtype = {
	.name = "lcs",
	.groups = lcs_attr_groups,
};
L
Linus Torvalds 已提交
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064

/**
 * lcs_probe_device is called on establishing a new ccwgroup_device.
 */
static int
lcs_probe_device(struct ccwgroup_device *ccwgdev)
{
	struct lcs_card *card;

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

	LCS_DBF_TEXT(2, setup, "add_dev");
        card = lcs_alloc_card();
        if (!card) {
2065
		LCS_DBF_TEXT_(2, setup, "  rc%d", -ENOMEM);
L
Linus Torvalds 已提交
2066 2067 2068
		put_device(&ccwgdev->dev);
                return -ENOMEM;
        }
2069
	dev_set_drvdata(&ccwgdev->dev, card);
L
Linus Torvalds 已提交
2070 2071
	ccwgdev->cdev[0]->handler = lcs_irq;
	ccwgdev->cdev[1]->handler = lcs_irq;
2072
	card->gdev = ccwgdev;
2073
	INIT_WORK(&card->kernel_thread_starter, lcs_start_kernel_thread);
2074 2075 2076
	card->thread_start_mask = 0;
	card->thread_allowed_mask = 0;
	card->thread_running_mask = 0;
2077 2078 2079
	ccwgdev->dev.type = &lcs_devtype;

	return 0;
L
Linus Torvalds 已提交
2080 2081 2082 2083 2084 2085 2086 2087
}

static int
lcs_register_netdev(struct ccwgroup_device *ccwgdev)
{
	struct lcs_card *card;

	LCS_DBF_TEXT(2, setup, "regnetdv");
2088
	card = dev_get_drvdata(&ccwgdev->dev);
L
Linus Torvalds 已提交
2089 2090 2091 2092 2093 2094 2095 2096 2097
	if (card->dev->reg_state != NETREG_UNINITIALIZED)
		return 0;
	SET_NETDEV_DEV(card->dev, &ccwgdev->dev);
	return register_netdev(card->dev);
}

/**
 * lcs_new_device will be called by setting the group device online.
 */
F
Frank Blaschka 已提交
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
static const struct net_device_ops lcs_netdev_ops = {
	.ndo_open		= lcs_open_device,
	.ndo_stop		= lcs_stop_device,
	.ndo_get_stats		= lcs_getstats,
	.ndo_start_xmit		= lcs_start_xmit,
};

static const struct net_device_ops lcs_mc_netdev_ops = {
	.ndo_open		= lcs_open_device,
	.ndo_stop		= lcs_stop_device,
	.ndo_get_stats		= lcs_getstats,
	.ndo_start_xmit		= lcs_start_xmit,
2110
	.ndo_set_rx_mode	= lcs_set_multicast_list,
F
Frank Blaschka 已提交
2111
};
L
Linus Torvalds 已提交
2112 2113 2114 2115 2116 2117 2118 2119 2120

static int
lcs_new_device(struct ccwgroup_device *ccwgdev)
{
	struct  lcs_card *card;
	struct net_device *dev=NULL;
	enum lcs_dev_states recover_state;
	int rc;

2121
	card = dev_get_drvdata(&ccwgdev->dev);
L
Linus Torvalds 已提交
2122 2123 2124 2125 2126 2127 2128 2129 2130
	if (!card)
		return -ENODEV;

	LCS_DBF_TEXT(2, setup, "newdev");
	LCS_DBF_HEX(3, setup, &card, sizeof(void*));
	card->read.ccwdev  = ccwgdev->cdev[0];
	card->write.ccwdev = ccwgdev->cdev[1];

	recover_state = card->state;
2131 2132 2133 2134 2135 2136
	rc = ccw_device_set_online(card->read.ccwdev);
	if (rc)
		goto out_err;
	rc = ccw_device_set_online(card->write.ccwdev);
	if (rc)
		goto out_werr;
L
Linus Torvalds 已提交
2137 2138 2139 2140 2141 2142 2143

	LCS_DBF_TEXT(3, setup, "lcsnewdv");

	lcs_setup_card(card);
	rc = lcs_detect(card);
	if (rc) {
		LCS_DBF_TEXT(2, setup, "dtctfail");
2144
		dev_err(&ccwgdev->dev,
2145 2146
			"Detecting a network adapter for LCS devices"
			" failed with rc=%d (0x%x)\n", rc, rc);
L
Linus Torvalds 已提交
2147 2148 2149 2150 2151 2152 2153 2154 2155
		lcs_stopcard(card);
		goto out;
	}
	if (card->dev) {
		LCS_DBF_TEXT(2, setup, "samedev");
		LCS_DBF_HEX(3, setup, &card, sizeof(void*));
		goto netdev_out;
	}
	switch (card->lan_type) {
2156
#ifdef CONFIG_ETHERNET
L
Linus Torvalds 已提交
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
	case LCS_FRAME_TYPE_ENET:
		card->lan_type_trans = eth_type_trans;
		dev = alloc_etherdev(0);
		break;
#endif
#ifdef CONFIG_FDDI
	case LCS_FRAME_TYPE_FDDI:
		card->lan_type_trans = fddi_type_trans;
		dev = alloc_fddidev(0);
		break;
#endif
	default:
		LCS_DBF_TEXT(3, setup, "errinit");
2170
		pr_err(" Initialization failed\n");
L
Linus Torvalds 已提交
2171 2172 2173 2174 2175
		goto out;
	}
	if (!dev)
		goto out;
	card->dev = dev;
2176
	card->dev->ml_priv = card;
F
Frank Blaschka 已提交
2177
	card->dev->netdev_ops = &lcs_netdev_ops;
L
Linus Torvalds 已提交
2178 2179 2180
	memcpy(card->dev->dev_addr, card->mac, LCS_MAC_LENGTH);
#ifdef CONFIG_IP_MULTICAST
	if (!lcs_check_multicast_support(card))
F
Frank Blaschka 已提交
2181
		card->dev->netdev_ops = &lcs_mc_netdev_ops;
L
Linus Torvalds 已提交
2182
#endif
2183
netdev_out:
L
Linus Torvalds 已提交
2184 2185 2186 2187
	lcs_set_allowed_threads(card,0xffffffff);
	if (recover_state == DEV_STATE_RECOVER) {
		lcs_set_multicast_list(card->dev);
		card->dev->flags |= IFF_UP;
2188
		netif_carrier_on(card->dev);
L
Linus Torvalds 已提交
2189 2190
		netif_wake_queue(card->dev);
		card->state = DEV_STATE_UP;
2191
	} else {
L
Linus Torvalds 已提交
2192
		lcs_stopcard(card);
2193
	}
L
Linus Torvalds 已提交
2194

2195 2196 2197 2198
	if (lcs_register_netdev(ccwgdev) != 0)
		goto out;

	/* Print out supported assists: IPv6 */
2199 2200 2201
	pr_info("LCS device %s %s IPv6 support\n", card->dev->name,
		(card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
		"with" : "without");
2202
	/* Print out supported assist: Multicast */
2203 2204 2205
	pr_info("LCS device %s %s Multicast support\n", card->dev->name,
		(card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
		"with" : "without");
L
Linus Torvalds 已提交
2206 2207 2208 2209
	return 0;
out:

	ccw_device_set_offline(card->write.ccwdev);
2210 2211 2212
out_werr:
	ccw_device_set_offline(card->read.ccwdev);
out_err:
L
Linus Torvalds 已提交
2213 2214 2215 2216 2217 2218 2219
	return -ENODEV;
}

/**
 * lcs_shutdown_device, called when setting the group device offline.
 */
static int
2220
__lcs_shutdown_device(struct ccwgroup_device *ccwgdev, int recovery_mode)
L
Linus Torvalds 已提交
2221 2222 2223
{
	struct lcs_card *card;
	enum lcs_dev_states recover_state;
2224
	int ret = 0, ret2 = 0, ret3 = 0;
L
Linus Torvalds 已提交
2225 2226

	LCS_DBF_TEXT(3, setup, "shtdndev");
2227
	card = dev_get_drvdata(&ccwgdev->dev);
L
Linus Torvalds 已提交
2228 2229
	if (!card)
		return -ENODEV;
2230 2231 2232 2233 2234
	if (recovery_mode == 0) {
		lcs_set_allowed_threads(card, 0);
		if (lcs_wait_for_threads(card, LCS_SET_MC_THREAD))
			return -ERESTARTSYS;
	}
L
Linus Torvalds 已提交
2235 2236 2237 2238
	LCS_DBF_HEX(3, setup, &card, sizeof(void*));
	recover_state = card->state;

	ret = lcs_stop_device(card->dev);
2239 2240 2241 2242 2243 2244
	ret2 = ccw_device_set_offline(card->read.ccwdev);
	ret3 = ccw_device_set_offline(card->write.ccwdev);
	if (!ret)
		ret = (ret2) ? ret2 : ret3;
	if (ret)
		LCS_DBF_TEXT_(3, setup, "1err:%d", ret);
L
Linus Torvalds 已提交
2245 2246 2247 2248 2249 2250
	if (recover_state == DEV_STATE_UP) {
		card->state = DEV_STATE_RECOVER;
	}
	return 0;
}

2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
static int
lcs_shutdown_device(struct ccwgroup_device *ccwgdev)
{
	return __lcs_shutdown_device(ccwgdev, 0);
}

/**
 * drive lcs recovery after startup and startlan initiated by Lan Gateway
 */
static int
lcs_recovery(void *ptr)
{
	struct lcs_card *card;
	struct ccwgroup_device *gdev;
        int rc;

	card = (struct lcs_card *) ptr;

	LCS_DBF_TEXT(4, trace, "recover1");
	if (!lcs_do_run_thread(card, LCS_RECOVERY_THREAD))
		return 0;
	LCS_DBF_TEXT(4, trace, "recover2");
	gdev = card->gdev;
2274 2275
	dev_warn(&gdev->dev,
		"A recovery process has been started for the LCS device\n");
2276 2277 2278
	rc = __lcs_shutdown_device(gdev, 1);
	rc = lcs_new_device(gdev);
	if (!rc)
2279 2280
		pr_info("Device %s successfully recovered!\n",
			card->dev->name);
2281
	else
2282 2283
		pr_info("Device %s could not be recovered!\n",
			card->dev->name);
2284 2285 2286 2287
	lcs_clear_thread_running_bit(card, LCS_RECOVERY_THREAD);
	return 0;
}

L
Linus Torvalds 已提交
2288 2289 2290 2291 2292 2293 2294 2295
/**
 * lcs_remove_device, free buffers and card
 */
static void
lcs_remove_device(struct ccwgroup_device *ccwgdev)
{
	struct lcs_card *card;

2296
	card = dev_get_drvdata(&ccwgdev->dev);
L
Linus Torvalds 已提交
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
	if (!card)
		return;

	LCS_DBF_TEXT(3, setup, "remdev");
	LCS_DBF_HEX(3, setup, &card, sizeof(void*));
	if (ccwgdev->state == CCWGROUP_ONLINE) {
		lcs_shutdown_device(ccwgdev);
	}
	if (card->dev)
		unregister_netdev(card->dev);
	lcs_cleanup_card(card);
	lcs_free_card(card);
2309
	dev_set_drvdata(&ccwgdev->dev, NULL);
L
Linus Torvalds 已提交
2310 2311 2312
	put_device(&ccwgdev->dev);
}

2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366
static int lcs_pm_suspend(struct lcs_card *card)
{
	if (card->dev)
		netif_device_detach(card->dev);
	lcs_set_allowed_threads(card, 0);
	lcs_wait_for_threads(card, 0xffffffff);
	if (card->state != DEV_STATE_DOWN)
		__lcs_shutdown_device(card->gdev, 1);
	return 0;
}

static int lcs_pm_resume(struct lcs_card *card)
{
	int rc = 0;

	if (card->state == DEV_STATE_RECOVER)
		rc = lcs_new_device(card->gdev);
	if (card->dev)
		netif_device_attach(card->dev);
	if (rc) {
		dev_warn(&card->gdev->dev, "The lcs device driver "
			"failed to recover the device\n");
	}
	return rc;
}

static int lcs_prepare(struct ccwgroup_device *gdev)
{
	return 0;
}

static void lcs_complete(struct ccwgroup_device *gdev)
{
	return;
}

static int lcs_freeze(struct ccwgroup_device *gdev)
{
	struct lcs_card *card = dev_get_drvdata(&gdev->dev);
	return lcs_pm_suspend(card);
}

static int lcs_thaw(struct ccwgroup_device *gdev)
{
	struct lcs_card *card = dev_get_drvdata(&gdev->dev);
	return lcs_pm_resume(card);
}

static int lcs_restore(struct ccwgroup_device *gdev)
{
	struct lcs_card *card = dev_get_drvdata(&gdev->dev);
	return lcs_pm_resume(card);
}

2367 2368 2369 2370 2371 2372 2373 2374 2375
static struct ccw_device_id lcs_ids[] = {
	{CCW_DEVICE(0x3088, 0x08), .driver_info = lcs_channel_type_parallel},
	{CCW_DEVICE(0x3088, 0x1f), .driver_info = lcs_channel_type_2216},
	{CCW_DEVICE(0x3088, 0x60), .driver_info = lcs_channel_type_osa2},
	{},
};
MODULE_DEVICE_TABLE(ccw, lcs_ids);

static struct ccw_driver lcs_ccw_driver = {
2376 2377 2378 2379
	.driver = {
		.owner	= THIS_MODULE,
		.name	= "lcs",
	},
2380 2381 2382
	.ids	= lcs_ids,
	.probe	= ccwgroup_probe_ccwdev,
	.remove	= ccwgroup_remove_ccwdev,
2383
	.int_class = IRQIO_LCS,
2384 2385
};

L
Linus Torvalds 已提交
2386 2387 2388 2389
/**
 * LCS ccwgroup driver registration
 */
static struct ccwgroup_driver lcs_group_driver = {
2390 2391 2392 2393
	.driver = {
		.owner	= THIS_MODULE,
		.name	= "lcs",
	},
2394
	.ccw_driver  = &lcs_ccw_driver,
2395
	.setup	     = lcs_probe_device,
L
Linus Torvalds 已提交
2396 2397 2398
	.remove      = lcs_remove_device,
	.set_online  = lcs_new_device,
	.set_offline = lcs_shutdown_device,
2399 2400 2401 2402 2403
	.prepare     = lcs_prepare,
	.complete    = lcs_complete,
	.freeze	     = lcs_freeze,
	.thaw	     = lcs_thaw,
	.restore     = lcs_restore,
L
Linus Torvalds 已提交
2404 2405
};

2406 2407
static ssize_t group_store(struct device_driver *ddrv, const char *buf,
			   size_t count)
2408 2409
{
	int err;
2410
	err = ccwgroup_create_dev(lcs_root_dev, &lcs_group_driver, 2, buf);
2411 2412
	return err ? err : count;
}
2413
static DRIVER_ATTR_WO(group);
2414

S
Sebastian Ott 已提交
2415
static struct attribute *lcs_drv_attrs[] = {
2416 2417 2418
	&driver_attr_group.attr,
	NULL,
};
S
Sebastian Ott 已提交
2419 2420
static struct attribute_group lcs_drv_attr_group = {
	.attrs = lcs_drv_attrs,
2421
};
S
Sebastian Ott 已提交
2422 2423
static const struct attribute_group *lcs_drv_attr_groups[] = {
	&lcs_drv_attr_group,
2424 2425 2426
	NULL,
};

L
Linus Torvalds 已提交
2427 2428 2429 2430 2431 2432 2433 2434
/**
 *  LCS Module/Kernel initialization function
 */
static int
__init lcs_init_module(void)
{
	int rc;

2435
	pr_info("Loading %s\n", version);
L
Linus Torvalds 已提交
2436 2437
	rc = lcs_register_debug_facility();
	LCS_DBF_TEXT(0, setup, "lcsinit");
2438 2439 2440
	if (rc)
		goto out_err;
	lcs_root_dev = root_device_register("lcs");
2441
	rc = PTR_ERR_OR_ZERO(lcs_root_dev);
2442 2443 2444 2445 2446
	if (rc)
		goto register_err;
	rc = ccw_driver_register(&lcs_ccw_driver);
	if (rc)
		goto ccw_err;
S
Sebastian Ott 已提交
2447
	lcs_group_driver.driver.groups = lcs_drv_attr_groups;
2448 2449 2450
	rc = ccwgroup_driver_register(&lcs_group_driver);
	if (rc)
		goto ccwgroup_err;
L
Linus Torvalds 已提交
2451
	return 0;
2452 2453 2454 2455 2456 2457 2458 2459 2460 2461

ccwgroup_err:
	ccw_driver_unregister(&lcs_ccw_driver);
ccw_err:
	root_device_unregister(lcs_root_dev);
register_err:
	lcs_unregister_debug_facility();
out_err:
	pr_err("Initializing the lcs device driver failed\n");
	return rc;
L
Linus Torvalds 已提交
2462 2463 2464 2465 2466 2467 2468 2469 2470
}


/**
 *  LCS module cleanup function
 */
static void
__exit lcs_cleanup_module(void)
{
2471
	pr_info("Terminating lcs module.\n");
L
Linus Torvalds 已提交
2472
	LCS_DBF_TEXT(0, trace, "cleanup");
2473 2474 2475
	ccwgroup_driver_unregister(&lcs_group_driver);
	ccw_driver_unregister(&lcs_ccw_driver);
	root_device_unregister(lcs_root_dev);
L
Linus Torvalds 已提交
2476 2477 2478 2479 2480 2481
	lcs_unregister_debug_facility();
}

module_init(lcs_init_module);
module_exit(lcs_cleanup_module);

F
Frank Pavlic 已提交
2482
MODULE_AUTHOR("Frank Pavlic <fpavlic@de.ibm.com>");
L
Linus Torvalds 已提交
2483 2484
MODULE_LICENSE("GPL");