linux/drivers/net/ethernet/qlogic/qlcnic/qlcnic_io.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * QLogic qlcnic NIC Driver
   4 * Copyright (c) 2009-2013 QLogic Corporation
   5 */
   6
   7#include <linux/netdevice.h>
   8#include <linux/if_vlan.h>
   9#include <net/ip.h>
  10#include <linux/ipv6.h>
  11#include <net/checksum.h>
  12#include <linux/printk.h>
  13
  14#include "qlcnic.h"
  15
  16#define QLCNIC_TX_ETHER_PKT             0x01
  17#define QLCNIC_TX_TCP_PKT               0x02
  18#define QLCNIC_TX_UDP_PKT               0x03
  19#define QLCNIC_TX_IP_PKT                0x04
  20#define QLCNIC_TX_TCP_LSO               0x05
  21#define QLCNIC_TX_TCP_LSO6              0x06
  22#define QLCNIC_TX_ENCAP_PKT             0x07
  23#define QLCNIC_TX_ENCAP_LSO             0x08
  24#define QLCNIC_TX_TCPV6_PKT             0x0b
  25#define QLCNIC_TX_UDPV6_PKT             0x0c
  26
  27#define QLCNIC_FLAGS_VLAN_TAGGED        0x10
  28#define QLCNIC_FLAGS_VLAN_OOB           0x40
  29
  30#define qlcnic_set_tx_vlan_tci(cmd_desc, v)     \
  31        (cmd_desc)->vlan_TCI = cpu_to_le16(v);
  32#define qlcnic_set_cmd_desc_port(cmd_desc, var) \
  33        ((cmd_desc)->port_ctxid |= ((var) & 0x0F))
  34#define qlcnic_set_cmd_desc_ctxid(cmd_desc, var)        \
  35        ((cmd_desc)->port_ctxid |= ((var) << 4 & 0xF0))
  36
  37#define qlcnic_set_tx_port(_desc, _port) \
  38        ((_desc)->port_ctxid = ((_port) & 0xf) | (((_port) << 4) & 0xf0))
  39
  40#define qlcnic_set_tx_flags_opcode(_desc, _flags, _opcode) \
  41        ((_desc)->flags_opcode |= \
  42        cpu_to_le16(((_flags) & 0x7f) | (((_opcode) & 0x3f) << 7)))
  43
  44#define qlcnic_set_tx_frags_len(_desc, _frags, _len) \
  45        ((_desc)->nfrags__length = \
  46        cpu_to_le32(((_frags) & 0xff) | (((_len) & 0xffffff) << 8)))
  47
  48/* owner bits of status_desc */
  49#define STATUS_OWNER_HOST       (0x1ULL << 56)
  50#define STATUS_OWNER_PHANTOM    (0x2ULL << 56)
  51
  52/* Status descriptor:
  53   0-3 port, 4-7 status, 8-11 type, 12-27 total_length
  54   28-43 reference_handle, 44-47 protocol, 48-52 pkt_offset
  55   53-55 desc_cnt, 56-57 owner, 58-63 opcode
  56 */
  57#define qlcnic_get_sts_port(sts_data)   \
  58        ((sts_data) & 0x0F)
  59#define qlcnic_get_sts_status(sts_data) \
  60        (((sts_data) >> 4) & 0x0F)
  61#define qlcnic_get_sts_type(sts_data)   \
  62        (((sts_data) >> 8) & 0x0F)
  63#define qlcnic_get_sts_totallength(sts_data)    \
  64        (((sts_data) >> 12) & 0xFFFF)
  65#define qlcnic_get_sts_refhandle(sts_data)      \
  66        (((sts_data) >> 28) & 0xFFFF)
  67#define qlcnic_get_sts_prot(sts_data)   \
  68        (((sts_data) >> 44) & 0x0F)
  69#define qlcnic_get_sts_pkt_offset(sts_data)     \
  70        (((sts_data) >> 48) & 0x1F)
  71#define qlcnic_get_sts_desc_cnt(sts_data)       \
  72        (((sts_data) >> 53) & 0x7)
  73#define qlcnic_get_sts_opcode(sts_data) \
  74        (((sts_data) >> 58) & 0x03F)
  75
  76#define qlcnic_get_lro_sts_refhandle(sts_data)  \
  77        ((sts_data) & 0x07FFF)
  78#define qlcnic_get_lro_sts_length(sts_data)     \
  79        (((sts_data) >> 16) & 0x0FFFF)
  80#define qlcnic_get_lro_sts_l2_hdr_offset(sts_data)      \
  81        (((sts_data) >> 32) & 0x0FF)
  82#define qlcnic_get_lro_sts_l4_hdr_offset(sts_data)      \
  83        (((sts_data) >> 40) & 0x0FF)
  84#define qlcnic_get_lro_sts_timestamp(sts_data)  \
  85        (((sts_data) >> 48) & 0x1)
  86#define qlcnic_get_lro_sts_type(sts_data)       \
  87        (((sts_data) >> 49) & 0x7)
  88#define qlcnic_get_lro_sts_push_flag(sts_data)          \
  89        (((sts_data) >> 52) & 0x1)
  90#define qlcnic_get_lro_sts_seq_number(sts_data)         \
  91        ((sts_data) & 0x0FFFFFFFF)
  92#define qlcnic_get_lro_sts_mss(sts_data1)               \
  93        ((sts_data1 >> 32) & 0x0FFFF)
  94
  95#define qlcnic_83xx_get_lro_sts_mss(sts) ((sts) & 0xffff)
  96
  97/* opcode field in status_desc */
  98#define QLCNIC_SYN_OFFLOAD      0x03
  99#define QLCNIC_RXPKT_DESC       0x04
 100#define QLCNIC_OLD_RXPKT_DESC   0x3f
 101#define QLCNIC_RESPONSE_DESC    0x05
 102#define QLCNIC_LRO_DESC         0x12
 103
 104#define QLCNIC_TCP_HDR_SIZE             20
 105#define QLCNIC_TCP_TS_OPTION_SIZE       12
 106#define QLCNIC_FETCH_RING_ID(handle)    ((handle) >> 63)
 107#define QLCNIC_DESC_OWNER_FW            cpu_to_le64(STATUS_OWNER_PHANTOM)
 108
 109#define QLCNIC_TCP_TS_HDR_SIZE (QLCNIC_TCP_HDR_SIZE + QLCNIC_TCP_TS_OPTION_SIZE)
 110
 111/* for status field in status_desc */
 112#define STATUS_CKSUM_LOOP       0
 113#define STATUS_CKSUM_OK         2
 114
 115#define qlcnic_83xx_pktln(sts)          ((sts >> 32) & 0x3FFF)
 116#define qlcnic_83xx_hndl(sts)           ((sts >> 48) & 0x7FFF)
 117#define qlcnic_83xx_csum_status(sts)    ((sts >> 39) & 7)
 118#define qlcnic_83xx_opcode(sts) ((sts >> 42) & 0xF)
 119#define qlcnic_83xx_vlan_tag(sts)       (((sts) >> 48) & 0xFFFF)
 120#define qlcnic_83xx_lro_pktln(sts)      (((sts) >> 32) & 0x3FFF)
 121#define qlcnic_83xx_l2_hdr_off(sts)     (((sts) >> 16) & 0xFF)
 122#define qlcnic_83xx_l4_hdr_off(sts)     (((sts) >> 24) & 0xFF)
 123#define qlcnic_83xx_pkt_cnt(sts)        (((sts) >> 16) & 0x7)
 124#define qlcnic_83xx_is_tstamp(sts)      (((sts) >> 40) & 1)
 125#define qlcnic_83xx_is_psh_bit(sts)     (((sts) >> 41) & 1)
 126#define qlcnic_83xx_is_ip_align(sts)    (((sts) >> 46) & 1)
 127#define qlcnic_83xx_has_vlan_tag(sts)   (((sts) >> 47) & 1)
 128
 129static int qlcnic_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring,
 130                                   int max);
 131
 132static struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *,
 133                                            struct qlcnic_host_rds_ring *,
 134                                            u16, u16);
 135
 136static inline u8 qlcnic_mac_hash(u64 mac, u16 vlan)
 137{
 138        return (u8)((mac & 0xff) ^ ((mac >> 40) & 0xff) ^ (vlan & 0xff));
 139}
 140
 141static inline u32 qlcnic_get_ref_handle(struct qlcnic_adapter *adapter,
 142                                        u16 handle, u8 ring_id)
 143{
 144        if (qlcnic_83xx_check(adapter))
 145                return handle | (ring_id << 15);
 146        else
 147                return handle;
 148}
 149
 150static inline int qlcnic_82xx_is_lb_pkt(u64 sts_data)
 151{
 152        return (qlcnic_get_sts_status(sts_data) == STATUS_CKSUM_LOOP) ? 1 : 0;
 153}
 154
 155static void qlcnic_delete_rx_list_mac(struct qlcnic_adapter *adapter,
 156                                      struct qlcnic_filter *fil,
 157                                      void *addr, u16 vlan_id)
 158{
 159        int ret;
 160        u8 op;
 161
 162        op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
 163        ret = qlcnic_sre_macaddr_change(adapter, addr, vlan_id, op);
 164        if (ret)
 165                return;
 166
 167        op = vlan_id ? QLCNIC_MAC_VLAN_DEL : QLCNIC_MAC_DEL;
 168        ret = qlcnic_sre_macaddr_change(adapter, addr, vlan_id, op);
 169        if (!ret) {
 170                hlist_del(&fil->fnode);
 171                adapter->rx_fhash.fnum--;
 172        }
 173}
 174
 175static struct qlcnic_filter *qlcnic_find_mac_filter(struct hlist_head *head,
 176                                                    void *addr, u16 vlan_id)
 177{
 178        struct qlcnic_filter *tmp_fil = NULL;
 179        struct hlist_node *n;
 180
 181        hlist_for_each_entry_safe(tmp_fil, n, head, fnode) {
 182                if (ether_addr_equal(tmp_fil->faddr, addr) &&
 183                    tmp_fil->vlan_id == vlan_id)
 184                        return tmp_fil;
 185        }
 186
 187        return NULL;
 188}
 189
 190static void qlcnic_add_lb_filter(struct qlcnic_adapter *adapter,
 191                                 struct sk_buff *skb, int loopback_pkt, u16 vlan_id)
 192{
 193        struct ethhdr *phdr = (struct ethhdr *)(skb->data);
 194        struct qlcnic_filter *fil, *tmp_fil;
 195        struct hlist_head *head;
 196        unsigned long time;
 197        u64 src_addr = 0;
 198        u8 hindex, op;
 199        int ret;
 200
 201        if (!qlcnic_sriov_pf_check(adapter) || (vlan_id == 0xffff))
 202                vlan_id = 0;
 203
 204        memcpy(&src_addr, phdr->h_source, ETH_ALEN);
 205        hindex = qlcnic_mac_hash(src_addr, vlan_id) &
 206                 (adapter->fhash.fbucket_size - 1);
 207
 208        if (loopback_pkt) {
 209                if (adapter->rx_fhash.fnum >= adapter->rx_fhash.fmax)
 210                        return;
 211
 212                head = &(adapter->rx_fhash.fhead[hindex]);
 213
 214                tmp_fil = qlcnic_find_mac_filter(head, &src_addr, vlan_id);
 215                if (tmp_fil) {
 216                        time = tmp_fil->ftime;
 217                        if (time_after(jiffies, QLCNIC_READD_AGE * HZ + time))
 218                                tmp_fil->ftime = jiffies;
 219                        return;
 220                }
 221
 222                fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
 223                if (!fil)
 224                        return;
 225
 226                fil->ftime = jiffies;
 227                memcpy(fil->faddr, &src_addr, ETH_ALEN);
 228                fil->vlan_id = vlan_id;
 229                spin_lock(&adapter->rx_mac_learn_lock);
 230                hlist_add_head(&(fil->fnode), head);
 231                adapter->rx_fhash.fnum++;
 232                spin_unlock(&adapter->rx_mac_learn_lock);
 233        } else {
 234                head = &adapter->fhash.fhead[hindex];
 235
 236                spin_lock(&adapter->mac_learn_lock);
 237
 238                tmp_fil = qlcnic_find_mac_filter(head, &src_addr, vlan_id);
 239                if (tmp_fil) {
 240                        op = vlan_id ? QLCNIC_MAC_VLAN_DEL : QLCNIC_MAC_DEL;
 241                        ret = qlcnic_sre_macaddr_change(adapter,
 242                                                        (u8 *)&src_addr,
 243                                                        vlan_id, op);
 244                        if (!ret) {
 245                                hlist_del(&tmp_fil->fnode);
 246                                adapter->fhash.fnum--;
 247                        }
 248
 249                        spin_unlock(&adapter->mac_learn_lock);
 250
 251                        return;
 252                }
 253
 254                spin_unlock(&adapter->mac_learn_lock);
 255
 256                head = &adapter->rx_fhash.fhead[hindex];
 257
 258                spin_lock(&adapter->rx_mac_learn_lock);
 259
 260                tmp_fil = qlcnic_find_mac_filter(head, &src_addr, vlan_id);
 261                if (tmp_fil)
 262                        qlcnic_delete_rx_list_mac(adapter, tmp_fil, &src_addr,
 263                                                  vlan_id);
 264
 265                spin_unlock(&adapter->rx_mac_learn_lock);
 266        }
 267}
 268
 269void qlcnic_82xx_change_filter(struct qlcnic_adapter *adapter, u64 *uaddr,
 270                               u16 vlan_id, struct qlcnic_host_tx_ring *tx_ring)
 271{
 272        struct cmd_desc_type0 *hwdesc;
 273        struct qlcnic_nic_req *req;
 274        struct qlcnic_mac_req *mac_req;
 275        struct qlcnic_vlan_req *vlan_req;
 276        u32 producer;
 277        u64 word;
 278
 279        producer = tx_ring->producer;
 280        hwdesc = &tx_ring->desc_head[tx_ring->producer];
 281
 282        req = (struct qlcnic_nic_req *)hwdesc;
 283        memset(req, 0, sizeof(struct qlcnic_nic_req));
 284        req->qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
 285
 286        word = QLCNIC_MAC_EVENT | ((u64)(adapter->portnum) << 16);
 287        req->req_hdr = cpu_to_le64(word);
 288
 289        mac_req = (struct qlcnic_mac_req *)&(req->words[0]);
 290        mac_req->op = vlan_id ? QLCNIC_MAC_VLAN_ADD : QLCNIC_MAC_ADD;
 291        memcpy(mac_req->mac_addr, uaddr, ETH_ALEN);
 292
 293        vlan_req = (struct qlcnic_vlan_req *)&req->words[1];
 294        vlan_req->vlan_id = cpu_to_le16(vlan_id);
 295
 296        tx_ring->producer = get_next_index(producer, tx_ring->num_desc);
 297        smp_mb();
 298}
 299
 300static void qlcnic_send_filter(struct qlcnic_adapter *adapter,
 301                               struct cmd_desc_type0 *first_desc,
 302                               struct sk_buff *skb,
 303                               struct qlcnic_host_tx_ring *tx_ring)
 304{
 305        struct vlan_ethhdr *vh = (struct vlan_ethhdr *)(skb->data);
 306        struct ethhdr *phdr = (struct ethhdr *)(skb->data);
 307        u16 protocol = ntohs(skb->protocol);
 308        struct qlcnic_filter *fil, *tmp_fil;
 309        struct hlist_head *head;
 310        struct hlist_node *n;
 311        u64 src_addr = 0;
 312        u16 vlan_id = 0;
 313        u8 hindex, hval;
 314
 315        if (ether_addr_equal(phdr->h_source, adapter->mac_addr))
 316                return;
 317
 318        if (adapter->flags & QLCNIC_VLAN_FILTERING) {
 319                if (protocol == ETH_P_8021Q) {
 320                        vh = (struct vlan_ethhdr *)skb->data;
 321                        vlan_id = ntohs(vh->h_vlan_TCI);
 322                } else if (skb_vlan_tag_present(skb)) {
 323                        vlan_id = skb_vlan_tag_get(skb);
 324                }
 325        }
 326
 327        memcpy(&src_addr, phdr->h_source, ETH_ALEN);
 328        hval = qlcnic_mac_hash(src_addr, vlan_id);
 329        hindex = hval & (adapter->fhash.fbucket_size - 1);
 330        head = &(adapter->fhash.fhead[hindex]);
 331
 332        hlist_for_each_entry_safe(tmp_fil, n, head, fnode) {
 333                if (ether_addr_equal(tmp_fil->faddr, (u8 *)&src_addr) &&
 334                    tmp_fil->vlan_id == vlan_id) {
 335                        if (jiffies > (QLCNIC_READD_AGE * HZ + tmp_fil->ftime))
 336                                qlcnic_change_filter(adapter, &src_addr,
 337                                                     vlan_id, tx_ring);
 338                        tmp_fil->ftime = jiffies;
 339                        return;
 340                }
 341        }
 342
 343        if (unlikely(adapter->fhash.fnum >= adapter->fhash.fmax)) {
 344                adapter->stats.mac_filter_limit_overrun++;
 345                return;
 346        }
 347
 348        fil = kzalloc(sizeof(struct qlcnic_filter), GFP_ATOMIC);
 349        if (!fil)
 350                return;
 351
 352        qlcnic_change_filter(adapter, &src_addr, vlan_id, tx_ring);
 353        fil->ftime = jiffies;
 354        fil->vlan_id = vlan_id;
 355        memcpy(fil->faddr, &src_addr, ETH_ALEN);
 356        spin_lock(&adapter->mac_learn_lock);
 357        hlist_add_head(&(fil->fnode), head);
 358        adapter->fhash.fnum++;
 359        spin_unlock(&adapter->mac_learn_lock);
 360}
 361
 362#define QLCNIC_ENCAP_VXLAN_PKT          BIT_0
 363#define QLCNIC_ENCAP_OUTER_L3_IP6       BIT_1
 364#define QLCNIC_ENCAP_INNER_L3_IP6       BIT_2
 365#define QLCNIC_ENCAP_INNER_L4_UDP       BIT_3
 366#define QLCNIC_ENCAP_DO_L3_CSUM         BIT_4
 367#define QLCNIC_ENCAP_DO_L4_CSUM         BIT_5
 368
 369static int qlcnic_tx_encap_pkt(struct qlcnic_adapter *adapter,
 370                               struct cmd_desc_type0 *first_desc,
 371                               struct sk_buff *skb,
 372                               struct qlcnic_host_tx_ring *tx_ring)
 373{
 374        u8 opcode = 0, inner_hdr_len = 0, outer_hdr_len = 0, total_hdr_len = 0;
 375        int copied, copy_len, descr_size;
 376        u32 producer = tx_ring->producer;
 377        struct cmd_desc_type0 *hwdesc;
 378        u16 flags = 0, encap_descr = 0;
 379
 380        opcode = QLCNIC_TX_ETHER_PKT;
 381        encap_descr = QLCNIC_ENCAP_VXLAN_PKT;
 382
 383        if (skb_is_gso(skb)) {
 384                inner_hdr_len = skb_inner_transport_header(skb) +
 385                                inner_tcp_hdrlen(skb) -
 386                                skb_inner_mac_header(skb);
 387
 388                /* VXLAN header size = 8 */
 389                outer_hdr_len = skb_transport_offset(skb) + 8 +
 390                                sizeof(struct udphdr);
 391                first_desc->outer_hdr_length = outer_hdr_len;
 392                total_hdr_len = inner_hdr_len + outer_hdr_len;
 393                encap_descr |= QLCNIC_ENCAP_DO_L3_CSUM |
 394                               QLCNIC_ENCAP_DO_L4_CSUM;
 395                first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
 396                first_desc->hdr_length = inner_hdr_len;
 397
 398                /* Copy inner and outer headers in Tx descriptor(s)
 399                 * If total_hdr_len > cmd_desc_type0, use multiple
 400                 * descriptors
 401                 */
 402                copied = 0;
 403                descr_size = (int)sizeof(struct cmd_desc_type0);
 404                while (copied < total_hdr_len) {
 405                        copy_len = min(descr_size, (total_hdr_len - copied));
 406                        hwdesc = &tx_ring->desc_head[producer];
 407                        tx_ring->cmd_buf_arr[producer].skb = NULL;
 408                        skb_copy_from_linear_data_offset(skb, copied,
 409                                                         (char *)hwdesc,
 410                                                         copy_len);
 411                        copied += copy_len;
 412                        producer = get_next_index(producer, tx_ring->num_desc);
 413                }
 414
 415                tx_ring->producer = producer;
 416
 417                /* Make sure updated tx_ring->producer is visible
 418                 * for qlcnic_tx_avail()
 419                 */
 420                smp_mb();
 421                adapter->stats.encap_lso_frames++;
 422
 423                opcode = QLCNIC_TX_ENCAP_LSO;
 424        } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
 425                if (inner_ip_hdr(skb)->version == 6) {
 426                        if (inner_ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
 427                                encap_descr |= QLCNIC_ENCAP_INNER_L4_UDP;
 428                } else {
 429                        if (inner_ip_hdr(skb)->protocol == IPPROTO_UDP)
 430                                encap_descr |= QLCNIC_ENCAP_INNER_L4_UDP;
 431                }
 432
 433                adapter->stats.encap_tx_csummed++;
 434                opcode = QLCNIC_TX_ENCAP_PKT;
 435        }
 436
 437        /* Prepare first 16 bits of byte offset 16 of Tx descriptor */
 438        if (ip_hdr(skb)->version == 6)
 439                encap_descr |= QLCNIC_ENCAP_OUTER_L3_IP6;
 440
 441        /* outer IP header's size in 32bit words size*/
 442        encap_descr |= (skb_network_header_len(skb) >> 2) << 6;
 443
 444        /* outer IP header offset */
 445        encap_descr |= skb_network_offset(skb) << 10;
 446        first_desc->encap_descr = cpu_to_le16(encap_descr);
 447
 448        first_desc->tcp_hdr_offset = skb_inner_transport_header(skb) -
 449                                     skb->data;
 450        first_desc->ip_hdr_offset = skb_inner_network_offset(skb);
 451
 452        qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
 453
 454        return 0;
 455}
 456
 457static int qlcnic_tx_pkt(struct qlcnic_adapter *adapter,
 458                         struct cmd_desc_type0 *first_desc, struct sk_buff *skb,
 459                         struct qlcnic_host_tx_ring *tx_ring)
 460{
 461        u8 l4proto, opcode = 0, hdr_len = 0, tag_vlan = 0;
 462        u16 flags = 0, vlan_tci = 0;
 463        int copied, offset, copy_len, size;
 464        struct cmd_desc_type0 *hwdesc;
 465        struct vlan_ethhdr *vh;
 466        u16 protocol = ntohs(skb->protocol);
 467        u32 producer = tx_ring->producer;
 468
 469        if (protocol == ETH_P_8021Q) {
 470                vh = (struct vlan_ethhdr *)skb->data;
 471                flags = QLCNIC_FLAGS_VLAN_TAGGED;
 472                vlan_tci = ntohs(vh->h_vlan_TCI);
 473                protocol = ntohs(vh->h_vlan_encapsulated_proto);
 474                tag_vlan = 1;
 475        } else if (skb_vlan_tag_present(skb)) {
 476                flags = QLCNIC_FLAGS_VLAN_OOB;
 477                vlan_tci = skb_vlan_tag_get(skb);
 478                tag_vlan = 1;
 479        }
 480        if (unlikely(adapter->tx_pvid)) {
 481                if (tag_vlan && !(adapter->flags & QLCNIC_TAGGING_ENABLED))
 482                        return -EIO;
 483                if (tag_vlan && (adapter->flags & QLCNIC_TAGGING_ENABLED))
 484                        goto set_flags;
 485
 486                flags = QLCNIC_FLAGS_VLAN_OOB;
 487                vlan_tci = adapter->tx_pvid;
 488        }
 489set_flags:
 490        qlcnic_set_tx_vlan_tci(first_desc, vlan_tci);
 491        qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
 492
 493        if (*(skb->data) & BIT_0) {
 494                flags |= BIT_0;
 495                memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
 496        }
 497        opcode = QLCNIC_TX_ETHER_PKT;
 498        if (skb_is_gso(skb)) {
 499                hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
 500                first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
 501                first_desc->hdr_length = hdr_len;
 502                opcode = (protocol == ETH_P_IPV6) ? QLCNIC_TX_TCP_LSO6 :
 503                                                    QLCNIC_TX_TCP_LSO;
 504
 505                /* For LSO, we need to copy the MAC/IP/TCP headers into
 506                * the descriptor ring */
 507                copied = 0;
 508                offset = 2;
 509
 510                if (flags & QLCNIC_FLAGS_VLAN_OOB) {
 511                        first_desc->hdr_length += VLAN_HLEN;
 512                        first_desc->tcp_hdr_offset = VLAN_HLEN;
 513                        first_desc->ip_hdr_offset = VLAN_HLEN;
 514
 515                        /* Only in case of TSO on vlan device */
 516                        flags |= QLCNIC_FLAGS_VLAN_TAGGED;
 517
 518                        /* Create a TSO vlan header template for firmware */
 519                        hwdesc = &tx_ring->desc_head[producer];
 520                        tx_ring->cmd_buf_arr[producer].skb = NULL;
 521
 522                        copy_len = min((int)sizeof(struct cmd_desc_type0) -
 523                                       offset, hdr_len + VLAN_HLEN);
 524
 525                        vh = (struct vlan_ethhdr *)((char *) hwdesc + 2);
 526                        skb_copy_from_linear_data(skb, vh, 12);
 527                        vh->h_vlan_proto = htons(ETH_P_8021Q);
 528                        vh->h_vlan_TCI = htons(vlan_tci);
 529
 530                        skb_copy_from_linear_data_offset(skb, 12,
 531                                                         (char *)vh + 16,
 532                                                         copy_len - 16);
 533                        copied = copy_len - VLAN_HLEN;
 534                        offset = 0;
 535                        producer = get_next_index(producer, tx_ring->num_desc);
 536                }
 537
 538                while (copied < hdr_len) {
 539                        size = (int)sizeof(struct cmd_desc_type0) - offset;
 540                        copy_len = min(size, (hdr_len - copied));
 541                        hwdesc = &tx_ring->desc_head[producer];
 542                        tx_ring->cmd_buf_arr[producer].skb = NULL;
 543                        skb_copy_from_linear_data_offset(skb, copied,
 544                                                         (char *)hwdesc +
 545                                                         offset, copy_len);
 546                        copied += copy_len;
 547                        offset = 0;
 548                        producer = get_next_index(producer, tx_ring->num_desc);
 549                }
 550
 551                tx_ring->producer = producer;
 552                smp_mb();
 553                adapter->stats.lso_frames++;
 554
 555        } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
 556                if (protocol == ETH_P_IP) {
 557                        l4proto = ip_hdr(skb)->protocol;
 558
 559                        if (l4proto == IPPROTO_TCP)
 560                                opcode = QLCNIC_TX_TCP_PKT;
 561                        else if (l4proto == IPPROTO_UDP)
 562                                opcode = QLCNIC_TX_UDP_PKT;
 563                } else if (protocol == ETH_P_IPV6) {
 564                        l4proto = ipv6_hdr(skb)->nexthdr;
 565
 566                        if (l4proto == IPPROTO_TCP)
 567                                opcode = QLCNIC_TX_TCPV6_PKT;
 568                        else if (l4proto == IPPROTO_UDP)
 569                                opcode = QLCNIC_TX_UDPV6_PKT;
 570                }
 571        }
 572        first_desc->tcp_hdr_offset += skb_transport_offset(skb);
 573        first_desc->ip_hdr_offset += skb_network_offset(skb);
 574        qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
 575
 576        return 0;
 577}
 578
 579static int qlcnic_map_tx_skb(struct pci_dev *pdev, struct sk_buff *skb,
 580                             struct qlcnic_cmd_buffer *pbuf)
 581{
 582        struct qlcnic_skb_frag *nf;
 583        skb_frag_t *frag;
 584        int i, nr_frags;
 585        dma_addr_t map;
 586
 587        nr_frags = skb_shinfo(skb)->nr_frags;
 588        nf = &pbuf->frag_array[0];
 589
 590        map = pci_map_single(pdev, skb->data, skb_headlen(skb),
 591                             PCI_DMA_TODEVICE);
 592        if (pci_dma_mapping_error(pdev, map))
 593                goto out_err;
 594
 595        nf->dma = map;
 596        nf->length = skb_headlen(skb);
 597
 598        for (i = 0; i < nr_frags; i++) {
 599                frag = &skb_shinfo(skb)->frags[i];
 600                nf = &pbuf->frag_array[i+1];
 601                map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
 602                                       DMA_TO_DEVICE);
 603                if (dma_mapping_error(&pdev->dev, map))
 604                        goto unwind;
 605
 606                nf->dma = map;
 607                nf->length = skb_frag_size(frag);
 608        }
 609
 610        return 0;
 611
 612unwind:
 613        while (--i >= 0) {
 614                nf = &pbuf->frag_array[i+1];
 615                pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
 616        }
 617
 618        nf = &pbuf->frag_array[0];
 619        pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
 620
 621out_err:
 622        return -ENOMEM;
 623}
 624
 625static void qlcnic_unmap_buffers(struct pci_dev *pdev, struct sk_buff *skb,
 626                                 struct qlcnic_cmd_buffer *pbuf)
 627{
 628        struct qlcnic_skb_frag *nf = &pbuf->frag_array[0];
 629        int i, nr_frags = skb_shinfo(skb)->nr_frags;
 630
 631        for (i = 0; i < nr_frags; i++) {
 632                nf = &pbuf->frag_array[i+1];
 633                pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
 634        }
 635
 636        nf = &pbuf->frag_array[0];
 637        pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
 638        pbuf->skb = NULL;
 639}
 640
 641static inline void qlcnic_clear_cmddesc(u64 *desc)
 642{
 643        desc[0] = 0ULL;
 644        desc[2] = 0ULL;
 645        desc[7] = 0ULL;
 646}
 647
 648netdev_tx_t qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
 649{
 650        struct qlcnic_adapter *adapter = netdev_priv(netdev);
 651        struct qlcnic_host_tx_ring *tx_ring;
 652        struct qlcnic_cmd_buffer *pbuf;
 653        struct qlcnic_skb_frag *buffrag;
 654        struct cmd_desc_type0 *hwdesc, *first_desc;
 655        struct pci_dev *pdev;
 656        struct ethhdr *phdr;
 657        int i, k, frag_count, delta = 0;
 658        u32 producer, num_txd;
 659        u16 protocol;
 660        bool l4_is_udp = false;
 661
 662        if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
 663                netif_tx_stop_all_queues(netdev);
 664                return NETDEV_TX_BUSY;
 665        }
 666
 667        if (adapter->flags & QLCNIC_MACSPOOF) {
 668                phdr = (struct ethhdr *)skb->data;
 669                if (!ether_addr_equal(phdr->h_source, adapter->mac_addr))
 670                        goto drop_packet;
 671        }
 672
 673        tx_ring = &adapter->tx_ring[skb_get_queue_mapping(skb)];
 674        num_txd = tx_ring->num_desc;
 675
 676        frag_count = skb_shinfo(skb)->nr_frags + 1;
 677
 678        /* 14 frags supported for normal packet and
 679         * 32 frags supported for TSO packet
 680         */
 681        if (!skb_is_gso(skb) && frag_count > QLCNIC_MAX_FRAGS_PER_TX) {
 682                for (i = 0; i < (frag_count - QLCNIC_MAX_FRAGS_PER_TX); i++)
 683                        delta += skb_frag_size(&skb_shinfo(skb)->frags[i]);
 684
 685                if (!__pskb_pull_tail(skb, delta))
 686                        goto drop_packet;
 687
 688                frag_count = 1 + skb_shinfo(skb)->nr_frags;
 689        }
 690
 691        if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
 692                netif_tx_stop_queue(tx_ring->txq);
 693                if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
 694                        netif_tx_start_queue(tx_ring->txq);
 695                } else {
 696                        tx_ring->tx_stats.xmit_off++;
 697                        return NETDEV_TX_BUSY;
 698                }
 699        }
 700
 701        producer = tx_ring->producer;
 702        pbuf = &tx_ring->cmd_buf_arr[producer];
 703        pdev = adapter->pdev;
 704        first_desc = &tx_ring->desc_head[producer];
 705        hwdesc = &tx_ring->desc_head[producer];
 706        qlcnic_clear_cmddesc((u64 *)hwdesc);
 707
 708        if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
 709                adapter->stats.tx_dma_map_error++;
 710                goto drop_packet;
 711        }
 712
 713        pbuf->skb = skb;
 714        pbuf->frag_count = frag_count;
 715
 716        qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
 717        qlcnic_set_tx_port(first_desc, adapter->portnum);
 718
 719        for (i = 0; i < frag_count; i++) {
 720                k = i % 4;
 721
 722                if ((k == 0) && (i > 0)) {
 723                        /* move to next desc.*/
 724                        producer = get_next_index(producer, num_txd);
 725                        hwdesc = &tx_ring->desc_head[producer];
 726                        qlcnic_clear_cmddesc((u64 *)hwdesc);
 727                        tx_ring->cmd_buf_arr[producer].skb = NULL;
 728                }
 729
 730                buffrag = &pbuf->frag_array[i];
 731                hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
 732                switch (k) {
 733                case 0:
 734                        hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
 735                        break;
 736                case 1:
 737                        hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
 738                        break;
 739                case 2:
 740                        hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
 741                        break;
 742                case 3:
 743                        hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
 744                        break;
 745                }
 746        }
 747
 748        tx_ring->producer = get_next_index(producer, num_txd);
 749        smp_mb();
 750
 751        protocol = ntohs(skb->protocol);
 752        if (protocol == ETH_P_IP)
 753                l4_is_udp = ip_hdr(skb)->protocol == IPPROTO_UDP;
 754        else if (protocol == ETH_P_IPV6)
 755                l4_is_udp = ipv6_hdr(skb)->nexthdr == IPPROTO_UDP;
 756
 757        /* Check if it is a VXLAN packet */
 758        if (!skb->encapsulation || !l4_is_udp ||
 759            !qlcnic_encap_tx_offload(adapter)) {
 760                if (unlikely(qlcnic_tx_pkt(adapter, first_desc, skb,
 761                                           tx_ring)))
 762                        goto unwind_buff;
 763        } else {
 764                if (unlikely(qlcnic_tx_encap_pkt(adapter, first_desc,
 765                                                 skb, tx_ring)))
 766                        goto unwind_buff;
 767        }
 768
 769        if (adapter->drv_mac_learn)
 770                qlcnic_send_filter(adapter, first_desc, skb, tx_ring);
 771
 772        tx_ring->tx_stats.tx_bytes += skb->len;
 773        tx_ring->tx_stats.xmit_called++;
 774
 775        /* Ensure writes are complete before HW fetches Tx descriptors */
 776        wmb();
 777        qlcnic_update_cmd_producer(tx_ring);
 778
 779        return NETDEV_TX_OK;
 780
 781unwind_buff:
 782        qlcnic_unmap_buffers(pdev, skb, pbuf);
 783drop_packet:
 784        adapter->stats.txdropped++;
 785        dev_kfree_skb_any(skb);
 786        return NETDEV_TX_OK;
 787}
 788
 789void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
 790{
 791        struct net_device *netdev = adapter->netdev;
 792
 793        if (adapter->ahw->linkup && !linkup) {
 794                netdev_info(netdev, "NIC Link is down\n");
 795                adapter->ahw->linkup = 0;
 796                netif_carrier_off(netdev);
 797        } else if (!adapter->ahw->linkup && linkup) {
 798                adapter->ahw->linkup = 1;
 799
 800                /* Do not advertise Link up to the stack if device
 801                 * is in loopback mode
 802                 */
 803                if (qlcnic_83xx_check(adapter) && adapter->ahw->lb_mode) {
 804                        netdev_info(netdev, "NIC Link is up for loopback test\n");
 805                        return;
 806                }
 807
 808                netdev_info(netdev, "NIC Link is up\n");
 809                netif_carrier_on(netdev);
 810        }
 811}
 812
 813static int qlcnic_alloc_rx_skb(struct qlcnic_adapter *adapter,
 814                               struct qlcnic_host_rds_ring *rds_ring,
 815                               struct qlcnic_rx_buffer *buffer)
 816{
 817        struct sk_buff *skb;
 818        dma_addr_t dma;
 819        struct pci_dev *pdev = adapter->pdev;
 820
 821        skb = netdev_alloc_skb(adapter->netdev, rds_ring->skb_size);
 822        if (!skb) {
 823                adapter->stats.skb_alloc_failure++;
 824                return -ENOMEM;
 825        }
 826
 827        skb_reserve(skb, NET_IP_ALIGN);
 828        dma = pci_map_single(pdev, skb->data,
 829                             rds_ring->dma_size, PCI_DMA_FROMDEVICE);
 830
 831        if (pci_dma_mapping_error(pdev, dma)) {
 832                adapter->stats.rx_dma_map_error++;
 833                dev_kfree_skb_any(skb);
 834                return -ENOMEM;
 835        }
 836
 837        buffer->skb = skb;
 838        buffer->dma = dma;
 839
 840        return 0;
 841}
 842
 843static void qlcnic_post_rx_buffers_nodb(struct qlcnic_adapter *adapter,
 844                                        struct qlcnic_host_rds_ring *rds_ring,
 845                                        u8 ring_id)
 846{
 847        struct rcv_desc *pdesc;
 848        struct qlcnic_rx_buffer *buffer;
 849        int  count = 0;
 850        uint32_t producer, handle;
 851        struct list_head *head;
 852
 853        if (!spin_trylock(&rds_ring->lock))
 854                return;
 855
 856        producer = rds_ring->producer;
 857        head = &rds_ring->free_list;
 858        while (!list_empty(head)) {
 859                buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
 860
 861                if (!buffer->skb) {
 862                        if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
 863                                break;
 864                }
 865                count++;
 866                list_del(&buffer->list);
 867
 868                /* make a rcv descriptor  */
 869                pdesc = &rds_ring->desc_head[producer];
 870                handle = qlcnic_get_ref_handle(adapter,
 871                                               buffer->ref_handle, ring_id);
 872                pdesc->reference_handle = cpu_to_le16(handle);
 873                pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
 874                pdesc->addr_buffer = cpu_to_le64(buffer->dma);
 875                producer = get_next_index(producer, rds_ring->num_desc);
 876        }
 877        if (count) {
 878                rds_ring->producer = producer;
 879                writel((producer - 1) & (rds_ring->num_desc - 1),
 880                       rds_ring->crb_rcv_producer);
 881        }
 882        spin_unlock(&rds_ring->lock);
 883}
 884
 885static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter,
 886                                   struct qlcnic_host_tx_ring *tx_ring,
 887                                   int budget)
 888{
 889        u32 sw_consumer, hw_consumer;
 890        int i, done, count = 0;
 891        struct qlcnic_cmd_buffer *buffer;
 892        struct pci_dev *pdev = adapter->pdev;
 893        struct net_device *netdev = adapter->netdev;
 894        struct qlcnic_skb_frag *frag;
 895
 896        if (!spin_trylock(&tx_ring->tx_clean_lock))
 897                return 1;
 898
 899        sw_consumer = tx_ring->sw_consumer;
 900        hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
 901
 902        while (sw_consumer != hw_consumer) {
 903                buffer = &tx_ring->cmd_buf_arr[sw_consumer];
 904                if (buffer->skb) {
 905                        frag = &buffer->frag_array[0];
 906                        pci_unmap_single(pdev, frag->dma, frag->length,
 907                                         PCI_DMA_TODEVICE);
 908                        frag->dma = 0ULL;
 909                        for (i = 1; i < buffer->frag_count; i++) {
 910                                frag++;
 911                                pci_unmap_page(pdev, frag->dma, frag->length,
 912                                               PCI_DMA_TODEVICE);
 913                                frag->dma = 0ULL;
 914                        }
 915                        tx_ring->tx_stats.xmit_finished++;
 916                        dev_kfree_skb_any(buffer->skb);
 917                        buffer->skb = NULL;
 918                }
 919
 920                sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
 921                if (++count >= budget)
 922                        break;
 923        }
 924
 925        tx_ring->sw_consumer = sw_consumer;
 926
 927        if (count && netif_running(netdev)) {
 928                smp_mb();
 929                if (netif_tx_queue_stopped(tx_ring->txq) &&
 930                    netif_carrier_ok(netdev)) {
 931                        if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
 932                                netif_tx_wake_queue(tx_ring->txq);
 933                                tx_ring->tx_stats.xmit_on++;
 934                        }
 935                }
 936                adapter->tx_timeo_cnt = 0;
 937        }
 938        /*
 939         * If everything is freed up to consumer then check if the ring is full
 940         * If the ring is full then check if more needs to be freed and
 941         * schedule the call back again.
 942         *
 943         * This happens when there are 2 CPUs. One could be freeing and the
 944         * other filling it. If the ring is full when we get out of here and
 945         * the card has already interrupted the host then the host can miss the
 946         * interrupt.
 947         *
 948         * There is still a possible race condition and the host could miss an
 949         * interrupt. The card has to take care of this.
 950         */
 951        hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
 952        done = (sw_consumer == hw_consumer);
 953
 954        spin_unlock(&tx_ring->tx_clean_lock);
 955
 956        return done;
 957}
 958
 959static int qlcnic_poll(struct napi_struct *napi, int budget)
 960{
 961        int tx_complete, work_done;
 962        struct qlcnic_host_sds_ring *sds_ring;
 963        struct qlcnic_adapter *adapter;
 964        struct qlcnic_host_tx_ring *tx_ring;
 965
 966        sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
 967        adapter = sds_ring->adapter;
 968        tx_ring = sds_ring->tx_ring;
 969
 970        tx_complete = qlcnic_process_cmd_ring(adapter, tx_ring,
 971                                              budget);
 972        work_done = qlcnic_process_rcv_ring(sds_ring, budget);
 973
 974        /* Check if we need a repoll */
 975        if (!tx_complete)
 976                work_done = budget;
 977
 978        if (work_done < budget) {
 979                napi_complete_done(&sds_ring->napi, work_done);
 980                if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
 981                        qlcnic_enable_sds_intr(adapter, sds_ring);
 982                        qlcnic_enable_tx_intr(adapter, tx_ring);
 983                }
 984        }
 985
 986        return work_done;
 987}
 988
 989static int qlcnic_tx_poll(struct napi_struct *napi, int budget)
 990{
 991        struct qlcnic_host_tx_ring *tx_ring;
 992        struct qlcnic_adapter *adapter;
 993        int work_done;
 994
 995        tx_ring = container_of(napi, struct qlcnic_host_tx_ring, napi);
 996        adapter = tx_ring->adapter;
 997
 998        work_done = qlcnic_process_cmd_ring(adapter, tx_ring, budget);
 999        if (work_done) {
1000                napi_complete(&tx_ring->napi);
1001                if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1002                        qlcnic_enable_tx_intr(adapter, tx_ring);
1003        } else {
1004                /* As qlcnic_process_cmd_ring() returned 0, we need a repoll*/
1005                work_done = budget;
1006        }
1007
1008        return work_done;
1009}
1010
1011static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
1012{
1013        struct qlcnic_host_sds_ring *sds_ring;
1014        struct qlcnic_adapter *adapter;
1015        int work_done;
1016
1017        sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
1018        adapter = sds_ring->adapter;
1019
1020        work_done = qlcnic_process_rcv_ring(sds_ring, budget);
1021
1022        if (work_done < budget) {
1023                napi_complete_done(&sds_ring->napi, work_done);
1024                if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
1025                        qlcnic_enable_sds_intr(adapter, sds_ring);
1026        }
1027
1028        return work_done;
1029}
1030
1031static void qlcnic_handle_linkevent(struct qlcnic_adapter *adapter,
1032                                    struct qlcnic_fw_msg *msg)
1033{
1034        u32 cable_OUI;
1035        u16 cable_len, link_speed;
1036        u8  link_status, module, duplex, autoneg, lb_status = 0;
1037        struct net_device *netdev = adapter->netdev;
1038
1039        adapter->ahw->has_link_events = 1;
1040
1041        cable_OUI = msg->body[1] & 0xffffffff;
1042        cable_len = (msg->body[1] >> 32) & 0xffff;
1043        link_speed = (msg->body[1] >> 48) & 0xffff;
1044
1045        link_status = msg->body[2] & 0xff;
1046        duplex = (msg->body[2] >> 16) & 0xff;
1047        autoneg = (msg->body[2] >> 24) & 0xff;
1048        lb_status = (msg->body[2] >> 32) & 0x3;
1049
1050        module = (msg->body[2] >> 8) & 0xff;
1051        if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE)
1052                dev_info(&netdev->dev,
1053                         "unsupported cable: OUI 0x%x, length %d\n",
1054                         cable_OUI, cable_len);
1055        else if (module == LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN)
1056                dev_info(&netdev->dev, "unsupported cable length %d\n",
1057                         cable_len);
1058
1059        if (!link_status && (lb_status == QLCNIC_ILB_MODE ||
1060            lb_status == QLCNIC_ELB_MODE))
1061                adapter->ahw->loopback_state |= QLCNIC_LINKEVENT;
1062
1063        qlcnic_advert_link_change(adapter, link_status);
1064
1065        if (duplex == LINKEVENT_FULL_DUPLEX)
1066                adapter->ahw->link_duplex = DUPLEX_FULL;
1067        else
1068                adapter->ahw->link_duplex = DUPLEX_HALF;
1069
1070        adapter->ahw->module_type = module;
1071        adapter->ahw->link_autoneg = autoneg;
1072
1073        if (link_status) {
1074                adapter->ahw->link_speed = link_speed;
1075        } else {
1076                adapter->ahw->link_speed = SPEED_UNKNOWN;
1077                adapter->ahw->link_duplex = DUPLEX_UNKNOWN;
1078        }
1079}
1080
1081static void qlcnic_handle_fw_message(int desc_cnt, int index,
1082                                     struct qlcnic_host_sds_ring *sds_ring)
1083{
1084        struct qlcnic_fw_msg msg;
1085        struct status_desc *desc;
1086        struct qlcnic_adapter *adapter;
1087        struct device *dev;
1088        int i = 0, opcode, ret;
1089
1090        while (desc_cnt > 0 && i < 8) {
1091                desc = &sds_ring->desc_head[index];
1092                msg.words[i++] = le64_to_cpu(desc->status_desc_data[0]);
1093                msg.words[i++] = le64_to_cpu(desc->status_desc_data[1]);
1094
1095                index = get_next_index(index, sds_ring->num_desc);
1096                desc_cnt--;
1097        }
1098
1099        adapter = sds_ring->adapter;
1100        dev = &adapter->pdev->dev;
1101        opcode = qlcnic_get_nic_msg_opcode(msg.body[0]);
1102
1103        switch (opcode) {
1104        case QLCNIC_C2H_OPCODE_GET_LINKEVENT_RESPONSE:
1105                qlcnic_handle_linkevent(adapter, &msg);
1106                break;
1107        case QLCNIC_C2H_OPCODE_CONFIG_LOOPBACK:
1108                ret = (u32)(msg.body[1]);
1109                switch (ret) {
1110                case 0:
1111                        adapter->ahw->loopback_state |= QLCNIC_LB_RESPONSE;
1112                        break;
1113                case 1:
1114                        dev_info(dev, "loopback already in progress\n");
1115                        adapter->ahw->diag_cnt = -EINPROGRESS;
1116                        break;
1117                case 2:
1118                        dev_info(dev, "loopback cable is not connected\n");
1119                        adapter->ahw->diag_cnt = -ENODEV;
1120                        break;
1121                default:
1122                        dev_info(dev,
1123                                 "loopback configure request failed, err %x\n",
1124                                 ret);
1125                        adapter->ahw->diag_cnt = -EIO;
1126                        break;
1127                }
1128                break;
1129        case QLCNIC_C2H_OPCODE_GET_DCB_AEN:
1130                qlcnic_dcb_aen_handler(adapter->dcb, (void *)&msg);
1131                break;
1132        default:
1133                break;
1134        }
1135}
1136
1137static struct sk_buff *qlcnic_process_rxbuf(struct qlcnic_adapter *adapter,
1138                                            struct qlcnic_host_rds_ring *ring,
1139                                            u16 index, u16 cksum)
1140{
1141        struct qlcnic_rx_buffer *buffer;
1142        struct sk_buff *skb;
1143
1144        buffer = &ring->rx_buf_arr[index];
1145        if (unlikely(buffer->skb == NULL)) {
1146                WARN_ON(1);
1147                return NULL;
1148        }
1149
1150        pci_unmap_single(adapter->pdev, buffer->dma, ring->dma_size,
1151                         PCI_DMA_FROMDEVICE);
1152
1153        skb = buffer->skb;
1154        if (likely((adapter->netdev->features & NETIF_F_RXCSUM) &&
1155                   (cksum == STATUS_CKSUM_OK || cksum == STATUS_CKSUM_LOOP))) {
1156                adapter->stats.csummed++;
1157                skb->ip_summed = CHECKSUM_UNNECESSARY;
1158        } else {
1159                skb_checksum_none_assert(skb);
1160        }
1161
1162
1163        buffer->skb = NULL;
1164
1165        return skb;
1166}
1167
1168static inline int qlcnic_check_rx_tagging(struct qlcnic_adapter *adapter,
1169                                          struct sk_buff *skb, u16 *vlan_tag)
1170{
1171        struct ethhdr *eth_hdr;
1172
1173        if (!__vlan_get_tag(skb, vlan_tag)) {
1174                eth_hdr = (struct ethhdr *)skb->data;
1175                memmove(skb->data + VLAN_HLEN, eth_hdr, ETH_ALEN * 2);
1176                skb_pull(skb, VLAN_HLEN);
1177        }
1178        if (!adapter->rx_pvid)
1179                return 0;
1180
1181        if (*vlan_tag == adapter->rx_pvid) {
1182                /* Outer vlan tag. Packet should follow non-vlan path */
1183                *vlan_tag = 0xffff;
1184                return 0;
1185        }
1186        if (adapter->flags & QLCNIC_TAGGING_ENABLED)
1187                return 0;
1188
1189        return -EINVAL;
1190}
1191
1192static struct qlcnic_rx_buffer *
1193qlcnic_process_rcv(struct qlcnic_adapter *adapter,
1194                   struct qlcnic_host_sds_ring *sds_ring, int ring,
1195                   u64 sts_data0)
1196{
1197        struct net_device *netdev = adapter->netdev;
1198        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1199        struct qlcnic_rx_buffer *buffer;
1200        struct sk_buff *skb;
1201        struct qlcnic_host_rds_ring *rds_ring;
1202        int index, length, cksum, pkt_offset, is_lb_pkt;
1203        u16 vid = 0xffff, t_vid;
1204
1205        if (unlikely(ring >= adapter->max_rds_rings))
1206                return NULL;
1207
1208        rds_ring = &recv_ctx->rds_rings[ring];
1209
1210        index = qlcnic_get_sts_refhandle(sts_data0);
1211        if (unlikely(index >= rds_ring->num_desc))
1212                return NULL;
1213
1214        buffer = &rds_ring->rx_buf_arr[index];
1215        length = qlcnic_get_sts_totallength(sts_data0);
1216        cksum  = qlcnic_get_sts_status(sts_data0);
1217        pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1218
1219        skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1220        if (!skb)
1221                return buffer;
1222
1223        if (adapter->rx_mac_learn) {
1224                t_vid = 0;
1225                is_lb_pkt = qlcnic_82xx_is_lb_pkt(sts_data0);
1226                qlcnic_add_lb_filter(adapter, skb, is_lb_pkt, t_vid);
1227        }
1228
1229        if (length > rds_ring->skb_size)
1230                skb_put(skb, rds_ring->skb_size);
1231        else
1232                skb_put(skb, length);
1233
1234        if (pkt_offset)
1235                skb_pull(skb, pkt_offset);
1236
1237        if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1238                adapter->stats.rxdropped++;
1239                dev_kfree_skb(skb);
1240                return buffer;
1241        }
1242
1243        skb->protocol = eth_type_trans(skb, netdev);
1244
1245        if (vid != 0xffff)
1246                __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1247
1248        napi_gro_receive(&sds_ring->napi, skb);
1249
1250        adapter->stats.rx_pkts++;
1251        adapter->stats.rxbytes += length;
1252
1253        return buffer;
1254}
1255
1256#define QLC_TCP_HDR_SIZE            20
1257#define QLC_TCP_TS_OPTION_SIZE      12
1258#define QLC_TCP_TS_HDR_SIZE         (QLC_TCP_HDR_SIZE + QLC_TCP_TS_OPTION_SIZE)
1259
1260static struct qlcnic_rx_buffer *
1261qlcnic_process_lro(struct qlcnic_adapter *adapter,
1262                   int ring, u64 sts_data0, u64 sts_data1)
1263{
1264        struct net_device *netdev = adapter->netdev;
1265        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1266        struct qlcnic_rx_buffer *buffer;
1267        struct sk_buff *skb;
1268        struct qlcnic_host_rds_ring *rds_ring;
1269        struct iphdr *iph;
1270        struct ipv6hdr *ipv6h;
1271        struct tcphdr *th;
1272        bool push, timestamp;
1273        int index, l2_hdr_offset, l4_hdr_offset, is_lb_pkt;
1274        u16 lro_length, length, data_offset, t_vid, vid = 0xffff;
1275        u32 seq_number;
1276
1277        if (unlikely(ring >= adapter->max_rds_rings))
1278                return NULL;
1279
1280        rds_ring = &recv_ctx->rds_rings[ring];
1281
1282        index = qlcnic_get_lro_sts_refhandle(sts_data0);
1283        if (unlikely(index >= rds_ring->num_desc))
1284                return NULL;
1285
1286        buffer = &rds_ring->rx_buf_arr[index];
1287
1288        timestamp = qlcnic_get_lro_sts_timestamp(sts_data0);
1289        lro_length = qlcnic_get_lro_sts_length(sts_data0);
1290        l2_hdr_offset = qlcnic_get_lro_sts_l2_hdr_offset(sts_data0);
1291        l4_hdr_offset = qlcnic_get_lro_sts_l4_hdr_offset(sts_data0);
1292        push = qlcnic_get_lro_sts_push_flag(sts_data0);
1293        seq_number = qlcnic_get_lro_sts_seq_number(sts_data1);
1294
1295        skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1296        if (!skb)
1297                return buffer;
1298
1299        if (adapter->rx_mac_learn) {
1300                t_vid = 0;
1301                is_lb_pkt = qlcnic_82xx_is_lb_pkt(sts_data0);
1302                qlcnic_add_lb_filter(adapter, skb, is_lb_pkt, t_vid);
1303        }
1304
1305        if (timestamp)
1306                data_offset = l4_hdr_offset + QLC_TCP_TS_HDR_SIZE;
1307        else
1308                data_offset = l4_hdr_offset + QLC_TCP_HDR_SIZE;
1309
1310        skb_put(skb, lro_length + data_offset);
1311        skb_pull(skb, l2_hdr_offset);
1312
1313        if (unlikely(qlcnic_check_rx_tagging(adapter, skb, &vid))) {
1314                adapter->stats.rxdropped++;
1315                dev_kfree_skb(skb);
1316                return buffer;
1317        }
1318
1319        skb->protocol = eth_type_trans(skb, netdev);
1320
1321        if (ntohs(skb->protocol) == ETH_P_IPV6) {
1322                ipv6h = (struct ipv6hdr *)skb->data;
1323                th = (struct tcphdr *)(skb->data + sizeof(struct ipv6hdr));
1324                length = (th->doff << 2) + lro_length;
1325                ipv6h->payload_len = htons(length);
1326        } else {
1327                iph = (struct iphdr *)skb->data;
1328                th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1329                length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1330                csum_replace2(&iph->check, iph->tot_len, htons(length));
1331                iph->tot_len = htons(length);
1332        }
1333
1334        th->psh = push;
1335        th->seq = htonl(seq_number);
1336        length = skb->len;
1337
1338        if (adapter->flags & QLCNIC_FW_LRO_MSS_CAP) {
1339                skb_shinfo(skb)->gso_size = qlcnic_get_lro_sts_mss(sts_data1);
1340                if (skb->protocol == htons(ETH_P_IPV6))
1341                        skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1342                else
1343                        skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1344        }
1345
1346        if (vid != 0xffff)
1347                __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1348        netif_receive_skb(skb);
1349
1350        adapter->stats.lro_pkts++;
1351        adapter->stats.lrobytes += length;
1352
1353        return buffer;
1354}
1355
1356static int qlcnic_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring, int max)
1357{
1358        struct qlcnic_host_rds_ring *rds_ring;
1359        struct qlcnic_adapter *adapter = sds_ring->adapter;
1360        struct list_head *cur;
1361        struct status_desc *desc;
1362        struct qlcnic_rx_buffer *rxbuf;
1363        int opcode, desc_cnt, count = 0;
1364        u64 sts_data0, sts_data1;
1365        u8 ring;
1366        u32 consumer = sds_ring->consumer;
1367
1368        while (count < max) {
1369                desc = &sds_ring->desc_head[consumer];
1370                sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1371
1372                if (!(sts_data0 & STATUS_OWNER_HOST))
1373                        break;
1374
1375                desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1376                opcode = qlcnic_get_sts_opcode(sts_data0);
1377                switch (opcode) {
1378                case QLCNIC_RXPKT_DESC:
1379                case QLCNIC_OLD_RXPKT_DESC:
1380                case QLCNIC_SYN_OFFLOAD:
1381                        ring = qlcnic_get_sts_type(sts_data0);
1382                        rxbuf = qlcnic_process_rcv(adapter, sds_ring, ring,
1383                                                   sts_data0);
1384                        break;
1385                case QLCNIC_LRO_DESC:
1386                        ring = qlcnic_get_lro_sts_type(sts_data0);
1387                        sts_data1 = le64_to_cpu(desc->status_desc_data[1]);
1388                        rxbuf = qlcnic_process_lro(adapter, ring, sts_data0,
1389                                                   sts_data1);
1390                        break;
1391                case QLCNIC_RESPONSE_DESC:
1392                        qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1393                default:
1394                        goto skip;
1395                }
1396                WARN_ON(desc_cnt > 1);
1397
1398                if (likely(rxbuf))
1399                        list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1400                else
1401                        adapter->stats.null_rxbuf++;
1402skip:
1403                for (; desc_cnt > 0; desc_cnt--) {
1404                        desc = &sds_ring->desc_head[consumer];
1405                        desc->status_desc_data[0] = QLCNIC_DESC_OWNER_FW;
1406                        consumer = get_next_index(consumer, sds_ring->num_desc);
1407                }
1408                count++;
1409        }
1410
1411        for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1412                rds_ring = &adapter->recv_ctx->rds_rings[ring];
1413                if (!list_empty(&sds_ring->free_list[ring])) {
1414                        list_for_each(cur, &sds_ring->free_list[ring]) {
1415                                rxbuf = list_entry(cur, struct qlcnic_rx_buffer,
1416                                                   list);
1417                                qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1418                        }
1419                        spin_lock(&rds_ring->lock);
1420                        list_splice_tail_init(&sds_ring->free_list[ring],
1421                                              &rds_ring->free_list);
1422                        spin_unlock(&rds_ring->lock);
1423                }
1424
1425                qlcnic_post_rx_buffers_nodb(adapter, rds_ring, ring);
1426        }
1427
1428        if (count) {
1429                sds_ring->consumer = consumer;
1430                writel(consumer, sds_ring->crb_sts_consumer);
1431        }
1432
1433        return count;
1434}
1435
1436void qlcnic_post_rx_buffers(struct qlcnic_adapter *adapter,
1437                            struct qlcnic_host_rds_ring *rds_ring, u8 ring_id)
1438{
1439        struct rcv_desc *pdesc;
1440        struct qlcnic_rx_buffer *buffer;
1441        int count = 0;
1442        u32 producer, handle;
1443        struct list_head *head;
1444
1445        producer = rds_ring->producer;
1446        head = &rds_ring->free_list;
1447
1448        while (!list_empty(head)) {
1449
1450                buffer = list_entry(head->next, struct qlcnic_rx_buffer, list);
1451
1452                if (!buffer->skb) {
1453                        if (qlcnic_alloc_rx_skb(adapter, rds_ring, buffer))
1454                                break;
1455                }
1456
1457                count++;
1458                list_del(&buffer->list);
1459
1460                /* make a rcv descriptor  */
1461                pdesc = &rds_ring->desc_head[producer];
1462                pdesc->addr_buffer = cpu_to_le64(buffer->dma);
1463                handle = qlcnic_get_ref_handle(adapter, buffer->ref_handle,
1464                                               ring_id);
1465                pdesc->reference_handle = cpu_to_le16(handle);
1466                pdesc->buffer_length = cpu_to_le32(rds_ring->dma_size);
1467                producer = get_next_index(producer, rds_ring->num_desc);
1468        }
1469
1470        if (count) {
1471                rds_ring->producer = producer;
1472                writel((producer-1) & (rds_ring->num_desc-1),
1473                       rds_ring->crb_rcv_producer);
1474        }
1475}
1476
1477static void dump_skb(struct sk_buff *skb, struct qlcnic_adapter *adapter)
1478{
1479        if (adapter->ahw->msg_enable & NETIF_MSG_DRV) {
1480                char prefix[30];
1481
1482                scnprintf(prefix, sizeof(prefix), "%s: %s: ",
1483                          dev_name(&adapter->pdev->dev), __func__);
1484
1485                print_hex_dump_debug(prefix, DUMP_PREFIX_NONE, 16, 1,
1486                                     skb->data, skb->len, true);
1487        }
1488}
1489
1490static void qlcnic_process_rcv_diag(struct qlcnic_adapter *adapter, int ring,
1491                                    u64 sts_data0)
1492{
1493        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1494        struct sk_buff *skb;
1495        struct qlcnic_host_rds_ring *rds_ring;
1496        int index, length, cksum, pkt_offset;
1497
1498        if (unlikely(ring >= adapter->max_rds_rings))
1499                return;
1500
1501        rds_ring = &recv_ctx->rds_rings[ring];
1502
1503        index = qlcnic_get_sts_refhandle(sts_data0);
1504        length = qlcnic_get_sts_totallength(sts_data0);
1505        if (unlikely(index >= rds_ring->num_desc))
1506                return;
1507
1508        cksum  = qlcnic_get_sts_status(sts_data0);
1509        pkt_offset = qlcnic_get_sts_pkt_offset(sts_data0);
1510
1511        skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1512        if (!skb)
1513                return;
1514
1515        if (length > rds_ring->skb_size)
1516                skb_put(skb, rds_ring->skb_size);
1517        else
1518                skb_put(skb, length);
1519
1520        if (pkt_offset)
1521                skb_pull(skb, pkt_offset);
1522
1523        if (!qlcnic_check_loopback_buff(skb->data, adapter->mac_addr))
1524                adapter->ahw->diag_cnt++;
1525        else
1526                dump_skb(skb, adapter);
1527
1528        dev_kfree_skb_any(skb);
1529        adapter->stats.rx_pkts++;
1530        adapter->stats.rxbytes += length;
1531
1532        return;
1533}
1534
1535void qlcnic_82xx_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring)
1536{
1537        struct qlcnic_adapter *adapter = sds_ring->adapter;
1538        struct status_desc *desc;
1539        u64 sts_data0;
1540        int ring, opcode, desc_cnt;
1541
1542        u32 consumer = sds_ring->consumer;
1543
1544        desc = &sds_ring->desc_head[consumer];
1545        sts_data0 = le64_to_cpu(desc->status_desc_data[0]);
1546
1547        if (!(sts_data0 & STATUS_OWNER_HOST))
1548                return;
1549
1550        desc_cnt = qlcnic_get_sts_desc_cnt(sts_data0);
1551        opcode = qlcnic_get_sts_opcode(sts_data0);
1552        switch (opcode) {
1553        case QLCNIC_RESPONSE_DESC:
1554                qlcnic_handle_fw_message(desc_cnt, consumer, sds_ring);
1555                break;
1556        default:
1557                ring = qlcnic_get_sts_type(sts_data0);
1558                qlcnic_process_rcv_diag(adapter, ring, sts_data0);
1559                break;
1560        }
1561
1562        for (; desc_cnt > 0; desc_cnt--) {
1563                desc = &sds_ring->desc_head[consumer];
1564                desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM);
1565                consumer = get_next_index(consumer, sds_ring->num_desc);
1566        }
1567
1568        sds_ring->consumer = consumer;
1569        writel(consumer, sds_ring->crb_sts_consumer);
1570}
1571
1572int qlcnic_82xx_napi_add(struct qlcnic_adapter *adapter,
1573                         struct net_device *netdev)
1574{
1575        int ring;
1576        struct qlcnic_host_sds_ring *sds_ring;
1577        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1578        struct qlcnic_host_tx_ring *tx_ring;
1579
1580        if (qlcnic_alloc_sds_rings(recv_ctx, adapter->drv_sds_rings))
1581                return -ENOMEM;
1582
1583        for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
1584                sds_ring = &recv_ctx->sds_rings[ring];
1585                if (qlcnic_check_multi_tx(adapter) &&
1586                    !adapter->ahw->diag_test) {
1587                        netif_napi_add(netdev, &sds_ring->napi, qlcnic_rx_poll,
1588                                       NAPI_POLL_WEIGHT);
1589                } else {
1590                        if (ring == (adapter->drv_sds_rings - 1))
1591                                netif_napi_add(netdev, &sds_ring->napi,
1592                                               qlcnic_poll,
1593                                               NAPI_POLL_WEIGHT);
1594                        else
1595                                netif_napi_add(netdev, &sds_ring->napi,
1596                                               qlcnic_rx_poll,
1597                                               NAPI_POLL_WEIGHT);
1598                }
1599        }
1600
1601        if (qlcnic_alloc_tx_rings(adapter, netdev)) {
1602                qlcnic_free_sds_rings(recv_ctx);
1603                return -ENOMEM;
1604        }
1605
1606        if (qlcnic_check_multi_tx(adapter) && !adapter->ahw->diag_test) {
1607                for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
1608                        tx_ring = &adapter->tx_ring[ring];
1609                        netif_tx_napi_add(netdev, &tx_ring->napi, qlcnic_tx_poll,
1610                                       NAPI_POLL_WEIGHT);
1611                }
1612        }
1613
1614        return 0;
1615}
1616
1617void qlcnic_82xx_napi_del(struct qlcnic_adapter *adapter)
1618{
1619        int ring;
1620        struct qlcnic_host_sds_ring *sds_ring;
1621        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1622        struct qlcnic_host_tx_ring *tx_ring;
1623
1624        for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
1625                sds_ring = &recv_ctx->sds_rings[ring];
1626                netif_napi_del(&sds_ring->napi);
1627        }
1628
1629        qlcnic_free_sds_rings(adapter->recv_ctx);
1630
1631        if (qlcnic_check_multi_tx(adapter) && !adapter->ahw->diag_test) {
1632                for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
1633                        tx_ring = &adapter->tx_ring[ring];
1634                        netif_napi_del(&tx_ring->napi);
1635                }
1636        }
1637
1638        qlcnic_free_tx_rings(adapter);
1639}
1640
1641void qlcnic_82xx_napi_enable(struct qlcnic_adapter *adapter)
1642{
1643        int ring;
1644        struct qlcnic_host_sds_ring *sds_ring;
1645        struct qlcnic_host_tx_ring *tx_ring;
1646        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1647
1648        if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1649                return;
1650
1651        for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
1652                sds_ring = &recv_ctx->sds_rings[ring];
1653                napi_enable(&sds_ring->napi);
1654                qlcnic_enable_sds_intr(adapter, sds_ring);
1655        }
1656
1657        if (qlcnic_check_multi_tx(adapter) &&
1658            (adapter->flags & QLCNIC_MSIX_ENABLED) &&
1659            !adapter->ahw->diag_test) {
1660                for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
1661                        tx_ring = &adapter->tx_ring[ring];
1662                        napi_enable(&tx_ring->napi);
1663                        qlcnic_enable_tx_intr(adapter, tx_ring);
1664                }
1665        }
1666}
1667
1668void qlcnic_82xx_napi_disable(struct qlcnic_adapter *adapter)
1669{
1670        int ring;
1671        struct qlcnic_host_sds_ring *sds_ring;
1672        struct qlcnic_host_tx_ring *tx_ring;
1673        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1674
1675        if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
1676                return;
1677
1678        for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
1679                sds_ring = &recv_ctx->sds_rings[ring];
1680                qlcnic_disable_sds_intr(adapter, sds_ring);
1681                napi_synchronize(&sds_ring->napi);
1682                napi_disable(&sds_ring->napi);
1683        }
1684
1685        if ((adapter->flags & QLCNIC_MSIX_ENABLED) &&
1686            !adapter->ahw->diag_test &&
1687            qlcnic_check_multi_tx(adapter)) {
1688                for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
1689                        tx_ring = &adapter->tx_ring[ring];
1690                        qlcnic_disable_tx_intr(adapter, tx_ring);
1691                        napi_synchronize(&tx_ring->napi);
1692                        napi_disable(&tx_ring->napi);
1693                }
1694        }
1695}
1696
1697#define QLC_83XX_NORMAL_LB_PKT  (1ULL << 36)
1698#define QLC_83XX_LRO_LB_PKT     (1ULL << 46)
1699
1700static inline int qlcnic_83xx_is_lb_pkt(u64 sts_data, int lro_pkt)
1701{
1702        if (lro_pkt)
1703                return (sts_data & QLC_83XX_LRO_LB_PKT) ? 1 : 0;
1704        else
1705                return (sts_data & QLC_83XX_NORMAL_LB_PKT) ? 1 : 0;
1706}
1707
1708#define QLCNIC_ENCAP_LENGTH_MASK        0x7f
1709
1710static inline u8 qlcnic_encap_length(u64 sts_data)
1711{
1712        return sts_data & QLCNIC_ENCAP_LENGTH_MASK;
1713}
1714
1715static struct qlcnic_rx_buffer *
1716qlcnic_83xx_process_rcv(struct qlcnic_adapter *adapter,
1717                        struct qlcnic_host_sds_ring *sds_ring,
1718                        u8 ring, u64 sts_data[])
1719{
1720        struct net_device *netdev = adapter->netdev;
1721        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1722        struct qlcnic_rx_buffer *buffer;
1723        struct sk_buff *skb;
1724        struct qlcnic_host_rds_ring *rds_ring;
1725        int index, length, cksum, is_lb_pkt;
1726        u16 vid = 0xffff;
1727        int err;
1728
1729        if (unlikely(ring >= adapter->max_rds_rings))
1730                return NULL;
1731
1732        rds_ring = &recv_ctx->rds_rings[ring];
1733
1734        index = qlcnic_83xx_hndl(sts_data[0]);
1735        if (unlikely(index >= rds_ring->num_desc))
1736                return NULL;
1737
1738        buffer = &rds_ring->rx_buf_arr[index];
1739        length = qlcnic_83xx_pktln(sts_data[0]);
1740        cksum  = qlcnic_83xx_csum_status(sts_data[1]);
1741        skb = qlcnic_process_rxbuf(adapter, rds_ring, index, cksum);
1742        if (!skb)
1743                return buffer;
1744
1745        if (length > rds_ring->skb_size)
1746                skb_put(skb, rds_ring->skb_size);
1747        else
1748                skb_put(skb, length);
1749
1750        err = qlcnic_check_rx_tagging(adapter, skb, &vid);
1751
1752        if (adapter->rx_mac_learn) {
1753                is_lb_pkt = qlcnic_83xx_is_lb_pkt(sts_data[1], 0);
1754                qlcnic_add_lb_filter(adapter, skb, is_lb_pkt, vid);
1755        }
1756
1757        if (unlikely(err)) {
1758                adapter->stats.rxdropped++;
1759                dev_kfree_skb(skb);
1760                return buffer;
1761        }
1762
1763        skb->protocol = eth_type_trans(skb, netdev);
1764
1765        if (qlcnic_encap_length(sts_data[1]) &&
1766            skb->ip_summed == CHECKSUM_UNNECESSARY) {
1767                skb->csum_level = 1;
1768                adapter->stats.encap_rx_csummed++;
1769        }
1770
1771        if (vid != 0xffff)
1772                __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1773
1774        napi_gro_receive(&sds_ring->napi, skb);
1775
1776        adapter->stats.rx_pkts++;
1777        adapter->stats.rxbytes += length;
1778
1779        return buffer;
1780}
1781
1782static struct qlcnic_rx_buffer *
1783qlcnic_83xx_process_lro(struct qlcnic_adapter *adapter,
1784                        u8 ring, u64 sts_data[])
1785{
1786        struct net_device *netdev = adapter->netdev;
1787        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
1788        struct qlcnic_rx_buffer *buffer;
1789        struct sk_buff *skb;
1790        struct qlcnic_host_rds_ring *rds_ring;
1791        struct iphdr *iph;
1792        struct ipv6hdr *ipv6h;
1793        struct tcphdr *th;
1794        bool push;
1795        int l2_hdr_offset, l4_hdr_offset;
1796        int index, is_lb_pkt;
1797        u16 lro_length, length, data_offset, gso_size;
1798        u16 vid = 0xffff;
1799        int err;
1800
1801        if (unlikely(ring >= adapter->max_rds_rings))
1802                return NULL;
1803
1804        rds_ring = &recv_ctx->rds_rings[ring];
1805
1806        index = qlcnic_83xx_hndl(sts_data[0]);
1807        if (unlikely(index >= rds_ring->num_desc))
1808                return NULL;
1809
1810        buffer = &rds_ring->rx_buf_arr[index];
1811
1812        lro_length = qlcnic_83xx_lro_pktln(sts_data[0]);
1813        l2_hdr_offset = qlcnic_83xx_l2_hdr_off(sts_data[1]);
1814        l4_hdr_offset = qlcnic_83xx_l4_hdr_off(sts_data[1]);
1815        push = qlcnic_83xx_is_psh_bit(sts_data[1]);
1816
1817        skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
1818        if (!skb)
1819                return buffer;
1820
1821        if (qlcnic_83xx_is_tstamp(sts_data[1]))
1822                data_offset = l4_hdr_offset + QLCNIC_TCP_TS_HDR_SIZE;
1823        else
1824                data_offset = l4_hdr_offset + QLCNIC_TCP_HDR_SIZE;
1825
1826        skb_put(skb, lro_length + data_offset);
1827        skb_pull(skb, l2_hdr_offset);
1828
1829        err = qlcnic_check_rx_tagging(adapter, skb, &vid);
1830
1831        if (adapter->rx_mac_learn) {
1832                is_lb_pkt = qlcnic_83xx_is_lb_pkt(sts_data[1], 1);
1833                qlcnic_add_lb_filter(adapter, skb, is_lb_pkt, vid);
1834        }
1835
1836        if (unlikely(err)) {
1837                adapter->stats.rxdropped++;
1838                dev_kfree_skb(skb);
1839                return buffer;
1840        }
1841
1842        skb->protocol = eth_type_trans(skb, netdev);
1843        if (ntohs(skb->protocol) == ETH_P_IPV6) {
1844                ipv6h = (struct ipv6hdr *)skb->data;
1845                th = (struct tcphdr *)(skb->data + sizeof(struct ipv6hdr));
1846
1847                length = (th->doff << 2) + lro_length;
1848                ipv6h->payload_len = htons(length);
1849        } else {
1850                iph = (struct iphdr *)skb->data;
1851                th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
1852                length = (iph->ihl << 2) + (th->doff << 2) + lro_length;
1853                csum_replace2(&iph->check, iph->tot_len, htons(length));
1854                iph->tot_len = htons(length);
1855        }
1856
1857        th->psh = push;
1858        length = skb->len;
1859
1860        if (adapter->flags & QLCNIC_FW_LRO_MSS_CAP) {
1861                gso_size = qlcnic_83xx_get_lro_sts_mss(sts_data[0]);
1862                skb_shinfo(skb)->gso_size = gso_size;
1863                if (skb->protocol == htons(ETH_P_IPV6))
1864                        skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1865                else
1866                        skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1867        }
1868
1869        if (vid != 0xffff)
1870                __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1871
1872        netif_receive_skb(skb);
1873
1874        adapter->stats.lro_pkts++;
1875        adapter->stats.lrobytes += length;
1876        return buffer;
1877}
1878
1879static int qlcnic_83xx_process_rcv_ring(struct qlcnic_host_sds_ring *sds_ring,
1880                                        int max)
1881{
1882        struct qlcnic_host_rds_ring *rds_ring;
1883        struct qlcnic_adapter *adapter = sds_ring->adapter;
1884        struct list_head *cur;
1885        struct status_desc *desc;
1886        struct qlcnic_rx_buffer *rxbuf = NULL;
1887        u8 ring;
1888        u64 sts_data[2];
1889        int count = 0, opcode;
1890        u32 consumer = sds_ring->consumer;
1891
1892        while (count < max) {
1893                desc = &sds_ring->desc_head[consumer];
1894                sts_data[1] = le64_to_cpu(desc->status_desc_data[1]);
1895                opcode = qlcnic_83xx_opcode(sts_data[1]);
1896                if (!opcode)
1897                        break;
1898                sts_data[0] = le64_to_cpu(desc->status_desc_data[0]);
1899                ring = QLCNIC_FETCH_RING_ID(sts_data[0]);
1900
1901                switch (opcode) {
1902                case QLC_83XX_REG_DESC:
1903                        rxbuf = qlcnic_83xx_process_rcv(adapter, sds_ring,
1904                                                        ring, sts_data);
1905                        break;
1906                case QLC_83XX_LRO_DESC:
1907                        rxbuf = qlcnic_83xx_process_lro(adapter, ring,
1908                                                        sts_data);
1909                        break;
1910                default:
1911                        dev_info(&adapter->pdev->dev,
1912                                 "Unknown opcode: 0x%x\n", opcode);
1913                        goto skip;
1914                }
1915
1916                if (likely(rxbuf))
1917                        list_add_tail(&rxbuf->list, &sds_ring->free_list[ring]);
1918                else
1919                        adapter->stats.null_rxbuf++;
1920skip:
1921                desc = &sds_ring->desc_head[consumer];
1922                /* Reset the descriptor */
1923                desc->status_desc_data[1] = 0;
1924                consumer = get_next_index(consumer, sds_ring->num_desc);
1925                count++;
1926        }
1927        for (ring = 0; ring < adapter->max_rds_rings; ring++) {
1928                rds_ring = &adapter->recv_ctx->rds_rings[ring];
1929                if (!list_empty(&sds_ring->free_list[ring])) {
1930                        list_for_each(cur, &sds_ring->free_list[ring]) {
1931                                rxbuf = list_entry(cur, struct qlcnic_rx_buffer,
1932                                                   list);
1933                                qlcnic_alloc_rx_skb(adapter, rds_ring, rxbuf);
1934                        }
1935                        spin_lock(&rds_ring->lock);
1936                        list_splice_tail_init(&sds_ring->free_list[ring],
1937                                              &rds_ring->free_list);
1938                        spin_unlock(&rds_ring->lock);
1939                }
1940                qlcnic_post_rx_buffers_nodb(adapter, rds_ring, ring);
1941        }
1942        if (count) {
1943                sds_ring->consumer = consumer;
1944                writel(consumer, sds_ring->crb_sts_consumer);
1945        }
1946        return count;
1947}
1948
1949static int qlcnic_83xx_msix_sriov_vf_poll(struct napi_struct *napi, int budget)
1950{
1951        int tx_complete;
1952        int work_done;
1953        struct qlcnic_host_sds_ring *sds_ring;
1954        struct qlcnic_adapter *adapter;
1955        struct qlcnic_host_tx_ring *tx_ring;
1956
1957        sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
1958        adapter = sds_ring->adapter;
1959        /* tx ring count = 1 */
1960        tx_ring = adapter->tx_ring;
1961
1962        tx_complete = qlcnic_process_cmd_ring(adapter, tx_ring, budget);
1963        work_done = qlcnic_83xx_process_rcv_ring(sds_ring, budget);
1964
1965        /* Check if we need a repoll */
1966        if (!tx_complete)
1967                work_done = budget;
1968
1969        if (work_done < budget) {
1970                napi_complete_done(&sds_ring->napi, work_done);
1971                qlcnic_enable_sds_intr(adapter, sds_ring);
1972        }
1973
1974        return work_done;
1975}
1976
1977static int qlcnic_83xx_poll(struct napi_struct *napi, int budget)
1978{
1979        int tx_complete;
1980        int work_done;
1981        struct qlcnic_host_sds_ring *sds_ring;
1982        struct qlcnic_adapter *adapter;
1983        struct qlcnic_host_tx_ring *tx_ring;
1984
1985        sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
1986        adapter = sds_ring->adapter;
1987        /* tx ring count = 1 */
1988        tx_ring = adapter->tx_ring;
1989
1990        tx_complete = qlcnic_process_cmd_ring(adapter, tx_ring, budget);
1991        work_done = qlcnic_83xx_process_rcv_ring(sds_ring, budget);
1992
1993        /* Check if we need a repoll */
1994        if (!tx_complete)
1995                work_done = budget;
1996
1997        if (work_done < budget) {
1998                napi_complete_done(&sds_ring->napi, work_done);
1999                qlcnic_enable_sds_intr(adapter, sds_ring);
2000        }
2001
2002        return work_done;
2003}
2004
2005static int qlcnic_83xx_msix_tx_poll(struct napi_struct *napi, int budget)
2006{
2007        int work_done;
2008        struct qlcnic_host_tx_ring *tx_ring;
2009        struct qlcnic_adapter *adapter;
2010
2011        tx_ring = container_of(napi, struct qlcnic_host_tx_ring, napi);
2012        adapter = tx_ring->adapter;
2013        work_done = qlcnic_process_cmd_ring(adapter, tx_ring, budget);
2014        if (work_done) {
2015                napi_complete(&tx_ring->napi);
2016                if (test_bit(__QLCNIC_DEV_UP , &adapter->state))
2017                        qlcnic_enable_tx_intr(adapter, tx_ring);
2018        } else {
2019                /* need a repoll */
2020                work_done = budget;
2021        }
2022
2023        return work_done;
2024}
2025
2026static int qlcnic_83xx_rx_poll(struct napi_struct *napi, int budget)
2027{
2028        int work_done;
2029        struct qlcnic_host_sds_ring *sds_ring;
2030        struct qlcnic_adapter *adapter;
2031
2032        sds_ring = container_of(napi, struct qlcnic_host_sds_ring, napi);
2033        adapter = sds_ring->adapter;
2034        work_done = qlcnic_83xx_process_rcv_ring(sds_ring, budget);
2035        if (work_done < budget) {
2036                napi_complete_done(&sds_ring->napi, work_done);
2037                if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
2038                        qlcnic_enable_sds_intr(adapter, sds_ring);
2039        }
2040
2041        return work_done;
2042}
2043
2044void qlcnic_83xx_napi_enable(struct qlcnic_adapter *adapter)
2045{
2046        int ring;
2047        struct qlcnic_host_sds_ring *sds_ring;
2048        struct qlcnic_host_tx_ring *tx_ring;
2049        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2050
2051        if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2052                return;
2053
2054        for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
2055                sds_ring = &recv_ctx->sds_rings[ring];
2056                napi_enable(&sds_ring->napi);
2057                if (adapter->flags & QLCNIC_MSIX_ENABLED)
2058                        qlcnic_enable_sds_intr(adapter, sds_ring);
2059        }
2060
2061        if ((adapter->flags & QLCNIC_MSIX_ENABLED) &&
2062            !(adapter->flags & QLCNIC_TX_INTR_SHARED)) {
2063                for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
2064                        tx_ring = &adapter->tx_ring[ring];
2065                        napi_enable(&tx_ring->napi);
2066                        qlcnic_enable_tx_intr(adapter, tx_ring);
2067                }
2068        }
2069}
2070
2071void qlcnic_83xx_napi_disable(struct qlcnic_adapter *adapter)
2072{
2073        int ring;
2074        struct qlcnic_host_sds_ring *sds_ring;
2075        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2076        struct qlcnic_host_tx_ring *tx_ring;
2077
2078        if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
2079                return;
2080
2081        for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
2082                sds_ring = &recv_ctx->sds_rings[ring];
2083                if (adapter->flags & QLCNIC_MSIX_ENABLED)
2084                        qlcnic_disable_sds_intr(adapter, sds_ring);
2085                napi_synchronize(&sds_ring->napi);
2086                napi_disable(&sds_ring->napi);
2087        }
2088
2089        if ((adapter->flags & QLCNIC_MSIX_ENABLED) &&
2090            !(adapter->flags & QLCNIC_TX_INTR_SHARED)) {
2091                for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
2092                        tx_ring = &adapter->tx_ring[ring];
2093                        qlcnic_disable_tx_intr(adapter, tx_ring);
2094                        napi_synchronize(&tx_ring->napi);
2095                        napi_disable(&tx_ring->napi);
2096                }
2097        }
2098}
2099
2100int qlcnic_83xx_napi_add(struct qlcnic_adapter *adapter,
2101                         struct net_device *netdev)
2102{
2103        int ring;
2104        struct qlcnic_host_sds_ring *sds_ring;
2105        struct qlcnic_host_tx_ring *tx_ring;
2106        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2107
2108        if (qlcnic_alloc_sds_rings(recv_ctx, adapter->drv_sds_rings))
2109                return -ENOMEM;
2110
2111        for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
2112                sds_ring = &recv_ctx->sds_rings[ring];
2113                if (adapter->flags & QLCNIC_MSIX_ENABLED) {
2114                        if (!(adapter->flags & QLCNIC_TX_INTR_SHARED))
2115                                netif_napi_add(netdev, &sds_ring->napi,
2116                                               qlcnic_83xx_rx_poll,
2117                                               NAPI_POLL_WEIGHT);
2118                        else
2119                                netif_napi_add(netdev, &sds_ring->napi,
2120                                               qlcnic_83xx_msix_sriov_vf_poll,
2121                                               NAPI_POLL_WEIGHT);
2122
2123                } else {
2124                        netif_napi_add(netdev, &sds_ring->napi,
2125                                       qlcnic_83xx_poll,
2126                                       NAPI_POLL_WEIGHT);
2127                }
2128        }
2129
2130        if (qlcnic_alloc_tx_rings(adapter, netdev)) {
2131                qlcnic_free_sds_rings(recv_ctx);
2132                return -ENOMEM;
2133        }
2134
2135        if ((adapter->flags & QLCNIC_MSIX_ENABLED) &&
2136            !(adapter->flags & QLCNIC_TX_INTR_SHARED)) {
2137                for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
2138                        tx_ring = &adapter->tx_ring[ring];
2139                        netif_tx_napi_add(netdev, &tx_ring->napi,
2140                                       qlcnic_83xx_msix_tx_poll,
2141                                       NAPI_POLL_WEIGHT);
2142                }
2143        }
2144
2145        return 0;
2146}
2147
2148void qlcnic_83xx_napi_del(struct qlcnic_adapter *adapter)
2149{
2150        int ring;
2151        struct qlcnic_host_sds_ring *sds_ring;
2152        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2153        struct qlcnic_host_tx_ring *tx_ring;
2154
2155        for (ring = 0; ring < adapter->drv_sds_rings; ring++) {
2156                sds_ring = &recv_ctx->sds_rings[ring];
2157                netif_napi_del(&sds_ring->napi);
2158        }
2159
2160        qlcnic_free_sds_rings(adapter->recv_ctx);
2161
2162        if ((adapter->flags & QLCNIC_MSIX_ENABLED) &&
2163            !(adapter->flags & QLCNIC_TX_INTR_SHARED)) {
2164                for (ring = 0; ring < adapter->drv_tx_rings; ring++) {
2165                        tx_ring = &adapter->tx_ring[ring];
2166                        netif_napi_del(&tx_ring->napi);
2167                }
2168        }
2169
2170        qlcnic_free_tx_rings(adapter);
2171}
2172
2173static void qlcnic_83xx_process_rcv_diag(struct qlcnic_adapter *adapter,
2174                                         int ring, u64 sts_data[])
2175{
2176        struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
2177        struct sk_buff *skb;
2178        struct qlcnic_host_rds_ring *rds_ring;
2179        int index, length;
2180
2181        if (unlikely(ring >= adapter->max_rds_rings))
2182                return;
2183
2184        rds_ring = &recv_ctx->rds_rings[ring];
2185        index = qlcnic_83xx_hndl(sts_data[0]);
2186        if (unlikely(index >= rds_ring->num_desc))
2187                return;
2188
2189        length = qlcnic_83xx_pktln(sts_data[0]);
2190
2191        skb = qlcnic_process_rxbuf(adapter, rds_ring, index, STATUS_CKSUM_OK);
2192        if (!skb)
2193                return;
2194
2195        if (length > rds_ring->skb_size)
2196                skb_put(skb, rds_ring->skb_size);
2197        else
2198                skb_put(skb, length);
2199
2200        if (!qlcnic_check_loopback_buff(skb->data, adapter->mac_addr))
2201                adapter->ahw->diag_cnt++;
2202        else
2203                dump_skb(skb, adapter);
2204
2205        dev_kfree_skb_any(skb);
2206        return;
2207}
2208
2209void qlcnic_83xx_process_rcv_ring_diag(struct qlcnic_host_sds_ring *sds_ring)
2210{
2211        struct qlcnic_adapter *adapter = sds_ring->adapter;
2212        struct status_desc *desc;
2213        u64 sts_data[2];
2214        int ring, opcode;
2215        u32 consumer = sds_ring->consumer;
2216
2217        desc = &sds_ring->desc_head[consumer];
2218        sts_data[0] = le64_to_cpu(desc->status_desc_data[0]);
2219        sts_data[1] = le64_to_cpu(desc->status_desc_data[1]);
2220        opcode = qlcnic_83xx_opcode(sts_data[1]);
2221        if (!opcode)
2222                return;
2223
2224        ring = QLCNIC_FETCH_RING_ID(sts_data[0]);
2225        qlcnic_83xx_process_rcv_diag(adapter, ring, sts_data);
2226        desc = &sds_ring->desc_head[consumer];
2227        desc->status_desc_data[0] = cpu_to_le64(STATUS_OWNER_PHANTOM);
2228        consumer = get_next_index(consumer, sds_ring->num_desc);
2229        sds_ring->consumer = consumer;
2230        writel(consumer, sds_ring->crb_sts_consumer);
2231}
2232