root/drivers/s390/net/qeth_l3_main.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. qeth_l3_ipaddr4_to_string
  2. qeth_l3_ipaddr6_to_string
  3. qeth_l3_ipaddr_to_string
  4. qeth_l3_get_addr_buffer
  5. qeth_l3_find_addr_by_ip
  6. qeth_l3_convert_addr_to_bits
  7. qeth_l3_is_addr_covered_by_ipato
  8. qeth_l3_delete_ip
  9. qeth_l3_add_ip
  10. qeth_l3_modify_ip
  11. qeth_l3_drain_rx_mode_cache
  12. qeth_l3_clear_ip_htable
  13. qeth_l3_recover_ip
  14. qeth_l3_setdelip_cb
  15. qeth_l3_send_setdelmc
  16. qeth_l3_fill_netmask
  17. qeth_l3_get_setdelip_flags
  18. qeth_l3_send_setdelip
  19. qeth_l3_send_setrouting
  20. qeth_l3_correct_routing_type
  21. qeth_l3_setrouting_v4
  22. qeth_l3_setrouting_v6
  23. qeth_l3_update_ipato
  24. qeth_l3_clear_ipato_list
  25. qeth_l3_add_ipato_entry
  26. qeth_l3_del_ipato_entry
  27. qeth_l3_modify_rxip_vipa
  28. qeth_l3_modify_hsuid
  29. qeth_l3_register_addr_entry
  30. qeth_l3_deregister_addr_entry
  31. qeth_l3_setadapter_parms
  32. qeth_l3_start_ipa_arp_processing
  33. qeth_l3_start_ipa_source_mac
  34. qeth_l3_start_ipa_vlan
  35. qeth_l3_start_ipa_multicast
  36. qeth_l3_softsetup_ipv6
  37. qeth_l3_start_ipa_ipv6
  38. qeth_l3_start_ipa_broadcast
  39. qeth_l3_start_ipassists
  40. qeth_l3_iqd_read_initial_mac_cb
  41. qeth_l3_iqd_read_initial_mac
  42. qeth_l3_get_unique_id_cb
  43. qeth_l3_get_unique_id
  44. qeth_diags_trace_cb
  45. qeth_diags_trace
  46. qeth_l3_add_mc_to_hash
  47. qeth_l3_add_vlan_mc
  48. qeth_l3_add_multicast_ipv4
  49. qeth_l3_add_mc6_to_hash
  50. qeth_l3_add_vlan_mc6
  51. qeth_l3_add_multicast_ipv6
  52. qeth_l3_vlan_rx_add_vid
  53. qeth_l3_vlan_rx_kill_vid
  54. qeth_l3_rebuild_skb
  55. qeth_l3_process_inbound_buffer
  56. qeth_l3_stop_card
  57. qeth_l3_set_promisc_mode
  58. qeth_l3_rx_mode_work
  59. qeth_l3_arp_makerc
  60. qeth_l3_arp_cmd_cb
  61. qeth_l3_arp_set_no_entries
  62. get_arp_entry_size
  63. arpentry_matches_prot
  64. qeth_l3_arp_query_cb
  65. qeth_l3_query_arp_cache_info
  66. qeth_l3_arp_query
  67. qeth_l3_arp_modify_entry
  68. qeth_l3_arp_flush_cache
  69. qeth_l3_do_ioctl
  70. qeth_l3_get_cast_type_rcu
  71. qeth_l3_get_cast_type
  72. qeth_l3_cast_type_to_flag
  73. qeth_l3_fill_header
  74. qeth_l3_fixup_headers
  75. qeth_l3_xmit
  76. qeth_l3_hard_start_xmit
  77. qeth_l3_set_rx_mode
  78. qeth_l3_neigh_setup_noarp
  79. qeth_l3_neigh_setup
  80. qeth_l3_osa_features_check
  81. qeth_l3_iqd_select_queue
  82. qeth_l3_osa_select_queue
  83. qeth_l3_setup_netdev
  84. qeth_l3_probe_device
  85. qeth_l3_remove_device
  86. qeth_l3_set_online
  87. __qeth_l3_set_offline
  88. qeth_l3_set_offline
  89. qeth_l3_recover
  90. qeth_l3_control_event
  91. qeth_l3_handle_ip_event
  92. qeth_l3_add_ip_worker
  93. qeth_l3_delete_ip_worker
  94. qeth_l3_get_card_from_dev
  95. qeth_l3_ip_event
  96. qeth_l3_ip6_event
  97. qeth_l3_register_notifiers
  98. qeth_l3_unregister_notifiers
  99. qeth_l3_init
  100. qeth_l3_exit

   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/in.h>
  22 #include <linux/ipv6.h>
  23 #include <linux/inetdevice.h>
  24 #include <linux/igmp.h>
  25 #include <linux/slab.h>
  26 #include <linux/if_ether.h>
  27 #include <linux/if_vlan.h>
  28 #include <linux/skbuff.h>
  29 
  30 #include <net/ip.h>
  31 #include <net/arp.h>
  32 #include <net/route.h>
  33 #include <net/ipv6.h>
  34 #include <net/ip6_route.h>
  35 #include <net/iucv/af_iucv.h>
  36 #include <linux/hashtable.h>
  37 
  38 #include "qeth_l3.h"
  39 
  40 
  41 static int qeth_l3_set_offline(struct ccwgroup_device *);
  42 static void qeth_l3_set_rx_mode(struct net_device *dev);
  43 static int qeth_l3_register_addr_entry(struct qeth_card *,
  44                 struct qeth_ipaddr *);
  45 static int qeth_l3_deregister_addr_entry(struct qeth_card *,
  46                 struct qeth_ipaddr *);
  47 
  48 static void qeth_l3_ipaddr4_to_string(const __u8 *addr, char *buf)
  49 {
  50         sprintf(buf, "%pI4", addr);
  51 }
  52 
  53 static void qeth_l3_ipaddr6_to_string(const __u8 *addr, char *buf)
  54 {
  55         sprintf(buf, "%pI6", addr);
  56 }
  57 
  58 void qeth_l3_ipaddr_to_string(enum qeth_prot_versions proto, const __u8 *addr,
  59                                 char *buf)
  60 {
  61         if (proto == QETH_PROT_IPV4)
  62                 qeth_l3_ipaddr4_to_string(addr, buf);
  63         else if (proto == QETH_PROT_IPV6)
  64                 qeth_l3_ipaddr6_to_string(addr, buf);
  65 }
  66 
  67 static struct qeth_ipaddr *qeth_l3_get_addr_buffer(enum qeth_prot_versions prot)
  68 {
  69         struct qeth_ipaddr *addr = kmalloc(sizeof(*addr), GFP_ATOMIC);
  70 
  71         if (addr)
  72                 qeth_l3_init_ipaddr(addr, QETH_IP_TYPE_NORMAL, prot);
  73         return addr;
  74 }
  75 
  76 static struct qeth_ipaddr *qeth_l3_find_addr_by_ip(struct qeth_card *card,
  77                                                    struct qeth_ipaddr *query)
  78 {
  79         u64 key = qeth_l3_ipaddr_hash(query);
  80         struct qeth_ipaddr *addr;
  81 
  82         if (query->is_multicast) {
  83                 hash_for_each_possible(card->ip_mc_htable, addr, hnode, key)
  84                         if (qeth_l3_addr_match_ip(addr, query))
  85                                 return addr;
  86         } else {
  87                 hash_for_each_possible(card->ip_htable,  addr, hnode, key)
  88                         if (qeth_l3_addr_match_ip(addr, query))
  89                                 return addr;
  90         }
  91         return NULL;
  92 }
  93 
  94 static void qeth_l3_convert_addr_to_bits(u8 *addr, u8 *bits, int len)
  95 {
  96         int i, j;
  97         u8 octet;
  98 
  99         for (i = 0; i < len; ++i) {
 100                 octet = addr[i];
 101                 for (j = 7; j >= 0; --j) {
 102                         bits[i*8 + j] = octet & 1;
 103                         octet >>= 1;
 104                 }
 105         }
 106 }
 107 
 108 static bool qeth_l3_is_addr_covered_by_ipato(struct qeth_card *card,
 109                                              struct qeth_ipaddr *addr)
 110 {
 111         struct qeth_ipato_entry *ipatoe;
 112         u8 addr_bits[128] = {0, };
 113         u8 ipatoe_bits[128] = {0, };
 114         int rc = 0;
 115 
 116         if (!card->ipato.enabled)
 117                 return false;
 118         if (addr->type != QETH_IP_TYPE_NORMAL)
 119                 return false;
 120 
 121         qeth_l3_convert_addr_to_bits((u8 *) &addr->u, addr_bits,
 122                                   (addr->proto == QETH_PROT_IPV4)? 4:16);
 123         list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
 124                 if (addr->proto != ipatoe->proto)
 125                         continue;
 126                 qeth_l3_convert_addr_to_bits(ipatoe->addr, ipatoe_bits,
 127                                           (ipatoe->proto == QETH_PROT_IPV4) ?
 128                                           4 : 16);
 129                 if (addr->proto == QETH_PROT_IPV4)
 130                         rc = !memcmp(addr_bits, ipatoe_bits,
 131                                      min(32, ipatoe->mask_bits));
 132                 else
 133                         rc = !memcmp(addr_bits, ipatoe_bits,
 134                                      min(128, ipatoe->mask_bits));
 135                 if (rc)
 136                         break;
 137         }
 138         /* invert? */
 139         if ((addr->proto == QETH_PROT_IPV4) && card->ipato.invert4)
 140                 rc = !rc;
 141         else if ((addr->proto == QETH_PROT_IPV6) && card->ipato.invert6)
 142                 rc = !rc;
 143 
 144         return rc;
 145 }
 146 
 147 static int qeth_l3_delete_ip(struct qeth_card *card,
 148                              struct qeth_ipaddr *tmp_addr)
 149 {
 150         int rc = 0;
 151         struct qeth_ipaddr *addr;
 152 
 153         if (tmp_addr->type == QETH_IP_TYPE_RXIP)
 154                 QETH_CARD_TEXT(card, 2, "delrxip");
 155         else if (tmp_addr->type == QETH_IP_TYPE_VIPA)
 156                 QETH_CARD_TEXT(card, 2, "delvipa");
 157         else
 158                 QETH_CARD_TEXT(card, 2, "delip");
 159 
 160         if (tmp_addr->proto == QETH_PROT_IPV4)
 161                 QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
 162         else {
 163                 QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
 164                 QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
 165         }
 166 
 167         addr = qeth_l3_find_addr_by_ip(card, tmp_addr);
 168         if (!addr || !qeth_l3_addr_match_all(addr, tmp_addr))
 169                 return -ENOENT;
 170 
 171         addr->ref_counter--;
 172         if (addr->type == QETH_IP_TYPE_NORMAL && addr->ref_counter > 0)
 173                 return rc;
 174         if (addr->in_progress)
 175                 return -EINPROGRESS;
 176 
 177         if (qeth_card_hw_is_reachable(card))
 178                 rc = qeth_l3_deregister_addr_entry(card, addr);
 179 
 180         hash_del(&addr->hnode);
 181         kfree(addr);
 182 
 183         return rc;
 184 }
 185 
 186 static int qeth_l3_add_ip(struct qeth_card *card, struct qeth_ipaddr *tmp_addr)
 187 {
 188         int rc = 0;
 189         struct qeth_ipaddr *addr;
 190         char buf[40];
 191 
 192         if (tmp_addr->type == QETH_IP_TYPE_RXIP)
 193                 QETH_CARD_TEXT(card, 2, "addrxip");
 194         else if (tmp_addr->type == QETH_IP_TYPE_VIPA)
 195                 QETH_CARD_TEXT(card, 2, "addvipa");
 196         else
 197                 QETH_CARD_TEXT(card, 2, "addip");
 198 
 199         if (tmp_addr->proto == QETH_PROT_IPV4)
 200                 QETH_CARD_HEX(card, 4, &tmp_addr->u.a4.addr, 4);
 201         else {
 202                 QETH_CARD_HEX(card, 4, &tmp_addr->u.a6.addr, 8);
 203                 QETH_CARD_HEX(card, 4, ((char *)&tmp_addr->u.a6.addr) + 8, 8);
 204         }
 205 
 206         addr = qeth_l3_find_addr_by_ip(card, tmp_addr);
 207         if (addr) {
 208                 if (tmp_addr->type != QETH_IP_TYPE_NORMAL)
 209                         return -EADDRINUSE;
 210                 if (qeth_l3_addr_match_all(addr, tmp_addr)) {
 211                         addr->ref_counter++;
 212                         return 0;
 213                 }
 214                 qeth_l3_ipaddr_to_string(tmp_addr->proto, (u8 *)&tmp_addr->u,
 215                                          buf);
 216                 dev_warn(&card->gdev->dev,
 217                          "Registering IP address %s failed\n", buf);
 218                 return -EADDRINUSE;
 219         } else {
 220                 addr = qeth_l3_get_addr_buffer(tmp_addr->proto);
 221                 if (!addr)
 222                         return -ENOMEM;
 223 
 224                 memcpy(addr, tmp_addr, sizeof(struct qeth_ipaddr));
 225                 addr->ref_counter = 1;
 226 
 227                 if (qeth_l3_is_addr_covered_by_ipato(card, addr)) {
 228                         QETH_CARD_TEXT(card, 2, "tkovaddr");
 229                         addr->ipato = 1;
 230                 }
 231                 hash_add(card->ip_htable, &addr->hnode,
 232                                 qeth_l3_ipaddr_hash(addr));
 233 
 234                 if (!qeth_card_hw_is_reachable(card)) {
 235                         addr->disp_flag = QETH_DISP_ADDR_ADD;
 236                         return 0;
 237                 }
 238 
 239                 /* qeth_l3_register_addr_entry can go to sleep
 240                  * if we add a IPV4 addr. It is caused by the reason
 241                  * that SETIP ipa cmd starts ARP staff for IPV4 addr.
 242                  * Thus we should unlock spinlock, and make a protection
 243                  * using in_progress variable to indicate that there is
 244                  * an hardware operation with this IPV4 address
 245                  */
 246                 if (addr->proto == QETH_PROT_IPV4) {
 247                         addr->in_progress = 1;
 248                         mutex_unlock(&card->ip_lock);
 249                         rc = qeth_l3_register_addr_entry(card, addr);
 250                         mutex_lock(&card->ip_lock);
 251                         addr->in_progress = 0;
 252                 } else
 253                         rc = qeth_l3_register_addr_entry(card, addr);
 254 
 255                 if (!rc || rc == -EADDRINUSE || rc == -ENETDOWN) {
 256                         addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
 257                         if (addr->ref_counter < 1) {
 258                                 qeth_l3_deregister_addr_entry(card, addr);
 259                                 hash_del(&addr->hnode);
 260                                 kfree(addr);
 261                         }
 262                 } else {
 263                         hash_del(&addr->hnode);
 264                         kfree(addr);
 265                 }
 266         }
 267         return rc;
 268 }
 269 
 270 static int qeth_l3_modify_ip(struct qeth_card *card, struct qeth_ipaddr *addr,
 271                              bool add)
 272 {
 273         int rc;
 274 
 275         mutex_lock(&card->ip_lock);
 276         rc = add ? qeth_l3_add_ip(card, addr) : qeth_l3_delete_ip(card, addr);
 277         mutex_unlock(&card->ip_lock);
 278 
 279         return rc;
 280 }
 281 
 282 static void qeth_l3_drain_rx_mode_cache(struct qeth_card *card)
 283 {
 284         struct qeth_ipaddr *addr;
 285         struct hlist_node *tmp;
 286         int i;
 287 
 288         hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
 289                 hash_del(&addr->hnode);
 290                 kfree(addr);
 291         }
 292 }
 293 
 294 static void qeth_l3_clear_ip_htable(struct qeth_card *card, int recover)
 295 {
 296         struct qeth_ipaddr *addr;
 297         struct hlist_node *tmp;
 298         int i;
 299 
 300         QETH_CARD_TEXT(card, 4, "clearip");
 301 
 302         mutex_lock(&card->ip_lock);
 303 
 304         hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
 305                 if (!recover) {
 306                         hash_del(&addr->hnode);
 307                         kfree(addr);
 308                         continue;
 309                 }
 310                 addr->disp_flag = QETH_DISP_ADDR_ADD;
 311         }
 312 
 313         mutex_unlock(&card->ip_lock);
 314 }
 315 
 316 static void qeth_l3_recover_ip(struct qeth_card *card)
 317 {
 318         struct qeth_ipaddr *addr;
 319         struct hlist_node *tmp;
 320         int i;
 321         int rc;
 322 
 323         QETH_CARD_TEXT(card, 4, "recovrip");
 324 
 325         mutex_lock(&card->ip_lock);
 326 
 327         hash_for_each_safe(card->ip_htable, i, tmp, addr, hnode) {
 328                 if (addr->disp_flag == QETH_DISP_ADDR_ADD) {
 329                         if (addr->proto == QETH_PROT_IPV4) {
 330                                 addr->in_progress = 1;
 331                                 mutex_unlock(&card->ip_lock);
 332                                 rc = qeth_l3_register_addr_entry(card, addr);
 333                                 mutex_lock(&card->ip_lock);
 334                                 addr->in_progress = 0;
 335                         } else
 336                                 rc = qeth_l3_register_addr_entry(card, addr);
 337 
 338                         if (!rc) {
 339                                 addr->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
 340                                 if (addr->ref_counter < 1)
 341                                         qeth_l3_delete_ip(card, addr);
 342                         } else {
 343                                 hash_del(&addr->hnode);
 344                                 kfree(addr);
 345                         }
 346                 }
 347         }
 348 
 349         mutex_unlock(&card->ip_lock);
 350 }
 351 
 352 static int qeth_l3_setdelip_cb(struct qeth_card *card, struct qeth_reply *reply,
 353                                unsigned long data)
 354 {
 355         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
 356 
 357         switch (cmd->hdr.return_code) {
 358         case IPA_RC_SUCCESS:
 359                 return 0;
 360         case IPA_RC_DUPLICATE_IP_ADDRESS:
 361                 return -EADDRINUSE;
 362         case IPA_RC_MC_ADDR_NOT_FOUND:
 363                 return -ENOENT;
 364         case IPA_RC_LAN_OFFLINE:
 365                 return -ENETDOWN;
 366         default:
 367                 return -EIO;
 368         }
 369 }
 370 
 371 static int qeth_l3_send_setdelmc(struct qeth_card *card,
 372                         struct qeth_ipaddr *addr, int ipacmd)
 373 {
 374         struct qeth_cmd_buffer *iob;
 375         struct qeth_ipa_cmd *cmd;
 376 
 377         QETH_CARD_TEXT(card, 4, "setdelmc");
 378 
 379         iob = qeth_ipa_alloc_cmd(card, ipacmd, addr->proto,
 380                                  IPA_DATA_SIZEOF(setdelipm));
 381         if (!iob)
 382                 return -ENOMEM;
 383         cmd = __ipa_cmd(iob);
 384         ether_addr_copy(cmd->data.setdelipm.mac, addr->mac);
 385         if (addr->proto == QETH_PROT_IPV6)
 386                 memcpy(cmd->data.setdelipm.ip6, &addr->u.a6.addr,
 387                        sizeof(struct in6_addr));
 388         else
 389                 memcpy(&cmd->data.setdelipm.ip4, &addr->u.a4.addr, 4);
 390 
 391         return qeth_send_ipa_cmd(card, iob, qeth_l3_setdelip_cb, NULL);
 392 }
 393 
 394 static void qeth_l3_fill_netmask(u8 *netmask, unsigned int len)
 395 {
 396         int i, j;
 397         for (i = 0; i < 16; i++) {
 398                 j = (len) - (i * 8);
 399                 if (j >= 8)
 400                         netmask[i] = 0xff;
 401                 else if (j > 0)
 402                         netmask[i] = (u8)(0xFF00 >> j);
 403                 else
 404                         netmask[i] = 0;
 405         }
 406 }
 407 
 408 static u32 qeth_l3_get_setdelip_flags(struct qeth_ipaddr *addr, bool set)
 409 {
 410         switch (addr->type) {
 411         case QETH_IP_TYPE_RXIP:
 412                 return (set) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0;
 413         case QETH_IP_TYPE_VIPA:
 414                 return (set) ? QETH_IPA_SETIP_VIPA_FLAG :
 415                                QETH_IPA_DELIP_VIPA_FLAG;
 416         default:
 417                 return (set && addr->ipato) ? QETH_IPA_SETIP_TAKEOVER_FLAG : 0;
 418         }
 419 }
 420 
 421 static int qeth_l3_send_setdelip(struct qeth_card *card,
 422                                  struct qeth_ipaddr *addr,
 423                                  enum qeth_ipa_cmds ipacmd)
 424 {
 425         struct qeth_cmd_buffer *iob;
 426         struct qeth_ipa_cmd *cmd;
 427         __u8 netmask[16];
 428         u32 flags;
 429 
 430         QETH_CARD_TEXT(card, 4, "setdelip");
 431 
 432         iob = qeth_ipa_alloc_cmd(card, ipacmd, addr->proto,
 433                                  IPA_DATA_SIZEOF(setdelip6));
 434         if (!iob)
 435                 return -ENOMEM;
 436         cmd = __ipa_cmd(iob);
 437 
 438         flags = qeth_l3_get_setdelip_flags(addr, ipacmd == IPA_CMD_SETIP);
 439         QETH_CARD_TEXT_(card, 4, "flags%02X", flags);
 440 
 441         if (addr->proto == QETH_PROT_IPV6) {
 442                 memcpy(cmd->data.setdelip6.ip_addr, &addr->u.a6.addr,
 443                        sizeof(struct in6_addr));
 444                 qeth_l3_fill_netmask(netmask, addr->u.a6.pfxlen);
 445                 memcpy(cmd->data.setdelip6.mask, netmask,
 446                        sizeof(struct in6_addr));
 447                 cmd->data.setdelip6.flags = flags;
 448         } else {
 449                 memcpy(cmd->data.setdelip4.ip_addr, &addr->u.a4.addr, 4);
 450                 memcpy(cmd->data.setdelip4.mask, &addr->u.a4.mask, 4);
 451                 cmd->data.setdelip4.flags = flags;
 452         }
 453 
 454         return qeth_send_ipa_cmd(card, iob, qeth_l3_setdelip_cb, NULL);
 455 }
 456 
 457 static int qeth_l3_send_setrouting(struct qeth_card *card,
 458         enum qeth_routing_types type, enum qeth_prot_versions prot)
 459 {
 460         int rc;
 461         struct qeth_ipa_cmd *cmd;
 462         struct qeth_cmd_buffer *iob;
 463 
 464         QETH_CARD_TEXT(card, 4, "setroutg");
 465         iob = qeth_ipa_alloc_cmd(card, IPA_CMD_SETRTG, prot,
 466                                  IPA_DATA_SIZEOF(setrtg));
 467         if (!iob)
 468                 return -ENOMEM;
 469         cmd = __ipa_cmd(iob);
 470         cmd->data.setrtg.type = (type);
 471         rc = qeth_send_ipa_cmd(card, iob, NULL, NULL);
 472 
 473         return rc;
 474 }
 475 
 476 static int qeth_l3_correct_routing_type(struct qeth_card *card,
 477                 enum qeth_routing_types *type, enum qeth_prot_versions prot)
 478 {
 479         if (IS_IQD(card)) {
 480                 switch (*type) {
 481                 case NO_ROUTER:
 482                 case PRIMARY_CONNECTOR:
 483                 case SECONDARY_CONNECTOR:
 484                 case MULTICAST_ROUTER:
 485                         return 0;
 486                 default:
 487                         goto out_inval;
 488                 }
 489         } else {
 490                 switch (*type) {
 491                 case NO_ROUTER:
 492                 case PRIMARY_ROUTER:
 493                 case SECONDARY_ROUTER:
 494                         return 0;
 495                 case MULTICAST_ROUTER:
 496                         if (qeth_is_ipafunc_supported(card, prot,
 497                                                       IPA_OSA_MC_ROUTER))
 498                                 return 0;
 499                 default:
 500                         goto out_inval;
 501                 }
 502         }
 503 out_inval:
 504         *type = NO_ROUTER;
 505         return -EINVAL;
 506 }
 507 
 508 int qeth_l3_setrouting_v4(struct qeth_card *card)
 509 {
 510         int rc;
 511 
 512         QETH_CARD_TEXT(card, 3, "setrtg4");
 513 
 514         rc = qeth_l3_correct_routing_type(card, &card->options.route4.type,
 515                                   QETH_PROT_IPV4);
 516         if (rc)
 517                 return rc;
 518 
 519         rc = qeth_l3_send_setrouting(card, card->options.route4.type,
 520                                   QETH_PROT_IPV4);
 521         if (rc) {
 522                 card->options.route4.type = NO_ROUTER;
 523                 QETH_DBF_MESSAGE(2, "Error (%#06x) while setting routing type on device %x. Type set to 'no router'.\n",
 524                                  rc, CARD_DEVID(card));
 525         }
 526         return rc;
 527 }
 528 
 529 int qeth_l3_setrouting_v6(struct qeth_card *card)
 530 {
 531         int rc = 0;
 532 
 533         QETH_CARD_TEXT(card, 3, "setrtg6");
 534 
 535         if (!qeth_is_supported(card, IPA_IPV6))
 536                 return 0;
 537         rc = qeth_l3_correct_routing_type(card, &card->options.route6.type,
 538                                   QETH_PROT_IPV6);
 539         if (rc)
 540                 return rc;
 541 
 542         rc = qeth_l3_send_setrouting(card, card->options.route6.type,
 543                                   QETH_PROT_IPV6);
 544         if (rc) {
 545                 card->options.route6.type = NO_ROUTER;
 546                 QETH_DBF_MESSAGE(2, "Error (%#06x) while setting routing type on device %x. Type set to 'no router'.\n",
 547                                  rc, CARD_DEVID(card));
 548         }
 549         return rc;
 550 }
 551 
 552 /*
 553  * IP address takeover related functions
 554  */
 555 
 556 /**
 557  * qeth_l3_update_ipato() - Update 'takeover' property, for all NORMAL IPs.
 558  *
 559  * Caller must hold ip_lock.
 560  */
 561 void qeth_l3_update_ipato(struct qeth_card *card)
 562 {
 563         struct qeth_ipaddr *addr;
 564         unsigned int i;
 565 
 566         hash_for_each(card->ip_htable, i, addr, hnode) {
 567                 if (addr->type != QETH_IP_TYPE_NORMAL)
 568                         continue;
 569                 addr->ipato = qeth_l3_is_addr_covered_by_ipato(card, addr);
 570         }
 571 }
 572 
 573 static void qeth_l3_clear_ipato_list(struct qeth_card *card)
 574 {
 575         struct qeth_ipato_entry *ipatoe, *tmp;
 576 
 577         mutex_lock(&card->ip_lock);
 578 
 579         list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
 580                 list_del(&ipatoe->entry);
 581                 kfree(ipatoe);
 582         }
 583 
 584         qeth_l3_update_ipato(card);
 585         mutex_unlock(&card->ip_lock);
 586 }
 587 
 588 int qeth_l3_add_ipato_entry(struct qeth_card *card,
 589                                 struct qeth_ipato_entry *new)
 590 {
 591         struct qeth_ipato_entry *ipatoe;
 592         int rc = 0;
 593 
 594         QETH_CARD_TEXT(card, 2, "addipato");
 595 
 596         mutex_lock(&card->ip_lock);
 597 
 598         list_for_each_entry(ipatoe, &card->ipato.entries, entry) {
 599                 if (ipatoe->proto != new->proto)
 600                         continue;
 601                 if (!memcmp(ipatoe->addr, new->addr,
 602                             (ipatoe->proto == QETH_PROT_IPV4)? 4:16) &&
 603                     (ipatoe->mask_bits == new->mask_bits)) {
 604                         rc = -EEXIST;
 605                         break;
 606                 }
 607         }
 608 
 609         if (!rc) {
 610                 list_add_tail(&new->entry, &card->ipato.entries);
 611                 qeth_l3_update_ipato(card);
 612         }
 613 
 614         mutex_unlock(&card->ip_lock);
 615 
 616         return rc;
 617 }
 618 
 619 int qeth_l3_del_ipato_entry(struct qeth_card *card,
 620                             enum qeth_prot_versions proto, u8 *addr,
 621                             int mask_bits)
 622 {
 623         struct qeth_ipato_entry *ipatoe, *tmp;
 624         int rc = -ENOENT;
 625 
 626         QETH_CARD_TEXT(card, 2, "delipato");
 627 
 628         mutex_lock(&card->ip_lock);
 629 
 630         list_for_each_entry_safe(ipatoe, tmp, &card->ipato.entries, entry) {
 631                 if (ipatoe->proto != proto)
 632                         continue;
 633                 if (!memcmp(ipatoe->addr, addr,
 634                             (proto == QETH_PROT_IPV4)? 4:16) &&
 635                     (ipatoe->mask_bits == mask_bits)) {
 636                         list_del(&ipatoe->entry);
 637                         qeth_l3_update_ipato(card);
 638                         kfree(ipatoe);
 639                         rc = 0;
 640                 }
 641         }
 642 
 643         mutex_unlock(&card->ip_lock);
 644         return rc;
 645 }
 646 
 647 int qeth_l3_modify_rxip_vipa(struct qeth_card *card, bool add, const u8 *ip,
 648                              enum qeth_ip_types type,
 649                              enum qeth_prot_versions proto)
 650 {
 651         struct qeth_ipaddr addr;
 652 
 653         qeth_l3_init_ipaddr(&addr, type, proto);
 654         if (proto == QETH_PROT_IPV4)
 655                 memcpy(&addr.u.a4.addr, ip, 4);
 656         else
 657                 memcpy(&addr.u.a6.addr, ip, 16);
 658 
 659         return qeth_l3_modify_ip(card, &addr, add);
 660 }
 661 
 662 int qeth_l3_modify_hsuid(struct qeth_card *card, bool add)
 663 {
 664         struct qeth_ipaddr addr;
 665         unsigned int i;
 666 
 667         qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV6);
 668         addr.u.a6.addr.s6_addr[0] = 0xfe;
 669         addr.u.a6.addr.s6_addr[1] = 0x80;
 670         for (i = 0; i < 8; i++)
 671                 addr.u.a6.addr.s6_addr[8+i] = card->options.hsuid[i];
 672 
 673         return qeth_l3_modify_ip(card, &addr, add);
 674 }
 675 
 676 static int qeth_l3_register_addr_entry(struct qeth_card *card,
 677                                 struct qeth_ipaddr *addr)
 678 {
 679         char buf[50];
 680         int rc = 0;
 681         int cnt = 3;
 682 
 683         if (card->options.sniffer)
 684                 return 0;
 685 
 686         if (addr->proto == QETH_PROT_IPV4) {
 687                 QETH_CARD_TEXT(card, 2, "setaddr4");
 688                 QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
 689         } else if (addr->proto == QETH_PROT_IPV6) {
 690                 QETH_CARD_TEXT(card, 2, "setaddr6");
 691                 QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
 692                 QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
 693         } else {
 694                 QETH_CARD_TEXT(card, 2, "setaddr?");
 695                 QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
 696         }
 697         do {
 698                 if (addr->is_multicast)
 699                         rc =  qeth_l3_send_setdelmc(card, addr, IPA_CMD_SETIPM);
 700                 else
 701                         rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_SETIP);
 702                 if (rc)
 703                         QETH_CARD_TEXT(card, 2, "failed");
 704         } while ((--cnt > 0) && rc);
 705         if (rc) {
 706                 QETH_CARD_TEXT(card, 2, "FAILED");
 707                 qeth_l3_ipaddr_to_string(addr->proto, (u8 *)&addr->u, buf);
 708                 dev_warn(&card->gdev->dev,
 709                         "Registering IP address %s failed\n", buf);
 710         }
 711         return rc;
 712 }
 713 
 714 static int qeth_l3_deregister_addr_entry(struct qeth_card *card,
 715                                                 struct qeth_ipaddr *addr)
 716 {
 717         int rc = 0;
 718 
 719         if (card->options.sniffer)
 720                 return 0;
 721 
 722         if (addr->proto == QETH_PROT_IPV4) {
 723                 QETH_CARD_TEXT(card, 2, "deladdr4");
 724                 QETH_CARD_HEX(card, 3, &addr->u.a4.addr, sizeof(int));
 725         } else if (addr->proto == QETH_PROT_IPV6) {
 726                 QETH_CARD_TEXT(card, 2, "deladdr6");
 727                 QETH_CARD_HEX(card, 3, &addr->u.a6.addr, 8);
 728                 QETH_CARD_HEX(card, 3, ((char *)&addr->u.a6.addr) + 8, 8);
 729         } else {
 730                 QETH_CARD_TEXT(card, 2, "deladdr?");
 731                 QETH_CARD_HEX(card, 3, addr, sizeof(struct qeth_ipaddr));
 732         }
 733         if (addr->is_multicast)
 734                 rc = qeth_l3_send_setdelmc(card, addr, IPA_CMD_DELIPM);
 735         else
 736                 rc = qeth_l3_send_setdelip(card, addr, IPA_CMD_DELIP);
 737         if (rc)
 738                 QETH_CARD_TEXT(card, 2, "failed");
 739 
 740         return rc;
 741 }
 742 
 743 static int qeth_l3_setadapter_parms(struct qeth_card *card)
 744 {
 745         int rc = 0;
 746 
 747         QETH_CARD_TEXT(card, 2, "setadprm");
 748 
 749         if (qeth_adp_supported(card, IPA_SETADP_ALTER_MAC_ADDRESS)) {
 750                 rc = qeth_setadpparms_change_macaddr(card);
 751                 if (rc)
 752                         dev_warn(&card->gdev->dev, "Reading the adapter MAC"
 753                                 " address failed\n");
 754         }
 755 
 756         return rc;
 757 }
 758 
 759 static int qeth_l3_start_ipa_arp_processing(struct qeth_card *card)
 760 {
 761         int rc;
 762 
 763         QETH_CARD_TEXT(card, 3, "ipaarp");
 764 
 765         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
 766                 dev_info(&card->gdev->dev,
 767                         "ARP processing not supported on %s!\n",
 768                         QETH_CARD_IFNAME(card));
 769                 return 0;
 770         }
 771         rc = qeth_send_simple_setassparms(card, IPA_ARP_PROCESSING,
 772                                           IPA_CMD_ASS_START, NULL);
 773         if (rc) {
 774                 dev_warn(&card->gdev->dev,
 775                         "Starting ARP processing support for %s failed\n",
 776                         QETH_CARD_IFNAME(card));
 777         }
 778         return rc;
 779 }
 780 
 781 static int qeth_l3_start_ipa_source_mac(struct qeth_card *card)
 782 {
 783         int rc;
 784 
 785         QETH_CARD_TEXT(card, 3, "stsrcmac");
 786 
 787         if (!qeth_is_supported(card, IPA_SOURCE_MAC)) {
 788                 dev_info(&card->gdev->dev,
 789                         "Inbound source MAC-address not supported on %s\n",
 790                         QETH_CARD_IFNAME(card));
 791                 return -EOPNOTSUPP;
 792         }
 793 
 794         rc = qeth_send_simple_setassparms(card, IPA_SOURCE_MAC,
 795                                           IPA_CMD_ASS_START, NULL);
 796         if (rc)
 797                 dev_warn(&card->gdev->dev,
 798                         "Starting source MAC-address support for %s failed\n",
 799                         QETH_CARD_IFNAME(card));
 800         return rc;
 801 }
 802 
 803 static int qeth_l3_start_ipa_vlan(struct qeth_card *card)
 804 {
 805         int rc = 0;
 806 
 807         QETH_CARD_TEXT(card, 3, "strtvlan");
 808 
 809         if (!qeth_is_supported(card, IPA_FULL_VLAN)) {
 810                 dev_info(&card->gdev->dev,
 811                         "VLAN not supported on %s\n", QETH_CARD_IFNAME(card));
 812                 return -EOPNOTSUPP;
 813         }
 814 
 815         rc = qeth_send_simple_setassparms(card, IPA_VLAN_PRIO,
 816                                           IPA_CMD_ASS_START, NULL);
 817         if (rc) {
 818                 dev_warn(&card->gdev->dev,
 819                         "Starting VLAN support for %s failed\n",
 820                         QETH_CARD_IFNAME(card));
 821         } else {
 822                 dev_info(&card->gdev->dev, "VLAN enabled\n");
 823         }
 824         return rc;
 825 }
 826 
 827 static int qeth_l3_start_ipa_multicast(struct qeth_card *card)
 828 {
 829         int rc;
 830 
 831         QETH_CARD_TEXT(card, 3, "stmcast");
 832 
 833         if (!qeth_is_supported(card, IPA_MULTICASTING)) {
 834                 dev_info(&card->gdev->dev,
 835                         "Multicast not supported on %s\n",
 836                         QETH_CARD_IFNAME(card));
 837                 return -EOPNOTSUPP;
 838         }
 839 
 840         rc = qeth_send_simple_setassparms(card, IPA_MULTICASTING,
 841                                           IPA_CMD_ASS_START, NULL);
 842         if (rc) {
 843                 dev_warn(&card->gdev->dev,
 844                         "Starting multicast support for %s failed\n",
 845                         QETH_CARD_IFNAME(card));
 846         } else {
 847                 dev_info(&card->gdev->dev, "Multicast enabled\n");
 848                 card->dev->flags |= IFF_MULTICAST;
 849         }
 850         return rc;
 851 }
 852 
 853 static int qeth_l3_softsetup_ipv6(struct qeth_card *card)
 854 {
 855         u32 ipv6_data = 3;
 856         int rc;
 857 
 858         QETH_CARD_TEXT(card, 3, "softipv6");
 859 
 860         if (IS_IQD(card))
 861                 goto out;
 862 
 863         rc = qeth_send_simple_setassparms(card, IPA_IPV6, IPA_CMD_ASS_START,
 864                                           &ipv6_data);
 865         if (rc) {
 866                 dev_err(&card->gdev->dev,
 867                         "Activating IPv6 support for %s failed\n",
 868                         QETH_CARD_IFNAME(card));
 869                 return rc;
 870         }
 871         rc = qeth_send_simple_setassparms_v6(card, IPA_IPV6, IPA_CMD_ASS_START,
 872                                              NULL);
 873         if (rc) {
 874                 dev_err(&card->gdev->dev,
 875                         "Activating IPv6 support for %s failed\n",
 876                          QETH_CARD_IFNAME(card));
 877                 return rc;
 878         }
 879         rc = qeth_send_simple_setassparms_v6(card, IPA_PASSTHRU,
 880                                              IPA_CMD_ASS_START, NULL);
 881         if (rc) {
 882                 dev_warn(&card->gdev->dev,
 883                         "Enabling the passthrough mode for %s failed\n",
 884                         QETH_CARD_IFNAME(card));
 885                 return rc;
 886         }
 887 out:
 888         dev_info(&card->gdev->dev, "IPV6 enabled\n");
 889         return 0;
 890 }
 891 
 892 static int qeth_l3_start_ipa_ipv6(struct qeth_card *card)
 893 {
 894         QETH_CARD_TEXT(card, 3, "strtipv6");
 895 
 896         if (!qeth_is_supported(card, IPA_IPV6)) {
 897                 dev_info(&card->gdev->dev,
 898                         "IPv6 not supported on %s\n", QETH_CARD_IFNAME(card));
 899                 return 0;
 900         }
 901         return qeth_l3_softsetup_ipv6(card);
 902 }
 903 
 904 static int qeth_l3_start_ipa_broadcast(struct qeth_card *card)
 905 {
 906         u32 filter_data = 1;
 907         int rc;
 908 
 909         QETH_CARD_TEXT(card, 3, "stbrdcst");
 910         card->info.broadcast_capable = 0;
 911         if (!qeth_is_supported(card, IPA_FILTERING)) {
 912                 dev_info(&card->gdev->dev,
 913                         "Broadcast not supported on %s\n",
 914                         QETH_CARD_IFNAME(card));
 915                 rc = -EOPNOTSUPP;
 916                 goto out;
 917         }
 918         rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
 919                                           IPA_CMD_ASS_START, NULL);
 920         if (rc) {
 921                 dev_warn(&card->gdev->dev, "Enabling broadcast filtering for "
 922                         "%s failed\n", QETH_CARD_IFNAME(card));
 923                 goto out;
 924         }
 925 
 926         rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
 927                                           IPA_CMD_ASS_CONFIGURE, &filter_data);
 928         if (rc) {
 929                 dev_warn(&card->gdev->dev,
 930                         "Setting up broadcast filtering for %s failed\n",
 931                         QETH_CARD_IFNAME(card));
 932                 goto out;
 933         }
 934         card->info.broadcast_capable = QETH_BROADCAST_WITH_ECHO;
 935         dev_info(&card->gdev->dev, "Broadcast enabled\n");
 936         rc = qeth_send_simple_setassparms(card, IPA_FILTERING,
 937                                           IPA_CMD_ASS_ENABLE, &filter_data);
 938         if (rc) {
 939                 dev_warn(&card->gdev->dev, "Setting up broadcast echo "
 940                         "filtering for %s failed\n", QETH_CARD_IFNAME(card));
 941                 goto out;
 942         }
 943         card->info.broadcast_capable = QETH_BROADCAST_WITHOUT_ECHO;
 944 out:
 945         if (card->info.broadcast_capable)
 946                 card->dev->flags |= IFF_BROADCAST;
 947         else
 948                 card->dev->flags &= ~IFF_BROADCAST;
 949         return rc;
 950 }
 951 
 952 static int qeth_l3_start_ipassists(struct qeth_card *card)
 953 {
 954         QETH_CARD_TEXT(card, 3, "strtipas");
 955 
 956         if (qeth_set_access_ctrl_online(card, 0))
 957                 return -EIO;
 958         qeth_l3_start_ipa_arp_processing(card); /* go on*/
 959         qeth_l3_start_ipa_source_mac(card);     /* go on*/
 960         qeth_l3_start_ipa_vlan(card);           /* go on*/
 961         qeth_l3_start_ipa_multicast(card);              /* go on*/
 962         qeth_l3_start_ipa_ipv6(card);           /* go on*/
 963         qeth_l3_start_ipa_broadcast(card);              /* go on*/
 964         return 0;
 965 }
 966 
 967 static int qeth_l3_iqd_read_initial_mac_cb(struct qeth_card *card,
 968                 struct qeth_reply *reply, unsigned long data)
 969 {
 970         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
 971 
 972         if (cmd->hdr.return_code)
 973                 return -EIO;
 974 
 975         ether_addr_copy(card->dev->dev_addr,
 976                         cmd->data.create_destroy_addr.unique_id);
 977         return 0;
 978 }
 979 
 980 static int qeth_l3_iqd_read_initial_mac(struct qeth_card *card)
 981 {
 982         int rc = 0;
 983         struct qeth_cmd_buffer *iob;
 984         struct qeth_ipa_cmd *cmd;
 985 
 986         QETH_CARD_TEXT(card, 2, "hsrmac");
 987 
 988         iob = qeth_ipa_alloc_cmd(card, IPA_CMD_CREATE_ADDR, QETH_PROT_IPV6,
 989                                  IPA_DATA_SIZEOF(create_destroy_addr));
 990         if (!iob)
 991                 return -ENOMEM;
 992         cmd = __ipa_cmd(iob);
 993         *((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
 994                         card->info.unique_id;
 995 
 996         rc = qeth_send_ipa_cmd(card, iob, qeth_l3_iqd_read_initial_mac_cb,
 997                                 NULL);
 998         return rc;
 999 }
1000 
1001 static int qeth_l3_get_unique_id_cb(struct qeth_card *card,
1002                 struct qeth_reply *reply, unsigned long data)
1003 {
1004         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1005 
1006         if (cmd->hdr.return_code == 0) {
1007                 card->info.unique_id = *((__u16 *)
1008                                 &cmd->data.create_destroy_addr.unique_id[6]);
1009                 return 0;
1010         }
1011 
1012         card->info.unique_id = UNIQUE_ID_IF_CREATE_ADDR_FAILED |
1013                                UNIQUE_ID_NOT_BY_CARD;
1014         dev_warn(&card->gdev->dev, "The network adapter failed to generate a unique ID\n");
1015         return -EIO;
1016 }
1017 
1018 static int qeth_l3_get_unique_id(struct qeth_card *card)
1019 {
1020         int rc = 0;
1021         struct qeth_cmd_buffer *iob;
1022         struct qeth_ipa_cmd *cmd;
1023 
1024         QETH_CARD_TEXT(card, 2, "guniqeid");
1025 
1026         if (!qeth_is_supported(card, IPA_IPV6)) {
1027                 card->info.unique_id =  UNIQUE_ID_IF_CREATE_ADDR_FAILED |
1028                                         UNIQUE_ID_NOT_BY_CARD;
1029                 return 0;
1030         }
1031 
1032         iob = qeth_ipa_alloc_cmd(card, IPA_CMD_CREATE_ADDR, QETH_PROT_IPV6,
1033                                  IPA_DATA_SIZEOF(create_destroy_addr));
1034         if (!iob)
1035                 return -ENOMEM;
1036         cmd = __ipa_cmd(iob);
1037         *((__u16 *) &cmd->data.create_destroy_addr.unique_id[6]) =
1038                         card->info.unique_id;
1039 
1040         rc = qeth_send_ipa_cmd(card, iob, qeth_l3_get_unique_id_cb, NULL);
1041         return rc;
1042 }
1043 
1044 static int
1045 qeth_diags_trace_cb(struct qeth_card *card, struct qeth_reply *reply,
1046                             unsigned long data)
1047 {
1048         struct qeth_ipa_cmd        *cmd;
1049         __u16 rc;
1050 
1051         QETH_CARD_TEXT(card, 2, "diastrcb");
1052 
1053         cmd = (struct qeth_ipa_cmd *)data;
1054         rc = cmd->hdr.return_code;
1055         if (rc)
1056                 QETH_CARD_TEXT_(card, 2, "dxter%x", rc);
1057         switch (cmd->data.diagass.action) {
1058         case QETH_DIAGS_CMD_TRACE_QUERY:
1059                 break;
1060         case QETH_DIAGS_CMD_TRACE_DISABLE:
1061                 switch (rc) {
1062                 case 0:
1063                 case IPA_RC_INVALID_SUBCMD:
1064                         card->info.promisc_mode = SET_PROMISC_MODE_OFF;
1065                         dev_info(&card->gdev->dev, "The HiperSockets network "
1066                                 "traffic analyzer is deactivated\n");
1067                         break;
1068                 default:
1069                         break;
1070                 }
1071                 break;
1072         case QETH_DIAGS_CMD_TRACE_ENABLE:
1073                 switch (rc) {
1074                 case 0:
1075                         card->info.promisc_mode = SET_PROMISC_MODE_ON;
1076                         dev_info(&card->gdev->dev, "The HiperSockets network "
1077                                 "traffic analyzer is activated\n");
1078                         break;
1079                 case IPA_RC_HARDWARE_AUTH_ERROR:
1080                         dev_warn(&card->gdev->dev, "The device is not "
1081                                 "authorized to run as a HiperSockets network "
1082                                 "traffic analyzer\n");
1083                         break;
1084                 case IPA_RC_TRACE_ALREADY_ACTIVE:
1085                         dev_warn(&card->gdev->dev, "A HiperSockets "
1086                                 "network traffic analyzer is already "
1087                                 "active in the HiperSockets LAN\n");
1088                         break;
1089                 default:
1090                         break;
1091                 }
1092                 break;
1093         default:
1094                 QETH_DBF_MESSAGE(2, "Unknown sniffer action (%#06x) on device %x\n",
1095                                  cmd->data.diagass.action, CARD_DEVID(card));
1096         }
1097 
1098         return rc ? -EIO : 0;
1099 }
1100 
1101 static int
1102 qeth_diags_trace(struct qeth_card *card, enum qeth_diags_trace_cmds diags_cmd)
1103 {
1104         struct qeth_cmd_buffer *iob;
1105         struct qeth_ipa_cmd    *cmd;
1106 
1107         QETH_CARD_TEXT(card, 2, "diagtrac");
1108 
1109         iob = qeth_get_diag_cmd(card, QETH_DIAGS_CMD_TRACE, 0);
1110         if (!iob)
1111                 return -ENOMEM;
1112         cmd = __ipa_cmd(iob);
1113         cmd->data.diagass.type = QETH_DIAGS_TYPE_HIPERSOCKET;
1114         cmd->data.diagass.action = diags_cmd;
1115         return qeth_send_ipa_cmd(card, iob, qeth_diags_trace_cb, NULL);
1116 }
1117 
1118 static void
1119 qeth_l3_add_mc_to_hash(struct qeth_card *card, struct in_device *in4_dev)
1120 {
1121         struct ip_mc_list *im4;
1122         struct qeth_ipaddr *tmp, *ipm;
1123 
1124         QETH_CARD_TEXT(card, 4, "addmc");
1125 
1126         tmp = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
1127         if (!tmp)
1128                 return;
1129 
1130         for (im4 = rcu_dereference(in4_dev->mc_list); im4 != NULL;
1131              im4 = rcu_dereference(im4->next_rcu)) {
1132                 ip_eth_mc_map(im4->multiaddr, tmp->mac);
1133                 tmp->u.a4.addr = be32_to_cpu(im4->multiaddr);
1134                 tmp->is_multicast = 1;
1135 
1136                 ipm = qeth_l3_find_addr_by_ip(card, tmp);
1137                 if (ipm) {
1138                         /* for mcast, by-IP match means full match */
1139                         ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
1140                 } else {
1141                         ipm = qeth_l3_get_addr_buffer(QETH_PROT_IPV4);
1142                         if (!ipm)
1143                                 continue;
1144                         ether_addr_copy(ipm->mac, tmp->mac);
1145                         ipm->u.a4.addr = be32_to_cpu(im4->multiaddr);
1146                         ipm->is_multicast = 1;
1147                         ipm->disp_flag = QETH_DISP_ADDR_ADD;
1148                         hash_add(card->ip_mc_htable,
1149                                         &ipm->hnode, qeth_l3_ipaddr_hash(ipm));
1150                 }
1151         }
1152 
1153         kfree(tmp);
1154 }
1155 
1156 /* called with rcu_read_lock */
1157 static void qeth_l3_add_vlan_mc(struct qeth_card *card)
1158 {
1159         struct in_device *in_dev;
1160         u16 vid;
1161 
1162         QETH_CARD_TEXT(card, 4, "addmcvl");
1163 
1164         if (!qeth_is_supported(card, IPA_FULL_VLAN))
1165                 return;
1166 
1167         for_each_set_bit(vid, card->active_vlans, VLAN_N_VID) {
1168                 struct net_device *netdev;
1169 
1170                 netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q),
1171                                               vid);
1172                 if (netdev == NULL ||
1173                     !(netdev->flags & IFF_UP))
1174                         continue;
1175                 in_dev = __in_dev_get_rcu(netdev);
1176                 if (!in_dev)
1177                         continue;
1178                 qeth_l3_add_mc_to_hash(card, in_dev);
1179         }
1180 }
1181 
1182 static void qeth_l3_add_multicast_ipv4(struct qeth_card *card)
1183 {
1184         struct in_device *in4_dev;
1185 
1186         QETH_CARD_TEXT(card, 4, "chkmcv4");
1187 
1188         rcu_read_lock();
1189         in4_dev = __in_dev_get_rcu(card->dev);
1190         if (in4_dev == NULL)
1191                 goto unlock;
1192         qeth_l3_add_mc_to_hash(card, in4_dev);
1193         qeth_l3_add_vlan_mc(card);
1194 unlock:
1195         rcu_read_unlock();
1196 }
1197 
1198 static void qeth_l3_add_mc6_to_hash(struct qeth_card *card,
1199                                     struct inet6_dev *in6_dev)
1200 {
1201         struct qeth_ipaddr *ipm;
1202         struct ifmcaddr6 *im6;
1203         struct qeth_ipaddr *tmp;
1204 
1205         QETH_CARD_TEXT(card, 4, "addmc6");
1206 
1207         tmp = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
1208         if (!tmp)
1209                 return;
1210 
1211         for (im6 = in6_dev->mc_list; im6 != NULL; im6 = im6->next) {
1212                 ipv6_eth_mc_map(&im6->mca_addr, tmp->mac);
1213                 memcpy(&tmp->u.a6.addr, &im6->mca_addr.s6_addr,
1214                        sizeof(struct in6_addr));
1215                 tmp->is_multicast = 1;
1216 
1217                 ipm = qeth_l3_find_addr_by_ip(card, tmp);
1218                 if (ipm) {
1219                         /* for mcast, by-IP match means full match */
1220                         ipm->disp_flag = QETH_DISP_ADDR_DO_NOTHING;
1221                         continue;
1222                 }
1223 
1224                 ipm = qeth_l3_get_addr_buffer(QETH_PROT_IPV6);
1225                 if (!ipm)
1226                         continue;
1227 
1228                 ether_addr_copy(ipm->mac, tmp->mac);
1229                 memcpy(&ipm->u.a6.addr, &im6->mca_addr.s6_addr,
1230                        sizeof(struct in6_addr));
1231                 ipm->is_multicast = 1;
1232                 ipm->disp_flag = QETH_DISP_ADDR_ADD;
1233                 hash_add(card->ip_mc_htable,
1234                                 &ipm->hnode, qeth_l3_ipaddr_hash(ipm));
1235 
1236         }
1237         kfree(tmp);
1238 }
1239 
1240 /* called with rcu_read_lock */
1241 static void qeth_l3_add_vlan_mc6(struct qeth_card *card)
1242 {
1243         struct inet6_dev *in_dev;
1244         u16 vid;
1245 
1246         QETH_CARD_TEXT(card, 4, "admc6vl");
1247 
1248         if (!qeth_is_supported(card, IPA_FULL_VLAN))
1249                 return;
1250 
1251         for_each_set_bit(vid, card->active_vlans, VLAN_N_VID) {
1252                 struct net_device *netdev;
1253 
1254                 netdev = __vlan_find_dev_deep_rcu(card->dev, htons(ETH_P_8021Q),
1255                                               vid);
1256                 if (netdev == NULL ||
1257                     !(netdev->flags & IFF_UP))
1258                         continue;
1259                 in_dev = in6_dev_get(netdev);
1260                 if (!in_dev)
1261                         continue;
1262                 read_lock_bh(&in_dev->lock);
1263                 qeth_l3_add_mc6_to_hash(card, in_dev);
1264                 read_unlock_bh(&in_dev->lock);
1265                 in6_dev_put(in_dev);
1266         }
1267 }
1268 
1269 static void qeth_l3_add_multicast_ipv6(struct qeth_card *card)
1270 {
1271         struct inet6_dev *in6_dev;
1272 
1273         QETH_CARD_TEXT(card, 4, "chkmcv6");
1274 
1275         if (!qeth_is_supported(card, IPA_IPV6))
1276                 return ;
1277         in6_dev = in6_dev_get(card->dev);
1278         if (!in6_dev)
1279                 return;
1280 
1281         rcu_read_lock();
1282         read_lock_bh(&in6_dev->lock);
1283         qeth_l3_add_mc6_to_hash(card, in6_dev);
1284         qeth_l3_add_vlan_mc6(card);
1285         read_unlock_bh(&in6_dev->lock);
1286         rcu_read_unlock();
1287         in6_dev_put(in6_dev);
1288 }
1289 
1290 static int qeth_l3_vlan_rx_add_vid(struct net_device *dev,
1291                                    __be16 proto, u16 vid)
1292 {
1293         struct qeth_card *card = dev->ml_priv;
1294 
1295         set_bit(vid, card->active_vlans);
1296         return 0;
1297 }
1298 
1299 static int qeth_l3_vlan_rx_kill_vid(struct net_device *dev,
1300                                     __be16 proto, u16 vid)
1301 {
1302         struct qeth_card *card = dev->ml_priv;
1303 
1304         QETH_CARD_TEXT_(card, 4, "kid:%d", vid);
1305 
1306         clear_bit(vid, card->active_vlans);
1307         qeth_l3_set_rx_mode(dev);
1308         return 0;
1309 }
1310 
1311 static void qeth_l3_rebuild_skb(struct qeth_card *card, struct sk_buff *skb,
1312                                 struct qeth_hdr *hdr)
1313 {
1314         struct af_iucv_trans_hdr *iucv = (struct af_iucv_trans_hdr *) skb->data;
1315         struct net_device *dev = skb->dev;
1316 
1317         if (IS_IQD(card) && iucv->magic == ETH_P_AF_IUCV) {
1318                 dev_hard_header(skb, dev, ETH_P_AF_IUCV, dev->dev_addr,
1319                                 "FAKELL", skb->len);
1320                 return;
1321         }
1322 
1323         if (!(hdr->hdr.l3.flags & QETH_HDR_PASSTHRU)) {
1324                 u16 prot = (hdr->hdr.l3.flags & QETH_HDR_IPV6) ? ETH_P_IPV6 :
1325                                                                  ETH_P_IP;
1326                 unsigned char tg_addr[ETH_ALEN];
1327 
1328                 skb_reset_network_header(skb);
1329                 switch (hdr->hdr.l3.flags & QETH_HDR_CAST_MASK) {
1330                 case QETH_CAST_MULTICAST:
1331                         if (prot == ETH_P_IP)
1332                                 ip_eth_mc_map(ip_hdr(skb)->daddr, tg_addr);
1333                         else
1334                                 ipv6_eth_mc_map(&ipv6_hdr(skb)->daddr, tg_addr);
1335                         QETH_CARD_STAT_INC(card, rx_multicast);
1336                         break;
1337                 case QETH_CAST_BROADCAST:
1338                         ether_addr_copy(tg_addr, card->dev->broadcast);
1339                         QETH_CARD_STAT_INC(card, rx_multicast);
1340                         break;
1341                 default:
1342                         if (card->options.sniffer)
1343                                 skb->pkt_type = PACKET_OTHERHOST;
1344                         ether_addr_copy(tg_addr, card->dev->dev_addr);
1345                 }
1346 
1347                 if (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_SRC_MAC_ADDR)
1348                         card->dev->header_ops->create(skb, card->dev, prot,
1349                                 tg_addr, &hdr->hdr.l3.next_hop.rx.src_mac,
1350                                 skb->len);
1351                 else
1352                         card->dev->header_ops->create(skb, card->dev, prot,
1353                                 tg_addr, "FAKELL", skb->len);
1354         }
1355 
1356         /* copy VLAN tag from hdr into skb */
1357         if (!card->options.sniffer &&
1358             (hdr->hdr.l3.ext_flags & (QETH_HDR_EXT_VLAN_FRAME |
1359                                       QETH_HDR_EXT_INCLUDE_VLAN_TAG))) {
1360                 u16 tag = (hdr->hdr.l3.ext_flags & QETH_HDR_EXT_VLAN_FRAME) ?
1361                                 hdr->hdr.l3.vlan_id :
1362                                 hdr->hdr.l3.next_hop.rx.vlan_id;
1363                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), tag);
1364         }
1365 
1366         qeth_rx_csum(card, skb, hdr->hdr.l3.ext_flags);
1367 }
1368 
1369 static int qeth_l3_process_inbound_buffer(struct qeth_card *card,
1370                                 int budget, int *done)
1371 {
1372         int work_done = 0;
1373         struct sk_buff *skb;
1374         struct qeth_hdr *hdr;
1375         unsigned int len;
1376 
1377         *done = 0;
1378         WARN_ON_ONCE(!budget);
1379         while (budget) {
1380                 skb = qeth_core_get_next_skb(card,
1381                         &card->qdio.in_q->bufs[card->rx.b_index],
1382                         &card->rx.b_element, &card->rx.e_offset, &hdr);
1383                 if (!skb) {
1384                         *done = 1;
1385                         break;
1386                 }
1387                 switch (hdr->hdr.l3.id) {
1388                 case QETH_HEADER_TYPE_LAYER3:
1389                         qeth_l3_rebuild_skb(card, skb, hdr);
1390                         /* fall through */
1391                 case QETH_HEADER_TYPE_LAYER2: /* for HiperSockets sniffer */
1392                         skb->protocol = eth_type_trans(skb, skb->dev);
1393                         len = skb->len;
1394                         napi_gro_receive(&card->napi, skb);
1395                         break;
1396                 default:
1397                         dev_kfree_skb_any(skb);
1398                         QETH_CARD_TEXT(card, 3, "inbunkno");
1399                         QETH_DBF_HEX(CTRL, 3, hdr, sizeof(*hdr));
1400                         continue;
1401                 }
1402                 work_done++;
1403                 budget--;
1404                 QETH_CARD_STAT_INC(card, rx_packets);
1405                 QETH_CARD_STAT_ADD(card, rx_bytes, len);
1406         }
1407         return work_done;
1408 }
1409 
1410 static void qeth_l3_stop_card(struct qeth_card *card)
1411 {
1412         QETH_CARD_TEXT(card, 2, "stopcard");
1413 
1414         qeth_set_allowed_threads(card, 0, 1);
1415 
1416         cancel_work_sync(&card->rx_mode_work);
1417         qeth_l3_drain_rx_mode_cache(card);
1418 
1419         if (card->options.sniffer &&
1420             (card->info.promisc_mode == SET_PROMISC_MODE_ON))
1421                 qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
1422 
1423         if (card->state == CARD_STATE_SOFTSETUP) {
1424                 qeth_l3_clear_ip_htable(card, 1);
1425                 qeth_clear_ipacmd_list(card);
1426                 card->state = CARD_STATE_HARDSETUP;
1427         }
1428         if (card->state == CARD_STATE_HARDSETUP) {
1429                 qeth_drain_output_queues(card);
1430                 qeth_clear_working_pool_list(card);
1431                 card->state = CARD_STATE_DOWN;
1432         }
1433 
1434         qeth_qdio_clear_card(card, 0);
1435         flush_workqueue(card->event_wq);
1436         card->info.promisc_mode = 0;
1437 }
1438 
1439 static void qeth_l3_set_promisc_mode(struct qeth_card *card)
1440 {
1441         bool enable = card->dev->flags & IFF_PROMISC;
1442 
1443         if (card->info.promisc_mode == enable)
1444                 return;
1445 
1446         if (IS_VM_NIC(card)) {          /* Guestlan trace */
1447                 if (qeth_adp_supported(card, IPA_SETADP_SET_PROMISC_MODE))
1448                         qeth_setadp_promisc_mode(card, enable);
1449         } else if (card->options.sniffer &&     /* HiperSockets trace */
1450                    qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) {
1451                 if (enable) {
1452                         QETH_CARD_TEXT(card, 3, "+promisc");
1453                         qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_ENABLE);
1454                 } else {
1455                         QETH_CARD_TEXT(card, 3, "-promisc");
1456                         qeth_diags_trace(card, QETH_DIAGS_CMD_TRACE_DISABLE);
1457                 }
1458         }
1459 }
1460 
1461 static void qeth_l3_rx_mode_work(struct work_struct *work)
1462 {
1463         struct qeth_card *card = container_of(work, struct qeth_card,
1464                                               rx_mode_work);
1465         struct qeth_ipaddr *addr;
1466         struct hlist_node *tmp;
1467         int i, rc;
1468 
1469         QETH_CARD_TEXT(card, 3, "setmulti");
1470 
1471         if (!card->options.sniffer) {
1472                 qeth_l3_add_multicast_ipv4(card);
1473                 qeth_l3_add_multicast_ipv6(card);
1474 
1475                 hash_for_each_safe(card->ip_mc_htable, i, tmp, addr, hnode) {
1476                         switch (addr->disp_flag) {
1477                         case QETH_DISP_ADDR_DELETE:
1478                                 rc = qeth_l3_deregister_addr_entry(card, addr);
1479                                 if (!rc || rc == -ENOENT) {
1480                                         hash_del(&addr->hnode);
1481                                         kfree(addr);
1482                                 }
1483                                 break;
1484                         case QETH_DISP_ADDR_ADD:
1485                                 rc = qeth_l3_register_addr_entry(card, addr);
1486                                 if (rc && rc != -ENETDOWN) {
1487                                         hash_del(&addr->hnode);
1488                                         kfree(addr);
1489                                         break;
1490                                 }
1491                                 addr->ref_counter = 1;
1492                                 /* fall through */
1493                         default:
1494                                 /* for next call to set_rx_mode(): */
1495                                 addr->disp_flag = QETH_DISP_ADDR_DELETE;
1496                         }
1497                 }
1498         }
1499 
1500         qeth_l3_set_promisc_mode(card);
1501 }
1502 
1503 static int qeth_l3_arp_makerc(u16 rc)
1504 {
1505         switch (rc) {
1506         case IPA_RC_SUCCESS:
1507                 return 0;
1508         case QETH_IPA_ARP_RC_NOTSUPP:
1509         case QETH_IPA_ARP_RC_Q_NOTSUPP:
1510                 return -EOPNOTSUPP;
1511         case QETH_IPA_ARP_RC_OUT_OF_RANGE:
1512                 return -EINVAL;
1513         case QETH_IPA_ARP_RC_Q_NO_DATA:
1514                 return -ENOENT;
1515         default:
1516                 return -EIO;
1517         }
1518 }
1519 
1520 static int qeth_l3_arp_cmd_cb(struct qeth_card *card, struct qeth_reply *reply,
1521                               unsigned long data)
1522 {
1523         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
1524 
1525         qeth_setassparms_cb(card, reply, data);
1526         return qeth_l3_arp_makerc(cmd->hdr.return_code);
1527 }
1528 
1529 static int qeth_l3_arp_set_no_entries(struct qeth_card *card, int no_entries)
1530 {
1531         struct qeth_cmd_buffer *iob;
1532         int rc;
1533 
1534         QETH_CARD_TEXT(card, 3, "arpstnoe");
1535 
1536         /*
1537          * currently GuestLAN only supports the ARP assist function
1538          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_SET_NO_ENTRIES;
1539          * thus we say EOPNOTSUPP for this ARP function
1540          */
1541         if (IS_VM_NIC(card))
1542                 return -EOPNOTSUPP;
1543         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1544                 return -EOPNOTSUPP;
1545         }
1546 
1547         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1548                                        IPA_CMD_ASS_ARP_SET_NO_ENTRIES,
1549                                        SETASS_DATA_SIZEOF(flags_32bit),
1550                                        QETH_PROT_IPV4);
1551         if (!iob)
1552                 return -ENOMEM;
1553 
1554         __ipa_cmd(iob)->data.setassparms.data.flags_32bit = (u32) no_entries;
1555         rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL);
1556         if (rc)
1557                 QETH_DBF_MESSAGE(2, "Could not set number of ARP entries on device %x: %#x\n",
1558                                  CARD_DEVID(card), rc);
1559         return rc;
1560 }
1561 
1562 static __u32 get_arp_entry_size(struct qeth_card *card,
1563                         struct qeth_arp_query_data *qdata,
1564                         struct qeth_arp_entrytype *type, __u8 strip_entries)
1565 {
1566         __u32 rc;
1567         __u8 is_hsi;
1568 
1569         is_hsi = qdata->reply_bits == 5;
1570         if (type->ip == QETHARP_IP_ADDR_V4) {
1571                 QETH_CARD_TEXT(card, 4, "arpev4");
1572                 if (strip_entries) {
1573                         rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5_short) :
1574                                 sizeof(struct qeth_arp_qi_entry7_short);
1575                 } else {
1576                         rc = is_hsi ? sizeof(struct qeth_arp_qi_entry5) :
1577                                 sizeof(struct qeth_arp_qi_entry7);
1578                 }
1579         } else if (type->ip == QETHARP_IP_ADDR_V6) {
1580                 QETH_CARD_TEXT(card, 4, "arpev6");
1581                 if (strip_entries) {
1582                         rc = is_hsi ?
1583                                 sizeof(struct qeth_arp_qi_entry5_short_ipv6) :
1584                                 sizeof(struct qeth_arp_qi_entry7_short_ipv6);
1585                 } else {
1586                         rc = is_hsi ?
1587                                 sizeof(struct qeth_arp_qi_entry5_ipv6) :
1588                                 sizeof(struct qeth_arp_qi_entry7_ipv6);
1589                 }
1590         } else {
1591                 QETH_CARD_TEXT(card, 4, "arpinv");
1592                 rc = 0;
1593         }
1594 
1595         return rc;
1596 }
1597 
1598 static int arpentry_matches_prot(struct qeth_arp_entrytype *type, __u16 prot)
1599 {
1600         return (type->ip == QETHARP_IP_ADDR_V4 && prot == QETH_PROT_IPV4) ||
1601                 (type->ip == QETHARP_IP_ADDR_V6 && prot == QETH_PROT_IPV6);
1602 }
1603 
1604 static int qeth_l3_arp_query_cb(struct qeth_card *card,
1605                 struct qeth_reply *reply, unsigned long data)
1606 {
1607         struct qeth_ipa_cmd *cmd;
1608         struct qeth_arp_query_data *qdata;
1609         struct qeth_arp_query_info *qinfo;
1610         int e;
1611         int entrybytes_done;
1612         int stripped_bytes;
1613         __u8 do_strip_entries;
1614 
1615         QETH_CARD_TEXT(card, 3, "arpquecb");
1616 
1617         qinfo = (struct qeth_arp_query_info *) reply->param;
1618         cmd = (struct qeth_ipa_cmd *) data;
1619         QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.prot_version);
1620         if (cmd->hdr.return_code) {
1621                 QETH_CARD_TEXT(card, 4, "arpcberr");
1622                 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1623                 return qeth_l3_arp_makerc(cmd->hdr.return_code);
1624         }
1625         if (cmd->data.setassparms.hdr.return_code) {
1626                 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
1627                 QETH_CARD_TEXT(card, 4, "setaperr");
1628                 QETH_CARD_TEXT_(card, 4, "%i", cmd->hdr.return_code);
1629                 return qeth_l3_arp_makerc(cmd->hdr.return_code);
1630         }
1631         qdata = &cmd->data.setassparms.data.query_arp;
1632         QETH_CARD_TEXT_(card, 4, "anoen%i", qdata->no_entries);
1633 
1634         do_strip_entries = (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES) > 0;
1635         stripped_bytes = do_strip_entries ? QETH_QARP_MEDIASPECIFIC_BYTES : 0;
1636         entrybytes_done = 0;
1637         for (e = 0; e < qdata->no_entries; ++e) {
1638                 char *cur_entry;
1639                 __u32 esize;
1640                 struct qeth_arp_entrytype *etype;
1641 
1642                 cur_entry = &qdata->data + entrybytes_done;
1643                 etype = &((struct qeth_arp_qi_entry5 *) cur_entry)->type;
1644                 if (!arpentry_matches_prot(etype, cmd->hdr.prot_version)) {
1645                         QETH_CARD_TEXT(card, 4, "pmis");
1646                         QETH_CARD_TEXT_(card, 4, "%i", etype->ip);
1647                         break;
1648                 }
1649                 esize = get_arp_entry_size(card, qdata, etype,
1650                         do_strip_entries);
1651                 QETH_CARD_TEXT_(card, 5, "esz%i", esize);
1652                 if (!esize)
1653                         break;
1654 
1655                 if ((qinfo->udata_len - qinfo->udata_offset) < esize) {
1656                         QETH_CARD_TEXT_(card, 4, "qaer3%i", -ENOSPC);
1657                         memset(qinfo->udata, 0, 4);
1658                         return -ENOSPC;
1659                 }
1660 
1661                 memcpy(qinfo->udata + qinfo->udata_offset,
1662                         &qdata->data + entrybytes_done + stripped_bytes,
1663                         esize);
1664                 entrybytes_done += esize + stripped_bytes;
1665                 qinfo->udata_offset += esize;
1666                 ++qinfo->no_entries;
1667         }
1668         /* check if all replies received ... */
1669         if (cmd->data.setassparms.hdr.seq_no <
1670             cmd->data.setassparms.hdr.number_of_replies)
1671                 return 1;
1672         QETH_CARD_TEXT_(card, 4, "nove%i", qinfo->no_entries);
1673         memcpy(qinfo->udata, &qinfo->no_entries, 4);
1674         /* keep STRIP_ENTRIES flag so the user program can distinguish
1675          * stripped entries from normal ones */
1676         if (qinfo->mask_bits & QETH_QARP_STRIP_ENTRIES)
1677                 qdata->reply_bits |= QETH_QARP_STRIP_ENTRIES;
1678         memcpy(qinfo->udata + QETH_QARP_MASK_OFFSET, &qdata->reply_bits, 2);
1679         QETH_CARD_TEXT_(card, 4, "rc%i", 0);
1680         return 0;
1681 }
1682 
1683 static int qeth_l3_query_arp_cache_info(struct qeth_card *card,
1684         enum qeth_prot_versions prot,
1685         struct qeth_arp_query_info *qinfo)
1686 {
1687         struct qeth_cmd_buffer *iob;
1688         struct qeth_ipa_cmd *cmd;
1689         int rc;
1690 
1691         QETH_CARD_TEXT_(card, 3, "qarpipv%i", prot);
1692 
1693         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1694                                        IPA_CMD_ASS_ARP_QUERY_INFO,
1695                                        SETASS_DATA_SIZEOF(query_arp), prot);
1696         if (!iob)
1697                 return -ENOMEM;
1698         cmd = __ipa_cmd(iob);
1699         cmd->data.setassparms.data.query_arp.request_bits = 0x000F;
1700         rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_query_cb, qinfo);
1701         if (rc)
1702                 QETH_DBF_MESSAGE(2, "Error while querying ARP cache on device %x: %#x\n",
1703                                  CARD_DEVID(card), rc);
1704         return rc;
1705 }
1706 
1707 static int qeth_l3_arp_query(struct qeth_card *card, char __user *udata)
1708 {
1709         struct qeth_arp_query_info qinfo = {0, };
1710         int rc;
1711 
1712         QETH_CARD_TEXT(card, 3, "arpquery");
1713 
1714         if (!qeth_is_supported(card,/*IPA_QUERY_ARP_ADDR_INFO*/
1715                                IPA_ARP_PROCESSING)) {
1716                 QETH_CARD_TEXT(card, 3, "arpqnsup");
1717                 rc = -EOPNOTSUPP;
1718                 goto out;
1719         }
1720         /* get size of userspace buffer and mask_bits -> 6 bytes */
1721         if (copy_from_user(&qinfo, udata, 6)) {
1722                 rc = -EFAULT;
1723                 goto out;
1724         }
1725         qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
1726         if (!qinfo.udata) {
1727                 rc = -ENOMEM;
1728                 goto out;
1729         }
1730         qinfo.udata_offset = QETH_QARP_ENTRIES_OFFSET;
1731         rc = qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV4, &qinfo);
1732         if (rc) {
1733                 if (copy_to_user(udata, qinfo.udata, 4))
1734                         rc = -EFAULT;
1735                 goto free_and_out;
1736         }
1737         if (qinfo.mask_bits & QETH_QARP_WITH_IPV6) {
1738                 /* fails in case of GuestLAN QDIO mode */
1739                 qeth_l3_query_arp_cache_info(card, QETH_PROT_IPV6, &qinfo);
1740         }
1741         if (copy_to_user(udata, qinfo.udata, qinfo.udata_len)) {
1742                 QETH_CARD_TEXT(card, 4, "qactf");
1743                 rc = -EFAULT;
1744                 goto free_and_out;
1745         }
1746         QETH_CARD_TEXT(card, 4, "qacts");
1747 
1748 free_and_out:
1749         kfree(qinfo.udata);
1750 out:
1751         return rc;
1752 }
1753 
1754 static int qeth_l3_arp_modify_entry(struct qeth_card *card,
1755                                     struct qeth_arp_cache_entry *entry,
1756                                     enum qeth_arp_process_subcmds arp_cmd)
1757 {
1758         struct qeth_arp_cache_entry *cmd_entry;
1759         struct qeth_cmd_buffer *iob;
1760         int rc;
1761 
1762         if (arp_cmd == IPA_CMD_ASS_ARP_ADD_ENTRY)
1763                 QETH_CARD_TEXT(card, 3, "arpadd");
1764         else
1765                 QETH_CARD_TEXT(card, 3, "arpdel");
1766 
1767         /*
1768          * currently GuestLAN only supports the ARP assist function
1769          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_ADD_ENTRY;
1770          * thus we say EOPNOTSUPP for this ARP function
1771          */
1772         if (IS_VM_NIC(card))
1773                 return -EOPNOTSUPP;
1774         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1775                 return -EOPNOTSUPP;
1776         }
1777 
1778         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING, arp_cmd,
1779                                        SETASS_DATA_SIZEOF(arp_entry),
1780                                        QETH_PROT_IPV4);
1781         if (!iob)
1782                 return -ENOMEM;
1783 
1784         cmd_entry = &__ipa_cmd(iob)->data.setassparms.data.arp_entry;
1785         ether_addr_copy(cmd_entry->macaddr, entry->macaddr);
1786         memcpy(cmd_entry->ipaddr, entry->ipaddr, 4);
1787         rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL);
1788         if (rc)
1789                 QETH_DBF_MESSAGE(2, "Could not modify (cmd: %#x) ARP entry on device %x: %#x\n",
1790                                  arp_cmd, CARD_DEVID(card), rc);
1791         return rc;
1792 }
1793 
1794 static int qeth_l3_arp_flush_cache(struct qeth_card *card)
1795 {
1796         struct qeth_cmd_buffer *iob;
1797         int rc;
1798 
1799         QETH_CARD_TEXT(card, 3, "arpflush");
1800 
1801         /*
1802          * currently GuestLAN only supports the ARP assist function
1803          * IPA_CMD_ASS_ARP_QUERY_INFO, but not IPA_CMD_ASS_ARP_FLUSH_CACHE;
1804          * thus we say EOPNOTSUPP for this ARP function
1805         */
1806         if (IS_VM_NIC(card) || IS_IQD(card))
1807                 return -EOPNOTSUPP;
1808         if (!qeth_is_supported(card, IPA_ARP_PROCESSING)) {
1809                 return -EOPNOTSUPP;
1810         }
1811 
1812         iob = qeth_get_setassparms_cmd(card, IPA_ARP_PROCESSING,
1813                                        IPA_CMD_ASS_ARP_FLUSH_CACHE, 0,
1814                                        QETH_PROT_IPV4);
1815         if (!iob)
1816                 return -ENOMEM;
1817 
1818         rc = qeth_send_ipa_cmd(card, iob, qeth_l3_arp_cmd_cb, NULL);
1819         if (rc)
1820                 QETH_DBF_MESSAGE(2, "Could not flush ARP cache on device %x: %#x\n",
1821                                  CARD_DEVID(card), rc);
1822         return rc;
1823 }
1824 
1825 static int qeth_l3_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1826 {
1827         struct qeth_card *card = dev->ml_priv;
1828         struct qeth_arp_cache_entry arp_entry;
1829         enum qeth_arp_process_subcmds arp_cmd;
1830         int rc = 0;
1831 
1832         switch (cmd) {
1833         case SIOC_QETH_ARP_SET_NO_ENTRIES:
1834                 if (!capable(CAP_NET_ADMIN)) {
1835                         rc = -EPERM;
1836                         break;
1837                 }
1838                 rc = qeth_l3_arp_set_no_entries(card, rq->ifr_ifru.ifru_ivalue);
1839                 break;
1840         case SIOC_QETH_ARP_QUERY_INFO:
1841                 if (!capable(CAP_NET_ADMIN)) {
1842                         rc = -EPERM;
1843                         break;
1844                 }
1845                 rc = qeth_l3_arp_query(card, rq->ifr_ifru.ifru_data);
1846                 break;
1847         case SIOC_QETH_ARP_ADD_ENTRY:
1848         case SIOC_QETH_ARP_REMOVE_ENTRY:
1849                 if (!capable(CAP_NET_ADMIN))
1850                         return -EPERM;
1851                 if (copy_from_user(&arp_entry, rq->ifr_data, sizeof(arp_entry)))
1852                         return -EFAULT;
1853 
1854                 arp_cmd = (cmd == SIOC_QETH_ARP_ADD_ENTRY) ?
1855                                 IPA_CMD_ASS_ARP_ADD_ENTRY :
1856                                 IPA_CMD_ASS_ARP_REMOVE_ENTRY;
1857                 return qeth_l3_arp_modify_entry(card, &arp_entry, arp_cmd);
1858         case SIOC_QETH_ARP_FLUSH_CACHE:
1859                 if (!capable(CAP_NET_ADMIN)) {
1860                         rc = -EPERM;
1861                         break;
1862                 }
1863                 rc = qeth_l3_arp_flush_cache(card);
1864                 break;
1865         default:
1866                 rc = -EOPNOTSUPP;
1867         }
1868         return rc;
1869 }
1870 
1871 static int qeth_l3_get_cast_type_rcu(struct sk_buff *skb, struct dst_entry *dst,
1872                                      int ipv)
1873 {
1874         struct neighbour *n = NULL;
1875 
1876         if (dst)
1877                 n = dst_neigh_lookup_skb(dst, skb);
1878 
1879         if (n) {
1880                 int cast_type = n->type;
1881 
1882                 neigh_release(n);
1883                 if ((cast_type == RTN_BROADCAST) ||
1884                     (cast_type == RTN_MULTICAST) ||
1885                     (cast_type == RTN_ANYCAST))
1886                         return cast_type;
1887                 return RTN_UNICAST;
1888         }
1889 
1890         /* no neighbour (eg AF_PACKET), fall back to target's IP address ... */
1891         switch (ipv) {
1892         case 4:
1893                 if (ipv4_is_lbcast(ip_hdr(skb)->daddr))
1894                         return RTN_BROADCAST;
1895                 return ipv4_is_multicast(ip_hdr(skb)->daddr) ?
1896                                 RTN_MULTICAST : RTN_UNICAST;
1897         case 6:
1898                 return ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) ?
1899                                 RTN_MULTICAST : RTN_UNICAST;
1900         default:
1901                 /* ... and MAC address */
1902                 return qeth_get_ether_cast_type(skb);
1903         }
1904 }
1905 
1906 static int qeth_l3_get_cast_type(struct sk_buff *skb)
1907 {
1908         int ipv = qeth_get_ip_version(skb);
1909         struct dst_entry *dst;
1910         int cast_type;
1911 
1912         rcu_read_lock();
1913         dst = qeth_dst_check_rcu(skb, ipv);
1914         cast_type = qeth_l3_get_cast_type_rcu(skb, dst, ipv);
1915         rcu_read_unlock();
1916 
1917         return cast_type;
1918 }
1919 
1920 static u8 qeth_l3_cast_type_to_flag(int cast_type)
1921 {
1922         if (cast_type == RTN_MULTICAST)
1923                 return QETH_CAST_MULTICAST;
1924         if (cast_type == RTN_ANYCAST)
1925                 return QETH_CAST_ANYCAST;
1926         if (cast_type == RTN_BROADCAST)
1927                 return QETH_CAST_BROADCAST;
1928         return QETH_CAST_UNICAST;
1929 }
1930 
1931 static void qeth_l3_fill_header(struct qeth_qdio_out_q *queue,
1932                                 struct qeth_hdr *hdr, struct sk_buff *skb,
1933                                 int ipv, unsigned int data_len)
1934 {
1935         struct qeth_hdr_layer3 *l3_hdr = &hdr->hdr.l3;
1936         struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
1937         struct qeth_card *card = queue->card;
1938         struct dst_entry *dst;
1939         int cast_type;
1940 
1941         hdr->hdr.l3.length = data_len;
1942 
1943         if (skb_is_gso(skb)) {
1944                 hdr->hdr.l3.id = QETH_HEADER_TYPE_L3_TSO;
1945         } else {
1946                 hdr->hdr.l3.id = QETH_HEADER_TYPE_LAYER3;
1947 
1948                 if (skb->protocol == htons(ETH_P_AF_IUCV)) {
1949                         l3_hdr->flags = QETH_HDR_IPV6 | QETH_CAST_UNICAST;
1950                         l3_hdr->next_hop.ipv6_addr.s6_addr16[0] = htons(0xfe80);
1951                         memcpy(&l3_hdr->next_hop.ipv6_addr.s6_addr32[2],
1952                                iucv_trans_hdr(skb)->destUserID, 8);
1953                         return;
1954                 }
1955 
1956                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1957                         qeth_tx_csum(skb, &hdr->hdr.l3.ext_flags, ipv);
1958                         /* some HW requires combined L3+L4 csum offload: */
1959                         if (ipv == 4)
1960                                 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_CSUM_HDR_REQ;
1961                 }
1962         }
1963 
1964         if (ipv == 4 || IS_IQD(card)) {
1965                 /* NETIF_F_HW_VLAN_CTAG_TX */
1966                 if (skb_vlan_tag_present(skb)) {
1967                         hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_VLAN_FRAME;
1968                         hdr->hdr.l3.vlan_id = skb_vlan_tag_get(skb);
1969                 }
1970         } else if (veth->h_vlan_proto == htons(ETH_P_8021Q)) {
1971                 hdr->hdr.l3.ext_flags |= QETH_HDR_EXT_INCLUDE_VLAN_TAG;
1972                 hdr->hdr.l3.vlan_id = ntohs(veth->h_vlan_TCI);
1973         }
1974 
1975         rcu_read_lock();
1976         dst = qeth_dst_check_rcu(skb, ipv);
1977 
1978         if (IS_IQD(card) && skb_get_queue_mapping(skb) != QETH_IQD_MCAST_TXQ)
1979                 cast_type = RTN_UNICAST;
1980         else
1981                 cast_type = qeth_l3_get_cast_type_rcu(skb, dst, ipv);
1982         l3_hdr->flags |= qeth_l3_cast_type_to_flag(cast_type);
1983 
1984         if (ipv == 4) {
1985                 struct rtable *rt = (struct rtable *) dst;
1986 
1987                 *((__be32 *) &hdr->hdr.l3.next_hop.ipv4.addr) = (rt) ?
1988                                 rt_nexthop(rt, ip_hdr(skb)->daddr) :
1989                                 ip_hdr(skb)->daddr;
1990         } else if (ipv == 6) {
1991                 struct rt6_info *rt = (struct rt6_info *) dst;
1992 
1993                 if (rt && !ipv6_addr_any(&rt->rt6i_gateway))
1994                         l3_hdr->next_hop.ipv6_addr = rt->rt6i_gateway;
1995                 else
1996                         l3_hdr->next_hop.ipv6_addr = ipv6_hdr(skb)->daddr;
1997 
1998                 hdr->hdr.l3.flags |= QETH_HDR_IPV6;
1999                 if (!IS_IQD(card))
2000                         hdr->hdr.l3.flags |= QETH_HDR_PASSTHRU;
2001         } else {
2002                 /* OSA only: */
2003                 l3_hdr->flags |= QETH_HDR_PASSTHRU;
2004         }
2005         rcu_read_unlock();
2006 }
2007 
2008 static void qeth_l3_fixup_headers(struct sk_buff *skb)
2009 {
2010         struct iphdr *iph = ip_hdr(skb);
2011 
2012         /* this is safe, IPv6 traffic takes a different path */
2013         if (skb->ip_summed == CHECKSUM_PARTIAL)
2014                 iph->check = 0;
2015         if (skb_is_gso(skb)) {
2016                 iph->tot_len = 0;
2017                 tcp_hdr(skb)->check = ~tcp_v4_check(0, iph->saddr,
2018                                                     iph->daddr, 0);
2019         }
2020 }
2021 
2022 static int qeth_l3_xmit(struct qeth_card *card, struct sk_buff *skb,
2023                         struct qeth_qdio_out_q *queue, int ipv)
2024 {
2025         unsigned int hw_hdr_len;
2026         int rc;
2027 
2028         /* re-use the L2 header area for the HW header: */
2029         hw_hdr_len = skb_is_gso(skb) ? sizeof(struct qeth_hdr_tso) :
2030                                        sizeof(struct qeth_hdr);
2031         rc = skb_cow_head(skb, hw_hdr_len - ETH_HLEN);
2032         if (rc)
2033                 return rc;
2034         skb_pull(skb, ETH_HLEN);
2035 
2036         qeth_l3_fixup_headers(skb);
2037         return qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header);
2038 }
2039 
2040 static netdev_tx_t qeth_l3_hard_start_xmit(struct sk_buff *skb,
2041                                            struct net_device *dev)
2042 {
2043         struct qeth_card *card = dev->ml_priv;
2044         u16 txq = skb_get_queue_mapping(skb);
2045         int ipv = qeth_get_ip_version(skb);
2046         struct qeth_qdio_out_q *queue;
2047         int rc;
2048 
2049         if (!skb_is_gso(skb))
2050                 qdisc_skb_cb(skb)->pkt_len = skb->len;
2051         if (IS_IQD(card)) {
2052                 queue = card->qdio.out_qs[qeth_iqd_translate_txq(dev, txq)];
2053 
2054                 if (card->options.sniffer)
2055                         goto tx_drop;
2056                 if ((card->options.cq != QETH_CQ_ENABLED && !ipv) ||
2057                     (card->options.cq == QETH_CQ_ENABLED &&
2058                      skb->protocol != htons(ETH_P_AF_IUCV)))
2059                         goto tx_drop;
2060         } else {
2061                 queue = card->qdio.out_qs[txq];
2062         }
2063 
2064         if (!(dev->flags & IFF_BROADCAST) &&
2065             qeth_l3_get_cast_type(skb) == RTN_BROADCAST)
2066                 goto tx_drop;
2067 
2068         if (ipv == 4 || IS_IQD(card))
2069                 rc = qeth_l3_xmit(card, skb, queue, ipv);
2070         else
2071                 rc = qeth_xmit(card, skb, queue, ipv, qeth_l3_fill_header);
2072 
2073         if (!rc)
2074                 return NETDEV_TX_OK;
2075 
2076 tx_drop:
2077         QETH_TXQ_STAT_INC(queue, tx_dropped);
2078         kfree_skb(skb);
2079         return NETDEV_TX_OK;
2080 }
2081 
2082 static void qeth_l3_set_rx_mode(struct net_device *dev)
2083 {
2084         struct qeth_card *card = dev->ml_priv;
2085 
2086         schedule_work(&card->rx_mode_work);
2087 }
2088 
2089 /*
2090  * we need NOARP for IPv4 but we want neighbor solicitation for IPv6. Setting
2091  * NOARP on the netdevice is no option because it also turns off neighbor
2092  * solicitation. For IPv4 we install a neighbor_setup function. We don't want
2093  * arp resolution but we want the hard header (packet socket will work
2094  * e.g. tcpdump)
2095  */
2096 static int qeth_l3_neigh_setup_noarp(struct neighbour *n)
2097 {
2098         n->nud_state = NUD_NOARP;
2099         memcpy(n->ha, "FAKELL", 6);
2100         n->output = n->ops->connected_output;
2101         return 0;
2102 }
2103 
2104 static int
2105 qeth_l3_neigh_setup(struct net_device *dev, struct neigh_parms *np)
2106 {
2107         if (np->tbl->family == AF_INET)
2108                 np->neigh_setup = qeth_l3_neigh_setup_noarp;
2109 
2110         return 0;
2111 }
2112 
2113 static netdev_features_t qeth_l3_osa_features_check(struct sk_buff *skb,
2114                                                     struct net_device *dev,
2115                                                     netdev_features_t features)
2116 {
2117         if (qeth_get_ip_version(skb) != 4)
2118                 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
2119         return qeth_features_check(skb, dev, features);
2120 }
2121 
2122 static u16 qeth_l3_iqd_select_queue(struct net_device *dev, struct sk_buff *skb,
2123                                     struct net_device *sb_dev)
2124 {
2125         return qeth_iqd_select_queue(dev, skb, qeth_l3_get_cast_type(skb),
2126                                      sb_dev);
2127 }
2128 
2129 static u16 qeth_l3_osa_select_queue(struct net_device *dev, struct sk_buff *skb,
2130                                     struct net_device *sb_dev)
2131 {
2132         struct qeth_card *card = dev->ml_priv;
2133 
2134         return qeth_get_priority_queue(card, skb);
2135 }
2136 
2137 static const struct net_device_ops qeth_l3_netdev_ops = {
2138         .ndo_open               = qeth_open,
2139         .ndo_stop               = qeth_stop,
2140         .ndo_get_stats64        = qeth_get_stats64,
2141         .ndo_start_xmit         = qeth_l3_hard_start_xmit,
2142         .ndo_select_queue       = qeth_l3_iqd_select_queue,
2143         .ndo_validate_addr      = eth_validate_addr,
2144         .ndo_set_rx_mode        = qeth_l3_set_rx_mode,
2145         .ndo_do_ioctl           = qeth_do_ioctl,
2146         .ndo_fix_features       = qeth_fix_features,
2147         .ndo_set_features       = qeth_set_features,
2148         .ndo_vlan_rx_add_vid    = qeth_l3_vlan_rx_add_vid,
2149         .ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
2150         .ndo_tx_timeout         = qeth_tx_timeout,
2151 };
2152 
2153 static const struct net_device_ops qeth_l3_osa_netdev_ops = {
2154         .ndo_open               = qeth_open,
2155         .ndo_stop               = qeth_stop,
2156         .ndo_get_stats64        = qeth_get_stats64,
2157         .ndo_start_xmit         = qeth_l3_hard_start_xmit,
2158         .ndo_features_check     = qeth_l3_osa_features_check,
2159         .ndo_select_queue       = qeth_l3_osa_select_queue,
2160         .ndo_validate_addr      = eth_validate_addr,
2161         .ndo_set_rx_mode        = qeth_l3_set_rx_mode,
2162         .ndo_do_ioctl           = qeth_do_ioctl,
2163         .ndo_fix_features       = qeth_fix_features,
2164         .ndo_set_features       = qeth_set_features,
2165         .ndo_vlan_rx_add_vid    = qeth_l3_vlan_rx_add_vid,
2166         .ndo_vlan_rx_kill_vid   = qeth_l3_vlan_rx_kill_vid,
2167         .ndo_tx_timeout         = qeth_tx_timeout,
2168         .ndo_neigh_setup        = qeth_l3_neigh_setup,
2169 };
2170 
2171 static int qeth_l3_setup_netdev(struct qeth_card *card, bool carrier_ok)
2172 {
2173         unsigned int headroom;
2174         int rc;
2175 
2176         if (IS_OSD(card) || IS_OSX(card)) {
2177                 if ((card->info.link_type == QETH_LINK_TYPE_LANE_TR) ||
2178                     (card->info.link_type == QETH_LINK_TYPE_HSTR)) {
2179                         pr_info("qeth_l3: ignoring TR device\n");
2180                         return -ENODEV;
2181                 }
2182 
2183                 card->dev->netdev_ops = &qeth_l3_osa_netdev_ops;
2184 
2185                 /*IPv6 address autoconfiguration stuff*/
2186                 qeth_l3_get_unique_id(card);
2187                 if (!(card->info.unique_id & UNIQUE_ID_NOT_BY_CARD))
2188                         card->dev->dev_id = card->info.unique_id & 0xffff;
2189 
2190                 if (!IS_VM_NIC(card)) {
2191                         card->dev->features |= NETIF_F_SG;
2192                         card->dev->hw_features |= NETIF_F_TSO |
2193                                 NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
2194                         card->dev->vlan_features |= NETIF_F_TSO |
2195                                 NETIF_F_RXCSUM | NETIF_F_IP_CSUM;
2196                 }
2197 
2198                 if (qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6)) {
2199                         card->dev->hw_features |= NETIF_F_IPV6_CSUM;
2200                         card->dev->vlan_features |= NETIF_F_IPV6_CSUM;
2201                 }
2202                 if (qeth_is_supported6(card, IPA_OUTBOUND_TSO)) {
2203                         card->dev->hw_features |= NETIF_F_TSO6;
2204                         card->dev->vlan_features |= NETIF_F_TSO6;
2205                 }
2206 
2207                 /* allow for de-acceleration of NETIF_F_HW_VLAN_CTAG_TX: */
2208                 if (card->dev->hw_features & NETIF_F_TSO6)
2209                         headroom = sizeof(struct qeth_hdr_tso) + VLAN_HLEN;
2210                 else if (card->dev->hw_features & NETIF_F_TSO)
2211                         headroom = sizeof(struct qeth_hdr_tso);
2212                 else
2213                         headroom = sizeof(struct qeth_hdr) + VLAN_HLEN;
2214         } else if (IS_IQD(card)) {
2215                 card->dev->flags |= IFF_NOARP;
2216                 card->dev->netdev_ops = &qeth_l3_netdev_ops;
2217                 headroom = sizeof(struct qeth_hdr) - ETH_HLEN;
2218 
2219                 rc = qeth_l3_iqd_read_initial_mac(card);
2220                 if (rc)
2221                         goto out;
2222         } else
2223                 return -ENODEV;
2224 
2225         card->dev->needed_headroom = headroom;
2226         card->dev->features |=  NETIF_F_HW_VLAN_CTAG_TX |
2227                                 NETIF_F_HW_VLAN_CTAG_RX |
2228                                 NETIF_F_HW_VLAN_CTAG_FILTER;
2229 
2230         netif_keep_dst(card->dev);
2231         if (card->dev->hw_features & (NETIF_F_TSO | NETIF_F_TSO6))
2232                 netif_set_gso_max_size(card->dev,
2233                                        PAGE_SIZE * (QETH_MAX_BUFFER_ELEMENTS(card) - 1));
2234 
2235         netif_napi_add(card->dev, &card->napi, qeth_poll, QETH_NAPI_WEIGHT);
2236         rc = register_netdev(card->dev);
2237         if (!rc && carrier_ok)
2238                 netif_carrier_on(card->dev);
2239 
2240 out:
2241         if (rc)
2242                 card->dev->netdev_ops = NULL;
2243         return rc;
2244 }
2245 
2246 static const struct device_type qeth_l3_devtype = {
2247         .name = "qeth_layer3",
2248         .groups = qeth_l3_attr_groups,
2249 };
2250 
2251 static int qeth_l3_probe_device(struct ccwgroup_device *gdev)
2252 {
2253         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2254         int rc;
2255 
2256         hash_init(card->ip_htable);
2257         mutex_init(&card->ip_lock);
2258         card->cmd_wq = alloc_ordered_workqueue("%s_cmd", 0,
2259                                                dev_name(&gdev->dev));
2260         if (!card->cmd_wq)
2261                 return -ENOMEM;
2262 
2263         if (gdev->dev.type == &qeth_generic_devtype) {
2264                 rc = qeth_l3_create_device_attributes(&gdev->dev);
2265                 if (rc) {
2266                         destroy_workqueue(card->cmd_wq);
2267                         return rc;
2268                 }
2269         }
2270 
2271         hash_init(card->ip_mc_htable);
2272         INIT_WORK(&card->rx_mode_work, qeth_l3_rx_mode_work);
2273         return 0;
2274 }
2275 
2276 static void qeth_l3_remove_device(struct ccwgroup_device *cgdev)
2277 {
2278         struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
2279 
2280         if (cgdev->dev.type == &qeth_generic_devtype)
2281                 qeth_l3_remove_device_attributes(&cgdev->dev);
2282 
2283         qeth_set_allowed_threads(card, 0, 1);
2284         wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
2285 
2286         if (cgdev->state == CCWGROUP_ONLINE)
2287                 qeth_l3_set_offline(cgdev);
2288 
2289         cancel_work_sync(&card->close_dev_work);
2290         if (qeth_netdev_is_registered(card->dev))
2291                 unregister_netdev(card->dev);
2292 
2293         flush_workqueue(card->cmd_wq);
2294         destroy_workqueue(card->cmd_wq);
2295         qeth_l3_clear_ip_htable(card, 0);
2296         qeth_l3_clear_ipato_list(card);
2297 }
2298 
2299 static int qeth_l3_set_online(struct ccwgroup_device *gdev)
2300 {
2301         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
2302         struct net_device *dev = card->dev;
2303         int rc = 0;
2304         bool carrier_ok;
2305 
2306         mutex_lock(&card->discipline_mutex);
2307         mutex_lock(&card->conf_mutex);
2308         QETH_CARD_TEXT(card, 2, "setonlin");
2309 
2310         rc = qeth_core_hardsetup_card(card, &carrier_ok);
2311         if (rc) {
2312                 QETH_CARD_TEXT_(card, 2, "2err%04x", rc);
2313                 rc = -ENODEV;
2314                 goto out_remove;
2315         }
2316 
2317         if (qeth_is_diagass_supported(card, QETH_DIAGS_CMD_TRAP)) {
2318                 if (card->info.hwtrap &&
2319                     qeth_hw_trap(card, QETH_DIAGS_TRAP_ARM))
2320                         card->info.hwtrap = 0;
2321         } else
2322                 card->info.hwtrap = 0;
2323 
2324         card->state = CARD_STATE_HARDSETUP;
2325         qeth_print_status_message(card);
2326 
2327         /* softsetup */
2328         QETH_CARD_TEXT(card, 2, "softsetp");
2329 
2330         rc = qeth_l3_setadapter_parms(card);
2331         if (rc)
2332                 QETH_CARD_TEXT_(card, 2, "2err%04x", rc);
2333         if (!card->options.sniffer) {
2334                 rc = qeth_l3_start_ipassists(card);
2335                 if (rc) {
2336                         QETH_CARD_TEXT_(card, 2, "3err%d", rc);
2337                         goto out_remove;
2338                 }
2339                 rc = qeth_l3_setrouting_v4(card);
2340                 if (rc)
2341                         QETH_CARD_TEXT_(card, 2, "4err%04x", rc);
2342                 rc = qeth_l3_setrouting_v6(card);
2343                 if (rc)
2344                         QETH_CARD_TEXT_(card, 2, "5err%04x", rc);
2345         }
2346 
2347         rc = qeth_init_qdio_queues(card);
2348         if (rc) {
2349                 QETH_CARD_TEXT_(card, 2, "6err%d", rc);
2350                 rc = -ENODEV;
2351                 goto out_remove;
2352         }
2353         card->state = CARD_STATE_SOFTSETUP;
2354 
2355         qeth_set_allowed_threads(card, 0xffffffff, 0);
2356         qeth_l3_recover_ip(card);
2357 
2358         if (!qeth_netdev_is_registered(dev)) {
2359                 rc = qeth_l3_setup_netdev(card, carrier_ok);
2360                 if (rc)
2361                         goto out_remove;
2362         } else {
2363                 rtnl_lock();
2364                 if (carrier_ok)
2365                         netif_carrier_on(dev);
2366                 else
2367                         netif_carrier_off(dev);
2368 
2369                 netif_device_attach(dev);
2370                 qeth_enable_hw_features(dev);
2371 
2372                 if (card->info.open_when_online) {
2373                         card->info.open_when_online = 0;
2374                         dev_open(dev, NULL);
2375                 }
2376                 rtnl_unlock();
2377         }
2378         qeth_trace_features(card);
2379         /* let user_space know that device is online */
2380         kobject_uevent(&gdev->dev.kobj, KOBJ_CHANGE);
2381         mutex_unlock(&card->conf_mutex);
2382         mutex_unlock(&card->discipline_mutex);
2383         return 0;
2384 out_remove:
2385         qeth_l3_stop_card(card);
2386         qeth_stop_channel(&card->data);
2387         qeth_stop_channel(&card->write);
2388         qeth_stop_channel(&card->read);
2389         qdio_free(CARD_DDEV(card));
2390 
2391         mutex_unlock(&card->conf_mutex);
2392         mutex_unlock(&card->discipline_mutex);
2393         return rc;
2394 }
2395 
2396 static int __qeth_l3_set_offline(struct ccwgroup_device *cgdev,
2397                         int recovery_mode)
2398 {
2399         struct qeth_card *card = dev_get_drvdata(&cgdev->dev);
2400         int rc = 0, rc2 = 0, rc3 = 0;
2401 
2402         mutex_lock(&card->discipline_mutex);
2403         mutex_lock(&card->conf_mutex);
2404         QETH_CARD_TEXT(card, 3, "setoffl");
2405 
2406         if ((!recovery_mode && card->info.hwtrap) || card->info.hwtrap == 2) {
2407                 qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
2408                 card->info.hwtrap = 1;
2409         }
2410 
2411         rtnl_lock();
2412         card->info.open_when_online = card->dev->flags & IFF_UP;
2413         dev_close(card->dev);
2414         netif_device_detach(card->dev);
2415         netif_carrier_off(card->dev);
2416         rtnl_unlock();
2417 
2418         qeth_l3_stop_card(card);
2419         if (card->options.cq == QETH_CQ_ENABLED) {
2420                 rtnl_lock();
2421                 call_netdevice_notifiers(NETDEV_REBOOT, card->dev);
2422                 rtnl_unlock();
2423         }
2424 
2425         rc  = qeth_stop_channel(&card->data);
2426         rc2 = qeth_stop_channel(&card->write);
2427         rc3 = qeth_stop_channel(&card->read);
2428         if (!rc)
2429                 rc = (rc2) ? rc2 : rc3;
2430         if (rc)
2431                 QETH_CARD_TEXT_(card, 2, "1err%d", rc);
2432         qdio_free(CARD_DDEV(card));
2433 
2434         /* let user_space know that device is offline */
2435         kobject_uevent(&cgdev->dev.kobj, KOBJ_CHANGE);
2436         mutex_unlock(&card->conf_mutex);
2437         mutex_unlock(&card->discipline_mutex);
2438         return 0;
2439 }
2440 
2441 static int qeth_l3_set_offline(struct ccwgroup_device *cgdev)
2442 {
2443         return __qeth_l3_set_offline(cgdev, 0);
2444 }
2445 
2446 static int qeth_l3_recover(void *ptr)
2447 {
2448         struct qeth_card *card;
2449         int rc = 0;
2450 
2451         card = (struct qeth_card *) ptr;
2452         QETH_CARD_TEXT(card, 2, "recover1");
2453         QETH_CARD_HEX(card, 2, &card, sizeof(void *));
2454         if (!qeth_do_run_thread(card, QETH_RECOVER_THREAD))
2455                 return 0;
2456         QETH_CARD_TEXT(card, 2, "recover2");
2457         dev_warn(&card->gdev->dev,
2458                 "A recovery process has been started for the device\n");
2459         __qeth_l3_set_offline(card->gdev, 1);
2460         rc = qeth_l3_set_online(card->gdev);
2461         if (!rc)
2462                 dev_info(&card->gdev->dev,
2463                         "Device successfully recovered!\n");
2464         else {
2465                 ccwgroup_set_offline(card->gdev);
2466                 dev_warn(&card->gdev->dev, "The qeth device driver "
2467                                 "failed to recover an error on the device\n");
2468         }
2469         qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
2470         qeth_clear_thread_running_bit(card, QETH_RECOVER_THREAD);
2471         return 0;
2472 }
2473 
2474 /* Returns zero if the command is successfully "consumed" */
2475 static int qeth_l3_control_event(struct qeth_card *card,
2476                                         struct qeth_ipa_cmd *cmd)
2477 {
2478         return 1;
2479 }
2480 
2481 struct qeth_discipline qeth_l3_discipline = {
2482         .devtype = &qeth_l3_devtype,
2483         .process_rx_buffer = qeth_l3_process_inbound_buffer,
2484         .recover = qeth_l3_recover,
2485         .setup = qeth_l3_probe_device,
2486         .remove = qeth_l3_remove_device,
2487         .set_online = qeth_l3_set_online,
2488         .set_offline = qeth_l3_set_offline,
2489         .do_ioctl = qeth_l3_do_ioctl,
2490         .control_event_handler = qeth_l3_control_event,
2491 };
2492 EXPORT_SYMBOL_GPL(qeth_l3_discipline);
2493 
2494 static int qeth_l3_handle_ip_event(struct qeth_card *card,
2495                                    struct qeth_ipaddr *addr,
2496                                    unsigned long event)
2497 {
2498         switch (event) {
2499         case NETDEV_UP:
2500                 qeth_l3_modify_ip(card, addr, true);
2501                 return NOTIFY_OK;
2502         case NETDEV_DOWN:
2503                 qeth_l3_modify_ip(card, addr, false);
2504                 return NOTIFY_OK;
2505         default:
2506                 return NOTIFY_DONE;
2507         }
2508 }
2509 
2510 struct qeth_l3_ip_event_work {
2511         struct work_struct work;
2512         struct qeth_card *card;
2513         struct qeth_ipaddr addr;
2514 };
2515 
2516 #define to_ip_work(w) container_of((w), struct qeth_l3_ip_event_work, work)
2517 
2518 static void qeth_l3_add_ip_worker(struct work_struct *work)
2519 {
2520         struct qeth_l3_ip_event_work *ip_work = to_ip_work(work);
2521 
2522         qeth_l3_modify_ip(ip_work->card, &ip_work->addr, true);
2523         kfree(work);
2524 }
2525 
2526 static void qeth_l3_delete_ip_worker(struct work_struct *work)
2527 {
2528         struct qeth_l3_ip_event_work *ip_work = to_ip_work(work);
2529 
2530         qeth_l3_modify_ip(ip_work->card, &ip_work->addr, false);
2531         kfree(work);
2532 }
2533 
2534 static struct qeth_card *qeth_l3_get_card_from_dev(struct net_device *dev)
2535 {
2536         if (is_vlan_dev(dev))
2537                 dev = vlan_dev_real_dev(dev);
2538         if (dev->netdev_ops == &qeth_l3_osa_netdev_ops ||
2539             dev->netdev_ops == &qeth_l3_netdev_ops)
2540                 return (struct qeth_card *) dev->ml_priv;
2541         return NULL;
2542 }
2543 
2544 static int qeth_l3_ip_event(struct notifier_block *this,
2545                             unsigned long event, void *ptr)
2546 {
2547 
2548         struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
2549         struct net_device *dev = ifa->ifa_dev->dev;
2550         struct qeth_ipaddr addr;
2551         struct qeth_card *card;
2552 
2553         if (dev_net(dev) != &init_net)
2554                 return NOTIFY_DONE;
2555 
2556         card = qeth_l3_get_card_from_dev(dev);
2557         if (!card)
2558                 return NOTIFY_DONE;
2559         QETH_CARD_TEXT(card, 3, "ipevent");
2560 
2561         qeth_l3_init_ipaddr(&addr, QETH_IP_TYPE_NORMAL, QETH_PROT_IPV4);
2562         addr.u.a4.addr = be32_to_cpu(ifa->ifa_address);
2563         addr.u.a4.mask = be32_to_cpu(ifa->ifa_mask);
2564 
2565         return qeth_l3_handle_ip_event(card, &addr, event);
2566 }
2567 
2568 static struct notifier_block qeth_l3_ip_notifier = {
2569         qeth_l3_ip_event,
2570         NULL,
2571 };
2572 
2573 static int qeth_l3_ip6_event(struct notifier_block *this,
2574                              unsigned long event, void *ptr)
2575 {
2576         struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
2577         struct net_device *dev = ifa->idev->dev;
2578         struct qeth_l3_ip_event_work *ip_work;
2579         struct qeth_card *card;
2580 
2581         if (event != NETDEV_UP && event != NETDEV_DOWN)
2582                 return NOTIFY_DONE;
2583 
2584         card = qeth_l3_get_card_from_dev(dev);
2585         if (!card)
2586                 return NOTIFY_DONE;
2587         QETH_CARD_TEXT(card, 3, "ip6event");
2588         if (!qeth_is_supported(card, IPA_IPV6))
2589                 return NOTIFY_DONE;
2590 
2591         ip_work = kmalloc(sizeof(*ip_work), GFP_ATOMIC);
2592         if (!ip_work)
2593                 return NOTIFY_DONE;
2594 
2595         if (event == NETDEV_UP)
2596                 INIT_WORK(&ip_work->work, qeth_l3_add_ip_worker);
2597         else
2598                 INIT_WORK(&ip_work->work, qeth_l3_delete_ip_worker);
2599 
2600         ip_work->card = card;
2601         qeth_l3_init_ipaddr(&ip_work->addr, QETH_IP_TYPE_NORMAL,
2602                             QETH_PROT_IPV6);
2603         ip_work->addr.u.a6.addr = ifa->addr;
2604         ip_work->addr.u.a6.pfxlen = ifa->prefix_len;
2605 
2606         queue_work(card->cmd_wq, &ip_work->work);
2607         return NOTIFY_OK;
2608 }
2609 
2610 static struct notifier_block qeth_l3_ip6_notifier = {
2611         qeth_l3_ip6_event,
2612         NULL,
2613 };
2614 
2615 static int qeth_l3_register_notifiers(void)
2616 {
2617         int rc;
2618 
2619         QETH_DBF_TEXT(SETUP, 5, "regnotif");
2620         rc = register_inetaddr_notifier(&qeth_l3_ip_notifier);
2621         if (rc)
2622                 return rc;
2623         rc = register_inet6addr_notifier(&qeth_l3_ip6_notifier);
2624         if (rc) {
2625                 unregister_inetaddr_notifier(&qeth_l3_ip_notifier);
2626                 return rc;
2627         }
2628         return 0;
2629 }
2630 
2631 static void qeth_l3_unregister_notifiers(void)
2632 {
2633         QETH_DBF_TEXT(SETUP, 5, "unregnot");
2634         WARN_ON(unregister_inetaddr_notifier(&qeth_l3_ip_notifier));
2635         WARN_ON(unregister_inet6addr_notifier(&qeth_l3_ip6_notifier));
2636 }
2637 
2638 static int __init qeth_l3_init(void)
2639 {
2640         pr_info("register layer 3 discipline\n");
2641         return qeth_l3_register_notifiers();
2642 }
2643 
2644 static void __exit qeth_l3_exit(void)
2645 {
2646         qeth_l3_unregister_notifiers();
2647         pr_info("unregister layer 3 discipline\n");
2648 }
2649 
2650 module_init(qeth_l3_init);
2651 module_exit(qeth_l3_exit);
2652 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
2653 MODULE_DESCRIPTION("qeth layer 3 discipline");
2654 MODULE_LICENSE("GPL");

/* [<][>][^][v][top][bottom][index][help] */