linux/drivers/scsi/fnic/fnic_fcs.c
<<
>>
Prefs
   1/*
   2 * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
   3 * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
   4 *
   5 * This program is free software; you may redistribute it and/or modify
   6 * it under the terms of the GNU General Public License as published by
   7 * the Free Software Foundation; version 2 of the License.
   8 *
   9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16 * SOFTWARE.
  17 */
  18#include <linux/errno.h>
  19#include <linux/pci.h>
  20#include <linux/slab.h>
  21#include <linux/skbuff.h>
  22#include <linux/interrupt.h>
  23#include <linux/spinlock.h>
  24#include <linux/if_ether.h>
  25#include <linux/if_vlan.h>
  26#include <linux/workqueue.h>
  27#include <scsi/fc/fc_fip.h>
  28#include <scsi/fc/fc_els.h>
  29#include <scsi/fc/fc_fcoe.h>
  30#include <scsi/fc_frame.h>
  31#include <scsi/libfc.h>
  32#include "fnic_io.h"
  33#include "fnic.h"
  34#include "fnic_fip.h"
  35#include "cq_enet_desc.h"
  36#include "cq_exch_desc.h"
  37
  38static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
  39struct workqueue_struct *fnic_fip_queue;
  40struct workqueue_struct *fnic_event_queue;
  41
  42static void fnic_set_eth_mode(struct fnic *);
  43static void fnic_fcoe_send_vlan_req(struct fnic *fnic);
  44static void fnic_fcoe_start_fcf_disc(struct fnic *fnic);
  45static void fnic_fcoe_process_vlan_resp(struct fnic *fnic, struct sk_buff *);
  46static int fnic_fcoe_vlan_check(struct fnic *fnic, u16 flag);
  47static int fnic_fcoe_handle_fip_frame(struct fnic *fnic, struct sk_buff *skb);
  48
  49void fnic_handle_link(struct work_struct *work)
  50{
  51        struct fnic *fnic = container_of(work, struct fnic, link_work);
  52        unsigned long flags;
  53        int old_link_status;
  54        u32 old_link_down_cnt;
  55
  56        spin_lock_irqsave(&fnic->fnic_lock, flags);
  57
  58        if (fnic->stop_rx_link_events) {
  59                spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  60                return;
  61        }
  62
  63        old_link_down_cnt = fnic->link_down_cnt;
  64        old_link_status = fnic->link_status;
  65        fnic->link_status = vnic_dev_link_status(fnic->vdev);
  66        fnic->link_down_cnt = vnic_dev_link_down_cnt(fnic->vdev);
  67
  68        atomic64_set(&fnic->fnic_stats.misc_stats.current_port_speed,
  69                        vnic_dev_port_speed(fnic->vdev));
  70        shost_printk(KERN_INFO, fnic->lport->host, "Current vnic speed set to :  %llu\n",
  71                        (u64)atomic64_read(
  72                        &fnic->fnic_stats.misc_stats.current_port_speed));
  73
  74        switch (vnic_dev_port_speed(fnic->vdev)) {
  75        case DCEM_PORTSPEED_10G:
  76                fc_host_speed(fnic->lport->host)   = FC_PORTSPEED_10GBIT;
  77                fnic->lport->link_supported_speeds = FC_PORTSPEED_10GBIT;
  78                break;
  79        case DCEM_PORTSPEED_20G:
  80                fc_host_speed(fnic->lport->host)   = FC_PORTSPEED_20GBIT;
  81                fnic->lport->link_supported_speeds = FC_PORTSPEED_20GBIT;
  82                break;
  83        case DCEM_PORTSPEED_25G:
  84                fc_host_speed(fnic->lport->host)   = FC_PORTSPEED_25GBIT;
  85                fnic->lport->link_supported_speeds = FC_PORTSPEED_25GBIT;
  86                break;
  87        case DCEM_PORTSPEED_40G:
  88        case DCEM_PORTSPEED_4x10G:
  89                fc_host_speed(fnic->lport->host)   = FC_PORTSPEED_40GBIT;
  90                fnic->lport->link_supported_speeds = FC_PORTSPEED_40GBIT;
  91                break;
  92        case DCEM_PORTSPEED_100G:
  93                fc_host_speed(fnic->lport->host)   = FC_PORTSPEED_100GBIT;
  94                fnic->lport->link_supported_speeds = FC_PORTSPEED_100GBIT;
  95                break;
  96        default:
  97                fc_host_speed(fnic->lport->host)   = FC_PORTSPEED_UNKNOWN;
  98                fnic->lport->link_supported_speeds = FC_PORTSPEED_UNKNOWN;
  99                break;
 100        }
 101
 102        if (old_link_status == fnic->link_status) {
 103                if (!fnic->link_status) {
 104                        /* DOWN -> DOWN */
 105                        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 106                        fnic_fc_trace_set_data(fnic->lport->host->host_no,
 107                                FNIC_FC_LE, "Link Status: DOWN->DOWN",
 108                                strlen("Link Status: DOWN->DOWN"));
 109                } else {
 110                        if (old_link_down_cnt != fnic->link_down_cnt) {
 111                                /* UP -> DOWN -> UP */
 112                                fnic->lport->host_stats.link_failure_count++;
 113                                spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 114                                fnic_fc_trace_set_data(
 115                                        fnic->lport->host->host_no,
 116                                        FNIC_FC_LE,
 117                                        "Link Status:UP_DOWN_UP",
 118                                        strlen("Link_Status:UP_DOWN_UP")
 119                                        );
 120                                FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
 121                                             "link down\n");
 122                                fcoe_ctlr_link_down(&fnic->ctlr);
 123                                if (fnic->config.flags & VFCF_FIP_CAPABLE) {
 124                                        /* start FCoE VLAN discovery */
 125                                        fnic_fc_trace_set_data(
 126                                                fnic->lport->host->host_no,
 127                                                FNIC_FC_LE,
 128                                                "Link Status: UP_DOWN_UP_VLAN",
 129                                                strlen(
 130                                                "Link Status: UP_DOWN_UP_VLAN")
 131                                                );
 132                                        fnic_fcoe_send_vlan_req(fnic);
 133                                        return;
 134                                }
 135                                FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
 136                                             "link up\n");
 137                                fcoe_ctlr_link_up(&fnic->ctlr);
 138                        } else {
 139                                /* UP -> UP */
 140                                spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 141                                fnic_fc_trace_set_data(
 142                                        fnic->lport->host->host_no, FNIC_FC_LE,
 143                                        "Link Status: UP_UP",
 144                                        strlen("Link Status: UP_UP"));
 145                        }
 146                }
 147        } else if (fnic->link_status) {
 148                /* DOWN -> UP */
 149                spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 150                if (fnic->config.flags & VFCF_FIP_CAPABLE) {
 151                        /* start FCoE VLAN discovery */
 152                                fnic_fc_trace_set_data(
 153                                fnic->lport->host->host_no,
 154                                FNIC_FC_LE, "Link Status: DOWN_UP_VLAN",
 155                                strlen("Link Status: DOWN_UP_VLAN"));
 156                        fnic_fcoe_send_vlan_req(fnic);
 157                        return;
 158                }
 159                FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host, "link up\n");
 160                fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_LE,
 161                        "Link Status: DOWN_UP", strlen("Link Status: DOWN_UP"));
 162                fcoe_ctlr_link_up(&fnic->ctlr);
 163        } else {
 164                /* UP -> DOWN */
 165                fnic->lport->host_stats.link_failure_count++;
 166                spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 167                FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host, "link down\n");
 168                fnic_fc_trace_set_data(
 169                        fnic->lport->host->host_no, FNIC_FC_LE,
 170                        "Link Status: UP_DOWN",
 171                        strlen("Link Status: UP_DOWN"));
 172                if (fnic->config.flags & VFCF_FIP_CAPABLE) {
 173                        FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
 174                                "deleting fip-timer during link-down\n");
 175                        del_timer_sync(&fnic->fip_timer);
 176                }
 177                fcoe_ctlr_link_down(&fnic->ctlr);
 178        }
 179
 180}
 181
 182/*
 183 * This function passes incoming fabric frames to libFC
 184 */
 185void fnic_handle_frame(struct work_struct *work)
 186{
 187        struct fnic *fnic = container_of(work, struct fnic, frame_work);
 188        struct fc_lport *lp = fnic->lport;
 189        unsigned long flags;
 190        struct sk_buff *skb;
 191        struct fc_frame *fp;
 192
 193        while ((skb = skb_dequeue(&fnic->frame_queue))) {
 194
 195                spin_lock_irqsave(&fnic->fnic_lock, flags);
 196                if (fnic->stop_rx_link_events) {
 197                        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 198                        dev_kfree_skb(skb);
 199                        return;
 200                }
 201                fp = (struct fc_frame *)skb;
 202
 203                /*
 204                 * If we're in a transitional state, just re-queue and return.
 205                 * The queue will be serviced when we get to a stable state.
 206                 */
 207                if (fnic->state != FNIC_IN_FC_MODE &&
 208                    fnic->state != FNIC_IN_ETH_MODE) {
 209                        skb_queue_head(&fnic->frame_queue, skb);
 210                        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 211                        return;
 212                }
 213                spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 214
 215                fc_exch_recv(lp, fp);
 216        }
 217}
 218
 219void fnic_fcoe_evlist_free(struct fnic *fnic)
 220{
 221        struct fnic_event *fevt = NULL;
 222        struct fnic_event *next = NULL;
 223        unsigned long flags;
 224
 225        spin_lock_irqsave(&fnic->fnic_lock, flags);
 226        if (list_empty(&fnic->evlist)) {
 227                spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 228                return;
 229        }
 230
 231        list_for_each_entry_safe(fevt, next, &fnic->evlist, list) {
 232                list_del(&fevt->list);
 233                kfree(fevt);
 234        }
 235        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 236}
 237
 238void fnic_handle_event(struct work_struct *work)
 239{
 240        struct fnic *fnic = container_of(work, struct fnic, event_work);
 241        struct fnic_event *fevt = NULL;
 242        struct fnic_event *next = NULL;
 243        unsigned long flags;
 244
 245        spin_lock_irqsave(&fnic->fnic_lock, flags);
 246        if (list_empty(&fnic->evlist)) {
 247                spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 248                return;
 249        }
 250
 251        list_for_each_entry_safe(fevt, next, &fnic->evlist, list) {
 252                if (fnic->stop_rx_link_events) {
 253                        list_del(&fevt->list);
 254                        kfree(fevt);
 255                        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 256                        return;
 257                }
 258                /*
 259                 * If we're in a transitional state, just re-queue and return.
 260                 * The queue will be serviced when we get to a stable state.
 261                 */
 262                if (fnic->state != FNIC_IN_FC_MODE &&
 263                    fnic->state != FNIC_IN_ETH_MODE) {
 264                        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 265                        return;
 266                }
 267
 268                list_del(&fevt->list);
 269                switch (fevt->event) {
 270                case FNIC_EVT_START_VLAN_DISC:
 271                        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 272                        fnic_fcoe_send_vlan_req(fnic);
 273                        spin_lock_irqsave(&fnic->fnic_lock, flags);
 274                        break;
 275                case FNIC_EVT_START_FCF_DISC:
 276                        FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
 277                                  "Start FCF Discovery\n");
 278                        fnic_fcoe_start_fcf_disc(fnic);
 279                        break;
 280                default:
 281                        FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
 282                                  "Unknown event 0x%x\n", fevt->event);
 283                        break;
 284                }
 285                kfree(fevt);
 286        }
 287        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 288}
 289
 290/**
 291 * Check if the Received FIP FLOGI frame is rejected
 292 * @fip: The FCoE controller that received the frame
 293 * @skb: The received FIP frame
 294 *
 295 * Returns non-zero if the frame is rejected with unsupported cmd with
 296 * insufficient resource els explanation.
 297 */
 298static inline int is_fnic_fip_flogi_reject(struct fcoe_ctlr *fip,
 299                                         struct sk_buff *skb)
 300{
 301        struct fc_lport *lport = fip->lp;
 302        struct fip_header *fiph;
 303        struct fc_frame_header *fh = NULL;
 304        struct fip_desc *desc;
 305        struct fip_encaps *els;
 306        enum fip_desc_type els_dtype = 0;
 307        u16 op;
 308        u8 els_op;
 309        u8 sub;
 310
 311        size_t els_len = 0;
 312        size_t rlen;
 313        size_t dlen = 0;
 314
 315        if (skb_linearize(skb))
 316                return 0;
 317
 318        if (skb->len < sizeof(*fiph))
 319                return 0;
 320
 321        fiph = (struct fip_header *)skb->data;
 322        op = ntohs(fiph->fip_op);
 323        sub = fiph->fip_subcode;
 324
 325        if (op != FIP_OP_LS)
 326                return 0;
 327
 328        if (sub != FIP_SC_REP)
 329                return 0;
 330
 331        rlen = ntohs(fiph->fip_dl_len) * 4;
 332        if (rlen + sizeof(*fiph) > skb->len)
 333                return 0;
 334
 335        desc = (struct fip_desc *)(fiph + 1);
 336        dlen = desc->fip_dlen * FIP_BPW;
 337
 338        if (desc->fip_dtype == FIP_DT_FLOGI) {
 339
 340                if (dlen < sizeof(*els) + sizeof(*fh) + 1)
 341                        return 0;
 342
 343                els_len = dlen - sizeof(*els);
 344                els = (struct fip_encaps *)desc;
 345                fh = (struct fc_frame_header *)(els + 1);
 346                els_dtype = desc->fip_dtype;
 347
 348                if (!fh)
 349                        return 0;
 350
 351                /*
 352                 * ELS command code, reason and explanation should be = Reject,
 353                 * unsupported command and insufficient resource
 354                 */
 355                els_op = *(u8 *)(fh + 1);
 356                if (els_op == ELS_LS_RJT) {
 357                        shost_printk(KERN_INFO, lport->host,
 358                                  "Flogi Request Rejected by Switch\n");
 359                        return 1;
 360                }
 361                shost_printk(KERN_INFO, lport->host,
 362                                "Flogi Request Accepted by Switch\n");
 363        }
 364        return 0;
 365}
 366
 367static void fnic_fcoe_send_vlan_req(struct fnic *fnic)
 368{
 369        struct fcoe_ctlr *fip = &fnic->ctlr;
 370        struct fnic_stats *fnic_stats = &fnic->fnic_stats;
 371        struct sk_buff *skb;
 372        char *eth_fr;
 373        int fr_len;
 374        struct fip_vlan *vlan;
 375        u64 vlan_tov;
 376
 377        fnic_fcoe_reset_vlans(fnic);
 378        fnic->set_vlan(fnic, 0);
 379
 380        if (printk_ratelimit())
 381                FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
 382                          "Sending VLAN request...\n");
 383
 384        skb = dev_alloc_skb(sizeof(struct fip_vlan));
 385        if (!skb)
 386                return;
 387
 388        fr_len = sizeof(*vlan);
 389        eth_fr = (char *)skb->data;
 390        vlan = (struct fip_vlan *)eth_fr;
 391
 392        memset(vlan, 0, sizeof(*vlan));
 393        memcpy(vlan->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
 394        memcpy(vlan->eth.h_dest, fcoe_all_fcfs, ETH_ALEN);
 395        vlan->eth.h_proto = htons(ETH_P_FIP);
 396
 397        vlan->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
 398        vlan->fip.fip_op = htons(FIP_OP_VLAN);
 399        vlan->fip.fip_subcode = FIP_SC_VL_REQ;
 400        vlan->fip.fip_dl_len = htons(sizeof(vlan->desc) / FIP_BPW);
 401
 402        vlan->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
 403        vlan->desc.mac.fd_desc.fip_dlen = sizeof(vlan->desc.mac) / FIP_BPW;
 404        memcpy(&vlan->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
 405
 406        vlan->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
 407        vlan->desc.wwnn.fd_desc.fip_dlen = sizeof(vlan->desc.wwnn) / FIP_BPW;
 408        put_unaligned_be64(fip->lp->wwnn, &vlan->desc.wwnn.fd_wwn);
 409        atomic64_inc(&fnic_stats->vlan_stats.vlan_disc_reqs);
 410
 411        skb_put(skb, sizeof(*vlan));
 412        skb->protocol = htons(ETH_P_FIP);
 413        skb_reset_mac_header(skb);
 414        skb_reset_network_header(skb);
 415        fip->send(fip, skb);
 416
 417        /* set a timer so that we can retry if there no response */
 418        vlan_tov = jiffies + msecs_to_jiffies(FCOE_CTLR_FIPVLAN_TOV);
 419        mod_timer(&fnic->fip_timer, round_jiffies(vlan_tov));
 420}
 421
 422static void fnic_fcoe_process_vlan_resp(struct fnic *fnic, struct sk_buff *skb)
 423{
 424        struct fcoe_ctlr *fip = &fnic->ctlr;
 425        struct fip_header *fiph;
 426        struct fip_desc *desc;
 427        struct fnic_stats *fnic_stats = &fnic->fnic_stats;
 428        u16 vid;
 429        size_t rlen;
 430        size_t dlen;
 431        struct fcoe_vlan *vlan;
 432        u64 sol_time;
 433        unsigned long flags;
 434
 435        FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
 436                  "Received VLAN response...\n");
 437
 438        fiph = (struct fip_header *) skb->data;
 439
 440        FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
 441                  "Received VLAN response... OP 0x%x SUB_OP 0x%x\n",
 442                  ntohs(fiph->fip_op), fiph->fip_subcode);
 443
 444        rlen = ntohs(fiph->fip_dl_len) * 4;
 445        fnic_fcoe_reset_vlans(fnic);
 446        spin_lock_irqsave(&fnic->vlans_lock, flags);
 447        desc = (struct fip_desc *)(fiph + 1);
 448        while (rlen > 0) {
 449                dlen = desc->fip_dlen * FIP_BPW;
 450                switch (desc->fip_dtype) {
 451                case FIP_DT_VLAN:
 452                        vid = ntohs(((struct fip_vlan_desc *)desc)->fd_vlan);
 453                        shost_printk(KERN_INFO, fnic->lport->host,
 454                                  "process_vlan_resp: FIP VLAN %d\n", vid);
 455                        vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
 456                        if (!vlan) {
 457                                /* retry from timer */
 458                                spin_unlock_irqrestore(&fnic->vlans_lock,
 459                                                        flags);
 460                                goto out;
 461                        }
 462                        vlan->vid = vid & 0x0fff;
 463                        vlan->state = FIP_VLAN_AVAIL;
 464                        list_add_tail(&vlan->list, &fnic->vlans);
 465                        break;
 466                }
 467                desc = (struct fip_desc *)((char *)desc + dlen);
 468                rlen -= dlen;
 469        }
 470
 471        /* any VLAN descriptors present ? */
 472        if (list_empty(&fnic->vlans)) {
 473                /* retry from timer */
 474                atomic64_inc(&fnic_stats->vlan_stats.resp_withno_vlanID);
 475                FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
 476                          "No VLAN descriptors in FIP VLAN response\n");
 477                spin_unlock_irqrestore(&fnic->vlans_lock, flags);
 478                goto out;
 479        }
 480
 481        vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
 482        fnic->set_vlan(fnic, vlan->vid);
 483        vlan->state = FIP_VLAN_SENT; /* sent now */
 484        vlan->sol_count++;
 485        spin_unlock_irqrestore(&fnic->vlans_lock, flags);
 486
 487        /* start the solicitation */
 488        fcoe_ctlr_link_up(fip);
 489
 490        sol_time = jiffies + msecs_to_jiffies(FCOE_CTLR_START_DELAY);
 491        mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
 492out:
 493        return;
 494}
 495
 496static void fnic_fcoe_start_fcf_disc(struct fnic *fnic)
 497{
 498        unsigned long flags;
 499        struct fcoe_vlan *vlan;
 500        u64 sol_time;
 501
 502        spin_lock_irqsave(&fnic->vlans_lock, flags);
 503        vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
 504        fnic->set_vlan(fnic, vlan->vid);
 505        vlan->state = FIP_VLAN_SENT; /* sent now */
 506        vlan->sol_count = 1;
 507        spin_unlock_irqrestore(&fnic->vlans_lock, flags);
 508
 509        /* start the solicitation */
 510        fcoe_ctlr_link_up(&fnic->ctlr);
 511
 512        sol_time = jiffies + msecs_to_jiffies(FCOE_CTLR_START_DELAY);
 513        mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
 514}
 515
 516static int fnic_fcoe_vlan_check(struct fnic *fnic, u16 flag)
 517{
 518        unsigned long flags;
 519        struct fcoe_vlan *fvlan;
 520
 521        spin_lock_irqsave(&fnic->vlans_lock, flags);
 522        if (list_empty(&fnic->vlans)) {
 523                spin_unlock_irqrestore(&fnic->vlans_lock, flags);
 524                return -EINVAL;
 525        }
 526
 527        fvlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
 528        if (fvlan->state == FIP_VLAN_USED) {
 529                spin_unlock_irqrestore(&fnic->vlans_lock, flags);
 530                return 0;
 531        }
 532
 533        if (fvlan->state == FIP_VLAN_SENT) {
 534                fvlan->state = FIP_VLAN_USED;
 535                spin_unlock_irqrestore(&fnic->vlans_lock, flags);
 536                return 0;
 537        }
 538        spin_unlock_irqrestore(&fnic->vlans_lock, flags);
 539        return -EINVAL;
 540}
 541
 542static void fnic_event_enq(struct fnic *fnic, enum fnic_evt ev)
 543{
 544        struct fnic_event *fevt;
 545        unsigned long flags;
 546
 547        fevt = kmalloc(sizeof(*fevt), GFP_ATOMIC);
 548        if (!fevt)
 549                return;
 550
 551        fevt->fnic = fnic;
 552        fevt->event = ev;
 553
 554        spin_lock_irqsave(&fnic->fnic_lock, flags);
 555        list_add_tail(&fevt->list, &fnic->evlist);
 556        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 557
 558        schedule_work(&fnic->event_work);
 559}
 560
 561static int fnic_fcoe_handle_fip_frame(struct fnic *fnic, struct sk_buff *skb)
 562{
 563        struct fip_header *fiph;
 564        int ret = 1;
 565        u16 op;
 566        u8 sub;
 567
 568        if (!skb || !(skb->data))
 569                return -1;
 570
 571        if (skb_linearize(skb))
 572                goto drop;
 573
 574        fiph = (struct fip_header *)skb->data;
 575        op = ntohs(fiph->fip_op);
 576        sub = fiph->fip_subcode;
 577
 578        if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
 579                goto drop;
 580
 581        if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
 582                goto drop;
 583
 584        if (op == FIP_OP_DISC && sub == FIP_SC_ADV) {
 585                if (fnic_fcoe_vlan_check(fnic, ntohs(fiph->fip_flags)))
 586                        goto drop;
 587                /* pass it on to fcoe */
 588                ret = 1;
 589        } else if (op == FIP_OP_VLAN && sub == FIP_SC_VL_NOTE) {
 590                /* set the vlan as used */
 591                fnic_fcoe_process_vlan_resp(fnic, skb);
 592                ret = 0;
 593        } else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK) {
 594                /* received CVL request, restart vlan disc */
 595                fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
 596                /* pass it on to fcoe */
 597                ret = 1;
 598        }
 599drop:
 600        return ret;
 601}
 602
 603void fnic_handle_fip_frame(struct work_struct *work)
 604{
 605        struct fnic *fnic = container_of(work, struct fnic, fip_frame_work);
 606        struct fnic_stats *fnic_stats = &fnic->fnic_stats;
 607        unsigned long flags;
 608        struct sk_buff *skb;
 609        struct ethhdr *eh;
 610
 611        while ((skb = skb_dequeue(&fnic->fip_frame_queue))) {
 612                spin_lock_irqsave(&fnic->fnic_lock, flags);
 613                if (fnic->stop_rx_link_events) {
 614                        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 615                        dev_kfree_skb(skb);
 616                        return;
 617                }
 618                /*
 619                 * If we're in a transitional state, just re-queue and return.
 620                 * The queue will be serviced when we get to a stable state.
 621                 */
 622                if (fnic->state != FNIC_IN_FC_MODE &&
 623                    fnic->state != FNIC_IN_ETH_MODE) {
 624                        skb_queue_head(&fnic->fip_frame_queue, skb);
 625                        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 626                        return;
 627                }
 628                spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 629                eh = (struct ethhdr *)skb->data;
 630                if (eh->h_proto == htons(ETH_P_FIP)) {
 631                        skb_pull(skb, sizeof(*eh));
 632                        if (fnic_fcoe_handle_fip_frame(fnic, skb) <= 0) {
 633                                dev_kfree_skb(skb);
 634                                continue;
 635                        }
 636                        /*
 637                         * If there's FLOGI rejects - clear all
 638                         * fcf's & restart from scratch
 639                         */
 640                        if (is_fnic_fip_flogi_reject(&fnic->ctlr, skb)) {
 641                                atomic64_inc(
 642                                        &fnic_stats->vlan_stats.flogi_rejects);
 643                                shost_printk(KERN_INFO, fnic->lport->host,
 644                                          "Trigger a Link down - VLAN Disc\n");
 645                                fcoe_ctlr_link_down(&fnic->ctlr);
 646                                /* start FCoE VLAN discovery */
 647                                fnic_fcoe_send_vlan_req(fnic);
 648                                dev_kfree_skb(skb);
 649                                continue;
 650                        }
 651                        fcoe_ctlr_recv(&fnic->ctlr, skb);
 652                        continue;
 653                }
 654        }
 655}
 656
 657/**
 658 * fnic_import_rq_eth_pkt() - handle received FCoE or FIP frame.
 659 * @fnic:       fnic instance.
 660 * @skb:        Ethernet Frame.
 661 */
 662static inline int fnic_import_rq_eth_pkt(struct fnic *fnic, struct sk_buff *skb)
 663{
 664        struct fc_frame *fp;
 665        struct ethhdr *eh;
 666        struct fcoe_hdr *fcoe_hdr;
 667        struct fcoe_crc_eof *ft;
 668
 669        /*
 670         * Undo VLAN encapsulation if present.
 671         */
 672        eh = (struct ethhdr *)skb->data;
 673        if (eh->h_proto == htons(ETH_P_8021Q)) {
 674                memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
 675                eh = skb_pull(skb, VLAN_HLEN);
 676                skb_reset_mac_header(skb);
 677        }
 678        if (eh->h_proto == htons(ETH_P_FIP)) {
 679                if (!(fnic->config.flags & VFCF_FIP_CAPABLE)) {
 680                        printk(KERN_ERR "Dropped FIP frame, as firmware "
 681                                        "uses non-FIP mode, Enable FIP "
 682                                        "using UCSM\n");
 683                        goto drop;
 684                }
 685                if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
 686                        FNIC_FC_RECV|0x80, (char *)skb->data, skb->len)) != 0) {
 687                        printk(KERN_ERR "fnic ctlr frame trace error!!!");
 688                }
 689                skb_queue_tail(&fnic->fip_frame_queue, skb);
 690                queue_work(fnic_fip_queue, &fnic->fip_frame_work);
 691                return 1;               /* let caller know packet was used */
 692        }
 693        if (eh->h_proto != htons(ETH_P_FCOE))
 694                goto drop;
 695        skb_set_network_header(skb, sizeof(*eh));
 696        skb_pull(skb, sizeof(*eh));
 697
 698        fcoe_hdr = (struct fcoe_hdr *)skb->data;
 699        if (FC_FCOE_DECAPS_VER(fcoe_hdr) != FC_FCOE_VER)
 700                goto drop;
 701
 702        fp = (struct fc_frame *)skb;
 703        fc_frame_init(fp);
 704        fr_sof(fp) = fcoe_hdr->fcoe_sof;
 705        skb_pull(skb, sizeof(struct fcoe_hdr));
 706        skb_reset_transport_header(skb);
 707
 708        ft = (struct fcoe_crc_eof *)(skb->data + skb->len - sizeof(*ft));
 709        fr_eof(fp) = ft->fcoe_eof;
 710        skb_trim(skb, skb->len - sizeof(*ft));
 711        return 0;
 712drop:
 713        dev_kfree_skb_irq(skb);
 714        return -1;
 715}
 716
 717/**
 718 * fnic_update_mac_locked() - set data MAC address and filters.
 719 * @fnic:       fnic instance.
 720 * @new:        newly-assigned FCoE MAC address.
 721 *
 722 * Called with the fnic lock held.
 723 */
 724void fnic_update_mac_locked(struct fnic *fnic, u8 *new)
 725{
 726        u8 *ctl = fnic->ctlr.ctl_src_addr;
 727        u8 *data = fnic->data_src_addr;
 728
 729        if (is_zero_ether_addr(new))
 730                new = ctl;
 731        if (ether_addr_equal(data, new))
 732                return;
 733        FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host, "update_mac %pM\n", new);
 734        if (!is_zero_ether_addr(data) && !ether_addr_equal(data, ctl))
 735                vnic_dev_del_addr(fnic->vdev, data);
 736        memcpy(data, new, ETH_ALEN);
 737        if (!ether_addr_equal(new, ctl))
 738                vnic_dev_add_addr(fnic->vdev, new);
 739}
 740
 741/**
 742 * fnic_update_mac() - set data MAC address and filters.
 743 * @lport:      local port.
 744 * @new:        newly-assigned FCoE MAC address.
 745 */
 746void fnic_update_mac(struct fc_lport *lport, u8 *new)
 747{
 748        struct fnic *fnic = lport_priv(lport);
 749
 750        spin_lock_irq(&fnic->fnic_lock);
 751        fnic_update_mac_locked(fnic, new);
 752        spin_unlock_irq(&fnic->fnic_lock);
 753}
 754
 755/**
 756 * fnic_set_port_id() - set the port_ID after successful FLOGI.
 757 * @lport:      local port.
 758 * @port_id:    assigned FC_ID.
 759 * @fp:         received frame containing the FLOGI accept or NULL.
 760 *
 761 * This is called from libfc when a new FC_ID has been assigned.
 762 * This causes us to reset the firmware to FC_MODE and setup the new MAC
 763 * address and FC_ID.
 764 *
 765 * It is also called with FC_ID 0 when we're logged off.
 766 *
 767 * If the FC_ID is due to point-to-point, fp may be NULL.
 768 */
 769void fnic_set_port_id(struct fc_lport *lport, u32 port_id, struct fc_frame *fp)
 770{
 771        struct fnic *fnic = lport_priv(lport);
 772        u8 *mac;
 773        int ret;
 774
 775        FNIC_FCS_DBG(KERN_DEBUG, lport->host, "set port_id %x fp %p\n",
 776                     port_id, fp);
 777
 778        /*
 779         * If we're clearing the FC_ID, change to use the ctl_src_addr.
 780         * Set ethernet mode to send FLOGI.
 781         */
 782        if (!port_id) {
 783                fnic_update_mac(lport, fnic->ctlr.ctl_src_addr);
 784                fnic_set_eth_mode(fnic);
 785                return;
 786        }
 787
 788        if (fp) {
 789                mac = fr_cb(fp)->granted_mac;
 790                if (is_zero_ether_addr(mac)) {
 791                        /* non-FIP - FLOGI already accepted - ignore return */
 792                        fcoe_ctlr_recv_flogi(&fnic->ctlr, lport, fp);
 793                }
 794                fnic_update_mac(lport, mac);
 795        }
 796
 797        /* Change state to reflect transition to FC mode */
 798        spin_lock_irq(&fnic->fnic_lock);
 799        if (fnic->state == FNIC_IN_ETH_MODE || fnic->state == FNIC_IN_FC_MODE)
 800                fnic->state = FNIC_IN_ETH_TRANS_FC_MODE;
 801        else {
 802                FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
 803                             "Unexpected fnic state %s while"
 804                             " processing flogi resp\n",
 805                             fnic_state_to_str(fnic->state));
 806                spin_unlock_irq(&fnic->fnic_lock);
 807                return;
 808        }
 809        spin_unlock_irq(&fnic->fnic_lock);
 810
 811        /*
 812         * Send FLOGI registration to firmware to set up FC mode.
 813         * The new address will be set up when registration completes.
 814         */
 815        ret = fnic_flogi_reg_handler(fnic, port_id);
 816
 817        if (ret < 0) {
 818                spin_lock_irq(&fnic->fnic_lock);
 819                if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE)
 820                        fnic->state = FNIC_IN_ETH_MODE;
 821                spin_unlock_irq(&fnic->fnic_lock);
 822        }
 823}
 824
 825static void fnic_rq_cmpl_frame_recv(struct vnic_rq *rq, struct cq_desc
 826                                    *cq_desc, struct vnic_rq_buf *buf,
 827                                    int skipped __attribute__((unused)),
 828                                    void *opaque)
 829{
 830        struct fnic *fnic = vnic_dev_priv(rq->vdev);
 831        struct sk_buff *skb;
 832        struct fc_frame *fp;
 833        struct fnic_stats *fnic_stats = &fnic->fnic_stats;
 834        unsigned int eth_hdrs_stripped;
 835        u8 type, color, eop, sop, ingress_port, vlan_stripped;
 836        u8 fcoe = 0, fcoe_sof, fcoe_eof;
 837        u8 fcoe_fc_crc_ok = 1, fcoe_enc_error = 0;
 838        u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
 839        u8 ipv6, ipv4, ipv4_fragment, rss_type, csum_not_calc;
 840        u8 fcs_ok = 1, packet_error = 0;
 841        u16 q_number, completed_index, bytes_written = 0, vlan, checksum;
 842        u32 rss_hash;
 843        u16 exchange_id, tmpl;
 844        u8 sof = 0;
 845        u8 eof = 0;
 846        u32 fcp_bytes_written = 0;
 847        unsigned long flags;
 848
 849        dma_unmap_single(&fnic->pdev->dev, buf->dma_addr, buf->len,
 850                         DMA_FROM_DEVICE);
 851        skb = buf->os_buf;
 852        fp = (struct fc_frame *)skb;
 853        buf->os_buf = NULL;
 854
 855        cq_desc_dec(cq_desc, &type, &color, &q_number, &completed_index);
 856        if (type == CQ_DESC_TYPE_RQ_FCP) {
 857                cq_fcp_rq_desc_dec((struct cq_fcp_rq_desc *)cq_desc,
 858                                   &type, &color, &q_number, &completed_index,
 859                                   &eop, &sop, &fcoe_fc_crc_ok, &exchange_id,
 860                                   &tmpl, &fcp_bytes_written, &sof, &eof,
 861                                   &ingress_port, &packet_error,
 862                                   &fcoe_enc_error, &fcs_ok, &vlan_stripped,
 863                                   &vlan);
 864                eth_hdrs_stripped = 1;
 865                skb_trim(skb, fcp_bytes_written);
 866                fr_sof(fp) = sof;
 867                fr_eof(fp) = eof;
 868
 869        } else if (type == CQ_DESC_TYPE_RQ_ENET) {
 870                cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
 871                                    &type, &color, &q_number, &completed_index,
 872                                    &ingress_port, &fcoe, &eop, &sop,
 873                                    &rss_type, &csum_not_calc, &rss_hash,
 874                                    &bytes_written, &packet_error,
 875                                    &vlan_stripped, &vlan, &checksum,
 876                                    &fcoe_sof, &fcoe_fc_crc_ok,
 877                                    &fcoe_enc_error, &fcoe_eof,
 878                                    &tcp_udp_csum_ok, &udp, &tcp,
 879                                    &ipv4_csum_ok, &ipv6, &ipv4,
 880                                    &ipv4_fragment, &fcs_ok);
 881                eth_hdrs_stripped = 0;
 882                skb_trim(skb, bytes_written);
 883                if (!fcs_ok) {
 884                        atomic64_inc(&fnic_stats->misc_stats.frame_errors);
 885                        FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
 886                                     "fcs error.  dropping packet.\n");
 887                        goto drop;
 888                }
 889                if (fnic_import_rq_eth_pkt(fnic, skb))
 890                        return;
 891
 892        } else {
 893                /* wrong CQ type*/
 894                shost_printk(KERN_ERR, fnic->lport->host,
 895                             "fnic rq_cmpl wrong cq type x%x\n", type);
 896                goto drop;
 897        }
 898
 899        if (!fcs_ok || packet_error || !fcoe_fc_crc_ok || fcoe_enc_error) {
 900                atomic64_inc(&fnic_stats->misc_stats.frame_errors);
 901                FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
 902                             "fnic rq_cmpl fcoe x%x fcsok x%x"
 903                             " pkterr x%x fcoe_fc_crc_ok x%x, fcoe_enc_err"
 904                             " x%x\n",
 905                             fcoe, fcs_ok, packet_error,
 906                             fcoe_fc_crc_ok, fcoe_enc_error);
 907                goto drop;
 908        }
 909
 910        spin_lock_irqsave(&fnic->fnic_lock, flags);
 911        if (fnic->stop_rx_link_events) {
 912                spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 913                goto drop;
 914        }
 915        fr_dev(fp) = fnic->lport;
 916        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
 917        if ((fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_RECV,
 918                                        (char *)skb->data, skb->len)) != 0) {
 919                printk(KERN_ERR "fnic ctlr frame trace error!!!");
 920        }
 921
 922        skb_queue_tail(&fnic->frame_queue, skb);
 923        queue_work(fnic_event_queue, &fnic->frame_work);
 924
 925        return;
 926drop:
 927        dev_kfree_skb_irq(skb);
 928}
 929
 930static int fnic_rq_cmpl_handler_cont(struct vnic_dev *vdev,
 931                                     struct cq_desc *cq_desc, u8 type,
 932                                     u16 q_number, u16 completed_index,
 933                                     void *opaque)
 934{
 935        struct fnic *fnic = vnic_dev_priv(vdev);
 936
 937        vnic_rq_service(&fnic->rq[q_number], cq_desc, completed_index,
 938                        VNIC_RQ_RETURN_DESC, fnic_rq_cmpl_frame_recv,
 939                        NULL);
 940        return 0;
 941}
 942
 943int fnic_rq_cmpl_handler(struct fnic *fnic, int rq_work_to_do)
 944{
 945        unsigned int tot_rq_work_done = 0, cur_work_done;
 946        unsigned int i;
 947        int err;
 948
 949        for (i = 0; i < fnic->rq_count; i++) {
 950                cur_work_done = vnic_cq_service(&fnic->cq[i], rq_work_to_do,
 951                                                fnic_rq_cmpl_handler_cont,
 952                                                NULL);
 953                if (cur_work_done) {
 954                        err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
 955                        if (err)
 956                                shost_printk(KERN_ERR, fnic->lport->host,
 957                                             "fnic_alloc_rq_frame can't alloc"
 958                                             " frame\n");
 959                }
 960                tot_rq_work_done += cur_work_done;
 961        }
 962
 963        return tot_rq_work_done;
 964}
 965
 966/*
 967 * This function is called once at init time to allocate and fill RQ
 968 * buffers. Subsequently, it is called in the interrupt context after RQ
 969 * buffer processing to replenish the buffers in the RQ
 970 */
 971int fnic_alloc_rq_frame(struct vnic_rq *rq)
 972{
 973        struct fnic *fnic = vnic_dev_priv(rq->vdev);
 974        struct sk_buff *skb;
 975        u16 len;
 976        dma_addr_t pa;
 977        int r;
 978
 979        len = FC_FRAME_HEADROOM + FC_MAX_FRAME + FC_FRAME_TAILROOM;
 980        skb = dev_alloc_skb(len);
 981        if (!skb) {
 982                FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
 983                             "Unable to allocate RQ sk_buff\n");
 984                return -ENOMEM;
 985        }
 986        skb_reset_mac_header(skb);
 987        skb_reset_transport_header(skb);
 988        skb_reset_network_header(skb);
 989        skb_put(skb, len);
 990        pa = dma_map_single(&fnic->pdev->dev, skb->data, len, DMA_FROM_DEVICE);
 991        if (dma_mapping_error(&fnic->pdev->dev, pa)) {
 992                r = -ENOMEM;
 993                printk(KERN_ERR "PCI mapping failed with error %d\n", r);
 994                goto free_skb;
 995        }
 996
 997        fnic_queue_rq_desc(rq, skb, pa, len);
 998        return 0;
 999
