linux/drivers/infiniband/hw/nes/nes_nic.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2006 - 2011 Intel Corporation.  All rights reserved.
   3 *
   4 * This software is available to you under a choice of one of two
   5 * licenses.  You may choose to be licensed under the terms of the GNU
   6 * General Public License (GPL) Version 2, available from the file
   7 * COPYING in the main directory of this source tree, or the
   8 * OpenIB.org BSD license below:
   9 *
  10 *     Redistribution and use in source and binary forms, with or
  11 *     without modification, are permitted provided that the following
  12 *     conditions are met:
  13 *
  14 *      - Redistributions of source code must retain the above
  15 *        copyright notice, this list of conditions and the following
  16 *        disclaimer.
  17 *
  18 *      - Redistributions in binary form must reproduce the above
  19 *        copyright notice, this list of conditions and the following
  20 *        disclaimer in the documentation and/or other materials
  21 *        provided with the distribution.
  22 *
  23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30 * SOFTWARE.
  31 *
  32 */
  33
  34#include <linux/module.h>
  35#include <linux/moduleparam.h>
  36#include <linux/netdevice.h>
  37#include <linux/etherdevice.h>
  38#include <linux/ip.h>
  39#include <linux/tcp.h>
  40#include <linux/if_arp.h>
  41#include <linux/if_vlan.h>
  42#include <linux/ethtool.h>
  43#include <linux/slab.h>
  44#include <net/tcp.h>
  45
  46#include <net/inet_common.h>
  47#include <linux/inet.h>
  48
  49#include "nes.h"
  50
  51static struct nic_qp_map nic_qp_mapping_0[] = {
  52        {16,0,0,1},{24,4,0,0},{28,8,0,0},{32,12,0,0},
  53        {20,2,2,1},{26,6,2,0},{30,10,2,0},{34,14,2,0},
  54        {18,1,1,1},{25,5,1,0},{29,9,1,0},{33,13,1,0},
  55        {22,3,3,1},{27,7,3,0},{31,11,3,0},{35,15,3,0}
  56};
  57
  58static struct nic_qp_map nic_qp_mapping_1[] = {
  59        {18,1,1,1},{25,5,1,0},{29,9,1,0},{33,13,1,0},
  60        {22,3,3,1},{27,7,3,0},{31,11,3,0},{35,15,3,0}
  61};
  62
  63static struct nic_qp_map nic_qp_mapping_2[] = {
  64        {20,2,2,1},{26,6,2,0},{30,10,2,0},{34,14,2,0}
  65};
  66
  67static struct nic_qp_map nic_qp_mapping_3[] = {
  68        {22,3,3,1},{27,7,3,0},{31,11,3,0},{35,15,3,0}
  69};
  70
  71static struct nic_qp_map nic_qp_mapping_4[] = {
  72        {28,8,0,0},{32,12,0,0}
  73};
  74
  75static struct nic_qp_map nic_qp_mapping_5[] = {
  76        {29,9,1,0},{33,13,1,0}
  77};
  78
  79static struct nic_qp_map nic_qp_mapping_6[] = {
  80        {30,10,2,0},{34,14,2,0}
  81};
  82
  83static struct nic_qp_map nic_qp_mapping_7[] = {
  84        {31,11,3,0},{35,15,3,0}
  85};
  86
  87static struct nic_qp_map *nic_qp_mapping_per_function[] = {
  88        nic_qp_mapping_0, nic_qp_mapping_1, nic_qp_mapping_2, nic_qp_mapping_3,
  89        nic_qp_mapping_4, nic_qp_mapping_5, nic_qp_mapping_6, nic_qp_mapping_7
  90};
  91
  92static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK
  93                | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN;
  94static int debug = -1;
  95static int nics_per_function = 1;
  96
  97/**
  98 * nes_netdev_poll
  99 */
 100static int nes_netdev_poll(struct napi_struct *napi, int budget)
 101{
 102        struct nes_vnic *nesvnic = container_of(napi, struct nes_vnic, napi);
 103        struct nes_device *nesdev = nesvnic->nesdev;
 104        struct nes_hw_nic_cq *nescq = &nesvnic->nic_cq;
 105
 106        nesvnic->budget = budget;
 107        nescq->cqes_pending = 0;
 108        nescq->rx_cqes_completed = 0;
 109        nescq->cqe_allocs_pending = 0;
 110        nescq->rx_pkts_indicated = 0;
 111
 112        nes_nic_ce_handler(nesdev, nescq);
 113
 114        if (nescq->cqes_pending == 0) {
 115                napi_complete(napi);
 116                /* clear out completed cqes and arm */
 117                nes_write32(nesdev->regs+NES_CQE_ALLOC, NES_CQE_ALLOC_NOTIFY_NEXT |
 118                                nescq->cq_number | (nescq->cqe_allocs_pending << 16));
 119                nes_read32(nesdev->regs+NES_CQE_ALLOC);
 120        } else {
 121                /* clear out completed cqes but don't arm */
 122                nes_write32(nesdev->regs+NES_CQE_ALLOC,
 123                                nescq->cq_number | (nescq->cqe_allocs_pending << 16));
 124                nes_debug(NES_DBG_NETDEV, "%s: exiting with work pending\n",
 125                                nesvnic->netdev->name);
 126        }
 127        return nescq->rx_pkts_indicated;
 128}
 129
 130
 131/**
 132 * nes_netdev_open - Activate the network interface; ifconfig
 133 * ethx up.
 134 */
 135static int nes_netdev_open(struct net_device *netdev)
 136{
 137        u32 macaddr_low;
 138        u16 macaddr_high;
 139        struct nes_vnic *nesvnic = netdev_priv(netdev);
 140        struct nes_device *nesdev = nesvnic->nesdev;
 141        int ret;
 142        int i;
 143        struct nes_vnic *first_nesvnic = NULL;
 144        u32 nic_active_bit;
 145        u32 nic_active;
 146        struct list_head *list_pos, *list_temp;
 147        unsigned long flags;
 148
 149        if (nesvnic->netdev_open == 1)
 150                return 0;
 151
 152        if (netif_msg_ifup(nesvnic))
 153                printk(KERN_INFO PFX "%s: enabling interface\n", netdev->name);
 154
 155        ret = nes_init_nic_qp(nesdev, netdev);
 156        if (ret) {
 157                return ret;
 158        }
 159
 160        netif_carrier_off(netdev);
 161        netif_stop_queue(netdev);
 162
 163        if ((!nesvnic->of_device_registered) && (nesvnic->rdma_enabled)) {
 164                nesvnic->nesibdev = nes_init_ofa_device(netdev);
 165                if (nesvnic->nesibdev == NULL) {
 166                        printk(KERN_ERR PFX "%s: nesvnic->nesibdev alloc failed", netdev->name);
 167                } else {
 168                        nesvnic->nesibdev->nesvnic = nesvnic;
 169                        ret = nes_register_ofa_device(nesvnic->nesibdev);
 170                        if (ret) {
 171                                printk(KERN_ERR PFX "%s: Unable to register RDMA device, ret = %d\n",
 172                                                netdev->name, ret);
 173                        }
 174                }
 175        }
 176        /* Set packet filters */
 177        nic_active_bit = 1 << nesvnic->nic_index;
 178        nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_ACTIVE);
 179        nic_active |= nic_active_bit;
 180        nes_write_indexed(nesdev, NES_IDX_NIC_ACTIVE, nic_active);
 181        nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_MULTICAST_ENABLE);
 182        nic_active |= nic_active_bit;
 183        nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ENABLE, nic_active);
 184        nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_BROADCAST_ON);
 185        nic_active |= nic_active_bit;
 186        nes_write_indexed(nesdev, NES_IDX_NIC_BROADCAST_ON, nic_active);
 187
 188        macaddr_high  = ((u16)netdev->dev_addr[0]) << 8;
 189        macaddr_high += (u16)netdev->dev_addr[1];
 190
 191        macaddr_low   = ((u32)netdev->dev_addr[2]) << 24;
 192        macaddr_low  += ((u32)netdev->dev_addr[3]) << 16;
 193        macaddr_low  += ((u32)netdev->dev_addr[4]) << 8;
 194        macaddr_low  += (u32)netdev->dev_addr[5];
 195
 196        /* Program the various MAC regs */
 197        for (i = 0; i < NES_MAX_PORT_COUNT; i++) {
 198                if (nesvnic->qp_nic_index[i] == 0xf) {
 199                        break;
 200                }
 201                nes_debug(NES_DBG_NETDEV, "i=%d, perfect filter table index= %d, PERF FILTER LOW"
 202                                " (Addr:%08X) = %08X, HIGH = %08X.\n",
 203                                i, nesvnic->qp_nic_index[i],
 204                                NES_IDX_PERFECT_FILTER_LOW+
 205                                        (nesvnic->qp_nic_index[i] * 8),
 206                                macaddr_low,
 207                                (u32)macaddr_high | NES_MAC_ADDR_VALID |
 208                                ((((u32)nesvnic->nic_index) << 16)));
 209                nes_write_indexed(nesdev,
 210                                NES_IDX_PERFECT_FILTER_LOW + (nesvnic->qp_nic_index[i] * 8),
 211                                macaddr_low);
 212                nes_write_indexed(nesdev,
 213                                NES_IDX_PERFECT_FILTER_HIGH + (nesvnic->qp_nic_index[i] * 8),
 214                                (u32)macaddr_high | NES_MAC_ADDR_VALID |
 215                                ((((u32)nesvnic->nic_index) << 16)));
 216        }
 217
 218
 219        nes_write32(nesdev->regs+NES_CQE_ALLOC, NES_CQE_ALLOC_NOTIFY_NEXT |
 220                        nesvnic->nic_cq.cq_number);
 221        nes_read32(nesdev->regs+NES_CQE_ALLOC);
 222        list_for_each_safe(list_pos, list_temp, &nesdev->nesadapter->nesvnic_list[nesdev->mac_index]) {
 223                first_nesvnic = container_of(list_pos, struct nes_vnic, list);
 224                if (first_nesvnic->netdev_open == 1)
 225                        break;
 226        }
 227        if (first_nesvnic->netdev_open == 0) {
 228                nes_debug(NES_DBG_INIT, "Setting up MAC interrupt mask.\n");
 229                nes_write_indexed(nesdev, NES_IDX_MAC_INT_MASK + (0x200 * nesdev->mac_index),
 230                                ~(NES_MAC_INT_LINK_STAT_CHG | NES_MAC_INT_XGMII_EXT |
 231                                NES_MAC_INT_TX_UNDERFLOW | NES_MAC_INT_TX_ERROR));
 232                first_nesvnic = nesvnic;
 233        }
 234
 235        if (first_nesvnic->linkup) {
 236                /* Enable network packets */
 237                nesvnic->linkup = 1;
 238                netif_start_queue(netdev);
 239                netif_carrier_on(netdev);
 240        }
 241
 242        spin_lock_irqsave(&nesdev->nesadapter->phy_lock, flags);
 243        if (nesdev->nesadapter->phy_type[nesdev->mac_index] == NES_PHY_TYPE_SFP_D) {
 244                nesdev->link_recheck = 1;
 245                mod_delayed_work(system_wq, &nesdev->work,
 246                                 NES_LINK_RECHECK_DELAY);
 247        }
 248        spin_unlock_irqrestore(&nesdev->nesadapter->phy_lock, flags);
 249
 250        spin_lock_irqsave(&nesvnic->port_ibevent_lock, flags);
 251        if (nesvnic->of_device_registered) {
 252                nesdev->nesadapter->send_term_ok = 1;
 253                if (nesvnic->linkup == 1) {
 254                        if (nesdev->iw_status == 0) {
 255                                nesdev->iw_status = 1;
 256                                nes_port_ibevent(nesvnic);
 257                        }
 258                } else {
 259                        nesdev->iw_status = 0;
 260                }
 261        }
 262        spin_unlock_irqrestore(&nesvnic->port_ibevent_lock, flags);
 263
 264        napi_enable(&nesvnic->napi);
 265        nesvnic->netdev_open = 1;
 266
 267        return 0;
 268}
 269
 270
 271/**
 272 * nes_netdev_stop
 273 */
 274static int nes_netdev_stop(struct net_device *netdev)
 275{
 276        struct nes_vnic *nesvnic = netdev_priv(netdev);
 277        struct nes_device *nesdev = nesvnic->nesdev;
 278        u32 nic_active_mask;
 279        u32 nic_active;
 280        struct nes_vnic *first_nesvnic = NULL;
 281        struct list_head *list_pos, *list_temp;
 282        unsigned long flags;
 283
 284        nes_debug(NES_DBG_SHUTDOWN, "nesvnic=%p, nesdev=%p, netdev=%p %s\n",
 285                        nesvnic, nesdev, netdev, netdev->name);
 286        if (nesvnic->netdev_open == 0)
 287                return 0;
 288
 289        if (netif_msg_ifdown(nesvnic))
 290                printk(KERN_INFO PFX "%s: disabling interface\n", netdev->name);
 291        netif_carrier_off(netdev);
 292
 293        /* Disable network packets */
 294        napi_disable(&nesvnic->napi);
 295        netif_stop_queue(netdev);
 296        list_for_each_safe(list_pos, list_temp, &nesdev->nesadapter->nesvnic_list[nesdev->mac_index]) {
 297                first_nesvnic = container_of(list_pos, struct nes_vnic, list);
 298                if ((first_nesvnic->netdev_open == 1) && (first_nesvnic != nesvnic))
 299                        break;
 300        }
 301
 302        if ((first_nesvnic->netdev_open == 1) && (first_nesvnic != nesvnic)  &&
 303                (PCI_FUNC(first_nesvnic->nesdev->pcidev->devfn) !=
 304                PCI_FUNC(nesvnic->nesdev->pcidev->devfn))) {
 305                        nes_write_indexed(nesdev, NES_IDX_MAC_INT_MASK+
 306                                (0x200*nesdev->mac_index), 0xffffffff);
 307                        nes_write_indexed(first_nesvnic->nesdev,
 308                                NES_IDX_MAC_INT_MASK+
 309                                (0x200*first_nesvnic->nesdev->mac_index),
 310                        ~(NES_MAC_INT_LINK_STAT_CHG | NES_MAC_INT_XGMII_EXT |
 311                        NES_MAC_INT_TX_UNDERFLOW | NES_MAC_INT_TX_ERROR));
 312        } else {
 313                nes_write_indexed(nesdev, NES_IDX_MAC_INT_MASK+(0x200*nesdev->mac_index), 0xffffffff);
 314        }
 315
 316        nic_active_mask = ~((u32)(1 << nesvnic->nic_index));
 317        nes_write_indexed(nesdev, NES_IDX_PERFECT_FILTER_HIGH+
 318                        (nesvnic->perfect_filter_index*8), 0);
 319        nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_ACTIVE);
 320        nic_active &= nic_active_mask;
 321        nes_write_indexed(nesdev, NES_IDX_NIC_ACTIVE, nic_active);
 322        nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL);
 323        nic_active &= nic_active_mask;
 324        nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL, nic_active);
 325        nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_MULTICAST_ENABLE);
 326        nic_active &= nic_active_mask;
 327        nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ENABLE, nic_active);
 328        nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL);
 329        nic_active &= nic_active_mask;
 330        nes_write_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL, nic_active);
 331        nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_BROADCAST_ON);
 332        nic_active &= nic_active_mask;
 333        nes_write_indexed(nesdev, NES_IDX_NIC_BROADCAST_ON, nic_active);
 334
 335        spin_lock_irqsave(&nesvnic->port_ibevent_lock, flags);
 336        if (nesvnic->of_device_registered) {
 337                nesdev->nesadapter->send_term_ok = 0;
 338                nesdev->iw_status = 0;
 339                if (nesvnic->linkup == 1)
 340                        nes_port_ibevent(nesvnic);
 341        }
 342        del_timer_sync(&nesvnic->event_timer);
 343        nesvnic->event_timer.function = NULL;
 344        spin_unlock_irqrestore(&nesvnic->port_ibevent_lock, flags);
 345
 346        nes_destroy_nic_qp(nesvnic);
 347
 348        nesvnic->netdev_open = 0;
 349
 350        return 0;
 351}
 352
 353
 354/**
 355 * nes_nic_send
 356 */
 357static bool nes_nic_send(struct sk_buff *skb, struct net_device *netdev)
 358{
 359        struct nes_vnic *nesvnic = netdev_priv(netdev);
 360        struct nes_device *nesdev = nesvnic->nesdev;
 361        struct nes_hw_nic *nesnic = &nesvnic->nic;
 362        struct nes_hw_nic_sq_wqe *nic_sqe;
 363        struct tcphdr *tcph;
 364        __le16 *wqe_fragment_length;
 365        u32 wqe_misc;
 366        u16 wqe_fragment_index = 1;     /* first fragment (0) is used by copy buffer */
 367        u16 skb_fragment_index;
 368        dma_addr_t bus_address;
 369
 370        nic_sqe = &nesnic->sq_vbase[nesnic->sq_head];
 371        wqe_fragment_length = (__le16 *)&nic_sqe->wqe_words[NES_NIC_SQ_WQE_LENGTH_0_TAG_IDX];
 372
 373        /* setup the VLAN tag if present */
 374        if (skb_vlan_tag_present(skb)) {
 375                nes_debug(NES_DBG_NIC_TX, "%s: VLAN packet to send... VLAN = %08X\n",
 376                                netdev->name, skb_vlan_tag_get(skb));
 377                wqe_misc = NES_NIC_SQ_WQE_TAGVALUE_ENABLE;
 378                wqe_fragment_length[0] = (__force __le16) skb_vlan_tag_get(skb);
 379        } else
 380                wqe_misc = 0;
 381
 382        /* bump past the vlan tag */
 383        wqe_fragment_length++;
 384        /*      wqe_fragment_address = (u64 *)&nic_sqe->wqe_words[NES_NIC_SQ_WQE_FRAG0_LOW_IDX]; */
 385        wqe_misc |= NES_NIC_SQ_WQE_COMPLETION;
 386
 387        if (skb->ip_summed == CHECKSUM_PARTIAL) {
 388                if (skb_is_gso(skb)) {
 389                        tcph = tcp_hdr(skb);
 390                        /* nes_debug(NES_DBG_NIC_TX, "%s: TSO request... is_gso = %u seg size = %u\n",
 391                                        netdev->name, skb_is_gso(skb), skb_shinfo(skb)->gso_size); */
 392                        wqe_misc |= NES_NIC_SQ_WQE_LSO_ENABLE | (u16)skb_shinfo(skb)->gso_size;
 393                        set_wqe_32bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_LSO_INFO_IDX,
 394                                        ((u32)tcph->doff) |
 395                                        (((u32)(((unsigned char *)tcph) - skb->data)) << 4));
 396                }
 397        } else {        /* CHECKSUM_HW */
 398                wqe_misc |= NES_NIC_SQ_WQE_DISABLE_CHKSUM;
 399        }
 400
 401        set_wqe_32bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_TOTAL_LENGTH_IDX,
 402                                skb->len);
 403        memcpy(&nesnic->first_frag_vbase[nesnic->sq_head].buffer,
 404                        skb->data, min(((unsigned int)NES_FIRST_FRAG_SIZE), skb_headlen(skb)));
 405        wqe_fragment_length[0] = cpu_to_le16(min(((unsigned int)NES_FIRST_FRAG_SIZE),
 406                        skb_headlen(skb)));
 407        wqe_fragment_length[1] = 0;
 408        if (skb_headlen(skb) > NES_FIRST_FRAG_SIZE) {
 409                if ((skb_shinfo(skb)->nr_frags + 1) > 4) {
 410                        nes_debug(NES_DBG_NIC_TX, "%s: Packet with %u fragments not sent, skb_headlen=%u\n",
 411                                        netdev->name, skb_shinfo(skb)->nr_frags + 2, skb_headlen(skb));
 412                        kfree_skb(skb);
 413                        nesvnic->tx_sw_dropped++;
 414                        return false;
 415                }
 416                set_bit(nesnic->sq_head, nesnic->first_frag_overflow);
 417                bus_address = pci_map_single(nesdev->pcidev, skb->data + NES_FIRST_FRAG_SIZE,
 418                                skb_headlen(skb) - NES_FIRST_FRAG_SIZE, PCI_DMA_TODEVICE);
 419                wqe_fragment_length[wqe_fragment_index++] =
 420                                cpu_to_le16(skb_headlen(skb) - NES_FIRST_FRAG_SIZE);
 421                wqe_fragment_length[wqe_fragment_index] = 0;
 422                set_wqe_64bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_FRAG1_LOW_IDX,
 423                                ((u64)(bus_address)));
 424                nesnic->tx_skb[nesnic->sq_head] = skb;
 425        }
 426
 427        if (skb_headlen(skb) == skb->len) {
 428                if (skb_headlen(skb) <= NES_FIRST_FRAG_SIZE) {
 429                        nic_sqe->wqe_words[NES_NIC_SQ_WQE_LENGTH_2_1_IDX] = 0;
 430                        nesnic->tx_skb[nesnic->sq_head] = skb;
 431                }
 432        } else {
 433                /* Deal with Fragments */
 434                nesnic->tx_skb[nesnic->sq_head] = skb;
 435                for (skb_fragment_index = 0; skb_fragment_index < skb_shinfo(skb)->nr_frags;
 436                                skb_fragment_index++) {
 437                        skb_frag_t *frag =
 438                                &skb_shinfo(skb)->frags[skb_fragment_index];
 439                        bus_address = skb_frag_dma_map(&nesdev->pcidev->dev,
 440                                                       frag, 0, skb_frag_size(frag),
 441                                                       DMA_TO_DEVICE);
 442                        wqe_fragment_length[wqe_fragment_index] =
 443                                        cpu_to_le16(skb_frag_size(&skb_shinfo(skb)->frags[skb_fragment_index]));
 444                        set_wqe_64bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_FRAG0_LOW_IDX+(2*wqe_fragment_index),
 445                                bus_address);
 446                        wqe_fragment_index++;
 447                        if (wqe_fragment_index < 5)
 448                                wqe_fragment_length[wqe_fragment_index] = 0;
 449                }
 450        }
 451
 452        set_wqe_32bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_MISC_IDX, wqe_misc);
 453        nesnic->sq_head++;
 454        nesnic->sq_head &= nesnic->sq_size - 1;
 455        return true;
 456}
 457
 458
 459/**
 460 * nes_netdev_start_xmit
 461 */
 462static netdev_tx_t nes_netdev_start_xmit(struct sk_buff *skb, struct net_device *netdev)
 463{
 464        struct nes_vnic *nesvnic = netdev_priv(netdev);
 465        struct nes_device *nesdev = nesvnic->nesdev;
 466        struct nes_hw_nic *nesnic = &nesvnic->nic;
 467        struct nes_hw_nic_sq_wqe *nic_sqe;
 468        struct tcphdr *tcph;
 469        /* struct udphdr *udph; */
 470#define NES_MAX_TSO_FRAGS MAX_SKB_FRAGS
 471        /* 64K segment plus overflow on each side */
 472        dma_addr_t tso_bus_address[NES_MAX_TSO_FRAGS];
 473        dma_addr_t bus_address;
 474        u32 tso_frag_index;
 475        u32 tso_frag_count;
 476        u32 tso_wqe_length;
 477        u32 curr_tcp_seq;
 478        u32 wqe_count=1;
 479        struct iphdr *iph;
 480        __le16 *wqe_fragment_length;
 481        u32 nr_frags;
 482        u32 original_first_length;
 483        /* u64 *wqe_fragment_address; */
 484        /* first fragment (0) is used by copy buffer */
 485        u16 wqe_fragment_index=1;
 486        u16 hoffset;
 487        u16 nhoffset;
 488        u16 wqes_needed;
 489        u16 wqes_available;
 490        u32 wqe_misc;
 491
 492        /*
 493         * nes_debug(NES_DBG_NIC_TX, "%s Request to tx NIC packet length %u, headlen %u,"
 494         *              " (%u frags), tso_size=%u\n",
 495         *              netdev->name, skb->len, skb_headlen(skb),
 496         *              skb_shinfo(skb)->nr_frags, skb_is_gso(skb));
 497         */
 498
 499        if (netif_queue_stopped(netdev))
 500                return NETDEV_TX_BUSY;
 501
 502        /* Check if SQ is full */
 503        if ((((nesnic->sq_tail+(nesnic->sq_size*2))-nesnic->sq_head) & (nesnic->sq_size - 1)) == 1) {
 504                if (!netif_queue_stopped(netdev)) {
 505                        netif_stop_queue(netdev);
 506                        barrier();
 507                        if ((((((volatile u16)nesnic->sq_tail)+(nesnic->sq_size*2))-nesnic->sq_head) & (nesnic->sq_size - 1)) != 1) {
 508                                netif_start_queue(netdev);
 509                                goto sq_no_longer_full;
 510                        }
 511                }
 512                nesvnic->sq_full++;
 513                return NETDEV_TX_BUSY;
 514        }
 515
 516sq_no_longer_full:
 517        nr_frags = skb_shinfo(skb)->nr_frags;
 518        if (skb_headlen(skb) > NES_FIRST_FRAG_SIZE) {
 519                nr_frags++;
 520        }
 521        /* Check if too many fragments */
 522        if (unlikely((nr_frags > 4))) {
 523                if (skb_is_gso(skb)) {
 524                        nesvnic->segmented_tso_requests++;
 525                        nesvnic->tso_requests++;
 526                        /* Basically 4 fragments available per WQE with extended fragments */
 527                        wqes_needed = nr_frags >> 2;
 528                        wqes_needed += (nr_frags&3)?1:0;
 529                        wqes_available = (((nesnic->sq_tail+nesnic->sq_size)-nesnic->sq_head) - 1) &
 530                                        (nesnic->sq_size - 1);
 531
 532                        if (unlikely(wqes_needed > wqes_available)) {
 533                                if (!netif_queue_stopped(netdev)) {
 534                                        netif_stop_queue(netdev);
 535                                        barrier();
 536                                        wqes_available = (((((volatile u16)nesnic->sq_tail)+nesnic->sq_size)-nesnic->sq_head) - 1) &
 537                                                (nesnic->sq_size - 1);
 538                                        if (wqes_needed <= wqes_available) {
 539                                                netif_start_queue(netdev);
 540                                                goto tso_sq_no_longer_full;
 541                                        }
 542                                }
 543                                nesvnic->sq_full++;
 544                                nes_debug(NES_DBG_NIC_TX, "%s: HNIC SQ full- TSO request has too many frags!\n",
 545                                                netdev->name);
 546                                return NETDEV_TX_BUSY;
 547                        }
 548tso_sq_no_longer_full:
 549                        /* Map all the buffers */
 550                        for (tso_frag_count=0; tso_frag_count < skb_shinfo(skb)->nr_frags;
 551                                        tso_frag_count++) {
 552                                skb_frag_t *frag =
 553                                        &skb_shinfo(skb)->frags[tso_frag_count];
 554                                tso_bus_address[tso_frag_count] =
 555                                        skb_frag_dma_map(&nesdev->pcidev->dev,
 556                                                         frag, 0, skb_frag_size(frag),
 557                                                         DMA_TO_DEVICE);
 558                        }
 559
 560                        tso_frag_index = 0;
 561                        curr_tcp_seq = ntohl(tcp_hdr(skb)->seq);
 562                        hoffset = skb_transport_header(skb) - skb->data;
 563                        nhoffset = skb_network_header(skb) - skb->data;
 564                        original_first_length = hoffset + ((((struct tcphdr *)skb_transport_header(skb))->doff)<<2);
 565
 566                        for (wqe_count=0; wqe_count<((u32)wqes_needed); wqe_count++) {
 567                                tso_wqe_length = 0;
 568                                nic_sqe = &nesnic->sq_vbase[nesnic->sq_head];
 569                                wqe_fragment_length =
 570                                                (__le16 *)&nic_sqe->wqe_words[NES_NIC_SQ_WQE_LENGTH_0_TAG_IDX];
 571                                /* setup the VLAN tag if present */
 572                                if (skb_vlan_tag_present(skb)) {
 573                                        nes_debug(NES_DBG_NIC_TX, "%s: VLAN packet to send... VLAN = %08X\n",
 574                                                        netdev->name,
 575                                                  skb_vlan_tag_get(skb));
 576                                        wqe_misc = NES_NIC_SQ_WQE_TAGVALUE_ENABLE;
 577                                        wqe_fragment_length[0] = (__force __le16) skb_vlan_tag_get(skb);
 578                                } else
 579                                        wqe_misc = 0;
 580
 581                                /* bump past the vlan tag */
 582                                wqe_fragment_length++;
 583
 584                                /* Assumes header totally fits in allocated buffer and is in first fragment */
 585                                if (original_first_length > NES_FIRST_FRAG_SIZE) {
 586                                        nes_debug(NES_DBG_NIC_TX, "ERROR: SKB header too big, headlen=%u, FIRST_FRAG_SIZE=%u\n",
 587                                                        original_first_length, NES_FIRST_FRAG_SIZE);
 588                                        nes_debug(NES_DBG_NIC_TX, "%s Request to tx NIC packet length %u, headlen %u,"
 589                                                        " (%u frags), is_gso = %u tso_size=%u\n",
 590                                                        netdev->name,
 591                                                        skb->len, skb_headlen(skb),
 592                                                        skb_shinfo(skb)->nr_frags, skb_is_gso(skb), skb_shinfo(skb)->gso_size);
 593                                }
 594                                memcpy(&nesnic->first_frag_vbase[nesnic->sq_head].buffer,
 595                                                skb->data, min(((unsigned int)NES_FIRST_FRAG_SIZE),
 596                                                original_first_length));
 597                                iph = (struct iphdr *)
 598                                (&nesnic->first_frag_vbase[nesnic->sq_head].buffer[nhoffset]);
 599                                tcph = (struct tcphdr *)
 600                                (&nesnic->first_frag_vbase[nesnic->sq_head].buffer[hoffset]);
 601                                if ((wqe_count+1)!=(u32)wqes_needed) {
 602                                        tcph->fin = 0;
 603                                        tcph->psh = 0;
 604                                        tcph->rst = 0;
 605                                        tcph->urg = 0;
 606                                }
 607                                if (wqe_count) {
 608                                        tcph->syn = 0;
 609                                }
 610                                tcph->seq = htonl(curr_tcp_seq);
 611                                wqe_fragment_length[0] = cpu_to_le16(min(((unsigned int)NES_FIRST_FRAG_SIZE),
 612                                                original_first_length));
 613
 614                                wqe_fragment_index = 1;
 615                                if ((wqe_count==0) && (skb_headlen(skb) > original_first_length)) {
 616                                        set_bit(nesnic->sq_head, nesnic->first_frag_overflow);
 617                                        bus_address = pci_map_single(nesdev->pcidev, skb->data + original_first_length,
 618                                                        skb_headlen(skb) - original_first_length, PCI_DMA_TODEVICE);
 619                                        wqe_fragment_length[wqe_fragment_index++] =
 620                                                cpu_to_le16(skb_headlen(skb) - original_first_length);
 621                                        wqe_fragment_length[wqe_fragment_index] = 0;
 622                                        set_wqe_64bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_FRAG1_LOW_IDX,
 623                                                                        bus_address);
 624                                        tso_wqe_length += skb_headlen(skb) -
 625                                                        original_first_length;
 626                                }
 627                                while (wqe_fragment_index < 5) {
 628                                        wqe_fragment_length[wqe_fragment_index] =
 629                                                        cpu_to_le16(skb_frag_size(&skb_shinfo(skb)->frags[tso_frag_index]));
 630                                        set_wqe_64bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_FRAG0_LOW_IDX+(2*wqe_fragment_index),
 631                                                (u64)tso_bus_address[tso_frag_index]);
 632                                        wqe_fragment_index++;
 633                                        tso_wqe_length += skb_frag_size(&skb_shinfo(skb)->frags[tso_frag_index++]);
 634                                        if (wqe_fragment_index < 5)
 635                                                wqe_fragment_length[wqe_fragment_index] = 0;
 636                                        if (tso_frag_index == tso_frag_count)
 637                                                break;
 638                                }
 639                                if ((wqe_count+1) == (u32)wqes_needed) {
 640                                        nesnic->tx_skb[nesnic->sq_head] = skb;
 641                                } else {
 642                                        nesnic->tx_skb[nesnic->sq_head] = NULL;
 643                                }
 644                                wqe_misc |= NES_NIC_SQ_WQE_COMPLETION | (u16)skb_shinfo(skb)->gso_size;
 645                                if ((tso_wqe_length + original_first_length) > skb_shinfo(skb)->gso_size) {
 646                                        wqe_misc |= NES_NIC_SQ_WQE_LSO_ENABLE;
 647                                } else {
 648                                        iph->tot_len = htons(tso_wqe_length + original_first_length - nhoffset);
 649                                }
 650
 651                                set_wqe_32bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_MISC_IDX,
 652                                                 wqe_misc);
 653                                set_wqe_32bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_LSO_INFO_IDX,
 654                                                ((u32)tcph->doff) | (((u32)hoffset) << 4));
 655
 656                                set_wqe_32bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_TOTAL_LENGTH_IDX,
 657                                                tso_wqe_length + original_first_length);
 658                                curr_tcp_seq += tso_wqe_length;
 659                                nesnic->sq_head++;
 660                                nesnic->sq_head &= nesnic->sq_size-1;
 661                        }
 662                } else {
 663                        hoffset = skb_transport_header(skb) - skb->data;
 664                        nhoffset = skb_network_header(skb) - skb->data;
 665                        if (skb_linearize(skb)) {
 666                                nesvnic->tx_sw_dropped++;
 667                                kfree_skb(skb);
 668                                return NETDEV_TX_OK;
 669                        }
 670                        nesvnic->linearized_skbs++;
 671                        skb_set_transport_header(skb, hoffset);
 672                        skb_set_network_header(skb, nhoffset);
 673                        if (!nes_nic_send(skb, netdev))
 674                                return NETDEV_TX_OK;
 675                }
 676        } else {
 677                if (!nes_nic_send(skb, netdev))
 678                        return NETDEV_TX_OK;
 679        }
 680
 681        barrier();
 682
 683        if (wqe_count)
 684                nes_write32(nesdev->regs+NES_WQE_ALLOC,
 685                                (wqe_count << 24) | (1 << 23) | nesvnic->nic.qp_id);
 686
 687        netif_trans_update(netdev);
 688
 689        return NETDEV_TX_OK;
 690}
 691
 692
 693/**
 694 * nes_netdev_get_stats
 695 */
 696static struct net_device_stats *nes_netdev_get_stats(struct net_device *netdev)
 697{
 698        struct nes_vnic *nesvnic = netdev_priv(netdev);
 699        struct nes_device *nesdev = nesvnic->nesdev;
 700        u64 u64temp;
 701        u32 u32temp;
 702
 703        u32temp = nes_read_indexed(nesdev,
 704                        NES_IDX_ENDNODE0_NSTAT_RX_DISCARD + (nesvnic->nic_index*0x200));
 705        nesvnic->netstats.rx_dropped += u32temp;
 706        nesvnic->endnode_nstat_rx_discard += u32temp;
 707
 708        u64temp = (u64)nes_read_indexed(nesdev,
 709                        NES_IDX_ENDNODE0_NSTAT_RX_OCTETS_LO + (nesvnic->nic_index*0x200));
 710        u64temp += ((u64)nes_read_indexed(nesdev,
 711                        NES_IDX_ENDNODE0_NSTAT_RX_OCTETS_HI + (nesvnic->nic_index*0x200))) << 32;
 712
 713        nesvnic->endnode_nstat_rx_octets += u64temp;
 714        nesvnic->netstats.rx_bytes += u64temp;
 715
 716        u64temp = (u64)nes_read_indexed(nesdev,
 717                        NES_IDX_ENDNODE0_NSTAT_RX_FRAMES_LO + (nesvnic->nic_index*0x200));
 718        u64temp += ((u64)nes_read_indexed(nesdev,
 719                        NES_IDX_ENDNODE0_NSTAT_RX_FRAMES_HI + (nesvnic->nic_index*0x200))) << 32;
 720
 721        nesvnic->endnode_nstat_rx_frames += u64temp;
 722        nesvnic->netstats.rx_packets += u64temp;
 723
 724        u64temp = (u64)nes_read_indexed(nesdev,
 725                        NES_IDX_ENDNODE0_NSTAT_TX_OCTETS_LO + (nesvnic->nic_index*0x200));
 726        u64temp += ((u64)nes_read_indexed(nesdev,
 727                        NES_IDX_ENDNODE0_NSTAT_TX_OCTETS_HI + (nesvnic->nic_index*0x200))) << 32;
 728
 729        nesvnic->endnode_nstat_tx_octets += u64temp;
 730        nesvnic->netstats.tx_bytes += u64temp;
 731
 732        u64temp = (u64)nes_read_indexed(nesdev,
 733                        NES_IDX_ENDNODE0_NSTAT_TX_FRAMES_LO + (nesvnic->nic_index*0x200));
 734        u64temp += ((u64)nes_read_indexed(nesdev,
 735                        NES_IDX_ENDNODE0_NSTAT_TX_FRAMES_HI + (nesvnic->nic_index*0x200))) << 32;
 736
 737        nesvnic->endnode_nstat_tx_frames += u64temp;
 738        nesvnic->netstats.tx_packets += u64temp;
 739
 740        u32temp = nes_read_indexed(nesdev,
 741                        NES_IDX_MAC_RX_SHORT_FRAMES + (nesvnic->nesdev->mac_index*0x200));
 742        nesvnic->netstats.rx_dropped += u32temp;
 743        nesvnic->nesdev->mac_rx_errors += u32temp;
 744        nesvnic->nesdev->mac_rx_short_frames += u32temp;
 745
 746        u32temp = nes_read_indexed(nesdev,
 747                        NES_IDX_MAC_RX_OVERSIZED_FRAMES + (nesvnic->nesdev->mac_index*0x200));
 748        nesvnic->netstats.rx_dropped += u32temp;
 749        nesvnic->nesdev->mac_rx_errors += u32temp;
 750        nesvnic->nesdev->mac_rx_oversized_frames += u32temp;
 751
 752        u32temp = nes_read_indexed(nesdev,
 753                        NES_IDX_MAC_RX_JABBER_FRAMES + (nesvnic->nesdev->mac_index*0x200));
 754        nesvnic->netstats.rx_dropped += u32temp;
 755        nesvnic->nesdev->mac_rx_errors += u32temp;
 756        nesvnic->nesdev->mac_rx_jabber_frames += u32temp;
 757
 758        u32temp = nes_read_indexed(nesdev,
 759                        NES_IDX_MAC_RX_SYMBOL_ERR_FRAMES + (nesvnic->nesdev->mac_index*0x200));
 760        nesvnic->netstats.rx_dropped += u32temp;
 761        nesvnic->nesdev->mac_rx_errors += u32temp;
 762        nesvnic->nesdev->mac_rx_symbol_err_frames += u32temp;
 763
 764        u32temp = nes_read_indexed(nesdev,
 765                        NES_IDX_MAC_RX_LENGTH_ERR_FRAMES + (nesvnic->nesdev->mac_index*0x200));
 766        nesvnic->netstats.rx_length_errors += u32temp;
 767        nesvnic->nesdev->mac_rx_errors += u32temp;
 768
 769        u32temp = nes_read_indexed(nesdev,
 770                        NES_IDX_MAC_RX_CRC_ERR_FRAMES + (nesvnic->nesdev->mac_index*0x200));
 771        nesvnic->nesdev->mac_rx_errors += u32temp;
 772        nesvnic->nesdev->mac_rx_crc_errors += u32temp;
 773        nesvnic->netstats.rx_crc_errors += u32temp;
 774
 775        u32temp = nes_read_indexed(nesdev,
 776                        NES_IDX_MAC_TX_ERRORS + (nesvnic->nesdev->mac_index*0x200));
 777        nesvnic->nesdev->mac_tx_errors += u32temp;
 778        nesvnic->netstats.tx_errors += u32temp;
 779
 780        return &nesvnic->netstats;
 781}
 782
 783
 784/**
 785 * nes_netdev_tx_timeout
 786 */
 787static void nes_netdev_tx_timeout(struct net_device *netdev)
 788{
 789        struct nes_vnic *nesvnic = netdev_priv(netdev);
 790
 791        if (netif_msg_timer(nesvnic))
 792                nes_debug(NES_DBG_NIC_TX, "%s: tx timeout\n", netdev->name);
 793}
 794
 795
 796/**
 797 * nes_netdev_set_mac_address
 798 */
 799static int nes_netdev_set_mac_address(struct net_device *netdev, void *p)
 800{
 801        struct nes_vnic *nesvnic = netdev_priv(netdev);
 802        struct nes_device *nesdev = nesvnic->nesdev;
 803        struct sockaddr *mac_addr = p;
 804        int i;
 805        u32 macaddr_low;
 806        u16 macaddr_high;
 807
 808        if (!is_valid_ether_addr(mac_addr->sa_data))
 809                return -EADDRNOTAVAIL;
 810
 811        memcpy(netdev->dev_addr, mac_addr->sa_data, netdev->addr_len);
 812        printk(PFX "%s: Address length = %d, Address = %pM\n",
 813               __func__, netdev->addr_len, mac_addr->sa_data);
 814        macaddr_high  = ((u16)netdev->dev_addr[0]) << 8;
 815        macaddr_high += (u16)netdev->dev_addr[1];
 816        macaddr_low   = ((u32)netdev->dev_addr[2]) << 24;
 817        macaddr_low  += ((u32)netdev->dev_addr[3]) << 16;
 818        macaddr_low  += ((u32)netdev->dev_addr[4]) << 8;
 819        macaddr_low  += (u32)netdev->dev_addr[5];
 820
 821        for (i = 0; i < NES_MAX_PORT_COUNT; i++) {
 822                if (nesvnic->qp_nic_index[i] == 0xf) {
 823                        break;
 824                }
 825                nes_write_indexed(nesdev,
 826                                NES_IDX_PERFECT_FILTER_LOW + (nesvnic->qp_nic_index[i] * 8),
 827                                macaddr_low);
 828                nes_write_indexed(nesdev,
 829                                NES_IDX_PERFECT_FILTER_HIGH + (nesvnic->qp_nic_index[i] * 8),
 830                                (u32)macaddr_high | NES_MAC_ADDR_VALID |
 831                                ((((u32)nesvnic->nic_index) << 16)));
 832        }
 833        return 0;
 834}
 835
 836
 837static void set_allmulti(struct nes_device *nesdev, u32 nic_active_bit)
 838{
 839        u32 nic_active;
 840
 841        nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL);
 842        nic_active |= nic_active_bit;
 843        nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL, nic_active);
 844        nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL);
 845        nic_active &= ~nic_active_bit;
 846        nes_write_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL, nic_active);
 847}
 848
 849#define get_addr(addrs, index) ((addrs) + (index) * ETH_ALEN)
 850
 851/**
 852 * nes_netdev_set_multicast_list
 853 */
 854static void nes_netdev_set_multicast_list(struct net_device *netdev)
 855{
 856        struct nes_vnic *nesvnic = netdev_priv(netdev);
 857        struct nes_device *nesdev = nesvnic->nesdev;
 858        struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
 859        u32 nic_active_bit;
 860        u32 nic_active;
 861        u32 perfect_filter_register_address;
 862        u32 macaddr_low;
 863        u16 macaddr_high;
 864        u8 mc_all_on = 0;
 865        u8 mc_index;
 866        int mc_nic_index = -1;
 867        u8 pft_entries_preallocated = max(nesadapter->adapter_fcn_count *
 868                                        nics_per_function, 4);
 869        u8 max_pft_entries_avaiable = NES_PFT_SIZE - pft_entries_preallocated;
 870        unsigned long flags;
 871        int mc_count = netdev_mc_count(netdev);
 872
 873        spin_lock_irqsave(&nesadapter->resource_lock, flags);
 874        nic_active_bit = 1 << nesvnic->nic_index;
 875
 876        if (netdev->flags & IFF_PROMISC) {
 877                nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL);
 878                nic_active |= nic_active_bit;
 879                nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL, nic_active);
 880                nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL);
 881                nic_active |= nic_active_bit;
 882                nes_write_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL, nic_active);
 883                mc_all_on = 1;
 884        } else if ((netdev->flags & IFF_ALLMULTI) ||
 885                           (nesvnic->nic_index > 3)) {
 886                set_allmulti(nesdev, nic_active_bit);
 887                mc_all_on = 1;
 888        } else {
 889                nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL);
 890                nic_active &= ~nic_active_bit;
 891                nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL, nic_active);
 892                nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL);
 893                nic_active &= ~nic_active_bit;
 894                nes_write_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL, nic_active);
 895        }
 896
 897        nes_debug(NES_DBG_NIC_RX, "Number of MC entries = %d, Promiscuous = %d, All Multicast = %d.\n",
 898                  mc_count, !!(netdev->flags & IFF_PROMISC),
 899                  !!(netdev->flags & IFF_ALLMULTI));
 900        if (!mc_all_on) {
 901                char *addrs;
 902                int i;
 903                struct netdev_hw_addr *ha;
 904
 905                addrs = kmalloc_array(mc_count, ETH_ALEN, GFP_ATOMIC);
 906                if (!addrs) {
 907                        set_allmulti(nesdev, nic_active_bit);
 908                        goto unlock;
 909                }
 910                i = 0;
 911                netdev_for_each_mc_addr(ha, netdev)
 912                        memcpy(get_addr(addrs, i++), ha->addr, ETH_ALEN);
 913
 914                perfect_filter_register_address = NES_IDX_PERFECT_FILTER_LOW +
 915                                                pft_entries_preallocated * 0x8;
 916                for (i = 0, mc_index = 0; mc_index < max_pft_entries_avaiable;
 917                     mc_index++) {
 918                        while (i < mc_count && nesvnic->mcrq_mcast_filter &&
 919                        ((mc_nic_index = nesvnic->mcrq_mcast_filter(nesvnic,
 920                                        get_addr(addrs, i++))) == 0));
 921                        if (mc_nic_index < 0)
 922                                mc_nic_index = nesvnic->nic_index;
 923                        while (nesadapter->pft_mcast_map[mc_index] < 16 &&
 924                                nesadapter->pft_mcast_map[mc_index] !=
 925                                        nesvnic->nic_index &&
 926                                        mc_index < max_pft_entries_avaiable) {
 927                                nes_debug(NES_DBG_NIC_RX,
 928                                          "mc_index=%d skipping nic_index=%d, used for=%d\n",
 929                                          mc_index, nesvnic->nic_index,
 930                                          nesadapter->pft_mcast_map[mc_index]);
 931                                mc_index++;
 932                        }
 933                        if (mc_index >= max_pft_entries_avaiable)
 934                                break;
 935                        if (i < mc_count) {
 936                                char *addr = get_addr(addrs, i++);
 937
 938                                nes_debug(NES_DBG_NIC_RX, "Assigning MC Address %pM to register 0x%04X nic_idx=%d\n",
 939                                          addr,
 940                                          perfect_filter_register_address+(mc_index * 8),
 941                                          mc_nic_index);
 942                                macaddr_high  = ((u8) addr[0]) << 8;
 943                                macaddr_high += (u8) addr[1];
 944                                macaddr_low   = ((u8) addr[2]) << 24;
 945                                macaddr_low  += ((u8) addr[3]) << 16;
 946                                macaddr_low  += ((u8) addr[4]) << 8;
 947                                macaddr_low  += (u8) addr[5];
 948
 949                                nes_write_indexed(nesdev,
 950                                                perfect_filter_register_address+(mc_index * 8),
 951                                                macaddr_low);
 952                                nes_write_indexed(nesdev,
 953                                                perfect_filter_register_address+4+(mc_index * 8),
 954                                                (u32)macaddr_high | NES_MAC_ADDR_VALID |
 955                                                ((((u32)(1<<mc_nic_index)) << 16)));
 956                                nesadapter->pft_mcast_map[mc_index] =
 957                                                        nesvnic->nic_index;
 958                        } else {
 959                                nes_debug(NES_DBG_NIC_RX, "Clearing MC Address at register 0x%04X\n",
 960                                                  perfect_filter_register_address+(mc_index * 8));
 961                                nes_write_indexed(nesdev,
 962                                                perfect_filter_register_address+4+(mc_index * 8),
 963                                                0);
 964                                nesadapter->pft_mcast_map[mc_index] = 255;
 965                        }
 966                }
 967                kfree(addrs);
 968                /* PFT is not large enough */
 969                if (i < mc_count)
 970                        set_allmulti(nesdev, nic_active_bit);
 971        }
 972
 973unlock:
 974        spin_unlock_irqrestore(&nesadapter->resource_lock, flags);
 975}
 976
 977
 978/**
 979 * nes_netdev_change_mtu
 980 */
 981static int nes_netdev_change_mtu(struct net_device *netdev, int new_mtu)
 982{
 983        struct nes_vnic *nesvnic = netdev_priv(netdev);
 984        struct nes_device *nesdev = nesvnic->nesdev;
 985        u8 jumbomode = 0;
 986        u32 nic_active;
 987        u32 nic_active_bit;
 988        u32 uc_all_active;
 989        u32 mc_all_active;
 990
 991        netdev->mtu = new_mtu;
 992        nesvnic->max_frame_size = new_mtu + VLAN_ETH_HLEN;
 993
 994        if (netdev->mtu > ETH_DATA_LEN) {
 995                jumbomode=1;
 996        }
 997        nes_nic_init_timer_defaults(nesdev, jumbomode);
 998
 999        if (netif_running(netdev)) {
1000                nic_active_bit = 1 << nesvnic->nic_index;
1001                mc_all_active = nes_read_indexed(nesdev,
1002                                NES_IDX_NIC_MULTICAST_ALL) & nic_active_bit;
1003                uc_all_active = nes_read_indexed(nesdev,
1004                                NES_IDX_NIC_UNICAST_ALL)  & nic_active_bit;
1005
1006                nes_netdev_stop(netdev);
1007                nes_netdev_open(netdev);
1008
1009                nic_active = nes_read_indexed(nesdev,
1010                                        NES_IDX_NIC_MULTICAST_ALL);
1011                nic_active |= mc_all_active;
1012                nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL,
1013                                                        nic_active);
1014
1015                nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL);
1016                nic_active |= uc_all_active;
1017                nes_write_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL, nic_active);
1018        }
1019
1020        return 0;
1021}
1022
1023
1024static const char nes_ethtool_stringset[][ETH_GSTRING_LEN] = {
1025        "Link Change Interrupts",
1026        "Linearized SKBs",
1027        "T/GSO Requests",
1028        "Pause Frames Sent",
1029        "Pause Frames Received",
1030        "Internal Routing Errors",
1031        "SQ SW Dropped SKBs",
1032        "SQ Full",
1033        "Segmented TSO Requests",
1034        "Rx Symbol Errors",
1035        "Rx Jabber Errors",
1036        "Rx Oversized Frames",
1037        "Rx Short Frames",
1038        "Rx Length Errors",
1039        "Rx CRC Errors",
1040        "Rx Port Discard",
1041        "Endnode Rx Discards",
1042        "Endnode Rx Octets",
1043        "Endnode Rx Frames",
1044        "Endnode Tx Octets",
1045        "Endnode Tx Frames",
1046        "Tx Errors",
1047        "mh detected",
1048        "mh pauses",
1049        "Retransmission Count",
1050        "CM Connects",
1051        "CM Accepts",
1052        "Disconnects",
1053        "Connected Events",
1054        "Connect Requests",
1055        "CM Rejects",
1056        "ModifyQP Timeouts",
1057        "CreateQPs",
1058        "SW DestroyQPs",
1059        "DestroyQPs",
1060        "CM Closes",
1061        "CM Packets Sent",
1062        "CM Packets Bounced",
1063        "CM Packets Created",
1064        "CM Packets Rcvd",
1065        "CM Packets Dropped",
1066        "CM Packets Retrans",
1067        "CM Listens Created",
1068        "CM Listens Destroyed",
1069        "CM Backlog Drops",
1070        "CM Loopbacks",
1071        "CM Nodes Created",
1072        "CM Nodes Destroyed",
1073        "CM Accel Drops",
1074        "CM Resets Received",
1075        "Free 4Kpbls",
1076        "Free 256pbls",
1077        "Timer Inits",
1078        "PAU CreateQPs",
1079        "PAU DestroyQPs",
1080};
1081#define NES_ETHTOOL_STAT_COUNT  ARRAY_SIZE(nes_ethtool_stringset)
1082
1083
1084/**
1085 * nes_netdev_get_sset_count
1086 */
1087static int nes_netdev_get_sset_count(struct net_device *netdev, int stringset)
1088{
1089        if (stringset == ETH_SS_STATS)
1090                return NES_ETHTOOL_STAT_COUNT;
1091        else
1092                return -EINVAL;
1093}
1094
1095
1096/**
1097 * nes_netdev_get_strings
1098 */
1099static void nes_netdev_get_strings(struct net_device *netdev, u32 stringset,
1100                u8 *ethtool_strings)
1101{
1102        if (stringset == ETH_SS_STATS)
1103                memcpy(ethtool_strings,
1104                                &nes_ethtool_stringset,
1105                                sizeof(nes_ethtool_stringset));
1106}
1107
1108
1109/**
1110 * nes_netdev_get_ethtool_stats
1111 */
1112
1113static void nes_netdev_get_ethtool_stats(struct net_device *netdev,
1114                struct ethtool_stats *target_ethtool_stats, u64 *target_stat_values)
1115{
1116        u64 u64temp;
1117        struct nes_vnic *nesvnic = netdev_priv(netdev);
1118        struct nes_device *nesdev = nesvnic->nesdev;
1119        struct nes_adapter *nesadapter = nesdev->nesadapter;
1120        u32 nic_count;
1121        u32 u32temp;
1122        u32 index = 0;
1123
1124        target_ethtool_stats->n_stats = NES_ETHTOOL_STAT_COUNT;
1125        target_stat_values[index] = nesvnic->nesdev->link_status_interrupts;
1126        target_stat_values[++index] = nesvnic->linearized_skbs;
1127        target_stat_values[++index] = nesvnic->tso_requests;
1128
1129        u32temp = nes_read_indexed(nesdev,
1130                        NES_IDX_MAC_TX_PAUSE_FRAMES + (nesvnic->nesdev->mac_index*0x200));
1131        nesvnic->nesdev->mac_pause_frames_sent += u32temp;
1132        target_stat_values[++index] = nesvnic->nesdev->mac_pause_frames_sent;
1133
1134        u32temp = nes_read_indexed(nesdev,
1135                        NES_IDX_MAC_RX_PAUSE_FRAMES + (nesvnic->nesdev->mac_index*0x200));
1136        nesvnic->nesdev->mac_pause_frames_received += u32temp;
1137
1138        u32temp = nes_read_indexed(nesdev,
1139                        NES_IDX_PORT_RX_DISCARDS + (nesvnic->nesdev->mac_index*0x40));
1140        nesvnic->nesdev->port_rx_discards += u32temp;
1141        nesvnic->netstats.rx_dropped += u32temp;
1142
1143        u32temp = nes_read_indexed(nesdev,
1144                        NES_IDX_PORT_TX_DISCARDS + (nesvnic->nesdev->mac_index*0x40));
1145        nesvnic->nesdev->port_tx_discards += u32temp;
1146        nesvnic->netstats.tx_dropped += u32temp;
1147
1148        u32temp = nes_read_indexed(nesdev,
1149                        NES_IDX_MAC_RX_SHORT_FRAMES + (nesvnic->nesdev->mac_index*0x200));
1150        nesvnic->netstats.rx_dropped += u32temp;
1151        nesvnic->nesdev->mac_rx_errors += u32temp;
1152        nesvnic->nesdev->mac_rx_short_frames += u32temp;
1153
1154        u32temp = nes_read_indexed(nesdev,
1155                        NES_IDX_MAC_RX_OVERSIZED_FRAMES + (nesvnic->nesdev->mac_index*0x200));
1156        nesvnic->netstats.rx_dropped += u32temp;
1157        nesvnic->nesdev->mac_rx_errors += u32temp;
1158        nesvnic->nesdev->mac_rx_oversized_frames += u32temp;
1159
1160        u32temp = nes_read_indexed(nesdev,
1161                        NES_IDX_MAC_RX_JABBER_FRAMES + (nesvnic->nesdev->mac_index*0x200));
1162        nesvnic->netstats.rx_dropped += u32temp;
1163        nesvnic->nesdev->mac_rx_errors += u32temp;
1164        nesvnic->nesdev->mac_rx_jabber_frames += u32temp;
1165
1166        u32temp = nes_read_indexed(nesdev,
1167                        NES_IDX_MAC_RX_SYMBOL_ERR_FRAMES + (nesvnic->nesdev->mac_index*0x200));
1168        nesvnic->netstats.rx_dropped += u32temp;
1169        nesvnic->nesdev->mac_rx_errors += u32temp;
1170        nesvnic->nesdev->mac_rx_symbol_err_frames += u32temp;
1171
1172        u32temp = nes_read_indexed(nesdev,
1173                        NES_IDX_MAC_RX_LENGTH_ERR_FRAMES + (nesvnic->nesdev->mac_index*0x200));
1174        nesvnic->netstats.rx_length_errors += u32temp;
1175        nesvnic->nesdev->mac_rx_errors += u32temp;
1176
1177        u32temp = nes_read_indexed(nesdev,
1178                        NES_IDX_MAC_RX_CRC_ERR_FRAMES + (nesvnic->nesdev->mac_index*0x200));
1179        nesvnic->nesdev->mac_rx_errors += u32temp;
1180        nesvnic->nesdev->mac_rx_crc_errors += u32temp;
1181        nesvnic->netstats.rx_crc_errors += u32temp;
1182
1183        u32temp = nes_read_indexed(nesdev,
1184                        NES_IDX_MAC_TX_ERRORS + (nesvnic->nesdev->mac_index*0x200));
1185        nesvnic->nesdev->mac_tx_errors += u32temp;
1186        nesvnic->netstats.tx_errors += u32temp;
1187
1188        for (nic_count = 0; nic_count < NES_MAX_PORT_COUNT; nic_count++) {
1189                if (nesvnic->qp_nic_index[nic_count] == 0xf)
1190                        break;
1191
1192                u32temp = nes_read_indexed(nesdev,
1193                                NES_IDX_ENDNODE0_NSTAT_RX_DISCARD +
1194                                (nesvnic->qp_nic_index[nic_count]*0x200));
1195                nesvnic->netstats.rx_dropped += u32temp;
1196                nesvnic->endnode_nstat_rx_discard += u32temp;
1197
1198                u64temp = (u64)nes_read_indexed(nesdev,
1199                                NES_IDX_ENDNODE0_NSTAT_RX_OCTETS_LO +
1200                                (nesvnic->qp_nic_index[nic_count]*0x200));
1201                u64temp += ((u64)nes_read_indexed(nesdev,
1202                                NES_IDX_ENDNODE0_NSTAT_RX_OCTETS_HI +
1203                                (nesvnic->qp_nic_index[nic_count]*0x200))) << 32;
1204
1205                nesvnic->endnode_nstat_rx_octets += u64temp;
1206                nesvnic->netstats.rx_bytes += u64temp;
1207
1208                u64temp = (u64)nes_read_indexed(nesdev,
1209                                NES_IDX_ENDNODE0_NSTAT_RX_FRAMES_LO +
1210                                (nesvnic->qp_nic_index[nic_count]*0x200));
1211                u64temp += ((u64)nes_read_indexed(nesdev,
1212                                NES_IDX_ENDNODE0_NSTAT_RX_FRAMES_HI +
1213                                (nesvnic->qp_nic_index[nic_count]*0x200))) << 32;
1214
1215                nesvnic->endnode_nstat_rx_frames += u64temp;
1216                nesvnic->netstats.rx_packets += u64temp;
1217
1218                u64temp = (u64)nes_read_indexed(nesdev,
1219                                NES_IDX_ENDNODE0_NSTAT_TX_OCTETS_LO +
1220                                (nesvnic->qp_nic_index[nic_count]*0x200));
1221                u64temp += ((u64)nes_read_indexed(nesdev,
1222                                NES_IDX_ENDNODE0_NSTAT_TX_OCTETS_HI +
1223                                (nesvnic->qp_nic_index[nic_count]*0x200))) << 32;
1224
1225                nesvnic->endnode_nstat_tx_octets += u64temp;
1226                nesvnic->netstats.tx_bytes += u64temp;
1227
1228                u64temp = (u64)nes_read_indexed(nesdev,
1229                                NES_IDX_ENDNODE0_NSTAT_TX_FRAMES_LO +
1230                                (nesvnic->qp_nic_index[nic_count]*0x200));
1231                u64temp += ((u64)nes_read_indexed(nesdev,
1232                                NES_IDX_ENDNODE0_NSTAT_TX_FRAMES_HI +
1233                                (nesvnic->qp_nic_index[nic_count]*0x200))) << 32;
1234
1235                nesvnic->endnode_nstat_tx_frames += u64temp;
1236                nesvnic->netstats.tx_packets += u64temp;
1237
1238                u32temp = nes_read_indexed(nesdev,
1239                                NES_IDX_IPV4_TCP_REXMITS + (nesvnic->qp_nic_index[nic_count]*0x200));
1240                nesvnic->endnode_ipv4_tcp_retransmits += u32temp;
1241        }
1242
1243        target_stat_values[++index] = nesvnic->nesdev->mac_pause_frames_received;
1244        target_stat_values[++index] = nesdev->nesadapter->nic_rx_eth_route_err;
1245        target_stat_values[++index] = nesvnic->tx_sw_dropped;
1246        target_stat_values[++index] = nesvnic->sq_full;
1247        target_stat_values[++index] = nesvnic->segmented_tso_requests;
1248        target_stat_values[++index] = nesvnic->nesdev->mac_rx_symbol_err_frames;
1249        target_stat_values[++index] = nesvnic->nesdev->mac_rx_jabber_frames;
1250        target_stat_values[++index] = nesvnic->nesdev->mac_rx_oversized_frames;
1251        target_stat_values[++index] = nesvnic->nesdev->mac_rx_short_frames;
1252        target_stat_values[++index] = nesvnic->netstats.rx_length_errors;
1253        target_stat_values[++index] = nesvnic->nesdev->mac_rx_crc_errors;
1254        target_stat_values[++index] = nesvnic->nesdev->port_rx_discards;
1255        target_stat_values[++index] = nesvnic->endnode_nstat_rx_discard;
1256        target_stat_values[++index] = nesvnic->endnode_nstat_rx_octets;
1257        target_stat_values[++index] = nesvnic->endnode_nstat_rx_frames;
1258        target_stat_values[++index] = nesvnic->endnode_nstat_tx_octets;
1259        target_stat_values[++index] = nesvnic->endnode_nstat_tx_frames;
1260        target_stat_values[++index] = nesvnic->nesdev->mac_tx_errors;
1261        target_stat_values[++index] = mh_detected;
1262        target_stat_values[++index] = mh_pauses_sent;
1263        target_stat_values[++index] = nesvnic->endnode_ipv4_tcp_retransmits;
1264        target_stat_values[++index] = atomic_read(&cm_connects);
1265        target_stat_values[++index] = atomic_read(&cm_accepts);
1266        target_stat_values[++index] = atomic_read(&cm_disconnects);
1267        target_stat_values[++index] = atomic_read(&cm_connecteds);
1268        target_stat_values[++index] = atomic_read(&cm_connect_reqs);
1269        target_stat_values[++index] = atomic_read(&cm_rejects);
1270        target_stat_values[++index] = atomic_read(&mod_qp_timouts);
1271        target_stat_values[++index] = atomic_read(&qps_created);
1272        target_stat_values[++index] = atomic_read(&sw_qps_destroyed);
1273        target_stat_values[++index] = atomic_read(&qps_destroyed);
1274        target_stat_values[++index] = atomic_read(&cm_closes);
1275        target_stat_values[++index] = cm_packets_sent;
1276        target_stat_values[++index] = cm_packets_bounced;
1277        target_stat_values[++index] = cm_packets_created;
1278        target_stat_values[++index] = cm_packets_received;
1279        target_stat_values[++index] = cm_packets_dropped;
1280        target_stat_values[++index] = cm_packets_retrans;
1281        target_stat_values[++index] = atomic_read(&cm_listens_created);
1282        target_stat_values[++index] = atomic_read(&cm_listens_destroyed);
1283        target_stat_values[++index] = cm_backlog_drops;
1284        target_stat_values[++index] = atomic_read(&cm_loopbacks);
1285        target_stat_values[++index] = atomic_read(&cm_nodes_created);
1286        target_stat_values[++index] = atomic_read(&cm_nodes_destroyed);
1287        target_stat_values[++index] = atomic_read(&cm_accel_dropped_pkts);
1288        target_stat_values[++index] = atomic_read(&cm_resets_recvd);
1289        target_stat_values[++index] = nesadapter->free_4kpbl;
1290        target_stat_values[++index] = nesadapter->free_256pbl;
1291        target_stat_values[++index] = int_mod_timer_init;
1292        target_stat_values[++index] = atomic_read(&pau_qps_created);
1293        target_stat_values[++index] = atomic_read(&pau_qps_destroyed);
1294}
1295
1296/**
1297 * nes_netdev_get_drvinfo
1298 */
1299static void nes_netdev_get_drvinfo(struct net_device *netdev,
1300                struct ethtool_drvinfo *drvinfo)
1301{
1302        struct nes_vnic *nesvnic = netdev_priv(netdev);
1303        struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
1304
1305        strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
1306        strlcpy(drvinfo->bus_info, pci_name(nesvnic->nesdev->pcidev),
1307                sizeof(drvinfo->bus_info));
1308        snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
1309                 "%u.%u", nesadapter->firmware_version >> 16,
1310                 nesadapter->firmware_version & 0x000000ff);
1311        strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
1312}
1313
1314
1315/**
1316 * nes_netdev_set_coalesce
1317 */
1318static int nes_netdev_set_coalesce(struct net_device *netdev,
1319                struct ethtool_coalesce *et_coalesce)
1320{
1321        struct nes_vnic *nesvnic = netdev_priv(netdev);
1322        struct nes_device *nesdev = nesvnic->nesdev;
1323        struct nes_adapter *nesadapter = nesdev->nesadapter;
1324        struct nes_hw_tune_timer *shared_timer = &nesadapter->tune_timer;
1325        unsigned long flags;
1326
1327        spin_lock_irqsave(&nesadapter->periodic_timer_lock, flags);
1328        if (et_coalesce->rx_max_coalesced_frames_low) {
1329                shared_timer->threshold_low = et_coalesce->rx_max_coalesced_frames_low;
1330        }
1331        if (et_coalesce->rx_max_coalesced_frames_irq) {
1332                shared_timer->threshold_target = et_coalesce->rx_max_coalesced_frames_irq;
1333        }
1334        if (et_coalesce->rx_max_coalesced_frames_high) {
1335                shared_timer->threshold_high = et_coalesce->rx_max_coalesced_frames_high;
1336        }
1337        if (et_coalesce->rx_coalesce_usecs_low) {
1338                shared_timer->timer_in_use_min = et_coalesce->rx_coalesce_usecs_low;
1339        }
1340        if (et_coalesce->rx_coalesce_usecs_high) {
1341                shared_timer->timer_in_use_max = et_coalesce->rx_coalesce_usecs_high;
1342        }
1343        spin_unlock_irqrestore(&nesadapter->periodic_timer_lock, flags);
1344
1345        /* using this to drive total interrupt moderation */
1346        nesadapter->et_rx_coalesce_usecs_irq = et_coalesce->rx_coalesce_usecs_irq;
1347        if (et_coalesce->use_adaptive_rx_coalesce) {
1348                nesadapter->et_use_adaptive_rx_coalesce = 1;
1349                nesadapter->timer_int_limit = NES_TIMER_INT_LIMIT_DYNAMIC;
1350                nesadapter->et_rx_coalesce_usecs_irq = 0;
1351                if (et_coalesce->pkt_rate_low) {
1352                        nesadapter->et_pkt_rate_low = et_coalesce->pkt_rate_low;
1353                }
1354        } else {
1355                nesadapter->et_use_adaptive_rx_coalesce = 0;
1356                nesadapter->timer_int_limit = NES_TIMER_INT_LIMIT;
1357                if (nesadapter->et_rx_coalesce_usecs_irq) {
1358                        nes_write32(nesdev->regs+NES_PERIODIC_CONTROL,
1359                                        0x80000000 | ((u32)(nesadapter->et_rx_coalesce_usecs_irq*8)));
1360                }
1361        }
1362        return 0;
1363}
1364
1365
1366/**
1367 * nes_netdev_get_coalesce
1368 */
1369static int nes_netdev_get_coalesce(struct net_device *netdev,
1370                struct ethtool_coalesce *et_coalesce)
1371{
1372        struct nes_vnic *nesvnic = netdev_priv(netdev);
1373        struct nes_device *nesdev = nesvnic->nesdev;
1374        struct nes_adapter *nesadapter = nesdev->nesadapter;
1375        struct ethtool_coalesce temp_et_coalesce;
1376        struct nes_hw_tune_timer *shared_timer = &nesadapter->tune_timer;
1377        unsigned long flags;
1378
1379        memset(&temp_et_coalesce, 0, sizeof(temp_et_coalesce));
1380        temp_et_coalesce.rx_coalesce_usecs_irq    = nesadapter->et_rx_coalesce_usecs_irq;
1381        temp_et_coalesce.use_adaptive_rx_coalesce = nesadapter->et_use_adaptive_rx_coalesce;
1382        temp_et_coalesce.rate_sample_interval     = nesadapter->et_rate_sample_interval;
1383        temp_et_coalesce.pkt_rate_low = nesadapter->et_pkt_rate_low;
1384        spin_lock_irqsave(&nesadapter->periodic_timer_lock,     flags);
1385        temp_et_coalesce.rx_max_coalesced_frames_low  = shared_timer->threshold_low;
1386        temp_et_coalesce.rx_max_coalesced_frames_irq  = shared_timer->threshold_target;
1387        temp_et_coalesce.rx_max_coalesced_frames_high = shared_timer->threshold_high;
1388        temp_et_coalesce.rx_coalesce_usecs_low  = shared_timer->timer_in_use_min;
1389        temp_et_coalesce.rx_coalesce_usecs_high = shared_timer->timer_in_use_max;
1390        if (nesadapter->et_use_adaptive_rx_coalesce) {
1391                temp_et_coalesce.rx_coalesce_usecs_irq = shared_timer->timer_in_use;
1392        }
1393        spin_unlock_irqrestore(&nesadapter->periodic_timer_lock, flags);
1394        memcpy(et_coalesce, &temp_et_coalesce, sizeof(*et_coalesce));
1395        return 0;
1396}
1397
1398
1399/**
1400 * nes_netdev_get_pauseparam
1401 */
1402static void nes_netdev_get_pauseparam(struct net_device *netdev,
1403                struct ethtool_pauseparam *et_pauseparam)
1404{
1405        struct nes_vnic *nesvnic = netdev_priv(netdev);
1406
1407        et_pauseparam->autoneg = 0;
1408        et_pauseparam->rx_pause = (nesvnic->nesdev->disable_rx_flow_control == 0) ? 1:0;
1409        et_pauseparam->tx_pause = (nesvnic->nesdev->disable_tx_flow_control == 0) ? 1:0;
1410}
1411
1412
1413/**
1414 * nes_netdev_set_pauseparam
1415 */
1416static int nes_netdev_set_pauseparam(struct net_device *netdev,
1417                struct ethtool_pauseparam *et_pauseparam)
1418{
1419        struct nes_vnic *nesvnic = netdev_priv(netdev);
1420        struct nes_device *nesdev = nesvnic->nesdev;
1421        u32 u32temp;
1422
1423        if (et_pauseparam->autoneg) {
1424                /* TODO: should return unsupported */
1425                return 0;
1426        }
1427        if ((et_pauseparam->tx_pause == 1) && (nesdev->disable_tx_flow_control == 1)) {
1428                u32temp = nes_read_indexed(nesdev,
1429                                NES_IDX_MAC_TX_CONFIG + (nesdev->mac_index*0x200));
1430                u32temp |= NES_IDX_MAC_TX_CONFIG_ENABLE_PAUSE;
1431                nes_write_indexed(nesdev,
1432                                NES_IDX_MAC_TX_CONFIG + (nesdev->mac_index*0x200), u32temp);
1433                nesdev->disable_tx_flow_control = 0;
1434        } else if ((et_pauseparam->tx_pause == 0) && (nesdev->disable_tx_flow_control == 0)) {
1435                u32temp = nes_read_indexed(nesdev,
1436                                NES_IDX_MAC_TX_CONFIG + (nesdev->mac_index*0x200));
1437                u32temp &= ~NES_IDX_MAC_TX_CONFIG_ENABLE_PAUSE;
1438                nes_write_indexed(nesdev,
1439                                NES_IDX_MAC_TX_CONFIG + (nesdev->mac_index*0x200), u32temp);
1440                nesdev->disable_tx_flow_control = 1;
1441        }
1442        if ((et_pauseparam->rx_pause == 1) && (nesdev->disable_rx_flow_control == 1)) {
1443                u32temp = nes_read_indexed(nesdev,
1444                                NES_IDX_MPP_DEBUG + (nesdev->mac_index*0x40));
1445                u32temp &= ~NES_IDX_MPP_DEBUG_PORT_DISABLE_PAUSE;
1446                nes_write_indexed(nesdev,
1447                                NES_IDX_MPP_DEBUG + (nesdev->mac_index*0x40), u32temp);
1448                nesdev->disable_rx_flow_control = 0;
1449        } else if ((et_pauseparam->rx_pause == 0) && (nesdev->disable_rx_flow_control == 0)) {
1450                u32temp = nes_read_indexed(nesdev,
1451                                NES_IDX_MPP_DEBUG + (nesdev->mac_index*0x40));
1452                u32temp |= NES_IDX_MPP_DEBUG_PORT_DISABLE_PAUSE;
1453                nes_write_indexed(nesdev,
1454                                NES_IDX_MPP_DEBUG + (nesdev->mac_index*0x40), u32temp);
1455                nesdev->disable_rx_flow_control = 1;
1456        }
1457
1458        return 0;
1459}
1460
1461
1462/**
1463 * nes_netdev_get_settings
1464 */
1465static int nes_netdev_get_link_ksettings(struct net_device *netdev,
1466                                         struct ethtool_link_ksettings *cmd)
1467{
1468        struct nes_vnic *nesvnic = netdev_priv(netdev);
1469        struct nes_device *nesdev = nesvnic->nesdev;
1470        struct nes_adapter *nesadapter = nesdev->nesadapter;
1471        u32 mac_index = nesdev->mac_index;
1472        u8 phy_type = nesadapter->phy_type[mac_index];
1473        u8 phy_index = nesadapter->phy_index[mac_index];
1474        u16 phy_data;
1475        u32 supported, advertising;
1476
1477        cmd->base.duplex = DUPLEX_FULL;
1478        cmd->base.port   = PORT_MII;
1479
1480        if (nesadapter->OneG_Mode) {
1481                cmd->base.speed = SPEED_1000;
1482                if (phy_type == NES_PHY_TYPE_PUMA_1G) {
1483                        supported   = SUPPORTED_1000baseT_Full;
1484                        advertising = ADVERTISED_1000baseT_Full;
1485                        cmd->base.autoneg     = AUTONEG_DISABLE;
1486                        cmd->base.phy_address = mac_index;
1487                } else {
1488                        unsigned long flags;
1489
1490                        supported = SUPPORTED_1000baseT_Full
1491                                | SUPPORTED_Autoneg;
1492                        advertising = ADVERTISED_1000baseT_Full
1493                                | ADVERTISED_Autoneg;
1494                        spin_lock_irqsave(&nesadapter->phy_lock, flags);
1495                        nes_read_1G_phy_reg(nesdev, 0, phy_index, &phy_data);
1496                        spin_unlock_irqrestore(&nesadapter->phy_lock, flags);
1497                        if (phy_data & 0x1000)
1498                                cmd->base.autoneg = AUTONEG_ENABLE;
1499                        else
1500                                cmd->base.autoneg = AUTONEG_DISABLE;
1501                        cmd->base.phy_address = phy_index;
1502                }
1503                ethtool_convert_legacy_u32_to_link_mode(
1504                        cmd->link_modes.supported, supported);
1505                ethtool_convert_legacy_u32_to_link_mode(
1506                        cmd->link_modes.advertising, advertising);
1507                return 0;
1508        }
1509        if ((phy_type == NES_PHY_TYPE_ARGUS) ||
1510            (phy_type == NES_PHY_TYPE_SFP_D) ||
1511            (phy_type == NES_PHY_TYPE_KR)) {
1512                cmd->base.port        = PORT_FIBRE;
1513                supported   = SUPPORTED_FIBRE;
1514                advertising = ADVERTISED_FIBRE;
1515                cmd->base.phy_address = phy_index;
1516        } else {
1517                supported   = SUPPORTED_10000baseT_Full;
1518                advertising = ADVERTISED_10000baseT_Full;
1519                cmd->base.phy_address = mac_index;
1520        }
1521        cmd->base.speed = SPEED_10000;
1522        cmd->base.autoneg = AUTONEG_DISABLE;
1523        ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1524                                                supported);
1525        ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1526                                                advertising);
1527
1528        return 0;
1529}
1530
1531
1532/**
1533 * nes_netdev_set_settings
1534 */
1535static int
1536nes_netdev_set_link_ksettings(struct net_device *netdev,
1537                              const struct ethtool_link_ksettings *cmd)
1538{
1539        struct nes_vnic *nesvnic = netdev_priv(netdev);
1540        struct nes_device *nesdev = nesvnic->nesdev;
1541        struct nes_adapter *nesadapter = nesdev->nesadapter;
1542
1543        if ((nesadapter->OneG_Mode) &&
1544            (nesadapter->phy_type[nesdev->mac_index] != NES_PHY_TYPE_PUMA_1G)) {
1545                unsigned long flags;
1546                u16 phy_data;
1547                u8 phy_index = nesadapter->phy_index[nesdev->mac_index];
1548
1549                spin_lock_irqsave(&nesadapter->phy_lock, flags);
1550                nes_read_1G_phy_reg(nesdev, 0, phy_index, &phy_data);
1551                if (cmd->base.autoneg) {
1552                        /* Turn on Full duplex, Autoneg, and restart autonegotiation */
1553                        phy_data |= 0x1300;
1554                } else {
1555                        /* Turn off autoneg */
1556                        phy_data &= ~0x1000;
1557                }
1558                nes_write_1G_phy_reg(nesdev, 0, phy_index, phy_data);
1559                spin_unlock_irqrestore(&nesadapter->phy_lock, flags);
1560        }
1561
1562        return 0;
1563}
1564
1565
1566static const struct ethtool_ops nes_ethtool_ops = {
1567        .get_link = ethtool_op_get_link,
1568        .get_strings = nes_netdev_get_strings,
1569        .get_sset_count = nes_netdev_get_sset_count,
1570        .get_ethtool_stats = nes_netdev_get_ethtool_stats,
1571        .get_drvinfo = nes_netdev_get_drvinfo,
1572        .get_coalesce = nes_netdev_get_coalesce,
1573        .set_coalesce = nes_netdev_set_coalesce,
1574        .get_pauseparam = nes_netdev_get_pauseparam,
1575        .set_pauseparam = nes_netdev_set_pauseparam,
1576        .get_link_ksettings = nes_netdev_get_link_ksettings,
1577        .set_link_ksettings = nes_netdev_set_link_ksettings,
1578};
1579
1580static void nes_vlan_mode(struct net_device *netdev, struct nes_device *nesdev, netdev_features_t features)
1581{
1582        struct nes_adapter *nesadapter = nesdev->nesadapter;
1583        u32 u32temp;
1584        unsigned long flags;
1585
1586        spin_lock_irqsave(&nesadapter->phy_lock, flags);
1587
1588        nes_debug(NES_DBG_NETDEV, "%s: %s\n", __func__, netdev->name);
1589
1590        /* Enable/Disable VLAN Stripping */
1591        u32temp = nes_read_indexed(nesdev, NES_IDX_PCIX_DIAG);
1592        if (features & NETIF_F_HW_VLAN_CTAG_RX)
1593                u32temp &= 0xfdffffff;
1594        else
1595                u32temp |= 0x02000000;
1596
1597        nes_write_indexed(nesdev, NES_IDX_PCIX_DIAG, u32temp);
1598        spin_unlock_irqrestore(&nesadapter->phy_lock, flags);
1599}
1600
1601static netdev_features_t nes_fix_features(struct net_device *netdev, netdev_features_t features)
1602{
1603        /*
1604         * Since there is no support for separate rx/tx vlan accel
1605         * enable/disable make sure tx flag is always in same state as rx.
1606         */
1607        if (features & NETIF_F_HW_VLAN_CTAG_RX)
1608                features |= NETIF_F_HW_VLAN_CTAG_TX;
1609        else
1610                features &= ~NETIF_F_HW_VLAN_CTAG_TX;
1611
1612        return features;
1613}
1614
1615static int nes_set_features(struct net_device *netdev, netdev_features_t features)
1616{
1617        struct nes_vnic *nesvnic = netdev_priv(netdev);
1618        struct nes_device *nesdev = nesvnic->nesdev;
1619        u32 changed = netdev->features ^ features;
1620
1621        if (changed & NETIF_F_HW_VLAN_CTAG_RX)
1622                nes_vlan_mode(netdev, nesdev, features);
1623
1624        return 0;
1625}
1626
1627static const struct net_device_ops nes_netdev_ops = {
1628        .ndo_open               = nes_netdev_open,
1629        .ndo_stop               = nes_netdev_stop,
1630        .ndo_start_xmit         = nes_netdev_start_xmit,
1631        .ndo_get_stats          = nes_netdev_get_stats,
1632        .ndo_tx_timeout         = nes_netdev_tx_timeout,
1633        .ndo_set_mac_address    = nes_netdev_set_mac_address,
1634        .ndo_set_rx_mode        = nes_netdev_set_multicast_list,
1635        .ndo_change_mtu         = nes_netdev_change_mtu,
1636        .ndo_validate_addr      = eth_validate_addr,
1637        .ndo_fix_features       = nes_fix_features,
1638        .ndo_set_features       = nes_set_features,
1639};
1640
1641/**
1642 * nes_netdev_init - initialize network device
1643 */
1644struct net_device *nes_netdev_init(struct nes_device *nesdev,
1645                void __iomem *mmio_addr)
1646{
1647        u64 u64temp;
1648        struct nes_vnic *nesvnic;
1649        struct net_device *netdev;
1650        struct nic_qp_map *curr_qp_map;
1651        u8 phy_type = nesdev->nesadapter->phy_type[nesdev->mac_index];
1652
1653        netdev = alloc_etherdev(sizeof(struct nes_vnic));
1654        if (!netdev) {
1655                printk(KERN_ERR PFX "nesvnic etherdev alloc failed");
1656                return NULL;
1657        }
1658        nesvnic = netdev_priv(netdev);
1659
1660        nes_debug(NES_DBG_INIT, "netdev = %p, %s\n", netdev, netdev->name);
1661
1662        SET_NETDEV_DEV(netdev, &nesdev->pcidev->dev);
1663
1664        netdev->watchdog_timeo = NES_TX_TIMEOUT;
1665        netdev->irq = nesdev->pcidev->irq;
1666        netdev->max_mtu = NES_MAX_MTU;
1667        netdev->hard_header_len = ETH_HLEN;
1668        netdev->addr_len = ETH_ALEN;
1669        netdev->type = ARPHRD_ETHER;
1670        netdev->netdev_ops = &nes_netdev_ops;
1671        netdev->ethtool_ops = &nes_ethtool_ops;
1672        netif_napi_add(netdev, &nesvnic->napi, nes_netdev_poll, 128);
1673        nes_debug(NES_DBG_INIT, "Enabling VLAN Insert/Delete.\n");
1674
1675        /* Fill in the port structure */
1676        nesvnic->netdev = netdev;
1677        nesvnic->nesdev = nesdev;
1678        nesvnic->msg_enable = netif_msg_init(debug, default_msg);
1679        nesvnic->netdev_index = nesdev->netdev_count;
1680        nesvnic->perfect_filter_index = nesdev->nesadapter->netdev_count;
1681        nesvnic->max_frame_size = netdev->mtu + netdev->hard_header_len + VLAN_HLEN;
1682
1683        curr_qp_map = nic_qp_mapping_per_function[PCI_FUNC(nesdev->pcidev->devfn)];
1684        nesvnic->nic.qp_id = curr_qp_map[nesdev->netdev_count].qpid;
1685        nesvnic->nic_index = curr_qp_map[nesdev->netdev_count].nic_index;
1686        nesvnic->logical_port = curr_qp_map[nesdev->netdev_count].logical_port;
1687
1688        /* Setup the burned in MAC address */
1689        u64temp = (u64)nesdev->nesadapter->mac_addr_low;
1690        u64temp += ((u64)nesdev->nesadapter->mac_addr_high) << 32;
1691        u64temp += nesvnic->nic_index;
1692        netdev->dev_addr[0] = (u8)(u64temp>>40);
1693        netdev->dev_addr[1] = (u8)(u64temp>>32);
1694        netdev->dev_addr[2] = (u8)(u64temp>>24);
1695        netdev->dev_addr[3] = (u8)(u64temp>>16);
1696        netdev->dev_addr[4] = (u8)(u64temp>>8);
1697        netdev->dev_addr[5] = (u8)u64temp;
1698
1699        netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
1700        if ((nesvnic->logical_port < 2) || (nesdev->nesadapter->hw_rev != NE020_REV))
1701                netdev->hw_features |= NETIF_F_TSO;
1702
1703        netdev->features = netdev->hw_features | NETIF_F_HIGHDMA | NETIF_F_HW_VLAN_CTAG_TX;
1704
1705        nes_debug(NES_DBG_INIT, "nesvnic = %p, reported features = 0x%lX, QPid = %d,"
1706                        " nic_index = %d, logical_port = %d, mac_index = %d.\n",
1707                        nesvnic, (unsigned long)netdev->features, nesvnic->nic.qp_id,
1708                        nesvnic->nic_index, nesvnic->logical_port,  nesdev->mac_index);
1709
1710        if (nesvnic->nesdev->nesadapter->port_count == 1 &&
1711                nesvnic->nesdev->nesadapter->adapter_fcn_count == 1) {
1712
1713                nesvnic->qp_nic_index[0] = nesvnic->nic_index;
1714                nesvnic->qp_nic_index[1] = nesvnic->nic_index + 1;
1715                if (nes_drv_opt & NES_DRV_OPT_DUAL_LOGICAL_PORT) {
1716                        nesvnic->qp_nic_index[2] = 0xf;
1717                        nesvnic->qp_nic_index[3] = 0xf;
1718                } else {
1719                        nesvnic->qp_nic_index[2] = nesvnic->nic_index + 2;
1720                        nesvnic->qp_nic_index[3] = nesvnic->nic_index + 3;
1721                }
1722        } else {
1723                if (nesvnic->nesdev->nesadapter->port_count == 2 ||
1724                        (nesvnic->nesdev->nesadapter->port_count == 1 &&
1725                        nesvnic->nesdev->nesadapter->adapter_fcn_count == 2)) {
1726                                nesvnic->qp_nic_index[0] = nesvnic->nic_index;
1727                                nesvnic->qp_nic_index[1] = nesvnic->nic_index
1728                                                                        + 2;
1729                                nesvnic->qp_nic_index[2] = 0xf;
1730                                nesvnic->qp_nic_index[3] = 0xf;
1731                } else {
1732                        nesvnic->qp_nic_index[0] = nesvnic->nic_index;
1733                        nesvnic->qp_nic_index[1] = 0xf;
1734                        nesvnic->qp_nic_index[2] = 0xf;
1735                        nesvnic->qp_nic_index[3] = 0xf;
1736                }
1737        }
1738        nesvnic->next_qp_nic_index = 0;
1739
1740        if (nesdev->netdev_count == 0) {
1741                nesvnic->rdma_enabled = 1;
1742        } else {
1743                nesvnic->rdma_enabled = 0;
1744        }
1745        nesvnic->nic_cq.cq_number = nesvnic->nic.qp_id;
1746        timer_setup(&nesvnic->event_timer, NULL, 0);
1747        spin_lock_init(&nesvnic->tx_lock);
1748        spin_lock_init(&nesvnic->port_ibevent_lock);
1749        nesdev->netdev[nesdev->netdev_count] = netdev;
1750
1751        nes_debug(NES_DBG_INIT, "Adding nesvnic (%p) to the adapters nesvnic_list for MAC%d.\n",
1752                        nesvnic, nesdev->mac_index);
1753        list_add_tail(&nesvnic->list, &nesdev->nesadapter->nesvnic_list[nesdev->mac_index]);
1754
1755        if ((nesdev->netdev_count == 0) &&
1756            ((PCI_FUNC(nesdev->pcidev->devfn) == nesdev->mac_index) ||
1757             ((phy_type == NES_PHY_TYPE_PUMA_1G) &&
1758              (((PCI_FUNC(nesdev->pcidev->devfn) == 1) && (nesdev->mac_index == 2)) ||
1759               ((PCI_FUNC(nesdev->pcidev->devfn) == 2) && (nesdev->mac_index == 1)))))) {
1760                u32 u32temp;
1761                u32 link_mask = 0;
1762                u32 link_val = 0;
1763                u16 temp_phy_data;
1764                u16 phy_data = 0;
1765                unsigned long flags;
1766
1767                u32temp = nes_read_indexed(nesdev, NES_IDX_PHY_PCS_CONTROL_STATUS0 +
1768                                (0x200 * (nesdev->mac_index & 1)));
1769                if (phy_type != NES_PHY_TYPE_PUMA_1G) {
1770                        u32temp |= 0x00200000;
1771                        nes_write_indexed(nesdev, NES_IDX_PHY_PCS_CONTROL_STATUS0 +
1772                                (0x200 * (nesdev->mac_index & 1)), u32temp);
1773                }
1774
1775                /* Check and set linkup here.  This is for back to back */
1776                /* configuration where second port won't get link interrupt */
1777                switch (phy_type) {
1778                case NES_PHY_TYPE_PUMA_1G:
1779                        if (nesdev->mac_index < 2) {
1780                                link_mask = 0x01010000;
1781                                link_val = 0x01010000;
1782                        } else {
1783                                link_mask = 0x02020000;
1784                                link_val = 0x02020000;
1785                        }
1786                        break;
1787                case NES_PHY_TYPE_SFP_D:
1788                        spin_lock_irqsave(&nesdev->nesadapter->phy_lock, flags);
1789                        nes_read_10G_phy_reg(nesdev,
1790                                             nesdev->nesadapter->phy_index[nesdev->mac_index],
1791                                             1, 0x9003);
1792                        temp_phy_data = (u16)nes_read_indexed(nesdev, NES_IDX_MAC_MDIO_CONTROL);
1793                        nes_read_10G_phy_reg(nesdev,
1794                                             nesdev->nesadapter->phy_index[nesdev->mac_index],
1795                                             3, 0x0021);
1796                        nes_read_indexed(nesdev, NES_IDX_MAC_MDIO_CONTROL);
1797                        nes_read_10G_phy_reg(nesdev,
1798                                             nesdev->nesadapter->phy_index[nesdev->mac_index],
1799                                             3, 0x0021);
1800                        phy_data = (u16)nes_read_indexed(nesdev, NES_IDX_MAC_MDIO_CONTROL);
1801                        spin_unlock_irqrestore(&nesdev->nesadapter->phy_lock, flags);
1802                        phy_data = (!temp_phy_data && (phy_data == 0x8000)) ? 0x4 : 0x0;
1803                        break;
1804                default:
1805                        link_mask = 0x0f1f0000;
1806                        link_val = 0x0f0f0000;
1807                        break;
1808                }
1809
1810                u32temp = nes_read_indexed(nesdev,
1811                                           NES_IDX_PHY_PCS_CONTROL_STATUS0 +
1812                                           (0x200 * (nesdev->mac_index & 1)));
1813
1814                if (phy_type == NES_PHY_TYPE_SFP_D) {
1815                        if (phy_data & 0x0004)
1816                                nesvnic->linkup = 1;
1817                } else {
1818                        if ((u32temp & link_mask) == link_val)
1819                                nesvnic->linkup = 1;
1820                }
1821
1822                /* clear the MAC interrupt status, assumes direct logical to physical mapping */
1823                u32temp = nes_read_indexed(nesdev, NES_IDX_MAC_INT_STATUS + (0x200 * nesdev->mac_index));
1824                nes_debug(NES_DBG_INIT, "Phy interrupt status = 0x%X.\n", u32temp);
1825                nes_write_indexed(nesdev, NES_IDX_MAC_INT_STATUS + (0x200 * nesdev->mac_index), u32temp);
1826
1827                nes_init_phy(nesdev);
1828        }
1829
1830        nes_vlan_mode(netdev, nesdev, netdev->features);
1831
1832        return netdev;
1833}
1834
1835
1836/**
1837 * nes_netdev_destroy - destroy network device structure
1838 */
1839void nes_netdev_destroy(struct net_device *netdev)
1840{
1841        struct nes_vnic *nesvnic = netdev_priv(netdev);
1842
1843        /* make sure 'stop' method is called by Linux stack */
1844        /* nes_netdev_stop(netdev); */
1845
1846        list_del(&nesvnic->list);
1847
1848        if (nesvnic->of_device_registered) {
1849                nes_destroy_ofa_device(nesvnic->nesibdev);
1850        }
1851
1852        free_netdev(netdev);
1853}
1854
1855
1856/**
1857 * nes_nic_cm_xmit -- CM calls this to send out pkts
1858 */
1859int nes_nic_cm_xmit(struct sk_buff *skb, struct net_device *netdev)
1860{
1861        int ret;
1862
1863        skb->dev = netdev;
1864        ret = dev_queue_xmit(skb);
1865        if (ret) {
1866                nes_debug(NES_DBG_CM, "Bad return code from dev_queue_xmit %d\n", ret);
1867        }
1868
1869        return ret;
1870}
1871