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