qeth_l3_main.c 63.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
F
Frank Blaschka 已提交
2
/*
3
 *    Copyright IBM Corp. 2007, 2009
F
Frank Blaschka 已提交
4 5 6 7 8 9
 *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
 *		 Frank Pavlic <fpavlic@de.ibm.com>,
 *		 Thomas Spatzier <tspat@de.ibm.com>,
 *		 Frank Blaschka <frank.blaschka@de.ibm.com>
 */

10 11 12
#define KMSG_COMPONENT "qeth"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

F
Frank Blaschka 已提交
13 14
#include <linux/module.h>
#include <linux/moduleparam.h>
J
Jiri Pirko 已提交
15
#include <linux/bitops.h>
F
Frank Blaschka 已提交
16 17 18 19 20
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/etherdevice.h>
#include <linux/ip.h>
21
#include <linux/in.h>
F
Frank Blaschka 已提交
22
#include <linux/ipv6.h>
F
Frank Blaschka 已提交
23 24
#include <linux/inetdevice.h>
#include <linux/igmp.h>
25
#include <linux/slab.h>
26
#include <linux/if_ether.h>
J
Jiri Pirko 已提交
27
#include <linux/if_vlan.h>
28
#include <linux/skbuff.h>
F
Frank Blaschka 已提交
29 30 31

#include <net/ip.h>
#include <net/arp.h>
32
#include <net/route.h>
33
#include <net/ipv6.h>
34
#include <net/ip6_route.h>
35
#include <net/iucv/af_iucv.h>
36
#include <linux/hashtable.h>
F
Frank Blaschka 已提交
37 38 39

#include "qeth_l3.h"

40

F
Frank Blaschka 已提交
41 42 43 44 45 46
static int qeth_l3_set_offline(struct ccwgroup_device *);
static int qeth_l3_register_addr_entry(struct qeth_card *,
		struct qeth_ipaddr *);
static int qeth_l3_deregister_addr_entry(struct qeth_card *,
		struct qeth_ipaddr *);

47 48
int qeth_l3_ipaddr_to_string(enum qeth_prot_versions proto, const u8 *addr,
			     char *buf)
F
Frank Blaschka 已提交
49 50
{
	if (proto == QETH_PROT_IPV4)
51 52 53
		return sprintf(buf, "%pI4", addr);
	else
		return sprintf(buf, "%pI6", addr);
F
Frank Blaschka 已提交
54 55
}

56 57 58
static struct qeth_ipaddr *qeth_l3_find_addr_by_ip(struct qeth_card *card,
						   struct qeth_ipaddr *query)
{
59
	u32 key = qeth_l3_ipaddr_hash(query);
60 61 62 63 64 65 66 67 68 69 70 71 72 73
	struct qeth_ipaddr *addr;

	if (query->is_multicast) {
		hash_for_each_possible(card->ip_mc_htable, addr, hnode, key)
			if (qeth_l3_addr_match_ip(addr, query))
				return addr;
	} else {
		hash_for_each_possible(card->ip_htable,  addr, hnode, key)
			if (qeth_l3_addr_match_ip(addr, query))
				return addr;
	}
	return NULL;
}

F
Frank Blaschka 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87
static void qeth_l3_convert_addr_to_bits(u8 *addr, u8 *bits, int len)
{
	int i, j;
	u8 octet;

	for (i = 0; i < len; ++i) {
		octet = addr[i];
		for (j = 7; j >= 0; --j) {
			bits[i*8 + j] = octet & 1;
			octet >>= 1;
		}
	}
}

88 89
static bool qeth_l3_is_addr_covered_by_ipato(struct qeth_card *card,
					     struct qeth_ipaddr *addr)
F
Frank Blaschka 已提交
90 91 92 93 94 95 96
{
	struct qeth_ipato_entry *ipatoe;
	u8 addr_bits[128] = {0, };
	u8 ipatoe_bits[128] = {0, };
	int rc = 0;

	if (!card->ipato.enabled)
97
		return false;
98
	if (addr->type != QETH_IP_TYPE_NORMAL)
99
		return false;
F
Frank Blaschka 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

	qeth_l3_convert_addr_to_bits((u8 *) &addr->u, addr_bits,
				  (addr->proto == QETH_PROT_IPV4)? 4:16);
	list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
		if (addr->proto != ipatoe->proto)
			continue;
		qeth_l3_convert_addr_to_bits(ipatoe->addr, ipatoe_bits,
					  (ipatoe->proto == QETH_PROT_IPV4) ?
					  4 : 16);
		if (addr->proto == QETH_PROT_IPV4)
			rc = !memcmp(addr_bits, ipatoe_bits,
				     min(32, ipatoe->mask_bits));
		else
			rc = !memcmp(addr_bits, ipatoe_bits,
				     min(128, ipatoe->mask_bits));
		if (rc)
			break;
	}
	/* invert? */
	if ((addr->proto == QETH_PROT_IPV4) && card->ipato.invert4)
		rc = !rc;
	else if ((addr->proto == QETH_PROT_IPV6) && card->ipato.invert6)
		rc = !rc;

	return rc;
}

127 128
static int qeth_l3_delete_ip(struct qeth_card *card,
			     struct qeth_ipaddr *tmp_addr)
F
Frank Blaschka 已提交
129 130
{
	int rc = 0;
131
	struct qeth_ipaddr *addr;
F
Frank Blaschka 已提交
132

133 134 135 136 137 138
	if (tmp_addr->type == QETH_IP_TYPE_RXIP)
		QETH_CARD_TEXT(card, 2, "delrxip");
	else if (tmp_addr->type == QETH_IP_TYPE_VIPA)
		QETH_CARD_TEXT(card, 2, "delvipa");
	else
		QETH_CARD_TEXT(card, 2, "delip");
F
Frank Blaschka 已提交
139

140 141
	if (tmp_addr->proto == QETH_PROT_IPV4)
		QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
F
Frank Blaschka 已提交
142
	else {
143 144
		QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
		QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
F
Frank Blaschka 已提交
145
	}
146

147 148
	addr = qeth_l3_find_addr_by_ip(card, tmp_addr);
	if (!addr || !qeth_l3_addr_match_all(addr, tmp_addr))
149 150 151
		return -ENOENT;

	addr->ref_counter--;
152
	if (addr->type == QETH_IP_TYPE_NORMAL && addr->ref_counter > 0)
153 154 155 156
		return rc;
	if (addr->in_progress)
		return -EINPROGRESS;

157 158
	if (qeth_card_hw_is_reachable(card))
		rc = qeth_l3_deregister_addr_entry(card, addr);
159 160 161 162

	hash_del(&addr->hnode);
	kfree(addr);

F
Frank Blaschka 已提交
163 164 165
	return rc;
}

166
static int qeth_l3_add_ip(struct qeth_card *card, struct qeth_ipaddr *tmp_addr)
F
Frank Blaschka 已提交
167 168
{
	int rc = 0;
169
	struct qeth_ipaddr *addr;
170
	char buf[40];
F
Frank Blaschka 已提交
171

172 173 174 175 176 177
	if (tmp_addr->type == QETH_IP_TYPE_RXIP)
		QETH_CARD_TEXT(card, 2, "addrxip");
	else if (tmp_addr->type == QETH_IP_TYPE_VIPA)
		QETH_CARD_TEXT(card, 2, "addvipa");
	else
		QETH_CARD_TEXT(card, 2, "addip");
178 179 180

	if (tmp_addr->proto == QETH_PROT_IPV4)
		QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
F
Frank Blaschka 已提交
181
	else {
182 183 184 185
		QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
		QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
	}

186 187 188 189 190 191 192 193 194 195 196 197 198 199
	addr = qeth_l3_find_addr_by_ip(card, tmp_addr);
	if (addr) {
		if (tmp_addr->type != QETH_IP_TYPE_NORMAL)
			return -EADDRINUSE;
		if (qeth_l3_addr_match_all(addr, tmp_addr)) {
			addr->ref_counter++;
			return 0;
		}
		qeth_l3_ipaddr_to_string(tmp_addr->proto, (u8 *)&tmp_addr->u,
					 buf);
		dev_warn(&card->gdev->dev,
			 "Registering IP address %s failed\n", buf);
		return -EADDRINUSE;
	} else {
200
		addr = kmemdup(tmp_addr, sizeof(*tmp_addr), GFP_KERNEL);
201 202 203
		if (!addr)
			return -ENOMEM;

204
		if (qeth_l3_is_addr_covered_by_ipato(card, addr)) {
205
			QETH_CARD_TEXT(card, 2, "tkovaddr");
206
			addr->ipato = 1;
207 208 209 210
		}
		hash_add(card->ip_htable, &addr->hnode,
				qeth_l3_ipaddr_hash(addr));

211 212 213 214 215
		if (!qeth_card_hw_is_reachable(card)) {
			addr->disp_flag = QETH_DISP_ADDR_ADD;
			return 0;
		}

216 217 218 219 220 221 222 223 224
		/* qeth_l3_register_addr_entry can go to sleep
		 * if we add a IPV4 addr. It is caused by the reason
		 * that SETIP ipa cmd starts ARP staff for IPV4 addr.
		 * Thus we should unlock spinlock, and make a protection
		 * using in_progress variable to indicate that there is
		 * an hardware operation with this IPV4 address
		 */
		if (addr->proto == QETH_PROT_IPV4) {
			addr->in_progress = 1;
225
			mutex_unlock(&card->ip_lock);
226
			rc = qeth_l3_register_addr_entry(card, addr);
227
			mutex_lock(&card->ip_lock);
228 229 230 231
			addr->in_progress = 0;
		} else
			rc = qeth_l3_register_addr_entry(card, addr);

232
		if (!rc || rc == -EADDRINUSE || rc == -ENETDOWN) {
233 234
			addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
			if (addr->ref_counter < 1) {
235 236
				qeth_l3_deregister_addr_entry(card, addr);
				hash_del(&addr->hnode);
237 238 239 240 241 242
				kfree(addr);
			}
		} else {
			hash_del(&addr->hnode);
			kfree(addr);
		}
F
Frank Blaschka 已提交
243 244 245 246
	}
	return rc;
}

247 248 249 250 251
static int qeth_l3_modify_ip(struct qeth_card *card, struct qeth_ipaddr *addr,
			     bool add)
{
	int rc;

252
	mutex_lock(&card->ip_lock);
253
	rc = add ? qeth_l3_add_ip(card, addr) : qeth_l3_delete_ip(card, addr);
254
	mutex_unlock(&card->ip_lock);
255 256 257 258

	return rc;
}

259 260 261 262 263 264 265 266 267 268 269 270
static void qeth_l3_drain_rx_mode_cache(struct qeth_card *card)
{
	struct qeth_ipaddr *addr;
	struct hlist_node *tmp;
	int i;

	hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
		hash_del(&addr->hnode);
		kfree(addr);
	}
}

271
static void qeth_l3_clear_ip_htable(struct qeth_card *card, int recover)
F
Frank Blaschka 已提交
272 273
{
	struct qeth_ipaddr *addr;
274 275
	struct hlist_node *tmp;
	int i;
F
Frank Blaschka 已提交
276

277
	QETH_CARD_TEXT(card, 4, "clearip");
F
Frank Blaschka 已提交
278

279
	mutex_lock(&card->ip_lock);
280 281 282 283 284

	hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
		if (!recover) {
			hash_del(&addr->hnode);
			kfree(addr);
F
Frank Blaschka 已提交
285 286
			continue;
		}
287
		addr->disp_flag = QETH_DISP_ADDR_ADD;
F
Frank Blaschka 已提交
288 289
	}

290
	mutex_unlock(&card->ip_lock);
291
}
292

293
static void qeth_l3_recover_ip(struct qeth_card *card)
F
Frank Blaschka 已提交
294
{
295 296 297 298
	struct qeth_ipaddr *addr;
	struct hlist_node *tmp;
	int i;
	int rc;
F
Frank Blaschka 已提交
299

300
	QETH_CARD_TEXT(card, 4, "recovrip");
301

302
	mutex_lock(&card->ip_lock);
303 304

	hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
305
		if (addr->disp_flag == QETH_DISP_ADDR_ADD) {
306 307
			if (addr->proto == QETH_PROT_IPV4) {
				addr->in_progress = 1;
308
				mutex_unlock(&card->ip_lock);
309
				rc = qeth_l3_register_addr_entry(card, addr);
310
				mutex_lock(&card->ip_lock);
311 312 313 314 315 316
				addr->in_progress = 0;
			} else
				rc = qeth_l3_register_addr_entry(card, addr);

			if (!rc) {
				addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
317
				if (addr->ref_counter < 1)
318 319 320 321 322 323 324
					qeth_l3_delete_ip(card, addr);
			} else {
				hash_del(&addr->hnode);
				kfree(addr);
			}
		}
	}
F
Frank Blaschka 已提交
325

326
	mutex_unlock(&card->ip_lock);
F
Frank Blaschka 已提交
327 328
}

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
static int qeth_l3_setdelip_cb(struct qeth_card *card, struct qeth_reply *reply,
			       unsigned long data)
{
	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;

	switch (cmd->hdr.return_code) {
	case IPA_RC_SUCCESS:
		return 0;
	case IPA_RC_DUPLICATE_IP_ADDRESS:
		return -EADDRINUSE;
	case IPA_RC_MC_ADDR_NOT_FOUND:
		return -ENOENT;
	case IPA_RC_LAN_OFFLINE:
		return -ENETDOWN;
	default:
		return -EIO;
	}
}

