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