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