F
Frank Blaschka 已提交
348 349 350 351 352 353
static int qeth_l3_send_setdelmc(struct qeth_card *card,
			struct qeth_ipaddr *addr, int ipacmd)
{
	struct qeth_cmd_buffer *iob;
	struct qeth_ipa_cmd *cmd;

C
Carsten Otte 已提交
354
	QETH_CARD_TEXT(card, 4, "setdelmc");
F
Frank Blaschka 已提交
355

356 357
	iob = qeth_ipa_alloc_cmd(card, ipacmd, addr->proto,
				 IPA_DATA_SIZEOF(setdelipm));
358 359
	if (!iob)
		return -ENOMEM;
360
	cmd = __ipa_cmd(iob);
361 362 363 364 365 366 367
	if (addr->proto == QETH_PROT_IPV6) {
		cmd->data.setdelipm.ip = addr->u.a6.addr;
		ipv6_eth_mc_map(&addr->u.a6.addr, cmd->data.setdelipm.mac);
	} else {
		cmd->data.setdelipm.ip.s6_addr32[3] = addr->u.a4.addr;
		ip_eth_mc_map(addr->u.a4.addr, cmd->data.setdelipm.mac);
	}
F
Frank Blaschka 已提交
368

369
	return qeth_send_ipa_cmd(card, iob, qeth_l3_setdelip_cb, NULL);
F
Frank Blaschka 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
}

static void qeth_l3_fill_netmask(u8 *netmask, unsigned int len)
{
	int i, j;
	for (i = 0; i < 16; i++) {
		j = (len) - (i * 8);
		if (j >= 8)
			netmask[i] = 0xff;
		else if (j > 0)
			netmask[i] = (u8)(0xFF00 >> j);
		else
			netmask[i] = 0;
	}
}

386 387 388 389 390 391 392 393 394 395 396 397 398
static u32 qeth_l3_get_setdelip_flags(struct qeth_ipaddr *addr, bool set)
{
	switch (addr->type) {
	case QETH_IP_TYPE_RXIP:
		return (set) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0;
	case QETH_IP_TYPE_VIPA:
		return (set) ? QETH_IPA_SETIP_VIPA_FLAG :
			       QETH_IPA_DELIP_VIPA_FLAG;
	default:
		return (set && addr->ipato) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0;
	}
}

F
Frank Blaschka 已提交
399
static int qeth_l3_send_setdelip(struct qeth_card *card,
400 401
				 struct qeth_ipaddr *addr,
				 enum qeth_ipa_cmds ipacmd)
F
Frank Blaschka 已提交
402 403 404 405
{
	struct qeth_cmd_buffer *iob;
	struct qeth_ipa_cmd *cmd;
	__u8 netmask[16];
406
	u32 flags;
F
Frank Blaschka 已提交
407

C
Carsten Otte 已提交
408
	QETH_CARD_TEXT(card, 4, "setdelip");
F
Frank Blaschka 已提交
409

410 411
	iob = qeth_ipa_alloc_cmd(card, ipacmd, addr->proto,
				 IPA_DATA_SIZEOF(setdelip6));
412 413
	if (!iob)
		return -ENOMEM;
414
	cmd = __ipa_cmd(iob);
415 416 417 418

	flags = qeth_l3_get_setdelip_flags(addr, ipacmd == IPA_CMD_SETIP);
	QETH_CARD_TEXT_(card, 4, "flags%02X", flags);

F
Frank Blaschka 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431
	if (addr->proto == QETH_PROT_IPV6) {
		memcpy(cmd->data.setdelip6.ip_addr, &addr->u.a6.addr,
		       sizeof(struct in6_addr));
		qeth_l3_fill_netmask(netmask, addr->u.a6.pfxlen);
		memcpy(cmd->data.setdelip6.mask, netmask,
		       sizeof(struct in6_addr));
		cmd->data.setdelip6.flags = flags;
	} else {
		memcpy(cmd->data.setdelip4.ip_addr, &addr->u.a4.addr, 4);
		memcpy(cmd->data.setdelip4.mask, &addr->u.a4.mask, 4);
		cmd->data.setdelip4.flags = flags;
	}

432
	return qeth_send_ipa_cmd(card, iob, qeth_l3_setdelip_cb, NULL);
F
Frank Blaschka 已提交
433 434 435 436 437 438 439 440 441
}

static int qeth_l3_send_setrouting(struct qeth_card *card,
	enum qeth_routing_types type, enum qeth_prot_versions prot)
{
	int rc;
	struct qeth_ipa_cmd *cmd;
	struct qeth_cmd_buffer *iob;

C
Carsten Otte 已提交
442
	QETH_CARD_TEXT(card, 4, "setroutg");
443 444
	iob = qeth_ipa_alloc_cmd(card, IPA_CMD_SETRTG, prot,
				 IPA_DATA_SIZEOF(setrtg));
445 446
	if (!iob)
		return -ENOMEM;
447
	cmd = __ipa_cmd(iob);
F
Frank Blaschka 已提交
448 449 450 451 452 453
	cmd->data.setrtg.type = (type);
	rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);

	return rc;
}

454
static int qeth_l3_correct_routing_type(struct qeth_card *card,
F
Frank Blaschka 已提交
455 456
		enum qeth_routing_types *type, enum qeth_prot_versions prot)
{
457
	if (IS_IQD(card)) {
F
Frank Blaschka 已提交
458 459 460 461 462
		switch (*type) {
		case NO_ROUTER:
		case PRIMARY_CONNECTOR:
		case SECONDARY_CONNECTOR:
		case MULTICAST_ROUTER:
463
			return 0;
F
Frank Blaschka 已提交
464 465 466 467 468 469 470 471
		default:
			goto out_inval;
		}
	} else {
		switch (*type) {
		case NO_ROUTER:
		case PRIMARY_ROUTER:
		case SECONDARY_ROUTER:
472
			return 0;
F
Frank Blaschka 已提交
473 474 475
		case MULTICAST_ROUTER:
			if (qeth_is_ipafunc_supported(card, prot,
						      IPA_OSA_MC_ROUTER))
476
				return 0;
F
Frank Blaschka 已提交
477 478 479 480 481 482
		default:
			goto out_inval;
		}
	}
out_inval:
	*type = NO_ROUTER;
483
	return -EINVAL;
F
Frank Blaschka 已提交
484 485 486 487 488 489
}

int qeth_l3_setrouting_v4(struct qeth_card *card)
{
	int rc;

C
Carsten Otte 已提交
490
	QETH_CARD_TEXT(card, 3, "setrtg4");
F
Frank Blaschka 已提交
491

492
	rc = qeth_l3_correct_routing_type(card, &card->options.route4.type,
F
Frank Blaschka 已提交
493
				  QETH_PROT_IPV4);
494 495
	if (rc)
		return rc;
F
Frank Blaschka 已提交
496 497 498 499 500

	rc = qeth_l3_send_setrouting(card, card->options.route4.type,
				  QETH_PROT_IPV4);
	if (rc) {
		card->options.route4.type = NO_ROUTER;
501 502
		QETH_DBF_MESSAGE(2, "Error (%#06x) while setting routing type on device %x. Type set to 'no router'.\n",
				 rc, CARD_DEVID(card));
F
Frank Blaschka 已提交
503 504 505 506 507 508 509 510
	}
	return rc;
}

int qeth_l3_setrouting_v6(struct qeth_card *card)
{
	int rc = 0;

C
Carsten Otte 已提交
511
	QETH_CARD_TEXT(card, 3, "setrtg6");
F
Frank Blaschka 已提交
512 513 514

	if (!qeth_is_supported(card, IPA_IPV6))
		return 0;
515
	rc = qeth_l3_correct_routing_type(card, &card->options.route6.type,
F
Frank Blaschka 已提交
516
				  QETH_PROT_IPV6);
517 518
	if (rc)
		return rc;
F
Frank Blaschka 已提交
519 520 521 522 523

	rc = qeth_l3_send_setrouting(card, card->options.route6.type,
				  QETH_PROT_IPV6);
	if (rc) {
		card->options.route6.type = NO_ROUTER;
524 525
		QETH_DBF_MESSAGE(2, "Error (%#06x) while setting routing type on device %x. Type set to 'no router'.\n",
				 rc, CARD_DEVID(card));
F
Frank Blaschka 已提交
526 527 528 529 530 531 532
	}
	return rc;
}

/*
 * IP address takeover related functions
 */
533 534 535 536 537 538 539 540 541 542 543 544 545 546

/**
 * qeth_l3_update_ipato() - Update 'takeover' property, for all NORMAL IPs.
 *
 * Caller must hold ip_lock.
 */
void qeth_l3_update_ipato(struct qeth_card *card)
{
	struct qeth_ipaddr *addr;
	unsigned int i;

	hash_for_each(card->ip_htable, i, addr, hnode) {
		if (addr->type != QETH_IP_TYPE_NORMAL)
			continue;
547
		addr->ipato = qeth_l3_is_addr_covered_by_ipato(card, addr);
548 549 550
	}
}

F
Frank Blaschka 已提交
551 552 553 554
static void qeth_l3_clear_ipato_list(struct qeth_card *card)
{
	struct qeth_ipato_entry *ipatoe, *tmp;

555
	mutex_lock(&card->ip_lock);
556

F
Frank Blaschka 已提交
557 558 559 560
	list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
		list_del(&ipatoe->entry);
		kfree(ipatoe);
	}
561

562
	qeth_l3_update_ipato(card);
563
	mutex_unlock(&card->ip_lock);
F
Frank Blaschka 已提交
564 565 566 567 568 569 570 571
}

int qeth_l3_add_ipato_entry(struct qeth_card *card,
				struct qeth_ipato_entry *new)
{
	struct qeth_ipato_entry *ipatoe;
	int rc = 0;

C
Carsten Otte 已提交
572
	QETH_CARD_TEXT(card, 2, "addipato");
573

574
	mutex_lock(&card->conf_mutex);
575
	mutex_lock(&card->ip_lock);
576

F
Frank Blaschka 已提交
577 578 579 580 581 582 583 584 585 586
	list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
		if (ipatoe->proto != new->proto)
			continue;
		if (!memcmp(ipatoe->addr, new->addr,
			    (ipatoe->proto == QETH_PROT_IPV4)? 4:16) &&
		    (ipatoe->mask_bits == new->mask_bits)) {
			rc = -EEXIST;
			break;
		}
	}
587

588
	if (!rc) {
F
Frank Blaschka 已提交
589
		list_add_tail(&new->entry, &card->ipato.entries);
590 591
		qeth_l3_update_ipato(card);
	}
F
Frank Blaschka 已提交
592

593
	mutex_unlock(&card->ip_lock);
594
	mutex_unlock(&card->conf_mutex);
595

F
Frank Blaschka 已提交
596 597 598
	return rc;
}

599 600 601
int qeth_l3_del_ipato_entry(struct qeth_card *card,
			    enum qeth_prot_versions proto, u8 *addr,
			    int mask_bits)
F
Frank Blaschka 已提交
602 603
{
	struct qeth_ipato_entry *ipatoe, *tmp;
604
	int rc = -ENOENT;
F
Frank Blaschka 已提交
605

C
Carsten Otte 已提交
606
	QETH_CARD_TEXT(card, 2, "delipato");
607

608
	mutex_lock(&card->conf_mutex);
609
	mutex_lock(&card->ip_lock);
610

F
Frank Blaschka 已提交
611 612 613 614 615 616 617
	list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
		if (ipatoe->proto != proto)
			continue;
		if (!memcmp(ipatoe->addr, addr,
			    (proto == QETH_PROT_IPV4)? 4:16) &&
		    (ipatoe->mask_bits == mask_bits)) {
			list_del(&ipatoe->entry);
618
			qeth_l3_update_ipato(card);
F
Frank Blaschka 已提交
619
			kfree(ipatoe);
620
			rc = 0;
F
Frank Blaschka 已提交
621 622
		}
	}
623

624
	mutex_unlock(&card->ip_lock);
625 626
	mutex_unlock(&card->conf_mutex);

627
	return rc;
F
Frank Blaschka 已提交
628 629
}

630 631 632
int qeth_l3_modify_rxip_vipa(struct qeth_card *card, bool add, const u8 *ip,
			     enum qeth_ip_types type,
			     enum qeth_prot_versions proto)
F
Frank Blaschka 已提交
633
{
634
	struct qeth_ipaddr addr;
635
	int rc;
F
Frank Blaschka 已提交
636

637 638 639 640 641
	qeth_l3_init_ipaddr(&addr, type, proto);
	if (proto == QETH_PROT_IPV4)
		memcpy(&addr.u.a4.addr, ip, 4);
	else
		memcpy(&addr.u.a6.addr, ip, 16);
642

643 644 645 646 647
	mutex_lock(&card->conf_mutex);
	rc = qeth_l3_modify_ip(card, &addr, add);
	mutex_unlock(&card->conf_mutex);

	return rc;
F
Frank Blaschka 已提交
648 649
}

650
int qeth_l3_modify_hsuid(struct qeth_card *card, bool add)
F
Frank Blaschka 已提交
651
{
652
	struct qeth_ipaddr addr;
653
	unsigned int i;
F
Frank Blaschka 已提交
654

655 656 657 658 659
	qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6);
	addr.u.a6.addr.s6_addr[0] = 0xfe;
	addr.u.a6.addr.s6_addr[1] = 0x80;
	for (i = 0; i < 8; i++)
		addr.u.a6.addr.s6_addr[8+i] = card->options.hsuid[i];
660

661
	return qeth_l3_modify_ip(card, &addr, add);
F
Frank Blaschka 已提交
662 663 664 665 666 667 668 669 670
}

static int qeth_l3_register_addr_entry(struct qeth_card *card,
				struct qeth_ipaddr *addr)
{
	char buf[50];
	int rc = 0;
	int cnt = 3;

671 672
	if (card->options.sniffer)
		return 0;
673

F
Frank Blaschka 已提交
674
	if (addr->proto == QETH_PROT_IPV4) {
C
Carsten Otte 已提交
675 676
		QETH_CARD_TEXT(card, 2, "setaddr4");
		QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
F
Frank Blaschka 已提交
677
	} else if (addr->proto == QETH_PROT_IPV6) {
C
Carsten Otte 已提交
678 679 680
		QETH_CARD_TEXT(card, 2, "setaddr6");
		QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
		QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
F
Frank Blaschka 已提交
681
	} else {
C
Carsten Otte 已提交
682 683
		QETH_CARD_TEXT(card, 2, "setaddr?");
		QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
F
Frank Blaschka 已提交
684 685 686 687 688
	}
	do {
		if (addr->is_multicast)
			rc =  qeth_l3_send_setdelmc(card, addr, IPA_CMD_SETIPM);
		else
689
			rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_SETIP);
F
Frank Blaschka 已提交
690
		if (rc)
C
Carsten Otte 已提交
691
			QETH_CARD_TEXT(card, 2, "failed");
F
Frank Blaschka 已提交
692 693
	} while ((--cnt > 0) && rc);
	if (rc) {
C
Carsten Otte 已提交
694
		QETH_CARD_TEXT(card, 2, "FAILED");
F
Frank Blaschka 已提交
695
		qeth_l3_ipaddr_to_string(addr->proto, (u8 *)&addr->u, buf);
696 697
		dev_warn(&card->gdev->dev,
			"Registering IP address %s failed\n", buf);
F
Frank Blaschka 已提交
698 699 700 701 702 703 704 705 706
	}
	return rc;
}

