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