Merge tag 'riscv-for-linus-4.15-rc4-riscv_fixes' of git://git.kernel.org/pub/scm...
[linux/fpc-iii.git] / drivers / s390 / net / qeth_l3_main.c
blob6a73894b0cb51d2896bea853f6a33113fed98c13
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/ipv6.h>
22 #include <linux/inetdevice.h>
23 #include <linux/igmp.h>
24 #include <linux/slab.h>
25 #include <linux/if_vlan.h>
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 #include <linux/hashtable.h>
35 #include "qeth_l3.h"
38 static int qeth_l3_set_offline(struct ccwgroup_device *);
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_register_addr_entry(struct qeth_card *,
42 struct qeth_ipaddr *);
43 static int qeth_l3_deregister_addr_entry(struct qeth_card *,
44 struct qeth_ipaddr *);
46 static int qeth_l3_isxdigit(char *buf)
48 while (*buf) {
49 if (!isxdigit(*buf++))
50 return 0;
52 return 1;
55 static void qeth_l3_ipaddr4_to_string(const __u8 *addr, char *buf)
57 sprintf(buf, "%pI4", addr);
60 static int qeth_l3_string_to_ipaddr4(const char *buf, __u8 *addr)
62 int count = 0, rc = 0;
63 unsigned int in[4];
64 char c;
66 rc = sscanf(buf, "%u.%u.%u.%u%c",
67 &in[0], &in[1], &in[2], &in[3], &c);
68 if (rc != 4 && (rc != 5 || c != '\n'))
69 return -EINVAL;
70 for (count = 0; count < 4; count++) {
71 if (in[count] > 255)
72 return -EINVAL;
73 addr[count] = in[count];
75 return 0;
78 static void qeth_l3_ipaddr6_to_string(const __u8 *addr, char *buf)
80 sprintf(buf, "%pI6", addr);
83 static int qeth_l3_string_to_ipaddr6(const char *buf, __u8 *addr)
85 const char *end, *end_tmp, *start;
86 __u16 *in;
87 char num[5];
88 int num2, cnt, out, found, save_cnt;
89 unsigned short in_tmp[8] = {0, };
91 cnt = out = found = save_cnt = num2 = 0;
92 end = start = buf;
93 in = (__u16 *) addr;
94 memset(in, 0, 16);
95 while (*end) {
96 end = strchr(start, ':');
97 if (end == NULL) {
98 end = buf + strlen(buf);
99 end_tmp = strchr(start, '\n');
100 if (end_tmp != NULL)
101 end = end_tmp;
102 out = 1;
104 if ((end - start)) {
105 memset(num, 0, 5);
106 if ((end - start) > 4)
107 return -EINVAL;
108 memcpy(num, start, end - start);
109 if (!qeth_l3_isxdigit(num))
110 return -EINVAL;
111 sscanf(start, "%x", &num2);
112 if (found)
113 in_tmp[save_cnt++] = num2;
114 else
115 in[cnt++] = num2;
116 if (out)
117 break;
118 } else {
119 if (found)
120 return -EINVAL;
121 found = 1;
123 start = ++end;
125 if (cnt + save_cnt > 8)
126 return -EINVAL;
127 cnt = 7;
128 while (save_cnt)
129 in[cnt--] = in_tmp[--save_cnt];
130 return 0;
133 void qeth_l3_ipaddr_to_string(enum qeth_prot_versions proto, const __u8 *addr,
134 char *buf)
136 if (proto == QETH_PROT_IPV4)
137 qeth_l3_ipaddr4_to_string(addr, buf);
138 else if (proto == QETH_PROT_IPV6)
139 qeth_l3_ipaddr6_to_string(addr, buf);
142 int qeth_l3_string_to_ipaddr(const char *buf, enum qeth_prot_versions proto,
143 __u8 *addr)
145 if (proto == QETH_PROT_IPV4)
146 return qeth_l3_string_to_ipaddr4(buf, addr);
147 else if (proto == QETH_PROT_IPV6)
148 return qeth_l3_string_to_ipaddr6(buf, addr);
149 else
150 return -EINVAL;
153 static void qeth_l3_convert_addr_to_bits(u8 *addr, u8 *bits, int len)
155 int i, j;
156 u8 octet;
158 for (i = 0; i < len; ++i) {
159 octet = addr[i];
160 for (j = 7; j >= 0; --j) {
161 bits[i*8 + j] = octet & 1;
162 octet >>= 1;
167 int qeth_l3_is_addr_covered_by_ipato(struct qeth_card *card,
168 struct qeth_ipaddr *addr)
170 struct qeth_ipato_entry *ipatoe;
171 u8 addr_bits[128] = {0, };
172 u8 ipatoe_bits[128] = {0, };
173 int rc = 0;
175 if (!card->ipato.enabled)
176 return 0;
178 qeth_l3_convert_addr_to_bits((u8 *) &addr->u, addr_bits,
179 (addr->proto == QETH_PROT_IPV4)? 4:16);
180 list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
181 if (addr->proto != ipatoe->proto)
182 continue;
183 qeth_l3_convert_addr_to_bits(ipatoe->addr, ipatoe_bits,
184 (ipatoe->proto == QETH_PROT_IPV4) ?
185 4 : 16);
186 if (addr->proto == QETH_PROT_IPV4)
187 rc = !memcmp(addr_bits, ipatoe_bits,
188 min(32, ipatoe->mask_bits));
189 else
190 rc = !memcmp(addr_bits, ipatoe_bits,
191 min(128, ipatoe->mask_bits));
192 if (rc)
193 break;
195 /* invert? */
196 if ((addr->proto == QETH_PROT_IPV4) && card->ipato.invert4)
197 rc = !rc;
198 else if ((addr->proto == QETH_PROT_IPV6) && card->ipato.invert6)
199 rc = !rc;
201 return rc;
204 inline int
205 qeth_l3_ipaddrs_is_equal(struct qeth_ipaddr *addr1, struct qeth_ipaddr *addr2)
207 return addr1->proto == addr2->proto &&
208 !memcmp(&addr1->u, &addr2->u, sizeof(addr1->u)) &&
209 !memcmp(&addr1->mac, &addr2->mac, sizeof(addr1->mac));
212 static struct qeth_ipaddr *
213 qeth_l3_ip_from_hash(struct qeth_card *card, struct qeth_ipaddr *tmp_addr)
215 struct qeth_ipaddr *addr;
217 if (tmp_addr->is_multicast) {
218 hash_for_each_possible(card->ip_mc_htable, addr,
219 hnode, qeth_l3_ipaddr_hash(tmp_addr))
220 if (qeth_l3_ipaddrs_is_equal(tmp_addr, addr))
221 return addr;
222 } else {
223 hash_for_each_possible(card->ip_htable, addr,
224 hnode, qeth_l3_ipaddr_hash(tmp_addr))
225 if (qeth_l3_ipaddrs_is_equal(tmp_addr, addr))
226 return addr;
229 return NULL;
232 int qeth_l3_delete_ip(struct qeth_card *card, struct qeth_ipaddr *tmp_addr)
234 int rc = 0;
235 struct qeth_ipaddr *addr;
237 QETH_CARD_TEXT(card, 4, "delip");
239 if (tmp_addr->proto == QETH_PROT_IPV4)
240 QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
241 else {
242 QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
243 QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
246 addr = qeth_l3_ip_from_hash(card, tmp_addr);
247 if (!addr)
248 return -ENOENT;
250 addr->ref_counter--;
251 if (addr->ref_counter > 0 && (addr->type == QETH_IP_TYPE_NORMAL ||
252 addr->type == QETH_IP_TYPE_RXIP))
253 return rc;
254 if (addr->in_progress)
255 return -EINPROGRESS;
257 if (!qeth_card_hw_is_reachable(card)) {
258 addr->disp_flag = QETH_DISP_ADDR_DELETE;
259 return 0;
262 rc = qeth_l3_deregister_addr_entry(card, addr);
264 hash_del(&addr->hnode);
265 kfree(addr);
267 return rc;
270 int qeth_l3_add_ip(struct qeth_card *card, struct qeth_ipaddr *tmp_addr)
272 int rc = 0;
273 struct qeth_ipaddr *addr;
275 QETH_CARD_TEXT(card, 4, "addip");
277 if (tmp_addr->proto == QETH_PROT_IPV4)
278 QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
279 else {
280 QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
281 QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
284 addr = qeth_l3_ip_from_hash(card, tmp_addr);
285 if (!addr) {
286 addr = qeth_l3_get_addr_buffer(tmp_addr->proto);
287 if (!addr)
288 return -ENOMEM;
290 memcpy(addr, tmp_addr, sizeof(struct qeth_ipaddr));
291 addr->ref_counter = 1;
293 if (addr->type == QETH_IP_TYPE_NORMAL &&
294 qeth_l3_is_addr_covered_by_ipato(card, addr)) {
295 QETH_CARD_TEXT(card, 2, "tkovaddr");
296 addr->set_flags |= QETH_IPA_SETIP_TAKEOVER_FLAG;
298 hash_add(card->ip_htable, &addr->hnode,
299 qeth_l3_ipaddr_hash(addr));
301 if (!qeth_card_hw_is_reachable(card)) {
302 addr->disp_flag = QETH_DISP_ADDR_ADD;
303 return 0;
306 /* qeth_l3_register_addr_entry can go to sleep
307 * if we add a IPV4 addr. It is caused by the reason
308 * that SETIP ipa cmd starts ARP staff for IPV4 addr.
309 * Thus we should unlock spinlock, and make a protection
310 * using in_progress variable to indicate that there is
311 * an hardware operation with this IPV4 address
313 if (addr->proto == QETH_PROT_IPV4) {
314 addr->in_progress = 1;
315 spin_unlock_bh(&card->ip_lock);
316 rc = qeth_l3_register_addr_entry(card, addr);
317 spin_lock_bh(&card->ip_lock);
318 addr->in_progress = 0;
319 } else
320 rc = qeth_l3_register_addr_entry(card, addr);
322 if (!rc || (rc == IPA_RC_DUPLICATE_IP_ADDRESS) ||
323 (rc == IPA_RC_LAN_OFFLINE)) {
324 addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
325 if (addr->ref_counter < 1) {
326 qeth_l3_delete_ip(card, addr);
327 kfree(addr);
329 } else {
330 hash_del(&addr->hnode);
331 kfree(addr);
333 } else {
334 if (addr->type == QETH_IP_TYPE_NORMAL ||
335 addr->type == QETH_IP_TYPE_RXIP)
336 addr->ref_counter++;
339 return rc;
343 struct qeth_ipaddr *qeth_l3_get_addr_buffer(
344 enum qeth_prot_versions prot)
346 struct qeth_ipaddr *addr;
348 addr = kzalloc(sizeof(struct qeth_ipaddr), GFP_ATOMIC);
349 if (!addr)
350 return NULL;
352 addr->type = QETH_IP_TYPE_NORMAL;
353 addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
354 addr->proto = prot;
356 return addr;
359 static void qeth_l3_clear_ip_htable(struct qeth_card *card, int recover)
361 struct qeth_ipaddr *addr;
362 struct hlist_node *tmp;
363 int i;
365 QETH_CARD_TEXT(card, 4, "clearip");
367 if (recover && card->options.sniffer)
368 return;
370 spin_lock_bh(&card->ip_lock);
372 hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
373 if (!recover) {
374 hash_del(&addr->hnode);
375 kfree(addr);
376 continue;
378 addr->disp_flag = QETH_DISP_ADDR_ADD;
381 spin_unlock_bh(&card->ip_lock);
383 spin_lock_bh(&card->mclock);
385 hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
386 hash_del(&addr->hnode);
387 kfree(addr);
390 spin_unlock_bh(&card->mclock);
394 static void qeth_l3_recover_ip(struct qeth_card *card)
396 struct qeth_ipaddr *addr;
397 struct hlist_node *tmp;
398 int i;
399 int rc;
401 QETH_CARD_TEXT(card, 4, "recovrip");
403 spin_lock_bh(&card->ip_lock);
405 hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
406 if (addr->disp_flag == QETH_DISP_ADDR_DELETE) {
407 qeth_l3_deregister_addr_entry(card, addr);
408 hash_del(&addr->hnode);
409 kfree(addr);
410 } else if (addr->disp_flag == QETH_DISP_ADDR_ADD) {
411 if (addr->proto == QETH_PROT_IPV4) {
412 addr->in_progress = 1;
413 spin_unlock_bh(&card->ip_lock);
414 rc = qeth_l3_register_addr_entry(card, addr);
415 spin_lock_bh(&card->ip_lock);
416 addr->in_progress = 0;
417 } else
418 rc = qeth_l3_register_addr_entry(card, addr);
420 if (!rc) {
421 addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
422 if (addr->ref_counter < 1)
423 qeth_l3_delete_ip(card, addr);
424 } else {
425 hash_del(&addr->hnode);
426 kfree(addr);
431 spin_unlock_bh(&card->ip_lock);
435 static int qeth_l3_send_setdelmc(struct qeth_card *card,
436 struct qeth_ipaddr *addr, int ipacmd)
438 int rc;
439 struct qeth_cmd_buffer *iob;
440 struct qeth_ipa_cmd *cmd;
442 QETH_CARD_TEXT(card, 4, "setdelmc");
444 iob = qeth_get_ipacmd_buffer(card, ipacmd, addr->proto);
445 if (!iob)
446 return -ENOMEM;
447 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
448 memcpy(&cmd->data.setdelipm.mac, addr->mac, OSA_ADDR_LEN);
449 if (addr->proto == QETH_PROT_IPV6)
450 memcpy(cmd->data.setdelipm.ip6, &addr->u.a6.addr,
451 sizeof(struct in6_addr));
452 else
453 memcpy(&cmd->data.setdelipm.ip4, &addr->u.a4.addr, 4);
455 rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
457 return rc;
460 static void qeth_l3_fill_netmask(u8 *netmask, unsigned int len)
462 int i, j;
463 for (i = 0; i < 16; i++) {
464 j = (len) - (i * 8);
465 if (j >= 8)
466 netmask[i] = 0xff;
467 else if (j > 0)
468 netmask[i] = (u8)(0xFF00 >> j);
469 else
470 netmask[i] = 0;
474 static int qeth_l3_send_setdelip(struct qeth_card *card,
475 struct qeth_ipaddr *addr, int ipacmd, unsigned int flags)
477 int rc;
478 struct qeth_cmd_buffer *iob;
479 struct qeth_ipa_cmd *cmd;
480 __u8 netmask[16];
482 QETH_CARD_TEXT(card, 4, "setdelip");
483 QETH_CARD_TEXT_(card, 4, "flags%02X", flags);
485 iob = qeth_get_ipacmd_buffer(card, ipacmd, addr->proto);
486 if (!iob)
487 return -ENOMEM;
488 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
489 if (addr->proto == QETH_PROT_IPV6) {
490 memcpy(cmd->data.setdelip6.ip_addr, &addr->u.a6.addr,
491 sizeof(struct in6_addr));
492 qeth_l3_fill_netmask(netmask, addr->u.a6.pfxlen);
493 memcpy(cmd->data.setdelip6.mask, netmask,
494 sizeof(struct in6_addr));
495 cmd->data.setdelip6.flags = flags;
496 } else {
497 memcpy(cmd->data.setdelip4.ip_addr, &addr->u.a4.addr, 4);
498 memcpy(cmd->data.setdelip4.mask, &addr->u.a4.mask, 4);
499 cmd->data.setdelip4.flags = flags;
502 rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
504 return rc;
507 static int qeth_l3_send_setrouting(struct qeth_card *card,
508 enum qeth_routing_types type, enum qeth_prot_versions prot)
510 int rc;
511 struct qeth_ipa_cmd *cmd;
512 struct qeth_cmd_buffer *iob;
514 QETH_CARD_TEXT(card, 4, "setroutg");
515 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SETRTG, prot);
516 if (!iob)
517 return -ENOMEM;
518 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
519 cmd->data.setrtg.type = (type);
520 rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
522 return rc;
525 static int qeth_l3_correct_routing_type(struct qeth_card *card,
526 enum qeth_routing_types *type, enum qeth_prot_versions prot)
528 if (card->info.type == QETH_CARD_TYPE_IQD) {
529 switch (*type) {
530 case NO_ROUTER:
531 case PRIMARY_CONNECTOR:
532 case SECONDARY_CONNECTOR:
533 case MULTICAST_ROUTER:
534 return 0;
535 default:
536 goto out_inval;
538 } else {
539 switch (*type) {
540 case NO_ROUTER:
541 case PRIMARY_ROUTER:
542 case SECONDARY_ROUTER:
543 return 0;
544 case MULTICAST_ROUTER:
545 if (qeth_is_ipafunc_supported(card, prot,
546 IPA_OSA_MC_ROUTER))
547 return 0;
548 default:
549 goto out_inval;
552 out_inval:
553 *type = NO_ROUTER;
554 return -EINVAL;
557 int qeth_l3_setrouting_v4(struct qeth_card *card)
559 int rc;
561 QETH_CARD_TEXT(card, 3, "setrtg4");
563 rc = qeth_l3_correct_routing_type(card, &card->options.route4.type,
564 QETH_PROT_IPV4);
565 if (rc)
566 return rc;
568 rc = qeth_l3_send_setrouting(card, card->options.route4.type,
569 QETH_PROT_IPV4);
570 if (rc) {
571 card->options.route4.type = NO_ROUTER;
572 QETH_DBF_MESSAGE(2, "Error (0x%04x) while setting routing type"
573 " on %s. Type set to 'no router'.\n", rc,
574 QETH_CARD_IFNAME(card));
576 return rc;
579 int qeth_l3_setrouting_v6(struct qeth_card *card)
581 int rc = 0;
583 QETH_CARD_TEXT(card, 3, "setrtg6");
584 #ifdef CONFIG_QETH_IPV6
586 if (!qeth_is_supported(card, IPA_IPV6))
587 return 0;
588 rc = qeth_l3_correct_routing_type(card, &card->options.route6.type,
589 QETH_PROT_IPV6);
590 if (rc)
591 return rc;
593 rc = qeth_l3_send_setrouting(card, card->options.route6.type,
594 QETH_PROT_IPV6);
595 if (rc) {
596 card->options.route6.type = NO_ROUTER;
597 QETH_DBF_MESSAGE(2, "Error (0x%04x) while setting routing type"
598 " on %s. Type set to 'no router'.\n", rc,
599 QETH_CARD_IFNAME(card));
601 #endif
602 return rc;
606 * IP address takeover related functions
608 static void qeth_l3_clear_ipato_list(struct qeth_card *card)
610 struct qeth_ipato_entry *ipatoe, *tmp;
612 spin_lock_bh(&card->ip_lock);
614 list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
615 list_del(&ipatoe->entry);
616 kfree(ipatoe);
619 spin_unlock_bh(&card->ip_lock);
622 int qeth_l3_add_ipato_entry(struct qeth_card *card,
623 struct qeth_ipato_entry *new)
625 struct qeth_ipato_entry *ipatoe;
626 int rc = 0;
628 QETH_CARD_TEXT(card, 2, "addipato");
630 spin_lock_bh(&card->ip_lock);
632 list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
633 if (ipatoe->proto != new->proto)
634 continue;
635 if (!memcmp(ipatoe->addr, new->addr,
636 (ipatoe->proto == QETH_PROT_IPV4)? 4:16) &&
637 (ipatoe->mask_bits == new->mask_bits)) {
638 rc = -EEXIST;
639 break;
643 if (!rc)
644 list_add_tail(&new->entry, &card->ipato.entries);
646 spin_unlock_bh(&card->ip_lock);
648 return rc;
651 void qeth_l3_del_ipato_entry(struct qeth_card *card,
652 enum qeth_prot_versions proto, u8 *addr, int mask_bits)
654 struct qeth_ipato_entry *ipatoe, *tmp;
656 QETH_CARD_TEXT(card, 2, "delipato");
658 spin_lock_bh(&card->ip_lock);
660 list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
661 if (ipatoe->proto != proto)
662 continue;
663 if (!memcmp(ipatoe->addr, addr,
664 (proto == QETH_PROT_IPV4)? 4:16) &&
665 (ipatoe->mask_bits == mask_bits)) {
666 list_del(&ipatoe->entry);
667 kfree(ipatoe);
671 spin_unlock_bh(&card->ip_lock);
675 * VIPA related functions
677 int qeth_l3_add_vipa(struct qeth_card *card, enum qeth_prot_versions proto,
678 const u8 *addr)
680 struct qeth_ipaddr *ipaddr;
681 int rc = 0;
683 ipaddr = qeth_l3_get_addr_buffer(proto);
684 if (ipaddr) {
685 if (proto == QETH_PROT_IPV4) {
686 QETH_CARD_TEXT(card, 2, "addvipa4");
687 memcpy(&ipaddr->u.a4.addr, addr, 4);
688 ipaddr->u.a4.mask = 0;
689 } else if (proto == QETH_PROT_IPV6) {
690 QETH_CARD_TEXT(card, 2, "addvipa6");
691 memcpy(&ipaddr->u.a6.addr, addr, 16);
692 ipaddr->u.a6.pfxlen = 0;
694 ipaddr->type = QETH_IP_TYPE_VIPA;
695 ipaddr->set_flags = QETH_IPA_SETIP_VIPA_FLAG;
696 ipaddr->del_flags = QETH_IPA_DELIP_VIPA_FLAG;
697 } else
698 return -ENOMEM;
700 spin_lock_bh(&card->ip_lock);
702 if (qeth_l3_ip_from_hash(card, ipaddr))
703 rc = -EEXIST;
704 else
705 qeth_l3_add_ip(card, ipaddr);
707 spin_unlock_bh(&card->ip_lock);
709 kfree(ipaddr);
711 return rc;
714 void qeth_l3_del_vipa(struct qeth_card *card, enum qeth_prot_versions proto,
715 const u8 *addr)
717 struct qeth_ipaddr *ipaddr;
719 ipaddr = qeth_l3_get_addr_buffer(proto);
720 if (ipaddr) {
721 if (proto == QETH_PROT_IPV4) {
722 QETH_CARD_TEXT(card, 2, "delvipa4");
723 memcpy(&ipaddr->u.a4.addr, addr, 4);
724 ipaddr->u.a4.mask = 0;
725 } else if (proto == QETH_PROT_IPV6) {
726 QETH_CARD_TEXT(card, 2, "delvipa6");
727 memcpy(&ipaddr->u.a6.addr, addr, 16);
728 ipaddr->u.a6.pfxlen = 0;
730 ipaddr->type = QETH_IP_TYPE_VIPA;
731 } else
732 return;
734 spin_lock_bh(&card->ip_lock);
735 qeth_l3_delete_ip(card, ipaddr);
736 spin_unlock_bh(&card->ip_lock);
738 kfree(ipaddr);
742 * proxy ARP related functions
744 int qeth_l3_add_rxip(struct qeth_card *card, enum qeth_prot_versions proto,
745 const u8 *addr)
747 struct qeth_ipaddr *ipaddr;
748 int rc = 0;
750 ipaddr = qeth_l3_get_addr_buffer(proto);
751 if (ipaddr) {
752 if (proto == QETH_PROT_IPV4) {
753 QETH_CARD_TEXT(card, 2, "addrxip4");
754 memcpy(&ipaddr->u.a4.addr, addr, 4);
755 ipaddr->u.a4.mask = 0;
756 } else if (proto == QETH_PROT_IPV6) {
757 QETH_CARD_TEXT(card, 2, "addrxip6");
758 memcpy(&ipaddr->u.a6.addr, addr, 16);
759 ipaddr->u.a6.pfxlen = 0;
762 ipaddr->type = QETH_IP_TYPE_RXIP;
763 ipaddr->set_flags = QETH_IPA_SETIP_TAKEOVER_FLAG;
764 ipaddr->del_flags = 0;
765 } else
766 return -ENOMEM;
768 spin_lock_bh(&card->ip_lock);
770 if (qeth_l3_ip_from_hash(card, ipaddr))
771 rc = -EEXIST;
772 else
773 qeth_l3_add_ip(card, ipaddr);
775 spin_unlock_bh(&card->ip_lock);
777 kfree(ipaddr);
779 return rc;
782 void qeth_l3_del_rxip(struct qeth_card *card, enum qeth_prot_versions proto,
783 const u8 *addr)
785 struct qeth_ipaddr *ipaddr;
787 ipaddr = qeth_l3_get_addr_buffer(proto);
788 if (ipaddr) {
789 if (proto == QETH_PROT_IPV4) {
790 QETH_CARD_TEXT(card, 2, "delrxip4");
791 memcpy(&ipaddr->u.a4.addr, addr, 4);
792 ipaddr->u.a4.mask = 0;
793 } else if (proto == QETH_PROT_IPV6) {
794 QETH_CARD_TEXT(card, 2, "delrxip6");
795 memcpy(&ipaddr->u.a6.addr, addr, 16);
796 ipaddr->u.a6.pfxlen = 0;
798 ipaddr->type = QETH_IP_TYPE_RXIP;
799 } else
800 return;
802 spin_lock_bh(&card->ip_lock);
803 qeth_l3_delete_ip(card, ipaddr);
804 spin_unlock_bh(&card->ip_lock);
806 kfree(ipaddr);
809 static int qeth_l3_register_addr_entry(struct qeth_card *card,
810 struct qeth_ipaddr *addr)
812 char buf[50];
813 int rc = 0;
814 int cnt = 3;
817 if (addr->proto == QETH_PROT_IPV4) {
818 QETH_CARD_TEXT(card, 2, "setaddr4");
819 QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
820 } else if (addr->proto == QETH_PROT_IPV6) {
821 QETH_CARD_TEXT(card, 2, "setaddr6");
822 QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
823 QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
824 } else {
825 QETH_CARD_TEXT(card, 2, "setaddr?");
826 QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
828 do {
829 if (addr->is_multicast)
830 rc = qeth_l3_send_setdelmc(card, addr, IPA_CMD_SETIPM);
831 else
832 rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_SETIP,
833 addr->set_flags);
834 if (rc)
835 QETH_CARD_TEXT(card, 2, "failed");
836 } while ((--cnt > 0) && rc);
837 if (rc) {
838 QETH_CARD_TEXT(card, 2, "FAILED");
839 qeth_l3_ipaddr_to_string(addr->proto, (u8 *)&addr->u, buf);
840 dev_warn(&card->gdev->dev,
841 "Registering IP address %s failed\n", buf);
843 return rc;
846 static int qeth_l3_deregister_addr_entry(struct qeth_card *card,
847 struct qeth_ipaddr *addr)
849 int rc = 0;
851 if (addr->proto == QETH_PROT_IPV4) {
852 QETH_CARD_TEXT(card, 2, "deladdr4");
853 QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
854 } else if (addr->proto == QETH_PROT_IPV6) {
855 QETH_CARD_TEXT(card, 2, "deladdr6");
856 QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
857 QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
858 } else {
859 QETH_CARD_TEXT(card, 2, "deladdr?");
860 QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
862 if (addr->is_multicast)
863 rc = qeth_l3_send_setdelmc(card, addr, IPA_CMD_DELIPM);
864 else
865 rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_DELIP,
866 addr->del_flags);
867 if (rc)
868 QETH_CARD_TEXT(card, 2, "failed");
870 return rc;
873 static u8 qeth_l3_get_qeth_hdr_flags4(int cast_type)
875 if (cast_type == RTN_MULTICAST)
876 return QETH_CAST_MULTICAST;
877 if (cast_type == RTN_BROADCAST)
878 return QETH_CAST_BROADCAST;
879 return QETH_CAST_UNICAST;
882 static u8 qeth_l3_get_qeth_hdr_flags6(int cast_type)
884 u8 ct = QETH_HDR_PASSTHRU | QETH_HDR_IPV6;
885 if (cast_type == RTN_MULTICAST)
886 return ct | QETH_CAST_MULTICAST;
887 if (cast_type == RTN_ANYCAST)
888 return ct | QETH_CAST_ANYCAST;
889 if (cast_type == RTN_BROADCAST)
890 return ct | QETH_CAST_BROADCAST;
891 return ct | QETH_CAST_UNICAST;
894 static int qeth_l3_setadapter_parms(struct qeth_card *card)
896 int rc = 0;
898 QETH_DBF_TEXT(SETUP, 2, "setadprm");
900 if (qeth_adp_supported(card, IPA_SETADP_ALTER_MAC_ADDRESS)) {
901 rc = qeth_setadpparms_change_macaddr(card);
902 if (rc)
903 dev_warn(&card->gdev->dev, "Reading the adapter MAC"
904 " address failed\n");
907 return rc;
910 #ifdef CONFIG_QETH_IPV6
911 static int qeth_l3_send_simple_setassparms_ipv6(struct qeth_card *card,
912 enum qeth_ipa_funcs ipa_func, __u16 cmd_code)
914 int rc;
915 struct qeth_cmd_buffer *iob;
917 QETH_CARD_TEXT(card, 4, "simassp6");
918 iob = qeth_get_setassparms_cmd(card, ipa_func, cmd_code,
919 0, QETH_PROT_IPV6);
920 if (!iob)
921 return -ENOMEM;
922 rc = qeth_send_setassparms(card, iob, 0, 0,
923 qeth_setassparms_cb, NULL);
924 return rc;
926 #endif
928 static int qeth_l3_start_ipa_arp_processing(struct qeth_card *card)
930 int rc;
932 QETH_CARD_TEXT(card, 3, "ipaarp");
934 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
935 dev_info(&card->gdev->dev,
936 "ARP processing not supported on %s!\n",
937 QETH_CARD_IFNAME(card));
938 return 0;
940 rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
941 IPA_CMD_ASS_START, 0);
942 if (rc) {
943 dev_warn(&card->gdev->dev,
944 "Starting ARP processing support for %s failed\n",
945 QETH_CARD_IFNAME(card));
947 return rc;
950 static int qeth_l3_start_ipa_source_mac(struct qeth_card *card)
952 int rc;
954 QETH_CARD_TEXT(card, 3, "stsrcmac");
956 if (!qeth_is_supported(card, IPA_SOURCE_MAC)) {
957 dev_info(&card->gdev->dev,
958 "Inbound source MAC-address not supported on %s\n",
959 QETH_CARD_IFNAME(card));
960 return -EOPNOTSUPP;
963 rc = qeth_send_simple_setassparms(card, IPA_SOURCE_MAC,
964 IPA_CMD_ASS_START, 0);
965 if (rc)
966 dev_warn(&card->gdev->dev,
967 "Starting source MAC-address support for %s failed\n",
968 QETH_CARD_IFNAME(card));
969 return rc;
972 static int qeth_l3_start_ipa_vlan(struct qeth_card *card)
974 int rc = 0;
976 QETH_CARD_TEXT(card, 3, "strtvlan");
978 if (!qeth_is_supported(card, IPA_FULL_VLAN)) {
979 dev_info(&card->gdev->dev,
980 "VLAN not supported on %s\n", QETH_CARD_IFNAME(card));
981 return -EOPNOTSUPP;
984 rc = qeth_send_simple_setassparms(card, IPA_VLAN_PRIO,
985 IPA_CMD_ASS_START, 0);
986 if (rc) {
987 dev_warn(&card->gdev->dev,
988 "Starting VLAN support for %s failed\n",
989 QETH_CARD_IFNAME(card));
990 } else {
991 dev_info(&card->gdev->dev, "VLAN enabled\n");
993 return rc;
996 static int qeth_l3_start_ipa_multicast(struct qeth_card *card)
998 int rc;
1000 QETH_CARD_TEXT(card, 3, "stmcast");
1002 if (!qeth_is_supported(card, IPA_MULTICASTING)) {
1003 dev_info(&card->gdev->dev,
1004 "Multicast not supported on %s\n",
1005 QETH_CARD_IFNAME(card));
1006 return -EOPNOTSUPP;
1009 rc = qeth_send_simple_setassparms(card, IPA_MULTICASTING,
1010 IPA_CMD_ASS_START, 0);
1011 if (rc) {
1012 dev_warn(&card->gdev->dev,
1013 "Starting multicast support for %s failed\n",
1014 QETH_CARD_IFNAME(card));
1015 } else {
1016 dev_info(&card->gdev->dev, "Multicast enabled\n");
1017 card->dev->flags |= IFF_MULTICAST;
1019 return rc;
1022 #ifdef CONFIG_QETH_IPV6
1023 static int qeth_l3_softsetup_ipv6(struct qeth_card *card)
1025 int rc;
1027 QETH_CARD_TEXT(card, 3, "softipv6");
1029 rc = qeth_query_ipassists(card, QETH_PROT_IPV6);
1030 if (rc) {
1031 dev_err(&card->gdev->dev,
1032 "Activating IPv6 support for %s failed\n",
1033 QETH_CARD_IFNAME(card));
1034 return rc;
1037 if (card->info.type == QETH_CARD_TYPE_IQD)
1038 goto out;
1040 rc = qeth_send_simple_setassparms(card, IPA_IPV6,
1041 IPA_CMD_ASS_START, 3);
1042 if (rc) {
1043 dev_err(&card->gdev->dev,
1044 "Activating IPv6 support for %s failed\n",
1045 QETH_CARD_IFNAME(card));
1046 return rc;
1048 rc = qeth_l3_send_simple_setassparms_ipv6(card, IPA_IPV6,
1049 IPA_CMD_ASS_START);
1050 if (rc) {
1051 dev_err(&card->gdev->dev,
1052 "Activating IPv6 support for %s failed\n",
1053 QETH_CARD_IFNAME(card));
1054 return rc;
1056 rc = qeth_l3_send_simple_setassparms_ipv6(card, IPA_PASSTHRU,
1057 IPA_CMD_ASS_START);
1058 if (rc) {
1059 dev_warn(&card->gdev->dev,
1060 "Enabling the passthrough mode for %s failed\n",
1061 QETH_CARD_IFNAME(card));
1062 return rc;
1064 out:
1065 dev_info(&card->gdev->dev, "IPV6 enabled\n");
1066 return 0;
1068 #endif
1070 static int qeth_l3_start_ipa_ipv6(struct qeth_card *card)
1072 int rc = 0;
1074 QETH_CARD_TEXT(card, 3, "strtipv6");
1076 if (!qeth_is_supported(card, IPA_IPV6)) {
1077 dev_info(&card->gdev->dev,
1078 "IPv6 not supported on %s\n", QETH_CARD_IFNAME(card));
1079 return 0;
1081 #ifdef CONFIG_QETH_IPV6
1082 rc = qeth_l3_softsetup_ipv6(card);
1083 #endif
1084 return rc ;
1087 static int qeth_l3_start_ipa_broadcast(struct qeth_card *card)
1089 int rc;
1091 QETH_CARD_TEXT(card, 3, "stbrdcst");
1092 card->info.broadcast_capable = 0;
1093 if (!qeth_is_supported(card, IPA_FILTERING)) {
1094 dev_info(&card->gdev->dev,
1095 "Broadcast not supported on %s\n",
1096 QETH_CARD_IFNAME(card));
1097 rc = -EOPNOTSUPP;
1098 goto out;
1100 rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
1101 IPA_CMD_ASS_START, 0);
1102 if (rc) {
1103 dev_warn(&card->gdev->dev, "Enabling broadcast filtering for "
1104 "%s failed\n", QETH_CARD_IFNAME(card));
1105 goto out;
1108 rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
1109 IPA_CMD_ASS_CONFIGURE, 1);
1110 if (rc) {
1111 dev_warn(&card->gdev->dev,
1112 "Setting up broadcast filtering for %s failed\n",
1113 QETH_CARD_IFNAME(card));
1114 goto out;
1116 card->info.broadcast_capable = QETH_BROADCAST_WITH_ECHO;
1117 dev_info(&card->gdev->dev, "Broadcast enabled\n");
1118 rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
1119 IPA_CMD_ASS_ENABLE, 1);
1120 if (rc) {
1121 dev_warn(&card->gdev->dev, "Setting up broadcast echo "
1122 "filtering for %s failed\n", QETH_CARD_IFNAME(card));
1123 goto out;
1125 card->info.broadcast_capable = QETH_BROADCAST_WITHOUT_ECHO;
1126 out:
1127 if (card->info.broadcast_capable)
1128 card->dev->flags |= IFF_BROADCAST;
1129 else
1130 card->dev->flags &= ~IFF_BROADCAST;
1131 return rc;
1134 static int qeth_l3_start_ipassists(struct qeth_card *card)
1136 QETH_CARD_TEXT(card, 3, "strtipas");
1138 if (qeth_set_access_ctrl_online(card, 0))
1139 return -EIO;
1140 qeth_l3_start_ipa_arp_processing(card); /* go on*/
1141 qeth_l3_start_ipa_source_mac(card); /* go on*/
1142 qeth_l3_start_ipa_vlan(card); /* go on*/
1143 qeth_l3_start_ipa_multicast(card); /* go on*/
1144 qeth_l3_start_ipa_ipv6(card); /* go on*/
1145 qeth_l3_start_ipa_broadcast(card); /* go on*/
1146 return 0;
1149 static int qeth_l3_iqd_read_initial_mac_cb(struct qeth_card *card,
1150 struct qeth_reply *reply, unsigned long data)
1152 struct qeth_ipa_cmd *cmd;
1154 cmd = (struct qeth_ipa_cmd *) data;
1155 if (cmd->hdr.return_code == 0)
1156 memcpy(card->dev->dev_addr,
1157 cmd->data.create_destroy_addr.unique_id, ETH_ALEN);
1158 else
1159 eth_random_addr(card->dev->dev_addr);
1161 return 0;
1164 static int qeth_l3_iqd_read_initial_mac(struct qeth_card *card)
1166 int rc = 0;
1167 struct qeth_cmd_buffer *iob;
1168 struct qeth_ipa_cmd *cmd;
1170 QETH_DBF_TEXT(SETUP, 2, "hsrmac");
1172 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_CREATE_ADDR,
1173 QETH_PROT_IPV6);
1174 if (!iob)
1175 return -ENOMEM;
1176 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1177 *((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
1178 card->info.unique_id;
1180 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_iqd_read_initial_mac_cb,
1181 NULL);
1182 return rc;
1185 static int qeth_l3_get_unique_id_cb(struct qeth_card *card,
1186 struct qeth_reply *reply, unsigned long data)
1188 struct qeth_ipa_cmd *cmd;
1190 cmd = (struct qeth_ipa_cmd *) data;
1191 if (cmd->hdr.return_code == 0)
1192 card->info.unique_id = *((__u16 *)
1193 &cmd->data.create_destroy_addr.unique_id[6]);
1194 else {
1195 card->info.unique_id = UNIQUE_ID_IF_CREATE_ADDR_FAILED |
1196 UNIQUE_ID_NOT_BY_CARD;
1197 dev_warn(&card->gdev->dev, "The network adapter failed to "
1198 "generate a unique ID\n");
1200 return 0;
1203 static int qeth_l3_get_unique_id(struct qeth_card *card)
1205 int rc = 0;
1206 struct qeth_cmd_buffer *iob;
1207 struct qeth_ipa_cmd *cmd;
1209 QETH_DBF_TEXT(SETUP, 2, "guniqeid");
1211 if (!qeth_is_supported(card, IPA_IPV6)) {
1212 card->info.unique_id = UNIQUE_ID_IF_CREATE_ADDR_FAILED |
1213 UNIQUE_ID_NOT_BY_CARD;
1214 return 0;
1217 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_CREATE_ADDR,
1218 QETH_PROT_IPV6);
1219 if (!iob)
1220 return -ENOMEM;
1221 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1222 *((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
1223 card->info.unique_id;
1225 rc = qeth_send_ipa_cmd(card, iob, qeth_l3_get_unique_id_cb, NULL);
1226 return rc;
1229 static int
1230 qeth_diags_trace_cb(struct qeth_card *card, struct qeth_reply *reply,
1231 unsigned long data)
1233 struct qeth_ipa_cmd *cmd;
1234 __u16 rc;
1236 QETH_DBF_TEXT(SETUP, 2, "diastrcb");
1238 cmd = (struct qeth_ipa_cmd *)data;
1239 rc = cmd->hdr.return_code;
1240 if (rc)
1241 QETH_CARD_TEXT_(card, 2, "dxter%x", rc);
1242 switch (cmd->data.diagass.action) {
1243 case QETH_DIAGS_CMD_TRACE_QUERY:
1244 break;
1245 case QETH_DIAGS_CMD_TRACE_DISABLE:
1246 switch (rc) {
1247 case 0:
1248 case IPA_RC_INVALID_SUBCMD:
1249 card->info.promisc_mode = SET_PROMISC_MODE_OFF;
1250 dev_info(&card->gdev->dev, "The HiperSockets network "
1251 "traffic analyzer is deactivated\n");
1252 break;
1253 default:
1254 break;
1256 break;
1257 case QETH_DIAGS_CMD_TRACE_ENABLE:
1258 switch (rc) {
1259 case 0:
1260 card->info.promisc_mode = SET_PROMISC_MODE_ON;
1261 dev_info(&card->gdev->dev, "The HiperSockets network "
1262 "traffic analyzer is activated\n");
1263 break;
1264 case IPA_RC_HARDWARE_AUTH_ERROR:
1265 dev_warn(&card->gdev->dev, "The device is not "
1266 "authorized to run as a HiperSockets network "
1267 "traffic analyzer\n");
1268 break;
1269 case IPA_RC_TRACE_ALREADY_ACTIVE:
1270 dev_warn(&card->gdev->dev, "A HiperSockets "
1271 "network traffic analyzer is already "
1272 "active in the HiperSockets LAN\n");
1273 break;
1274 default:
1275 break;
1277 break;
1278 default:
1279 QETH_DBF_MESSAGE(2, "Unknown sniffer action (0x%04x) on %s\n",
1280 cmd->data.diagass.action, QETH_CARD_IFNAME(card));
1283 return 0;
1286 static int
1287 qeth_diags_trace(struct qeth_card *card, enum qeth_diags_trace_cmds diags_cmd)
1289 struct qeth_cmd_buffer *iob;
1290 struct qeth_ipa_cmd *cmd;
1292 QETH_DBF_TEXT(SETUP, 2, "diagtrac");
1294 iob = qeth_get_ipacmd_buffer(card, IPA_CMD_SET_DIAG_ASS, 0);
1295 if (!iob)
1296 return -ENOMEM;
1297 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
1298 cmd->data.diagass.subcmd_len = 16;
1299 cmd->data.diagass.subcmd = QETH_DIAGS_CMD_TRACE;
1300 cmd->data.diagass.type = QETH_DIAGS_TYPE_HIPERSOCKET;
1301 cmd->data.diagass.action = diags_cmd;
1302 return qeth_send_ipa_cmd(card, iob, qeth_diags_trace_cb, NULL);
1305 static void qeth_l3_get_mac_for_ipm(__be32 ipm, char *mac)
1307 ip_eth_mc_map(ipm, mac);
1310 static void qeth_l3_mark_all_mc_to_be_deleted(struct qeth_card *card)
1312 struct qeth_ipaddr *addr;
1313 int i;
1315 hash_for_each(card->ip_mc_htable, i, addr, hnode)
1316 addr->disp_flag = QETH_DISP_ADDR_DELETE;
1320 static void qeth_l3_add_all_new_mc(struct qeth_card *card)
1322 struct qeth_ipaddr *addr;
1323 struct hlist_node *tmp;
1324 int i;
1325 int rc;
1327 hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
1328 if (addr->disp_flag == QETH_DISP_ADDR_ADD) {
1329 rc = qeth_l3_register_addr_entry(card, addr);
1330 if (!rc || (rc == IPA_RC_LAN_OFFLINE))
1331 addr->ref_counter = 1;
1332 else {
1333 hash_del(&addr->hnode);
1334 kfree(addr);
1341 static void qeth_l3_delete_nonused_mc(struct qeth_card *card)
1343 struct qeth_ipaddr *addr;
1344 struct hlist_node *tmp;
1345 int i;
1346 int rc;
1348 hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
1349 if (addr->disp_flag == QETH_DISP_ADDR_DELETE) {
1350 rc = qeth_l3_deregister_addr_entry(card, addr);
1351 if (!rc || (rc == IPA_RC_MC_ADDR_NOT_FOUND)) {
1352 hash_del(&addr->hnode);
1353 kfree(addr);
1361 static void
1362 qeth_l3_add_mc_to_hash(struct qeth_card *card, struct in_device *in4_dev)
1364 struct ip_mc_list *im4;
1365 struct qeth_ipaddr *tmp, *ipm;
1366 char buf[MAX_ADDR_LEN];
1368 QETH_CARD_TEXT(card, 4, "addmc");
1370 tmp = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
1371 if (!tmp)
1372 return;
1374 for (im4 = rcu_dereference(in4_dev->mc_list); im4 != NULL;
1375 im4 = rcu_dereference(im4->next_rcu)) {
1376 qeth_l3_get_mac_for_ipm(im4->multiaddr, buf);
1378 tmp->u.a4.addr = be32_to_cpu(im4->multiaddr);
1379 memcpy(tmp->mac, buf, sizeof(tmp->mac));
1380 tmp->is_multicast = 1;
1382 ipm = qeth_l3_ip_from_hash(card, tmp);
1383 if (ipm) {
1384 ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
1385 } else {
1386 ipm = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
1387 if (!ipm)
1388 continue;
1389 memcpy(ipm->mac, buf, sizeof(tmp->mac));
1390 ipm->u.a4.addr = be32_to_cpu(im4->multiaddr);
1391 ipm->is_multicast = 1;
1392 ipm->disp_flag = QETH_DISP_ADDR_ADD;
1393 hash_add(card->ip_mc_htable,
1394 &ipm->hnode, qeth_l3_ipaddr_hash(ipm));
1398 kfree(tmp);
1401 /* called with rcu_read_lock */
1402 static void qeth_l3_add_vlan_mc(struct qeth_card *card)
1404 struct in_device *in_dev;
1405 u16 vid;
1407 QETH_CARD_TEXT(card, 4, "addmcvl");
1409 if (!qeth_is_supported(card, IPA_FULL_VLAN))
1410 return;
1412 for_each_set_bit(vid, card->active_vlans, VLAN_N_VID) {
1413 struct net_device *netdev;
1415 netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q),
1416 vid);
1417 if (netdev == NULL ||
1418 !(netdev->flags & IFF_UP))
1419 continue;
1420 in_dev = __in_dev_get_rcu(netdev);
1421 if (!in_dev)
1422 continue;
1423 qeth_l3_add_mc_to_hash(card, in_dev);
1427 static void qeth_l3_add_multicast_ipv4(struct qeth_card *card)
1429 struct in_device *in4_dev;
1431 QETH_CARD_TEXT(card, 4, "chkmcv4");
1433 rcu_read_lock();
1434 in4_dev = __in_dev_get_rcu(card->dev);
1435 if (in4_dev == NULL)
1436 goto unlock;
1437 qeth_l3_add_mc_to_hash(card, in4_dev);
1438 qeth_l3_add_vlan_mc(card);
1439 unlock:
1440 rcu_read_unlock();
1443 #ifdef CONFIG_QETH_IPV6
1444 static void
1445 qeth_l3_add_mc6_to_hash(struct qeth_card *card, struct inet6_dev *in6_dev)
1447 struct qeth_ipaddr *ipm;
1448 struct ifmcaddr6 *im6;
1449 struct qeth_ipaddr *tmp;
1450 char buf[MAX_ADDR_LEN];
1452 QETH_CARD_TEXT(card, 4, "addmc6");
1454 tmp = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
1455 if (!tmp)
1456 return;
1458 for (im6 = in6_dev->mc_list; im6 != NULL; im6 = im6->next) {
1459 ndisc_mc_map(&im6->mca_addr, buf, in6_dev->dev, 0);
1461 memcpy(tmp->mac, buf, sizeof(tmp->mac));
1462 memcpy(&tmp->u.a6.addr, &im6->mca_addr.s6_addr,
1463 sizeof(struct in6_addr));
1464 tmp->is_multicast = 1;
1466 ipm = qeth_l3_ip_from_hash(card, tmp);
1467 if (ipm) {
1468 ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
1469 continue;
1472 ipm = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
1473 if (!ipm)
1474 continue;
1476 memcpy(ipm->mac, buf, OSA_ADDR_LEN);
1477 memcpy(&ipm->u.a6.addr, &im6->mca_addr.s6_addr,
1478 sizeof(struct in6_addr));
1479 ipm->is_multicast = 1;
1480 ipm->disp_flag = QETH_DISP_ADDR_ADD;
1481 hash_add(card->ip_mc_htable,
1482 &ipm->hnode, qeth_l3_ipaddr_hash(ipm));
1485 kfree(tmp);
1488 /* called with rcu_read_lock */
1489 static void qeth_l3_add_vlan_mc6(struct qeth_card *card)
1491 struct inet6_dev *in_dev;
1492 u16 vid;
1494 QETH_CARD_TEXT(card, 4, "admc6vl");
1496 if (!qeth_is_supported(card, IPA_FULL_VLAN))
1497 return;
1499 for_each_set_bit(vid, card->active_vlans, VLAN_N_VID) {
1500 struct net_device *netdev;
1502 netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q),
1503 vid);
1504 if (netdev == NULL ||
1505 !(netdev->flags & IFF_UP))
1506 continue;
1507 in_dev = in6_dev_get(netdev);
1508 if (!in_dev)
1509 continue;
1510 read_lock_bh(&in_dev->lock);
1511 qeth_l3_add_mc6_to_hash(card, in_dev);
1512 read_unlock_bh(&in_dev->lock);
1513 in6_dev_put(in_dev);
1517 static void qeth_l3_add_multicast_ipv6(struct qeth_card *card)
1519 struct inet6_dev *in6_dev;
1521 QETH_CARD_TEXT(card, 4, "chkmcv6");
1523 if (!qeth_is_supported(card, IPA_IPV6))
1524 return ;
1525 in6_dev = in6_dev_get(card->dev);
1526 if (!in6_dev)
1527 return;
1529 rcu_read_lock();
1530 read_lock_bh(&in6_dev->lock);
1531 qeth_l3_add_mc6_to_hash(card, in6_dev);
1532 qeth_l3_add_vlan_mc6(card);
1533 read_unlock_bh(&in6_dev->lock);
1534 rcu_read_unlock();
1535 in6_dev_put(in6_dev);
1537 #endif /* CONFIG_QETH_IPV6 */
1539 static void qeth_l3_free_vlan_addresses4(struct qeth_card *card,
1540 unsigned short vid)
1542 struct in_device *in_dev;
1543 struct in_ifaddr *ifa;
1544 struct qeth_ipaddr *addr;
1545 struct net_device *netdev;
1547 QETH_CARD_TEXT(card, 4, "frvaddr4");
1549 netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q), vid);
1550 if (!netdev)
1551 return;
1552 in_dev = in_dev_get(netdev);
1553 if (!in_dev)
1554 return;
1556 addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
1557 if (!addr)
1558 goto out;
1560 spin_lock_bh(&card->ip_lock);
1562 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
1563 addr->u.a4.addr = be32_to_cpu(ifa->ifa_address);
1564 addr->u.a4.mask = be32_to_cpu(ifa->ifa_mask);
1565 addr->type = QETH_IP_TYPE_NORMAL;
1566 qeth_l3_delete_ip(card, addr);
1569 spin_unlock_bh(&card->ip_lock);
1571 kfree(addr);
1572 out:
1573 in_dev_put(in_dev);
1576 static void qeth_l3_free_vlan_addresses6(struct qeth_card *card,
1577 unsigned short vid)
1579 #ifdef CONFIG_QETH_IPV6
1580 struct inet6_dev *in6_dev;
1581 struct inet6_ifaddr *ifa;
1582 struct qeth_ipaddr *addr;
1583 struct net_device *netdev;
1585 QETH_CARD_TEXT(card, 4, "frvaddr6");
1587 netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q), vid);
1588 if (!netdev)
1589 return;
1591 in6_dev = in6_dev_get(netdev);
1592 if (!in6_dev)
1593 return;
1595 addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
1596 if (!addr)
1597 goto out;
1599 spin_lock_bh(&card->ip_lock);
1601 list_for_each_entry(ifa, &in6_dev->addr_list, if_list) {
1602 memcpy(&addr->u.a6.addr, &ifa->addr,
1603 sizeof(struct in6_addr));
1604 addr->u.a6.pfxlen = ifa->prefix_len;
1605 addr->type = QETH_IP_TYPE_NORMAL;
1606 qeth_l3_delete_ip(card, addr);
1609 spin_unlock_bh(&card->ip_lock);
1611 kfree(addr);
1612 out:
1613 in6_dev_put(in6_dev);
1614 #endif /* CONFIG_QETH_IPV6 */
1617 static void qeth_l3_free_vlan_addresses(struct qeth_card *card,
1618 unsigned short vid)
1620 rcu_read_lock();
1621 qeth_l3_free_vlan_addresses4(card, vid);
1622 qeth_l3_free_vlan_addresses6(card, vid);
1623 rcu_read_unlock();
1626 static int qeth_l3_vlan_rx_add_vid(struct net_device *dev,
1627 __be16 proto, u16 vid)
1629 struct qeth_card *card = dev->ml_priv;
1631 set_bit(vid, card->active_vlans);
1632 return 0;
1635 static int qeth_l3_vlan_rx_kill_vid(struct net_device *dev,
1636 __be16 proto, u16 vid)
1638 struct qeth_card *card = dev->ml_priv;
1640 QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
1642 if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
1643 QETH_CARD_TEXT(card, 3, "kidREC");
1644 return 0;
1646 /* unregister IP addresses of vlan device */
1647 qeth_l3_free_vlan_addresses(card, vid);
1648 clear_bit(vid, card->active_vlans);
1649 qeth_l3_set_multicast_list(card->dev);
1650 return 0;
1653 static void qeth_l3_rebuild_skb(struct qeth_card *card, struct sk_buff *skb,
1654 struct qeth_hdr *hdr)
1656 __u16 prot;
1657 struct iphdr *ip_hdr;
1658 unsigned char tg_addr[MAX_ADDR_LEN];
1660 if (!(hdr->hdr.l3.flags & QETH_HDR_PASSTHRU)) {
1661 prot = (hdr->hdr.l3.flags & QETH_HDR_IPV6) ? ETH_P_IPV6 :
1662 ETH_P_IP;
1663 switch (hdr->hdr.l3.flags & QETH_HDR_CAST_MASK) {
1664 case QETH_CAST_MULTICAST:
1665 switch (prot) {
1666 #ifdef CONFIG_QETH_IPV6
1667 case ETH_P_IPV6:
1668 ndisc_mc_map((struct in6_addr *)
1669 skb->data + 24,
1670 tg_addr, card->dev, 0);
1671 break;
1672 #endif
1673 case ETH_P_IP:
1674 ip_hdr = (struct iphdr *)skb->data;
1675 ip_eth_mc_map(ip_hdr->daddr, tg_addr);
1676 break;
1677 default:
1678 memcpy(tg_addr, card->dev->broadcast,
1679 card->dev->addr_len);
1681 card->stats.multicast++;
1682 skb->pkt_type = PACKET_MULTICAST;
1683 break;
1684 case QETH_CAST_BROADCAST:
1685 memcpy(tg_addr, card->dev->broadcast,
1686 card->dev->addr_len);
1687 card->stats.multicast++;
1688 skb->pkt_type = PACKET_BROADCAST;
1689 break;
1690 case QETH_CAST_UNICAST:
1691 case QETH_CAST_ANYCAST:
1692 case QETH_CAST_NOCAST:
1693 default:
1694 if (card->options.sniffer)
1695 skb->pkt_type = PACKET_OTHERHOST;
1696 else
1697 skb->pkt_type = PACKET_HOST;
1698 memcpy(tg_addr, card->dev->dev_addr,
1699 card->dev->addr_len);
1701 if (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_SRC_MAC_ADDR)
1702 card->dev->header_ops->create(skb, card->dev, prot,
1703 tg_addr, &hdr->hdr.l3.dest_addr[2],
1704 card->dev->addr_len);
1705 else
1706 card->dev->header_ops->create(skb, card->dev, prot,
1707 tg_addr, "FAKELL", card->dev->addr_len);
1710 skb->protocol = eth_type_trans(skb, card->dev);
1712 /* copy VLAN tag from hdr into skb */
1713 if (!card->options.sniffer &&
1714 (hdr->hdr.l3.ext_flags & (QETH_HDR_EXT_VLAN_FRAME |
1715 QETH_HDR_EXT_INCLUDE_VLAN_TAG))) {
1716 u16 tag = (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_VLAN_FRAME) ?
1717 hdr->hdr.l3.vlan_id :
1718 *((u16 *)&hdr->hdr.l3.dest_addr[12]);
1719 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), tag);
1722 if (card->dev->features & NETIF_F_RXCSUM) {
1723 if ((hdr->hdr.l3.ext_flags &
1724 (QETH_HDR_EXT_CSUM_HDR_REQ |
1725 QETH_HDR_EXT_CSUM_TRANSP_REQ)) ==
1726 (QETH_HDR_EXT_CSUM_HDR_REQ |
1727 QETH_HDR_EXT_CSUM_TRANSP_REQ))
1728 skb->ip_summed = CHECKSUM_UNNECESSARY;
1729 else
1730 skb->ip_summed = CHECKSUM_NONE;
1731 } else
1732 skb->ip_summed = CHECKSUM_NONE;
1735 static int qeth_l3_process_inbound_buffer(struct qeth_card *card,
1736 int budget, int *done)
1738 int work_done = 0;
1739 struct sk_buff *skb;
1740 struct qeth_hdr *hdr;
1741 unsigned int len;
1742 __u16 magic;
1744 *done = 0;
1745 WARN_ON_ONCE(!budget);
1746 while (budget) {
1747 skb = qeth_core_get_next_skb(card,
1748 &card->qdio.in_q->bufs[card->rx.b_index],
1749 &card->rx.b_element, &card->rx.e_offset, &hdr);
1750 if (!skb) {
1751 *done = 1;
1752 break;
1754 skb->dev = card->dev;
1755 switch (hdr->hdr.l3.id) {
1756 case QETH_HEADER_TYPE_LAYER3:
1757 magic = *(__u16 *)skb->data;
1758 if ((card->info.type == QETH_CARD_TYPE_IQD) &&
1759 (magic == ETH_P_AF_IUCV)) {
1760 skb->protocol = cpu_to_be16(ETH_P_AF_IUCV);
1761 skb->pkt_type = PACKET_HOST;
1762 skb->mac_header = NET_SKB_PAD;
1763 skb->dev = card->dev;
1764 len = skb->len;
1765 card->dev->header_ops->create(skb, card->dev, 0,
1766 card->dev->dev_addr, "FAKELL",
1767 card->dev->addr_len);
1768 netif_receive_skb(skb);
1769 } else {
1770 qeth_l3_rebuild_skb(card, skb, hdr);
1771 len = skb->len;
1772 napi_gro_receive(&card->napi, skb);
1774 break;
1775 case QETH_HEADER_TYPE_LAYER2: /* for HiperSockets sniffer */
1776 skb->pkt_type = PACKET_HOST;
1777 skb->protocol = eth_type_trans(skb, skb->dev);
1778 len = skb->len;
1779 netif_receive_skb(skb);
1780 break;
1781 default:
1782 dev_kfree_skb_any(skb);
1783 QETH_CARD_TEXT(card, 3, "inbunkno");
1784 QETH_DBF_HEX(CTRL, 3, hdr, QETH_DBF_CTRL_LEN);
1785 continue;
1787 work_done++;
1788 budget--;
1789 card->stats.rx_packets++;
1790 card->stats.rx_bytes += len;
1792 return work_done;
1795 static int qeth_l3_verify_vlan_dev(struct net_device *dev,
1796 struct qeth_card *card)
1798 int rc = 0;
1799 u16 vid;
1801 for_each_set_bit(vid, card->active_vlans, VLAN_N_VID) {
1802 struct net_device *netdev;
1804 rcu_read_lock();
1805 netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q),
1806 vid);
1807 rcu_read_unlock();
1808 if (netdev == dev) {
1809 rc = QETH_VLAN_CARD;
1810 break;
1814 if (rc && !(vlan_dev_real_dev(dev)->ml_priv == (void *)card))
1815 return 0;
1817 return rc;
1820 static int qeth_l3_verify_dev(struct net_device *dev)
1822 struct qeth_card *card;
1823 int rc = 0;
1824 unsigned long flags;
1826 read_lock_irqsave(&qeth_core_card_list.rwlock, flags);
1827 list_for_each_entry(card, &qeth_core_card_list.list, list) {
1828 if (card->dev == dev) {
1829 rc = QETH_REAL_CARD;
1830 break;
1832 rc = qeth_l3_verify_vlan_dev(dev, card);
1833 if (rc)
1834 break;
1836 read_unlock_irqrestore(&qeth_core_card_list.rwlock, flags);
1838 return rc;
1841 static struct qeth_card *qeth_l3_get_card_from_dev(struct net_device *dev)
1843 struct qeth_card *card = NULL;
1844 int rc;
1846 rc = qeth_l3_verify_dev(dev);
1847 if (rc == QETH_REAL_CARD)
1848 card = dev->ml_priv;
1849 else if (rc == QETH_VLAN_CARD)
1850 card = vlan_dev_real_dev(dev)->ml_priv;
1851 if (card && card->options.layer2)
1852 card = NULL;
1853 if (card)
1854 QETH_CARD_TEXT_(card, 4, "%d", rc);
1855 return card ;
1858 static void qeth_l3_stop_card(struct qeth_card *card, int recovery_mode)
1860 QETH_DBF_TEXT(SETUP, 2, "stopcard");
1861 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1863 qeth_set_allowed_threads(card, 0, 1);
1864 if (card->options.sniffer &&
1865 (card->info.promisc_mode == SET_PROMISC_MODE_ON))
1866 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
1867 if (card->read.state == CH_STATE_UP &&
1868 card->write.state == CH_STATE_UP &&
1869 (card->state == CARD_STATE_UP)) {
1870 if (recovery_mode)
1871 qeth_l3_stop(card->dev);
1872 else {
1873 rtnl_lock();
1874 dev_close(card->dev);
1875 rtnl_unlock();
1877 card->state = CARD_STATE_SOFTSETUP;
1879 if (card->state == CARD_STATE_SOFTSETUP) {
1880 qeth_l3_clear_ip_htable(card, 1);
1881 qeth_clear_ipacmd_list(card);
1882 card->state = CARD_STATE_HARDSETUP;
1884 if (card->state == CARD_STATE_HARDSETUP) {
1885 qeth_qdio_clear_card(card, 0);
1886 qeth_clear_qdio_buffers(card);
1887 qeth_clear_working_pool_list(card);
1888 card->state = CARD_STATE_DOWN;
1890 if (card->state == CARD_STATE_DOWN) {
1891 qeth_clear_cmd_buffers(&card->read);
1892 qeth_clear_cmd_buffers(&card->write);
1897 * test for and Switch promiscuous mode (on or off)
1898 * either for guestlan or HiperSocket Sniffer
1900 static void
1901 qeth_l3_handle_promisc_mode(struct qeth_card *card)
1903 struct net_device *dev = card->dev;
1905 if (((dev->flags & IFF_PROMISC) &&
1906 (card->info.promisc_mode == SET_PROMISC_MODE_ON)) ||
1907 (!(dev->flags & IFF_PROMISC) &&
1908 (card->info.promisc_mode == SET_PROMISC_MODE_OFF)))
1909 return;
1911 if (card->info.guestlan) { /* Guestlan trace */
1912 if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
1913 qeth_setadp_promisc_mode(card);
1914 } else if (card->options.sniffer && /* HiperSockets trace */
1915 qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) {
1916 if (dev->flags & IFF_PROMISC) {
1917 QETH_CARD_TEXT(card, 3, "+promisc");
1918 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_ENABLE);
1919 } else {
1920 QETH_CARD_TEXT(card, 3, "-promisc");
1921 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
1926 static void qeth_l3_set_multicast_list(struct net_device *dev)
1928 struct qeth_card *card = dev->ml_priv;
1930 QETH_CARD_TEXT(card, 3, "setmulti");
1931 if (qeth_threads_running(card, QETH_RECOVER_THREAD) &&
1932 (card->state != CARD_STATE_UP))
1933 return;
1934 if (!card->options.sniffer) {
1936 spin_lock_bh(&card->mclock);
1938 qeth_l3_mark_all_mc_to_be_deleted(card);
1940 qeth_l3_add_multicast_ipv4(card);
1941 #ifdef CONFIG_QETH_IPV6
1942 qeth_l3_add_multicast_ipv6(card);
1943 #endif
1944 qeth_l3_delete_nonused_mc(card);
1945 qeth_l3_add_all_new_mc(card);
1947 spin_unlock_bh(&card->mclock);
1949 if (!qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
1950 return;
1952 qeth_l3_handle_promisc_mode(card);
1955 static const char *qeth_l3_arp_get_error_cause(int *rc)
1957 switch (*rc) {
1958 case QETH_IPA_ARP_RC_FAILED:
1959 *rc = -EIO;
1960 return "operation failed";
1961 case QETH_IPA_ARP_RC_NOTSUPP:
1962 *rc = -EOPNOTSUPP;
1963 return "operation not supported";
1964 case QETH_IPA_ARP_RC_OUT_OF_RANGE:
1965 *rc = -EINVAL;
1966 return "argument out of range";
1967 case QETH_IPA_ARP_RC_Q_NOTSUPP:
1968 *rc = -EOPNOTSUPP;
1969 return "query operation not supported";
1970 case QETH_IPA_ARP_RC_Q_NO_DATA:
1971 *rc = -ENOENT;
1972 return "no query data available";
1973 default:
1974 return "unknown error";
1978 static int qeth_l3_arp_set_no_entries(struct qeth_card *card, int no_entries)
1980 int tmp;
1981 int rc;
1983 QETH_CARD_TEXT(card, 3, "arpstnoe");
1986 * currently GuestLAN only supports the ARP assist function
1987 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_SET_NO_ENTRIES;
1988 * thus we say EOPNOTSUPP for this ARP function
1990 if (card->info.guestlan)
1991 return -EOPNOTSUPP;
1992 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1993 return -EOPNOTSUPP;
1995 rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
1996 IPA_CMD_ASS_ARP_SET_NO_ENTRIES,
1997 no_entries);
1998 if (rc) {
1999 tmp = rc;
2000 QETH_DBF_MESSAGE(2, "Could not set number of ARP entries on "
2001 "%s: %s (0x%x/%d)\n", QETH_CARD_IFNAME(card),
2002 qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2004 return rc;
2007 static __u32 get_arp_entry_size(struct qeth_card *card,
2008 struct qeth_arp_query_data *qdata,
2009 struct qeth_arp_entrytype *type, __u8 strip_entries)
2011 __u32 rc;
2012 __u8 is_hsi;
2014 is_hsi = qdata->reply_bits == 5;
2015 if (type->ip == QETHARP_IP_ADDR_V4) {
2016 QETH_CARD_TEXT(card, 4, "arpev4");
2017 if (strip_entries) {
2018 rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5_short) :
2019 sizeof(struct qeth_arp_qi_entry7_short);
2020 } else {
2021 rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5) :
2022 sizeof(struct qeth_arp_qi_entry7);
2024 } else if (type->ip == QETHARP_IP_ADDR_V6) {
2025 QETH_CARD_TEXT(card, 4, "arpev6");
2026 if (strip_entries) {
2027 rc = is_hsi ?
2028 sizeof(struct qeth_arp_qi_entry5_short_ipv6) :
2029 sizeof(struct qeth_arp_qi_entry7_short_ipv6);
2030 } else {
2031 rc = is_hsi ?
2032 sizeof(struct qeth_arp_qi_entry5_ipv6) :
2033 sizeof(struct qeth_arp_qi_entry7_ipv6);
2035 } else {
2036 QETH_CARD_TEXT(card, 4, "arpinv");
2037 rc = 0;
2040 return rc;
2043 static int arpentry_matches_prot(struct qeth_arp_entrytype *type, __u16 prot)
2045 return (type->ip == QETHARP_IP_ADDR_V4 && prot == QETH_PROT_IPV4) ||
2046 (type->ip == QETHARP_IP_ADDR_V6 && prot == QETH_PROT_IPV6);
2049 static int qeth_l3_arp_query_cb(struct qeth_card *card,
2050 struct qeth_reply *reply, unsigned long data)
2052 struct qeth_ipa_cmd *cmd;
2053 struct qeth_arp_query_data *qdata;
2054 struct qeth_arp_query_info *qinfo;
2055 int i;
2056 int e;
2057 int entrybytes_done;
2058 int stripped_bytes;
2059 __u8 do_strip_entries;
2061 QETH_CARD_TEXT(card, 3, "arpquecb");
2063 qinfo = (struct qeth_arp_query_info *) reply->param;
2064 cmd = (struct qeth_ipa_cmd *) data;
2065 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.prot_version);
2066 if (cmd->hdr.return_code) {
2067 QETH_CARD_TEXT(card, 4, "arpcberr");
2068 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
2069 return 0;
2071 if (cmd->data.setassparms.hdr.return_code) {
2072 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
2073 QETH_CARD_TEXT(card, 4, "setaperr");
2074 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
2075 return 0;
2077 qdata = &cmd->data.setassparms.data.query_arp;
2078 QETH_CARD_TEXT_(card, 4, "anoen%i", qdata->no_entries);
2080 do_strip_entries = (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) > 0;
2081 stripped_bytes = do_strip_entries ? QETH_QARP_MEDIASPECIFIC_BYTES : 0;
2082 entrybytes_done = 0;
2083 for (e = 0; e < qdata->no_entries; ++e) {
2084 char *cur_entry;
2085 __u32 esize;
2086 struct qeth_arp_entrytype *etype;
2088 cur_entry = &qdata->data + entrybytes_done;
2089 etype = &((struct qeth_arp_qi_entry5 *) cur_entry)->type;
2090 if (!arpentry_matches_prot(etype, cmd->hdr.prot_version)) {
2091 QETH_CARD_TEXT(card, 4, "pmis");
2092 QETH_CARD_TEXT_(card, 4, "%i", etype->ip);
2093 break;
2095 esize = get_arp_entry_size(card, qdata, etype,
2096 do_strip_entries);
2097 QETH_CARD_TEXT_(card, 5, "esz%i", esize);
2098 if (!esize)
2099 break;
2101 if ((qinfo->udata_len - qinfo->udata_offset) < esize) {
2102 QETH_CARD_TEXT_(card, 4, "qaer3%i", -ENOMEM);
2103 cmd->hdr.return_code = IPA_RC_ENOMEM;
2104 goto out_error;
2107 memcpy(qinfo->udata + qinfo->udata_offset,
2108 &qdata->data + entrybytes_done + stripped_bytes,
2109 esize);
2110 entrybytes_done += esize + stripped_bytes;
2111 qinfo->udata_offset += esize;
2112 ++qinfo->no_entries;
2114 /* check if all replies received ... */
2115 if (cmd->data.setassparms.hdr.seq_no <
2116 cmd->data.setassparms.hdr.number_of_replies)
2117 return 1;
2118 QETH_CARD_TEXT_(card, 4, "nove%i", qinfo->no_entries);
2119 memcpy(qinfo->udata, &qinfo->no_entries, 4);
2120 /* keep STRIP_ENTRIES flag so the user program can distinguish
2121 * stripped entries from normal ones */
2122 if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
2123 qdata->reply_bits |= QETH_QARP_STRIP_ENTRIES;
2124 memcpy(qinfo->udata + QETH_QARP_MASK_OFFSET, &qdata->reply_bits, 2);
2125 QETH_CARD_TEXT_(card, 4, "rc%i", 0);
2126 return 0;
2127 out_error:
2128 i = 0;
2129 memcpy(qinfo->udata, &i, 4);
2130 return 0;
2133 static int qeth_l3_send_ipa_arp_cmd(struct qeth_card *card,
2134 struct qeth_cmd_buffer *iob, int len,
2135 int (*reply_cb)(struct qeth_card *, struct qeth_reply *,
2136 unsigned long),
2137 void *reply_param)
2139 QETH_CARD_TEXT(card, 4, "sendarp");
2141 memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
2142 memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
2143 &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2144 return qeth_send_control_data(card, IPA_PDU_HEADER_SIZE + len, iob,
2145 reply_cb, reply_param);
2148 static int qeth_l3_query_arp_cache_info(struct qeth_card *card,
2149 enum qeth_prot_versions prot,
2150 struct qeth_arp_query_info *qinfo)
2152 struct qeth_cmd_buffer *iob;
2153 struct qeth_ipa_cmd *cmd;
2154 int tmp;
2155 int rc;
2157 QETH_CARD_TEXT_(card, 3, "qarpipv%i", prot);
2159 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
2160 IPA_CMD_ASS_ARP_QUERY_INFO,
2161 sizeof(struct qeth_arp_query_data)
2162 - sizeof(char),
2163 prot);
2164 if (!iob)
2165 return -ENOMEM;
2166 cmd = (struct qeth_ipa_cmd *)(iob->data+IPA_PDU_HEADER_SIZE);
2167 cmd->data.setassparms.data.query_arp.request_bits = 0x000F;
2168 cmd->data.setassparms.data.query_arp.reply_bits = 0;
2169 cmd->data.setassparms.data.query_arp.no_entries = 0;
2170 rc = qeth_l3_send_ipa_arp_cmd(card, iob,
2171 QETH_SETASS_BASE_LEN+QETH_ARP_CMD_LEN,
2172 qeth_l3_arp_query_cb, (void *)qinfo);
2173 if (rc) {
2174 tmp = rc;
2175 QETH_DBF_MESSAGE(2,
2176 "Error while querying ARP cache on %s: %s "
2177 "(0x%x/%d)\n", QETH_CARD_IFNAME(card),
2178 qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2181 return rc;
2184 static int qeth_l3_arp_query(struct qeth_card *card, char __user *udata)
2186 struct qeth_arp_query_info qinfo = {0, };
2187 int rc;
2189 QETH_CARD_TEXT(card, 3, "arpquery");
2191 if (!qeth_is_supported(card,/*IPA_QUERY_ARP_ADDR_INFO*/
2192 IPA_ARP_PROCESSING)) {
2193 QETH_CARD_TEXT(card, 3, "arpqnsup");
2194 rc = -EOPNOTSUPP;
2195 goto out;
2197 /* get size of userspace buffer and mask_bits -> 6 bytes */
2198 if (copy_from_user(&qinfo, udata, 6)) {
2199 rc = -EFAULT;
2200 goto out;
2202 qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
2203 if (!qinfo.udata) {
2204 rc = -ENOMEM;
2205 goto out;
2207 qinfo.udata_offset = QETH_QARP_ENTRIES_OFFSET;
2208 rc = qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV4, &qinfo);
2209 if (rc) {
2210 if (copy_to_user(udata, qinfo.udata, 4))
2211 rc = -EFAULT;
2212 goto free_and_out;
2214 #ifdef CONFIG_QETH_IPV6
2215 if (qinfo.mask_bits & QETH_QARP_WITH_IPV6) {
2216 /* fails in case of GuestLAN QDIO mode */
2217 qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV6, &qinfo);
2219 #endif
2220 if (copy_to_user(udata, qinfo.udata, qinfo.udata_len)) {
2221 QETH_CARD_TEXT(card, 4, "qactf");
2222 rc = -EFAULT;
2223 goto free_and_out;
2225 QETH_CARD_TEXT(card, 4, "qacts");
2227 free_and_out:
2228 kfree(qinfo.udata);
2229 out:
2230 return rc;
2233 static int qeth_l3_arp_add_entry(struct qeth_card *card,
2234 struct qeth_arp_cache_entry *entry)
2236 struct qeth_cmd_buffer *iob;
2237 char buf[16];
2238 int tmp;
2239 int rc;
2241 QETH_CARD_TEXT(card, 3, "arpadent");
2244 * currently GuestLAN only supports the ARP assist function
2245 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_ADD_ENTRY;
2246 * thus we say EOPNOTSUPP for this ARP function
2248 if (card->info.guestlan)
2249 return -EOPNOTSUPP;
2250 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
2251 return -EOPNOTSUPP;
2254 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
2255 IPA_CMD_ASS_ARP_ADD_ENTRY,
2256 sizeof(struct qeth_arp_cache_entry),
2257 QETH_PROT_IPV4);
2258 if (!iob)
2259 return -ENOMEM;
2260 rc = qeth_send_setassparms(card, iob,
2261 sizeof(struct qeth_arp_cache_entry),
2262 (unsigned long) entry,
2263 qeth_setassparms_cb, NULL);
2264 if (rc) {
2265 tmp = rc;
2266 qeth_l3_ipaddr4_to_string((u8 *)entry->ipaddr, buf);
2267 QETH_DBF_MESSAGE(2, "Could not add ARP entry for address %s "
2268 "on %s: %s (0x%x/%d)\n", buf, QETH_CARD_IFNAME(card),
2269 qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2271 return rc;
2274 static int qeth_l3_arp_remove_entry(struct qeth_card *card,
2275 struct qeth_arp_cache_entry *entry)
2277 struct qeth_cmd_buffer *iob;
2278 char buf[16] = {0, };
2279 int tmp;
2280 int rc;
2282 QETH_CARD_TEXT(card, 3, "arprment");
2285 * currently GuestLAN only supports the ARP assist function
2286 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_REMOVE_ENTRY;
2287 * thus we say EOPNOTSUPP for this ARP function
2289 if (card->info.guestlan)
2290 return -EOPNOTSUPP;
2291 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
2292 return -EOPNOTSUPP;
2294 memcpy(buf, entry, 12);
2295 iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
2296 IPA_CMD_ASS_ARP_REMOVE_ENTRY,
2298 QETH_PROT_IPV4);
2299 if (!iob)
2300 return -ENOMEM;
2301 rc = qeth_send_setassparms(card, iob,
2302 12, (unsigned long)buf,
2303 qeth_setassparms_cb, NULL);
2304 if (rc) {
2305 tmp = rc;
2306 memset(buf, 0, 16);
2307 qeth_l3_ipaddr4_to_string((u8 *)entry->ipaddr, buf);
2308 QETH_DBF_MESSAGE(2, "Could not delete ARP entry for address %s"
2309 " on %s: %s (0x%x/%d)\n", buf, QETH_CARD_IFNAME(card),
2310 qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2312 return rc;
2315 static int qeth_l3_arp_flush_cache(struct qeth_card *card)
2317 int rc;
2318 int tmp;
2320 QETH_CARD_TEXT(card, 3, "arpflush");
2323 * currently GuestLAN only supports the ARP assist function
2324 * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_FLUSH_CACHE;
2325 * thus we say EOPNOTSUPP for this ARP function
2327 if (card->info.guestlan || (card->info.type == QETH_CARD_TYPE_IQD))
2328 return -EOPNOTSUPP;
2329 if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
2330 return -EOPNOTSUPP;
2332 rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
2333 IPA_CMD_ASS_ARP_FLUSH_CACHE, 0);
2334 if (rc) {
2335 tmp = rc;
2336 QETH_DBF_MESSAGE(2, "Could not flush ARP cache on %s: %s "
2337 "(0x%x/%d)\n", QETH_CARD_IFNAME(card),
2338 qeth_l3_arp_get_error_cause(&rc), tmp, tmp);
2340 return rc;
2343 static int qeth_l3_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2345 struct qeth_card *card = dev->ml_priv;
2346 struct qeth_arp_cache_entry arp_entry;
2347 int rc = 0;
2349 switch (cmd) {
2350 case SIOC_QETH_ARP_SET_NO_ENTRIES:
2351 if (!capable(CAP_NET_ADMIN)) {
2352 rc = -EPERM;
2353 break;
2355 rc = qeth_l3_arp_set_no_entries(card, rq->ifr_ifru.ifru_ivalue);
2356 break;
2357 case SIOC_QETH_ARP_QUERY_INFO:
2358 if (!capable(CAP_NET_ADMIN)) {
2359 rc = -EPERM;
2360 break;
2362 rc = qeth_l3_arp_query(card, rq->ifr_ifru.ifru_data);
2363 break;
2364 case SIOC_QETH_ARP_ADD_ENTRY:
2365 if (!capable(CAP_NET_ADMIN)) {
2366 rc = -EPERM;
2367 break;
2369 if (copy_from_user(&arp_entry, rq->ifr_ifru.ifru_data,
2370 sizeof(struct qeth_arp_cache_entry)))
2371 rc = -EFAULT;
2372 else
2373 rc = qeth_l3_arp_add_entry(card, &arp_entry);
2374 break;
2375 case SIOC_QETH_ARP_REMOVE_ENTRY:
2376 if (!capable(CAP_NET_ADMIN)) {
2377 rc = -EPERM;
2378 break;
2380 if (copy_from_user(&arp_entry, rq->ifr_ifru.ifru_data,
2381 sizeof(struct qeth_arp_cache_entry)))
2382 rc = -EFAULT;
2383 else
2384 rc = qeth_l3_arp_remove_entry(card, &arp_entry);
2385 break;
2386 case SIOC_QETH_ARP_FLUSH_CACHE:
2387 if (!capable(CAP_NET_ADMIN)) {
2388 rc = -EPERM;
2389 break;
2391 rc = qeth_l3_arp_flush_cache(card);
2392 break;
2393 default:
2394 rc = -EOPNOTSUPP;
2396 return rc;
2399 static int qeth_l3_get_cast_type(struct qeth_card *card, struct sk_buff *skb)
2401 int cast_type = RTN_UNSPEC;
2402 struct neighbour *n = NULL;
2403 struct dst_entry *dst;
2405 rcu_read_lock();
2406 dst = skb_dst(skb);
2407 if (dst)
2408 n = dst_neigh_lookup_skb(dst, skb);
2409 if (n) {
2410 cast_type = n->type;
2411 rcu_read_unlock();
2412 neigh_release(n);
2413 if ((cast_type == RTN_BROADCAST) ||
2414 (cast_type == RTN_MULTICAST) ||
2415 (cast_type == RTN_ANYCAST))
2416 return cast_type;
2417 else
2418 return RTN_UNSPEC;
2420 rcu_read_unlock();
2422 /* try something else */
2423 if (be16_to_cpu(skb->protocol) == ETH_P_IPV6)
2424 return (skb_network_header(skb)[24] == 0xff) ?
2425 RTN_MULTICAST : 0;
2426 else if (be16_to_cpu(skb->protocol) == ETH_P_IP)
2427 return ((skb_network_header(skb)[16] & 0xf0) == 0xe0) ?
2428 RTN_MULTICAST : 0;
2429 /* ... */
2430 if (!memcmp(skb->data, skb->dev->broadcast, 6))
2431 return RTN_BROADCAST;
2432 else {
2433 u16 hdr_mac;
2435 hdr_mac = *((u16 *)skb->data);
2436 /* tr multicast? */
2437 switch (card->info.link_type) {
2438 case QETH_LINK_TYPE_HSTR:
2439 case QETH_LINK_TYPE_LANE_TR:
2440 if ((hdr_mac == QETH_TR_MAC_NC) ||
2441 (hdr_mac == QETH_TR_MAC_C))
2442 return RTN_MULTICAST;
2443 break;
2444 /* eth or so multicast? */
2445 default:
2446 if ((hdr_mac == QETH_ETH_MAC_V4) ||
2447 (hdr_mac == QETH_ETH_MAC_V6))
2448 return RTN_MULTICAST;
2451 return cast_type;
2454 static void qeth_l3_fill_af_iucv_hdr(struct qeth_card *card,
2455 struct qeth_hdr *hdr, struct sk_buff *skb)
2457 char daddr[16];
2458 struct af_iucv_trans_hdr *iucv_hdr;
2460 memset(hdr, 0, sizeof(struct qeth_hdr));
2461 hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
2462 hdr->hdr.l3.ext_flags = 0;
2463 hdr->hdr.l3.length = skb->len - ETH_HLEN;
2464 hdr->hdr.l3.flags = QETH_HDR_IPV6 | QETH_CAST_UNICAST;
2466 iucv_hdr = (struct af_iucv_trans_hdr *) (skb->data + ETH_HLEN);
2467 memset(daddr, 0, sizeof(daddr));
2468 daddr[0] = 0xfe;
2469 daddr[1] = 0x80;
2470 memcpy(&daddr[8], iucv_hdr->destUserID, 8);
2471 memcpy(hdr->hdr.l3.dest_addr, daddr, 16);
2474 static void qeth_l3_fill_header(struct qeth_card *card, struct qeth_hdr *hdr,
2475 struct sk_buff *skb, int ipv, int cast_type)
2477 struct dst_entry *dst;
2479 memset(hdr, 0, sizeof(struct qeth_hdr));
2480 hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
2481 hdr->hdr.l3.ext_flags = 0;
2484 * before we're going to overwrite this location with next hop ip.
2485 * v6 uses passthrough, v4 sets the tag in the QDIO header.
2487 if (skb_vlan_tag_present(skb)) {
2488 if ((ipv == 4) || (card->info.type == QETH_CARD_TYPE_IQD))
2489 hdr->hdr.l3.ext_flags = QETH_HDR_EXT_VLAN_FRAME;
2490 else
2491 hdr->hdr.l3.ext_flags = QETH_HDR_EXT_INCLUDE_VLAN_TAG;
2492 hdr->hdr.l3.vlan_id = skb_vlan_tag_get(skb);
2495 hdr->hdr.l3.length = skb->len - sizeof(struct qeth_hdr);
2497 rcu_read_lock();
2498 dst = skb_dst(skb);
2499 if (ipv == 4) {
2500 struct rtable *rt = (struct rtable *) dst;
2501 __be32 *pkey = &ip_hdr(skb)->daddr;
2503 if (rt && rt->rt_gateway)
2504 pkey = &rt->rt_gateway;
2506 /* IPv4 */
2507 hdr->hdr.l3.flags = qeth_l3_get_qeth_hdr_flags4(cast_type);
2508 memset(hdr->hdr.l3.dest_addr, 0, 12);
2509 *((__be32 *) (&hdr->hdr.l3.dest_addr[12])) = *pkey;
2510 } else if (ipv == 6) {
2511 struct rt6_info *rt = (struct rt6_info *) dst;
2512 struct in6_addr *pkey = &ipv6_hdr(skb)->daddr;
2514 if (rt && !ipv6_addr_any(&rt->rt6i_gateway))
2515 pkey = &rt->rt6i_gateway;
2517 /* IPv6 */
2518 hdr->hdr.l3.flags = qeth_l3_get_qeth_hdr_flags6(cast_type);
2519 if (card->info.type == QETH_CARD_TYPE_IQD)
2520 hdr->hdr.l3.flags &= ~QETH_HDR_PASSTHRU;
2521 memcpy(hdr->hdr.l3.dest_addr, pkey, 16);
2522 } else {
2523 if (!memcmp(skb->data + sizeof(struct qeth_hdr),
2524 skb->dev->broadcast, 6)) {
2525 /* broadcast? */
2526 hdr->hdr.l3.flags = QETH_CAST_BROADCAST |
2527 QETH_HDR_PASSTHRU;
2528 } else {
2529 hdr->hdr.l3.flags = (cast_type == RTN_MULTICAST) ?
2530 QETH_CAST_MULTICAST | QETH_HDR_PASSTHRU :
2531 QETH_CAST_UNICAST | QETH_HDR_PASSTHRU;
2534 rcu_read_unlock();
2537 static void qeth_l3_hdr_csum(struct qeth_card *card, struct qeth_hdr *hdr,
2538 struct sk_buff *skb)
2540 struct iphdr *iph = ip_hdr(skb);
2542 /* tcph->check contains already the pseudo hdr checksum
2543 * so just set the header flags
2545 if (iph->protocol == IPPROTO_UDP)
2546 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_UDP;
2547 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_CSUM_TRANSP_REQ |
2548 QETH_HDR_EXT_CSUM_HDR_REQ;
2549 iph->check = 0;
2550 if (card->options.performance_stats)
2551 card->perf_stats.tx_csum++;
2554 static void qeth_tso_fill_header(struct qeth_card *card,
2555 struct qeth_hdr *qhdr, struct sk_buff *skb)
2557 struct qeth_hdr_tso *hdr = (struct qeth_hdr_tso *)qhdr;
2558 struct tcphdr *tcph = tcp_hdr(skb);
2559 struct iphdr *iph = ip_hdr(skb);
2560 struct ipv6hdr *ip6h = ipv6_hdr(skb);
2562 /*fix header to TSO values ...*/
2563 hdr->hdr.hdr.l3.id = QETH_HEADER_TYPE_TSO;
2564 hdr->hdr.hdr.l3.length = skb->len - sizeof(struct qeth_hdr_tso);
2565 /*set values which are fix for the first approach ...*/
2566 hdr->ext.hdr_tot_len = (__u16) sizeof(struct qeth_hdr_ext_tso);
2567 hdr->ext.imb_hdr_no = 1;
2568 hdr->ext.hdr_type = 1;
2569 hdr->ext.hdr_version = 1;
2570 hdr->ext.hdr_len = 28;
2571 /*insert non-fix values */
2572 hdr->ext.mss = skb_shinfo(skb)->gso_size;
2573 hdr->ext.dg_hdr_len = (__u16)(ip_hdrlen(skb) + tcp_hdrlen(skb));
2574 hdr->ext.payload_len = (__u16)(skb->len - hdr->ext.dg_hdr_len -
2575 sizeof(struct qeth_hdr_tso));
2576 tcph->check = 0;
2577 if (be16_to_cpu(skb->protocol) == ETH_P_IPV6) {
2578 ip6h->payload_len = 0;
2579 tcph->check = ~csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
2580 0, IPPROTO_TCP, 0);
2581 } else {
2582 /*OSA want us to set these values ...*/
2583 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
2584 0, IPPROTO_TCP, 0);
2585 iph->tot_len = 0;
2586 iph->check = 0;
2591 * qeth_l3_get_elements_no_tso() - find number of SBALEs for skb data for tso
2592 * @card: qeth card structure, to check max. elems.
2593 * @skb: SKB address
2594 * @extra_elems: extra elems needed, to check against max.
2596 * Returns the number of pages, and thus QDIO buffer elements, needed to cover
2597 * skb data, including linear part and fragments, but excluding TCP header.
2598 * (Exclusion of TCP header distinguishes it from qeth_get_elements_no().)
2599 * Checks if the result plus extra_elems fits under the limit for the card.
2600 * Returns 0 if it does not.
2601 * Note: extra_elems is not included in the returned result.
2603 static int qeth_l3_get_elements_no_tso(struct qeth_card *card,
2604 struct sk_buff *skb, int extra_elems)
2606 addr_t tcpdptr = (addr_t)tcp_hdr(skb) + tcp_hdrlen(skb);
2607 int elements = qeth_get_elements_for_range(
2608 tcpdptr,
2609 (addr_t)skb->data + skb_headlen(skb)) +
2610 qeth_get_elements_for_frags(skb);
2612 if ((elements + extra_elems) > QETH_MAX_BUFFER_ELEMENTS(card)) {
2613 QETH_DBF_MESSAGE(2,
2614 "Invalid size of TSO IP packet (Number=%d / Length=%d). Discarded.\n",
2615 elements + extra_elems, skb->len);
2616 return 0;
2618 return elements;
2621 static netdev_tx_t qeth_l3_hard_start_xmit(struct sk_buff *skb,
2622 struct net_device *dev)
2624 int rc;
2625 __be16 *tag;
2626 struct qeth_hdr *hdr = NULL;
2627 int hdr_elements = 0;
2628 int elements;
2629 struct qeth_card *card = dev->ml_priv;
2630 struct sk_buff *new_skb = NULL;
2631 int ipv = qeth_get_ip_version(skb);
2632 int cast_type = qeth_l3_get_cast_type(card, skb);
2633 struct qeth_qdio_out_q *queue =
2634 card->qdio.out_qs[card->qdio.do_prio_queueing
2635 || (cast_type && card->info.is_multicast_different) ?
2636 qeth_get_priority_queue(card, skb, ipv, cast_type) :
2637 card->qdio.default_out_queue];
2638 int tx_bytes = skb->len;
2639 unsigned int hd_len = 0;
2640 bool use_tso;
2641 int data_offset = -1;
2642 unsigned int nr_frags;
2644 if (((card->info.type == QETH_CARD_TYPE_IQD) &&
2645 (((card->options.cq != QETH_CQ_ENABLED) && !ipv) ||
2646 ((card->options.cq == QETH_CQ_ENABLED) &&
2647 (be16_to_cpu(skb->protocol) != ETH_P_AF_IUCV)))) ||
2648 card->options.sniffer)
2649 goto tx_drop;
2651 if ((card->state != CARD_STATE_UP) || !card->lan_online) {
2652 card->stats.tx_carrier_errors++;
2653 goto tx_drop;
2656 if ((cast_type == RTN_BROADCAST) &&
2657 (card->info.broadcast_capable == 0))
2658 goto tx_drop;
2660 if (card->options.performance_stats) {
2661 card->perf_stats.outbound_cnt++;
2662 card->perf_stats.outbound_start_time = qeth_get_micros();
2665 /* Ignore segment size from skb_is_gso(), 1 page is always used. */
2666 use_tso = skb_is_gso(skb) &&
2667 (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4);
2669 if (card->info.type == QETH_CARD_TYPE_IQD) {
2670 new_skb = skb;
2671 data_offset = ETH_HLEN;
2672 hd_len = sizeof(*hdr);
2673 hdr = kmem_cache_alloc(qeth_core_header_cache, GFP_ATOMIC);
2674 if (!hdr)
2675 goto tx_drop;
2676 hdr_elements++;
2677 } else {
2678 /* create a clone with writeable headroom */
2679 new_skb = skb_realloc_headroom(skb, sizeof(struct qeth_hdr_tso)
2680 + VLAN_HLEN);
2681 if (!new_skb)
2682 goto tx_drop;
2684 if (ipv == 4) {
2685 skb_pull(new_skb, ETH_HLEN);
2688 if (ipv != 4 && skb_vlan_tag_present(new_skb)) {
2689 skb_push(new_skb, VLAN_HLEN);
2690 skb_copy_to_linear_data(new_skb, new_skb->data + 4, 4);
2691 skb_copy_to_linear_data_offset(new_skb, 4,
2692 new_skb->data + 8, 4);
2693 skb_copy_to_linear_data_offset(new_skb, 8,
2694 new_skb->data + 12, 4);
2695 tag = (__be16 *)(new_skb->data + 12);
2696 *tag = cpu_to_be16(ETH_P_8021Q);
2697 *(tag + 1) = cpu_to_be16(skb_vlan_tag_get(new_skb));
2701 netif_stop_queue(dev);
2703 /* fix hardware limitation: as long as we do not have sbal
2704 * chaining we can not send long frag lists
2706 if ((card->info.type != QETH_CARD_TYPE_IQD) &&
2707 ((use_tso && !qeth_l3_get_elements_no_tso(card, new_skb, 1)) ||
2708 (!use_tso && !qeth_get_elements_no(card, new_skb, 0, 0)))) {
2709 int lin_rc = skb_linearize(new_skb);
2711 if (card->options.performance_stats) {
2712 if (lin_rc)
2713 card->perf_stats.tx_linfail++;
2714 else
2715 card->perf_stats.tx_lin++;
2717 if (lin_rc)
2718 goto tx_drop;
2720 nr_frags = skb_shinfo(new_skb)->nr_frags;
2722 if (use_tso) {
2723 hdr = skb_push(new_skb, sizeof(struct qeth_hdr_tso));
2724 memset(hdr, 0, sizeof(struct qeth_hdr_tso));
2725 qeth_l3_fill_header(card, hdr, new_skb, ipv, cast_type);
2726 qeth_tso_fill_header(card, hdr, new_skb);
2727 hdr_elements++;
2728 } else {
2729 if (data_offset < 0) {
2730 hdr = skb_push(new_skb, sizeof(struct qeth_hdr));
2731 qeth_l3_fill_header(card, hdr, new_skb, ipv,
2732 cast_type);
2733 } else {
2734 if (be16_to_cpu(new_skb->protocol) == ETH_P_AF_IUCV)
2735 qeth_l3_fill_af_iucv_hdr(card, hdr, new_skb);
2736 else {
2737 qeth_l3_fill_header(card, hdr, new_skb, ipv,
2738 cast_type);
2739 hdr->hdr.l3.length = new_skb->len - data_offset;
2743 if (skb->ip_summed == CHECKSUM_PARTIAL)
2744 qeth_l3_hdr_csum(card, hdr, new_skb);
2747 elements = use_tso ?
2748 qeth_l3_get_elements_no_tso(card, new_skb, hdr_elements) :
2749 qeth_get_elements_no(card, new_skb, hdr_elements,
2750 (data_offset > 0) ? data_offset : 0);
2751 if (!elements) {
2752 if (data_offset >= 0)
2753 kmem_cache_free(qeth_core_header_cache, hdr);
2754 goto tx_drop;
2756 elements += hdr_elements;
2758 if (card->info.type != QETH_CARD_TYPE_IQD) {
2759 int len;
2760 if (use_tso) {
2761 hd_len = sizeof(struct qeth_hdr_tso) +
2762 ip_hdrlen(new_skb) + tcp_hdrlen(new_skb);
2763 len = hd_len;
2764 } else {
2765 len = sizeof(struct qeth_hdr_layer3);
2768 if (qeth_hdr_chk_and_bounce(new_skb, &hdr, len))
2769 goto tx_drop;
2770 rc = qeth_do_send_packet(card, queue, new_skb, hdr, hd_len,
2771 hd_len, elements);
2772 } else
2773 rc = qeth_do_send_packet_fast(queue, new_skb, hdr, data_offset,
2774 hd_len);
2776 if (!rc) {
2777 card->stats.tx_packets++;
2778 card->stats.tx_bytes += tx_bytes;
2779 if (new_skb != skb)
2780 dev_kfree_skb_any(skb);
2781 if (card->options.performance_stats) {
2782 if (use_tso) {
2783 card->perf_stats.large_send_bytes += tx_bytes;
2784 card->perf_stats.large_send_cnt++;
2786 if (nr_frags) {
2787 card->perf_stats.sg_skbs_sent++;
2788 /* nr_frags + skb->data */
2789 card->perf_stats.sg_frags_sent += nr_frags + 1;
2792 rc = NETDEV_TX_OK;
2793 } else {
2794 if (data_offset >= 0)
2795 kmem_cache_free(qeth_core_header_cache, hdr);
2797 if (rc == -EBUSY) {
2798 if (new_skb != skb)
2799 dev_kfree_skb_any(new_skb);
2800 return NETDEV_TX_BUSY;
2801 } else
2802 goto tx_drop;
2805 netif_wake_queue(dev);
2806 if (card->options.performance_stats)
2807 card->perf_stats.outbound_time += qeth_get_micros() -
2808 card->perf_stats.outbound_start_time;
2809 return rc;
2811 tx_drop:
2812 card->stats.tx_dropped++;
2813 card->stats.tx_errors++;
2814 if ((new_skb != skb) && new_skb)
2815 dev_kfree_skb_any(new_skb);
2816 dev_kfree_skb_any(skb);
2817 netif_wake_queue(dev);
2818 return NETDEV_TX_OK;
2821 static int __qeth_l3_open(struct net_device *dev)
2823 struct qeth_card *card = dev->ml_priv;
2824 int rc = 0;
2826 QETH_CARD_TEXT(card, 4, "qethopen");
2827 if (card->state == CARD_STATE_UP)
2828 return rc;
2829 if (card->state != CARD_STATE_SOFTSETUP)
2830 return -ENODEV;
2831 card->data.state = CH_STATE_UP;
2832 card->state = CARD_STATE_UP;
2833 netif_start_queue(dev);
2835 if (qdio_stop_irq(card->data.ccwdev, 0) >= 0) {
2836 napi_enable(&card->napi);
2837 napi_schedule(&card->napi);
2838 } else
2839 rc = -EIO;
2840 return rc;
2843 static int qeth_l3_open(struct net_device *dev)
2845 struct qeth_card *card = dev->ml_priv;
2847 QETH_CARD_TEXT(card, 5, "qethope_");
2848 if (qeth_wait_for_threads(card, QETH_RECOVER_THREAD)) {
2849 QETH_CARD_TEXT(card, 3, "openREC");
2850 return -ERESTARTSYS;
2852 return __qeth_l3_open(dev);
2855 static int qeth_l3_stop(struct net_device *dev)
2857 struct qeth_card *card = dev->ml_priv;
2859 QETH_CARD_TEXT(card, 4, "qethstop");
2860 netif_tx_disable(dev);
2861 if (card->state == CARD_STATE_UP) {
2862 card->state = CARD_STATE_SOFTSETUP;
2863 napi_disable(&card->napi);
2865 return 0;
2868 static const struct ethtool_ops qeth_l3_ethtool_ops = {
2869 .get_link = ethtool_op_get_link,
2870 .get_strings = qeth_core_get_strings,
2871 .get_ethtool_stats = qeth_core_get_ethtool_stats,
2872 .get_sset_count = qeth_core_get_sset_count,
2873 .get_drvinfo = qeth_core_get_drvinfo,
2874 .get_link_ksettings = qeth_core_ethtool_get_link_ksettings,
2878 * we need NOARP for IPv4 but we want neighbor solicitation for IPv6. Setting
2879 * NOARP on the netdevice is no option because it also turns off neighbor
2880 * solicitation. For IPv4 we install a neighbor_setup function. We don't want
2881 * arp resolution but we want the hard header (packet socket will work
2882 * e.g. tcpdump)
2884 static int qeth_l3_neigh_setup_noarp(struct neighbour *n)
2886 n->nud_state = NUD_NOARP;
2887 memcpy(n->ha, "FAKELL", 6);
2888 n->output = n->ops->connected_output;
2889 return 0;
2892 static int
2893 qeth_l3_neigh_setup(struct net_device *dev, struct neigh_parms *np)
2895 if (np->tbl->family == AF_INET)
2896 np->neigh_setup = qeth_l3_neigh_setup_noarp;
2898 return 0;
2901 static const struct net_device_ops qeth_l3_netdev_ops = {
2902 .ndo_open = qeth_l3_open,
2903 .ndo_stop = qeth_l3_stop,
2904 .ndo_get_stats = qeth_get_stats,
2905 .ndo_start_xmit = qeth_l3_hard_start_xmit,
2906 .ndo_validate_addr = eth_validate_addr,
2907 .ndo_set_rx_mode = qeth_l3_set_multicast_list,
2908 .ndo_do_ioctl = qeth_do_ioctl,
2909 .ndo_change_mtu = qeth_change_mtu,
2910 .ndo_fix_features = qeth_fix_features,
2911 .ndo_set_features = qeth_set_features,
2912 .ndo_vlan_rx_add_vid = qeth_l3_vlan_rx_add_vid,
2913 .ndo_vlan_rx_kill_vid = qeth_l3_vlan_rx_kill_vid,
2914 .ndo_tx_timeout = qeth_tx_timeout,
2917 static const struct net_device_ops qeth_l3_osa_netdev_ops = {
2918 .ndo_open = qeth_l3_open,
2919 .ndo_stop = qeth_l3_stop,
2920 .ndo_get_stats = qeth_get_stats,
2921 .ndo_start_xmit = qeth_l3_hard_start_xmit,
2922 .ndo_features_check = qeth_features_check,
2923 .ndo_validate_addr = eth_validate_addr,
2924 .ndo_set_rx_mode = qeth_l3_set_multicast_list,
2925 .ndo_do_ioctl = qeth_do_ioctl,
2926 .ndo_change_mtu = qeth_change_mtu,
2927 .ndo_fix_features = qeth_fix_features,
2928 .ndo_set_features = qeth_set_features,
2929 .ndo_vlan_rx_add_vid = qeth_l3_vlan_rx_add_vid,
2930 .ndo_vlan_rx_kill_vid = qeth_l3_vlan_rx_kill_vid,
2931 .ndo_tx_timeout = qeth_tx_timeout,
2932 .ndo_neigh_setup = qeth_l3_neigh_setup,
2935 static int qeth_l3_setup_netdev(struct qeth_card *card)
2937 int rc;
2939 if (card->info.type == QETH_CARD_TYPE_OSD ||
2940 card->info.type == QETH_CARD_TYPE_OSX) {
2941 if ((card->info.link_type == QETH_LINK_TYPE_LANE_TR) ||
2942 (card->info.link_type == QETH_LINK_TYPE_HSTR)) {
2943 pr_info("qeth_l3: ignoring TR device\n");
2944 return -ENODEV;
2945 } else {
2946 card->dev = alloc_etherdev(0);
2947 if (!card->dev)
2948 return -ENODEV;
2949 card->dev->netdev_ops = &qeth_l3_osa_netdev_ops;
2951 /*IPv6 address autoconfiguration stuff*/
2952 qeth_l3_get_unique_id(card);
2953 if (!(card->info.unique_id & UNIQUE_ID_NOT_BY_CARD))
2954 card->dev->dev_id = card->info.unique_id &
2955 0xffff;
2956 if (!card->info.guestlan) {
2957 card->dev->hw_features = NETIF_F_SG |
2958 NETIF_F_RXCSUM | NETIF_F_IP_CSUM |
2959 NETIF_F_TSO;
2960 card->dev->vlan_features = NETIF_F_SG |
2961 NETIF_F_RXCSUM | NETIF_F_IP_CSUM |
2962 NETIF_F_TSO;
2963 card->dev->features |= NETIF_F_SG;
2966 } else if (card->info.type == QETH_CARD_TYPE_IQD) {
2967 card->dev = alloc_netdev(0, "hsi%d", NET_NAME_UNKNOWN,
2968 ether_setup);
2969 if (!card->dev)
2970 return -ENODEV;
2971 card->dev->flags |= IFF_NOARP;
2972 card->dev->netdev_ops = &qeth_l3_netdev_ops;
2973 rc = qeth_l3_iqd_read_initial_mac(card);
2974 if (rc)
2975 return rc;
2976 if (card->options.hsuid[0])
2977 memcpy(card->dev->perm_addr, card->options.hsuid, 9);
2978 } else
2979 return -ENODEV;
2981 card->dev->ml_priv = card;
2982 card->dev->watchdog_timeo = QETH_TX_TIMEOUT;
2983 card->dev->mtu = card->info.initial_mtu;
2984 card->dev->min_mtu = 64;
2985 card->dev->max_mtu = ETH_MAX_MTU;
2986 card->dev->ethtool_ops = &qeth_l3_ethtool_ops;
2987 card->dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2988 NETIF_F_HW_VLAN_CTAG_RX |
2989 NETIF_F_HW_VLAN_CTAG_FILTER;
2990 netif_keep_dst(card->dev);
2991 netif_set_gso_max_size(card->dev, (QETH_MAX_BUFFER_ELEMENTS(card) - 1) *
2992 PAGE_SIZE);
2994 SET_NETDEV_DEV(card->dev, &card->gdev->dev);
2995 netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
2996 netif_carrier_off(card->dev);
2997 return register_netdev(card->dev);
3000 static const struct device_type qeth_l3_devtype = {
3001 .name = "qeth_layer3",
3002 .groups = qeth_l3_attr_groups,
3005 static int qeth_l3_probe_device(struct ccwgroup_device *gdev)
3007 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
3008 int rc;
3010 if (gdev->dev.type == &qeth_generic_devtype) {
3011 rc = qeth_l3_create_device_attributes(&gdev->dev);
3012 if (rc)
3013 return rc;
3015 hash_init(card->ip_htable);
3016 hash_init(card->ip_mc_htable);
3017 card->options.layer2 = 0;
3018 card->info.hwtrap = 0;
3019 return 0;
3022 static void qeth_l3_remove_device(struct ccwgroup_device *cgdev)
3024 struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
3026 if (cgdev->dev.type == &qeth_generic_devtype)
3027 qeth_l3_remove_device_attributes(&cgdev->dev);
3029 qeth_set_allowed_threads(card, 0, 1);
3030 wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
3032 if (cgdev->state == CCWGROUP_ONLINE)
3033 qeth_l3_set_offline(cgdev);
3035 if (card->dev) {
3036 netif_napi_del(&card->napi);
3037 unregister_netdev(card->dev);
3038 card->dev = NULL;
3041 qeth_l3_clear_ip_htable(card, 0);
3042 qeth_l3_clear_ipato_list(card);
3043 return;
3046 static int __qeth_l3_set_online(struct ccwgroup_device *gdev, int recovery_mode)
3048 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
3049 int rc = 0;
3050 enum qeth_card_states recover_flag;
3052 mutex_lock(&card->discipline_mutex);
3053 mutex_lock(&card->conf_mutex);
3054 QETH_DBF_TEXT(SETUP, 2, "setonlin");
3055 QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
3057 recover_flag = card->state;
3058 rc = qeth_core_hardsetup_card(card);
3059 if (rc) {
3060 QETH_DBF_TEXT_(SETUP, 2, "2err%04x", rc);
3061 rc = -ENODEV;
3062 goto out_remove;
3065 if (!card->dev && qeth_l3_setup_netdev(card)) {
3066 rc = -ENODEV;
3067 goto out_remove;
3070 if (qeth_is_diagass_supported(card, QETH_DIAGS_CMD_TRAP)) {
3071 if (card->info.hwtrap &&
3072 qeth_hw_trap(card, QETH_DIAGS_TRAP_ARM))
3073 card->info.hwtrap = 0;
3074 } else
3075 card->info.hwtrap = 0;
3077 card->state = CARD_STATE_HARDSETUP;
3078 memset(&card->rx, 0, sizeof(struct qeth_rx));
3079 qeth_print_status_message(card);
3081 /* softsetup */
3082 QETH_DBF_TEXT(SETUP, 2, "softsetp");
3084 rc = qeth_l3_setadapter_parms(card);
3085 if (rc)
3086 QETH_DBF_TEXT_(SETUP, 2, "2err%04x", rc);
3087 if (!card->options.sniffer) {
3088 rc = qeth_l3_start_ipassists(card);
3089 if (rc) {
3090 QETH_DBF_TEXT_(SETUP, 2, "3err%d", rc);
3091 goto out_remove;
3093 rc = qeth_l3_setrouting_v4(card);
3094 if (rc)
3095 QETH_DBF_TEXT_(SETUP, 2, "4err%04x", rc);
3096 rc = qeth_l3_setrouting_v6(card);
3097 if (rc)
3098 QETH_DBF_TEXT_(SETUP, 2, "5err%04x", rc);
3100 netif_tx_disable(card->dev);
3102 rc = qeth_init_qdio_queues(card);
3103 if (rc) {
3104 QETH_DBF_TEXT_(SETUP, 2, "6err%d", rc);
3105 rc = -ENODEV;
3106 goto out_remove;
3108 card->state = CARD_STATE_SOFTSETUP;
3110 qeth_set_allowed_threads(card, 0xffffffff, 0);
3111 qeth_l3_recover_ip(card);
3112 if (card->lan_online)
3113 netif_carrier_on(card->dev);
3114 else
3115 netif_carrier_off(card->dev);
3116 if (recover_flag == CARD_STATE_RECOVER) {
3117 rtnl_lock();
3118 if (recovery_mode)
3119 __qeth_l3_open(card->dev);
3120 else
3121 dev_open(card->dev);
3122 qeth_l3_set_multicast_list(card->dev);
3123 qeth_recover_features(card->dev);
3124 rtnl_unlock();
3126 qeth_trace_features(card);
3127 /* let user_space know that device is online */
3128 kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
3129 mutex_unlock(&card->conf_mutex);
3130 mutex_unlock(&card->discipline_mutex);
3131 return 0;
3132 out_remove:
3133 qeth_l3_stop_card(card, 0);
3134 ccw_device_set_offline(CARD_DDEV(card));
3135 ccw_device_set_offline(CARD_WDEV(card));
3136 ccw_device_set_offline(CARD_RDEV(card));
3137 qdio_free(CARD_DDEV(card));
3138 if (recover_flag == CARD_STATE_RECOVER)
3139 card->state = CARD_STATE_RECOVER;
3140 else
3141 card->state = CARD_STATE_DOWN;
3142 mutex_unlock(&card->conf_mutex);
3143 mutex_unlock(&card->discipline_mutex);
3144 return rc;
3147 static int qeth_l3_set_online(struct ccwgroup_device *gdev)
3149 return __qeth_l3_set_online(gdev, 0);
3152 static int __qeth_l3_set_offline(struct ccwgroup_device *cgdev,
3153 int recovery_mode)
3155 struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
3156 int rc = 0, rc2 = 0, rc3 = 0;
3157 enum qeth_card_states recover_flag;
3159 mutex_lock(&card->discipline_mutex);
3160 mutex_lock(&card->conf_mutex);
3161 QETH_DBF_TEXT(SETUP, 3, "setoffl");
3162 QETH_DBF_HEX(SETUP, 3, &card, sizeof(void *));
3164 if (card->dev && netif_carrier_ok(card->dev))
3165 netif_carrier_off(card->dev);
3166 recover_flag = card->state;
3167 if ((!recovery_mode && card->info.hwtrap) || card->info.hwtrap == 2) {
3168 qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
3169 card->info.hwtrap = 1;
3171 qeth_l3_stop_card(card, recovery_mode);
3172 if ((card->options.cq == QETH_CQ_ENABLED) && card->dev) {
3173 rtnl_lock();
3174 call_netdevice_notifiers(NETDEV_REBOOT, card->dev);
3175 rtnl_unlock();
3177 rc = ccw_device_set_offline(CARD_DDEV(card));
3178 rc2 = ccw_device_set_offline(CARD_WDEV(card));
3179 rc3 = ccw_device_set_offline(CARD_RDEV(card));
3180 if (!rc)
3181 rc = (rc2) ? rc2 : rc3;
3182 if (rc)
3183 QETH_DBF_TEXT_(SETUP, 2, "1err%d", rc);
3184 qdio_free(CARD_DDEV(card));
3185 if (recover_flag == CARD_STATE_UP)
3186 card->state = CARD_STATE_RECOVER;
3187 /* let user_space know that device is offline */
3188 kobject_uevent(&cgdev->dev.kobj, KOBJ_CHANGE);
3189 mutex_unlock(&card->conf_mutex);
3190 mutex_unlock(&card->discipline_mutex);
3191 return 0;
3194 static int qeth_l3_set_offline(struct ccwgroup_device *cgdev)
3196 return __qeth_l3_set_offline(cgdev, 0);
3199 static int qeth_l3_recover(void *ptr)
3201 struct qeth_card *card;
3202 int rc = 0;
3204 card = (struct qeth_card *) ptr;
3205 QETH_CARD_TEXT(card, 2, "recover1");
3206 QETH_CARD_HEX(card, 2, &card, sizeof(void *));
3207 if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
3208 return 0;
3209 QETH_CARD_TEXT(card, 2, "recover2");
3210 dev_warn(&card->gdev->dev,
3211 "A recovery process has been started for the device\n");
3212 qeth_set_recovery_task(card);
3213 __qeth_l3_set_offline(card->gdev, 1);
3214 rc = __qeth_l3_set_online(card->gdev, 1);
3215 if (!rc)
3216 dev_info(&card->gdev->dev,
3217 "Device successfully recovered!\n");
3218 else {
3219 qeth_close_dev(card);
3220 dev_warn(&card->gdev->dev, "The qeth device driver "
3221 "failed to recover an error on the device\n");
3223 qeth_clear_recovery_task(card);
3224 qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
3225 qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
3226 return 0;
3229 static int qeth_l3_pm_suspend(struct ccwgroup_device *gdev)
3231 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
3233 if (card->dev)
3234 netif_device_detach(card->dev);
3235 qeth_set_allowed_threads(card, 0, 1);
3236 wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
3237 if (gdev->state == CCWGROUP_OFFLINE)
3238 return 0;
3239 if (card->state == CARD_STATE_UP) {
3240 if (card->info.hwtrap)
3241 qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
3242 __qeth_l3_set_offline(card->gdev, 1);
3243 } else
3244 __qeth_l3_set_offline(card->gdev, 0);
3245 return 0;
3248 static int qeth_l3_pm_resume(struct ccwgroup_device *gdev)
3250 struct qeth_card *card = dev_get_drvdata(&gdev->dev);
3251 int rc = 0;
3253 if (gdev->state == CCWGROUP_OFFLINE)
3254 goto out;
3256 if (card->state == CARD_STATE_RECOVER) {
3257 rc = __qeth_l3_set_online(card->gdev, 1);
3258 if (rc) {
3259 rtnl_lock();
3260 dev_close(card->dev);
3261 rtnl_unlock();
3263 } else
3264 rc = __qeth_l3_set_online(card->gdev, 0);
3265 out:
3266 qeth_set_allowed_threads(card, 0xffffffff, 0);
3267 if (card->dev)
3268 netif_device_attach(card->dev);
3269 if (rc)
3270 dev_warn(&card->gdev->dev, "The qeth device driver "
3271 "failed to recover an error on the device\n");
3272 return rc;
3275 /* Returns zero if the command is successfully "consumed" */
3276 static int qeth_l3_control_event(struct qeth_card *card,
3277 struct qeth_ipa_cmd *cmd)
3279 return 1;
3282 struct qeth_discipline qeth_l3_discipline = {
3283 .devtype = &qeth_l3_devtype,
3284 .start_poll = qeth_qdio_start_poll,
3285 .input_handler = (qdio_handler_t *) qeth_qdio_input_handler,
3286 .output_handler = (qdio_handler_t *) qeth_qdio_output_handler,
3287 .process_rx_buffer = qeth_l3_process_inbound_buffer,
3288 .recover = qeth_l3_recover,
3289 .setup = qeth_l3_probe_device,
3290 .remove = qeth_l3_remove_device,
3291 .set_online = qeth_l3_set_online,
3292 .set_offline = qeth_l3_set_offline,
3293 .freeze = qeth_l3_pm_suspend,
3294 .thaw = qeth_l3_pm_resume,
3295 .restore = qeth_l3_pm_resume,
3296 .do_ioctl = qeth_l3_do_ioctl,
3297 .control_event_handler = qeth_l3_control_event,
3299 EXPORT_SYMBOL_GPL(qeth_l3_discipline);
3301 static int qeth_l3_ip_event(struct notifier_block *this,
3302 unsigned long event, void *ptr)
3305 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
3306 struct net_device *dev = (struct net_device *)ifa->ifa_dev->dev;
3307 struct qeth_ipaddr *addr;
3308 struct qeth_card *card;
3310 if (dev_net(dev) != &init_net)
3311 return NOTIFY_DONE;
3313 card = qeth_l3_get_card_from_dev(dev);
3314 if (!card)
3315 return NOTIFY_DONE;
3316 QETH_CARD_TEXT(card, 3, "ipevent");
3318 addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
3319 if (addr) {
3320 addr->u.a4.addr = be32_to_cpu(ifa->ifa_address);
3321 addr->u.a4.mask = be32_to_cpu(ifa->ifa_mask);
3322 addr->type = QETH_IP_TYPE_NORMAL;
3323 } else
3324 return NOTIFY_DONE;
3326 switch (event) {
3327 case NETDEV_UP:
3328 spin_lock_bh(&card->ip_lock);
3329 qeth_l3_add_ip(card, addr);
3330 spin_unlock_bh(&card->ip_lock);
3331 break;
3332 case NETDEV_DOWN:
3333 spin_lock_bh(&card->ip_lock);
3334 qeth_l3_delete_ip(card, addr);
3335 spin_unlock_bh(&card->ip_lock);
3336 break;
3339 kfree(addr);
3340 return NOTIFY_DONE;
3343 static struct notifier_block qeth_l3_ip_notifier = {
3344 qeth_l3_ip_event,
3345 NULL,
3348 #ifdef CONFIG_QETH_IPV6
3350 * IPv6 event handler
3352 static int qeth_l3_ip6_event(struct notifier_block *this,
3353 unsigned long event, void *ptr)
3355 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
3356 struct net_device *dev = (struct net_device *)ifa->idev->dev;
3357 struct qeth_ipaddr *addr;
3358 struct qeth_card *card;
3360 card = qeth_l3_get_card_from_dev(dev);
3361 if (!card)
3362 return NOTIFY_DONE;
3363 QETH_CARD_TEXT(card, 3, "ip6event");
3364 if (!qeth_is_supported(card, IPA_IPV6))
3365 return NOTIFY_DONE;
3367 addr = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
3368 if (addr) {
3369 memcpy(&addr->u.a6.addr, &ifa->addr, sizeof(struct in6_addr));
3370 addr->u.a6.pfxlen = ifa->prefix_len;
3371 addr->type = QETH_IP_TYPE_NORMAL;
3372 } else
3373 return NOTIFY_DONE;
3375 switch (event) {
3376 case NETDEV_UP:
3377 spin_lock_bh(&card->ip_lock);
3378 qeth_l3_add_ip(card, addr);
3379 spin_unlock_bh(&card->ip_lock);
3380 break;
3381 case NETDEV_DOWN:
3382 spin_lock_bh(&card->ip_lock);
3383 qeth_l3_delete_ip(card, addr);
3384 spin_unlock_bh(&card->ip_lock);
3385 break;
3388 kfree(addr);
3389 return NOTIFY_DONE;
3392 static struct notifier_block qeth_l3_ip6_notifier = {
3393 qeth_l3_ip6_event,
3394 NULL,
3396 #endif
3398 static int qeth_l3_register_notifiers(void)
3400 int rc;
3402 QETH_DBF_TEXT(SETUP, 5, "regnotif");
3403 rc = register_inetaddr_notifier(&qeth_l3_ip_notifier);
3404 if (rc)
3405 return rc;
3406 #ifdef CONFIG_QETH_IPV6
3407 rc = register_inet6addr_notifier(&qeth_l3_ip6_notifier);
3408 if (rc) {
3409 unregister_inetaddr_notifier(&qeth_l3_ip_notifier);
3410 return rc;
3412 #else
3413 pr_warn("There is no IPv6 support for the layer 3 discipline\n");
3414 #endif
3415 return 0;
3418 static void qeth_l3_unregister_notifiers(void)
3421 QETH_DBF_TEXT(SETUP, 5, "unregnot");
3422 WARN_ON(unregister_inetaddr_notifier(&qeth_l3_ip_notifier));
3423 #ifdef CONFIG_QETH_IPV6
3424 WARN_ON(unregister_inet6addr_notifier(&qeth_l3_ip6_notifier));
3425 #endif /* QETH_IPV6 */
3428 static int __init qeth_l3_init(void)
3430 int rc = 0;
3432 pr_info("register layer 3 discipline\n");
3433 rc = qeth_l3_register_notifiers();
3434 return rc;
3437 static void __exit qeth_l3_exit(void)
3439 qeth_l3_unregister_notifiers();
3440 pr_info("unregister layer 3 discipline\n");
3443 module_init(qeth_l3_init);
3444 module_exit(qeth_l3_exit);
3445 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
3446 MODULE_DESCRIPTION("qeth layer 3 discipline");
3447 MODULE_LICENSE("GPL");