static int qeth_l3_deregister_addr_entry(struct qeth_card *card,
						struct qeth_ipaddr *addr)
{
	int rc = 0;

707 708 709
	if (card->options.sniffer)
		return 0;

F
Frank Blaschka 已提交
710
	if (addr->proto == QETH_PROT_IPV4) {
C
Carsten Otte 已提交
711 712
		QETH_CARD_TEXT(card, 2, "deladdr4");
		QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
F
Frank Blaschka 已提交
713
	} else if (addr->proto == QETH_PROT_IPV6) {
C
Carsten Otte 已提交
714 715 716
		QETH_CARD_TEXT(card, 2, "deladdr6");
		QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
		QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
F
Frank Blaschka 已提交
717
	} else {
C
Carsten Otte 已提交
718 719
		QETH_CARD_TEXT(card, 2, "deladdr?");
		QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
F
Frank Blaschka 已提交
720 721 722 723
	}
	if (addr->is_multicast)
		rc = qeth_l3_send_setdelmc(card, addr, IPA_CMD_DELIPM);
	else
724
		rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_DELIP);
725
	if (rc)
C
Carsten Otte 已提交
726
		QETH_CARD_TEXT(card, 2, "failed");
F
Frank Blaschka 已提交
727 728 729 730 731 732

	return rc;
}

static int qeth_l3_setadapter_parms(struct qeth_card *card)
{
733
	int rc = 0;
F
Frank Blaschka 已提交
734

735
	QETH_CARD_TEXT(card, 2, "setadprm");
F
Frank Blaschka 已提交
736 737 738 739

	if (qeth_adp_supported(card, IPA_SETADP_ALTER_MAC_ADDRESS)) {
		rc = qeth_setadpparms_change_macaddr(card);
		if (rc)
740
			dev_warn(&card->gdev->dev, "Reading the adapter MAC"
741
				" address failed\n");
F
Frank Blaschka 已提交
742 743 744 745 746 747 748 749 750
	}

	return rc;
}

static int qeth_l3_start_ipa_arp_processing(struct qeth_card *card)
{
	int rc;

C
Carsten Otte 已提交
751
	QETH_CARD_TEXT(card, 3, "ipaarp");
F
Frank Blaschka 已提交
752 753

	if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
754 755 756
		dev_info(&card->gdev->dev,
			"ARP processing not supported on %s!\n",
			QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
757 758
		return 0;
	}
759
	rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
760
					  IPA_CMD_ASS_START, NULL);
F
Frank Blaschka 已提交
761
	if (rc) {
762 763 764
		dev_warn(&card->gdev->dev,
			"Starting ARP processing support for %s failed\n",
			QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
765 766 767 768 769 770 771 772
	}
	return rc;
}

