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