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