static int qeth_l3_start_ipa_source_mac(struct qeth_card *card)
{
	int rc;

C
Carsten Otte 已提交
773
	QETH_CARD_TEXT(card, 3, "stsrcmac");
F
Frank Blaschka 已提交
774 775

	if (!qeth_is_supported(card, IPA_SOURCE_MAC)) {
776
		dev_info(&card->gdev->dev,
777
			"Inbound source MAC-address not supported on %s\n",
778
			QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
779 780 781
		return -EOPNOTSUPP;
	}

782
	rc = qeth_send_simple_setassparms(card, IPA_SOURCE_MAC,
783
					  IPA_CMD_ASS_START, NULL);
F
Frank Blaschka 已提交
784
	if (rc)
785
		dev_warn(&card->gdev->dev,
786
			"Starting source MAC-address support for %s failed\n",
787
			QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
788 789 790 791 792 793 794
	return rc;
}

static int qeth_l3_start_ipa_vlan(struct qeth_card *card)
{
	int rc = 0;

C
Carsten Otte 已提交
795
	QETH_CARD_TEXT(card, 3, "strtvlan");
F
Frank Blaschka 已提交
796 797

	if (!qeth_is_supported(card, IPA_FULL_VLAN)) {
798 799
		dev_info(&card->gdev->dev,
			"VLAN not supported on %s\n", QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
800 801 802
		return -EOPNOTSUPP;
	}

803
	rc = qeth_send_simple_setassparms(card, IPA_VLAN_PRIO,
804
					  IPA_CMD_ASS_START, NULL);
F
Frank Blaschka 已提交
805
	if (rc) {
806 807 808
		dev_warn(&card->gdev->dev,
			"Starting VLAN support for %s failed\n",
			QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
809
	} else {
810
		dev_info(&card->gdev->dev, "VLAN enabled\n");
F
Frank Blaschka 已提交
811 812 813 814 815 816 817 818
	}
	return rc;
}

static int qeth_l3_start_ipa_multicast(struct qeth_card *card)
{
	int rc;

C
Carsten Otte 已提交
819
	QETH_CARD_TEXT(card, 3, "stmcast");
F
Frank Blaschka 已提交
820 821

	if (!qeth_is_supported(card, IPA_MULTICASTING)) {
822 823 824
		dev_info(&card->gdev->dev,
			"Multicast not supported on %s\n",
			QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
825 826 827
		return -EOPNOTSUPP;
	}

828
	rc = qeth_send_simple_setassparms(card, IPA_MULTICASTING,
829
					  IPA_CMD_ASS_START, NULL);
F
Frank Blaschka 已提交
830
	if (rc) {
831 832 833
		dev_warn(&card->gdev->dev,
			"Starting multicast support for %s failed\n",
			QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
834
	} else {
835
		dev_info(&card->gdev->dev, "Multicast enabled\n");
F
Frank Blaschka 已提交
836 837 838 839 840 841 842
		card->dev->flags |= IFF_MULTICAST;
	}
	return rc;
}

static int qeth_l3_softsetup_ipv6(struct qeth_card *card)
{
843
	u32 ipv6_data = 3;
F
Frank Blaschka 已提交
844 845
	int rc;

C
Carsten Otte 已提交
846
	QETH_CARD_TEXT(card, 3, "softipv6");
F
Frank Blaschka 已提交
847

848
	if (IS_IQD(card))
849 850
		goto out;

851 852
	rc = qeth_send_simple_setassparms(card, IPA_IPV6, IPA_CMD_ASS_START,
					  &ipv6_data);
F
Frank Blaschka 已提交
853
	if (rc) {
854 855 856
		dev_err(&card->gdev->dev,
			"Activating IPv6 support for %s failed\n",
			QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
857 858
		return rc;
	}
859 860
	rc = qeth_send_simple_setassparms_v6(card, IPA_IPV6, IPA_CMD_ASS_START,
					     NULL);
F
Frank Blaschka 已提交
861
	if (rc) {
862 863 864
		dev_err(&card->gdev->dev,
			"Activating IPv6 support for %s failed\n",
			 QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
865 866
		return rc;
	}
867
	rc = qeth_send_simple_setassparms_v6(card, IPA_PASSTHRU,
868
					     IPA_CMD_ASS_START, NULL);
F
Frank Blaschka 已提交
869
	if (rc) {
870 871 872
		dev_warn(&card->gdev->dev,
			"Enabling the passthrough mode for %s failed\n",
			QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
873 874 875
		return rc;
	}
out:
876
	dev_info(&card->gdev->dev, "IPV6 enabled\n");
F
Frank Blaschka 已提交
877 878 879 880 881
	return 0;
}

static int qeth_l3_start_ipa_ipv6(struct qeth_card *card)
{
C
Carsten Otte 已提交
882
	QETH_CARD_TEXT(card, 3, "strtipv6");
F
Frank Blaschka 已提交
883 884

	if (!qeth_is_supported(card, IPA_IPV6)) {
885 886
		dev_info(&card->gdev->dev,
			"IPv6 not supported on %s\n", QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
887 888
		return 0;
	}
J
Julian Wiedmann 已提交
889
	return qeth_l3_softsetup_ipv6(card);
F
Frank Blaschka 已提交
890 891 892 893
}

static int qeth_l3_start_ipa_broadcast(struct qeth_card *card)
{
894
	u32 filter_data = 1;
F
Frank Blaschka 已提交
895 896
	int rc;

C
Carsten Otte 已提交
897
	QETH_CARD_TEXT(card, 3, "stbrdcst");
F
Frank Blaschka 已提交
898 899
	card->info.broadcast_capable = 0;
	if (!qeth_is_supported(card, IPA_FILTERING)) {
900 901 902
		dev_info(&card->gdev->dev,
			"Broadcast not supported on %s\n",
			QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
903 904 905
		rc = -EOPNOTSUPP;
		goto out;
	}
906
	rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
907
					  IPA_CMD_ASS_START, NULL);
F
Frank Blaschka 已提交
908
	if (rc) {
909 910
		dev_warn(&card->gdev->dev, "Enabling broadcast filtering for "
			"%s failed\n", QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
911 912 913
		goto out;
	}

914
	rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
915
					  IPA_CMD_ASS_CONFIGURE, &filter_data);
F
Frank Blaschka 已提交
916
	if (rc) {
917 918 919
		dev_warn(&card->gdev->dev,
			"Setting up broadcast filtering for %s failed\n",
			QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
920 921 922
		goto out;
	}
	card->info.broadcast_capable = QETH_BROADCAST_WITH_ECHO;
923
	dev_info(&card->gdev->dev, "Broadcast enabled\n");
924
	rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
925
					  IPA_CMD_ASS_ENABLE, &filter_data);
F
Frank Blaschka 已提交
926
	if (rc) {
927 928
		dev_warn(&card->gdev->dev, "Setting up broadcast echo "
			"filtering for %s failed\n", QETH_CARD_IFNAME(card));
F
Frank Blaschka 已提交
929 930 931 932 933 934 935 936 937 938 939 940 941
		goto out;
	}
	card->info.broadcast_capable = QETH_BROADCAST_WITHOUT_ECHO;
out:
	if (card->info.broadcast_capable)
		card->dev->flags |= IFF_BROADCAST;
	else
		card->dev->flags &= ~IFF_BROADCAST;
	return rc;
}

static int qeth_l3_start_ipassists(struct qeth_card *card)
{
C
Carsten Otte 已提交
942
	QETH_CARD_TEXT(card, 3, "strtipas");
E
Einar Lueck 已提交
943

F
Frank Blaschka 已提交
944 945 946 947 948 949 950 951 952 953 954 955
	qeth_l3_start_ipa_arp_processing(card);	/* go on*/
	qeth_l3_start_ipa_source_mac(card);	/* go on*/
	qeth_l3_start_ipa_vlan(card);		/* go on*/
	qeth_l3_start_ipa_multicast(card);		/* go on*/
	qeth_l3_start_ipa_ipv6(card);		/* go on*/
	qeth_l3_start_ipa_broadcast(card);		/* go on*/
	return 0;
}

static int qeth_l3_iqd_read_initial_mac_cb(struct qeth_card *card,
		struct qeth_reply *reply, unsigned long data)
{
956
	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
F
Frank Blaschka 已提交
957

958 959 960 961 962
	if (cmd->hdr.return_code)
		return -EIO;

	ether_addr_copy(card->dev->dev_addr,
			cmd->data.create_destroy_addr.unique_id);
F
Frank Blaschka 已提交
963 964 965 966 967 968 969 970 971
	return 0;
}

static int qeth_l3_iqd_read_initial_mac(struct qeth_card *card)
{
	int rc = 0;
	struct qeth_cmd_buffer *iob;
	struct qeth_ipa_cmd *cmd;

972
	QETH_CARD_TEXT(card, 2, "hsrmac");
F
Frank Blaschka 已提交
973

974 975
	iob = qeth_ipa_alloc_cmd(card, IPA_CMD_CREATE_ADDR, QETH_PROT_IPV6,
				 IPA_DATA_SIZEOF(create_destroy_addr));
976 977
	if (!iob)
		return -ENOMEM;
978
	cmd = __ipa_cmd(iob);
F
Frank Blaschka 已提交
979 980 981 982 983 984 985 986 987 988 989
	*((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
			card->info.unique_id;

	rc = qeth_send_ipa_cmd(card, iob, qeth_l3_iqd_read_initial_mac_cb,
				NULL);
	return rc;
}

static int qeth_l3_get_unique_id_cb(struct qeth_card *card,
		struct qeth_reply *reply, unsigned long data)
{
990
	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
F
Frank Blaschka 已提交
991

992
	if (cmd->hdr.return_code == 0) {
F
Frank Blaschka 已提交
993 994
		card->info.unique_id = *((__u16 *)
				&cmd->data.create_destroy_addr.unique_id[6]);
995
		return 0;
F
Frank Blaschka 已提交
996
	}
997 998 999 1000 1001

	card->info.unique_id = UNIQUE_ID_IF_CREATE_ADDR_FAILED |
			       UNIQUE_ID_NOT_BY_CARD;
	dev_warn(&card->gdev->dev, "The network adapter failed to generate a unique ID\n");
	return -EIO;
F
Frank Blaschka 已提交
1002 1003 1004 1005 1006 1007 1008 1009
}

static int qeth_l3_get_unique_id(struct qeth_card *card)
{
	int rc = 0;
	struct qeth_cmd_buffer *iob;
	struct qeth_ipa_cmd *cmd;

1010
	QETH_CARD_TEXT(card, 2, "guniqeid");
F
Frank Blaschka 已提交
1011 1012 1013 1014 1015 1016 1017

	if (!qeth_is_supported(card, IPA_IPV6)) {
		card->info.unique_id =  UNIQUE_ID_IF_CREATE_ADDR_FAILED |
					UNIQUE_ID_NOT_BY_CARD;
		return 0;
	}

1018 1019
	iob = qeth_ipa_alloc_cmd(card, IPA_CMD_CREATE_ADDR, QETH_PROT_IPV6,
				 IPA_DATA_SIZEOF(create_destroy_addr));
1020 1021
	if (!iob)
		return -ENOMEM;
1022
	cmd = __ipa_cmd(iob);
F
Frank Blaschka 已提交
1023 1024 1025 1026 1027 1028 1029
	*((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
			card->info.unique_id;

	rc = qeth_send_ipa_cmd(card, iob, qeth_l3_get_unique_id_cb, NULL);
	return rc;
}

1030 1031 1032 1033 1034 1035 1036
static int
qeth_diags_trace_cb(struct qeth_card *card, struct qeth_reply *reply,
			    unsigned long data)
{
	struct qeth_ipa_cmd	   *cmd;
	__u16 rc;

1037
	QETH_CARD_TEXT(card, 2, "diastrcb");
1038 1039 1040

	cmd = (struct qeth_ipa_cmd *)data;
	rc = cmd->hdr.return_code;
1041
	if (rc)
C
Carsten Otte 已提交
1042
		QETH_CARD_TEXT_(card, 2, "dxter%x", rc);
1043 1044 1045 1046
	switch (cmd->data.diagass.action) {
	case QETH_DIAGS_CMD_TRACE_QUERY:
		break;
	case QETH_DIAGS_CMD_TRACE_DISABLE:
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
		switch (rc) {
		case 0:
		case IPA_RC_INVALID_SUBCMD:
			card->info.promisc_mode = SET_PROMISC_MODE_OFF;
			dev_info(&card->gdev->dev, "The HiperSockets network "
				"traffic analyzer is deactivated\n");
			break;
		default:
			break;
		}
1057 1058
		break;
	case QETH_DIAGS_CMD_TRACE_ENABLE:
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
		switch (rc) {
		case 0:
			card->info.promisc_mode = SET_PROMISC_MODE_ON;
			dev_info(&card->gdev->dev, "The HiperSockets network "
				"traffic analyzer is activated\n");
			break;
		case IPA_RC_HARDWARE_AUTH_ERROR:
			dev_warn(&card->gdev->dev, "The device is not "
				"authorized to run as a HiperSockets network "
				"traffic analyzer\n");
			break;
		case IPA_RC_TRACE_ALREADY_ACTIVE:
			dev_warn(&card->gdev->dev, "A HiperSockets "
				"network traffic analyzer is already "
				"active in the HiperSockets LAN\n");
			break;
		default:
			break;
		}
1078 1079
		break;
	default:
1080 1081
		QETH_DBF_MESSAGE(2, "Unknown sniffer action (%#06x) on device %x\n",
				 cmd->data.diagass.action, CARD_DEVID(card));
1082 1083
	}

1084
	return rc ? -EIO : 0;
1085 1086 1087 1088 1089 1090 1091 1092
}

static int
qeth_diags_trace(struct qeth_card *card, enum qeth_diags_trace_cmds diags_cmd)
{
	struct qeth_cmd_buffer *iob;
	struct qeth_ipa_cmd    *cmd;

1093
	QETH_CARD_TEXT(card, 2, "diagtrac");
1094

1095
	iob = qeth_get_diag_cmd(card, QETH_DIAGS_CMD_TRACE, 0);
1096 1097
	if (!iob)
		return -ENOMEM;
1098
	cmd = __ipa_cmd(iob);
1099 1100 1101 1102 1103
	cmd->data.diagass.type = QETH_DIAGS_TYPE_HIPERSOCKET;
	cmd->data.diagass.action = diags_cmd;
	return qeth_send_ipa_cmd(card, iob, qeth_diags_trace_cb, NULL);
}

1104
static int qeth_l3_add_mcast_rtnl(struct net_device *dev, int vid, void *arg)
F
Frank Blaschka 已提交
1105
{
1106
	struct qeth_card *card = arg;
1107 1108 1109
	struct inet6_dev *in6_dev;
	struct in_device *in4_dev;
	struct qeth_ipaddr *ipm;
1110
	struct qeth_ipaddr tmp;
F
Frank Blaschka 已提交
1111
	struct ip_mc_list *im4;
1112
	struct ifmcaddr6 *im6;
F
Frank Blaschka 已提交
1113

C
Carsten Otte 已提交
1114
	QETH_CARD_TEXT(card, 4, "addmc");
1115

1116 1117 1118
	if (!dev || !(dev->flags & IFF_UP))
		goto out;

1119
	in4_dev = __in_dev_get_rtnl(dev);
1120 1121
	if (!in4_dev)
		goto walk_ipv6;
1122

1123 1124 1125 1126
	qeth_l3_init_ipaddr(&tmp, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4);
	tmp.disp_flag = QETH_DISP_ADDR_ADD;
	tmp.is_multicast = 1;

1127 1128
	for (im4 = rtnl_dereference(in4_dev->mc_list); im4 != NULL;
	     im4 = rtnl_dereference(im4->next_rcu)) {
1129
		tmp.u.a4.addr = im4->multiaddr;
1130

1131
		ipm = qeth_l3_find_addr_by_ip(card, &tmp);
1132
		if (ipm) {
1133
			/* for mcast, by-IP match means full match */
1134
			ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
1135
			continue;
1136
		}
1137 1138 1139 1140 1141 1142 1143

		ipm = kmemdup(&tmp, sizeof(tmp), GFP_KERNEL);
		if (!ipm)
			continue;

		hash_add(card->ip_mc_htable, &ipm->hnode,
			 qeth_l3_ipaddr_hash(ipm));
F
Frank Blaschka 已提交
1144
	}
1145

1146 1147 1148
walk_ipv6:
	if (!qeth_is_supported(card, IPA_IPV6))
		goto out;
F
Frank Blaschka 已提交
1149

1150 1151 1152
	in6_dev = __in6_dev_get(dev);
	if (!in6_dev)
		goto out;
1153

1154 1155 1156
	qeth_l3_init_ipaddr(&tmp, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6);
	tmp.disp_flag = QETH_DISP_ADDR_ADD;
	tmp.is_multicast = 1;
1157

1158
	read_lock_bh(&in6_dev->lock);
F
Frank Blaschka 已提交
1159
	for (im6 = in6_dev->mc_list; im6 != NULL; im6 = im6->next) {
1160
		tmp.u.a6.addr = im6->mca_addr;
1161

1162
		ipm = qeth_l3_find_addr_by_ip(card, &tmp);
1163
		if (ipm) {
1164
			/* for mcast, by-IP match means full match */
1165 1166 1167 1168
			ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
			continue;
		}

1169
		ipm = kmemdup(&tmp, sizeof(tmp), GFP_ATOMIC);
F
Frank Blaschka 已提交
1170 1171
		if (!ipm)
			continue;
1172 1173 1174 1175

		hash_add(card->ip_mc_htable,
				&ipm->hnode, qeth_l3_ipaddr_hash(ipm));

F
Frank Blaschka 已提交
1176
	}
1177 1178
	read_unlock_bh(&in6_dev->lock);

1179
out:
1180
	return 0;
F
Frank Blaschka 已提交
1181 1182
}

1183 1184
static int qeth_l3_vlan_rx_add_vid(struct net_device *dev,
				   __be16 proto, u16 vid)
F
Frank Blaschka 已提交
1185
{
1186
	struct qeth_card *card = dev->ml_priv;
F
Frank Blaschka 已提交
1187

1188
	QETH_CARD_TEXT_(card, 4, "aid:%d", vid);
1189
	return 0;
F
Frank Blaschka 已提交
1190 1191
}

1192 1193
static int qeth_l3_vlan_rx_kill_vid(struct net_device *dev,
				    __be16 proto, u16 vid)
F
Frank Blaschka 已提交
1194
{
1195
	struct qeth_card *card = dev->ml_priv;
F
Frank Blaschka 已提交
1196

C
Carsten Otte 已提交
1197
	QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
1198
	return 0;
F
Frank Blaschka 已提交
1199 1200
}

1201 1202
static void qeth_l3_rebuild_skb(struct qeth_card *card, struct sk_buff *skb,
				struct qeth_hdr *hdr)
F
Frank Blaschka 已提交
1203
{
1204 1205 1206 1207 1208 1209 1210 1211 1212
	struct af_iucv_trans_hdr *iucv = (struct af_iucv_trans_hdr *) skb->data;
	struct net_device *dev = skb->dev;

	if (IS_IQD(card) && iucv->magic == ETH_P_AF_IUCV) {
		dev_hard_header(skb, dev, ETH_P_AF_IUCV, dev->dev_addr,
				"FAKELL", skb->len);
		return;
	}

F
Frank Blaschka 已提交
1213
	if (!(hdr->hdr.l3.flags & QETH_HDR_PASSTHRU)) {
1214 1215
		u16 prot = (hdr->hdr.l3.flags & QETH_HDR_IPV6) ? ETH_P_IPV6 :
								 ETH_P_IP;
1216
		unsigned char tg_addr[ETH_ALEN];
1217 1218

		skb_reset_network_header(skb);
F
Frank Blaschka 已提交
1219 1220
		switch (hdr->hdr.l3.flags & QETH_HDR_CAST_MASK) {
		case QETH_CAST_MULTICAST:
J
Julian Wiedmann 已提交
1221
			if (prot == ETH_P_IP)
1222
				ip_eth_mc_map(ip_hdr(skb)->daddr, tg_addr);
J
Julian Wiedmann 已提交
1223 1224
			else
				ipv6_eth_mc_map(&ipv6_hdr(skb)->daddr, tg_addr);
1225
			QETH_CARD_STAT_INC(card, rx_multicast);
F
Frank Blaschka 已提交
1226 1227
			break;
		case QETH_CAST_BROADCAST:
1228
			ether_addr_copy(tg_addr, card->dev->broadcast);
1229
			QETH_CARD_STAT_INC(card, rx_multicast);
F
Frank Blaschka 已提交
1230 1231
			break;
		default:
1232 1233
			if (card->options.sniffer)
				skb->pkt_type = PACKET_OTHERHOST;
1234
			ether_addr_copy(tg_addr, card->dev->dev_addr);
F
Frank Blaschka 已提交
1235
		}
J
Julian Wiedmann 已提交
1236

1237 1238
		if (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_SRC_MAC_ADDR)
			card->dev->header_ops->create(skb, card->dev, prot,
1239
				tg_addr, &hdr->hdr.l3.next_hop.rx.src_mac,
1240
				skb->len);
1241 1242
		else
			card->dev->header_ops->create(skb, card->dev, prot,
1243
				tg_addr, "FAKELL", skb->len);
F
Frank Blaschka 已提交
1244 1245
	}

1246 1247 1248 1249 1250 1251
	/* copy VLAN tag from hdr into skb */
	if (!card->options.sniffer &&
	    (hdr->hdr.l3.ext_flags & (QETH_HDR_EXT_VLAN_FRAME |
				      QETH_HDR_EXT_INCLUDE_VLAN_TAG))) {
		u16 tag = (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_VLAN_FRAME) ?
				hdr->hdr.l3.vlan_id :
1252
				hdr->hdr.l3.next_hop.rx.vlan_id;
1253
		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), tag);
F
Frank Blaschka 已提交
1254 1255
	}

1256
	qeth_rx_csum(card, skb, hdr->hdr.l3.ext_flags);
F
Frank Blaschka 已提交
1257 1258
}

1259 1260
static int qeth_l3_process_inbound_buffer(struct qeth_card *card,
				int budget, int *done)
F
Frank Blaschka 已提交
1261
{
1262
	int work_done = 0;
F
Frank Blaschka 已提交
1263 1264 1265
	struct sk_buff *skb;
	struct qeth_hdr *hdr;

1266
	*done = 0;
S
Stefan Raspl 已提交
1267
	WARN_ON_ONCE(!budget);
1268 1269
	while (budget) {
		skb = qeth_core_get_next_skb(card,
1270
			&card->qdio.in_q->bufs[card->rx.b_index],
1271 1272 1273 1274 1275
			&card->rx.b_element, &card->rx.e_offset, &hdr);
		if (!skb) {
			*done = 1;
			break;
		}
1276 1277

		if (hdr->hdr.l3.id == QETH_HEADER_TYPE_LAYER3)
1278
			qeth_l3_rebuild_skb(card, skb, hdr);
1279 1280 1281 1282 1283 1284

		skb->protocol = eth_type_trans(skb, skb->dev);
		QETH_CARD_STAT_INC(card, rx_packets);
		QETH_CARD_STAT_ADD(card, rx_bytes, skb->len);

		napi_gro_receive(&card->napi, skb);
1285 1286
		work_done++;
		budget--;
F
Frank Blaschka 已提交
1287
	}
1288 1289 1290
	return work_done;
}

1291
static void qeth_l3_stop_card(struct qeth_card *card)
F
Frank Blaschka 已提交
1292
{
1293
	QETH_CARD_TEXT(card, 2, "stopcard");
F
Frank Blaschka 已提交
1294 1295

	qeth_set_allowed_threads(card, 0, 1);
1296

1297 1298 1299
	cancel_work_sync(&card->rx_mode_work);
	qeth_l3_drain_rx_mode_cache(card);

1300 1301 1302
	if (card->options.sniffer &&
	    (card->info.promisc_mode == SET_PROMISC_MODE_ON))
		qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
1303

F
Frank Blaschka 已提交
1304
	if (card->state == CARD_STATE_SOFTSETUP) {
1305
		qeth_l3_clear_ip_htable(card, 1);
F
Frank Blaschka 已提交
1306 1307 1308 1309 1310
		qeth_clear_ipacmd_list(card);
		card->state = CARD_STATE_HARDSETUP;
	}
	if (card->state == CARD_STATE_HARDSETUP) {
		qeth_qdio_clear_card(card, 0);
1311
		qeth_drain_output_queues(card);
F
Frank Blaschka 已提交
1312 1313 1314
		qeth_clear_working_pool_list(card);
		card->state = CARD_STATE_DOWN;
	}
1315 1316

	flush_workqueue(card->event_wq);
F
Frank Blaschka 已提交
1317 1318
}

1319
static void qeth_l3_set_promisc_mode(struct qeth_card *card)
1320
{
1321
	bool enable = card->dev->flags & IFF_PROMISC;
1322

1323
	if (card->info.promisc_mode == enable)
1324 1325
		return;

1326
	if (IS_VM_NIC(card)) {		/* Guestlan trace */
1327
		if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
1328
			qeth_setadp_promisc_mode(card, enable);
1329 1330
	} else if (card->options.sniffer &&	/* HiperSockets trace */
		   qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) {
1331
		if (enable) {
C
Carsten Otte 已提交
1332
			QETH_CARD_TEXT(card, 3, "+promisc");
1333 1334
			qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_ENABLE);
		} else {
C
Carsten Otte 已提交
1335
			QETH_CARD_TEXT(card, 3, "-promisc");
1336 1337 1338 1339 1340
			qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
		}
	}
}

1341
static void qeth_l3_rx_mode_work(struct work_struct *work)
F
Frank Blaschka 已提交
1342
{
1343 1344
	struct qeth_card *card = container_of(work, struct qeth_card,
					      rx_mode_work);
1345 1346 1347
	struct qeth_ipaddr *addr;
	struct hlist_node *tmp;
	int i, rc;
F
Frank Blaschka 已提交
1348

C
Carsten Otte 已提交
1349
	QETH_CARD_TEXT(card, 3, "setmulti");
1350

1351
	if (!card->options.sniffer) {
1352 1353
		rtnl_lock();
		qeth_l3_add_mcast_rtnl(card->dev, 0, card);
1354
		if (qeth_is_supported(card, IPA_FULL_VLAN))
1355 1356
			vlan_for_each(card->dev, qeth_l3_add_mcast_rtnl, card);
		rtnl_unlock();
1357 1358 1359 1360 1361

		hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
			switch (addr->disp_flag) {
			case QETH_DISP_ADDR_DELETE:
				rc = qeth_l3_deregister_addr_entry(card, addr);
1362
				if (!rc || rc == -ENOENT) {
1363 1364 1365 1366 1367 1368
					hash_del(&addr->hnode);
					kfree(addr);
				}
				break;
			case QETH_DISP_ADDR_ADD:
				rc = qeth_l3_register_addr_entry(card, addr);
1369
				if (rc && rc != -ENETDOWN) {
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
					hash_del(&addr->hnode);
					kfree(addr);
					break;
				}
				addr->ref_counter = 1;
				/* fall through */
			default:
				/* for next call to set_rx_mode(): */
				addr->disp_flag = QETH_DISP_ADDR_DELETE;
			}
		}
1381
	}
1382 1383

	qeth_l3_set_promisc_mode(card);
F
Frank Blaschka 已提交
1384 1385
}

1386
static int qeth_l3_arp_makerc(u16 rc)
F
Frank Blaschka 已提交
1387
{
1388 1389 1390
	switch (rc) {
	case IPA_RC_SUCCESS:
		return 0;
F
Frank Blaschka 已提交
1391 1392
	case QETH_IPA_ARP_RC_NOTSUPP:
	case QETH_IPA_ARP_RC_Q_NOTSUPP:
1393 1394 1395
		return -EOPNOTSUPP;
	case QETH_IPA_ARP_RC_OUT_OF_RANGE:
		return -EINVAL;
F
Frank Blaschka 已提交
1396
	case QETH_IPA_ARP_RC_Q_NO_DATA:
1397
		return -ENOENT;
F
Frank Blaschka 已提交
1398
	default:
1399
		return -EIO;
F
Frank Blaschka 已提交
1400 1401 1402
	}
}

1403 1404 1405 1406 1407 1408 1409 1410 1411
static int qeth_l3_arp_cmd_cb(struct qeth_card *card, struct qeth_reply *reply,
			      unsigned long data)
{
	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;

	qeth_setassparms_cb(card, reply, data);
	return qeth_l3_arp_makerc(cmd->hdr.return_code);
}

F
Frank Blaschka 已提交
1412 1413
static int qeth_l3_arp_set_no_entries(struct qeth_card *card, int no_entries)
{
1414
	struct qeth_cmd_buffer *iob;
F
Frank Blaschka 已提交
1415 1416
	int rc;

C
Carsten Otte 已提交
1417
	QETH_CARD_TEXT(card, 3, "arpstnoe");
F
Frank Blaschka 已提交
1418 1419 1420 1421 1422 1423

	/*
	 * currently GuestLAN only supports the ARP assist function
	 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_SET_NO_ENTRIES;
	 * thus we say EOPNOTSUPP for this ARP function
	 */
1424
	if (IS_VM_NIC(card))
F
Frank Blaschka 已提交
1425 1426 1427 1428
		return -EOPNOTSUPP;
	if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
		return -EOPNOTSUPP;
	}
1429 1430

	iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1431 1432
				       IPA_CMD_ASS_ARP_SET_NO_ENTRIES,
				       SETASS_DATA_SIZEOF(flags_32bit),
1433 1434 1435 1436 1437 1438
				       QETH_PROT_IPV4);
	if (!iob)
		return -ENOMEM;

	__ipa_cmd(iob)->data.setassparms.data.flags_32bit = (u32) no_entries;
	rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL);
1439 1440 1441
	if (rc)
		QETH_DBF_MESSAGE(2, "Could not set number of ARP entries on device %x: %#x\n",
				 CARD_DEVID(card), rc);
1442
	return rc;
F
Frank Blaschka 已提交
1443 1444
}

1445 1446 1447
static __u32 get_arp_entry_size(struct qeth_card *card,
			struct qeth_arp_query_data *qdata,
			struct qeth_arp_entrytype *type, __u8 strip_entries)
F
Frank Blaschka 已提交
1448
{
1449 1450
	__u32 rc;
	__u8 is_hsi;
F
Frank Blaschka 已提交
1451

1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
	is_hsi = qdata->reply_bits == 5;
	if (type->ip == QETHARP_IP_ADDR_V4) {
		QETH_CARD_TEXT(card, 4, "arpev4");
		if (strip_entries) {
			rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5_short) :
				sizeof(struct qeth_arp_qi_entry7_short);
		} else {
			rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5) :
				sizeof(struct qeth_arp_qi_entry7);
		}
	} else if (type->ip == QETHARP_IP_ADDR_V6) {
		QETH_CARD_TEXT(card, 4, "arpev6");
		if (strip_entries) {
			rc = is_hsi ?
				sizeof(struct qeth_arp_qi_entry5_short_ipv6) :
				sizeof(struct qeth_arp_qi_entry7_short_ipv6);
		} else {
			rc = is_hsi ?
				sizeof(struct qeth_arp_qi_entry5_ipv6) :
				sizeof(struct qeth_arp_qi_entry7_ipv6);
		}
	} else {
		QETH_CARD_TEXT(card, 4, "arpinv");
		rc = 0;
F
Frank Blaschka 已提交
1476
	}
1477 1478 1479 1480 1481 1482 1483 1484

	return rc;
}

static int arpentry_matches_prot(struct qeth_arp_entrytype *type, __u16 prot)
{
	return (type->ip == QETHARP_IP_ADDR_V4 && prot == QETH_PROT_IPV4) ||
		(type->ip == QETHARP_IP_ADDR_V6 && prot == QETH_PROT_IPV6);
F
Frank Blaschka 已提交
1485 1486 1487 1488 1489 1490 1491 1492
}

static int qeth_l3_arp_query_cb(struct qeth_card *card,
		struct qeth_reply *reply, unsigned long data)
{
	struct qeth_ipa_cmd *cmd;
	struct qeth_arp_query_data *qdata;
	struct qeth_arp_query_info *qinfo;
1493 1494 1495 1496
	int e;
	int entrybytes_done;
	int stripped_bytes;
	__u8 do_strip_entries;
F
Frank Blaschka 已提交
1497

1498
	QETH_CARD_TEXT(card, 3, "arpquecb");
F
Frank Blaschka 已提交
1499 1500 1501

	qinfo = (struct qeth_arp_query_info *) reply->param;
	cmd = (struct qeth_ipa_cmd *) data;
1502
	QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.prot_version);
F
Frank Blaschka 已提交
1503
	if (cmd->hdr.return_code) {
1504 1505
		QETH_CARD_TEXT(card, 4, "arpcberr");
		QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1506
		return qeth_l3_arp_makerc(cmd->hdr.return_code);
F
Frank Blaschka 已提交
1507 1508 1509
	}
	if (cmd->data.setassparms.hdr.return_code) {
		cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
1510 1511
		QETH_CARD_TEXT(card, 4, "setaperr");
		QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1512
		return qeth_l3_arp_makerc(cmd->hdr.return_code);
F
Frank Blaschka 已提交
1513 1514
	}
	qdata = &cmd->data.setassparms.data.query_arp;
C
Carsten Otte 已提交
1515
	QETH_CARD_TEXT_(card, 4, "anoen%i", qdata->no_entries);
F
Frank Blaschka 已提交
1516

1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
	do_strip_entries = (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) > 0;
	stripped_bytes = do_strip_entries ? QETH_QARP_MEDIASPECIFIC_BYTES : 0;
	entrybytes_done = 0;
	for (e = 0; e < qdata->no_entries; ++e) {
		char *cur_entry;
		__u32 esize;
		struct qeth_arp_entrytype *etype;

		cur_entry = &qdata->data + entrybytes_done;
		etype = &((struct qeth_arp_qi_entry5 *) cur_entry)->type;
		if (!arpentry_matches_prot(etype, cmd->hdr.prot_version)) {
			QETH_CARD_TEXT(card, 4, "pmis");
			QETH_CARD_TEXT_(card, 4, "%i", etype->ip);
			break;
		}
		esize = get_arp_entry_size(card, qdata, etype,
			do_strip_entries);
		QETH_CARD_TEXT_(card, 5, "esz%i", esize);
		if (!esize)
			break;

		if ((qinfo->udata_len - qinfo->udata_offset) < esize) {
1539 1540 1541
			QETH_CARD_TEXT_(card, 4, "qaer3%i", -ENOSPC);
			memset(qinfo->udata, 0, 4);
			return -ENOSPC;
1542
		}
F
Frank Blaschka 已提交
1543

1544 1545 1546 1547 1548 1549 1550
		memcpy(qinfo->udata + qinfo->udata_offset,
			&qdata->data + entrybytes_done + stripped_bytes,
			esize);
		entrybytes_done += esize + stripped_bytes;
		qinfo->udata_offset += esize;
		++qinfo->no_entries;
	}
F
Frank Blaschka 已提交
1551 1552 1553 1554
	/* check if all replies received ... */
	if (cmd->data.setassparms.hdr.seq_no <
	    cmd->data.setassparms.hdr.number_of_replies)
		return 1;
1555
	QETH_CARD_TEXT_(card, 4, "nove%i", qinfo->no_entries);
F
Frank Blaschka 已提交
1556 1557 1558 1559 1560 1561
	memcpy(qinfo->udata, &qinfo->no_entries, 4);
	/* keep STRIP_ENTRIES flag so the user program can distinguish
	 * stripped entries from normal ones */
	if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
		qdata->reply_bits |= QETH_QARP_STRIP_ENTRIES;
	memcpy(qinfo->udata + QETH_QARP_MASK_OFFSET, &qdata->reply_bits, 2);
1562
	QETH_CARD_TEXT_(card, 4, "rc%i", 0);
F
Frank Blaschka 已提交
1563 1564 1565
	return 0;
}

1566 1567 1568
static int qeth_l3_query_arp_cache_info(struct qeth_card *card,
	enum qeth_prot_versions prot,
	struct qeth_arp_query_info *qinfo)
F
Frank Blaschka 已提交
1569 1570
{
	struct qeth_cmd_buffer *iob;
1571
	struct qeth_ipa_cmd *cmd;
F
Frank Blaschka 已提交
1572 1573
	int rc;

1574 1575
	QETH_CARD_TEXT_(card, 3, "qarpipv%i", prot);

1576 1577
	iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
				       IPA_CMD_ASS_ARP_QUERY_INFO,
1578
				       SETASS_DATA_SIZEOF(query_arp), prot);
1579 1580
	if (!iob)
		return -ENOMEM;
1581
	cmd = __ipa_cmd(iob);
1582
	cmd->data.setassparms.data.query_arp.request_bits = 0x000F;
1583
	rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_query_cb, qinfo);
1584 1585 1586
	if (rc)
		QETH_DBF_MESSAGE(2, "Error while querying ARP cache on device %x: %#x\n",
				 CARD_DEVID(card), rc);
1587
	return rc;
1588 1589 1590 1591 1592 1593 1594
}

static int qeth_l3_arp_query(struct qeth_card *card, char __user *udata)
{
	struct qeth_arp_query_info qinfo = {0, };
	int rc;

C
Carsten Otte 已提交
1595
	QETH_CARD_TEXT(card, 3, "arpquery");
F
Frank Blaschka 已提交
1596 1597 1598

	if (!qeth_is_supported(card,/*IPA_QUERY_ARP_ADDR_INFO*/
			       IPA_ARP_PROCESSING)) {
1599 1600 1601
		QETH_CARD_TEXT(card, 3, "arpqnsup");
		rc = -EOPNOTSUPP;
		goto out;
F
Frank Blaschka 已提交
1602 1603
	}
	/* get size of userspace buffer and mask_bits -> 6 bytes */
1604 1605 1606 1607
	if (copy_from_user(&qinfo, udata, 6)) {
		rc = -EFAULT;
		goto out;
	}
F
Frank Blaschka 已提交
1608
	qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
1609 1610 1611 1612
	if (!qinfo.udata) {
		rc = -ENOMEM;
		goto out;
	}
F
Frank Blaschka 已提交
1613
	qinfo.udata_offset = QETH_QARP_ENTRIES_OFFSET;
1614
	rc = qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV4, &qinfo);
F
Frank Blaschka 已提交
1615 1616 1617
	if (rc) {
		if (copy_to_user(udata, qinfo.udata, 4))
			rc = -EFAULT;
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
		goto free_and_out;
	}
	if (qinfo.mask_bits & QETH_QARP_WITH_IPV6) {
		/* fails in case of GuestLAN QDIO mode */
		qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV6, &qinfo);
	}
	if (copy_to_user(udata, qinfo.udata, qinfo.udata_len)) {
		QETH_CARD_TEXT(card, 4, "qactf");
		rc = -EFAULT;
		goto free_and_out;
F
Frank Blaschka 已提交
1628
	}
1629 1630
	QETH_CARD_TEXT(card, 4, "qacts");

1631
free_and_out:
F
Frank Blaschka 已提交
1632
	kfree(qinfo.udata);
1633
out:
F
Frank Blaschka 已提交
1634 1635 1636
	return rc;
}

