linux/drivers/scsi/bfa/bfad_im.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
   3 * Copyright (c) 2014- QLogic Corporation.
   4 * All rights reserved
   5 * www.qlogic.com
   6 *
   7 * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
   8 *
   9 * This program is free software; you can redistribute it and/or modify it
  10 * under the terms of the GNU General Public License (GPL) Version 2 as
  11 * published by the Free Software Foundation
  12 *
  13 * This program is distributed in the hope that it will be useful, but
  14 * WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16 * General Public License for more details.
  17 */
  18
  19/*
  20 *  bfad_im.c Linux driver IM module.
  21 */
  22
  23#include <linux/export.h>
  24
  25#include "bfad_drv.h"
  26#include "bfad_im.h"
  27#include "bfa_fcs.h"
  28
  29BFA_TRC_FILE(LDRV, IM);
  30
  31DEFINE_IDR(bfad_im_port_index);
  32struct scsi_transport_template *bfad_im_scsi_transport_template;
  33struct scsi_transport_template *bfad_im_scsi_vport_transport_template;
  34static void bfad_im_itnim_work_handler(struct work_struct *work);
  35static int bfad_im_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmnd);
  36static int bfad_im_slave_alloc(struct scsi_device *sdev);
  37static void bfad_im_fc_rport_add(struct bfad_im_port_s  *im_port,
  38                                struct bfad_itnim_s *itnim);
  39
  40void
  41bfa_cb_ioim_done(void *drv, struct bfad_ioim_s *dio,
  42                        enum bfi_ioim_status io_status, u8 scsi_status,
  43                        int sns_len, u8 *sns_info, s32 residue)
  44{
  45        struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
  46        struct bfad_s         *bfad = drv;
  47        struct bfad_itnim_data_s *itnim_data;
  48        struct bfad_itnim_s *itnim;
  49        u8         host_status = DID_OK;
  50
  51        switch (io_status) {
  52        case BFI_IOIM_STS_OK:
  53                bfa_trc(bfad, scsi_status);
  54                scsi_set_resid(cmnd, 0);
  55
  56                if (sns_len > 0) {
  57                        bfa_trc(bfad, sns_len);
  58                        if (sns_len > SCSI_SENSE_BUFFERSIZE)
  59                                sns_len = SCSI_SENSE_BUFFERSIZE;
  60                        memcpy(cmnd->sense_buffer, sns_info, sns_len);
  61                }
  62
  63                if (residue > 0) {
  64                        bfa_trc(bfad, residue);
  65                        scsi_set_resid(cmnd, residue);
  66                        if (!sns_len && (scsi_status == SAM_STAT_GOOD) &&
  67                                (scsi_bufflen(cmnd) - residue) <
  68                                        cmnd->underflow) {
  69                                bfa_trc(bfad, 0);
  70                                host_status = DID_ERROR;
  71                        }
  72                }
  73                cmnd->result = ScsiResult(host_status, scsi_status);
  74
  75                break;
  76
  77        case BFI_IOIM_STS_ABORTED:
  78        case BFI_IOIM_STS_TIMEDOUT:
  79        case BFI_IOIM_STS_PATHTOV:
  80        default:
  81                host_status = DID_ERROR;
  82                cmnd->result = ScsiResult(host_status, 0);
  83        }
  84
  85        /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
  86        if (cmnd->device->host != NULL)
  87                scsi_dma_unmap(cmnd);
  88
  89        cmnd->host_scribble = NULL;
  90        bfa_trc(bfad, cmnd->result);
  91
  92        itnim_data = cmnd->device->hostdata;
  93        if (itnim_data) {
  94                itnim = itnim_data->itnim;
  95                if (!cmnd->result && itnim &&
  96                         (bfa_lun_queue_depth > cmnd->device->queue_depth)) {
  97                        /* Queue depth adjustment for good status completion */
  98                        bfad_ramp_up_qdepth(itnim, cmnd->device);
  99                } else if (cmnd->result == SAM_STAT_TASK_SET_FULL && itnim) {
 100                        /* qfull handling */
 101                        bfad_handle_qfull(itnim, cmnd->device);
 102                }
 103        }
 104
 105        cmnd->scsi_done(cmnd);
 106}
 107
 108void
 109bfa_cb_ioim_good_comp(void *drv, struct bfad_ioim_s *dio)
 110{
 111        struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
 112        struct bfad_itnim_data_s *itnim_data;
 113        struct bfad_itnim_s *itnim;
 114
 115        cmnd->result = ScsiResult(DID_OK, SCSI_STATUS_GOOD);
 116
 117        /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
 118        if (cmnd->device->host != NULL)
 119                scsi_dma_unmap(cmnd);
 120
 121        cmnd->host_scribble = NULL;
 122
 123        /* Queue depth adjustment */
 124        if (bfa_lun_queue_depth > cmnd->device->queue_depth) {
 125                itnim_data = cmnd->device->hostdata;
 126                if (itnim_data) {
 127                        itnim = itnim_data->itnim;
 128                        if (itnim)
 129                                bfad_ramp_up_qdepth(itnim, cmnd->device);
 130                }
 131        }
 132
 133        cmnd->scsi_done(cmnd);
 134}
 135
 136void
 137bfa_cb_ioim_abort(void *drv, struct bfad_ioim_s *dio)
 138{
 139        struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dio;
 140        struct bfad_s         *bfad = drv;
 141
 142        cmnd->result = ScsiResult(DID_ERROR, 0);
 143
 144        /* Unmap DMA, if host is NULL, it means a scsi passthru cmd */
 145        if (cmnd->device->host != NULL)
 146                scsi_dma_unmap(cmnd);
 147
 148        bfa_trc(bfad, cmnd->result);
 149        cmnd->host_scribble = NULL;
 150}
 151
 152void
 153bfa_cb_tskim_done(void *bfad, struct bfad_tskim_s *dtsk,
 154                   enum bfi_tskim_status tsk_status)
 155{
 156        struct scsi_cmnd *cmnd = (struct scsi_cmnd *)dtsk;
 157        wait_queue_head_t *wq;
 158
 159        cmnd->SCp.Status |= tsk_status << 1;
 160        set_bit(IO_DONE_BIT, (unsigned long *)&cmnd->SCp.Status);
 161        wq = (wait_queue_head_t *) cmnd->SCp.ptr;
 162        cmnd->SCp.ptr = NULL;
 163
 164        if (wq)
 165                wake_up(wq);
 166}
 167
 168/*
 169 *  Scsi_Host_template SCSI host template
 170 */
 171/*
 172 * Scsi_Host template entry, returns BFAD PCI info.
 173 */
 174static const char *
 175bfad_im_info(struct Scsi_Host *shost)
 176{
 177        static char     bfa_buf[256];
 178        struct bfad_im_port_s *im_port =
 179                        (struct bfad_im_port_s *) shost->hostdata[0];
 180        struct bfad_s *bfad = im_port->bfad;
 181
 182        memset(bfa_buf, 0, sizeof(bfa_buf));
 183        snprintf(bfa_buf, sizeof(bfa_buf),
 184                "QLogic BR-series FC/FCOE Adapter, hwpath: %s driver: %s",
 185                bfad->pci_name, BFAD_DRIVER_VERSION);
 186
 187        return bfa_buf;
 188}
 189
 190/*
 191 * Scsi_Host template entry, aborts the specified SCSI command.
 192 *
 193 * Returns: SUCCESS or FAILED.
 194 */
 195static int
 196bfad_im_abort_handler(struct scsi_cmnd *cmnd)
 197{
 198        struct Scsi_Host *shost = cmnd->device->host;
 199        struct bfad_im_port_s *im_port =
 200                        (struct bfad_im_port_s *) shost->hostdata[0];
 201        struct bfad_s         *bfad = im_port->bfad;
 202        struct bfa_ioim_s *hal_io;
 203        unsigned long   flags;
 204        u32        timeout;
 205        int             rc = FAILED;
 206
 207        spin_lock_irqsave(&bfad->bfad_lock, flags);
 208        hal_io = (struct bfa_ioim_s *) cmnd->host_scribble;
 209        if (!hal_io) {
 210                /* IO has been completed, return success */
 211                rc = SUCCESS;
 212                goto out;
 213        }
 214        if (hal_io->dio != (struct bfad_ioim_s *) cmnd) {
 215                rc = FAILED;
 216                goto out;
 217        }
 218
 219        bfa_trc(bfad, hal_io->iotag);
 220        BFA_LOG(KERN_INFO, bfad, bfa_log_level,
 221                "scsi%d: abort cmnd %p iotag %x\n",
 222                im_port->shost->host_no, cmnd, hal_io->iotag);
 223        (void) bfa_ioim_abort(hal_io);
 224        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 225
 226        /* Need to wait until the command get aborted */
 227        timeout = 10;
 228        while ((struct bfa_ioim_s *) cmnd->host_scribble == hal_io) {
 229                set_current_state(TASK_UNINTERRUPTIBLE);
 230                schedule_timeout(timeout);
 231                if (timeout < 4 * HZ)
 232                        timeout *= 2;
 233        }
 234
 235        cmnd->scsi_done(cmnd);
 236        bfa_trc(bfad, hal_io->iotag);
 237        BFA_LOG(KERN_INFO, bfad, bfa_log_level,
 238                "scsi%d: complete abort 0x%p iotag 0x%x\n",
 239                im_port->shost->host_no, cmnd, hal_io->iotag);
 240        return SUCCESS;
 241out:
 242        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 243        return rc;
 244}
 245
 246static bfa_status_t
 247bfad_im_target_reset_send(struct bfad_s *bfad, struct scsi_cmnd *cmnd,
 248                     struct bfad_itnim_s *itnim)
 249{
 250        struct bfa_tskim_s *tskim;
 251        struct bfa_itnim_s *bfa_itnim;
 252        bfa_status_t    rc = BFA_STATUS_OK;
 253        struct scsi_lun scsilun;
 254
 255        tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
 256        if (!tskim) {
 257                BFA_LOG(KERN_ERR, bfad, bfa_log_level,
 258                        "target reset, fail to allocate tskim\n");
 259                rc = BFA_STATUS_FAILED;
 260                goto out;
 261        }
 262
 263        /*
 264         * Set host_scribble to NULL to avoid aborting a task command if
 265         * happens.
 266         */
 267        cmnd->host_scribble = NULL;
 268        cmnd->SCp.Status = 0;
 269        bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
 270        memset(&scsilun, 0, sizeof(scsilun));
 271        bfa_tskim_start(tskim, bfa_itnim, scsilun,
 272                            FCP_TM_TARGET_RESET, BFAD_TARGET_RESET_TMO);
 273out:
 274        return rc;
 275}
 276
 277/*
 278 * Scsi_Host template entry, resets a LUN and abort its all commands.
 279 *
 280 * Returns: SUCCESS or FAILED.
 281 *
 282 */
 283static int
 284bfad_im_reset_lun_handler(struct scsi_cmnd *cmnd)
 285{
 286        struct Scsi_Host *shost = cmnd->device->host;
 287        struct bfad_im_port_s *im_port =
 288                        (struct bfad_im_port_s *) shost->hostdata[0];
 289        struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
 290        struct bfad_s         *bfad = im_port->bfad;
 291        struct bfa_tskim_s *tskim;
 292        struct bfad_itnim_s   *itnim;
 293        struct bfa_itnim_s *bfa_itnim;
 294        DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
 295        int             rc = SUCCESS;
 296        unsigned long   flags;
 297        enum bfi_tskim_status task_status;
 298        struct scsi_lun scsilun;
 299
 300        spin_lock_irqsave(&bfad->bfad_lock, flags);
 301        itnim = itnim_data->itnim;
 302        if (!itnim) {
 303                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 304                rc = FAILED;
 305                goto out;
 306        }
 307
 308        tskim = bfa_tskim_alloc(&bfad->bfa, (struct bfad_tskim_s *) cmnd);
 309        if (!tskim) {
 310                BFA_LOG(KERN_ERR, bfad, bfa_log_level,
 311                                "LUN reset, fail to allocate tskim");
 312                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 313                rc = FAILED;
 314                goto out;
 315        }
 316
 317        /*
 318         * Set host_scribble to NULL to avoid aborting a task command
 319         * if happens.
 320         */
 321        cmnd->host_scribble = NULL;
 322        cmnd->SCp.ptr = (char *)&wq;
 323        cmnd->SCp.Status = 0;
 324        bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim->fcs_itnim);
 325        int_to_scsilun(cmnd->device->lun, &scsilun);
 326        bfa_tskim_start(tskim, bfa_itnim, scsilun,
 327                            FCP_TM_LUN_RESET, BFAD_LUN_RESET_TMO);
 328        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 329
 330        wait_event(wq, test_bit(IO_DONE_BIT,
 331                        (unsigned long *)&cmnd->SCp.Status));
 332
 333        task_status = cmnd->SCp.Status >> 1;
 334        if (task_status != BFI_TSKIM_STS_OK) {
 335                BFA_LOG(KERN_ERR, bfad, bfa_log_level,
 336                        "LUN reset failure, status: %d\n", task_status);
 337                rc = FAILED;
 338        }
 339
 340out:
 341        return rc;
 342}
 343
 344/*
 345 * Scsi_Host template entry, resets the bus and abort all commands.
 346 */
 347static int
 348bfad_im_reset_bus_handler(struct scsi_cmnd *cmnd)
 349{
 350        struct Scsi_Host *shost = cmnd->device->host;
 351        struct bfad_im_port_s *im_port =
 352                                (struct bfad_im_port_s *) shost->hostdata[0];
 353        struct bfad_s         *bfad = im_port->bfad;
 354        struct bfad_itnim_s   *itnim;
 355        unsigned long   flags;
 356        u32        i, rc, err_cnt = 0;
 357        DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
 358        enum bfi_tskim_status task_status;
 359
 360        spin_lock_irqsave(&bfad->bfad_lock, flags);
 361        for (i = 0; i < MAX_FCP_TARGET; i++) {
 362                itnim = bfad_get_itnim(im_port, i);
 363                if (itnim) {
 364                        cmnd->SCp.ptr = (char *)&wq;
 365                        rc = bfad_im_target_reset_send(bfad, cmnd, itnim);
 366                        if (rc != BFA_STATUS_OK) {
 367                                err_cnt++;
 368                                continue;
 369                        }
 370
 371                        /* wait target reset to complete */
 372                        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 373                        wait_event(wq, test_bit(IO_DONE_BIT,
 374                                        (unsigned long *)&cmnd->SCp.Status));
 375                        spin_lock_irqsave(&bfad->bfad_lock, flags);
 376
 377                        task_status = cmnd->SCp.Status >> 1;
 378                        if (task_status != BFI_TSKIM_STS_OK) {
 379                                BFA_LOG(KERN_ERR, bfad, bfa_log_level,
 380                                        "target reset failure,"
 381                                        " status: %d\n", task_status);
 382                                err_cnt++;
 383                        }
 384                }
 385        }
 386        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 387
 388        if (err_cnt)
 389                return FAILED;
 390
 391        return SUCCESS;
 392}
 393
 394/*
 395 * Scsi_Host template entry slave_destroy.
 396 */
 397static void
 398bfad_im_slave_destroy(struct scsi_device *sdev)
 399{
 400        sdev->hostdata = NULL;
 401        return;
 402}
 403
 404/*
 405 *  BFA FCS itnim callbacks
 406 */
 407
 408/*
 409 * BFA FCS itnim alloc callback, after successful PRLI
 410 * Context: Interrupt
 411 */
 412void
 413bfa_fcb_itnim_alloc(struct bfad_s *bfad, struct bfa_fcs_itnim_s **itnim,
 414                    struct bfad_itnim_s **itnim_drv)
 415{
 416        *itnim_drv = kzalloc(sizeof(struct bfad_itnim_s), GFP_ATOMIC);
 417        if (*itnim_drv == NULL)
 418                return;
 419
 420        (*itnim_drv)->im = bfad->im;
 421        *itnim = &(*itnim_drv)->fcs_itnim;
 422        (*itnim_drv)->state = ITNIM_STATE_NONE;
 423
 424        /*
 425         * Initiaze the itnim_work
 426         */
 427        INIT_WORK(&(*itnim_drv)->itnim_work, bfad_im_itnim_work_handler);
 428        bfad->bfad_flags |= BFAD_RPORT_ONLINE;
 429}
 430
 431/*
 432 * BFA FCS itnim free callback.
 433 * Context: Interrupt. bfad_lock is held
 434 */
 435void
 436bfa_fcb_itnim_free(struct bfad_s *bfad, struct bfad_itnim_s *itnim_drv)
 437{
 438        struct bfad_port_s    *port;
 439        wwn_t wwpn;
 440        u32 fcid;
 441        char wwpn_str[32], fcid_str[16];
 442        struct bfad_im_s        *im = itnim_drv->im;
 443
 444        /* online to free state transtion should not happen */
 445        WARN_ON(itnim_drv->state == ITNIM_STATE_ONLINE);
 446
 447        itnim_drv->queue_work = 1;
 448        /* offline request is not yet done, use the same request to free */
 449        if (itnim_drv->state == ITNIM_STATE_OFFLINE_PENDING)
 450                itnim_drv->queue_work = 0;
 451
 452        itnim_drv->state = ITNIM_STATE_FREE;
 453        port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
 454        itnim_drv->im_port = port->im_port;
 455        wwpn = bfa_fcs_itnim_get_pwwn(&itnim_drv->fcs_itnim);
 456        fcid = bfa_fcs_itnim_get_fcid(&itnim_drv->fcs_itnim);
 457        wwn2str(wwpn_str, wwpn);
 458        fcid2str(fcid_str, fcid);
 459        BFA_LOG(KERN_INFO, bfad, bfa_log_level,
 460                "ITNIM FREE scsi%d: FCID: %s WWPN: %s\n",
 461                port->im_port->shost->host_no,
 462                fcid_str, wwpn_str);
 463
 464        /* ITNIM processing */
 465        if (itnim_drv->queue_work)
 466                queue_work(im->drv_workq, &itnim_drv->itnim_work);
 467}
 468
 469/*
 470 * BFA FCS itnim online callback.
 471 * Context: Interrupt. bfad_lock is held
 472 */
 473void
 474bfa_fcb_itnim_online(struct bfad_itnim_s *itnim_drv)
 475{
 476        struct bfad_port_s    *port;
 477        struct bfad_im_s        *im = itnim_drv->im;
 478
 479        itnim_drv->bfa_itnim = bfa_fcs_itnim_get_halitn(&itnim_drv->fcs_itnim);
 480        port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
 481        itnim_drv->state = ITNIM_STATE_ONLINE;
 482        itnim_drv->queue_work = 1;
 483        itnim_drv->im_port = port->im_port;
 484
 485        /* ITNIM processing */
 486        if (itnim_drv->queue_work)
 487                queue_work(im->drv_workq, &itnim_drv->itnim_work);
 488}
 489
 490/*
 491 * BFA FCS itnim offline callback.
 492 * Context: Interrupt. bfad_lock is held
 493 */
 494void
 495bfa_fcb_itnim_offline(struct bfad_itnim_s *itnim_drv)
 496{
 497        struct bfad_port_s    *port;
 498        struct bfad_s *bfad;
 499        struct bfad_im_s        *im = itnim_drv->im;
 500
 501        port = bfa_fcs_itnim_get_drvport(&itnim_drv->fcs_itnim);
 502        bfad = port->bfad;
 503        if ((bfad->pport.flags & BFAD_PORT_DELETE) ||
 504                 (port->flags & BFAD_PORT_DELETE)) {
 505                itnim_drv->state = ITNIM_STATE_OFFLINE;
 506                return;
 507        }
 508        itnim_drv->im_port = port->im_port;
 509        itnim_drv->state = ITNIM_STATE_OFFLINE_PENDING;
 510        itnim_drv->queue_work = 1;
 511
 512        /* ITNIM processing */
 513        if (itnim_drv->queue_work)
 514                queue_work(im->drv_workq, &itnim_drv->itnim_work);
 515}
 516
 517/*
 518 * Allocate a Scsi_Host for a port.
 519 */
 520int
 521bfad_im_scsi_host_alloc(struct bfad_s *bfad, struct bfad_im_port_s *im_port,
 522                        struct device *dev)
 523{
 524        int error = 1;
 525
 526        mutex_lock(&bfad_mutex);
 527        error = idr_alloc(&bfad_im_port_index, im_port, 0, 0, GFP_KERNEL);
 528        if (error < 0) {
 529                mutex_unlock(&bfad_mutex);
 530                printk(KERN_WARNING "idr_alloc failure\n");
 531                goto out;
 532        }
 533        im_port->idr_id = error;
 534        mutex_unlock(&bfad_mutex);
 535
 536        im_port->shost = bfad_scsi_host_alloc(im_port, bfad);
 537        if (!im_port->shost) {
 538                error = 1;
 539                goto out_free_idr;
 540        }
 541
 542        im_port->shost->hostdata[0] = (unsigned long)im_port;
 543        im_port->shost->unique_id = im_port->idr_id;
 544        im_port->shost->this_id = -1;
 545        im_port->shost->max_id = MAX_FCP_TARGET;
 546        im_port->shost->max_lun = MAX_FCP_LUN;
 547        im_port->shost->max_cmd_len = 16;
 548        im_port->shost->can_queue = bfad->cfg_data.ioc_queue_depth;
 549        if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
 550                im_port->shost->transportt = bfad_im_scsi_transport_template;
 551        else
 552                im_port->shost->transportt =
 553                                bfad_im_scsi_vport_transport_template;
 554
 555        error = scsi_add_host_with_dma(im_port->shost, dev, &bfad->pcidev->dev);
 556        if (error) {
 557                printk(KERN_WARNING "scsi_add_host failure %d\n", error);
 558                goto out_fc_rel;
 559        }
 560
 561        return 0;
 562
 563out_fc_rel:
 564        scsi_host_put(im_port->shost);
 565        im_port->shost = NULL;
 566out_free_idr:
 567        mutex_lock(&bfad_mutex);
 568        idr_remove(&bfad_im_port_index, im_port->idr_id);
 569        mutex_unlock(&bfad_mutex);
 570out:
 571        return error;
 572}
 573
 574void
 575bfad_im_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
 576{
 577        bfa_trc(bfad, bfad->inst_no);
 578        BFA_LOG(KERN_INFO, bfad, bfa_log_level, "Free scsi%d\n",
 579                        im_port->shost->host_no);
 580
 581        fc_remove_host(im_port->shost);
 582
 583        scsi_remove_host(im_port->shost);
 584        scsi_host_put(im_port->shost);
 585
 586        mutex_lock(&bfad_mutex);
 587        idr_remove(&bfad_im_port_index, im_port->idr_id);
 588        mutex_unlock(&bfad_mutex);
 589}
 590
 591static void
 592bfad_im_port_delete_handler(struct work_struct *work)
 593{
 594        struct bfad_im_port_s *im_port =
 595                container_of(work, struct bfad_im_port_s, port_delete_work);
 596
 597        if (im_port->port->pvb_type != BFAD_PORT_PHYS_BASE) {
 598                im_port->flags |= BFAD_PORT_DELETE;
 599                fc_vport_terminate(im_port->fc_vport);
 600        }
 601}
 602
 603bfa_status_t
 604bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port)
 605{
 606        int             rc = BFA_STATUS_OK;
 607        struct bfad_im_port_s *im_port;
 608
 609        im_port = kzalloc(sizeof(struct bfad_im_port_s), GFP_ATOMIC);
 610        if (im_port == NULL) {
 611                rc = BFA_STATUS_ENOMEM;
 612                goto ext;
 613        }
 614        port->im_port = im_port;
 615        im_port->port = port;
 616        im_port->bfad = bfad;
 617
 618        INIT_WORK(&im_port->port_delete_work, bfad_im_port_delete_handler);
 619        INIT_LIST_HEAD(&im_port->itnim_mapped_list);
 620        INIT_LIST_HEAD(&im_port->binding_list);
 621
 622ext:
 623        return rc;
 624}
 625
 626void
 627bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port)
 628{
 629        struct bfad_im_port_s *im_port = port->im_port;
 630
 631        queue_work(bfad->im->drv_workq,
 632                                &im_port->port_delete_work);
 633}
 634
 635void
 636bfad_im_port_clean(struct bfad_im_port_s *im_port)
 637{
 638        struct bfad_fcp_binding *bp, *bp_new;
 639        unsigned long flags;
 640        struct bfad_s *bfad =  im_port->bfad;
 641
 642        spin_lock_irqsave(&bfad->bfad_lock, flags);
 643        list_for_each_entry_safe(bp, bp_new, &im_port->binding_list,
 644                                        list_entry) {
 645                list_del(&bp->list_entry);
 646                kfree(bp);
 647        }
 648
 649        /* the itnim_mapped_list must be empty at this time */
 650        WARN_ON(!list_empty(&im_port->itnim_mapped_list));
 651
 652        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
 653}
 654
 655static void bfad_aen_im_notify_handler(struct work_struct *work)
 656{
 657        struct bfad_im_s *im =
 658                container_of(work, struct bfad_im_s, aen_im_notify_work);
 659        struct bfa_aen_entry_s *aen_entry;
 660        struct bfad_s *bfad = im->bfad;
 661        struct Scsi_Host *shost = bfad->pport.im_port->shost;
 662        void *event_data;
 663        unsigned long flags;
 664
 665        while (!list_empty(&bfad->active_aen_q)) {
 666                spin_lock_irqsave(&bfad->bfad_aen_spinlock, flags);
 667                bfa_q_deq(&bfad->active_aen_q, &aen_entry);
 668                spin_unlock_irqrestore(&bfad->bfad_aen_spinlock, flags);
 669                event_data = (char *)aen_entry + sizeof(struct list_head);
 670                fc_host_post_vendor_event(shost, fc_get_event_number(),
 671                                sizeof(struct bfa_aen_entry_s) -
 672                                sizeof(struct list_head),
 673                                (char *)event_data, BFAD_NL_VENDOR_ID);
 674                spin_lock_irqsave(&bfad->bfad_aen_spinlock, flags);
 675                list_add_tail(&aen_entry->qe, &bfad->free_aen_q);
 676                spin_unlock_irqrestore(&bfad->bfad_aen_spinlock, flags);
 677        }
 678}
 679
 680bfa_status_t
 681bfad_im_probe(struct bfad_s *bfad)
 682{
 683        struct bfad_im_s      *im;
 684
 685        im = kzalloc(sizeof(struct bfad_im_s), GFP_KERNEL);
 686        if (im == NULL)
 687                return BFA_STATUS_ENOMEM;
 688
 689        bfad->im = im;
 690        im->bfad = bfad;
 691
 692        if (bfad_thread_workq(bfad) != BFA_STATUS_OK) {
 693                kfree(im);
 694                return BFA_STATUS_FAILED;
 695        }
 696
 697        INIT_WORK(&im->aen_im_notify_work, bfad_aen_im_notify_handler);
 698        return BFA_STATUS_OK;
 699}
 700
 701void
 702bfad_im_probe_undo(struct bfad_s *bfad)
 703{
 704        if (bfad->im) {
 705                bfad_destroy_workq(bfad->im);
 706                kfree(bfad->im);
 707                bfad->im = NULL;
 708        }
 709}
 710
 711struct Scsi_Host *
 712bfad_scsi_host_alloc(struct bfad_im_port_s *im_port, struct bfad_s *bfad)
 713{
 714        struct scsi_host_template *sht;
 715
 716        if (im_port->port->pvb_type == BFAD_PORT_PHYS_BASE)
 717                sht = &bfad_im_scsi_host_template;
 718        else
 719                sht = &bfad_im_vport_template;
 720
 721        if (max_xfer_size != BFAD_MAX_SECTORS >> 1)
 722                sht->max_sectors = max_xfer_size << 1;
 723
 724        sht->sg_tablesize = bfad->cfg_data.io_max_sge;
 725
 726        return scsi_host_alloc(sht, sizeof(unsigned long));
 727}
 728
 729void
 730bfad_scsi_host_free(struct bfad_s *bfad, struct bfad_im_port_s *im_port)
 731{
 732        if (!(im_port->flags & BFAD_PORT_DELETE))
 733                flush_workqueue(bfad->im->drv_workq);
 734        bfad_im_scsi_host_free(im_port->bfad, im_port);
 735        bfad_im_port_clean(im_port);
 736        kfree(im_port);
 737}
 738
 739void
 740bfad_destroy_workq(struct bfad_im_s *im)
 741{
 742        if (im && im->drv_workq) {
 743                flush_workqueue(im->drv_workq);
 744                destroy_workqueue(im->drv_workq);
 745                im->drv_workq = NULL;
 746        }
 747}
 748
 749bfa_status_t
 750bfad_thread_workq(struct bfad_s *bfad)
 751{
 752        struct bfad_im_s      *im = bfad->im;
 753
 754        bfa_trc(bfad, 0);
 755        snprintf(im->drv_workq_name, KOBJ_NAME_LEN, "bfad_wq_%d",
 756                 bfad->inst_no);
 757        im->drv_workq = create_singlethread_workqueue(im->drv_workq_name);
 758        if (!im->drv_workq)
 759                return BFA_STATUS_FAILED;
 760
 761        return BFA_STATUS_OK;
 762}
 763
 764/*
 765 * Scsi_Host template entry.
 766 *
 767 * Description:
 768 * OS entry point to adjust the queue_depths on a per-device basis.
 769 * Called once per device during the bus scan.
 770 * Return non-zero if fails.
 771 */
 772static int
 773bfad_im_slave_configure(struct scsi_device *sdev)
 774{
 775        if (sdev->tagged_supported)
 776                scsi_activate_tcq(sdev, bfa_lun_queue_depth);
 777        else
 778                scsi_deactivate_tcq(sdev, bfa_lun_queue_depth);
 779
 780        return 0;
 781}
 782
 783struct scsi_host_template bfad_im_scsi_host_template = {
 784        .module = THIS_MODULE,
 785        .name = BFAD_DRIVER_NAME,
 786        .info = bfad_im_info,
 787        .queuecommand = bfad_im_queuecommand,
 788        .eh_abort_handler = bfad_im_abort_handler,
 789        .eh_device_reset_handler = bfad_im_reset_lun_handler,
 790        .eh_bus_reset_handler = bfad_im_reset_bus_handler,
 791
 792        .slave_alloc = bfad_im_slave_alloc,
 793        .slave_configure = bfad_im_slave_configure,
 794        .slave_destroy = bfad_im_slave_destroy,
 795
 796        .this_id = -1,
 797        .sg_tablesize = BFAD_IO_MAX_SGE,
 798        .cmd_per_lun = 3,
 799        .use_clustering = ENABLE_CLUSTERING,
 800        .shost_attrs = bfad_im_host_attrs,
 801        .max_sectors = BFAD_MAX_SECTORS,
 802        .vendor_id = BFA_PCI_VENDOR_ID_BROCADE,
 803};
 804
 805struct scsi_host_template bfad_im_vport_template = {
 806        .module = THIS_MODULE,
 807        .name = BFAD_DRIVER_NAME,
 808        .info = bfad_im_info,
 809        .queuecommand = bfad_im_queuecommand,
 810        .eh_abort_handler = bfad_im_abort_handler,
 811        .eh_device_reset_handler = bfad_im_reset_lun_handler,
 812        .eh_bus_reset_handler = bfad_im_reset_bus_handler,
 813
 814        .slave_alloc = bfad_im_slave_alloc,
 815        .slave_configure = bfad_im_slave_configure,
 816        .slave_destroy = bfad_im_slave_destroy,
 817
 818        .this_id = -1,
 819        .sg_tablesize = BFAD_IO_MAX_SGE,
 820        .cmd_per_lun = 3,
 821        .use_clustering = ENABLE_CLUSTERING,
 822        .shost_attrs = bfad_im_vport_attrs,
 823        .max_sectors = BFAD_MAX_SECTORS,
 824};
 825
 826bfa_status_t
 827bfad_im_module_init(void)
 828{
 829        bfad_im_scsi_transport_template =
 830                fc_attach_transport(&bfad_im_fc_function_template);
 831        if (!bfad_im_scsi_transport_template)
 832                return BFA_STATUS_ENOMEM;
 833
 834        bfad_im_scsi_vport_transport_template =
 835                fc_attach_transport(&bfad_im_vport_fc_function_template);
 836        if (!bfad_im_scsi_vport_transport_template) {
 837                fc_release_transport(bfad_im_scsi_transport_template);
 838                return BFA_STATUS_ENOMEM;
 839        }
 840
 841        return BFA_STATUS_OK;
 842}
 843
 844void
 845bfad_im_module_exit(void)
 846{
 847        if (bfad_im_scsi_transport_template)
 848                fc_release_transport(bfad_im_scsi_transport_template);
 849
 850        if (bfad_im_scsi_vport_transport_template)
 851                fc_release_transport(bfad_im_scsi_vport_transport_template);
 852}
 853
 854void
 855bfad_ramp_up_qdepth(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
 856{
 857        struct scsi_device *tmp_sdev;
 858
 859        if (((jiffies - itnim->last_ramp_up_time) >
 860                BFA_QUEUE_FULL_RAMP_UP_TIME * HZ) &&
 861                ((jiffies - itnim->last_queue_full_time) >
 862                BFA_QUEUE_FULL_RAMP_UP_TIME * HZ)) {
 863                shost_for_each_device(tmp_sdev, sdev->host) {
 864                        if (bfa_lun_queue_depth > tmp_sdev->queue_depth) {
 865                                if (tmp_sdev->id != sdev->id)
 866                                        continue;
 867                                if (tmp_sdev->ordered_tags)
 868                                        scsi_adjust_queue_depth(tmp_sdev,
 869                                                MSG_ORDERED_TAG,
 870                                                tmp_sdev->queue_depth + 1);
 871                                else
 872                                        scsi_adjust_queue_depth(tmp_sdev,
 873                                                MSG_SIMPLE_TAG,
 874                                                tmp_sdev->queue_depth + 1);
 875
 876                                itnim->last_ramp_up_time = jiffies;
 877                        }
 878                }
 879        }
 880}
 881
 882void
 883bfad_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev)
 884{
 885        struct scsi_device *tmp_sdev;
 886
 887        itnim->last_queue_full_time = jiffies;
 888
 889        shost_for_each_device(tmp_sdev, sdev->host) {
 890                if (tmp_sdev->id != sdev->id)
 891                        continue;
 892                scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
 893        }
 894}
 895
 896struct bfad_itnim_s *
 897bfad_get_itnim(struct bfad_im_port_s *im_port, int id)
 898{
 899        struct bfad_itnim_s   *itnim = NULL;
 900
 901        /* Search the mapped list for this target ID */
 902        list_for_each_entry(itnim, &im_port->itnim_mapped_list, list_entry) {
 903                if (id == itnim->scsi_tgt_id)
 904                        return itnim;
 905        }
 906
 907        return NULL;
 908}
 909
 910/*
 911 * Function is invoked from the SCSI Host Template slave_alloc() entry point.
 912 * Has the logic to query the LUN Mask database to check if this LUN needs to
 913 * be made visible to the SCSI mid-layer or not.
 914 *
 915 * Returns BFA_STATUS_OK if this LUN needs to be added to the OS stack.
 916 * Returns -ENXIO to notify SCSI mid-layer to not add this LUN to the OS stack.
 917 */
 918static int
 919bfad_im_check_if_make_lun_visible(struct scsi_device *sdev,
 920                                  struct fc_rport *rport)
 921{
 922        struct bfad_itnim_data_s *itnim_data =
 923                                (struct bfad_itnim_data_s *) rport->dd_data;
 924        struct bfa_s *bfa = itnim_data->itnim->bfa_itnim->bfa;
 925        struct bfa_rport_s *bfa_rport = itnim_data->itnim->bfa_itnim->rport;
 926        struct bfa_lun_mask_s *lun_list = bfa_get_lun_mask_list(bfa);
 927        int i = 0, ret = -ENXIO;
 928
 929        for (i = 0; i < MAX_LUN_MASK_CFG; i++) {
 930                if (lun_list[i].state == BFA_IOIM_LUN_MASK_ACTIVE &&
 931                    scsilun_to_int(&lun_list[i].lun) == sdev->lun &&
 932                    lun_list[i].rp_tag == bfa_rport->rport_tag &&
 933                    lun_list[i].lp_tag == (u8)bfa_rport->rport_info.lp_tag) {
 934                        ret = BFA_STATUS_OK;
 935                        break;
 936                }
 937        }
 938        return ret;
 939}
 940
 941/*
 942 * Scsi_Host template entry slave_alloc
 943 */
 944static int
 945bfad_im_slave_alloc(struct scsi_device *sdev)
 946{
 947        struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
 948        struct bfad_itnim_data_s *itnim_data;
 949        struct bfa_s *bfa;
 950
 951        if (!rport || fc_remote_port_chkready(rport))
 952                return -ENXIO;
 953
 954        itnim_data = (struct bfad_itnim_data_s *) rport->dd_data;
 955        bfa = itnim_data->itnim->bfa_itnim->bfa;
 956
 957        if (bfa_get_lun_mask_status(bfa) == BFA_LUNMASK_ENABLED) {
 958                /*
 959                 * We should not mask LUN 0 - since this will translate
 960                 * to no LUN / TARGET for SCSI ml resulting no scan.
 961                 */
 962                if (sdev->lun == 0) {
 963                        sdev->sdev_bflags |= BLIST_NOREPORTLUN |
 964                                             BLIST_SPARSELUN;
 965                        goto done;
 966                }
 967
 968                /*
 969                 * Query LUN Mask configuration - to expose this LUN
 970                 * to the SCSI mid-layer or to mask it.
 971                 */
 972                if (bfad_im_check_if_make_lun_visible(sdev, rport) !=
 973                                                        BFA_STATUS_OK)
 974                        return -ENXIO;
 975        }
 976done:
 977        sdev->hostdata = rport->dd_data;
 978
 979        return 0;
 980}
 981
 982u32
 983bfad_im_supported_speeds(struct bfa_s *bfa)
 984{
 985        struct bfa_ioc_attr_s *ioc_attr;
 986        u32 supported_speed = 0;
 987
 988        ioc_attr = kzalloc(sizeof(struct bfa_ioc_attr_s), GFP_KERNEL);
 989        if (!ioc_attr)
 990                return 0;
 991
 992        bfa_ioc_get_attr(&bfa->ioc, ioc_attr);
 993        if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_16GBPS)
 994                supported_speed |=  FC_PORTSPEED_16GBIT | FC_PORTSPEED_8GBIT |
 995                                FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT;
 996        else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_8GBPS) {
 997                if (ioc_attr->adapter_attr.is_mezz) {
 998                        supported_speed |= FC_PORTSPEED_8GBIT |
 999                                FC_PORTSPEED_4GBIT |
1000                                FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
1001                } else {
1002                        supported_speed |= FC_PORTSPEED_8GBIT |
1003                                FC_PORTSPEED_4GBIT |
1004                                FC_PORTSPEED_2GBIT;
1005                }
1006        } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_4GBPS) {
1007                supported_speed |=  FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
1008                                FC_PORTSPEED_1GBIT;
1009        } else if (ioc_attr->adapter_attr.max_speed == BFA_PORT_SPEED_10GBPS) {
1010                supported_speed |= FC_PORTSPEED_10GBIT;
1011        }
1012        kfree(ioc_attr);
1013        return supported_speed;
1014}
1015
1016void
1017bfad_fc_host_init(struct bfad_im_port_s *im_port)
1018{
1019        struct Scsi_Host *host = im_port->shost;
1020        struct bfad_s         *bfad = im_port->bfad;
1021        struct bfad_port_s    *port = im_port->port;
1022        char symname[BFA_SYMNAME_MAXLEN];
1023        struct bfa_fcport_s *fcport = BFA_FCPORT_MOD(&bfad->bfa);
1024
1025        fc_host_node_name(host) =
1026                cpu_to_be64((bfa_fcs_lport_get_nwwn(port->fcs_port)));
1027        fc_host_port_name(host) =
1028                cpu_to_be64((bfa_fcs_lport_get_pwwn(port->fcs_port)));
1029        fc_host_max_npiv_vports(host) = bfa_lps_get_max_vport(&bfad->bfa);
1030
1031        fc_host_supported_classes(host) = FC_COS_CLASS3;
1032
1033        memset(fc_host_supported_fc4s(host), 0,
1034               sizeof(fc_host_supported_fc4s(host)));
1035        if (supported_fc4s & BFA_LPORT_ROLE_FCP_IM)
1036                /* For FCP type 0x08 */
1037                fc_host_supported_fc4s(host)[2] = 1;
1038        /* For fibre channel services type 0x20 */
1039        fc_host_supported_fc4s(host)[7] = 1;
1040
1041        strlcpy(symname, bfad->bfa_fcs.fabric.bport.port_cfg.sym_name.symname,
1042                BFA_SYMNAME_MAXLEN);
1043        sprintf(fc_host_symbolic_name(host), "%s", symname);
1044
1045        fc_host_supported_speeds(host) = bfad_im_supported_speeds(&bfad->bfa);
1046        fc_host_maxframe_size(host) = fcport->cfg.maxfrsize;
1047}
1048
1049static void
1050bfad_im_fc_rport_add(struct bfad_im_port_s *im_port, struct bfad_itnim_s *itnim)
1051{
1052        struct fc_rport_identifiers rport_ids;
1053        struct fc_rport *fc_rport;
1054        struct bfad_itnim_data_s *itnim_data;
1055
1056        rport_ids.node_name =
1057                cpu_to_be64(bfa_fcs_itnim_get_nwwn(&itnim->fcs_itnim));
1058        rport_ids.port_name =
1059                cpu_to_be64(bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
1060        rport_ids.port_id =
1061                bfa_hton3b(bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim));
1062        rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
1063
1064        itnim->fc_rport = fc_rport =
1065                fc_remote_port_add(im_port->shost, 0, &rport_ids);
1066
1067        if (!fc_rport)
1068                return;
1069
1070        fc_rport->maxframe_size =
1071                bfa_fcs_itnim_get_maxfrsize(&itnim->fcs_itnim);
1072        fc_rport->supported_classes = bfa_fcs_itnim_get_cos(&itnim->fcs_itnim);
1073
1074        itnim_data = fc_rport->dd_data;
1075        itnim_data->itnim = itnim;
1076
1077        rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1078
1079        if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1080                fc_remote_port_rolechg(fc_rport, rport_ids.roles);
1081
1082        if ((fc_rport->scsi_target_id != -1)
1083            && (fc_rport->scsi_target_id < MAX_FCP_TARGET))
1084                itnim->scsi_tgt_id = fc_rport->scsi_target_id;
1085
1086        itnim->channel = fc_rport->channel;
1087
1088        return;
1089}
1090
1091/*
1092 * Work queue handler using FC transport service
1093* Context: kernel
1094 */
1095static void
1096bfad_im_itnim_work_handler(struct work_struct *work)
1097{
1098        struct bfad_itnim_s   *itnim = container_of(work, struct bfad_itnim_s,
1099                                                        itnim_work);
1100        struct bfad_im_s      *im = itnim->im;
1101        struct bfad_s         *bfad = im->bfad;
1102        struct bfad_im_port_s *im_port;
1103        unsigned long   flags;
1104        struct fc_rport *fc_rport;
1105        wwn_t wwpn;
1106        u32 fcid;
1107        char wwpn_str[32], fcid_str[16];
1108
1109        spin_lock_irqsave(&bfad->bfad_lock, flags);
1110        im_port = itnim->im_port;
1111        bfa_trc(bfad, itnim->state);
1112        switch (itnim->state) {
1113        case ITNIM_STATE_ONLINE:
1114                if (!itnim->fc_rport) {
1115                        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1116                        bfad_im_fc_rport_add(im_port, itnim);
1117                        spin_lock_irqsave(&bfad->bfad_lock, flags);
1118                        wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1119                        fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1120                        wwn2str(wwpn_str, wwpn);
1121                        fcid2str(fcid_str, fcid);
1122                        list_add_tail(&itnim->list_entry,
1123                                &im_port->itnim_mapped_list);
1124                        BFA_LOG(KERN_INFO, bfad, bfa_log_level,
1125                                "ITNIM ONLINE Target: %d:0:%d "
1126                                "FCID: %s WWPN: %s\n",
1127                                im_port->shost->host_no,
1128                                itnim->scsi_tgt_id,
1129                                fcid_str, wwpn_str);
1130                } else {
1131                        printk(KERN_WARNING
1132                                "%s: itnim %llx is already in online state\n",
1133                                __func__,
1134                                bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim));
1135                }
1136
1137                break;
1138        case ITNIM_STATE_OFFLINE_PENDING:
1139                itnim->state = ITNIM_STATE_OFFLINE;
1140                if (itnim->fc_rport) {
1141                        fc_rport = itnim->fc_rport;
1142                        ((struct bfad_itnim_data_s *)
1143                                fc_rport->dd_data)->itnim = NULL;
1144                        itnim->fc_rport = NULL;
1145                        if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1146                                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1147                                fc_rport->dev_loss_tmo =
1148                                        bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1149                                fc_remote_port_delete(fc_rport);
1150                                spin_lock_irqsave(&bfad->bfad_lock, flags);
1151                        }
1152                        wwpn = bfa_fcs_itnim_get_pwwn(&itnim->fcs_itnim);
1153                        fcid = bfa_fcs_itnim_get_fcid(&itnim->fcs_itnim);
1154                        wwn2str(wwpn_str, wwpn);
1155                        fcid2str(fcid_str, fcid);
1156                        list_del(&itnim->list_entry);
1157                        BFA_LOG(KERN_INFO, bfad, bfa_log_level,
1158                                "ITNIM OFFLINE Target: %d:0:%d "
1159                                "FCID: %s WWPN: %s\n",
1160                                im_port->shost->host_no,
1161                                itnim->scsi_tgt_id,
1162                                fcid_str, wwpn_str);
1163                }
1164                break;
1165        case ITNIM_STATE_FREE:
1166                if (itnim->fc_rport) {
1167                        fc_rport = itnim->fc_rport;
1168                        ((struct bfad_itnim_data_s *)
1169                                fc_rport->dd_data)->itnim = NULL;
1170                        itnim->fc_rport = NULL;
1171                        if (!(im_port->port->flags & BFAD_PORT_DELETE)) {
1172                                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1173                                fc_rport->dev_loss_tmo =
1174                                        bfa_fcpim_path_tov_get(&bfad->bfa) + 1;
1175                                fc_remote_port_delete(fc_rport);
1176                                spin_lock_irqsave(&bfad->bfad_lock, flags);
1177                        }
1178                        list_del(&itnim->list_entry);
1179                }
1180
1181                kfree(itnim);
1182                break;
1183        default:
1184                WARN_ON(1);
1185                break;
1186        }
1187
1188        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1189}
1190
1191/*
1192 * Scsi_Host template entry, queue a SCSI command to the BFAD.
1193 */
1194static int
1195bfad_im_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1196{
1197        struct bfad_im_port_s *im_port =
1198                (struct bfad_im_port_s *) cmnd->device->host->hostdata[0];
1199        struct bfad_s         *bfad = im_port->bfad;
1200        struct bfad_itnim_data_s *itnim_data = cmnd->device->hostdata;
1201        struct bfad_itnim_s   *itnim;
1202        struct bfa_ioim_s *hal_io;
1203        unsigned long   flags;
1204        int             rc;
1205        int       sg_cnt = 0;
1206        struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1207
1208        rc = fc_remote_port_chkready(rport);
1209        if (rc) {
1210                cmnd->result = rc;
1211                done(cmnd);
1212                return 0;
1213        }
1214
1215        if (bfad->bfad_flags & BFAD_EEH_BUSY) {
1216                if (bfad->bfad_flags & BFAD_EEH_PCI_CHANNEL_IO_PERM_FAILURE)
1217                        cmnd->result = DID_NO_CONNECT << 16;
1218                else
1219                        cmnd->result = DID_REQUEUE << 16;
1220                done(cmnd);
1221                return 0;
1222        }
1223
1224        sg_cnt = scsi_dma_map(cmnd);
1225        if (sg_cnt < 0)
1226                return SCSI_MLQUEUE_HOST_BUSY;
1227
1228        cmnd->scsi_done = done;
1229
1230        spin_lock_irqsave(&bfad->bfad_lock, flags);
1231        if (!(bfad->bfad_flags & BFAD_HAL_START_DONE)) {
1232                printk(KERN_WARNING
1233                        "bfad%d, queuecommand %p %x failed, BFA stopped\n",
1234                       bfad->inst_no, cmnd, cmnd->cmnd[0]);
1235                cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
1236                goto out_fail_cmd;
1237        }
1238
1239
1240        itnim = itnim_data->itnim;
1241        if (!itnim) {
1242                cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
1243                goto out_fail_cmd;
1244        }
1245
1246        hal_io = bfa_ioim_alloc(&bfad->bfa, (struct bfad_ioim_s *) cmnd,
1247                                    itnim->bfa_itnim, sg_cnt);
1248        if (!hal_io) {
1249                printk(KERN_WARNING "hal_io failure\n");
1250                spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1251                scsi_dma_unmap(cmnd);
1252                return SCSI_MLQUEUE_HOST_BUSY;
1253        }
1254
1255        cmnd->host_scribble = (char *)hal_io;
1256        bfa_ioim_start(hal_io);
1257        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1258
1259        return 0;
1260
1261out_fail_cmd:
1262        spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1263        scsi_dma_unmap(cmnd);
1264        if (done)
1265                done(cmnd);
1266
1267        return 0;
1268}
1269
1270static DEF_SCSI_QCMD(bfad_im_queuecommand)
1271
1272void
1273bfad_rport_online_wait(struct bfad_s *bfad)
1274{
1275        int i;
1276        int rport_delay = 10;
1277
1278        for (i = 0; !(bfad->bfad_flags & BFAD_PORT_ONLINE)
1279                && i < bfa_linkup_delay; i++) {
1280                set_current_state(TASK_UNINTERRUPTIBLE);
1281                schedule_timeout(HZ);
1282        }
1283
1284        if (bfad->bfad_flags & BFAD_PORT_ONLINE) {
1285                rport_delay = rport_delay < bfa_linkup_delay ?
1286                        rport_delay : bfa_linkup_delay;
1287                for (i = 0; !(bfad->bfad_flags & BFAD_RPORT_ONLINE)
1288                        && i < rport_delay; i++) {
1289                        set_current_state(TASK_UNINTERRUPTIBLE);
1290                        schedule_timeout(HZ);
1291                }
1292
1293                if (rport_delay > 0 && (bfad->bfad_flags & BFAD_RPORT_ONLINE)) {
1294                        set_current_state(TASK_UNINTERRUPTIBLE);
1295                        schedule_timeout(rport_delay * HZ);
1296                }
1297        }
1298}
1299
1300int
1301bfad_get_linkup_delay(struct bfad_s *bfad)
1302{
1303        u8              nwwns = 0;
1304        wwn_t           wwns[BFA_PREBOOT_BOOTLUN_MAX];
1305        int             linkup_delay;
1306
1307        /*
1308         * Querying for the boot target port wwns
1309         * -- read from boot information in flash.
1310         * If nwwns > 0 => boot over SAN and set linkup_delay = 30
1311         * else => local boot machine set linkup_delay = 0
1312         */
1313
1314        bfa_iocfc_get_bootwwns(&bfad->bfa, &nwwns, wwns);
1315
1316        if (nwwns > 0)
1317                /* If Boot over SAN set linkup_delay = 30sec */
1318                linkup_delay = 30;
1319        else
1320                /* If local boot; no linkup_delay */
1321                linkup_delay = 0;
1322
1323        return linkup_delay;
1324}
1325