xref: /openbmc/linux/drivers/s390/net/qeth_l3_main.c (revision 165f2d28)
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->ip_mc_htable, 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->ip_mc_htable, 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->ip_mc_htable, &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->ip_mc_htable,
1128 				&ipm->hnode, 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 		cancel_delayed_work_sync(&card->buffer_reclaim_work);
1173 		card->state = CARD_STATE_DOWN;
1174 	}
1175 
1176 	qeth_qdio_clear_card(card, 0);
1177 	qeth_clear_working_pool_list(card);
1178 	flush_workqueue(card->event_wq);
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->ip_mc_htable, 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 				/* fall through */
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.ipv6_addr.s6_addr16[0] = htons(0xfe80);
1697 			memcpy(&l3_hdr->next_hop.ipv6_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 		struct rtable *rt = (struct rtable *) dst;
1732 
1733 		*((__be32 *) &hdr->hdr.l3.next_hop.ipv4.addr) = (rt) ?
1734 				rt_nexthop(rt, ip_hdr(skb)->daddr) :
1735 				ip_hdr(skb)->daddr;
1736 	} else if (ipv == 6) {
1737 		struct rt6_info *rt = (struct rt6_info *) dst;
1738 
1739 		if (rt && !ipv6_addr_any(&rt->rt6i_gateway))
1740 			l3_hdr->next_hop.ipv6_addr = rt->rt6i_gateway;
1741 		else
1742 			l3_hdr->next_hop.ipv6_addr = ipv6_hdr(skb)->daddr;
1743 
1744 		hdr->hdr.l3.flags |= QETH_HDR_IPV6;
1745 		if (!IS_IQD(card))
1746 			hdr->hdr.l3.flags |= QETH_HDR_PASSTHRU;
1747 	} else {
1748 		/* OSA only: */
1749 		l3_hdr->flags |= QETH_HDR_PASSTHRU;
1750 	}
1751 	rcu_read_unlock();
1752 }
1753 
1754 static void qeth_l3_fixup_headers(struct sk_buff *skb)
1755 {
1756 	struct iphdr *iph = ip_hdr(skb);
1757 
1758 	/* this is safe, IPv6 traffic takes a different path */
1759 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1760 		iph->check = 0;
1761 	if (skb_is_gso(skb)) {
1762 		iph->tot_len = 0;
1763 		tcp_hdr(skb)->check = ~tcp_v4_check(0, iph->saddr,
1764 						    iph->daddr, 0);
1765 	}
1766 }
1767 
1768 static int qeth_l3_xmit(struct qeth_card *card, struct sk_buff *skb,
1769 			struct qeth_qdio_out_q *queue, int ipv)
1770 {
1771 	unsigned int hw_hdr_len;
1772 	int rc;
1773 
1774 	/* re-use the L2 header area for the HW header: */
1775 	hw_hdr_len = skb_is_gso(skb) ? sizeof(struct qeth_hdr_tso) :
1776 				       sizeof(struct qeth_hdr);
1777 	rc = skb_cow_head(skb, hw_hdr_len - ETH_HLEN);
1778 	if (rc)
1779 		return rc;
1780 	skb_pull(skb, ETH_HLEN);
1781 
1782 	qeth_l3_fixup_headers(skb);
1783 	return qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header);
1784 }
1785 
1786 static netdev_tx_t qeth_l3_hard_start_xmit(struct sk_buff *skb,
1787 					   struct net_device *dev)
1788 {
1789 	struct qeth_card *card = dev->ml_priv;
1790 	u16 txq = skb_get_queue_mapping(skb);
1791 	int ipv = qeth_get_ip_version(skb);
1792 	struct qeth_qdio_out_q *queue;
1793 	int rc;
1794 
1795 	if (!skb_is_gso(skb))
1796 		qdisc_skb_cb(skb)->pkt_len = skb->len;
1797 	if (IS_IQD(card)) {
1798 		queue = card->qdio.out_qs[qeth_iqd_translate_txq(dev, txq)];
1799 
1800 		if (card->options.sniffer)
1801 			goto tx_drop;
1802 		if ((card->options.cq != QETH_CQ_ENABLED && !ipv) ||
1803 		    (card->options.cq == QETH_CQ_ENABLED &&
1804 		     skb->protocol != htons(ETH_P_AF_IUCV)))
1805 			goto tx_drop;
1806 	} else {
1807 		queue = card->qdio.out_qs[txq];
1808 	}
1809 
1810 	if (!(dev->flags & IFF_BROADCAST) &&
1811 	    qeth_l3_get_cast_type(skb) == RTN_BROADCAST)
1812 		goto tx_drop;
1813 
1814 	if (ipv == 4 || IS_IQD(card))
1815 		rc = qeth_l3_xmit(card, skb, queue, ipv);
1816 	else
1817 		rc = qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header);
1818 
1819 	if (!rc)
1820 		return NETDEV_TX_OK;
1821 
1822 tx_drop:
1823 	QETH_TXQ_STAT_INC(queue, tx_dropped);
1824 	kfree_skb(skb);
1825 	return NETDEV_TX_OK;
1826 }
1827 
1828 static void qeth_l3_set_rx_mode(struct net_device *dev)
1829 {
1830 	struct qeth_card *card = dev->ml_priv;
1831 
1832 	schedule_work(&card->rx_mode_work);
1833 }
1834 
1835 /*
1836  * we need NOARP for IPv4 but we want neighbor solicitation for IPv6. Setting
1837  * NOARP on the netdevice is no option because it also turns off neighbor
1838  * solicitation. For IPv4 we install a neighbor_setup function. We don't want
1839  * arp resolution but we want the hard header (packet socket will work
1840  * e.g. tcpdump)
1841  */
1842 static int qeth_l3_neigh_setup_noarp(struct neighbour *n)
1843 {
1844 	n->nud_state = NUD_NOARP;
1845 	memcpy(n->ha, "FAKELL", 6);
1846 	n->output = n->ops->connected_output;
1847 	return 0;
1848 }
1849 
1850 static int
1851 qeth_l3_neigh_setup(struct net_device *dev, struct neigh_parms *np)
1852 {
1853 	if (np->tbl->family == AF_INET)
1854 		np->neigh_setup = qeth_l3_neigh_setup_noarp;
1855 
1856 	return 0;
1857 }
1858 
1859 static netdev_features_t qeth_l3_osa_features_check(struct sk_buff *skb,
1860 						    struct net_device *dev,
1861 						    netdev_features_t features)
1862 {
1863 	if (qeth_get_ip_version(skb) != 4)
1864 		features &= ~NETIF_F_HW_VLAN_CTAG_TX;
1865 	return qeth_features_check(skb, dev, features);
1866 }
1867 
1868 static u16 qeth_l3_iqd_select_queue(struct net_device *dev, struct sk_buff *skb,
1869 				    struct net_device *sb_dev)
1870 {
1871 	return qeth_iqd_select_queue(dev, skb, qeth_l3_get_cast_type(skb),
1872 				     sb_dev);
1873 }
1874 
1875 static u16 qeth_l3_osa_select_queue(struct net_device *dev, struct sk_buff *skb,
1876 				    struct net_device *sb_dev)
1877 {
1878 	struct qeth_card *card = dev->ml_priv;
1879 
1880 	return IS_VM_NIC(card) ? netdev_pick_tx(dev, skb, sb_dev) :
1881 				 qeth_get_priority_queue(card, skb);
1882 }
1883 
1884 static const struct net_device_ops qeth_l3_netdev_ops = {
1885 	.ndo_open		= qeth_open,
1886 	.ndo_stop		= qeth_stop,
1887 	.ndo_get_stats64	= qeth_get_stats64,
1888 	.ndo_start_xmit		= qeth_l3_hard_start_xmit,
1889 	.ndo_select_queue	= qeth_l3_iqd_select_queue,
1890 	.ndo_validate_addr	= eth_validate_addr,
1891 	.ndo_set_rx_mode	= qeth_l3_set_rx_mode,
1892 	.ndo_do_ioctl		= qeth_do_ioctl,
1893 	.ndo_fix_features	= qeth_fix_features,
1894 	.ndo_set_features	= qeth_set_features,
1895 	.ndo_vlan_rx_add_vid	= qeth_l3_vlan_rx_add_vid,
1896 	.ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
1897 	.ndo_tx_timeout		= qeth_tx_timeout,
1898 };
1899 
1900 static const struct net_device_ops qeth_l3_osa_netdev_ops = {
1901 	.ndo_open		= qeth_open,
1902 	.ndo_stop		= qeth_stop,
1903 	.ndo_get_stats64	= qeth_get_stats64,
1904 	.ndo_start_xmit		= qeth_l3_hard_start_xmit,
1905 	.ndo_features_check	= qeth_l3_osa_features_check,
1906 	.ndo_select_queue	= qeth_l3_osa_select_queue,
1907 	.ndo_validate_addr	= eth_validate_addr,
1908 	.ndo_set_rx_mode	= qeth_l3_set_rx_mode,
1909 	.ndo_do_ioctl		= qeth_do_ioctl,
1910 	.ndo_fix_features	= qeth_fix_features,
1911 	.ndo_set_features	= qeth_set_features,
1912 	.ndo_vlan_rx_add_vid	= qeth_l3_vlan_rx_add_vid,
1913 	.ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
1914 	.ndo_tx_timeout		= qeth_tx_timeout,
1915 	.ndo_neigh_setup	= qeth_l3_neigh_setup,
1916 };
1917 
1918 static int qeth_l3_setup_netdev(struct qeth_card *card)
1919 {
1920 	struct net_device *dev = card->dev;
1921 	unsigned int headroom;
1922 	int rc;
1923 
1924 	rc = qeth_setup_netdev(card);
1925 	if (rc)
1926 		return rc;
1927 
1928 	if (IS_OSD(card) || IS_OSX(card)) {
1929 		if ((card->info.link_type == QETH_LINK_TYPE_LANE_TR) ||
1930 		    (card->info.link_type == QETH_LINK_TYPE_HSTR)) {
1931 			pr_info("qeth_l3: ignoring TR device\n");
1932 			return -ENODEV;
1933 		}
1934 
1935 		card->dev->netdev_ops = &qeth_l3_osa_netdev_ops;
1936 
1937 		/*IPv6 address autoconfiguration stuff*/
1938 		dev->dev_id = qeth_l3_get_unique_id(card, dev->dev_id);
1939 
1940 		if (!IS_VM_NIC(card)) {
1941 			card->dev->features |= NETIF_F_SG;
1942 			card->dev->hw_features |= NETIF_F_TSO |
1943 				NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
1944 			card->dev->vlan_features |= NETIF_F_TSO |
1945 				NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
1946 		}
1947 
1948 		if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) {
1949 			card->dev->hw_features |= NETIF_F_IPV6_CSUM;
1950 			card->dev->vlan_features |= NETIF_F_IPV6_CSUM;
1951 		}
1952 		if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) {
1953 			card->dev->hw_features |= NETIF_F_TSO6;
1954 			card->dev->vlan_features |= NETIF_F_TSO6;
1955 		}
1956 
1957 		/* allow for de-acceleration of NETIF_F_HW_VLAN_CTAG_TX: */
1958 		if (card->dev->hw_features & NETIF_F_TSO6)
1959 			headroom = sizeof(struct qeth_hdr_tso) + VLAN_HLEN;
1960 		else if (card->dev->hw_features & NETIF_F_TSO)
1961 			headroom = sizeof(struct qeth_hdr_tso);
1962 		else
1963 			headroom = sizeof(struct qeth_hdr) + VLAN_HLEN;
1964 	} else if (IS_IQD(card)) {
1965 		card->dev->flags |= IFF_NOARP;
1966 		card->dev->netdev_ops = &qeth_l3_netdev_ops;
1967 		headroom = sizeof(struct qeth_hdr) - ETH_HLEN;
1968 
1969 		rc = qeth_l3_iqd_read_initial_mac(card);
1970 		if (rc)
1971 			return rc;
1972 	} else
1973 		return -ENODEV;
1974 
1975 	card->dev->needed_headroom = headroom;
1976 	card->dev->features |=	NETIF_F_HW_VLAN_CTAG_TX |
1977 				NETIF_F_HW_VLAN_CTAG_RX |
1978 				NETIF_F_HW_VLAN_CTAG_FILTER;
1979 
1980 	netif_keep_dst(card->dev);
1981 	if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6))
1982 		netif_set_gso_max_size(card->dev,
1983 				       PAGE_SIZE * (QETH_MAX_BUFFER_ELEMENTS(card) - 1));
1984 
1985 	netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
1986 	return register_netdev(card->dev);
1987 }
1988 
1989 static const struct device_type qeth_l3_devtype = {
1990 	.name = "qeth_layer3",
1991 	.groups = qeth_l3_attr_groups,
1992 };
1993 
1994 static int qeth_l3_probe_device(struct ccwgroup_device *gdev)
1995 {
1996 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1997 	int rc;
1998 
1999 	hash_init(card->ip_htable);
2000 	mutex_init(&card->ip_lock);
2001 	card->cmd_wq = alloc_ordered_workqueue("%s_cmd", 0,
2002 					       dev_name(&gdev->dev));
2003 	if (!card->cmd_wq)
2004 		return -ENOMEM;
2005 
2006 	if (gdev->dev.type == &qeth_generic_devtype) {
2007 		rc = qeth_l3_create_device_attributes(&gdev->dev);
2008 		if (rc) {
2009 			destroy_workqueue(card->cmd_wq);
2010 			return rc;
2011 		}
2012 	}
2013 
2014 	hash_init(card->ip_mc_htable);
2015 	INIT_WORK(&card->rx_mode_work, qeth_l3_rx_mode_work);
2016 	return 0;
2017 }
2018 
2019 static void qeth_l3_remove_device(struct ccwgroup_device *cgdev)
2020 {
2021 	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
2022 
2023 	if (cgdev->dev.type == &qeth_generic_devtype)
2024 		qeth_l3_remove_device_attributes(&cgdev->dev);
2025 
2026 	qeth_set_allowed_threads(card, 0, 1);
2027 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
2028 
2029 	if (cgdev->state == CCWGROUP_ONLINE)
2030 		qeth_set_offline(card, false);
2031 
2032 	cancel_work_sync(&card->close_dev_work);
2033 	if (card->dev->reg_state == NETREG_REGISTERED)
2034 		unregister_netdev(card->dev);
2035 
2036 	flush_workqueue(card->cmd_wq);
2037 	destroy_workqueue(card->cmd_wq);
2038 	qeth_l3_clear_ip_htable(card, 0);
2039 	qeth_l3_clear_ipato_list(card);
2040 }
2041 
2042 static int qeth_l3_set_online(struct qeth_card *card)
2043 {
2044 	struct ccwgroup_device *gdev = card->gdev;
2045 	struct net_device *dev = card->dev;
2046 	int rc = 0;
2047 	bool carrier_ok;
2048 
2049 	rc = qeth_core_hardsetup_card(card, &carrier_ok);
2050 	if (rc) {
2051 		QETH_CARD_TEXT_(card, 2, "2err%04x", rc);
2052 		rc = -ENODEV;
2053 		goto out_remove;
2054 	}
2055 
2056 	qeth_print_status_message(card);
2057 
2058 	/* softsetup */
2059 	QETH_CARD_TEXT(card, 2, "softsetp");
2060 
2061 	rc = qeth_l3_setadapter_parms(card);
2062 	if (rc)
2063 		QETH_CARD_TEXT_(card, 2, "2err%04x", rc);
2064 	if (!card->options.sniffer) {
2065 		qeth_l3_start_ipassists(card);
2066 
2067 		rc = qeth_l3_setrouting_v4(card);
2068 		if (rc)
2069 			QETH_CARD_TEXT_(card, 2, "4err%04x", rc);
2070 		rc = qeth_l3_setrouting_v6(card);
2071 		if (rc)
2072 			QETH_CARD_TEXT_(card, 2, "5err%04x", rc);
2073 	}
2074 
2075 	card->state = CARD_STATE_SOFTSETUP;
2076 
2077 	qeth_set_allowed_threads(card, 0xffffffff, 0);
2078 	qeth_l3_recover_ip(card);
2079 
2080 	if (dev->reg_state != NETREG_REGISTERED) {
2081 		rc = qeth_l3_setup_netdev(card);
2082 		if (rc)
2083 			goto out_remove;
2084 
2085 		if (carrier_ok)
2086 			netif_carrier_on(dev);
2087 	} else {
2088 		rtnl_lock();
2089 		if (carrier_ok)
2090 			netif_carrier_on(dev);
2091 		else
2092 			netif_carrier_off(dev);
2093 
2094 		netif_device_attach(dev);
2095 		qeth_enable_hw_features(dev);
2096 
2097 		if (card->info.open_when_online) {
2098 			card->info.open_when_online = 0;
2099 			dev_open(dev, NULL);
2100 		}
2101 		rtnl_unlock();
2102 	}
2103 	qeth_trace_features(card);
2104 	/* let user_space know that device is online */
2105 	kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
2106 	return 0;
2107 out_remove:
2108 	qeth_l3_stop_card(card);
2109 	qeth_stop_channel(&card->data);
2110 	qeth_stop_channel(&card->write);
2111 	qeth_stop_channel(&card->read);
2112 	qdio_free(CARD_DDEV(card));
2113 	return rc;
2114 }
2115 
2116 static void qeth_l3_set_offline(struct qeth_card *card)
2117 {
2118 	qeth_l3_stop_card(card);
2119 }
2120 
2121 /* Returns zero if the command is successfully "consumed" */
2122 static int qeth_l3_control_event(struct qeth_card *card,
2123 					struct qeth_ipa_cmd *cmd)
2124 {
2125 	return 1;
2126 }
2127 
2128 struct qeth_discipline qeth_l3_discipline = {
2129 	.devtype = &qeth_l3_devtype,
2130 	.setup = qeth_l3_probe_device,
2131 	.remove = qeth_l3_remove_device,
2132 	.set_online = qeth_l3_set_online,
2133 	.set_offline = qeth_l3_set_offline,
2134 	.do_ioctl = qeth_l3_do_ioctl,
2135 	.control_event_handler = qeth_l3_control_event,
2136 };
2137 EXPORT_SYMBOL_GPL(qeth_l3_discipline);
2138 
2139 static int qeth_l3_handle_ip_event(struct qeth_card *card,
2140 				   struct qeth_ipaddr *addr,
2141 				   unsigned long event)
2142 {
2143 	switch (event) {
2144 	case NETDEV_UP:
2145 		qeth_l3_modify_ip(card, addr, true);
2146 		return NOTIFY_OK;
2147 	case NETDEV_DOWN:
2148 		qeth_l3_modify_ip(card, addr, false);
2149 		return NOTIFY_OK;
2150 	default:
2151 		return NOTIFY_DONE;
2152 	}
2153 }
2154 
2155 struct qeth_l3_ip_event_work {
2156 	struct work_struct work;
2157 	struct qeth_card *card;
2158 	struct qeth_ipaddr addr;
2159 };
2160 
2161 #define to_ip_work(w) container_of((w), struct qeth_l3_ip_event_work, work)
2162 
2163 static void qeth_l3_add_ip_worker(struct work_struct *work)
2164 {
2165 	struct qeth_l3_ip_event_work *ip_work = to_ip_work(work);
2166 
2167 	qeth_l3_modify_ip(ip_work->card, &ip_work->addr, true);
2168 	kfree(work);
2169 }
2170 
2171 static void qeth_l3_delete_ip_worker(struct work_struct *work)
2172 {
2173 	struct qeth_l3_ip_event_work *ip_work = to_ip_work(work);
2174 
2175 	qeth_l3_modify_ip(ip_work->card, &ip_work->addr, false);
2176 	kfree(work);
2177 }
2178 
2179 static struct qeth_card *qeth_l3_get_card_from_dev(struct net_device *dev)
2180 {
2181 	if (is_vlan_dev(dev))
2182 		dev = vlan_dev_real_dev(dev);
2183 	if (dev->netdev_ops == &qeth_l3_osa_netdev_ops ||
2184 	    dev->netdev_ops == &qeth_l3_netdev_ops)
2185 		return (struct qeth_card *) dev->ml_priv;
2186 	return NULL;
2187 }
2188 
2189 static int qeth_l3_ip_event(struct notifier_block *this,
2190 			    unsigned long event, void *ptr)
2191 {
2192 
2193 	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2194 	struct net_device *dev = ifa->ifa_dev->dev;
2195 	struct qeth_ipaddr addr;
2196 	struct qeth_card *card;
2197 
2198 	card = qeth_l3_get_card_from_dev(dev);
2199 	if (!card)
2200 		return NOTIFY_DONE;
2201 	QETH_CARD_TEXT(card, 3, "ipevent");
2202 
2203 	qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4);
2204 	addr.u.a4.addr = ifa->ifa_address;
2205 	addr.u.a4.mask = ifa->ifa_mask;
2206 
2207 	return qeth_l3_handle_ip_event(card, &addr, event);
2208 }
2209 
2210 static struct notifier_block qeth_l3_ip_notifier = {
2211 	qeth_l3_ip_event,
2212 	NULL,
2213 };
2214 
2215 static int qeth_l3_ip6_event(struct notifier_block *this,
2216 			     unsigned long event, void *ptr)
2217 {
2218 	struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
2219 	struct net_device *dev = ifa->idev->dev;
2220 	struct qeth_l3_ip_event_work *ip_work;
2221 	struct qeth_card *card;
2222 
2223 	if (event != NETDEV_UP && event != NETDEV_DOWN)
2224 		return NOTIFY_DONE;
2225 
2226 	card = qeth_l3_get_card_from_dev(dev);
2227 	if (!card)
2228 		return NOTIFY_DONE;
2229 	QETH_CARD_TEXT(card, 3, "ip6event");
2230 	if (!qeth_is_supported(card, IPA_IPV6))
2231 		return NOTIFY_DONE;
2232 
2233 	ip_work = kmalloc(sizeof(*ip_work), GFP_ATOMIC);
2234 	if (!ip_work)
2235 		return NOTIFY_DONE;
2236 
2237 	if (event == NETDEV_UP)
2238 		INIT_WORK(&ip_work->work, qeth_l3_add_ip_worker);
2239 	else
2240 		INIT_WORK(&ip_work->work, qeth_l3_delete_ip_worker);
2241 
2242 	ip_work->card = card;
2243 	qeth_l3_init_ipaddr(&ip_work->addr, QETH_IP_TYPE_NORMAL,
2244 			    QETH_PROT_IPV6);
2245 	ip_work->addr.u.a6.addr = ifa->addr;
2246 	ip_work->addr.u.a6.pfxlen = ifa->prefix_len;
2247 
2248 	queue_work(card->cmd_wq, &ip_work->work);
2249 	return NOTIFY_OK;
2250 }
2251 
2252 static struct notifier_block qeth_l3_ip6_notifier = {
2253 	qeth_l3_ip6_event,
2254 	NULL,
2255 };
2256 
2257 static int qeth_l3_register_notifiers(void)
2258 {
2259 	int rc;
2260 
2261 	QETH_DBF_TEXT(SETUP, 5, "regnotif");
2262 	rc = register_inetaddr_notifier(&qeth_l3_ip_notifier);
2263 	if (rc)
2264 		return rc;
2265 	rc = register_inet6addr_notifier(&qeth_l3_ip6_notifier);
2266 	if (rc) {
2267 		unregister_inetaddr_notifier(&qeth_l3_ip_notifier);
2268 		return rc;
2269 	}
2270 	return 0;
2271 }
2272 
2273 static void qeth_l3_unregister_notifiers(void)
2274 {
2275 	QETH_DBF_TEXT(SETUP, 5, "unregnot");
2276 	WARN_ON(unregister_inetaddr_notifier(&qeth_l3_ip_notifier));
2277 	WARN_ON(unregister_inet6addr_notifier(&qeth_l3_ip6_notifier));
2278 }
2279 
2280 static int __init qeth_l3_init(void)
2281 {
2282 	pr_info("register layer 3 discipline\n");
2283 	return qeth_l3_register_notifiers();
2284 }
2285 
2286 static void __exit qeth_l3_exit(void)
2287 {
2288 	qeth_l3_unregister_notifiers();
2289 	pr_info("unregister layer 3 discipline\n");
2290 }
2291 
2292 module_init(qeth_l3_init);
2293 module_exit(qeth_l3_exit);
2294 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
2295 MODULE_DESCRIPTION("qeth layer 3 discipline");
2296 MODULE_LICENSE("GPL");
2297