J
Julian Wiedmann 已提交
1637 1638 1639
static int qeth_l3_arp_modify_entry(struct qeth_card *card,
				    struct qeth_arp_cache_entry *entry,
				    enum qeth_arp_process_subcmds arp_cmd)
F
Frank Blaschka 已提交
1640
{
J
Julian Wiedmann 已提交
1641
	struct qeth_arp_cache_entry *cmd_entry;
F
Frank Blaschka 已提交
1642 1643 1644
	struct qeth_cmd_buffer *iob;
	int rc;

J
Julian Wiedmann 已提交
1645 1646 1647 1648
	if (arp_cmd == IPA_CMD_ASS_ARP_ADD_ENTRY)
		QETH_CARD_TEXT(card, 3, "arpadd");
	else
		QETH_CARD_TEXT(card, 3, "arpdel");
F
Frank Blaschka 已提交
1649 1650 1651 1652 1653 1654

	/*
	 * currently GuestLAN only supports the ARP assist function
	 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_ADD_ENTRY;
	 * thus we say EOPNOTSUPP for this ARP function
	 */
1655
	if (IS_VM_NIC(card))
F
Frank Blaschka 已提交
1656 1657 1658 1659 1660
		return -EOPNOTSUPP;
	if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
		return -EOPNOTSUPP;
	}

