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