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