J
Julian Wiedmann 已提交
1661
	iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING, arp_cmd,
1662 1663
				       SETASS_DATA_SIZEOF(arp_entry),
				       QETH_PROT_IPV4);
1664 1665
	if (!iob)
		return -ENOMEM;
F
Frank Blaschka 已提交
1666

J
Julian Wiedmann 已提交
1667 1668 1669
	cmd_entry = &__ipa_cmd(iob)->data.setassparms.data.arp_entry;
	ether_addr_copy(cmd_entry->macaddr, entry->macaddr);
	memcpy(cmd_entry->ipaddr, entry->ipaddr, 4);
1670
	rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL);
1671
	if (rc)
J
Julian Wiedmann 已提交
1672 1673
		QETH_DBF_MESSAGE(2, "Could not modify (cmd: %#x) ARP entry on device %x: %#x\n",
				 arp_cmd, CARD_DEVID(card), rc);
1674
	return rc;
F
Frank Blaschka 已提交
1675 1676 1677 1678
}

static int qeth_l3_arp_flush_cache(struct qeth_card *card)
{
1679
	struct qeth_cmd_buffer *iob;
F
Frank Blaschka 已提交
1680 1681
	int rc;

C
Carsten Otte 已提交
1682
	QETH_CARD_TEXT(card, 3, "arpflush");
F
Frank Blaschka 已提交
1683 1684 1685 1686 1687 1688

	/*
	 * currently GuestLAN only supports the ARP assist function
	 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_FLUSH_CACHE;
	 * thus we say EOPNOTSUPP for this ARP function
	*/
1689
	if (IS_VM_NIC(card) || IS_IQD(card))
F
Frank Blaschka 已提交
1690 1691 1692 1693
		return -EOPNOTSUPP;
	if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
		return -EOPNOTSUPP;
	}
1694 1695 1696 1697 1698 1699 1700 1701

	iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
				       IPA_CMD_ASS_ARP_FLUSH_CACHE, 0,
				       QETH_PROT_IPV4);
	if (!iob)
		return -ENOMEM;

	rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL);
1702 1703 1704
	if (rc)
		QETH_DBF_MESSAGE(2, "Could not flush ARP cache on device %x: %#x\n",
				 CARD_DEVID(card), rc);
1705
	return rc;
F
Frank Blaschka 已提交
1706 1707 1708 1709
}

static int qeth_l3_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
1710
	struct qeth_card *card = dev->ml_priv;
F
Frank Blaschka 已提交
1711
	struct qeth_arp_cache_entry arp_entry;
J
Julian Wiedmann 已提交
1712
	enum qeth_arp_process_subcmds arp_cmd;
F
Frank Blaschka 已提交
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
	int rc = 0;

	switch (cmd) {
	case SIOC_QETH_ARP_SET_NO_ENTRIES:
		if (!capable(CAP_NET_ADMIN)) {
			rc = -EPERM;
			break;
		}
		rc = qeth_l3_arp_set_no_entries(card, rq->ifr_ifru.ifru_ivalue);
		break;
	case SIOC_QETH_ARP_QUERY_INFO:
		if (!capable(CAP_NET_ADMIN)) {
			rc = -EPERM;
			break;
		}
		rc = qeth_l3_arp_query(card, rq->ifr_ifru.ifru_data);
		break;
	case SIOC_QETH_ARP_ADD_ENTRY:
	case SIOC_QETH_ARP_REMOVE_ENTRY:
J
Julian Wiedmann 已提交
1732 1733 1734 1735 1736 1737 1738 1739 1740
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		if (copy_from_user(&arp_entry, rq->ifr_data, sizeof(arp_entry)))
			return -EFAULT;

		arp_cmd = (cmd == SIOC_QETH_ARP_ADD_ENTRY) ?
				IPA_CMD_ASS_ARP_ADD_ENTRY :
				IPA_CMD_ASS_ARP_REMOVE_ENTRY;
		return qeth_l3_arp_modify_entry(card, &arp_entry, arp_cmd);
F
Frank Blaschka 已提交
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
	case SIOC_QETH_ARP_FLUSH_CACHE:
		if (!capable(CAP_NET_ADMIN)) {
			rc = -EPERM;
			break;
		}
		rc = qeth_l3_arp_flush_cache(card);
		break;
	default:
		rc = -EOPNOTSUPP;
	}
	return rc;
}

1754 1755
static int qeth_l3_get_cast_type_rcu(struct sk_buff *skb, struct dst_entry *dst,
				     int ipv)
1756
{
1757 1758
	struct neighbour *n = NULL;

1759 1760
	if (dst)
		n = dst_neigh_lookup_skb(dst, skb);
1761

1762
	if (n) {
1763 1764
		int cast_type = n->type;

1765
		neigh_release(n);
1766 1767 1768 1769
		if ((cast_type == RTN_BROADCAST) ||
		    (cast_type == RTN_MULTICAST) ||
		    (cast_type == RTN_ANYCAST))
			return cast_type;
1770
		return RTN_UNICAST;
1771
	}
F
Frank Blaschka 已提交
1772

1773
	/* no neighbour (eg AF_PACKET), fall back to target's IP address ... */
1774
	switch (ipv) {
1775
	case 4:
1776 1777
		if (ipv4_is_lbcast(ip_hdr(skb)->daddr))
			return RTN_BROADCAST;
1778
		return ipv4_is_multicast(ip_hdr(skb)->daddr) ?
1779
				RTN_MULTICAST : RTN_UNICAST;
1780 1781 1782 1783 1784
	case 6:
		return ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ?
				RTN_MULTICAST : RTN_UNICAST;
	default:
		/* ... and MAC address */
1785
		return qeth_get_ether_cast_type(skb);
1786
	}
1787 1788
}

1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
static int qeth_l3_get_cast_type(struct sk_buff *skb)
{
	int ipv = qeth_get_ip_version(skb);
	struct dst_entry *dst;
	int cast_type;

	rcu_read_lock();
	dst = qeth_dst_check_rcu(skb, ipv);
	cast_type = qeth_l3_get_cast_type_rcu(skb, dst, ipv);
	rcu_read_unlock();

	return cast_type;
}

1803
static u8 qeth_l3_cast_type_to_flag(int cast_type)
F
Frank Blaschka 已提交
1804
{
1805 1806 1807 1808 1809 1810 1811 1812
	if (cast_type == RTN_MULTICAST)
		return QETH_CAST_MULTICAST;
	if (cast_type == RTN_ANYCAST)
		return QETH_CAST_ANYCAST;
	if (cast_type == RTN_BROADCAST)
		return QETH_CAST_BROADCAST;
	return QETH_CAST_UNICAST;
}
1813

1814 1815
static void qeth_l3_fill_header(struct qeth_qdio_out_q *queue,
				struct qeth_hdr *hdr, struct sk_buff *skb,
1816
				int ipv, unsigned int data_len)
1817
{
1818
	struct qeth_hdr_layer3 *l3_hdr = &hdr->hdr.l3;
1819
	struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
1820
	struct qeth_card *card = queue->card;
1821
	struct dst_entry *dst;
1822
	int cast_type;
1823

1824
	hdr->hdr.l3.length = data_len;
F
Frank Blaschka 已提交
1825

1826
	if (skb_is_gso(skb)) {
1827
		hdr->hdr.l3.id = QETH_HEADER_TYPE_L3_TSO;
1828 1829
	} else {
		hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
1830 1831 1832 1833 1834 1835 1836 1837 1838

		if (skb->protocol == htons(ETH_P_AF_IUCV)) {
			l3_hdr->flags = QETH_HDR_IPV6 | QETH_CAST_UNICAST;
			l3_hdr->next_hop.ipv6_addr.s6_addr16[0] = htons(0xfe80);
			memcpy(&l3_hdr->next_hop.ipv6_addr.s6_addr32[2],
			       iucv_trans_hdr(skb)->destUserID, 8);
			return;
		}

1839 1840 1841 1842 1843 1844 1845 1846
		if (skb->ip_summed == CHECKSUM_PARTIAL) {
			qeth_tx_csum(skb, &hdr->hdr.l3.ext_flags, ipv);
			/* some HW requires combined L3+L4 csum offload: */
			if (ipv == 4)
				hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_CSUM_HDR_REQ;
		}
	}

1847 1848 1849 1850 1851 1852 1853 1854 1855
	if (ipv == 4 || IS_IQD(card)) {
		/* NETIF_F_HW_VLAN_CTAG_TX */
		if (skb_vlan_tag_present(skb)) {
			hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_VLAN_FRAME;
			hdr->hdr.l3.vlan_id = skb_vlan_tag_get(skb);
		}
	} else if (veth->h_vlan_proto == htons(ETH_P_8021Q)) {
		hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_INCLUDE_VLAN_TAG;
		hdr->hdr.l3.vlan_id = ntohs(veth->h_vlan_TCI);
F
Frank Blaschka 已提交
1856 1857
	}

F
Frank Blaschka 已提交
1858
	rcu_read_lock();
1859
	dst = qeth_dst_check_rcu(skb, ipv);
1860

1861 1862 1863 1864 1865 1866
	if (IS_IQD(card) && skb_get_queue_mapping(skb) != QETH_IQD_MCAST_TXQ)
		cast_type = RTN_UNICAST;
	else
		cast_type = qeth_l3_get_cast_type_rcu(skb, dst, ipv);
	l3_hdr->flags |= qeth_l3_cast_type_to_flag(cast_type);

F
Frank Blaschka 已提交
1867
	if (ipv == 4) {
1868
		struct rtable *rt = (struct rtable *) dst;
1869

1870 1871 1872
		*((__be32 *) &hdr->hdr.l3.next_hop.ipv4.addr) = (rt) ?
				rt_nexthop(rt, ip_hdr(skb)->daddr) :
				ip_hdr(skb)->daddr;
1873
	} else if (ipv == 6) {
1874
		struct rt6_info *rt = (struct rt6_info *) dst;
1875

1876
		if (rt && !ipv6_addr_any(&rt->rt6i_gateway))
1877
			l3_hdr->next_hop.ipv6_addr = rt->rt6i_gateway;
1878
		else
1879
			l3_hdr->next_hop.ipv6_addr = ipv6_hdr(skb)->daddr;
1880

1881
		hdr->hdr.l3.flags |= QETH_HDR_IPV6;
1882
		if (!IS_IQD(card))
1883
			hdr->hdr.l3.flags |= QETH_HDR_PASSTHRU;
1884 1885 1886
	} else {
		/* OSA only: */
		l3_hdr->flags |= QETH_HDR_PASSTHRU;
F
Frank Blaschka 已提交
1887
	}
F
Frank Blaschka 已提交
1888
	rcu_read_unlock();
F
Frank Blaschka 已提交
1889 1890
}

1891 1892 1893 1894 1895 1896 1897
static void qeth_l3_fixup_headers(struct sk_buff *skb)
{
	struct iphdr *iph = ip_hdr(skb);

	/* this is safe, IPv6 traffic takes a different path */
	if (skb->ip_summed == CHECKSUM_PARTIAL)
		iph->check = 0;
1898 1899 1900 1901 1902
	if (skb_is_gso(skb)) {
		iph->tot_len = 0;
		tcp_hdr(skb)->check = ~tcp_v4_check(0, iph->saddr,
						    iph->daddr, 0);
	}
1903 1904
}

1905
static int qeth_l3_xmit(struct qeth_card *card, struct sk_buff *skb,
1906
			struct qeth_qdio_out_q *queue, int ipv)
