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