1000free_skb:
1001        kfree_skb(skb);
1002        return r;
1003}
1004
1005void fnic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
1006{
1007        struct fc_frame *fp = buf->os_buf;
1008        struct fnic *fnic = vnic_dev_priv(rq->vdev);
1009
1010        dma_unmap_single(&fnic->pdev->dev, buf->dma_addr, buf->len,
1011                         DMA_FROM_DEVICE);
1012
1013        dev_kfree_skb(fp_skb(fp));
1014        buf->os_buf = NULL;
1015}
1016
1017/**
1018 * fnic_eth_send() - Send Ethernet frame.
1019 * @fip:        fcoe_ctlr instance.
1020 * @skb:        Ethernet Frame, FIP, without VLAN encapsulation.
1021 */
1022void fnic_eth_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1023{
1024        struct fnic *fnic = fnic_from_ctlr(fip);
1025        struct vnic_wq *wq = &fnic->wq[0];
1026        dma_addr_t pa;
1027        struct ethhdr *eth_hdr;
1028        struct vlan_ethhdr *vlan_hdr;
1029        unsigned long flags;
1030
1031        if (!fnic->vlan_hw_insert) {
1032                eth_hdr = (struct ethhdr *)skb_mac_header(skb);
1033                vlan_hdr = skb_push(skb, sizeof(*vlan_hdr) - sizeof(*eth_hdr));
1034                memcpy(vlan_hdr, eth_hdr, 2 * ETH_ALEN);
1035                vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
1036                vlan_hdr->h_vlan_encapsulated_proto = eth_hdr->h_proto;
1037                vlan_hdr->h_vlan_TCI = htons(fnic->vlan_id);
1038                if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
1039                        FNIC_FC_SEND|0x80, (char *)eth_hdr, skb->len)) != 0) {
1040                        printk(KERN_ERR "fnic ctlr frame trace error!!!");
1041                }
1042        } else {
1043                if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
1044                        FNIC_FC_SEND|0x80, (char *)skb->data, skb->len)) != 0) {
1045                        printk(KERN_ERR "fnic ctlr frame trace error!!!");
1046                }
1047        }
1048
1049        pa = dma_map_single(&fnic->pdev->dev, skb->data, skb->len,
1050                        DMA_TO_DEVICE);
1051        if (dma_mapping_error(&fnic->pdev->dev, pa)) {
1052                printk(KERN_ERR "DMA mapping failed\n");
1053                goto free_skb;
1054        }
1055
1056        spin_lock_irqsave(&fnic->wq_lock[0], flags);
1057        if (!vnic_wq_desc_avail(wq))
1058                goto irq_restore;
1059
1060        fnic_queue_wq_eth_desc(wq, skb, pa, skb->len,
1061                               0 /* hw inserts cos value */,
1062                               fnic->vlan_id, 1);
1063        spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1064        return;
1065
1066irq_restore:
1067        spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1068        dma_unmap_single(&fnic->pdev->dev, pa, skb->len, DMA_TO_DEVICE);
1069free_skb:
1070        kfree_skb(skb);
1071}
1072
1073/*
1074 * Send FC frame.
1075 */
1076static int fnic_send_frame(struct fnic *fnic, struct fc_frame *fp)
1077{
1078        struct vnic_wq *wq = &fnic->wq[0];
1079        struct sk_buff *skb;
1080        dma_addr_t pa;
1081        struct ethhdr *eth_hdr;
1082        struct vlan_ethhdr *vlan_hdr;
1083        struct fcoe_hdr *fcoe_hdr;
1084        struct fc_frame_header *fh;
1085        u32 tot_len, eth_hdr_len;
1086        int ret = 0;
1087        unsigned long flags;
1088
1089        fh = fc_frame_header_get(fp);
1090        skb = fp_skb(fp);
1091
1092        if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1093            fcoe_ctlr_els_send(&fnic->ctlr, fnic->lport, skb))
1094                return 0;
1095
1096        if (!fnic->vlan_hw_insert) {
1097                eth_hdr_len = sizeof(*vlan_hdr) + sizeof(*fcoe_hdr);
1098                vlan_hdr = skb_push(skb, eth_hdr_len);
1099                eth_hdr = (struct ethhdr *)vlan_hdr;
1100                vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
1101                vlan_hdr->h_vlan_encapsulated_proto = htons(ETH_P_FCOE);
1102                vlan_hdr->h_vlan_TCI = htons(fnic->vlan_id);
1103                fcoe_hdr = (struct fcoe_hdr *)(vlan_hdr + 1);
1104        } else {
1105                eth_hdr_len = sizeof(*eth_hdr) + sizeof(*fcoe_hdr);
1106                eth_hdr = skb_push(skb, eth_hdr_len);
1107                eth_hdr->h_proto = htons(ETH_P_FCOE);
1108                fcoe_hdr = (struct fcoe_hdr *)(eth_hdr + 1);
1109        }
1110
1111        if (fnic->ctlr.map_dest)
1112                fc_fcoe_set_mac(eth_hdr->h_dest, fh->fh_d_id);
1113        else
1114                memcpy(eth_hdr->h_dest, fnic->ctlr.dest_addr, ETH_ALEN);
1115        memcpy(eth_hdr->h_source, fnic->data_src_addr, ETH_ALEN);
1116
1117        tot_len = skb->len;
1118        BUG_ON(tot_len % 4);
1119
1120        memset(fcoe_hdr, 0, sizeof(*fcoe_hdr));
1121        fcoe_hdr->fcoe_sof = fr_sof(fp);
1122        if (FC_FCOE_VER)
1123                FC_FCOE_ENCAPS_VER(fcoe_hdr, FC_FCOE_VER);
1124
1125        pa = dma_map_single(&fnic->pdev->dev, eth_hdr, tot_len, DMA_TO_DEVICE);
1126        if (dma_mapping_error(&fnic->pdev->dev, pa)) {
1127                ret = -ENOMEM;
1128                printk(KERN_ERR "DMA map failed with error %d\n", ret);
1129                goto free_skb_on_err;
1130        }
1131
1132        if ((fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_SEND,
1133                                (char *)eth_hdr, tot_len)) != 0) {
1134                printk(KERN_ERR "fnic ctlr frame trace error!!!");
1135        }
1136
1137        spin_lock_irqsave(&fnic->wq_lock[0], flags);
1138
1139        if (!vnic_wq_desc_avail(wq)) {
1140                dma_unmap_single(&fnic->pdev->dev, pa, tot_len, DMA_TO_DEVICE);
1141                ret = -1;
1142                goto irq_restore;
1143        }
1144
1145        fnic_queue_wq_desc(wq, skb, pa, tot_len, fr_eof(fp),
1146                           0 /* hw inserts cos value */,
1147                           fnic->vlan_id, 1, 1, 1);
1148
1149irq_restore:
1150        spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1151
1152free_skb_on_err:
1153        if (ret)
1154                dev_kfree_skb_any(fp_skb(fp));
1155
1156        return ret;
1157}
1158
1159/*
1160 * fnic_send
1161 * Routine to send a raw frame
1162 */
1163int fnic_send(struct fc_lport *lp, struct fc_frame *fp)
1164{
1165        struct fnic *fnic = lport_priv(lp);
1166        unsigned long flags;
1167
1168        if (fnic->in_remove) {
1169                dev_kfree_skb(fp_skb(fp));
1170                return -1;
1171        }
1172
1173        /*
1174         * Queue frame if in a transitional state.
1175         * This occurs while registering the Port_ID / MAC address after FLOGI.
1176         */
1177        spin_lock_irqsave(&fnic->fnic_lock, flags);
1178        if (fnic->state != FNIC_IN_FC_MODE && fnic->state != FNIC_IN_ETH_MODE) {
1179                skb_queue_tail(&fnic->tx_queue, fp_skb(fp));
1180                spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1181                return 0;
1182        }
1183        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1184
1185        return fnic_send_frame(fnic, fp);
1186}
1187
1188/**
1189 * fnic_flush_tx() - send queued frames.
1190 * @fnic: fnic device
1191 *
1192 * Send frames that were waiting to go out in FC or Ethernet mode.
1193 * Whenever changing modes we purge queued frames, so these frames should
1194 * be queued for the stable mode that we're in, either FC or Ethernet.
1195 *
1196 * Called without fnic_lock held.
1197 */
1198void fnic_flush_tx(struct fnic *fnic)
1199{
1200        struct sk_buff *skb;
1201        struct fc_frame *fp;
1202
1203        while ((skb = skb_dequeue(&fnic->tx_queue))) {
1204                fp = (struct fc_frame *)skb;
1205                fnic_send_frame(fnic, fp);
1206        }
1207}
1208
1209/**
1210 * fnic_set_eth_mode() - put fnic into ethernet mode.
1211 * @fnic: fnic device
1212 *
1213 * Called without fnic lock held.
1214 */
1215static void fnic_set_eth_mode(struct fnic *fnic)
1216{
1217        unsigned long flags;
1218        enum fnic_state old_state;
1219        int ret;
1220
1221        spin_lock_irqsave(&fnic->fnic_lock, flags);
1222again:
1223        old_state = fnic->state;
1224        switch (old_state) {
1225        case FNIC_IN_FC_MODE:
1226        case FNIC_IN_ETH_TRANS_FC_MODE:
1227        default:
1228                fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1229                spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1230
1231                ret = fnic_fw_reset_handler(fnic);
1232
1233                spin_lock_irqsave(&fnic->fnic_lock, flags);
1234                if (fnic->state != FNIC_IN_FC_TRANS_ETH_MODE)
1235                        goto again;
1236                if (ret)
1237                        fnic->state = old_state;
1238                break;
1239
1240        case FNIC_IN_FC_TRANS_ETH_MODE:
1241        case FNIC_IN_ETH_MODE:
1242                break;
1243        }
1244        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1245}
1246
1247static void fnic_wq_complete_frame_send(struct vnic_wq *wq,
1248                                        struct cq_desc *cq_desc,
1249                                        struct vnic_wq_buf *buf, void *opaque)
1250{
1251        struct sk_buff *skb = buf->os_buf;
1252        struct fc_frame *fp = (struct fc_frame *)skb;
1253        struct fnic *fnic = vnic_dev_priv(wq->vdev);
1254
1255        dma_unmap_single(&fnic->pdev->dev, buf->dma_addr, buf->len,
1256                         DMA_TO_DEVICE);
1257        dev_kfree_skb_irq(fp_skb(fp));
1258        buf->os_buf = NULL;
1259}
1260
1261static int fnic_wq_cmpl_handler_cont(struct vnic_dev *vdev,
1262                                     struct cq_desc *cq_desc, u8 type,
1263                                     u16 q_number, u16 completed_index,
1264                                     void *opaque)
1265{
1266        struct fnic *fnic = vnic_dev_priv(vdev);
1267        unsigned long flags;
1268
1269        spin_lock_irqsave(&fnic->wq_lock[q_number], flags);
1270        vnic_wq_service(&fnic->wq[q_number], cq_desc, completed_index,
1271                        fnic_wq_complete_frame_send, NULL);
1272        spin_unlock_irqrestore(&fnic->wq_lock[q_number], flags);
1273
1274        return 0;
1275}
1276
1277int fnic_wq_cmpl_handler(struct fnic *fnic, int work_to_do)
1278{
1279        unsigned int wq_work_done = 0;
1280        unsigned int i;
1281
1282        for (i = 0; i < fnic->raw_wq_count; i++) {
1283                wq_work_done  += vnic_cq_service(&fnic->cq[fnic->rq_count+i],
1284                                                 work_to_do,
1285                                                 fnic_wq_cmpl_handler_cont,
1286                                                 NULL);
1287        }
1288
1289        return wq_work_done;
1290}
1291
1292
1293void fnic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
1294{
1295        struct fc_frame *fp = buf->os_buf;
1296        struct fnic *fnic = vnic_dev_priv(wq->vdev);
1297
1298        dma_unmap_single(&fnic->pdev->dev, buf->dma_addr, buf->len,
1299                         DMA_TO_DEVICE);
1300
1301        dev_kfree_skb(fp_skb(fp));
1302        buf->os_buf = NULL;
1303}
1304
1305void fnic_fcoe_reset_vlans(struct fnic *fnic)
1306{
1307        unsigned long flags;
1308        struct fcoe_vlan *vlan;
1309        struct fcoe_vlan *next;
1310
1311        /*
1312         * indicate a link down to fcoe so that all fcf's are free'd
1313         * might not be required since we did this before sending vlan
1314         * discovery request
1315         */
1316        spin_lock_irqsave(&fnic->vlans_lock, flags);
1317        if (!list_empty(&fnic->vlans)) {
1318                list_for_each_entry_safe(vlan, next, &fnic->vlans, list) {
1319                        list_del(&vlan->list);
1320                        kfree(vlan);
1321                }
1322        }
1323        spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1324}
1325
1326void fnic_handle_fip_timer(struct fnic *fnic)
1327{
1328        unsigned long flags;
1329        struct fcoe_vlan *vlan;
1330        struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1331        u64 sol_time;
1332
1333        spin_lock_irqsave(&fnic->fnic_lock, flags);
1334        if (fnic->stop_rx_link_events) {
1335                spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1336                return;
1337        }
1338        spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1339
1340        if (fnic->ctlr.mode == FIP_MODE_NON_FIP)
1341                return;
1342
1343        spin_lock_irqsave(&fnic->vlans_lock, flags);
1344        if (list_empty(&fnic->vlans)) {
1345                spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1346                /* no vlans available, try again */
1347                if (printk_ratelimit())
1348                        FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1349                                  "Start VLAN Discovery\n");
1350                fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1351                return;
1352        }
1353
1354        vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
1355        shost_printk(KERN_DEBUG, fnic->lport->host,
1356                  "fip_timer: vlan %d state %d sol_count %d\n",
1357                  vlan->vid, vlan->state, vlan->sol_count);
1358        switch (vlan->state) {
1359        case FIP_VLAN_USED:
1360                FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1361                          "FIP VLAN is selected for FC transaction\n");
1362                spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1363                break;
1364        case FIP_VLAN_FAILED:
1365                spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1366                /* if all vlans are in failed state, restart vlan disc */
1367                if (printk_ratelimit())
1368                        FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1369                                  "Start VLAN Discovery\n");
1370                fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1371                break;
1372        case FIP_VLAN_SENT:
1373                if (vlan->sol_count >= FCOE_CTLR_MAX_SOL) {
1374                        /*
1375                         * no response on this vlan, remove  from the list.
1376                         * Try the next vlan
1377                         */
1378                        shost_printk(KERN_INFO, fnic->lport->host,
1379                                  "Dequeue this VLAN ID %d from list\n",
1380                                  vlan->vid);
1381                        list_del(&vlan->list);
1382                        kfree(vlan);
1383                        vlan = NULL;
1384                        if (list_empty(&fnic->vlans)) {
1385                                /* we exhausted all vlans, restart vlan disc */
1386                                spin_unlock_irqrestore(&fnic->vlans_lock,
1387                                                        flags);
1388                                shost_printk(KERN_INFO, fnic->lport->host,
1389                                          "fip_timer: vlan list empty, "
1390                                          "trigger vlan disc\n");
1391                                fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1392                                return;
1393                        }
1394                        /* check the next vlan */
1395                        vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan,
1396                                                        list);
1397                        fnic->set_vlan(fnic, vlan->vid);
1398                        vlan->state = FIP_VLAN_SENT; /* sent now */
1399                }
1400                spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1401                atomic64_inc(&fnic_stats->vlan_stats.sol_expiry_count);
1402                vlan->sol_count++;
1403                sol_time = jiffies + msecs_to_jiffies
1404                                        (FCOE_CTLR_START_DELAY);
1405                mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
1406                break;
1407        }
1408}
1409