1907
{
J
Julian Wiedmann 已提交
1908 1909
	unsigned int hw_hdr_len;
	int rc;
1910

1911
	/* re-use the L2 header area for the HW header: */
J
Julian Wiedmann 已提交
1912 1913
	hw_hdr_len = skb_is_gso(skb) ? sizeof(struct qeth_hdr_tso) :
				       sizeof(struct qeth_hdr);
1914 1915 1916 1917 1918
	rc = skb_cow_head(skb, hw_hdr_len - ETH_HLEN);
	if (rc)
		return rc;
	skb_pull(skb, ETH_HLEN);

1919
	qeth_l3_fixup_headers(skb);
1920
	return qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header);
1921 1922
}

1923 1924 1925 1926
static netdev_tx_t qeth_l3_hard_start_xmit(struct sk_buff *skb,
					   struct net_device *dev)
{
	struct qeth_card *card = dev->ml_priv;
1927
	u16 txq = skb_get_queue_mapping(skb);
1928 1929
	int ipv = qeth_get_ip_version(skb);
	struct qeth_qdio_out_q *queue;
1930
	int rc;
1931

1932 1933
	if (!skb_is_gso(skb))
		qdisc_skb_cb(skb)->pkt_len = skb->len;
1934
	if (IS_IQD(card)) {
1935 1936
		queue = card->qdio.out_qs[qeth_iqd_translate_txq(dev, txq)];

1937 1938 1939 1940 1941
		if (card->options.sniffer)
			goto tx_drop;
		if ((card->options.cq != QETH_CQ_ENABLED && !ipv) ||
		    (card->options.cq == QETH_CQ_ENABLED &&
		     skb->protocol != htons(ETH_P_AF_IUCV)))
F
Frank Blaschka 已提交
1942
			goto tx_drop;
1943
	} else {
1944
		queue = card->qdio.out_qs[txq];
F
Frank Blaschka 已提交
1945 1946
	}

1947 1948
	if (!(dev->flags & IFF_BROADCAST) &&
	    qeth_l3_get_cast_type(skb) == RTN_BROADCAST)
1949 1950
		goto tx_drop;

1951
	if (ipv == 4 || IS_IQD(card))
1952
		rc = qeth_l3_xmit(card, skb, queue, ipv);
1953
	else
1954
		rc = qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header);
1955

1956
	if (!rc)
1957
		return NETDEV_TX_OK;
F
Frank Blaschka 已提交
1958 1959

tx_drop:
1960
	QETH_TXQ_STAT_INC(queue, tx_dropped);
1961
	kfree_skb(skb);
F
Frank Blaschka 已提交
1962 1963 1964
	return NETDEV_TX_OK;
}

1965 1966 1967 1968 1969 1970 1971
static void qeth_l3_set_rx_mode(struct net_device *dev)
{
	struct qeth_card *card = dev->ml_priv;

	schedule_work(&card->rx_mode_work);
}

F
Frank Blaschka 已提交
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
/*
 * we need NOARP for IPv4 but we want neighbor solicitation for IPv6. Setting
 * NOARP on the netdevice is no option because it also turns off neighbor
 * solicitation. For IPv4 we install a neighbor_setup function. We don't want
 * arp resolution but we want the hard header (packet socket will work
 * e.g. tcpdump)
 */
static int qeth_l3_neigh_setup_noarp(struct neighbour *n)
{
	n->nud_state = NUD_NOARP;
	memcpy(n->ha, "FAKELL", 6);
	n->output = n->ops->connected_output;
	return 0;
}

static int
qeth_l3_neigh_setup(struct net_device *dev, struct neigh_parms *np)
{
	if (np->tbl->family == AF_INET)
		np->neigh_setup = qeth_l3_neigh_setup_noarp;

	return 0;
}

1996 1997 1998 1999 2000 2001 2002 2003 2004
static netdev_features_t qeth_l3_osa_features_check(struct sk_buff *skb,
						    struct net_device *dev,
						    netdev_features_t features)
{
	if (qeth_get_ip_version(skb) != 4)
		features &= ~NETIF_F_HW_VLAN_CTAG_TX;
	return qeth_features_check(skb, dev, features);
}

2005 2006 2007 2008 2009 2010 2011
static u16 qeth_l3_iqd_select_queue(struct net_device *dev, struct sk_buff *skb,
				    struct net_device *sb_dev)
{
	return qeth_iqd_select_queue(dev, skb, qeth_l3_get_cast_type(skb),
				     sb_dev);
}

2012 2013 2014 2015 2016 2017 2018 2019
static u16 qeth_l3_osa_select_queue(struct net_device *dev, struct sk_buff *skb,
				    struct net_device *sb_dev)
{
	struct qeth_card *card = dev->ml_priv;

	return qeth_get_priority_queue(card, skb);
}

F
Frank Blaschka 已提交
2020
static const struct net_device_ops qeth_l3_netdev_ops = {
2021 2022
	.ndo_open		= qeth_open,
	.ndo_stop		= qeth_stop,
2023
	.ndo_get_stats64	= qeth_get_stats64,
F
Frank Blaschka 已提交
2024
	.ndo_start_xmit		= qeth_l3_hard_start_xmit,
2025
	.ndo_select_queue	= qeth_l3_iqd_select_queue,
F
Frank Blaschka 已提交
2026
	.ndo_validate_addr	= eth_validate_addr,
2027
	.ndo_set_rx_mode	= qeth_l3_set_rx_mode,
2028
	.ndo_do_ioctl		= qeth_do_ioctl,
2029 2030
	.ndo_fix_features	= qeth_fix_features,
	.ndo_set_features	= qeth_set_features,
F
Frank Blaschka 已提交
2031 2032
	.ndo_vlan_rx_add_vid	= qeth_l3_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
J
Jiri Pirko 已提交
2033
	.ndo_tx_timeout		= qeth_tx_timeout,
F
Frank Blaschka 已提交
2034 2035
};

F
Frank Blaschka 已提交
2036
static const struct net_device_ops qeth_l3_osa_netdev_ops = {
2037 2038
	.ndo_open		= qeth_open,
	.ndo_stop		= qeth_stop,
2039
	.ndo_get_stats64	= qeth_get_stats64,
F
Frank Blaschka 已提交
2040
	.ndo_start_xmit		= qeth_l3_hard_start_xmit,
2041
	.ndo_features_check	= qeth_l3_osa_features_check,
2042
	.ndo_select_queue	= qeth_l3_osa_select_queue,
F
Frank Blaschka 已提交
2043
	.ndo_validate_addr	= eth_validate_addr,
2044
	.ndo_set_rx_mode	= qeth_l3_set_rx_mode,
2045
	.ndo_do_ioctl		= qeth_do_ioctl,
2046 2047
	.ndo_fix_features	= qeth_fix_features,
	.ndo_set_features	= qeth_set_features,
F
Frank Blaschka 已提交
2048 2049
	.ndo_vlan_rx_add_vid	= qeth_l3_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
J
Jiri Pirko 已提交
2050
	.ndo_tx_timeout		= qeth_tx_timeout,
F
Frank Blaschka 已提交
2051 2052 2053
	.ndo_neigh_setup	= qeth_l3_neigh_setup,
};

J
Julian Wiedmann 已提交
2054
static int qeth_l3_setup_netdev(struct qeth_card *card, bool carrier_ok)
F
Frank Blaschka 已提交
2055
{
2056
	unsigned int headroom;
2057 2058
	int rc;

2059
	if (IS_OSD(card) || IS_OSX(card)) {
F
Frank Blaschka 已提交
2060 2061
		if ((card->info.link_type == QETH_LINK_TYPE_LANE_TR) ||
		    (card->info.link_type == QETH_LINK_TYPE_HSTR)) {
2062 2063
			pr_info("qeth_l3: ignoring TR device\n");
			return -ENODEV;
2064 2065 2066 2067 2068 2069 2070 2071 2072
		}

		card->dev->netdev_ops = &qeth_l3_osa_netdev_ops;

		/*IPv6 address autoconfiguration stuff*/
		qeth_l3_get_unique_id(card);
		if (!(card->info.unique_id & UNIQUE_ID_NOT_BY_CARD))
			card->dev->dev_id = card->info.unique_id & 0xffff;

2073
		if (!IS_VM_NIC(card)) {
2074 2075 2076 2077 2078
			card->dev->features |= NETIF_F_SG;
			card->dev->hw_features |= NETIF_F_TSO |
				NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
			card->dev->vlan_features |= NETIF_F_TSO |
				NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
F
Frank Blaschka 已提交
2079
		}
2080 2081 2082 2083 2084

		if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) {
			card->dev->hw_features |= NETIF_F_IPV6_CSUM;
			card->dev->vlan_features |= NETIF_F_IPV6_CSUM;
		}
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
		if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) {
			card->dev->hw_features |= NETIF_F_TSO6;
			card->dev->vlan_features |= NETIF_F_TSO6;
		}

		/* allow for de-acceleration of NETIF_F_HW_VLAN_CTAG_TX: */
		if (card->dev->hw_features & NETIF_F_TSO6)
			headroom = sizeof(struct qeth_hdr_tso) + VLAN_HLEN;
		else if (card->dev->hw_features & NETIF_F_TSO)
			headroom = sizeof(struct qeth_hdr_tso);
		else
			headroom = sizeof(struct qeth_hdr) + VLAN_HLEN;
2097
	} else if (IS_IQD(card)) {
F
Frank Blaschka 已提交
2098
		card->dev->flags |= IFF_NOARP;
F
Frank Blaschka 已提交
2099
		card->dev->netdev_ops = &qeth_l3_netdev_ops;
2100
		headroom = sizeof(struct qeth_hdr) - ETH_HLEN;
2101

2102 2103
		rc = qeth_l3_iqd_read_initial_mac(card);
		if (rc)
2104
			goto out;
F
Frank Blaschka 已提交
2105 2106 2107
	} else
		return -ENODEV;

2108
	card->dev->needed_headroom = headroom;
2109 2110 2111
	card->dev->features |=	NETIF_F_HW_VLAN_CTAG_TX |
				NETIF_F_HW_VLAN_CTAG_RX |
				NETIF_F_HW_VLAN_CTAG_FILTER;
2112

2113
	netif_keep_dst(card->dev);
2114
	if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6))
2115 2116
		netif_set_gso_max_size(card->dev,
				       PAGE_SIZE * (QETH_MAX_BUFFER_ELEMENTS(card) - 1));
F
Frank Blaschka 已提交
2117

2118
	netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
2119
	rc = register_netdev(card->dev);
J
Julian Wiedmann 已提交
2120 2121 2122
	if (!rc && carrier_ok)
		netif_carrier_on(card->dev);

2123 2124 2125 2126
out:
	if (rc)
		card->dev->netdev_ops = NULL;
	return rc;
F
Frank Blaschka 已提交
2127 2128
}

2129 2130 2131 2132 2133
static const struct device_type qeth_l3_devtype = {
	.name = "qeth_layer3",
	.groups = qeth_l3_attr_groups,
};

F
Frank Blaschka 已提交
2134 2135 2136
static int qeth_l3_probe_device(struct ccwgroup_device *gdev)
{
	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2137
	int rc;
F
Frank Blaschka 已提交
2138

2139
	hash_init(card->ip_htable);
2140
	mutex_init(&card->ip_lock);
2141 2142 2143 2144
	card->cmd_wq = alloc_ordered_workqueue("%s_cmd", 0,
					       dev_name(&gdev->dev));
	if (!card->cmd_wq)
		return -ENOMEM;
2145

2146 2147
	if (gdev->dev.type == &qeth_generic_devtype) {
		rc = qeth_l3_create_device_attributes(&gdev->dev);
2148 2149
		if (rc) {
			destroy_workqueue(card->cmd_wq);
2150
			return rc;
2151
		}
2152
	}
2153

2154
	hash_init(card->ip_mc_htable);
2155
	INIT_WORK(&card->rx_mode_work, qeth_l3_rx_mode_work);
F
Frank Blaschka 已提交
2156 2157 2158 2159 2160 2161 2162
	return 0;
}

static void qeth_l3_remove_device(struct ccwgroup_device *cgdev)
{
	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);

2163 2164
	if (cgdev->dev.type == &qeth_generic_devtype)
		qeth_l3_remove_device_attributes(&cgdev->dev);
2165

2166
	qeth_set_allowed_threads(card, 0, 1);
F
Frank Blaschka 已提交
2167 2168
	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);

2169
	if (cgdev->state == CCWGROUP_ONLINE)
F
Frank Blaschka 已提交
2170 2171
		qeth_l3_set_offline(cgdev);

2172
	cancel_work_sync(&card->close_dev_work);
2173 2174
	if (qeth_netdev_is_registered(card->dev))
		unregister_netdev(card->dev);
2175 2176 2177

	flush_workqueue(card->cmd_wq);
	destroy_workqueue(card->cmd_wq);
2178
	qeth_l3_clear_ip_htable(card, 0);
F
Frank Blaschka 已提交
2179 2180 2181
	qeth_l3_clear_ipato_list(card);
}

