xref: /openbmc/linux/drivers/s390/net/qeth_l3_main.c (revision bc05aa6e)
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/ip6_fib.h>
36 #include <net/ip6_checksum.h>
37 #include <net/iucv/af_iucv.h>
38 #include <linux/hashtable.h>
39 
40 #include "qeth_l3.h"
41 
42 
43 static int qeth_l3_set_offline(struct ccwgroup_device *);
44 static int qeth_l3_stop(struct net_device *);
45 static void qeth_l3_set_rx_mode(struct net_device *dev);
46 static int qeth_l3_register_addr_entry(struct qeth_card *,
47 		struct qeth_ipaddr *);
48 static int qeth_l3_deregister_addr_entry(struct qeth_card *,
49 		struct qeth_ipaddr *);
50 
51 static void qeth_l3_ipaddr4_to_string(const __u8 *addr, char *buf)
52 {
53 	sprintf(buf, "%pI4", addr);
54 }
55 
56 static void qeth_l3_ipaddr6_to_string(const __u8 *addr, char *buf)
57 {
58 	sprintf(buf, "%pI6", addr);
59 }
60 
61 void qeth_l3_ipaddr_to_string(enum qeth_prot_versions proto, const __u8 *addr,
62 				char *buf)
63 {
64 	if (proto == QETH_PROT_IPV4)
65 		qeth_l3_ipaddr4_to_string(addr, buf);
66 	else if (proto == QETH_PROT_IPV6)
67 		qeth_l3_ipaddr6_to_string(addr, buf);
68 }
69 
70 static struct qeth_ipaddr *qeth_l3_find_addr_by_ip(struct qeth_card *card,
71 						   struct qeth_ipaddr *query)
72 {
73 	u64 key = qeth_l3_ipaddr_hash(query);
74 	struct qeth_ipaddr *addr;
75 
76 	if (query->is_multicast) {
77 		hash_for_each_possible(card->ip_mc_htable, addr, hnode, key)
78 			if (qeth_l3_addr_match_ip(addr, query))
79 				return addr;
80 	} else {
81 		hash_for_each_possible(card->ip_htable,  addr, hnode, key)
82 			if (qeth_l3_addr_match_ip(addr, query))
83 				return addr;
84 	}
85 	return NULL;
86 }
87 
88 static void qeth_l3_convert_addr_to_bits(u8 *addr, u8 *bits, int len)
89 {
90 	int i, j;
91 	u8 octet;
92 
93 	for (i = 0; i < len; ++i) {
94 		octet = addr[i];
95 		for (j = 7; j >= 0; --j) {
96 			bits[i*8 + j] = octet & 1;
97 			octet >>= 1;
98 		}
99 	}
100 }
101 
102 static bool qeth_l3_is_addr_covered_by_ipato(struct qeth_card *card,
103 					     struct qeth_ipaddr *addr)
104 {
105 	struct qeth_ipato_entry *ipatoe;
106 	u8 addr_bits[128] = {0, };
107 	u8 ipatoe_bits[128] = {0, };
108 	int rc = 0;
109 
110 	if (!card->ipato.enabled)
111 		return 0;
112 	if (addr->type != QETH_IP_TYPE_NORMAL)
113 		return 0;
114 
115 	qeth_l3_convert_addr_to_bits((u8 *) &addr->u, addr_bits,
116 				  (addr->proto == QETH_PROT_IPV4)? 4:16);
117 	list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
118 		if (addr->proto != ipatoe->proto)
119 			continue;
120 		qeth_l3_convert_addr_to_bits(ipatoe->addr, ipatoe_bits,
121 					  (ipatoe->proto == QETH_PROT_IPV4) ?
122 					  4 : 16);
123 		if (addr->proto == QETH_PROT_IPV4)
124 			rc = !memcmp(addr_bits, ipatoe_bits,
125 				     min(32, ipatoe->mask_bits));
126 		else
127 			rc = !memcmp(addr_bits, ipatoe_bits,
128 				     min(128, ipatoe->mask_bits));
129 		if (rc)
130 			break;
131 	}
132 	/* invert? */
133 	if ((addr->proto == QETH_PROT_IPV4) && card->ipato.invert4)
134 		rc = !rc;
135 	else if ((addr->proto == QETH_PROT_IPV6) && card->ipato.invert6)
136 		rc = !rc;
137 
138 	return rc;
139 }
140 
141 int qeth_l3_delete_ip(struct qeth_card *card, struct qeth_ipaddr *tmp_addr)
142 {
143 	int rc = 0;
144 	struct qeth_ipaddr *addr;
145 
146 	QETH_CARD_TEXT(card, 4, "delip");
147 
148 	if (tmp_addr->proto == QETH_PROT_IPV4)
149 		QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
150 	else {
151 		QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
152 		QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
153 	}
154 
155 	addr = qeth_l3_find_addr_by_ip(card, tmp_addr);
156 	if (!addr || !qeth_l3_addr_match_all(addr, tmp_addr))
157 		return -ENOENT;
158 
159 	addr->ref_counter--;
160 	if (addr->type == QETH_IP_TYPE_NORMAL && addr->ref_counter > 0)
161 		return rc;
162 	if (addr->in_progress)
163 		return -EINPROGRESS;
164 
165 	if (qeth_card_hw_is_reachable(card))
166 		rc = qeth_l3_deregister_addr_entry(card, addr);
167 
168 	hash_del(&addr->hnode);
169 	kfree(addr);
170 
171 	return rc;
172 }
173 
174 int qeth_l3_add_ip(struct qeth_card *card, struct qeth_ipaddr *tmp_addr)
175 {
176 	int rc = 0;
177 	struct qeth_ipaddr *addr;
178 	char buf[40];
179 
180 	QETH_CARD_TEXT(card, 4, "addip");
181 
182 	if (tmp_addr->proto == QETH_PROT_IPV4)
183 		QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
184 	else {
185 		QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
186 		QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
187 	}
188 
189 	addr = qeth_l3_find_addr_by_ip(card, tmp_addr);
190 	if (addr) {
191 		if (tmp_addr->type != QETH_IP_TYPE_NORMAL)
192 			return -EADDRINUSE;
193 		if (qeth_l3_addr_match_all(addr, tmp_addr)) {
194 			addr->ref_counter++;
195 			return 0;
196 		}
197 		qeth_l3_ipaddr_to_string(tmp_addr->proto, (u8 *)&tmp_addr->u,
198 					 buf);
199 		dev_warn(&card->gdev->dev,
200 			 "Registering IP address %s failed\n", buf);
201 		return -EADDRINUSE;
202 	} else {
203 		addr = qeth_l3_get_addr_buffer(tmp_addr->proto);
204 		if (!addr)
205 			return -ENOMEM;
206 
207 		memcpy(addr, tmp_addr, sizeof(struct qeth_ipaddr));
208 		addr->ref_counter = 1;
209 
210 		if (qeth_l3_is_addr_covered_by_ipato(card, addr)) {
211 			QETH_CARD_TEXT(card, 2, "tkovaddr");
212 			addr->set_flags |= QETH_IPA_SETIP_TAKEOVER_FLAG;
213 		}
214 		hash_add(card->ip_htable, &addr->hnode,
215 				qeth_l3_ipaddr_hash(addr));
216 
217 		if (!qeth_card_hw_is_reachable(card)) {
218 			addr->disp_flag = QETH_DISP_ADDR_ADD;
219 			return 0;
220 		}
221 
222 		/* qeth_l3_register_addr_entry can go to sleep
223 		 * if we add a IPV4 addr. It is caused by the reason
224 		 * that SETIP ipa cmd starts ARP staff for IPV4 addr.
225 		 * Thus we should unlock spinlock, and make a protection
226 		 * using in_progress variable to indicate that there is
227 		 * an hardware operation with this IPV4 address
228 		 */
229 		if (addr->proto == QETH_PROT_IPV4) {
230 			addr->in_progress = 1;
231 			spin_unlock_bh(&card->ip_lock);
232 			rc = qeth_l3_register_addr_entry(card, addr);
233 			spin_lock_bh(&card->ip_lock);
234 			addr->in_progress = 0;
235 		} else
236 			rc = qeth_l3_register_addr_entry(card, addr);
237 
238 		if (!rc || (rc == IPA_RC_DUPLICATE_IP_ADDRESS) ||
239 				(rc == IPA_RC_LAN_OFFLINE)) {
240 			addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
241 			if (addr->ref_counter < 1) {
242 				qeth_l3_deregister_addr_entry(card, addr);
243 				hash_del(&addr->hnode);
244 				kfree(addr);
245 			}
246 		} else {
247 			hash_del(&addr->hnode);
248 			kfree(addr);
249 		}
250 	}
251 	return rc;
252 }
253 
254 
255 struct qeth_ipaddr *qeth_l3_get_addr_buffer(
256 				enum qeth_prot_versions prot)
257 {
258 	struct qeth_ipaddr *addr;
259 
260 	addr = kzalloc(sizeof(struct qeth_ipaddr), GFP_ATOMIC);
261 	if (!addr)
262 		return NULL;
263 
264 	addr->type = QETH_IP_TYPE_NORMAL;
265 	addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
266 	addr->proto = prot;
267 
268 	return addr;
269 }
270 
271 static void qeth_l3_clear_ip_htable(struct qeth_card *card, int recover)
272 {
273 	struct qeth_ipaddr *addr;
274 	struct hlist_node *tmp;
275 	int i;
276 
277 	QETH_CARD_TEXT(card, 4, "clearip");
278 
279 	if (recover && card->options.sniffer)
280 		return;
281 
282 	spin_lock_bh(&card->ip_lock);
283 
284 	hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
285 		if (!recover) {
286 			hash_del(&addr->hnode);
287 			kfree(addr);
288 			continue;
289 		}
290 		addr->disp_flag = QETH_DISP_ADDR_ADD;
291 	}
292 
293 	spin_unlock_bh(&card->ip_lock);
294 
295 	spin_lock_bh(&card->mclock);
296 
297 	hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
298 		hash_del(&addr->hnode);
299 		kfree(addr);
300 	}
301 
302 	spin_unlock_bh(&card->mclock);
303 
304 
305 }
306 static void qeth_l3_recover_ip(struct qeth_card *card)
307 {
308 	struct qeth_ipaddr *addr;
309 	struct hlist_node *tmp;
310 	int i;
311 	int rc;
312 
313 	QETH_CARD_TEXT(card, 4, "recovrip");
314 
315 	spin_lock_bh(&card->ip_lock);
316 
317 	hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
318 		if (addr->disp_flag == QETH_DISP_ADDR_ADD) {
319 			if (addr->proto == QETH_PROT_IPV4) {
320 				addr->in_progress = 1;
321 				spin_unlock_bh(&card->ip_lock);
322 				rc = qeth_l3_register_addr_entry(card, addr);
323 				spin_lock_bh(&card->ip_lock);
324 				addr->in_progress = 0;
325 			} else
326 				rc = qeth_l3_register_addr_entry(card, addr);
327 
328 			if (!rc) {
329 				addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
330 				if (addr->ref_counter < 1)
331 					qeth_l3_delete_ip(card, addr);
332 			} else {
333 				hash_del(&addr->hnode);
334 				kfree(addr);
335 			}
336 		}
337 	}
338 
339 	spin_unlock_bh(&card->ip_lock);
340 
341 }
342 
343 static int qeth_l3_send_setdelmc(struct qeth_card *card,
344 			struct qeth_ipaddr *addr, int ipacmd)
345 {
346 	int rc;
347 	struct qeth_cmd_buffer *iob;
348 	struct qeth_ipa_cmd *cmd;
349 
350 	QETH_CARD_TEXT(card, 4, "setdelmc");
351 
352 	iob = qeth_get_ipacmd_buffer(card, ipacmd, addr->proto);
353 	if (!iob)
354 		return -ENOMEM;
355 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
356 	ether_addr_copy(cmd->data.setdelipm.mac, addr->mac);
357 	if (addr->proto == QETH_PROT_IPV6)
358 		memcpy(cmd->data.setdelipm.ip6, &addr->u.a6.addr,
359 		       sizeof(struct in6_addr));
360 	else
361 		memcpy(&cmd->data.setdelipm.ip4, &addr->u.a4.addr, 4);
362 
363 	rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
364 
365 	return rc;
366 }
367 
368 static void qeth_l3_fill_netmask(u8 *netmask, unsigned int len)
369 {
370 	int i, j;
371 	for (i = 0; i < 16; i++) {
372 		j = (len) - (i * 8);
373 		if (j >= 8)
374 			netmask[i] = 0xff;
375 		else if (j > 0)
376 			netmask[i] = (u8)(0xFF00 >> j);
377 		else
378 			netmask[i] = 0;
379 	}
380 }
381 
382 static int qeth_l3_send_setdelip(struct qeth_card *card,
383 		struct qeth_ipaddr *addr, int ipacmd, unsigned int flags)
384 {
385 	int rc;
386 	struct qeth_cmd_buffer *iob;
387 	struct qeth_ipa_cmd *cmd;
388 	__u8 netmask[16];
389 
390 	QETH_CARD_TEXT(card, 4, "setdelip");
391 	QETH_CARD_TEXT_(card, 4, "flags%02X", flags);
392 
393 	iob = qeth_get_ipacmd_buffer(card, ipacmd, addr->proto);
394 	if (!iob)
395 		return -ENOMEM;
396 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
397 	if (addr->proto == QETH_PROT_IPV6) {
398 		memcpy(cmd->data.setdelip6.ip_addr, &addr->u.a6.addr,
399 		       sizeof(struct in6_addr));
400 		qeth_l3_fill_netmask(netmask, addr->u.a6.pfxlen);
401 		memcpy(cmd->data.setdelip6.mask, netmask,
402 		       sizeof(struct in6_addr));
403 		cmd->data.setdelip6.flags = flags;
404 	} else {
405 		memcpy(cmd->data.setdelip4.ip_addr, &addr->u.a4.addr, 4);
406 		memcpy(cmd->data.setdelip4.mask, &addr->u.a4.mask, 4);
407 		cmd->data.setdelip4.flags = flags;
408 	}
409 
410 	rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
411 
412 	return rc;
413 }
414 
415 static int qeth_l3_send_setrouting(struct qeth_card *card,
416 	enum qeth_routing_types type, enum qeth_prot_versions prot)
417 {
418 	int rc;
419 	struct qeth_ipa_cmd *cmd;
420 	struct qeth_cmd_buffer *iob;
421 
422 	QETH_CARD_TEXT(card, 4, "setroutg");
423 	iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETRTG, prot);
424 	if (!iob)
425 		return -ENOMEM;
426 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
427 	cmd->data.setrtg.type = (type);
428 	rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
429 
430 	return rc;
431 }
432 
433 static int qeth_l3_correct_routing_type(struct qeth_card *card,
434 		enum qeth_routing_types *type, enum qeth_prot_versions prot)
435 {
436 	if (card->info.type == QETH_CARD_TYPE_IQD) {
437 		switch (*type) {
438 		case NO_ROUTER:
439 		case PRIMARY_CONNECTOR:
440 		case SECONDARY_CONNECTOR:
441 		case MULTICAST_ROUTER:
442 			return 0;
443 		default:
444 			goto out_inval;
445 		}
446 	} else {
447 		switch (*type) {
448 		case NO_ROUTER:
449 		case PRIMARY_ROUTER:
450 		case SECONDARY_ROUTER:
451 			return 0;
452 		case MULTICAST_ROUTER:
453 			if (qeth_is_ipafunc_supported(card, prot,
454 						      IPA_OSA_MC_ROUTER))
455 				return 0;
456 		default:
457 			goto out_inval;
458 		}
459 	}
460 out_inval:
461 	*type = NO_ROUTER;
462 	return -EINVAL;
463 }
464 
465 int qeth_l3_setrouting_v4(struct qeth_card *card)
466 {
467 	int rc;
468 
469 	QETH_CARD_TEXT(card, 3, "setrtg4");
470 
471 	rc = qeth_l3_correct_routing_type(card, &card->options.route4.type,
472 				  QETH_PROT_IPV4);
473 	if (rc)
474 		return rc;
475 
476 	rc = qeth_l3_send_setrouting(card, card->options.route4.type,
477 				  QETH_PROT_IPV4);
478 	if (rc) {
479 		card->options.route4.type = NO_ROUTER;
480 		QETH_DBF_MESSAGE(2, "Error (0x%04x) while setting routing type"
481 			" on %s. Type set to 'no router'.\n", rc,
482 			QETH_CARD_IFNAME(card));
483 	}
484 	return rc;
485 }
486 
487 int qeth_l3_setrouting_v6(struct qeth_card *card)
488 {
489 	int rc = 0;
490 
491 	QETH_CARD_TEXT(card, 3, "setrtg6");
492 
493 	if (!qeth_is_supported(card, IPA_IPV6))
494 		return 0;
495 	rc = qeth_l3_correct_routing_type(card, &card->options.route6.type,
496 				  QETH_PROT_IPV6);
497 	if (rc)
498 		return rc;
499 
500 	rc = qeth_l3_send_setrouting(card, card->options.route6.type,
501 				  QETH_PROT_IPV6);
502 	if (rc) {
503 		card->options.route6.type = NO_ROUTER;
504 		QETH_DBF_MESSAGE(2, "Error (0x%04x) while setting routing type"
505 			" on %s. Type set to 'no router'.\n", rc,
506 			QETH_CARD_IFNAME(card));
507 	}
508 	return rc;
509 }
510 
511 /*
512  * IP address takeover related functions
513  */
514 
515 /**
516  * qeth_l3_update_ipato() - Update 'takeover' property, for all NORMAL IPs.
517  *
518  * Caller must hold ip_lock.
519  */
520 void qeth_l3_update_ipato(struct qeth_card *card)
521 {
522 	struct qeth_ipaddr *addr;
523 	unsigned int i;
524 
525 	hash_for_each(card->ip_htable, i, addr, hnode) {
526 		if (addr->type != QETH_IP_TYPE_NORMAL)
527 			continue;
528 		if (qeth_l3_is_addr_covered_by_ipato(card, addr))
529 			addr->set_flags |= QETH_IPA_SETIP_TAKEOVER_FLAG;
530 		else
531 			addr->set_flags &= ~QETH_IPA_SETIP_TAKEOVER_FLAG;
532 	}
533 }
534 
535 static void qeth_l3_clear_ipato_list(struct qeth_card *card)
536 {
537 	struct qeth_ipato_entry *ipatoe, *tmp;
538 
539 	spin_lock_bh(&card->ip_lock);
540 
541 	list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
542 		list_del(&ipatoe->entry);
543 		kfree(ipatoe);
544 	}
545 
546 	qeth_l3_update_ipato(card);
547 	spin_unlock_bh(&card->ip_lock);
548 }
549 
550 int qeth_l3_add_ipato_entry(struct qeth_card *card,
551 				struct qeth_ipato_entry *new)
552 {
553 	struct qeth_ipato_entry *ipatoe;
554 	int rc = 0;
555 
556 	QETH_CARD_TEXT(card, 2, "addipato");
557 
558 	spin_lock_bh(&card->ip_lock);
559 
560 	list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
561 		if (ipatoe->proto != new->proto)
562 			continue;
563 		if (!memcmp(ipatoe->addr, new->addr,
564 			    (ipatoe->proto == QETH_PROT_IPV4)? 4:16) &&
565 		    (ipatoe->mask_bits == new->mask_bits)) {
566 			rc = -EEXIST;
567 			break;
568 		}
569 	}
570 
571 	if (!rc) {
572 		list_add_tail(&new->entry, &card->ipato.entries);
573 		qeth_l3_update_ipato(card);
574 	}
575 
576 	spin_unlock_bh(&card->ip_lock);
577 
578 	return rc;
579 }
580 
581 int qeth_l3_del_ipato_entry(struct qeth_card *card,
582 			    enum qeth_prot_versions proto, u8 *addr,
583 			    int mask_bits)
584 {
585 	struct qeth_ipato_entry *ipatoe, *tmp;
586 	int rc = -ENOENT;
587 
588 	QETH_CARD_TEXT(card, 2, "delipato");
589 
590 	spin_lock_bh(&card->ip_lock);
591 
592 	list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
593 		if (ipatoe->proto != proto)
594 			continue;
595 		if (!memcmp(ipatoe->addr, addr,
596 			    (proto == QETH_PROT_IPV4)? 4:16) &&
597 		    (ipatoe->mask_bits == mask_bits)) {
598 			list_del(&ipatoe->entry);
599 			qeth_l3_update_ipato(card);
600 			kfree(ipatoe);
601 			rc = 0;
602 		}
603 	}
604 
605 	spin_unlock_bh(&card->ip_lock);
606 	return rc;
607 }
608 
609 /*
610  * VIPA related functions
611  */
612 int qeth_l3_add_vipa(struct qeth_card *card, enum qeth_prot_versions proto,
613 	      const u8 *addr)
614 {
615 	struct qeth_ipaddr *ipaddr;
616 	int rc;
617 
618 	ipaddr = qeth_l3_get_addr_buffer(proto);
619 	if (ipaddr) {
620 		if (proto == QETH_PROT_IPV4) {
621 			QETH_CARD_TEXT(card, 2, "addvipa4");
622 			memcpy(&ipaddr->u.a4.addr, addr, 4);
623 			ipaddr->u.a4.mask = 0;
624 		} else if (proto == QETH_PROT_IPV6) {
625 			QETH_CARD_TEXT(card, 2, "addvipa6");
626 			memcpy(&ipaddr->u.a6.addr, addr, 16);
627 			ipaddr->u.a6.pfxlen = 0;
628 		}
629 		ipaddr->type = QETH_IP_TYPE_VIPA;
630 		ipaddr->set_flags = QETH_IPA_SETIP_VIPA_FLAG;
631 		ipaddr->del_flags = QETH_IPA_DELIP_VIPA_FLAG;
632 	} else
633 		return -ENOMEM;
634 
635 	spin_lock_bh(&card->ip_lock);
636 	rc = qeth_l3_add_ip(card, ipaddr);
637 	spin_unlock_bh(&card->ip_lock);
638 
639 	kfree(ipaddr);
640 
641 	return rc;
642 }
643 
644 int qeth_l3_del_vipa(struct qeth_card *card, enum qeth_prot_versions proto,
645 		     const u8 *addr)
646 {
647 	struct qeth_ipaddr *ipaddr;
648 	int rc;
649 
650 	ipaddr = qeth_l3_get_addr_buffer(proto);
651 	if (ipaddr) {
652 		if (proto == QETH_PROT_IPV4) {
653 			QETH_CARD_TEXT(card, 2, "delvipa4");
654 			memcpy(&ipaddr->u.a4.addr, addr, 4);
655 			ipaddr->u.a4.mask = 0;
656 		} else if (proto == QETH_PROT_IPV6) {
657 			QETH_CARD_TEXT(card, 2, "delvipa6");
658 			memcpy(&ipaddr->u.a6.addr, addr, 16);
659 			ipaddr->u.a6.pfxlen = 0;
660 		}
661 		ipaddr->type = QETH_IP_TYPE_VIPA;
662 	} else
663 		return -ENOMEM;
664 
665 	spin_lock_bh(&card->ip_lock);
666 	rc = qeth_l3_delete_ip(card, ipaddr);
667 	spin_unlock_bh(&card->ip_lock);
668 
669 	kfree(ipaddr);
670 	return rc;
671 }
672 
673 /*
674  * proxy ARP related functions
675  */
676 int qeth_l3_add_rxip(struct qeth_card *card, enum qeth_prot_versions proto,
677 	      const u8 *addr)
678 {
679 	struct qeth_ipaddr *ipaddr;
680 	int rc;
681 
682 	ipaddr = qeth_l3_get_addr_buffer(proto);
683 	if (ipaddr) {
684 		if (proto == QETH_PROT_IPV4) {
685 			QETH_CARD_TEXT(card, 2, "addrxip4");
686 			memcpy(&ipaddr->u.a4.addr, addr, 4);
687 			ipaddr->u.a4.mask = 0;
688 		} else if (proto == QETH_PROT_IPV6) {
689 			QETH_CARD_TEXT(card, 2, "addrxip6");
690 			memcpy(&ipaddr->u.a6.addr, addr, 16);
691 			ipaddr->u.a6.pfxlen = 0;
692 		}
693 
694 		ipaddr->type = QETH_IP_TYPE_RXIP;
695 		ipaddr->set_flags = QETH_IPA_SETIP_TAKEOVER_FLAG;
696 		ipaddr->del_flags = 0;
697 	} else
698 		return -ENOMEM;
699 
700 	spin_lock_bh(&card->ip_lock);
701 	rc = qeth_l3_add_ip(card, ipaddr);
702 	spin_unlock_bh(&card->ip_lock);
703 
704 	kfree(ipaddr);
705 
706 	return rc;
707 }
708 
709 int qeth_l3_del_rxip(struct qeth_card *card, enum qeth_prot_versions proto,
710 		     const u8 *addr)
711 {
712 	struct qeth_ipaddr *ipaddr;
713 	int rc;
714 
715 	ipaddr = qeth_l3_get_addr_buffer(proto);
716 	if (ipaddr) {
717 		if (proto == QETH_PROT_IPV4) {
718 			QETH_CARD_TEXT(card, 2, "delrxip4");
719 			memcpy(&ipaddr->u.a4.addr, addr, 4);
720 			ipaddr->u.a4.mask = 0;
721 		} else if (proto == QETH_PROT_IPV6) {
722 			QETH_CARD_TEXT(card, 2, "delrxip6");
723 			memcpy(&ipaddr->u.a6.addr, addr, 16);
724 			ipaddr->u.a6.pfxlen = 0;
725 		}
726 		ipaddr->type = QETH_IP_TYPE_RXIP;
727 	} else
728 		return -ENOMEM;
729 
730 	spin_lock_bh(&card->ip_lock);
731 	rc = qeth_l3_delete_ip(card, ipaddr);
732 	spin_unlock_bh(&card->ip_lock);
733 
734 	kfree(ipaddr);
735 	return rc;
736 }
737 
738 static int qeth_l3_register_addr_entry(struct qeth_card *card,
739 				struct qeth_ipaddr *addr)
740 {
741 	char buf[50];
742 	int rc = 0;
743 	int cnt = 3;
744 
745 
746 	if (addr->proto == QETH_PROT_IPV4) {
747 		QETH_CARD_TEXT(card, 2, "setaddr4");
748 		QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
749 	} else if (addr->proto == QETH_PROT_IPV6) {
750 		QETH_CARD_TEXT(card, 2, "setaddr6");
751 		QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
752 		QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
753 	} else {
754 		QETH_CARD_TEXT(card, 2, "setaddr?");
755 		QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
756 	}
757 	do {
758 		if (addr->is_multicast)
759 			rc =  qeth_l3_send_setdelmc(card, addr, IPA_CMD_SETIPM);
760 		else
761 			rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_SETIP,
762 					addr->set_flags);
763 		if (rc)
764 			QETH_CARD_TEXT(card, 2, "failed");
765 	} while ((--cnt > 0) && rc);
766 	if (rc) {
767 		QETH_CARD_TEXT(card, 2, "FAILED");
768 		qeth_l3_ipaddr_to_string(addr->proto, (u8 *)&addr->u, buf);
769 		dev_warn(&card->gdev->dev,
770 			"Registering IP address %s failed\n", buf);
771 	}
772 	return rc;
773 }
774 
775 static int qeth_l3_deregister_addr_entry(struct qeth_card *card,
776 						struct qeth_ipaddr *addr)
777 {
778 	int rc = 0;
779 
780 	if (addr->proto == QETH_PROT_IPV4) {
781 		QETH_CARD_TEXT(card, 2, "deladdr4");
782 		QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
783 	} else if (addr->proto == QETH_PROT_IPV6) {
784 		QETH_CARD_TEXT(card, 2, "deladdr6");
785 		QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
786 		QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
787 	} else {
788 		QETH_CARD_TEXT(card, 2, "deladdr?");
789 		QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
790 	}
791 	if (addr->is_multicast)
792 		rc = qeth_l3_send_setdelmc(card, addr, IPA_CMD_DELIPM);
793 	else
794 		rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_DELIP,
795 					addr->del_flags);
796 	if (rc)
797 		QETH_CARD_TEXT(card, 2, "failed");
798 
799 	return rc;
800 }
801 
802 static int qeth_l3_setadapter_parms(struct qeth_card *card)
803 {
804 	int rc = 0;
805 
806 	QETH_DBF_TEXT(SETUP, 2, "setadprm");
807 
808 	if (qeth_adp_supported(card, IPA_SETADP_ALTER_MAC_ADDRESS)) {
809 		rc = qeth_setadpparms_change_macaddr(card);
810 		if (rc)
811 			dev_warn(&card->gdev->dev, "Reading the adapter MAC"
812 				" address failed\n");
813 	}
814 
815 	return rc;
816 }
817 
818 static int qeth_l3_send_simple_setassparms_ipv6(struct qeth_card *card,
819 		enum qeth_ipa_funcs ipa_func, __u16 cmd_code)
820 {
821 	int rc;
822 	struct qeth_cmd_buffer *iob;
823 
824 	QETH_CARD_TEXT(card, 4, "simassp6");
825 	iob = qeth_get_setassparms_cmd(card, ipa_func, cmd_code,
826 				       0, QETH_PROT_IPV6);
827 	if (!iob)
828 		return -ENOMEM;
829 	rc = qeth_send_setassparms(card, iob, 0, 0,
830 				   qeth_setassparms_cb, NULL);
831 	return rc;
832 }
833 
834 static int qeth_l3_start_ipa_arp_processing(struct qeth_card *card)
835 {
836 	int rc;
837 
838 	QETH_CARD_TEXT(card, 3, "ipaarp");
839 
840 	if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
841 		dev_info(&card->gdev->dev,
842 			"ARP processing not supported on %s!\n",
843 			QETH_CARD_IFNAME(card));
844 		return 0;
845 	}
846 	rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
847 					  IPA_CMD_ASS_START, 0);
848 	if (rc) {
849 		dev_warn(&card->gdev->dev,
850 			"Starting ARP processing support for %s failed\n",
851 			QETH_CARD_IFNAME(card));
852 	}
853 	return rc;
854 }
855 
856 static int qeth_l3_start_ipa_source_mac(struct qeth_card *card)
857 {
858 	int rc;
859 
860 	QETH_CARD_TEXT(card, 3, "stsrcmac");
861 
862 	if (!qeth_is_supported(card, IPA_SOURCE_MAC)) {
863 		dev_info(&card->gdev->dev,
864 			"Inbound source MAC-address not supported on %s\n",
865 			QETH_CARD_IFNAME(card));
866 		return -EOPNOTSUPP;
867 	}
868 
869 	rc = qeth_send_simple_setassparms(card, IPA_SOURCE_MAC,
870 					  IPA_CMD_ASS_START, 0);
871 	if (rc)
872 		dev_warn(&card->gdev->dev,
873 			"Starting source MAC-address support for %s failed\n",
874 			QETH_CARD_IFNAME(card));
875 	return rc;
876 }
877 
878 static int qeth_l3_start_ipa_vlan(struct qeth_card *card)
879 {
880 	int rc = 0;
881 
882 	QETH_CARD_TEXT(card, 3, "strtvlan");
883 
884 	if (!qeth_is_supported(card, IPA_FULL_VLAN)) {
885 		dev_info(&card->gdev->dev,
886 			"VLAN not supported on %s\n", QETH_CARD_IFNAME(card));
887 		return -EOPNOTSUPP;
888 	}
889 
890 	rc = qeth_send_simple_setassparms(card, IPA_VLAN_PRIO,
891 					  IPA_CMD_ASS_START, 0);
892 	if (rc) {
893 		dev_warn(&card->gdev->dev,
894 			"Starting VLAN support for %s failed\n",
895 			QETH_CARD_IFNAME(card));
896 	} else {
897 		dev_info(&card->gdev->dev, "VLAN enabled\n");
898 	}
899 	return rc;
900 }
901 
902 static int qeth_l3_start_ipa_multicast(struct qeth_card *card)
903 {
904 	int rc;
905 
906 	QETH_CARD_TEXT(card, 3, "stmcast");
907 
908 	if (!qeth_is_supported(card, IPA_MULTICASTING)) {
909 		dev_info(&card->gdev->dev,
910 			"Multicast not supported on %s\n",
911 			QETH_CARD_IFNAME(card));
912 		return -EOPNOTSUPP;
913 	}
914 
915 	rc = qeth_send_simple_setassparms(card, IPA_MULTICASTING,
916 					  IPA_CMD_ASS_START, 0);
917 	if (rc) {
918 		dev_warn(&card->gdev->dev,
919 			"Starting multicast support for %s failed\n",
920 			QETH_CARD_IFNAME(card));
921 	} else {
922 		dev_info(&card->gdev->dev, "Multicast enabled\n");
923 		card->dev->flags |= IFF_MULTICAST;
924 	}
925 	return rc;
926 }
927 
928 static int qeth_l3_softsetup_ipv6(struct qeth_card *card)
929 {
930 	int rc;
931 
932 	QETH_CARD_TEXT(card, 3, "softipv6");
933 
934 	rc = qeth_query_ipassists(card, QETH_PROT_IPV6);
935 	if (rc) {
936 		dev_err(&card->gdev->dev,
937 			"Activating IPv6 support for %s failed\n",
938 			QETH_CARD_IFNAME(card));
939 		return rc;
940 	}
941 
942 	if (card->info.type == QETH_CARD_TYPE_IQD)
943 		goto out;
944 
945 	rc = qeth_send_simple_setassparms(card, IPA_IPV6,
946 					  IPA_CMD_ASS_START, 3);
947 	if (rc) {
948 		dev_err(&card->gdev->dev,
949 			"Activating IPv6 support for %s failed\n",
950 			QETH_CARD_IFNAME(card));
951 		return rc;
952 	}
953 	rc = qeth_l3_send_simple_setassparms_ipv6(card, IPA_IPV6,
954 					       IPA_CMD_ASS_START);
955 	if (rc) {
956 		dev_err(&card->gdev->dev,
957 			"Activating IPv6 support for %s failed\n",
958 			 QETH_CARD_IFNAME(card));
959 		return rc;
960 	}
961 	rc = qeth_l3_send_simple_setassparms_ipv6(card, IPA_PASSTHRU,
962 					       IPA_CMD_ASS_START);
963 	if (rc) {
964 		dev_warn(&card->gdev->dev,
965 			"Enabling the passthrough mode for %s failed\n",
966 			QETH_CARD_IFNAME(card));
967 		return rc;
968 	}
969 out:
970 	dev_info(&card->gdev->dev, "IPV6 enabled\n");
971 	return 0;
972 }
973 
974 static int qeth_l3_start_ipa_ipv6(struct qeth_card *card)
975 {
976 	QETH_CARD_TEXT(card, 3, "strtipv6");
977 
978 	if (!qeth_is_supported(card, IPA_IPV6)) {
979 		dev_info(&card->gdev->dev,
980 			"IPv6 not supported on %s\n", QETH_CARD_IFNAME(card));
981 		return 0;
982 	}
983 	return qeth_l3_softsetup_ipv6(card);
984 }
985 
986 static int qeth_l3_start_ipa_broadcast(struct qeth_card *card)
987 {
988 	int rc;
989 
990 	QETH_CARD_TEXT(card, 3, "stbrdcst");
991 	card->info.broadcast_capable = 0;
992 	if (!qeth_is_supported(card, IPA_FILTERING)) {
993 		dev_info(&card->gdev->dev,
994 			"Broadcast not supported on %s\n",
995 			QETH_CARD_IFNAME(card));
996 		rc = -EOPNOTSUPP;
997 		goto out;
998 	}
999 	rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
1000 					  IPA_CMD_ASS_START, 0);
1001 	if (rc) {
1002 		dev_warn(&card->gdev->dev, "Enabling broadcast filtering for "
1003 			"%s failed\n", QETH_CARD_IFNAME(card));
1004 		goto out;
1005 	}
1006 
1007 	rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
1008 					  IPA_CMD_ASS_CONFIGURE, 1);
1009 	if (rc) {
1010 		dev_warn(&card->gdev->dev,
1011 			"Setting up broadcast filtering for %s failed\n",
1012 			QETH_CARD_IFNAME(card));
1013 		goto out;
1014 	}
1015 	card->info.broadcast_capable = QETH_BROADCAST_WITH_ECHO;
1016 	dev_info(&card->gdev->dev, "Broadcast enabled\n");
1017 	rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
1018 					  IPA_CMD_ASS_ENABLE, 1);
1019 	if (rc) {
1020 		dev_warn(&card->gdev->dev, "Setting up broadcast echo "
1021 			"filtering for %s failed\n", QETH_CARD_IFNAME(card));
1022 		goto out;
1023 	}
1024 	card->info.broadcast_capable = QETH_BROADCAST_WITHOUT_ECHO;
1025 out:
1026 	if (card->info.broadcast_capable)
1027 		card->dev->flags |= IFF_BROADCAST;
1028 	else
1029 		card->dev->flags &= ~IFF_BROADCAST;
1030 	return rc;
1031 }
1032 
1033 static int qeth_l3_start_ipassists(struct qeth_card *card)
1034 {
1035 	QETH_CARD_TEXT(card, 3, "strtipas");
1036 
1037 	if (qeth_set_access_ctrl_online(card, 0))
1038 		return -EIO;
1039 	qeth_l3_start_ipa_arp_processing(card);	/* go on*/
1040 	qeth_l3_start_ipa_source_mac(card);	/* go on*/
1041 	qeth_l3_start_ipa_vlan(card);		/* go on*/
1042 	qeth_l3_start_ipa_multicast(card);		/* go on*/
1043 	qeth_l3_start_ipa_ipv6(card);		/* go on*/
1044 	qeth_l3_start_ipa_broadcast(card);		/* go on*/
1045 	return 0;
1046 }
1047 
1048 static int qeth_l3_iqd_read_initial_mac_cb(struct qeth_card *card,
1049 		struct qeth_reply *reply, unsigned long data)
1050 {
1051 	struct qeth_ipa_cmd *cmd;
1052 
1053 	cmd = (struct qeth_ipa_cmd *) data;
1054 	if (cmd->hdr.return_code == 0)
1055 		ether_addr_copy(card->dev->dev_addr,
1056 				cmd->data.create_destroy_addr.unique_id);
1057 	else
1058 		eth_random_addr(card->dev->dev_addr);
1059 
1060 	return 0;
1061 }
1062 
1063 static int qeth_l3_iqd_read_initial_mac(struct qeth_card *card)
1064 {
1065 	int rc = 0;
1066 	struct qeth_cmd_buffer *iob;
1067 	struct qeth_ipa_cmd *cmd;
1068 
1069 	QETH_DBF_TEXT(SETUP, 2, "hsrmac");
1070 
1071 	iob = qeth_get_ipacmd_buffer(card, IPA_CMD_CREATE_ADDR,
1072 				     QETH_PROT_IPV6);
1073 	if (!iob)
1074 		return -ENOMEM;
1075 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1076 	*((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
1077 			card->info.unique_id;
1078 
1079 	rc = qeth_send_ipa_cmd(card, iob, qeth_l3_iqd_read_initial_mac_cb,
1080 				NULL);
1081 	return rc;
1082 }
1083 
1084 static int qeth_l3_get_unique_id_cb(struct qeth_card *card,
1085 		struct qeth_reply *reply, unsigned long data)
1086 {
1087 	struct qeth_ipa_cmd *cmd;
1088 
1089 	cmd = (struct qeth_ipa_cmd *) data;
1090 	if (cmd->hdr.return_code == 0)
1091 		card->info.unique_id = *((__u16 *)
1092 				&cmd->data.create_destroy_addr.unique_id[6]);
1093 	else {
1094 		card->info.unique_id =  UNIQUE_ID_IF_CREATE_ADDR_FAILED |
1095 					UNIQUE_ID_NOT_BY_CARD;
1096 		dev_warn(&card->gdev->dev, "The network adapter failed to "
1097 			"generate a unique ID\n");
1098 	}
1099 	return 0;
1100 }
1101 
1102 static int qeth_l3_get_unique_id(struct qeth_card *card)
1103 {
1104 	int rc = 0;
1105 	struct qeth_cmd_buffer *iob;
1106 	struct qeth_ipa_cmd *cmd;
1107 
1108 	QETH_DBF_TEXT(SETUP, 2, "guniqeid");
1109 
1110 	if (!qeth_is_supported(card, IPA_IPV6)) {
1111 		card->info.unique_id =  UNIQUE_ID_IF_CREATE_ADDR_FAILED |
1112 					UNIQUE_ID_NOT_BY_CARD;
1113 		return 0;
1114 	}
1115 
1116 	iob = qeth_get_ipacmd_buffer(card, IPA_CMD_CREATE_ADDR,
1117 				     QETH_PROT_IPV6);
1118 	if (!iob)
1119 		return -ENOMEM;
1120 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1121 	*((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
1122 			card->info.unique_id;
1123 
1124 	rc = qeth_send_ipa_cmd(card, iob, qeth_l3_get_unique_id_cb, NULL);
1125 	return rc;
1126 }
1127 
1128 static int
1129 qeth_diags_trace_cb(struct qeth_card *card, struct qeth_reply *reply,
1130 			    unsigned long data)
1131 {
1132 	struct qeth_ipa_cmd	   *cmd;
1133 	__u16 rc;
1134 
1135 	QETH_DBF_TEXT(SETUP, 2, "diastrcb");
1136 
1137 	cmd = (struct qeth_ipa_cmd *)data;
1138 	rc = cmd->hdr.return_code;
1139 	if (rc)
1140 		QETH_CARD_TEXT_(card, 2, "dxter%x", rc);
1141 	switch (cmd->data.diagass.action) {
1142 	case QETH_DIAGS_CMD_TRACE_QUERY:
1143 		break;
1144 	case QETH_DIAGS_CMD_TRACE_DISABLE:
1145 		switch (rc) {
1146 		case 0:
1147 		case IPA_RC_INVALID_SUBCMD:
1148 			card->info.promisc_mode = SET_PROMISC_MODE_OFF;
1149 			dev_info(&card->gdev->dev, "The HiperSockets network "
1150 				"traffic analyzer is deactivated\n");
1151 			break;
1152 		default:
1153 			break;
1154 		}
1155 		break;
1156 	case QETH_DIAGS_CMD_TRACE_ENABLE:
1157 		switch (rc) {
1158 		case 0:
1159 			card->info.promisc_mode = SET_PROMISC_MODE_ON;
1160 			dev_info(&card->gdev->dev, "The HiperSockets network "
1161 				"traffic analyzer is activated\n");
1162 			break;
1163 		case IPA_RC_HARDWARE_AUTH_ERROR:
1164 			dev_warn(&card->gdev->dev, "The device is not "
1165 				"authorized to run as a HiperSockets network "
1166 				"traffic analyzer\n");
1167 			break;
1168 		case IPA_RC_TRACE_ALREADY_ACTIVE:
1169 			dev_warn(&card->gdev->dev, "A HiperSockets "
1170 				"network traffic analyzer is already "
1171 				"active in the HiperSockets LAN\n");
1172 			break;
1173 		default:
1174 			break;
1175 		}
1176 		break;
1177 	default:
1178 		QETH_DBF_MESSAGE(2, "Unknown sniffer action (0x%04x) on %s\n",
1179 			cmd->data.diagass.action, QETH_CARD_IFNAME(card));
1180 	}
1181 
1182 	return 0;
1183 }
1184 
1185 static int
1186 qeth_diags_trace(struct qeth_card *card, enum qeth_diags_trace_cmds diags_cmd)
1187 {
1188 	struct qeth_cmd_buffer *iob;
1189 	struct qeth_ipa_cmd    *cmd;
1190 
1191 	QETH_DBF_TEXT(SETUP, 2, "diagtrac");
1192 
1193 	iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
1194 	if (!iob)
1195 		return -ENOMEM;
1196 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1197 	cmd->data.diagass.subcmd_len = 16;
1198 	cmd->data.diagass.subcmd = QETH_DIAGS_CMD_TRACE;
1199 	cmd->data.diagass.type = QETH_DIAGS_TYPE_HIPERSOCKET;
1200 	cmd->data.diagass.action = diags_cmd;
1201 	return qeth_send_ipa_cmd(card, iob, qeth_diags_trace_cb, NULL);
1202 }
1203 
1204 static void
1205 qeth_l3_add_mc_to_hash(struct qeth_card *card, struct in_device *in4_dev)
1206 {
1207 	struct ip_mc_list *im4;
1208 	struct qeth_ipaddr *tmp, *ipm;
1209 
1210 	QETH_CARD_TEXT(card, 4, "addmc");
1211 
1212 	tmp = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
1213 	if (!tmp)
1214 		return;
1215 
1216 	for (im4 = rcu_dereference(in4_dev->mc_list); im4 != NULL;
1217 	     im4 = rcu_dereference(im4->next_rcu)) {
1218 		ip_eth_mc_map(im4->multiaddr, tmp->mac);
1219 		tmp->u.a4.addr = be32_to_cpu(im4->multiaddr);
1220 		tmp->is_multicast = 1;
1221 
1222 		ipm = qeth_l3_find_addr_by_ip(card, tmp);
1223 		if (ipm) {
1224 			/* for mcast, by-IP match means full match */
1225 			ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
1226 		} else {
1227 			ipm = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
1228 			if (!ipm)
1229 				continue;
1230 			ether_addr_copy(ipm->mac, tmp->mac);
1231 			ipm->u.a4.addr = be32_to_cpu(im4->multiaddr);
1232 			ipm->is_multicast = 1;
1233 			ipm->disp_flag = QETH_DISP_ADDR_ADD;
1234 			hash_add(card->ip_mc_htable,
1235 					&ipm->hnode, qeth_l3_ipaddr_hash(ipm));
1236 		}
1237 	}
1238 
1239 	kfree(tmp);
1240 }
1241 
1242 /* called with rcu_read_lock */
1243 static void qeth_l3_add_vlan_mc(struct qeth_card *card)
1244 {
1245 	struct in_device *in_dev;
1246 	u16 vid;
1247 
1248 	QETH_CARD_TEXT(card, 4, "addmcvl");
1249 
1250 	if (!qeth_is_supported(card, IPA_FULL_VLAN))
1251 		return;
1252 
1253 	for_each_set_bit(vid, card->active_vlans, VLAN_N_VID) {
1254 		struct net_device *netdev;
1255 
1256 		netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q),
1257 					      vid);
1258 		if (netdev == NULL ||
1259 		    !(netdev->flags & IFF_UP))
1260 			continue;
1261 		in_dev = __in_dev_get_rcu(netdev);
1262 		if (!in_dev)
1263 			continue;
1264 		qeth_l3_add_mc_to_hash(card, in_dev);
1265 	}
1266 }
1267 
1268 static void qeth_l3_add_multicast_ipv4(struct qeth_card *card)
1269 {
1270 	struct in_device *in4_dev;
1271 
1272 	QETH_CARD_TEXT(card, 4, "chkmcv4");
1273 
1274 	rcu_read_lock();
1275 	in4_dev = __in_dev_get_rcu(card->dev);
1276 	if (in4_dev == NULL)
1277 		goto unlock;
1278 	qeth_l3_add_mc_to_hash(card, in4_dev);
1279 	qeth_l3_add_vlan_mc(card);
1280 unlock:
1281 	rcu_read_unlock();
1282 }
1283 
1284 static void qeth_l3_add_mc6_to_hash(struct qeth_card *card,
1285 				    struct inet6_dev *in6_dev)
1286 {
1287 	struct qeth_ipaddr *ipm;
1288 	struct ifmcaddr6 *im6;
1289 	struct qeth_ipaddr *tmp;
1290 
1291 	QETH_CARD_TEXT(card, 4, "addmc6");
1292 
1293 	tmp = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
1294 	if (!tmp)
1295 		return;
1296 
1297 	for (im6 = in6_dev->mc_list; im6 != NULL; im6 = im6->next) {
1298 		ipv6_eth_mc_map(&im6->mca_addr, tmp->mac);
1299 		memcpy(&tmp->u.a6.addr, &im6->mca_addr.s6_addr,
1300 		       sizeof(struct in6_addr));
1301 		tmp->is_multicast = 1;
1302 
1303 		ipm = qeth_l3_find_addr_by_ip(card, tmp);
1304 		if (ipm) {
1305 			/* for mcast, by-IP match means full match */
1306 			ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
1307 			continue;
1308 		}
1309 
1310 		ipm = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
1311 		if (!ipm)
1312 			continue;
1313 
1314 		ether_addr_copy(ipm->mac, tmp->mac);
1315 		memcpy(&ipm->u.a6.addr, &im6->mca_addr.s6_addr,
1316 		       sizeof(struct in6_addr));
1317 		ipm->is_multicast = 1;
1318 		ipm->disp_flag = QETH_DISP_ADDR_ADD;
1319 		hash_add(card->ip_mc_htable,
1320 				&ipm->hnode, qeth_l3_ipaddr_hash(ipm));
1321 
1322 	}
1323 	kfree(tmp);
1324 }
1325 
1326 /* called with rcu_read_lock */
1327 static void qeth_l3_add_vlan_mc6(struct qeth_card *card)
1328 {
1329 	struct inet6_dev *in_dev;
1330 	u16 vid;
1331 
1332 	QETH_CARD_TEXT(card, 4, "admc6vl");
1333 
1334 	if (!qeth_is_supported(card, IPA_FULL_VLAN))
1335 		return;
1336 
1337 	for_each_set_bit(vid, card->active_vlans, VLAN_N_VID) {
1338 		struct net_device *netdev;
1339 
1340 		netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q),
1341 					      vid);
1342 		if (netdev == NULL ||
1343 		    !(netdev->flags & IFF_UP))
1344 			continue;
1345 		in_dev = in6_dev_get(netdev);
1346 		if (!in_dev)
1347 			continue;
1348 		read_lock_bh(&in_dev->lock);
1349 		qeth_l3_add_mc6_to_hash(card, in_dev);
1350 		read_unlock_bh(&in_dev->lock);
1351 		in6_dev_put(in_dev);
1352 	}
1353 }
1354 
1355 static void qeth_l3_add_multicast_ipv6(struct qeth_card *card)
1356 {
1357 	struct inet6_dev *in6_dev;
1358 
1359 	QETH_CARD_TEXT(card, 4, "chkmcv6");
1360 
1361 	if (!qeth_is_supported(card, IPA_IPV6))
1362 		return ;
1363 	in6_dev = in6_dev_get(card->dev);
1364 	if (!in6_dev)
1365 		return;
1366 
1367 	rcu_read_lock();
1368 	read_lock_bh(&in6_dev->lock);
1369 	qeth_l3_add_mc6_to_hash(card, in6_dev);
1370 	qeth_l3_add_vlan_mc6(card);
1371 	read_unlock_bh(&in6_dev->lock);
1372 	rcu_read_unlock();
1373 	in6_dev_put(in6_dev);
1374 }
1375 
1376 static void qeth_l3_free_vlan_addresses4(struct qeth_card *card,
1377 			unsigned short vid)
1378 {
1379 	struct in_device *in_dev;
1380 	struct in_ifaddr *ifa;
1381 	struct qeth_ipaddr *addr;
1382 	struct net_device *netdev;
1383 
1384 	QETH_CARD_TEXT(card, 4, "frvaddr4");
1385 
1386 	netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q), vid);
1387 	if (!netdev)
1388 		return;
1389 	in_dev = in_dev_get(netdev);
1390 	if (!in_dev)
1391 		return;
1392 
1393 	addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
1394 	if (!addr)
1395 		goto out;
1396 
1397 	spin_lock_bh(&card->ip_lock);
1398 
1399 	for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1400 		addr->u.a4.addr = be32_to_cpu(ifa->ifa_address);
1401 		addr->u.a4.mask = be32_to_cpu(ifa->ifa_mask);
1402 		addr->type = QETH_IP_TYPE_NORMAL;
1403 		qeth_l3_delete_ip(card, addr);
1404 	}
1405 
1406 	spin_unlock_bh(&card->ip_lock);
1407 
1408 	kfree(addr);
1409 out:
1410 	in_dev_put(in_dev);
1411 }
1412 
1413 static void qeth_l3_free_vlan_addresses6(struct qeth_card *card,
1414 					 unsigned short vid)
1415 {
1416 	struct inet6_dev *in6_dev;
1417 	struct inet6_ifaddr *ifa;
1418 	struct qeth_ipaddr *addr;
1419 	struct net_device *netdev;
1420 
1421 	QETH_CARD_TEXT(card, 4, "frvaddr6");
1422 
1423 	netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q), vid);
1424 	if (!netdev)
1425 		return;
1426 
1427 	in6_dev = in6_dev_get(netdev);
1428 	if (!in6_dev)
1429 		return;
1430 
1431 	addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
1432 	if (!addr)
1433 		goto out;
1434 
1435 	spin_lock_bh(&card->ip_lock);
1436 
1437 	list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
1438 		memcpy(&addr->u.a6.addr, &ifa->addr,
1439 		       sizeof(struct in6_addr));
1440 		addr->u.a6.pfxlen = ifa->prefix_len;
1441 		addr->type = QETH_IP_TYPE_NORMAL;
1442 		qeth_l3_delete_ip(card, addr);
1443 	}
1444 
1445 	spin_unlock_bh(&card->ip_lock);
1446 
1447 	kfree(addr);
1448 out:
1449 	in6_dev_put(in6_dev);
1450 }
1451 
1452 static void qeth_l3_free_vlan_addresses(struct qeth_card *card,
1453 			unsigned short vid)
1454 {
1455 	rcu_read_lock();
1456 	qeth_l3_free_vlan_addresses4(card, vid);
1457 	qeth_l3_free_vlan_addresses6(card, vid);
1458 	rcu_read_unlock();
1459 }
1460 
1461 static int qeth_l3_vlan_rx_add_vid(struct net_device *dev,
1462 				   __be16 proto, u16 vid)
1463 {
1464 	struct qeth_card *card = dev->ml_priv;
1465 
1466 	set_bit(vid, card->active_vlans);
1467 	return 0;
1468 }
1469 
1470 static int qeth_l3_vlan_rx_kill_vid(struct net_device *dev,
1471 				    __be16 proto, u16 vid)
1472 {
1473 	struct qeth_card *card = dev->ml_priv;
1474 
1475 	QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
1476 
1477 	if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
1478 		QETH_CARD_TEXT(card, 3, "kidREC");
1479 		return 0;
1480 	}
1481 	/* unregister IP addresses of vlan device */
1482 	qeth_l3_free_vlan_addresses(card, vid);
1483 	clear_bit(vid, card->active_vlans);
1484 	qeth_l3_set_rx_mode(dev);
1485 	return 0;
1486 }
1487 
1488 static void qeth_l3_rebuild_skb(struct qeth_card *card, struct sk_buff *skb,
1489 				struct qeth_hdr *hdr)
1490 {
1491 	if (!(hdr->hdr.l3.flags & QETH_HDR_PASSTHRU)) {
1492 		u16 prot = (hdr->hdr.l3.flags & QETH_HDR_IPV6) ? ETH_P_IPV6 :
1493 								 ETH_P_IP;
1494 		unsigned char tg_addr[ETH_ALEN];
1495 
1496 		skb_reset_network_header(skb);
1497 		switch (hdr->hdr.l3.flags & QETH_HDR_CAST_MASK) {
1498 		case QETH_CAST_MULTICAST:
1499 			if (prot == ETH_P_IP)
1500 				ip_eth_mc_map(ip_hdr(skb)->daddr, tg_addr);
1501 			else
1502 				ipv6_eth_mc_map(&ipv6_hdr(skb)->daddr, tg_addr);
1503 
1504 			card->stats.multicast++;
1505 			skb->pkt_type = PACKET_MULTICAST;
1506 			break;
1507 		case QETH_CAST_BROADCAST:
1508 			ether_addr_copy(tg_addr, card->dev->broadcast);
1509 			card->stats.multicast++;
1510 			skb->pkt_type = PACKET_BROADCAST;
1511 			break;
1512 		case QETH_CAST_UNICAST:
1513 		case QETH_CAST_ANYCAST:
1514 		case QETH_CAST_NOCAST:
1515 		default:
1516 			if (card->options.sniffer)
1517 				skb->pkt_type = PACKET_OTHERHOST;
1518 			else
1519 				skb->pkt_type = PACKET_HOST;
1520 			ether_addr_copy(tg_addr, card->dev->dev_addr);
1521 		}
1522 		if (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_SRC_MAC_ADDR)
1523 			card->dev->header_ops->create(skb, card->dev, prot,
1524 				tg_addr, &hdr->hdr.l3.next_hop.rx.src_mac,
1525 				card->dev->addr_len);
1526 		else
1527 			card->dev->header_ops->create(skb, card->dev, prot,
1528 				tg_addr, "FAKELL", card->dev->addr_len);
1529 	}
1530 
1531 	skb->protocol = eth_type_trans(skb, card->dev);
1532 
1533 	/* copy VLAN tag from hdr into skb */
1534 	if (!card->options.sniffer &&
1535 	    (hdr->hdr.l3.ext_flags & (QETH_HDR_EXT_VLAN_FRAME |
1536 				      QETH_HDR_EXT_INCLUDE_VLAN_TAG))) {
1537 		u16 tag = (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_VLAN_FRAME) ?
1538 				hdr->hdr.l3.vlan_id :
1539 				hdr->hdr.l3.next_hop.rx.vlan_id;
1540 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), tag);
1541 	}
1542 
1543 	if (card->dev->features & NETIF_F_RXCSUM) {
1544 		if ((hdr->hdr.l3.ext_flags &
1545 		    (QETH_HDR_EXT_CSUM_HDR_REQ |
1546 		     QETH_HDR_EXT_CSUM_TRANSP_REQ)) ==
1547 		    (QETH_HDR_EXT_CSUM_HDR_REQ |
1548 		     QETH_HDR_EXT_CSUM_TRANSP_REQ))
1549 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1550 		else
1551 			skb->ip_summed = CHECKSUM_NONE;
1552 	} else
1553 		skb->ip_summed = CHECKSUM_NONE;
1554 }
1555 
1556 static int qeth_l3_process_inbound_buffer(struct qeth_card *card,
1557 				int budget, int *done)
1558 {
1559 	int work_done = 0;
1560 	struct sk_buff *skb;
1561 	struct qeth_hdr *hdr;
1562 	unsigned int len;
1563 	__u16 magic;
1564 
1565 	*done = 0;
1566 	WARN_ON_ONCE(!budget);
1567 	while (budget) {
1568 		skb = qeth_core_get_next_skb(card,
1569 			&card->qdio.in_q->bufs[card->rx.b_index],
1570 			&card->rx.b_element, &card->rx.e_offset, &hdr);
1571 		if (!skb) {
1572 			*done = 1;
1573 			break;
1574 		}
1575 		skb->dev = card->dev;
1576 		switch (hdr->hdr.l3.id) {
1577 		case QETH_HEADER_TYPE_LAYER3:
1578 			magic = *(__u16 *)skb->data;
1579 			if ((card->info.type == QETH_CARD_TYPE_IQD) &&
1580 			    (magic == ETH_P_AF_IUCV)) {
1581 				skb->protocol = cpu_to_be16(ETH_P_AF_IUCV);
1582 				skb->pkt_type = PACKET_HOST;
1583 				skb->mac_header = NET_SKB_PAD;
1584 				skb->dev = card->dev;
1585 				len = skb->len;
1586 				card->dev->header_ops->create(skb, card->dev, 0,
1587 					card->dev->dev_addr, "FAKELL",
1588 					card->dev->addr_len);
1589 				netif_receive_skb(skb);
1590 			} else {
1591 				qeth_l3_rebuild_skb(card, skb, hdr);
1592 				len = skb->len;
1593 				napi_gro_receive(&card->napi, skb);
1594 			}
1595 			break;
1596 		case QETH_HEADER_TYPE_LAYER2: /* for HiperSockets sniffer */
1597 			skb->pkt_type = PACKET_HOST;
1598 			skb->protocol = eth_type_trans(skb, skb->dev);
1599 			len = skb->len;
1600 			netif_receive_skb(skb);
1601 			break;
1602 		default:
1603 			dev_kfree_skb_any(skb);
1604 			QETH_CARD_TEXT(card, 3, "inbunkno");
1605 			QETH_DBF_HEX(CTRL, 3, hdr, QETH_DBF_CTRL_LEN);
1606 			continue;
1607 		}
1608 		work_done++;
1609 		budget--;
1610 		card->stats.rx_packets++;
1611 		card->stats.rx_bytes += len;
1612 	}
1613 	return work_done;
1614 }
1615 
1616 static int qeth_l3_verify_vlan_dev(struct net_device *dev,
1617 			struct qeth_card *card)
1618 {
1619 	int rc = 0;
1620 	u16 vid;
1621 
1622 	for_each_set_bit(vid, card->active_vlans, VLAN_N_VID) {
1623 		struct net_device *netdev;
1624 
1625 		rcu_read_lock();
1626 		netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q),
1627 					      vid);
1628 		rcu_read_unlock();
1629 		if (netdev == dev) {
1630 			rc = QETH_VLAN_CARD;
1631 			break;
1632 		}
1633 	}
1634 
1635 	if (rc && !(vlan_dev_real_dev(dev)->ml_priv == (void *)card))
1636 		return 0;
1637 
1638 	return rc;
1639 }
1640 
1641 static int qeth_l3_verify_dev(struct net_device *dev)
1642 {
1643 	struct qeth_card *card;
1644 	int rc = 0;
1645 	unsigned long flags;
1646 
1647 	read_lock_irqsave(&qeth_core_card_list.rwlock, flags);
1648 	list_for_each_entry(card, &qeth_core_card_list.list, list) {
1649 		if (card->dev == dev) {
1650 			rc = QETH_REAL_CARD;
1651 			break;
1652 		}
1653 		rc = qeth_l3_verify_vlan_dev(dev, card);
1654 		if (rc)
1655 			break;
1656 	}
1657 	read_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
1658 
1659 	return rc;
1660 }
1661 
1662 static struct qeth_card *qeth_l3_get_card_from_dev(struct net_device *dev)
1663 {
1664 	struct qeth_card *card = NULL;
1665 	int rc;
1666 
1667 	rc = qeth_l3_verify_dev(dev);
1668 	if (rc == QETH_REAL_CARD)
1669 		card = dev->ml_priv;
1670 	else if (rc == QETH_VLAN_CARD)
1671 		card = vlan_dev_real_dev(dev)->ml_priv;
1672 	if (card && card->options.layer2)
1673 		card = NULL;
1674 	if (card)
1675 		QETH_CARD_TEXT_(card, 4, "%d", rc);
1676 	return card ;
1677 }
1678 
1679 static void qeth_l3_stop_card(struct qeth_card *card, int recovery_mode)
1680 {
1681 	QETH_DBF_TEXT(SETUP, 2, "stopcard");
1682 	QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1683 
1684 	qeth_set_allowed_threads(card, 0, 1);
1685 	if (card->options.sniffer &&
1686 	    (card->info.promisc_mode == SET_PROMISC_MODE_ON))
1687 		qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
1688 	if (card->read.state == CH_STATE_UP &&
1689 	    card->write.state == CH_STATE_UP &&
1690 	    (card->state == CARD_STATE_UP)) {
1691 		if (recovery_mode)
1692 			qeth_l3_stop(card->dev);
1693 		else {
1694 			rtnl_lock();
1695 			dev_close(card->dev);
1696 			rtnl_unlock();
1697 		}
1698 		card->state = CARD_STATE_SOFTSETUP;
1699 	}
1700 	if (card->state == CARD_STATE_SOFTSETUP) {
1701 		qeth_l3_clear_ip_htable(card, 1);
1702 		qeth_clear_ipacmd_list(card);
1703 		card->state = CARD_STATE_HARDSETUP;
1704 	}
1705 	if (card->state == CARD_STATE_HARDSETUP) {
1706 		qeth_qdio_clear_card(card, 0);
1707 		qeth_clear_qdio_buffers(card);
1708 		qeth_clear_working_pool_list(card);
1709 		card->state = CARD_STATE_DOWN;
1710 	}
1711 	if (card->state == CARD_STATE_DOWN) {
1712 		qeth_clear_cmd_buffers(&card->read);
1713 		qeth_clear_cmd_buffers(&card->write);
1714 	}
1715 }
1716 
1717 /*
1718  * test for and Switch promiscuous mode (on or off)
1719  *  either for guestlan or HiperSocket Sniffer
1720  */
1721 static void
1722 qeth_l3_handle_promisc_mode(struct qeth_card *card)
1723 {
1724 	struct net_device *dev = card->dev;
1725 
1726 	if (((dev->flags & IFF_PROMISC) &&
1727 	     (card->info.promisc_mode == SET_PROMISC_MODE_ON)) ||
1728 	    (!(dev->flags & IFF_PROMISC) &&
1729 	     (card->info.promisc_mode == SET_PROMISC_MODE_OFF)))
1730 		return;
1731 
1732 	if (card->info.guestlan) {		/* Guestlan trace */
1733 		if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
1734 			qeth_setadp_promisc_mode(card);
1735 	} else if (card->options.sniffer &&	/* HiperSockets trace */
1736 		   qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) {
1737 		if (dev->flags & IFF_PROMISC) {
1738 			QETH_CARD_TEXT(card, 3, "+promisc");
1739 			qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_ENABLE);
1740 		} else {
1741 			QETH_CARD_TEXT(card, 3, "-promisc");
1742 			qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
1743 		}
1744 	}
1745 }
1746 
1747 static void qeth_l3_set_rx_mode(struct net_device *dev)
1748 {
1749 	struct qeth_card *card = dev->ml_priv;
1750 	struct qeth_ipaddr *addr;
1751 	struct hlist_node *tmp;
1752 	int i, rc;
1753 
1754 	QETH_CARD_TEXT(card, 3, "setmulti");
1755 	if (qeth_threads_running(card, QETH_RECOVER_THREAD) &&
1756 	    (card->state != CARD_STATE_UP))
1757 		return;
1758 	if (!card->options.sniffer) {
1759 		spin_lock_bh(&card->mclock);
1760 
1761 		qeth_l3_add_multicast_ipv4(card);
1762 		qeth_l3_add_multicast_ipv6(card);
1763 
1764 		hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
1765 			switch (addr->disp_flag) {
1766 			case QETH_DISP_ADDR_DELETE:
1767 				rc = qeth_l3_deregister_addr_entry(card, addr);
1768 				if (!rc || rc == IPA_RC_MC_ADDR_NOT_FOUND) {
1769 					hash_del(&addr->hnode);
1770 					kfree(addr);
1771 				}
1772 				break;
1773 			case QETH_DISP_ADDR_ADD:
1774 				rc = qeth_l3_register_addr_entry(card, addr);
1775 				if (rc && rc != IPA_RC_LAN_OFFLINE) {
1776 					hash_del(&addr->hnode);
1777 					kfree(addr);
1778 					break;
1779 				}
1780 				addr->ref_counter = 1;
1781 				/* fall through */
1782 			default:
1783 				/* for next call to set_rx_mode(): */
1784 				addr->disp_flag = QETH_DISP_ADDR_DELETE;
1785 			}
1786 		}
1787 
1788 		spin_unlock_bh(&card->mclock);
1789 
1790 		if (!qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
1791 			return;
1792 	}
1793 	qeth_l3_handle_promisc_mode(card);
1794 }
1795 
1796 static const char *qeth_l3_arp_get_error_cause(int *rc)
1797 {
1798 	switch (*rc) {
1799 	case QETH_IPA_ARP_RC_FAILED:
1800 		*rc = -EIO;
1801 		return "operation failed";
1802 	case QETH_IPA_ARP_RC_NOTSUPP:
1803 		*rc = -EOPNOTSUPP;
1804 		return "operation not supported";
1805 	case QETH_IPA_ARP_RC_OUT_OF_RANGE:
1806 		*rc = -EINVAL;
1807 		return "argument out of range";
1808 	case QETH_IPA_ARP_RC_Q_NOTSUPP:
1809 		*rc = -EOPNOTSUPP;
1810 		return "query operation not supported";
1811 	case QETH_IPA_ARP_RC_Q_NO_DATA:
1812 		*rc = -ENOENT;
1813 		return "no query data available";
1814 	default:
1815 		return "unknown error";
1816 	}
1817 }
1818 
1819 static int qeth_l3_arp_set_no_entries(struct qeth_card *card, int no_entries)
1820 {
1821 	int tmp;
1822 	int rc;
1823 
1824 	QETH_CARD_TEXT(card, 3, "arpstnoe");
1825 
1826 	/*
1827 	 * currently GuestLAN only supports the ARP assist function
1828 	 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_SET_NO_ENTRIES;
1829 	 * thus we say EOPNOTSUPP for this ARP function
1830 	 */
1831 	if (card->info.guestlan)
1832 		return -EOPNOTSUPP;
1833 	if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1834 		return -EOPNOTSUPP;
1835 	}
1836 	rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
1837 					  IPA_CMD_ASS_ARP_SET_NO_ENTRIES,
1838 					  no_entries);
1839 	if (rc) {
1840 		tmp = rc;
1841 		QETH_DBF_MESSAGE(2, "Could not set number of ARP entries on "
1842 			"%s: %s (0x%x/%d)\n", QETH_CARD_IFNAME(card),
1843 			qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
1844 	}
1845 	return rc;
1846 }
1847 
1848 static __u32 get_arp_entry_size(struct qeth_card *card,
1849 			struct qeth_arp_query_data *qdata,
1850 			struct qeth_arp_entrytype *type, __u8 strip_entries)
1851 {
1852 	__u32 rc;
1853 	__u8 is_hsi;
1854 
1855 	is_hsi = qdata->reply_bits == 5;
1856 	if (type->ip == QETHARP_IP_ADDR_V4) {
1857 		QETH_CARD_TEXT(card, 4, "arpev4");
1858 		if (strip_entries) {
1859 			rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5_short) :
1860 				sizeof(struct qeth_arp_qi_entry7_short);
1861 		} else {
1862 			rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5) :
1863 				sizeof(struct qeth_arp_qi_entry7);
1864 		}
1865 	} else if (type->ip == QETHARP_IP_ADDR_V6) {
1866 		QETH_CARD_TEXT(card, 4, "arpev6");
1867 		if (strip_entries) {
1868 			rc = is_hsi ?
1869 				sizeof(struct qeth_arp_qi_entry5_short_ipv6) :
1870 				sizeof(struct qeth_arp_qi_entry7_short_ipv6);
1871 		} else {
1872 			rc = is_hsi ?
1873 				sizeof(struct qeth_arp_qi_entry5_ipv6) :
1874 				sizeof(struct qeth_arp_qi_entry7_ipv6);
1875 		}
1876 	} else {
1877 		QETH_CARD_TEXT(card, 4, "arpinv");
1878 		rc = 0;
1879 	}
1880 
1881 	return rc;
1882 }
1883 
1884 static int arpentry_matches_prot(struct qeth_arp_entrytype *type, __u16 prot)
1885 {
1886 	return (type->ip == QETHARP_IP_ADDR_V4 && prot == QETH_PROT_IPV4) ||
1887 		(type->ip == QETHARP_IP_ADDR_V6 && prot == QETH_PROT_IPV6);
1888 }
1889 
1890 static int qeth_l3_arp_query_cb(struct qeth_card *card,
1891 		struct qeth_reply *reply, unsigned long data)
1892 {
1893 	struct qeth_ipa_cmd *cmd;
1894 	struct qeth_arp_query_data *qdata;
1895 	struct qeth_arp_query_info *qinfo;
1896 	int i;
1897 	int e;
1898 	int entrybytes_done;
1899 	int stripped_bytes;
1900 	__u8 do_strip_entries;
1901 
1902 	QETH_CARD_TEXT(card, 3, "arpquecb");
1903 
1904 	qinfo = (struct qeth_arp_query_info *) reply->param;
1905 	cmd = (struct qeth_ipa_cmd *) data;
1906 	QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.prot_version);
1907 	if (cmd->hdr.return_code) {
1908 		QETH_CARD_TEXT(card, 4, "arpcberr");
1909 		QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1910 		return 0;
1911 	}
1912 	if (cmd->data.setassparms.hdr.return_code) {
1913 		cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
1914 		QETH_CARD_TEXT(card, 4, "setaperr");
1915 		QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1916 		return 0;
1917 	}
1918 	qdata = &cmd->data.setassparms.data.query_arp;
1919 	QETH_CARD_TEXT_(card, 4, "anoen%i", qdata->no_entries);
1920 
1921 	do_strip_entries = (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) > 0;
1922 	stripped_bytes = do_strip_entries ? QETH_QARP_MEDIASPECIFIC_BYTES : 0;
1923 	entrybytes_done = 0;
1924 	for (e = 0; e < qdata->no_entries; ++e) {
1925 		char *cur_entry;
1926 		__u32 esize;
1927 		struct qeth_arp_entrytype *etype;
1928 
1929 		cur_entry = &qdata->data + entrybytes_done;
1930 		etype = &((struct qeth_arp_qi_entry5 *) cur_entry)->type;
1931 		if (!arpentry_matches_prot(etype, cmd->hdr.prot_version)) {
1932 			QETH_CARD_TEXT(card, 4, "pmis");
1933 			QETH_CARD_TEXT_(card, 4, "%i", etype->ip);
1934 			break;
1935 		}
1936 		esize = get_arp_entry_size(card, qdata, etype,
1937 			do_strip_entries);
1938 		QETH_CARD_TEXT_(card, 5, "esz%i", esize);
1939 		if (!esize)
1940 			break;
1941 
1942 		if ((qinfo->udata_len - qinfo->udata_offset) < esize) {
1943 			QETH_CARD_TEXT_(card, 4, "qaer3%i", -ENOMEM);
1944 			cmd->hdr.return_code = IPA_RC_ENOMEM;
1945 			goto out_error;
1946 		}
1947 
1948 		memcpy(qinfo->udata + qinfo->udata_offset,
1949 			&qdata->data + entrybytes_done + stripped_bytes,
1950 			esize);
1951 		entrybytes_done += esize + stripped_bytes;
1952 		qinfo->udata_offset += esize;
1953 		++qinfo->no_entries;
1954 	}
1955 	/* check if all replies received ... */
1956 	if (cmd->data.setassparms.hdr.seq_no <
1957 	    cmd->data.setassparms.hdr.number_of_replies)
1958 		return 1;
1959 	QETH_CARD_TEXT_(card, 4, "nove%i", qinfo->no_entries);
1960 	memcpy(qinfo->udata, &qinfo->no_entries, 4);
1961 	/* keep STRIP_ENTRIES flag so the user program can distinguish
1962 	 * stripped entries from normal ones */
1963 	if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
1964 		qdata->reply_bits |= QETH_QARP_STRIP_ENTRIES;
1965 	memcpy(qinfo->udata + QETH_QARP_MASK_OFFSET, &qdata->reply_bits, 2);
1966 	QETH_CARD_TEXT_(card, 4, "rc%i", 0);
1967 	return 0;
1968 out_error:
1969 	i = 0;
1970 	memcpy(qinfo->udata, &i, 4);
1971 	return 0;
1972 }
1973 
1974 static int qeth_l3_send_ipa_arp_cmd(struct qeth_card *card,
1975 		struct qeth_cmd_buffer *iob, int len,
1976 		int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
1977 			unsigned long),
1978 		void *reply_param)
1979 {
1980 	QETH_CARD_TEXT(card, 4, "sendarp");
1981 
1982 	memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
1983 	memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
1984 	       &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
1985 	return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
1986 				      reply_cb, reply_param);
1987 }
1988 
1989 static int qeth_l3_query_arp_cache_info(struct qeth_card *card,
1990 	enum qeth_prot_versions prot,
1991 	struct qeth_arp_query_info *qinfo)
1992 {
1993 	struct qeth_cmd_buffer *iob;
1994 	struct qeth_ipa_cmd *cmd;
1995 	int tmp;
1996 	int rc;
1997 
1998 	QETH_CARD_TEXT_(card, 3, "qarpipv%i", prot);
1999 
2000 	iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
2001 				       IPA_CMD_ASS_ARP_QUERY_INFO,
2002 				       sizeof(struct qeth_arp_query_data)
2003 						- sizeof(char),
2004 				       prot);
2005 	if (!iob)
2006 		return -ENOMEM;
2007 	cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2008 	cmd->data.setassparms.data.query_arp.request_bits = 0x000F;
2009 	cmd->data.setassparms.data.query_arp.reply_bits = 0;
2010 	cmd->data.setassparms.data.query_arp.no_entries = 0;
2011 	rc = qeth_l3_send_ipa_arp_cmd(card, iob,
2012 			   QETH_SETASS_BASE_LEN+QETH_ARP_CMD_LEN,
2013 			   qeth_l3_arp_query_cb, (void *)qinfo);
2014 	if (rc) {
2015 		tmp = rc;
2016 		QETH_DBF_MESSAGE(2,
2017 			"Error while querying ARP cache on %s: %s "
2018 			"(0x%x/%d)\n", QETH_CARD_IFNAME(card),
2019 			qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2020 	}
2021 
2022 	return rc;
2023 }
2024 
2025 static int qeth_l3_arp_query(struct qeth_card *card, char __user *udata)
2026 {
2027 	struct qeth_arp_query_info qinfo = {0, };
2028 	int rc;
2029 
2030 	QETH_CARD_TEXT(card, 3, "arpquery");
2031 
2032 	if (!qeth_is_supported(card,/*IPA_QUERY_ARP_ADDR_INFO*/
2033 			       IPA_ARP_PROCESSING)) {
2034 		QETH_CARD_TEXT(card, 3, "arpqnsup");
2035 		rc = -EOPNOTSUPP;
2036 		goto out;
2037 	}
2038 	/* get size of userspace buffer and mask_bits -> 6 bytes */
2039 	if (copy_from_user(&qinfo, udata, 6)) {
2040 		rc = -EFAULT;
2041 		goto out;
2042 	}
2043 	qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
2044 	if (!qinfo.udata) {
2045 		rc = -ENOMEM;
2046 		goto out;
2047 	}
2048 	qinfo.udata_offset = QETH_QARP_ENTRIES_OFFSET;
2049 	rc = qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV4, &qinfo);
2050 	if (rc) {
2051 		if (copy_to_user(udata, qinfo.udata, 4))
2052 			rc = -EFAULT;
2053 		goto free_and_out;
2054 	}
2055 	if (qinfo.mask_bits & QETH_QARP_WITH_IPV6) {
2056 		/* fails in case of GuestLAN QDIO mode */
2057 		qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV6, &qinfo);
2058 	}
2059 	if (copy_to_user(udata, qinfo.udata, qinfo.udata_len)) {
2060 		QETH_CARD_TEXT(card, 4, "qactf");
2061 		rc = -EFAULT;
2062 		goto free_and_out;
2063 	}
2064 	QETH_CARD_TEXT(card, 4, "qacts");
2065 
2066 free_and_out:
2067 	kfree(qinfo.udata);
2068 out:
2069 	return rc;
2070 }
2071 
2072 static int qeth_l3_arp_add_entry(struct qeth_card *card,
2073 				struct qeth_arp_cache_entry *entry)
2074 {
2075 	struct qeth_cmd_buffer *iob;
2076 	char buf[16];
2077 	int tmp;
2078 	int rc;
2079 
2080 	QETH_CARD_TEXT(card, 3, "arpadent");
2081 
2082 	/*
2083 	 * currently GuestLAN only supports the ARP assist function
2084 	 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_ADD_ENTRY;
2085 	 * thus we say EOPNOTSUPP for this ARP function
2086 	 */
2087 	if (card->info.guestlan)
2088 		return -EOPNOTSUPP;
2089 	if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
2090 		return -EOPNOTSUPP;
2091 	}
2092 
2093 	iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
2094 				       IPA_CMD_ASS_ARP_ADD_ENTRY,
2095 				       sizeof(struct qeth_arp_cache_entry),
2096 				       QETH_PROT_IPV4);
2097 	if (!iob)
2098 		return -ENOMEM;
2099 	rc = qeth_send_setassparms(card, iob,
2100 				   sizeof(struct qeth_arp_cache_entry),
2101 				   (unsigned long) entry,
2102 				   qeth_setassparms_cb, NULL);
2103 	if (rc) {
2104 		tmp = rc;
2105 		qeth_l3_ipaddr4_to_string((u8 *)entry->ipaddr, buf);
2106 		QETH_DBF_MESSAGE(2, "Could not add ARP entry for address %s "
2107 			"on %s: %s (0x%x/%d)\n", buf, QETH_CARD_IFNAME(card),
2108 			qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2109 	}
2110 	return rc;
2111 }
2112 
2113 static int qeth_l3_arp_remove_entry(struct qeth_card *card,
2114 				struct qeth_arp_cache_entry *entry)
2115 {
2116 	struct qeth_cmd_buffer *iob;
2117 	char buf[16] = {0, };
2118 	int tmp;
2119 	int rc;
2120 
2121 	QETH_CARD_TEXT(card, 3, "arprment");
2122 
2123 	/*
2124 	 * currently GuestLAN only supports the ARP assist function
2125 	 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_REMOVE_ENTRY;
2126 	 * thus we say EOPNOTSUPP for this ARP function
2127 	 */
2128 	if (card->info.guestlan)
2129 		return -EOPNOTSUPP;
2130 	if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
2131 		return -EOPNOTSUPP;
2132 	}
2133 	memcpy(buf, entry, 12);
2134 	iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
2135 				       IPA_CMD_ASS_ARP_REMOVE_ENTRY,
2136 				       12,
2137 				       QETH_PROT_IPV4);
2138 	if (!iob)
2139 		return -ENOMEM;
2140 	rc = qeth_send_setassparms(card, iob,
2141 				   12, (unsigned long)buf,
2142 				   qeth_setassparms_cb, NULL);
2143 	if (rc) {
2144 		tmp = rc;
2145 		memset(buf, 0, 16);
2146 		qeth_l3_ipaddr4_to_string((u8 *)entry->ipaddr, buf);
2147 		QETH_DBF_MESSAGE(2, "Could not delete ARP entry for address %s"
2148 			" on %s: %s (0x%x/%d)\n", buf, QETH_CARD_IFNAME(card),
2149 			qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2150 	}
2151 	return rc;
2152 }
2153 
2154 static int qeth_l3_arp_flush_cache(struct qeth_card *card)
2155 {
2156 	int rc;
2157 	int tmp;
2158 
2159 	QETH_CARD_TEXT(card, 3, "arpflush");
2160 
2161 	/*
2162 	 * currently GuestLAN only supports the ARP assist function
2163 	 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_FLUSH_CACHE;
2164 	 * thus we say EOPNOTSUPP for this ARP function
2165 	*/
2166 	if (card->info.guestlan || (card->info.type == QETH_CARD_TYPE_IQD))
2167 		return -EOPNOTSUPP;
2168 	if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
2169 		return -EOPNOTSUPP;
2170 	}
2171 	rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
2172 					  IPA_CMD_ASS_ARP_FLUSH_CACHE, 0);
2173 	if (rc) {
2174 		tmp = rc;
2175 		QETH_DBF_MESSAGE(2, "Could not flush ARP cache on %s: %s "
2176 			"(0x%x/%d)\n", QETH_CARD_IFNAME(card),
2177 			qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2178 	}
2179 	return rc;
2180 }
2181 
2182 static int qeth_l3_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2183 {
2184 	struct qeth_card *card = dev->ml_priv;
2185 	struct qeth_arp_cache_entry arp_entry;
2186 	int rc = 0;
2187 
2188 	switch (cmd) {
2189 	case SIOC_QETH_ARP_SET_NO_ENTRIES:
2190 		if (!capable(CAP_NET_ADMIN)) {
2191 			rc = -EPERM;
2192 			break;
2193 		}
2194 		rc = qeth_l3_arp_set_no_entries(card, rq->ifr_ifru.ifru_ivalue);
2195 		break;
2196 	case SIOC_QETH_ARP_QUERY_INFO:
2197 		if (!capable(CAP_NET_ADMIN)) {
2198 			rc = -EPERM;
2199 			break;
2200 		}
2201 		rc = qeth_l3_arp_query(card, rq->ifr_ifru.ifru_data);
2202 		break;
2203 	case SIOC_QETH_ARP_ADD_ENTRY:
2204 		if (!capable(CAP_NET_ADMIN)) {
2205 			rc = -EPERM;
2206 			break;
2207 		}
2208 		if (copy_from_user(&arp_entry, rq->ifr_ifru.ifru_data,
2209 				   sizeof(struct qeth_arp_cache_entry)))
2210 			rc = -EFAULT;
2211 		else
2212 			rc = qeth_l3_arp_add_entry(card, &arp_entry);
2213 		break;
2214 	case SIOC_QETH_ARP_REMOVE_ENTRY:
2215 		if (!capable(CAP_NET_ADMIN)) {
2216 			rc = -EPERM;
2217 			break;
2218 		}
2219 		if (copy_from_user(&arp_entry, rq->ifr_ifru.ifru_data,
2220 				   sizeof(struct qeth_arp_cache_entry)))
2221 			rc = -EFAULT;
2222 		else
2223 			rc = qeth_l3_arp_remove_entry(card, &arp_entry);
2224 		break;
2225 	case SIOC_QETH_ARP_FLUSH_CACHE:
2226 		if (!capable(CAP_NET_ADMIN)) {
2227 			rc = -EPERM;
2228 			break;
2229 		}
2230 		rc = qeth_l3_arp_flush_cache(card);
2231 		break;
2232 	default:
2233 		rc = -EOPNOTSUPP;
2234 	}
2235 	return rc;
2236 }
2237 
2238 static int qeth_l3_get_cast_type(struct sk_buff *skb)
2239 {
2240 	struct neighbour *n = NULL;
2241 	struct dst_entry *dst;
2242 
2243 	rcu_read_lock();
2244 	dst = skb_dst(skb);
2245 	if (dst)
2246 		n = dst_neigh_lookup_skb(dst, skb);
2247 	if (n) {
2248 		int cast_type = n->type;
2249 
2250 		rcu_read_unlock();
2251 		neigh_release(n);
2252 		if ((cast_type == RTN_BROADCAST) ||
2253 		    (cast_type == RTN_MULTICAST) ||
2254 		    (cast_type == RTN_ANYCAST))
2255 			return cast_type;
2256 		return RTN_UNSPEC;
2257 	}
2258 	rcu_read_unlock();
2259 
2260 	/* no neighbour (eg AF_PACKET), fall back to target's IP address ... */
2261 	if (be16_to_cpu(skb->protocol) == ETH_P_IPV6)
2262 		return ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ?
2263 				RTN_MULTICAST : RTN_UNSPEC;
2264 	else if (be16_to_cpu(skb->protocol) == ETH_P_IP)
2265 		return ipv4_is_multicast(ip_hdr(skb)->daddr) ?
2266 				RTN_MULTICAST : RTN_UNSPEC;
2267 
2268 	/* ... and MAC address */
2269 	if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest, skb->dev->broadcast))
2270 		return RTN_BROADCAST;
2271 	if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2272 		return RTN_MULTICAST;
2273 
2274 	/* default to unicast */
2275 	return RTN_UNSPEC;
2276 }
2277 
2278 static void qeth_l3_fill_af_iucv_hdr(struct qeth_card *card,
2279 		struct qeth_hdr *hdr, struct sk_buff *skb)
2280 {
2281 	char daddr[16];
2282 	struct af_iucv_trans_hdr *iucv_hdr;
2283 
2284 	memset(hdr, 0, sizeof(struct qeth_hdr));
2285 	hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
2286 	hdr->hdr.l3.ext_flags = 0;
2287 	hdr->hdr.l3.length = skb->len - ETH_HLEN;
2288 	hdr->hdr.l3.flags = QETH_HDR_IPV6 | QETH_CAST_UNICAST;
2289 
2290 	iucv_hdr = (struct af_iucv_trans_hdr *) (skb->data + ETH_HLEN);
2291 	memset(daddr, 0, sizeof(daddr));
2292 	daddr[0] = 0xfe;
2293 	daddr[1] = 0x80;
2294 	memcpy(&daddr[8], iucv_hdr->destUserID, 8);
2295 	memcpy(hdr->hdr.l3.next_hop.ipv6_addr, daddr, 16);
2296 }
2297 
2298 static u8 qeth_l3_cast_type_to_flag(int cast_type)
2299 {
2300 	if (cast_type == RTN_MULTICAST)
2301 		return QETH_CAST_MULTICAST;
2302 	if (cast_type == RTN_ANYCAST)
2303 		return QETH_CAST_ANYCAST;
2304 	if (cast_type == RTN_BROADCAST)
2305 		return QETH_CAST_BROADCAST;
2306 	return QETH_CAST_UNICAST;
2307 }
2308 
2309 static void qeth_l3_fill_header(struct qeth_card *card, struct qeth_hdr *hdr,
2310 				struct sk_buff *skb, int ipv, int cast_type,
2311 				unsigned int data_len)
2312 {
2313 	memset(hdr, 0, sizeof(struct qeth_hdr));
2314 	hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
2315 	hdr->hdr.l3.length = data_len;
2316 
2317 	/*
2318 	 * before we're going to overwrite this location with next hop ip.
2319 	 * v6 uses passthrough, v4 sets the tag in the QDIO header.
2320 	 */
2321 	if (skb_vlan_tag_present(skb)) {
2322 		if ((ipv == 4) || (card->info.type == QETH_CARD_TYPE_IQD))
2323 			hdr->hdr.l3.ext_flags = QETH_HDR_EXT_VLAN_FRAME;
2324 		else
2325 			hdr->hdr.l3.ext_flags = QETH_HDR_EXT_INCLUDE_VLAN_TAG;
2326 		hdr->hdr.l3.vlan_id = skb_vlan_tag_get(skb);
2327 	}
2328 
2329 	/* OSA only: */
2330 	if (!ipv) {
2331 		hdr->hdr.l3.flags = QETH_HDR_PASSTHRU;
2332 		if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest,
2333 					    skb->dev->broadcast))
2334 			hdr->hdr.l3.flags |= QETH_CAST_BROADCAST;
2335 		else
2336 			hdr->hdr.l3.flags |= (cast_type == RTN_MULTICAST) ?
2337 				QETH_CAST_MULTICAST : QETH_CAST_UNICAST;
2338 		return;
2339 	}
2340 
2341 	hdr->hdr.l3.flags = qeth_l3_cast_type_to_flag(cast_type);
2342 	rcu_read_lock();
2343 	if (ipv == 4) {
2344 		struct rtable *rt = skb_rtable(skb);
2345 
2346 		*((__be32 *) &hdr->hdr.l3.next_hop.ipv4.addr) = (rt) ?
2347 				rt_nexthop(rt, ip_hdr(skb)->daddr) :
2348 				ip_hdr(skb)->daddr;
2349 	} else {
2350 		/* IPv6 */
2351 		const struct rt6_info *rt = skb_rt6_info(skb);
2352 		const struct in6_addr *next_hop;
2353 
2354 		if (rt && !ipv6_addr_any(&rt->rt6i_gateway))
2355 			next_hop = &rt->rt6i_gateway;
2356 		else
2357 			next_hop = &ipv6_hdr(skb)->daddr;
2358 		memcpy(hdr->hdr.l3.next_hop.ipv6_addr, next_hop, 16);
2359 
2360 		hdr->hdr.l3.flags |= QETH_HDR_IPV6;
2361 		if (card->info.type != QETH_CARD_TYPE_IQD)
2362 			hdr->hdr.l3.flags |= QETH_HDR_PASSTHRU;
2363 	}
2364 	rcu_read_unlock();
2365 }
2366 
2367 static void qeth_l3_hdr_csum(struct qeth_card *card, struct qeth_hdr *hdr,
2368 			     struct sk_buff *skb)
2369 {
2370 	struct iphdr *iph = ip_hdr(skb);
2371 
2372 	/* tcph->check contains already the pseudo hdr checksum
2373 	 * so just set the header flags
2374 	 */
2375 	if (iph->protocol == IPPROTO_UDP)
2376 		hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_UDP;
2377 	hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_CSUM_TRANSP_REQ |
2378 		QETH_HDR_EXT_CSUM_HDR_REQ;
2379 	iph->check = 0;
2380 	if (card->options.performance_stats)
2381 		card->perf_stats.tx_csum++;
2382 }
2383 
2384 static void qeth_tso_fill_header(struct qeth_card *card,
2385 		struct qeth_hdr *qhdr, struct sk_buff *skb)
2386 {
2387 	struct qeth_hdr_tso *hdr = (struct qeth_hdr_tso *)qhdr;
2388 	struct tcphdr *tcph = tcp_hdr(skb);
2389 	struct iphdr *iph = ip_hdr(skb);
2390 	struct ipv6hdr *ip6h = ipv6_hdr(skb);
2391 
2392 	/*fix header to TSO values ...*/
2393 	hdr->hdr.hdr.l3.id = QETH_HEADER_TYPE_TSO;
2394 	/*set values which are fix for the first approach ...*/
2395 	hdr->ext.hdr_tot_len = (__u16) sizeof(struct qeth_hdr_ext_tso);
2396 	hdr->ext.imb_hdr_no  = 1;
2397 	hdr->ext.hdr_type    = 1;
2398 	hdr->ext.hdr_version = 1;
2399 	hdr->ext.hdr_len     = 28;
2400 	/*insert non-fix values */
2401 	hdr->ext.mss = skb_shinfo(skb)->gso_size;
2402 	hdr->ext.dg_hdr_len = (__u16)(ip_hdrlen(skb) + tcp_hdrlen(skb));
2403 	hdr->ext.payload_len = (__u16)(skb->len - hdr->ext.dg_hdr_len -
2404 				       sizeof(struct qeth_hdr_tso));
2405 	tcph->check = 0;
2406 	if (be16_to_cpu(skb->protocol) == ETH_P_IPV6) {
2407 		ip6h->payload_len = 0;
2408 		tcph->check = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
2409 					       0, IPPROTO_TCP, 0);
2410 	} else {
2411 		/*OSA want us to set these values ...*/
2412 		tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
2413 					 0, IPPROTO_TCP, 0);
2414 		iph->tot_len = 0;
2415 		iph->check = 0;
2416 	}
2417 }
2418 
2419 /**
2420  * qeth_l3_get_elements_no_tso() - find number of SBALEs for skb data for tso
2421  * @card:			   qeth card structure, to check max. elems.
2422  * @skb:			   SKB address
2423  * @extra_elems:		   extra elems needed, to check against max.
2424  *
2425  * Returns the number of pages, and thus QDIO buffer elements, needed to cover
2426  * skb data, including linear part and fragments, but excluding TCP header.
2427  * (Exclusion of TCP header distinguishes it from qeth_get_elements_no().)
2428  * Checks if the result plus extra_elems fits under the limit for the card.
2429  * Returns 0 if it does not.
2430  * Note: extra_elems is not included in the returned result.
2431  */
2432 static int qeth_l3_get_elements_no_tso(struct qeth_card *card,
2433 			struct sk_buff *skb, int extra_elems)
2434 {
2435 	addr_t start = (addr_t)tcp_hdr(skb) + tcp_hdrlen(skb);
2436 	addr_t end = (addr_t)skb->data + skb_headlen(skb);
2437 	int elements = qeth_get_elements_for_frags(skb);
2438 
2439 	if (start != end)
2440 		elements += qeth_get_elements_for_range(start, end);
2441 
2442 	if ((elements + extra_elems) > QETH_MAX_BUFFER_ELEMENTS(card)) {
2443 		QETH_DBF_MESSAGE(2,
2444 	"Invalid size of TSO IP packet (Number=%d / Length=%d). Discarded.\n",
2445 				elements + extra_elems, skb->len);
2446 		return 0;
2447 	}
2448 	return elements;
2449 }
2450 
2451 static netdev_tx_t qeth_l3_hard_start_xmit(struct sk_buff *skb,
2452 					   struct net_device *dev)
2453 {
2454 	int rc;
2455 	__be16 *tag;
2456 	struct qeth_hdr *hdr = NULL;
2457 	int hdr_elements = 0;
2458 	int elements;
2459 	struct qeth_card *card = dev->ml_priv;
2460 	struct sk_buff *new_skb = NULL;
2461 	int ipv = qeth_get_ip_version(skb);
2462 	int cast_type = qeth_l3_get_cast_type(skb);
2463 	struct qeth_qdio_out_q *queue =
2464 		card->qdio.out_qs[card->qdio.do_prio_queueing
2465 			|| (cast_type && card->info.is_multicast_different) ?
2466 			qeth_get_priority_queue(card, skb, ipv, cast_type) :
2467 			card->qdio.default_out_queue];
2468 	int tx_bytes = skb->len;
2469 	unsigned int hd_len = 0;
2470 	bool use_tso;
2471 	int data_offset = -1;
2472 	unsigned int nr_frags;
2473 
2474 	if (((card->info.type == QETH_CARD_TYPE_IQD) &&
2475 	     (((card->options.cq != QETH_CQ_ENABLED) && !ipv) ||
2476 	      ((card->options.cq == QETH_CQ_ENABLED) &&
2477 	       (be16_to_cpu(skb->protocol) != ETH_P_AF_IUCV)))) ||
2478 	    card->options.sniffer)
2479 			goto tx_drop;
2480 
2481 	if ((card->state != CARD_STATE_UP) || !card->lan_online) {
2482 		card->stats.tx_carrier_errors++;
2483 		goto tx_drop;
2484 	}
2485 
2486 	if ((cast_type == RTN_BROADCAST) &&
2487 	    (card->info.broadcast_capable == 0))
2488 		goto tx_drop;
2489 
2490 	if (card->options.performance_stats) {
2491 		card->perf_stats.outbound_cnt++;
2492 		card->perf_stats.outbound_start_time = qeth_get_micros();
2493 	}
2494 
2495 	/* Ignore segment size from skb_is_gso(), 1 page is always used. */
2496 	use_tso = skb_is_gso(skb) &&
2497 		  (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4);
2498 
2499 	if (card->info.type == QETH_CARD_TYPE_IQD) {
2500 		new_skb = skb;
2501 		data_offset = ETH_HLEN;
2502 		hd_len = sizeof(*hdr);
2503 		hdr = kmem_cache_alloc(qeth_core_header_cache, GFP_ATOMIC);
2504 		if (!hdr)
2505 			goto tx_drop;
2506 		hdr_elements++;
2507 	} else {
2508 		/* create a clone with writeable headroom */
2509 		new_skb = skb_realloc_headroom(skb, sizeof(struct qeth_hdr_tso)
2510 					+ VLAN_HLEN);
2511 		if (!new_skb)
2512 			goto tx_drop;
2513 
2514 		if (ipv == 4) {
2515 			skb_pull(new_skb, ETH_HLEN);
2516 		}
2517 
2518 		if (ipv != 4 && skb_vlan_tag_present(new_skb)) {
2519 			skb_push(new_skb, VLAN_HLEN);
2520 			skb_copy_to_linear_data(new_skb, new_skb->data + 4, 4);
2521 			skb_copy_to_linear_data_offset(new_skb, 4,
2522 				new_skb->data + 8, 4);
2523 			skb_copy_to_linear_data_offset(new_skb, 8,
2524 				new_skb->data + 12, 4);
2525 			tag = (__be16 *)(new_skb->data + 12);
2526 			*tag = cpu_to_be16(ETH_P_8021Q);
2527 			*(tag + 1) = cpu_to_be16(skb_vlan_tag_get(new_skb));
2528 		}
2529 	}
2530 
2531 	netif_stop_queue(dev);
2532 
2533 	/* fix hardware limitation: as long as we do not have sbal
2534 	 * chaining we can not send long frag lists
2535 	 */
2536 	if ((card->info.type != QETH_CARD_TYPE_IQD) &&
2537 	    ((use_tso && !qeth_l3_get_elements_no_tso(card, new_skb, 1)) ||
2538 	     (!use_tso && !qeth_get_elements_no(card, new_skb, 0, 0)))) {
2539 		int lin_rc = skb_linearize(new_skb);
2540 
2541 		if (card->options.performance_stats) {
2542 			if (lin_rc)
2543 				card->perf_stats.tx_linfail++;
2544 			else
2545 				card->perf_stats.tx_lin++;
2546 		}
2547 		if (lin_rc)
2548 			goto tx_drop;
2549 	}
2550 	nr_frags = skb_shinfo(new_skb)->nr_frags;
2551 
2552 	if (use_tso) {
2553 		hdr = skb_push(new_skb, sizeof(struct qeth_hdr_tso));
2554 		memset(hdr, 0, sizeof(struct qeth_hdr_tso));
2555 		qeth_l3_fill_header(card, hdr, new_skb, ipv, cast_type,
2556 				    new_skb->len - sizeof(struct qeth_hdr_tso));
2557 		qeth_tso_fill_header(card, hdr, new_skb);
2558 		hdr_elements++;
2559 	} else {
2560 		if (data_offset < 0) {
2561 			hdr = skb_push(new_skb, sizeof(struct qeth_hdr));
2562 			qeth_l3_fill_header(card, hdr, new_skb, ipv, cast_type,
2563 					    new_skb->len -
2564 					    sizeof(struct qeth_hdr));
2565 		} else {
2566 			if (be16_to_cpu(new_skb->protocol) == ETH_P_AF_IUCV)
2567 				qeth_l3_fill_af_iucv_hdr(card, hdr, new_skb);
2568 			else {
2569 				qeth_l3_fill_header(card, hdr, new_skb, ipv,
2570 						    cast_type,
2571 						    new_skb->len - data_offset);
2572 			}
2573 		}
2574 
2575 		if (skb->ip_summed == CHECKSUM_PARTIAL)
2576 			qeth_l3_hdr_csum(card, hdr, new_skb);
2577 	}
2578 
2579 	elements = use_tso ?
2580 		   qeth_l3_get_elements_no_tso(card, new_skb, hdr_elements) :
2581 		   qeth_get_elements_no(card, new_skb, hdr_elements,
2582 					(data_offset > 0) ? data_offset : 0);
2583 	if (!elements) {
2584 		if (data_offset >= 0)
2585 			kmem_cache_free(qeth_core_header_cache, hdr);
2586 		goto tx_drop;
2587 	}
2588 	elements += hdr_elements;
2589 
2590 	if (card->info.type != QETH_CARD_TYPE_IQD) {
2591 		int len;
2592 		if (use_tso) {
2593 			hd_len = sizeof(struct qeth_hdr_tso) +
2594 				 ip_hdrlen(new_skb) + tcp_hdrlen(new_skb);
2595 			len = hd_len;
2596 		} else {
2597 			len = sizeof(struct qeth_hdr_layer3);
2598 		}
2599 
2600 		if (qeth_hdr_chk_and_bounce(new_skb, &hdr, len))
2601 			goto tx_drop;
2602 		rc = qeth_do_send_packet(card, queue, new_skb, hdr, hd_len,
2603 					 hd_len, elements);
2604 	} else
2605 		rc = qeth_do_send_packet_fast(queue, new_skb, hdr, data_offset,
2606 					      hd_len);
2607 
2608 	if (!rc) {
2609 		card->stats.tx_packets++;
2610 		card->stats.tx_bytes += tx_bytes;
2611 		if (new_skb != skb)
2612 			dev_kfree_skb_any(skb);
2613 		if (card->options.performance_stats) {
2614 			if (use_tso) {
2615 				card->perf_stats.large_send_bytes += tx_bytes;
2616 				card->perf_stats.large_send_cnt++;
2617 			}
2618 			if (nr_frags) {
2619 				card->perf_stats.sg_skbs_sent++;
2620 				/* nr_frags + skb->data */
2621 				card->perf_stats.sg_frags_sent += nr_frags + 1;
2622 			}
2623 		}
2624 		rc = NETDEV_TX_OK;
2625 	} else {
2626 		if (data_offset >= 0)
2627 			kmem_cache_free(qeth_core_header_cache, hdr);
2628 
2629 		if (rc == -EBUSY) {
2630 			if (new_skb != skb)
2631 				dev_kfree_skb_any(new_skb);
2632 			return NETDEV_TX_BUSY;
2633 		} else
2634 			goto tx_drop;
2635 	}
2636 
2637 	netif_wake_queue(dev);
2638 	if (card->options.performance_stats)
2639 		card->perf_stats.outbound_time += qeth_get_micros() -
2640 			card->perf_stats.outbound_start_time;
2641 	return rc;
2642 
2643 tx_drop:
2644 	card->stats.tx_dropped++;
2645 	card->stats.tx_errors++;
2646 	if ((new_skb != skb) && new_skb)
2647 		dev_kfree_skb_any(new_skb);
2648 	dev_kfree_skb_any(skb);
2649 	netif_wake_queue(dev);
2650 	return NETDEV_TX_OK;
2651 }
2652 
2653 static int __qeth_l3_open(struct net_device *dev)
2654 {
2655 	struct qeth_card *card = dev->ml_priv;
2656 	int rc = 0;
2657 
2658 	QETH_CARD_TEXT(card, 4, "qethopen");
2659 	if (card->state == CARD_STATE_UP)
2660 		return rc;
2661 	if (card->state != CARD_STATE_SOFTSETUP)
2662 		return -ENODEV;
2663 	card->data.state = CH_STATE_UP;
2664 	card->state = CARD_STATE_UP;
2665 	netif_start_queue(dev);
2666 
2667 	if (qdio_stop_irq(card->data.ccwdev, 0) >= 0) {
2668 		napi_enable(&card->napi);
2669 		napi_schedule(&card->napi);
2670 	} else
2671 		rc = -EIO;
2672 	return rc;
2673 }
2674 
2675 static int qeth_l3_open(struct net_device *dev)
2676 {
2677 	struct qeth_card *card = dev->ml_priv;
2678 
2679 	QETH_CARD_TEXT(card, 5, "qethope_");
2680 	if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
2681 		QETH_CARD_TEXT(card, 3, "openREC");
2682 		return -ERESTARTSYS;
2683 	}
2684 	return __qeth_l3_open(dev);
2685 }
2686 
2687 static int qeth_l3_stop(struct net_device *dev)
2688 {
2689 	struct qeth_card *card = dev->ml_priv;
2690 
2691 	QETH_CARD_TEXT(card, 4, "qethstop");
2692 	netif_tx_disable(dev);
2693 	if (card->state == CARD_STATE_UP) {
2694 		card->state = CARD_STATE_SOFTSETUP;
2695 		napi_disable(&card->napi);
2696 	}
2697 	return 0;
2698 }
2699 
2700 static const struct ethtool_ops qeth_l3_ethtool_ops = {
2701 	.get_link = ethtool_op_get_link,
2702 	.get_strings = qeth_core_get_strings,
2703 	.get_ethtool_stats = qeth_core_get_ethtool_stats,
2704 	.get_sset_count = qeth_core_get_sset_count,
2705 	.get_drvinfo = qeth_core_get_drvinfo,
2706 	.get_link_ksettings = qeth_core_ethtool_get_link_ksettings,
2707 };
2708 
2709 /*
2710  * we need NOARP for IPv4 but we want neighbor solicitation for IPv6. Setting
2711  * NOARP on the netdevice is no option because it also turns off neighbor
2712  * solicitation. For IPv4 we install a neighbor_setup function. We don't want
2713  * arp resolution but we want the hard header (packet socket will work
2714  * e.g. tcpdump)
2715  */
2716 static int qeth_l3_neigh_setup_noarp(struct neighbour *n)
2717 {
2718 	n->nud_state = NUD_NOARP;
2719 	memcpy(n->ha, "FAKELL", 6);
2720 	n->output = n->ops->connected_output;
2721 	return 0;
2722 }
2723 
2724 static int
2725 qeth_l3_neigh_setup(struct net_device *dev, struct neigh_parms *np)
2726 {
2727 	if (np->tbl->family == AF_INET)
2728 		np->neigh_setup = qeth_l3_neigh_setup_noarp;
2729 
2730 	return 0;
2731 }
2732 
2733 static const struct net_device_ops qeth_l3_netdev_ops = {
2734 	.ndo_open		= qeth_l3_open,
2735 	.ndo_stop		= qeth_l3_stop,
2736 	.ndo_get_stats		= qeth_get_stats,
2737 	.ndo_start_xmit		= qeth_l3_hard_start_xmit,
2738 	.ndo_validate_addr	= eth_validate_addr,
2739 	.ndo_set_rx_mode	= qeth_l3_set_rx_mode,
2740 	.ndo_do_ioctl		= qeth_do_ioctl,
2741 	.ndo_change_mtu		= qeth_change_mtu,
2742 	.ndo_fix_features	= qeth_fix_features,
2743 	.ndo_set_features	= qeth_set_features,
2744 	.ndo_vlan_rx_add_vid	= qeth_l3_vlan_rx_add_vid,
2745 	.ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
2746 	.ndo_tx_timeout		= qeth_tx_timeout,
2747 };
2748 
2749 static const struct net_device_ops qeth_l3_osa_netdev_ops = {
2750 	.ndo_open		= qeth_l3_open,
2751 	.ndo_stop		= qeth_l3_stop,
2752 	.ndo_get_stats		= qeth_get_stats,
2753 	.ndo_start_xmit		= qeth_l3_hard_start_xmit,
2754 	.ndo_features_check	= qeth_features_check,
2755 	.ndo_validate_addr	= eth_validate_addr,
2756 	.ndo_set_rx_mode	= qeth_l3_set_rx_mode,
2757 	.ndo_do_ioctl		= qeth_do_ioctl,
2758 	.ndo_change_mtu		= qeth_change_mtu,
2759 	.ndo_fix_features	= qeth_fix_features,
2760 	.ndo_set_features	= qeth_set_features,
2761 	.ndo_vlan_rx_add_vid	= qeth_l3_vlan_rx_add_vid,
2762 	.ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
2763 	.ndo_tx_timeout		= qeth_tx_timeout,
2764 	.ndo_neigh_setup	= qeth_l3_neigh_setup,
2765 };
2766 
2767 static int qeth_l3_setup_netdev(struct qeth_card *card)
2768 {
2769 	int rc;
2770 
2771 	if (card->info.type == QETH_CARD_TYPE_OSD ||
2772 	    card->info.type == QETH_CARD_TYPE_OSX) {
2773 		if ((card->info.link_type == QETH_LINK_TYPE_LANE_TR) ||
2774 		    (card->info.link_type == QETH_LINK_TYPE_HSTR)) {
2775 			pr_info("qeth_l3: ignoring TR device\n");
2776 			return -ENODEV;
2777 		} else {
2778 			card->dev = alloc_etherdev(0);
2779 			if (!card->dev)
2780 				return -ENODEV;
2781 			card->dev->netdev_ops = &qeth_l3_osa_netdev_ops;
2782 
2783 			/*IPv6 address autoconfiguration stuff*/
2784 			qeth_l3_get_unique_id(card);
2785 			if (!(card->info.unique_id & UNIQUE_ID_NOT_BY_CARD))
2786 				card->dev->dev_id = card->info.unique_id &
2787 							 0xffff;
2788 			if (!card->info.guestlan) {
2789 				card->dev->hw_features = NETIF_F_SG |
2790 					NETIF_F_RXCSUM | NETIF_F_IP_CSUM |
2791 					NETIF_F_TSO;
2792 				card->dev->vlan_features = NETIF_F_SG |
2793 					NETIF_F_RXCSUM | NETIF_F_IP_CSUM |
2794 					NETIF_F_TSO;
2795 				card->dev->features |= NETIF_F_SG;
2796 			}
2797 		}
2798 	} else if (card->info.type == QETH_CARD_TYPE_IQD) {
2799 		card->dev = alloc_netdev(0, "hsi%d", NET_NAME_UNKNOWN,
2800 					 ether_setup);
2801 		if (!card->dev)
2802 			return -ENODEV;
2803 		card->dev->flags |= IFF_NOARP;
2804 		card->dev->netdev_ops = &qeth_l3_netdev_ops;
2805 		rc = qeth_l3_iqd_read_initial_mac(card);
2806 		if (rc)
2807 			return rc;
2808 		if (card->options.hsuid[0])
2809 			memcpy(card->dev->perm_addr, card->options.hsuid, 9);
2810 	} else
2811 		return -ENODEV;
2812 
2813 	card->dev->ml_priv = card;
2814 	card->dev->watchdog_timeo = QETH_TX_TIMEOUT;
2815 	card->dev->mtu = card->info.initial_mtu;
2816 	card->dev->min_mtu = 64;
2817 	card->dev->max_mtu = ETH_MAX_MTU;
2818 	card->dev->ethtool_ops = &qeth_l3_ethtool_ops;
2819 	card->dev->features |=	NETIF_F_HW_VLAN_CTAG_TX |
2820 				NETIF_F_HW_VLAN_CTAG_RX |
2821 				NETIF_F_HW_VLAN_CTAG_FILTER;
2822 	netif_keep_dst(card->dev);
2823 	netif_set_gso_max_size(card->dev, (QETH_MAX_BUFFER_ELEMENTS(card) - 1) *
2824 					  PAGE_SIZE);
2825 
2826 	SET_NETDEV_DEV(card->dev, &card->gdev->dev);
2827 	netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
2828 	netif_carrier_off(card->dev);
2829 	return register_netdev(card->dev);
2830 }
2831 
2832 static const struct device_type qeth_l3_devtype = {
2833 	.name = "qeth_layer3",
2834 	.groups = qeth_l3_attr_groups,
2835 };
2836 
2837 static int qeth_l3_probe_device(struct ccwgroup_device *gdev)
2838 {
2839 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2840 	int rc;
2841 
2842 	if (gdev->dev.type == &qeth_generic_devtype) {
2843 		rc = qeth_l3_create_device_attributes(&gdev->dev);
2844 		if (rc)
2845 			return rc;
2846 	}
2847 	hash_init(card->ip_htable);
2848 	hash_init(card->ip_mc_htable);
2849 	card->options.layer2 = 0;
2850 	card->info.hwtrap = 0;
2851 	return 0;
2852 }
2853 
2854 static void qeth_l3_remove_device(struct ccwgroup_device *cgdev)
2855 {
2856 	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
2857 
2858 	if (cgdev->dev.type == &qeth_generic_devtype)
2859 		qeth_l3_remove_device_attributes(&cgdev->dev);
2860 
2861 	qeth_set_allowed_threads(card, 0, 1);
2862 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
2863 
2864 	if (cgdev->state == CCWGROUP_ONLINE)
2865 		qeth_l3_set_offline(cgdev);
2866 
2867 	if (card->dev) {
2868 		unregister_netdev(card->dev);
2869 		free_netdev(card->dev);
2870 		card->dev = NULL;
2871 	}
2872 
2873 	qeth_l3_clear_ip_htable(card, 0);
2874 	qeth_l3_clear_ipato_list(card);
2875 	return;
2876 }
2877 
2878 static int __qeth_l3_set_online(struct ccwgroup_device *gdev, int recovery_mode)
2879 {
2880 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2881 	int rc = 0;
2882 	enum qeth_card_states recover_flag;
2883 
2884 	mutex_lock(&card->discipline_mutex);
2885 	mutex_lock(&card->conf_mutex);
2886 	QETH_DBF_TEXT(SETUP, 2, "setonlin");
2887 	QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
2888 
2889 	recover_flag = card->state;
2890 	rc = qeth_core_hardsetup_card(card);
2891 	if (rc) {
2892 		QETH_DBF_TEXT_(SETUP, 2, "2err%04x", rc);
2893 		rc = -ENODEV;
2894 		goto out_remove;
2895 	}
2896 
2897 	if (!card->dev && qeth_l3_setup_netdev(card)) {
2898 		rc = -ENODEV;
2899 		goto out_remove;
2900 	}
2901 
2902 	if (qeth_is_diagass_supported(card, QETH_DIAGS_CMD_TRAP)) {
2903 		if (card->info.hwtrap &&
2904 		    qeth_hw_trap(card, QETH_DIAGS_TRAP_ARM))
2905 			card->info.hwtrap = 0;
2906 	} else
2907 		card->info.hwtrap = 0;
2908 
2909 	card->state = CARD_STATE_HARDSETUP;
2910 	memset(&card->rx, 0, sizeof(struct qeth_rx));
2911 	qeth_print_status_message(card);
2912 
2913 	/* softsetup */
2914 	QETH_DBF_TEXT(SETUP, 2, "softsetp");
2915 
2916 	rc = qeth_l3_setadapter_parms(card);
2917 	if (rc)
2918 		QETH_DBF_TEXT_(SETUP, 2, "2err%04x", rc);
2919 	if (!card->options.sniffer) {
2920 		rc = qeth_l3_start_ipassists(card);
2921 		if (rc) {
2922 			QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
2923 			goto out_remove;
2924 		}
2925 		rc = qeth_l3_setrouting_v4(card);
2926 		if (rc)
2927 			QETH_DBF_TEXT_(SETUP, 2, "4err%04x", rc);
2928 		rc = qeth_l3_setrouting_v6(card);
2929 		if (rc)
2930 			QETH_DBF_TEXT_(SETUP, 2, "5err%04x", rc);
2931 	}
2932 	netif_tx_disable(card->dev);
2933 
2934 	rc = qeth_init_qdio_queues(card);
2935 	if (rc) {
2936 		QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
2937 		rc = -ENODEV;
2938 		goto out_remove;
2939 	}
2940 	card->state = CARD_STATE_SOFTSETUP;
2941 
2942 	qeth_set_allowed_threads(card, 0xffffffff, 0);
2943 	qeth_l3_recover_ip(card);
2944 	if (card->lan_online)
2945 		netif_carrier_on(card->dev);
2946 	else
2947 		netif_carrier_off(card->dev);
2948 	if (recover_flag == CARD_STATE_RECOVER) {
2949 		rtnl_lock();
2950 		if (recovery_mode)
2951 			__qeth_l3_open(card->dev);
2952 		else
2953 			dev_open(card->dev);
2954 		qeth_l3_set_rx_mode(card->dev);
2955 		qeth_recover_features(card->dev);
2956 		rtnl_unlock();
2957 	}
2958 	qeth_trace_features(card);
2959 	/* let user_space know that device is online */
2960 	kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
2961 	mutex_unlock(&card->conf_mutex);
2962 	mutex_unlock(&card->discipline_mutex);
2963 	return 0;
2964 out_remove:
2965 	qeth_l3_stop_card(card, 0);
2966 	ccw_device_set_offline(CARD_DDEV(card));
2967 	ccw_device_set_offline(CARD_WDEV(card));
2968 	ccw_device_set_offline(CARD_RDEV(card));
2969 	qdio_free(CARD_DDEV(card));
2970 	if (recover_flag == CARD_STATE_RECOVER)
2971 		card->state = CARD_STATE_RECOVER;
2972 	else
2973 		card->state = CARD_STATE_DOWN;
2974 	mutex_unlock(&card->conf_mutex);
2975 	mutex_unlock(&card->discipline_mutex);
2976 	return rc;
2977 }
2978 
2979 static int qeth_l3_set_online(struct ccwgroup_device *gdev)
2980 {
2981 	return __qeth_l3_set_online(gdev, 0);
2982 }
2983 
2984 static int __qeth_l3_set_offline(struct ccwgroup_device *cgdev,
2985 			int recovery_mode)
2986 {
2987 	struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
2988 	int rc = 0, rc2 = 0, rc3 = 0;
2989 	enum qeth_card_states recover_flag;
2990 
2991 	mutex_lock(&card->discipline_mutex);
2992 	mutex_lock(&card->conf_mutex);
2993 	QETH_DBF_TEXT(SETUP, 3, "setoffl");
2994 	QETH_DBF_HEX(SETUP, 3, &card, sizeof(void *));
2995 
2996 	if (card->dev && netif_carrier_ok(card->dev))
2997 		netif_carrier_off(card->dev);
2998 	recover_flag = card->state;
2999 	if ((!recovery_mode && card->info.hwtrap) || card->info.hwtrap == 2) {
3000 		qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
3001 		card->info.hwtrap = 1;
3002 	}
3003 	qeth_l3_stop_card(card, recovery_mode);
3004 	if ((card->options.cq == QETH_CQ_ENABLED) && card->dev) {
3005 		rtnl_lock();
3006 		call_netdevice_notifiers(NETDEV_REBOOT, card->dev);
3007 		rtnl_unlock();
3008 	}
3009 	rc  = ccw_device_set_offline(CARD_DDEV(card));
3010 	rc2 = ccw_device_set_offline(CARD_WDEV(card));
3011 	rc3 = ccw_device_set_offline(CARD_RDEV(card));
3012 	if (!rc)
3013 		rc = (rc2) ? rc2 : rc3;
3014 	if (rc)
3015 		QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
3016 	qdio_free(CARD_DDEV(card));
3017 	if (recover_flag == CARD_STATE_UP)
3018 		card->state = CARD_STATE_RECOVER;
3019 	/* let user_space know that device is offline */
3020 	kobject_uevent(&cgdev->dev.kobj, KOBJ_CHANGE);
3021 	mutex_unlock(&card->conf_mutex);
3022 	mutex_unlock(&card->discipline_mutex);
3023 	return 0;
3024 }
3025 
3026 static int qeth_l3_set_offline(struct ccwgroup_device *cgdev)
3027 {
3028 	return __qeth_l3_set_offline(cgdev, 0);
3029 }
3030 
3031 static int qeth_l3_recover(void *ptr)
3032 {
3033 	struct qeth_card *card;
3034 	int rc = 0;
3035 
3036 	card = (struct qeth_card *) ptr;
3037 	QETH_CARD_TEXT(card, 2, "recover1");
3038 	QETH_CARD_HEX(card, 2, &card, sizeof(void *));
3039 	if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
3040 		return 0;
3041 	QETH_CARD_TEXT(card, 2, "recover2");
3042 	dev_warn(&card->gdev->dev,
3043 		"A recovery process has been started for the device\n");
3044 	qeth_set_recovery_task(card);
3045 	__qeth_l3_set_offline(card->gdev, 1);
3046 	rc = __qeth_l3_set_online(card->gdev, 1);
3047 	if (!rc)
3048 		dev_info(&card->gdev->dev,
3049 			"Device successfully recovered!\n");
3050 	else {
3051 		qeth_close_dev(card);
3052 		dev_warn(&card->gdev->dev, "The qeth device driver "
3053 				"failed to recover an error on the device\n");
3054 	}
3055 	qeth_clear_recovery_task(card);
3056 	qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
3057 	qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
3058 	return 0;
3059 }
3060 
3061 static int qeth_l3_pm_suspend(struct ccwgroup_device *gdev)
3062 {
3063 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
3064 
3065 	if (card->dev)
3066 		netif_device_detach(card->dev);
3067 	qeth_set_allowed_threads(card, 0, 1);
3068 	wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
3069 	if (gdev->state == CCWGROUP_OFFLINE)
3070 		return 0;
3071 	if (card->state == CARD_STATE_UP) {
3072 		if (card->info.hwtrap)
3073 			qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
3074 		__qeth_l3_set_offline(card->gdev, 1);
3075 	} else
3076 		__qeth_l3_set_offline(card->gdev, 0);
3077 	return 0;
3078 }
3079 
3080 static int qeth_l3_pm_resume(struct ccwgroup_device *gdev)
3081 {
3082 	struct qeth_card *card = dev_get_drvdata(&gdev->dev);
3083 	int rc = 0;
3084 
3085 	if (gdev->state == CCWGROUP_OFFLINE)
3086 		goto out;
3087 
3088 	if (card->state == CARD_STATE_RECOVER) {
3089 		rc = __qeth_l3_set_online(card->gdev, 1);
3090 		if (rc) {
3091 			rtnl_lock();
3092 			dev_close(card->dev);
3093 			rtnl_unlock();
3094 		}
3095 	} else
3096 		rc = __qeth_l3_set_online(card->gdev, 0);
3097 out:
3098 	qeth_set_allowed_threads(card, 0xffffffff, 0);
3099 	if (card->dev)
3100 		netif_device_attach(card->dev);
3101 	if (rc)
3102 		dev_warn(&card->gdev->dev, "The qeth device driver "
3103 			"failed to recover an error on the device\n");
3104 	return rc;
3105 }
3106 
3107 /* Returns zero if the command is successfully "consumed" */
3108 static int qeth_l3_control_event(struct qeth_card *card,
3109 					struct qeth_ipa_cmd *cmd)
3110 {
3111 	return 1;
3112 }
3113 
3114 struct qeth_discipline qeth_l3_discipline = {
3115 	.devtype = &qeth_l3_devtype,
3116 	.start_poll = qeth_qdio_start_poll,
3117 	.input_handler = (qdio_handler_t *) qeth_qdio_input_handler,
3118 	.output_handler = (qdio_handler_t *) qeth_qdio_output_handler,
3119 	.process_rx_buffer = qeth_l3_process_inbound_buffer,
3120 	.recover = qeth_l3_recover,
3121 	.setup = qeth_l3_probe_device,
3122 	.remove = qeth_l3_remove_device,
3123 	.set_online = qeth_l3_set_online,
3124 	.set_offline = qeth_l3_set_offline,
3125 	.freeze = qeth_l3_pm_suspend,
3126 	.thaw = qeth_l3_pm_resume,
3127 	.restore = qeth_l3_pm_resume,
3128 	.do_ioctl = qeth_l3_do_ioctl,
3129 	.control_event_handler = qeth_l3_control_event,
3130 };
3131 EXPORT_SYMBOL_GPL(qeth_l3_discipline);
3132 
3133 static int qeth_l3_ip_event(struct notifier_block *this,
3134 			    unsigned long event, void *ptr)
3135 {
3136 
3137 	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
3138 	struct net_device *dev = (struct net_device *)ifa->ifa_dev->dev;
3139 	struct qeth_ipaddr *addr;
3140 	struct qeth_card *card;
3141 
3142 	if (dev_net(dev) != &init_net)
3143 		return NOTIFY_DONE;
3144 
3145 	card = qeth_l3_get_card_from_dev(dev);
3146 	if (!card)
3147 		return NOTIFY_DONE;
3148 	QETH_CARD_TEXT(card, 3, "ipevent");
3149 
3150 	addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
3151 	if (addr) {
3152 		addr->u.a4.addr = be32_to_cpu(ifa->ifa_address);
3153 		addr->u.a4.mask = be32_to_cpu(ifa->ifa_mask);
3154 		addr->type = QETH_IP_TYPE_NORMAL;
3155 	} else
3156 		return NOTIFY_DONE;
3157 
3158 	switch (event) {
3159 	case NETDEV_UP:
3160 		spin_lock_bh(&card->ip_lock);
3161 		qeth_l3_add_ip(card, addr);
3162 		spin_unlock_bh(&card->ip_lock);
3163 		break;
3164 	case NETDEV_DOWN:
3165 		spin_lock_bh(&card->ip_lock);
3166 		qeth_l3_delete_ip(card, addr);
3167 		spin_unlock_bh(&card->ip_lock);
3168 		break;
3169 	}
3170 
3171 	kfree(addr);
3172 	return NOTIFY_DONE;
3173 }
3174 
3175 static struct notifier_block qeth_l3_ip_notifier = {
3176 	qeth_l3_ip_event,
3177 	NULL,
3178 };
3179 
3180 static int qeth_l3_ip6_event(struct notifier_block *this,
3181 			     unsigned long event, void *ptr)
3182 {
3183 	struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
3184 	struct net_device *dev = (struct net_device *)ifa->idev->dev;
3185 	struct qeth_ipaddr *addr;
3186 	struct qeth_card *card;
3187 
3188 	card = qeth_l3_get_card_from_dev(dev);
3189 	if (!card)
3190 		return NOTIFY_DONE;
3191 	QETH_CARD_TEXT(card, 3, "ip6event");
3192 	if (!qeth_is_supported(card, IPA_IPV6))
3193 		return NOTIFY_DONE;
3194 
3195 	addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
3196 	if (addr) {
3197 		memcpy(&addr->u.a6.addr, &ifa->addr, sizeof(struct in6_addr));
3198 		addr->u.a6.pfxlen = ifa->prefix_len;
3199 		addr->type = QETH_IP_TYPE_NORMAL;
3200 	} else
3201 		return NOTIFY_DONE;
3202 
3203 	switch (event) {
3204 	case NETDEV_UP:
3205 		spin_lock_bh(&card->ip_lock);
3206 		qeth_l3_add_ip(card, addr);
3207 		spin_unlock_bh(&card->ip_lock);
3208 		break;
3209 	case NETDEV_DOWN:
3210 		spin_lock_bh(&card->ip_lock);
3211 		qeth_l3_delete_ip(card, addr);
3212 		spin_unlock_bh(&card->ip_lock);
3213 		break;
3214 	}
3215 
3216 	kfree(addr);
3217 	return NOTIFY_DONE;
3218 }
3219 
3220 static struct notifier_block qeth_l3_ip6_notifier = {
3221 	qeth_l3_ip6_event,
3222 	NULL,
3223 };
3224 
3225 static int qeth_l3_register_notifiers(void)
3226 {
3227 	int rc;
3228 
3229 	QETH_DBF_TEXT(SETUP, 5, "regnotif");
3230 	rc = register_inetaddr_notifier(&qeth_l3_ip_notifier);
3231 	if (rc)
3232 		return rc;
3233 	rc = register_inet6addr_notifier(&qeth_l3_ip6_notifier);
3234 	if (rc) {
3235 		unregister_inetaddr_notifier(&qeth_l3_ip_notifier);
3236 		return rc;
3237 	}
3238 	return 0;
3239 }
3240 
3241 static void qeth_l3_unregister_notifiers(void)
3242 {
3243 	QETH_DBF_TEXT(SETUP, 5, "unregnot");
3244 	WARN_ON(unregister_inetaddr_notifier(&qeth_l3_ip_notifier));
3245 	WARN_ON(unregister_inet6addr_notifier(&qeth_l3_ip6_notifier));
3246 }
3247 
3248 static int __init qeth_l3_init(void)
3249 {
3250 	pr_info("register layer 3 discipline\n");
3251 	return qeth_l3_register_notifiers();
3252 }
3253 
3254 static void __exit qeth_l3_exit(void)
3255 {
3256 	qeth_l3_unregister_notifiers();
3257 	pr_info("unregister layer 3 discipline\n");
3258 }
3259 
3260 module_init(qeth_l3_init);
3261 module_exit(qeth_l3_exit);
3262 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
3263 MODULE_DESCRIPTION("qeth layer 3 discipline");
3264 MODULE_LICENSE("GPL");
3265