treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / s390 / net / qeth_l3_main.c
blob317d56647a4a3b8965b155be0fcfc83b0bdc157e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright IBM Corp. 2007, 2009
4 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
5 * Frank Pavlic <fpavlic@de.ibm.com>,
6 * Thomas Spatzier <tspat@de.ibm.com>,
7 * Frank Blaschka <frank.blaschka@de.ibm.com>
8 */
10 #define KMSG_COMPONENT "qeth"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/bitops.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/etherdevice.h>
20 #include <linux/ip.h>
21 #include <linux/in.h>
22 #include <linux/ipv6.h>
23 #include <linux/inetdevice.h>
24 #include <linux/igmp.h>
25 #include <linux/slab.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/skbuff.h>
30 #include <net/ip.h>
31 #include <net/arp.h>
32 #include <net/route.h>
33 #include <net/ipv6.h>
34 #include <net/ip6_route.h>
35 #include <net/iucv/af_iucv.h>
36 #include <linux/hashtable.h>
38 #include "qeth_l3.h"
40 static int qeth_l3_register_addr_entry(struct qeth_card *,
41 struct qeth_ipaddr *);
42 static int qeth_l3_deregister_addr_entry(struct qeth_card *,
43 struct qeth_ipaddr *);
45 int qeth_l3_ipaddr_to_string(enum qeth_prot_versions proto, const u8 *addr,
46 char *buf)
48 if (proto == QETH_PROT_IPV4)
49 return sprintf(buf, "%pI4", addr);
50 else
51 return sprintf(buf, "%pI6", addr);
54 static struct qeth_ipaddr *qeth_l3_find_addr_by_ip(struct qeth_card *card,
55 struct qeth_ipaddr *query)
57 u32 key = qeth_l3_ipaddr_hash(query);
58 struct qeth_ipaddr *addr;
60 if (query->is_multicast) {
61 hash_for_each_possible(card->ip_mc_htable, addr, hnode, key)
62 if (qeth_l3_addr_match_ip(addr, query))
63 return addr;
64 } else {
65 hash_for_each_possible(card->ip_htable, addr, hnode, key)
66 if (qeth_l3_addr_match_ip(addr, query))
67 return addr;
69 return NULL;
72 static void qeth_l3_convert_addr_to_bits(u8 *addr, u8 *bits, int len)
74 int i, j;
75 u8 octet;
77 for (i = 0; i < len; ++i) {
78 octet = addr[i];
79 for (j = 7; j >= 0; --j) {
80 bits[i*8 + j] = octet & 1;
81 octet >>= 1;
86 static bool qeth_l3_is_addr_covered_by_ipato(struct qeth_card *card,
87 struct qeth_ipaddr *addr)
89 struct qeth_ipato_entry *ipatoe;
90 u8 addr_bits[128] = {0, };
91 u8 ipatoe_bits[128] = {0, };
92 int rc = 0;
94 if (!card->ipato.enabled)
95 return false;
96 if (addr->type != QETH_IP_TYPE_NORMAL)
97 return false;
99 qeth_l3_convert_addr_to_bits((u8 *) &addr->u, addr_bits,
100 (addr->proto == QETH_PROT_IPV4)? 4:16);
101 list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
102 if (addr->proto != ipatoe->proto)
103 continue;
104 qeth_l3_convert_addr_to_bits(ipatoe->addr, ipatoe_bits,
105 (ipatoe->proto == QETH_PROT_IPV4) ?
106 4 : 16);
107 if (addr->proto == QETH_PROT_IPV4)
108 rc = !memcmp(addr_bits, ipatoe_bits,
109 min(32, ipatoe->mask_bits));
110 else
111 rc = !memcmp(addr_bits, ipatoe_bits,
112 min(128, ipatoe->mask_bits));
113 if (rc)
114 break;
116 /* invert? */
117 if ((addr->proto == QETH_PROT_IPV4) && card->ipato.invert4)
118 rc = !rc;
119 else if ((addr->proto == QETH_PROT_IPV6) && card->ipato.invert6)
120 rc = !rc;
122 return rc;
125 static int qeth_l3_delete_ip(struct qeth_card *card,
126 struct qeth_ipaddr *tmp_addr)
128 int rc = 0;
129 struct qeth_ipaddr *addr;
131 if (tmp_addr->type == QETH_IP_TYPE_RXIP)
132 QETH_CARD_TEXT(card, 2, "delrxip");
133 else if (tmp_addr->type == QETH_IP_TYPE_VIPA)
134 QETH_CARD_TEXT(card, 2, "delvipa");
135 else
136 QETH_CARD_TEXT(card, 2, "delip");
138 if (tmp_addr->proto == QETH_PROT_IPV4)
139 QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
140 else {
141 QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
142 QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
145 addr = qeth_l3_find_addr_by_ip(card, tmp_addr);
146 if (!addr || !qeth_l3_addr_match_all(addr, tmp_addr))
147 return -ENOENT;
149 addr->ref_counter--;
150 if (addr->type == QETH_IP_TYPE_NORMAL && addr->ref_counter > 0)
151 return rc;
153 if (qeth_card_hw_is_reachable(card))
154 rc = qeth_l3_deregister_addr_entry(card, addr);
156 hash_del(&addr->hnode);
157 kfree(addr);
159 return rc;
162 static int qeth_l3_add_ip(struct qeth_card *card, struct qeth_ipaddr *tmp_addr)
164 int rc = 0;
165 struct qeth_ipaddr *addr;
166 char buf[40];
168 if (tmp_addr->type == QETH_IP_TYPE_RXIP)
169 QETH_CARD_TEXT(card, 2, "addrxip");
170 else if (tmp_addr->type == QETH_IP_TYPE_VIPA)
171 QETH_CARD_TEXT(card, 2, "addvipa");
172 else
173 QETH_CARD_TEXT(card, 2, "addip");
175 if (tmp_addr->proto == QETH_PROT_IPV4)
176 QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
177 else {
178 QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
179 QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
182 addr = qeth_l3_find_addr_by_ip(card, tmp_addr);
183 if (addr) {
184 if (tmp_addr->type != QETH_IP_TYPE_NORMAL)
185 return -EADDRINUSE;
186 if (qeth_l3_addr_match_all(addr, tmp_addr)) {
187 addr->ref_counter++;
188 return 0;
190 qeth_l3_ipaddr_to_string(tmp_addr->proto, (u8 *)&tmp_addr->u,
191 buf);
192 dev_warn(&card->gdev->dev,
193 "Registering IP address %s failed\n", buf);
194 return -EADDRINUSE;
195 } else {
196 addr = kmemdup(tmp_addr, sizeof(*tmp_addr), GFP_KERNEL);
197 if (!addr)
198 return -ENOMEM;
200 if (qeth_l3_is_addr_covered_by_ipato(card, addr)) {
201 QETH_CARD_TEXT(card, 2, "tkovaddr");
202 addr->ipato = 1;
204 hash_add(card->ip_htable, &addr->hnode,
205 qeth_l3_ipaddr_hash(addr));
207 if (!qeth_card_hw_is_reachable(card)) {
208 addr->disp_flag = QETH_DISP_ADDR_ADD;
209 return 0;
212 rc = qeth_l3_register_addr_entry(card, addr);
214 if (!rc || rc == -EADDRINUSE || rc == -ENETDOWN) {
215 addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
216 } else {
217 hash_del(&addr->hnode);
218 kfree(addr);
221 return rc;
224 static int qeth_l3_modify_ip(struct qeth_card *card, struct qeth_ipaddr *addr,
225 bool add)
227 int rc;
229 mutex_lock(&card->ip_lock);
230 rc = add ? qeth_l3_add_ip(card, addr) : qeth_l3_delete_ip(card, addr);
231 mutex_unlock(&card->ip_lock);
233 return rc;
236 static void qeth_l3_drain_rx_mode_cache(struct qeth_card *card)
238 struct qeth_ipaddr *addr;
239 struct hlist_node *tmp;
240 int i;
242 hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
243 hash_del(&addr->hnode);
244 kfree(addr);
248 static void qeth_l3_clear_ip_htable(struct qeth_card *card, int recover)
250 struct qeth_ipaddr *addr;
251 struct hlist_node *tmp;
252 int i;
254 QETH_CARD_TEXT(card, 4, "clearip");
256 mutex_lock(&card->ip_lock);
258 hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
259 if (!recover) {
260 hash_del(&addr->hnode);
261 kfree(addr);
262 continue;
264 addr->disp_flag = QETH_DISP_ADDR_ADD;
267 mutex_unlock(&card->ip_lock);
270 static void qeth_l3_recover_ip(struct qeth_card *card)
272 struct qeth_ipaddr *addr;
273 struct hlist_node *tmp;
274 int i;
275 int rc;
277 QETH_CARD_TEXT(card, 4, "recovrip");
279 mutex_lock(&card->ip_lock);
281 hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
282 if (addr->disp_flag == QETH_DISP_ADDR_ADD) {
283 rc = qeth_l3_register_addr_entry(card, addr);
285 if (!rc) {
286 addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
287 } else {
288 hash_del(&addr->hnode);
289 kfree(addr);
294 mutex_unlock(&card->ip_lock);
297 static int qeth_l3_setdelip_cb(struct qeth_card *card, struct qeth_reply *reply,
298 unsigned long data)
300 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
302 switch (cmd->hdr.return_code) {
303 case IPA_RC_SUCCESS:
304 return 0;
305 case IPA_RC_DUPLICATE_IP_ADDRESS:
306 return -EADDRINUSE;
307 case IPA_RC_MC_ADDR_NOT_FOUND:
308 return -ENOENT;
309 case IPA_RC_LAN_OFFLINE:
310 return -ENETDOWN;
311 default:
312 return -EIO;
316 static int qeth_l3_send_setdelmc(struct qeth_card *card,
317 struct qeth_ipaddr *addr, int ipacmd)
319 struct qeth_cmd_buffer *iob;
320 struct qeth_ipa_cmd *cmd;
322 QETH_CARD_TEXT(card, 4, "setdelmc");
324 iob = qeth_ipa_alloc_cmd(card, ipacmd, addr->proto,
325 IPA_DATA_SIZEOF(setdelipm));
326 if (!iob)
327 return -ENOMEM;
328 cmd = __ipa_cmd(iob);
329 if (addr->proto == QETH_PROT_IPV6) {
330 cmd->data.setdelipm.ip = addr->u.a6.addr;
331 ipv6_eth_mc_map(&addr->u.a6.addr, cmd->data.setdelipm.mac);
332 } else {
333 cmd->data.setdelipm.ip.s6_addr32[3] = addr->u.a4.addr;
334 ip_eth_mc_map(addr->u.a4.addr, cmd->data.setdelipm.mac);
337 return qeth_send_ipa_cmd(card, iob, qeth_l3_setdelip_cb, NULL);
340 static void qeth_l3_set_ipv6_prefix(struct in6_addr *prefix, unsigned int len)
342 unsigned int i = 0;
344 while (len && i < 4) {
345 int mask_len = min_t(int, len, 32);
347 prefix->s6_addr32[i] = inet_make_mask(mask_len);
348 len -= mask_len;
349 i++;
353 static u32 qeth_l3_get_setdelip_flags(struct qeth_ipaddr *addr, bool set)
355 switch (addr->type) {
356 case QETH_IP_TYPE_RXIP:
357 return (set) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0;
358 case QETH_IP_TYPE_VIPA:
359 return (set) ? QETH_IPA_SETIP_VIPA_FLAG :
360 QETH_IPA_DELIP_VIPA_FLAG;
361 default:
362 return (set && addr->ipato) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0;
366 static int qeth_l3_send_setdelip(struct qeth_card *card,
367 struct qeth_ipaddr *addr,
368 enum qeth_ipa_cmds ipacmd)
370 struct qeth_cmd_buffer *iob;
371 struct qeth_ipa_cmd *cmd;
372 u32 flags;
374 QETH_CARD_TEXT(card, 4, "setdelip");
376 iob = qeth_ipa_alloc_cmd(card, ipacmd, addr->proto,
377 IPA_DATA_SIZEOF(setdelip6));
378 if (!iob)
379 return -ENOMEM;
380 cmd = __ipa_cmd(iob);
382 flags = qeth_l3_get_setdelip_flags(addr, ipacmd == IPA_CMD_SETIP);
383 QETH_CARD_TEXT_(card, 4, "flags%02X", flags);
385 if (addr->proto == QETH_PROT_IPV6) {
386 cmd->data.setdelip6.addr = addr->u.a6.addr;
387 qeth_l3_set_ipv6_prefix(&cmd->data.setdelip6.prefix,
388 addr->u.a6.pfxlen);
389 cmd->data.setdelip6.flags = flags;
390 } else {
391 cmd->data.setdelip4.addr = addr->u.a4.addr;
392 cmd->data.setdelip4.mask = addr->u.a4.mask;
393 cmd->data.setdelip4.flags = flags;
396 return qeth_send_ipa_cmd(card, iob, qeth_l3_setdelip_cb, NULL);
399 static int qeth_l3_send_setrouting(struct qeth_card *card,
400 enum qeth_routing_types type, enum qeth_prot_versions prot)
402 int rc;
403 struct qeth_ipa_cmd *cmd;
404 struct qeth_cmd_buffer *iob;
406 QETH_CARD_TEXT(card, 4, "setroutg");
407 iob = qeth_ipa_alloc_cmd(card, IPA_CMD_SETRTG, prot,
408 IPA_DATA_SIZEOF(setrtg));
409 if (!iob)
410 return -ENOMEM;
411 cmd = __ipa_cmd(iob);
412 cmd->data.setrtg.type = (type);
413 rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
415 return rc;
418 static int qeth_l3_correct_routing_type(struct qeth_card *card,
419 enum qeth_routing_types *type, enum qeth_prot_versions prot)
421 if (IS_IQD(card)) {
422 switch (*type) {
423 case NO_ROUTER:
424 case PRIMARY_CONNECTOR:
425 case SECONDARY_CONNECTOR:
426 case MULTICAST_ROUTER:
427 return 0;
428 default:
429 goto out_inval;
431 } else {
432 switch (*type) {
433 case NO_ROUTER:
434 case PRIMARY_ROUTER:
435 case SECONDARY_ROUTER:
436 return 0;
437 case MULTICAST_ROUTER:
438 if (qeth_is_ipafunc_supported(card, prot,
439 IPA_OSA_MC_ROUTER))
440 return 0;
441 default:
442 goto out_inval;
445 out_inval:
446 *type = NO_ROUTER;
447 return -EINVAL;
450 int qeth_l3_setrouting_v4(struct qeth_card *card)
452 int rc;
454 QETH_CARD_TEXT(card, 3, "setrtg4");
456 rc = qeth_l3_correct_routing_type(card, &card->options.route4.type,
457 QETH_PROT_IPV4);
458 if (rc)
459 return rc;
461 rc = qeth_l3_send_setrouting(card, card->options.route4.type,
462 QETH_PROT_IPV4);
463 if (rc) {
464 card->options.route4.type = NO_ROUTER;
465 QETH_DBF_MESSAGE(2, "Error (%#06x) while setting routing type on device %x. Type set to 'no router'.\n",
466 rc, CARD_DEVID(card));
468 return rc;
471 int qeth_l3_setrouting_v6(struct qeth_card *card)
473 int rc = 0;
475 QETH_CARD_TEXT(card, 3, "setrtg6");
477 if (!qeth_is_supported(card, IPA_IPV6))
478 return 0;
479 rc = qeth_l3_correct_routing_type(card, &card->options.route6.type,
480 QETH_PROT_IPV6);
481 if (rc)
482 return rc;
484 rc = qeth_l3_send_setrouting(card, card->options.route6.type,
485 QETH_PROT_IPV6);
486 if (rc) {
487 card->options.route6.type = NO_ROUTER;
488 QETH_DBF_MESSAGE(2, "Error (%#06x) while setting routing type on device %x. Type set to 'no router'.\n",
489 rc, CARD_DEVID(card));
491 return rc;
495 * IP address takeover related functions
499 * qeth_l3_update_ipato() - Update 'takeover' property, for all NORMAL IPs.
501 * Caller must hold ip_lock.
503 void qeth_l3_update_ipato(struct qeth_card *card)
505 struct qeth_ipaddr *addr;
506 unsigned int i;
508 hash_for_each(card->ip_htable, i, addr, hnode) {
509 if (addr->type != QETH_IP_TYPE_NORMAL)
510 continue;
511 addr->ipato = qeth_l3_is_addr_covered_by_ipato(card, addr);
515 static void qeth_l3_clear_ipato_list(struct qeth_card *card)
517 struct qeth_ipato_entry *ipatoe, *tmp;
519 mutex_lock(&card->ip_lock);
521 list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
522 list_del(&ipatoe->entry);
523 kfree(ipatoe);
526 qeth_l3_update_ipato(card);
527 mutex_unlock(&card->ip_lock);
530 int qeth_l3_add_ipato_entry(struct qeth_card *card,
531 struct qeth_ipato_entry *new)
533 struct qeth_ipato_entry *ipatoe;
534 int rc = 0;
536 QETH_CARD_TEXT(card, 2, "addipato");
538 mutex_lock(&card->conf_mutex);
539 mutex_lock(&card->ip_lock);
541 list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
542 if (ipatoe->proto != new->proto)
543 continue;
544 if (!memcmp(ipatoe->addr, new->addr,
545 (ipatoe->proto == QETH_PROT_IPV4)? 4:16) &&
546 (ipatoe->mask_bits == new->mask_bits)) {
547 rc = -EEXIST;
548 break;
552 if (!rc) {
553 list_add_tail(&new->entry, &card->ipato.entries);
554 qeth_l3_update_ipato(card);
557 mutex_unlock(&card->ip_lock);
558 mutex_unlock(&card->conf_mutex);
560 return rc;
563 int qeth_l3_del_ipato_entry(struct qeth_card *card,
564 enum qeth_prot_versions proto, u8 *addr,
565 int mask_bits)
567 struct qeth_ipato_entry *ipatoe, *tmp;
568 int rc = -ENOENT;
570 QETH_CARD_TEXT(card, 2, "delipato");
572 mutex_lock(&card->conf_mutex);
573 mutex_lock(&card->ip_lock);
575 list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
576 if (ipatoe->proto != proto)
577 continue;
578 if (!memcmp(ipatoe->addr, addr,
579 (proto == QETH_PROT_IPV4)? 4:16) &&
580 (ipatoe->mask_bits == mask_bits)) {
581 list_del(&ipatoe->entry);
582 qeth_l3_update_ipato(card);
583 kfree(ipatoe);
584 rc = 0;
588 mutex_unlock(&card->ip_lock);
589 mutex_unlock(&card->conf_mutex);
591 return rc;
594 int qeth_l3_modify_rxip_vipa(struct qeth_card *card, bool add, const u8 *ip,
595 enum qeth_ip_types type,
596 enum qeth_prot_versions proto)
598 struct qeth_ipaddr addr;
599 int rc;
601 qeth_l3_init_ipaddr(&addr, type, proto);
602 if (proto == QETH_PROT_IPV4)
603 memcpy(&addr.u.a4.addr, ip, 4);
604 else
605 memcpy(&addr.u.a6.addr, ip, 16);
607 mutex_lock(&card->conf_mutex);
608 rc = qeth_l3_modify_ip(card, &addr, add);
609 mutex_unlock(&card->conf_mutex);
611 return rc;
614 int qeth_l3_modify_hsuid(struct qeth_card *card, bool add)
616 struct qeth_ipaddr addr;
617 unsigned int i;
619 qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6);
620 addr.u.a6.addr.s6_addr[0] = 0xfe;
621 addr.u.a6.addr.s6_addr[1] = 0x80;
622 for (i = 0; i < 8; i++)
623 addr.u.a6.addr.s6_addr[8+i] = card->options.hsuid[i];
625 return qeth_l3_modify_ip(card, &addr, add);
628 static int qeth_l3_register_addr_entry(struct qeth_card *card,
629 struct qeth_ipaddr *addr)
631 char buf[50];
632 int rc = 0;
633 int cnt = 3;
635 if (card->options.sniffer)
636 return 0;
638 if (addr->proto == QETH_PROT_IPV4) {
639 QETH_CARD_TEXT(card, 2, "setaddr4");
640 QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
641 } else if (addr->proto == QETH_PROT_IPV6) {
642 QETH_CARD_TEXT(card, 2, "setaddr6");
643 QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
644 QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
645 } else {
646 QETH_CARD_TEXT(card, 2, "setaddr?");
647 QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
649 do {
650 if (addr->is_multicast)
651 rc = qeth_l3_send_setdelmc(card, addr, IPA_CMD_SETIPM);
652 else
653 rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_SETIP);
654 if (rc)
655 QETH_CARD_TEXT(card, 2, "failed");
656 } while ((--cnt > 0) && rc);
657 if (rc) {
658 QETH_CARD_TEXT(card, 2, "FAILED");
659 qeth_l3_ipaddr_to_string(addr->proto, (u8 *)&addr->u, buf);
660 dev_warn(&card->gdev->dev,
661 "Registering IP address %s failed\n", buf);
663 return rc;
666 static int qeth_l3_deregister_addr_entry(struct qeth_card *card,
667 struct qeth_ipaddr *addr)
669 int rc = 0;
671 if (card->options.sniffer)
672 return 0;
674 if (addr->proto == QETH_PROT_IPV4) {
675 QETH_CARD_TEXT(card, 2, "deladdr4");
676 QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
677 } else if (addr->proto == QETH_PROT_IPV6) {
678 QETH_CARD_TEXT(card, 2, "deladdr6");
679 QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
680 QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
681 } else {
682 QETH_CARD_TEXT(card, 2, "deladdr?");
683 QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
685 if (addr->is_multicast)
686 rc = qeth_l3_send_setdelmc(card, addr, IPA_CMD_DELIPM);
687 else
688 rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_DELIP);
689 if (rc)
690 QETH_CARD_TEXT(card, 2, "failed");
692 return rc;
695 static int qeth_l3_setadapter_parms(struct qeth_card *card)
697 int rc = 0;
699 QETH_CARD_TEXT(card, 2, "setadprm");
701 if (qeth_adp_supported(card, IPA_SETADP_ALTER_MAC_ADDRESS)) {
702 rc = qeth_setadpparms_change_macaddr(card);
703 if (rc)
704 dev_warn(&card->gdev->dev, "Reading the adapter MAC"
705 " address failed\n");
708 return rc;
711 static int qeth_l3_start_ipa_arp_processing(struct qeth_card *card)
713 int rc;
715 QETH_CARD_TEXT(card, 3, "ipaarp");
717 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
718 dev_info(&card->gdev->dev,
719 "ARP processing not supported on %s!\n",
720 QETH_CARD_IFNAME(card));
721 return 0;
723 rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
724 IPA_CMD_ASS_START, NULL);
725 if (rc) {
726 dev_warn(&card->gdev->dev,
727 "Starting ARP processing support for %s failed\n",
728 QETH_CARD_IFNAME(card));
730 return rc;
733 static int qeth_l3_start_ipa_source_mac(struct qeth_card *card)
735 int rc;
737 QETH_CARD_TEXT(card, 3, "stsrcmac");
739 if (!qeth_is_supported(card, IPA_SOURCE_MAC)) {
740 dev_info(&card->gdev->dev,
741 "Inbound source MAC-address not supported on %s\n",
742 QETH_CARD_IFNAME(card));
743 return -EOPNOTSUPP;
746 rc = qeth_send_simple_setassparms(card, IPA_SOURCE_MAC,
747 IPA_CMD_ASS_START, NULL);
748 if (rc)
749 dev_warn(&card->gdev->dev,
750 "Starting source MAC-address support for %s failed\n",
751 QETH_CARD_IFNAME(card));
752 return rc;
755 static int qeth_l3_start_ipa_vlan(struct qeth_card *card)
757 int rc = 0;
759 QETH_CARD_TEXT(card, 3, "strtvlan");
761 if (!qeth_is_supported(card, IPA_FULL_VLAN)) {
762 dev_info(&card->gdev->dev,
763 "VLAN not supported on %s\n", QETH_CARD_IFNAME(card));
764 return -EOPNOTSUPP;
767 rc = qeth_send_simple_setassparms(card, IPA_VLAN_PRIO,
768 IPA_CMD_ASS_START, NULL);
769 if (rc) {
770 dev_warn(&card->gdev->dev,
771 "Starting VLAN support for %s failed\n",
772 QETH_CARD_IFNAME(card));
773 } else {
774 dev_info(&card->gdev->dev, "VLAN enabled\n");
776 return rc;
779 static int qeth_l3_start_ipa_multicast(struct qeth_card *card)
781 int rc;
783 QETH_CARD_TEXT(card, 3, "stmcast");
785 if (!qeth_is_supported(card, IPA_MULTICASTING)) {
786 dev_info(&card->gdev->dev,
787 "Multicast not supported on %s\n",
788 QETH_CARD_IFNAME(card));
789 return -EOPNOTSUPP;
792 rc = qeth_send_simple_setassparms(card, IPA_MULTICASTING,
793 IPA_CMD_ASS_START, NULL);
794 if (rc) {
795 dev_warn(&card->gdev->dev,
796 "Starting multicast support for %s failed\n",
797 QETH_CARD_IFNAME(card));
798 } else {
799 dev_info(&card->gdev->dev, "Multicast enabled\n");
800 card->dev->flags |= IFF_MULTICAST;
802 return rc;
805 static int qeth_l3_softsetup_ipv6(struct qeth_card *card)
807 u32 ipv6_data = 3;
808 int rc;
810 QETH_CARD_TEXT(card, 3, "softipv6");
812 if (IS_IQD(card))
813 goto out;
815 rc = qeth_send_simple_setassparms(card, IPA_IPV6, IPA_CMD_ASS_START,
816 &ipv6_data);
817 if (rc) {
818 dev_err(&card->gdev->dev,
819 "Activating IPv6 support for %s failed\n",
820 QETH_CARD_IFNAME(card));
821 return rc;
823 rc = qeth_send_simple_setassparms_v6(card, IPA_IPV6, IPA_CMD_ASS_START,
824 NULL);
825 if (rc) {
826 dev_err(&card->gdev->dev,
827 "Activating IPv6 support for %s failed\n",
828 QETH_CARD_IFNAME(card));
829 return rc;
831 rc = qeth_send_simple_setassparms_v6(card, IPA_PASSTHRU,
832 IPA_CMD_ASS_START, NULL);
833 if (rc) {
834 dev_warn(&card->gdev->dev,
835 "Enabling the passthrough mode for %s failed\n",
836 QETH_CARD_IFNAME(card));
837 return rc;
839 out:
840 dev_info(&card->gdev->dev, "IPV6 enabled\n");
841 return 0;
844 static int qeth_l3_start_ipa_ipv6(struct qeth_card *card)
846 QETH_CARD_TEXT(card, 3, "strtipv6");
848 if (!qeth_is_supported(card, IPA_IPV6)) {
849 dev_info(&card->gdev->dev,
850 "IPv6 not supported on %s\n", QETH_CARD_IFNAME(card));
851 return 0;
853 return qeth_l3_softsetup_ipv6(card);
856 static int qeth_l3_start_ipa_broadcast(struct qeth_card *card)
858 u32 filter_data = 1;
859 int rc;
861 QETH_CARD_TEXT(card, 3, "stbrdcst");
862 card->info.broadcast_capable = 0;
863 if (!qeth_is_supported(card, IPA_FILTERING)) {
864 dev_info(&card->gdev->dev,
865 "Broadcast not supported on %s\n",
866 QETH_CARD_IFNAME(card));
867 rc = -EOPNOTSUPP;
868 goto out;
870 rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
871 IPA_CMD_ASS_START, NULL);
872 if (rc) {
873 dev_warn(&card->gdev->dev, "Enabling broadcast filtering for "
874 "%s failed\n", QETH_CARD_IFNAME(card));
875 goto out;
878 rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
879 IPA_CMD_ASS_CONFIGURE, &filter_data);
880 if (rc) {
881 dev_warn(&card->gdev->dev,
882 "Setting up broadcast filtering for %s failed\n",
883 QETH_CARD_IFNAME(card));
884 goto out;
886 card->info.broadcast_capable = QETH_BROADCAST_WITH_ECHO;
887 dev_info(&card->gdev->dev, "Broadcast enabled\n");
888 rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
889 IPA_CMD_ASS_ENABLE, &filter_data);
890 if (rc) {
891 dev_warn(&card->gdev->dev, "Setting up broadcast echo "
892 "filtering for %s failed\n", QETH_CARD_IFNAME(card));
893 goto out;
895 card->info.broadcast_capable = QETH_BROADCAST_WITHOUT_ECHO;
896 out:
897 if (card->info.broadcast_capable)
898 card->dev->flags |= IFF_BROADCAST;
899 else
900 card->dev->flags &= ~IFF_BROADCAST;
901 return rc;
904 static void qeth_l3_start_ipassists(struct qeth_card *card)
906 QETH_CARD_TEXT(card, 3, "strtipas");
908 qeth_l3_start_ipa_arp_processing(card); /* go on*/
909 qeth_l3_start_ipa_source_mac(card); /* go on*/
910 qeth_l3_start_ipa_vlan(card); /* go on*/
911 qeth_l3_start_ipa_multicast(card); /* go on*/
912 qeth_l3_start_ipa_ipv6(card); /* go on*/
913 qeth_l3_start_ipa_broadcast(card); /* go on*/
916 static int qeth_l3_iqd_read_initial_mac_cb(struct qeth_card *card,
917 struct qeth_reply *reply, unsigned long data)
919 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
921 if (cmd->hdr.return_code)
922 return -EIO;
924 ether_addr_copy(card->dev->dev_addr,
925 cmd->data.create_destroy_addr.unique_id);
926 return 0;
929 static int qeth_l3_iqd_read_initial_mac(struct qeth_card *card)
931 int rc = 0;
932 struct qeth_cmd_buffer *iob;
933 struct qeth_ipa_cmd *cmd;
935 QETH_CARD_TEXT(card, 2, "hsrmac");
937 iob = qeth_ipa_alloc_cmd(card, IPA_CMD_CREATE_ADDR, QETH_PROT_IPV6,
938 IPA_DATA_SIZEOF(create_destroy_addr));
939 if (!iob)
940 return -ENOMEM;
941 cmd = __ipa_cmd(iob);
942 *((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
943 card->info.unique_id;
945 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_iqd_read_initial_mac_cb,
946 NULL);
947 return rc;
950 static int qeth_l3_get_unique_id_cb(struct qeth_card *card,
951 struct qeth_reply *reply, unsigned long data)
953 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
955 if (cmd->hdr.return_code == 0) {
956 card->info.unique_id = *((__u16 *)
957 &cmd->data.create_destroy_addr.unique_id[6]);
958 return 0;
961 card->info.unique_id = UNIQUE_ID_IF_CREATE_ADDR_FAILED |
962 UNIQUE_ID_NOT_BY_CARD;
963 dev_warn(&card->gdev->dev, "The network adapter failed to generate a unique ID\n");
964 return -EIO;
967 static int qeth_l3_get_unique_id(struct qeth_card *card)
969 int rc = 0;
970 struct qeth_cmd_buffer *iob;
971 struct qeth_ipa_cmd *cmd;
973 QETH_CARD_TEXT(card, 2, "guniqeid");
975 if (!qeth_is_supported(card, IPA_IPV6)) {
976 card->info.unique_id = UNIQUE_ID_IF_CREATE_ADDR_FAILED |
977 UNIQUE_ID_NOT_BY_CARD;
978 return 0;
981 iob = qeth_ipa_alloc_cmd(card, IPA_CMD_CREATE_ADDR, QETH_PROT_IPV6,
982 IPA_DATA_SIZEOF(create_destroy_addr));
983 if (!iob)
984 return -ENOMEM;
985 cmd = __ipa_cmd(iob);
986 *((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
987 card->info.unique_id;
989 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_get_unique_id_cb, NULL);
990 return rc;
993 static int
994 qeth_diags_trace_cb(struct qeth_card *card, struct qeth_reply *reply,
995 unsigned long data)
997 struct qeth_ipa_cmd *cmd;
998 __u16 rc;
1000 QETH_CARD_TEXT(card, 2, "diastrcb");
1002 cmd = (struct qeth_ipa_cmd *)data;
1003 rc = cmd->hdr.return_code;
1004 if (rc)
1005 QETH_CARD_TEXT_(card, 2, "dxter%x", rc);
1006 switch (cmd->data.diagass.action) {
1007 case QETH_DIAGS_CMD_TRACE_QUERY:
1008 break;
1009 case QETH_DIAGS_CMD_TRACE_DISABLE:
1010 switch (rc) {
1011 case 0:
1012 case IPA_RC_INVALID_SUBCMD:
1013 card->info.promisc_mode = SET_PROMISC_MODE_OFF;
1014 dev_info(&card->gdev->dev, "The HiperSockets network "
1015 "traffic analyzer is deactivated\n");
1016 break;
1017 default:
1018 break;
1020 break;
1021 case QETH_DIAGS_CMD_TRACE_ENABLE:
1022 switch (rc) {
1023 case 0:
1024 card->info.promisc_mode = SET_PROMISC_MODE_ON;
1025 dev_info(&card->gdev->dev, "The HiperSockets network "
1026 "traffic analyzer is activated\n");
1027 break;
1028 case IPA_RC_HARDWARE_AUTH_ERROR:
1029 dev_warn(&card->gdev->dev, "The device is not "
1030 "authorized to run as a HiperSockets network "
1031 "traffic analyzer\n");
1032 break;
1033 case IPA_RC_TRACE_ALREADY_ACTIVE:
1034 dev_warn(&card->gdev->dev, "A HiperSockets "
1035 "network traffic analyzer is already "
1036 "active in the HiperSockets LAN\n");
1037 break;
1038 default:
1039 break;
1041 break;
1042 default:
1043 QETH_DBF_MESSAGE(2, "Unknown sniffer action (%#06x) on device %x\n",
1044 cmd->data.diagass.action, CARD_DEVID(card));
1047 return rc ? -EIO : 0;
1050 static int
1051 qeth_diags_trace(struct qeth_card *card, enum qeth_diags_trace_cmds diags_cmd)
1053 struct qeth_cmd_buffer *iob;
1054 struct qeth_ipa_cmd *cmd;
1056 QETH_CARD_TEXT(card, 2, "diagtrac");
1058 iob = qeth_get_diag_cmd(card, QETH_DIAGS_CMD_TRACE, 0);
1059 if (!iob)
1060 return -ENOMEM;
1061 cmd = __ipa_cmd(iob);
1062 cmd->data.diagass.type = QETH_DIAGS_TYPE_HIPERSOCKET;
1063 cmd->data.diagass.action = diags_cmd;
1064 return qeth_send_ipa_cmd(card, iob, qeth_diags_trace_cb, NULL);
1067 static int qeth_l3_add_mcast_rtnl(struct net_device *dev, int vid, void *arg)
1069 struct qeth_card *card = arg;
1070 struct inet6_dev *in6_dev;
1071 struct in_device *in4_dev;
1072 struct qeth_ipaddr *ipm;
1073 struct qeth_ipaddr tmp;
1074 struct ip_mc_list *im4;
1075 struct ifmcaddr6 *im6;
1077 QETH_CARD_TEXT(card, 4, "addmc");
1079 if (!dev || !(dev->flags & IFF_UP))
1080 goto out;
1082 in4_dev = __in_dev_get_rtnl(dev);
1083 if (!in4_dev)
1084 goto walk_ipv6;
1086 qeth_l3_init_ipaddr(&tmp, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4);
1087 tmp.disp_flag = QETH_DISP_ADDR_ADD;
1088 tmp.is_multicast = 1;
1090 for (im4 = rtnl_dereference(in4_dev->mc_list); im4 != NULL;
1091 im4 = rtnl_dereference(im4->next_rcu)) {
1092 tmp.u.a4.addr = im4->multiaddr;
1094 ipm = qeth_l3_find_addr_by_ip(card, &tmp);
1095 if (ipm) {
1096 /* for mcast, by-IP match means full match */
1097 ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
1098 continue;
1101 ipm = kmemdup(&tmp, sizeof(tmp), GFP_KERNEL);
1102 if (!ipm)
1103 continue;
1105 hash_add(card->ip_mc_htable, &ipm->hnode,
1106 qeth_l3_ipaddr_hash(ipm));
1109 walk_ipv6:
1110 if (!qeth_is_supported(card, IPA_IPV6))
1111 goto out;
1113 in6_dev = __in6_dev_get(dev);
1114 if (!in6_dev)
1115 goto out;
1117 qeth_l3_init_ipaddr(&tmp, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6);
1118 tmp.disp_flag = QETH_DISP_ADDR_ADD;
1119 tmp.is_multicast = 1;
1121 read_lock_bh(&in6_dev->lock);
1122 for (im6 = in6_dev->mc_list; im6 != NULL; im6 = im6->next) {
1123 tmp.u.a6.addr = im6->mca_addr;
1125 ipm = qeth_l3_find_addr_by_ip(card, &tmp);
1126 if (ipm) {
1127 /* for mcast, by-IP match means full match */
1128 ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
1129 continue;
1132 ipm = kmemdup(&tmp, sizeof(tmp), GFP_ATOMIC);
1133 if (!ipm)
1134 continue;
1136 hash_add(card->ip_mc_htable,
1137 &ipm->hnode, qeth_l3_ipaddr_hash(ipm));
1140 read_unlock_bh(&in6_dev->lock);
1142 out:
1143 return 0;
1146 static int qeth_l3_vlan_rx_add_vid(struct net_device *dev,
1147 __be16 proto, u16 vid)
1149 struct qeth_card *card = dev->ml_priv;
1151 QETH_CARD_TEXT_(card, 4, "aid:%d", vid);
1152 return 0;
1155 static int qeth_l3_vlan_rx_kill_vid(struct net_device *dev,
1156 __be16 proto, u16 vid)
1158 struct qeth_card *card = dev->ml_priv;
1160 QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
1161 return 0;
1164 static void qeth_l3_stop_card(struct qeth_card *card)
1166 QETH_CARD_TEXT(card, 2, "stopcard");
1168 qeth_set_allowed_threads(card, 0, 1);
1170 cancel_work_sync(&card->rx_mode_work);
1171 qeth_l3_drain_rx_mode_cache(card);
1173 if (card->options.sniffer &&
1174 (card->info.promisc_mode == SET_PROMISC_MODE_ON))
1175 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
1177 if (card->state == CARD_STATE_SOFTSETUP) {
1178 qeth_l3_clear_ip_htable(card, 1);
1179 qeth_clear_ipacmd_list(card);
1180 qeth_drain_output_queues(card);
1181 card->state = CARD_STATE_DOWN;
1184 qeth_qdio_clear_card(card, 0);
1185 qeth_clear_working_pool_list(card);
1186 flush_workqueue(card->event_wq);
1187 card->info.promisc_mode = 0;
1190 static void qeth_l3_set_promisc_mode(struct qeth_card *card)
1192 bool enable = card->dev->flags & IFF_PROMISC;
1194 if (card->info.promisc_mode == enable)
1195 return;
1197 if (IS_VM_NIC(card)) { /* Guestlan trace */
1198 if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
1199 qeth_setadp_promisc_mode(card, enable);
1200 } else if (card->options.sniffer && /* HiperSockets trace */
1201 qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) {
1202 if (enable) {
1203 QETH_CARD_TEXT(card, 3, "+promisc");
1204 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_ENABLE);
1205 } else {
1206 QETH_CARD_TEXT(card, 3, "-promisc");
1207 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
1212 static void qeth_l3_rx_mode_work(struct work_struct *work)
1214 struct qeth_card *card = container_of(work, struct qeth_card,
1215 rx_mode_work);
1216 struct qeth_ipaddr *addr;
1217 struct hlist_node *tmp;
1218 int i, rc;
1220 QETH_CARD_TEXT(card, 3, "setmulti");
1222 if (!card->options.sniffer) {
1223 rtnl_lock();
1224 qeth_l3_add_mcast_rtnl(card->dev, 0, card);
1225 if (qeth_is_supported(card, IPA_FULL_VLAN))
1226 vlan_for_each(card->dev, qeth_l3_add_mcast_rtnl, card);
1227 rtnl_unlock();
1229 hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
1230 switch (addr->disp_flag) {
1231 case QETH_DISP_ADDR_DELETE:
1232 rc = qeth_l3_deregister_addr_entry(card, addr);
1233 if (!rc || rc == -ENOENT) {
1234 hash_del(&addr->hnode);
1235 kfree(addr);
1237 break;
1238 case QETH_DISP_ADDR_ADD:
1239 rc = qeth_l3_register_addr_entry(card, addr);
1240 if (rc && rc != -ENETDOWN) {
1241 hash_del(&addr->hnode);
1242 kfree(addr);
1243 break;
1245 addr->ref_counter = 1;
1246 /* fall through */
1247 default:
1248 /* for next call to set_rx_mode(): */
1249 addr->disp_flag = QETH_DISP_ADDR_DELETE;
1254 qeth_l3_set_promisc_mode(card);
1257 static int qeth_l3_arp_makerc(u16 rc)
1259 switch (rc) {
1260 case IPA_RC_SUCCESS:
1261 return 0;
1262 case QETH_IPA_ARP_RC_NOTSUPP:
1263 case QETH_IPA_ARP_RC_Q_NOTSUPP:
1264 return -EOPNOTSUPP;
1265 case QETH_IPA_ARP_RC_OUT_OF_RANGE:
1266 return -EINVAL;
1267 case QETH_IPA_ARP_RC_Q_NO_DATA:
1268 return -ENOENT;
1269 default:
1270 return -EIO;
1274 static int qeth_l3_arp_cmd_cb(struct qeth_card *card, struct qeth_reply *reply,
1275 unsigned long data)
1277 struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1279 qeth_setassparms_cb(card, reply, data);
1280 return qeth_l3_arp_makerc(cmd->hdr.return_code);
1283 static int qeth_l3_arp_set_no_entries(struct qeth_card *card, int no_entries)
1285 struct qeth_cmd_buffer *iob;
1286 int rc;
1288 QETH_CARD_TEXT(card, 3, "arpstnoe");
1291 * currently GuestLAN only supports the ARP assist function
1292 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_SET_NO_ENTRIES;
1293 * thus we say EOPNOTSUPP for this ARP function
1295 if (IS_VM_NIC(card))
1296 return -EOPNOTSUPP;
1297 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1298 return -EOPNOTSUPP;
1301 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1302 IPA_CMD_ASS_ARP_SET_NO_ENTRIES,
1303 SETASS_DATA_SIZEOF(flags_32bit),
1304 QETH_PROT_IPV4);
1305 if (!iob)
1306 return -ENOMEM;
1308 __ipa_cmd(iob)->data.setassparms.data.flags_32bit = (u32) no_entries;
1309 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL);
1310 if (rc)
1311 QETH_DBF_MESSAGE(2, "Could not set number of ARP entries on device %x: %#x\n",
1312 CARD_DEVID(card), rc);
1313 return rc;
1316 static __u32 get_arp_entry_size(struct qeth_card *card,
1317 struct qeth_arp_query_data *qdata,
1318 struct qeth_arp_entrytype *type, __u8 strip_entries)
1320 __u32 rc;
1321 __u8 is_hsi;
1323 is_hsi = qdata->reply_bits == 5;
1324 if (type->ip == QETHARP_IP_ADDR_V4) {
1325 QETH_CARD_TEXT(card, 4, "arpev4");
1326 if (strip_entries) {
1327 rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5_short) :
1328 sizeof(struct qeth_arp_qi_entry7_short);
1329 } else {
1330 rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5) :
1331 sizeof(struct qeth_arp_qi_entry7);
1333 } else if (type->ip == QETHARP_IP_ADDR_V6) {
1334 QETH_CARD_TEXT(card, 4, "arpev6");
1335 if (strip_entries) {
1336 rc = is_hsi ?
1337 sizeof(struct qeth_arp_qi_entry5_short_ipv6) :
1338 sizeof(struct qeth_arp_qi_entry7_short_ipv6);
1339 } else {
1340 rc = is_hsi ?
1341 sizeof(struct qeth_arp_qi_entry5_ipv6) :
1342 sizeof(struct qeth_arp_qi_entry7_ipv6);
1344 } else {
1345 QETH_CARD_TEXT(card, 4, "arpinv");
1346 rc = 0;
1349 return rc;
1352 static int arpentry_matches_prot(struct qeth_arp_entrytype *type, __u16 prot)
1354 return (type->ip == QETHARP_IP_ADDR_V4 && prot == QETH_PROT_IPV4) ||
1355 (type->ip == QETHARP_IP_ADDR_V6 && prot == QETH_PROT_IPV6);
1358 static int qeth_l3_arp_query_cb(struct qeth_card *card,
1359 struct qeth_reply *reply, unsigned long data)
1361 struct qeth_ipa_cmd *cmd;
1362 struct qeth_arp_query_data *qdata;
1363 struct qeth_arp_query_info *qinfo;
1364 int e;
1365 int entrybytes_done;
1366 int stripped_bytes;
1367 __u8 do_strip_entries;
1369 QETH_CARD_TEXT(card, 3, "arpquecb");
1371 qinfo = (struct qeth_arp_query_info *) reply->param;
1372 cmd = (struct qeth_ipa_cmd *) data;
1373 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.prot_version);
1374 if (cmd->hdr.return_code) {
1375 QETH_CARD_TEXT(card, 4, "arpcberr");
1376 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1377 return qeth_l3_arp_makerc(cmd->hdr.return_code);
1379 if (cmd->data.setassparms.hdr.return_code) {
1380 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
1381 QETH_CARD_TEXT(card, 4, "setaperr");
1382 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1383 return qeth_l3_arp_makerc(cmd->hdr.return_code);
1385 qdata = &cmd->data.setassparms.data.query_arp;
1386 QETH_CARD_TEXT_(card, 4, "anoen%i", qdata->no_entries);
1388 do_strip_entries = (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) > 0;
1389 stripped_bytes = do_strip_entries ? QETH_QARP_MEDIASPECIFIC_BYTES : 0;
1390 entrybytes_done = 0;
1391 for (e = 0; e < qdata->no_entries; ++e) {
1392 char *cur_entry;
1393 __u32 esize;
1394 struct qeth_arp_entrytype *etype;
1396 cur_entry = &qdata->data + entrybytes_done;
1397 etype = &((struct qeth_arp_qi_entry5 *) cur_entry)->type;
1398 if (!arpentry_matches_prot(etype, cmd->hdr.prot_version)) {
1399 QETH_CARD_TEXT(card, 4, "pmis");
1400 QETH_CARD_TEXT_(card, 4, "%i", etype->ip);
1401 break;
1403 esize = get_arp_entry_size(card, qdata, etype,
1404 do_strip_entries);
1405 QETH_CARD_TEXT_(card, 5, "esz%i", esize);
1406 if (!esize)
1407 break;
1409 if ((qinfo->udata_len - qinfo->udata_offset) < esize) {
1410 QETH_CARD_TEXT_(card, 4, "qaer3%i", -ENOSPC);
1411 memset(qinfo->udata, 0, 4);
1412 return -ENOSPC;
1415 memcpy(qinfo->udata + qinfo->udata_offset,
1416 &qdata->data + entrybytes_done + stripped_bytes,
1417 esize);
1418 entrybytes_done += esize + stripped_bytes;
1419 qinfo->udata_offset += esize;
1420 ++qinfo->no_entries;
1422 /* check if all replies received ... */
1423 if (cmd->data.setassparms.hdr.seq_no <
1424 cmd->data.setassparms.hdr.number_of_replies)
1425 return 1;
1426 QETH_CARD_TEXT_(card, 4, "nove%i", qinfo->no_entries);
1427 memcpy(qinfo->udata, &qinfo->no_entries, 4);
1428 /* keep STRIP_ENTRIES flag so the user program can distinguish
1429 * stripped entries from normal ones */
1430 if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
1431 qdata->reply_bits |= QETH_QARP_STRIP_ENTRIES;
1432 memcpy(qinfo->udata + QETH_QARP_MASK_OFFSET, &qdata->reply_bits, 2);
1433 QETH_CARD_TEXT_(card, 4, "rc%i", 0);
1434 return 0;
1437 static int qeth_l3_query_arp_cache_info(struct qeth_card *card,
1438 enum qeth_prot_versions prot,
1439 struct qeth_arp_query_info *qinfo)
1441 struct qeth_cmd_buffer *iob;
1442 struct qeth_ipa_cmd *cmd;
1443 int rc;
1445 QETH_CARD_TEXT_(card, 3, "qarpipv%i", prot);
1447 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1448 IPA_CMD_ASS_ARP_QUERY_INFO,
1449 SETASS_DATA_SIZEOF(query_arp), prot);
1450 if (!iob)
1451 return -ENOMEM;
1452 cmd = __ipa_cmd(iob);
1453 cmd->data.setassparms.data.query_arp.request_bits = 0x000F;
1454 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_query_cb, qinfo);
1455 if (rc)
1456 QETH_DBF_MESSAGE(2, "Error while querying ARP cache on device %x: %#x\n",
1457 CARD_DEVID(card), rc);
1458 return rc;
1461 static int qeth_l3_arp_query(struct qeth_card *card, char __user *udata)
1463 struct qeth_arp_query_info qinfo = {0, };
1464 int rc;
1466 QETH_CARD_TEXT(card, 3, "arpquery");
1468 if (!qeth_is_supported(card,/*IPA_QUERY_ARP_ADDR_INFO*/
1469 IPA_ARP_PROCESSING)) {
1470 QETH_CARD_TEXT(card, 3, "arpqnsup");
1471 rc = -EOPNOTSUPP;
1472 goto out;
1474 /* get size of userspace buffer and mask_bits -> 6 bytes */
1475 if (copy_from_user(&qinfo, udata, 6)) {
1476 rc = -EFAULT;
1477 goto out;
1479 qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
1480 if (!qinfo.udata) {
1481 rc = -ENOMEM;
1482 goto out;
1484 qinfo.udata_offset = QETH_QARP_ENTRIES_OFFSET;
1485 rc = qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV4, &qinfo);
1486 if (rc) {
1487 if (copy_to_user(udata, qinfo.udata, 4))
1488 rc = -EFAULT;
1489 goto free_and_out;
1491 if (qinfo.mask_bits & QETH_QARP_WITH_IPV6) {
1492 /* fails in case of GuestLAN QDIO mode */
1493 qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV6, &qinfo);
1495 if (copy_to_user(udata, qinfo.udata, qinfo.udata_len)) {
1496 QETH_CARD_TEXT(card, 4, "qactf");
1497 rc = -EFAULT;
1498 goto free_and_out;
1500 QETH_CARD_TEXT(card, 4, "qacts");
1502 free_and_out:
1503 kfree(qinfo.udata);
1504 out:
1505 return rc;
1508 static int qeth_l3_arp_modify_entry(struct qeth_card *card,
1509 struct qeth_arp_cache_entry *entry,
1510 enum qeth_arp_process_subcmds arp_cmd)
1512 struct qeth_arp_cache_entry *cmd_entry;
1513 struct qeth_cmd_buffer *iob;
1514 int rc;
1516 if (arp_cmd == IPA_CMD_ASS_ARP_ADD_ENTRY)
1517 QETH_CARD_TEXT(card, 3, "arpadd");
1518 else
1519 QETH_CARD_TEXT(card, 3, "arpdel");
1522 * currently GuestLAN only supports the ARP assist function
1523 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_ADD_ENTRY;
1524 * thus we say EOPNOTSUPP for this ARP function
1526 if (IS_VM_NIC(card))
1527 return -EOPNOTSUPP;
1528 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1529 return -EOPNOTSUPP;
1532 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING, arp_cmd,
1533 SETASS_DATA_SIZEOF(arp_entry),
1534 QETH_PROT_IPV4);
1535 if (!iob)
1536 return -ENOMEM;
1538 cmd_entry = &__ipa_cmd(iob)->data.setassparms.data.arp_entry;
1539 ether_addr_copy(cmd_entry->macaddr, entry->macaddr);
1540 memcpy(cmd_entry->ipaddr, entry->ipaddr, 4);
1541 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL);
1542 if (rc)
1543 QETH_DBF_MESSAGE(2, "Could not modify (cmd: %#x) ARP entry on device %x: %#x\n",
1544 arp_cmd, CARD_DEVID(card), rc);
1545 return rc;
1548 static int qeth_l3_arp_flush_cache(struct qeth_card *card)
1550 struct qeth_cmd_buffer *iob;
1551 int rc;
1553 QETH_CARD_TEXT(card, 3, "arpflush");
1556 * currently GuestLAN only supports the ARP assist function
1557 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_FLUSH_CACHE;
1558 * thus we say EOPNOTSUPP for this ARP function
1560 if (IS_VM_NIC(card) || IS_IQD(card))
1561 return -EOPNOTSUPP;
1562 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1563 return -EOPNOTSUPP;
1566 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1567 IPA_CMD_ASS_ARP_FLUSH_CACHE, 0,
1568 QETH_PROT_IPV4);
1569 if (!iob)
1570 return -ENOMEM;
1572 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL);
1573 if (rc)
1574 QETH_DBF_MESSAGE(2, "Could not flush ARP cache on device %x: %#x\n",
1575 CARD_DEVID(card), rc);
1576 return rc;
1579 static int qeth_l3_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1581 struct qeth_card *card = dev->ml_priv;
1582 struct qeth_arp_cache_entry arp_entry;
1583 enum qeth_arp_process_subcmds arp_cmd;
1584 int rc = 0;
1586 switch (cmd) {
1587 case SIOC_QETH_ARP_SET_NO_ENTRIES:
1588 if (!capable(CAP_NET_ADMIN)) {
1589 rc = -EPERM;
1590 break;
1592 rc = qeth_l3_arp_set_no_entries(card, rq->ifr_ifru.ifru_ivalue);
1593 break;
1594 case SIOC_QETH_ARP_QUERY_INFO:
1595 if (!capable(CAP_NET_ADMIN)) {
1596 rc = -EPERM;
1597 break;
1599 rc = qeth_l3_arp_query(card, rq->ifr_ifru.ifru_data);
1600 break;
1601 case SIOC_QETH_ARP_ADD_ENTRY:
1602 case SIOC_QETH_ARP_REMOVE_ENTRY:
1603 if (!capable(CAP_NET_ADMIN))
1604 return -EPERM;
1605 if (copy_from_user(&arp_entry, rq->ifr_data, sizeof(arp_entry)))
1606 return -EFAULT;
1608 arp_cmd = (cmd == SIOC_QETH_ARP_ADD_ENTRY) ?
1609 IPA_CMD_ASS_ARP_ADD_ENTRY :
1610 IPA_CMD_ASS_ARP_REMOVE_ENTRY;
1611 return qeth_l3_arp_modify_entry(card, &arp_entry, arp_cmd);
1612 case SIOC_QETH_ARP_FLUSH_CACHE:
1613 if (!capable(CAP_NET_ADMIN)) {
1614 rc = -EPERM;
1615 break;
1617 rc = qeth_l3_arp_flush_cache(card);
1618 break;
1619 default:
1620 rc = -EOPNOTSUPP;
1622 return rc;
1625 static int qeth_l3_get_cast_type_rcu(struct sk_buff *skb, struct dst_entry *dst,
1626 int ipv)
1628 struct neighbour *n = NULL;
1630 if (dst)
1631 n = dst_neigh_lookup_skb(dst, skb);
1633 if (n) {
1634 int cast_type = n->type;
1636 neigh_release(n);
1637 if ((cast_type == RTN_BROADCAST) ||
1638 (cast_type == RTN_MULTICAST) ||
1639 (cast_type == RTN_ANYCAST))
1640 return cast_type;
1641 return RTN_UNICAST;
1644 /* no neighbour (eg AF_PACKET), fall back to target's IP address ... */
1645 switch (ipv) {
1646 case 4:
1647 if (ipv4_is_lbcast(ip_hdr(skb)->daddr))
1648 return RTN_BROADCAST;
1649 return ipv4_is_multicast(ip_hdr(skb)->daddr) ?
1650 RTN_MULTICAST : RTN_UNICAST;
1651 case 6:
1652 return ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ?
1653 RTN_MULTICAST : RTN_UNICAST;
1654 default:
1655 /* ... and MAC address */
1656 return qeth_get_ether_cast_type(skb);
1660 static int qeth_l3_get_cast_type(struct sk_buff *skb)
1662 int ipv = qeth_get_ip_version(skb);
1663 struct dst_entry *dst;
1664 int cast_type;
1666 rcu_read_lock();
1667 dst = qeth_dst_check_rcu(skb, ipv);
1668 cast_type = qeth_l3_get_cast_type_rcu(skb, dst, ipv);
1669 rcu_read_unlock();
1671 return cast_type;
1674 static u8 qeth_l3_cast_type_to_flag(int cast_type)
1676 if (cast_type == RTN_MULTICAST)
1677 return QETH_CAST_MULTICAST;
1678 if (cast_type == RTN_ANYCAST)
1679 return QETH_CAST_ANYCAST;
1680 if (cast_type == RTN_BROADCAST)
1681 return QETH_CAST_BROADCAST;
1682 return QETH_CAST_UNICAST;
1685 static void qeth_l3_fill_header(struct qeth_qdio_out_q *queue,
1686 struct qeth_hdr *hdr, struct sk_buff *skb,
1687 int ipv, unsigned int data_len)
1689 struct qeth_hdr_layer3 *l3_hdr = &hdr->hdr.l3;
1690 struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
1691 struct qeth_card *card = queue->card;
1692 struct dst_entry *dst;
1693 int cast_type;
1695 hdr->hdr.l3.length = data_len;
1697 if (skb_is_gso(skb)) {
1698 hdr->hdr.l3.id = QETH_HEADER_TYPE_L3_TSO;
1699 } else {
1700 hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
1702 if (skb->protocol == htons(ETH_P_AF_IUCV)) {
1703 l3_hdr->flags = QETH_HDR_IPV6 | QETH_CAST_UNICAST;
1704 l3_hdr->next_hop.ipv6_addr.s6_addr16[0] = htons(0xfe80);
1705 memcpy(&l3_hdr->next_hop.ipv6_addr.s6_addr32[2],
1706 iucv_trans_hdr(skb)->destUserID, 8);
1707 return;
1710 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1711 qeth_tx_csum(skb, &hdr->hdr.l3.ext_flags, ipv);
1712 /* some HW requires combined L3+L4 csum offload: */
1713 if (ipv == 4)
1714 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_CSUM_HDR_REQ;
1718 if (ipv == 4 || IS_IQD(card)) {
1719 /* NETIF_F_HW_VLAN_CTAG_TX */
1720 if (skb_vlan_tag_present(skb)) {
1721 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_VLAN_FRAME;
1722 hdr->hdr.l3.vlan_id = skb_vlan_tag_get(skb);
1724 } else if (veth->h_vlan_proto == htons(ETH_P_8021Q)) {
1725 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_INCLUDE_VLAN_TAG;
1726 hdr->hdr.l3.vlan_id = ntohs(veth->h_vlan_TCI);
1729 rcu_read_lock();
1730 dst = qeth_dst_check_rcu(skb, ipv);
1732 if (IS_IQD(card) && skb_get_queue_mapping(skb) != QETH_IQD_MCAST_TXQ)
1733 cast_type = RTN_UNICAST;
1734 else
1735 cast_type = qeth_l3_get_cast_type_rcu(skb, dst, ipv);
1736 l3_hdr->flags |= qeth_l3_cast_type_to_flag(cast_type);
1738 if (ipv == 4) {
1739 struct rtable *rt = (struct rtable *) dst;
1741 *((__be32 *) &hdr->hdr.l3.next_hop.ipv4.addr) = (rt) ?
1742 rt_nexthop(rt, ip_hdr(skb)->daddr) :
1743 ip_hdr(skb)->daddr;
1744 } else if (ipv == 6) {
1745 struct rt6_info *rt = (struct rt6_info *) dst;
1747 if (rt && !ipv6_addr_any(&rt->rt6i_gateway))
1748 l3_hdr->next_hop.ipv6_addr = rt->rt6i_gateway;
1749 else
1750 l3_hdr->next_hop.ipv6_addr = ipv6_hdr(skb)->daddr;
1752 hdr->hdr.l3.flags |= QETH_HDR_IPV6;
1753 if (!IS_IQD(card))
1754 hdr->hdr.l3.flags |= QETH_HDR_PASSTHRU;
1755 } else {
1756 /* OSA only: */
1757 l3_hdr->flags |= QETH_HDR_PASSTHRU;
1759 rcu_read_unlock();
1762 static void qeth_l3_fixup_headers(struct sk_buff *skb)
1764 struct iphdr *iph = ip_hdr(skb);
1766 /* this is safe, IPv6 traffic takes a different path */
1767 if (skb->ip_summed == CHECKSUM_PARTIAL)
1768 iph->check = 0;
1769 if (skb_is_gso(skb)) {
1770 iph->tot_len = 0;
1771 tcp_hdr(skb)->check = ~tcp_v4_check(0, iph->saddr,
1772 iph->daddr, 0);
1776 static int qeth_l3_xmit(struct qeth_card *card, struct sk_buff *skb,
1777 struct qeth_qdio_out_q *queue, int ipv)
1779 unsigned int hw_hdr_len;
1780 int rc;
1782 /* re-use the L2 header area for the HW header: */
1783 hw_hdr_len = skb_is_gso(skb) ? sizeof(struct qeth_hdr_tso) :
1784 sizeof(struct qeth_hdr);
1785 rc = skb_cow_head(skb, hw_hdr_len - ETH_HLEN);
1786 if (rc)
1787 return rc;
1788 skb_pull(skb, ETH_HLEN);
1790 qeth_l3_fixup_headers(skb);
1791 return qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header);
1794 static netdev_tx_t qeth_l3_hard_start_xmit(struct sk_buff *skb,
1795 struct net_device *dev)
1797 struct qeth_card *card = dev->ml_priv;
1798 u16 txq = skb_get_queue_mapping(skb);
1799 int ipv = qeth_get_ip_version(skb);
1800 struct qeth_qdio_out_q *queue;
1801 int rc;
1803 if (!skb_is_gso(skb))
1804 qdisc_skb_cb(skb)->pkt_len = skb->len;
1805 if (IS_IQD(card)) {
1806 queue = card->qdio.out_qs[qeth_iqd_translate_txq(dev, txq)];
1808 if (card->options.sniffer)
1809 goto tx_drop;
1810 if ((card->options.cq != QETH_CQ_ENABLED && !ipv) ||
1811 (card->options.cq == QETH_CQ_ENABLED &&
1812 skb->protocol != htons(ETH_P_AF_IUCV)))
1813 goto tx_drop;
1814 } else {
1815 queue = card->qdio.out_qs[txq];
1818 if (!(dev->flags & IFF_BROADCAST) &&
1819 qeth_l3_get_cast_type(skb) == RTN_BROADCAST)
1820 goto tx_drop;
1822 if (ipv == 4 || IS_IQD(card))
1823 rc = qeth_l3_xmit(card, skb, queue, ipv);
1824 else
1825 rc = qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header);
1827 if (!rc)
1828 return NETDEV_TX_OK;
1830 tx_drop:
1831 QETH_TXQ_STAT_INC(queue, tx_dropped);
1832 kfree_skb(skb);
1833 return NETDEV_TX_OK;
1836 static void qeth_l3_set_rx_mode(struct net_device *dev)
1838 struct qeth_card *card = dev->ml_priv;
1840 schedule_work(&card->rx_mode_work);
1844 * we need NOARP for IPv4 but we want neighbor solicitation for IPv6. Setting
1845 * NOARP on the netdevice is no option because it also turns off neighbor
1846 * solicitation. For IPv4 we install a neighbor_setup function. We don't want
1847 * arp resolution but we want the hard header (packet socket will work
1848 * e.g. tcpdump)
1850 static int qeth_l3_neigh_setup_noarp(struct neighbour *n)
1852 n->nud_state = NUD_NOARP;
1853 memcpy(n->ha, "FAKELL", 6);
1854 n->output = n->ops->connected_output;
1855 return 0;
1858 static int
1859 qeth_l3_neigh_setup(struct net_device *dev, struct neigh_parms *np)
1861 if (np->tbl->family == AF_INET)
1862 np->neigh_setup = qeth_l3_neigh_setup_noarp;
1864 return 0;
1867 static netdev_features_t qeth_l3_osa_features_check(struct sk_buff *skb,
1868 struct net_device *dev,
1869 netdev_features_t features)
1871 if (qeth_get_ip_version(skb) != 4)
1872 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
1873 return qeth_features_check(skb, dev, features);
1876 static u16 qeth_l3_iqd_select_queue(struct net_device *dev, struct sk_buff *skb,
1877 struct net_device *sb_dev)
1879 return qeth_iqd_select_queue(dev, skb, qeth_l3_get_cast_type(skb),
1880 sb_dev);
1883 static u16 qeth_l3_osa_select_queue(struct net_device *dev, struct sk_buff *skb,
1884 struct net_device *sb_dev)
1886 struct qeth_card *card = dev->ml_priv;
1888 return qeth_get_priority_queue(card, skb);
1891 static const struct net_device_ops qeth_l3_netdev_ops = {
1892 .ndo_open = qeth_open,
1893 .ndo_stop = qeth_stop,
1894 .ndo_get_stats64 = qeth_get_stats64,
1895 .ndo_start_xmit = qeth_l3_hard_start_xmit,
1896 .ndo_select_queue = qeth_l3_iqd_select_queue,
1897 .ndo_validate_addr = eth_validate_addr,
1898 .ndo_set_rx_mode = qeth_l3_set_rx_mode,
1899 .ndo_do_ioctl = qeth_do_ioctl,
1900 .ndo_fix_features = qeth_fix_features,
1901 .ndo_set_features = qeth_set_features,
1902 .ndo_vlan_rx_add_vid = qeth_l3_vlan_rx_add_vid,
1903 .ndo_vlan_rx_kill_vid = qeth_l3_vlan_rx_kill_vid,
1904 .ndo_tx_timeout = qeth_tx_timeout,
1907 static const struct net_device_ops qeth_l3_osa_netdev_ops = {
1908 .ndo_open = qeth_open,
1909 .ndo_stop = qeth_stop,
1910 .ndo_get_stats64 = qeth_get_stats64,
1911 .ndo_start_xmit = qeth_l3_hard_start_xmit,
1912 .ndo_features_check = qeth_l3_osa_features_check,
1913 .ndo_select_queue = qeth_l3_osa_select_queue,
1914 .ndo_validate_addr = eth_validate_addr,
1915 .ndo_set_rx_mode = qeth_l3_set_rx_mode,
1916 .ndo_do_ioctl = qeth_do_ioctl,
1917 .ndo_fix_features = qeth_fix_features,
1918 .ndo_set_features = qeth_set_features,
1919 .ndo_vlan_rx_add_vid = qeth_l3_vlan_rx_add_vid,
1920 .ndo_vlan_rx_kill_vid = qeth_l3_vlan_rx_kill_vid,
1921 .ndo_tx_timeout = qeth_tx_timeout,
1922 .ndo_neigh_setup = qeth_l3_neigh_setup,
1925 static int qeth_l3_setup_netdev(struct qeth_card *card, bool carrier_ok)
1927 unsigned int headroom;
1928 int rc;
1930 if (IS_OSD(card) || IS_OSX(card)) {
1931 if ((card->info.link_type == QETH_LINK_TYPE_LANE_TR) ||
1932 (card->info.link_type == QETH_LINK_TYPE_HSTR)) {
1933 pr_info("qeth_l3: ignoring TR device\n");
1934 return -ENODEV;
1937 card->dev->netdev_ops = &qeth_l3_osa_netdev_ops;
1939 /*IPv6 address autoconfiguration stuff*/
1940 qeth_l3_get_unique_id(card);
1941 if (!(card->info.unique_id & UNIQUE_ID_NOT_BY_CARD))
1942 card->dev->dev_id = card->info.unique_id & 0xffff;
1944 if (!IS_VM_NIC(card)) {
1945 card->dev->features |= NETIF_F_SG;
1946 card->dev->hw_features |= NETIF_F_TSO |
1947 NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
1948 card->dev->vlan_features |= NETIF_F_TSO |
1949 NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
1952 if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) {
1953 card->dev->hw_features |= NETIF_F_IPV6_CSUM;
1954 card->dev->vlan_features |= NETIF_F_IPV6_CSUM;
1956 if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) {
1957 card->dev->hw_features |= NETIF_F_TSO6;
1958 card->dev->vlan_features |= NETIF_F_TSO6;
1961 /* allow for de-acceleration of NETIF_F_HW_VLAN_CTAG_TX: */
1962 if (card->dev->hw_features & NETIF_F_TSO6)
1963 headroom = sizeof(struct qeth_hdr_tso) + VLAN_HLEN;
1964 else if (card->dev->hw_features & NETIF_F_TSO)
1965 headroom = sizeof(struct qeth_hdr_tso);
1966 else
1967 headroom = sizeof(struct qeth_hdr) + VLAN_HLEN;
1968 } else if (IS_IQD(card)) {
1969 card->dev->flags |= IFF_NOARP;
1970 card->dev->netdev_ops = &qeth_l3_netdev_ops;
1971 headroom = sizeof(struct qeth_hdr) - ETH_HLEN;
1973 rc = qeth_l3_iqd_read_initial_mac(card);
1974 if (rc)
1975 goto out;
1976 } else
1977 return -ENODEV;
1979 card->dev->needed_headroom = headroom;
1980 card->dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
1981 NETIF_F_HW_VLAN_CTAG_RX |
1982 NETIF_F_HW_VLAN_CTAG_FILTER;
1984 netif_keep_dst(card->dev);
1985 if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6))
1986 netif_set_gso_max_size(card->dev,
1987 PAGE_SIZE * (QETH_MAX_BUFFER_ELEMENTS(card) - 1));
1989 netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
1990 rc = register_netdev(card->dev);
1991 if (!rc && carrier_ok)
1992 netif_carrier_on(card->dev);
1994 out:
1995 if (rc)
1996 card->dev->netdev_ops = NULL;
1997 return rc;
2000 static const struct device_type qeth_l3_devtype = {
2001 .name = "qeth_layer3",
2002 .groups = qeth_l3_attr_groups,
2005 static int qeth_l3_probe_device(struct ccwgroup_device *gdev)
2007 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2008 int rc;
2010 hash_init(card->ip_htable);
2011 mutex_init(&card->ip_lock);
2012 card->cmd_wq = alloc_ordered_workqueue("%s_cmd", 0,
2013 dev_name(&gdev->dev));
2014 if (!card->cmd_wq)
2015 return -ENOMEM;
2017 if (gdev->dev.type == &qeth_generic_devtype) {
2018 rc = qeth_l3_create_device_attributes(&gdev->dev);
2019 if (rc) {
2020 destroy_workqueue(card->cmd_wq);
2021 return rc;
2025 hash_init(card->ip_mc_htable);
2026 INIT_WORK(&card->rx_mode_work, qeth_l3_rx_mode_work);
2027 return 0;
2030 static void qeth_l3_remove_device(struct ccwgroup_device *cgdev)
2032 struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
2034 if (cgdev->dev.type == &qeth_generic_devtype)
2035 qeth_l3_remove_device_attributes(&cgdev->dev);
2037 qeth_set_allowed_threads(card, 0, 1);
2038 wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
2040 if (cgdev->state == CCWGROUP_ONLINE)
2041 qeth_set_offline(card, false);
2043 cancel_work_sync(&card->close_dev_work);
2044 if (qeth_netdev_is_registered(card->dev))
2045 unregister_netdev(card->dev);
2047 flush_workqueue(card->cmd_wq);
2048 destroy_workqueue(card->cmd_wq);
2049 qeth_l3_clear_ip_htable(card, 0);
2050 qeth_l3_clear_ipato_list(card);
2053 static int qeth_l3_set_online(struct qeth_card *card)
2055 struct ccwgroup_device *gdev = card->gdev;
2056 struct net_device *dev = card->dev;
2057 int rc = 0;
2058 bool carrier_ok;
2060 rc = qeth_core_hardsetup_card(card, &carrier_ok);
2061 if (rc) {
2062 QETH_CARD_TEXT_(card, 2, "2err%04x", rc);
2063 rc = -ENODEV;
2064 goto out_remove;
2067 qeth_print_status_message(card);
2069 /* softsetup */
2070 QETH_CARD_TEXT(card, 2, "softsetp");
2072 rc = qeth_l3_setadapter_parms(card);
2073 if (rc)
2074 QETH_CARD_TEXT_(card, 2, "2err%04x", rc);
2075 if (!card->options.sniffer) {
2076 qeth_l3_start_ipassists(card);
2078 rc = qeth_l3_setrouting_v4(card);
2079 if (rc)
2080 QETH_CARD_TEXT_(card, 2, "4err%04x", rc);
2081 rc = qeth_l3_setrouting_v6(card);
2082 if (rc)
2083 QETH_CARD_TEXT_(card, 2, "5err%04x", rc);
2086 card->state = CARD_STATE_SOFTSETUP;
2088 qeth_set_allowed_threads(card, 0xffffffff, 0);
2089 qeth_l3_recover_ip(card);
2091 if (!qeth_netdev_is_registered(dev)) {
2092 rc = qeth_l3_setup_netdev(card, carrier_ok);
2093 if (rc)
2094 goto out_remove;
2095 } else {
2096 rtnl_lock();
2097 if (carrier_ok)
2098 netif_carrier_on(dev);
2099 else
2100 netif_carrier_off(dev);
2102 netif_device_attach(dev);
2103 qeth_enable_hw_features(dev);
2105 if (card->info.open_when_online) {
2106 card->info.open_when_online = 0;
2107 dev_open(dev, NULL);
2109 rtnl_unlock();
2111 qeth_trace_features(card);
2112 /* let user_space know that device is online */
2113 kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
2114 return 0;
2115 out_remove:
2116 qeth_l3_stop_card(card);
2117 qeth_stop_channel(&card->data);
2118 qeth_stop_channel(&card->write);
2119 qeth_stop_channel(&card->read);
2120 qdio_free(CARD_DDEV(card));
2121 return rc;
2124 static void qeth_l3_set_offline(struct qeth_card *card)
2126 qeth_l3_stop_card(card);
2129 /* Returns zero if the command is successfully "consumed" */
2130 static int qeth_l3_control_event(struct qeth_card *card,
2131 struct qeth_ipa_cmd *cmd)
2133 return 1;
2136 struct qeth_discipline qeth_l3_discipline = {
2137 .devtype = &qeth_l3_devtype,
2138 .setup = qeth_l3_probe_device,
2139 .remove = qeth_l3_remove_device,
2140 .set_online = qeth_l3_set_online,
2141 .set_offline = qeth_l3_set_offline,
2142 .do_ioctl = qeth_l3_do_ioctl,
2143 .control_event_handler = qeth_l3_control_event,
2145 EXPORT_SYMBOL_GPL(qeth_l3_discipline);
2147 static int qeth_l3_handle_ip_event(struct qeth_card *card,
2148 struct qeth_ipaddr *addr,
2149 unsigned long event)
2151 switch (event) {
2152 case NETDEV_UP:
2153 qeth_l3_modify_ip(card, addr, true);
2154 return NOTIFY_OK;
2155 case NETDEV_DOWN:
2156 qeth_l3_modify_ip(card, addr, false);
2157 return NOTIFY_OK;
2158 default:
2159 return NOTIFY_DONE;
2163 struct qeth_l3_ip_event_work {
2164 struct work_struct work;
2165 struct qeth_card *card;
2166 struct qeth_ipaddr addr;
2169 #define to_ip_work(w) container_of((w), struct qeth_l3_ip_event_work, work)
2171 static void qeth_l3_add_ip_worker(struct work_struct *work)
2173 struct qeth_l3_ip_event_work *ip_work = to_ip_work(work);
2175 qeth_l3_modify_ip(ip_work->card, &ip_work->addr, true);
2176 kfree(work);
2179 static void qeth_l3_delete_ip_worker(struct work_struct *work)
2181 struct qeth_l3_ip_event_work *ip_work = to_ip_work(work);
2183 qeth_l3_modify_ip(ip_work->card, &ip_work->addr, false);
2184 kfree(work);
2187 static struct qeth_card *qeth_l3_get_card_from_dev(struct net_device *dev)
2189 if (is_vlan_dev(dev))
2190 dev = vlan_dev_real_dev(dev);
2191 if (dev->netdev_ops == &qeth_l3_osa_netdev_ops ||
2192 dev->netdev_ops == &qeth_l3_netdev_ops)
2193 return (struct qeth_card *) dev->ml_priv;
2194 return NULL;
2197 static int qeth_l3_ip_event(struct notifier_block *this,
2198 unsigned long event, void *ptr)
2201 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2202 struct net_device *dev = ifa->ifa_dev->dev;
2203 struct qeth_ipaddr addr;
2204 struct qeth_card *card;
2206 if (dev_net(dev) != &init_net)
2207 return NOTIFY_DONE;
2209 card = qeth_l3_get_card_from_dev(dev);
2210 if (!card)
2211 return NOTIFY_DONE;
2212 QETH_CARD_TEXT(card, 3, "ipevent");
2214 qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4);
2215 addr.u.a4.addr = ifa->ifa_address;
2216 addr.u.a4.mask = ifa->ifa_mask;
2218 return qeth_l3_handle_ip_event(card, &addr, event);
2221 static struct notifier_block qeth_l3_ip_notifier = {
2222 qeth_l3_ip_event,
2223 NULL,
2226 static int qeth_l3_ip6_event(struct notifier_block *this,
2227 unsigned long event, void *ptr)
2229 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
2230 struct net_device *dev = ifa->idev->dev;
2231 struct qeth_l3_ip_event_work *ip_work;
2232 struct qeth_card *card;
2234 if (event != NETDEV_UP && event != NETDEV_DOWN)
2235 return NOTIFY_DONE;
2237 card = qeth_l3_get_card_from_dev(dev);
2238 if (!card)
2239 return NOTIFY_DONE;
2240 QETH_CARD_TEXT(card, 3, "ip6event");
2241 if (!qeth_is_supported(card, IPA_IPV6))
2242 return NOTIFY_DONE;
2244 ip_work = kmalloc(sizeof(*ip_work), GFP_ATOMIC);
2245 if (!ip_work)
2246 return NOTIFY_DONE;
2248 if (event == NETDEV_UP)
2249 INIT_WORK(&ip_work->work, qeth_l3_add_ip_worker);
2250 else
2251 INIT_WORK(&ip_work->work, qeth_l3_delete_ip_worker);
2253 ip_work->card = card;
2254 qeth_l3_init_ipaddr(&ip_work->addr, QETH_IP_TYPE_NORMAL,
2255 QETH_PROT_IPV6);
2256 ip_work->addr.u.a6.addr = ifa->addr;
2257 ip_work->addr.u.a6.pfxlen = ifa->prefix_len;
2259 queue_work(card->cmd_wq, &ip_work->work);
2260 return NOTIFY_OK;
2263 static struct notifier_block qeth_l3_ip6_notifier = {
2264 qeth_l3_ip6_event,
2265 NULL,
2268 static int qeth_l3_register_notifiers(void)
2270 int rc;
2272 QETH_DBF_TEXT(SETUP, 5, "regnotif");
2273 rc = register_inetaddr_notifier(&qeth_l3_ip_notifier);
2274 if (rc)
2275 return rc;
2276 rc = register_inet6addr_notifier(&qeth_l3_ip6_notifier);
2277 if (rc) {
2278 unregister_inetaddr_notifier(&qeth_l3_ip_notifier);
2279 return rc;
2281 return 0;
2284 static void qeth_l3_unregister_notifiers(void)
2286 QETH_DBF_TEXT(SETUP, 5, "unregnot");
2287 WARN_ON(unregister_inetaddr_notifier(&qeth_l3_ip_notifier));
2288 WARN_ON(unregister_inet6addr_notifier(&qeth_l3_ip6_notifier));
2291 static int __init qeth_l3_init(void)
2293 pr_info("register layer 3 discipline\n");
2294 return qeth_l3_register_notifiers();
2297 static void __exit qeth_l3_exit(void)
2299 qeth_l3_unregister_notifiers();
2300 pr_info("unregister layer 3 discipline\n");
2303 module_init(qeth_l3_init);
2304 module_exit(qeth_l3_exit);
2305 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
2306 MODULE_DESCRIPTION("qeth layer 3 discipline");
2307 MODULE_LICENSE("GPL");