2182
static int qeth_l3_set_online(struct ccwgroup_device *gdev)
F
Frank Blaschka 已提交
2183 2184
{
	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2185
	struct net_device *dev = card->dev;
F
Frank Blaschka 已提交
2186
	int rc = 0;
J
Julian Wiedmann 已提交
2187
	bool carrier_ok;
F
Frank Blaschka 已提交
2188

2189
	mutex_lock(&card->discipline_mutex);
2190
	mutex_lock(&card->conf_mutex);
2191
	QETH_CARD_TEXT(card, 2, "setonlin");
F
Frank Blaschka 已提交
2192

J
Julian Wiedmann 已提交
2193
	rc = qeth_core_hardsetup_card(card, &carrier_ok);
F
Frank Blaschka 已提交
2194
	if (rc) {
2195
		QETH_CARD_TEXT_(card, 2, "2err%04x", rc);
2196
		rc = -ENODEV;
F
Frank Blaschka 已提交
2197 2198 2199 2200 2201 2202 2203
		goto out_remove;
	}

	card->state = CARD_STATE_HARDSETUP;
	qeth_print_status_message(card);

	/* softsetup */
2204
	QETH_CARD_TEXT(card, 2, "softsetp");
F
Frank Blaschka 已提交
2205 2206 2207

	rc = qeth_l3_setadapter_parms(card);
	if (rc)
2208
		QETH_CARD_TEXT_(card, 2, "2err%04x", rc);
2209 2210
	if (!card->options.sniffer) {
		rc = qeth_l3_start_ipassists(card);
S
Stefan Raspl 已提交
2211
		if (rc) {
2212
			QETH_CARD_TEXT_(card, 2, "3err%d", rc);
S
Stefan Raspl 已提交
2213 2214
			goto out_remove;
		}
2215 2216
		rc = qeth_l3_setrouting_v4(card);
		if (rc)
2217
			QETH_CARD_TEXT_(card, 2, "4err%04x", rc);
2218 2219
		rc = qeth_l3_setrouting_v6(card);
		if (rc)
2220
			QETH_CARD_TEXT_(card, 2, "5err%04x", rc);
2221
	}
F
Frank Blaschka 已提交
2222 2223 2224

	rc = qeth_init_qdio_queues(card);
	if (rc) {
2225
		QETH_CARD_TEXT_(card, 2, "6err%d", rc);
2226
		rc = -ENODEV;
F
Frank Blaschka 已提交
2227 2228 2229 2230 2231
		goto out_remove;
	}
	card->state = CARD_STATE_SOFTSETUP;

	qeth_set_allowed_threads(card, 0xffffffff, 0);
2232
	qeth_l3_recover_ip(card);
2233

2234 2235 2236 2237 2238
	if (!qeth_netdev_is_registered(dev)) {
		rc = qeth_l3_setup_netdev(card, carrier_ok);
		if (rc)
			goto out_remove;
	} else {
2239
		rtnl_lock();
2240 2241 2242 2243 2244
		if (carrier_ok)
			netif_carrier_on(dev);
		else
			netif_carrier_off(dev);

2245
		netif_device_attach(dev);
2246 2247
		qeth_enable_hw_features(dev);

2248 2249
		if (card->info.open_when_online) {
			card->info.open_when_online = 0;
2250
			dev_open(dev, NULL);
2251
		}
2252
		rtnl_unlock();
F
Frank Blaschka 已提交
2253
	}
S
Stefan Raspl 已提交
2254
	qeth_trace_features(card);
F
Frank Blaschka 已提交
2255 2256
	/* let user_space know that device is online */
	kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
2257
	mutex_unlock(&card->conf_mutex);
2258
	mutex_unlock(&card->discipline_mutex);
F
Frank Blaschka 已提交
2259 2260
	return 0;
out_remove:
2261
	qeth_l3_stop_card(card);
2262 2263 2264
	qeth_stop_channel(&card->data);
	qeth_stop_channel(&card->write);
	qeth_stop_channel(&card->read);
2265
	qdio_free(CARD_DDEV(card));
2266

2267
	mutex_unlock(&card->conf_mutex);
2268
	mutex_unlock(&card->discipline_mutex);
2269
	return rc;
F
Frank Blaschka 已提交
2270 2271 2272 2273 2274 2275 2276 2277
}

static int __qeth_l3_set_offline(struct ccwgroup_device *cgdev,
			int recovery_mode)
{
	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
	int rc = 0, rc2 = 0, rc3 = 0;

2278
	mutex_lock(&card->discipline_mutex);
2279
	mutex_lock(&card->conf_mutex);
2280
	QETH_CARD_TEXT(card, 3, "setoffl");
F
Frank Blaschka 已提交
2281

2282 2283 2284 2285 2286
	if ((!recovery_mode && card->info.hwtrap) || card->info.hwtrap == 2) {
		qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
		card->info.hwtrap = 1;
	}

2287
	rtnl_lock();
2288
	card->info.open_when_online = card->dev->flags & IFF_UP;
2289
	dev_close(card->dev);
2290
	netif_device_detach(card->dev);
2291
	netif_carrier_off(card->dev);
2292 2293
	rtnl_unlock();

2294
	qeth_l3_stop_card(card);
2295
	if (card->options.cq == QETH_CQ_ENABLED) {
2296 2297 2298 2299
		rtnl_lock();
		call_netdevice_notifiers(NETDEV_REBOOT, card->dev);
		rtnl_unlock();
	}
2300 2301 2302 2303

	rc  = qeth_stop_channel(&card->data);
	rc2 = qeth_stop_channel(&card->write);
	rc3 = qeth_stop_channel(&card->read);
F
Frank Blaschka 已提交
2304 2305 2306
	if (!rc)
		rc = (rc2) ? rc2 : rc3;
	if (rc)
2307
		QETH_CARD_TEXT_(card, 2, "1err%d", rc);
2308
	qdio_free(CARD_DDEV(card));
2309

F
Frank Blaschka 已提交
2310 2311
	/* let user_space know that device is offline */
	kobject_uevent(&cgdev->dev.kobj, KOBJ_CHANGE);
2312
	mutex_unlock(&card->conf_mutex);
2313
	mutex_unlock(&card->discipline_mutex);
F
Frank Blaschka 已提交
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
	return 0;
}

static int qeth_l3_set_offline(struct ccwgroup_device *cgdev)
{
	return __qeth_l3_set_offline(cgdev, 0);
}

static int qeth_l3_recover(void *ptr)
{
	struct qeth_card *card;
	int rc = 0;

	card = (struct qeth_card *) ptr;
C
Carsten Otte 已提交
2328 2329
	QETH_CARD_TEXT(card, 2, "recover1");
	QETH_CARD_HEX(card, 2, &card, sizeof(void *));
F
Frank Blaschka 已提交
2330 2331
	if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
		return 0;
C
Carsten Otte 已提交
2332
	QETH_CARD_TEXT(card, 2, "recover2");
2333 2334
	dev_warn(&card->gdev->dev,
		"A recovery process has been started for the device\n");
F
Frank Blaschka 已提交
2335
	__qeth_l3_set_offline(card->gdev, 1);
2336
	rc = qeth_l3_set_online(card->gdev);
F
Frank Blaschka 已提交
2337
	if (!rc)
2338 2339
		dev_info(&card->gdev->dev,
			"Device successfully recovered!\n");
2340
	else {
2341
		ccwgroup_set_offline(card->gdev);
S
Stefan Raspl 已提交
2342
		dev_warn(&card->gdev->dev, "The qeth device driver "
2343
				"failed to recover an error on the device\n");
2344
	}
2345 2346
	qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
	qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
F
Frank Blaschka 已提交
2347 2348 2349
	return 0;
}

2350 2351 2352 2353 2354 2355 2356
/* Returns zero if the command is successfully "consumed" */
static int qeth_l3_control_event(struct qeth_card *card,
					struct qeth_ipa_cmd *cmd)
{
	return 1;
}

2357
struct qeth_discipline qeth_l3_discipline = {
2358
	.devtype = &qeth_l3_devtype,
2359
	.process_rx_buffer = qeth_l3_process_inbound_buffer,
2360 2361
	.recover = qeth_l3_recover,
	.setup = qeth_l3_probe_device,
F
Frank Blaschka 已提交
2362 2363 2364
	.remove = qeth_l3_remove_device,
	.set_online = qeth_l3_set_online,
	.set_offline = qeth_l3_set_offline,
2365
	.do_ioctl = qeth_l3_do_ioctl,
2366
	.control_event_handler = qeth_l3_control_event,
F
Frank Blaschka 已提交
2367
};
2368
EXPORT_SYMBOL_GPL(qeth_l3_discipline);
F
Frank Blaschka 已提交
2369

2370 2371 2372 2373 2374 2375
static int qeth_l3_handle_ip_event(struct qeth_card *card,
				   struct qeth_ipaddr *addr,
				   unsigned long event)
{
	switch (event) {
	case NETDEV_UP:
2376
		qeth_l3_modify_ip(card, addr, true);
2377 2378
		return NOTIFY_OK;
	case NETDEV_DOWN:
2379
		qeth_l3_modify_ip(card, addr, false);
2380 2381 2382 2383 2384 2385
		return NOTIFY_OK;
	default:
		return NOTIFY_DONE;
	}
}

2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409
struct qeth_l3_ip_event_work {
	struct work_struct work;
	struct qeth_card *card;
	struct qeth_ipaddr addr;
};

#define to_ip_work(w) container_of((w), struct qeth_l3_ip_event_work, work)

static void qeth_l3_add_ip_worker(struct work_struct *work)
{
	struct qeth_l3_ip_event_work *ip_work = to_ip_work(work);

	qeth_l3_modify_ip(ip_work->card, &ip_work->addr, true);
	kfree(work);
}

static void qeth_l3_delete_ip_worker(struct work_struct *work)
{
	struct qeth_l3_ip_event_work *ip_work = to_ip_work(work);

	qeth_l3_modify_ip(ip_work->card, &ip_work->addr, false);
	kfree(work);
}

2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
static struct qeth_card *qeth_l3_get_card_from_dev(struct net_device *dev)
{
	if (is_vlan_dev(dev))
		dev = vlan_dev_real_dev(dev);
	if (dev->netdev_ops == &qeth_l3_osa_netdev_ops ||
	    dev->netdev_ops == &qeth_l3_netdev_ops)
		return (struct qeth_card *) dev->ml_priv;
	return NULL;
}

F
Frank Blaschka 已提交
2420
static int qeth_l3_ip_event(struct notifier_block *this,
2421
			    unsigned long event, void *ptr)
F
Frank Blaschka 已提交
2422
{
2423

F
Frank Blaschka 已提交
2424
	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2425
	struct net_device *dev = ifa->ifa_dev->dev;
2426
	struct qeth_ipaddr addr;
F
Frank Blaschka 已提交
2427 2428
	struct qeth_card *card;

2429
	if (dev_net(dev) != &init_net)
2430 2431
		return NOTIFY_DONE;

F
Frank Blaschka 已提交
2432 2433 2434
	card = qeth_l3_get_card_from_dev(dev);
	if (!card)
		return NOTIFY_DONE;
2435
	QETH_CARD_TEXT(card, 3, "ipevent");
F
Frank Blaschka 已提交
2436

2437
	qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4);
2438
	addr.u.a4.addr = ifa->ifa_address;
2439
	addr.u.a4.mask = be32_to_cpu(ifa->ifa_mask);
2440

2441
	return qeth_l3_handle_ip_event(card, &addr, event);
F
Frank Blaschka 已提交
2442 2443 2444 2445 2446 2447 2448 2449
}

static struct notifier_block qeth_l3_ip_notifier = {
	qeth_l3_ip_event,
	NULL,
};

static int qeth_l3_ip6_event(struct notifier_block *this,
2450
			     unsigned long event, void *ptr)
F
Frank Blaschka 已提交
2451 2452
{
	struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
2453
	struct net_device *dev = ifa->idev->dev;
2454
	struct qeth_l3_ip_event_work *ip_work;
F
Frank Blaschka 已提交
2455 2456
	struct qeth_card *card;

2457 2458 2459
	if (event != NETDEV_UP && event != NETDEV_DOWN)
		return NOTIFY_DONE;

F
Frank Blaschka 已提交
2460 2461 2462
	card = qeth_l3_get_card_from_dev(dev);
	if (!card)
		return NOTIFY_DONE;
C
Carsten Otte 已提交
2463
	QETH_CARD_TEXT(card, 3, "ip6event");
F
Frank Blaschka 已提交
2464 2465 2466
	if (!qeth_is_supported(card, IPA_IPV6))
		return NOTIFY_DONE;

2467 2468 2469
	ip_work = kmalloc(sizeof(*ip_work), GFP_ATOMIC);
	if (!ip_work)
		return NOTIFY_DONE;
2470

2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
	if (event == NETDEV_UP)
		INIT_WORK(&ip_work->work, qeth_l3_add_ip_worker);
	else
		INIT_WORK(&ip_work->work, qeth_l3_delete_ip_worker);

	ip_work->card = card;
	qeth_l3_init_ipaddr(&ip_work->addr, QETH_IP_TYPE_NORMAL,
			    QETH_PROT_IPV6);
	ip_work->addr.u.a6.addr = ifa->addr;
	ip_work->addr.u.a6.pfxlen = ifa->prefix_len;

	queue_work(card->cmd_wq, &ip_work->work);
	return NOTIFY_OK;
F
Frank Blaschka 已提交
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494
}

static struct notifier_block qeth_l3_ip6_notifier = {
	qeth_l3_ip6_event,
	NULL,
};

static int qeth_l3_register_notifiers(void)
{
	int rc;

C
Carsten Otte 已提交
2495
	QETH_DBF_TEXT(SETUP, 5, "regnotif");
F
Frank Blaschka 已提交
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
	rc = register_inetaddr_notifier(&qeth_l3_ip_notifier);
	if (rc)
		return rc;
	rc = register_inet6addr_notifier(&qeth_l3_ip6_notifier);
	if (rc) {
		unregister_inetaddr_notifier(&qeth_l3_ip_notifier);
		return rc;
	}
	return 0;
}

static void qeth_l3_unregister_notifiers(void)
{
C
Carsten Otte 已提交
2509
	QETH_DBF_TEXT(SETUP, 5, "unregnot");
S
Stefan Raspl 已提交
2510 2511
	WARN_ON(unregister_inetaddr_notifier(&qeth_l3_ip_notifier));
	WARN_ON(unregister_inet6addr_notifier(&qeth_l3_ip6_notifier));
F
Frank Blaschka 已提交
2512 2513 2514 2515
}

static int __init qeth_l3_init(void)
{
2516
	pr_info("register layer 3 discipline\n");
J
Julian Wiedmann 已提交
2517
	return qeth_l3_register_notifiers();
F
Frank Blaschka 已提交
2518 2519 2520 2521 2522
}

static void __exit qeth_l3_exit(void)
{
	qeth_l3_unregister_notifiers();
2523
	pr_info("unregister layer 3 discipline\n");
F
Frank Blaschka 已提交
2524 2525 2526 2527 2528 2529 2530
}

module_init(qeth_l3_init);
module_exit(qeth_l3_exit);
MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
MODULE_DESCRIPTION("qeth layer 3 discipline");
MODULE_LICENSE("GPL");