xref: /linux/drivers/s390/net/qeth_l3_main.c (revision 64b14a184e83eb62ea0615e31a409956049d40e7)
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 	eth_hw_addr_set(card->dev, 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_ndo_siocdevprivate(struct net_device *dev, struct ifreq *rq,
1515 				      void __user *data, 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, 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, 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 = qeth_siocdevprivate(dev, rq, data, cmd);
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 const struct net_device_ops qeth_l3_netdev_ops = {
1826 	.ndo_open		= qeth_open,
1827 	.ndo_stop		= qeth_stop,
1828 	.ndo_get_stats64	= qeth_get_stats64,
1829 	.ndo_start_xmit		= qeth_l3_hard_start_xmit,
1830 	.ndo_select_queue	= qeth_l3_iqd_select_queue,
1831 	.ndo_validate_addr	= eth_validate_addr,
1832 	.ndo_set_rx_mode	= qeth_l3_set_rx_mode,
1833 	.ndo_eth_ioctl		= qeth_do_ioctl,
1834 	.ndo_siocdevprivate	= qeth_l3_ndo_siocdevprivate,
1835 	.ndo_fix_features	= qeth_fix_features,
1836 	.ndo_set_features	= qeth_set_features,
1837 	.ndo_tx_timeout		= qeth_tx_timeout,
1838 };
1839 
1840 static const struct net_device_ops qeth_l3_osa_netdev_ops = {
1841 	.ndo_open		= qeth_open,
1842 	.ndo_stop		= qeth_stop,
1843 	.ndo_get_stats64	= qeth_get_stats64,
1844 	.ndo_start_xmit		= qeth_l3_hard_start_xmit,
1845 	.ndo_features_check	= qeth_l3_osa_features_check,
1846 	.ndo_select_queue	= qeth_osa_select_queue,
1847 	.ndo_validate_addr	= eth_validate_addr,
1848 	.ndo_set_rx_mode	= qeth_l3_set_rx_mode,
1849 	.ndo_eth_ioctl		= qeth_do_ioctl,
1850 	.ndo_siocdevprivate	= qeth_l3_ndo_siocdevprivate,
1851 	.ndo_fix_features	= qeth_fix_features,
1852 	.ndo_set_features	= qeth_set_features,
1853 	.ndo_tx_timeout		= qeth_tx_timeout,
1854 	.ndo_neigh_setup	= qeth_l3_neigh_setup,
1855 };
1856 
1857 static int qeth_l3_setup_netdev(struct qeth_card *card)
1858 {
1859 	struct net_device *dev = card->dev;
1860 	unsigned int headroom;
1861 	int rc;
1862 
1863 	if (IS_OSD(card) || IS_OSX(card)) {
1864 		card->dev->netdev_ops = &qeth_l3_osa_netdev_ops;
1865 
1866 		/*IPv6 address autoconfiguration stuff*/
1867 		dev->dev_id = qeth_l3_get_unique_id(card, dev->dev_id);
1868 
1869 		if (!IS_VM_NIC(card)) {
1870 			card->dev->features |= NETIF_F_SG;
1871 			card->dev->hw_features |= NETIF_F_TSO |
1872 				NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
1873 			card->dev->vlan_features |= NETIF_F_TSO |
1874 				NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
1875 		}
1876 
1877 		if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) {
1878 			card->dev->hw_features |= NETIF_F_IPV6_CSUM;
1879 			card->dev->vlan_features |= NETIF_F_IPV6_CSUM;
1880 		}
1881 		if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) {
1882 			card->dev->hw_features |= NETIF_F_TSO6;
1883 			card->dev->vlan_features |= NETIF_F_TSO6;
1884 		}
1885 
1886 		/* allow for de-acceleration of NETIF_F_HW_VLAN_CTAG_TX: */
1887 		if (card->dev->hw_features & NETIF_F_TSO6)
1888 			headroom = sizeof(struct qeth_hdr_tso) + VLAN_HLEN;
1889 		else if (card->dev->hw_features & NETIF_F_TSO)
1890 			headroom = sizeof(struct qeth_hdr_tso);
1891 		else
1892 			headroom = sizeof(struct qeth_hdr) + VLAN_HLEN;
1893 	} else if (IS_IQD(card)) {
1894 		card->dev->flags |= IFF_NOARP;
1895 		card->dev->netdev_ops = &qeth_l3_netdev_ops;
1896 		headroom = sizeof(struct qeth_hdr) - ETH_HLEN;
1897 
1898 		rc = qeth_l3_iqd_read_initial_mac(card);
1899 		if (rc)
1900 			return rc;
1901 	} else
1902 		return -ENODEV;
1903 
1904 	card->dev->needed_headroom = headroom;
1905 	card->dev->features |=	NETIF_F_HW_VLAN_CTAG_TX |
1906 				NETIF_F_HW_VLAN_CTAG_RX;
1907 
1908 	netif_keep_dst(card->dev);
1909 	if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6))
1910 		netif_set_gso_max_size(card->dev,
1911 				       PAGE_SIZE * (QETH_MAX_BUFFER_ELEMENTS(card) - 1));
1912 
1913 	netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
1914 	return register_netdev(card->dev);
1915 }
1916 
1917 static const struct device_type qeth_l3_devtype = {
1918 	.name = "qeth_layer3",
1919 	.groups = qeth_l3_attr_groups,
1920 };
1921 
1922 static int qeth_l3_probe_device(struct ccwgroup_device *gdev)
1923 {
1924 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
1925 	int rc;
1926 
1927 	hash_init(card->ip_htable);
1928 	mutex_init(&card->ip_lock);
1929 	card->cmd_wq = alloc_ordered_workqueue("%s_cmd", 0,
1930 					       dev_name(&gdev->dev));
1931 	if (!card->cmd_wq)
1932 		return -ENOMEM;
1933 
1934 	if (gdev->dev.type) {
1935 		rc = device_add_groups(&gdev->dev, qeth_l3_attr_groups);
1936 		if (rc) {
1937 			destroy_workqueue(card->cmd_wq);
1938 			return rc;
1939 		}
1940 	} else {
1941 		gdev->dev.type = &qeth_l3_devtype;
1942 	}
1943 
1944 	INIT_WORK(&card->rx_mode_work, qeth_l3_rx_mode_work);
1945 	return 0;
1946 }
1947 
1948 static void qeth_l3_remove_device(struct ccwgroup_device *cgdev)
1949 {
1950 	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
1951 
1952 	if (cgdev->dev.type != &qeth_l3_devtype)
1953 		device_remove_groups(&cgdev->dev, qeth_l3_attr_groups);
1954 
1955 	qeth_set_allowed_threads(card, 0, 1);
1956 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
1957 
1958 	if (cgdev->state == CCWGROUP_ONLINE)
1959 		qeth_set_offline(card, card->discipline, false);
1960 
1961 	if (card->dev->reg_state == NETREG_REGISTERED)
1962 		unregister_netdev(card->dev);
1963 
1964 	flush_workqueue(card->cmd_wq);
1965 	destroy_workqueue(card->cmd_wq);
1966 	qeth_l3_clear_ip_htable(card, 0);
1967 	qeth_l3_clear_ipato_list(card);
1968 }
1969 
1970 static int qeth_l3_set_online(struct qeth_card *card, bool carrier_ok)
1971 {
1972 	struct net_device *dev = card->dev;
1973 	int rc = 0;
1974 
1975 	/* softsetup */
1976 	QETH_CARD_TEXT(card, 2, "softsetp");
1977 
1978 	rc = qeth_l3_setadapter_parms(card);
1979 	if (rc)
1980 		QETH_CARD_TEXT_(card, 2, "2err%04x", rc);
1981 	if (!card->options.sniffer) {
1982 		qeth_l3_start_ipassists(card);
1983 
1984 		rc = qeth_l3_setrouting_v4(card);
1985 		if (rc)
1986 			QETH_CARD_TEXT_(card, 2, "4err%04x", rc);
1987 		rc = qeth_l3_setrouting_v6(card);
1988 		if (rc)
1989 			QETH_CARD_TEXT_(card, 2, "5err%04x", rc);
1990 	}
1991 
1992 	card->state = CARD_STATE_SOFTSETUP;
1993 
1994 	qeth_set_allowed_threads(card, 0xffffffff, 0);
1995 	qeth_l3_recover_ip(card);
1996 
1997 	if (dev->reg_state != NETREG_REGISTERED) {
1998 		rc = qeth_l3_setup_netdev(card);
1999 		if (rc)
2000 			goto err_setup;
2001 
2002 		if (carrier_ok)
2003 			netif_carrier_on(dev);
2004 	} else {
2005 		rtnl_lock();
2006 		rc = qeth_set_real_num_tx_queues(card,
2007 						 qeth_tx_actual_queues(card));
2008 		if (rc) {
2009 			rtnl_unlock();
2010 			goto err_set_queues;
2011 		}
2012 
2013 		if (carrier_ok)
2014 			netif_carrier_on(dev);
2015 		else
2016 			netif_carrier_off(dev);
2017 
2018 		netif_device_attach(dev);
2019 		qeth_enable_hw_features(dev);
2020 
2021 		if (card->info.open_when_online) {
2022 			card->info.open_when_online = 0;
2023 			dev_open(dev, NULL);
2024 		}
2025 		rtnl_unlock();
2026 	}
2027 	return 0;
2028 
2029 err_set_queues:
2030 err_setup:
2031 	qeth_set_allowed_threads(card, 0, 1);
2032 	card->state = CARD_STATE_DOWN;
2033 	qeth_l3_clear_ip_htable(card, 1);
2034 	return rc;
2035 }
2036 
2037 static void qeth_l3_set_offline(struct qeth_card *card)
2038 {
2039 	qeth_set_allowed_threads(card, 0, 1);
2040 	qeth_l3_drain_rx_mode_cache(card);
2041 
2042 	if (card->options.sniffer &&
2043 	    (card->info.promisc_mode == SET_PROMISC_MODE_ON))
2044 		qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
2045 
2046 	if (card->state == CARD_STATE_SOFTSETUP) {
2047 		card->state = CARD_STATE_DOWN;
2048 		qeth_l3_clear_ip_htable(card, 1);
2049 	}
2050 }
2051 
2052 /* Returns zero if the command is successfully "consumed" */
2053 static int qeth_l3_control_event(struct qeth_card *card,
2054 					struct qeth_ipa_cmd *cmd)
2055 {
2056 	return 1;
2057 }
2058 
2059 const struct qeth_discipline qeth_l3_discipline = {
2060 	.setup = qeth_l3_probe_device,
2061 	.remove = qeth_l3_remove_device,
2062 	.set_online = qeth_l3_set_online,
2063 	.set_offline = qeth_l3_set_offline,
2064 	.control_event_handler = qeth_l3_control_event,
2065 };
2066 EXPORT_SYMBOL_GPL(qeth_l3_discipline);
2067 
2068 static int qeth_l3_handle_ip_event(struct qeth_card *card,
2069 				   struct qeth_ipaddr *addr,
2070 				   unsigned long event)
2071 {
2072 	switch (event) {
2073 	case NETDEV_UP:
2074 		qeth_l3_modify_ip(card, addr, true);
2075 		return NOTIFY_OK;
2076 	case NETDEV_DOWN:
2077 		qeth_l3_modify_ip(card, addr, false);
2078 		return NOTIFY_OK;
2079 	default:
2080 		return NOTIFY_DONE;
2081 	}
2082 }
2083 
2084 struct qeth_l3_ip_event_work {
2085 	struct work_struct work;
2086 	struct qeth_card *card;
2087 	struct qeth_ipaddr addr;
2088 };
2089 
2090 #define to_ip_work(w) container_of((w), struct qeth_l3_ip_event_work, work)
2091 
2092 static void qeth_l3_add_ip_worker(struct work_struct *work)
2093 {
2094 	struct qeth_l3_ip_event_work *ip_work = to_ip_work(work);
2095 
2096 	qeth_l3_modify_ip(ip_work->card, &ip_work->addr, true);
2097 	kfree(work);
2098 }
2099 
2100 static void qeth_l3_delete_ip_worker(struct work_struct *work)
2101 {
2102 	struct qeth_l3_ip_event_work *ip_work = to_ip_work(work);
2103 
2104 	qeth_l3_modify_ip(ip_work->card, &ip_work->addr, false);
2105 	kfree(work);
2106 }
2107 
2108 static struct qeth_card *qeth_l3_get_card_from_dev(struct net_device *dev)
2109 {
2110 	if (is_vlan_dev(dev))
2111 		dev = vlan_dev_real_dev(dev);
2112 	if (dev->netdev_ops == &qeth_l3_osa_netdev_ops ||
2113 	    dev->netdev_ops == &qeth_l3_netdev_ops)
2114 		return (struct qeth_card *) dev->ml_priv;
2115 	return NULL;
2116 }
2117 
2118 static int qeth_l3_ip_event(struct notifier_block *this,
2119 			    unsigned long event, void *ptr)
2120 {
2121 	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2122 	struct net_device *dev = ifa->ifa_dev->dev;
2123 	struct qeth_ipaddr addr;
2124 	struct qeth_card *card;
2125 
2126 	card = qeth_l3_get_card_from_dev(dev);
2127 	if (!card)
2128 		return NOTIFY_DONE;
2129 	QETH_CARD_TEXT(card, 3, "ipevent");
2130 
2131 	qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4);
2132 	addr.u.a4.addr = ifa->ifa_address;
2133 	addr.u.a4.mask = ifa->ifa_mask;
2134 
2135 	return qeth_l3_handle_ip_event(card, &addr, event);
2136 }
2137 
2138 static struct notifier_block qeth_l3_ip_notifier = {
2139 	qeth_l3_ip_event,
2140 	NULL,
2141 };
2142 
2143 static int qeth_l3_ip6_event(struct notifier_block *this,
2144 			     unsigned long event, void *ptr)
2145 {
2146 	struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
2147 	struct net_device *dev = ifa->idev->dev;
2148 	struct qeth_l3_ip_event_work *ip_work;
2149 	struct qeth_card *card;
2150 
2151 	if (event != NETDEV_UP && event != NETDEV_DOWN)
2152 		return NOTIFY_DONE;
2153 
2154 	card = qeth_l3_get_card_from_dev(dev);
2155 	if (!card)
2156 		return NOTIFY_DONE;
2157 	QETH_CARD_TEXT(card, 3, "ip6event");
2158 	if (!qeth_is_supported(card, IPA_IPV6))
2159 		return NOTIFY_DONE;
2160 
2161 	ip_work = kmalloc(sizeof(*ip_work), GFP_ATOMIC);
2162 	if (!ip_work)
2163 		return NOTIFY_DONE;
2164 
2165 	if (event == NETDEV_UP)
2166 		INIT_WORK(&ip_work->work, qeth_l3_add_ip_worker);
2167 	else
2168 		INIT_WORK(&ip_work->work, qeth_l3_delete_ip_worker);
2169 
2170 	ip_work->card = card;
2171 	qeth_l3_init_ipaddr(&ip_work->addr, QETH_IP_TYPE_NORMAL,
2172 			    QETH_PROT_IPV6);
2173 	ip_work->addr.u.a6.addr = ifa->addr;
2174 	ip_work->addr.u.a6.pfxlen = ifa->prefix_len;
2175 
2176 	queue_work(card->cmd_wq, &ip_work->work);
2177 	return NOTIFY_OK;
2178 }
2179 
2180 static struct notifier_block qeth_l3_ip6_notifier = {
2181 	qeth_l3_ip6_event,
2182 	NULL,
2183 };
2184 
2185 static int qeth_l3_register_notifiers(void)
2186 {
2187 	int rc;
2188 
2189 	QETH_DBF_TEXT(SETUP, 5, "regnotif");
2190 	rc = register_inetaddr_notifier(&qeth_l3_ip_notifier);
2191 	if (rc)
2192 		return rc;
2193 	rc = register_inet6addr_notifier(&qeth_l3_ip6_notifier);
2194 	if (rc) {
2195 		unregister_inetaddr_notifier(&qeth_l3_ip_notifier);
2196 		return rc;
2197 	}
2198 	return 0;
2199 }
2200 
2201 static void qeth_l3_unregister_notifiers(void)
2202 {
2203 	QETH_DBF_TEXT(SETUP, 5, "unregnot");
2204 	WARN_ON(unregister_inetaddr_notifier(&qeth_l3_ip_notifier));
2205 	WARN_ON(unregister_inet6addr_notifier(&qeth_l3_ip6_notifier));
2206 }
2207 
2208 static int __init qeth_l3_init(void)
2209 {
2210 	pr_info("register layer 3 discipline\n");
2211 	return qeth_l3_register_notifiers();
2212 }
2213 
2214 static void __exit qeth_l3_exit(void)
2215 {
2216 	qeth_l3_unregister_notifiers();
2217 	pr_info("unregister layer 3 discipline\n");
2218 }
2219 
2220 module_init(qeth_l3_init);
2221 module_exit(qeth_l3_exit);
2222 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
2223 MODULE_DESCRIPTION("qeth layer 3 discipline");
2224 MODULE_LICENSE("GPL");
2225