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