xref: /openbmc/linux/drivers/s390/net/qeth_l3_main.c (revision f97769fd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Copyright IBM Corp. 2007, 2009
4  *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
5  *		 Frank Pavlic <fpavlic@de.ibm.com>,
6  *		 Thomas Spatzier <tspat@de.ibm.com>,
7  *		 Frank Blaschka <frank.blaschka@de.ibm.com>
8  */
9 
10 #define KMSG_COMPONENT "qeth"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/bitops.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/etherdevice.h>
20 #include <linux/ip.h>
21 #include <linux/in.h>
22 #include <linux/ipv6.h>
23 #include <linux/inetdevice.h>
24 #include <linux/igmp.h>
25 #include <linux/slab.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/skbuff.h>
29 
30 #include <net/ip.h>
31 #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>
37 
38 #include "qeth_l3.h"
39 
40 static int qeth_l3_register_addr_entry(struct qeth_card *,
41 		struct qeth_ipaddr *);
42 static int qeth_l3_deregister_addr_entry(struct qeth_card *,
43 		struct qeth_ipaddr *);
44 
45 int qeth_l3_ipaddr_to_string(enum qeth_prot_versions proto, const u8 *addr,
46 			     char *buf)
47 {
48 	if (proto == QETH_PROT_IPV4)
49 		return sprintf(buf, "%pI4", addr);
50 	else
51 		return sprintf(buf, "%pI6", addr);
52 }
53 
54 static struct qeth_ipaddr *qeth_l3_find_addr_by_ip(struct qeth_card *card,
55 						   struct qeth_ipaddr *query)
56 {
57 	u32 key = qeth_l3_ipaddr_hash(query);
58 	struct qeth_ipaddr *addr;
59 
60 	if (query->is_multicast) {
61 		hash_for_each_possible(card->rx_mode_addrs, addr, hnode, key)
62 			if (qeth_l3_addr_match_ip(addr, query))
63 				return addr;
64 	} else {
65 		hash_for_each_possible(card->ip_htable,  addr, hnode, key)
66 			if (qeth_l3_addr_match_ip(addr, query))
67 				return addr;
68 	}
69 	return NULL;
70 }
71 
72 static void qeth_l3_convert_addr_to_bits(u8 *addr, u8 *bits, int len)
73 {
74 	int i, j;
75 	u8 octet;
76 
77 	for (i = 0; i < len; ++i) {
78 		octet = addr[i];
79 		for (j = 7; j >= 0; --j) {
80 			bits[i*8 + j] = octet & 1;
81 			octet >>= 1;
82 		}
83 	}
84 }
85 
86 static bool qeth_l3_is_addr_covered_by_ipato(struct qeth_card *card,
87 					     struct qeth_ipaddr *addr)
88 {
89 	struct qeth_ipato_entry *ipatoe;
90 	u8 addr_bits[128] = {0, };
91 	u8 ipatoe_bits[128] = {0, };
92 	int rc = 0;
93 
94 	if (!card->ipato.enabled)
95 		return false;
96 	if (addr->type != QETH_IP_TYPE_NORMAL)
97 		return false;
98 
99 	qeth_l3_convert_addr_to_bits((u8 *) &addr->u, addr_bits,
100 				  (addr->proto == QETH_PROT_IPV4)? 4:16);
101 	list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
102 		if (addr->proto != ipatoe->proto)
103 			continue;
104 		qeth_l3_convert_addr_to_bits(ipatoe->addr, ipatoe_bits,
105 					  (ipatoe->proto == QETH_PROT_IPV4) ?
106 					  4 : 16);
107 		if (addr->proto == QETH_PROT_IPV4)
108 			rc = !memcmp(addr_bits, ipatoe_bits,
109 				     min(32, ipatoe->mask_bits));
110 		else
111 			rc = !memcmp(addr_bits, ipatoe_bits,
112 				     min(128, ipatoe->mask_bits));
113 		if (rc)
114 			break;
115 	}
116 	/* invert? */
117 	if ((addr->proto == QETH_PROT_IPV4) && card->ipato.invert4)
118 		rc = !rc;
119 	else if ((addr->proto == QETH_PROT_IPV6) && card->ipato.invert6)
120 		rc = !rc;
121 
122 	return rc;
123 }
124 
125 static int qeth_l3_delete_ip(struct qeth_card *card,
126 			     struct qeth_ipaddr *tmp_addr)
127 {
128 	int rc = 0;
129 	struct qeth_ipaddr *addr;
130 
131 	if (tmp_addr->type == QETH_IP_TYPE_RXIP)
132 		QETH_CARD_TEXT(card, 2, "delrxip");
133 	else if (tmp_addr->type == QETH_IP_TYPE_VIPA)
134 		QETH_CARD_TEXT(card, 2, "delvipa");
135 	else
136 		QETH_CARD_TEXT(card, 2, "delip");
137 
138 	if (tmp_addr->proto == QETH_PROT_IPV4)
139 		QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
140 	else {
141 		QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
142 		QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
143 	}
144 
145 	addr = qeth_l3_find_addr_by_ip(card, tmp_addr);
146 	if (!addr || !qeth_l3_addr_match_all(addr, tmp_addr))
147 		return -ENOENT;
148 
149 	addr->ref_counter--;
150 	if (addr->type == QETH_IP_TYPE_NORMAL && addr->ref_counter > 0)
151 		return rc;
152 
153 	if (qeth_card_hw_is_reachable(card))
154 		rc = qeth_l3_deregister_addr_entry(card, addr);
155 
156 	hash_del(&addr->hnode);
157 	kfree(addr);
158 
159 	return rc;
160 }
161 
162 static int qeth_l3_add_ip(struct qeth_card *card, struct qeth_ipaddr *tmp_addr)
163 {
164 	int rc = 0;
165 	struct qeth_ipaddr *addr;
166 	char buf[40];
167 
168 	if (tmp_addr->type == QETH_IP_TYPE_RXIP)
169 		QETH_CARD_TEXT(card, 2, "addrxip");
170 	else if (tmp_addr->type == QETH_IP_TYPE_VIPA)
171 		QETH_CARD_TEXT(card, 2, "addvipa");
172 	else
173 		QETH_CARD_TEXT(card, 2, "addip");
174 
175 	if (tmp_addr->proto == QETH_PROT_IPV4)
176 		QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
177 	else {
178 		QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
179 		QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
180 	}
181 
182 	addr = qeth_l3_find_addr_by_ip(card, tmp_addr);
183 	if (addr) {
184 		if (tmp_addr->type != QETH_IP_TYPE_NORMAL)
185 			return -EADDRINUSE;
186 		if (qeth_l3_addr_match_all(addr, tmp_addr)) {
187 			addr->ref_counter++;
188 			return 0;
189 		}
190 		qeth_l3_ipaddr_to_string(tmp_addr->proto, (u8 *)&tmp_addr->u,
191 					 buf);
192 		dev_warn(&card->gdev->dev,
193 			 "Registering IP address %s failed\n", buf);
194 		return -EADDRINUSE;
195 	} else {
196 		addr = kmemdup(tmp_addr, sizeof(*tmp_addr), GFP_KERNEL);
197 		if (!addr)
198 			return -ENOMEM;
199 
200 		if (qeth_l3_is_addr_covered_by_ipato(card, addr)) {
201 			QETH_CARD_TEXT(card, 2, "tkovaddr");
202 			addr->ipato = 1;
203 		}
204 		hash_add(card->ip_htable, &addr->hnode,
205 				qeth_l3_ipaddr_hash(addr));
206 
207 		if (!qeth_card_hw_is_reachable(card)) {
208 			addr->disp_flag = QETH_DISP_ADDR_ADD;
209 			return 0;
210 		}
211 
212 		rc = qeth_l3_register_addr_entry(card, addr);
213 
214 		if (!rc || rc == -EADDRINUSE || rc == -ENETDOWN) {
215 			addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
216 		} else {
217 			hash_del(&addr->hnode);
218 			kfree(addr);
219 		}
220 	}
221 	return rc;
222 }
223 
224 static int qeth_l3_modify_ip(struct qeth_card *card, struct qeth_ipaddr *addr,
225 			     bool add)
226 {
227 	int rc;
228 
229 	mutex_lock(&card->ip_lock);
230 	rc = add ? qeth_l3_add_ip(card, addr) : qeth_l3_delete_ip(card, addr);
231 	mutex_unlock(&card->ip_lock);
232 
233 	return rc;
234 }
235 
236 static void qeth_l3_drain_rx_mode_cache(struct qeth_card *card)
237 {
238 	struct qeth_ipaddr *addr;
239 	struct hlist_node *tmp;
240 	int i;
241 
242 	hash_for_each_safe(card->rx_mode_addrs, i, tmp, addr, hnode) {
243 		hash_del(&addr->hnode);
244 		kfree(addr);
245 	}
246 }
247 
248 static void qeth_l3_clear_ip_htable(struct qeth_card *card, int recover)
249 {
250 	struct qeth_ipaddr *addr;
251 	struct hlist_node *tmp;
252 	int i;
253 
254 	QETH_CARD_TEXT(card, 4, "clearip");
255 
256 	mutex_lock(&card->ip_lock);
257 
258 	hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
259 		if (!recover) {
260 			hash_del(&addr->hnode);
261 			kfree(addr);
262 			continue;
263 		}
264 		addr->disp_flag = QETH_DISP_ADDR_ADD;
265 	}
266 
267 	mutex_unlock(&card->ip_lock);
268 }
269 
270 static void qeth_l3_recover_ip(struct qeth_card *card)
271 {
272 	struct qeth_ipaddr *addr;
273 	struct hlist_node *tmp;
274 	int i;
275 	int rc;
276 
277 	QETH_CARD_TEXT(card, 4, "recovrip");
278 
279 	mutex_lock(&card->ip_lock);
280 
281 	hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
282 		if (addr->disp_flag == QETH_DISP_ADDR_ADD) {
283 			rc = qeth_l3_register_addr_entry(card, addr);
284 
285 			if (!rc) {
286 				addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
287 			} else {
288 				hash_del(&addr->hnode);
289 				kfree(addr);
290 			}
291 		}
292 	}
293 
294 	mutex_unlock(&card->ip_lock);
295 }
296 
297 static int qeth_l3_setdelip_cb(struct qeth_card *card, struct qeth_reply *reply,
298 			       unsigned long data)
299 {
300 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
301 
302 	switch (cmd->hdr.return_code) {
303 	case IPA_RC_SUCCESS:
304 		return 0;
305 	case IPA_RC_DUPLICATE_IP_ADDRESS:
306 		return -EADDRINUSE;
307 	case IPA_RC_MC_ADDR_NOT_FOUND:
308 		return -ENOENT;
309 	case IPA_RC_LAN_OFFLINE:
310 		return -ENETDOWN;
311 	default:
312 		return -EIO;
313 	}
314 }
315 
316 static int qeth_l3_send_setdelmc(struct qeth_card *card,
317 			struct qeth_ipaddr *addr, int ipacmd)
318 {
319 	struct qeth_cmd_buffer *iob;
320 	struct qeth_ipa_cmd *cmd;
321 
322 	QETH_CARD_TEXT(card, 4, "setdelmc");
323 
324 	iob = qeth_ipa_alloc_cmd(card, ipacmd, addr->proto,
325 				 IPA_DATA_SIZEOF(setdelipm));
326 	if (!iob)
327 		return -ENOMEM;
328 	cmd = __ipa_cmd(iob);
329 	if (addr->proto == QETH_PROT_IPV6) {
330 		cmd->data.setdelipm.ip = addr->u.a6.addr;
331 		ipv6_eth_mc_map(&addr->u.a6.addr, cmd->data.setdelipm.mac);
332 	} else {
333 		cmd->data.setdelipm.ip.s6_addr32[3] = addr->u.a4.addr;
334 		ip_eth_mc_map(addr->u.a4.addr, cmd->data.setdelipm.mac);
335 	}
336 
337 	return qeth_send_ipa_cmd(card, iob, qeth_l3_setdelip_cb, NULL);
338 }
339 
340 static void qeth_l3_set_ipv6_prefix(struct in6_addr *prefix, unsigned int len)
341 {
342 	unsigned int i = 0;
343 
344 	while (len && i < 4) {
345 		int mask_len = min_t(int, len, 32);
346 
347 		prefix->s6_addr32[i] = inet_make_mask(mask_len);
348 		len -= mask_len;
349 		i++;
350 	}
351 }
352 
353 static u32 qeth_l3_get_setdelip_flags(struct qeth_ipaddr *addr, bool set)
354 {
355 	switch (addr->type) {
356 	case QETH_IP_TYPE_RXIP:
357 		return (set) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0;
358 	case QETH_IP_TYPE_VIPA:
359 		return (set) ? QETH_IPA_SETIP_VIPA_FLAG :
360 			       QETH_IPA_DELIP_VIPA_FLAG;
361 	default:
362 		return (set && addr->ipato) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0;
363 	}
364 }
365 
366 static int qeth_l3_send_setdelip(struct qeth_card *card,
367 				 struct qeth_ipaddr *addr,
368 				 enum qeth_ipa_cmds ipacmd)
369 {
370 	struct qeth_cmd_buffer *iob;
371 	struct qeth_ipa_cmd *cmd;
372 	u32 flags;
373 
374 	QETH_CARD_TEXT(card, 4, "setdelip");
375 
376 	iob = qeth_ipa_alloc_cmd(card, ipacmd, addr->proto,
377 				 IPA_DATA_SIZEOF(setdelip6));
378 	if (!iob)
379 		return -ENOMEM;
380 	cmd = __ipa_cmd(iob);
381 
382 	flags = qeth_l3_get_setdelip_flags(addr, ipacmd == IPA_CMD_SETIP);
383 	QETH_CARD_TEXT_(card, 4, "flags%02X", flags);
384 
385 	if (addr->proto == QETH_PROT_IPV6) {
386 		cmd->data.setdelip6.addr = addr->u.a6.addr;
387 		qeth_l3_set_ipv6_prefix(&cmd->data.setdelip6.prefix,
388 					addr->u.a6.pfxlen);
389 		cmd->data.setdelip6.flags = flags;
390 	} else {
391 		cmd->data.setdelip4.addr = addr->u.a4.addr;
392 		cmd->data.setdelip4.mask = addr->u.a4.mask;
393 		cmd->data.setdelip4.flags = flags;
394 	}
395 
396 	return qeth_send_ipa_cmd(card, iob, qeth_l3_setdelip_cb, NULL);
397 }
398 
399 static int qeth_l3_send_setrouting(struct qeth_card *card,
400 	enum qeth_routing_types type, enum qeth_prot_versions prot)
401 {
402 	int rc;
403 	struct qeth_ipa_cmd *cmd;
404 	struct qeth_cmd_buffer *iob;
405 
406 	QETH_CARD_TEXT(card, 4, "setroutg");
407 	iob = qeth_ipa_alloc_cmd(card, IPA_CMD_SETRTG, prot,
408 				 IPA_DATA_SIZEOF(setrtg));
409 	if (!iob)
410 		return -ENOMEM;
411 	cmd = __ipa_cmd(iob);
412 	cmd->data.setrtg.type = (type);
413 	rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
414 
415 	return rc;
416 }
417 
418 static int qeth_l3_correct_routing_type(struct qeth_card *card,
419 		enum qeth_routing_types *type, enum qeth_prot_versions prot)
420 {
421 	if (IS_IQD(card)) {
422 		switch (*type) {
423 		case NO_ROUTER:
424 		case PRIMARY_CONNECTOR:
425 		case SECONDARY_CONNECTOR:
426 		case MULTICAST_ROUTER:
427 			return 0;
428 		default:
429 			goto out_inval;
430 		}
431 	} else {
432 		switch (*type) {
433 		case NO_ROUTER:
434 		case PRIMARY_ROUTER:
435 		case SECONDARY_ROUTER:
436 			return 0;
437 		case MULTICAST_ROUTER:
438 			if (qeth_is_ipafunc_supported(card, prot,
439 						      IPA_OSA_MC_ROUTER))
440 				return 0;
441 		default:
442 			goto out_inval;
443 		}
444 	}
445 out_inval:
446 	*type = NO_ROUTER;
447 	return -EINVAL;
448 }
449 
450 int qeth_l3_setrouting_v4(struct qeth_card *card)
451 {
452 	int rc;
453 
454 	QETH_CARD_TEXT(card, 3, "setrtg4");
455 
456 	rc = qeth_l3_correct_routing_type(card, &card->options.route4.type,
457 				  QETH_PROT_IPV4);
458 	if (rc)
459 		return rc;
460 
461 	rc = qeth_l3_send_setrouting(card, card->options.route4.type,
462 				  QETH_PROT_IPV4);
463 	if (rc) {
464 		card->options.route4.type = NO_ROUTER;
465 		QETH_DBF_MESSAGE(2, "Error (%#06x) while setting routing type on device %x. Type set to 'no router'.\n",
466 				 rc, CARD_DEVID(card));
467 	}
468 	return rc;
469 }
470 
471 int qeth_l3_setrouting_v6(struct qeth_card *card)
472 {
473 	int rc = 0;
474 
475 	QETH_CARD_TEXT(card, 3, "setrtg6");
476 
477 	if (!qeth_is_supported(card, IPA_IPV6))
478 		return 0;
479 	rc = qeth_l3_correct_routing_type(card, &card->options.route6.type,
480 				  QETH_PROT_IPV6);
481 	if (rc)
482 		return rc;
483 
484 	rc = qeth_l3_send_setrouting(card, card->options.route6.type,
485 				  QETH_PROT_IPV6);
486 	if (rc) {
487 		card->options.route6.type = NO_ROUTER;
488 		QETH_DBF_MESSAGE(2, "Error (%#06x) while setting routing type on device %x. Type set to 'no router'.\n",
489 				 rc, CARD_DEVID(card));
490 	}
491 	return rc;
492 }
493 
494 /*
495  * IP address takeover related functions
496  */
497 
498 /**
499  * qeth_l3_update_ipato() - Update 'takeover' property, for all NORMAL IPs.
500  *
501  * Caller must hold ip_lock.
502  */
503 void qeth_l3_update_ipato(struct qeth_card *card)
504 {
505 	struct qeth_ipaddr *addr;
506 	unsigned int i;
507 
508 	hash_for_each(card->ip_htable, i, addr, hnode) {
509 		if (addr->type != QETH_IP_TYPE_NORMAL)
510 			continue;
511 		addr->ipato = qeth_l3_is_addr_covered_by_ipato(card, addr);
512 	}
513 }
514 
515 static void qeth_l3_clear_ipato_list(struct qeth_card *card)
516 {
517 	struct qeth_ipato_entry *ipatoe, *tmp;
518 
519 	mutex_lock(&card->ip_lock);
520 
521 	list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
522 		list_del(&ipatoe->entry);
523 		kfree(ipatoe);
524 	}
525 
526 	qeth_l3_update_ipato(card);
527 	mutex_unlock(&card->ip_lock);
528 }
529 
530 int qeth_l3_add_ipato_entry(struct qeth_card *card,
531 				struct qeth_ipato_entry *new)
532 {
533 	struct qeth_ipato_entry *ipatoe;
534 	int rc = 0;
535 
536 	QETH_CARD_TEXT(card, 2, "addipato");
537 
538 	mutex_lock(&card->conf_mutex);
539 	mutex_lock(&card->ip_lock);
540 
541 	list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
542 		if (ipatoe->proto != new->proto)
543 			continue;
544 		if (!memcmp(ipatoe->addr, new->addr,
545 			    (ipatoe->proto == QETH_PROT_IPV4)? 4:16) &&
546 		    (ipatoe->mask_bits == new->mask_bits)) {
547 			rc = -EEXIST;
548 			break;
549 		}
550 	}
551 
552 	if (!rc) {
553 		list_add_tail(&new->entry, &card->ipato.entries);
554 		qeth_l3_update_ipato(card);
555 	}
556 
557 	mutex_unlock(&card->ip_lock);
558 	mutex_unlock(&card->conf_mutex);
559 
560 	return rc;
561 }
562 
563 int qeth_l3_del_ipato_entry(struct qeth_card *card,
564 			    enum qeth_prot_versions proto, u8 *addr,
565 			    int mask_bits)
566 {
567 	struct qeth_ipato_entry *ipatoe, *tmp;
568 	int rc = -ENOENT;
569 
570 	QETH_CARD_TEXT(card, 2, "delipato");
571 
572 	mutex_lock(&card->conf_mutex);
573 	mutex_lock(&card->ip_lock);
574 
575 	list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
576 		if (ipatoe->proto != proto)
577 			continue;
578 		if (!memcmp(ipatoe->addr, addr,
579 			    (proto == QETH_PROT_IPV4)? 4:16) &&
580 		    (ipatoe->mask_bits == mask_bits)) {
581 			list_del(&ipatoe->entry);
582 			qeth_l3_update_ipato(card);
583 			kfree(ipatoe);
584 			rc = 0;
585 		}
586 	}
587 
588 	mutex_unlock(&card->ip_lock);
589 	mutex_unlock(&card->conf_mutex);
590 
591 	return rc;
592 }
593 
594 int qeth_l3_modify_rxip_vipa(struct qeth_card *card, bool add, const u8 *ip,
595 			     enum qeth_ip_types type,
596 			     enum qeth_prot_versions proto)
597 {
598 	struct qeth_ipaddr addr;
599 	int rc;
600 
601 	qeth_l3_init_ipaddr(&addr, type, proto);
602 	if (proto == QETH_PROT_IPV4)
603 		memcpy(&addr.u.a4.addr, ip, 4);
604 	else
605 		memcpy(&addr.u.a6.addr, ip, 16);
606 
607 	mutex_lock(&card->conf_mutex);
608 	rc = qeth_l3_modify_ip(card, &addr, add);
609 	mutex_unlock(&card->conf_mutex);
610 
611 	return rc;
612 }
613 
614 int qeth_l3_modify_hsuid(struct qeth_card *card, bool add)
615 {
616 	struct qeth_ipaddr addr;
617 	unsigned int i;
618 
619 	qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6);
620 	addr.u.a6.addr.s6_addr[0] = 0xfe;
621 	addr.u.a6.addr.s6_addr[1] = 0x80;
622 	for (i = 0; i < 8; i++)
623 		addr.u.a6.addr.s6_addr[8+i] = card->options.hsuid[i];
624 
625 	return qeth_l3_modify_ip(card, &addr, add);
626 }
627 
628 static int qeth_l3_register_addr_entry(struct qeth_card *card,
629 				struct qeth_ipaddr *addr)
630 {
631 	char buf[50];
632 	int rc = 0;
633 	int cnt = 3;
634 
635 	if (card->options.sniffer)
636 		return 0;
637 
638 	if (addr->proto == QETH_PROT_IPV4) {
639 		QETH_CARD_TEXT(card, 2, "setaddr4");
640 		QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
641 	} else if (addr->proto == QETH_PROT_IPV6) {
642 		QETH_CARD_TEXT(card, 2, "setaddr6");
643 		QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
644 		QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
645 	} else {
646 		QETH_CARD_TEXT(card, 2, "setaddr?");
647 		QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
648 	}
649 	do {
650 		if (addr->is_multicast)
651 			rc =  qeth_l3_send_setdelmc(card, addr, IPA_CMD_SETIPM);
652 		else
653 			rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_SETIP);
654 		if (rc)
655 			QETH_CARD_TEXT(card, 2, "failed");
656 	} while ((--cnt > 0) && rc);
657 	if (rc) {
658 		QETH_CARD_TEXT(card, 2, "FAILED");
659 		qeth_l3_ipaddr_to_string(addr->proto, (u8 *)&addr->u, buf);
660 		dev_warn(&card->gdev->dev,
661 			"Registering IP address %s failed\n", buf);
662 	}
663 	return rc;
664 }
665 
666 static int qeth_l3_deregister_addr_entry(struct qeth_card *card,
667 						struct qeth_ipaddr *addr)
668 {
669 	int rc = 0;
670 
671 	if (card->options.sniffer)
672 		return 0;
673 
674 	if (addr->proto == QETH_PROT_IPV4) {
675 		QETH_CARD_TEXT(card, 2, "deladdr4");
676 		QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
677 	} else if (addr->proto == QETH_PROT_IPV6) {
678 		QETH_CARD_TEXT(card, 2, "deladdr6");
679 		QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
680 		QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
681 	} else {
682 		QETH_CARD_TEXT(card, 2, "deladdr?");
683 		QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
684 	}
685 	if (addr->is_multicast)
686 		rc = qeth_l3_send_setdelmc(card, addr, IPA_CMD_DELIPM);
687 	else
688 		rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_DELIP);
689 	if (rc)
690 		QETH_CARD_TEXT(card, 2, "failed");
691 
692 	return rc;
693 }
694 
695 static int qeth_l3_setadapter_parms(struct qeth_card *card)
696 {
697 	int rc = 0;
698 
699 	QETH_CARD_TEXT(card, 2, "setadprm");
700 
701 	if (qeth_adp_supported(card, IPA_SETADP_ALTER_MAC_ADDRESS)) {
702 		rc = qeth_setadpparms_change_macaddr(card);
703 		if (rc)
704 			dev_warn(&card->gdev->dev, "Reading the adapter MAC"
705 				" address failed\n");
706 	}
707 
708 	return rc;
709 }
710 
711 static int qeth_l3_start_ipa_arp_processing(struct qeth_card *card)
712 {
713 	int rc;
714 
715 	QETH_CARD_TEXT(card, 3, "ipaarp");
716 
717 	if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
718 		dev_info(&card->gdev->dev,
719 			"ARP processing not supported on %s!\n",
720 			QETH_CARD_IFNAME(card));
721 		return 0;
722 	}
723 	rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
724 					  IPA_CMD_ASS_START, NULL);
725 	if (rc) {
726 		dev_warn(&card->gdev->dev,
727 			"Starting ARP processing support for %s failed\n",
728 			QETH_CARD_IFNAME(card));
729 	}
730 	return rc;
731 }
732 
733 static int qeth_l3_start_ipa_source_mac(struct qeth_card *card)
734 {
735 	int rc;
736 
737 	QETH_CARD_TEXT(card, 3, "stsrcmac");
738 
739 	if (!qeth_is_supported(card, IPA_SOURCE_MAC)) {
740 		dev_info(&card->gdev->dev,
741 			"Inbound source MAC-address not supported on %s\n",
742 			QETH_CARD_IFNAME(card));
743 		return -EOPNOTSUPP;
744 	}
745 
746 	rc = qeth_send_simple_setassparms(card, IPA_SOURCE_MAC,
747 					  IPA_CMD_ASS_START, NULL);
748 	if (rc)
749 		dev_warn(&card->gdev->dev,
750 			"Starting source MAC-address support for %s failed\n",
751 			QETH_CARD_IFNAME(card));
752 	return rc;
753 }
754 
755 static int qeth_l3_start_ipa_vlan(struct qeth_card *card)
756 {
757 	int rc = 0;
758 
759 	QETH_CARD_TEXT(card, 3, "strtvlan");
760 
761 	if (!qeth_is_supported(card, IPA_FULL_VLAN)) {
762 		dev_info(&card->gdev->dev,
763 			"VLAN not supported on %s\n", QETH_CARD_IFNAME(card));
764 		return -EOPNOTSUPP;
765 	}
766 
767 	rc = qeth_send_simple_setassparms(card, IPA_VLAN_PRIO,
768 					  IPA_CMD_ASS_START, NULL);
769 	if (rc) {
770 		dev_warn(&card->gdev->dev,
771 			"Starting VLAN support for %s failed\n",
772 			QETH_CARD_IFNAME(card));
773 	} else {
774 		dev_info(&card->gdev->dev, "VLAN enabled\n");
775 	}
776 	return rc;
777 }
778 
779 static int qeth_l3_start_ipa_multicast(struct qeth_card *card)
780 {
781 	int rc;
782 
783 	QETH_CARD_TEXT(card, 3, "stmcast");
784 
785 	if (!qeth_is_supported(card, IPA_MULTICASTING)) {
786 		dev_info(&card->gdev->dev,
787 			"Multicast not supported on %s\n",
788 			QETH_CARD_IFNAME(card));
789 		return -EOPNOTSUPP;
790 	}
791 
792 	rc = qeth_send_simple_setassparms(card, IPA_MULTICASTING,
793 					  IPA_CMD_ASS_START, NULL);
794 	if (rc) {
795 		dev_warn(&card->gdev->dev,
796 			"Starting multicast support for %s failed\n",
797 			QETH_CARD_IFNAME(card));
798 	} else {
799 		dev_info(&card->gdev->dev, "Multicast enabled\n");
800 		card->dev->flags |= IFF_MULTICAST;
801 	}
802 	return rc;
803 }
804 
805 static int qeth_l3_softsetup_ipv6(struct qeth_card *card)
806 {
807 	u32 ipv6_data = 3;
808 	int rc;
809 
810 	QETH_CARD_TEXT(card, 3, "softipv6");
811 
812 	if (IS_IQD(card))
813 		goto out;
814 
815 	rc = qeth_send_simple_setassparms(card, IPA_IPV6, IPA_CMD_ASS_START,
816 					  &ipv6_data);
817 	if (rc) {
818 		dev_err(&card->gdev->dev,
819 			"Activating IPv6 support for %s failed\n",
820 			QETH_CARD_IFNAME(card));
821 		return rc;
822 	}
823 	rc = qeth_send_simple_setassparms_v6(card, IPA_IPV6, IPA_CMD_ASS_START,
824 					     NULL);
825 	if (rc) {
826 		dev_err(&card->gdev->dev,
827 			"Activating IPv6 support for %s failed\n",
828 			 QETH_CARD_IFNAME(card));
829 		return rc;
830 	}
831 	rc = qeth_send_simple_setassparms_v6(card, IPA_PASSTHRU,
832 					     IPA_CMD_ASS_START, NULL);
833 	if (rc) {
834 		dev_warn(&card->gdev->dev,
835 			"Enabling the passthrough mode for %s failed\n",
836 			QETH_CARD_IFNAME(card));
837 		return rc;
838 	}
839 out:
840 	dev_info(&card->gdev->dev, "IPV6 enabled\n");
841 	return 0;
842 }
843 
844 static int qeth_l3_start_ipa_ipv6(struct qeth_card *card)
845 {
846 	QETH_CARD_TEXT(card, 3, "strtipv6");
847 
848 	if (!qeth_is_supported(card, IPA_IPV6)) {
849 		dev_info(&card->gdev->dev,
850 			"IPv6 not supported on %s\n", QETH_CARD_IFNAME(card));
851 		return 0;
852 	}
853 	return qeth_l3_softsetup_ipv6(card);
854 }
855 
856 static int qeth_l3_start_ipa_broadcast(struct qeth_card *card)
857 {
858 	u32 filter_data = 1;
859 	int rc;
860 
861 	QETH_CARD_TEXT(card, 3, "stbrdcst");
862 	card->info.broadcast_capable = 0;
863 	if (!qeth_is_supported(card, IPA_FILTERING)) {
864 		dev_info(&card->gdev->dev,
865 			"Broadcast not supported on %s\n",
866 			QETH_CARD_IFNAME(card));
867 		rc = -EOPNOTSUPP;
868 		goto out;
869 	}
870 	rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
871 					  IPA_CMD_ASS_START, NULL);
872 	if (rc) {
873 		dev_warn(&card->gdev->dev, "Enabling broadcast filtering for "
874 			"%s failed\n", QETH_CARD_IFNAME(card));
875 		goto out;
876 	}
877 
878 	rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
879 					  IPA_CMD_ASS_CONFIGURE, &filter_data);
880 	if (rc) {
881 		dev_warn(&card->gdev->dev,
882 			"Setting up broadcast filtering for %s failed\n",
883 			QETH_CARD_IFNAME(card));
884 		goto out;
885 	}
886 	card->info.broadcast_capable = QETH_BROADCAST_WITH_ECHO;
887 	dev_info(&card->gdev->dev, "Broadcast enabled\n");
888 	rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
889 					  IPA_CMD_ASS_ENABLE, &filter_data);
890 	if (rc) {
891 		dev_warn(&card->gdev->dev, "Setting up broadcast echo "
892 			"filtering for %s failed\n", QETH_CARD_IFNAME(card));
893 		goto out;
894 	}
895 	card->info.broadcast_capable = QETH_BROADCAST_WITHOUT_ECHO;
896 out:
897 	if (card->info.broadcast_capable)
898 		card->dev->flags |= IFF_BROADCAST;
899 	else
900 		card->dev->flags &= ~IFF_BROADCAST;
901 	return rc;
902 }
903 
904 static void qeth_l3_start_ipassists(struct qeth_card *card)
905 {
906 	QETH_CARD_TEXT(card, 3, "strtipas");
907 
908 	qeth_l3_start_ipa_arp_processing(card);	/* go on*/
909 	qeth_l3_start_ipa_source_mac(card);	/* go on*/
910 	qeth_l3_start_ipa_vlan(card);		/* go on*/
911 	qeth_l3_start_ipa_multicast(card);		/* go on*/
912 	qeth_l3_start_ipa_ipv6(card);		/* go on*/
913 	qeth_l3_start_ipa_broadcast(card);		/* go on*/
914 }
915 
916 static int qeth_l3_iqd_read_initial_mac_cb(struct qeth_card *card,
917 		struct qeth_reply *reply, unsigned long data)
918 {
919 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
920 
921 	if (cmd->hdr.return_code)
922 		return -EIO;
923 	if (!is_valid_ether_addr(cmd->data.create_destroy_addr.mac_addr))
924 		return -EADDRNOTAVAIL;
925 
926 	ether_addr_copy(card->dev->dev_addr,
927 			cmd->data.create_destroy_addr.mac_addr);
928 	return 0;
929 }
930 
931 static int qeth_l3_iqd_read_initial_mac(struct qeth_card *card)
932 {
933 	int rc = 0;
934 	struct qeth_cmd_buffer *iob;
935 
936 	QETH_CARD_TEXT(card, 2, "hsrmac");
937 
938 	iob = qeth_ipa_alloc_cmd(card, IPA_CMD_CREATE_ADDR, QETH_PROT_IPV6,
939 				 IPA_DATA_SIZEOF(create_destroy_addr));
940 	if (!iob)
941 		return -ENOMEM;
942 
943 	rc = qeth_send_ipa_cmd(card, iob, qeth_l3_iqd_read_initial_mac_cb,
944 				NULL);
945 	return rc;
946 }
947 
948 static int qeth_l3_get_unique_id_cb(struct qeth_card *card,
949 		struct qeth_reply *reply, unsigned long data)
950 {
951 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
952 	u16 *uid = reply->param;
953 
954 	if (cmd->hdr.return_code == 0) {
955 		*uid = cmd->data.create_destroy_addr.uid;
956 		return 0;
957 	}
958 
959 	dev_warn(&card->gdev->dev, "The network adapter failed to generate a unique ID\n");
960 	return -EIO;
961 }
962 
963 static u16 qeth_l3_get_unique_id(struct qeth_card *card, u16 uid)
964 {
965 	struct qeth_cmd_buffer *iob;
966 
967 	QETH_CARD_TEXT(card, 2, "guniqeid");
968 
969 	if (!qeth_is_supported(card, IPA_IPV6))
970 		goto out;
971 
972 	iob = qeth_ipa_alloc_cmd(card, IPA_CMD_CREATE_ADDR, QETH_PROT_IPV6,
973 				 IPA_DATA_SIZEOF(create_destroy_addr));
974 	if (!iob)
975 		goto out;
976 
977 	__ipa_cmd(iob)->data.create_destroy_addr.uid = uid;
978 	qeth_send_ipa_cmd(card, iob, qeth_l3_get_unique_id_cb, &uid);
979 
980 out:
981 	return uid;
982 }
983 
984 static int
985 qeth_diags_trace_cb(struct qeth_card *card, struct qeth_reply *reply,
986 			    unsigned long data)
987 {
988 	struct qeth_ipa_cmd	   *cmd;
989 	__u16 rc;
990 
991 	QETH_CARD_TEXT(card, 2, "diastrcb");
992 
993 	cmd = (struct qeth_ipa_cmd *)data;
994 	rc = cmd->hdr.return_code;
995 	if (rc)
996 		QETH_CARD_TEXT_(card, 2, "dxter%x", rc);
997 	switch (cmd->data.diagass.action) {
998 	case QETH_DIAGS_CMD_TRACE_QUERY:
999 		break;
1000 	case QETH_DIAGS_CMD_TRACE_DISABLE:
1001 		switch (rc) {
1002 		case 0:
1003 		case IPA_RC_INVALID_SUBCMD:
1004 			card->info.promisc_mode = SET_PROMISC_MODE_OFF;
1005 			dev_info(&card->gdev->dev, "The HiperSockets network "
1006 				"traffic analyzer is deactivated\n");
1007 			break;
1008 		default:
1009 			break;
1010 		}
1011 		break;
1012 	case QETH_DIAGS_CMD_TRACE_ENABLE:
1013 		switch (rc) {
1014 		case 0:
1015 			card->info.promisc_mode = SET_PROMISC_MODE_ON;
1016 			dev_info(&card->gdev->dev, "The HiperSockets network "
1017 				"traffic analyzer is activated\n");
1018 			break;
1019 		case IPA_RC_HARDWARE_AUTH_ERROR:
1020 			dev_warn(&card->gdev->dev, "The device is not "
1021 				"authorized to run as a HiperSockets network "
1022 				"traffic analyzer\n");
1023 			break;
1024 		case IPA_RC_TRACE_ALREADY_ACTIVE:
1025 			dev_warn(&card->gdev->dev, "A HiperSockets "
1026 				"network traffic analyzer is already "
1027 				"active in the HiperSockets LAN\n");
1028 			break;
1029 		default:
1030 			break;
1031 		}
1032 		break;
1033 	default:
1034 		QETH_DBF_MESSAGE(2, "Unknown sniffer action (%#06x) on device %x\n",
1035 				 cmd->data.diagass.action, CARD_DEVID(card));
1036 	}
1037 
1038 	return rc ? -EIO : 0;
1039 }
1040 
1041 static int
1042 qeth_diags_trace(struct qeth_card *card, enum qeth_diags_trace_cmds diags_cmd)
1043 {
1044 	struct qeth_cmd_buffer *iob;
1045 	struct qeth_ipa_cmd    *cmd;
1046 
1047 	QETH_CARD_TEXT(card, 2, "diagtrac");
1048 
1049 	iob = qeth_get_diag_cmd(card, QETH_DIAGS_CMD_TRACE, 0);
1050 	if (!iob)
1051 		return -ENOMEM;
1052 	cmd = __ipa_cmd(iob);
1053 	cmd->data.diagass.type = QETH_DIAGS_TYPE_HIPERSOCKET;
1054 	cmd->data.diagass.action = diags_cmd;
1055 	return qeth_send_ipa_cmd(card, iob, qeth_diags_trace_cb, NULL);
1056 }
1057 
1058 static int qeth_l3_add_mcast_rtnl(struct net_device *dev, int vid, void *arg)
1059 {
1060 	struct qeth_card *card = arg;
1061 	struct inet6_dev *in6_dev;
1062 	struct in_device *in4_dev;
1063 	struct qeth_ipaddr *ipm;
1064 	struct qeth_ipaddr tmp;
1065 	struct ip_mc_list *im4;
1066 	struct ifmcaddr6 *im6;
1067 
1068 	QETH_CARD_TEXT(card, 4, "addmc");
1069 
1070 	if (!dev || !(dev->flags & IFF_UP))
1071 		goto out;
1072 
1073 	in4_dev = __in_dev_get_rtnl(dev);
1074 	if (!in4_dev)
1075 		goto walk_ipv6;
1076 
1077 	qeth_l3_init_ipaddr(&tmp, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4);
1078 	tmp.disp_flag = QETH_DISP_ADDR_ADD;
1079 	tmp.is_multicast = 1;
1080 
1081 	for (im4 = rtnl_dereference(in4_dev->mc_list); im4 != NULL;
1082 	     im4 = rtnl_dereference(im4->next_rcu)) {
1083 		tmp.u.a4.addr = im4->multiaddr;
1084 
1085 		ipm = qeth_l3_find_addr_by_ip(card, &tmp);
1086 		if (ipm) {
1087 			/* for mcast, by-IP match means full match */
1088 			ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
1089 			continue;
1090 		}
1091 
1092 		ipm = kmemdup(&tmp, sizeof(tmp), GFP_KERNEL);
1093 		if (!ipm)
1094 			continue;
1095 
1096 		hash_add(card->rx_mode_addrs, &ipm->hnode,
1097 			 qeth_l3_ipaddr_hash(ipm));
1098 	}
1099 
1100 walk_ipv6:
1101 	if (!qeth_is_supported(card, IPA_IPV6))
1102 		goto out;
1103 
1104 	in6_dev = __in6_dev_get(dev);
1105 	if (!in6_dev)
1106 		goto out;
1107 
1108 	qeth_l3_init_ipaddr(&tmp, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6);
1109 	tmp.disp_flag = QETH_DISP_ADDR_ADD;
1110 	tmp.is_multicast = 1;
1111 
1112 	read_lock_bh(&in6_dev->lock);
1113 	for (im6 = in6_dev->mc_list; im6 != NULL; im6 = im6->next) {
1114 		tmp.u.a6.addr = im6->mca_addr;
1115 
1116 		ipm = qeth_l3_find_addr_by_ip(card, &tmp);
1117 		if (ipm) {
1118 			/* for mcast, by-IP match means full match */
1119 			ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
1120 			continue;
1121 		}
1122 
1123 		ipm = kmemdup(&tmp, sizeof(tmp), GFP_ATOMIC);
1124 		if (!ipm)
1125 			continue;
1126 
1127 		hash_add(card->rx_mode_addrs, &ipm->hnode,
1128 			 qeth_l3_ipaddr_hash(ipm));
1129 
1130 	}
1131 	read_unlock_bh(&in6_dev->lock);
1132 
1133 out:
1134 	return 0;
1135 }
1136 
1137 static int qeth_l3_vlan_rx_add_vid(struct net_device *dev,
1138 				   __be16 proto, u16 vid)
1139 {
1140 	struct qeth_card *card = dev->ml_priv;
1141 
1142 	QETH_CARD_TEXT_(card, 4, "aid:%d", vid);
1143 	return 0;
1144 }
1145 
1146 static int qeth_l3_vlan_rx_kill_vid(struct net_device *dev,
1147 				    __be16 proto, u16 vid)
1148 {
1149 	struct qeth_card *card = dev->ml_priv;
1150 
1151 	QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
1152 	return 0;
1153 }
1154 
1155 static void qeth_l3_stop_card(struct qeth_card *card)
1156 {
1157 	QETH_CARD_TEXT(card, 2, "stopcard");
1158 
1159 	qeth_set_allowed_threads(card, 0, 1);
1160 
1161 	cancel_work_sync(&card->rx_mode_work);
1162 	qeth_l3_drain_rx_mode_cache(card);
1163 
1164 	if (card->options.sniffer &&
1165 	    (card->info.promisc_mode == SET_PROMISC_MODE_ON))
1166 		qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
1167 
1168 	if (card->state == CARD_STATE_SOFTSETUP) {
1169 		qeth_l3_clear_ip_htable(card, 1);
1170 		qeth_clear_ipacmd_list(card);
1171 		qeth_drain_output_queues(card);
1172 		card->state = CARD_STATE_DOWN;
1173 	}
1174 
1175 	qeth_qdio_clear_card(card, 0);
1176 	qeth_clear_working_pool_list(card);
1177 	flush_workqueue(card->event_wq);
1178 	qeth_flush_local_addrs(card);
1179 	card->info.promisc_mode = 0;
1180 }
1181 
1182 static void qeth_l3_set_promisc_mode(struct qeth_card *card)
1183 {
1184 	bool enable = card->dev->flags & IFF_PROMISC;
1185 
1186 	if (card->info.promisc_mode == enable)
1187 		return;
1188 
1189 	if (IS_VM_NIC(card)) {		/* Guestlan trace */
1190 		if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
1191 			qeth_setadp_promisc_mode(card, enable);
1192 	} else if (card->options.sniffer &&	/* HiperSockets trace */
1193 		   qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) {
1194 		if (enable) {
1195 			QETH_CARD_TEXT(card, 3, "+promisc");
1196 			qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_ENABLE);
1197 		} else {
1198 			QETH_CARD_TEXT(card, 3, "-promisc");
1199 			qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
1200 		}
1201 	}
1202 }
1203 
1204 static void qeth_l3_rx_mode_work(struct work_struct *work)
1205 {
1206 	struct qeth_card *card = container_of(work, struct qeth_card,
1207 					      rx_mode_work);
1208 	struct qeth_ipaddr *addr;
1209 	struct hlist_node *tmp;
1210 	int i, rc;
1211 
1212 	QETH_CARD_TEXT(card, 3, "setmulti");
1213 
1214 	if (!card->options.sniffer) {
1215 		rtnl_lock();
1216 		qeth_l3_add_mcast_rtnl(card->dev, 0, card);
1217 		if (qeth_is_supported(card, IPA_FULL_VLAN))
1218 			vlan_for_each(card->dev, qeth_l3_add_mcast_rtnl, card);
1219 		rtnl_unlock();
1220 
1221 		hash_for_each_safe(card->rx_mode_addrs, i, tmp, addr, hnode) {
1222 			switch (addr->disp_flag) {
1223 			case QETH_DISP_ADDR_DELETE:
1224 				rc = qeth_l3_deregister_addr_entry(card, addr);
1225 				if (!rc || rc == -ENOENT) {
1226 					hash_del(&addr->hnode);
1227 					kfree(addr);
1228 				}
1229 				break;
1230 			case QETH_DISP_ADDR_ADD:
1231 				rc = qeth_l3_register_addr_entry(card, addr);
1232 				if (rc && rc != -ENETDOWN) {
1233 					hash_del(&addr->hnode);
1234 					kfree(addr);
1235 					break;
1236 				}
1237 				addr->ref_counter = 1;
1238 				fallthrough;
1239 			default:
1240 				/* for next call to set_rx_mode(): */
1241 				addr->disp_flag = QETH_DISP_ADDR_DELETE;
1242 			}
1243 		}
1244 	}
1245 
1246 	qeth_l3_set_promisc_mode(card);
1247 }
1248 
1249 static int qeth_l3_arp_makerc(u16 rc)
1250 {
1251 	switch (rc) {
1252 	case IPA_RC_SUCCESS:
1253 		return 0;
1254 	case QETH_IPA_ARP_RC_NOTSUPP:
1255 	case QETH_IPA_ARP_RC_Q_NOTSUPP:
1256 		return -EOPNOTSUPP;
1257 	case QETH_IPA_ARP_RC_OUT_OF_RANGE:
1258 		return -EINVAL;
1259 	case QETH_IPA_ARP_RC_Q_NO_DATA:
1260 		return -ENOENT;
1261 	default:
1262 		return -EIO;
1263 	}
1264 }
1265 
1266 static int qeth_l3_arp_cmd_cb(struct qeth_card *card, struct qeth_reply *reply,
1267 			      unsigned long data)
1268 {
1269 	struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1270 
1271 	qeth_setassparms_cb(card, reply, data);
1272 	return qeth_l3_arp_makerc(cmd->hdr.return_code);
1273 }
1274 
1275 static int qeth_l3_arp_set_no_entries(struct qeth_card *card, int no_entries)
1276 {
1277 	struct qeth_cmd_buffer *iob;
1278 	int rc;
1279 
1280 	QETH_CARD_TEXT(card, 3, "arpstnoe");
1281 
1282 	/*
1283 	 * currently GuestLAN only supports the ARP assist function
1284 	 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_SET_NO_ENTRIES;
1285 	 * thus we say EOPNOTSUPP for this ARP function
1286 	 */
1287 	if (IS_VM_NIC(card))
1288 		return -EOPNOTSUPP;
1289 	if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1290 		return -EOPNOTSUPP;
1291 	}
1292 
1293 	iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1294 				       IPA_CMD_ASS_ARP_SET_NO_ENTRIES,
1295 				       SETASS_DATA_SIZEOF(flags_32bit),
1296 				       QETH_PROT_IPV4);
1297 	if (!iob)
1298 		return -ENOMEM;
1299 
1300 	__ipa_cmd(iob)->data.setassparms.data.flags_32bit = (u32) no_entries;
1301 	rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL);
1302 	if (rc)
1303 		QETH_DBF_MESSAGE(2, "Could not set number of ARP entries on device %x: %#x\n",
1304 				 CARD_DEVID(card), rc);
1305 	return rc;
1306 }
1307 
1308 static __u32 get_arp_entry_size(struct qeth_card *card,
1309 			struct qeth_arp_query_data *qdata,
1310 			struct qeth_arp_entrytype *type, __u8 strip_entries)
1311 {
1312 	__u32 rc;
1313 	__u8 is_hsi;
1314 
1315 	is_hsi = qdata->reply_bits == 5;
1316 	if (type->ip == QETHARP_IP_ADDR_V4) {
1317 		QETH_CARD_TEXT(card, 4, "arpev4");
1318 		if (strip_entries) {
1319 			rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5_short) :
1320 				sizeof(struct qeth_arp_qi_entry7_short);
1321 		} else {
1322 			rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5) :
1323 				sizeof(struct qeth_arp_qi_entry7);
1324 		}
1325 	} else if (type->ip == QETHARP_IP_ADDR_V6) {
1326 		QETH_CARD_TEXT(card, 4, "arpev6");
1327 		if (strip_entries) {
1328 			rc = is_hsi ?
1329 				sizeof(struct qeth_arp_qi_entry5_short_ipv6) :
1330 				sizeof(struct qeth_arp_qi_entry7_short_ipv6);
1331 		} else {
1332 			rc = is_hsi ?
1333 				sizeof(struct qeth_arp_qi_entry5_ipv6) :
1334 				sizeof(struct qeth_arp_qi_entry7_ipv6);
1335 		}
1336 	} else {
1337 		QETH_CARD_TEXT(card, 4, "arpinv");
1338 		rc = 0;
1339 	}
1340 
1341 	return rc;
1342 }
1343 
1344 static int arpentry_matches_prot(struct qeth_arp_entrytype *type, __u16 prot)
1345 {
1346 	return (type->ip == QETHARP_IP_ADDR_V4 && prot == QETH_PROT_IPV4) ||
1347 		(type->ip == QETHARP_IP_ADDR_V6 && prot == QETH_PROT_IPV6);
1348 }
1349 
1350 static int qeth_l3_arp_query_cb(struct qeth_card *card,
1351 		struct qeth_reply *reply, unsigned long data)
1352 {
1353 	struct qeth_ipa_cmd *cmd;
1354 	struct qeth_arp_query_data *qdata;
1355 	struct qeth_arp_query_info *qinfo;
1356 	int e;
1357 	int entrybytes_done;
1358 	int stripped_bytes;
1359 	__u8 do_strip_entries;
1360 
1361 	QETH_CARD_TEXT(card, 3, "arpquecb");
1362 
1363 	qinfo = (struct qeth_arp_query_info *) reply->param;
1364 	cmd = (struct qeth_ipa_cmd *) data;
1365 	QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.prot_version);
1366 	if (cmd->hdr.return_code) {
1367 		QETH_CARD_TEXT(card, 4, "arpcberr");
1368 		QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1369 		return qeth_l3_arp_makerc(cmd->hdr.return_code);
1370 	}
1371 	if (cmd->data.setassparms.hdr.return_code) {
1372 		cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
1373 		QETH_CARD_TEXT(card, 4, "setaperr");
1374 		QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1375 		return qeth_l3_arp_makerc(cmd->hdr.return_code);
1376 	}
1377 	qdata = &cmd->data.setassparms.data.query_arp;
1378 	QETH_CARD_TEXT_(card, 4, "anoen%i", qdata->no_entries);
1379 
1380 	do_strip_entries = (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) > 0;
1381 	stripped_bytes = do_strip_entries ? QETH_QARP_MEDIASPECIFIC_BYTES : 0;
1382 	entrybytes_done = 0;
1383 	for (e = 0; e < qdata->no_entries; ++e) {
1384 		char *cur_entry;
1385 		__u32 esize;
1386 		struct qeth_arp_entrytype *etype;
1387 
1388 		cur_entry = &qdata->data + entrybytes_done;
1389 		etype = &((struct qeth_arp_qi_entry5 *) cur_entry)->type;
1390 		if (!arpentry_matches_prot(etype, cmd->hdr.prot_version)) {
1391 			QETH_CARD_TEXT(card, 4, "pmis");
1392 			QETH_CARD_TEXT_(card, 4, "%i", etype->ip);
1393 			break;
1394 		}
1395 		esize = get_arp_entry_size(card, qdata, etype,
1396 			do_strip_entries);
1397 		QETH_CARD_TEXT_(card, 5, "esz%i", esize);
1398 		if (!esize)
1399 			break;
1400 
1401 		if ((qinfo->udata_len - qinfo->udata_offset) < esize) {
1402 			QETH_CARD_TEXT_(card, 4, "qaer3%i", -ENOSPC);
1403 			memset(qinfo->udata, 0, 4);
1404 			return -ENOSPC;
1405 		}
1406 
1407 		memcpy(qinfo->udata + qinfo->udata_offset,
1408 			&qdata->data + entrybytes_done + stripped_bytes,
1409 			esize);
1410 		entrybytes_done += esize + stripped_bytes;
1411 		qinfo->udata_offset += esize;
1412 		++qinfo->no_entries;
1413 	}
1414 	/* check if all replies received ... */
1415 	if (cmd->data.setassparms.hdr.seq_no <
1416 	    cmd->data.setassparms.hdr.number_of_replies)
1417 		return 1;
1418 	QETH_CARD_TEXT_(card, 4, "nove%i", qinfo->no_entries);
1419 	memcpy(qinfo->udata, &qinfo->no_entries, 4);
1420 	/* keep STRIP_ENTRIES flag so the user program can distinguish
1421 	 * stripped entries from normal ones */
1422 	if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
1423 		qdata->reply_bits |= QETH_QARP_STRIP_ENTRIES;
1424 	memcpy(qinfo->udata + QETH_QARP_MASK_OFFSET, &qdata->reply_bits, 2);
1425 	QETH_CARD_TEXT_(card, 4, "rc%i", 0);
1426 	return 0;
1427 }
1428 
1429 static int qeth_l3_query_arp_cache_info(struct qeth_card *card,
1430 	enum qeth_prot_versions prot,
1431 	struct qeth_arp_query_info *qinfo)
1432 {
1433 	struct qeth_cmd_buffer *iob;
1434 	struct qeth_ipa_cmd *cmd;
1435 	int rc;
1436 
1437 	QETH_CARD_TEXT_(card, 3, "qarpipv%i", prot);
1438 
1439 	iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1440 				       IPA_CMD_ASS_ARP_QUERY_INFO,
1441 				       SETASS_DATA_SIZEOF(query_arp), prot);
1442 	if (!iob)
1443 		return -ENOMEM;
1444 	cmd = __ipa_cmd(iob);
1445 	cmd->data.setassparms.data.query_arp.request_bits = 0x000F;
1446 	rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_query_cb, qinfo);
1447 	if (rc)
1448 		QETH_DBF_MESSAGE(2, "Error while querying ARP cache on device %x: %#x\n",
1449 				 CARD_DEVID(card), rc);
1450 	return rc;
1451 }
1452 
1453 static int qeth_l3_arp_query(struct qeth_card *card, char __user *udata)
1454 {
1455 	struct qeth_arp_query_info qinfo = {0, };
1456 	int rc;
1457 
1458 	QETH_CARD_TEXT(card, 3, "arpquery");
1459 
1460 	if (!qeth_is_supported(card,/*IPA_QUERY_ARP_ADDR_INFO*/
1461 			       IPA_ARP_PROCESSING)) {
1462 		QETH_CARD_TEXT(card, 3, "arpqnsup");
1463 		rc = -EOPNOTSUPP;
1464 		goto out;
1465 	}
1466 	/* get size of userspace buffer and mask_bits -> 6 bytes */
1467 	if (copy_from_user(&qinfo, udata, 6)) {
1468 		rc = -EFAULT;
1469 		goto out;
1470 	}
1471 	qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
1472 	if (!qinfo.udata) {
1473 		rc = -ENOMEM;
1474 		goto out;
1475 	}
1476 	qinfo.udata_offset = QETH_QARP_ENTRIES_OFFSET;
1477 	rc = qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV4, &qinfo);
1478 	if (rc) {
1479 		if (copy_to_user(udata, qinfo.udata, 4))
1480 			rc = -EFAULT;
1481 		goto free_and_out;
1482 	}
1483 	if (qinfo.mask_bits & QETH_QARP_WITH_IPV6) {
1484 		/* fails in case of GuestLAN QDIO mode */
1485 		qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV6, &qinfo);
1486 	}
1487 	if (copy_to_user(udata, qinfo.udata, qinfo.udata_len)) {
1488 		QETH_CARD_TEXT(card, 4, "qactf");
1489 		rc = -EFAULT;
1490 		goto free_and_out;
1491 	}
1492 	QETH_CARD_TEXT(card, 4, "qacts");
1493 
1494 free_and_out:
1495 	kfree(qinfo.udata);
1496 out:
1497 	return rc;
1498 }
1499 
1500 static int qeth_l3_arp_modify_entry(struct qeth_card *card,
1501 				    struct qeth_arp_cache_entry *entry,
1502 				    enum qeth_arp_process_subcmds arp_cmd)
1503 {
1504 	struct qeth_arp_cache_entry *cmd_entry;
1505 	struct qeth_cmd_buffer *iob;
1506 	int rc;
1507 
1508 	if (arp_cmd == IPA_CMD_ASS_ARP_ADD_ENTRY)
1509 		QETH_CARD_TEXT(card, 3, "arpadd");
1510 	else
1511 		QETH_CARD_TEXT(card, 3, "arpdel");
1512 
1513 	/*
1514 	 * currently GuestLAN only supports the ARP assist function
1515 	 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_ADD_ENTRY;
1516 	 * thus we say EOPNOTSUPP for this ARP function
1517 	 */
1518 	if (IS_VM_NIC(card))
1519 		return -EOPNOTSUPP;
1520 	if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1521 		return -EOPNOTSUPP;
1522 	}
1523 
1524 	iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING, arp_cmd,
1525 				       SETASS_DATA_SIZEOF(arp_entry),
1526 				       QETH_PROT_IPV4);
1527 	if (!iob)
1528 		return -ENOMEM;
1529 
1530 	cmd_entry = &__ipa_cmd(iob)->data.setassparms.data.arp_entry;
1531 	ether_addr_copy(cmd_entry->macaddr, entry->macaddr);
1532 	memcpy(cmd_entry->ipaddr, entry->ipaddr, 4);
1533 	rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL);
1534 	if (rc)
1535 		QETH_DBF_MESSAGE(2, "Could not modify (cmd: %#x) ARP entry on device %x: %#x\n",
1536 				 arp_cmd, CARD_DEVID(card), rc);
1537 	return rc;
1538 }
1539 
1540 static int qeth_l3_arp_flush_cache(struct qeth_card *card)
1541 {
1542 	struct qeth_cmd_buffer *iob;
1543 	int rc;
1544 
1545 	QETH_CARD_TEXT(card, 3, "arpflush");
1546 
1547 	/*
1548 	 * currently GuestLAN only supports the ARP assist function
1549 	 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_FLUSH_CACHE;
1550 	 * thus we say EOPNOTSUPP for this ARP function
1551 	*/
1552 	if (IS_VM_NIC(card) || IS_IQD(card))
1553 		return -EOPNOTSUPP;
1554 	if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1555 		return -EOPNOTSUPP;
1556 	}
1557 
1558 	iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1559 				       IPA_CMD_ASS_ARP_FLUSH_CACHE, 0,
1560 				       QETH_PROT_IPV4);
1561 	if (!iob)
1562 		return -ENOMEM;
1563 
1564 	rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL);
1565 	if (rc)
1566 		QETH_DBF_MESSAGE(2, "Could not flush ARP cache on device %x: %#x\n",
1567 				 CARD_DEVID(card), rc);
1568 	return rc;
1569 }
1570 
1571 static int qeth_l3_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1572 {
1573 	struct qeth_card *card = dev->ml_priv;
1574 	struct qeth_arp_cache_entry arp_entry;
1575 	enum qeth_arp_process_subcmds arp_cmd;
1576 	int rc = 0;
1577 
1578 	switch (cmd) {
1579 	case SIOC_QETH_ARP_SET_NO_ENTRIES:
1580 		if (!capable(CAP_NET_ADMIN)) {
1581 			rc = -EPERM;
1582 			break;
1583 		}
1584 		rc = qeth_l3_arp_set_no_entries(card, rq->ifr_ifru.ifru_ivalue);
1585 		break;
1586 	case SIOC_QETH_ARP_QUERY_INFO:
1587 		if (!capable(CAP_NET_ADMIN)) {
1588 			rc = -EPERM;
1589 			break;
1590 		}
1591 		rc = qeth_l3_arp_query(card, rq->ifr_ifru.ifru_data);
1592 		break;
1593 	case SIOC_QETH_ARP_ADD_ENTRY:
1594 	case SIOC_QETH_ARP_REMOVE_ENTRY:
1595 		if (!capable(CAP_NET_ADMIN))
1596 			return -EPERM;
1597 		if (copy_from_user(&arp_entry, rq->ifr_data, sizeof(arp_entry)))
1598 			return -EFAULT;
1599 
1600 		arp_cmd = (cmd == SIOC_QETH_ARP_ADD_ENTRY) ?
1601 				IPA_CMD_ASS_ARP_ADD_ENTRY :
1602 				IPA_CMD_ASS_ARP_REMOVE_ENTRY;
1603 		return qeth_l3_arp_modify_entry(card, &arp_entry, arp_cmd);
1604 	case SIOC_QETH_ARP_FLUSH_CACHE:
1605 		if (!capable(CAP_NET_ADMIN)) {
1606 			rc = -EPERM;
1607 			break;
1608 		}
1609 		rc = qeth_l3_arp_flush_cache(card);
1610 		break;
1611 	default:
1612 		rc = -EOPNOTSUPP;
1613 	}
1614 	return rc;
1615 }
1616 
1617 static int qeth_l3_get_cast_type_rcu(struct sk_buff *skb, struct dst_entry *dst,
1618 				     int ipv)
1619 {
1620 	struct neighbour *n = NULL;
1621 
1622 	if (dst)
1623 		n = dst_neigh_lookup_skb(dst, skb);
1624 
1625 	if (n) {
1626 		int cast_type = n->type;
1627 
1628 		neigh_release(n);
1629 		if ((cast_type == RTN_BROADCAST) ||
1630 		    (cast_type == RTN_MULTICAST) ||
1631 		    (cast_type == RTN_ANYCAST))
1632 			return cast_type;
1633 		return RTN_UNICAST;
1634 	}
1635 
1636 	/* no neighbour (eg AF_PACKET), fall back to target's IP address ... */
1637 	switch (ipv) {
1638 	case 4:
1639 		if (ipv4_is_lbcast(ip_hdr(skb)->daddr))
1640 			return RTN_BROADCAST;
1641 		return ipv4_is_multicast(ip_hdr(skb)->daddr) ?
1642 				RTN_MULTICAST : RTN_UNICAST;
1643 	case 6:
1644 		return ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ?
1645 				RTN_MULTICAST : RTN_UNICAST;
1646 	default:
1647 		/* ... and MAC address */
1648 		return qeth_get_ether_cast_type(skb);
1649 	}
1650 }
1651 
1652 static int qeth_l3_get_cast_type(struct sk_buff *skb)
1653 {
1654 	int ipv = qeth_get_ip_version(skb);
1655 	struct dst_entry *dst;
1656 	int cast_type;
1657 
1658 	rcu_read_lock();
1659 	dst = qeth_dst_check_rcu(skb, ipv);
1660 	cast_type = qeth_l3_get_cast_type_rcu(skb, dst, ipv);
1661 	rcu_read_unlock();
1662 
1663 	return cast_type;
1664 }
1665 
1666 static u8 qeth_l3_cast_type_to_flag(int cast_type)
1667 {
1668 	if (cast_type == RTN_MULTICAST)
1669 		return QETH_CAST_MULTICAST;
1670 	if (cast_type == RTN_ANYCAST)
1671 		return QETH_CAST_ANYCAST;
1672 	if (cast_type == RTN_BROADCAST)
1673 		return QETH_CAST_BROADCAST;
1674 	return QETH_CAST_UNICAST;
1675 }
1676 
1677 static void qeth_l3_fill_header(struct qeth_qdio_out_q *queue,
1678 				struct qeth_hdr *hdr, struct sk_buff *skb,
1679 				int ipv, unsigned int data_len)
1680 {
1681 	struct qeth_hdr_layer3 *l3_hdr = &hdr->hdr.l3;
1682 	struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
1683 	struct qeth_card *card = queue->card;
1684 	struct dst_entry *dst;
1685 	int cast_type;
1686 
1687 	hdr->hdr.l3.length = data_len;
1688 
1689 	if (skb_is_gso(skb)) {
1690 		hdr->hdr.l3.id = QETH_HEADER_TYPE_L3_TSO;
1691 	} else {
1692 		hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
1693 
1694 		if (skb->protocol == htons(ETH_P_AF_IUCV)) {
1695 			l3_hdr->flags = QETH_HDR_IPV6 | QETH_CAST_UNICAST;
1696 			l3_hdr->next_hop.addr.s6_addr16[0] = htons(0xfe80);
1697 			memcpy(&l3_hdr->next_hop.addr.s6_addr32[2],
1698 			       iucv_trans_hdr(skb)->destUserID, 8);
1699 			return;
1700 		}
1701 
1702 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
1703 			qeth_tx_csum(skb, &hdr->hdr.l3.ext_flags, ipv);
1704 			/* some HW requires combined L3+L4 csum offload: */
1705 			if (ipv == 4)
1706 				hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_CSUM_HDR_REQ;
1707 		}
1708 	}
1709 
1710 	if (ipv == 4 || IS_IQD(card)) {
1711 		/* NETIF_F_HW_VLAN_CTAG_TX */
1712 		if (skb_vlan_tag_present(skb)) {
1713 			hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_VLAN_FRAME;
1714 			hdr->hdr.l3.vlan_id = skb_vlan_tag_get(skb);
1715 		}
1716 	} else if (veth->h_vlan_proto == htons(ETH_P_8021Q)) {
1717 		hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_INCLUDE_VLAN_TAG;
1718 		hdr->hdr.l3.vlan_id = ntohs(veth->h_vlan_TCI);
1719 	}
1720 
1721 	rcu_read_lock();
1722 	dst = qeth_dst_check_rcu(skb, ipv);
1723 
1724 	if (IS_IQD(card) && skb_get_queue_mapping(skb) != QETH_IQD_MCAST_TXQ)
1725 		cast_type = RTN_UNICAST;
1726 	else
1727 		cast_type = qeth_l3_get_cast_type_rcu(skb, dst, ipv);
1728 	l3_hdr->flags |= qeth_l3_cast_type_to_flag(cast_type);
1729 
1730 	if (ipv == 4) {
1731 		l3_hdr->next_hop.addr.s6_addr32[3] =
1732 					qeth_next_hop_v4_rcu(skb, dst);
1733 	} else if (ipv == 6) {
1734 		l3_hdr->next_hop.addr = *qeth_next_hop_v6_rcu(skb, dst);
1735 
1736 		hdr->hdr.l3.flags |= QETH_HDR_IPV6;
1737 		if (!IS_IQD(card))
1738 			hdr->hdr.l3.flags |= QETH_HDR_PASSTHRU;
1739 	} else {
1740 		/* OSA only: */
1741 		l3_hdr->flags |= QETH_HDR_PASSTHRU;
1742 	}
1743 	rcu_read_unlock();
1744 }
1745 
1746 static void qeth_l3_fixup_headers(struct sk_buff *skb)
1747 {
1748 	struct iphdr *iph = ip_hdr(skb);
1749 
1750 	/* this is safe, IPv6 traffic takes a different path */
1751 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1752 		iph->check = 0;
1753 	if (skb_is_gso(skb)) {
1754 		iph->tot_len = 0;
1755 		tcp_hdr(skb)->check = ~tcp_v4_check(0, iph->saddr,
1756 						    iph->daddr, 0);
1757 	}
1758 }
1759 
1760 static int qeth_l3_xmit(struct qeth_card *card, struct sk_buff *skb,
1761 			struct qeth_qdio_out_q *queue, int ipv)
1762 {
1763 	unsigned int hw_hdr_len;
1764 	int rc;
1765 
1766 	/* re-use the L2 header area for the HW header: */
1767 	hw_hdr_len = skb_is_gso(skb) ? sizeof(struct qeth_hdr_tso) :
1768 				       sizeof(struct qeth_hdr);
1769 	rc = skb_cow_head(skb, hw_hdr_len - ETH_HLEN);
1770 	if (rc)
1771 		return rc;
1772 	skb_pull(skb, ETH_HLEN);
1773 
1774 	qeth_l3_fixup_headers(skb);
1775 	return qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header);
1776 }
1777 
1778 static netdev_tx_t qeth_l3_hard_start_xmit(struct sk_buff *skb,
1779 					   struct net_device *dev)
1780 {
1781 	struct qeth_card *card = dev->ml_priv;
1782 	u16 txq = skb_get_queue_mapping(skb);
1783 	int ipv = qeth_get_ip_version(skb);
1784 	struct qeth_qdio_out_q *queue;
1785 	int rc;
1786 
1787 	if (!skb_is_gso(skb))
1788 		qdisc_skb_cb(skb)->pkt_len = skb->len;
1789 	if (IS_IQD(card)) {
1790 		queue = card->qdio.out_qs[qeth_iqd_translate_txq(dev, txq)];
1791 
1792 		if (card->options.sniffer)
1793 			goto tx_drop;
1794 		if ((card->options.cq != QETH_CQ_ENABLED && !ipv) ||
1795 		    (card->options.cq == QETH_CQ_ENABLED &&
1796 		     skb->protocol != htons(ETH_P_AF_IUCV)))
1797 			goto tx_drop;
1798 	} else {
1799 		queue = card->qdio.out_qs[txq];
1800 	}
1801 
1802 	if (!(dev->flags & IFF_BROADCAST) &&
1803 	    qeth_l3_get_cast_type(skb) == RTN_BROADCAST)
1804 		goto tx_drop;
1805 
1806 	if (ipv == 4 || IS_IQD(card))
1807 		rc = qeth_l3_xmit(card, skb, queue, ipv);
1808 	else
1809 		rc = qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header);
1810 
1811 	if (!rc)
1812 		return NETDEV_TX_OK;
1813 
1814 tx_drop:
1815 	QETH_TXQ_STAT_INC(queue, tx_dropped);
1816 	kfree_skb(skb);
1817 	return NETDEV_TX_OK;
1818 }
1819 
1820 static void qeth_l3_set_rx_mode(struct net_device *dev)
1821 {
1822 	struct qeth_card *card = dev->ml_priv;
1823 
1824 	schedule_work(&card->rx_mode_work);
1825 }
1826 
1827 /*
1828  * we need NOARP for IPv4 but we want neighbor solicitation for IPv6. Setting
1829  * NOARP on the netdevice is no option because it also turns off neighbor
1830  * solicitation. For IPv4 we install a neighbor_setup function. We don't want
1831  * arp resolution but we want the hard header (packet socket will work
1832  * e.g. tcpdump)
1833  */
1834 static int qeth_l3_neigh_setup_noarp(struct neighbour *n)
1835 {
1836 	n->nud_state = NUD_NOARP;
1837 	memcpy(n->ha, "FAKELL", 6);
1838 	n->output = n->ops->connected_output;
1839 	return 0;
1840 }
1841 
1842 static int
1843 qeth_l3_neigh_setup(struct net_device *dev, struct neigh_parms *np)
1844 {
1845 	if (np->tbl->family == AF_INET)
1846 		np->neigh_setup = qeth_l3_neigh_setup_noarp;
1847 
1848 	return 0;
1849 }
1850 
1851 static netdev_features_t qeth_l3_osa_features_check(struct sk_buff *skb,
1852 						    struct net_device *dev,
1853 						    netdev_features_t features)
1854 {
1855 	if (qeth_get_ip_version(skb) != 4)
1856 		features &= ~NETIF_F_HW_VLAN_CTAG_TX;
1857 	return qeth_features_check(skb, dev, features);
1858 }
1859 
1860 static u16 qeth_l3_iqd_select_queue(struct net_device *dev, struct sk_buff *skb,
1861 				    struct net_device *sb_dev)
1862 {
1863 	return qeth_iqd_select_queue(dev, skb, qeth_l3_get_cast_type(skb),
1864 				     sb_dev);
1865 }
1866 
1867 static u16 qeth_l3_osa_select_queue(struct net_device *dev, struct sk_buff *skb,
1868 				    struct net_device *sb_dev)
1869 {
1870 	struct qeth_card *card = dev->ml_priv;
1871 
1872 	return IS_VM_NIC(card) ? netdev_pick_tx(dev, skb, sb_dev) :
1873 				 qeth_get_priority_queue(card, skb);
1874 }
1875 
1876 static const struct net_device_ops qeth_l3_netdev_ops = {
1877 	.ndo_open		= qeth_open,
1878 	.ndo_stop		= qeth_stop,
1879 	.ndo_get_stats64	= qeth_get_stats64,
1880 	.ndo_start_xmit		= qeth_l3_hard_start_xmit,
1881 	.ndo_select_queue	= qeth_l3_iqd_select_queue,
1882 	.ndo_validate_addr	= eth_validate_addr,
1883 	.ndo_set_rx_mode	= qeth_l3_set_rx_mode,
1884 	.ndo_do_ioctl		= qeth_do_ioctl,
1885 	.ndo_fix_features	= qeth_fix_features,
1886 	.ndo_set_features	= qeth_set_features,
1887 	.ndo_vlan_rx_add_vid	= qeth_l3_vlan_rx_add_vid,
1888 	.ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
1889 	.ndo_tx_timeout		= qeth_tx_timeout,
1890 };
1891 
1892 static const struct net_device_ops qeth_l3_osa_netdev_ops = {
1893 	.ndo_open		= qeth_open,
1894 	.ndo_stop		= qeth_stop,
1895 	.ndo_get_stats64	= qeth_get_stats64,
1896 	.ndo_start_xmit		= qeth_l3_hard_start_xmit,
1897 	.ndo_features_check	= qeth_l3_osa_features_check,
1898 	.ndo_select_queue	= qeth_l3_osa_select_queue,
1899 	.ndo_validate_addr	= eth_validate_addr,
1900 	.ndo_set_rx_mode	= qeth_l3_set_rx_mode,
1901 	.ndo_do_ioctl		= qeth_do_ioctl,
1902 	.ndo_fix_features	= qeth_fix_features,
1903 	.ndo_set_features	= qeth_set_features,
1904 	.ndo_vlan_rx_add_vid	= qeth_l3_vlan_rx_add_vid,
1905 	.ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
1906 	.ndo_tx_timeout		= qeth_tx_timeout,
1907 	.ndo_neigh_setup	= qeth_l3_neigh_setup,
1908 };
1909 
1910 static int qeth_l3_setup_netdev(struct qeth_card *card)
1911 {
1912 	struct net_device *dev = card->dev;
1913 	unsigned int headroom;
1914 	int rc;
1915 
1916 	rc = qeth_setup_netdev(card);
1917 	if (rc)
1918 		return rc;
1919 
1920 	if (IS_OSD(card) || IS_OSX(card)) {
1921 		card->dev->netdev_ops = &qeth_l3_osa_netdev_ops;
1922 
1923 		/*IPv6 address autoconfiguration stuff*/
1924 		dev->dev_id = qeth_l3_get_unique_id(card, dev->dev_id);
1925 
1926 		if (!IS_VM_NIC(card)) {
1927 			card->dev->features |= NETIF_F_SG;
1928 			card->dev->hw_features |= NETIF_F_TSO |
1929 				NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
1930 			card->dev->vlan_features |= NETIF_F_TSO |
1931 				NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
1932 		}
1933 
1934 		if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) {
1935 			card->dev->hw_features |= NETIF_F_IPV6_CSUM;
1936 			card->dev->vlan_features |= NETIF_F_IPV6_CSUM;
1937 		}
1938 		if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) {
1939 			card->dev->hw_features |= NETIF_F_TSO6;
1940 			card->dev->vlan_features |= NETIF_F_TSO6;
1941 		}
1942 
1943 		/* allow for de-acceleration of NETIF_F_HW_VLAN_CTAG_TX: */
1944 		if (card->dev->hw_features & NETIF_F_TSO6)
1945 			headroom = sizeof(struct qeth_hdr_tso) + VLAN_HLEN;
1946 		else if (card->dev->hw_features & NETIF_F_TSO)
1947 			headroom = sizeof(struct qeth_hdr_tso);
1948 		else
1949 			headroom = sizeof(struct qeth_hdr) + VLAN_HLEN;
1950 	} else if (IS_IQD(card)) {
1951 		card->dev->flags |= IFF_NOARP;
1952 		card->dev->netdev_ops = &qeth_l3_netdev_ops;
1953 		headroom = sizeof(struct qeth_hdr) - ETH_HLEN;
1954 
1955 		rc = qeth_l3_iqd_read_initial_mac(card);
1956 		if (rc)
1957 			return rc;
1958 	} else
1959 		return -ENODEV;
1960 
1961 	card->dev->needed_headroom = headroom;
1962 	card->dev->features |=	NETIF_F_HW_VLAN_CTAG_TX |
1963 				NETIF_F_HW_VLAN_CTAG_RX |
1964 				NETIF_F_HW_VLAN_CTAG_FILTER;
1965 
1966 	netif_keep_dst(card->dev);
1967 	if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6))
1968 		netif_set_gso_max_size(card->dev,
1969 				       PAGE_SIZE * (QETH_MAX_BUFFER_ELEMENTS(card) - 1));
1970 
1971 	netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
1972 	return register_netdev(card->dev);
1973 }
1974 
1975 static const struct device_type qeth_l3_devtype = {
1976 	.name = "qeth_layer3",
1977 	.groups = qeth_l3_attr_groups,
1978 };
1979 
1980 static int qeth_l3_probe_device(struct ccwgroup_device *gdev)
1981 {
1982 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1983 	int rc;
1984 
1985 	hash_init(card->ip_htable);
1986 	mutex_init(&card->ip_lock);
1987 	card->cmd_wq = alloc_ordered_workqueue("%s_cmd", 0,
1988 					       dev_name(&gdev->dev));
1989 	if (!card->cmd_wq)
1990 		return -ENOMEM;
1991 
1992 	if (gdev->dev.type == &qeth_generic_devtype) {
1993 		rc = qeth_l3_create_device_attributes(&gdev->dev);
1994 		if (rc) {
1995 			destroy_workqueue(card->cmd_wq);
1996 			return rc;
1997 		}
1998 	}
1999 
2000 	INIT_WORK(&card->rx_mode_work, qeth_l3_rx_mode_work);
2001 	return 0;
2002 }
2003 
2004 static void qeth_l3_remove_device(struct ccwgroup_device *cgdev)
2005 {
2006 	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
2007 
2008 	if (cgdev->dev.type == &qeth_generic_devtype)
2009 		qeth_l3_remove_device_attributes(&cgdev->dev);
2010 
2011 	qeth_set_allowed_threads(card, 0, 1);
2012 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
2013 
2014 	if (cgdev->state == CCWGROUP_ONLINE)
2015 		qeth_set_offline(card, false);
2016 
2017 	cancel_work_sync(&card->close_dev_work);
2018 	if (card->dev->reg_state == NETREG_REGISTERED)
2019 		unregister_netdev(card->dev);
2020 
2021 	flush_workqueue(card->cmd_wq);
2022 	destroy_workqueue(card->cmd_wq);
2023 	qeth_l3_clear_ip_htable(card, 0);
2024 	qeth_l3_clear_ipato_list(card);
2025 }
2026 
2027 static int qeth_l3_set_online(struct qeth_card *card)
2028 {
2029 	struct ccwgroup_device *gdev = card->gdev;
2030 	struct net_device *dev = card->dev;
2031 	int rc = 0;
2032 	bool carrier_ok;
2033 
2034 	rc = qeth_core_hardsetup_card(card, &carrier_ok);
2035 	if (rc) {
2036 		QETH_CARD_TEXT_(card, 2, "2err%04x", rc);
2037 		rc = -ENODEV;
2038 		goto out_remove;
2039 	}
2040 
2041 	qeth_print_status_message(card);
2042 
2043 	/* softsetup */
2044 	QETH_CARD_TEXT(card, 2, "softsetp");
2045 
2046 	rc = qeth_l3_setadapter_parms(card);
2047 	if (rc)
2048 		QETH_CARD_TEXT_(card, 2, "2err%04x", rc);
2049 	if (!card->options.sniffer) {
2050 		qeth_l3_start_ipassists(card);
2051 
2052 		rc = qeth_l3_setrouting_v4(card);
2053 		if (rc)
2054 			QETH_CARD_TEXT_(card, 2, "4err%04x", rc);
2055 		rc = qeth_l3_setrouting_v6(card);
2056 		if (rc)
2057 			QETH_CARD_TEXT_(card, 2, "5err%04x", rc);
2058 	}
2059 
2060 	card->state = CARD_STATE_SOFTSETUP;
2061 
2062 	qeth_set_allowed_threads(card, 0xffffffff, 0);
2063 	qeth_l3_recover_ip(card);
2064 
2065 	if (dev->reg_state != NETREG_REGISTERED) {
2066 		rc = qeth_l3_setup_netdev(card);
2067 		if (rc)
2068 			goto out_remove;
2069 
2070 		if (carrier_ok)
2071 			netif_carrier_on(dev);
2072 	} else {
2073 		rtnl_lock();
2074 		if (carrier_ok)
2075 			netif_carrier_on(dev);
2076 		else
2077 			netif_carrier_off(dev);
2078 
2079 		netif_device_attach(dev);
2080 		qeth_enable_hw_features(dev);
2081 
2082 		if (card->info.open_when_online) {
2083 			card->info.open_when_online = 0;
2084 			dev_open(dev, NULL);
2085 		}
2086 		rtnl_unlock();
2087 	}
2088 	qeth_trace_features(card);
2089 	/* let user_space know that device is online */
2090 	kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
2091 	return 0;
2092 out_remove:
2093 	qeth_l3_stop_card(card);
2094 	qeth_stop_channel(&card->data);
2095 	qeth_stop_channel(&card->write);
2096 	qeth_stop_channel(&card->read);
2097 	qdio_free(CARD_DDEV(card));
2098 	return rc;
2099 }
2100 
2101 static void qeth_l3_set_offline(struct qeth_card *card)
2102 {
2103 	qeth_l3_stop_card(card);
2104 }
2105 
2106 /* Returns zero if the command is successfully "consumed" */
2107 static int qeth_l3_control_event(struct qeth_card *card,
2108 					struct qeth_ipa_cmd *cmd)
2109 {
2110 	return 1;
2111 }
2112 
2113 struct qeth_discipline qeth_l3_discipline = {
2114 	.devtype = &qeth_l3_devtype,
2115 	.setup = qeth_l3_probe_device,
2116 	.remove = qeth_l3_remove_device,
2117 	.set_online = qeth_l3_set_online,
2118 	.set_offline = qeth_l3_set_offline,
2119 	.do_ioctl = qeth_l3_do_ioctl,
2120 	.control_event_handler = qeth_l3_control_event,
2121 };
2122 EXPORT_SYMBOL_GPL(qeth_l3_discipline);
2123 
2124 static int qeth_l3_handle_ip_event(struct qeth_card *card,
2125 				   struct qeth_ipaddr *addr,
2126 				   unsigned long event)
2127 {
2128 	switch (event) {
2129 	case NETDEV_UP:
2130 		qeth_l3_modify_ip(card, addr, true);
2131 		return NOTIFY_OK;
2132 	case NETDEV_DOWN:
2133 		qeth_l3_modify_ip(card, addr, false);
2134 		return NOTIFY_OK;
2135 	default:
2136 		return NOTIFY_DONE;
2137 	}
2138 }
2139 
2140 struct qeth_l3_ip_event_work {
2141 	struct work_struct work;
2142 	struct qeth_card *card;
2143 	struct qeth_ipaddr addr;
2144 };
2145 
2146 #define to_ip_work(w) container_of((w), struct qeth_l3_ip_event_work, work)
2147 
2148 static void qeth_l3_add_ip_worker(struct work_struct *work)
2149 {
2150 	struct qeth_l3_ip_event_work *ip_work = to_ip_work(work);
2151 
2152 	qeth_l3_modify_ip(ip_work->card, &ip_work->addr, true);
2153 	kfree(work);
2154 }
2155 
2156 static void qeth_l3_delete_ip_worker(struct work_struct *work)
2157 {
2158 	struct qeth_l3_ip_event_work *ip_work = to_ip_work(work);
2159 
2160 	qeth_l3_modify_ip(ip_work->card, &ip_work->addr, false);
2161 	kfree(work);
2162 }
2163 
2164 static struct qeth_card *qeth_l3_get_card_from_dev(struct net_device *dev)
2165 {
2166 	if (is_vlan_dev(dev))
2167 		dev = vlan_dev_real_dev(dev);
2168 	if (dev->netdev_ops == &qeth_l3_osa_netdev_ops ||
2169 	    dev->netdev_ops == &qeth_l3_netdev_ops)
2170 		return (struct qeth_card *) dev->ml_priv;
2171 	return NULL;
2172 }
2173 
2174 static int qeth_l3_ip_event(struct notifier_block *this,
2175 			    unsigned long event, void *ptr)
2176 {
2177 
2178 	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2179 	struct net_device *dev = ifa->ifa_dev->dev;
2180 	struct qeth_ipaddr addr;
2181 	struct qeth_card *card;
2182 
2183 	card = qeth_l3_get_card_from_dev(dev);
2184 	if (!card)
2185 		return NOTIFY_DONE;
2186 	QETH_CARD_TEXT(card, 3, "ipevent");
2187 
2188 	qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4);
2189 	addr.u.a4.addr = ifa->ifa_address;
2190 	addr.u.a4.mask = ifa->ifa_mask;
2191 
2192 	return qeth_l3_handle_ip_event(card, &addr, event);
2193 }
2194 
2195 static struct notifier_block qeth_l3_ip_notifier = {
2196 	qeth_l3_ip_event,
2197 	NULL,
2198 };
2199 
2200 static int qeth_l3_ip6_event(struct notifier_block *this,
2201 			     unsigned long event, void *ptr)
2202 {
2203 	struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
2204 	struct net_device *dev = ifa->idev->dev;
2205 	struct qeth_l3_ip_event_work *ip_work;
2206 	struct qeth_card *card;
2207 
2208 	if (event != NETDEV_UP && event != NETDEV_DOWN)
2209 		return NOTIFY_DONE;
2210 
2211 	card = qeth_l3_get_card_from_dev(dev);
2212 	if (!card)
2213 		return NOTIFY_DONE;
2214 	QETH_CARD_TEXT(card, 3, "ip6event");
2215 	if (!qeth_is_supported(card, IPA_IPV6))
2216 		return NOTIFY_DONE;
2217 
2218 	ip_work = kmalloc(sizeof(*ip_work), GFP_ATOMIC);
2219 	if (!ip_work)
2220 		return NOTIFY_DONE;
2221 
2222 	if (event == NETDEV_UP)
2223 		INIT_WORK(&ip_work->work, qeth_l3_add_ip_worker);
2224 	else
2225 		INIT_WORK(&ip_work->work, qeth_l3_delete_ip_worker);
2226 
2227 	ip_work->card = card;
2228 	qeth_l3_init_ipaddr(&ip_work->addr, QETH_IP_TYPE_NORMAL,
2229 			    QETH_PROT_IPV6);
2230 	ip_work->addr.u.a6.addr = ifa->addr;
2231 	ip_work->addr.u.a6.pfxlen = ifa->prefix_len;
2232 
2233 	queue_work(card->cmd_wq, &ip_work->work);
2234 	return NOTIFY_OK;
2235 }
2236 
2237 static struct notifier_block qeth_l3_ip6_notifier = {
2238 	qeth_l3_ip6_event,
2239 	NULL,
2240 };
2241 
2242 static int qeth_l3_register_notifiers(void)
2243 {
2244 	int rc;
2245 
2246 	QETH_DBF_TEXT(SETUP, 5, "regnotif");
2247 	rc = register_inetaddr_notifier(&qeth_l3_ip_notifier);
2248 	if (rc)
2249 		return rc;
2250 	rc = register_inet6addr_notifier(&qeth_l3_ip6_notifier);
2251 	if (rc) {
2252 		unregister_inetaddr_notifier(&qeth_l3_ip_notifier);
2253 		return rc;
2254 	}
2255 	return 0;
2256 }
2257 
2258 static void qeth_l3_unregister_notifiers(void)
2259 {
2260 	QETH_DBF_TEXT(SETUP, 5, "unregnot");
2261 	WARN_ON(unregister_inetaddr_notifier(&qeth_l3_ip_notifier));
2262 	WARN_ON(unregister_inet6addr_notifier(&qeth_l3_ip6_notifier));
2263 }
2264 
2265 static int __init qeth_l3_init(void)
2266 {
2267 	pr_info("register layer 3 discipline\n");
2268 	return qeth_l3_register_notifiers();
2269 }
2270 
2271 static void __exit qeth_l3_exit(void)
2272 {
2273 	qeth_l3_unregister_notifiers();
2274 	pr_info("unregister layer 3 discipline\n");
2275 }
2276 
2277 module_init(qeth_l3_init);
2278 module_exit(qeth_l3_exit);
2279 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
2280 MODULE_DESCRIPTION("qeth layer 3 discipline");
2281 MODULE_LICENSE("GPL");
2282