linux/drivers/scsi/lpfc/lpfc_sli.c
<<
>>
Prefs
   1/*******************************************************************
   2 * This file is part of the Emulex Linux Device Driver for         *
   3 * Fibre Channel Host Bus Adapters.                                *
   4 * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
   5 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries.  *
   6 * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
   7 * EMULEX and SLI are trademarks of Emulex.                        *
   8 * www.broadcom.com                                                *
   9 * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
  10 *                                                                 *
  11 * This program is free software; you can redistribute it and/or   *
  12 * modify it under the terms of version 2 of the GNU General       *
  13 * Public License as published by the Free Software Foundation.    *
  14 * This program is distributed in the hope that it will be useful. *
  15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
  16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
  17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
  18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  19 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
  20 * more details, a copy of which can be found in the file COPYING  *
  21 * included with this package.                                     *
  22 *******************************************************************/
  23
  24#include <linux/blkdev.h>
  25#include <linux/pci.h>
  26#include <linux/interrupt.h>
  27#include <linux/delay.h>
  28#include <linux/slab.h>
  29#include <linux/lockdep.h>
  30
  31#include <scsi/scsi.h>
  32#include <scsi/scsi_cmnd.h>
  33#include <scsi/scsi_device.h>
  34#include <scsi/scsi_host.h>
  35#include <scsi/scsi_transport_fc.h>
  36#include <scsi/fc/fc_fs.h>
  37#include <linux/aer.h>
  38#ifdef CONFIG_X86
  39#include <asm/set_memory.h>
  40#endif
  41
  42#include <linux/nvme-fc-driver.h>
  43
  44#include "lpfc_hw4.h"
  45#include "lpfc_hw.h"
  46#include "lpfc_sli.h"
  47#include "lpfc_sli4.h"
  48#include "lpfc_nl.h"
  49#include "lpfc_disc.h"
  50#include "lpfc.h"
  51#include "lpfc_scsi.h"
  52#include "lpfc_nvme.h"
  53#include "lpfc_nvmet.h"
  54#include "lpfc_crtn.h"
  55#include "lpfc_logmsg.h"
  56#include "lpfc_compat.h"
  57#include "lpfc_debugfs.h"
  58#include "lpfc_vport.h"
  59#include "lpfc_version.h"
  60
  61/* There are only four IOCB completion types. */
  62typedef enum _lpfc_iocb_type {
  63        LPFC_UNKNOWN_IOCB,
  64        LPFC_UNSOL_IOCB,
  65        LPFC_SOL_IOCB,
  66        LPFC_ABORT_IOCB
  67} lpfc_iocb_type;
  68
  69
  70/* Provide function prototypes local to this module. */
  71static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  72                                  uint32_t);
  73static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  74                              uint8_t *, uint32_t *);
  75static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  76                                                         struct lpfc_iocbq *);
  77static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  78                                      struct hbq_dmabuf *);
  79static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
  80                                          struct hbq_dmabuf *dmabuf);
  81static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
  82                                    struct lpfc_cqe *);
  83static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
  84                                       int);
  85static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
  86                                     struct lpfc_eqe *eqe, uint32_t qidx);
  87static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
  88static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
  89static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
  90                                   struct lpfc_sli_ring *pring,
  91                                   struct lpfc_iocbq *cmdiocb);
  92
  93static IOCB_t *
  94lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  95{
  96        return &iocbq->iocb;
  97}
  98
  99/**
 100 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
 101 * @q: The Work Queue to operate on.
 102 * @wqe: The work Queue Entry to put on the Work queue.
 103 *
 104 * This routine will copy the contents of @wqe to the next available entry on
 105 * the @q. This function will then ring the Work Queue Doorbell to signal the
 106 * HBA to start processing the Work Queue Entry. This function returns 0 if
 107 * successful. If no entries are available on @q then this function will return
 108 * -ENOMEM.
 109 * The caller is expected to hold the hbalock when calling this routine.
 110 **/
 111static int
 112lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
 113{
 114        union lpfc_wqe *temp_wqe;
 115        struct lpfc_register doorbell;
 116        uint32_t host_index;
 117        uint32_t idx;
 118        uint32_t i = 0;
 119        uint8_t *tmp;
 120
 121        /* sanity check on queue memory */
 122        if (unlikely(!q))
 123                return -ENOMEM;
 124        temp_wqe = q->qe[q->host_index].wqe;
 125
 126        /* If the host has not yet processed the next entry then we are done */
 127        idx = ((q->host_index + 1) % q->entry_count);
 128        if (idx == q->hba_index) {
 129                q->WQ_overflow++;
 130                return -EBUSY;
 131        }
 132        q->WQ_posted++;
 133        /* set consumption flag every once in a while */
 134        if (!((q->host_index + 1) % q->entry_repost))
 135                bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
 136        else
 137                bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
 138        if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
 139                bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
 140        lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
 141        if (q->dpp_enable && q->phba->cfg_enable_dpp) {
 142                /* write to DPP aperture taking advatage of Combined Writes */
 143                tmp = (uint8_t *)temp_wqe;
 144#ifdef __raw_writeq
 145                for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
 146                        __raw_writeq(*((uint64_t *)(tmp + i)),
 147                                        q->dpp_regaddr + i);
 148#else
 149                for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
 150                        __raw_writel(*((uint32_t *)(tmp + i)),
 151                                        q->dpp_regaddr + i);
 152#endif
 153        }
 154        /* ensure WQE bcopy and DPP flushed before doorbell write */
 155        wmb();
 156
 157        /* Update the host index before invoking device */
 158        host_index = q->host_index;
 159
 160        q->host_index = idx;
 161
 162        /* Ring Doorbell */
 163        doorbell.word0 = 0;
 164        if (q->db_format == LPFC_DB_LIST_FORMAT) {
 165                if (q->dpp_enable && q->phba->cfg_enable_dpp) {
 166                        bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
 167                        bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
 168                        bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
 169                            q->dpp_id);
 170                        bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
 171                            q->queue_id);
 172                } else {
 173                        bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
 174                        bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
 175                        bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
 176                }
 177        } else if (q->db_format == LPFC_DB_RING_FORMAT) {
 178                bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
 179                bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
 180        } else {
 181                return -EINVAL;
 182        }
 183        writel(doorbell.word0, q->db_regaddr);
 184
 185        return 0;
 186}
 187
 188/**
 189 * lpfc_sli4_wq_release - Updates internal hba index for WQ
 190 * @q: The Work Queue to operate on.
 191 * @index: The index to advance the hba index to.
 192 *
 193 * This routine will update the HBA index of a queue to reflect consumption of
 194 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
 195 * an entry the host calls this function to update the queue's internal
 196 * pointers. This routine returns the number of entries that were consumed by
 197 * the HBA.
 198 **/
 199static uint32_t
 200lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
 201{
 202        uint32_t released = 0;
 203
 204        /* sanity check on queue memory */
 205        if (unlikely(!q))
 206                return 0;
 207
 208        if (q->hba_index == index)
 209                return 0;
 210        do {
 211                q->hba_index = ((q->hba_index + 1) % q->entry_count);
 212                released++;
 213        } while (q->hba_index != index);
 214        return released;
 215}
 216
 217/**
 218 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
 219 * @q: The Mailbox Queue to operate on.
 220 * @wqe: The Mailbox Queue Entry to put on the Work queue.
 221 *
 222 * This routine will copy the contents of @mqe to the next available entry on
 223 * the @q. This function will then ring the Work Queue Doorbell to signal the
 224 * HBA to start processing the Work Queue Entry. This function returns 0 if
 225 * successful. If no entries are available on @q then this function will return
 226 * -ENOMEM.
 227 * The caller is expected to hold the hbalock when calling this routine.
 228 **/
 229static uint32_t
 230lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
 231{
 232        struct lpfc_mqe *temp_mqe;
 233        struct lpfc_register doorbell;
 234
 235        /* sanity check on queue memory */
 236        if (unlikely(!q))
 237                return -ENOMEM;
 238        temp_mqe = q->qe[q->host_index].mqe;
 239
 240        /* If the host has not yet processed the next entry then we are done */
 241        if (((q->host_index + 1) % q->entry_count) == q->hba_index)
 242                return -ENOMEM;
 243        lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
 244        /* Save off the mailbox pointer for completion */
 245        q->phba->mbox = (MAILBOX_t *)temp_mqe;
 246
 247        /* Update the host index before invoking device */
 248        q->host_index = ((q->host_index + 1) % q->entry_count);
 249
 250        /* Ring Doorbell */
 251        doorbell.word0 = 0;
 252        bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
 253        bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
 254        writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
 255        return 0;
 256}
 257
 258/**
 259 * lpfc_sli4_mq_release - Updates internal hba index for MQ
 260 * @q: The Mailbox Queue to operate on.
 261 *
 262 * This routine will update the HBA index of a queue to reflect consumption of
 263 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
 264 * an entry the host calls this function to update the queue's internal
 265 * pointers. This routine returns the number of entries that were consumed by
 266 * the HBA.
 267 **/
 268static uint32_t
 269lpfc_sli4_mq_release(struct lpfc_queue *q)
 270{
 271        /* sanity check on queue memory */
 272        if (unlikely(!q))
 273                return 0;
 274
 275        /* Clear the mailbox pointer for completion */
 276        q->phba->mbox = NULL;
 277        q->hba_index = ((q->hba_index + 1) % q->entry_count);
 278        return 1;
 279}
 280
 281/**
 282 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
 283 * @q: The Event Queue to get the first valid EQE from
 284 *
 285 * This routine will get the first valid Event Queue Entry from @q, update
 286 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
 287 * the Queue (no more work to do), or the Queue is full of EQEs that have been
 288 * processed, but not popped back to the HBA then this routine will return NULL.
 289 **/
 290static struct lpfc_eqe *
 291lpfc_sli4_eq_get(struct lpfc_queue *q)
 292{
 293        struct lpfc_hba *phba;
 294        struct lpfc_eqe *eqe;
 295        uint32_t idx;
 296
 297        /* sanity check on queue memory */
 298        if (unlikely(!q))
 299                return NULL;
 300        phba = q->phba;
 301        eqe = q->qe[q->hba_index].eqe;
 302
 303        /* If the next EQE is not valid then we are done */
 304        if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
 305                return NULL;
 306        /* If the host has not yet processed the next entry then we are done */
 307        idx = ((q->hba_index + 1) % q->entry_count);
 308        if (idx == q->host_index)
 309                return NULL;
 310
 311        q->hba_index = idx;
 312        /* if the index wrapped around, toggle the valid bit */
 313        if (phba->sli4_hba.pc_sli4_params.eqav && !q->hba_index)
 314                q->qe_valid = (q->qe_valid) ? 0 : 1;
 315
 316
 317        /*
 318         * insert barrier for instruction interlock : data from the hardware
 319         * must have the valid bit checked before it can be copied and acted
 320         * upon. Speculative instructions were allowing a bcopy at the start
 321         * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
 322         * after our return, to copy data before the valid bit check above
 323         * was done. As such, some of the copied data was stale. The barrier
 324         * ensures the check is before any data is copied.
 325         */
 326        mb();
 327        return eqe;
 328}
 329
 330/**
 331 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
 332 * @q: The Event Queue to disable interrupts
 333 *
 334 **/
 335inline void
 336lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
 337{
 338        struct lpfc_register doorbell;
 339
 340        doorbell.word0 = 0;
 341        bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
 342        bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
 343        bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
 344                (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
 345        bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
 346        writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
 347}
 348
 349/**
 350 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
 351 * @q: The Event Queue to disable interrupts
 352 *
 353 **/
 354inline void
 355lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
 356{
 357        struct lpfc_register doorbell;
 358
 359        doorbell.word0 = 0;
 360        bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
 361        bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
 362        bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
 363                (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
 364        bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
 365        writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
 366}
 367
 368/**
 369 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
 370 * @q: The Event Queue that the host has completed processing for.
 371 * @arm: Indicates whether the host wants to arms this CQ.
 372 *
 373 * This routine will mark all Event Queue Entries on @q, from the last
 374 * known completed entry to the last entry that was processed, as completed
 375 * by clearing the valid bit for each completion queue entry. Then it will
 376 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
 377 * The internal host index in the @q will be updated by this routine to indicate
 378 * that the host has finished processing the entries. The @arm parameter
 379 * indicates that the queue should be rearmed when ringing the doorbell.
 380 *
 381 * This function will return the number of EQEs that were popped.
 382 **/
 383uint32_t
 384lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
 385{
 386        uint32_t released = 0;
 387        struct lpfc_hba *phba;
 388        struct lpfc_eqe *temp_eqe;
 389        struct lpfc_register doorbell;
 390
 391        /* sanity check on queue memory */
 392        if (unlikely(!q))
 393                return 0;
 394        phba = q->phba;
 395
 396        /* while there are valid entries */
 397        while (q->hba_index != q->host_index) {
 398                if (!phba->sli4_hba.pc_sli4_params.eqav) {
 399                        temp_eqe = q->qe[q->host_index].eqe;
 400                        bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
 401                }
 402                released++;
 403                q->host_index = ((q->host_index + 1) % q->entry_count);
 404        }
 405        if (unlikely(released == 0 && !arm))
 406                return 0;
 407
 408        /* ring doorbell for number popped */
 409        doorbell.word0 = 0;
 410        if (arm) {
 411                bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
 412                bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
 413        }
 414        bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
 415        bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
 416        bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
 417                        (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
 418        bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
 419        writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
 420        /* PCI read to flush PCI pipeline on re-arming for INTx mode */
 421        if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
 422                readl(q->phba->sli4_hba.EQDBregaddr);
 423        return released;
 424}
 425
 426/**
 427 * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
 428 * @q: The Event Queue that the host has completed processing for.
 429 * @arm: Indicates whether the host wants to arms this CQ.
 430 *
 431 * This routine will mark all Event Queue Entries on @q, from the last
 432 * known completed entry to the last entry that was processed, as completed
 433 * by clearing the valid bit for each completion queue entry. Then it will
 434 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
 435 * The internal host index in the @q will be updated by this routine to indicate
 436 * that the host has finished processing the entries. The @arm parameter
 437 * indicates that the queue should be rearmed when ringing the doorbell.
 438 *
 439 * This function will return the number of EQEs that were popped.
 440 **/
 441uint32_t
 442lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
 443{
 444        uint32_t released = 0;
 445        struct lpfc_hba *phba;
 446        struct lpfc_eqe *temp_eqe;
 447        struct lpfc_register doorbell;
 448
 449        /* sanity check on queue memory */
 450        if (unlikely(!q))
 451                return 0;
 452        phba = q->phba;
 453
 454        /* while there are valid entries */
 455        while (q->hba_index != q->host_index) {
 456                if (!phba->sli4_hba.pc_sli4_params.eqav) {
 457                        temp_eqe = q->qe[q->host_index].eqe;
 458                        bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
 459                }
 460                released++;
 461                q->host_index = ((q->host_index + 1) % q->entry_count);
 462        }
 463        if (unlikely(released == 0 && !arm))
 464                return 0;
 465
 466        /* ring doorbell for number popped */
 467        doorbell.word0 = 0;
 468        if (arm)
 469                bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
 470        bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
 471        bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
 472        writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
 473        /* PCI read to flush PCI pipeline on re-arming for INTx mode */
 474        if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
 475                readl(q->phba->sli4_hba.EQDBregaddr);
 476        return released;
 477}
 478
 479/**
 480 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
 481 * @q: The Completion Queue to get the first valid CQE from
 482 *
 483 * This routine will get the first valid Completion Queue Entry from @q, update
 484 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
 485 * the Queue (no more work to do), or the Queue is full of CQEs that have been
 486 * processed, but not popped back to the HBA then this routine will return NULL.
 487 **/
 488static struct lpfc_cqe *
 489lpfc_sli4_cq_get(struct lpfc_queue *q)
 490{
 491        struct lpfc_hba *phba;
 492        struct lpfc_cqe *cqe;
 493        uint32_t idx;
 494
 495        /* sanity check on queue memory */
 496        if (unlikely(!q))
 497                return NULL;
 498        phba = q->phba;
 499        cqe = q->qe[q->hba_index].cqe;
 500
 501        /* If the next CQE is not valid then we are done */
 502        if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
 503                return NULL;
 504        /* If the host has not yet processed the next entry then we are done */
 505        idx = ((q->hba_index + 1) % q->entry_count);
 506        if (idx == q->host_index)
 507                return NULL;
 508
 509        q->hba_index = idx;
 510        /* if the index wrapped around, toggle the valid bit */
 511        if (phba->sli4_hba.pc_sli4_params.cqav && !q->hba_index)
 512                q->qe_valid = (q->qe_valid) ? 0 : 1;
 513
 514        /*
 515         * insert barrier for instruction interlock : data from the hardware
 516         * must have the valid bit checked before it can be copied and acted
 517         * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
 518         * instructions allowing action on content before valid bit checked,
 519         * add barrier here as well. May not be needed as "content" is a
 520         * single 32-bit entity here (vs multi word structure for cq's).
 521         */
 522        mb();
 523        return cqe;
 524}
 525
 526/**
 527 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
 528 * @q: The Completion Queue that the host has completed processing for.
 529 * @arm: Indicates whether the host wants to arms this CQ.
 530 *
 531 * This routine will mark all Completion queue entries on @q, from the last
 532 * known completed entry to the last entry that was processed, as completed
 533 * by clearing the valid bit for each completion queue entry. Then it will
 534 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
 535 * The internal host index in the @q will be updated by this routine to indicate
 536 * that the host has finished processing the entries. The @arm parameter
 537 * indicates that the queue should be rearmed when ringing the doorbell.
 538 *
 539 * This function will return the number of CQEs that were released.
 540 **/
 541uint32_t
 542lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
 543{
 544        uint32_t released = 0;
 545        struct lpfc_hba *phba;
 546        struct lpfc_cqe *temp_qe;
 547        struct lpfc_register doorbell;
 548
 549        /* sanity check on queue memory */
 550        if (unlikely(!q))
 551                return 0;
 552        phba = q->phba;
 553
 554        /* while there are valid entries */
 555        while (q->hba_index != q->host_index) {
 556                if (!phba->sli4_hba.pc_sli4_params.cqav) {
 557                        temp_qe = q->qe[q->host_index].cqe;
 558                        bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
 559                }
 560                released++;
 561                q->host_index = ((q->host_index + 1) % q->entry_count);
 562        }
 563        if (unlikely(released == 0 && !arm))
 564                return 0;
 565
 566        /* ring doorbell for number popped */
 567        doorbell.word0 = 0;
 568        if (arm)
 569                bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
 570        bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
 571        bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
 572        bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
 573                        (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
 574        bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
 575        writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
 576        return released;
 577}
 578
 579/**
 580 * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
 581 * @q: The Completion Queue that the host has completed processing for.
 582 * @arm: Indicates whether the host wants to arms this CQ.
 583 *
 584 * This routine will mark all Completion queue entries on @q, from the last
 585 * known completed entry to the last entry that was processed, as completed
 586 * by clearing the valid bit for each completion queue entry. Then it will
 587 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
 588 * The internal host index in the @q will be updated by this routine to indicate
 589 * that the host has finished processing the entries. The @arm parameter
 590 * indicates that the queue should be rearmed when ringing the doorbell.
 591 *
 592 * This function will return the number of CQEs that were released.
 593 **/
 594uint32_t
 595lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
 596{
 597        uint32_t released = 0;
 598        struct lpfc_hba *phba;
 599        struct lpfc_cqe *temp_qe;
 600        struct lpfc_register doorbell;
 601
 602        /* sanity check on queue memory */
 603        if (unlikely(!q))
 604                return 0;
 605        phba = q->phba;
 606
 607        /* while there are valid entries */
 608        while (q->hba_index != q->host_index) {
 609                if (!phba->sli4_hba.pc_sli4_params.cqav) {
 610                        temp_qe = q->qe[q->host_index].cqe;
 611                        bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
 612                }
 613                released++;
 614                q->host_index = ((q->host_index + 1) % q->entry_count);
 615        }
 616        if (unlikely(released == 0 && !arm))
 617                return 0;
 618
 619        /* ring doorbell for number popped */
 620        doorbell.word0 = 0;
 621        if (arm)
 622                bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
 623        bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
 624        bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
 625        writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
 626        return released;
 627}
 628
 629/**
 630 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
 631 * @q: The Header Receive Queue to operate on.
 632 * @wqe: The Receive Queue Entry to put on the Receive queue.
 633 *
 634 * This routine will copy the contents of @wqe to the next available entry on
 635 * the @q. This function will then ring the Receive Queue Doorbell to signal the
 636 * HBA to start processing the Receive Queue Entry. This function returns the
 637 * index that the rqe was copied to if successful. If no entries are available
 638 * on @q then this function will return -ENOMEM.
 639 * The caller is expected to hold the hbalock when calling this routine.
 640 **/
 641int
 642lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
 643                 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
 644{
 645        struct lpfc_rqe *temp_hrqe;
 646        struct lpfc_rqe *temp_drqe;
 647        struct lpfc_register doorbell;
 648        int hq_put_index;
 649        int dq_put_index;
 650
 651        /* sanity check on queue memory */
 652        if (unlikely(!hq) || unlikely(!dq))
 653                return -ENOMEM;
 654        hq_put_index = hq->host_index;
 655        dq_put_index = dq->host_index;
 656        temp_hrqe = hq->qe[hq_put_index].rqe;
 657        temp_drqe = dq->qe[dq_put_index].rqe;
 658
 659        if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
 660                return -EINVAL;
 661        if (hq_put_index != dq_put_index)
 662                return -EINVAL;
 663        /* If the host has not yet processed the next entry then we are done */
 664        if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
 665                return -EBUSY;
 666        lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
 667        lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
 668
 669        /* Update the host index to point to the next slot */
 670        hq->host_index = ((hq_put_index + 1) % hq->entry_count);
 671        dq->host_index = ((dq_put_index + 1) % dq->entry_count);
 672        hq->RQ_buf_posted++;
 673
 674        /* Ring The Header Receive Queue Doorbell */
 675        if (!(hq->host_index % hq->entry_repost)) {
 676                doorbell.word0 = 0;
 677                if (hq->db_format == LPFC_DB_RING_FORMAT) {
 678                        bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
 679                               hq->entry_repost);
 680                        bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
 681                } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
 682                        bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
 683                               hq->entry_repost);
 684                        bf_set(lpfc_rq_db_list_fm_index, &doorbell,
 685                               hq->host_index);
 686                        bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
 687                } else {
 688                        return -EINVAL;
 689                }
 690                writel(doorbell.word0, hq->db_regaddr);
 691        }
 692        return hq_put_index;
 693}
 694
 695/**
 696 * lpfc_sli4_rq_release - Updates internal hba index for RQ
 697 * @q: The Header Receive Queue to operate on.
 698 *
 699 * This routine will update the HBA index of a queue to reflect consumption of
 700 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
 701 * consumed an entry the host calls this function to update the queue's
 702 * internal pointers. This routine returns the number of entries that were
 703 * consumed by the HBA.
 704 **/
 705static uint32_t
 706lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
 707{
 708        /* sanity check on queue memory */
 709        if (unlikely(!hq) || unlikely(!dq))
 710                return 0;
 711
 712        if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
 713                return 0;
 714        hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
 715        dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
 716        return 1;
 717}
 718
 719/**
 720 * lpfc_cmd_iocb - Get next command iocb entry in the ring
 721 * @phba: Pointer to HBA context object.
 722 * @pring: Pointer to driver SLI ring object.
 723 *
 724 * This function returns pointer to next command iocb entry
 725 * in the command ring. The caller must hold hbalock to prevent
 726 * other threads consume the next command iocb.
 727 * SLI-2/SLI-3 provide different sized iocbs.
 728 **/
 729static inline IOCB_t *
 730lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 731{
 732        return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
 733                           pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
 734}
 735
 736/**
 737 * lpfc_resp_iocb - Get next response iocb entry in the ring
 738 * @phba: Pointer to HBA context object.
 739 * @pring: Pointer to driver SLI ring object.
 740 *
 741 * This function returns pointer to next response iocb entry
 742 * in the response ring. The caller must hold hbalock to make sure
 743 * that no other thread consume the next response iocb.
 744 * SLI-2/SLI-3 provide different sized iocbs.
 745 **/
 746static inline IOCB_t *
 747lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 748{
 749        return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
 750                           pring->sli.sli3.rspidx * phba->iocb_rsp_size);
 751}
 752
 753/**
 754 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
 755 * @phba: Pointer to HBA context object.
 756 *
 757 * This function is called with hbalock held. This function
 758 * allocates a new driver iocb object from the iocb pool. If the
 759 * allocation is successful, it returns pointer to the newly
 760 * allocated iocb object else it returns NULL.
 761 **/
 762struct lpfc_iocbq *
 763__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
 764{
 765        struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
 766        struct lpfc_iocbq * iocbq = NULL;
 767
 768        lockdep_assert_held(&phba->hbalock);
 769
 770        list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
 771        if (iocbq)
 772                phba->iocb_cnt++;
 773        if (phba->iocb_cnt > phba->iocb_max)
 774                phba->iocb_max = phba->iocb_cnt;
 775        return iocbq;
 776}
 777
 778/**
 779 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
 780 * @phba: Pointer to HBA context object.
 781 * @xritag: XRI value.
 782 *
 783 * This function clears the sglq pointer from the array of acive
 784 * sglq's. The xritag that is passed in is used to index into the
 785 * array. Before the xritag can be used it needs to be adjusted
 786 * by subtracting the xribase.
 787 *
 788 * Returns sglq ponter = success, NULL = Failure.
 789 **/
 790struct lpfc_sglq *
 791__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
 792{
 793        struct lpfc_sglq *sglq;
 794
 795        sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
 796        phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
 797        return sglq;
 798}
 799
 800/**
 801 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
 802 * @phba: Pointer to HBA context object.
 803 * @xritag: XRI value.
 804 *
 805 * This function returns the sglq pointer from the array of acive
 806 * sglq's. The xritag that is passed in is used to index into the
 807 * array. Before the xritag can be used it needs to be adjusted
 808 * by subtracting the xribase.
 809 *
 810 * Returns sglq ponter = success, NULL = Failure.
 811 **/
 812struct lpfc_sglq *
 813__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
 814{
 815        struct lpfc_sglq *sglq;
 816
 817        sglq =  phba->sli4_hba.lpfc_sglq_active_list[xritag];
 818        return sglq;
 819}
 820
 821/**
 822 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
 823 * @phba: Pointer to HBA context object.
 824 * @xritag: xri used in this exchange.
 825 * @rrq: The RRQ to be cleared.
 826 *
 827 **/
 828void
 829lpfc_clr_rrq_active(struct lpfc_hba *phba,
 830                    uint16_t xritag,
 831                    struct lpfc_node_rrq *rrq)
 832{
 833        struct lpfc_nodelist *ndlp = NULL;
 834
 835        if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
 836                ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
 837
 838        /* The target DID could have been swapped (cable swap)
 839         * we should use the ndlp from the findnode if it is
 840         * available.
 841         */
 842        if ((!ndlp) && rrq->ndlp)
 843                ndlp = rrq->ndlp;
 844
 845        if (!ndlp)
 846                goto out;
 847
 848        if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
 849                rrq->send_rrq = 0;
 850                rrq->xritag = 0;
 851                rrq->rrq_stop_time = 0;
 852        }
 853out:
 854        mempool_free(rrq, phba->rrq_pool);
 855}
 856
 857/**
 858 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
 859 * @phba: Pointer to HBA context object.
 860 *
 861 * This function is called with hbalock held. This function
 862 * Checks if stop_time (ratov from setting rrq active) has
 863 * been reached, if it has and the send_rrq flag is set then
 864 * it will call lpfc_send_rrq. If the send_rrq flag is not set
 865 * then it will just call the routine to clear the rrq and
 866 * free the rrq resource.
 867 * The timer is set to the next rrq that is going to expire before
 868 * leaving the routine.
 869 *
 870 **/
 871void
 872lpfc_handle_rrq_active(struct lpfc_hba *phba)
 873{
 874        struct lpfc_node_rrq *rrq;
 875        struct lpfc_node_rrq *nextrrq;
 876        unsigned long next_time;
 877        unsigned long iflags;
 878        LIST_HEAD(send_rrq);
 879
 880        spin_lock_irqsave(&phba->hbalock, iflags);
 881        phba->hba_flag &= ~HBA_RRQ_ACTIVE;
 882        next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
 883        list_for_each_entry_safe(rrq, nextrrq,
 884                                 &phba->active_rrq_list, list) {
 885                if (time_after(jiffies, rrq->rrq_stop_time))
 886                        list_move(&rrq->list, &send_rrq);
 887                else if (time_before(rrq->rrq_stop_time, next_time))
 888                        next_time = rrq->rrq_stop_time;
 889        }
 890        spin_unlock_irqrestore(&phba->hbalock, iflags);
 891        if ((!list_empty(&phba->active_rrq_list)) &&
 892            (!(phba->pport->load_flag & FC_UNLOADING)))
 893                mod_timer(&phba->rrq_tmr, next_time);
 894        list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
 895                list_del(&rrq->list);
 896                if (!rrq->send_rrq)
 897                        /* this call will free the rrq */
 898                lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
 899                else if (lpfc_send_rrq(phba, rrq)) {
 900                        /* if we send the rrq then the completion handler
 901                        *  will clear the bit in the xribitmap.
 902                        */
 903                        lpfc_clr_rrq_active(phba, rrq->xritag,
 904                                            rrq);
 905                }
 906        }
 907}
 908
 909/**
 910 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
 911 * @vport: Pointer to vport context object.
 912 * @xri: The xri used in the exchange.
 913 * @did: The targets DID for this exchange.
 914 *
 915 * returns NULL = rrq not found in the phba->active_rrq_list.
 916 *         rrq = rrq for this xri and target.
 917 **/
 918struct lpfc_node_rrq *
 919lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
 920{
 921        struct lpfc_hba *phba = vport->phba;
 922        struct lpfc_node_rrq *rrq;
 923        struct lpfc_node_rrq *nextrrq;
 924        unsigned long iflags;
 925
 926        if (phba->sli_rev != LPFC_SLI_REV4)
 927                return NULL;
 928        spin_lock_irqsave(&phba->hbalock, iflags);
 929        list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
 930                if (rrq->vport == vport && rrq->xritag == xri &&
 931                                rrq->nlp_DID == did){
 932                        list_del(&rrq->list);
 933                        spin_unlock_irqrestore(&phba->hbalock, iflags);
 934                        return rrq;
 935                }
 936        }
 937        spin_unlock_irqrestore(&phba->hbalock, iflags);
 938        return NULL;
 939}
 940
 941/**
 942 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
 943 * @vport: Pointer to vport context object.
 944 * @ndlp: Pointer to the lpfc_node_list structure.
 945 * If ndlp is NULL Remove all active RRQs for this vport from the
 946 * phba->active_rrq_list and clear the rrq.
 947 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
 948 **/
 949void
 950lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
 951
 952{
 953        struct lpfc_hba *phba = vport->phba;
 954        struct lpfc_node_rrq *rrq;
 955        struct lpfc_node_rrq *nextrrq;
 956        unsigned long iflags;
 957        LIST_HEAD(rrq_list);
 958
 959        if (phba->sli_rev != LPFC_SLI_REV4)
 960                return;
 961        if (!ndlp) {
 962                lpfc_sli4_vport_delete_els_xri_aborted(vport);
 963                lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
 964        }
 965        spin_lock_irqsave(&phba->hbalock, iflags);
 966        list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
 967                if ((rrq->vport == vport) && (!ndlp  || rrq->ndlp == ndlp))
 968                        list_move(&rrq->list, &rrq_list);
 969        spin_unlock_irqrestore(&phba->hbalock, iflags);
 970
 971        list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
 972                list_del(&rrq->list);
 973                lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
 974        }
 975}
 976
 977/**
 978 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
 979 * @phba: Pointer to HBA context object.
 980 * @ndlp: Targets nodelist pointer for this exchange.
 981 * @xritag the xri in the bitmap to test.
 982 *
 983 * This function is called with hbalock held. This function
 984 * returns 0 = rrq not active for this xri
 985 *         1 = rrq is valid for this xri.
 986 **/
 987int
 988lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
 989                        uint16_t  xritag)
 990{
 991        lockdep_assert_held(&phba->hbalock);
 992        if (!ndlp)
 993                return 0;
 994        if (!ndlp->active_rrqs_xri_bitmap)
 995                return 0;
 996        if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
 997                        return 1;
 998        else
 999                return 0;
1000}
1001
1002/**
1003 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1004 * @phba: Pointer to HBA context object.
1005 * @ndlp: nodelist pointer for this target.
1006 * @xritag: xri used in this exchange.
1007 * @rxid: Remote Exchange ID.
1008 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1009 *
1010 * This function takes the hbalock.
1011 * The active bit is always set in the active rrq xri_bitmap even
1012 * if there is no slot avaiable for the other rrq information.
1013 *
1014 * returns 0 rrq actived for this xri
1015 *         < 0 No memory or invalid ndlp.
1016 **/
1017int
1018lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1019                    uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
1020{
1021        unsigned long iflags;
1022        struct lpfc_node_rrq *rrq;
1023        int empty;
1024
1025        if (!ndlp)
1026                return -EINVAL;
1027
1028        if (!phba->cfg_enable_rrq)
1029                return -EINVAL;
1030
1031        spin_lock_irqsave(&phba->hbalock, iflags);
1032        if (phba->pport->load_flag & FC_UNLOADING) {
1033                phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1034                goto out;
1035        }
1036
1037        /*
1038         * set the active bit even if there is no mem available.
1039         */
1040        if (NLP_CHK_FREE_REQ(ndlp))
1041                goto out;
1042
1043        if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1044                goto out;
1045
1046        if (!ndlp->active_rrqs_xri_bitmap)
1047                goto out;
1048
1049        if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
1050                goto out;
1051
1052        spin_unlock_irqrestore(&phba->hbalock, iflags);
1053        rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
1054        if (!rrq) {
1055                lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1056                                "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1057                                " DID:0x%x Send:%d\n",
1058                                xritag, rxid, ndlp->nlp_DID, send_rrq);
1059                return -EINVAL;
1060        }
1061        if (phba->cfg_enable_rrq == 1)
1062                rrq->send_rrq = send_rrq;
1063        else
1064                rrq->send_rrq = 0;
1065        rrq->xritag = xritag;
1066        rrq->rrq_stop_time = jiffies +
1067                                msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
1068        rrq->ndlp = ndlp;
1069        rrq->nlp_DID = ndlp->nlp_DID;
1070        rrq->vport = ndlp->vport;
1071        rrq->rxid = rxid;
1072        spin_lock_irqsave(&phba->hbalock, iflags);
1073        empty = list_empty(&phba->active_rrq_list);
1074        list_add_tail(&rrq->list, &phba->active_rrq_list);
1075        phba->hba_flag |= HBA_RRQ_ACTIVE;
1076        if (empty)
1077                lpfc_worker_wake_up(phba);
1078        spin_unlock_irqrestore(&phba->hbalock, iflags);
1079        return 0;
1080out:
1081        spin_unlock_irqrestore(&phba->hbalock, iflags);
1082        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1083                        "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1084                        " DID:0x%x Send:%d\n",
1085                        xritag, rxid, ndlp->nlp_DID, send_rrq);
1086        return -EINVAL;
1087}
1088
1089/**
1090 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
1091 * @phba: Pointer to HBA context object.
1092 * @piocb: Pointer to the iocbq.
1093 *
1094 * This function is called with the ring lock held. This function
1095 * gets a new driver sglq object from the sglq list. If the
1096 * list is not empty then it is successful, it returns pointer to the newly
1097 * allocated sglq object else it returns NULL.
1098 **/
1099static struct lpfc_sglq *
1100__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1101{
1102        struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
1103        struct lpfc_sglq *sglq = NULL;
1104        struct lpfc_sglq *start_sglq = NULL;
1105        struct lpfc_scsi_buf *lpfc_cmd;
1106        struct lpfc_nodelist *ndlp;
1107        int found = 0;
1108
1109        lockdep_assert_held(&phba->hbalock);
1110
1111        if (piocbq->iocb_flag &  LPFC_IO_FCP) {
1112                lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
1113                ndlp = lpfc_cmd->rdata->pnode;
1114        } else  if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
1115                        !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
1116                ndlp = piocbq->context_un.ndlp;
1117        } else  if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1118                if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1119                        ndlp = NULL;
1120                else
1121                        ndlp = piocbq->context_un.ndlp;
1122        } else {
1123                ndlp = piocbq->context1;
1124        }
1125
1126        spin_lock(&phba->sli4_hba.sgl_list_lock);
1127        list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
1128        start_sglq = sglq;
1129        while (!found) {
1130                if (!sglq)
1131                        break;
1132                if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1133                    test_bit(sglq->sli4_lxritag,
1134                    ndlp->active_rrqs_xri_bitmap)) {
1135                        /* This xri has an rrq outstanding for this DID.
1136                         * put it back in the list and get another xri.
1137                         */
1138                        list_add_tail(&sglq->list, lpfc_els_sgl_list);
1139                        sglq = NULL;
1140                        list_remove_head(lpfc_els_sgl_list, sglq,
1141                                                struct lpfc_sglq, list);
1142                        if (sglq == start_sglq) {
1143                                list_add_tail(&sglq->list, lpfc_els_sgl_list);
1144                                sglq = NULL;
1145                                break;
1146                        } else
1147                                continue;
1148                }
1149                sglq->ndlp = ndlp;
1150                found = 1;
1151                phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1152                sglq->state = SGL_ALLOCATED;
1153        }
1154        spin_unlock(&phba->sli4_hba.sgl_list_lock);
1155        return sglq;
1156}
1157
1158/**
1159 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1160 * @phba: Pointer to HBA context object.
1161 * @piocb: Pointer to the iocbq.
1162 *
1163 * This function is called with the sgl_list lock held. This function
1164 * gets a new driver sglq object from the sglq list. If the
1165 * list is not empty then it is successful, it returns pointer to the newly
1166 * allocated sglq object else it returns NULL.
1167 **/
1168struct lpfc_sglq *
1169__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1170{
1171        struct list_head *lpfc_nvmet_sgl_list;
1172        struct lpfc_sglq *sglq = NULL;
1173
1174        lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1175
1176        lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1177
1178        list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1179        if (!sglq)
1180                return NULL;
1181        phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1182        sglq->state = SGL_ALLOCATED;
1183        return sglq;
1184}
1185
1186/**
1187 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
1188 * @phba: Pointer to HBA context object.
1189 *
1190 * This function is called with no lock held. This function
1191 * allocates a new driver iocb object from the iocb pool. If the
1192 * allocation is successful, it returns pointer to the newly
1193 * allocated iocb object else it returns NULL.
1194 **/
1195struct lpfc_iocbq *
1196lpfc_sli_get_iocbq(struct lpfc_hba *phba)
1197{
1198        struct lpfc_iocbq * iocbq = NULL;
1199        unsigned long iflags;
1200
1201        spin_lock_irqsave(&phba->hbalock, iflags);
1202        iocbq = __lpfc_sli_get_iocbq(phba);
1203        spin_unlock_irqrestore(&phba->hbalock, iflags);
1204        return iocbq;
1205}
1206
1207/**
1208 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1209 * @phba: Pointer to HBA context object.
1210 * @iocbq: Pointer to driver iocb object.
1211 *
1212 * This function is called with hbalock held to release driver
1213 * iocb object to the iocb pool. The iotag in the iocb object
1214 * does not change for each use of the iocb object. This function
1215 * clears all other fields of the iocb object when it is freed.
1216 * The sqlq structure that holds the xritag and phys and virtual
1217 * mappings for the scatter gather list is retrieved from the
1218 * active array of sglq. The get of the sglq pointer also clears
1219 * the entry in the array. If the status of the IO indiactes that
1220 * this IO was aborted then the sglq entry it put on the
1221 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1222 * IO has good status or fails for any other reason then the sglq
1223 * entry is added to the free list (lpfc_els_sgl_list).
1224 **/
1225static void
1226__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1227{
1228        struct lpfc_sglq *sglq;
1229        size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1230        unsigned long iflag = 0;
1231        struct lpfc_sli_ring *pring;
1232
1233        lockdep_assert_held(&phba->hbalock);
1234
1235        if (iocbq->sli4_xritag == NO_XRI)
1236                sglq = NULL;
1237        else
1238                sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1239
1240
1241        if (sglq)  {
1242                if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1243                        spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1244                                          iflag);
1245                        sglq->state = SGL_FREED;
1246                        sglq->ndlp = NULL;
1247                        list_add_tail(&sglq->list,
1248                                      &phba->sli4_hba.lpfc_nvmet_sgl_list);
1249                        spin_unlock_irqrestore(
1250                                &phba->sli4_hba.sgl_list_lock, iflag);
1251                        goto out;
1252                }
1253
1254                pring = phba->sli4_hba.els_wq->pring;
1255                if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1256                        (sglq->state != SGL_XRI_ABORTED)) {
1257                        spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1258                                          iflag);
1259                        list_add(&sglq->list,
1260                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1261                        spin_unlock_irqrestore(
1262                                &phba->sli4_hba.sgl_list_lock, iflag);
1263                } else {
1264                        spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1265                                          iflag);
1266                        sglq->state = SGL_FREED;
1267                        sglq->ndlp = NULL;
1268                        list_add_tail(&sglq->list,
1269                                      &phba->sli4_hba.lpfc_els_sgl_list);
1270                        spin_unlock_irqrestore(
1271                                &phba->sli4_hba.sgl_list_lock, iflag);
1272
1273                        /* Check if TXQ queue needs to be serviced */
1274                        if (!list_empty(&pring->txq))
1275                                lpfc_worker_wake_up(phba);
1276                }
1277        }
1278
1279out:
1280        /*
1281         * Clean all volatile data fields, preserve iotag and node struct.
1282         */
1283        memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1284        iocbq->sli4_lxritag = NO_XRI;
1285        iocbq->sli4_xritag = NO_XRI;
1286        iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1287                              LPFC_IO_NVME_LS);
1288        list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1289}
1290
1291
1292/**
1293 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1294 * @phba: Pointer to HBA context object.
1295 * @iocbq: Pointer to driver iocb object.
1296 *
1297 * This function is called with hbalock held to release driver
1298 * iocb object to the iocb pool. The iotag in the iocb object
1299 * does not change for each use of the iocb object. This function
1300 * clears all other fields of the iocb object when it is freed.
1301 **/
1302static void
1303__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1304{
1305        size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1306
1307        lockdep_assert_held(&phba->hbalock);
1308
1309        /*
1310         * Clean all volatile data fields, preserve iotag and node struct.
1311         */
1312        memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1313        iocbq->sli4_xritag = NO_XRI;
1314        list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1315}
1316
1317/**
1318 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1319 * @phba: Pointer to HBA context object.
1320 * @iocbq: Pointer to driver iocb object.
1321 *
1322 * This function is called with hbalock held to release driver
1323 * iocb object to the iocb pool. The iotag in the iocb object
1324 * does not change for each use of the iocb object. This function
1325 * clears all other fields of the iocb object when it is freed.
1326 **/
1327static void
1328__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1329{
1330        lockdep_assert_held(&phba->hbalock);
1331
1332        phba->__lpfc_sli_release_iocbq(phba, iocbq);
1333        phba->iocb_cnt--;
1334}
1335
1336/**
1337 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1338 * @phba: Pointer to HBA context object.
1339 * @iocbq: Pointer to driver iocb object.
1340 *
1341 * This function is called with no lock held to release the iocb to
1342 * iocb pool.
1343 **/
1344void
1345lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1346{
1347        unsigned long iflags;
1348
1349        /*
1350         * Clean all volatile data fields, preserve iotag and node struct.
1351         */
1352        spin_lock_irqsave(&phba->hbalock, iflags);
1353        __lpfc_sli_release_iocbq(phba, iocbq);
1354        spin_unlock_irqrestore(&phba->hbalock, iflags);
1355}
1356
1357/**
1358 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1359 * @phba: Pointer to HBA context object.
1360 * @iocblist: List of IOCBs.
1361 * @ulpstatus: ULP status in IOCB command field.
1362 * @ulpWord4: ULP word-4 in IOCB command field.
1363 *
1364 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1365 * on the list by invoking the complete callback function associated with the
1366 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1367 * fields.
1368 **/
1369void
1370lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1371                      uint32_t ulpstatus, uint32_t ulpWord4)
1372{
1373        struct lpfc_iocbq *piocb;
1374
1375        while (!list_empty(iocblist)) {
1376                list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1377                if (!piocb->iocb_cmpl)
1378                        lpfc_sli_release_iocbq(phba, piocb);
1379                else {
1380                        piocb->iocb.ulpStatus = ulpstatus;
1381                        piocb->iocb.un.ulpWord[4] = ulpWord4;
1382                        (piocb->iocb_cmpl) (phba, piocb, piocb);
1383                }
1384        }
1385        return;
1386}
1387
1388/**
1389 * lpfc_sli_iocb_cmd_type - Get the iocb type
1390 * @iocb_cmnd: iocb command code.
1391 *
1392 * This function is called by ring event handler function to get the iocb type.
1393 * This function translates the iocb command to an iocb command type used to
1394 * decide the final disposition of each completed IOCB.
1395 * The function returns
1396 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1397 * LPFC_SOL_IOCB     if it is a solicited iocb completion
1398 * LPFC_ABORT_IOCB   if it is an abort iocb
1399 * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
1400 *
1401 * The caller is not required to hold any lock.
1402 **/
1403static lpfc_iocb_type
1404lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1405{
1406        lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1407
1408        if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1409                return 0;
1410
1411        switch (iocb_cmnd) {
1412        case CMD_XMIT_SEQUENCE_CR:
1413        case CMD_XMIT_SEQUENCE_CX:
1414        case CMD_XMIT_BCAST_CN:
1415        case CMD_XMIT_BCAST_CX:
1416        case CMD_ELS_REQUEST_CR:
1417        case CMD_ELS_REQUEST_CX:
1418        case CMD_CREATE_XRI_CR:
1419        case CMD_CREATE_XRI_CX:
1420        case CMD_GET_RPI_CN:
1421        case CMD_XMIT_ELS_RSP_CX:
1422        case CMD_GET_RPI_CR:
1423        case CMD_FCP_IWRITE_CR:
1424        case CMD_FCP_IWRITE_CX:
1425        case CMD_FCP_IREAD_CR:
1426        case CMD_FCP_IREAD_CX:
1427        case CMD_FCP_ICMND_CR:
1428        case CMD_FCP_ICMND_CX:
1429        case CMD_FCP_TSEND_CX:
1430        case CMD_FCP_TRSP_CX:
1431        case CMD_FCP_TRECEIVE_CX:
1432        case CMD_FCP_AUTO_TRSP_CX:
1433        case CMD_ADAPTER_MSG:
1434        case CMD_ADAPTER_DUMP:
1435        case CMD_XMIT_SEQUENCE64_CR:
1436        case CMD_XMIT_SEQUENCE64_CX:
1437        case CMD_XMIT_BCAST64_CN:
1438        case CMD_XMIT_BCAST64_CX:
1439        case CMD_ELS_REQUEST64_CR:
1440        case CMD_ELS_REQUEST64_CX:
1441        case CMD_FCP_IWRITE64_CR:
1442        case CMD_FCP_IWRITE64_CX:
1443        case CMD_FCP_IREAD64_CR:
1444        case CMD_FCP_IREAD64_CX:
1445        case CMD_FCP_ICMND64_CR:
1446        case CMD_FCP_ICMND64_CX:
1447        case CMD_FCP_TSEND64_CX:
1448        case CMD_FCP_TRSP64_CX:
1449        case CMD_FCP_TRECEIVE64_CX:
1450        case CMD_GEN_REQUEST64_CR:
1451        case CMD_GEN_REQUEST64_CX:
1452        case CMD_XMIT_ELS_RSP64_CX:
1453        case DSSCMD_IWRITE64_CR:
1454        case DSSCMD_IWRITE64_CX:
1455        case DSSCMD_IREAD64_CR:
1456        case DSSCMD_IREAD64_CX:
1457                type = LPFC_SOL_IOCB;
1458                break;
1459        case CMD_ABORT_XRI_CN:
1460        case CMD_ABORT_XRI_CX:
1461        case CMD_CLOSE_XRI_CN:
1462        case CMD_CLOSE_XRI_CX:
1463        case CMD_XRI_ABORTED_CX:
1464        case CMD_ABORT_MXRI64_CN:
1465        case CMD_XMIT_BLS_RSP64_CX:
1466                type = LPFC_ABORT_IOCB;
1467                break;
1468        case CMD_RCV_SEQUENCE_CX:
1469        case CMD_RCV_ELS_REQ_CX:
1470        case CMD_RCV_SEQUENCE64_CX:
1471        case CMD_RCV_ELS_REQ64_CX:
1472        case CMD_ASYNC_STATUS:
1473        case CMD_IOCB_RCV_SEQ64_CX:
1474        case CMD_IOCB_RCV_ELS64_CX:
1475        case CMD_IOCB_RCV_CONT64_CX:
1476        case CMD_IOCB_RET_XRI64_CX:
1477                type = LPFC_UNSOL_IOCB;
1478                break;
1479        case CMD_IOCB_XMIT_MSEQ64_CR:
1480        case CMD_IOCB_XMIT_MSEQ64_CX:
1481        case CMD_IOCB_RCV_SEQ_LIST64_CX:
1482        case CMD_IOCB_RCV_ELS_LIST64_CX:
1483        case CMD_IOCB_CLOSE_EXTENDED_CN:
1484        case CMD_IOCB_ABORT_EXTENDED_CN:
1485        case CMD_IOCB_RET_HBQE64_CN:
1486        case CMD_IOCB_FCP_IBIDIR64_CR:
1487        case CMD_IOCB_FCP_IBIDIR64_CX:
1488        case CMD_IOCB_FCP_ITASKMGT64_CX:
1489        case CMD_IOCB_LOGENTRY_CN:
1490        case CMD_IOCB_LOGENTRY_ASYNC_CN:
1491                printk("%s - Unhandled SLI-3 Command x%x\n",
1492                                __func__, iocb_cmnd);
1493                type = LPFC_UNKNOWN_IOCB;
1494                break;
1495        default:
1496                type = LPFC_UNKNOWN_IOCB;
1497                break;
1498        }
1499
1500        return type;
1501}
1502
1503/**
1504 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1505 * @phba: Pointer to HBA context object.
1506 *
1507 * This function is called from SLI initialization code
1508 * to configure every ring of the HBA's SLI interface. The
1509 * caller is not required to hold any lock. This function issues
1510 * a config_ring mailbox command for each ring.
1511 * This function returns zero if successful else returns a negative
1512 * error code.
1513 **/
1514static int
1515lpfc_sli_ring_map(struct lpfc_hba *phba)
1516{
1517        struct lpfc_sli *psli = &phba->sli;
1518        LPFC_MBOXQ_t *pmb;
1519        MAILBOX_t *pmbox;
1520        int i, rc, ret = 0;
1521
1522        pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1523        if (!pmb)
1524                return -ENOMEM;
1525        pmbox = &pmb->u.mb;
1526        phba->link_state = LPFC_INIT_MBX_CMDS;
1527        for (i = 0; i < psli->num_rings; i++) {
1528                lpfc_config_ring(phba, i, pmb);
1529                rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1530                if (rc != MBX_SUCCESS) {
1531                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1532                                        "0446 Adapter failed to init (%d), "
1533                                        "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1534                                        "ring %d\n",
1535                                        rc, pmbox->mbxCommand,
1536                                        pmbox->mbxStatus, i);
1537                        phba->link_state = LPFC_HBA_ERROR;
1538                        ret = -ENXIO;
1539                        break;
1540                }
1541        }
1542        mempool_free(pmb, phba->mbox_mem_pool);
1543        return ret;
1544}
1545
1546/**
1547 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1548 * @phba: Pointer to HBA context object.
1549 * @pring: Pointer to driver SLI ring object.
1550 * @piocb: Pointer to the driver iocb object.
1551 *
1552 * This function is called with hbalock held. The function adds the
1553 * new iocb to txcmplq of the given ring. This function always returns
1554 * 0. If this function is called for ELS ring, this function checks if
1555 * there is a vport associated with the ELS command. This function also
1556 * starts els_tmofunc timer if this is an ELS command.
1557 **/
1558static int
1559lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1560                        struct lpfc_iocbq *piocb)
1561{
1562        lockdep_assert_held(&phba->hbalock);
1563
1564        BUG_ON(!piocb);
1565
1566        list_add_tail(&piocb->list, &pring->txcmplq);
1567        piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
1568
1569        if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1570           (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1571           (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1572                BUG_ON(!piocb->vport);
1573                if (!(piocb->vport->load_flag & FC_UNLOADING))
1574                        mod_timer(&piocb->vport->els_tmofunc,
1575                                  jiffies +
1576                                  msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1577        }
1578
1579        return 0;
1580}
1581
1582/**
1583 * lpfc_sli_ringtx_get - Get first element of the txq
1584 * @phba: Pointer to HBA context object.
1585 * @pring: Pointer to driver SLI ring object.
1586 *
1587 * This function is called with hbalock held to get next
1588 * iocb in txq of the given ring. If there is any iocb in
1589 * the txq, the function returns first iocb in the list after
1590 * removing the iocb from the list, else it returns NULL.
1591 **/
1592struct lpfc_iocbq *
1593lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1594{
1595        struct lpfc_iocbq *cmd_iocb;
1596
1597        lockdep_assert_held(&phba->hbalock);
1598
1599        list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1600        return cmd_iocb;
1601}
1602
1603/**
1604 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1605 * @phba: Pointer to HBA context object.
1606 * @pring: Pointer to driver SLI ring object.
1607 *
1608 * This function is called with hbalock held and the caller must post the
1609 * iocb without releasing the lock. If the caller releases the lock,
1610 * iocb slot returned by the function is not guaranteed to be available.
1611 * The function returns pointer to the next available iocb slot if there
1612 * is available slot in the ring, else it returns NULL.
1613 * If the get index of the ring is ahead of the put index, the function
1614 * will post an error attention event to the worker thread to take the
1615 * HBA to offline state.
1616 **/
1617static IOCB_t *
1618lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1619{
1620        struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1621        uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;
1622
1623        lockdep_assert_held(&phba->hbalock);
1624
1625        if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1626           (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1627                pring->sli.sli3.next_cmdidx = 0;
1628
1629        if (unlikely(pring->sli.sli3.local_getidx ==
1630                pring->sli.sli3.next_cmdidx)) {
1631
1632                pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
1633
1634                if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
1635                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1636                                        "0315 Ring %d issue: portCmdGet %d "
1637                                        "is bigger than cmd ring %d\n",
1638                                        pring->ringno,
1639                                        pring->sli.sli3.local_getidx,
1640                                        max_cmd_idx);
1641
1642                        phba->link_state = LPFC_HBA_ERROR;
1643                        /*
1644                         * All error attention handlers are posted to
1645                         * worker thread
1646                         */
1647                        phba->work_ha |= HA_ERATT;
1648                        phba->work_hs = HS_FFER3;
1649
1650                        lpfc_worker_wake_up(phba);
1651
1652                        return NULL;
1653                }
1654
1655                if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
1656                        return NULL;
1657        }
1658
1659        return lpfc_cmd_iocb(phba, pring);
1660}
1661
1662/**
1663 * lpfc_sli_next_iotag - Get an iotag for the iocb
1664 * @phba: Pointer to HBA context object.
1665 * @iocbq: Pointer to driver iocb object.
1666 *
1667 * This function gets an iotag for the iocb. If there is no unused iotag and
1668 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1669 * array and assigns a new iotag.
1670 * The function returns the allocated iotag if successful, else returns zero.
1671 * Zero is not a valid iotag.
1672 * The caller is not required to hold any lock.
1673 **/
1674uint16_t
1675lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1676{
1677        struct lpfc_iocbq **new_arr;
1678        struct lpfc_iocbq **old_arr;
1679        size_t new_len;
1680        struct lpfc_sli *psli = &phba->sli;
1681        uint16_t iotag;
1682
1683        spin_lock_irq(&phba->hbalock);
1684        iotag = psli->last_iotag;
1685        if(++iotag < psli->iocbq_lookup_len) {
1686                psli->last_iotag = iotag;
1687                psli->iocbq_lookup[iotag] = iocbq;
1688                spin_unlock_irq(&phba->hbalock);
1689                iocbq->iotag = iotag;
1690                return iotag;
1691        } else if (psli->iocbq_lookup_len < (0xffff
1692                                           - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1693                new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1694                spin_unlock_irq(&phba->hbalock);
1695                new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1696                                  GFP_KERNEL);
1697                if (new_arr) {
1698                        spin_lock_irq(&phba->hbalock);
1699                        old_arr = psli->iocbq_lookup;
1700                        if (new_len <= psli->iocbq_lookup_len) {
1701                                /* highly unprobable case */
1702                                kfree(new_arr);
1703                                iotag = psli->last_iotag;
1704                                if(++iotag < psli->iocbq_lookup_len) {
1705                                        psli->last_iotag = iotag;
1706                                        psli->iocbq_lookup[iotag] = iocbq;
1707                                        spin_unlock_irq(&phba->hbalock);
1708                                        iocbq->iotag = iotag;
1709                                        return iotag;
1710                                }
1711                                spin_unlock_irq(&phba->hbalock);
1712                                return 0;
1713                        }
1714                        if (psli->iocbq_lookup)
1715                                memcpy(new_arr, old_arr,
1716                                       ((psli->last_iotag  + 1) *
1717                                        sizeof (struct lpfc_iocbq *)));
1718                        psli->iocbq_lookup = new_arr;
1719                        psli->iocbq_lookup_len = new_len;
1720                        psli->last_iotag = iotag;
1721                        psli->iocbq_lookup[iotag] = iocbq;
1722                        spin_unlock_irq(&phba->hbalock);
1723                        iocbq->iotag = iotag;
1724                        kfree(old_arr);
1725                        return iotag;
1726                }
1727        } else
1728                spin_unlock_irq(&phba->hbalock);
1729
1730        lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1731                        "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1732                        psli->last_iotag);
1733
1734        return 0;
1735}
1736
1737/**
1738 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1739 * @phba: Pointer to HBA context object.
1740 * @pring: Pointer to driver SLI ring object.
1741 * @iocb: Pointer to iocb slot in the ring.
1742 * @nextiocb: Pointer to driver iocb object which need to be
1743 *            posted to firmware.
1744 *
1745 * This function is called with hbalock held to post a new iocb to
1746 * the firmware. This function copies the new iocb to ring iocb slot and
1747 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1748 * a completion call back for this iocb else the function will free the
1749 * iocb object.
1750 **/
1751static void
1752lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1753                IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1754{
1755        lockdep_assert_held(&phba->hbalock);
1756        /*
1757         * Set up an iotag
1758         */
1759        nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1760
1761
1762        if (pring->ringno == LPFC_ELS_RING) {
1763                lpfc_debugfs_slow_ring_trc(phba,
1764                        "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1765                        *(((uint32_t *) &nextiocb->iocb) + 4),
1766                        *(((uint32_t *) &nextiocb->iocb) + 6),
1767                        *(((uint32_t *) &nextiocb->iocb) + 7));
1768        }
1769
1770        /*
1771         * Issue iocb command to adapter
1772         */
1773        lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1774        wmb();
1775        pring->stats.iocb_cmd++;
1776
1777        /*
1778         * If there is no completion routine to call, we can release the
1779         * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1780         * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1781         */
1782        if (nextiocb->iocb_cmpl)
1783                lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1784        else
1785                __lpfc_sli_release_iocbq(phba, nextiocb);
1786
1787        /*
1788         * Let the HBA know what IOCB slot will be the next one the
1789         * driver will put a command into.
1790         */
1791        pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1792        writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1793}
1794
1795/**
1796 * lpfc_sli_update_full_ring - Update the chip attention register
1797 * @phba: Pointer to HBA context object.
1798 * @pring: Pointer to driver SLI ring object.
1799 *
1800 * The caller is not required to hold any lock for calling this function.
1801 * This function updates the chip attention bits for the ring to inform firmware
1802 * that there are pending work to be done for this ring and requests an
1803 * interrupt when there is space available in the ring. This function is
1804 * called when the driver is unable to post more iocbs to the ring due
1805 * to unavailability of space in the ring.
1806 **/
1807static void
1808lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1809{
1810        int ringno = pring->ringno;
1811
1812        pring->flag |= LPFC_CALL_RING_AVAILABLE;
1813
1814        wmb();
1815
1816        /*
1817         * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1818         * The HBA will tell us when an IOCB entry is available.
1819         */
1820        writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1821        readl(phba->CAregaddr); /* flush */
1822
1823        pring->stats.iocb_cmd_full++;
1824}
1825
1826/**
1827 * lpfc_sli_update_ring - Update chip attention register
1828 * @phba: Pointer to HBA context object.
1829 * @pring: Pointer to driver SLI ring object.
1830 *
1831 * This function updates the chip attention register bit for the
1832 * given ring to inform HBA that there is more work to be done
1833 * in this ring. The caller is not required to hold any lock.
1834 **/
1835static void
1836lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1837{
1838        int ringno = pring->ringno;
1839
1840        /*
1841         * Tell the HBA that there is work to do in this ring.
1842         */
1843        if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1844                wmb();
1845                writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1846                readl(phba->CAregaddr); /* flush */
1847        }
1848}
1849
1850/**
1851 * lpfc_sli_resume_iocb - Process iocbs in the txq
1852 * @phba: Pointer to HBA context object.
1853 * @pring: Pointer to driver SLI ring object.
1854 *
1855 * This function is called with hbalock held to post pending iocbs
1856 * in the txq to the firmware. This function is called when driver
1857 * detects space available in the ring.
1858 **/
1859static void
1860lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1861{
1862        IOCB_t *iocb;
1863        struct lpfc_iocbq *nextiocb;
1864
1865        lockdep_assert_held(&phba->hbalock);
1866
1867        /*
1868         * Check to see if:
1869         *  (a) there is anything on the txq to send
1870         *  (b) link is up
1871         *  (c) link attention events can be processed (fcp ring only)
1872         *  (d) IOCB processing is not blocked by the outstanding mbox command.
1873         */
1874
1875        if (lpfc_is_link_up(phba) &&
1876            (!list_empty(&pring->txq)) &&
1877            (pring->ringno != LPFC_FCP_RING ||
1878             phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1879
1880                while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1881                       (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1882                        lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1883
1884                if (iocb)
1885                        lpfc_sli_update_ring(phba, pring);
1886                else
1887                        lpfc_sli_update_full_ring(phba, pring);
1888        }
1889
1890        return;
1891}
1892
1893/**
1894 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1895 * @phba: Pointer to HBA context object.
1896 * @hbqno: HBQ number.
1897 *
1898 * This function is called with hbalock held to get the next
1899 * available slot for the given HBQ. If there is free slot
1900 * available for the HBQ it will return pointer to the next available
1901 * HBQ entry else it will return NULL.
1902 **/
1903static struct lpfc_hbq_entry *
1904lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1905{
1906        struct hbq_s *hbqp = &phba->hbqs[hbqno];
1907
1908        lockdep_assert_held(&phba->hbalock);
1909
1910        if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1911            ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1912                hbqp->next_hbqPutIdx = 0;
1913
1914        if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1915                uint32_t raw_index = phba->hbq_get[hbqno];
1916                uint32_t getidx = le32_to_cpu(raw_index);
1917
1918                hbqp->local_hbqGetIdx = getidx;
1919
1920                if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1921                        lpfc_printf_log(phba, KERN_ERR,
1922                                        LOG_SLI | LOG_VPORT,
1923                                        "1802 HBQ %d: local_hbqGetIdx "
1924                                        "%u is > than hbqp->entry_count %u\n",
1925                                        hbqno, hbqp->local_hbqGetIdx,
1926                                        hbqp->entry_count);
1927
1928                        phba->link_state = LPFC_HBA_ERROR;
1929                        return NULL;
1930                }
1931
1932                if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1933                        return NULL;
1934        }
1935
1936        return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1937                        hbqp->hbqPutIdx;
1938}
1939
1940/**
1941 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1942 * @phba: Pointer to HBA context object.
1943 *
1944 * This function is called with no lock held to free all the
1945 * hbq buffers while uninitializing the SLI interface. It also
1946 * frees the HBQ buffers returned by the firmware but not yet
1947 * processed by the upper layers.
1948 **/
1949void
1950lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1951{
1952        struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1953        struct hbq_dmabuf *hbq_buf;
1954        unsigned long flags;
1955        int i, hbq_count;
1956
1957        hbq_count = lpfc_sli_hbq_count();
1958        /* Return all memory used by all HBQs */
1959        spin_lock_irqsave(&phba->hbalock, flags);
1960        for (i = 0; i < hbq_count; ++i) {
1961                list_for_each_entry_safe(dmabuf, next_dmabuf,
1962                                &phba->hbqs[i].hbq_buffer_list, list) {
1963                        hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1964                        list_del(&hbq_buf->dbuf.list);
1965                        (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1966                }
1967                phba->hbqs[i].buffer_count = 0;
1968        }
1969
1970        /* Mark the HBQs not in use */
1971        phba->hbq_in_use = 0;
1972        spin_unlock_irqrestore(&phba->hbalock, flags);
1973}
1974
1975/**
1976 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1977 * @phba: Pointer to HBA context object.
1978 * @hbqno: HBQ number.
1979 * @hbq_buf: Pointer to HBQ buffer.
1980 *
1981 * This function is called with the hbalock held to post a
1982 * hbq buffer to the firmware. If the function finds an empty
1983 * slot in the HBQ, it will post the buffer. The function will return
1984 * pointer to the hbq entry if it successfully post the buffer
1985 * else it will return NULL.
1986 **/
1987static int
1988lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1989                         struct hbq_dmabuf *hbq_buf)
1990{
1991        lockdep_assert_held(&phba->hbalock);
1992        return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1993}
1994
1995/**
1996 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1997 * @phba: Pointer to HBA context object.
1998 * @hbqno: HBQ number.
1999 * @hbq_buf: Pointer to HBQ buffer.
2000 *
2001 * This function is called with the hbalock held to post a hbq buffer to the
2002 * firmware. If the function finds an empty slot in the HBQ, it will post the
2003 * buffer and place it on the hbq_buffer_list. The function will return zero if
2004 * it successfully post the buffer else it will return an error.
2005 **/
2006static int
2007lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2008                            struct hbq_dmabuf *hbq_buf)
2009{
2010        struct lpfc_hbq_entry *hbqe;
2011        dma_addr_t physaddr = hbq_buf->dbuf.phys;
2012
2013        lockdep_assert_held(&phba->hbalock);
2014        /* Get next HBQ entry slot to use */
2015        hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2016        if (hbqe) {
2017                struct hbq_s *hbqp = &phba->hbqs[hbqno];
2018
2019                hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2020                hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
2021                hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
2022                hbqe->bde.tus.f.bdeFlags = 0;
2023                hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2024                hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2025                                /* Sync SLIM */
2026                hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2027                writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
2028                                /* flush */
2029                readl(phba->hbq_put + hbqno);
2030                list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
2031                return 0;
2032        } else
2033                return -ENOMEM;
2034}
2035
2036/**
2037 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2038 * @phba: Pointer to HBA context object.
2039 * @hbqno: HBQ number.
2040 * @hbq_buf: Pointer to HBQ buffer.
2041 *
2042 * This function is called with the hbalock held to post an RQE to the SLI4
2043 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2044 * the hbq_buffer_list and return zero, otherwise it will return an error.
2045 **/
2046static int
2047lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2048                            struct hbq_dmabuf *hbq_buf)
2049{
2050        int rc;
2051        struct lpfc_rqe hrqe;
2052        struct lpfc_rqe drqe;
2053        struct lpfc_queue *hrq;
2054        struct lpfc_queue *drq;
2055
2056        if (hbqno != LPFC_ELS_HBQ)
2057                return 1;
2058        hrq = phba->sli4_hba.hdr_rq;
2059        drq = phba->sli4_hba.dat_rq;
2060
2061        lockdep_assert_held(&phba->hbalock);
2062        hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2063        hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2064        drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2065        drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
2066        rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
2067        if (rc < 0)
2068                return rc;
2069        hbq_buf->tag = (rc | (hbqno << 16));
2070        list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2071        return 0;
2072}
2073
2074/* HBQ for ELS and CT traffic. */
2075static struct lpfc_hbq_init lpfc_els_hbq = {
2076        .rn = 1,
2077        .entry_count = 256,
2078        .mask_count = 0,
2079        .profile = 0,
2080        .ring_mask = (1 << LPFC_ELS_RING),
2081        .buffer_count = 0,
2082        .init_count = 40,
2083        .add_count = 40,
2084};
2085
2086/* Array of HBQs */
2087struct lpfc_hbq_init *lpfc_hbq_defs[] = {
2088        &lpfc_els_hbq,
2089};
2090
2091/**
2092 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
2093 * @phba: Pointer to HBA context object.
2094 * @hbqno: HBQ number.
2095 * @count: Number of HBQ buffers to be posted.
2096 *
2097 * This function is called with no lock held to post more hbq buffers to the
2098 * given HBQ. The function returns the number of HBQ buffers successfully
2099 * posted.
2100 **/
2101static int
2102lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2103{
2104        uint32_t i, posted = 0;
2105        unsigned long flags;
2106        struct hbq_dmabuf *hbq_buffer;
2107        LIST_HEAD(hbq_buf_list);
2108        if (!phba->hbqs[hbqno].hbq_alloc_buffer)
2109                return 0;
2110
2111        if ((phba->hbqs[hbqno].buffer_count + count) >
2112            lpfc_hbq_defs[hbqno]->entry_count)
2113                count = lpfc_hbq_defs[hbqno]->entry_count -
2114                                        phba->hbqs[hbqno].buffer_count;
2115        if (!count)
2116                return 0;
2117        /* Allocate HBQ entries */
2118        for (i = 0; i < count; i++) {
2119                hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2120                if (!hbq_buffer)
2121                        break;
2122                list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2123        }
2124        /* Check whether HBQ is still in use */
2125        spin_lock_irqsave(&phba->hbalock, flags);
2126        if (!phba->hbq_in_use)
2127                goto err;
2128        while (!list_empty(&hbq_buf_list)) {
2129                list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2130                                 dbuf.list);
2131                hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2132                                      (hbqno << 16));
2133                if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
2134                        phba->hbqs[hbqno].buffer_count++;
2135                        posted++;
2136                } else
2137                        (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2138        }
2139        spin_unlock_irqrestore(&phba->hbalock, flags);
2140        return posted;
2141err:
2142        spin_unlock_irqrestore(&phba->hbalock, flags);
2143        while (!list_empty(&hbq_buf_list)) {
2144                list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2145                                 dbuf.list);
2146                (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2147        }
2148        return 0;
2149}
2150
2151/**
2152 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
2153 * @phba: Pointer to HBA context object.
2154 * @qno: HBQ number.
2155 *
2156 * This function posts more buffers to the HBQ. This function
2157 * is called with no lock held. The function returns the number of HBQ entries
2158 * successfully allocated.
2159 **/
2160int
2161lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
2162{
2163        if (phba->sli_rev == LPFC_SLI_REV4)
2164                return 0;
2165        else
2166                return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2167                                         lpfc_hbq_defs[qno]->add_count);
2168}
2169
2170/**
2171 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
2172 * @phba: Pointer to HBA context object.
2173 * @qno:  HBQ queue number.
2174 *
2175 * This function is called from SLI initialization code path with
2176 * no lock held to post initial HBQ buffers to firmware. The
2177 * function returns the number of HBQ entries successfully allocated.
2178 **/
2179static int
2180lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
2181{
2182        if (phba->sli_rev == LPFC_SLI_REV4)
2183                return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2184                                        lpfc_hbq_defs[qno]->entry_count);
2185        else
2186                return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2187                                         lpfc_hbq_defs[qno]->init_count);
2188}
2189
2190/**
2191 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2192 * @phba: Pointer to HBA context object.
2193 * @hbqno: HBQ number.
2194 *
2195 * This function removes the first hbq buffer on an hbq list and returns a
2196 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2197 **/
2198static struct hbq_dmabuf *
2199lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2200{
2201        struct lpfc_dmabuf *d_buf;
2202
2203        list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2204        if (!d_buf)
2205                return NULL;
2206        return container_of(d_buf, struct hbq_dmabuf, dbuf);
2207}
2208
2209/**
2210 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2211 * @phba: Pointer to HBA context object.
2212 * @hbqno: HBQ number.
2213 *
2214 * This function removes the first RQ buffer on an RQ buffer list and returns a
2215 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2216 **/
2217static struct rqb_dmabuf *
2218lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2219{
2220        struct lpfc_dmabuf *h_buf;
2221        struct lpfc_rqb *rqbp;
2222
2223        rqbp = hrq->rqbp;
2224        list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2225                         struct lpfc_dmabuf, list);
2226        if (!h_buf)
2227                return NULL;
2228        rqbp->buffer_count--;
2229        return container_of(h_buf, struct rqb_dmabuf, hbuf);
2230}
2231
2232/**
2233 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
2234 * @phba: Pointer to HBA context object.
2235 * @tag: Tag of the hbq buffer.
2236 *
2237 * This function searches for the hbq buffer associated with the given tag in
2238 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2239 * otherwise it returns NULL.
2240 **/
2241static struct hbq_dmabuf *
2242lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2243{
2244        struct lpfc_dmabuf *d_buf;
2245        struct hbq_dmabuf *hbq_buf;
2246        uint32_t hbqno;
2247
2248        hbqno = tag >> 16;
2249        if (hbqno >= LPFC_MAX_HBQS)
2250                return NULL;
2251
2252        spin_lock_irq(&phba->hbalock);
2253        list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2254                hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2255                if (hbq_buf->tag == tag) {
2256                        spin_unlock_irq(&phba->hbalock);
2257                        return hbq_buf;
2258                }
2259        }
2260        spin_unlock_irq(&phba->hbalock);
2261        lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
2262                        "1803 Bad hbq tag. Data: x%x x%x\n",
2263                        tag, phba->hbqs[tag >> 16].buffer_count);
2264        return NULL;
2265}
2266
2267/**
2268 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2269 * @phba: Pointer to HBA context object.
2270 * @hbq_buffer: Pointer to HBQ buffer.
2271 *
2272 * This function is called with hbalock. This function gives back
2273 * the hbq buffer to firmware. If the HBQ does not have space to
2274 * post the buffer, it will free the buffer.
2275 **/
2276void
2277lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2278{
2279        uint32_t hbqno;
2280
2281        if (hbq_buffer) {
2282                hbqno = hbq_buffer->tag >> 16;
2283                if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2284                        (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2285        }
2286}
2287
2288/**
2289 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2290 * @mbxCommand: mailbox command code.
2291 *
2292 * This function is called by the mailbox event handler function to verify
2293 * that the completed mailbox command is a legitimate mailbox command. If the
2294 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2295 * and the mailbox event handler will take the HBA offline.
2296 **/
2297static int
2298lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2299{
2300        uint8_t ret;
2301
2302        switch (mbxCommand) {
2303        case MBX_LOAD_SM:
2304        case MBX_READ_NV:
2305        case MBX_WRITE_NV:
2306        case MBX_WRITE_VPARMS:
2307        case MBX_RUN_BIU_DIAG:
2308        case MBX_INIT_LINK:
2309        case MBX_DOWN_LINK:
2310        case MBX_CONFIG_LINK:
2311        case MBX_CONFIG_RING:
2312        case MBX_RESET_RING:
2313        case MBX_READ_CONFIG:
2314        case MBX_READ_RCONFIG:
2315        case MBX_READ_SPARM:
2316        case MBX_READ_STATUS:
2317        case MBX_READ_RPI:
2318        case MBX_READ_XRI:
2319        case MBX_READ_REV:
2320        case MBX_READ_LNK_STAT:
2321        case MBX_REG_LOGIN:
2322        case MBX_UNREG_LOGIN:
2323        case MBX_CLEAR_LA:
2324        case MBX_DUMP_MEMORY:
2325        case MBX_DUMP_CONTEXT:
2326        case MBX_RUN_DIAGS:
2327        case MBX_RESTART:
2328        case MBX_UPDATE_CFG:
2329        case MBX_DOWN_LOAD:
2330        case MBX_DEL_LD_ENTRY:
2331        case MBX_RUN_PROGRAM:
2332        case MBX_SET_MASK:
2333        case MBX_SET_VARIABLE:
2334        case MBX_UNREG_D_ID:
2335        case MBX_KILL_BOARD:
2336        case MBX_CONFIG_FARP:
2337        case MBX_BEACON:
2338        case MBX_LOAD_AREA:
2339        case MBX_RUN_BIU_DIAG64:
2340        case MBX_CONFIG_PORT:
2341        case MBX_READ_SPARM64:
2342        case MBX_READ_RPI64:
2343        case MBX_REG_LOGIN64:
2344        case MBX_READ_TOPOLOGY:
2345        case MBX_WRITE_WWN:
2346        case MBX_SET_DEBUG:
2347        case MBX_LOAD_EXP_ROM:
2348        case MBX_ASYNCEVT_ENABLE:
2349        case MBX_REG_VPI:
2350        case MBX_UNREG_VPI:
2351        case MBX_HEARTBEAT:
2352        case MBX_PORT_CAPABILITIES:
2353        case MBX_PORT_IOV_CONTROL:
2354        case MBX_SLI4_CONFIG:
2355        case MBX_SLI4_REQ_FTRS:
2356        case MBX_REG_FCFI:
2357        case MBX_UNREG_FCFI:
2358        case MBX_REG_VFI:
2359        case MBX_UNREG_VFI:
2360        case MBX_INIT_VPI:
2361        case MBX_INIT_VFI:
2362        case MBX_RESUME_RPI:
2363        case MBX_READ_EVENT_LOG_STATUS:
2364        case MBX_READ_EVENT_LOG:
2365        case MBX_SECURITY_MGMT:
2366        case MBX_AUTH_PORT:
2367        case MBX_ACCESS_VDATA:
2368                ret = mbxCommand;
2369                break;
2370        default:
2371                ret = MBX_SHUTDOWN;
2372                break;
2373        }
2374        return ret;
2375}
2376
2377/**
2378 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2379 * @phba: Pointer to HBA context object.
2380 * @pmboxq: Pointer to mailbox command.
2381 *
2382 * This is completion handler function for mailbox commands issued from
2383 * lpfc_sli_issue_mbox_wait function. This function is called by the
2384 * mailbox event handler function with no lock held. This function
2385 * will wake up thread waiting on the wait queue pointed by context1
2386 * of the mailbox.
2387 **/
2388void
2389lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2390{
2391        unsigned long drvr_flag;
2392        struct completion *pmbox_done;
2393
2394        /*
2395         * If pmbox_done is empty, the driver thread gave up waiting and
2396         * continued running.
2397         */
2398        pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2399        spin_lock_irqsave(&phba->hbalock, drvr_flag);
2400        pmbox_done = (struct completion *)pmboxq->context3;
2401        if (pmbox_done)
2402                complete(pmbox_done);
2403        spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2404        return;
2405}
2406
2407
2408/**
2409 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2410 * @phba: Pointer to HBA context object.
2411 * @pmb: Pointer to mailbox object.
2412 *
2413 * This function is the default mailbox completion handler. It
2414 * frees the memory resources associated with the completed mailbox
2415 * command. If the completed command is a REG_LOGIN mailbox command,
2416 * this function will issue a UREG_LOGIN to re-claim the RPI.
2417 **/
2418void
2419lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2420{
2421        struct lpfc_vport  *vport = pmb->vport;
2422        struct lpfc_dmabuf *mp;
2423        struct lpfc_nodelist *ndlp;
2424        struct Scsi_Host *shost;
2425        uint16_t rpi, vpi;
2426        int rc;
2427
2428        mp = (struct lpfc_dmabuf *) (pmb->context1);
2429
2430        if (mp) {
2431                lpfc_mbuf_free(phba, mp->virt, mp->phys);
2432                kfree(mp);
2433        }
2434
2435        /*
2436         * If a REG_LOGIN succeeded  after node is destroyed or node
2437         * is in re-discovery driver need to cleanup the RPI.
2438         */
2439        if (!(phba->pport->load_flag & FC_UNLOADING) &&
2440            pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2441            !pmb->u.mb.mbxStatus) {
2442                rpi = pmb->u.mb.un.varWords[0];
2443                vpi = pmb->u.mb.un.varRegLogin.vpi;
2444                lpfc_unreg_login(phba, vpi, rpi, pmb);
2445                pmb->vport = vport;
2446                pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2447                rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2448                if (rc != MBX_NOT_FINISHED)
2449                        return;
2450        }
2451
2452        if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2453                !(phba->pport->load_flag & FC_UNLOADING) &&
2454                !pmb->u.mb.mbxStatus) {
2455                shost = lpfc_shost_from_vport(vport);
2456                spin_lock_irq(shost->host_lock);
2457                vport->vpi_state |= LPFC_VPI_REGISTERED;
2458                vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2459                spin_unlock_irq(shost->host_lock);
2460        }
2461
2462        if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2463                ndlp = (struct lpfc_nodelist *)pmb->context2;
2464                lpfc_nlp_put(ndlp);
2465                pmb->context2 = NULL;
2466        }
2467
2468        /* Check security permission status on INIT_LINK mailbox command */
2469        if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2470            (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2471                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2472                                "2860 SLI authentication is required "
2473                                "for INIT_LINK but has not done yet\n");
2474
2475        if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2476                lpfc_sli4_mbox_cmd_free(phba, pmb);
2477        else
2478                mempool_free(pmb, phba->mbox_mem_pool);
2479}
2480 /**
2481 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2482 * @phba: Pointer to HBA context object.
2483 * @pmb: Pointer to mailbox object.
2484 *
2485 * This function is the unreg rpi mailbox completion handler. It
2486 * frees the memory resources associated with the completed mailbox
2487 * command. An additional refrenece is put on the ndlp to prevent
2488 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2489 * the unreg mailbox command completes, this routine puts the
2490 * reference back.
2491 *
2492 **/
2493void
2494lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2495{
2496        struct lpfc_vport  *vport = pmb->vport;
2497        struct lpfc_nodelist *ndlp;
2498
2499        ndlp = pmb->context1;
2500        if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2501                if (phba->sli_rev == LPFC_SLI_REV4 &&
2502                    (bf_get(lpfc_sli_intf_if_type,
2503                     &phba->sli4_hba.sli_intf) >=
2504                     LPFC_SLI_INTF_IF_TYPE_2)) {
2505                        if (ndlp) {
2506                                lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2507                                                 "0010 UNREG_LOGIN vpi:%x "
2508                                                 "rpi:%x DID:%x map:%x %p\n",
2509                                                 vport->vpi, ndlp->nlp_rpi,
2510                                                 ndlp->nlp_DID,
2511                                                 ndlp->nlp_usg_map, ndlp);
2512                                ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2513                                lpfc_nlp_put(ndlp);
2514                        }
2515                }
2516        }
2517
2518        mempool_free(pmb, phba->mbox_mem_pool);
2519}
2520
2521/**
2522 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2523 * @phba: Pointer to HBA context object.
2524 *
2525 * This function is called with no lock held. This function processes all
2526 * the completed mailbox commands and gives it to upper layers. The interrupt
2527 * service routine processes mailbox completion interrupt and adds completed
2528 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2529 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2530 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2531 * function returns the mailbox commands to the upper layer by calling the
2532 * completion handler function of each mailbox.
2533 **/
2534int
2535lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2536{
2537        MAILBOX_t *pmbox;
2538        LPFC_MBOXQ_t *pmb;
2539        int rc;
2540        LIST_HEAD(cmplq);
2541
2542        phba->sli.slistat.mbox_event++;
2543
2544        /* Get all completed mailboxe buffers into the cmplq */
2545        spin_lock_irq(&phba->hbalock);
2546        list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2547        spin_unlock_irq(&phba->hbalock);
2548
2549        /* Get a Mailbox buffer to setup mailbox commands for callback */
2550        do {
2551                list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2552                if (pmb == NULL)
2553                        break;
2554
2555                pmbox = &pmb->u.mb;
2556
2557                if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2558                        if (pmb->vport) {
2559                                lpfc_debugfs_disc_trc(pmb->vport,
2560                                        LPFC_DISC_TRC_MBOX_VPORT,
2561                                        "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2562                                        (uint32_t)pmbox->mbxCommand,
2563                                        pmbox->un.varWords[0],
2564                                        pmbox->un.varWords[1]);
2565                        }
2566                        else {
2567                                lpfc_debugfs_disc_trc(phba->pport,
2568                                        LPFC_DISC_TRC_MBOX,
2569                                        "MBOX cmpl:       cmd:x%x mb:x%x x%x",
2570                                        (uint32_t)pmbox->mbxCommand,
2571                                        pmbox->un.varWords[0],
2572                                        pmbox->un.varWords[1]);
2573                        }
2574                }
2575
2576                /*
2577                 * It is a fatal error if unknown mbox command completion.
2578                 */
2579                if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2580                    MBX_SHUTDOWN) {
2581                        /* Unknown mailbox command compl */
2582                        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2583                                        "(%d):0323 Unknown Mailbox command "
2584                                        "x%x (x%x/x%x) Cmpl\n",
2585                                        pmb->vport ? pmb->vport->vpi : 0,
2586                                        pmbox->mbxCommand,
2587                                        lpfc_sli_config_mbox_subsys_get(phba,
2588                                                                        pmb),
2589                                        lpfc_sli_config_mbox_opcode_get(phba,
2590                                                                        pmb));
2591                        phba->link_state = LPFC_HBA_ERROR;
2592                        phba->work_hs = HS_FFER3;
2593                        lpfc_handle_eratt(phba);
2594                        continue;
2595                }
2596
2597                if (pmbox->mbxStatus) {
2598                        phba->sli.slistat.mbox_stat_err++;
2599                        if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2600                                /* Mbox cmd cmpl error - RETRYing */
2601                                lpfc_printf_log(phba, KERN_INFO,
2602                                        LOG_MBOX | LOG_SLI,
2603                                        "(%d):0305 Mbox cmd cmpl "
2604                                        "error - RETRYing Data: x%x "
2605                                        "(x%x/x%x) x%x x%x x%x\n",
2606                                        pmb->vport ? pmb->vport->vpi : 0,
2607                                        pmbox->mbxCommand,
2608                                        lpfc_sli_config_mbox_subsys_get(phba,
2609                                                                        pmb),
2610                                        lpfc_sli_config_mbox_opcode_get(phba,
2611                                                                        pmb),
2612                                        pmbox->mbxStatus,
2613                                        pmbox->un.varWords[0],
2614                                        pmb->vport->port_state);
2615                                pmbox->mbxStatus = 0;
2616                                pmbox->mbxOwner = OWN_HOST;
2617                                rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2618                                if (rc != MBX_NOT_FINISHED)
2619                                        continue;
2620                        }
2621                }
2622
2623                /* Mailbox cmd <cmd> Cmpl <cmpl> */
2624                lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2625                                "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2626                                "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2627                                "x%x x%x x%x\n",
2628                                pmb->vport ? pmb->vport->vpi : 0,
2629                                pmbox->mbxCommand,
2630                                lpfc_sli_config_mbox_subsys_get(phba, pmb),
2631                                lpfc_sli_config_mbox_opcode_get(phba, pmb),
2632                                pmb->mbox_cmpl,
2633                                *((uint32_t *) pmbox),
2634                                pmbox->un.varWords[0],
2635                                pmbox->un.varWords[1],
2636                                pmbox->un.varWords[2],
2637                                pmbox->un.varWords[3],
2638                                pmbox->un.varWords[4],
2639                                pmbox->un.varWords[5],
2640                                pmbox->un.varWords[6],
2641                                pmbox->un.varWords[7],
2642                                pmbox->un.varWords[8],
2643                                pmbox->un.varWords[9],
2644                                pmbox->un.varWords[10]);
2645
2646                if (pmb->mbox_cmpl)
2647                        pmb->mbox_cmpl(phba,pmb);
2648        } while (1);
2649        return 0;
2650}
2651
2652/**
2653 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2654 * @phba: Pointer to HBA context object.
2655 * @pring: Pointer to driver SLI ring object.
2656 * @tag: buffer tag.
2657 *
2658 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2659 * is set in the tag the buffer is posted for a particular exchange,
2660 * the function will return the buffer without replacing the buffer.
2661 * If the buffer is for unsolicited ELS or CT traffic, this function
2662 * returns the buffer and also posts another buffer to the firmware.
2663 **/
2664static struct lpfc_dmabuf *
2665lpfc_sli_get_buff(struct lpfc_hba *phba,
2666                  struct lpfc_sli_ring *pring,
2667                  uint32_t tag)
2668{
2669        struct hbq_dmabuf *hbq_entry;
2670
2671        if (tag & QUE_BUFTAG_BIT)
2672                return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2673        hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2674        if (!hbq_entry)
2675                return NULL;
2676        return &hbq_entry->dbuf;
2677}
2678
2679/**
2680 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2681 * @phba: Pointer to HBA context object.
2682 * @pring: Pointer to driver SLI ring object.
2683 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2684 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2685 * @fch_type: the type for the first frame of the sequence.
2686 *
2687 * This function is called with no lock held. This function uses the r_ctl and
2688 * type of the received sequence to find the correct callback function to call
2689 * to process the sequence.
2690 **/
2691static int
2692lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2693                         struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2694                         uint32_t fch_type)
2695{
2696        int i;
2697
2698        switch (fch_type) {
2699        case FC_TYPE_NVME:
2700                lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
2701                return 1;
2702        default:
2703                break;
2704        }
2705
2706        /* unSolicited Responses */
2707        if (pring->prt[0].profile) {
2708                if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2709                        (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2710                                                                        saveq);
2711                return 1;
2712        }
2713        /* We must search, based on rctl / type
2714           for the right routine */
2715        for (i = 0; i < pring->num_mask; i++) {
2716                if ((pring->prt[i].rctl == fch_r_ctl) &&
2717                    (pring->prt[i].type == fch_type)) {
2718                        if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2719                                (pring->prt[i].lpfc_sli_rcv_unsol_event)
2720                                                (phba, pring, saveq);
2721                        return 1;
2722                }
2723        }
2724        return 0;
2725}
2726
2727/**
2728 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2729 * @phba: Pointer to HBA context object.
2730 * @pring: Pointer to driver SLI ring object.
2731 * @saveq: Pointer to the unsolicited iocb.
2732 *
2733 * This function is called with no lock held by the ring event handler
2734 * when there is an unsolicited iocb posted to the response ring by the
2735 * firmware. This function gets the buffer associated with the iocbs
2736 * and calls the event handler for the ring. This function handles both
2737 * qring buffers and hbq buffers.
2738 * When the function returns 1 the caller can free the iocb object otherwise
2739 * upper layer functions will free the iocb objects.
2740 **/
2741static int
2742lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2743                            struct lpfc_iocbq *saveq)
2744{
2745        IOCB_t           * irsp;
2746        WORD5            * w5p;
2747        uint32_t           Rctl, Type;
2748        struct lpfc_iocbq *iocbq;
2749        struct lpfc_dmabuf *dmzbuf;
2750
2751        irsp = &(saveq->iocb);
2752
2753        if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2754                if (pring->lpfc_sli_rcv_async_status)
2755                        pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2756                else
2757                        lpfc_printf_log(phba,
2758                                        KERN_WARNING,
2759                                        LOG_SLI,
2760                                        "0316 Ring %d handler: unexpected "
2761                                        "ASYNC_STATUS iocb received evt_code "
2762                                        "0x%x\n",
2763                                        pring->ringno,
2764                                        irsp->un.asyncstat.evt_code);
2765                return 1;
2766        }
2767
2768        if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2769                (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2770                if (irsp->ulpBdeCount > 0) {
2771                        dmzbuf = lpfc_sli_get_buff(phba, pring,
2772                                        irsp->un.ulpWord[3]);
2773                        lpfc_in_buf_free(phba, dmzbuf);
2774                }
2775
2776                if (irsp->ulpBdeCount > 1) {
2777                        dmzbuf = lpfc_sli_get_buff(phba, pring,
2778                                        irsp->unsli3.sli3Words[3]);
2779                        lpfc_in_buf_free(phba, dmzbuf);
2780                }
2781
2782                if (irsp->ulpBdeCount > 2) {
2783                        dmzbuf = lpfc_sli_get_buff(phba, pring,
2784                                irsp->unsli3.sli3Words[7]);
2785                        lpfc_in_buf_free(phba, dmzbuf);
2786                }
2787
2788                return 1;
2789        }
2790
2791        if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2792                if (irsp->ulpBdeCount != 0) {
2793                        saveq->context2 = lpfc_sli_get_buff(phba, pring,
2794                                                irsp->un.ulpWord[3]);
2795                        if (!saveq->context2)
2796                                lpfc_printf_log(phba,
2797                                        KERN_ERR,
2798                                        LOG_SLI,
2799                                        "0341 Ring %d Cannot find buffer for "
2800                                        "an unsolicited iocb. tag 0x%x\n",
2801                                        pring->ringno,
2802                                        irsp->un.ulpWord[3]);
2803                }
2804                if (irsp->ulpBdeCount == 2) {
2805                        saveq->context3 = lpfc_sli_get_buff(phba, pring,
2806                                                irsp->unsli3.sli3Words[7]);
2807                        if (!saveq->context3)
2808                                lpfc_printf_log(phba,
2809                                        KERN_ERR,
2810                                        LOG_SLI,
2811                                        "0342 Ring %d Cannot find buffer for an"
2812                                        " unsolicited iocb. tag 0x%x\n",
2813                                        pring->ringno,
2814                                        irsp->unsli3.sli3Words[7]);
2815                }
2816                list_for_each_entry(iocbq, &saveq->list, list) {
2817                        irsp = &(iocbq->iocb);
2818                        if (irsp->ulpBdeCount != 0) {
2819                                iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2820                                                        irsp->un.ulpWord[3]);
2821                                if (!iocbq->context2)
2822                                        lpfc_printf_log(phba,
2823                                                KERN_ERR,
2824                                                LOG_SLI,
2825                                                "0343 Ring %d Cannot find "
2826                                                "buffer for an unsolicited iocb"
2827                                                ". tag 0x%x\n", pring->ringno,
2828                                                irsp->un.ulpWord[3]);
2829                        }
2830                        if (irsp->ulpBdeCount == 2) {
2831                                iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2832                                                irsp->unsli3.sli3Words[7]);
2833                                if (!iocbq->context3)
2834                                        lpfc_printf_log(phba,
2835                                                KERN_ERR,
2836                                                LOG_SLI,
2837                                                "0344 Ring %d Cannot find "
2838                                                "buffer for an unsolicited "
2839                                                "iocb. tag 0x%x\n",
2840                                                pring->ringno,
2841                                                irsp->unsli3.sli3Words[7]);
2842                        }
2843                }
2844        }
2845        if (irsp->ulpBdeCount != 0 &&
2846            (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2847             irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2848                int found = 0;
2849
2850                /* search continue save q for same XRI */
2851                list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2852                        if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2853                                saveq->iocb.unsli3.rcvsli3.ox_id) {
2854                                list_add_tail(&saveq->list, &iocbq->list);
2855                                found = 1;
2856                                break;
2857                        }
2858                }
2859                if (!found)
2860                        list_add_tail(&saveq->clist,
2861                                      &pring->iocb_continue_saveq);
2862                if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2863                        list_del_init(&iocbq->clist);
2864                        saveq = iocbq;
2865                        irsp = &(saveq->iocb);
2866                } else
2867                        return 0;
2868        }
2869        if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2870            (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2871            (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2872                Rctl = FC_RCTL_ELS_REQ;
2873                Type = FC_TYPE_ELS;
2874        } else {
2875                w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2876                Rctl = w5p->hcsw.Rctl;
2877                Type = w5p->hcsw.Type;
2878
2879                /* Firmware Workaround */
2880                if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2881                        (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2882                         irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2883                        Rctl = FC_RCTL_ELS_REQ;
2884                        Type = FC_TYPE_ELS;
2885                        w5p->hcsw.Rctl = Rctl;
2886                        w5p->hcsw.Type = Type;
2887                }
2888        }
2889
2890        if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2891                lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2892                                "0313 Ring %d handler: unexpected Rctl x%x "
2893                                "Type x%x received\n",
2894                                pring->ringno, Rctl, Type);
2895
2896        return 1;
2897}
2898
2899/**
2900 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2901 * @phba: Pointer to HBA context object.
2902 * @pring: Pointer to driver SLI ring object.
2903 * @prspiocb: Pointer to response iocb object.
2904 *
2905 * This function looks up the iocb_lookup table to get the command iocb
2906 * corresponding to the given response iocb using the iotag of the
2907 * response iocb. This function is called with the hbalock held
2908 * for sli3 devices or the ring_lock for sli4 devices.
2909 * This function returns the command iocb object if it finds the command
2910 * iocb else returns NULL.
2911 **/
2912static struct lpfc_iocbq *
2913lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2914                      struct lpfc_sli_ring *pring,
2915                      struct lpfc_iocbq *prspiocb)
2916{
2917        struct lpfc_iocbq *cmd_iocb = NULL;
2918        uint16_t iotag;
2919        lockdep_assert_held(&phba->hbalock);
2920
2921        iotag = prspiocb->iocb.ulpIoTag;
2922
2923        if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2924                cmd_iocb = phba->sli.iocbq_lookup[iotag];
2925                if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2926                        /* remove from txcmpl queue list */
2927                        list_del_init(&cmd_iocb->list);
2928                        cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2929                        return cmd_iocb;
2930                }
2931        }
2932
2933        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2934                        "0317 iotag x%x is out of "
2935                        "range: max iotag x%x wd0 x%x\n",
2936                        iotag, phba->sli.last_iotag,
2937                        *(((uint32_t *) &prspiocb->iocb) + 7));
2938        return NULL;
2939}
2940
2941/**
2942 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2943 * @phba: Pointer to HBA context object.
2944 * @pring: Pointer to driver SLI ring object.
2945 * @iotag: IOCB tag.
2946 *
2947 * This function looks up the iocb_lookup table to get the command iocb
2948 * corresponding to the given iotag. This function is called with the
2949 * hbalock held.
2950 * This function returns the command iocb object if it finds the command
2951 * iocb else returns NULL.
2952 **/
2953static struct lpfc_iocbq *
2954lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2955                             struct lpfc_sli_ring *pring, uint16_t iotag)
2956{
2957        struct lpfc_iocbq *cmd_iocb = NULL;
2958
2959        lockdep_assert_held(&phba->hbalock);
2960        if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2961                cmd_iocb = phba->sli.iocbq_lookup[iotag];
2962                if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2963                        /* remove from txcmpl queue list */
2964                        list_del_init(&cmd_iocb->list);
2965                        cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2966                        return cmd_iocb;
2967                }
2968        }
2969
2970        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2971                        "0372 iotag x%x lookup error: max iotag (x%x) "
2972                        "iocb_flag x%x\n",
2973                        iotag, phba->sli.last_iotag,
2974                        cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
2975        return NULL;
2976}
2977
2978/**
2979 * lpfc_sli_process_sol_iocb - process solicited iocb completion
2980 * @phba: Pointer to HBA context object.
2981 * @pring: Pointer to driver SLI ring object.
2982 * @saveq: Pointer to the response iocb to be processed.
2983 *
2984 * This function is called by the ring event handler for non-fcp
2985 * rings when there is a new response iocb in the response ring.
2986 * The caller is not required to hold any locks. This function
2987 * gets the command iocb associated with the response iocb and
2988 * calls the completion handler for the command iocb. If there
2989 * is no completion handler, the function will free the resources
2990 * associated with command iocb. If the response iocb is for
2991 * an already aborted command iocb, the status of the completion
2992 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2993 * This function always returns 1.
2994 **/
2995static int
2996lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2997                          struct lpfc_iocbq *saveq)
2998{
2999        struct lpfc_iocbq *cmdiocbp;
3000        int rc = 1;
3001        unsigned long iflag;
3002
3003        /* Based on the iotag field, get the cmd IOCB from the txcmplq */
3004        if (phba->sli_rev == LPFC_SLI_REV4)
3005                spin_lock_irqsave(&pring->ring_lock, iflag);
3006        else
3007                spin_lock_irqsave(&phba->hbalock, iflag);
3008        cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
3009        if (phba->sli_rev == LPFC_SLI_REV4)
3010                spin_unlock_irqrestore(&pring->ring_lock, iflag);
3011        else
3012                spin_unlock_irqrestore(&phba->hbalock, iflag);
3013
3014        if (cmdiocbp) {
3015                if (cmdiocbp->iocb_cmpl) {
3016                        /*
3017                         * If an ELS command failed send an event to mgmt
3018                         * application.
3019                         */
3020                        if (saveq->iocb.ulpStatus &&
3021                             (pring->ringno == LPFC_ELS_RING) &&
3022                             (cmdiocbp->iocb.ulpCommand ==
3023                                CMD_ELS_REQUEST64_CR))
3024                                lpfc_send_els_failure_event(phba,
3025                                        cmdiocbp, saveq);
3026
3027                        /*
3028                         * Post all ELS completions to the worker thread.
3029                         * All other are passed to the completion callback.
3030                         */
3031                        if (pring->ringno == LPFC_ELS_RING) {
3032                                if ((phba->sli_rev < LPFC_SLI_REV4) &&
3033                                    (cmdiocbp->iocb_flag &
3034                                                        LPFC_DRIVER_ABORTED)) {
3035                                        spin_lock_irqsave(&phba->hbalock,
3036                                                          iflag);
3037                                        cmdiocbp->iocb_flag &=
3038                                                ~LPFC_DRIVER_ABORTED;
3039                                        spin_unlock_irqrestore(&phba->hbalock,
3040                                                               iflag);
3041                                        saveq->iocb.ulpStatus =
3042                                                IOSTAT_LOCAL_REJECT;
3043                                        saveq->iocb.un.ulpWord[4] =
3044                                                IOERR_SLI_ABORTED;
3045
3046                                        /* Firmware could still be in progress
3047                                         * of DMAing payload, so don't free data
3048                                         * buffer till after a hbeat.
3049                                         */
3050                                        spin_lock_irqsave(&phba->hbalock,
3051                                                          iflag);
3052                                        saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
3053                                        spin_unlock_irqrestore(&phba->hbalock,
3054                                                               iflag);
3055                                }
3056                                if (phba->sli_rev == LPFC_SLI_REV4) {
3057                                        if (saveq->iocb_flag &
3058                                            LPFC_EXCHANGE_BUSY) {
3059                                                /* Set cmdiocb flag for the
3060                                                 * exchange busy so sgl (xri)
3061                                                 * will not be released until
3062                                                 * the abort xri is received
3063                                                 * from hba.
3064                                                 */
3065                                                spin_lock_irqsave(
3066                                                        &phba->hbalock, iflag);
3067                                                cmdiocbp->iocb_flag |=
3068                                                        LPFC_EXCHANGE_BUSY;
3069                                                spin_unlock_irqrestore(
3070                                                        &phba->hbalock, iflag);
3071                                        }
3072                                        if (cmdiocbp->iocb_flag &
3073                                            LPFC_DRIVER_ABORTED) {
3074                                                /*
3075                                                 * Clear LPFC_DRIVER_ABORTED
3076                                                 * bit in case it was driver
3077                                                 * initiated abort.
3078                                                 */
3079                                                spin_lock_irqsave(
3080                                                        &phba->hbalock, iflag);
3081                                                cmdiocbp->iocb_flag &=
3082                                                        ~LPFC_DRIVER_ABORTED;
3083                                                spin_unlock_irqrestore(
3084                                                        &phba->hbalock, iflag);
3085                                                cmdiocbp->iocb.ulpStatus =
3086                                                        IOSTAT_LOCAL_REJECT;
3087                                                cmdiocbp->iocb.un.ulpWord[4] =
3088                                                        IOERR_ABORT_REQUESTED;
3089                                                /*
3090                                                 * For SLI4, irsiocb contains
3091                                                 * NO_XRI in sli_xritag, it
3092                                                 * shall not affect releasing
3093                                                 * sgl (xri) process.
3094                                                 */
3095                                                saveq->iocb.ulpStatus =
3096                                                        IOSTAT_LOCAL_REJECT;
3097                                                saveq->iocb.un.ulpWord[4] =
3098                                                        IOERR_SLI_ABORTED;
3099                                                spin_lock_irqsave(
3100                                                        &phba->hbalock, iflag);
3101                                                saveq->iocb_flag |=
3102                                                        LPFC_DELAY_MEM_FREE;
3103                                                spin_unlock_irqrestore(
3104                                                        &phba->hbalock, iflag);
3105                                        }
3106                                }
3107                        }
3108                        (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
3109                } else
3110                        lpfc_sli_release_iocbq(phba, cmdiocbp);
3111        } else {
3112                /*
3113                 * Unknown initiating command based on the response iotag.
3114                 * This could be the case on the ELS ring because of
3115                 * lpfc_els_abort().
3116                 */
3117                if (pring->ringno != LPFC_ELS_RING) {
3118                        /*
3119                         * Ring <ringno> handler: unexpected completion IoTag
3120                         * <IoTag>
3121                         */
3122                        lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3123                                         "0322 Ring %d handler: "
3124                                         "unexpected completion IoTag x%x "
3125                                         "Data: x%x x%x x%x x%x\n",
3126                                         pring->ringno,
3127                                         saveq->iocb.ulpIoTag,
3128                                         saveq->iocb.ulpStatus,
3129                                         saveq->iocb.un.ulpWord[4],
3130                                         saveq->iocb.ulpCommand,
3131                                         saveq->iocb.ulpContext);
3132                }
3133        }
3134
3135        return rc;
3136}
3137
3138/**
3139 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
3140 * @phba: Pointer to HBA context object.
3141 * @pring: Pointer to driver SLI ring object.
3142 *
3143 * This function is called from the iocb ring event handlers when
3144 * put pointer is ahead of the get pointer for a ring. This function signal
3145 * an error attention condition to the worker thread and the worker
3146 * thread will transition the HBA to offline state.
3147 **/
3148static void
3149lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3150{
3151        struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3152        /*
3153         * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3154         * rsp ring <portRspMax>
3155         */
3156        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3157                        "0312 Ring %d handler: portRspPut %d "
3158                        "is bigger than rsp ring %d\n",
3159                        pring->ringno, le32_to_cpu(pgp->rspPutInx),
3160                        pring->sli.sli3.numRiocb);
3161
3162        phba->link_state = LPFC_HBA_ERROR;
3163
3164        /*
3165         * All error attention handlers are posted to
3166         * worker thread
3167         */
3168        phba->work_ha |= HA_ERATT;
3169        phba->work_hs = HS_FFER3;
3170
3171        lpfc_worker_wake_up(phba);
3172
3173        return;
3174}
3175
3176/**
3177 * lpfc_poll_eratt - Error attention polling timer timeout handler
3178 * @ptr: Pointer to address of HBA context object.
3179 *
3180 * This function is invoked by the Error Attention polling timer when the
3181 * timer times out. It will check the SLI Error Attention register for
3182 * possible attention events. If so, it will post an Error Attention event
3183 * and wake up worker thread to process it. Otherwise, it will set up the
3184 * Error Attention polling timer for the next poll.
3185 **/
3186void lpfc_poll_eratt(struct timer_list *t)
3187{
3188        struct lpfc_hba *phba;
3189        uint32_t eratt = 0;
3190        uint64_t sli_intr, cnt;
3191
3192        phba = from_timer(phba, t, eratt_poll);
3193
3194        /* Here we will also keep track of interrupts per sec of the hba */
3195        sli_intr = phba->sli.slistat.sli_intr;
3196
3197        if (phba->sli.slistat.sli_prev_intr > sli_intr)
3198                cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3199                        sli_intr);
3200        else
3201                cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3202
3203        /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3204        do_div(cnt, phba->eratt_poll_interval);
3205        phba->sli.slistat.sli_ips = cnt;
3206
3207        phba->sli.slistat.sli_prev_intr = sli_intr;
3208
3209        /* Check chip HA register for error event */
3210        eratt = lpfc_sli_check_eratt(phba);
3211
3212        if (eratt)
3213                /* Tell the worker thread there is work to do */
3214                lpfc_worker_wake_up(phba);
3215        else
3216                /* Restart the timer for next eratt poll */
3217                mod_timer(&phba->eratt_poll,
3218                          jiffies +
3219                          msecs_to_jiffies(1000 * phba->eratt_poll_interval));
3220        return;
3221}
3222
3223
3224/**
3225 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
3226 * @phba: Pointer to HBA context object.
3227 * @pring: Pointer to driver SLI ring object.
3228 * @mask: Host attention register mask for this ring.
3229 *
3230 * This function is called from the interrupt context when there is a ring
3231 * event for the fcp ring. The caller does not hold any lock.
3232 * The function processes each response iocb in the response ring until it
3233 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
3234 * LE bit set. The function will call the completion handler of the command iocb
3235 * if the response iocb indicates a completion for a command iocb or it is
3236 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3237 * function if this is an unsolicited iocb.
3238 * This routine presumes LPFC_FCP_RING handling and doesn't bother
3239 * to check it explicitly.
3240 */
3241int
3242lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3243                                struct lpfc_sli_ring *pring, uint32_t mask)
3244{
3245        struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
3246        IOCB_t *irsp = NULL;
3247        IOCB_t *entry = NULL;
3248        struct lpfc_iocbq *cmdiocbq = NULL;
3249        struct lpfc_iocbq rspiocbq;
3250        uint32_t status;
3251        uint32_t portRspPut, portRspMax;
3252        int rc = 1;
3253        lpfc_iocb_type type;
3254        unsigned long iflag;
3255        uint32_t rsp_cmpl = 0;
3256
3257        spin_lock_irqsave(&phba->hbalock, iflag);
3258        pring->stats.iocb_event++;
3259
3260        /*
3261         * The next available response entry should never exceed the maximum
3262         * entries.  If it does, treat it as an adapter hardware error.
3263         */
3264        portRspMax = pring->sli.sli3.numRiocb;
3265        portRspPut = le32_to_cpu(pgp->rspPutInx);
3266        if (unlikely(portRspPut >= portRspMax)) {
3267                lpfc_sli_rsp_pointers_error(phba, pring);
3268                spin_unlock_irqrestore(&phba->hbalock, iflag);
3269                return 1;
3270        }
3271        if (phba->fcp_ring_in_use) {
3272                spin_unlock_irqrestore(&phba->hbalock, iflag);
3273                return 1;
3274        } else
3275                phba->fcp_ring_in_use = 1;
3276
3277        rmb();
3278        while (pring->sli.sli3.rspidx != portRspPut) {
3279                /*
3280                 * Fetch an entry off the ring and copy it into a local data
3281                 * structure.  The copy involves a byte-swap since the
3282                 * network byte order and pci byte orders are different.
3283                 */
3284                entry = lpfc_resp_iocb(phba, pring);
3285                phba->last_completion_time = jiffies;
3286
3287                if (++pring->sli.sli3.rspidx >= portRspMax)
3288                        pring->sli.sli3.rspidx = 0;
3289
3290                lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3291                                      (uint32_t *) &rspiocbq.iocb,
3292                                      phba->iocb_rsp_size);
3293                INIT_LIST_HEAD(&(rspiocbq.list));
3294                irsp = &rspiocbq.iocb;
3295
3296                type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3297                pring->stats.iocb_rsp++;
3298                rsp_cmpl++;
3299
3300                if (unlikely(irsp->ulpStatus)) {
3301                        /*
3302                         * If resource errors reported from HBA, reduce
3303                         * queuedepths of the SCSI device.
3304                         */
3305                        if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3306                            ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3307                             IOERR_NO_RESOURCES)) {
3308                                spin_unlock_irqrestore(&phba->hbalock, iflag);
3309                                phba->lpfc_rampdown_queue_depth(phba);
3310                                spin_lock_irqsave(&phba->hbalock, iflag);
3311                        }
3312
3313                        /* Rsp ring <ringno> error: IOCB */
3314                        lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3315                                        "0336 Rsp Ring %d error: IOCB Data: "
3316                                        "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3317                                        pring->ringno,
3318                                        irsp->un.ulpWord[0],
3319                                        irsp->un.ulpWord[1],
3320                                        irsp->un.ulpWord[2],
3321                                        irsp->un.ulpWord[3],
3322                                        irsp->un.ulpWord[4],
3323                                        irsp->un.ulpWord[5],
3324                                        *(uint32_t *)&irsp->un1,
3325                                        *((uint32_t *)&irsp->un1 + 1));
3326                }
3327
3328                switch (type) {
3329                case LPFC_ABORT_IOCB:
3330                case LPFC_SOL_IOCB:
3331                        /*
3332                         * Idle exchange closed via ABTS from port.  No iocb
3333                         * resources need to be recovered.
3334                         */
3335                        if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3336                                lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3337                                                "0333 IOCB cmd 0x%x"
3338                                                " processed. Skipping"
3339                                                " completion\n",
3340                                                irsp->ulpCommand);
3341                                break;
3342                        }
3343
3344                        cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3345                                                         &rspiocbq);
3346                        if (unlikely(!cmdiocbq))
3347                                break;
3348                        if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3349                                cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3350                        if (cmdiocbq->iocb_cmpl) {
3351                                spin_unlock_irqrestore(&phba->hbalock, iflag);
3352                                (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3353                                                      &rspiocbq);
3354                                spin_lock_irqsave(&phba->hbalock, iflag);
3355                        }
3356                        break;
3357                case LPFC_UNSOL_IOCB:
3358                        spin_unlock_irqrestore(&phba->hbalock, iflag);
3359                        lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3360                        spin_lock_irqsave(&phba->hbalock, iflag);
3361                        break;
3362                default:
3363                        if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3364                                char adaptermsg[LPFC_MAX_ADPTMSG];
3365                                memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3366                                memcpy(&adaptermsg[0], (uint8_t *) irsp,
3367                                       MAX_MSG_DATA);
3368                                dev_warn(&((phba->pcidev)->dev),
3369                                         "lpfc%d: %s\n",
3370                                         phba->brd_no, adaptermsg);
3371                        } else {
3372                                /* Unknown IOCB command */
3373                                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3374                                                "0334 Unknown IOCB command "
3375                                                "Data: x%x, x%x x%x x%x x%x\n",
3376                                                type, irsp->ulpCommand,
3377                                                irsp->ulpStatus,
3378                                                irsp->ulpIoTag,
3379                                                irsp->ulpContext);
3380                        }
3381                        break;
3382                }
3383
3384                /*
3385                 * The response IOCB has been processed.  Update the ring
3386                 * pointer in SLIM.  If the port response put pointer has not
3387                 * been updated, sync the pgp->rspPutInx and fetch the new port
3388                 * response put pointer.
3389                 */
3390                writel(pring->sli.sli3.rspidx,
3391                        &phba->host_gp[pring->ringno].rspGetInx);
3392
3393                if (pring->sli.sli3.rspidx == portRspPut)
3394                        portRspPut = le32_to_cpu(pgp->rspPutInx);
3395        }
3396
3397        if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3398                pring->stats.iocb_rsp_full++;
3399                status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3400                writel(status, phba->CAregaddr);
3401                readl(phba->CAregaddr);
3402        }
3403        if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3404                pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3405                pring->stats.iocb_cmd_empty++;
3406
3407                /* Force update of the local copy of cmdGetInx */
3408                pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3409                lpfc_sli_resume_iocb(phba, pring);
3410
3411                if ((pring->lpfc_sli_cmd_available))
3412                        (pring->lpfc_sli_cmd_available) (phba, pring);
3413
3414        }
3415
3416        phba->fcp_ring_in_use = 0;
3417        spin_unlock_irqrestore(&phba->hbalock, iflag);
3418        return rc;
3419}
3420
3421/**
3422 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3423 * @phba: Pointer to HBA context object.
3424 * @pring: Pointer to driver SLI ring object.
3425 * @rspiocbp: Pointer to driver response IOCB object.
3426 *
3427 * This function is called from the worker thread when there is a slow-path
3428 * response IOCB to process. This function chains all the response iocbs until
3429 * seeing the iocb with the LE bit set. The function will call
3430 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3431 * completion of a command iocb. The function will call the
3432 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3433 * The function frees the resources or calls the completion handler if this
3434 * iocb is an abort completion. The function returns NULL when the response
3435 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3436 * this function shall chain the iocb on to the iocb_continueq and return the
3437 * response iocb passed in.
3438 **/
3439static struct lpfc_iocbq *
3440lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3441                        struct lpfc_iocbq *rspiocbp)
3442{
3443        struct lpfc_iocbq *saveq;
3444        struct lpfc_iocbq *cmdiocbp;
3445        struct lpfc_iocbq *next_iocb;
3446        IOCB_t *irsp = NULL;
3447        uint32_t free_saveq;
3448        uint8_t iocb_cmd_type;
3449        lpfc_iocb_type type;
3450        unsigned long iflag;
3451        int rc;
3452
3453        spin_lock_irqsave(&phba->hbalock, iflag);
3454        /* First add the response iocb to the countinueq list */
3455        list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3456        pring->iocb_continueq_cnt++;
3457
3458        /* Now, determine whether the list is completed for processing */
3459        irsp = &rspiocbp->iocb;
3460        if (irsp->ulpLe) {
3461                /*
3462                 * By default, the driver expects to free all resources
3463                 * associated with this iocb completion.
3464                 */
3465                free_saveq = 1;
3466                saveq = list_get_first(&pring->iocb_continueq,
3467                                       struct lpfc_iocbq, list);
3468                irsp = &(saveq->iocb);
3469                list_del_init(&pring->iocb_continueq);
3470                pring->iocb_continueq_cnt = 0;
3471
3472                pring->stats.iocb_rsp++;
3473
3474                /*
3475                 * If resource errors reported from HBA, reduce
3476                 * queuedepths of the SCSI device.
3477                 */
3478                if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3479                    ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3480                     IOERR_NO_RESOURCES)) {
3481                        spin_unlock_irqrestore(&phba->hbalock, iflag);
3482                        phba->lpfc_rampdown_queue_depth(phba);
3483                        spin_lock_irqsave(&phba->hbalock, iflag);
3484                }
3485
3486                if (irsp->ulpStatus) {
3487                        /* Rsp ring <ringno> error: IOCB */
3488                        lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3489                                        "0328 Rsp Ring %d error: "
3490                                        "IOCB Data: "
3491                                        "x%x x%x x%x x%x "
3492                                        "x%x x%x x%x x%x "
3493                                        "x%x x%x x%x x%x "
3494                                        "x%x x%x x%x x%x\n",
3495                                        pring->ringno,
3496                                        irsp->un.ulpWord[0],
3497                                        irsp->un.ulpWord[1],
3498                                        irsp->un.ulpWord[2],
3499                                        irsp->un.ulpWord[3],
3500                                        irsp->un.ulpWord[4],
3501                                        irsp->un.ulpWord[5],
3502                                        *(((uint32_t *) irsp) + 6),
3503                                        *(((uint32_t *) irsp) + 7),
3504                                        *(((uint32_t *) irsp) + 8),
3505                                        *(((uint32_t *) irsp) + 9),
3506                                        *(((uint32_t *) irsp) + 10),
3507                                        *(((uint32_t *) irsp) + 11),
3508                                        *(((uint32_t *) irsp) + 12),
3509                                        *(((uint32_t *) irsp) + 13),
3510                                        *(((uint32_t *) irsp) + 14),
3511                                        *(((uint32_t *) irsp) + 15));
3512                }
3513
3514                /*
3515                 * Fetch the IOCB command type and call the correct completion
3516                 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3517                 * get freed back to the lpfc_iocb_list by the discovery
3518                 * kernel thread.
3519                 */
3520                iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3521                type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3522                switch (type) {
3523                case LPFC_SOL_IOCB:
3524                        spin_unlock_irqrestore(&phba->hbalock, iflag);
3525                        rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3526                        spin_lock_irqsave(&phba->hbalock, iflag);
3527                        break;
3528
3529                case LPFC_UNSOL_IOCB:
3530                        spin_unlock_irqrestore(&phba->hbalock, iflag);
3531                        rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3532                        spin_lock_irqsave(&phba->hbalock, iflag);
3533                        if (!rc)
3534                                free_saveq = 0;
3535                        break;
3536
3537                case LPFC_ABORT_IOCB:
3538                        cmdiocbp = NULL;
3539                        if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3540                                cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3541                                                                 saveq);
3542                        if (cmdiocbp) {
3543                                /* Call the specified completion routine */
3544                                if (cmdiocbp->iocb_cmpl) {
3545                                        spin_unlock_irqrestore(&phba->hbalock,
3546                                                               iflag);
3547                                        (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3548                                                              saveq);
3549                                        spin_lock_irqsave(&phba->hbalock,
3550                                                          iflag);
3551                                } else
3552                                        __lpfc_sli_release_iocbq(phba,
3553                                                                 cmdiocbp);
3554                        }
3555                        break;
3556
3557                case LPFC_UNKNOWN_IOCB:
3558                        if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3559                                char adaptermsg[LPFC_MAX_ADPTMSG];
3560                                memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3561                                memcpy(&adaptermsg[0], (uint8_t *)irsp,
3562                                       MAX_MSG_DATA);
3563                                dev_warn(&((phba->pcidev)->dev),
3564                                         "lpfc%d: %s\n",
3565                                         phba->brd_no, adaptermsg);
3566                        } else {
3567                                /* Unknown IOCB command */
3568                                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3569                                                "0335 Unknown IOCB "
3570                                                "command Data: x%x "
3571                                                "x%x x%x x%x\n",
3572                                                irsp->ulpCommand,
3573                                                irsp->ulpStatus,
3574                                                irsp->ulpIoTag,
3575                                                irsp->ulpContext);
3576                        }
3577                        break;
3578                }
3579
3580                if (free_saveq) {
3581                        list_for_each_entry_safe(rspiocbp, next_iocb,
3582                                                 &saveq->list, list) {
3583                                list_del_init(&rspiocbp->list);
3584                                __lpfc_sli_release_iocbq(phba, rspiocbp);
3585                        }
3586                        __lpfc_sli_release_iocbq(phba, saveq);
3587                }
3588                rspiocbp = NULL;
3589        }
3590        spin_unlock_irqrestore(&phba->hbalock, iflag);
3591        return rspiocbp;
3592}
3593
3594/**
3595 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3596 * @phba: Pointer to HBA context object.
3597 * @pring: Pointer to driver SLI ring object.
3598 * @mask: Host attention register mask for this ring.
3599 *
3600 * This routine wraps the actual slow_ring event process routine from the
3601 * API jump table function pointer from the lpfc_hba struct.
3602 **/
3603void
3604lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3605                                struct lpfc_sli_ring *pring, uint32_t mask)
3606{
3607        phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3608}
3609
3610/**
3611 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3612 * @phba: Pointer to HBA context object.
3613 * @pring: Pointer to driver SLI ring object.
3614 * @mask: Host attention register mask for this ring.
3615 *
3616 * This function is called from the worker thread when there is a ring event
3617 * for non-fcp rings. The caller does not hold any lock. The function will
3618 * remove each response iocb in the response ring and calls the handle
3619 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3620 **/
3621static void
3622lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3623                                   struct lpfc_sli_ring *pring, uint32_t mask)
3624{
3625        struct lpfc_pgp *pgp;
3626        IOCB_t *entry;
3627        IOCB_t *irsp = NULL;
3628        struct lpfc_iocbq *rspiocbp = NULL;
3629        uint32_t portRspPut, portRspMax;
3630        unsigned long iflag;
3631        uint32_t status;
3632
3633        pgp = &phba->port_gp[pring->ringno];
3634        spin_lock_irqsave(&phba->hbalock, iflag);
3635        pring->stats.iocb_event++;
3636
3637        /*
3638         * The next available response entry should never exceed the maximum
3639         * entries.  If it does, treat it as an adapter hardware error.
3640         */
3641        portRspMax = pring->sli.sli3.numRiocb;
3642        portRspPut = le32_to_cpu(pgp->rspPutInx);
3643        if (portRspPut >= portRspMax) {
3644                /*
3645                 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3646                 * rsp ring <portRspMax>
3647                 */
3648                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3649                                "0303 Ring %d handler: portRspPut %d "
3650                                "is bigger than rsp ring %d\n",
3651                                pring->ringno, portRspPut, portRspMax);
3652
3653                phba->link_state = LPFC_HBA_ERROR;
3654                spin_unlock_irqrestore(&phba->hbalock, iflag);
3655
3656                phba->work_hs = HS_FFER3;
3657                lpfc_handle_eratt(phba);
3658
3659                return;
3660        }
3661
3662        rmb();
3663        while (pring->sli.sli3.rspidx != portRspPut) {
3664                /*
3665                 * Build a completion list and call the appropriate handler.
3666                 * The process is to get the next available response iocb, get
3667                 * a free iocb from the list, copy the response data into the
3668                 * free iocb, insert to the continuation list, and update the
3669                 * next response index to slim.  This process makes response
3670                 * iocb's in the ring available to DMA as fast as possible but
3671                 * pays a penalty for a copy operation.  Since the iocb is
3672                 * only 32 bytes, this penalty is considered small relative to
3673                 * the PCI reads for register values and a slim write.  When
3674                 * the ulpLe field is set, the entire Command has been
3675                 * received.
3676                 */
3677                entry = lpfc_resp_iocb(phba, pring);
3678
3679                phba->last_completion_time = jiffies;
3680                rspiocbp = __lpfc_sli_get_iocbq(phba);
3681                if (rspiocbp == NULL) {
3682                        printk(KERN_ERR "%s: out of buffers! Failing "
3683                               "completion.\n", __func__);
3684                        break;
3685                }
3686
3687                lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3688                                      phba->iocb_rsp_size);
3689                irsp = &rspiocbp->iocb;
3690
3691                if (++pring->sli.sli3.rspidx >= portRspMax)
3692                        pring->sli.sli3.rspidx = 0;
3693
3694                if (pring->ringno == LPFC_ELS_RING) {
3695                        lpfc_debugfs_slow_ring_trc(phba,
3696                        "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
3697                                *(((uint32_t *) irsp) + 4),
3698                                *(((uint32_t *) irsp) + 6),
3699                                *(((uint32_t *) irsp) + 7));
3700                }
3701
3702                writel(pring->sli.sli3.rspidx,
3703                        &phba->host_gp[pring->ringno].rspGetInx);
3704
3705                spin_unlock_irqrestore(&phba->hbalock, iflag);
3706                /* Handle the response IOCB */
3707                rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3708                spin_lock_irqsave(&phba->hbalock, iflag);
3709
3710                /*
3711                 * If the port response put pointer has not been updated, sync
3712                 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3713                 * response put pointer.
3714                 */
3715                if (pring->sli.sli3.rspidx == portRspPut) {
3716                        portRspPut = le32_to_cpu(pgp->rspPutInx);
3717                }
3718        } /* while (pring->sli.sli3.rspidx != portRspPut) */
3719
3720        if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3721                /* At least one response entry has been freed */
3722                pring->stats.iocb_rsp_full++;
3723                /* SET RxRE_RSP in Chip Att register */
3724                status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3725                writel(status, phba->CAregaddr);
3726                readl(phba->CAregaddr); /* flush */
3727        }
3728        if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3729                pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3730                pring->stats.iocb_cmd_empty++;
3731
3732                /* Force update of the local copy of cmdGetInx */
3733                pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3734                lpfc_sli_resume_iocb(phba, pring);
3735
3736                if ((pring->lpfc_sli_cmd_available))
3737                        (pring->lpfc_sli_cmd_available) (phba, pring);
3738
3739        }
3740
3741        spin_unlock_irqrestore(&phba->hbalock, iflag);
3742        return;
3743}
3744
3745/**
3746 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3747 * @phba: Pointer to HBA context object.
3748 * @pring: Pointer to driver SLI ring object.
3749 * @mask: Host attention register mask for this ring.
3750 *
3751 * This function is called from the worker thread when there is a pending
3752 * ELS response iocb on the driver internal slow-path response iocb worker
3753 * queue. The caller does not hold any lock. The function will remove each
3754 * response iocb from the response worker queue and calls the handle
3755 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3756 **/
3757static void
3758lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3759                                   struct lpfc_sli_ring *pring, uint32_t mask)
3760{
3761        struct lpfc_iocbq *irspiocbq;
3762        struct hbq_dmabuf *dmabuf;
3763        struct lpfc_cq_event *cq_event;
3764        unsigned long iflag;
3765
3766        spin_lock_irqsave(&phba->hbalock, iflag);
3767        phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3768        spin_unlock_irqrestore(&phba->hbalock, iflag);
3769        while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3770                /* Get the response iocb from the head of work queue */
3771                spin_lock_irqsave(&phba->hbalock, iflag);
3772                list_remove_head(&phba->sli4_hba.sp_queue_event,
3773                                 cq_event, struct lpfc_cq_event, list);
3774                spin_unlock_irqrestore(&phba->hbalock, iflag);
3775
3776                switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3777                case CQE_CODE_COMPL_WQE:
3778                        irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3779                                                 cq_event);
3780                        /* Translate ELS WCQE to response IOCBQ */
3781                        irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3782                                                                   irspiocbq);
3783                        if (irspiocbq)
3784                                lpfc_sli_sp_handle_rspiocb(phba, pring,
3785                                                           irspiocbq);
3786                        break;
3787                case CQE_CODE_RECEIVE:
3788                case CQE_CODE_RECEIVE_V1:
3789                        dmabuf = container_of(cq_event, struct hbq_dmabuf,
3790                                              cq_event);
3791                        lpfc_sli4_handle_received_buffer(phba, dmabuf);
3792                        break;
3793                default:
3794                        break;
3795                }
3796        }
3797}
3798
3799/**
3800 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3801 * @phba: Pointer to HBA context object.
3802 * @pring: Pointer to driver SLI ring object.
3803 *
3804 * This function aborts all iocbs in the given ring and frees all the iocb
3805 * objects in txq. This function issues an abort iocb for all the iocb commands
3806 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3807 * the return of this function. The caller is not required to hold any locks.
3808 **/
3809void
3810lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3811{
3812        LIST_HEAD(completions);
3813        struct lpfc_iocbq *iocb, *next_iocb;
3814
3815        if (pring->ringno == LPFC_ELS_RING) {
3816                lpfc_fabric_abort_hba(phba);
3817        }
3818
3819        /* Error everything on txq and txcmplq
3820         * First do the txq.
3821         */
3822        if (phba->sli_rev >= LPFC_SLI_REV4) {
3823                spin_lock_irq(&pring->ring_lock);
3824                list_splice_init(&pring->txq, &completions);
3825                pring->txq_cnt = 0;
3826                spin_unlock_irq(&pring->ring_lock);
3827
3828                spin_lock_irq(&phba->hbalock);
3829                /* Next issue ABTS for everything on the txcmplq */
3830                list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3831                        lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3832                spin_unlock_irq(&phba->hbalock);
3833        } else {
3834                spin_lock_irq(&phba->hbalock);
3835                list_splice_init(&pring->txq, &completions);
3836                pring->txq_cnt = 0;
3837
3838                /* Next issue ABTS for everything on the txcmplq */
3839                list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3840                        lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3841                spin_unlock_irq(&phba->hbalock);
3842        }
3843
3844        /* Cancel all the IOCBs from the completions list */
3845        lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3846                              IOERR_SLI_ABORTED);
3847}
3848
3849/**
3850 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3851 * @phba: Pointer to HBA context object.
3852 * @pring: Pointer to driver SLI ring object.
3853 *
3854 * This function aborts all iocbs in the given ring and frees all the iocb
3855 * objects in txq. This function issues an abort iocb for all the iocb commands
3856 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3857 * the return of this function. The caller is not required to hold any locks.
3858 **/
3859void
3860lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3861{
3862        LIST_HEAD(completions);
3863        struct lpfc_iocbq *iocb, *next_iocb;
3864
3865        if (pring->ringno == LPFC_ELS_RING)
3866                lpfc_fabric_abort_hba(phba);
3867
3868        spin_lock_irq(&phba->hbalock);
3869        /* Next issue ABTS for everything on the txcmplq */
3870        list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3871                lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3872        spin_unlock_irq(&phba->hbalock);
3873}
3874
3875
3876/**
3877 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3878 * @phba: Pointer to HBA context object.
3879 * @pring: Pointer to driver SLI ring object.
3880 *
3881 * This function aborts all iocbs in FCP rings and frees all the iocb
3882 * objects in txq. This function issues an abort iocb for all the iocb commands
3883 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3884 * the return of this function. The caller is not required to hold any locks.
3885 **/
3886void
3887lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3888{
3889        struct lpfc_sli *psli = &phba->sli;
3890        struct lpfc_sli_ring  *pring;
3891        uint32_t i;
3892
3893        /* Look on all the FCP Rings for the iotag */
3894        if (phba->sli_rev >= LPFC_SLI_REV4) {
3895                for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3896                        pring = phba->sli4_hba.fcp_wq[i]->pring;
3897                        lpfc_sli_abort_iocb_ring(phba, pring);
3898                }
3899        } else {
3900                pring = &psli->sli3_ring[LPFC_FCP_RING];
3901                lpfc_sli_abort_iocb_ring(phba, pring);
3902        }
3903}
3904
3905/**
3906 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3907 * @phba: Pointer to HBA context object.
3908 *
3909 * This function aborts all wqes in NVME rings. This function issues an
3910 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3911 * the txcmplq is not guaranteed to complete before the return of this
3912 * function. The caller is not required to hold any locks.
3913 **/
3914void
3915lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3916{
3917        struct lpfc_sli_ring  *pring;
3918        uint32_t i;
3919
3920        if (phba->sli_rev < LPFC_SLI_REV4)
3921                return;
3922
3923        /* Abort all IO on each NVME ring. */
3924        for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3925                pring = phba->sli4_hba.nvme_wq[i]->pring;
3926                lpfc_sli_abort_wqe_ring(phba, pring);
3927        }
3928}
3929
3930
3931/**
3932 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3933 * @phba: Pointer to HBA context object.
3934 *
3935 * This function flushes all iocbs in the fcp ring and frees all the iocb
3936 * objects in txq and txcmplq. This function will not issue abort iocbs
3937 * for all the iocb commands in txcmplq, they will just be returned with
3938 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3939 * slot has been permanently disabled.
3940 **/
3941void
3942lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3943{
3944        LIST_HEAD(txq);
3945        LIST_HEAD(txcmplq);
3946        struct lpfc_sli *psli = &phba->sli;
3947        struct lpfc_sli_ring  *pring;
3948        uint32_t i;
3949        struct lpfc_iocbq *piocb, *next_iocb;
3950
3951        spin_lock_irq(&phba->hbalock);
3952        /* Indicate the I/O queues are flushed */
3953        phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
3954        spin_unlock_irq(&phba->hbalock);
3955
3956        /* Look on all the FCP Rings for the iotag */
3957        if (phba->sli_rev >= LPFC_SLI_REV4) {
3958                for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3959                        pring = phba->sli4_hba.fcp_wq[i]->pring;
3960
3961                        spin_lock_irq(&pring->ring_lock);
3962                        /* Retrieve everything on txq */
3963                        list_splice_init(&pring->txq, &txq);
3964                        list_for_each_entry_safe(piocb, next_iocb,
3965                                                 &pring->txcmplq, list)
3966                                piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3967                        /* Retrieve everything on the txcmplq */
3968                        list_splice_init(&pring->txcmplq, &txcmplq);
3969                        pring->txq_cnt = 0;
3970                        pring->txcmplq_cnt = 0;
3971                        spin_unlock_irq(&pring->ring_lock);
3972
3973                        /* Flush the txq */
3974                        lpfc_sli_cancel_iocbs(phba, &txq,
3975                                              IOSTAT_LOCAL_REJECT,
3976                                              IOERR_SLI_DOWN);
3977                        /* Flush the txcmpq */
3978                        lpfc_sli_cancel_iocbs(phba, &txcmplq,
3979                                              IOSTAT_LOCAL_REJECT,
3980                                              IOERR_SLI_DOWN);
3981                }
3982        } else {
3983                pring = &psli->sli3_ring[LPFC_FCP_RING];
3984
3985                spin_lock_irq(&phba->hbalock);
3986                /* Retrieve everything on txq */
3987                list_splice_init(&pring->txq, &txq);
3988                list_for_each_entry_safe(piocb, next_iocb,
3989                                         &pring->txcmplq, list)
3990                        piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3991                /* Retrieve everything on the txcmplq */
3992                list_splice_init(&pring->txcmplq, &txcmplq);
3993                pring->txq_cnt = 0;
3994                pring->txcmplq_cnt = 0;
3995                spin_unlock_irq(&phba->hbalock);
3996
3997                /* Flush the txq */
3998                lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3999                                      IOERR_SLI_DOWN);
4000                /* Flush the txcmpq */
4001                lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4002                                      IOERR_SLI_DOWN);
4003        }
4004}
4005
4006/**
4007 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
4008 * @phba: Pointer to HBA context object.
4009 *
4010 * This function flushes all wqes in the nvme rings and frees all resources
4011 * in the txcmplq. This function does not issue abort wqes for the IO
4012 * commands in txcmplq, they will just be returned with
4013 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4014 * slot has been permanently disabled.
4015 **/
4016void
4017lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
4018{
4019        LIST_HEAD(txcmplq);
4020        struct lpfc_sli_ring  *pring;
4021        uint32_t i;
4022        struct lpfc_iocbq *piocb, *next_iocb;
4023
4024        if (phba->sli_rev < LPFC_SLI_REV4)
4025                return;
4026
4027        /* Hint to other driver operations that a flush is in progress. */
4028        spin_lock_irq(&phba->hbalock);
4029        phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
4030        spin_unlock_irq(&phba->hbalock);
4031
4032        /* Cycle through all NVME rings and complete each IO with
4033         * a local driver reason code.  This is a flush so no
4034         * abort exchange to FW.
4035         */
4036        for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
4037                pring = phba->sli4_hba.nvme_wq[i]->pring;
4038
4039                spin_lock_irq(&pring->ring_lock);
4040                list_for_each_entry_safe(piocb, next_iocb,
4041                                         &pring->txcmplq, list)
4042                        piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
4043                /* Retrieve everything on the txcmplq */
4044                list_splice_init(&pring->txcmplq, &txcmplq);
4045                pring->txcmplq_cnt = 0;
4046                spin_unlock_irq(&pring->ring_lock);
4047
4048                /* Flush the txcmpq &&&PAE */
4049                lpfc_sli_cancel_iocbs(phba, &txcmplq,
4050                                      IOSTAT_LOCAL_REJECT,
4051                                      IOERR_SLI_DOWN);
4052        }
4053}
4054
4055/**
4056 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
4057 * @phba: Pointer to HBA context object.
4058 * @mask: Bit mask to be checked.
4059 *
4060 * This function reads the host status register and compares
4061 * with the provided bit mask to check if HBA completed
4062 * the restart. This function will wait in a loop for the
4063 * HBA to complete restart. If the HBA does not restart within
4064 * 15 iterations, the function will reset the HBA again. The
4065 * function returns 1 when HBA fail to restart otherwise returns
4066 * zero.
4067 **/
4068static int
4069lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
4070{
4071        uint32_t status;
4072        int i = 0;
4073        int retval = 0;
4074
4075        /* Read the HBA Host Status Register */
4076        if (lpfc_readl(phba->HSregaddr, &status))
4077                return 1;
4078
4079        /*
4080         * Check status register every 100ms for 5 retries, then every
4081         * 500ms for 5, then every 2.5 sec for 5, then reset board and
4082         * every 2.5 sec for 4.
4083         * Break our of the loop if errors occurred during init.
4084         */
4085        while (((status & mask) != mask) &&
4086               !(status & HS_FFERM) &&
4087               i++ < 20) {
4088
4089                if (i <= 5)
4090                        msleep(10);
4091                else if (i <= 10)
4092                        msleep(500);
4093                else
4094                        msleep(2500);
4095
4096                if (i == 15) {
4097                                /* Do post */
4098                        phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4099                        lpfc_sli_brdrestart(phba);
4100                }
4101                /* Read the HBA Host Status Register */
4102                if (lpfc_readl(phba->HSregaddr, &status)) {
4103                        retval = 1;
4104                        break;
4105                }
4106        }
4107
4108        /* Check to see if any errors occurred during init */
4109        if ((status & HS_FFERM) || (i >= 20)) {
4110                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4111                                "2751 Adapter failed to restart, "
4112                                "status reg x%x, FW Data: A8 x%x AC x%x\n",
4113                                status,
4114                                readl(phba->MBslimaddr + 0xa8),
4115                                readl(phba->MBslimaddr + 0xac));
4116                phba->link_state = LPFC_HBA_ERROR;
4117                retval = 1;
4118        }
4119
4120        return retval;
4121}
4122
4123/**
4124 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4125 * @phba: Pointer to HBA context object.
4126 * @mask: Bit mask to be checked.
4127 *
4128 * This function checks the host status register to check if HBA is
4129 * ready. This function will wait in a loop for the HBA to be ready
4130 * If the HBA is not ready , the function will will reset the HBA PCI
4131 * function again. The function returns 1 when HBA fail to be ready
4132 * otherwise returns zero.
4133 **/
4134static int
4135lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4136{
4137        uint32_t status;
4138        int retval = 0;
4139
4140        /* Read the HBA Host Status Register */
4141        status = lpfc_sli4_post_status_check(phba);
4142
4143        if (status) {
4144                phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4145                lpfc_sli_brdrestart(phba);
4146                status = lpfc_sli4_post_status_check(phba);
4147        }
4148
4149        /* Check to see if any errors occurred during init */
4150        if (status) {
4151                phba->link_state = LPFC_HBA_ERROR;
4152                retval = 1;
4153        } else
4154                phba->sli4_hba.intr_enable = 0;
4155
4156        return retval;
4157}
4158
4159/**
4160 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4161 * @phba: Pointer to HBA context object.
4162 * @mask: Bit mask to be checked.
4163 *
4164 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4165 * from the API jump table function pointer from the lpfc_hba struct.
4166 **/
4167int
4168lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4169{
4170        return phba->lpfc_sli_brdready(phba, mask);
4171}
4172
4173#define BARRIER_TEST_PATTERN (0xdeadbeef)
4174
4175/**
4176 * lpfc_reset_barrier - Make HBA ready for HBA reset
4177 * @phba: Pointer to HBA context object.
4178 *
4179 * This function is called before resetting an HBA. This function is called
4180 * with hbalock held and requests HBA to quiesce DMAs before a reset.
4181 **/
4182void lpfc_reset_barrier(struct lpfc_hba *phba)
4183{
4184        uint32_t __iomem *resp_buf;
4185        uint32_t __iomem *mbox_buf;
4186        volatile uint32_t mbox;
4187        uint32_t hc_copy, ha_copy, resp_data;
4188        int  i;
4189        uint8_t hdrtype;
4190
4191        lockdep_assert_held(&phba->hbalock);
4192
4193        pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4194        if (hdrtype != 0x80 ||
4195            (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4196             FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4197                return;
4198
4199        /*
4200         * Tell the other part of the chip to suspend temporarily all
4201         * its DMA activity.
4202         */
4203        resp_buf = phba->MBslimaddr;
4204
4205        /* Disable the error attention */
4206        if (lpfc_readl(phba->HCregaddr, &hc_copy))
4207                return;
4208        writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4209        readl(phba->HCregaddr); /* flush */
4210        phba->link_flag |= LS_IGNORE_ERATT;
4211
4212        if (lpfc_readl(phba->HAregaddr, &ha_copy))
4213                return;
4214        if (ha_copy & HA_ERATT) {
4215                /* Clear Chip error bit */
4216                writel(HA_ERATT, phba->HAregaddr);
4217                phba->pport->stopped = 1;
4218        }
4219
4220        mbox = 0;
4221        ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4222        ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4223
4224        writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
4225        mbox_buf = phba->MBslimaddr;
4226        writel(mbox, mbox_buf);
4227
4228        for (i = 0; i < 50; i++) {
4229                if (lpfc_readl((resp_buf + 1), &resp_data))
4230                        return;
4231                if (resp_data != ~(BARRIER_TEST_PATTERN))
4232                        mdelay(1);
4233                else
4234                        break;
4235        }
4236        resp_data = 0;
4237        if (lpfc_readl((resp_buf + 1), &resp_data))
4238                return;
4239        if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
4240                if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
4241                    phba->pport->stopped)
4242                        goto restore_hc;
4243                else
4244                        goto clear_errat;
4245        }
4246
4247        ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
4248        resp_data = 0;
4249        for (i = 0; i < 500; i++) {
4250                if (lpfc_readl(resp_buf, &resp_data))
4251                        return;
4252                if (resp_data != mbox)
4253                        mdelay(1);
4254                else
4255                        break;
4256        }
4257
4258clear_errat:
4259
4260        while (++i < 500) {
4261                if (lpfc_readl(phba->HAregaddr, &ha_copy))
4262                        return;
4263                if (!(ha_copy & HA_ERATT))
4264                        mdelay(1);
4265                else
4266                        break;
4267        }
4268
4269        if (readl(phba->HAregaddr) & HA_ERATT) {
4270                writel(HA_ERATT, phba->HAregaddr);
4271                phba->pport->stopped = 1;
4272        }
4273
4274restore_hc:
4275        phba->link_flag &= ~LS_IGNORE_ERATT;
4276        writel(hc_copy, phba->HCregaddr);
4277        readl(phba->HCregaddr); /* flush */
4278}
4279
4280/**
4281 * lpfc_sli_brdkill - Issue a kill_board mailbox command
4282 * @phba: Pointer to HBA context object.
4283 *
4284 * This function issues a kill_board mailbox command and waits for
4285 * the error attention interrupt. This function is called for stopping
4286 * the firmware processing. The caller is not required to hold any
4287 * locks. This function calls lpfc_hba_down_post function to free
4288 * any pending commands after the kill. The function will return 1 when it
4289 * fails to kill the board else will return 0.
4290 **/
4291int
4292lpfc_sli_brdkill(struct lpfc_hba *phba)
4293{
4294        struct lpfc_sli *psli;
4295        LPFC_MBOXQ_t *pmb;
4296        uint32_t status;
4297        uint32_t ha_copy;
4298        int retval;
4299        int i = 0;
4300
4301        psli = &phba->sli;
4302
4303        /* Kill HBA */
4304        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4305                        "0329 Kill HBA Data: x%x x%x\n",
4306                        phba->pport->port_state, psli->sli_flag);
4307
4308        pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4309        if (!pmb)
4310                return 1;
4311
4312        /* Disable the error attention */
4313        spin_lock_irq(&phba->hbalock);
4314        if (lpfc_readl(phba->HCregaddr, &status)) {
4315                spin_unlock_irq(&phba->hbalock);
4316                mempool_free(pmb, phba->mbox_mem_pool);
4317                return 1;
4318        }
4319        status &= ~HC_ERINT_ENA;
4320        writel(status, phba->HCregaddr);
4321        readl(phba->HCregaddr); /* flush */
4322        phba->link_flag |= LS_IGNORE_ERATT;
4323        spin_unlock_irq(&phba->hbalock);
4324
4325        lpfc_kill_board(phba, pmb);
4326        pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4327        retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4328
4329        if (retval != MBX_SUCCESS) {
4330                if (retval != MBX_BUSY)
4331                        mempool_free(pmb, phba->mbox_mem_pool);
4332                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4333                                "2752 KILL_BOARD command failed retval %d\n",
4334                                retval);
4335                spin_lock_irq(&phba->hbalock);
4336                phba->link_flag &= ~LS_IGNORE_ERATT;
4337                spin_unlock_irq(&phba->hbalock);
4338                return 1;
4339        }
4340
4341        spin_lock_irq(&phba->hbalock);
4342        psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4343        spin_unlock_irq(&phba->hbalock);
4344
4345        mempool_free(pmb, phba->mbox_mem_pool);
4346
4347        /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4348         * attention every 100ms for 3 seconds. If we don't get ERATT after
4349         * 3 seconds we still set HBA_ERROR state because the status of the
4350         * board is now undefined.
4351         */
4352        if (lpfc_readl(phba->HAregaddr, &ha_copy))
4353                return 1;
4354        while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4355                mdelay(100);
4356                if (lpfc_readl(phba->HAregaddr, &ha_copy))
4357                        return 1;
4358        }
4359
4360        del_timer_sync(&psli->mbox_tmo);
4361        if (ha_copy & HA_ERATT) {
4362                writel(HA_ERATT, phba->HAregaddr);
4363                phba->pport->stopped = 1;
4364        }
4365        spin_lock_irq(&phba->hbalock);
4366        psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4367        psli->mbox_active = NULL;
4368        phba->link_flag &= ~LS_IGNORE_ERATT;
4369        spin_unlock_irq(&phba->hbalock);
4370
4371        lpfc_hba_down_post(phba);
4372        phba->link_state = LPFC_HBA_ERROR;
4373
4374        return ha_copy & HA_ERATT ? 0 : 1;
4375}
4376
4377/**
4378 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
4379 * @phba: Pointer to HBA context object.
4380 *
4381 * This function resets the HBA by writing HC_INITFF to the control
4382 * register. After the HBA resets, this function resets all the iocb ring
4383 * indices. This function disables PCI layer parity checking during
4384 * the reset.
4385 * This function returns 0 always.
4386 * The caller is not required to hold any locks.
4387 **/
4388int
4389lpfc_sli_brdreset(struct lpfc_hba *phba)
4390{
4391        struct lpfc_sli *psli;
4392        struct lpfc_sli_ring *pring;
4393        uint16_t cfg_value;
4394        int i;
4395
4396        psli = &phba->sli;
4397
4398        /* Reset HBA */
4399        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4400                        "0325 Reset HBA Data: x%x x%x\n",
4401                        (phba->pport) ? phba->pport->port_state : 0,
4402                        psli->sli_flag);
4403
4404        /* perform board reset */
4405        phba->fc_eventTag = 0;
4406        phba->link_events = 0;
4407        if (phba->pport) {
4408                phba->pport->fc_myDID = 0;
4409                phba->pport->fc_prevDID = 0;
4410        }
4411
4412        /* Turn off parity checking and serr during the physical reset */
4413        pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4414        pci_write_config_word(phba->pcidev, PCI_COMMAND,
4415                              (cfg_value &
4416                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4417
4418        psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4419
4420        /* Now toggle INITFF bit in the Host Control Register */
4421        writel(HC_INITFF, phba->HCregaddr);
4422        mdelay(1);
4423        readl(phba->HCregaddr); /* flush */
4424        writel(0, phba->HCregaddr);
4425        readl(phba->HCregaddr); /* flush */
4426
4427        /* Restore PCI cmd register */
4428        pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4429
4430        /* Initialize relevant SLI info */
4431        for (i = 0; i < psli->num_rings; i++) {
4432                pring = &psli->sli3_ring[i];
4433                pring->flag = 0;
4434                pring->sli.sli3.rspidx = 0;
4435                pring->sli.sli3.next_cmdidx  = 0;
4436                pring->sli.sli3.local_getidx = 0;
4437                pring->sli.sli3.cmdidx = 0;
4438                pring->missbufcnt = 0;
4439        }
4440
4441        phba->link_state = LPFC_WARM_START;
4442        return 0;
4443}
4444
4445/**
4446 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4447 * @phba: Pointer to HBA context object.
4448 *
4449 * This function resets a SLI4 HBA. This function disables PCI layer parity
4450 * checking during resets the device. The caller is not required to hold
4451 * any locks.
4452 *
4453 * This function returns 0 always.
4454 **/
4455int
4456lpfc_sli4_brdreset(struct lpfc_hba *phba)
4457{
4458        struct lpfc_sli *psli = &phba->sli;
4459        uint16_t cfg_value;
4460        int rc = 0;
4461
4462        /* Reset HBA */
4463        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4464                        "0295 Reset HBA Data: x%x x%x x%x\n",
4465                        phba->pport->port_state, psli->sli_flag,
4466                        phba->hba_flag);
4467
4468        /* perform board reset */
4469        phba->fc_eventTag = 0;
4470        phba->link_events = 0;
4471        phba->pport->fc_myDID = 0;
4472        phba->pport->fc_prevDID = 0;
4473
4474        spin_lock_irq(&phba->hbalock);
4475        psli->sli_flag &= ~(LPFC_PROCESS_LA);
4476        phba->fcf.fcf_flag = 0;
4477        spin_unlock_irq(&phba->hbalock);
4478
4479        /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4480        if (phba->hba_flag & HBA_FW_DUMP_OP) {
4481                phba->hba_flag &= ~HBA_FW_DUMP_OP;
4482                return rc;
4483        }
4484
4485        /* Now physically reset the device */
4486        lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4487                        "0389 Performing PCI function reset!\n");
4488
4489        /* Turn off parity checking and serr during the physical reset */
4490        pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4491        pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4492                              ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4493
4494        /* Perform FCoE PCI function reset before freeing queue memory */
4495        rc = lpfc_pci_function_reset(phba);
4496
4497        /* Restore PCI cmd register */
4498        pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4499
4500        return rc;
4501}
4502
4503/**
4504 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4505 * @phba: Pointer to HBA context object.
4506 *
4507 * This function is called in the SLI initialization code path to
4508 * restart the HBA. The caller is not required to hold any lock.
4509 * This function writes MBX_RESTART mailbox command to the SLIM and
4510 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4511 * function to free any pending commands. The function enables
4512 * POST only during the first initialization. The function returns zero.
4513 * The function does not guarantee completion of MBX_RESTART mailbox
4514 * command before the return of this function.
4515 **/
4516static int
4517lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4518{
4519        MAILBOX_t *mb;
4520        struct lpfc_sli *psli;
4521        volatile uint32_t word0;
4522        void __iomem *to_slim;
4523        uint32_t hba_aer_enabled;
4524
4525        spin_lock_irq(&phba->hbalock);
4526
4527        /* Take PCIe device Advanced Error Reporting (AER) state */
4528        hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4529
4530        psli = &phba->sli;
4531
4532        /* Restart HBA */
4533        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4534                        "0337 Restart HBA Data: x%x x%x\n",
4535                        (phba->pport) ? phba->pport->port_state : 0,
4536                        psli->sli_flag);
4537
4538        word0 = 0;
4539        mb = (MAILBOX_t *) &word0;
4540        mb->mbxCommand = MBX_RESTART;
4541        mb->mbxHc = 1;
4542
4543        lpfc_reset_barrier(phba);
4544
4545        to_slim = phba->MBslimaddr;
4546        writel(*(uint32_t *) mb, to_slim);
4547        readl(to_slim); /* flush */
4548
4549        /* Only skip post after fc_ffinit is completed */
4550        if (phba->pport && phba->pport->port_state)
4551                word0 = 1;      /* This is really setting up word1 */
4552        else
4553                word0 = 0;      /* This is really setting up word1 */
4554        to_slim = phba->MBslimaddr + sizeof (uint32_t);
4555        writel(*(uint32_t *) mb, to_slim);
4556        readl(to_slim); /* flush */
4557
4558        lpfc_sli_brdreset(phba);
4559        if (phba->pport)
4560                phba->pport->stopped = 0;
4561        phba->link_state = LPFC_INIT_START;
4562        phba->hba_flag = 0;
4563        spin_unlock_irq(&phba->hbalock);
4564
4565        memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4566        psli->stats_start = get_seconds();
4567
4568        /* Give the INITFF and Post time to settle. */
4569        mdelay(100);
4570
4571        /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4572        if (hba_aer_enabled)
4573                pci_disable_pcie_error_reporting(phba->pcidev);
4574
4575        lpfc_hba_down_post(phba);
4576
4577        return 0;
4578}
4579
4580/**
4581 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4582 * @phba: Pointer to HBA context object.
4583 *
4584 * This function is called in the SLI initialization code path to restart
4585 * a SLI4 HBA. The caller is not required to hold any lock.
4586 * At the end of the function, it calls lpfc_hba_down_post function to
4587 * free any pending commands.
4588 **/
4589static int
4590lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4591{
4592        struct lpfc_sli *psli = &phba->sli;
4593        uint32_t hba_aer_enabled;
4594        int rc;
4595
4596        /* Restart HBA */
4597        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4598                        "0296 Restart HBA Data: x%x x%x\n",
4599                        phba->pport->port_state, psli->sli_flag);
4600
4601        /* Take PCIe device Advanced Error Reporting (AER) state */
4602        hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4603
4604        rc = lpfc_sli4_brdreset(phba);
4605
4606        spin_lock_irq(&phba->hbalock);
4607        phba->pport->stopped = 0;
4608        phba->link_state = LPFC_INIT_START;
4609        phba->hba_flag = 0;
4610        spin_unlock_irq(&phba->hbalock);
4611
4612        memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4613        psli->stats_start = get_seconds();
4614
4615        /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4616        if (hba_aer_enabled)
4617                pci_disable_pcie_error_reporting(phba->pcidev);
4618
4619        lpfc_hba_down_post(phba);
4620        lpfc_sli4_queue_destroy(phba);
4621
4622        return rc;
4623}
4624
4625/**
4626 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4627 * @phba: Pointer to HBA context object.
4628 *
4629 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4630 * API jump table function pointer from the lpfc_hba struct.
4631**/
4632int
4633lpfc_sli_brdrestart(struct lpfc_hba *phba)
4634{
4635        return phba->lpfc_sli_brdrestart(phba);
4636}
4637
4638/**
4639 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4640 * @phba: Pointer to HBA context object.
4641 *
4642 * This function is called after a HBA restart to wait for successful
4643 * restart of the HBA. Successful restart of the HBA is indicated by
4644 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4645 * iteration, the function will restart the HBA again. The function returns
4646 * zero if HBA successfully restarted else returns negative error code.
4647 **/
4648int
4649lpfc_sli_chipset_init(struct lpfc_hba *phba)
4650{
4651        uint32_t status, i = 0;
4652
4653        /* Read the HBA Host Status Register */
4654        if (lpfc_readl(phba->HSregaddr, &status))
4655                return -EIO;
4656
4657        /* Check status register to see what current state is */
4658        i = 0;
4659        while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4660
4661                /* Check every 10ms for 10 retries, then every 100ms for 90
4662                 * retries, then every 1 sec for 50 retires for a total of
4663                 * ~60 seconds before reset the board again and check every
4664                 * 1 sec for 50 retries. The up to 60 seconds before the
4665                 * board ready is required by the Falcon FIPS zeroization
4666                 * complete, and any reset the board in between shall cause
4667                 * restart of zeroization, further delay the board ready.
4668                 */
4669                if (i++ >= 200) {
4670                        /* Adapter failed to init, timeout, status reg
4671                           <status> */
4672                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4673                                        "0436 Adapter failed to init, "
4674                                        "timeout, status reg x%x, "
4675                                        "FW Data: A8 x%x AC x%x\n", status,
4676                                        readl(phba->MBslimaddr + 0xa8),
4677                                        readl(phba->MBslimaddr + 0xac));
4678                        phba->link_state = LPFC_HBA_ERROR;
4679                        return -ETIMEDOUT;
4680                }
4681
4682                /* Check to see if any errors occurred during init */
4683                if (status & HS_FFERM) {
4684                        /* ERROR: During chipset initialization */
4685                        /* Adapter failed to init, chipset, status reg
4686                           <status> */
4687                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4688                                        "0437 Adapter failed to init, "
4689                                        "chipset, status reg x%x, "
4690                                        "FW Data: A8 x%x AC x%x\n", status,
4691                                        readl(phba->MBslimaddr + 0xa8),
4692                                        readl(phba->MBslimaddr + 0xac));
4693                        phba->link_state = LPFC_HBA_ERROR;
4694                        return -EIO;
4695                }
4696
4697                if (i <= 10)
4698                        msleep(10);
4699                else if (i <= 100)
4700                        msleep(100);
4701                else
4702                        msleep(1000);
4703
4704                if (i == 150) {
4705                        /* Do post */
4706                        phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4707                        lpfc_sli_brdrestart(phba);
4708                }
4709                /* Read the HBA Host Status Register */
4710                if (lpfc_readl(phba->HSregaddr, &status))
4711                        return -EIO;
4712        }
4713
4714        /* Check to see if any errors occurred during init */
4715        if (status & HS_FFERM) {
4716                /* ERROR: During chipset initialization */
4717                /* Adapter failed to init, chipset, status reg <status> */
4718                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4719                                "0438 Adapter failed to init, chipset, "
4720                                "status reg x%x, "
4721                                "FW Data: A8 x%x AC x%x\n", status,
4722                                readl(phba->MBslimaddr + 0xa8),
4723                                readl(phba->MBslimaddr + 0xac));
4724                phba->link_state = LPFC_HBA_ERROR;
4725                return -EIO;
4726        }
4727
4728        /* Clear all interrupt enable conditions */
4729        writel(0, phba->HCregaddr);
4730        readl(phba->HCregaddr); /* flush */
4731
4732        /* setup host attn register */
4733        writel(0xffffffff, phba->HAregaddr);
4734        readl(phba->HAregaddr); /* flush */
4735        return 0;
4736}
4737
4738/**
4739 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4740 *
4741 * This function calculates and returns the number of HBQs required to be
4742 * configured.
4743 **/
4744int
4745lpfc_sli_hbq_count(void)
4746{
4747        return ARRAY_SIZE(lpfc_hbq_defs);
4748}
4749
4750/**
4751 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4752 *
4753 * This function adds the number of hbq entries in every HBQ to get
4754 * the total number of hbq entries required for the HBA and returns
4755 * the total count.
4756 **/
4757static int
4758lpfc_sli_hbq_entry_count(void)
4759{
4760        int  hbq_count = lpfc_sli_hbq_count();
4761        int  count = 0;
4762        int  i;
4763
4764        for (i = 0; i < hbq_count; ++i)
4765                count += lpfc_hbq_defs[i]->entry_count;
4766        return count;
4767}
4768
4769/**
4770 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4771 *
4772 * This function calculates amount of memory required for all hbq entries
4773 * to be configured and returns the total memory required.
4774 **/
4775int
4776lpfc_sli_hbq_size(void)
4777{
4778        return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4779}
4780
4781/**
4782 * lpfc_sli_hbq_setup - configure and initialize HBQs
4783 * @phba: Pointer to HBA context object.
4784 *
4785 * This function is called during the SLI initialization to configure
4786 * all the HBQs and post buffers to the HBQ. The caller is not
4787 * required to hold any locks. This function will return zero if successful
4788 * else it will return negative error code.
4789 **/
4790static int
4791lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4792{
4793        int  hbq_count = lpfc_sli_hbq_count();
4794        LPFC_MBOXQ_t *pmb;
4795        MAILBOX_t *pmbox;
4796        uint32_t hbqno;
4797        uint32_t hbq_entry_index;
4798
4799                                /* Get a Mailbox buffer to setup mailbox
4800                                 * commands for HBA initialization
4801                                 */
4802        pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4803
4804        if (!pmb)
4805                return -ENOMEM;
4806
4807        pmbox = &pmb->u.mb;
4808
4809        /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4810        phba->link_state = LPFC_INIT_MBX_CMDS;
4811        phba->hbq_in_use = 1;
4812
4813        hbq_entry_index = 0;
4814        for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4815                phba->hbqs[hbqno].next_hbqPutIdx = 0;
4816                phba->hbqs[hbqno].hbqPutIdx      = 0;
4817                phba->hbqs[hbqno].local_hbqGetIdx   = 0;
4818                phba->hbqs[hbqno].entry_count =
4819                        lpfc_hbq_defs[hbqno]->entry_count;
4820                lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4821                        hbq_entry_index, pmb);
4822                hbq_entry_index += phba->hbqs[hbqno].entry_count;
4823
4824                if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4825                        /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4826                           mbxStatus <status>, ring <num> */
4827
4828                        lpfc_printf_log(phba, KERN_ERR,
4829                                        LOG_SLI | LOG_VPORT,
4830                                        "1805 Adapter failed to init. "
4831                                        "Data: x%x x%x x%x\n",
4832                                        pmbox->mbxCommand,
4833                                        pmbox->mbxStatus, hbqno);
4834
4835                        phba->link_state = LPFC_HBA_ERROR;
4836                        mempool_free(pmb, phba->mbox_mem_pool);
4837                        return -ENXIO;
4838                }
4839        }
4840        phba->hbq_count = hbq_count;
4841
4842        mempool_free(pmb, phba->mbox_mem_pool);
4843
4844        /* Initially populate or replenish the HBQs */
4845        for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4846                lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4847        return 0;
4848}
4849
4850/**
4851 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4852 * @phba: Pointer to HBA context object.
4853 *
4854 * This function is called during the SLI initialization to configure
4855 * all the HBQs and post buffers to the HBQ. The caller is not
4856 * required to hold any locks. This function will return zero if successful
4857 * else it will return negative error code.
4858 **/
4859static int
4860lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4861{
4862        phba->hbq_in_use = 1;
4863        phba->hbqs[LPFC_ELS_HBQ].entry_count =
4864                lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
4865        phba->hbq_count = 1;
4866        lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
4867        /* Initially populate or replenish the HBQs */
4868        return 0;
4869}
4870
4871/**
4872 * lpfc_sli_config_port - Issue config port mailbox command
4873 * @phba: Pointer to HBA context object.
4874 * @sli_mode: sli mode - 2/3
4875 *
4876 * This function is called by the sli initialization code path
4877 * to issue config_port mailbox command. This function restarts the
4878 * HBA firmware and issues a config_port mailbox command to configure
4879 * the SLI interface in the sli mode specified by sli_mode
4880 * variable. The caller is not required to hold any locks.
4881 * The function returns 0 if successful, else returns negative error
4882 * code.
4883 **/
4884int
4885lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4886{
4887        LPFC_MBOXQ_t *pmb;
4888        uint32_t resetcount = 0, rc = 0, done = 0;
4889
4890        pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4891        if (!pmb) {
4892                phba->link_state = LPFC_HBA_ERROR;
4893                return -ENOMEM;
4894        }
4895
4896        phba->sli_rev = sli_mode;
4897        while (resetcount < 2 && !done) {
4898                spin_lock_irq(&phba->hbalock);
4899                phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4900                spin_unlock_irq(&phba->hbalock);
4901                phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4902                lpfc_sli_brdrestart(phba);
4903                rc = lpfc_sli_chipset_init(phba);
4904                if (rc)
4905                        break;
4906
4907                spin_lock_irq(&phba->hbalock);
4908                phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4909                spin_unlock_irq(&phba->hbalock);
4910                resetcount++;
4911
4912                /* Call pre CONFIG_PORT mailbox command initialization.  A
4913                 * value of 0 means the call was successful.  Any other
4914                 * nonzero value is a failure, but if ERESTART is returned,
4915                 * the driver may reset the HBA and try again.
4916                 */
4917                rc = lpfc_config_port_prep(phba);
4918                if (rc == -ERESTART) {
4919                        phba->link_state = LPFC_LINK_UNKNOWN;
4920                        continue;
4921                } else if (rc)
4922                        break;
4923
4924                phba->link_state = LPFC_INIT_MBX_CMDS;
4925                lpfc_config_port(phba, pmb);
4926                rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4927                phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4928                                        LPFC_SLI3_HBQ_ENABLED |
4929                                        LPFC_SLI3_CRP_ENABLED |
4930                                        LPFC_SLI3_BG_ENABLED |
4931                                        LPFC_SLI3_DSS_ENABLED);
4932                if (rc != MBX_SUCCESS) {
4933                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4934                                "0442 Adapter failed to init, mbxCmd x%x "
4935                                "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4936                                pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4937                        spin_lock_irq(&phba->hbalock);
4938                        phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4939                        spin_unlock_irq(&phba->hbalock);
4940                        rc = -ENXIO;
4941                } else {
4942                        /* Allow asynchronous mailbox command to go through */
4943                        spin_lock_irq(&phba->hbalock);
4944                        phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4945                        spin_unlock_irq(&phba->hbalock);
4946                        done = 1;
4947
4948                        if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4949                            (pmb->u.mb.un.varCfgPort.gasabt == 0))
4950                                lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4951                                        "3110 Port did not grant ASABT\n");
4952                }
4953        }
4954        if (!done) {
4955                rc = -EINVAL;
4956                goto do_prep_failed;
4957        }
4958        if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4959                if (!pmb->u.mb.un.varCfgPort.cMA) {
4960                        rc = -ENXIO;
4961                        goto do_prep_failed;
4962                }
4963                if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4964                        phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4965                        phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4966                        phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4967                                phba->max_vpi : phba->max_vports;
4968
4969                } else
4970                        phba->max_vpi = 0;
4971                phba->fips_level = 0;
4972                phba->fips_spec_rev = 0;
4973                if (pmb->u.mb.un.varCfgPort.gdss) {
4974                        phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4975                        phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4976                        phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4977                        lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4978                                        "2850 Security Crypto Active. FIPS x%d "
4979                                        "(Spec Rev: x%d)",
4980                                        phba->fips_level, phba->fips_spec_rev);
4981                }
4982                if (pmb->u.mb.un.varCfgPort.sec_err) {
4983                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4984                                        "2856 Config Port Security Crypto "
4985                                        "Error: x%x ",
4986                                        pmb->u.mb.un.varCfgPort.sec_err);
4987                }
4988                if (pmb->u.mb.un.varCfgPort.gerbm)
4989                        phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4990                if (pmb->u.mb.un.varCfgPort.gcrp)
4991                        phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4992
4993                phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4994                phba->port_gp = phba->mbox->us.s3_pgp.port;
4995
4996                if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
4997                        if (pmb->u.mb.un.varCfgPort.gbg == 0) {
4998                                phba->cfg_enable_bg = 0;
4999                                phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
5000                                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5001                                                "0443 Adapter did not grant "
5002                                                "BlockGuard\n");
5003                        }
5004                }
5005        } else {
5006                phba->hbq_get = NULL;
5007                phba->port_gp = phba->mbox->us.s2.port;
5008                phba->max_vpi = 0;
5009        }
5010do_prep_failed:
5011        mempool_free(pmb, phba->mbox_mem_pool);
5012        return rc;
5013}
5014
5015
5016/**
5017 * lpfc_sli_hba_setup - SLI initialization function
5018 * @phba: Pointer to HBA context object.
5019 *
5020 * This function is the main SLI initialization function. This function
5021 * is called by the HBA initialization code, HBA reset code and HBA
5022 * error attention handler code. Caller is not required to hold any
5023 * locks. This function issues config_port mailbox command to configure
5024 * the SLI, setup iocb rings and HBQ rings. In the end the function
5025 * calls the config_port_post function to issue init_link mailbox
5026 * command and to start the discovery. The function will return zero
5027 * if successful, else it will return negative error code.
5028 **/
5029int
5030lpfc_sli_hba_setup(struct lpfc_hba *phba)
5031{
5032        uint32_t rc;
5033        int  mode = 3, i;
5034        int longs;
5035
5036        switch (phba->cfg_sli_mode) {
5037        case 2:
5038                if (phba->cfg_enable_npiv) {
5039                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
5040                                "1824 NPIV enabled: Override sli_mode "
5041                                "parameter (%d) to auto (0).\n",
5042                                phba->cfg_sli_mode);
5043                        break;
5044                }
5045                mode = 2;
5046                break;
5047        case 0:
5048        case 3:
5049                break;
5050        default:
5051                lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
5052                                "1819 Unrecognized sli_mode parameter: %d.\n",
5053                                phba->cfg_sli_mode);
5054
5055                break;
5056        }
5057        phba->fcp_embed_io = 0; /* SLI4 FC support only */
5058
5059        rc = lpfc_sli_config_port(phba, mode);
5060
5061        if (rc && phba->cfg_sli_mode == 3)
5062                lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
5063                                "1820 Unable to select SLI-3.  "
5064                                "Not supported by adapter.\n");
5065        if (rc && mode != 2)
5066                rc = lpfc_sli_config_port(phba, 2);
5067        else if (rc && mode == 2)
5068                rc = lpfc_sli_config_port(phba, 3);
5069        if (rc)
5070                goto lpfc_sli_hba_setup_error;
5071
5072        /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5073        if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5074                rc = pci_enable_pcie_error_reporting(phba->pcidev);
5075                if (!rc) {
5076                        lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5077                                        "2709 This device supports "
5078                                        "Advanced Error Reporting (AER)\n");
5079                        spin_lock_irq(&phba->hbalock);
5080                        phba->hba_flag |= HBA_AER_ENABLED;
5081                        spin_unlock_irq(&phba->hbalock);
5082                } else {
5083                        lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5084                                        "2708 This device does not support "
5085                                        "Advanced Error Reporting (AER): %d\n",
5086                                        rc);
5087                        phba->cfg_aer_support = 0;
5088                }
5089        }
5090
5091        if (phba->sli_rev == 3) {
5092                phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5093                phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
5094        } else {
5095                phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5096                phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
5097                phba->sli3_options = 0;
5098        }
5099
5100        lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5101                        "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5102                        phba->sli_rev, phba->max_vpi);
5103        rc = lpfc_sli_ring_map(phba);
5104
5105        if (rc)
5106                goto lpfc_sli_hba_setup_error;
5107
5108        /* Initialize VPIs. */
5109        if (phba->sli_rev == LPFC_SLI_REV3) {
5110                /*
5111                 * The VPI bitmask and physical ID array are allocated
5112                 * and initialized once only - at driver load.  A port
5113                 * reset doesn't need to reinitialize this memory.
5114                 */
5115                if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5116                        longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5117                        phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
5118                                                  GFP_KERNEL);
5119                        if (!phba->vpi_bmask) {
5120                                rc = -ENOMEM;
5121                                goto lpfc_sli_hba_setup_error;
5122                        }
5123
5124                        phba->vpi_ids = kzalloc(
5125                                        (phba->max_vpi+1) * sizeof(uint16_t),
5126                                        GFP_KERNEL);
5127                        if (!phba->vpi_ids) {
5128                                kfree(phba->vpi_bmask);
5129                                rc = -ENOMEM;
5130                                goto lpfc_sli_hba_setup_error;
5131                        }
5132                        for (i = 0; i < phba->max_vpi; i++)
5133                                phba->vpi_ids[i] = i;
5134                }
5135        }
5136
5137        /* Init HBQs */
5138        if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5139                rc = lpfc_sli_hbq_setup(phba);
5140                if (rc)
5141                        goto lpfc_sli_hba_setup_error;
5142        }
5143        spin_lock_irq(&phba->hbalock);
5144        phba->sli.sli_flag |= LPFC_PROCESS_LA;
5145        spin_unlock_irq(&phba->hbalock);
5146
5147        rc = lpfc_config_port_post(phba);
5148        if (rc)
5149                goto lpfc_sli_hba_setup_error;
5150
5151        return rc;
5152
5153lpfc_sli_hba_setup_error:
5154        phba->link_state = LPFC_HBA_ERROR;
5155        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5156                        "0445 Firmware initialization failed\n");
5157        return rc;
5158}
5159
5160/**
5161 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5162 * @phba: Pointer to HBA context object.
5163 * @mboxq: mailbox pointer.
5164 * This function issue a dump mailbox command to read config region
5165 * 23 and parse the records in the region and populate driver
5166 * data structure.
5167 **/
5168static int
5169lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
5170{
5171        LPFC_MBOXQ_t *mboxq;
5172        struct lpfc_dmabuf *mp;
5173        struct lpfc_mqe *mqe;
5174        uint32_t data_length;
5175        int rc;
5176
5177        /* Program the default value of vlan_id and fc_map */
5178        phba->valid_vlan = 0;
5179        phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5180        phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5181        phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5182
5183        mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5184        if (!mboxq)
5185                return -ENOMEM;
5186
5187        mqe = &mboxq->u.mqe;
5188        if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5189                rc = -ENOMEM;
5190                goto out_free_mboxq;
5191        }
5192
5193        mp = (struct lpfc_dmabuf *) mboxq->context1;
5194        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5195
5196        lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5197                        "(%d):2571 Mailbox cmd x%x Status x%x "
5198                        "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5199                        "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5200                        "CQ: x%x x%x x%x x%x\n",
5201                        mboxq->vport ? mboxq->vport->vpi : 0,
5202                        bf_get(lpfc_mqe_command, mqe),
5203                        bf_get(lpfc_mqe_status, mqe),
5204                        mqe->un.mb_words[0], mqe->un.mb_words[1],
5205                        mqe->un.mb_words[2], mqe->un.mb_words[3],
5206                        mqe->un.mb_words[4], mqe->un.mb_words[5],
5207                        mqe->un.mb_words[6], mqe->un.mb_words[7],
5208                        mqe->un.mb_words[8], mqe->un.mb_words[9],
5209                        mqe->un.mb_words[10], mqe->un.mb_words[11],
5210                        mqe->un.mb_words[12], mqe->un.mb_words[13],
5211                        mqe->un.mb_words[14], mqe->un.mb_words[15],
5212                        mqe->un.mb_words[16], mqe->un.mb_words[50],
5213                        mboxq->mcqe.word0,
5214                        mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5215                        mboxq->mcqe.trailer);
5216
5217        if (rc) {
5218                lpfc_mbuf_free(phba, mp->virt, mp->phys);
5219                kfree(mp);
5220                rc = -EIO;
5221                goto out_free_mboxq;
5222        }
5223        data_length = mqe->un.mb_words[5];
5224        if (data_length > DMP_RGN23_SIZE) {
5225                lpfc_mbuf_free(phba, mp->virt, mp->phys);
5226                kfree(mp);
5227                rc = -EIO;
5228                goto out_free_mboxq;
5229        }
5230
5231        lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5232        lpfc_mbuf_free(phba, mp->virt, mp->phys);
5233        kfree(mp);
5234        rc = 0;
5235
5236out_free_mboxq:
5237        mempool_free(mboxq, phba->mbox_mem_pool);
5238        return rc;
5239}
5240
5241/**
5242 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5243 * @phba: pointer to lpfc hba data structure.
5244 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5245 * @vpd: pointer to the memory to hold resulting port vpd data.
5246 * @vpd_size: On input, the number of bytes allocated to @vpd.
5247 *            On output, the number of data bytes in @vpd.
5248 *
5249 * This routine executes a READ_REV SLI4 mailbox command.  In
5250 * addition, this routine gets the port vpd data.
5251 *
5252 * Return codes
5253 *      0 - successful
5254 *      -ENOMEM - could not allocated memory.
5255 **/
5256static int
5257lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5258                    uint8_t *vpd, uint32_t *vpd_size)
5259{
5260        int rc = 0;
5261        uint32_t dma_size;
5262        struct lpfc_dmabuf *dmabuf;
5263        struct lpfc_mqe *mqe;
5264
5265        dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5266        if (!dmabuf)
5267                return -ENOMEM;
5268
5269        /*
5270         * Get a DMA buffer for the vpd data resulting from the READ_REV
5271         * mailbox command.
5272         */
5273        dma_size = *vpd_size;
5274        dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5275                                           &dmabuf->phys, GFP_KERNEL);
5276        if (!dmabuf->virt) {
5277                kfree(dmabuf);
5278                return -ENOMEM;
5279        }
5280
5281        /*
5282         * The SLI4 implementation of READ_REV conflicts at word1,
5283         * bits 31:16 and SLI4 adds vpd functionality not present
5284         * in SLI3.  This code corrects the conflicts.
5285         */
5286        lpfc_read_rev(phba, mboxq);
5287        mqe = &mboxq->u.mqe;
5288        mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5289        mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5290        mqe->un.read_rev.word1 &= 0x0000FFFF;
5291        bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5292        bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5293
5294        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5295        if (rc) {
5296                dma_free_coherent(&phba->pcidev->dev, dma_size,
5297                                  dmabuf->virt, dmabuf->phys);
5298                kfree(dmabuf);
5299                return -EIO;
5300        }
5301
5302        /*
5303         * The available vpd length cannot be bigger than the
5304         * DMA buffer passed to the port.  Catch the less than
5305         * case and update the caller's size.
5306         */
5307        if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5308                *vpd_size = mqe->un.read_rev.avail_vpd_len;
5309
5310        memcpy(vpd, dmabuf->virt, *vpd_size);
5311
5312        dma_free_coherent(&phba->pcidev->dev, dma_size,
5313                          dmabuf->virt, dmabuf->phys);
5314        kfree(dmabuf);
5315        return 0;
5316}
5317
5318/**
5319 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5320 * @phba: pointer to lpfc hba data structure.
5321 *
5322 * This routine retrieves SLI4 device physical port name this PCI function
5323 * is attached to.
5324 *
5325 * Return codes
5326 *      0 - successful
5327 *      otherwise - failed to retrieve physical port name
5328 **/
5329static int
5330lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5331{
5332        LPFC_MBOXQ_t *mboxq;
5333        struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5334        struct lpfc_controller_attribute *cntl_attr;
5335        struct lpfc_mbx_get_port_name *get_port_name;
5336        void *virtaddr = NULL;
5337        uint32_t alloclen, reqlen;
5338        uint32_t shdr_status, shdr_add_status;
5339        union lpfc_sli4_cfg_shdr *shdr;
5340        char cport_name = 0;
5341        int rc;
5342
5343        /* We assume nothing at this point */
5344        phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5345        phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5346
5347        mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5348        if (!mboxq)
5349                return -ENOMEM;
5350        /* obtain link type and link number via READ_CONFIG */
5351        phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5352        lpfc_sli4_read_config(phba);
5353        if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5354                goto retrieve_ppname;
5355
5356        /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5357        reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5358        alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5359                        LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5360                        LPFC_SLI4_MBX_NEMBED);
5361        if (alloclen < reqlen) {
5362                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5363                                "3084 Allocated DMA memory size (%d) is "
5364                                "less than the requested DMA memory size "
5365                                "(%d)\n", alloclen, reqlen);
5366                rc = -ENOMEM;
5367                goto out_free_mboxq;
5368        }
5369        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5370        virtaddr = mboxq->sge_array->addr[0];
5371        mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5372        shdr = &mbx_cntl_attr->cfg_shdr;
5373        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5374        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5375        if (shdr_status || shdr_add_status || rc) {
5376                lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5377                                "3085 Mailbox x%x (x%x/x%x) failed, "
5378                                "rc:x%x, status:x%x, add_status:x%x\n",
5379                                bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5380                                lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5381                                lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5382                                rc, shdr_status, shdr_add_status);
5383                rc = -ENXIO;
5384                goto out_free_mboxq;
5385        }
5386        cntl_attr = &mbx_cntl_attr->cntl_attr;
5387        phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5388        phba->sli4_hba.lnk_info.lnk_tp =
5389                bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5390        phba->sli4_hba.lnk_info.lnk_no =
5391                bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5392        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5393                        "3086 lnk_type:%d, lnk_numb:%d\n",
5394                        phba->sli4_hba.lnk_info.lnk_tp,
5395                        phba->sli4_hba.lnk_info.lnk_no);
5396
5397retrieve_ppname:
5398        lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5399                LPFC_MBOX_OPCODE_GET_PORT_NAME,
5400                sizeof(struct lpfc_mbx_get_port_name) -
5401                sizeof(struct lpfc_sli4_cfg_mhdr),
5402                LPFC_SLI4_MBX_EMBED);
5403        get_port_name = &mboxq->u.mqe.un.get_port_name;
5404        shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5405        bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5406        bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5407                phba->sli4_hba.lnk_info.lnk_tp);
5408        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5409        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5410        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5411        if (shdr_status || shdr_add_status || rc) {
5412                lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5413                                "3087 Mailbox x%x (x%x/x%x) failed: "
5414                                "rc:x%x, status:x%x, add_status:x%x\n",
5415                                bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5416                                lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5417                                lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5418                                rc, shdr_status, shdr_add_status);
5419                rc = -ENXIO;
5420                goto out_free_mboxq;
5421        }
5422        switch (phba->sli4_hba.lnk_info.lnk_no) {
5423        case LPFC_LINK_NUMBER_0:
5424                cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5425                                &get_port_name->u.response);
5426                phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5427                break;
5428        case LPFC_LINK_NUMBER_1:
5429                cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5430                                &get_port_name->u.response);
5431                phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5432                break;
5433        case LPFC_LINK_NUMBER_2:
5434                cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5435                                &get_port_name->u.response);
5436                phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5437                break;
5438        case LPFC_LINK_NUMBER_3:
5439                cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5440                                &get_port_name->u.response);
5441                phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5442                break;
5443        default:
5444                break;
5445        }
5446
5447        if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5448                phba->Port[0] = cport_name;
5449                phba->Port[1] = '\0';
5450                lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5451                                "3091 SLI get port name: %s\n", phba->Port);
5452        }
5453
5454out_free_mboxq:
5455        if (rc != MBX_TIMEOUT) {
5456                if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5457                        lpfc_sli4_mbox_cmd_free(phba, mboxq);
5458                else
5459                        mempool_free(mboxq, phba->mbox_mem_pool);
5460        }
5461        return rc;
5462}
5463
5464/**
5465 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5466 * @phba: pointer to lpfc hba data structure.
5467 *
5468 * This routine is called to explicitly arm the SLI4 device's completion and
5469 * event queues
5470 **/
5471static void
5472lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5473{
5474        int qidx;
5475        struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
5476
5477        sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5478        sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5479        if (sli4_hba->nvmels_cq)
5480                sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
5481                                                LPFC_QUEUE_REARM);
5482
5483        if (sli4_hba->fcp_cq)
5484                for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5485                        sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
5486                                                LPFC_QUEUE_REARM);
5487
5488        if (sli4_hba->nvme_cq)
5489                for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5490                        sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
5491                                                LPFC_QUEUE_REARM);
5492
5493        if (phba->cfg_fof)
5494                sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
5495
5496        if (sli4_hba->hba_eq)
5497                for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5498                        sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
5499                                                        LPFC_QUEUE_REARM);
5500
5501        if (phba->nvmet_support) {
5502                for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5503                        sli4_hba->sli4_cq_release(
5504                                sli4_hba->nvmet_cqset[qidx],
5505                                LPFC_QUEUE_REARM);
5506                }
5507        }
5508
5509        if (phba->cfg_fof)
5510                sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
5511}
5512
5513/**
5514 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5515 * @phba: Pointer to HBA context object.
5516 * @type: The resource extent type.
5517 * @extnt_count: buffer to hold port available extent count.
5518 * @extnt_size: buffer to hold element count per extent.
5519 *
5520 * This function calls the port and retrievs the number of available
5521 * extents and their size for a particular extent type.
5522 *
5523 * Returns: 0 if successful.  Nonzero otherwise.
5524 **/
5525int
5526lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5527                               uint16_t *extnt_count, uint16_t *extnt_size)
5528{
5529        int rc = 0;
5530        uint32_t length;
5531        uint32_t mbox_tmo;
5532        struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5533        LPFC_MBOXQ_t *mbox;
5534
5535        mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5536        if (!mbox)
5537                return -ENOMEM;
5538
5539        /* Find out how many extents are available for this resource type */
5540        length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5541                  sizeof(struct lpfc_sli4_cfg_mhdr));
5542        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5543                         LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5544                         length, LPFC_SLI4_MBX_EMBED);
5545
5546        /* Send an extents count of 0 - the GET doesn't use it. */
5547        rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5548                                        LPFC_SLI4_MBX_EMBED);
5549        if (unlikely(rc)) {
5550                rc = -EIO;
5551                goto err_exit;
5552        }
5553
5554        if (!phba->sli4_hba.intr_enable)
5555                rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5556        else {
5557                mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5558                rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5559        }
5560        if (unlikely(rc)) {
5561                rc = -EIO;
5562                goto err_exit;
5563        }
5564
5565        rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5566        if (bf_get(lpfc_mbox_hdr_status,
5567                   &rsrc_info->header.cfg_shdr.response)) {
5568                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5569                                "2930 Failed to get resource extents "
5570                                "Status 0x%x Add'l Status 0x%x\n",
5571                                bf_get(lpfc_mbox_hdr_status,
5572                                       &rsrc_info->header.cfg_shdr.response),
5573                                bf_get(lpfc_mbox_hdr_add_status,
5574                                       &rsrc_info->header.cfg_shdr.response));
5575                rc = -EIO;
5576                goto err_exit;
5577        }
5578
5579        *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5580                              &rsrc_info->u.rsp);
5581        *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5582                             &rsrc_info->u.rsp);
5583
5584        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5585                        "3162 Retrieved extents type-%d from port: count:%d, "
5586                        "size:%d\n", type, *extnt_count, *extnt_size);
5587
5588err_exit:
5589        mempool_free(mbox, phba->mbox_mem_pool);
5590        return rc;
5591}
5592
5593/**
5594 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5595 * @phba: Pointer to HBA context object.
5596 * @type: The extent type to check.
5597 *
5598 * This function reads the current available extents from the port and checks
5599 * if the extent count or extent size has changed since the last access.
5600 * Callers use this routine post port reset to understand if there is a
5601 * extent reprovisioning requirement.
5602 *
5603 * Returns:
5604 *   -Error: error indicates problem.
5605 *   1: Extent count or size has changed.
5606 *   0: No changes.
5607 **/
5608static int
5609lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5610{
5611        uint16_t curr_ext_cnt, rsrc_ext_cnt;
5612        uint16_t size_diff, rsrc_ext_size;
5613        int rc = 0;
5614        struct lpfc_rsrc_blks *rsrc_entry;
5615        struct list_head *rsrc_blk_list = NULL;
5616
5617        size_diff = 0;
5618        curr_ext_cnt = 0;
5619        rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5620                                            &rsrc_ext_cnt,
5621                                            &rsrc_ext_size);
5622        if (unlikely(rc))
5623                return -EIO;
5624
5625        switch (type) {
5626        case LPFC_RSC_TYPE_FCOE_RPI:
5627                rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5628                break;
5629        case LPFC_RSC_TYPE_FCOE_VPI:
5630                rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5631                break;
5632        case LPFC_RSC_TYPE_FCOE_XRI:
5633                rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5634                break;
5635        case LPFC_RSC_TYPE_FCOE_VFI:
5636                rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5637                break;
5638        default:
5639                break;
5640        }
5641
5642        list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5643                curr_ext_cnt++;
5644                if (rsrc_entry->rsrc_size != rsrc_ext_size)
5645                        size_diff++;
5646        }
5647
5648        if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5649                rc = 1;
5650
5651        return rc;
5652}
5653
5654/**
5655 * lpfc_sli4_cfg_post_extnts -
5656 * @phba: Pointer to HBA context object.
5657 * @extnt_cnt - number of available extents.
5658 * @type - the extent type (rpi, xri, vfi, vpi).
5659 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5660 * @mbox - pointer to the caller's allocated mailbox structure.
5661 *
5662 * This function executes the extents allocation request.  It also
5663 * takes care of the amount of memory needed to allocate or get the
5664 * allocated extents. It is the caller's responsibility to evaluate
5665 * the response.
5666 *
5667 * Returns:
5668 *   -Error:  Error value describes the condition found.
5669 *   0: if successful
5670 **/
5671static int
5672lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
5673                          uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5674{
5675        int rc = 0;
5676        uint32_t req_len;
5677        uint32_t emb_len;
5678        uint32_t alloc_len, mbox_tmo;
5679
5680        /* Calculate the total requested length of the dma memory */
5681        req_len = extnt_cnt * sizeof(uint16_t);
5682
5683        /*
5684         * Calculate the size of an embedded mailbox.  The uint32_t
5685         * accounts for extents-specific word.
5686         */
5687        emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5688                sizeof(uint32_t);
5689
5690        /*
5691         * Presume the allocation and response will fit into an embedded
5692         * mailbox.  If not true, reconfigure to a non-embedded mailbox.
5693         */
5694        *emb = LPFC_SLI4_MBX_EMBED;
5695        if (req_len > emb_len) {
5696                req_len = extnt_cnt * sizeof(uint16_t) +
5697                        sizeof(union lpfc_sli4_cfg_shdr) +
5698                        sizeof(uint32_t);
5699                *emb = LPFC_SLI4_MBX_NEMBED;
5700        }
5701
5702        alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5703                                     LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5704                                     req_len, *emb);
5705        if (alloc_len < req_len) {
5706                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5707                        "2982 Allocated DMA memory size (x%x) is "
5708                        "less than the requested DMA memory "
5709                        "size (x%x)\n", alloc_len, req_len);
5710                return -ENOMEM;
5711        }
5712        rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
5713        if (unlikely(rc))
5714                return -EIO;
5715
5716        if (!phba->sli4_hba.intr_enable)
5717                rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5718        else {
5719                mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5720                rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5721        }
5722
5723        if (unlikely(rc))
5724                rc = -EIO;
5725        return rc;
5726}
5727
5728/**
5729 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5730 * @phba: Pointer to HBA context object.
5731 * @type:  The resource extent type to allocate.
5732 *
5733 * This function allocates the number of elements for the specified
5734 * resource type.
5735 **/
5736static int
5737lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5738{
5739        bool emb = false;
5740        uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5741        uint16_t rsrc_id, rsrc_start, j, k;
5742        uint16_t *ids;
5743        int i, rc;
5744        unsigned long longs;
5745        unsigned long *bmask;
5746        struct lpfc_rsrc_blks *rsrc_blks;
5747        LPFC_MBOXQ_t *mbox;
5748        uint32_t length;
5749        struct lpfc_id_range *id_array = NULL;
5750        void *virtaddr = NULL;
5751        struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5752        struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5753        struct list_head *ext_blk_list;
5754
5755        rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5756                                            &rsrc_cnt,
5757                                            &rsrc_size);
5758        if (unlikely(rc))
5759                return -EIO;
5760
5761        if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5762                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5763                        "3009 No available Resource Extents "
5764                        "for resource type 0x%x: Count: 0x%x, "
5765                        "Size 0x%x\n", type, rsrc_cnt,
5766                        rsrc_size);
5767                return -ENOMEM;
5768        }
5769
5770        lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5771                        "2903 Post resource extents type-0x%x: "
5772                        "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
5773
5774        mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5775        if (!mbox)
5776                return -ENOMEM;
5777
5778        rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
5779        if (unlikely(rc)) {
5780                rc = -EIO;
5781                goto err_exit;
5782        }
5783
5784        /*
5785         * Figure out where the response is located.  Then get local pointers
5786         * to the response data.  The port does not guarantee to respond to
5787         * all extents counts request so update the local variable with the
5788         * allocated count from the port.
5789         */
5790        if (emb == LPFC_SLI4_MBX_EMBED) {
5791                rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5792                id_array = &rsrc_ext->u.rsp.id[0];
5793                rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5794        } else {
5795                virtaddr = mbox->sge_array->addr[0];
5796                n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5797                rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5798                id_array = &n_rsrc->id;
5799        }
5800
5801        longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5802        rsrc_id_cnt = rsrc_cnt * rsrc_size;
5803
5804        /*
5805         * Based on the resource size and count, correct the base and max
5806         * resource values.
5807         */
5808        length = sizeof(struct lpfc_rsrc_blks);
5809        switch (type) {
5810        case LPFC_RSC_TYPE_FCOE_RPI:
5811                phba->sli4_hba.rpi_bmask = kzalloc(longs *
5812                                                   sizeof(unsigned long),
5813                                                   GFP_KERNEL);
5814                if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5815                        rc = -ENOMEM;
5816                        goto err_exit;
5817                }
5818                phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5819                                                 sizeof(uint16_t),
5820                                                 GFP_KERNEL);
5821                if (unlikely(!phba->sli4_hba.rpi_ids)) {
5822                        kfree(phba->sli4_hba.rpi_bmask);
5823                        rc = -ENOMEM;
5824                        goto err_exit;
5825                }
5826
5827                /*
5828                 * The next_rpi was initialized with the maximum available
5829                 * count but the port may allocate a smaller number.  Catch
5830                 * that case and update the next_rpi.
5831                 */
5832                phba->sli4_hba.next_rpi = rsrc_id_cnt;
5833
5834                /* Initialize local ptrs for common extent processing later. */
5835                bmask = phba->sli4_hba.rpi_bmask;
5836                ids = phba->sli4_hba.rpi_ids;
5837                ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5838                break;
5839        case LPFC_RSC_TYPE_FCOE_VPI:
5840                phba->vpi_bmask = kzalloc(longs *
5841                                          sizeof(unsigned long),
5842                                          GFP_KERNEL);
5843                if (unlikely(!phba->vpi_bmask)) {
5844                        rc = -ENOMEM;
5845                        goto err_exit;
5846                }
5847                phba->vpi_ids = kzalloc(rsrc_id_cnt *
5848                                         sizeof(uint16_t),
5849                                         GFP_KERNEL);
5850                if (unlikely(!phba->vpi_ids)) {
5851                        kfree(phba->vpi_bmask);
5852                        rc = -ENOMEM;
5853                        goto err_exit;
5854                }
5855
5856                /* Initialize local ptrs for common extent processing later. */
5857                bmask = phba->vpi_bmask;
5858                ids = phba->vpi_ids;
5859                ext_blk_list = &phba->lpfc_vpi_blk_list;
5860                break;
5861        case LPFC_RSC_TYPE_FCOE_XRI:
5862                phba->sli4_hba.xri_bmask = kzalloc(longs *
5863                                                   sizeof(unsigned long),
5864                                                   GFP_KERNEL);
5865                if (unlikely(!phba->sli4_hba.xri_bmask)) {
5866                        rc = -ENOMEM;
5867                        goto err_exit;
5868                }
5869                phba->sli4_hba.max_cfg_param.xri_used = 0;
5870                phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5871                                                 sizeof(uint16_t),
5872                                                 GFP_KERNEL);
5873                if (unlikely(!phba->sli4_hba.xri_ids)) {
5874                        kfree(phba->sli4_hba.xri_bmask);
5875                        rc = -ENOMEM;
5876                        goto err_exit;
5877                }
5878
5879                /* Initialize local ptrs for common extent processing later. */
5880                bmask = phba->sli4_hba.xri_bmask;
5881                ids = phba->sli4_hba.xri_ids;
5882                ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5883                break;
5884        case LPFC_RSC_TYPE_FCOE_VFI:
5885                phba->sli4_hba.vfi_bmask = kzalloc(longs *
5886                                                   sizeof(unsigned long),
5887                                                   GFP_KERNEL);
5888                if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5889                        rc = -ENOMEM;
5890                        goto err_exit;
5891                }
5892                phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5893                                                 sizeof(uint16_t),
5894                                                 GFP_KERNEL);
5895                if (unlikely(!phba->sli4_hba.vfi_ids)) {
5896                        kfree(phba->sli4_hba.vfi_bmask);
5897                        rc = -ENOMEM;
5898                        goto err_exit;
5899                }
5900
5901                /* Initialize local ptrs for common extent processing later. */
5902                bmask = phba->sli4_hba.vfi_bmask;
5903                ids = phba->sli4_hba.vfi_ids;
5904                ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5905                break;
5906        default:
5907                /* Unsupported Opcode.  Fail call. */
5908                id_array = NULL;
5909                bmask = NULL;
5910                ids = NULL;
5911                ext_blk_list = NULL;
5912                goto err_exit;
5913        }
5914
5915        /*
5916         * Complete initializing the extent configuration with the
5917         * allocated ids assigned to this function.  The bitmask serves
5918         * as an index into the array and manages the available ids.  The
5919         * array just stores the ids communicated to the port via the wqes.
5920         */
5921        for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5922                if ((i % 2) == 0)
5923                        rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5924                                         &id_array[k]);
5925                else
5926                        rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5927                                         &id_array[k]);
5928
5929                rsrc_blks = kzalloc(length, GFP_KERNEL);
5930                if (unlikely(!rsrc_blks)) {
5931                        rc = -ENOMEM;
5932                        kfree(bmask);
5933                        kfree(ids);
5934                        goto err_exit;
5935                }
5936                rsrc_blks->rsrc_start = rsrc_id;
5937                rsrc_blks->rsrc_size = rsrc_size;
5938                list_add_tail(&rsrc_blks->list, ext_blk_list);
5939                rsrc_start = rsrc_id;
5940                if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
5941                        phba->sli4_hba.scsi_xri_start = rsrc_start +
5942                                lpfc_sli4_get_iocb_cnt(phba);
5943                        phba->sli4_hba.nvme_xri_start =
5944                                phba->sli4_hba.scsi_xri_start +
5945                                phba->sli4_hba.scsi_xri_max;
5946                }
5947
5948                while (rsrc_id < (rsrc_start + rsrc_size)) {
5949                        ids[j] = rsrc_id;
5950                        rsrc_id++;
5951                        j++;
5952                }
5953                /* Entire word processed.  Get next word.*/
5954                if ((i % 2) == 1)
5955                        k++;
5956        }
5957 err_exit:
5958        lpfc_sli4_mbox_cmd_free(phba, mbox);
5959        return rc;
5960}
5961
5962
5963
5964/**
5965 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5966 * @phba: Pointer to HBA context object.
5967 * @type: the extent's type.
5968 *
5969 * This function deallocates all extents of a particular resource type.
5970 * SLI4 does not allow for deallocating a particular extent range.  It
5971 * is the caller's responsibility to release all kernel memory resources.
5972 **/
5973static int
5974lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5975{
5976        int rc;
5977        uint32_t length, mbox_tmo = 0;
5978        LPFC_MBOXQ_t *mbox;
5979        struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5980        struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5981
5982        mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5983        if (!mbox)
5984                return -ENOMEM;
5985
5986        /*
5987         * This function sends an embedded mailbox because it only sends the
5988         * the resource type.  All extents of this type are released by the
5989         * port.
5990         */
5991        length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5992                  sizeof(struct lpfc_sli4_cfg_mhdr));
5993        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5994                         LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5995                         length, LPFC_SLI4_MBX_EMBED);
5996
5997        /* Send an extents count of 0 - the dealloc doesn't use it. */
5998        rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5999                                        LPFC_SLI4_MBX_EMBED);
6000        if (unlikely(rc)) {
6001                rc = -EIO;
6002                goto out_free_mbox;
6003        }
6004        if (!phba->sli4_hba.intr_enable)
6005                rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6006        else {
6007                mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6008                rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6009        }
6010        if (unlikely(rc)) {
6011                rc = -EIO;
6012                goto out_free_mbox;
6013        }
6014
6015        dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6016        if (bf_get(lpfc_mbox_hdr_status,
6017                   &dealloc_rsrc->header.cfg_shdr.response)) {
6018                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6019                                "2919 Failed to release resource extents "
6020                                "for type %d - Status 0x%x Add'l Status 0x%x. "
6021                                "Resource memory not released.\n",
6022                                type,
6023                                bf_get(lpfc_mbox_hdr_status,
6024                                    &dealloc_rsrc->header.cfg_shdr.response),
6025                                bf_get(lpfc_mbox_hdr_add_status,
6026                                    &dealloc_rsrc->header.cfg_shdr.response));
6027                rc = -EIO;
6028                goto out_free_mbox;
6029        }
6030
6031        /* Release kernel memory resources for the specific type. */
6032        switch (type) {
6033        case LPFC_RSC_TYPE_FCOE_VPI:
6034                kfree(phba->vpi_bmask);
6035                kfree(phba->vpi_ids);
6036                bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6037                list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6038                                    &phba->lpfc_vpi_blk_list, list) {
6039                        list_del_init(&rsrc_blk->list);
6040                        kfree(rsrc_blk);
6041                }
6042                phba->sli4_hba.max_cfg_param.vpi_used = 0;
6043                break;
6044        case LPFC_RSC_TYPE_FCOE_XRI:
6045                kfree(phba->sli4_hba.xri_bmask);
6046                kfree(phba->sli4_hba.xri_ids);
6047                list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6048                                    &phba->sli4_hba.lpfc_xri_blk_list, list) {
6049                        list_del_init(&rsrc_blk->list);
6050                        kfree(rsrc_blk);
6051                }
6052                break;
6053        case LPFC_RSC_TYPE_FCOE_VFI:
6054                kfree(phba->sli4_hba.vfi_bmask);
6055                kfree(phba->sli4_hba.vfi_ids);
6056                bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6057                list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6058                                    &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6059                        list_del_init(&rsrc_blk->list);
6060                        kfree(rsrc_blk);
6061                }
6062                break;
6063        case LPFC_RSC_TYPE_FCOE_RPI:
6064                /* RPI bitmask and physical id array are cleaned up earlier. */
6065                list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6066                                    &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6067                        list_del_init(&rsrc_blk->list);
6068                        kfree(rsrc_blk);
6069                }
6070                break;
6071        default:
6072                break;
6073        }
6074
6075        bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6076
6077 out_free_mbox:
6078        mempool_free(mbox, phba->mbox_mem_pool);
6079        return rc;
6080}
6081
6082static void
6083lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6084                  uint32_t feature)
6085{
6086        uint32_t len;
6087
6088        len = sizeof(struct lpfc_mbx_set_feature) -
6089                sizeof(struct lpfc_sli4_cfg_mhdr);
6090        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6091                         LPFC_MBOX_OPCODE_SET_FEATURES, len,
6092                         LPFC_SLI4_MBX_EMBED);
6093
6094        switch (feature) {
6095        case LPFC_SET_UE_RECOVERY:
6096                bf_set(lpfc_mbx_set_feature_UER,
6097                       &mbox->u.mqe.un.set_feature, 1);
6098                mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6099                mbox->u.mqe.un.set_feature.param_len = 8;
6100                break;
6101        case LPFC_SET_MDS_DIAGS:
6102                bf_set(lpfc_mbx_set_feature_mds,
6103                       &mbox->u.mqe.un.set_feature, 1);
6104                bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
6105                       &mbox->u.mqe.un.set_feature, 1);
6106                mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6107                mbox->u.mqe.un.set_feature.param_len = 8;
6108                break;
6109        }
6110
6111        return;
6112}
6113
6114/**
6115 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6116 * @phba: Pointer to HBA context object.
6117 *
6118 * This function allocates all SLI4 resource identifiers.
6119 **/
6120int
6121lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6122{
6123        int i, rc, error = 0;
6124        uint16_t count, base;
6125        unsigned long longs;
6126
6127        if (!phba->sli4_hba.rpi_hdrs_in_use)
6128                phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6129        if (phba->sli4_hba.extents_in_use) {
6130                /*
6131                 * The port supports resource extents. The XRI, VPI, VFI, RPI
6132                 * resource extent count must be read and allocated before
6133                 * provisioning the resource id arrays.
6134                 */
6135                if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6136                    LPFC_IDX_RSRC_RDY) {
6137                        /*
6138                         * Extent-based resources are set - the driver could
6139                         * be in a port reset. Figure out if any corrective
6140                         * actions need to be taken.
6141                         */
6142                        rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6143                                                 LPFC_RSC_TYPE_FCOE_VFI);
6144                        if (rc != 0)
6145                                error++;
6146                        rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6147                                                 LPFC_RSC_TYPE_FCOE_VPI);
6148                        if (rc != 0)
6149                                error++;
6150                        rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6151                                                 LPFC_RSC_TYPE_FCOE_XRI);
6152                        if (rc != 0)
6153                                error++;
6154                        rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6155                                                 LPFC_RSC_TYPE_FCOE_RPI);
6156                        if (rc != 0)
6157                                error++;
6158
6159                        /*
6160                         * It's possible that the number of resources
6161                         * provided to this port instance changed between
6162                         * resets.  Detect this condition and reallocate
6163                         * resources.  Otherwise, there is no action.
6164                         */
6165                        if (error) {
6166                                lpfc_printf_log(phba, KERN_INFO,
6167                                                LOG_MBOX | LOG_INIT,
6168                                                "2931 Detected extent resource "
6169                                                "change.  Reallocating all "
6170                                                "extents.\n");
6171                                rc = lpfc_sli4_dealloc_extent(phba,
6172                                                 LPFC_RSC_TYPE_FCOE_VFI);
6173                                rc = lpfc_sli4_dealloc_extent(phba,
6174                                                 LPFC_RSC_TYPE_FCOE_VPI);
6175                                rc = lpfc_sli4_dealloc_extent(phba,
6176                                                 LPFC_RSC_TYPE_FCOE_XRI);
6177                                rc = lpfc_sli4_dealloc_extent(phba,
6178                                                 LPFC_RSC_TYPE_FCOE_RPI);
6179                        } else
6180                                return 0;
6181                }
6182
6183                rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6184                if (unlikely(rc))
6185                        goto err_exit;
6186
6187                rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6188                if (unlikely(rc))
6189                        goto err_exit;
6190
6191                rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6192                if (unlikely(rc))
6193                        goto err_exit;
6194
6195                rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6196                if (unlikely(rc))
6197                        goto err_exit;
6198                bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6199                       LPFC_IDX_RSRC_RDY);
6200                return rc;
6201        } else {
6202                /*
6203                 * The port does not support resource extents.  The XRI, VPI,
6204                 * VFI, RPI resource ids were determined from READ_CONFIG.
6205                 * Just allocate the bitmasks and provision the resource id
6206                 * arrays.  If a port reset is active, the resources don't
6207                 * need any action - just exit.
6208                 */
6209                if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6210                    LPFC_IDX_RSRC_RDY) {
6211                        lpfc_sli4_dealloc_resource_identifiers(phba);
6212                        lpfc_sli4_remove_rpis(phba);
6213                }
6214                /* RPIs. */
6215                count = phba->sli4_hba.max_cfg_param.max_rpi;
6216                if (count <= 0) {
6217                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6218                                        "3279 Invalid provisioning of "
6219                                        "rpi:%d\n", count);
6220                        rc = -EINVAL;
6221                        goto err_exit;
6222                }
6223                base = phba->sli4_hba.max_cfg_param.rpi_base;
6224                longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6225                phba->sli4_hba.rpi_bmask = kzalloc(longs *
6226                                                   sizeof(unsigned long),
6227                                                   GFP_KERNEL);
6228                if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6229                        rc = -ENOMEM;
6230                        goto err_exit;
6231                }
6232                phba->sli4_hba.rpi_ids = kzalloc(count *
6233                                                 sizeof(uint16_t),
6234                                                 GFP_KERNEL);
6235                if (unlikely(!phba->sli4_hba.rpi_ids)) {
6236                        rc = -ENOMEM;
6237                        goto free_rpi_bmask;
6238                }
6239
6240                for (i = 0; i < count; i++)
6241                        phba->sli4_hba.rpi_ids[i] = base + i;
6242
6243                /* VPIs. */
6244                count = phba->sli4_hba.max_cfg_param.max_vpi;
6245                if (count <= 0) {
6246                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6247                                        "3280 Invalid provisioning of "
6248                                        "vpi:%d\n", count);
6249                        rc = -EINVAL;
6250                        goto free_rpi_ids;
6251                }
6252                base = phba->sli4_hba.max_cfg_param.vpi_base;
6253                longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6254                phba->vpi_bmask = kzalloc(longs *
6255                                          sizeof(unsigned long),
6256                                          GFP_KERNEL);
6257                if (unlikely(!phba->vpi_bmask)) {
6258                        rc = -ENOMEM;
6259                        goto free_rpi_ids;
6260                }
6261                phba->vpi_ids = kzalloc(count *
6262                                        sizeof(uint16_t),
6263                                        GFP_KERNEL);
6264                if (unlikely(!phba->vpi_ids)) {
6265                        rc = -ENOMEM;
6266                        goto free_vpi_bmask;
6267                }
6268
6269                for (i = 0; i < count; i++)
6270                        phba->vpi_ids[i] = base + i;
6271
6272                /* XRIs. */
6273                count = phba->sli4_hba.max_cfg_param.max_xri;
6274                if (count <= 0) {
6275                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6276                                        "3281 Invalid provisioning of "
6277                                        "xri:%d\n", count);
6278                        rc = -EINVAL;
6279                        goto free_vpi_ids;
6280                }
6281                base = phba->sli4_hba.max_cfg_param.xri_base;
6282                longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6283                phba->sli4_hba.xri_bmask = kzalloc(longs *
6284                                                   sizeof(unsigned long),
6285                                                   GFP_KERNEL);
6286                if (unlikely(!phba->sli4_hba.xri_bmask)) {
6287                        rc = -ENOMEM;
6288                        goto free_vpi_ids;
6289                }
6290                phba->sli4_hba.max_cfg_param.xri_used = 0;
6291                phba->sli4_hba.xri_ids = kzalloc(count *
6292                                                 sizeof(uint16_t),
6293                                                 GFP_KERNEL);
6294                if (unlikely(!phba->sli4_hba.xri_ids)) {
6295                        rc = -ENOMEM;
6296                        goto free_xri_bmask;
6297                }
6298
6299                for (i = 0; i < count; i++)
6300                        phba->sli4_hba.xri_ids[i] = base + i;
6301
6302                /* VFIs. */
6303                count = phba->sli4_hba.max_cfg_param.max_vfi;
6304                if (count <= 0) {
6305                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6306                                        "3282 Invalid provisioning of "
6307                                        "vfi:%d\n", count);
6308                        rc = -EINVAL;
6309                        goto free_xri_ids;
6310                }
6311                base = phba->sli4_hba.max_cfg_param.vfi_base;
6312                longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6313                phba->sli4_hba.vfi_bmask = kzalloc(longs *
6314                                                   sizeof(unsigned long),
6315                                                   GFP_KERNEL);
6316                if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6317                        rc = -ENOMEM;
6318                        goto free_xri_ids;
6319                }
6320                phba->sli4_hba.vfi_ids = kzalloc(count *
6321                                                 sizeof(uint16_t),
6322                                                 GFP_KERNEL);
6323                if (unlikely(!phba->sli4_hba.vfi_ids)) {
6324                        rc = -ENOMEM;
6325                        goto free_vfi_bmask;
6326                }
6327
6328                for (i = 0; i < count; i++)
6329                        phba->sli4_hba.vfi_ids[i] = base + i;
6330
6331                /*
6332                 * Mark all resources ready.  An HBA reset doesn't need
6333                 * to reset the initialization.
6334                 */
6335                bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6336                       LPFC_IDX_RSRC_RDY);
6337                return 0;
6338        }
6339
6340 free_vfi_bmask:
6341        kfree(phba->sli4_hba.vfi_bmask);
6342        phba->sli4_hba.vfi_bmask = NULL;
6343 free_xri_ids:
6344        kfree(phba->sli4_hba.xri_ids);
6345        phba->sli4_hba.xri_ids = NULL;
6346 free_xri_bmask:
6347        kfree(phba->sli4_hba.xri_bmask);
6348        phba->sli4_hba.xri_bmask = NULL;
6349 free_vpi_ids:
6350        kfree(phba->vpi_ids);
6351        phba->vpi_ids = NULL;
6352 free_vpi_bmask:
6353        kfree(phba->vpi_bmask);
6354        phba->vpi_bmask = NULL;
6355 free_rpi_ids:
6356        kfree(phba->sli4_hba.rpi_ids);
6357        phba->sli4_hba.rpi_ids = NULL;
6358 free_rpi_bmask:
6359        kfree(phba->sli4_hba.rpi_bmask);
6360        phba->sli4_hba.rpi_bmask = NULL;
6361 err_exit:
6362        return rc;
6363}
6364
6365/**
6366 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6367 * @phba: Pointer to HBA context object.
6368 *
6369 * This function allocates the number of elements for the specified
6370 * resource type.
6371 **/
6372int
6373lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6374{
6375        if (phba->sli4_hba.extents_in_use) {
6376                lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6377                lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6378                lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6379                lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6380        } else {
6381                kfree(phba->vpi_bmask);
6382                phba->sli4_hba.max_cfg_param.vpi_used = 0;
6383                kfree(phba->vpi_ids);
6384                bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6385                kfree(phba->sli4_hba.xri_bmask);
6386                kfree(phba->sli4_hba.xri_ids);
6387                kfree(phba->sli4_hba.vfi_bmask);
6388                kfree(phba->sli4_hba.vfi_ids);
6389                bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6390                bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6391        }
6392
6393        return 0;
6394}
6395
6396/**
6397 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6398 * @phba: Pointer to HBA context object.
6399 * @type: The resource extent type.
6400 * @extnt_count: buffer to hold port extent count response
6401 * @extnt_size: buffer to hold port extent size response.
6402 *
6403 * This function calls the port to read the host allocated extents
6404 * for a particular type.
6405 **/
6406int
6407lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6408                               uint16_t *extnt_cnt, uint16_t *extnt_size)
6409{
6410        bool emb;
6411        int rc = 0;
6412        uint16_t curr_blks = 0;
6413        uint32_t req_len, emb_len;
6414        uint32_t alloc_len, mbox_tmo;
6415        struct list_head *blk_list_head;
6416        struct lpfc_rsrc_blks *rsrc_blk;
6417        LPFC_MBOXQ_t *mbox;
6418        void *virtaddr = NULL;
6419        struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6420        struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6421        union  lpfc_sli4_cfg_shdr *shdr;
6422
6423        switch (type) {
6424        case LPFC_RSC_TYPE_FCOE_VPI:
6425                blk_list_head = &phba->lpfc_vpi_blk_list;
6426                break;
6427        case LPFC_RSC_TYPE_FCOE_XRI:
6428                blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6429                break;
6430        case LPFC_RSC_TYPE_FCOE_VFI:
6431                blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6432                break;
6433        case LPFC_RSC_TYPE_FCOE_RPI:
6434                blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6435                break;
6436        default:
6437                return -EIO;
6438        }
6439
6440        /* Count the number of extents currently allocatd for this type. */
6441        list_for_each_entry(rsrc_blk, blk_list_head, list) {
6442                if (curr_blks == 0) {
6443                        /*
6444                         * The GET_ALLOCATED mailbox does not return the size,
6445                         * just the count.  The size should be just the size
6446                         * stored in the current allocated block and all sizes
6447                         * for an extent type are the same so set the return
6448                         * value now.
6449                         */
6450                        *extnt_size = rsrc_blk->rsrc_size;
6451                }
6452                curr_blks++;
6453        }
6454
6455        /*
6456         * Calculate the size of an embedded mailbox.  The uint32_t
6457         * accounts for extents-specific word.
6458         */
6459        emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6460                sizeof(uint32_t);
6461
6462        /*
6463         * Presume the allocation and response will fit into an embedded
6464         * mailbox.  If not true, reconfigure to a non-embedded mailbox.
6465         */
6466        emb = LPFC_SLI4_MBX_EMBED;
6467        req_len = emb_len;
6468        if (req_len > emb_len) {
6469                req_len = curr_blks * sizeof(uint16_t) +
6470                        sizeof(union lpfc_sli4_cfg_shdr) +
6471                        sizeof(uint32_t);
6472                emb = LPFC_SLI4_MBX_NEMBED;
6473        }
6474
6475        mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6476        if (!mbox)
6477                return -ENOMEM;
6478        memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6479
6480        alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6481                                     LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6482                                     req_len, emb);
6483        if (alloc_len < req_len) {
6484                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6485                        "2983 Allocated DMA memory size (x%x) is "
6486                        "less than the requested DMA memory "
6487                        "size (x%x)\n", alloc_len, req_len);
6488                rc = -ENOMEM;
6489                goto err_exit;
6490        }
6491        rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6492        if (unlikely(rc)) {
6493                rc = -EIO;
6494                goto err_exit;
6495        }
6496
6497        if (!phba->sli4_hba.intr_enable)
6498                rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6499        else {
6500                mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6501                rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6502        }
6503
6504        if (unlikely(rc)) {
6505                rc = -EIO;
6506                goto err_exit;
6507        }
6508
6509        /*
6510         * Figure out where the response is located.  Then get local pointers
6511         * to the response data.  The port does not guarantee to respond to
6512         * all extents counts request so update the local variable with the
6513         * allocated count from the port.
6514         */
6515        if (emb == LPFC_SLI4_MBX_EMBED) {
6516                rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6517                shdr = &rsrc_ext->header.cfg_shdr;
6518                *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6519        } else {
6520                virtaddr = mbox->sge_array->addr[0];
6521                n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6522                shdr = &n_rsrc->cfg_shdr;
6523                *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6524        }
6525
6526        if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6527                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6528                        "2984 Failed to read allocated resources "
6529                        "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6530                        type,
6531                        bf_get(lpfc_mbox_hdr_status, &shdr->response),
6532                        bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6533                rc = -EIO;
6534                goto err_exit;
6535        }
6536 err_exit:
6537        lpfc_sli4_mbox_cmd_free(phba, mbox);
6538        return rc;
6539}
6540
6541/**
6542 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
6543 * @phba: pointer to lpfc hba data structure.
6544 * @pring: Pointer to driver SLI ring object.
6545 * @sgl_list: linked link of sgl buffers to post
6546 * @cnt: number of linked list buffers
6547 *
6548 * This routine walks the list of buffers that have been allocated and
6549 * repost them to the port by using SGL block post. This is needed after a
6550 * pci_function_reset/warm_start or start. It attempts to construct blocks
6551 * of buffer sgls which contains contiguous xris and uses the non-embedded
6552 * SGL block post mailbox commands to post them to the port. For single
6553 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6554 * mailbox command for posting.
6555 *
6556 * Returns: 0 = success, non-zero failure.
6557 **/
6558static int
6559lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6560                          struct list_head *sgl_list, int cnt)
6561{
6562        struct lpfc_sglq *sglq_entry = NULL;
6563        struct lpfc_sglq *sglq_entry_next = NULL;
6564        struct lpfc_sglq *sglq_entry_first = NULL;
6565        int status, total_cnt;
6566        int post_cnt = 0, num_posted = 0, block_cnt = 0;
6567        int last_xritag = NO_XRI;
6568        LIST_HEAD(prep_sgl_list);
6569        LIST_HEAD(blck_sgl_list);
6570        LIST_HEAD(allc_sgl_list);
6571        LIST_HEAD(post_sgl_list);
6572        LIST_HEAD(free_sgl_list);
6573
6574        spin_lock_irq(&phba->hbalock);
6575        spin_lock(&phba->sli4_hba.sgl_list_lock);
6576        list_splice_init(sgl_list, &allc_sgl_list);
6577        spin_unlock(&phba->sli4_hba.sgl_list_lock);
6578        spin_unlock_irq(&phba->hbalock);
6579
6580        total_cnt = cnt;
6581        list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6582                                 &allc_sgl_list, list) {
6583                list_del_init(&sglq_entry->list);
6584                block_cnt++;
6585                if ((last_xritag != NO_XRI) &&
6586                    (sglq_entry->sli4_xritag != last_xritag + 1)) {
6587                        /* a hole in xri block, form a sgl posting block */
6588                        list_splice_init(&prep_sgl_list, &blck_sgl_list);
6589                        post_cnt = block_cnt - 1;
6590                        /* prepare list for next posting block */
6591                        list_add_tail(&sglq_entry->list, &prep_sgl_list);
6592                        block_cnt = 1;
6593                } else {
6594                        /* prepare list for next posting block */
6595                        list_add_tail(&sglq_entry->list, &prep_sgl_list);
6596                        /* enough sgls for non-embed sgl mbox command */
6597                        if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6598                                list_splice_init(&prep_sgl_list,
6599                                                 &blck_sgl_list);
6600                                post_cnt = block_cnt;
6601                                block_cnt = 0;
6602                        }
6603                }
6604                num_posted++;
6605
6606                /* keep track of last sgl's xritag */
6607                last_xritag = sglq_entry->sli4_xritag;
6608
6609                /* end of repost sgl list condition for buffers */
6610                if (num_posted == total_cnt) {
6611                        if (post_cnt == 0) {
6612                                list_splice_init(&prep_sgl_list,
6613                                                 &blck_sgl_list);
6614                                post_cnt = block_cnt;
6615                        } else if (block_cnt == 1) {
6616                                status = lpfc_sli4_post_sgl(phba,
6617                                                sglq_entry->phys, 0,
6618                                                sglq_entry->sli4_xritag);
6619                                if (!status) {
6620                                        /* successful, put sgl to posted list */
6621                                        list_add_tail(&sglq_entry->list,
6622                                                      &post_sgl_list);
6623                                } else {
6624                                        /* Failure, put sgl to free list */
6625                                        lpfc_printf_log(phba, KERN_WARNING,
6626                                                LOG_SLI,
6627                                                "3159 Failed to post "
6628                                                "sgl, xritag:x%x\n",
6629                                                sglq_entry->sli4_xritag);
6630                                        list_add_tail(&sglq_entry->list,
6631                                                      &free_sgl_list);
6632                                        total_cnt--;
6633                                }
6634                        }
6635                }
6636
6637                /* continue until a nembed page worth of sgls */
6638                if (post_cnt == 0)
6639                        continue;
6640
6641                /* post the buffer list sgls as a block */
6642                status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6643                                                 post_cnt);
6644
6645                if (!status) {
6646                        /* success, put sgl list to posted sgl list */
6647                        list_splice_init(&blck_sgl_list, &post_sgl_list);
6648                } else {
6649                        /* Failure, put sgl list to free sgl list */
6650                        sglq_entry_first = list_first_entry(&blck_sgl_list,
6651                                                            struct lpfc_sglq,
6652                                                            list);
6653                        lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6654                                        "3160 Failed to post sgl-list, "
6655                                        "xritag:x%x-x%x\n",
6656                                        sglq_entry_first->sli4_xritag,
6657                                        (sglq_entry_first->sli4_xritag +
6658                                         post_cnt - 1));
6659                        list_splice_init(&blck_sgl_list, &free_sgl_list);
6660                        total_cnt -= post_cnt;
6661                }
6662
6663                /* don't reset xirtag due to hole in xri block */
6664                if (block_cnt == 0)
6665                        last_xritag = NO_XRI;
6666
6667                /* reset sgl post count for next round of posting */
6668                post_cnt = 0;
6669        }
6670
6671        /* free the sgls failed to post */
6672        lpfc_free_sgl_list(phba, &free_sgl_list);
6673
6674        /* push sgls posted to the available list */
6675        if (!list_empty(&post_sgl_list)) {
6676                spin_lock_irq(&phba->hbalock);
6677                spin_lock(&phba->sli4_hba.sgl_list_lock);
6678                list_splice_init(&post_sgl_list, sgl_list);
6679                spin_unlock(&phba->sli4_hba.sgl_list_lock);
6680                spin_unlock_irq(&phba->hbalock);
6681        } else {
6682                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6683                                "3161 Failure to post sgl to port.\n");
6684                return -EIO;
6685        }
6686
6687        /* return the number of XRIs actually posted */
6688        return total_cnt;
6689}
6690
6691void
6692lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6693{
6694        uint32_t len;
6695
6696        len = sizeof(struct lpfc_mbx_set_host_data) -
6697                sizeof(struct lpfc_sli4_cfg_mhdr);
6698        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6699                         LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6700                         LPFC_SLI4_MBX_EMBED);
6701
6702        mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
6703        mbox->u.mqe.un.set_host_data.param_len =
6704                                        LPFC_HOST_OS_DRIVER_VERSION_SIZE;
6705        snprintf(mbox->u.mqe.un.set_host_data.data,
6706                 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6707                 "Linux %s v"LPFC_DRIVER_VERSION,
6708                 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6709}
6710
6711int
6712lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
6713                    struct lpfc_queue *drq, int count, int idx)
6714{
6715        int rc, i;
6716        struct lpfc_rqe hrqe;
6717        struct lpfc_rqe drqe;
6718        struct lpfc_rqb *rqbp;
6719        unsigned long flags;
6720        struct rqb_dmabuf *rqb_buffer;
6721        LIST_HEAD(rqb_buf_list);
6722
6723        spin_lock_irqsave(&phba->hbalock, flags);
6724        rqbp = hrq->rqbp;
6725        for (i = 0; i < count; i++) {
6726                /* IF RQ is already full, don't bother */
6727                if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6728                        break;
6729                rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6730                if (!rqb_buffer)
6731                        break;
6732                rqb_buffer->hrq = hrq;
6733                rqb_buffer->drq = drq;
6734                rqb_buffer->idx = idx;
6735                list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6736        }
6737        while (!list_empty(&rqb_buf_list)) {
6738                list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6739                                 hbuf.list);
6740
6741                hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6742                hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6743                drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6744                drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6745                rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6746                if (rc < 0) {
6747                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6748                                        "6421 Cannot post to HRQ %d: %x %x %x "
6749                                        "DRQ %x %x\n",
6750                                        hrq->queue_id,
6751                                        hrq->host_index,
6752                                        hrq->hba_index,
6753                                        hrq->entry_count,
6754                                        drq->host_index,
6755                                        drq->hba_index);
6756                        rqbp->rqb_free_buffer(phba, rqb_buffer);
6757                } else {
6758                        list_add_tail(&rqb_buffer->hbuf.list,
6759                                      &rqbp->rqb_buffer_list);
6760                        rqbp->buffer_count++;
6761                }
6762        }
6763        spin_unlock_irqrestore(&phba->hbalock, flags);
6764        return 1;
6765}
6766
6767/**
6768 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
6769 * @phba: Pointer to HBA context object.
6770 *
6771 * This function is the main SLI4 device initialization PCI function. This
6772 * function is called by the HBA initialization code, HBA reset code and
6773 * HBA error attention handler code. Caller is not required to hold any
6774 * locks.
6775 **/
6776int
6777lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6778{
6779        int rc, i, cnt;
6780        LPFC_MBOXQ_t *mboxq;
6781        struct lpfc_mqe *mqe;
6782        uint8_t *vpd;
6783        uint32_t vpd_size;
6784        uint32_t ftr_rsp = 0;
6785        struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6786        struct lpfc_vport *vport = phba->pport;
6787        struct lpfc_dmabuf *mp;
6788        struct lpfc_rqb *rqbp;
6789
6790        /* Perform a PCI function reset to start from clean */
6791        rc = lpfc_pci_function_reset(phba);
6792        if (unlikely(rc))
6793                return -ENODEV;
6794
6795        /* Check the HBA Host Status Register for readyness */
6796        rc = lpfc_sli4_post_status_check(phba);
6797        if (unlikely(rc))
6798                return -ENODEV;
6799        else {
6800                spin_lock_irq(&phba->hbalock);
6801                phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6802                spin_unlock_irq(&phba->hbalock);
6803        }
6804
6805        /*
6806         * Allocate a single mailbox container for initializing the
6807         * port.
6808         */
6809        mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6810        if (!mboxq)
6811                return -ENOMEM;
6812
6813        /* Issue READ_REV to collect vpd and FW information. */
6814        vpd_size = SLI4_PAGE_SIZE;
6815        vpd = kzalloc(vpd_size, GFP_KERNEL);
6816        if (!vpd) {
6817                rc = -ENOMEM;
6818                goto out_free_mbox;
6819        }
6820
6821        rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
6822        if (unlikely(rc)) {
6823                kfree(vpd);
6824                goto out_free_mbox;
6825        }
6826
6827        mqe = &mboxq->u.mqe;
6828        phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6829        if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
6830                phba->hba_flag |= HBA_FCOE_MODE;
6831                phba->fcp_embed_io = 0; /* SLI4 FC support only */
6832        } else {
6833                phba->hba_flag &= ~HBA_FCOE_MODE;
6834        }
6835
6836        if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6837                LPFC_DCBX_CEE_MODE)
6838                phba->hba_flag |= HBA_FIP_SUPPORT;
6839        else
6840                phba->hba_flag &= ~HBA_FIP_SUPPORT;
6841
6842        phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6843
6844        if (phba->sli_rev != LPFC_SLI_REV4) {
6845                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6846                        "0376 READ_REV Error. SLI Level %d "
6847                        "FCoE enabled %d\n",
6848                        phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
6849                rc = -EIO;
6850                kfree(vpd);
6851                goto out_free_mbox;
6852        }
6853
6854        /*
6855         * Continue initialization with default values even if driver failed
6856         * to read FCoE param config regions, only read parameters if the
6857         * board is FCoE
6858         */
6859        if (phba->hba_flag & HBA_FCOE_MODE &&
6860            lpfc_sli4_read_fcoe_params(phba))
6861                lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6862                        "2570 Failed to read FCoE parameters\n");
6863
6864        /*
6865         * Retrieve sli4 device physical port name, failure of doing it
6866         * is considered as non-fatal.
6867         */
6868        rc = lpfc_sli4_retrieve_pport_name(phba);
6869        if (!rc)
6870                lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6871                                "3080 Successful retrieving SLI4 device "
6872                                "physical port name: %s.\n", phba->Port);
6873
6874        /*
6875         * Evaluate the read rev and vpd data. Populate the driver
6876         * state with the results. If this routine fails, the failure
6877         * is not fatal as the driver will use generic values.
6878         */
6879        rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6880        if (unlikely(!rc)) {
6881                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6882                                "0377 Error %d parsing vpd. "
6883                                "Using defaults.\n", rc);
6884                rc = 0;
6885        }
6886        kfree(vpd);
6887
6888        /* Save information as VPD data */
6889        phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6890        phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6891
6892        /*
6893         * This is because first G7 ASIC doesn't support the standard
6894         * 0x5a NVME cmd descriptor type/subtype
6895         */
6896        if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6897                        LPFC_SLI_INTF_IF_TYPE_6) &&
6898            (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
6899            (phba->vpd.rev.smRev == 0) &&
6900            (phba->cfg_nvme_embed_cmd == 1))
6901                phba->cfg_nvme_embed_cmd = 0;
6902
6903        phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6904        phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6905                                         &mqe->un.read_rev);
6906        phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6907                                       &mqe->un.read_rev);
6908        phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6909                                            &mqe->un.read_rev);
6910        phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6911                                           &mqe->un.read_rev);
6912        phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6913        memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6914        phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6915        memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6916        phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6917        memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6918        lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6919                        "(%d):0380 READ_REV Status x%x "
6920                        "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6921                        mboxq->vport ? mboxq->vport->vpi : 0,
6922                        bf_get(lpfc_mqe_status, mqe),
6923                        phba->vpd.rev.opFwName,
6924                        phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6925                        phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
6926
6927        /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3)  */
6928        rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6929        if (phba->pport->cfg_lun_queue_depth > rc) {
6930                lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6931                                "3362 LUN queue depth changed from %d to %d\n",
6932                                phba->pport->cfg_lun_queue_depth, rc);
6933                phba->pport->cfg_lun_queue_depth = rc;
6934        }
6935
6936        if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6937            LPFC_SLI_INTF_IF_TYPE_0) {
6938                lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6939                rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6940                if (rc == MBX_SUCCESS) {
6941                        phba->hba_flag |= HBA_RECOVERABLE_UE;
6942                        /* Set 1Sec interval to detect UE */
6943                        phba->eratt_poll_interval = 1;
6944                        phba->sli4_hba.ue_to_sr = bf_get(
6945                                        lpfc_mbx_set_feature_UESR,
6946                                        &mboxq->u.mqe.un.set_feature);
6947                        phba->sli4_hba.ue_to_rp = bf_get(
6948                                        lpfc_mbx_set_feature_UERP,
6949                                        &mboxq->u.mqe.un.set_feature);
6950                }
6951        }
6952
6953        if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6954                /* Enable MDS Diagnostics only if the SLI Port supports it */
6955                lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6956                rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6957                if (rc != MBX_SUCCESS)
6958                        phba->mds_diags_support = 0;
6959        }
6960
6961        /*
6962         * Discover the port's supported feature set and match it against the
6963         * hosts requests.
6964         */
6965        lpfc_request_features(phba, mboxq);
6966        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6967        if (unlikely(rc)) {
6968                rc = -EIO;
6969                goto out_free_mbox;
6970        }
6971
6972        /*
6973         * The port must support FCP initiator mode as this is the
6974         * only mode running in the host.
6975         */
6976        if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6977                lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6978                                "0378 No support for fcpi mode.\n");
6979                ftr_rsp++;
6980        }
6981
6982        /* Performance Hints are ONLY for FCoE */
6983        if (phba->hba_flag & HBA_FCOE_MODE) {
6984                if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6985                        phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6986                else
6987                        phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6988        }
6989
6990        /*
6991         * If the port cannot support the host's requested features
6992         * then turn off the global config parameters to disable the
6993         * feature in the driver.  This is not a fatal error.
6994         */
6995        if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
6996                if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
6997                        phba->cfg_enable_bg = 0;
6998                        phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6999                        ftr_rsp++;
7000                }
7001        }
7002
7003        if (phba->max_vpi && phba->cfg_enable_npiv &&
7004            !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7005                ftr_rsp++;
7006
7007        if (ftr_rsp) {
7008                lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7009                                "0379 Feature Mismatch Data: x%08x %08x "
7010                                "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7011                                mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7012                                phba->cfg_enable_npiv, phba->max_vpi);
7013                if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7014                        phba->cfg_enable_bg = 0;
7015                if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7016                        phba->cfg_enable_npiv = 0;
7017        }
7018
7019        /* These SLI3 features are assumed in SLI4 */
7020        spin_lock_irq(&phba->hbalock);
7021        phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7022        spin_unlock_irq(&phba->hbalock);
7023
7024        /*
7025         * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
7026         * calls depends on these resources to complete port setup.
7027         */
7028        rc = lpfc_sli4_alloc_resource_identifiers(phba);
7029        if (rc) {
7030                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7031                                "2920 Failed to alloc Resource IDs "
7032                                "rc = x%x\n", rc);
7033                goto out_free_mbox;
7034        }
7035
7036        lpfc_set_host_data(phba, mboxq);
7037
7038        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7039        if (rc) {
7040                lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7041                                "2134 Failed to set host os driver version %x",
7042                                rc);
7043        }
7044
7045        /* Read the port's service parameters. */
7046        rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7047        if (rc) {
7048                phba->link_state = LPFC_HBA_ERROR;
7049                rc = -ENOMEM;
7050                goto out_free_mbox;
7051        }
7052
7053        mboxq->vport = vport;
7054        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7055        mp = (struct lpfc_dmabuf *) mboxq->context1;
7056        if (rc == MBX_SUCCESS) {
7057                memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7058                rc = 0;
7059        }
7060
7061        /*
7062         * This memory was allocated by the lpfc_read_sparam routine. Release
7063         * it to the mbuf pool.
7064         */
7065        lpfc_mbuf_free(phba, mp->virt, mp->phys);
7066        kfree(mp);
7067        mboxq->context1 = NULL;
7068        if (unlikely(rc)) {
7069                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7070                                "0382 READ_SPARAM command failed "
7071                                "status %d, mbxStatus x%x\n",
7072                                rc, bf_get(lpfc_mqe_status, mqe));
7073                phba->link_state = LPFC_HBA_ERROR;
7074                rc = -EIO;
7075                goto out_free_mbox;
7076        }
7077
7078        lpfc_update_vport_wwn(vport);
7079
7080        /* Update the fc_host data structures with new wwn. */
7081        fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7082        fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7083
7084        /* Create all the SLI4 queues */
7085        rc = lpfc_sli4_queue_create(phba);
7086        if (rc) {
7087                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7088                                "3089 Failed to allocate queues\n");
7089                rc = -ENODEV;
7090                goto out_free_mbox;
7091        }
7092        /* Set up all the queues to the device */
7093        rc = lpfc_sli4_queue_setup(phba);
7094        if (unlikely(rc)) {
7095                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7096                                "0381 Error %d during queue setup.\n ", rc);
7097                goto out_stop_timers;
7098        }
7099        /* Initialize the driver internal SLI layer lists. */
7100        lpfc_sli4_setup(phba);
7101        lpfc_sli4_queue_init(phba);
7102
7103        /* update host els xri-sgl sizes and mappings */
7104        rc = lpfc_sli4_els_sgl_update(phba);
7105        if (unlikely(rc)) {
7106                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7107                                "1400 Failed to update xri-sgl size and "
7108                                "mapping: %d\n", rc);
7109                goto out_destroy_queue;
7110        }
7111
7112        /* register the els sgl pool to the port */
7113        rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7114                                       phba->sli4_hba.els_xri_cnt);
7115        if (unlikely(rc < 0)) {
7116                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7117                                "0582 Error %d during els sgl post "
7118                                "operation\n", rc);
7119                rc = -ENODEV;
7120                goto out_destroy_queue;
7121        }
7122        phba->sli4_hba.els_xri_cnt = rc;
7123
7124        if (phba->nvmet_support) {
7125                /* update host nvmet xri-sgl sizes and mappings */
7126                rc = lpfc_sli4_nvmet_sgl_update(phba);
7127                if (unlikely(rc)) {
7128                        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7129                                        "6308 Failed to update nvmet-sgl size "
7130                                        "and mapping: %d\n", rc);
7131                        goto out_destroy_queue;
7132                }
7133
7134                /* register the nvmet sgl pool to the port */
7135                rc = lpfc_sli4_repost_sgl_list(
7136                        phba,
7137                        &phba->sli4_hba.lpfc_nvmet_sgl_list,
7138                        phba->sli4_hba.nvmet_xri_cnt);
7139                if (unlikely(rc < 0)) {
7140                        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7141                                        "3117 Error %d during nvmet "
7142                                        "sgl post\n", rc);
7143                        rc = -ENODEV;
7144                        goto out_destroy_queue;
7145                }
7146                phba->sli4_hba.nvmet_xri_cnt = rc;
7147
7148                cnt = phba->cfg_iocb_cnt * 1024;
7149                /* We need 1 iocbq for every SGL, for IO processing */
7150                cnt += phba->sli4_hba.nvmet_xri_cnt;
7151        } else {
7152                /* update host scsi xri-sgl sizes and mappings */
7153                rc = lpfc_sli4_scsi_sgl_update(phba);
7154                if (unlikely(rc)) {
7155                        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7156                                        "6309 Failed to update scsi-sgl size "
7157                                        "and mapping: %d\n", rc);
7158                        goto out_destroy_queue;
7159                }
7160
7161                /* update host nvme xri-sgl sizes and mappings */
7162                rc = lpfc_sli4_nvme_sgl_update(phba);
7163                if (unlikely(rc)) {
7164                        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7165                                        "6082 Failed to update nvme-sgl size "
7166                                        "and mapping: %d\n", rc);
7167                        goto out_destroy_queue;
7168                }
7169
7170                cnt = phba->cfg_iocb_cnt * 1024;
7171        }
7172
7173        if (!phba->sli.iocbq_lookup) {
7174                /* Initialize and populate the iocb list per host */
7175                lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7176                                "2821 initialize iocb list %d total %d\n",
7177                                phba->cfg_iocb_cnt, cnt);
7178                rc = lpfc_init_iocb_list(phba, cnt);
7179                if (rc) {
7180                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7181                                        "1413 Failed to init iocb list.\n");
7182                        goto out_destroy_queue;
7183                }
7184        }
7185
7186        if (phba->nvmet_support)
7187                lpfc_nvmet_create_targetport(phba);
7188
7189        if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
7190                /* Post initial buffers to all RQs created */
7191                for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7192                        rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7193                        INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7194                        rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7195                        rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
7196                        rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
7197                        rqbp->buffer_count = 0;
7198
7199                        lpfc_post_rq_buffer(
7200                                phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7201                                phba->sli4_hba.nvmet_mrq_data[i],
7202                                LPFC_NVMET_RQE_DEF_COUNT, i);
7203                }
7204        }
7205
7206        if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7207                /* register the allocated scsi sgl pool to the port */
7208                rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7209                if (unlikely(rc)) {
7210                        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7211                                        "0383 Error %d during scsi sgl post "
7212                                        "operation\n", rc);
7213                        /* Some Scsi buffers were moved to abort scsi list */
7214                        /* A pci function reset will repost them */
7215                        rc = -ENODEV;
7216                        goto out_destroy_queue;
7217                }
7218        }
7219
7220        if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7221            (phba->nvmet_support == 0)) {
7222
7223                /* register the allocated nvme sgl pool to the port */
7224                rc = lpfc_repost_nvme_sgl_list(phba);
7225                if (unlikely(rc)) {
7226                        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7227                                        "6116 Error %d during nvme sgl post "
7228                                        "operation\n", rc);
7229                        /* Some NVME buffers were moved to abort nvme list */
7230                        /* A pci function reset will repost them */
7231                        rc = -ENODEV;
7232                        goto out_destroy_queue;
7233                }
7234        }
7235
7236        /* Post the rpi header region to the device. */
7237        rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7238        if (unlikely(rc)) {
7239                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7240                                "0393 Error %d during rpi post operation\n",
7241                                rc);
7242                rc = -ENODEV;
7243                goto out_destroy_queue;
7244        }
7245        lpfc_sli4_node_prep(phba);
7246
7247        if (!(phba->hba_flag & HBA_FCOE_MODE)) {
7248                if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
7249                        /*
7250                         * The FC Port needs to register FCFI (index 0)
7251                         */
7252                        lpfc_reg_fcfi(phba, mboxq);
7253                        mboxq->vport = phba->pport;
7254                        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7255                        if (rc != MBX_SUCCESS)
7256                                goto out_unset_queue;
7257                        rc = 0;
7258                        phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7259                                                &mboxq->u.mqe.un.reg_fcfi);
7260                } else {
7261                        /* We are a NVME Target mode with MRQ > 1 */
7262
7263                        /* First register the FCFI */
7264                        lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7265                        mboxq->vport = phba->pport;
7266                        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7267                        if (rc != MBX_SUCCESS)
7268                                goto out_unset_queue;
7269                        rc = 0;
7270                        phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7271                                                &mboxq->u.mqe.un.reg_fcfi_mrq);
7272
7273                        /* Next register the MRQs */
7274                        lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7275                        mboxq->vport = phba->pport;
7276                        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7277                        if (rc != MBX_SUCCESS)
7278                                goto out_unset_queue;
7279                        rc = 0;
7280                }
7281                /* Check if the port is configured to be disabled */
7282                lpfc_sli_read_link_ste(phba);
7283        }
7284
7285        /* Arm the CQs and then EQs on device */
7286        lpfc_sli4_arm_cqeq_intr(phba);
7287
7288        /* Indicate device interrupt mode */
7289        phba->sli4_hba.intr_enable = 1;
7290
7291        /* Allow asynchronous mailbox command to go through */
7292        spin_lock_irq(&phba->hbalock);
7293        phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7294        spin_unlock_irq(&phba->hbalock);
7295
7296        /* Post receive buffers to the device */
7297        lpfc_sli4_rb_setup(phba);
7298
7299        /* Reset HBA FCF states after HBA reset */
7300        phba->fcf.fcf_flag = 0;
7301        phba->fcf.current_rec.flag = 0;
7302
7303        /* Start the ELS watchdog timer */
7304        mod_timer(&vport->els_tmofunc,
7305                  jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
7306
7307        /* Start heart beat timer */
7308        mod_timer(&phba->hb_tmofunc,
7309                  jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
7310        phba->hb_outstanding = 0;
7311        phba->last_completion_time = jiffies;
7312
7313        /* Start error attention (ERATT) polling timer */
7314        mod_timer(&phba->eratt_poll,
7315                  jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
7316
7317        /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7318        if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7319                rc = pci_enable_pcie_error_reporting(phba->pcidev);
7320                if (!rc) {
7321                        lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7322                                        "2829 This device supports "
7323                                        "Advanced Error Reporting (AER)\n");
7324                        spin_lock_irq(&phba->hbalock);
7325                        phba->hba_flag |= HBA_AER_ENABLED;
7326                        spin_unlock_irq(&phba->hbalock);
7327                } else {
7328                        lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7329                                        "2830 This device does not support "
7330                                        "Advanced Error Reporting (AER)\n");
7331                        phba->cfg_aer_support = 0;
7332                }
7333                rc = 0;
7334        }
7335
7336        /*
7337         * The port is ready, set the host's link state to LINK_DOWN
7338         * in preparation for link interrupts.
7339         */
7340        spin_lock_irq(&phba->hbalock);
7341        phba->link_state = LPFC_LINK_DOWN;
7342        spin_unlock_irq(&phba->hbalock);
7343        if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7344            (phba->hba_flag & LINK_DISABLED)) {
7345                lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7346                                "3103 Adapter Link is disabled.\n");
7347                lpfc_down_link(phba, mboxq);
7348                rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7349                if (rc != MBX_SUCCESS) {
7350                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7351                                        "3104 Adapter failed to issue "
7352                                        "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7353                        goto out_unset_queue;
7354                }
7355        } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
7356                /* don't perform init_link on SLI4 FC port loopback test */
7357                if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7358                        rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7359                        if (rc)
7360                                goto out_unset_queue;
7361                }
7362        }
7363        mempool_free(mboxq, phba->mbox_mem_pool);
7364        return rc;
7365out_unset_queue:
7366        /* Unset all the queues set up in this routine when error out */
7367        lpfc_sli4_queue_unset(phba);
7368out_destroy_queue:
7369        lpfc_free_iocb_list(phba);
7370        lpfc_sli4_queue_destroy(phba);
7371out_stop_timers:
7372        lpfc_stop_hba_timers(phba);
7373out_free_mbox:
7374        mempool_free(mboxq, phba->mbox_mem_pool);
7375        return rc;
7376}
7377
7378/**
7379 * lpfc_mbox_timeout - Timeout call back function for mbox timer
7380 * @ptr: context object - pointer to hba structure.
7381 *
7382 * This is the callback function for mailbox timer. The mailbox
7383 * timer is armed when a new mailbox command is issued and the timer
7384 * is deleted when the mailbox complete. The function is called by
7385 * the kernel timer code when a mailbox does not complete within
7386 * expected time. This function wakes up the worker thread to
7387 * process the mailbox timeout and returns. All the processing is
7388 * done by the worker thread function lpfc_mbox_timeout_handler.
7389 **/
7390void
7391lpfc_mbox_timeout(struct timer_list *t)
7392{
7393        struct lpfc_hba  *phba = from_timer(phba, t, sli.mbox_tmo);
7394        unsigned long iflag;
7395        uint32_t tmo_posted;
7396
7397        spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
7398        tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
7399        if (!tmo_posted)
7400                phba->pport->work_port_events |= WORKER_MBOX_TMO;
7401        spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7402
7403        if (!tmo_posted)
7404                lpfc_worker_wake_up(phba);
7405        return;
7406}
7407
7408/**
7409 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7410 *                                    are pending
7411 * @phba: Pointer to HBA context object.
7412 *
7413 * This function checks if any mailbox completions are present on the mailbox
7414 * completion queue.
7415 **/
7416static bool
7417lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7418{
7419
7420        uint32_t idx;
7421        struct lpfc_queue *mcq;
7422        struct lpfc_mcqe *mcqe;
7423        bool pending_completions = false;
7424        uint8_t qe_valid;
7425
7426        if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7427                return false;
7428
7429        /* Check for completions on mailbox completion queue */
7430
7431        mcq = phba->sli4_hba.mbx_cq;
7432        idx = mcq->hba_index;
7433        qe_valid = mcq->qe_valid;
7434        while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
7435                mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7436                if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7437                    (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7438                        pending_completions = true;
7439                        break;
7440                }
7441                idx = (idx + 1) % mcq->entry_count;
7442                if (mcq->hba_index == idx)
7443                        break;
7444
7445                /* if the index wrapped around, toggle the valid bit */
7446                if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
7447                        qe_valid = (qe_valid) ? 0 : 1;
7448        }
7449        return pending_completions;
7450
7451}
7452
7453/**
7454 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7455 *                                            that were missed.
7456 * @phba: Pointer to HBA context object.
7457 *
7458 * For sli4, it is possible to miss an interrupt. As such mbox completions
7459 * maybe missed causing erroneous mailbox timeouts to occur. This function
7460 * checks to see if mbox completions are on the mailbox completion queue
7461 * and will process all the completions associated with the eq for the
7462 * mailbox completion queue.
7463 **/
7464bool
7465lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7466{
7467        struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
7468        uint32_t eqidx;
7469        struct lpfc_queue *fpeq = NULL;
7470        struct lpfc_eqe *eqe;
7471        bool mbox_pending;
7472
7473        if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7474                return false;
7475
7476        /* Find the eq associated with the mcq */
7477
7478        if (sli4_hba->hba_eq)
7479                for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
7480                        if (sli4_hba->hba_eq[eqidx]->queue_id ==
7481                            sli4_hba->mbx_cq->assoc_qid) {
7482                                fpeq = sli4_hba->hba_eq[eqidx];
7483                                break;
7484                        }
7485        if (!fpeq)
7486                return false;
7487
7488        /* Turn off interrupts from this EQ */
7489
7490        sli4_hba->sli4_eq_clr_intr(fpeq);
7491
7492        /* Check to see if a mbox completion is pending */
7493
7494        mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7495
7496        /*
7497         * If a mbox completion is pending, process all the events on EQ
7498         * associated with the mbox completion queue (this could include
7499         * mailbox commands, async events, els commands, receive queue data
7500         * and fcp commands)
7501         */
7502
7503        if (mbox_pending)
7504                while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7505                        lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7506                        fpeq->EQ_processed++;
7507                }
7508
7509        /* Always clear and re-arm the EQ */
7510
7511        sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7512
7513        return mbox_pending;
7514
7515}
7516
7517/**
7518 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
7519 * @phba: Pointer to HBA context object.
7520 *
7521 * This function is called from worker thread when a mailbox command times out.
7522 * The caller is not required to hold any locks. This function will reset the
7523 * HBA and recover all the pending commands.
7524 **/
7525void
7526lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7527{
7528        LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
7529        MAILBOX_t *mb = NULL;
7530
7531        struct lpfc_sli *psli = &phba->sli;
7532
7533        /* If the mailbox completed, process the completion and return */
7534        if (lpfc_sli4_process_missed_mbox_completions(phba))
7535                return;
7536
7537        if (pmbox != NULL)
7538                mb = &pmbox->u.mb;
7539        /* Check the pmbox pointer first.  There is a race condition
7540         * between the mbox timeout handler getting executed in the
7541         * worklist and the mailbox actually completing. When this
7542         * race condition occurs, the mbox_active will be NULL.
7543         */
7544        spin_lock_irq(&phba->hbalock);
7545        if (pmbox == NULL) {
7546                lpfc_printf_log(phba, KERN_WARNING,
7547                                LOG_MBOX | LOG_SLI,
7548                                "0353 Active Mailbox cleared - mailbox timeout "
7549                                "exiting\n");
7550                spin_unlock_irq(&phba->hbalock);
7551                return;
7552        }
7553
7554        /* Mbox cmd <mbxCommand> timeout */
7555        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7556                        "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
7557                        mb->mbxCommand,
7558                        phba->pport->port_state,
7559                        phba->sli.sli_flag,
7560                        phba->sli.mbox_active);
7561        spin_unlock_irq(&phba->hbalock);
7562
7563        /* Setting state unknown so lpfc_sli_abort_iocb_ring
7564         * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
7565         * it to fail all outstanding SCSI IO.
7566         */
7567        spin_lock_irq(&phba->pport->work_port_lock);
7568        phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7569        spin_unlock_irq(&phba->pport->work_port_lock);
7570        spin_lock_irq(&phba->hbalock);
7571        phba->link_state = LPFC_LINK_UNKNOWN;
7572        psli->sli_flag &= ~LPFC_SLI_ACTIVE;
7573        spin_unlock_irq(&phba->hbalock);
7574
7575        lpfc_sli_abort_fcp_rings(phba);
7576
7577        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7578                        "0345 Resetting board due to mailbox timeout\n");
7579
7580        /* Reset the HBA device */
7581        lpfc_reset_hba(phba);
7582}
7583
7584/**
7585 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
7586 * @phba: Pointer to HBA context object.
7587 * @pmbox: Pointer to mailbox object.
7588 * @flag: Flag indicating how the mailbox need to be processed.
7589 *
7590 * This function is called by discovery code and HBA management code
7591 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7592 * function gets the hbalock to protect the data structures.
7593 * The mailbox command can be submitted in polling mode, in which case
7594 * this function will wait in a polling loop for the completion of the
7595 * mailbox.
7596 * If the mailbox is submitted in no_wait mode (not polling) the
7597 * function will submit the command and returns immediately without waiting
7598 * for the mailbox completion. The no_wait is supported only when HBA
7599 * is in SLI2/SLI3 mode - interrupts are enabled.
7600 * The SLI interface allows only one mailbox pending at a time. If the
7601 * mailbox is issued in polling mode and there is already a mailbox
7602 * pending, then the function will return an error. If the mailbox is issued
7603 * in NO_WAIT mode and there is a mailbox pending already, the function
7604 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7605 * The sli layer owns the mailbox object until the completion of mailbox
7606 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7607 * return codes the caller owns the mailbox command after the return of
7608 * the function.
7609 **/
7610static int
7611lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7612                       uint32_t flag)
7613{
7614        MAILBOX_t *mbx;
7615        struct lpfc_sli *psli = &phba->sli;
7616        uint32_t status, evtctr;
7617        uint32_t ha_copy, hc_copy;
7618        int i;
7619        unsigned long timeout;
7620        unsigned long drvr_flag = 0;
7621        uint32_t word0, ldata;
7622        void __iomem *to_slim;
7623        int processing_queue = 0;
7624
7625        spin_lock_irqsave(&phba->hbalock, drvr_flag);
7626        if (!pmbox) {
7627                phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7628                /* processing mbox queue from intr_handler */
7629                if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7630                        spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7631                        return MBX_SUCCESS;
7632                }
7633                processing_queue = 1;
7634                pmbox = lpfc_mbox_get(phba);
7635                if (!pmbox) {
7636                        spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7637                        return MBX_SUCCESS;
7638                }
7639        }
7640
7641        if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
7642                pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
7643                if(!pmbox->vport) {
7644                        spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7645                        lpfc_printf_log(phba, KERN_ERR,
7646                                        LOG_MBOX | LOG_VPORT,
7647                                        "1806 Mbox x%x failed. No vport\n",
7648                                        pmbox->u.mb.mbxCommand);
7649                        dump_stack();
7650                        goto out_not_finished;
7651                }
7652        }
7653
7654        /* If the PCI channel is in offline state, do not post mbox. */
7655        if (unlikely(pci_channel_offline(phba->pcidev))) {
7656                spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7657                goto out_not_finished;
7658        }
7659
7660        /* If HBA has a deferred error attention, fail the iocb. */
7661        if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7662                spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7663                goto out_not_finished;
7664        }
7665
7666        psli = &phba->sli;
7667
7668        mbx = &pmbox->u.mb;
7669        status = MBX_SUCCESS;
7670
7671        if (phba->link_state == LPFC_HBA_ERROR) {
7672                spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7673
7674                /* Mbox command <mbxCommand> cannot issue */
7675                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7676                                "(%d):0311 Mailbox command x%x cannot "
7677                                "issue Data: x%x x%x\n",
7678                                pmbox->vport ? pmbox->vport->vpi : 0,
7679                                pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7680                goto out_not_finished;
7681        }
7682
7683        if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
7684                if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7685                        !(hc_copy & HC_MBINT_ENA)) {
7686                        spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7687                        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7688                                "(%d):2528 Mailbox command x%x cannot "
7689                                "issue Data: x%x x%x\n",
7690                                pmbox->vport ? pmbox->vport->vpi : 0,
7691                                pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7692                        goto out_not_finished;
7693                }
7694        }
7695
7696        if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7697                /* Polling for a mbox command when another one is already active
7698                 * is not allowed in SLI. Also, the driver must have established
7699                 * SLI2 mode to queue and process multiple mbox commands.
7700                 */
7701
7702                if (flag & MBX_POLL) {
7703                        spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7704
7705                        /* Mbox command <mbxCommand> cannot issue */
7706                        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7707                                        "(%d):2529 Mailbox command x%x "
7708                                        "cannot issue Data: x%x x%x\n",
7709                                        pmbox->vport ? pmbox->vport->vpi : 0,
7710                                        pmbox->u.mb.mbxCommand,
7711                                        psli->sli_flag, flag);
7712                        goto out_not_finished;
7713                }
7714
7715                if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
7716                        spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7717                        /* Mbox command <mbxCommand> cannot issue */
7718                        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7719                                        "(%d):2530 Mailbox command x%x "
7720                                        "cannot issue Data: x%x x%x\n",
7721                                        pmbox->vport ? pmbox->vport->vpi : 0,
7722                                        pmbox->u.mb.mbxCommand,
7723                                        psli->sli_flag, flag);
7724                        goto out_not_finished;
7725                }
7726
7727                /* Another mailbox command is still being processed, queue this
7728                 * command to be processed later.
7729                 */
7730                lpfc_mbox_put(phba, pmbox);
7731
7732                /* Mbox cmd issue - BUSY */
7733                lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7734                                "(%d):0308 Mbox cmd issue - BUSY Data: "
7735                                "x%x x%x x%x x%x\n",
7736                                pmbox->vport ? pmbox->vport->vpi : 0xffffff,
7737                                mbx->mbxCommand,
7738                                phba->pport ? phba->pport->port_state : 0xff,
7739                                psli->sli_flag, flag);
7740
7741                psli->slistat.mbox_busy++;
7742                spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7743
7744                if (pmbox->vport) {
7745                        lpfc_debugfs_disc_trc(pmbox->vport,
7746                                LPFC_DISC_TRC_MBOX_VPORT,
7747                                "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
7748                                (uint32_t)mbx->mbxCommand,
7749                                mbx->un.varWords[0], mbx->un.varWords[1]);
7750                }
7751                else {
7752                        lpfc_debugfs_disc_trc(phba->pport,
7753                                LPFC_DISC_TRC_MBOX,
7754                                "MBOX Bsy:        cmd:x%x mb:x%x x%x",
7755                                (uint32_t)mbx->mbxCommand,
7756                                mbx->un.varWords[0], mbx->un.varWords[1]);
7757                }
7758
7759                return MBX_BUSY;
7760        }
7761
7762        psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7763
7764        /* If we are not polling, we MUST be in SLI2 mode */
7765        if (flag != MBX_POLL) {
7766                if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
7767                    (mbx->mbxCommand != MBX_KILL_BOARD)) {
7768                        psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7769                        spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7770                        /* Mbox command <mbxCommand> cannot issue */
7771                        lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7772                                        "(%d):2531 Mailbox command x%x "
7773                                        "cannot issue Data: x%x x%x\n",
7774                                        pmbox->vport ? pmbox->vport->vpi : 0,
7775                                        pmbox->u.mb.mbxCommand,
7776                                        psli->sli_flag, flag);
7777                        goto out_not_finished;
7778                }
7779                /* timeout active mbox command */
7780                timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7781                                           1000);
7782                mod_timer(&psli->mbox_tmo, jiffies + timeout);
7783        }
7784
7785        /* Mailbox cmd <cmd> issue */
7786        lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7787                        "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
7788                        "x%x\n",
7789                        pmbox->vport ? pmbox->vport->vpi : 0,
7790                        mbx->mbxCommand,
7791                        phba->pport ? phba->pport->port_state : 0xff,
7792                        psli->sli_flag, flag);
7793
7794        if (mbx->mbxCommand != MBX_HEARTBEAT) {
7795                if (pmbox->vport) {
7796                        lpfc_debugfs_disc_trc(pmbox->vport,
7797                                LPFC_DISC_TRC_MBOX_VPORT,
7798                                "MBOX Send vport: cmd:x%x mb:x%x x%x",
7799                                (uint32_t)mbx->mbxCommand,
7800                                mbx->un.varWords[0], mbx->un.varWords[1]);
7801                }
7802                else {
7803                        lpfc_debugfs_disc_trc(phba->pport,
7804                                LPFC_DISC_TRC_MBOX,
7805                                "MBOX Send:       cmd:x%x mb:x%x x%x",
7806                                (uint32_t)mbx->mbxCommand,
7807                                mbx->un.varWords[0], mbx->un.varWords[1]);
7808                }
7809        }
7810
7811        psli->slistat.mbox_cmd++;
7812        evtctr = psli->slistat.mbox_event;
7813
7814        /* next set own bit for the adapter and copy over command word */
7815        mbx->mbxOwner = OWN_CHIP;
7816
7817        if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7818                /* Populate mbox extension offset word. */
7819                if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
7820                        *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7821                                = (uint8_t *)phba->mbox_ext
7822                                  - (uint8_t *)phba->mbox;
7823                }
7824
7825                /* Copy the mailbox extension data */
7826                if (pmbox->in_ext_byte_len && pmbox->context2) {
7827                        lpfc_sli_pcimem_bcopy(pmbox->context2,
7828                                (uint8_t *)phba->mbox_ext,
7829                                pmbox->in_ext_byte_len);
7830                }
7831                /* Copy command data to host SLIM area */
7832                lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
7833        } else {
7834                /* Populate mbox extension offset word. */
7835                if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
7836                        *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7837                                = MAILBOX_HBA_EXT_OFFSET;
7838
7839                /* Copy the mailbox extension data */
7840                if (pmbox->in_ext_byte_len && pmbox->context2)
7841                        lpfc_memcpy_to_slim(phba->MBslimaddr +
7842                                MAILBOX_HBA_EXT_OFFSET,
7843                                pmbox->context2, pmbox->in_ext_byte_len);
7844
7845                if (mbx->mbxCommand == MBX_CONFIG_PORT)
7846                        /* copy command data into host mbox for cmpl */
7847                        lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7848                                              MAILBOX_CMD_SIZE);
7849
7850                /* First copy mbox command data to HBA SLIM, skip past first
7851                   word */
7852                to_slim = phba->MBslimaddr + sizeof (uint32_t);
7853                lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
7854                            MAILBOX_CMD_SIZE - sizeof (uint32_t));
7855
7856                /* Next copy over first word, with mbxOwner set */
7857                ldata = *((uint32_t *)mbx);
7858                to_slim = phba->MBslimaddr;
7859                writel(ldata, to_slim);
7860                readl(to_slim); /* flush */
7861
7862                if (mbx->mbxCommand == MBX_CONFIG_PORT)
7863                        /* switch over to host mailbox */
7864                        psli->sli_flag |= LPFC_SLI_ACTIVE;
7865        }
7866
7867        wmb();
7868
7869        switch (flag) {
7870        case MBX_NOWAIT:
7871                /* Set up reference to mailbox command */
7872                psli->mbox_active = pmbox;
7873                /* Interrupt board to do it */
7874                writel(CA_MBATT, phba->CAregaddr);
7875                readl(phba->CAregaddr); /* flush */
7876                /* Don't wait for it to finish, just return */
7877                break;
7878
7879        case MBX_POLL:
7880                /* Set up null reference to mailbox command */
7881                psli->mbox_active = NULL;
7882                /* Interrupt board to do it */
7883                writel(CA_MBATT, phba->CAregaddr);
7884                readl(phba->CAregaddr); /* flush */
7885
7886                if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7887                        /* First read mbox status word */
7888                        word0 = *((uint32_t *)phba->mbox);
7889                        word0 = le32_to_cpu(word0);
7890                } else {
7891                        /* First read mbox status word */
7892                        if (lpfc_readl(phba->MBslimaddr, &word0)) {
7893                                spin_unlock_irqrestore(&phba->hbalock,
7894                                                       drvr_flag);
7895                                goto out_not_finished;
7896                        }
7897                }
7898
7899                /* Read the HBA Host Attention Register */
7900                if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7901                        spin_unlock_irqrestore(&phba->hbalock,
7902                                                       drvr_flag);
7903                        goto out_not_finished;
7904                }
7905                timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7906                                                        1000) + jiffies;
7907                i = 0;
7908                /* Wait for command to complete */
7909                while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7910                       (!(ha_copy & HA_MBATT) &&
7911                        (phba->link_state > LPFC_WARM_START))) {
7912                        if (time_after(jiffies, timeout)) {
7913                                psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7914                                spin_unlock_irqrestore(&phba->hbalock,
7915                                                       drvr_flag);
7916                                goto out_not_finished;
7917                        }
7918
7919                        /* Check if we took a mbox interrupt while we were
7920                           polling */
7921                        if (((word0 & OWN_CHIP) != OWN_CHIP)
7922                            && (evtctr != psli->slistat.mbox_event))
7923                                break;
7924
7925                        if (i++ > 10) {
7926                                spin_unlock_irqrestore(&phba->hbalock,
7927                                                       drvr_flag);
7928                                msleep(1);
7929                                spin_lock_irqsave(&phba->hbalock, drvr_flag);
7930                        }
7931
7932                        if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7933                                /* First copy command data */
7934                                word0 = *((uint32_t *)phba->mbox);
7935                                word0 = le32_to_cpu(word0);
7936                                if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7937                                        MAILBOX_t *slimmb;
7938                                        uint32_t slimword0;
7939                                        /* Check real SLIM for any errors */
7940                                        slimword0 = readl(phba->MBslimaddr);
7941                                        slimmb = (MAILBOX_t *) & slimword0;
7942                                        if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7943                                            && slimmb->mbxStatus) {
7944                                                psli->sli_flag &=
7945                                                    ~LPFC_SLI_ACTIVE;
7946                                                word0 = slimword0;
7947                                        }
7948                                }
7949                        } else {
7950                                /* First copy command data */
7951                                word0 = readl(phba->MBslimaddr);
7952                        }
7953                        /* Read the HBA Host Attention Register */
7954                        if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7955                                spin_unlock_irqrestore(&phba->hbalock,
7956                                                       drvr_flag);
7957                                goto out_not_finished;
7958                        }
7959                }
7960
7961                if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7962                        /* copy results back to user */
7963                        lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7964                                                MAILBOX_CMD_SIZE);
7965                        /* Copy the mailbox extension data */
7966                        if (pmbox->out_ext_byte_len && pmbox->context2) {
7967                                lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7968                                                      pmbox->context2,
7969                                                      pmbox->out_ext_byte_len);
7970                        }
7971                } else {
7972                        /* First copy command data */
7973                        lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
7974                                                MAILBOX_CMD_SIZE);
7975                        /* Copy the mailbox extension data */
7976                        if (pmbox->out_ext_byte_len && pmbox->context2) {
7977                                lpfc_memcpy_from_slim(pmbox->context2,
7978                                        phba->MBslimaddr +
7979                                        MAILBOX_HBA_EXT_OFFSET,
7980                                        pmbox->out_ext_byte_len);
7981                        }
7982                }
7983
7984                writel(HA_MBATT, phba->HAregaddr);
7985                readl(phba->HAregaddr); /* flush */
7986
7987                psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7988                status = mbx->mbxStatus;
7989        }
7990
7991        spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7992        return status;
7993
7994out_not_finished:
7995        if (processing_queue) {
7996                pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
7997                lpfc_mbox_cmpl_put(phba, pmbox);
7998        }
7999        return MBX_NOT_FINISHED;
8000}
8001
8002/**
8003 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8004 * @phba: Pointer to HBA context object.
8005 *
8006 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8007 * the driver internal pending mailbox queue. It will then try to wait out the
8008 * possible outstanding mailbox command before return.
8009 *
8010 * Returns:
8011 *      0 - the outstanding mailbox command completed; otherwise, the wait for
8012 *      the outstanding mailbox command timed out.
8013 **/
8014static int
8015lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8016{
8017        struct lpfc_sli *psli = &phba->sli;
8018        int rc = 0;
8019        unsigned long timeout = 0;
8020
8021        /* Mark the asynchronous mailbox command posting as blocked */
8022        spin_lock_irq(&phba->hbalock);
8023        psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8024        /* Determine how long we might wait for the active mailbox
8025         * command to be gracefully completed by firmware.
8026         */
8027        if (phba->sli.mbox_active)
8028                timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8029                                                phba->sli.mbox_active) *
8030                                                1000) + jiffies;
8031        spin_unlock_irq(&phba->hbalock);
8032
8033        /* Make sure the mailbox is really active */
8034        if (timeout)
8035                lpfc_sli4_process_missed_mbox_completions(phba);
8036
8037        /* Wait for the outstnading mailbox command to complete */
8038        while (phba->sli.mbox_active) {
8039                /* Check active mailbox complete status every 2ms */
8040                msleep(2);
8041                if (time_after(jiffies, timeout)) {
8042                        /* Timeout, marked the outstanding cmd not complete */
8043                        rc = 1;
8044                        break;
8045                }
8046        }
8047
8048        /* Can not cleanly block async mailbox command, fails it */
8049        if (rc) {
8050                spin_lock_irq(&phba->hbalock);
8051                psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8052                spin_unlock_irq(&phba->hbalock);
8053        }
8054        return rc;
8055}
8056
8057/**
8058 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8059 * @phba: Pointer to HBA context object.
8060 *
8061 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8062 * commands from the driver internal pending mailbox queue. It makes sure
8063 * that there is no outstanding mailbox command before resuming posting
8064 * asynchronous mailbox commands. If, for any reason, there is outstanding
8065 * mailbox command, it will try to wait it out before resuming asynchronous
8066 * mailbox command posting.
8067 **/
8068static void
8069lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8070{
8071        struct lpfc_sli *psli = &phba->sli;
8072
8073        spin_lock_irq(&phba->hbalock);
8074        if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8075                /* Asynchronous mailbox posting is not blocked, do nothing */
8076                spin_unlock_irq(&phba->hbalock);
8077                return;
8078        }
8079
8080        /* Outstanding synchronous mailbox command is guaranteed to be done,
8081         * successful or timeout, after timing-out the outstanding mailbox
8082         * command shall always be removed, so just unblock posting async
8083         * mailbox command and resume
8084         */
8085        psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8086        spin_unlock_irq(&phba->hbalock);
8087
8088        /* wake up worker thread to post asynchronlous mailbox command */
8089        lpfc_worker_wake_up(phba);
8090}
8091
8092/**
8093 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8094 * @phba: Pointer to HBA context object.
8095 * @mboxq: Pointer to mailbox object.
8096 *
8097 * The function waits for the bootstrap mailbox register ready bit from
8098 * port for twice the regular mailbox command timeout value.
8099 *
8100 *      0 - no timeout on waiting for bootstrap mailbox register ready.
8101 *      MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8102 **/
8103static int
8104lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8105{
8106        uint32_t db_ready;
8107        unsigned long timeout;
8108        struct lpfc_register bmbx_reg;
8109
8110        timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8111                                   * 1000) + jiffies;
8112
8113        do {
8114                bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8115                db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8116                if (!db_ready)
8117                        msleep(2);
8118
8119                if (time_after(jiffies, timeout))
8120                        return MBXERR_ERROR;
8121        } while (!db_ready);
8122
8123        return 0;
8124}
8125
8126/**
8127 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8128 * @phba: Pointer to HBA context object.
8129 * @mboxq: Pointer to mailbox object.
8130 *
8131 * The function posts a mailbox to the port.  The mailbox is expected
8132 * to be comletely filled in and ready for the port to operate on it.
8133 * This routine executes a synchronous completion operation on the
8134 * mailbox by polling for its completion.
8135 *
8136 * The caller must not be holding any locks when calling this routine.
8137 *
8138 * Returns:
8139 *      MBX_SUCCESS - mailbox posted successfully
8140 *      Any of the MBX error values.
8141 **/
8142static int
8143lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8144{
8145        int rc = MBX_SUCCESS;
8146        unsigned long iflag;
8147        uint32_t mcqe_status;
8148        uint32_t mbx_cmnd;
8149        struct lpfc_sli *psli = &phba->sli;
8150        struct lpfc_mqe *mb = &mboxq->u.mqe;
8151        struct lpfc_bmbx_create *mbox_rgn;
8152        struct dma_address *dma_address;
8153
8154        /*
8155         * Only one mailbox can be active to the bootstrap mailbox region
8156         * at a time and there is no queueing provided.
8157         */
8158        spin_lock_irqsave(&phba->hbalock, iflag);
8159        if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8160                spin_unlock_irqrestore(&phba->hbalock, iflag);
8161                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8162                                "(%d):2532 Mailbox command x%x (x%x/x%x) "
8163                                "cannot issue Data: x%x x%x\n",
8164                                mboxq->vport ? mboxq->vport->vpi : 0,
8165                                mboxq->u.mb.mbxCommand,
8166                                lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8167                                lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8168                                psli->sli_flag, MBX_POLL);
8169                return MBXERR_ERROR;
8170        }
8171        /* The server grabs the token and owns it until release */
8172        psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8173        phba->sli.mbox_active = mboxq;
8174        spin_unlock_irqrestore(&phba->hbalock, iflag);
8175
8176        /* wait for bootstrap mbox register for readyness */
8177        rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8178        if (rc)
8179                goto exit;
8180
8181        /*
8182         * Initialize the bootstrap memory region to avoid stale data areas
8183         * in the mailbox post.  Then copy the caller's mailbox contents to
8184         * the bmbx mailbox region.
8185         */
8186        mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8187        memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
8188        lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8189                              sizeof(struct lpfc_mqe));
8190
8191        /* Post the high mailbox dma address to the port and wait for ready. */
8192        dma_address = &phba->sli4_hba.bmbx.dma_address;
8193        writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8194
8195        /* wait for bootstrap mbox register for hi-address write done */
8196        rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8197        if (rc)
8198                goto exit;
8199
8200        /* Post the low mailbox dma address to the port. */
8201        writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
8202
8203        /* wait for bootstrap mbox register for low address write done */
8204        rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8205        if (rc)
8206                goto exit;
8207
8208        /*
8209         * Read the CQ to ensure the mailbox has completed.
8210         * If so, update the mailbox status so that the upper layers
8211         * can complete the request normally.
8212         */
8213        lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8214                              sizeof(struct lpfc_mqe));
8215        mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8216        lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8217                              sizeof(struct lpfc_mcqe));
8218        mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
8219        /*
8220         * When the CQE status indicates a failure and the mailbox status
8221         * indicates success then copy the CQE status into the mailbox status
8222         * (and prefix it with x4000).
8223         */
8224        if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
8225                if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8226                        bf_set(lpfc_mqe_status, mb,
8227                               (LPFC_MBX_ERROR_RANGE | mcqe_status));
8228                rc = MBXERR_ERROR;
8229        } else
8230                lpfc_sli4_swap_str(phba, mboxq);
8231
8232        lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8233                        "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
8234                        "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8235                        " x%x x%x CQ: x%x x%x x%x x%x\n",
8236                        mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8237                        lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8238                        lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8239                        bf_get(lpfc_mqe_status, mb),
8240                        mb->un.mb_words[0], mb->un.mb_words[1],
8241                        mb->un.mb_words[2], mb->un.mb_words[3],
8242                        mb->un.mb_words[4], mb->un.mb_words[5],
8243                        mb->un.mb_words[6], mb->un.mb_words[7],
8244                        mb->un.mb_words[8], mb->un.mb_words[9],
8245                        mb->un.mb_words[10], mb->un.mb_words[11],
8246                        mb->un.mb_words[12], mboxq->mcqe.word0,
8247                        mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
8248                        mboxq->mcqe.trailer);
8249exit:
8250        /* We are holding the token, no needed for lock when release */
8251        spin_lock_irqsave(&phba->hbalock, iflag);
8252        psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8253        phba->sli.mbox_active = NULL;
8254        spin_unlock_irqrestore(&phba->hbalock, iflag);
8255        return rc;
8256}
8257
8258/**
8259 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8260 * @phba: Pointer to HBA context object.
8261 * @pmbox: Pointer to mailbox object.
8262 * @flag: Flag indicating how the mailbox need to be processed.
8263 *
8264 * This function is called by discovery code and HBA management code to submit
8265 * a mailbox command to firmware with SLI-4 interface spec.
8266 *
8267 * Return codes the caller owns the mailbox command after the return of the
8268 * function.
8269 **/
8270static int
8271lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8272                       uint32_t flag)
8273{
8274        struct lpfc_sli *psli = &phba->sli;
8275        unsigned long iflags;
8276        int rc;
8277
8278        /* dump from issue mailbox command if setup */
8279        lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8280
8281        rc = lpfc_mbox_dev_check(phba);
8282        if (unlikely(rc)) {
8283                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8284                                "(%d):2544 Mailbox command x%x (x%x/x%x) "
8285                                "cannot issue Data: x%x x%x\n",
8286                                mboxq->vport ? mboxq->vport->vpi : 0,
8287                                mboxq->u.mb.mbxCommand,
8288                                lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8289                                lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8290                                psli->sli_flag, flag);
8291                goto out_not_finished;
8292        }
8293
8294        /* Detect polling mode and jump to a handler */
8295        if (!phba->sli4_hba.intr_enable) {
8296                if (flag == MBX_POLL)
8297                        rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8298                else
8299                        rc = -EIO;
8300                if (rc != MBX_SUCCESS)
8301                        lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8302                                        "(%d):2541 Mailbox command x%x "
8303                                        "(x%x/x%x) failure: "
8304                                        "mqe_sta: x%x mcqe_sta: x%x/x%x "
8305                                        "Data: x%x x%x\n,",
8306                                        mboxq->vport ? mboxq->vport->vpi : 0,
8307                                        mboxq->u.mb.mbxCommand,
8308                                        lpfc_sli_config_mbox_subsys_get(phba,
8309                                                                        mboxq),
8310                                        lpfc_sli_config_mbox_opcode_get(phba,
8311                                                                        mboxq),
8312                                        bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8313                                        bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8314                                        bf_get(lpfc_mcqe_ext_status,
8315                                               &mboxq->mcqe),
8316                                        psli->sli_flag, flag);
8317                return rc;
8318        } else if (flag == MBX_POLL) {
8319                lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8320                                "(%d):2542 Try to issue mailbox command "
8321                                "x%x (x%x/x%x) synchronously ahead of async "
8322                                "mailbox command queue: x%x x%x\n",
8323                                mboxq->vport ? mboxq->vport->vpi : 0,
8324                                mboxq->u.mb.mbxCommand,
8325                                lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8326                                lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8327                                psli->sli_flag, flag);
8328                /* Try to block the asynchronous mailbox posting */
8329                rc = lpfc_sli4_async_mbox_block(phba);
8330                if (!rc) {
8331                        /* Successfully blocked, now issue sync mbox cmd */
8332                        rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8333                        if (rc != MBX_SUCCESS)
8334                                lpfc_printf_log(phba, KERN_WARNING,
8335                                        LOG_MBOX | LOG_SLI,
8336                                        "(%d):2597 Sync Mailbox command "
8337                                        "x%x (x%x/x%x) failure: "
8338                                        "mqe_sta: x%x mcqe_sta: x%x/x%x "
8339                                        "Data: x%x x%x\n,",
8340                                        mboxq->vport ? mboxq->vport->vpi : 0,
8341                                        mboxq->u.mb.mbxCommand,
8342                                        lpfc_sli_config_mbox_subsys_get(phba,
8343                                                                        mboxq),
8344                                        lpfc_sli_config_mbox_opcode_get(phba,
8345                                                                        mboxq),
8346                                        bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8347                                        bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8348                                        bf_get(lpfc_mcqe_ext_status,
8349                                               &mboxq->mcqe),
8350                                        psli->sli_flag, flag);
8351                        /* Unblock the async mailbox posting afterward */
8352                        lpfc_sli4_async_mbox_unblock(phba);
8353                }
8354                return rc;
8355        }
8356
8357        /* Now, interrupt mode asynchrous mailbox command */
8358        rc = lpfc_mbox_cmd_check(phba, mboxq);
8359        if (rc) {
8360                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8361                                "(%d):2543 Mailbox command x%x (x%x/x%x) "
8362                                "cannot issue Data: x%x x%x\n",
8363                                mboxq->vport ? mboxq->vport->vpi : 0,
8364                                mboxq->u.mb.mbxCommand,
8365                                lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8366                                lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8367                                psli->sli_flag, flag);
8368                goto out_not_finished;
8369        }
8370
8371        /* Put the mailbox command to the driver internal FIFO */
8372        psli->slistat.mbox_busy++;
8373        spin_lock_irqsave(&phba->hbalock, iflags);
8374        lpfc_mbox_put(phba, mboxq);
8375        spin_unlock_irqrestore(&phba->hbalock, iflags);
8376        lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8377                        "(%d):0354 Mbox cmd issue - Enqueue Data: "
8378                        "x%x (x%x/x%x) x%x x%x x%x\n",
8379                        mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8380                        bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8381                        lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8382                        lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8383                        phba->pport->port_state,
8384                        psli->sli_flag, MBX_NOWAIT);
8385        /* Wake up worker thread to transport mailbox command from head */
8386        lpfc_worker_wake_up(phba);
8387
8388        return MBX_BUSY;
8389
8390out_not_finished:
8391        return MBX_NOT_FINISHED;
8392}
8393
8394/**
8395 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8396 * @phba: Pointer to HBA context object.
8397 *
8398 * This function is called by worker thread to send a mailbox command to
8399 * SLI4 HBA firmware.
8400 *
8401 **/
8402int
8403lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8404{
8405        struct lpfc_sli *psli = &phba->sli;
8406        LPFC_MBOXQ_t *mboxq;
8407        int rc = MBX_SUCCESS;
8408        unsigned long iflags;
8409        struct lpfc_mqe *mqe;
8410        uint32_t mbx_cmnd;
8411
8412        /* Check interrupt mode before post async mailbox command */
8413        if (unlikely(!phba->sli4_hba.intr_enable))
8414                return MBX_NOT_FINISHED;
8415
8416        /* Check for mailbox command service token */
8417        spin_lock_irqsave(&phba->hbalock, iflags);
8418        if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8419                spin_unlock_irqrestore(&phba->hbalock, iflags);
8420                return MBX_NOT_FINISHED;
8421        }
8422        if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8423                spin_unlock_irqrestore(&phba->hbalock, iflags);
8424                return MBX_NOT_FINISHED;
8425        }
8426        if (unlikely(phba->sli.mbox_active)) {
8427                spin_unlock_irqrestore(&phba->hbalock, iflags);
8428                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8429                                "0384 There is pending active mailbox cmd\n");
8430                return MBX_NOT_FINISHED;
8431        }
8432        /* Take the mailbox command service token */
8433        psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8434
8435        /* Get the next mailbox command from head of queue */
8436        mboxq = lpfc_mbox_get(phba);
8437
8438        /* If no more mailbox command waiting for post, we're done */
8439        if (!mboxq) {
8440                psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8441                spin_unlock_irqrestore(&phba->hbalock, iflags);
8442                return MBX_SUCCESS;
8443        }
8444        phba->sli.mbox_active = mboxq;
8445        spin_unlock_irqrestore(&phba->hbalock, iflags);
8446
8447        /* Check device readiness for posting mailbox command */
8448        rc = lpfc_mbox_dev_check(phba);
8449        if (unlikely(rc))
8450                /* Driver clean routine will clean up pending mailbox */
8451                goto out_not_finished;
8452
8453        /* Prepare the mbox command to be posted */
8454        mqe = &mboxq->u.mqe;
8455        mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8456
8457        /* Start timer for the mbox_tmo and log some mailbox post messages */
8458        mod_timer(&psli->mbox_tmo, (jiffies +
8459                  msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
8460
8461        lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8462                        "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
8463                        "x%x x%x\n",
8464                        mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8465                        lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8466                        lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8467                        phba->pport->port_state, psli->sli_flag);
8468
8469        if (mbx_cmnd != MBX_HEARTBEAT) {
8470                if (mboxq->vport) {
8471                        lpfc_debugfs_disc_trc(mboxq->vport,
8472                                LPFC_DISC_TRC_MBOX_VPORT,
8473                                "MBOX Send vport: cmd:x%x mb:x%x x%x",
8474                                mbx_cmnd, mqe->un.mb_words[0],
8475                                mqe->un.mb_words[1]);
8476                } else {
8477                        lpfc_debugfs_disc_trc(phba->pport,
8478                                LPFC_DISC_TRC_MBOX,
8479                                "MBOX Send: cmd:x%x mb:x%x x%x",
8480                                mbx_cmnd, mqe->un.mb_words[0],
8481                                mqe->un.mb_words[1]);
8482                }
8483        }
8484        psli->slistat.mbox_cmd++;
8485
8486        /* Post the mailbox command to the port */
8487        rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8488        if (rc != MBX_SUCCESS) {
8489                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8490                                "(%d):2533 Mailbox command x%x (x%x/x%x) "
8491                                "cannot issue Data: x%x x%x\n",
8492                                mboxq->vport ? mboxq->vport->vpi : 0,
8493                                mboxq->u.mb.mbxCommand,
8494                                lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8495                                lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8496                                psli->sli_flag, MBX_NOWAIT);
8497                goto out_not_finished;
8498        }
8499
8500        return rc;
8501
8502out_not_finished:
8503        spin_lock_irqsave(&phba->hbalock, iflags);
8504        if (phba->sli.mbox_active) {
8505                mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8506                __lpfc_mbox_cmpl_put(phba, mboxq);
8507                /* Release the token */
8508                psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8509                phba->sli.mbox_active = NULL;
8510        }
8511        spin_unlock_irqrestore(&phba->hbalock, iflags);
8512
8513        return MBX_NOT_FINISHED;
8514}
8515
8516/**
8517 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8518 * @phba: Pointer to HBA context object.
8519 * @pmbox: Pointer to mailbox object.
8520 * @flag: Flag indicating how the mailbox need to be processed.
8521 *
8522 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8523 * the API jump table function pointer from the lpfc_hba struct.
8524 *
8525 * Return codes the caller owns the mailbox command after the return of the
8526 * function.
8527 **/
8528int
8529lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8530{
8531        return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8532}
8533
8534/**
8535 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
8536 * @phba: The hba struct for which this call is being executed.
8537 * @dev_grp: The HBA PCI-Device group number.
8538 *
8539 * This routine sets up the mbox interface API function jump table in @phba
8540 * struct.
8541 * Returns: 0 - success, -ENODEV - failure.
8542 **/
8543int
8544lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8545{
8546
8547        switch (dev_grp) {
8548        case LPFC_PCI_DEV_LP:
8549                phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8550                phba->lpfc_sli_handle_slow_ring_event =
8551                                lpfc_sli_handle_slow_ring_event_s3;
8552                phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8553                phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8554                phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8555                break;
8556        case LPFC_PCI_DEV_OC:
8557                phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8558                phba->lpfc_sli_handle_slow_ring_event =
8559                                lpfc_sli_handle_slow_ring_event_s4;
8560                phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8561                phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8562                phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8563                break;
8564        default:
8565                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8566                                "1420 Invalid HBA PCI-device group: 0x%x\n",
8567                                dev_grp);
8568                return -ENODEV;
8569                break;
8570        }
8571        return 0;
8572}
8573
8574/**
8575 * __lpfc_sli_ringtx_put - Add an iocb to the txq
8576 * @phba: Pointer to HBA context object.
8577 * @pring: Pointer to driver SLI ring object.
8578 * @piocb: Pointer to address of newly added command iocb.
8579 *
8580 * This function is called with hbalock held to add a command
8581 * iocb to the txq when SLI layer cannot submit the command iocb
8582 * to the ring.
8583 **/
8584void
8585__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8586                    struct lpfc_iocbq *piocb)
8587{
8588        lockdep_assert_held(&phba->hbalock);
8589        /* Insert the caller's iocb in the txq tail for later processing. */
8590        list_add_tail(&piocb->list, &pring->txq);
8591}
8592
8593/**
8594 * lpfc_sli_next_iocb - Get the next iocb in the txq
8595 * @phba: Pointer to HBA context object.
8596 * @pring: Pointer to driver SLI ring object.
8597 * @piocb: Pointer to address of newly added command iocb.
8598 *
8599 * This function is called with hbalock held before a new
8600 * iocb is submitted to the firmware. This function checks
8601 * txq to flush the iocbs in txq to Firmware before
8602 * submitting new iocbs to the Firmware.
8603 * If there are iocbs in the txq which need to be submitted
8604 * to firmware, lpfc_sli_next_iocb returns the first element
8605 * of the txq after dequeuing it from txq.
8606 * If there is no iocb in the txq then the function will return
8607 * *piocb and *piocb is set to NULL. Caller needs to check
8608 * *piocb to find if there are more commands in the txq.
8609 **/
8610static struct lpfc_iocbq *
8611lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8612                   struct lpfc_iocbq **piocb)
8613{
8614        struct lpfc_iocbq * nextiocb;
8615
8616        lockdep_assert_held(&phba->hbalock);
8617
8618        nextiocb = lpfc_sli_ringtx_get(phba, pring);
8619        if (!nextiocb) {
8620                nextiocb = *piocb;
8621                *piocb = NULL;
8622        }
8623
8624        return nextiocb;
8625}
8626
8627/**
8628 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
8629 * @phba: Pointer to HBA context object.
8630 * @ring_number: SLI ring number to issue iocb on.
8631 * @piocb: Pointer to command iocb.
8632 * @flag: Flag indicating if this command can be put into txq.
8633 *
8634 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8635 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8636 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8637 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8638 * this function allows only iocbs for posting buffers. This function finds
8639 * next available slot in the command ring and posts the command to the
8640 * available slot and writes the port attention register to request HBA start
8641 * processing new iocb. If there is no slot available in the ring and
8642 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8643 * the function returns IOCB_BUSY.
8644 *
8645 * This function is called with hbalock held. The function will return success
8646 * after it successfully submit the iocb to firmware or after adding to the
8647 * txq.
8648 **/
8649static int
8650__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
8651                    struct lpfc_iocbq *piocb, uint32_t flag)
8652{
8653        struct lpfc_iocbq *nextiocb;
8654        IOCB_t *iocb;
8655        struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
8656
8657        lockdep_assert_held(&phba->hbalock);
8658
8659        if (piocb->iocb_cmpl && (!piocb->vport) &&
8660           (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8661           (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8662                lpfc_printf_log(phba, KERN_ERR,
8663                                LOG_SLI | LOG_VPORT,
8664                                "1807 IOCB x%x failed. No vport\n",
8665                                piocb->iocb.ulpCommand);
8666                dump_stack();
8667                return IOCB_ERROR;
8668        }
8669
8670
8671        /* If the PCI channel is in offline state, do not post iocbs. */
8672        if (unlikely(pci_channel_offline(phba->pcidev)))
8673                return IOCB_ERROR;
8674
8675        /* If HBA has a deferred error attention, fail the iocb. */
8676        if (unlikely(phba->hba_flag & DEFER_ERATT))
8677                return IOCB_ERROR;
8678
8679        /*
8680         * We should never get an IOCB if we are in a < LINK_DOWN state
8681         */
8682        if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8683                return IOCB_ERROR;
8684
8685        /*
8686         * Check to see if we are blocking IOCB processing because of a
8687         * outstanding event.
8688         */
8689        if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
8690                goto iocb_busy;
8691
8692        if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
8693                /*
8694                 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
8695                 * can be issued if the link is not up.
8696                 */
8697                switch (piocb->iocb.ulpCommand) {
8698                case CMD_GEN_REQUEST64_CR:
8699                case CMD_GEN_REQUEST64_CX:
8700                        if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8701                                (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
8702                                        FC_RCTL_DD_UNSOL_CMD) ||
8703                                (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8704                                        MENLO_TRANSPORT_TYPE))
8705
8706                                goto iocb_busy;
8707                        break;
8708                case CMD_QUE_RING_BUF_CN:
8709                case CMD_QUE_RING_BUF64_CN:
8710                        /*
8711                         * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8712                         * completion, iocb_cmpl MUST be 0.
8713                         */
8714                        if (piocb->iocb_cmpl)
8715                                piocb->iocb_cmpl = NULL;
8716                        /*FALLTHROUGH*/
8717                case CMD_CREATE_XRI_CR:
8718                case CMD_CLOSE_XRI_CN:
8719                case CMD_CLOSE_XRI_CX:
8720                        break;
8721                default:
8722                        goto iocb_busy;
8723                }
8724
8725        /*
8726         * For FCP commands, we must be in a state where we can process link
8727         * attention events.
8728         */
8729        } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
8730                            !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
8731                goto iocb_busy;
8732        }
8733
8734        while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8735               (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8736                lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8737
8738        if (iocb)
8739                lpfc_sli_update_ring(phba, pring);
8740        else
8741                lpfc_sli_update_full_ring(phba, pring);
8742
8743        if (!piocb)
8744                return IOCB_SUCCESS;
8745
8746        goto out_busy;
8747
8748 iocb_busy:
8749        pring->stats.iocb_cmd_delay++;
8750
8751 out_busy:
8752
8753        if (!(flag & SLI_IOCB_RET_IOCB)) {
8754                __lpfc_sli_ringtx_put(phba, pring, piocb);
8755                return IOCB_SUCCESS;
8756        }
8757
8758        return IOCB_BUSY;
8759}
8760
8761/**
8762 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8763 * @phba: Pointer to HBA context object.
8764 * @piocb: Pointer to command iocb.
8765 * @sglq: Pointer to the scatter gather queue object.
8766 *
8767 * This routine converts the bpl or bde that is in the IOCB
8768 * to a sgl list for the sli4 hardware. The physical address
8769 * of the bpl/bde is converted back to a virtual address.
8770 * If the IOCB contains a BPL then the list of BDE's is
8771 * converted to sli4_sge's. If the IOCB contains a single
8772 * BDE then it is converted to a single sli_sge.
8773 * The IOCB is still in cpu endianess so the contents of
8774 * the bpl can be used without byte swapping.
8775 *
8776 * Returns valid XRI = Success, NO_XRI = Failure.
8777**/
8778static uint16_t
8779lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8780                struct lpfc_sglq *sglq)
8781{
8782        uint16_t xritag = NO_XRI;
8783        struct ulp_bde64 *bpl = NULL;
8784        struct ulp_bde64 bde;
8785        struct sli4_sge *sgl  = NULL;
8786        struct lpfc_dmabuf *dmabuf;
8787        IOCB_t *icmd;
8788        int numBdes = 0;
8789        int i = 0;
8790        uint32_t offset = 0; /* accumulated offset in the sg request list */
8791        int inbound = 0; /* number of sg reply entries inbound from firmware */
8792
8793        if (!piocbq || !sglq)
8794                return xritag;
8795
8796        sgl  = (struct sli4_sge *)sglq->sgl;
8797        icmd = &piocbq->iocb;
8798        if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8799                return sglq->sli4_xritag;
8800        if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8801                numBdes = icmd->un.genreq64.bdl.bdeSize /
8802                                sizeof(struct ulp_bde64);
8803                /* The addrHigh and addrLow fields within the IOCB
8804                 * have not been byteswapped yet so there is no
8805                 * need to swap them back.
8806                 */
8807                if (piocbq->context3)
8808                        dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8809                else
8810                        return xritag;
8811
8812                bpl  = (struct ulp_bde64 *)dmabuf->virt;
8813                if (!bpl)
8814                        return xritag;
8815
8816                for (i = 0; i < numBdes; i++) {
8817                        /* Should already be byte swapped. */
8818                        sgl->addr_hi = bpl->addrHigh;
8819                        sgl->addr_lo = bpl->addrLow;
8820
8821                        sgl->word2 = le32_to_cpu(sgl->word2);
8822                        if ((i+1) == numBdes)
8823                                bf_set(lpfc_sli4_sge_last, sgl, 1);
8824                        else
8825                                bf_set(lpfc_sli4_sge_last, sgl, 0);
8826                        /* swap the size field back to the cpu so we
8827                         * can assign it to the sgl.
8828                         */
8829                        bde.tus.w = le32_to_cpu(bpl->tus.w);
8830                        sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
8831                        /* The offsets in the sgl need to be accumulated
8832                         * separately for the request and reply lists.
8833                         * The request is always first, the reply follows.
8834                         */
8835                        if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8836                                /* add up the reply sg entries */
8837                                if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8838                                        inbound++;
8839                                /* first inbound? reset the offset */
8840                                if (inbound == 1)
8841                                        offset = 0;
8842                                bf_set(lpfc_sli4_sge_offset, sgl, offset);
8843                                bf_set(lpfc_sli4_sge_type, sgl,
8844                                        LPFC_SGE_TYPE_DATA);
8845                                offset += bde.tus.f.bdeSize;
8846                        }
8847                        sgl->word2 = cpu_to_le32(sgl->word2);
8848                        bpl++;
8849                        sgl++;
8850                }
8851        } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8852                        /* The addrHigh and addrLow fields of the BDE have not
8853                         * been byteswapped yet so they need to be swapped
8854                         * before putting them in the sgl.
8855                         */
8856                        sgl->addr_hi =
8857                                cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8858                        sgl->addr_lo =
8859                                cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
8860                        sgl->word2 = le32_to_cpu(sgl->word2);
8861                        bf_set(lpfc_sli4_sge_last, sgl, 1);
8862                        sgl->word2 = cpu_to_le32(sgl->word2);
8863                        sgl->sge_len =
8864                                cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
8865        }
8866        return sglq->sli4_xritag;
8867}
8868
8869/**
8870 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8871 * @phba: Pointer to HBA context object.
8872 * @piocb: Pointer to command iocb.
8873 * @wqe: Pointer to the work queue entry.
8874 *
8875 * This routine converts the iocb command to its Work Queue Entry
8876 * equivalent. The wqe pointer should not have any fields set when
8877 * this routine is called because it will memcpy over them.
8878 * This routine does not set the CQ_ID or the WQEC bits in the
8879 * wqe.
8880 *
8881 * Returns: 0 = Success, IOCB_ERROR = Failure.
8882 **/
8883static int
8884lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8885                union lpfc_wqe128 *wqe)
8886{
8887        uint32_t xmit_len = 0, total_len = 0;
8888        uint8_t ct = 0;
8889        uint32_t fip;
8890        uint32_t abort_tag;
8891        uint8_t command_type = ELS_COMMAND_NON_FIP;
8892        uint8_t cmnd;
8893        uint16_t xritag;
8894        uint16_t abrt_iotag;
8895        struct lpfc_iocbq *abrtiocbq;
8896        struct ulp_bde64 *bpl = NULL;
8897        uint32_t els_id = LPFC_ELS_ID_DEFAULT;
8898        int numBdes, i;
8899        struct ulp_bde64 bde;
8900        struct lpfc_nodelist *ndlp;
8901        uint32_t *pcmd;
8902        uint32_t if_type;
8903
8904        fip = phba->hba_flag & HBA_FIP_SUPPORT;
8905        /* The fcp commands will set command type */
8906        if (iocbq->iocb_flag &  LPFC_IO_FCP)
8907                command_type = FCP_COMMAND;
8908        else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
8909                command_type = ELS_COMMAND_FIP;
8910        else
8911                command_type = ELS_COMMAND_NON_FIP;
8912
8913        if (phba->fcp_embed_io)
8914                memset(wqe, 0, sizeof(union lpfc_wqe128));
8915        /* Some of the fields are in the right position already */
8916        memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
8917        if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8918                /* The ct field has moved so reset */
8919                wqe->generic.wqe_com.word7 = 0;
8920                wqe->generic.wqe_com.word10 = 0;
8921        }
8922
8923        abort_tag = (uint32_t) iocbq->iotag;
8924        xritag = iocbq->sli4_xritag;
8925        /* words0-2 bpl convert bde */
8926        if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8927                numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8928                                sizeof(struct ulp_bde64);
8929                bpl  = (struct ulp_bde64 *)
8930                        ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8931                if (!bpl)
8932                        return IOCB_ERROR;
8933
8934                /* Should already be byte swapped. */
8935                wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
8936                wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
8937                /* swap the size field back to the cpu so we
8938                 * can assign it to the sgl.
8939                 */
8940                wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
8941                xmit_len = wqe->generic.bde.tus.f.bdeSize;
8942                total_len = 0;
8943                for (i = 0; i < numBdes; i++) {
8944                        bde.tus.w  = le32_to_cpu(bpl[i].tus.w);
8945                        total_len += bde.tus.f.bdeSize;
8946                }
8947        } else
8948                xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
8949
8950        iocbq->iocb.ulpIoTag = iocbq->iotag;
8951        cmnd = iocbq->iocb.ulpCommand;
8952
8953        switch (iocbq->iocb.ulpCommand) {
8954        case CMD_ELS_REQUEST64_CR:
8955                if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8956                        ndlp = iocbq->context_un.ndlp;
8957                else
8958                        ndlp = (struct lpfc_nodelist *)iocbq->context1;
8959                if (!iocbq->iocb.ulpLe) {
8960                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8961                                "2007 Only Limited Edition cmd Format"
8962                                " supported 0x%x\n",
8963                                iocbq->iocb.ulpCommand);
8964                        return IOCB_ERROR;
8965                }
8966
8967                wqe->els_req.payload_len = xmit_len;
8968                /* Els_reguest64 has a TMO */
8969                bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8970                        iocbq->iocb.ulpTimeout);
8971                /* Need a VF for word 4 set the vf bit*/
8972                bf_set(els_req64_vf, &wqe->els_req, 0);
8973                /* And a VFID for word 12 */
8974                bf_set(els_req64_vfid, &wqe->els_req, 0);
8975                ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8976                bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8977                       iocbq->iocb.ulpContext);
8978                bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8979                bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
8980                /* CCP CCPE PV PRI in word10 were set in the memcpy */
8981                if (command_type == ELS_COMMAND_FIP)
8982                        els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8983                                        >> LPFC_FIP_ELS_ID_SHIFT);
8984                pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8985                                        iocbq->context2)->virt);
8986                if_type = bf_get(lpfc_sli_intf_if_type,
8987                                        &phba->sli4_hba.sli_intf);
8988                if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
8989                        if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
8990                                *pcmd == ELS_CMD_SCR ||
8991                                *pcmd == ELS_CMD_FDISC ||
8992                                *pcmd == ELS_CMD_LOGO ||
8993                                *pcmd == ELS_CMD_PLOGI)) {
8994                                bf_set(els_req64_sp, &wqe->els_req, 1);
8995                                bf_set(els_req64_sid, &wqe->els_req,
8996                                        iocbq->vport->fc_myDID);
8997                                if ((*pcmd == ELS_CMD_FLOGI) &&
8998                                        !(phba->fc_topology ==
8999                                                LPFC_TOPOLOGY_LOOP))
9000                                        bf_set(els_req64_sid, &wqe->els_req, 0);
9001                                bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9002                                bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9003                                        phba->vpi_ids[iocbq->vport->vpi]);
9004                        } else if (pcmd && iocbq->context1) {
9005                                bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9006                                bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9007                                        phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9008                        }
9009                }
9010                bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9011                       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9012                bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9013                bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9014                bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9015                bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9016                bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9017                bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
9018                wqe->els_req.max_response_payload_len = total_len - xmit_len;
9019                break;
9020        case CMD_XMIT_SEQUENCE64_CX:
9021                bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9022                       iocbq->iocb.un.ulpWord[3]);
9023                bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
9024                       iocbq->iocb.unsli3.rcvsli3.ox_id);
9025                /* The entire sequence is transmitted for this IOCB */
9026                xmit_len = total_len;
9027                cmnd = CMD_XMIT_SEQUENCE64_CR;
9028                if (phba->link_flag & LS_LOOPBACK_MODE)
9029                        bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
9030        case CMD_XMIT_SEQUENCE64_CR:
9031                /* word3 iocb=io_tag32 wqe=reserved */
9032                wqe->xmit_sequence.rsvd3 = 0;
9033                /* word4 relative_offset memcpy */
9034                /* word5 r_ctl/df_ctl memcpy */
9035                bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9036                bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9037                bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9038                       LPFC_WQE_IOD_WRITE);
9039                bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9040                       LPFC_WQE_LENLOC_WORD12);
9041                bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
9042                wqe->xmit_sequence.xmit_len = xmit_len;
9043                command_type = OTHER_COMMAND;
9044                break;
9045        case CMD_XMIT_BCAST64_CN:
9046                /* word3 iocb=iotag32 wqe=seq_payload_len */
9047                wqe->xmit_bcast64.seq_payload_len = xmit_len;
9048                /* word4 iocb=rsvd wqe=rsvd */
9049                /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9050                /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
9051                bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
9052                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9053                bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9054                bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9055                bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9056                       LPFC_WQE_LENLOC_WORD3);
9057                bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
9058                break;
9059        case CMD_FCP_IWRITE64_CR:
9060                command_type = FCP_COMMAND_DATA_OUT;
9061                /* word3 iocb=iotag wqe=payload_offset_len */
9062                /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9063                bf_set(payload_offset_len, &wqe->fcp_iwrite,
9064                       xmit_len + sizeof(struct fcp_rsp));
9065                bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9066                       0);
9067                /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9068                /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9069                bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9070                       iocbq->iocb.ulpFCP2Rcvy);
9071                bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9072                /* Always open the exchange */
9073                bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9074                bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9075                       LPFC_WQE_LENLOC_WORD4);
9076                bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
9077                bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
9078                if (iocbq->iocb_flag & LPFC_IO_OAS) {
9079                        bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
9080                        bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9081                        if (iocbq->priority) {
9082                                bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9083                                       (iocbq->priority << 1));
9084                        } else {
9085                                bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9086                                       (phba->cfg_XLanePriority << 1));
9087                        }
9088                }
9089                /* Note, word 10 is already initialized to 0 */
9090
9091                /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9092                if (phba->fcp_embed_pbde)
9093                        bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9094                else
9095                        bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9096
9097                if (phba->fcp_embed_io) {
9098                        struct lpfc_scsi_buf *lpfc_cmd;
9099                        struct sli4_sge *sgl;
9100                        struct fcp_cmnd *fcp_cmnd;
9101                        uint32_t *ptr;
9102
9103                        /* 128 byte wqe support here */
9104
9105                        lpfc_cmd = iocbq->context1;
9106                        sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9107                        fcp_cmnd = lpfc_cmd->fcp_cmnd;
9108
9109                        /* Word 0-2 - FCP_CMND */
9110                        wqe->generic.bde.tus.f.bdeFlags =
9111                                BUFF_TYPE_BDE_IMMED;
9112                        wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9113                        wqe->generic.bde.addrHigh = 0;
9114                        wqe->generic.bde.addrLow =  88;  /* Word 22 */
9115
9116                        bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9117                        bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
9118
9119                        /* Word 22-29  FCP CMND Payload */
9120                        ptr = &wqe->words[22];
9121                        memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9122                }
9123                break;
9124        case CMD_FCP_IREAD64_CR:
9125                /* word3 iocb=iotag wqe=payload_offset_len */
9126                /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9127                bf_set(payload_offset_len, &wqe->fcp_iread,
9128                       xmit_len + sizeof(struct fcp_rsp));
9129                bf_set(cmd_buff_len, &wqe->fcp_iread,
9130                       0);
9131                /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9132                /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9133                bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9134                       iocbq->iocb.ulpFCP2Rcvy);
9135                bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
9136                /* Always open the exchange */
9137                bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9138                bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9139                       LPFC_WQE_LENLOC_WORD4);
9140                bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
9141                bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
9142                if (iocbq->iocb_flag & LPFC_IO_OAS) {
9143                        bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
9144                        bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9145                        if (iocbq->priority) {
9146                                bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9147                                       (iocbq->priority << 1));
9148                        } else {
9149                                bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9150                                       (phba->cfg_XLanePriority << 1));
9151                        }
9152                }
9153                /* Note, word 10 is already initialized to 0 */
9154
9155                /* Don't set PBDE for Perf hints, just fcp_embed_pbde */
9156                if (phba->fcp_embed_pbde)
9157                        bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9158                else
9159                        bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9160
9161                if (phba->fcp_embed_io) {
9162                        struct lpfc_scsi_buf *lpfc_cmd;
9163                        struct sli4_sge *sgl;
9164                        struct fcp_cmnd *fcp_cmnd;
9165                        uint32_t *ptr;
9166
9167                        /* 128 byte wqe support here */
9168
9169                        lpfc_cmd = iocbq->context1;
9170                        sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9171                        fcp_cmnd = lpfc_cmd->fcp_cmnd;
9172
9173                        /* Word 0-2 - FCP_CMND */
9174                        wqe->generic.bde.tus.f.bdeFlags =
9175                                BUFF_TYPE_BDE_IMMED;
9176                        wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9177                        wqe->generic.bde.addrHigh = 0;
9178                        wqe->generic.bde.addrLow =  88;  /* Word 22 */
9179
9180                        bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9181                        bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
9182
9183                        /* Word 22-29  FCP CMND Payload */
9184                        ptr = &wqe->words[22];
9185                        memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9186                }
9187                break;
9188        case CMD_FCP_ICMND64_CR:
9189                /* word3 iocb=iotag wqe=payload_offset_len */
9190                /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9191                bf_set(payload_offset_len, &wqe->fcp_icmd,
9192                       xmit_len + sizeof(struct fcp_rsp));
9193                bf_set(cmd_buff_len, &wqe->fcp_icmd,
9194                       0);
9195                /* word3 iocb=IO_TAG wqe=reserved */
9196                bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
9197                /* Always open the exchange */
9198                bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9199                bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9200                bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9201                bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9202                       LPFC_WQE_LENLOC_NONE);
9203                bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9204                       iocbq->iocb.ulpFCP2Rcvy);
9205                if (iocbq->iocb_flag & LPFC_IO_OAS) {
9206                        bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
9207                        bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9208                        if (iocbq->priority) {
9209                                bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9210                                       (iocbq->priority << 1));
9211                        } else {
9212                                bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9213                                       (phba->cfg_XLanePriority << 1));
9214                        }
9215                }
9216                /* Note, word 10 is already initialized to 0 */
9217
9218                if (phba->fcp_embed_io) {
9219                        struct lpfc_scsi_buf *lpfc_cmd;
9220                        struct sli4_sge *sgl;
9221                        struct fcp_cmnd *fcp_cmnd;
9222                        uint32_t *ptr;
9223
9224                        /* 128 byte wqe support here */
9225
9226                        lpfc_cmd = iocbq->context1;
9227                        sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9228                        fcp_cmnd = lpfc_cmd->fcp_cmnd;
9229
9230                        /* Word 0-2 - FCP_CMND */
9231                        wqe->generic.bde.tus.f.bdeFlags =
9232                                BUFF_TYPE_BDE_IMMED;
9233                        wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9234                        wqe->generic.bde.addrHigh = 0;
9235                        wqe->generic.bde.addrLow =  88;  /* Word 22 */
9236
9237                        bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9238                        bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
9239
9240                        /* Word 22-29  FCP CMND Payload */
9241                        ptr = &wqe->words[22];
9242                        memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9243                }
9244                break;
9245        case CMD_GEN_REQUEST64_CR:
9246                /* For this command calculate the xmit length of the
9247                 * request bde.
9248                 */
9249                xmit_len = 0;
9250                numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9251                        sizeof(struct ulp_bde64);
9252                for (i = 0; i < numBdes; i++) {
9253                        bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9254                        if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9255                                break;
9256                        xmit_len += bde.tus.f.bdeSize;
9257                }
9258                /* word3 iocb=IO_TAG wqe=request_payload_len */
9259                wqe->gen_req.request_payload_len = xmit_len;
9260                /* word4 iocb=parameter wqe=relative_offset memcpy */
9261                /* word5 [rctl, type, df_ctl, la] copied in memcpy */
9262                /* word6 context tag copied in memcpy */
9263                if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
9264                        ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9265                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9266                                "2015 Invalid CT %x command 0x%x\n",
9267                                ct, iocbq->iocb.ulpCommand);
9268                        return IOCB_ERROR;
9269                }
9270                bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9271                bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9272                bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9273                bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9274                bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9275                bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9276                bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9277                bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
9278                wqe->gen_req.max_response_payload_len = total_len - xmit_len;
9279                command_type = OTHER_COMMAND;
9280                break;
9281        case CMD_XMIT_ELS_RSP64_CX:
9282                ndlp = (struct lpfc_nodelist *)iocbq->context1;
9283                /* words0-2 BDE memcpy */
9284                /* word3 iocb=iotag32 wqe=response_payload_len */
9285                wqe->xmit_els_rsp.response_payload_len = xmit_len;
9286                /* word4 */
9287                wqe->xmit_els_rsp.word4 = 0;
9288                /* word5 iocb=rsvd wge=did */
9289                bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
9290                         iocbq->iocb.un.xseq64.xmit_els_remoteID);
9291
9292                if_type = bf_get(lpfc_sli_intf_if_type,
9293                                        &phba->sli4_hba.sli_intf);
9294                if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
9295                        if (iocbq->vport->fc_flag & FC_PT2PT) {
9296                                bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9297                                bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9298                                        iocbq->vport->fc_myDID);
9299                                if (iocbq->vport->fc_myDID == Fabric_DID) {
9300                                        bf_set(wqe_els_did,
9301                                                &wqe->xmit_els_rsp.wqe_dest, 0);
9302                                }
9303                        }
9304                }
9305                bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9306                       ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9307                bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9308                bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
9309                       iocbq->iocb.unsli3.rcvsli3.ox_id);
9310                if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
9311                        bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
9312                               phba->vpi_ids[iocbq->vport->vpi]);
9313                bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9314                bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9315                bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9316                bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9317                       LPFC_WQE_LENLOC_WORD3);
9318                bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
9319                bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9320                       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9321                pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9322                                        iocbq->context2)->virt);
9323                if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
9324                                bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9325                                bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9326                                        iocbq->vport->fc_myDID);
9327                                bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9328                                bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
9329                                        phba->vpi_ids[phba->pport->vpi]);
9330                }
9331                command_type = OTHER_COMMAND;
9332                break;
9333        case CMD_CLOSE_XRI_CN:
9334        case CMD_ABORT_XRI_CN:
9335        case CMD_ABORT_XRI_CX:
9336                /* words 0-2 memcpy should be 0 rserved */
9337                /* port will send abts */
9338                abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9339                if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9340                        abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9341                        fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9342                } else
9343                        fip = 0;
9344
9345                if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
9346                        /*
9347                         * The link is down, or the command was ELS_FIP
9348                         * so the fw does not need to send abts
9349                         * on the wire.
9350                         */
9351                        bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9352                else
9353                        bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9354                bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
9355                /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9356                wqe->abort_cmd.rsrvd5 = 0;
9357                bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
9358                        ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9359                abort_tag = iocbq->iocb.un.acxri.abortIoTag;
9360                /*
9361                 * The abort handler will send us CMD_ABORT_XRI_CN or
9362                 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9363                 */
9364                bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9365                bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9366                bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9367                       LPFC_WQE_LENLOC_NONE);
9368                cmnd = CMD_ABORT_XRI_CX;
9369                command_type = OTHER_COMMAND;
9370                xritag = 0;
9371                break;
9372        case CMD_XMIT_BLS_RSP64_CX:
9373                ndlp = (struct lpfc_nodelist *)iocbq->context1;
9374                /* As BLS ABTS RSP WQE is very different from other WQEs,
9375                 * we re-construct this WQE here based on information in
9376                 * iocbq from scratch.
9377                 */
9378                memset(wqe, 0, sizeof(union lpfc_wqe));
9379                /* OX_ID is invariable to who sent ABTS to CT exchange */
9380                bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
9381                       bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9382                if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
9383                    LPFC_ABTS_UNSOL_INT) {
9384                        /* ABTS sent by initiator to CT exchange, the
9385                         * RX_ID field will be filled with the newly
9386                         * allocated responder XRI.
9387                         */
9388                        bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9389                               iocbq->sli4_xritag);
9390                } else {
9391                        /* ABTS sent by responder to CT exchange, the
9392                         * RX_ID field will be filled with the responder
9393                         * RX_ID from ABTS.
9394                         */
9395                        bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9396                               bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
9397                }
9398                bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9399                bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
9400
9401                /* Use CT=VPI */
9402                bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9403                        ndlp->nlp_DID);
9404                bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9405                        iocbq->iocb.ulpContext);
9406                bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
9407                bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
9408                        phba->vpi_ids[phba->pport->vpi]);
9409                bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9410                bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9411                       LPFC_WQE_LENLOC_NONE);
9412                /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9413                command_type = OTHER_COMMAND;
9414                if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9415                        bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9416                               bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9417                        bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9418                               bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9419                        bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9420                               bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9421                }
9422
9423                break;
9424        case CMD_SEND_FRAME:
9425                bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9426                bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9427                return 0;
9428        case CMD_XRI_ABORTED_CX:
9429        case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
9430        case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9431        case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9432        case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9433        case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9434        default:
9435                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9436                                "2014 Invalid command 0x%x\n",
9437                                iocbq->iocb.ulpCommand);
9438                return IOCB_ERROR;
9439                break;
9440        }
9441
9442        if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9443                bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9444        else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9445                bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9446        else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9447                bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9448        iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9449                              LPFC_IO_DIF_INSERT);
9450        bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9451        bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9452        wqe->generic.wqe_com.abort_tag = abort_tag;
9453        bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9454        bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9455        bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9456        bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
9457        return 0;
9458}
9459
9460/**
9461 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9462 * @phba: Pointer to HBA context object.
9463 * @ring_number: SLI ring number to issue iocb on.
9464 * @piocb: Pointer to command iocb.
9465 * @flag: Flag indicating if this command can be put into txq.
9466 *
9467 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9468 * an iocb command to an HBA with SLI-4 interface spec.
9469 *
9470 * This function is called with hbalock held. The function will return success
9471 * after it successfully submit the iocb to firmware or after adding to the
9472 * txq.
9473 **/
9474static int
9475__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9476                         struct lpfc_iocbq *piocb, uint32_t flag)
9477{
9478        struct lpfc_sglq *sglq;
9479        union lpfc_wqe128 wqe;
9480        struct lpfc_queue *wq;
9481        struct lpfc_sli_ring *pring;
9482
9483        /* Get the WQ */
9484        if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9485            (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9486                if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9487                        wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9488                else
9489                        wq = phba->sli4_hba.oas_wq;
9490        } else {
9491                wq = phba->sli4_hba.els_wq;
9492        }
9493
9494        /* Get corresponding ring */
9495        pring = wq->pring;
9496
9497        /*
9498         * The WQE can be either 64 or 128 bytes,
9499         */
9500
9501        lockdep_assert_held(&phba->hbalock);
9502
9503        if (piocb->sli4_xritag == NO_XRI) {
9504                if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
9505                    piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
9506                        sglq = NULL;
9507                else {
9508                        if (!list_empty(&pring->txq)) {
9509                                if (!(flag & SLI_IOCB_RET_IOCB)) {
9510                                        __lpfc_sli_ringtx_put(phba,
9511                                                pring, piocb);
9512                                        return IOCB_SUCCESS;
9513                                } else {
9514                                        return IOCB_BUSY;
9515                                }
9516                        } else {
9517                                sglq = __lpfc_sli_get_els_sglq(phba, piocb);
9518                                if (!sglq) {
9519                                        if (!(flag & SLI_IOCB_RET_IOCB)) {
9520                                                __lpfc_sli_ringtx_put(phba,
9521                                                                pring,
9522                                                                piocb);
9523                                                return IOCB_SUCCESS;
9524                                        } else
9525                                                return IOCB_BUSY;
9526                                }
9527                        }
9528                }
9529        } else if (piocb->iocb_flag &  LPFC_IO_FCP)
9530                /* These IO's already have an XRI and a mapped sgl. */
9531                sglq = NULL;
9532        else {
9533                /*
9534                 * This is a continuation of a commandi,(CX) so this
9535                 * sglq is on the active list
9536                 */
9537                sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
9538                if (!sglq)
9539                        return IOCB_ERROR;
9540        }
9541
9542        if (sglq) {
9543                piocb->sli4_lxritag = sglq->sli4_lxritag;
9544                piocb->sli4_xritag = sglq->sli4_xritag;
9545                if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
9546                        return IOCB_ERROR;
9547        }
9548
9549        if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
9550                return IOCB_ERROR;
9551
9552        if (lpfc_sli4_wq_put(wq, &wqe))
9553                return IOCB_ERROR;
9554        lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9555
9556        return 0;
9557}
9558
9559/**
9560 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9561 *
9562 * This routine wraps the actual lockless version for issusing IOCB function
9563 * pointer from the lpfc_hba struct.
9564 *
9565 * Return codes:
9566 * IOCB_ERROR - Error
9567 * IOCB_SUCCESS - Success
9568 * IOCB_BUSY - Busy
9569 **/
9570int
9571__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9572                struct lpfc_iocbq *piocb, uint32_t flag)
9573{
9574        return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9575}
9576
9577/**
9578 * lpfc_sli_api_table_setup - Set up sli api function jump table
9579 * @phba: The hba struct for which this call is being executed.
9580 * @dev_grp: The HBA PCI-Device group number.
9581 *
9582 * This routine sets up the SLI interface API function jump table in @phba
9583 * struct.
9584 * Returns: 0 - success, -ENODEV - failure.
9585 **/
9586int
9587lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9588{
9589
9590        switch (dev_grp) {
9591        case LPFC_PCI_DEV_LP:
9592                phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9593                phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9594                break;
9595        case LPFC_PCI_DEV_OC:
9596                phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9597                phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9598                break;
9599        default:
9600                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9601                                "1419 Invalid HBA PCI-device group: 0x%x\n",
9602                                dev_grp);
9603                return -ENODEV;
9604                break;
9605        }
9606        phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9607        return 0;
9608}
9609
9610/**
9611 * lpfc_sli4_calc_ring - Calculates which ring to use
9612 * @phba: Pointer to HBA context object.
9613 * @piocb: Pointer to command iocb.
9614 *
9615 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9616 * hba_wqidx, thus we need to calculate the corresponding ring.
9617 * Since ABORTS must go on the same WQ of the command they are
9618 * aborting, we use command's hba_wqidx.
9619 */
9620struct lpfc_sli_ring *
9621lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
9622{
9623        if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
9624                if (!(phba->cfg_fof) ||
9625                    (!(piocb->iocb_flag & LPFC_IO_FOF))) {
9626                        if (unlikely(!phba->sli4_hba.fcp_wq))
9627                                return NULL;
9628                        /*
9629                         * for abort iocb hba_wqidx should already
9630                         * be setup based on what work queue we used.
9631                         */
9632                        if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9633                                piocb->hba_wqidx =
9634                                        lpfc_sli4_scmd_to_wqidx_distr(phba,
9635                                                              piocb->context1);
9636                                piocb->hba_wqidx = piocb->hba_wqidx %
9637                                        phba->cfg_fcp_io_channel;
9638                        }
9639                        return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
9640                } else {
9641                        if (unlikely(!phba->sli4_hba.oas_wq))
9642                                return NULL;
9643                        piocb->hba_wqidx = 0;
9644                        return phba->sli4_hba.oas_wq->pring;
9645                }
9646        } else {
9647                if (unlikely(!phba->sli4_hba.els_wq))
9648                        return NULL;
9649                piocb->hba_wqidx = 0;
9650                return phba->sli4_hba.els_wq->pring;
9651        }
9652}
9653
9654/**
9655 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9656 * @phba: Pointer to HBA context object.
9657 * @pring: Pointer to driver SLI ring object.
9658 * @piocb: Pointer to command iocb.
9659 * @flag: Flag indicating if this command can be put into txq.
9660 *
9661 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9662 * function. This function gets the hbalock and calls
9663 * __lpfc_sli_issue_iocb function and will return the error returned
9664 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9665 * functions which do not hold hbalock.
9666 **/
9667int
9668lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9669                    struct lpfc_iocbq *piocb, uint32_t flag)
9670{
9671        struct lpfc_hba_eq_hdl *hba_eq_hdl;
9672        struct lpfc_sli_ring *pring;
9673        struct lpfc_queue *fpeq;
9674        struct lpfc_eqe *eqe;
9675        unsigned long iflags;
9676        int rc, idx;
9677
9678        if (phba->sli_rev == LPFC_SLI_REV4) {
9679                pring = lpfc_sli4_calc_ring(phba, piocb);
9680                if (unlikely(pring == NULL))
9681                        return IOCB_ERROR;
9682
9683                spin_lock_irqsave(&pring->ring_lock, iflags);
9684                rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9685                spin_unlock_irqrestore(&pring->ring_lock, iflags);
9686
9687                if (lpfc_fcp_look_ahead && (piocb->iocb_flag &  LPFC_IO_FCP)) {
9688                        idx = piocb->hba_wqidx;
9689                        hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
9690
9691                        if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
9692
9693                                /* Get associated EQ with this index */
9694                                fpeq = phba->sli4_hba.hba_eq[idx];
9695
9696                                /* Turn off interrupts from this EQ */
9697                                phba->sli4_hba.sli4_eq_clr_intr(fpeq);
9698
9699                                /*
9700                                 * Process all the events on FCP EQ
9701                                 */
9702                                while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9703                                        lpfc_sli4_hba_handle_eqe(phba,
9704                                                eqe, idx);
9705                                        fpeq->EQ_processed++;
9706                                }
9707
9708                                /* Always clear and re-arm the EQ */
9709                                phba->sli4_hba.sli4_eq_release(fpeq,
9710                                        LPFC_QUEUE_REARM);
9711                        }
9712                        atomic_inc(&hba_eq_hdl->hba_eq_in_use);
9713                }
9714        } else {
9715                /* For now, SLI2/3 will still use hbalock */
9716                spin_lock_irqsave(&phba->hbalock, iflags);
9717                rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9718                spin_unlock_irqrestore(&phba->hbalock, iflags);
9719        }
9720        return rc;
9721}
9722
9723/**
9724 * lpfc_extra_ring_setup - Extra ring setup function
9725 * @phba: Pointer to HBA context object.
9726 *
9727 * This function is called while driver attaches with the
9728 * HBA to setup the extra ring. The extra ring is used
9729 * only when driver needs to support target mode functionality
9730 * or IP over FC functionalities.
9731 *
9732 * This function is called with no lock held. SLI3 only.
9733 **/
9734static int
9735lpfc_extra_ring_setup( struct lpfc_hba *phba)
9736{
9737        struct lpfc_sli *psli;
9738        struct lpfc_sli_ring *pring;
9739
9740        psli = &phba->sli;
9741
9742        /* Adjust cmd/rsp ring iocb entries more evenly */
9743
9744        /* Take some away from the FCP ring */
9745        pring = &psli->sli3_ring[LPFC_FCP_RING];
9746        pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9747        pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9748        pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9749        pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9750
9751        /* and give them to the extra ring */
9752        pring = &psli->sli3_ring[LPFC_EXTRA_RING];
9753
9754        pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9755        pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9756        pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9757        pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9758
9759        /* Setup default profile for this ring */
9760        pring->iotag_max = 4096;
9761        pring->num_mask = 1;
9762        pring->prt[0].profile = 0;      /* Mask 0 */
9763        pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9764        pring->prt[0].type = phba->cfg_multi_ring_type;
9765        pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9766        return 0;
9767}
9768
9769/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9770 * @phba: Pointer to HBA context object.
9771 * @iocbq: Pointer to iocb object.
9772 *
9773 * The async_event handler calls this routine when it receives
9774 * an ASYNC_STATUS_CN event from the port.  The port generates
9775 * this event when an Abort Sequence request to an rport fails
9776 * twice in succession.  The abort could be originated by the
9777 * driver or by the port.  The ABTS could have been for an ELS
9778 * or FCP IO.  The port only generates this event when an ABTS
9779 * fails to complete after one retry.
9780 */
9781static void
9782lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9783                          struct lpfc_iocbq *iocbq)
9784{
9785        struct lpfc_nodelist *ndlp = NULL;
9786        uint16_t rpi = 0, vpi = 0;
9787        struct lpfc_vport *vport = NULL;
9788
9789        /* The rpi in the ulpContext is vport-sensitive. */
9790        vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9791        rpi = iocbq->iocb.ulpContext;
9792
9793        lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9794                        "3092 Port generated ABTS async event "
9795                        "on vpi %d rpi %d status 0x%x\n",
9796                        vpi, rpi, iocbq->iocb.ulpStatus);
9797
9798        vport = lpfc_find_vport_by_vpid(phba, vpi);
9799        if (!vport)
9800                goto err_exit;
9801        ndlp = lpfc_findnode_rpi(vport, rpi);
9802        if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9803                goto err_exit;
9804
9805        if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9806                lpfc_sli_abts_recover_port(vport, ndlp);
9807        return;
9808
9809 err_exit:
9810        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9811                        "3095 Event Context not found, no "
9812                        "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9813                        iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9814                        vpi, rpi);
9815}
9816
9817/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9818 * @phba: pointer to HBA context object.
9819 * @ndlp: nodelist pointer for the impacted rport.
9820 * @axri: pointer to the wcqe containing the failed exchange.
9821 *
9822 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9823 * port.  The port generates this event when an abort exchange request to an
9824 * rport fails twice in succession with no reply.  The abort could be originated
9825 * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
9826 */
9827void
9828lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9829                           struct lpfc_nodelist *ndlp,
9830                           struct sli4_wcqe_xri_aborted *axri)
9831{
9832        struct lpfc_vport *vport;
9833        uint32_t ext_status = 0;
9834
9835        if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
9836                lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9837                                "3115 Node Context not found, driver "
9838                                "ignoring abts err event\n");
9839                return;
9840        }
9841
9842        vport = ndlp->vport;
9843        lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9844                        "3116 Port generated FCP XRI ABORT event on "
9845                        "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
9846                        ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
9847                        bf_get(lpfc_wcqe_xa_xri, axri),
9848                        bf_get(lpfc_wcqe_xa_status, axri),
9849                        axri->parameter);
9850
9851        /*
9852         * Catch the ABTS protocol failure case.  Older OCe FW releases returned
9853         * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9854         * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9855         */
9856        ext_status = axri->parameter & IOERR_PARAM_MASK;
9857        if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9858            ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
9859                lpfc_sli_abts_recover_port(vport, ndlp);
9860}
9861
9862/**
9863 * lpfc_sli_async_event_handler - ASYNC iocb handler function
9864 * @phba: Pointer to HBA context object.
9865 * @pring: Pointer to driver SLI ring object.
9866 * @iocbq: Pointer to iocb object.
9867 *
9868 * This function is called by the slow ring event handler
9869 * function when there is an ASYNC event iocb in the ring.
9870 * This function is called with no lock held.
9871 * Currently this function handles only temperature related
9872 * ASYNC events. The function decodes the temperature sensor
9873 * event message and posts events for the management applications.
9874 **/
9875static void
9876lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9877        struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9878{
9879        IOCB_t *icmd;
9880        uint16_t evt_code;
9881        struct temp_event temp_event_data;
9882        struct Scsi_Host *shost;
9883        uint32_t *iocb_w;
9884
9885        icmd = &iocbq->iocb;
9886        evt_code = icmd->un.asyncstat.evt_code;
9887
9888        switch (evt_code) {
9889        case ASYNC_TEMP_WARN:
9890        case ASYNC_TEMP_SAFE:
9891                temp_event_data.data = (uint32_t) icmd->ulpContext;
9892                temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9893                if (evt_code == ASYNC_TEMP_WARN) {
9894                        temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9895                        lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9896                                "0347 Adapter is very hot, please take "
9897                                "corrective action. temperature : %d Celsius\n",
9898                                (uint32_t) icmd->ulpContext);
9899                } else {
9900                        temp_event_data.event_code = LPFC_NORMAL_TEMP;
9901                        lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9902                                "0340 Adapter temperature is OK now. "
9903                                "temperature : %d Celsius\n",
9904                                (uint32_t) icmd->ulpContext);
9905                }
9906
9907                /* Send temperature change event to applications */
9908                shost = lpfc_shost_from_vport(phba->pport);
9909                fc_host_post_vendor_event(shost, fc_get_event_number(),
9910                        sizeof(temp_event_data), (char *) &temp_event_data,
9911                        LPFC_NL_VENDOR_ID);
9912                break;
9913        case ASYNC_STATUS_CN:
9914                lpfc_sli_abts_err_handler(phba, iocbq);
9915                break;
9916        default:
9917                iocb_w = (uint32_t *) icmd;
9918                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9919                        "0346 Ring %d handler: unexpected ASYNC_STATUS"
9920                        " evt_code 0x%x\n"
9921                        "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
9922                        "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
9923                        "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
9924                        "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
9925                        pring->ringno, icmd->un.asyncstat.evt_code,
9926                        iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9927                        iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9928                        iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9929                        iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9930
9931                break;
9932        }
9933}
9934
9935
9936/**
9937 * lpfc_sli4_setup - SLI ring setup function
9938 * @phba: Pointer to HBA context object.
9939 *
9940 * lpfc_sli_setup sets up rings of the SLI interface with
9941 * number of iocbs per ring and iotags. This function is
9942 * called while driver attach to the HBA and before the
9943 * interrupts are enabled. So there is no need for locking.
9944 *
9945 * This function always returns 0.
9946 **/
9947int
9948lpfc_sli4_setup(struct lpfc_hba *phba)
9949{
9950        struct lpfc_sli_ring *pring;
9951
9952        pring = phba->sli4_hba.els_wq->pring;
9953        pring->num_mask = LPFC_MAX_RING_MASK;
9954        pring->prt[0].profile = 0;      /* Mask 0 */
9955        pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9956        pring->prt[0].type = FC_TYPE_ELS;
9957        pring->prt[0].lpfc_sli_rcv_unsol_event =
9958            lpfc_els_unsol_event;
9959        pring->prt[1].profile = 0;      /* Mask 1 */
9960        pring->prt[1].rctl = FC_RCTL_ELS_REP;
9961        pring->prt[1].type = FC_TYPE_ELS;
9962        pring->prt[1].lpfc_sli_rcv_unsol_event =
9963            lpfc_els_unsol_event;
9964        pring->prt[2].profile = 0;      /* Mask 2 */
9965        /* NameServer Inquiry */
9966        pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9967        /* NameServer */
9968        pring->prt[2].type = FC_TYPE_CT;
9969        pring->prt[2].lpfc_sli_rcv_unsol_event =
9970            lpfc_ct_unsol_event;
9971        pring->prt[3].profile = 0;      /* Mask 3 */
9972        /* NameServer response */
9973        pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9974        /* NameServer */
9975        pring->prt[3].type = FC_TYPE_CT;
9976        pring->prt[3].lpfc_sli_rcv_unsol_event =
9977            lpfc_ct_unsol_event;
9978        return 0;
9979}
9980
9981/**
9982 * lpfc_sli_setup - SLI ring setup function
9983 * @phba: Pointer to HBA context object.
9984 *
9985 * lpfc_sli_setup sets up rings of the SLI interface with
9986 * number of iocbs per ring and iotags. This function is
9987 * called while driver attach to the HBA and before the
9988 * interrupts are enabled. So there is no need for locking.
9989 *
9990 * This function always returns 0. SLI3 only.
9991 **/
9992int
9993lpfc_sli_setup(struct lpfc_hba *phba)
9994{
9995        int i, totiocbsize = 0;
9996        struct lpfc_sli *psli = &phba->sli;
9997        struct lpfc_sli_ring *pring;
9998
9999        psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
10000        psli->sli_flag = 0;
10001
10002        psli->iocbq_lookup = NULL;
10003        psli->iocbq_lookup_len = 0;
10004        psli->last_iotag = 0;
10005
10006        for (i = 0; i < psli->num_rings; i++) {
10007                pring = &psli->sli3_ring[i];
10008                switch (i) {
10009                case LPFC_FCP_RING:     /* ring 0 - FCP */
10010                        /* numCiocb and numRiocb are used in config_port */
10011                        pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10012                        pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10013                        pring->sli.sli3.numCiocb +=
10014                                SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10015                        pring->sli.sli3.numRiocb +=
10016                                SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10017                        pring->sli.sli3.numCiocb +=
10018                                SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10019                        pring->sli.sli3.numRiocb +=
10020                                SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10021                        pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10022                                                        SLI3_IOCB_CMD_SIZE :
10023                                                        SLI2_IOCB_CMD_SIZE;
10024                        pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10025                                                        SLI3_IOCB_RSP_SIZE :
10026                                                        SLI2_IOCB_RSP_SIZE;
10027                        pring->iotag_ctr = 0;
10028                        pring->iotag_max =
10029                            (phba->cfg_hba_queue_depth * 2);
10030                        pring->fast_iotag = pring->iotag_max;
10031                        pring->num_mask = 0;
10032                        break;
10033                case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
10034                        /* numCiocb and numRiocb are used in config_port */
10035                        pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10036                        pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10037                        pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10038                                                        SLI3_IOCB_CMD_SIZE :
10039                                                        SLI2_IOCB_CMD_SIZE;
10040                        pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10041                                                        SLI3_IOCB_RSP_SIZE :
10042                                                        SLI2_IOCB_RSP_SIZE;
10043                        pring->iotag_max = phba->cfg_hba_queue_depth;
10044                        pring->num_mask = 0;
10045                        break;
10046                case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
10047                        /* numCiocb and numRiocb are used in config_port */
10048                        pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10049                        pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10050                        pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
10051                                                        SLI3_IOCB_CMD_SIZE :
10052                                                        SLI2_IOCB_CMD_SIZE;
10053                        pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
10054                                                        SLI3_IOCB_RSP_SIZE :
10055                                                        SLI2_IOCB_RSP_SIZE;
10056                        pring->fast_iotag = 0;
10057                        pring->iotag_ctr = 0;
10058                        pring->iotag_max = 4096;
10059                        pring->lpfc_sli_rcv_async_status =
10060                                lpfc_sli_async_event_handler;
10061                        pring->num_mask = LPFC_MAX_RING_MASK;
10062                        pring->prt[0].profile = 0;      /* Mask 0 */
10063                        pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10064                        pring->prt[0].type = FC_TYPE_ELS;
10065                        pring->prt[0].lpfc_sli_rcv_unsol_event =
10066                            lpfc_els_unsol_event;
10067                        pring->prt[1].profile = 0;      /* Mask 1 */
10068                        pring->prt[1].rctl = FC_RCTL_ELS_REP;
10069                        pring->prt[1].type = FC_TYPE_ELS;
10070                        pring->prt[1].lpfc_sli_rcv_unsol_event =
10071                            lpfc_els_unsol_event;
10072                        pring->prt[2].profile = 0;      /* Mask 2 */
10073                        /* NameServer Inquiry */
10074                        pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10075                        /* NameServer */
10076                        pring->prt[2].type = FC_TYPE_CT;
10077                        pring->prt[2].lpfc_sli_rcv_unsol_event =
10078                            lpfc_ct_unsol_event;
10079                        pring->prt[3].profile = 0;      /* Mask 3 */
10080                        /* NameServer response */
10081                        pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10082                        /* NameServer */
10083                        pring->prt[3].type = FC_TYPE_CT;
10084                        pring->prt[3].lpfc_sli_rcv_unsol_event =
10085                            lpfc_ct_unsol_event;
10086                        break;
10087                }
10088                totiocbsize += (pring->sli.sli3.numCiocb *
10089                        pring->sli.sli3.sizeCiocb) +
10090                        (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
10091        }
10092        if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
10093                /* Too many cmd / rsp ring entries in SLI2 SLIM */
10094                printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10095                       "SLI2 SLIM Data: x%x x%lx\n",
10096                       phba->brd_no, totiocbsize,
10097                       (unsigned long) MAX_SLIM_IOCB_SIZE);
10098        }
10099        if (phba->cfg_multi_ring_support == 2)
10100                lpfc_extra_ring_setup(phba);
10101
10102        return 0;
10103}
10104
10105/**
10106 * lpfc_sli4_queue_init - Queue initialization function
10107 * @phba: Pointer to HBA context object.
10108 *
10109 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
10110 * ring. This function also initializes ring indices of each ring.
10111 * This function is called during the initialization of the SLI
10112 * interface of an HBA.
10113 * This function is called with no lock held and always returns
10114 * 1.
10115 **/
10116void
10117lpfc_sli4_queue_init(struct lpfc_hba *phba)
10118{
10119        struct lpfc_sli *psli;
10120        struct lpfc_sli_ring *pring;
10121        int i;
10122
10123        psli = &phba->sli;
10124        spin_lock_irq(&phba->hbalock);
10125        INIT_LIST_HEAD(&psli->mboxq);
10126        INIT_LIST_HEAD(&psli->mboxq_cmpl);
10127        /* Initialize list headers for txq and txcmplq as double linked lists */
10128        for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
10129                pring = phba->sli4_hba.fcp_wq[i]->pring;
10130                pring->flag = 0;
10131                pring->ringno = LPFC_FCP_RING;
10132                INIT_LIST_HEAD(&pring->txq);
10133                INIT_LIST_HEAD(&pring->txcmplq);
10134                INIT_LIST_HEAD(&pring->iocb_continueq);
10135                spin_lock_init(&pring->ring_lock);
10136        }
10137        for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
10138                pring = phba->sli4_hba.nvme_wq[i]->pring;
10139                pring->flag = 0;
10140                pring->ringno = LPFC_FCP_RING;
10141                INIT_LIST_HEAD(&pring->txq);
10142                INIT_LIST_HEAD(&pring->txcmplq);
10143                INIT_LIST_HEAD(&pring->iocb_continueq);
10144                spin_lock_init(&pring->ring_lock);
10145        }
10146        pring = phba->sli4_hba.els_wq->pring;
10147        pring->flag = 0;
10148        pring->ringno = LPFC_ELS_RING;
10149        INIT_LIST_HEAD(&pring->txq);
10150        INIT_LIST_HEAD(&pring->txcmplq);
10151        INIT_LIST_HEAD(&pring->iocb_continueq);
10152        spin_lock_init(&pring->ring_lock);
10153
10154        if (phba->cfg_nvme_io_channel) {
10155                pring = phba->sli4_hba.nvmels_wq->pring;
10156                pring->flag = 0;
10157                pring->ringno = LPFC_ELS_RING;
10158                INIT_LIST_HEAD(&pring->txq);
10159                INIT_LIST_HEAD(&pring->txcmplq);
10160                INIT_LIST_HEAD(&pring->iocb_continueq);
10161                spin_lock_init(&pring->ring_lock);
10162        }
10163
10164        if (phba->cfg_fof) {
10165                pring = phba->sli4_hba.oas_wq->pring;
10166                pring->flag = 0;
10167                pring->ringno = LPFC_FCP_RING;
10168                INIT_LIST_HEAD(&pring->txq);
10169                INIT_LIST_HEAD(&pring->txcmplq);
10170                INIT_LIST_HEAD(&pring->iocb_continueq);
10171                spin_lock_init(&pring->ring_lock);
10172        }
10173
10174        spin_unlock_irq(&phba->hbalock);
10175}
10176
10177/**
10178 * lpfc_sli_queue_init - Queue initialization function
10179 * @phba: Pointer to HBA context object.
10180 *
10181 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10182 * ring. This function also initializes ring indices of each ring.
10183 * This function is called during the initialization of the SLI
10184 * interface of an HBA.
10185 * This function is called with no lock held and always returns
10186 * 1.
10187 **/
10188void
10189lpfc_sli_queue_init(struct lpfc_hba *phba)
10190{
10191        struct lpfc_sli *psli;
10192        struct lpfc_sli_ring *pring;
10193        int i;
10194
10195        psli = &phba->sli;
10196        spin_lock_irq(&phba->hbalock);
10197        INIT_LIST_HEAD(&psli->mboxq);
10198        INIT_LIST_HEAD(&psli->mboxq_cmpl);
10199        /* Initialize list headers for txq and txcmplq as double linked lists */
10200        for (i = 0; i < psli->num_rings; i++) {
10201                pring = &psli->sli3_ring[i];
10202                pring->ringno = i;
10203                pring->sli.sli3.next_cmdidx  = 0;
10204                pring->sli.sli3.local_getidx = 0;
10205                pring->sli.sli3.cmdidx = 0;
10206                INIT_LIST_HEAD(&pring->iocb_continueq);
10207                INIT_LIST_HEAD(&pring->iocb_continue_saveq);
10208                INIT_LIST_HEAD(&pring->postbufq);
10209                pring->flag = 0;
10210                INIT_LIST_HEAD(&pring->txq);
10211                INIT_LIST_HEAD(&pring->txcmplq);
10212                spin_lock_init(&pring->ring_lock);
10213        }
10214        spin_unlock_irq(&phba->hbalock);
10215}
10216
10217/**
10218 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10219 * @phba: Pointer to HBA context object.
10220 *
10221 * This routine flushes the mailbox command subsystem. It will unconditionally
10222 * flush all the mailbox commands in the three possible stages in the mailbox
10223 * command sub-system: pending mailbox command queue; the outstanding mailbox
10224 * command; and completed mailbox command queue. It is caller's responsibility
10225 * to make sure that the driver is in the proper state to flush the mailbox
10226 * command sub-system. Namely, the posting of mailbox commands into the
10227 * pending mailbox command queue from the various clients must be stopped;
10228 * either the HBA is in a state that it will never works on the outstanding
10229 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10230 * mailbox command has been completed.
10231 **/
10232static void
10233lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10234{
10235        LIST_HEAD(completions);
10236        struct lpfc_sli *psli = &phba->sli;
10237        LPFC_MBOXQ_t *pmb;
10238        unsigned long iflag;
10239
10240        /* Flush all the mailbox commands in the mbox system */
10241        spin_lock_irqsave(&phba->hbalock, iflag);
10242        /* The pending mailbox command queue */
10243        list_splice_init(&phba->sli.mboxq, &completions);
10244        /* The outstanding active mailbox command */
10245        if (psli->mbox_active) {
10246                list_add_tail(&psli->mbox_active->list, &completions);
10247                psli->mbox_active = NULL;
10248                psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10249        }
10250        /* The completed mailbox command queue */
10251        list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10252        spin_unlock_irqrestore(&phba->hbalock, iflag);
10253
10254        /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10255        while (!list_empty(&completions)) {
10256                list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10257                pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10258                if (pmb->mbox_cmpl)
10259                        pmb->mbox_cmpl(phba, pmb);
10260        }
10261}
10262
10263/**
10264 * lpfc_sli_host_down - Vport cleanup function
10265 * @vport: Pointer to virtual port object.
10266 *
10267 * lpfc_sli_host_down is called to clean up the resources
10268 * associated with a vport before destroying virtual
10269 * port data structures.
10270 * This function does following operations:
10271 * - Free discovery resources associated with this virtual
10272 *   port.
10273 * - Free iocbs associated with this virtual port in
10274 *   the txq.
10275 * - Send abort for all iocb commands associated with this
10276 *   vport in txcmplq.
10277 *
10278 * This function is called with no lock held and always returns 1.
10279 **/
10280int
10281lpfc_sli_host_down(struct lpfc_vport *vport)
10282{
10283        LIST_HEAD(completions);
10284        struct lpfc_hba *phba = vport->phba;
10285        struct lpfc_sli *psli = &phba->sli;
10286        struct lpfc_queue *qp = NULL;
10287        struct lpfc_sli_ring *pring;
10288        struct lpfc_iocbq *iocb, *next_iocb;
10289        int i;
10290        unsigned long flags = 0;
10291        uint16_t prev_pring_flag;
10292
10293        lpfc_cleanup_discovery_resources(vport);
10294
10295        spin_lock_irqsave(&phba->hbalock, flags);
10296
10297        /*
10298         * Error everything on the txq since these iocbs
10299         * have not been given to the FW yet.
10300         * Also issue ABTS for everything on the txcmplq
10301         */
10302        if (phba->sli_rev != LPFC_SLI_REV4) {
10303                for (i = 0; i < psli->num_rings; i++) {
10304                        pring = &psli->sli3_ring[i];
10305                        prev_pring_flag = pring->flag;
10306                        /* Only slow rings */
10307                        if (pring->ringno == LPFC_ELS_RING) {
10308                                pring->flag |= LPFC_DEFERRED_RING_EVENT;
10309                                /* Set the lpfc data pending flag */
10310                                set_bit(LPFC_DATA_READY, &phba->data_flags);
10311                        }
10312                        list_for_each_entry_safe(iocb, next_iocb,
10313                                                 &pring->txq, list) {
10314                                if (iocb->vport != vport)
10315                                        continue;
10316                                list_move_tail(&iocb->list, &completions);
10317                        }
10318                        list_for_each_entry_safe(iocb, next_iocb,
10319                                                 &pring->txcmplq, list) {
10320                                if (iocb->vport != vport)
10321                                        continue;
10322                                lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10323                        }
10324                        pring->flag = prev_pring_flag;
10325                }
10326        } else {
10327                list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10328                        pring = qp->pring;
10329                        if (!pring)
10330                                continue;
10331                        if (pring == phba->sli4_hba.els_wq->pring) {
10332                                pring->flag |= LPFC_DEFERRED_RING_EVENT;
10333                                /* Set the lpfc data pending flag */
10334                                set_bit(LPFC_DATA_READY, &phba->data_flags);
10335                        }
10336                        prev_pring_flag = pring->flag;
10337                        spin_lock_irq(&pring->ring_lock);
10338                        list_for_each_entry_safe(iocb, next_iocb,
10339                                                 &pring->txq, list) {
10340                                if (iocb->vport != vport)
10341                                        continue;
10342                                list_move_tail(&iocb->list, &completions);
10343                        }
10344                        spin_unlock_irq(&pring->ring_lock);
10345                        list_for_each_entry_safe(iocb, next_iocb,
10346                                                 &pring->txcmplq, list) {
10347                                if (iocb->vport != vport)
10348                                        continue;
10349                                lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10350                        }
10351                        pring->flag = prev_pring_flag;
10352                }
10353        }
10354        spin_unlock_irqrestore(&phba->hbalock, flags);
10355
10356        /* Cancel all the IOCBs from the completions list */
10357        lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10358                              IOERR_SLI_DOWN);
10359        return 1;
10360}
10361
10362/**
10363 * lpfc_sli_hba_down - Resource cleanup function for the HBA
10364 * @phba: Pointer to HBA context object.
10365 *
10366 * This function cleans up all iocb, buffers, mailbox commands
10367 * while shutting down the HBA. This function is called with no
10368 * lock held and always returns 1.
10369 * This function does the following to cleanup driver resources:
10370 * - Free discovery resources for each virtual port
10371 * - Cleanup any pending fabric iocbs
10372 * - Iterate through the iocb txq and free each entry
10373 *   in the list.
10374 * - Free up any buffer posted to the HBA
10375 * - Free mailbox commands in the mailbox queue.
10376 **/
10377int
10378lpfc_sli_hba_down(struct lpfc_hba *phba)
10379{
10380        LIST_HEAD(completions);
10381        struct lpfc_sli *psli = &phba->sli;
10382        struct lpfc_queue *qp = NULL;
10383        struct lpfc_sli_ring *pring;
10384        struct lpfc_dmabuf *buf_ptr;
10385        unsigned long flags = 0;
10386        int i;
10387
10388        /* Shutdown the mailbox command sub-system */
10389        lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
10390
10391        lpfc_hba_down_prep(phba);
10392
10393        lpfc_fabric_abort_hba(phba);
10394
10395        spin_lock_irqsave(&phba->hbalock, flags);
10396
10397        /*
10398         * Error everything on the txq since these iocbs
10399         * have not been given to the FW yet.
10400         */
10401        if (phba->sli_rev != LPFC_SLI_REV4) {
10402                for (i = 0; i < psli->num_rings; i++) {
10403                        pring = &psli->sli3_ring[i];
10404                        /* Only slow rings */
10405                        if (pring->ringno == LPFC_ELS_RING) {
10406                                pring->flag |= LPFC_DEFERRED_RING_EVENT;
10407                                /* Set the lpfc data pending flag */
10408                                set_bit(LPFC_DATA_READY, &phba->data_flags);
10409                        }
10410                        list_splice_init(&pring->txq, &completions);
10411                }
10412        } else {
10413                list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10414                        pring = qp->pring;
10415                        if (!pring)
10416                                continue;
10417                        spin_lock_irq(&pring->ring_lock);
10418                        list_splice_init(&pring->txq, &completions);
10419                        spin_unlock_irq(&pring->ring_lock);
10420                        if (pring == phba->sli4_hba.els_wq->pring) {
10421                                pring->flag |= LPFC_DEFERRED_RING_EVENT;
10422                                /* Set the lpfc data pending flag */
10423                                set_bit(LPFC_DATA_READY, &phba->data_flags);
10424                        }
10425                }
10426        }
10427        spin_unlock_irqrestore(&phba->hbalock, flags);
10428
10429        /* Cancel all the IOCBs from the completions list */
10430        lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10431                              IOERR_SLI_DOWN);
10432
10433        spin_lock_irqsave(&phba->hbalock, flags);
10434        list_splice_init(&phba->elsbuf, &completions);
10435        phba->elsbuf_cnt = 0;
10436        phba->elsbuf_prev_cnt = 0;
10437        spin_unlock_irqrestore(&phba->hbalock, flags);
10438
10439        while (!list_empty(&completions)) {
10440                list_remove_head(&completions, buf_ptr,
10441                        struct lpfc_dmabuf, list);
10442                lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10443                kfree(buf_ptr);
10444        }
10445
10446        /* Return any active mbox cmds */
10447        del_timer_sync(&psli->mbox_tmo);
10448
10449        spin_lock_irqsave(&phba->pport->work_port_lock, flags);
10450        phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10451        spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
10452
10453        return 1;
10454}
10455
10456/**
10457 * lpfc_sli_pcimem_bcopy - SLI memory copy function
10458 * @srcp: Source memory pointer.
10459 * @destp: Destination memory pointer.
10460 * @cnt: Number of words required to be copied.
10461 *
10462 * This function is used for copying data between driver memory
10463 * and the SLI memory. This function also changes the endianness
10464 * of each word if native endianness is different from SLI
10465 * endianness. This function can be called with or without
10466 * lock.
10467 **/
10468void
10469lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10470{
10471        uint32_t *src = srcp;
10472        uint32_t *dest = destp;
10473        uint32_t ldata;
10474        int i;
10475
10476        for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10477                ldata = *src;
10478                ldata = le32_to_cpu(ldata);
10479                *dest = ldata;
10480                src++;
10481                dest++;
10482        }
10483}
10484
10485
10486/**
10487 * lpfc_sli_bemem_bcopy - SLI memory copy function
10488 * @srcp: Source memory pointer.
10489 * @destp: Destination memory pointer.
10490 * @cnt: Number of words required to be copied.
10491 *
10492 * This function is used for copying data between a data structure
10493 * with big endian representation to local endianness.
10494 * This function can be called with or without lock.
10495 **/
10496void
10497lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10498{
10499        uint32_t *src = srcp;
10500        uint32_t *dest = destp;
10501        uint32_t ldata;
10502        int i;
10503
10504        for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10505                ldata = *src;
10506                ldata = be32_to_cpu(ldata);
10507                *dest = ldata;
10508                src++;
10509                dest++;
10510        }
10511}
10512
10513/**
10514 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
10515 * @phba: Pointer to HBA context object.
10516 * @pring: Pointer to driver SLI ring object.
10517 * @mp: Pointer to driver buffer object.
10518 *
10519 * This function is called with no lock held.
10520 * It always return zero after adding the buffer to the postbufq
10521 * buffer list.
10522 **/
10523int
10524lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10525                         struct lpfc_dmabuf *mp)
10526{
10527        /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10528           later */
10529        spin_lock_irq(&phba->hbalock);
10530        list_add_tail(&mp->list, &pring->postbufq);
10531        pring->postbufq_cnt++;
10532        spin_unlock_irq(&phba->hbalock);
10533        return 0;
10534}
10535
10536/**
10537 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
10538 * @phba: Pointer to HBA context object.
10539 *
10540 * When HBQ is enabled, buffers are searched based on tags. This function
10541 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10542 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10543 * does not conflict with tags of buffer posted for unsolicited events.
10544 * The function returns the allocated tag. The function is called with
10545 * no locks held.
10546 **/
10547uint32_t
10548lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10549{
10550        spin_lock_irq(&phba->hbalock);
10551        phba->buffer_tag_count++;
10552        /*
10553         * Always set the QUE_BUFTAG_BIT to distiguish between
10554         * a tag assigned by HBQ.
10555         */
10556        phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10557        spin_unlock_irq(&phba->hbalock);
10558        return phba->buffer_tag_count;
10559}
10560
10561/**
10562 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
10563 * @phba: Pointer to HBA context object.
10564 * @pring: Pointer to driver SLI ring object.
10565 * @tag: Buffer tag.
10566 *
10567 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10568 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10569 * iocb is posted to the response ring with the tag of the buffer.
10570 * This function searches the pring->postbufq list using the tag
10571 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10572 * iocb. If the buffer is found then lpfc_dmabuf object of the
10573 * buffer is returned to the caller else NULL is returned.
10574 * This function is called with no lock held.
10575 **/
10576struct lpfc_dmabuf *
10577lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10578                        uint32_t tag)
10579{
10580        struct lpfc_dmabuf *mp, *next_mp;
10581        struct list_head *slp = &pring->postbufq;
10582
10583        /* Search postbufq, from the beginning, looking for a match on tag */
10584        spin_lock_irq(&phba->hbalock);
10585        list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10586                if (mp->buffer_tag == tag) {
10587                        list_del_init(&mp->list);
10588                        pring->postbufq_cnt--;
10589                        spin_unlock_irq(&phba->hbalock);
10590                        return mp;
10591                }
10592        }
10593
10594        spin_unlock_irq(&phba->hbalock);
10595        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10596                        "0402 Cannot find virtual addr for buffer tag on "
10597                        "ring %d Data x%lx x%p x%p x%x\n",
10598                        pring->ringno, (unsigned long) tag,
10599                        slp->next, slp->prev, pring->postbufq_cnt);
10600
10601        return NULL;
10602}
10603
10604/**
10605 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
10606 * @phba: Pointer to HBA context object.
10607 * @pring: Pointer to driver SLI ring object.
10608 * @phys: DMA address of the buffer.
10609 *
10610 * This function searches the buffer list using the dma_address
10611 * of unsolicited event to find the driver's lpfc_dmabuf object
10612 * corresponding to the dma_address. The function returns the
10613 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10614 * This function is called by the ct and els unsolicited event
10615 * handlers to get the buffer associated with the unsolicited
10616 * event.
10617 *
10618 * This function is called with no lock held.
10619 **/
10620struct lpfc_dmabuf *
10621lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10622                         dma_addr_t phys)
10623{
10624        struct lpfc_dmabuf *mp, *next_mp;
10625        struct list_head *slp = &pring->postbufq;
10626
10627        /* Search postbufq, from the beginning, looking for a match on phys */
10628        spin_lock_irq(&phba->hbalock);
10629        list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10630                if (mp->phys == phys) {
10631                        list_del_init(&mp->list);
10632                        pring->postbufq_cnt--;
10633                        spin_unlock_irq(&phba->hbalock);
10634                        return mp;
10635                }
10636        }
10637
10638        spin_unlock_irq(&phba->hbalock);
10639        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10640                        "0410 Cannot find virtual addr for mapped buf on "
10641                        "ring %d Data x%llx x%p x%p x%x\n",
10642                        pring->ringno, (unsigned long long)phys,
10643                        slp->next, slp->prev, pring->postbufq_cnt);
10644        return NULL;
10645}
10646
10647/**
10648 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
10649 * @phba: Pointer to HBA context object.
10650 * @cmdiocb: Pointer to driver command iocb object.
10651 * @rspiocb: Pointer to driver response iocb object.
10652 *
10653 * This function is the completion handler for the abort iocbs for
10654 * ELS commands. This function is called from the ELS ring event
10655 * handler with no lock held. This function frees memory resources
10656 * associated with the abort iocb.
10657 **/
10658static void
10659lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10660                        struct lpfc_iocbq *rspiocb)
10661{
10662        IOCB_t *irsp = &rspiocb->iocb;
10663        uint16_t abort_iotag, abort_context;
10664        struct lpfc_iocbq *abort_iocb = NULL;
10665
10666        if (irsp->ulpStatus) {
10667
10668                /*
10669                 * Assume that the port already completed and returned, or
10670                 * will return the iocb. Just Log the message.
10671                 */
10672                abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10673                abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10674
10675                spin_lock_irq(&phba->hbalock);
10676                if (phba->sli_rev < LPFC_SLI_REV4) {
10677                        if (abort_iotag != 0 &&
10678                                abort_iotag <= phba->sli.last_iotag)
10679                                abort_iocb =
10680                                        phba->sli.iocbq_lookup[abort_iotag];
10681                } else
10682                        /* For sli4 the abort_tag is the XRI,
10683                         * so the abort routine puts the iotag  of the iocb
10684                         * being aborted in the context field of the abort
10685                         * IOCB.
10686                         */
10687                        abort_iocb = phba->sli.iocbq_lookup[abort_context];
10688
10689                lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10690                                "0327 Cannot abort els iocb %p "
10691                                "with tag %x context %x, abort status %x, "
10692                                "abort code %x\n",
10693                                abort_iocb, abort_iotag, abort_context,
10694                                irsp->ulpStatus, irsp->un.ulpWord[4]);
10695
10696                spin_unlock_irq(&phba->hbalock);
10697        }
10698        lpfc_sli_release_iocbq(phba, cmdiocb);
10699        return;
10700}
10701
10702/**
10703 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
10704 * @phba: Pointer to HBA context object.
10705 * @cmdiocb: Pointer to driver command iocb object.
10706 * @rspiocb: Pointer to driver response iocb object.
10707 *
10708 * The function is called from SLI ring event handler with no
10709 * lock held. This function is the completion handler for ELS commands
10710 * which are aborted. The function frees memory resources used for
10711 * the aborted ELS commands.
10712 **/
10713static void
10714lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10715                     struct lpfc_iocbq *rspiocb)
10716{
10717        IOCB_t *irsp = &rspiocb->iocb;
10718
10719        /* ELS cmd tag <ulpIoTag> completes */
10720        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10721                        "0139 Ignoring ELS cmd tag x%x completion Data: "
10722                        "x%x x%x x%x\n",
10723                        irsp->ulpIoTag, irsp->ulpStatus,
10724                        irsp->un.ulpWord[4], irsp->ulpTimeout);
10725        if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10726                lpfc_ct_free_iocb(phba, cmdiocb);
10727        else
10728                lpfc_els_free_iocb(phba, cmdiocb);
10729        return;
10730}
10731
10732/**
10733 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
10734 * @phba: Pointer to HBA context object.
10735 * @pring: Pointer to driver SLI ring object.
10736 * @cmdiocb: Pointer to driver command iocb object.
10737 *
10738 * This function issues an abort iocb for the provided command iocb down to
10739 * the port. Other than the case the outstanding command iocb is an abort
10740 * request, this function issues abort out unconditionally. This function is
10741 * called with hbalock held. The function returns 0 when it fails due to
10742 * memory allocation failure or when the command iocb is an abort request.
10743 **/
10744static int
10745lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10746                           struct lpfc_iocbq *cmdiocb)
10747{
10748        struct lpfc_vport *vport = cmdiocb->vport;
10749        struct lpfc_iocbq *abtsiocbp;
10750        IOCB_t *icmd = NULL;
10751        IOCB_t *iabt = NULL;
10752        int retval;
10753        unsigned long iflags;
10754
10755        lockdep_assert_held(&phba->hbalock);
10756
10757        /*
10758         * There are certain command types we don't want to abort.  And we
10759         * don't want to abort commands that are already in the process of
10760         * being aborted.
10761         */
10762        icmd = &cmdiocb->iocb;
10763        if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10764            icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10765            (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10766                return 0;
10767
10768        /* issue ABTS for this IOCB based on iotag */
10769        abtsiocbp = __lpfc_sli_get_iocbq(phba);
10770        if (abtsiocbp == NULL)
10771                return 0;
10772
10773        /* This signals the response to set the correct status
10774         * before calling the completion handler
10775         */
10776        cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10777
10778        iabt = &abtsiocbp->iocb;
10779        iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10780        iabt->un.acxri.abortContextTag = icmd->ulpContext;
10781        if (phba->sli_rev == LPFC_SLI_REV4) {
10782                iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
10783                iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10784        }
10785        else
10786                iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
10787        iabt->ulpLe = 1;
10788        iabt->ulpClass = icmd->ulpClass;
10789
10790        /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10791        abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
10792        if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10793                abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
10794        if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10795                abtsiocbp->iocb_flag |= LPFC_IO_FOF;
10796
10797        if (phba->link_state >= LPFC_LINK_UP)
10798                iabt->ulpCommand = CMD_ABORT_XRI_CN;
10799        else
10800                iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10801
10802        abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
10803        abtsiocbp->vport = vport;
10804
10805        lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10806                         "0339 Abort xri x%x, original iotag x%x, "
10807                         "abort cmd iotag x%x\n",
10808                         iabt->un.acxri.abortIoTag,
10809                         iabt->un.acxri.abortContextTag,
10810                         abtsiocbp->iotag);
10811
10812        if (phba->sli_rev == LPFC_SLI_REV4) {
10813                pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10814                if (unlikely(pring == NULL))
10815                        return 0;
10816                /* Note: both hbalock and ring_lock need to be set here */
10817                spin_lock_irqsave(&pring->ring_lock, iflags);
10818                retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10819                        abtsiocbp, 0);
10820                spin_unlock_irqrestore(&pring->ring_lock, iflags);
10821        } else {
10822                retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10823                        abtsiocbp, 0);
10824        }
10825
10826        if (retval)
10827                __lpfc_sli_release_iocbq(phba, abtsiocbp);
10828
10829        /*
10830         * Caller to this routine should check for IOCB_ERROR
10831         * and handle it properly.  This routine no longer removes
10832         * iocb off txcmplq and call compl in case of IOCB_ERROR.
10833         */
10834        return retval;
10835}
10836
10837/**
10838 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10839 * @phba: Pointer to HBA context object.
10840 * @pring: Pointer to driver SLI ring object.
10841 * @cmdiocb: Pointer to driver command iocb object.
10842 *
10843 * This function issues an abort iocb for the provided command iocb. In case
10844 * of unloading, the abort iocb will not be issued to commands on the ELS
10845 * ring. Instead, the callback function shall be changed to those commands
10846 * so that nothing happens when them finishes. This function is called with
10847 * hbalock held. The function returns 0 when the command iocb is an abort
10848 * request.
10849 **/
10850int
10851lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10852                           struct lpfc_iocbq *cmdiocb)
10853{
10854        struct lpfc_vport *vport = cmdiocb->vport;
10855        int retval = IOCB_ERROR;
10856        IOCB_t *icmd = NULL;
10857
10858        lockdep_assert_held(&phba->hbalock);
10859
10860        /*
10861         * There are certain command types we don't want to abort.  And we
10862         * don't want to abort commands that are already in the process of
10863         * being aborted.
10864         */
10865        icmd = &cmdiocb->iocb;
10866        if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10867            icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10868            (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10869                return 0;
10870
10871        if (!pring) {
10872                if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10873                        cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10874                else
10875                        cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10876                goto abort_iotag_exit;
10877        }
10878
10879        /*
10880         * If we're unloading, don't abort iocb on the ELS ring, but change
10881         * the callback so that nothing happens when it finishes.
10882         */
10883        if ((vport->load_flag & FC_UNLOADING) &&
10884            (pring->ringno == LPFC_ELS_RING)) {
10885                if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10886                        cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10887                else
10888                        cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10889                goto abort_iotag_exit;
10890        }
10891
10892        /* Now, we try to issue the abort to the cmdiocb out */
10893        retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10894
10895abort_iotag_exit:
10896        /*
10897         * Caller to this routine should check for IOCB_ERROR
10898         * and handle it properly.  This routine no longer removes
10899         * iocb off txcmplq and call compl in case of IOCB_ERROR.
10900         */
10901        return retval;
10902}
10903
10904/**
10905 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10906 * @phba: Pointer to HBA context object.
10907 * @pring: Pointer to driver SLI ring object.
10908 * @cmdiocb: Pointer to driver command iocb object.
10909 *
10910 * This function issues an abort iocb for the provided command iocb down to
10911 * the port. Other than the case the outstanding command iocb is an abort
10912 * request, this function issues abort out unconditionally. This function is
10913 * called with hbalock held. The function returns 0 when it fails due to
10914 * memory allocation failure or when the command iocb is an abort request.
10915 **/
10916static int
10917lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10918                        struct lpfc_iocbq *cmdiocb)
10919{
10920        struct lpfc_vport *vport = cmdiocb->vport;
10921        struct lpfc_iocbq *abtsiocbp;
10922        union lpfc_wqe128 *abts_wqe;
10923        int retval;
10924
10925        /*
10926         * There are certain command types we don't want to abort.  And we
10927         * don't want to abort commands that are already in the process of
10928         * being aborted.
10929         */
10930        if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10931            cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10932            (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10933                return 0;
10934
10935        /* issue ABTS for this io based on iotag */
10936        abtsiocbp = __lpfc_sli_get_iocbq(phba);
10937        if (abtsiocbp == NULL)
10938                return 0;
10939
10940        /* This signals the response to set the correct status
10941         * before calling the completion handler
10942         */
10943        cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10944
10945        /* Complete prepping the abort wqe and issue to the FW. */
10946        abts_wqe = &abtsiocbp->wqe;
10947        bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10948        bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10949
10950        /* Explicitly set reserved fields to zero.*/
10951        abts_wqe->abort_cmd.rsrvd4 = 0;
10952        abts_wqe->abort_cmd.rsrvd5 = 0;
10953
10954        /* WQE Common - word 6.  Context is XRI tag.  Set 0. */
10955        bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10956        bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10957
10958        /* word 7 */
10959        bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10960        bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10961        bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10962               cmdiocb->iocb.ulpClass);
10963
10964        /* word 8 - tell the FW to abort the IO associated with this
10965         * outstanding exchange ID.
10966         */
10967        abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10968
10969        /* word 9 - this is the iotag for the abts_wqe completion. */
10970        bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10971               abtsiocbp->iotag);
10972
10973        /* word 10 */
10974        bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10975        bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10976        bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10977
10978        /* word 11 */
10979        bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10980        bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10981        bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10982
10983        /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10984        abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10985        abtsiocbp->vport = vport;
10986        abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
10987        retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
10988        if (retval) {
10989                lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10990                                 "6147 Failed abts issue_wqe with status x%x "
10991                                 "for oxid x%x\n",
10992                                 retval, cmdiocb->sli4_xritag);
10993                lpfc_sli_release_iocbq(phba, abtsiocbp);
10994                return retval;
10995        }
10996
10997        lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10998                         "6148 Drv Abort NVME Request Issued for "
10999                         "ox_id x%x on reqtag x%x\n",
11000                         cmdiocb->sli4_xritag,
11001                         abtsiocbp->iotag);
11002
11003        return retval;
11004}
11005
11006/**
11007 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11008 * @phba: pointer to lpfc HBA data structure.
11009 *
11010 * This routine will abort all pending and outstanding iocbs to an HBA.
11011 **/
11012void
11013lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11014{
11015        struct lpfc_sli *psli = &phba->sli;
11016        struct lpfc_sli_ring *pring;
11017        struct lpfc_queue *qp = NULL;
11018        int i;
11019
11020        if (phba->sli_rev != LPFC_SLI_REV4) {
11021                for (i = 0; i < psli->num_rings; i++) {
11022                        pring = &psli->sli3_ring[i];
11023                        lpfc_sli_abort_iocb_ring(phba, pring);
11024                }
11025                return;
11026        }
11027        list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11028                pring = qp->pring;
11029                if (!pring)
11030                        continue;
11031                lpfc_sli_abort_iocb_ring(phba, pring);
11032        }
11033}
11034
11035/**
11036 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
11037 * @iocbq: Pointer to driver iocb object.
11038 * @vport: Pointer to driver virtual port object.
11039 * @tgt_id: SCSI ID of the target.
11040 * @lun_id: LUN ID of the scsi device.
11041 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11042 *
11043 * This function acts as an iocb filter for functions which abort or count
11044 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11045 * 0 if the filtering criteria is met for the given iocb and will return
11046 * 1 if the filtering criteria is not met.
11047 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11048 * given iocb is for the SCSI device specified by vport, tgt_id and
11049 * lun_id parameter.
11050 * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
11051 * given iocb is for the SCSI target specified by vport and tgt_id
11052 * parameters.
11053 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11054 * given iocb is for the SCSI host associated with the given vport.
11055 * This function is called with no locks held.
11056 **/
11057static int
11058lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11059                           uint16_t tgt_id, uint64_t lun_id,
11060                           lpfc_ctx_cmd ctx_cmd)
11061{
11062        struct lpfc_scsi_buf *lpfc_cmd;
11063        int rc = 1;
11064
11065        if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
11066                return rc;
11067
11068        if (iocbq->vport != vport)
11069                return rc;
11070
11071        lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11072
11073        if (lpfc_cmd->pCmd == NULL)
11074                return rc;
11075
11076        switch (ctx_cmd) {
11077        case LPFC_CTX_LUN:
11078                if ((lpfc_cmd->rdata->pnode) &&
11079                    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11080                    (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
11081                        rc = 0;
11082                break;
11083        case LPFC_CTX_TGT:
11084                if ((lpfc_cmd->rdata->pnode) &&
11085                    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
11086                        rc = 0;
11087                break;
11088        case LPFC_CTX_HOST:
11089                rc = 0;
11090                break;
11091        default:
11092                printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
11093                        __func__, ctx_cmd);
11094                break;
11095        }
11096
11097        return rc;
11098}
11099
11100/**
11101 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
11102 * @vport: Pointer to virtual port.
11103 * @tgt_id: SCSI ID of the target.
11104 * @lun_id: LUN ID of the scsi device.
11105 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11106 *
11107 * This function returns number of FCP commands pending for the vport.
11108 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11109 * commands pending on the vport associated with SCSI device specified
11110 * by tgt_id and lun_id parameters.
11111 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11112 * commands pending on the vport associated with SCSI target specified
11113 * by tgt_id parameter.
11114 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11115 * commands pending on the vport.
11116 * This function returns the number of iocbs which satisfy the filter.
11117 * This function is called without any lock held.
11118 **/
11119int
11120lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11121                  lpfc_ctx_cmd ctx_cmd)
11122{
11123        struct lpfc_hba *phba = vport->phba;
11124        struct lpfc_iocbq *iocbq;
11125        int sum, i;
11126
11127        spin_lock_irq(&phba->hbalock);
11128        for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11129                iocbq = phba->sli.iocbq_lookup[i];
11130
11131                if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11132                                                ctx_cmd) == 0)
11133                        sum++;
11134        }
11135        spin_unlock_irq(&phba->hbalock);
11136
11137        return sum;
11138}
11139
11140/**
11141 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
11142 * @phba: Pointer to HBA context object
11143 * @cmdiocb: Pointer to command iocb object.
11144 * @rspiocb: Pointer to response iocb object.
11145 *
11146 * This function is called when an aborted FCP iocb completes. This
11147 * function is called by the ring event handler with no lock held.
11148 * This function frees the iocb.
11149 **/
11150void
11151lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11152                        struct lpfc_iocbq *rspiocb)
11153{
11154        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11155                        "3096 ABORT_XRI_CN completing on rpi x%x "
11156                        "original iotag x%x, abort cmd iotag x%x "
11157                        "status 0x%x, reason 0x%x\n",
11158                        cmdiocb->iocb.un.acxri.abortContextTag,
11159                        cmdiocb->iocb.un.acxri.abortIoTag,
11160                        cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11161                        rspiocb->iocb.un.ulpWord[4]);
11162        lpfc_sli_release_iocbq(phba, cmdiocb);
11163        return;
11164}
11165
11166/**
11167 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
11168 * @vport: Pointer to virtual port.
11169 * @pring: Pointer to driver SLI ring object.
11170 * @tgt_id: SCSI ID of the target.
11171 * @lun_id: LUN ID of the scsi device.
11172 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11173 *
11174 * This function sends an abort command for every SCSI command
11175 * associated with the given virtual port pending on the ring
11176 * filtered by lpfc_sli_validate_fcp_iocb function.
11177 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11178 * FCP iocbs associated with lun specified by tgt_id and lun_id
11179 * parameters
11180 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11181 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11182 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11183 * FCP iocbs associated with virtual port.
11184 * This function returns number of iocbs it failed to abort.
11185 * This function is called with no locks held.
11186 **/
11187int
11188lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11189                    uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
11190{
11191        struct lpfc_hba *phba = vport->phba;
11192        struct lpfc_iocbq *iocbq;
11193        struct lpfc_iocbq *abtsiocb;
11194        struct lpfc_sli_ring *pring_s4;
11195        IOCB_t *cmd = NULL;
11196        int errcnt = 0, ret_val = 0;
11197        int i;
11198
11199        for (i = 1; i <= phba->sli.last_iotag; i++) {
11200                iocbq = phba->sli.iocbq_lookup[i];
11201
11202                if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11203                                               abort_cmd) != 0)
11204                        continue;
11205
11206                /*
11207                 * If the iocbq is already being aborted, don't take a second
11208                 * action, but do count it.
11209                 */
11210                if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11211                        continue;
11212
11213                /* issue ABTS for this IOCB based on iotag */
11214                abtsiocb = lpfc_sli_get_iocbq(phba);
11215                if (abtsiocb == NULL) {
11216                        errcnt++;
11217                        continue;
11218                }
11219
11220                /* indicate the IO is being aborted by the driver. */
11221                iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11222
11223                cmd = &iocbq->iocb;
11224                abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11225                abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
11226                if (phba->sli_rev == LPFC_SLI_REV4)
11227                        abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11228                else
11229                        abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
11230                abtsiocb->iocb.ulpLe = 1;
11231                abtsiocb->iocb.ulpClass = cmd->ulpClass;
11232                abtsiocb->vport = vport;
11233
11234                /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11235                abtsiocb->hba_wqidx = iocbq->hba_wqidx;
11236                if (iocbq->iocb_flag & LPFC_IO_FCP)
11237                        abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
11238                if (iocbq->iocb_flag & LPFC_IO_FOF)
11239                        abtsiocb->iocb_flag |= LPFC_IO_FOF;
11240
11241                if (lpfc_is_link_up(phba))
11242                        abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11243                else
11244                        abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11245
11246                /* Setup callback routine and issue the command. */
11247                abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11248                if (phba->sli_rev == LPFC_SLI_REV4) {
11249                        pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11250                        if (!pring_s4)
11251                                continue;
11252                        ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11253                                                      abtsiocb, 0);
11254                } else
11255                        ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11256                                                      abtsiocb, 0);
11257                if (ret_val == IOCB_ERROR) {
11258                        lpfc_sli_release_iocbq(phba, abtsiocb);
11259                        errcnt++;
11260                        continue;
11261                }
11262        }
11263
11264        return errcnt;
11265}
11266
11267/**
11268 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11269 * @vport: Pointer to virtual port.
11270 * @pring: Pointer to driver SLI ring object.
11271 * @tgt_id: SCSI ID of the target.
11272 * @lun_id: LUN ID of the scsi device.
11273 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11274 *
11275 * This function sends an abort command for every SCSI command
11276 * associated with the given virtual port pending on the ring
11277 * filtered by lpfc_sli_validate_fcp_iocb function.
11278 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11279 * FCP iocbs associated with lun specified by tgt_id and lun_id
11280 * parameters
11281 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11282 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11283 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11284 * FCP iocbs associated with virtual port.
11285 * This function returns number of iocbs it aborted .
11286 * This function is called with no locks held right after a taskmgmt
11287 * command is sent.
11288 **/
11289int
11290lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11291                        uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11292{
11293        struct lpfc_hba *phba = vport->phba;
11294        struct lpfc_scsi_buf *lpfc_cmd;
11295        struct lpfc_iocbq *abtsiocbq;
11296        struct lpfc_nodelist *ndlp;
11297        struct lpfc_iocbq *iocbq;
11298        IOCB_t *icmd;
11299        int sum, i, ret_val;
11300        unsigned long iflags;
11301        struct lpfc_sli_ring *pring_s4;
11302
11303        spin_lock_irq(&phba->hbalock);
11304
11305        /* all I/Os are in process of being flushed */
11306        if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11307                spin_unlock_irq(&phba->hbalock);
11308                return 0;
11309        }
11310        sum = 0;
11311
11312        for (i = 1; i <= phba->sli.last_iotag; i++) {
11313                iocbq = phba->sli.iocbq_lookup[i];
11314
11315                if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11316                                               cmd) != 0)
11317                        continue;
11318
11319                /*
11320                 * If the iocbq is already being aborted, don't take a second
11321                 * action, but do count it.
11322                 */
11323                if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11324                        continue;
11325
11326                /* issue ABTS for this IOCB based on iotag */
11327                abtsiocbq = __lpfc_sli_get_iocbq(phba);
11328                if (abtsiocbq == NULL)
11329                        continue;
11330
11331                icmd = &iocbq->iocb;
11332                abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11333                abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11334                if (phba->sli_rev == LPFC_SLI_REV4)
11335                        abtsiocbq->iocb.un.acxri.abortIoTag =
11336                                                         iocbq->sli4_xritag;
11337                else
11338                        abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11339                abtsiocbq->iocb.ulpLe = 1;
11340                abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11341                abtsiocbq->vport = vport;
11342
11343                /* ABTS WQE must go to the same WQ as the WQE to be aborted */
11344                abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
11345                if (iocbq->iocb_flag & LPFC_IO_FCP)
11346                        abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
11347                if (iocbq->iocb_flag & LPFC_IO_FOF)
11348                        abtsiocbq->iocb_flag |= LPFC_IO_FOF;
11349
11350                lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11351                ndlp = lpfc_cmd->rdata->pnode;
11352
11353                if (lpfc_is_link_up(phba) &&
11354                    (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
11355                        abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11356                else
11357                        abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11358
11359                /* Setup callback routine and issue the command. */
11360                abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11361
11362                /*
11363                 * Indicate the IO is being aborted by the driver and set
11364                 * the caller's flag into the aborted IO.
11365                 */
11366                iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11367
11368                if (phba->sli_rev == LPFC_SLI_REV4) {
11369                        pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11370                        if (pring_s4 == NULL)
11371                                continue;
11372                        /* Note: both hbalock and ring_lock must be set here */
11373                        spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11374                        ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11375                                                        abtsiocbq, 0);
11376                        spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11377                } else {
11378                        ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11379                                                        abtsiocbq, 0);
11380                }
11381
11382
11383                if (ret_val == IOCB_ERROR)
11384                        __lpfc_sli_release_iocbq(phba, abtsiocbq);
11385                else
11386                        sum++;
11387        }
11388        spin_unlock_irq(&phba->hbalock);
11389        return sum;
11390}
11391
11392/**
11393 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
11394 * @phba: Pointer to HBA context object.
11395 * @cmdiocbq: Pointer to command iocb.
11396 * @rspiocbq: Pointer to response iocb.
11397 *
11398 * This function is the completion handler for iocbs issued using
11399 * lpfc_sli_issue_iocb_wait function. This function is called by the
11400 * ring event handler function without any lock held. This function
11401 * can be called from both worker thread context and interrupt
11402 * context. This function also can be called from other thread which
11403 * cleans up the SLI layer objects.
11404 * This function copy the contents of the response iocb to the
11405 * response iocb memory object provided by the caller of
11406 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11407 * sleeps for the iocb completion.
11408 **/
11409static void
11410lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11411                        struct lpfc_iocbq *cmdiocbq,
11412                        struct lpfc_iocbq *rspiocbq)
11413{
11414        wait_queue_head_t *pdone_q;
11415        unsigned long iflags;
11416        struct lpfc_scsi_buf *lpfc_cmd;
11417
11418        spin_lock_irqsave(&phba->hbalock, iflags);
11419        if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11420
11421                /*
11422                 * A time out has occurred for the iocb.  If a time out
11423                 * completion handler has been supplied, call it.  Otherwise,
11424                 * just free the iocbq.
11425                 */
11426
11427                spin_unlock_irqrestore(&phba->hbalock, iflags);
11428                cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11429                cmdiocbq->wait_iocb_cmpl = NULL;
11430                if (cmdiocbq->iocb_cmpl)
11431                        (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11432                else
11433                        lpfc_sli_release_iocbq(phba, cmdiocbq);
11434                return;
11435        }
11436
11437        cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11438        if (cmdiocbq->context2 && rspiocbq)
11439                memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11440                       &rspiocbq->iocb, sizeof(IOCB_t));
11441
11442        /* Set the exchange busy flag for task management commands */
11443        if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11444                !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11445                lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11446                        cur_iocbq);
11447                lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11448        }
11449
11450        pdone_q = cmdiocbq->context_un.wait_queue;
11451        if (pdone_q)
11452                wake_up(pdone_q);
11453        spin_unlock_irqrestore(&phba->hbalock, iflags);
11454        return;
11455}
11456
11457/**
11458 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11459 * @phba: Pointer to HBA context object..
11460 * @piocbq: Pointer to command iocb.
11461 * @flag: Flag to test.
11462 *
11463 * This routine grabs the hbalock and then test the iocb_flag to
11464 * see if the passed in flag is set.
11465 * Returns:
11466 * 1 if flag is set.
11467 * 0 if flag is not set.
11468 **/
11469static int
11470lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11471                 struct lpfc_iocbq *piocbq, uint32_t flag)
11472{
11473        unsigned long iflags;
11474        int ret;
11475
11476        spin_lock_irqsave(&phba->hbalock, iflags);
11477        ret = piocbq->iocb_flag & flag;
11478        spin_unlock_irqrestore(&phba->hbalock, iflags);
11479        return ret;
11480
11481}
11482
11483/**
11484 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
11485 * @phba: Pointer to HBA context object..
11486 * @pring: Pointer to sli ring.
11487 * @piocb: Pointer to command iocb.
11488 * @prspiocbq: Pointer to response iocb.
11489 * @timeout: Timeout in number of seconds.
11490 *
11491 * This function issues the iocb to firmware and waits for the
11492 * iocb to complete. The iocb_cmpl field of the shall be used
11493 * to handle iocbs which time out. If the field is NULL, the
11494 * function shall free the iocbq structure.  If more clean up is
11495 * needed, the caller is expected to provide a completion function
11496 * that will provide the needed clean up.  If the iocb command is
11497 * not completed within timeout seconds, the function will either
11498 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11499 * completion function set in the iocb_cmpl field and then return
11500 * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
11501 * resources if this function returns IOCB_TIMEDOUT.
11502 * The function waits for the iocb completion using an
11503 * non-interruptible wait.
11504 * This function will sleep while waiting for iocb completion.
11505 * So, this function should not be called from any context which
11506 * does not allow sleeping. Due to the same reason, this function
11507 * cannot be called with interrupt disabled.
11508 * This function assumes that the iocb completions occur while
11509 * this function sleep. So, this function cannot be called from
11510 * the thread which process iocb completion for this ring.
11511 * This function clears the iocb_flag of the iocb object before
11512 * issuing the iocb and the iocb completion handler sets this
11513 * flag and wakes this thread when the iocb completes.
11514 * The contents of the response iocb will be copied to prspiocbq
11515 * by the completion handler when the command completes.
11516 * This function returns IOCB_SUCCESS when success.
11517 * This function is called with no lock held.
11518 **/
11519int
11520lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
11521                         uint32_t ring_number,
11522                         struct lpfc_iocbq *piocb,
11523                         struct lpfc_iocbq *prspiocbq,
11524                         uint32_t timeout)
11525{
11526        DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
11527        long timeleft, timeout_req = 0;
11528        int retval = IOCB_SUCCESS;
11529        uint32_t creg_val;
11530        struct lpfc_iocbq *iocb;
11531        int txq_cnt = 0;
11532        int txcmplq_cnt = 0;
11533        struct lpfc_sli_ring *pring;
11534        unsigned long iflags;
11535        bool iocb_completed = true;
11536
11537        if (phba->sli_rev >= LPFC_SLI_REV4)
11538                pring = lpfc_sli4_calc_ring(phba, piocb);
11539        else
11540                pring = &phba->sli.sli3_ring[ring_number];
11541        /*
11542         * If the caller has provided a response iocbq buffer, then context2
11543         * is NULL or its an error.
11544         */
11545        if (prspiocbq) {
11546                if (piocb->context2)
11547                        return IOCB_ERROR;
11548                piocb->context2 = prspiocbq;
11549        }
11550
11551        piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
11552        piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11553        piocb->context_un.wait_queue = &done_q;
11554        piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
11555
11556        if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
11557                if (lpfc_readl(phba->HCregaddr, &creg_val))
11558                        return IOCB_ERROR;
11559                creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11560                writel(creg_val, phba->HCregaddr);
11561                readl(phba->HCregaddr); /* flush */
11562        }
11563
11564        retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11565                                     SLI_IOCB_RET_IOCB);
11566        if (retval == IOCB_SUCCESS) {
11567                timeout_req = msecs_to_jiffies(timeout * 1000);
11568                timeleft = wait_event_timeout(done_q,
11569                                lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
11570                                timeout_req);
11571                spin_lock_irqsave(&phba->hbalock, iflags);
11572                if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
11573
11574                        /*
11575                         * IOCB timed out.  Inform the wake iocb wait
11576                         * completion function and set local status
11577                         */
11578
11579                        iocb_completed = false;
11580                        piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11581                }
11582                spin_unlock_irqrestore(&phba->hbalock, iflags);
11583                if (iocb_completed) {
11584                        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11585                                        "0331 IOCB wake signaled\n");
11586                        /* Note: we are not indicating if the IOCB has a success
11587                         * status or not - that's for the caller to check.
11588                         * IOCB_SUCCESS means just that the command was sent and
11589                         * completed. Not that it completed successfully.
11590                         * */
11591                } else if (timeleft == 0) {
11592                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11593                                        "0338 IOCB wait timeout error - no "
11594                                        "wake response Data x%x\n", timeout);
11595                        retval = IOCB_TIMEDOUT;
11596                } else {
11597                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11598                                        "0330 IOCB wake NOT set, "
11599                                        "Data x%x x%lx\n",
11600                                        timeout, (timeleft / jiffies));
11601                        retval = IOCB_TIMEDOUT;
11602                }
11603        } else if (retval == IOCB_BUSY) {
11604                if (phba->cfg_log_verbose & LOG_SLI) {
11605                        list_for_each_entry(iocb, &pring->txq, list) {
11606                                txq_cnt++;
11607                        }
11608                        list_for_each_entry(iocb, &pring->txcmplq, list) {
11609                                txcmplq_cnt++;
11610                        }
11611                        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11612                                "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11613                                phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11614                }
11615                return retval;
11616        } else {
11617                lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11618                                "0332 IOCB wait issue failed, Data x%x\n",
11619                                retval);
11620                retval = IOCB_ERROR;
11621        }
11622
11623        if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
11624                if (lpfc_readl(phba->HCregaddr, &creg_val))
11625                        return IOCB_ERROR;
11626                creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11627                writel(creg_val, phba->HCregaddr);
11628                readl(phba->HCregaddr); /* flush */
11629        }
11630
11631        if (prspiocbq)
11632                piocb->context2 = NULL;
11633
11634        piocb->context_un.wait_queue = NULL;
11635        piocb->iocb_cmpl = NULL;
11636        return retval;
11637}
11638
11639/**
11640 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
11641 * @phba: Pointer to HBA context object.
11642 * @pmboxq: Pointer to driver mailbox object.
11643 * @timeout: Timeout in number of seconds.
11644 *
11645 * This function issues the mailbox to firmware and waits for the
11646 * mailbox command to complete. If the mailbox command is not
11647 * completed within timeout seconds, it returns MBX_TIMEOUT.
11648 * The function waits for the mailbox completion using an
11649 * interruptible wait. If the thread is woken up due to a
11650 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11651 * should not free the mailbox resources, if this function returns
11652 * MBX_TIMEOUT.
11653 * This function will sleep while waiting for mailbox completion.
11654 * So, this function should not be called from any context which
11655 * does not allow sleeping. Due to the same reason, this function
11656 * cannot be called with interrupt disabled.
11657 * This function assumes that the mailbox completion occurs while
11658 * this function sleep. So, this function cannot be called from
11659 * the worker thread which processes mailbox completion.
11660 * This function is called in the context of HBA management
11661 * applications.
11662 * This function returns MBX_SUCCESS when successful.
11663 * This function is called with no lock held.
11664 **/
11665int
11666lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
11667                         uint32_t timeout)
11668{
11669        struct completion mbox_done;
11670        int retval;
11671        unsigned long flag;
11672
11673        pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
11674        /* setup wake call as IOCB callback */
11675        pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11676
11677        /* setup context3 field to pass wait_queue pointer to wake function  */
11678        init_completion(&mbox_done);
11679        pmboxq->context3 = &mbox_done;
11680        /* now issue the command */
11681        retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
11682        if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
11683                wait_for_completion_timeout(&mbox_done,
11684                                            msecs_to_jiffies(timeout * 1000));
11685
11686                spin_lock_irqsave(&phba->hbalock, flag);
11687                pmboxq->context3 = NULL;
11688                /*
11689                 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11690                 * else do not free the resources.
11691                 */
11692                if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
11693                        retval = MBX_SUCCESS;
11694                } else {
11695                        retval = MBX_TIMEOUT;
11696                        pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11697                }
11698                spin_unlock_irqrestore(&phba->hbalock, flag);
11699        }
11700        return retval;
11701}
11702
11703/**
11704 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
11705 * @phba: Pointer to HBA context.
11706 *
11707 * This function is called to shutdown the driver's mailbox sub-system.
11708 * It first marks the mailbox sub-system is in a block state to prevent
11709 * the asynchronous mailbox command from issued off the pending mailbox
11710 * command queue. If the mailbox command sub-system shutdown is due to
11711 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11712 * the mailbox sub-system flush routine to forcefully bring down the
11713 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11714 * as with offline or HBA function reset), this routine will wait for the
11715 * outstanding mailbox command to complete before invoking the mailbox
11716 * sub-system flush routine to gracefully bring down mailbox sub-system.
11717 **/
11718void
11719lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
11720{
11721        struct lpfc_sli *psli = &phba->sli;
11722        unsigned long timeout;
11723
11724        if (mbx_action == LPFC_MBX_NO_WAIT) {
11725                /* delay 100ms for port state */
11726                msleep(100);
11727                lpfc_sli_mbox_sys_flush(phba);
11728                return;
11729        }
11730        timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
11731
11732        spin_lock_irq(&phba->hbalock);
11733        psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
11734
11735        if (psli->sli_flag & LPFC_SLI_ACTIVE) {
11736                /* Determine how long we might wait for the active mailbox
11737                 * command to be gracefully completed by firmware.
11738                 */
11739                if (phba->sli.mbox_active)
11740                        timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11741                                                phba->sli.mbox_active) *
11742                                                1000) + jiffies;
11743                spin_unlock_irq(&phba->hbalock);
11744
11745                while (phba->sli.mbox_active) {
11746                        /* Check active mailbox complete status every 2ms */
11747                        msleep(2);
11748                        if (time_after(jiffies, timeout))
11749                                /* Timeout, let the mailbox flush routine to
11750                                 * forcefully release active mailbox command
11751                                 */
11752                                break;
11753                }
11754        } else
11755                spin_unlock_irq(&phba->hbalock);
11756
11757        lpfc_sli_mbox_sys_flush(phba);
11758}
11759
11760/**
11761 * lpfc_sli_eratt_read - read sli-3 error attention events
11762 * @phba: Pointer to HBA context.
11763 *
11764 * This function is called to read the SLI3 device error attention registers
11765 * for possible error attention events. The caller must hold the hostlock
11766 * with spin_lock_irq().
11767 *
11768 * This function returns 1 when there is Error Attention in the Host Attention
11769 * Register and returns 0 otherwise.
11770 **/
11771static int
11772lpfc_sli_eratt_read(struct lpfc_hba *phba)
11773{
11774        uint32_t ha_copy;
11775
11776        /* Read chip Host Attention (HA) register */
11777        if (lpfc_readl(phba->HAregaddr, &ha_copy))
11778                goto unplug_err;
11779
11780        if (ha_copy & HA_ERATT) {
11781                /* Read host status register to retrieve error event */
11782                if (lpfc_sli_read_hs(phba))
11783                        goto unplug_err;
11784
11785                /* Check if there is a deferred error condition is active */
11786                if ((HS_FFER1 & phba->work_hs) &&
11787                    ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
11788                      HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
11789                        phba->hba_flag |= DEFER_ERATT;
11790                        /* Clear all interrupt enable conditions */
11791                        writel(0, phba->HCregaddr);
11792                        readl(phba->HCregaddr);
11793                }
11794
11795                /* Set the driver HA work bitmap */
11796                phba->work_ha |= HA_ERATT;
11797                /* Indicate polling handles this ERATT */
11798                phba->hba_flag |= HBA_ERATT_HANDLED;
11799                return 1;
11800        }
11801        return 0;
11802
11803unplug_err:
11804        /* Set the driver HS work bitmap */
11805        phba->work_hs |= UNPLUG_ERR;
11806        /* Set the driver HA work bitmap */
11807        phba->work_ha |= HA_ERATT;
11808        /* Indicate polling handles this ERATT */
11809        phba->hba_flag |= HBA_ERATT_HANDLED;
11810        return 1;
11811}
11812
11813/**
11814 * lpfc_sli4_eratt_read - read sli-4 error attention events
11815 * @phba: Pointer to HBA context.
11816 *
11817 * This function is called to read the SLI4 device error attention registers
11818 * for possible error attention events. The caller must hold the hostlock
11819 * with spin_lock_irq().
11820 *
11821 * This function returns 1 when there is Error Attention in the Host Attention
11822 * Register and returns 0 otherwise.
11823 **/
11824static int
11825lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11826{
11827        uint32_t uerr_sta_hi, uerr_sta_lo;
11828        uint32_t if_type, portsmphr;
11829        struct lpfc_register portstat_reg;
11830
11831        /*
11832         * For now, use the SLI4 device internal unrecoverable error
11833         * registers for error attention. This can be changed later.
11834         */
11835        if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11836        switch (if_type) {
11837        case LPFC_SLI_INTF_IF_TYPE_0:
11838                if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11839                        &uerr_sta_lo) ||
11840                        lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11841                        &uerr_sta_hi)) {
11842                        phba->work_hs |= UNPLUG_ERR;
11843                        phba->work_ha |= HA_ERATT;
11844                        phba->hba_flag |= HBA_ERATT_HANDLED;
11845                        return 1;
11846                }
11847                if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11848                    (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11849                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11850                                        "1423 HBA Unrecoverable error: "
11851                                        "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11852                                        "ue_mask_lo_reg=0x%x, "
11853                                        "ue_mask_hi_reg=0x%x\n",
11854                                        uerr_sta_lo, uerr_sta_hi,
11855                                        phba->sli4_hba.ue_mask_lo,
11856                                        phba->sli4_hba.ue_mask_hi);
11857                        phba->work_status[0] = uerr_sta_lo;
11858                        phba->work_status[1] = uerr_sta_hi;
11859                        phba->work_ha |= HA_ERATT;
11860                        phba->hba_flag |= HBA_ERATT_HANDLED;
11861                        return 1;
11862                }
11863                break;
11864        case LPFC_SLI_INTF_IF_TYPE_2:
11865        case LPFC_SLI_INTF_IF_TYPE_6:
11866                if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11867                        &portstat_reg.word0) ||
11868                        lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11869                        &portsmphr)){
11870                        phba->work_hs |= UNPLUG_ERR;
11871                        phba->work_ha |= HA_ERATT;
11872                        phba->hba_flag |= HBA_ERATT_HANDLED;
11873                        return 1;
11874                }
11875                if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11876                        phba->work_status[0] =
11877                                readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11878                        phba->work_status[1] =
11879                                readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11880                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11881                                        "2885 Port Status Event: "
11882                                        "port status reg 0x%x, "
11883                                        "port smphr reg 0x%x, "
11884                                        "error 1=0x%x, error 2=0x%x\n",
11885                                        portstat_reg.word0,
11886                                        portsmphr,
11887                                        phba->work_status[0],
11888                                        phba->work_status[1]);
11889                        phba->work_ha |= HA_ERATT;
11890                        phba->hba_flag |= HBA_ERATT_HANDLED;
11891                        return 1;
11892                }
11893                break;
11894        case LPFC_SLI_INTF_IF_TYPE_1:
11895        default:
11896                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11897                                "2886 HBA Error Attention on unsupported "
11898                                "if type %d.", if_type);
11899                return 1;
11900        }
11901
11902        return 0;
11903}
11904
11905/**
11906 * lpfc_sli_check_eratt - check error attention events
11907 * @phba: Pointer to HBA context.
11908 *
11909 * This function is called from timer soft interrupt context to check HBA's
11910 * error attention register bit for error attention events.
11911 *
11912 * This function returns 1 when there is Error Attention in the Host Attention
11913 * Register and returns 0 otherwise.
11914 **/
11915int
11916lpfc_sli_check_eratt(struct lpfc_hba *phba)
11917{
11918        uint32_t ha_copy;
11919
11920        /* If somebody is waiting to handle an eratt, don't process it
11921         * here. The brdkill function will do this.
11922         */
11923        if (phba->link_flag & LS_IGNORE_ERATT)
11924                return 0;
11925
11926        /* Check if interrupt handler handles this ERATT */
11927        spin_lock_irq(&phba->hbalock);
11928        if (phba->hba_flag & HBA_ERATT_HANDLED) {
11929                /* Interrupt handler has handled ERATT */
11930                spin_unlock_irq(&phba->hbalock);
11931                return 0;
11932        }
11933
11934        /*
11935         * If there is deferred error attention, do not check for error
11936         * attention
11937         */
11938        if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11939                spin_unlock_irq(&phba->hbalock);
11940                return 0;
11941        }
11942
11943        /* If PCI channel is offline, don't process it */
11944        if (unlikely(pci_channel_offline(phba->pcidev))) {
11945                spin_unlock_irq(&phba->hbalock);
11946                return 0;
11947        }
11948
11949        switch (phba->sli_rev) {
11950        case LPFC_SLI_REV2:
11951        case LPFC_SLI_REV3:
11952                /* Read chip Host Attention (HA) register */
11953                ha_copy = lpfc_sli_eratt_read(phba);
11954                break;
11955        case LPFC_SLI_REV4:
11956                /* Read device Uncoverable Error (UERR) registers */
11957                ha_copy = lpfc_sli4_eratt_read(phba);
11958                break;
11959        default:
11960                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11961                                "0299 Invalid SLI revision (%d)\n",
11962                                phba->sli_rev);
11963                ha_copy = 0;
11964                break;
11965        }
11966        spin_unlock_irq(&phba->hbalock);
11967
11968        return ha_copy;
11969}
11970
11971/**
11972 * lpfc_intr_state_check - Check device state for interrupt handling
11973 * @phba: Pointer to HBA context.
11974 *
11975 * This inline routine checks whether a device or its PCI slot is in a state
11976 * that the interrupt should be handled.
11977 *
11978 * This function returns 0 if the device or the PCI slot is in a state that
11979 * interrupt should be handled, otherwise -EIO.
11980 */
11981static inline int
11982lpfc_intr_state_check(struct lpfc_hba *phba)
11983{
11984        /* If the pci channel is offline, ignore all the interrupts */
11985        if (unlikely(pci_channel_offline(phba->pcidev)))
11986                return -EIO;
11987
11988        /* Update device level interrupt statistics */
11989        phba->sli.slistat.sli_intr++;
11990
11991        /* Ignore all interrupts during initialization. */
11992        if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11993                return -EIO;
11994
11995        return 0;
11996}
11997
11998/**
11999 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
12000 * @irq: Interrupt number.
12001 * @dev_id: The device context pointer.
12002 *
12003 * This function is directly called from the PCI layer as an interrupt
12004 * service routine when device with SLI-3 interface spec is enabled with
12005 * MSI-X multi-message interrupt mode and there are slow-path events in
12006 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12007 * interrupt mode, this function is called as part of the device-level
12008 * interrupt handler. When the PCI slot is in error recovery or the HBA
12009 * is undergoing initialization, the interrupt handler will not process
12010 * the interrupt. The link attention and ELS ring attention events are
12011 * handled by the worker thread. The interrupt handler signals the worker
12012 * thread and returns for these events. This function is called without
12013 * any lock held. It gets the hbalock to access and update SLI data
12014 * structures.
12015 *
12016 * This function returns IRQ_HANDLED when interrupt is handled else it
12017 * returns IRQ_NONE.
12018 **/
12019irqreturn_t
12020lpfc_sli_sp_intr_handler(int irq, void *dev_id)
12021{
12022        struct lpfc_hba  *phba;
12023        uint32_t ha_copy, hc_copy;
12024        uint32_t work_ha_copy;
12025        unsigned long status;
12026        unsigned long iflag;
12027        uint32_t control;
12028
12029        MAILBOX_t *mbox, *pmbox;
12030        struct lpfc_vport *vport;
12031        struct lpfc_nodelist *ndlp;
12032        struct lpfc_dmabuf *mp;
12033        LPFC_MBOXQ_t *pmb;
12034        int rc;
12035
12036        /*
12037         * Get the driver's phba structure from the dev_id and
12038         * assume the HBA is not interrupting.
12039         */
12040        phba = (struct lpfc_hba *)dev_id;
12041
12042        if (unlikely(!phba))
12043                return IRQ_NONE;
12044
12045        /*
12046         * Stuff needs to be attented to when this function is invoked as an
12047         * individual interrupt handler in MSI-X multi-message interrupt mode
12048         */
12049        if (phba->intr_type == MSIX) {
12050                /* Check device state for handling interrupt */
12051                if (lpfc_intr_state_check(phba))
12052                        return IRQ_NONE;
12053                /* Need to read HA REG for slow-path events */
12054                spin_lock_irqsave(&phba->hbalock, iflag);
12055                if (lpfc_readl(phba->HAregaddr, &ha_copy))
12056                        goto unplug_error;
12057                /* If somebody is waiting to handle an eratt don't process it
12058                 * here. The brdkill function will do this.
12059                 */
12060                if (phba->link_flag & LS_IGNORE_ERATT)
12061                        ha_copy &= ~HA_ERATT;
12062                /* Check the need for handling ERATT in interrupt handler */
12063                if (ha_copy & HA_ERATT) {
12064                        if (phba->hba_flag & HBA_ERATT_HANDLED)
12065                                /* ERATT polling has handled ERATT */
12066                                ha_copy &= ~HA_ERATT;
12067                        else
12068                                /* Indicate interrupt handler handles ERATT */
12069                                phba->hba_flag |= HBA_ERATT_HANDLED;
12070                }
12071
12072                /*
12073                 * If there is deferred error attention, do not check for any
12074                 * interrupt.
12075                 */
12076                if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12077                        spin_unlock_irqrestore(&phba->hbalock, iflag);
12078                        return IRQ_NONE;
12079                }
12080
12081                /* Clear up only attention source related to slow-path */
12082                if (lpfc_readl(phba->HCregaddr, &hc_copy))
12083                        goto unplug_error;
12084
12085                writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12086                        HC_LAINT_ENA | HC_ERINT_ENA),
12087                        phba->HCregaddr);
12088                writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12089                        phba->HAregaddr);
12090                writel(hc_copy, phba->HCregaddr);
12091                readl(phba->HAregaddr); /* flush */
12092                spin_unlock_irqrestore(&phba->hbalock, iflag);
12093        } else
12094                ha_copy = phba->ha_copy;
12095
12096        work_ha_copy = ha_copy & phba->work_ha_mask;
12097
12098        if (work_ha_copy) {
12099                if (work_ha_copy & HA_LATT) {
12100                        if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12101                                /*
12102                                 * Turn off Link Attention interrupts
12103                                 * until CLEAR_LA done
12104                                 */
12105                                spin_lock_irqsave(&phba->hbalock, iflag);
12106                                phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
12107                                if (lpfc_readl(phba->HCregaddr, &control))
12108                                        goto unplug_error;
12109                                control &= ~HC_LAINT_ENA;
12110                                writel(control, phba->HCregaddr);
12111                                readl(phba->HCregaddr); /* flush */
12112                                spin_unlock_irqrestore(&phba->hbalock, iflag);
12113                        }
12114                        else
12115                                work_ha_copy &= ~HA_LATT;
12116                }
12117
12118                if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
12119                        /*
12120                         * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12121                         * the only slow ring.
12122                         */
12123                        status = (work_ha_copy &
12124                                (HA_RXMASK  << (4*LPFC_ELS_RING)));
12125                        status >>= (4*LPFC_ELS_RING);
12126                        if (status & HA_RXMASK) {
12127                                spin_lock_irqsave(&phba->hbalock, iflag);
12128                                if (lpfc_readl(phba->HCregaddr, &control))
12129                                        goto unplug_error;
12130
12131                                lpfc_debugfs_slow_ring_trc(phba,
12132                                "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
12133                                control, status,
12134                                (uint32_t)phba->sli.slistat.sli_intr);
12135
12136                                if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
12137                                        lpfc_debugfs_slow_ring_trc(phba,
12138                                                "ISR Disable ring:"
12139                                                "pwork:x%x hawork:x%x wait:x%x",
12140                                                phba->work_ha, work_ha_copy,
12141                                                (uint32_t)((unsigned long)
12142                                                &phba->work_waitq));
12143
12144                                        control &=
12145                                            ~(HC_R0INT_ENA << LPFC_ELS_RING);
12146                                        writel(control, phba->HCregaddr);
12147                                        readl(phba->HCregaddr); /* flush */
12148                                }
12149                                else {
12150                                        lpfc_debugfs_slow_ring_trc(phba,
12151                                                "ISR slow ring:   pwork:"
12152                                                "x%x hawork:x%x wait:x%x",
12153                                                phba->work_ha, work_ha_copy,
12154                                                (uint32_t)((unsigned long)
12155                                                &phba->work_waitq));
12156                                }
12157                                spin_unlock_irqrestore(&phba->hbalock, iflag);
12158                        }
12159                }
12160                spin_lock_irqsave(&phba->hbalock, iflag);
12161                if (work_ha_copy & HA_ERATT) {
12162                        if (lpfc_sli_read_hs(phba))
12163                                goto unplug_error;
12164                        /*
12165                         * Check if there is a deferred error condition
12166                         * is active
12167                         */
12168                        if ((HS_FFER1 & phba->work_hs) &&
12169                                ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
12170                                  HS_FFER6 | HS_FFER7 | HS_FFER8) &
12171                                  phba->work_hs)) {
12172                                phba->hba_flag |= DEFER_ERATT;
12173                                /* Clear all interrupt enable conditions */
12174                                writel(0, phba->HCregaddr);
12175                                readl(phba->HCregaddr);
12176                        }
12177                }
12178
12179                if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
12180                        pmb = phba->sli.mbox_active;
12181                        pmbox = &pmb->u.mb;
12182                        mbox = phba->mbox;
12183                        vport = pmb->vport;
12184
12185                        /* First check out the status word */
12186                        lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12187                        if (pmbox->mbxOwner != OWN_HOST) {
12188                                spin_unlock_irqrestore(&phba->hbalock, iflag);
12189                                /*
12190                                 * Stray Mailbox Interrupt, mbxCommand <cmd>
12191                                 * mbxStatus <status>
12192                                 */
12193                                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12194                                                LOG_SLI,
12195                                                "(%d):0304 Stray Mailbox "
12196                                                "Interrupt mbxCommand x%x "
12197                                                "mbxStatus x%x\n",
12198                                                (vport ? vport->vpi : 0),
12199                                                pmbox->mbxCommand,
12200                                                pmbox->mbxStatus);
12201                                /* clear mailbox attention bit */
12202                                work_ha_copy &= ~HA_MBATT;
12203                        } else {
12204                                phba->sli.mbox_active = NULL;
12205                                spin_unlock_irqrestore(&phba->hbalock, iflag);
12206                                phba->last_completion_time = jiffies;
12207                                del_timer(&phba->sli.mbox_tmo);
12208                                if (pmb->mbox_cmpl) {
12209                                        lpfc_sli_pcimem_bcopy(mbox, pmbox,
12210                                                        MAILBOX_CMD_SIZE);
12211                                        if (pmb->out_ext_byte_len &&
12212                                                pmb->context2)
12213                                                lpfc_sli_pcimem_bcopy(
12214                                                phba->mbox_ext,
12215                                                pmb->context2,
12216                                                pmb->out_ext_byte_len);
12217                                }
12218                                if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12219                                        pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12220
12221                                        lpfc_debugfs_disc_trc(vport,
12222                                                LPFC_DISC_TRC_MBOX_VPORT,
12223                                                "MBOX dflt rpi: : "
12224                                                "status:x%x rpi:x%x",
12225                                                (uint32_t)pmbox->mbxStatus,
12226                                                pmbox->un.varWords[0], 0);
12227
12228                                        if (!pmbox->mbxStatus) {
12229                                                mp = (struct lpfc_dmabuf *)
12230                                                        (pmb->context1);
12231                                                ndlp = (struct lpfc_nodelist *)
12232                                                        pmb->context2;
12233
12234                                                /* Reg_LOGIN of dflt RPI was
12235                                                 * successful. new lets get
12236                                                 * rid of the RPI using the
12237                                                 * same mbox buffer.
12238                                                 */
12239                                                lpfc_unreg_login(phba,
12240                                                        vport->vpi,
12241                                                        pmbox->un.varWords[0],
12242                                                        pmb);
12243                                                pmb->mbox_cmpl =
12244                                                        lpfc_mbx_cmpl_dflt_rpi;
12245                                                pmb->context1 = mp;
12246                                                pmb->context2 = ndlp;
12247                                                pmb->vport = vport;
12248                                                rc = lpfc_sli_issue_mbox(phba,
12249                                                                pmb,
12250                                                                MBX_NOWAIT);
12251                                                if (rc != MBX_BUSY)
12252                                                        lpfc_printf_log(phba,
12253                                                        KERN_ERR,
12254                                                        LOG_MBOX | LOG_SLI,
12255                                                        "0350 rc should have"
12256                                                        "been MBX_BUSY\n");
12257                                                if (rc != MBX_NOT_FINISHED)
12258                                                        goto send_current_mbox;
12259                                        }
12260                                }
12261                                spin_lock_irqsave(
12262                                                &phba->pport->work_port_lock,
12263                                                iflag);
12264                                phba->pport->work_port_events &=
12265                                        ~WORKER_MBOX_TMO;
12266                                spin_unlock_irqrestore(
12267                                                &phba->pport->work_port_lock,
12268                                                iflag);
12269                                lpfc_mbox_cmpl_put(phba, pmb);
12270                        }
12271                } else
12272                        spin_unlock_irqrestore(&phba->hbalock, iflag);
12273
12274                if ((work_ha_copy & HA_MBATT) &&
12275                    (phba->sli.mbox_active == NULL)) {
12276send_current_mbox:
12277                        /* Process next mailbox command if there is one */
12278                        do {
12279                                rc = lpfc_sli_issue_mbox(phba, NULL,
12280                                                         MBX_NOWAIT);
12281                        } while (rc == MBX_NOT_FINISHED);
12282                        if (rc != MBX_SUCCESS)
12283                                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12284                                                LOG_SLI, "0349 rc should be "
12285                                                "MBX_SUCCESS\n");
12286                }
12287
12288                spin_lock_irqsave(&phba->hbalock, iflag);
12289                phba->work_ha |= work_ha_copy;
12290                spin_unlock_irqrestore(&phba->hbalock, iflag);
12291                lpfc_worker_wake_up(phba);
12292        }
12293        return IRQ_HANDLED;
12294unplug_error:
12295        spin_unlock_irqrestore(&phba->hbalock, iflag);
12296        return IRQ_HANDLED;
12297
12298} /* lpfc_sli_sp_intr_handler */
12299
12300/**
12301 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
12302 * @irq: Interrupt number.
12303 * @dev_id: The device context pointer.
12304 *
12305 * This function is directly called from the PCI layer as an interrupt
12306 * service routine when device with SLI-3 interface spec is enabled with
12307 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12308 * ring event in the HBA. However, when the device is enabled with either
12309 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12310 * device-level interrupt handler. When the PCI slot is in error recovery
12311 * or the HBA is undergoing initialization, the interrupt handler will not
12312 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12313 * the intrrupt context. This function is called without any lock held.
12314 * It gets the hbalock to access and update SLI data structures.
12315 *
12316 * This function returns IRQ_HANDLED when interrupt is handled else it
12317 * returns IRQ_NONE.
12318 **/
12319irqreturn_t
12320lpfc_sli_fp_intr_handler(int irq, void *dev_id)
12321{
12322        struct lpfc_hba  *phba;
12323        uint32_t ha_copy;
12324        unsigned long status;
12325        unsigned long iflag;
12326        struct lpfc_sli_ring *pring;
12327
12328        /* Get the driver's phba structure from the dev_id and
12329         * assume the HBA is not interrupting.
12330         */
12331        phba = (struct lpfc_hba *) dev_id;
12332
12333        if (unlikely(!phba))
12334                return IRQ_NONE;
12335
12336        /*
12337         * Stuff needs to be attented to when this function is invoked as an
12338         * individual interrupt handler in MSI-X multi-message interrupt mode
12339         */
12340        if (phba->intr_type == MSIX) {
12341                /* Check device state for handling interrupt */
12342                if (lpfc_intr_state_check(phba))
12343                        return IRQ_NONE;
12344                /* Need to read HA REG for FCP ring and other ring events */
12345                if (lpfc_readl(phba->HAregaddr, &ha_copy))
12346                        return IRQ_HANDLED;
12347                /* Clear up only attention source related to fast-path */
12348                spin_lock_irqsave(&phba->hbalock, iflag);
12349                /*
12350                 * If there is deferred error attention, do not check for
12351                 * any interrupt.
12352                 */
12353                if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12354                        spin_unlock_irqrestore(&phba->hbalock, iflag);
12355                        return IRQ_NONE;
12356                }
12357                writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12358                        phba->HAregaddr);
12359                readl(phba->HAregaddr); /* flush */
12360                spin_unlock_irqrestore(&phba->hbalock, iflag);
12361        } else
12362                ha_copy = phba->ha_copy;
12363
12364        /*
12365         * Process all events on FCP ring. Take the optimized path for FCP IO.
12366         */
12367        ha_copy &= ~(phba->work_ha_mask);
12368
12369        status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12370        status >>= (4*LPFC_FCP_RING);
12371        pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12372        if (status & HA_RXMASK)
12373                lpfc_sli_handle_fast_ring_event(phba, pring, status);
12374
12375        if (phba->cfg_multi_ring_support == 2) {
12376                /*
12377                 * Process all events on extra ring. Take the optimized path
12378                 * for extra ring IO.
12379                 */
12380                status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12381                status >>= (4*LPFC_EXTRA_RING);
12382                if (status & HA_RXMASK) {
12383                        lpfc_sli_handle_fast_ring_event(phba,
12384                                        &phba->sli.sli3_ring[LPFC_EXTRA_RING],
12385                                        status);
12386                }
12387        }
12388        return IRQ_HANDLED;
12389}  /* lpfc_sli_fp_intr_handler */
12390
12391/**
12392 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
12393 * @irq: Interrupt number.
12394 * @dev_id: The device context pointer.
12395 *
12396 * This function is the HBA device-level interrupt handler to device with
12397 * SLI-3 interface spec, called from the PCI layer when either MSI or
12398 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12399 * requires driver attention. This function invokes the slow-path interrupt
12400 * attention handling function and fast-path interrupt attention handling
12401 * function in turn to process the relevant HBA attention events. This
12402 * function is called without any lock held. It gets the hbalock to access
12403 * and update SLI data structures.
12404 *
12405 * This function returns IRQ_HANDLED when interrupt is handled, else it
12406 * returns IRQ_NONE.
12407 **/
12408irqreturn_t
12409lpfc_sli_intr_handler(int irq, void *dev_id)
12410{
12411        struct lpfc_hba  *phba;
12412        irqreturn_t sp_irq_rc, fp_irq_rc;
12413        unsigned long status1, status2;
12414        uint32_t hc_copy;
12415
12416        /*
12417         * Get the driver's phba structure from the dev_id and
12418         * assume the HBA is not interrupting.
12419         */
12420        phba = (struct lpfc_hba *) dev_id;
12421
12422        if (unlikely(!phba))
12423                return IRQ_NONE;
12424
12425        /* Check device state for handling interrupt */
12426        if (lpfc_intr_state_check(phba))
12427                return IRQ_NONE;
12428
12429        spin_lock(&phba->hbalock);
12430        if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12431                spin_unlock(&phba->hbalock);
12432                return IRQ_HANDLED;
12433        }
12434
12435        if (unlikely(!phba->ha_copy)) {
12436                spin_unlock(&phba->hbalock);
12437                return IRQ_NONE;
12438        } else if (phba->ha_copy & HA_ERATT) {
12439                if (phba->hba_flag & HBA_ERATT_HANDLED)
12440                        /* ERATT polling has handled ERATT */
12441                        phba->ha_copy &= ~HA_ERATT;
12442                else
12443                        /* Indicate interrupt handler handles ERATT */
12444                        phba->hba_flag |= HBA_ERATT_HANDLED;
12445        }
12446
12447        /*
12448         * If there is deferred error attention, do not check for any interrupt.
12449         */
12450        if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12451                spin_unlock(&phba->hbalock);
12452                return IRQ_NONE;
12453        }
12454
12455        /* Clear attention sources except link and error attentions */
12456        if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12457                spin_unlock(&phba->hbalock);
12458                return IRQ_HANDLED;
12459        }
12460        writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12461                | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12462                phba->HCregaddr);
12463        writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
12464        writel(hc_copy, phba->HCregaddr);
12465        readl(phba->HAregaddr); /* flush */
12466        spin_unlock(&phba->hbalock);
12467
12468        /*
12469         * Invokes slow-path host attention interrupt handling as appropriate.
12470         */
12471
12472        /* status of events with mailbox and link attention */
12473        status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12474
12475        /* status of events with ELS ring */
12476        status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
12477        status2 >>= (4*LPFC_ELS_RING);
12478
12479        if (status1 || (status2 & HA_RXMASK))
12480                sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
12481        else
12482                sp_irq_rc = IRQ_NONE;
12483
12484        /*
12485         * Invoke fast-path host attention interrupt handling as appropriate.
12486         */
12487
12488        /* status of events with FCP ring */
12489        status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12490        status1 >>= (4*LPFC_FCP_RING);
12491
12492        /* status of events with extra ring */
12493        if (phba->cfg_multi_ring_support == 2) {
12494                status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12495                status2 >>= (4*LPFC_EXTRA_RING);
12496        } else
12497                status2 = 0;
12498
12499        if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
12500                fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
12501        else
12502                fp_irq_rc = IRQ_NONE;
12503
12504        /* Return device-level interrupt handling status */
12505        return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
12506}  /* lpfc_sli_intr_handler */
12507
12508/**
12509 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12510 * @phba: pointer to lpfc hba data structure.
12511 *
12512 * This routine is invoked by the worker thread to process all the pending
12513 * SLI4 FCP abort XRI events.
12514 **/
12515void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12516{
12517        struct lpfc_cq_event *cq_event;
12518
12519        /* First, declare the fcp xri abort event has been handled */
12520        spin_lock_irq(&phba->hbalock);
12521        phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12522        spin_unlock_irq(&phba->hbalock);
12523        /* Now, handle all the fcp xri abort events */
12524        while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12525                /* Get the first event from the head of the event queue */
12526                spin_lock_irq(&phba->hbalock);
12527                list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12528                                 cq_event, struct lpfc_cq_event, list);
12529                spin_unlock_irq(&phba->hbalock);
12530                /* Notify aborted XRI for FCP work queue */
12531                lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12532                /* Free the event processed back to the free pool */
12533                lpfc_sli4_cq_event_release(phba, cq_event);
12534        }
12535}
12536
12537/**
12538 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12539 * @phba: pointer to lpfc hba data structure.
12540 *
12541 * This routine is invoked by the worker thread to process all the pending
12542 * SLI4 els abort xri events.
12543 **/
12544void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12545{
12546        struct lpfc_cq_event *cq_event;
12547
12548        /* First, declare the els xri abort event has been handled */
12549        spin_lock_irq(&phba->hbalock);
12550        phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12551        spin_unlock_irq(&phba->hbalock);
12552        /* Now, handle all the els xri abort events */
12553        while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12554                /* Get the first event from the head of the event queue */
12555                spin_lock_irq(&phba->hbalock);
12556                list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12557                                 cq_event, struct lpfc_cq_event, list);
12558                spin_unlock_irq(&phba->hbalock);
12559                /* Notify aborted XRI for ELS work queue */
12560                lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12561                /* Free the event processed back to the free pool */
12562                lpfc_sli4_cq_event_release(phba, cq_event);
12563        }
12564}
12565
12566/**
12567 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12568 * @phba: pointer to lpfc hba data structure
12569 * @pIocbIn: pointer to the rspiocbq
12570 * @pIocbOut: pointer to the cmdiocbq
12571 * @wcqe: pointer to the complete wcqe
12572 *
12573 * This routine transfers the fields of a command iocbq to a response iocbq
12574 * by copying all the IOCB fields from command iocbq and transferring the
12575 * completion status information from the complete wcqe.
12576 **/
12577static void
12578lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12579                              struct lpfc_iocbq *pIocbIn,
12580                              struct lpfc_iocbq *pIocbOut,
12581                              struct lpfc_wcqe_complete *wcqe)
12582{
12583        int numBdes, i;
12584        unsigned long iflags;
12585        uint32_t status, max_response;
12586        struct lpfc_dmabuf *dmabuf;
12587        struct ulp_bde64 *bpl, bde;
12588        size_t offset = offsetof(struct lpfc_iocbq, iocb);
12589
12590        memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12591               sizeof(struct lpfc_iocbq) - offset);
12592        /* Map WCQE parameters into irspiocb parameters */
12593        status = bf_get(lpfc_wcqe_c_status, wcqe);
12594        pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
12595        if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12596                if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12597                        pIocbIn->iocb.un.fcpi.fcpi_parm =
12598                                        pIocbOut->iocb.un.fcpi.fcpi_parm -
12599                                        wcqe->total_data_placed;
12600                else
12601                        pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12602        else {
12603                pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
12604                switch (pIocbOut->iocb.ulpCommand) {
12605                case CMD_ELS_REQUEST64_CR:
12606                        dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12607                        bpl  = (struct ulp_bde64 *)dmabuf->virt;
12608                        bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12609                        max_response = bde.tus.f.bdeSize;
12610                        break;
12611                case CMD_GEN_REQUEST64_CR:
12612                        max_response = 0;
12613                        if (!pIocbOut->context3)
12614                                break;
12615                        numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12616                                        sizeof(struct ulp_bde64);
12617                        dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12618                        bpl = (struct ulp_bde64 *)dmabuf->virt;
12619                        for (i = 0; i < numBdes; i++) {
12620                                bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12621                                if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12622                                        max_response += bde.tus.f.bdeSize;
12623                        }
12624                        break;
12625                default:
12626                        max_response = wcqe->total_data_placed;
12627                        break;
12628                }
12629                if (max_response < wcqe->total_data_placed)
12630                        pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12631                else
12632                        pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12633                                wcqe->total_data_placed;
12634        }
12635
12636        /* Convert BG errors for completion status */
12637        if (status == CQE_STATUS_DI_ERROR) {
12638                pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12639
12640                if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12641                        pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12642                else
12643                        pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12644
12645                pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12646                if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12647                        pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12648                                BGS_GUARD_ERR_MASK;
12649                if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12650                        pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12651                                BGS_APPTAG_ERR_MASK;
12652                if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12653                        pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12654                                BGS_REFTAG_ERR_MASK;
12655
12656                /* Check to see if there was any good data before the error */
12657                if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12658                        pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12659                                BGS_HI_WATER_MARK_PRESENT_MASK;
12660                        pIocbIn->iocb.unsli3.sli3_bg.bghm =
12661                                wcqe->total_data_placed;
12662                }
12663
12664                /*
12665                * Set ALL the error bits to indicate we don't know what
12666                * type of error it is.
12667                */
12668                if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12669                        pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12670                                (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12671                                BGS_GUARD_ERR_MASK);
12672        }
12673
12674        /* Pick up HBA exchange busy condition */
12675        if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12676                spin_lock_irqsave(&phba->hbalock, iflags);
12677                pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12678                spin_unlock_irqrestore(&phba->hbalock, iflags);
12679        }
12680}
12681
12682/**
12683 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12684 * @phba: Pointer to HBA context object.
12685 * @wcqe: Pointer to work-queue completion queue entry.
12686 *
12687 * This routine handles an ELS work-queue completion event and construct
12688 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12689 * discovery engine to handle.
12690 *
12691 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12692 **/
12693static struct lpfc_iocbq *
12694lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12695                               struct lpfc_iocbq *irspiocbq)
12696{
12697        struct lpfc_sli_ring *pring;
12698        struct lpfc_iocbq *cmdiocbq;
12699        struct lpfc_wcqe_complete *wcqe;
12700        unsigned long iflags;
12701
12702        pring = lpfc_phba_elsring(phba);
12703        if (unlikely(!pring))
12704                return NULL;
12705
12706        wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
12707        spin_lock_irqsave(&pring->ring_lock, iflags);
12708        pring->stats.iocb_event++;
12709        /* Look up the ELS command IOCB and create pseudo response IOCB */
12710        cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12711                                bf_get(lpfc_wcqe_c_request_tag, wcqe));
12712        if (unlikely(!cmdiocbq)) {
12713                spin_unlock_irqrestore(&pring->ring_lock, iflags);
12714                lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12715                                "0386 ELS complete with no corresponding "
12716                                "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12717                                wcqe->word0, wcqe->total_data_placed,
12718                                wcqe->parameter, wcqe->word3);
12719                lpfc_sli_release_iocbq(phba, irspiocbq);
12720                return NULL;
12721        }
12722
12723        /* Put the iocb back on the txcmplq */
12724        lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12725        spin_unlock_irqrestore(&pring->ring_lock, iflags);
12726
12727        /* Fake the irspiocbq and copy necessary response information */
12728        lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
12729
12730        return irspiocbq;
12731}
12732
12733inline struct lpfc_cq_event *
12734lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12735{
12736        struct lpfc_cq_event *cq_event;
12737
12738        /* Allocate a new internal CQ_EVENT entry */
12739        cq_event = lpfc_sli4_cq_event_alloc(phba);
12740        if (!cq_event) {
12741                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12742                                "0602 Failed to alloc CQ_EVENT entry\n");
12743                return NULL;
12744        }
12745
12746        /* Move the CQE into the event */
12747        memcpy(&cq_event->cqe, entry, size);
12748        return cq_event;
12749}
12750
12751/**
12752 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12753 * @phba: Pointer to HBA context object.
12754 * @cqe: Pointer to mailbox completion queue entry.
12755 *
12756 * This routine process a mailbox completion queue entry with asynchrous
12757 * event.
12758 *
12759 * Return: true if work posted to worker thread, otherwise false.
12760 **/
12761static bool
12762lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12763{
12764        struct lpfc_cq_event *cq_event;
12765        unsigned long iflags;
12766
12767        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12768                        "0392 Async Event: word0:x%x, word1:x%x, "
12769                        "word2:x%x, word3:x%x\n", mcqe->word0,
12770                        mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12771
12772        cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12773        if (!cq_event)
12774                return false;
12775        spin_lock_irqsave(&phba->hbalock, iflags);
12776        list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12777        /* Set the async event flag */
12778        phba->hba_flag |= ASYNC_EVENT;
12779        spin_unlock_irqrestore(&phba->hbalock, iflags);
12780
12781        return true;
12782}
12783
12784/**
12785 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12786 * @phba: Pointer to HBA context object.
12787 * @cqe: Pointer to mailbox completion queue entry.
12788 *
12789 * This routine process a mailbox completion queue entry with mailbox
12790 * completion event.
12791 *
12792 * Return: true if work posted to worker thread, otherwise false.
12793 **/
12794static bool
12795lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12796{
12797        uint32_t mcqe_status;
12798        MAILBOX_t *mbox, *pmbox;
12799        struct lpfc_mqe *mqe;
12800        struct lpfc_vport *vport;
12801        struct lpfc_nodelist *ndlp;
12802        struct lpfc_dmabuf *mp;
12803        unsigned long iflags;
12804        LPFC_MBOXQ_t *pmb;
12805        bool workposted = false;
12806        int rc;
12807
12808        /* If not a mailbox complete MCQE, out by checking mailbox consume */
12809        if (!bf_get(lpfc_trailer_completed, mcqe))
12810                goto out_no_mqe_complete;
12811
12812        /* Get the reference to the active mbox command */
12813        spin_lock_irqsave(&phba->hbalock, iflags);
12814        pmb = phba->sli.mbox_active;
12815        if (unlikely(!pmb)) {
12816                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12817                                "1832 No pending MBOX command to handle\n");
12818                spin_unlock_irqrestore(&phba->hbalock, iflags);
12819                goto out_no_mqe_complete;
12820        }
12821        spin_unlock_irqrestore(&phba->hbalock, iflags);
12822        mqe = &pmb->u.mqe;
12823        pmbox = (MAILBOX_t *)&pmb->u.mqe;
12824        mbox = phba->mbox;
12825        vport = pmb->vport;
12826
12827        /* Reset heartbeat timer */
12828        phba->last_completion_time = jiffies;
12829        del_timer(&phba->sli.mbox_tmo);
12830
12831        /* Move mbox data to caller's mailbox region, do endian swapping */
12832        if (pmb->mbox_cmpl && mbox)
12833                lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
12834
12835        /*
12836         * For mcqe errors, conditionally move a modified error code to
12837         * the mbox so that the error will not be missed.
12838         */
12839        mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12840        if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12841                if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12842                        bf_set(lpfc_mqe_status, mqe,
12843                               (LPFC_MBX_ERROR_RANGE | mcqe_status));
12844        }
12845        if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12846                pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12847                lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12848                                      "MBOX dflt rpi: status:x%x rpi:x%x",
12849                                      mcqe_status,
12850                                      pmbox->un.varWords[0], 0);
12851                if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12852                        mp = (struct lpfc_dmabuf *)(pmb->context1);
12853                        ndlp = (struct lpfc_nodelist *)pmb->context2;
12854                        /* Reg_LOGIN of dflt RPI was successful. Now lets get
12855                         * RID of the PPI using the same mbox buffer.
12856                         */
12857                        lpfc_unreg_login(phba, vport->vpi,
12858                                         pmbox->un.varWords[0], pmb);
12859                        pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12860                        pmb->context1 = mp;
12861                        pmb->context2 = ndlp;
12862                        pmb->vport = vport;
12863                        rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12864                        if (rc != MBX_BUSY)
12865                                lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12866                                                LOG_SLI, "0385 rc should "
12867                                                "have been MBX_BUSY\n");
12868                        if (rc != MBX_NOT_FINISHED)
12869                                goto send_current_mbox;
12870                }
12871        }
12872        spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12873        phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12874        spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12875
12876        /* There is mailbox completion work to do */
12877        spin_lock_irqsave(&phba->hbalock, iflags);
12878        __lpfc_mbox_cmpl_put(phba, pmb);
12879        phba->work_ha |= HA_MBATT;
12880        spin_unlock_irqrestore(&phba->hbalock, iflags);
12881        workposted = true;
12882
12883send_current_mbox:
12884        spin_lock_irqsave(&phba->hbalock, iflags);
12885        /* Release the mailbox command posting token */
12886        phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12887        /* Setting active mailbox pointer need to be in sync to flag clear */
12888        phba->sli.mbox_active = NULL;
12889        spin_unlock_irqrestore(&phba->hbalock, iflags);
12890        /* Wake up worker thread to post the next pending mailbox command */
12891        lpfc_worker_wake_up(phba);
12892out_no_mqe_complete:
12893        if (bf_get(lpfc_trailer_consumed, mcqe))
12894                lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12895        return workposted;
12896}
12897
12898/**
12899 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12900 * @phba: Pointer to HBA context object.
12901 * @cqe: Pointer to mailbox completion queue entry.
12902 *
12903 * This routine process a mailbox completion queue entry, it invokes the
12904 * proper mailbox complete handling or asynchrous event handling routine
12905 * according to the MCQE's async bit.
12906 *
12907 * Return: true if work posted to worker thread, otherwise false.
12908 **/
12909static bool
12910lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12911{
12912        struct lpfc_mcqe mcqe;
12913        bool workposted;
12914
12915        /* Copy the mailbox MCQE and convert endian order as needed */
12916        lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12917
12918        /* Invoke the proper event handling routine */
12919        if (!bf_get(lpfc_trailer_async, &mcqe))
12920                workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12921        else
12922                workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12923        return workposted;
12924}
12925
12926/**
12927 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12928 * @phba: Pointer to HBA context object.
12929 * @cq: Pointer to associated CQ
12930 * @wcqe: Pointer to work-queue completion queue entry.
12931 *
12932 * This routine handles an ELS work-queue completion event.
12933 *
12934 * Return: true if work posted to worker thread, otherwise false.
12935 **/
12936static bool
12937lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12938                             struct lpfc_wcqe_complete *wcqe)
12939{
12940        struct lpfc_iocbq *irspiocbq;
12941        unsigned long iflags;
12942        struct lpfc_sli_ring *pring = cq->pring;
12943        int txq_cnt = 0;
12944        int txcmplq_cnt = 0;
12945        int fcp_txcmplq_cnt = 0;
12946
12947        /* Get an irspiocbq for later ELS response processing use */
12948        irspiocbq = lpfc_sli_get_iocbq(phba);
12949        if (!irspiocbq) {
12950                if (!list_empty(&pring->txq))
12951                        txq_cnt++;
12952                if (!list_empty(&pring->txcmplq))
12953                        txcmplq_cnt++;
12954                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12955                        "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12956                        "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
12957                        txq_cnt, phba->iocb_cnt,
12958                        fcp_txcmplq_cnt,
12959                        txcmplq_cnt);
12960                return false;
12961        }
12962
12963        /* Save off the slow-path queue event for work thread to process */
12964        memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
12965        spin_lock_irqsave(&phba->hbalock, iflags);
12966        list_add_tail(&irspiocbq->cq_event.list,
12967                      &phba->sli4_hba.sp_queue_event);
12968        phba->hba_flag |= HBA_SP_QUEUE_EVT;
12969        spin_unlock_irqrestore(&phba->hbalock, iflags);
12970
12971        return true;
12972}
12973
12974/**
12975 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12976 * @phba: Pointer to HBA context object.
12977 * @wcqe: Pointer to work-queue completion queue entry.
12978 *
12979 * This routine handles slow-path WQ entry consumed event by invoking the
12980 * proper WQ release routine to the slow-path WQ.
12981 **/
12982static void
12983lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12984                             struct lpfc_wcqe_release *wcqe)
12985{
12986        /* sanity check on queue memory */
12987        if (unlikely(!phba->sli4_hba.els_wq))
12988                return;
12989        /* Check for the slow-path ELS work queue */
12990        if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12991                lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12992                                     bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12993        else
12994                lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12995                                "2579 Slow-path wqe consume event carries "
12996                                "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12997                                bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12998                                phba->sli4_hba.els_wq->queue_id);
12999}
13000
13001/**
13002 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13003 * @phba: Pointer to HBA context object.
13004 * @cq: Pointer to a WQ completion queue.
13005 * @wcqe: Pointer to work-queue completion queue entry.
13006 *
13007 * This routine handles an XRI abort event.
13008 *
13009 * Return: true if work posted to worker thread, otherwise false.
13010 **/
13011static bool
13012lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13013                                   struct lpfc_queue *cq,
13014                                   struct sli4_wcqe_xri_aborted *wcqe)
13015{
13016        bool workposted = false;
13017        struct lpfc_cq_event *cq_event;
13018        unsigned long iflags;
13019
13020        switch (cq->subtype) {
13021        case LPFC_FCP:
13022                cq_event = lpfc_cq_event_setup(
13023                        phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13024                if (!cq_event)
13025                        return false;
13026                spin_lock_irqsave(&phba->hbalock, iflags);
13027                list_add_tail(&cq_event->list,
13028                              &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
13029                /* Set the fcp xri abort event flag */
13030                phba->hba_flag |= FCP_XRI_ABORT_EVENT;
13031                spin_unlock_irqrestore(&phba->hbalock, iflags);
13032                workposted = true;
13033                break;
13034        case LPFC_NVME_LS: /* NVME LS uses ELS resources */
13035        case LPFC_ELS:
13036                cq_event = lpfc_cq_event_setup(
13037                        phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13038                if (!cq_event)
13039                        return false;
13040                spin_lock_irqsave(&phba->hbalock, iflags);
13041                list_add_tail(&cq_event->list,
13042                              &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13043                /* Set the els xri abort event flag */
13044                phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13045                spin_unlock_irqrestore(&phba->hbalock, iflags);
13046                workposted = true;
13047                break;
13048        case LPFC_NVME:
13049                /* Notify aborted XRI for NVME work queue */
13050                if (phba->nvmet_support)
13051                        lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13052                else
13053                        lpfc_sli4_nvme_xri_aborted(phba, wcqe);
13054
13055                workposted = false;
13056                break;
13057        default:
13058                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13059                                "0603 Invalid CQ subtype %d: "
13060                                "%08x %08x %08x %08x\n",
13061                                cq->subtype, wcqe->word0, wcqe->parameter,
13062                                wcqe->word2, wcqe->word3);
13063                workposted = false;
13064                break;
13065        }
13066        return workposted;
13067}
13068
13069/**
13070 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
13071 * @phba: Pointer to HBA context object.
13072 * @rcqe: Pointer to receive-queue completion queue entry.
13073 *
13074 * This routine process a receive-queue completion queue entry.
13075 *
13076 * Return: true if work posted to worker thread, otherwise false.
13077 **/
13078static bool
13079lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13080{
13081        bool workposted = false;
13082        struct fc_frame_header *fc_hdr;
13083        struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13084        struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
13085        struct lpfc_nvmet_tgtport *tgtp;
13086        struct hbq_dmabuf *dma_buf;
13087        uint32_t status, rq_id;
13088        unsigned long iflags;
13089
13090        /* sanity check on queue memory */
13091        if (unlikely(!hrq) || unlikely(!drq))
13092                return workposted;
13093
13094        if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13095                rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13096        else
13097                rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13098        if (rq_id != hrq->queue_id)
13099                goto out;
13100
13101        status = bf_get(lpfc_rcqe_status, rcqe);
13102        switch (status) {
13103        case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13104                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13105                                "2537 Receive Frame Truncated!!\n");
13106        case FC_STATUS_RQ_SUCCESS:
13107                spin_lock_irqsave(&phba->hbalock, iflags);
13108                lpfc_sli4_rq_release(hrq, drq);
13109                dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13110                if (!dma_buf) {
13111                        hrq->RQ_no_buf_found++;
13112                        spin_unlock_irqrestore(&phba->hbalock, iflags);
13113                        goto out;
13114                }
13115                hrq->RQ_rcv_buf++;
13116                hrq->RQ_buf_posted--;
13117                memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
13118
13119                /* If a NVME LS event (type 0x28), treat it as Fast path */
13120                fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13121
13122                /* save off the frame for the word thread to process */
13123                list_add_tail(&dma_buf->cq_event.list,
13124                              &phba->sli4_hba.sp_queue_event);
13125                /* Frame received */
13126                phba->hba_flag |= HBA_SP_QUEUE_EVT;
13127                spin_unlock_irqrestore(&phba->hbalock, iflags);
13128                workposted = true;
13129                break;
13130        case FC_STATUS_INSUFF_BUF_FRM_DISC:
13131                if (phba->nvmet_support) {
13132                        tgtp = phba->targetport->private;
13133                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13134                                        "6402 RQE Error x%x, posted %d err_cnt "
13135                                        "%d: %x %x %x\n",
13136                                        status, hrq->RQ_buf_posted,
13137                                        hrq->RQ_no_posted_buf,
13138                                        atomic_read(&tgtp->rcv_fcp_cmd_in),
13139                                        atomic_read(&tgtp->rcv_fcp_cmd_out),
13140                                        atomic_read(&tgtp->xmt_fcp_release));
13141                }
13142                /* fallthrough */
13143
13144        case FC_STATUS_INSUFF_BUF_NEED_BUF:
13145                hrq->RQ_no_posted_buf++;
13146                /* Post more buffers if possible */
13147                spin_lock_irqsave(&phba->hbalock, iflags);
13148                phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13149                spin_unlock_irqrestore(&phba->hbalock, iflags);
13150                workposted = true;
13151                break;
13152        }
13153out:
13154        return workposted;
13155}
13156
13157/**
13158 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13159 * @phba: Pointer to HBA context object.
13160 * @cq: Pointer to the completion queue.
13161 * @wcqe: Pointer to a completion queue entry.
13162 *
13163 * This routine process a slow-path work-queue or receive queue completion queue
13164 * entry.
13165 *
13166 * Return: true if work posted to worker thread, otherwise false.
13167 **/
13168static bool
13169lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13170                         struct lpfc_cqe *cqe)
13171{
13172        struct lpfc_cqe cqevt;
13173        bool workposted = false;
13174
13175        /* Copy the work queue CQE and convert endian order if needed */
13176        lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
13177
13178        /* Check and process for different type of WCQE and dispatch */
13179        switch (bf_get(lpfc_cqe_code, &cqevt)) {
13180        case CQE_CODE_COMPL_WQE:
13181                /* Process the WQ/RQ complete event */
13182                phba->last_completion_time = jiffies;
13183                workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
13184                                (struct lpfc_wcqe_complete *)&cqevt);
13185                break;
13186        case CQE_CODE_RELEASE_WQE:
13187                /* Process the WQ release event */
13188                lpfc_sli4_sp_handle_rel_wcqe(phba,
13189                                (struct lpfc_wcqe_release *)&cqevt);
13190                break;
13191        case CQE_CODE_XRI_ABORTED:
13192                /* Process the WQ XRI abort event */
13193                phba->last_completion_time = jiffies;
13194                workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13195                                (struct sli4_wcqe_xri_aborted *)&cqevt);
13196                break;
13197        case CQE_CODE_RECEIVE:
13198        case CQE_CODE_RECEIVE_V1:
13199                /* Process the RQ event */
13200                phba->last_completion_time = jiffies;
13201                workposted = lpfc_sli4_sp_handle_rcqe(phba,
13202                                (struct lpfc_rcqe *)&cqevt);
13203                break;
13204        default:
13205                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13206                                "0388 Not a valid WCQE code: x%x\n",
13207                                bf_get(lpfc_cqe_code, &cqevt));
13208                break;
13209        }
13210        return workposted;
13211}
13212
13213/**
13214 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13215 * @phba: Pointer to HBA context object.
13216 * @eqe: Pointer to fast-path event queue entry.
13217 *
13218 * This routine process a event queue entry from the slow-path event queue.
13219 * It will check the MajorCode and MinorCode to determine this is for a
13220 * completion event on a completion queue, if not, an error shall be logged
13221 * and just return. Otherwise, it will get to the corresponding completion
13222 * queue and process all the entries on that completion queue, rearm the
13223 * completion queue, and then return.
13224 *
13225 **/
13226static void
13227lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13228        struct lpfc_queue *speq)
13229{
13230        struct lpfc_queue *cq = NULL, *childq;
13231        uint16_t cqid;
13232
13233        /* Get the reference to the corresponding CQ */
13234        cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13235
13236        list_for_each_entry(childq, &speq->child_list, list) {
13237                if (childq->queue_id == cqid) {
13238                        cq = childq;
13239                        break;
13240                }
13241        }
13242        if (unlikely(!cq)) {
13243                if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13244                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13245                                        "0365 Slow-path CQ identifier "
13246                                        "(%d) does not exist\n", cqid);
13247                return;
13248        }
13249
13250        /* Save EQ associated with this CQ */
13251        cq->assoc_qp = speq;
13252
13253        if (!queue_work(phba->wq, &cq->spwork))
13254                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13255                                "0390 Cannot schedule soft IRQ "
13256                                "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13257                                cqid, cq->queue_id, smp_processor_id());
13258}
13259
13260/**
13261 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13262 * @phba: Pointer to HBA context object.
13263 *
13264 * This routine process a event queue entry from the slow-path event queue.
13265 * It will check the MajorCode and MinorCode to determine this is for a
13266 * completion event on a completion queue, if not, an error shall be logged
13267 * and just return. Otherwise, it will get to the corresponding completion
13268 * queue and process all the entries on that completion queue, rearm the
13269 * completion queue, and then return.
13270 *
13271 **/
13272static void
13273lpfc_sli4_sp_process_cq(struct work_struct *work)
13274{
13275        struct lpfc_queue *cq =
13276                container_of(work, struct lpfc_queue, spwork);
13277        struct lpfc_hba *phba = cq->phba;
13278        struct lpfc_cqe *cqe;
13279        bool workposted = false;
13280        int ccount = 0;
13281
13282        /* Process all the entries to the CQ */
13283        switch (cq->type) {
13284        case LPFC_MCQ:
13285                while ((cqe = lpfc_sli4_cq_get(cq))) {
13286                        workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
13287                        if (!(++ccount % cq->entry_repost))
13288                                break;
13289                        cq->CQ_mbox++;
13290                }
13291                break;
13292        case LPFC_WCQ:
13293                while ((cqe = lpfc_sli4_cq_get(cq))) {
13294                        if (cq->subtype == LPFC_FCP ||
13295                            cq->subtype == LPFC_NVME) {
13296#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13297                                if (phba->ktime_on)
13298                                        cq->isr_timestamp = ktime_get_ns();
13299                                else
13300                                        cq->isr_timestamp = 0;
13301#endif
13302                                workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
13303                                                                       cqe);
13304                        } else {
13305                                workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13306                                                                      cqe);
13307                        }
13308                        if (!(++ccount % cq->entry_repost))
13309                                break;
13310                }
13311
13312                /* Track the max number of CQEs processed in 1 EQ */
13313                if (ccount > cq->CQ_max_cqe)
13314                        cq->CQ_max_cqe = ccount;
13315                break;
13316        default:
13317                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13318                                "0370 Invalid completion queue type (%d)\n",
13319                                cq->type);
13320                return;
13321        }
13322
13323        /* Catch the no cq entry condition, log an error */
13324        if (unlikely(ccount == 0))
13325                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13326                                "0371 No entry from the CQ: identifier "
13327                                "(x%x), type (%d)\n", cq->queue_id, cq->type);
13328
13329        /* In any case, flash and re-arm the RCQ */
13330        phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
13331
13332        /* wake up worker thread if there are works to be done */
13333        if (workposted)
13334                lpfc_worker_wake_up(phba);
13335}
13336
13337/**
13338 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
13339 * @phba: Pointer to HBA context object.
13340 * @cq: Pointer to associated CQ
13341 * @wcqe: Pointer to work-queue completion queue entry.
13342 *
13343 * This routine process a fast-path work queue completion entry from fast-path
13344 * event queue for FCP command response completion.
13345 **/
13346static void
13347lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13348                             struct lpfc_wcqe_complete *wcqe)
13349{
13350        struct lpfc_sli_ring *pring = cq->pring;
13351        struct lpfc_iocbq *cmdiocbq;
13352        struct lpfc_iocbq irspiocbq;
13353        unsigned long iflags;
13354
13355        /* Check for response status */
13356        if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13357                /* If resource errors reported from HBA, reduce queue
13358                 * depth of the SCSI device.
13359                 */
13360                if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13361                     IOSTAT_LOCAL_REJECT)) &&
13362                    ((wcqe->parameter & IOERR_PARAM_MASK) ==
13363                     IOERR_NO_RESOURCES))
13364                        phba->lpfc_rampdown_queue_depth(phba);
13365
13366                /* Log the error status */
13367                lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13368                                "0373 FCP complete error: status=x%x, "
13369                                "hw_status=x%x, total_data_specified=%d, "
13370                                "parameter=x%x, word3=x%x\n",
13371                                bf_get(lpfc_wcqe_c_status, wcqe),
13372                                bf_get(lpfc_wcqe_c_hw_status, wcqe),
13373                                wcqe->total_data_placed, wcqe->parameter,
13374                                wcqe->word3);
13375        }
13376
13377        /* Look up the FCP command IOCB and create pseudo response IOCB */
13378        spin_lock_irqsave(&pring->ring_lock, iflags);
13379        pring->stats.iocb_event++;
13380        cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13381                                bf_get(lpfc_wcqe_c_request_tag, wcqe));
13382        spin_unlock_irqrestore(&pring->ring_lock, iflags);
13383        if (unlikely(!cmdiocbq)) {
13384                lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13385                                "0374 FCP complete with no corresponding "
13386                                "cmdiocb: iotag (%d)\n",
13387                                bf_get(lpfc_wcqe_c_request_tag, wcqe));
13388                return;
13389        }
13390#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13391        cmdiocbq->isr_timestamp = cq->isr_timestamp;
13392#endif
13393        if (cmdiocbq->iocb_cmpl == NULL) {
13394                if (cmdiocbq->wqe_cmpl) {
13395                        if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13396                                spin_lock_irqsave(&phba->hbalock, iflags);
13397                                cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13398                                spin_unlock_irqrestore(&phba->hbalock, iflags);
13399                        }
13400
13401                        /* Pass the cmd_iocb and the wcqe to the upper layer */
13402                        (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13403                        return;
13404                }
13405                lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13406                                "0375 FCP cmdiocb not callback function "
13407                                "iotag: (%d)\n",
13408                                bf_get(lpfc_wcqe_c_request_tag, wcqe));
13409                return;
13410        }
13411
13412        /* Fake the irspiocb and copy necessary response information */
13413        lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
13414
13415        if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13416                spin_lock_irqsave(&phba->hbalock, iflags);
13417                cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13418                spin_unlock_irqrestore(&phba->hbalock, iflags);
13419        }
13420
13421        /* Pass the cmd_iocb and the rsp state to the upper layer */
13422        (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13423}
13424
13425/**
13426 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13427 * @phba: Pointer to HBA context object.
13428 * @cq: Pointer to completion queue.
13429 * @wcqe: Pointer to work-queue completion queue entry.
13430 *
13431 * This routine handles an fast-path WQ entry consumed event by invoking the
13432 * proper WQ release routine to the slow-path WQ.
13433 **/
13434static void
13435lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13436                             struct lpfc_wcqe_release *wcqe)
13437{
13438        struct lpfc_queue *childwq;
13439        bool wqid_matched = false;
13440        uint16_t hba_wqid;
13441
13442        /* Check for fast-path FCP work queue release */
13443        hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
13444        list_for_each_entry(childwq, &cq->child_list, list) {
13445                if (childwq->queue_id == hba_wqid) {
13446                        lpfc_sli4_wq_release(childwq,
13447                                        bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13448                        if (childwq->q_flag & HBA_NVMET_WQFULL)
13449                                lpfc_nvmet_wqfull_process(phba, childwq);
13450                        wqid_matched = true;
13451                        break;
13452                }
13453        }
13454        /* Report warning log message if no match found */
13455        if (wqid_matched != true)
13456                lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13457                                "2580 Fast-path wqe consume event carries "
13458                                "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
13459}
13460
13461/**
13462 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13463 * @phba: Pointer to HBA context object.
13464 * @rcqe: Pointer to receive-queue completion queue entry.
13465 *
13466 * This routine process a receive-queue completion queue entry.
13467 *
13468 * Return: true if work posted to worker thread, otherwise false.
13469 **/
13470static bool
13471lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13472                            struct lpfc_rcqe *rcqe)
13473{
13474        bool workposted = false;
13475        struct lpfc_queue *hrq;
13476        struct lpfc_queue *drq;
13477        struct rqb_dmabuf *dma_buf;
13478        struct fc_frame_header *fc_hdr;
13479        struct lpfc_nvmet_tgtport *tgtp;
13480        uint32_t status, rq_id;
13481        unsigned long iflags;
13482        uint32_t fctl, idx;
13483
13484        if ((phba->nvmet_support == 0) ||
13485            (phba->sli4_hba.nvmet_cqset == NULL))
13486                return workposted;
13487
13488        idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13489        hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13490        drq = phba->sli4_hba.nvmet_mrq_data[idx];
13491
13492        /* sanity check on queue memory */
13493        if (unlikely(!hrq) || unlikely(!drq))
13494                return workposted;
13495
13496        if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13497                rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13498        else
13499                rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13500
13501        if ((phba->nvmet_support == 0) ||
13502            (rq_id != hrq->queue_id))
13503                return workposted;
13504
13505        status = bf_get(lpfc_rcqe_status, rcqe);
13506        switch (status) {
13507        case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13508                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13509                                "6126 Receive Frame Truncated!!\n");
13510                /* Drop thru */
13511        case FC_STATUS_RQ_SUCCESS:
13512                spin_lock_irqsave(&phba->hbalock, iflags);
13513                lpfc_sli4_rq_release(hrq, drq);
13514                dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13515                if (!dma_buf) {
13516                        hrq->RQ_no_buf_found++;
13517                        spin_unlock_irqrestore(&phba->hbalock, iflags);
13518                        goto out;
13519                }
13520                spin_unlock_irqrestore(&phba->hbalock, iflags);
13521                hrq->RQ_rcv_buf++;
13522                hrq->RQ_buf_posted--;
13523                fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13524
13525                /* Just some basic sanity checks on FCP Command frame */
13526                fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13527                fc_hdr->fh_f_ctl[1] << 8 |
13528                fc_hdr->fh_f_ctl[2]);
13529                if (((fctl &
13530                    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13531                    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13532                    (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13533                        goto drop;
13534
13535                if (fc_hdr->fh_type == FC_TYPE_FCP) {
13536                        dma_buf->bytes_recv = bf_get(lpfc_rcqe_length,  rcqe);
13537                        lpfc_nvmet_unsol_fcp_event(
13538                                phba, idx, dma_buf,
13539                                cq->isr_timestamp);
13540                        return false;
13541                }
13542drop:
13543                lpfc_in_buf_free(phba, &dma_buf->dbuf);
13544                break;
13545        case FC_STATUS_INSUFF_BUF_FRM_DISC:
13546                if (phba->nvmet_support) {
13547                        tgtp = phba->targetport->private;
13548                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13549                                        "6401 RQE Error x%x, posted %d err_cnt "
13550                                        "%d: %x %x %x\n",
13551                                        status, hrq->RQ_buf_posted,
13552                                        hrq->RQ_no_posted_buf,
13553                                        atomic_read(&tgtp->rcv_fcp_cmd_in),
13554                                        atomic_read(&tgtp->rcv_fcp_cmd_out),
13555                                        atomic_read(&tgtp->xmt_fcp_release));
13556                }
13557                /* fallthrough */
13558
13559        case FC_STATUS_INSUFF_BUF_NEED_BUF:
13560                hrq->RQ_no_posted_buf++;
13561                /* Post more buffers if possible */
13562                break;
13563        }
13564out:
13565        return workposted;
13566}
13567
13568/**
13569 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
13570 * @cq: Pointer to the completion queue.
13571 * @eqe: Pointer to fast-path completion queue entry.
13572 *
13573 * This routine process a fast-path work queue completion entry from fast-path
13574 * event queue for FCP command response completion.
13575 **/
13576static int
13577lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13578                         struct lpfc_cqe *cqe)
13579{
13580        struct lpfc_wcqe_release wcqe;
13581        bool workposted = false;
13582
13583        /* Copy the work queue CQE and convert endian order if needed */
13584        lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13585
13586        /* Check and process for different type of WCQE and dispatch */
13587        switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13588        case CQE_CODE_COMPL_WQE:
13589        case CQE_CODE_NVME_ERSP:
13590                cq->CQ_wq++;
13591                /* Process the WQ complete event */
13592                phba->last_completion_time = jiffies;
13593                if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13594                        lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13595                                (struct lpfc_wcqe_complete *)&wcqe);
13596                if (cq->subtype == LPFC_NVME_LS)
13597                        lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13598                                (struct lpfc_wcqe_complete *)&wcqe);
13599                break;
13600        case CQE_CODE_RELEASE_WQE:
13601                cq->CQ_release_wqe++;
13602                /* Process the WQ release event */
13603                lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13604                                (struct lpfc_wcqe_release *)&wcqe);
13605                break;
13606        case CQE_CODE_XRI_ABORTED:
13607                cq->CQ_xri_aborted++;
13608                /* Process the WQ XRI abort event */
13609                phba->last_completion_time = jiffies;
13610                workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13611                                (struct sli4_wcqe_xri_aborted *)&wcqe);
13612                break;
13613        case CQE_CODE_RECEIVE_V1:
13614        case CQE_CODE_RECEIVE:
13615                phba->last_completion_time = jiffies;
13616                if (cq->subtype == LPFC_NVMET) {
13617                        workposted = lpfc_sli4_nvmet_handle_rcqe(
13618                                phba, cq, (struct lpfc_rcqe *)&wcqe);
13619                }
13620                break;
13621        default:
13622                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13623                                "0144 Not a valid CQE code: x%x\n",
13624                                bf_get(lpfc_wcqe_c_code, &wcqe));
13625                break;
13626        }
13627        return workposted;
13628}
13629
13630/**
13631 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
13632 * @phba: Pointer to HBA context object.
13633 * @eqe: Pointer to fast-path event queue entry.
13634 *
13635 * This routine process a event queue entry from the fast-path event queue.
13636 * It will check the MajorCode and MinorCode to determine this is for a
13637 * completion event on a completion queue, if not, an error shall be logged
13638 * and just return. Otherwise, it will get to the corresponding completion
13639 * queue and process all the entries on the completion queue, rearm the
13640 * completion queue, and then return.
13641 **/
13642static void
13643lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13644                        uint32_t qidx)
13645{
13646        struct lpfc_queue *cq = NULL;
13647        uint16_t cqid, id;
13648
13649        if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13650                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13651                                "0366 Not a valid completion "
13652                                "event: majorcode=x%x, minorcode=x%x\n",
13653                                bf_get_le32(lpfc_eqe_major_code, eqe),
13654                                bf_get_le32(lpfc_eqe_minor_code, eqe));
13655                return;
13656        }
13657
13658        /* Get the reference to the corresponding CQ */
13659        cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13660
13661        if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13662                id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13663                if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13664                        /* Process NVMET unsol rcv */
13665                        cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13666                        goto  process_cq;
13667                }
13668        }
13669
13670        if (phba->sli4_hba.nvme_cq_map &&
13671            (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
13672                /* Process NVME / NVMET command completion */
13673                cq = phba->sli4_hba.nvme_cq[qidx];
13674                goto  process_cq;
13675        }
13676
13677        if (phba->sli4_hba.fcp_cq_map &&
13678            (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13679                /* Process FCP command completion */
13680                cq = phba->sli4_hba.fcp_cq[qidx];
13681                goto  process_cq;
13682        }
13683
13684        if (phba->sli4_hba.nvmels_cq &&
13685            (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13686                /* Process NVME unsol rcv */
13687                cq = phba->sli4_hba.nvmels_cq;
13688        }
13689
13690        /* Otherwise this is a Slow path event */
13691        if (cq == NULL) {
13692                lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13693                return;
13694        }
13695
13696process_cq:
13697        if (unlikely(cqid != cq->queue_id)) {
13698                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13699                                "0368 Miss-matched fast-path completion "
13700                                "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13701                                cqid, cq->queue_id);
13702                return;
13703        }
13704
13705        /* Save EQ associated with this CQ */
13706        cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13707
13708        if (!queue_work(phba->wq, &cq->irqwork))
13709                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13710                                "0363 Cannot schedule soft IRQ "
13711                                "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13712                                cqid, cq->queue_id, smp_processor_id());
13713}
13714
13715/**
13716 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13717 * @phba: Pointer to HBA context object.
13718 * @eqe: Pointer to fast-path event queue entry.
13719 *
13720 * This routine process a event queue entry from the fast-path event queue.
13721 * It will check the MajorCode and MinorCode to determine this is for a
13722 * completion event on a completion queue, if not, an error shall be logged
13723 * and just return. Otherwise, it will get to the corresponding completion
13724 * queue and process all the entries on the completion queue, rearm the
13725 * completion queue, and then return.
13726 **/
13727static void
13728lpfc_sli4_hba_process_cq(struct work_struct *work)
13729{
13730        struct lpfc_queue *cq =
13731                container_of(work, struct lpfc_queue, irqwork);
13732        struct lpfc_hba *phba = cq->phba;
13733        struct lpfc_cqe *cqe;
13734        bool workposted = false;
13735        int ccount = 0;
13736
13737        /* Process all the entries to the CQ */
13738        while ((cqe = lpfc_sli4_cq_get(cq))) {
13739#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13740                if (phba->ktime_on)
13741                        cq->isr_timestamp = ktime_get_ns();
13742                else
13743                        cq->isr_timestamp = 0;
13744#endif
13745                workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
13746                if (!(++ccount % cq->entry_repost))
13747                        break;
13748        }
13749
13750        /* Track the max number of CQEs processed in 1 EQ */
13751        if (ccount > cq->CQ_max_cqe)
13752                cq->CQ_max_cqe = ccount;
13753        cq->assoc_qp->EQ_cqe_cnt += ccount;
13754
13755        /* Catch the no cq entry condition */
13756        if (unlikely(ccount == 0))
13757                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13758                                "0369 No entry from fast-path completion "
13759                                "queue fcpcqid=%d\n", cq->queue_id);
13760
13761        /* In any case, flash and re-arm the CQ */
13762        phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
13763
13764        /* wake up worker thread if there are works to be done */
13765        if (workposted)
13766                lpfc_worker_wake_up(phba);
13767}
13768
13769static void
13770lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13771{
13772        struct lpfc_eqe *eqe;
13773
13774        /* walk all the EQ entries and drop on the floor */
13775        while ((eqe = lpfc_sli4_eq_get(eq)))
13776                ;
13777
13778        /* Clear and re-arm the EQ */
13779        phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
13780}
13781
13782
13783/**
13784 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13785 *                           entry
13786 * @phba: Pointer to HBA context object.
13787 * @eqe: Pointer to fast-path event queue entry.
13788 *
13789 * This routine process a event queue entry from the Flash Optimized Fabric
13790 * event queue.  It will check the MajorCode and MinorCode to determine this
13791 * is for a completion event on a completion queue, if not, an error shall be
13792 * logged and just return. Otherwise, it will get to the corresponding
13793 * completion queue and process all the entries on the completion queue, rearm
13794 * the completion queue, and then return.
13795 **/
13796static void
13797lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13798{
13799        struct lpfc_queue *cq;
13800        uint16_t cqid;
13801
13802        if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13803                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13804                                "9147 Not a valid completion "
13805                                "event: majorcode=x%x, minorcode=x%x\n",
13806                                bf_get_le32(lpfc_eqe_major_code, eqe),
13807                                bf_get_le32(lpfc_eqe_minor_code, eqe));
13808                return;
13809        }
13810
13811        /* Get the reference to the corresponding CQ */
13812        cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13813
13814        /* Next check for OAS */
13815        cq = phba->sli4_hba.oas_cq;
13816        if (unlikely(!cq)) {
13817                if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13818                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13819                                        "9148 OAS completion queue "
13820                                        "does not exist\n");
13821                return;
13822        }
13823
13824        if (unlikely(cqid != cq->queue_id)) {
13825                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13826                                "9149 Miss-matched fast-path compl "
13827                                "queue id: eqcqid=%d, fcpcqid=%d\n",
13828                                cqid, cq->queue_id);
13829                return;
13830        }
13831
13832        /* Save EQ associated with this CQ */
13833        cq->assoc_qp = phba->sli4_hba.fof_eq;
13834
13835        /* CQ work will be processed on CPU affinitized to this IRQ */
13836        if (!queue_work(phba->wq, &cq->irqwork))
13837                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13838                                "0367 Cannot schedule soft IRQ "
13839                                "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13840                                cqid, cq->queue_id, smp_processor_id());
13841}
13842
13843/**
13844 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13845 * @irq: Interrupt number.
13846 * @dev_id: The device context pointer.
13847 *
13848 * This function is directly called from the PCI layer as an interrupt
13849 * service routine when device with SLI-4 interface spec is enabled with
13850 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13851 * IOCB ring event in the HBA. However, when the device is enabled with either
13852 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13853 * device-level interrupt handler. When the PCI slot is in error recovery
13854 * or the HBA is undergoing initialization, the interrupt handler will not
13855 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13856 * the intrrupt context. This function is called without any lock held.
13857 * It gets the hbalock to access and update SLI data structures. Note that,
13858 * the EQ to CQ are one-to-one map such that the EQ index is
13859 * equal to that of CQ index.
13860 *
13861 * This function returns IRQ_HANDLED when interrupt is handled else it
13862 * returns IRQ_NONE.
13863 **/
13864irqreturn_t
13865lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13866{
13867        struct lpfc_hba *phba;
13868        struct lpfc_hba_eq_hdl *hba_eq_hdl;
13869        struct lpfc_queue *eq;
13870        struct lpfc_eqe *eqe;
13871        unsigned long iflag;
13872        int ecount = 0;
13873
13874        /* Get the driver's phba structure from the dev_id */
13875        hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13876        phba = hba_eq_hdl->phba;
13877
13878        if (unlikely(!phba))
13879                return IRQ_NONE;
13880
13881        /* Get to the EQ struct associated with this vector */
13882        eq = phba->sli4_hba.fof_eq;
13883        if (unlikely(!eq))
13884                return IRQ_NONE;
13885
13886        /* Check device state for handling interrupt */
13887        if (unlikely(lpfc_intr_state_check(phba))) {
13888                /* Check again for link_state with lock held */
13889                spin_lock_irqsave(&phba->hbalock, iflag);
13890                if (phba->link_state < LPFC_LINK_DOWN)
13891                        /* Flush, clear interrupt, and rearm the EQ */
13892                        lpfc_sli4_eq_flush(phba, eq);
13893                spin_unlock_irqrestore(&phba->hbalock, iflag);
13894                return IRQ_NONE;
13895        }
13896
13897        /*
13898         * Process all the event on FCP fast-path EQ
13899         */
13900        while ((eqe = lpfc_sli4_eq_get(eq))) {
13901                lpfc_sli4_fof_handle_eqe(phba, eqe);
13902                if (!(++ecount % eq->entry_repost))
13903                        break;
13904                eq->EQ_processed++;
13905        }
13906
13907        /* Track the max number of EQEs processed in 1 intr */
13908        if (ecount > eq->EQ_max_eqe)
13909                eq->EQ_max_eqe = ecount;
13910
13911
13912        if (unlikely(ecount == 0)) {
13913                eq->EQ_no_entry++;
13914
13915                if (phba->intr_type == MSIX)
13916                        /* MSI-X treated interrupt served as no EQ share INT */
13917                        lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13918                                        "9145 MSI-X interrupt with no EQE\n");
13919                else {
13920                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13921                                        "9146 ISR interrupt with no EQE\n");
13922                        /* Non MSI-X treated on interrupt as EQ share INT */
13923                        return IRQ_NONE;
13924                }
13925        }
13926        /* Always clear and re-arm the fast-path EQ */
13927        phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
13928        return IRQ_HANDLED;
13929}
13930
13931/**
13932 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
13933 * @irq: Interrupt number.
13934 * @dev_id: The device context pointer.
13935 *
13936 * This function is directly called from the PCI layer as an interrupt
13937 * service routine when device with SLI-4 interface spec is enabled with
13938 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13939 * ring event in the HBA. However, when the device is enabled with either
13940 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13941 * device-level interrupt handler. When the PCI slot is in error recovery
13942 * or the HBA is undergoing initialization, the interrupt handler will not
13943 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13944 * the intrrupt context. This function is called without any lock held.
13945 * It gets the hbalock to access and update SLI data structures. Note that,
13946 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13947 * equal to that of FCP CQ index.
13948 *
13949 * The link attention and ELS ring attention events are handled
13950 * by the worker thread. The interrupt handler signals the worker thread
13951 * and returns for these events. This function is called without any lock
13952 * held. It gets the hbalock to access and update SLI data structures.
13953 *
13954 * This function returns IRQ_HANDLED when interrupt is handled else it
13955 * returns IRQ_NONE.
13956 **/
13957irqreturn_t
13958lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
13959{
13960        struct lpfc_hba *phba;
13961        struct lpfc_hba_eq_hdl *hba_eq_hdl;
13962        struct lpfc_queue *fpeq;
13963        struct lpfc_eqe *eqe;
13964        unsigned long iflag;
13965        int ecount = 0;
13966        int hba_eqidx;
13967
13968        /* Get the driver's phba structure from the dev_id */
13969        hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13970        phba = hba_eq_hdl->phba;
13971        hba_eqidx = hba_eq_hdl->idx;
13972
13973        if (unlikely(!phba))
13974                return IRQ_NONE;
13975        if (unlikely(!phba->sli4_hba.hba_eq))
13976                return IRQ_NONE;
13977
13978        /* Get to the EQ struct associated with this vector */
13979        fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
13980        if (unlikely(!fpeq))
13981                return IRQ_NONE;
13982
13983        if (lpfc_fcp_look_ahead) {
13984                if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
13985                        phba->sli4_hba.sli4_eq_clr_intr(fpeq);
13986                else {
13987                        atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13988                        return IRQ_NONE;
13989                }
13990        }
13991
13992        /* Check device state for handling interrupt */
13993        if (unlikely(lpfc_intr_state_check(phba))) {
13994                /* Check again for link_state with lock held */
13995                spin_lock_irqsave(&phba->hbalock, iflag);
13996                if (phba->link_state < LPFC_LINK_DOWN)
13997                        /* Flush, clear interrupt, and rearm the EQ */
13998                        lpfc_sli4_eq_flush(phba, fpeq);
13999                spin_unlock_irqrestore(&phba->hbalock, iflag);
14000                if (lpfc_fcp_look_ahead)
14001                        atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14002                return IRQ_NONE;
14003        }
14004
14005        /*
14006         * Process all the event on FCP fast-path EQ
14007         */
14008        while ((eqe = lpfc_sli4_eq_get(fpeq))) {
14009                lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
14010                if (!(++ecount % fpeq->entry_repost))
14011                        break;
14012                fpeq->EQ_processed++;
14013        }
14014
14015        /* Track the max number of EQEs processed in 1 intr */
14016        if (ecount > fpeq->EQ_max_eqe)
14017                fpeq->EQ_max_eqe = ecount;
14018
14019        /* Always clear and re-arm the fast-path EQ */
14020        phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
14021
14022        if (unlikely(ecount == 0)) {
14023                fpeq->EQ_no_entry++;
14024
14025                if (lpfc_fcp_look_ahead) {
14026                        atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14027                        return IRQ_NONE;
14028                }
14029
14030                if (phba->intr_type == MSIX)
14031                        /* MSI-X treated interrupt served as no EQ share INT */
14032                        lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14033                                        "0358 MSI-X interrupt with no EQE\n");
14034                else
14035                        /* Non MSI-X treated on interrupt as EQ share INT */
14036                        return IRQ_NONE;
14037        }
14038
14039        if (lpfc_fcp_look_ahead)
14040                atomic_inc(&hba_eq_hdl->hba_eq_in_use);
14041
14042        return IRQ_HANDLED;
14043} /* lpfc_sli4_fp_intr_handler */
14044
14045/**
14046 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14047 * @irq: Interrupt number.
14048 * @dev_id: The device context pointer.
14049 *
14050 * This function is the device-level interrupt handler to device with SLI-4
14051 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14052 * interrupt mode is enabled and there is an event in the HBA which requires
14053 * driver attention. This function invokes the slow-path interrupt attention
14054 * handling function and fast-path interrupt attention handling function in
14055 * turn to process the relevant HBA attention events. This function is called
14056 * without any lock held. It gets the hbalock to access and update SLI data
14057 * structures.
14058 *
14059 * This function returns IRQ_HANDLED when interrupt is handled, else it
14060 * returns IRQ_NONE.
14061 **/
14062irqreturn_t
14063lpfc_sli4_intr_handler(int irq, void *dev_id)
14064{
14065        struct lpfc_hba  *phba;
14066        irqreturn_t hba_irq_rc;
14067        bool hba_handled = false;
14068        int qidx;
14069
14070        /* Get the driver's phba structure from the dev_id */
14071        phba = (struct lpfc_hba *)dev_id;
14072
14073        if (unlikely(!phba))
14074                return IRQ_NONE;
14075
14076        /*
14077         * Invoke fast-path host attention interrupt handling as appropriate.
14078         */
14079        for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
14080                hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
14081                                        &phba->sli4_hba.hba_eq_hdl[qidx]);
14082                if (hba_irq_rc == IRQ_HANDLED)
14083                        hba_handled |= true;
14084        }
14085
14086        if (phba->cfg_fof) {
14087                hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
14088                                        &phba->sli4_hba.hba_eq_hdl[qidx]);
14089                if (hba_irq_rc == IRQ_HANDLED)
14090                        hba_handled |= true;
14091        }
14092
14093        return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
14094} /* lpfc_sli4_intr_handler */
14095
14096/**
14097 * lpfc_sli4_queue_free - free a queue structure and associated memory
14098 * @queue: The queue structure to free.
14099 *
14100 * This function frees a queue structure and the DMAable memory used for
14101 * the host resident queue. This function must be called after destroying the
14102 * queue on the HBA.
14103 **/
14104void
14105lpfc_sli4_queue_free(struct lpfc_queue *queue)
14106{
14107        struct lpfc_dmabuf *dmabuf;
14108
14109        if (!queue)
14110                return;
14111
14112        while (!list_empty(&queue->page_list)) {
14113                list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14114                                 list);
14115                dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
14116                                  dmabuf->virt, dmabuf->phys);
14117                kfree(dmabuf);
14118        }
14119        if (queue->rqbp) {
14120                lpfc_free_rq_buffer(queue->phba, queue);
14121                kfree(queue->rqbp);
14122        }
14123
14124        if (!list_empty(&queue->wq_list))
14125                list_del(&queue->wq_list);
14126
14127        kfree(queue);
14128        return;
14129}
14130
14131/**
14132 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14133 * @phba: The HBA that this queue is being created on.
14134 * @page_size: The size of a queue page
14135 * @entry_size: The size of each queue entry for this queue.
14136 * @entry count: The number of entries that this queue will handle.
14137 *
14138 * This function allocates a queue structure and the DMAable memory used for
14139 * the host resident queue. This function must be called before creating the
14140 * queue on the HBA.
14141 **/
14142struct lpfc_queue *
14143lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14144                      uint32_t entry_size, uint32_t entry_count)
14145{
14146        struct lpfc_queue *queue;
14147        struct lpfc_dmabuf *dmabuf;
14148        int x, total_qe_count;
14149        void *dma_pointer;
14150        uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14151
14152        if (!phba->sli4_hba.pc_sli4_params.supported)
14153                hw_page_size = page_size;
14154
14155        queue = kzalloc(sizeof(struct lpfc_queue) +
14156                        (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14157        if (!queue)
14158                return NULL;
14159        queue->page_count = (ALIGN(entry_size * entry_count,
14160                        hw_page_size))/hw_page_size;
14161
14162        /* If needed, Adjust page count to match the max the adapter supports */
14163        if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14164                queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14165
14166        INIT_LIST_HEAD(&queue->list);
14167        INIT_LIST_HEAD(&queue->wq_list);
14168        INIT_LIST_HEAD(&queue->wqfull_list);
14169        INIT_LIST_HEAD(&queue->page_list);
14170        INIT_LIST_HEAD(&queue->child_list);
14171
14172        /* Set queue parameters now.  If the system cannot provide memory
14173         * resources, the free routine needs to know what was allocated.
14174         */
14175        queue->entry_size = entry_size;
14176        queue->entry_count = entry_count;
14177        queue->page_size = hw_page_size;
14178        queue->phba = phba;
14179
14180        for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14181                dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14182                if (!dmabuf)
14183                        goto out_fail;
14184                dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14185                                                   hw_page_size, &dmabuf->phys,
14186                                                   GFP_KERNEL);
14187                if (!dmabuf->virt) {
14188                        kfree(dmabuf);
14189                        goto out_fail;
14190                }
14191                dmabuf->buffer_tag = x;
14192                list_add_tail(&dmabuf->list, &queue->page_list);
14193                /* initialize queue's entry array */
14194                dma_pointer = dmabuf->virt;
14195                for (; total_qe_count < entry_count &&
14196                     dma_pointer < (hw_page_size + dmabuf->virt);
14197                     total_qe_count++, dma_pointer += entry_size) {
14198                        queue->qe[total_qe_count].address = dma_pointer;
14199                }
14200        }
14201        INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14202        INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
14203
14204        /* entry_repost will be set during q creation */
14205
14206        return queue;
14207out_fail:
14208        lpfc_sli4_queue_free(queue);
14209        return NULL;
14210}
14211
14212/**
14213 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14214 * @phba: HBA structure that indicates port to create a queue on.
14215 * @pci_barset: PCI BAR set flag.
14216 *
14217 * This function shall perform iomap of the specified PCI BAR address to host
14218 * memory address if not already done so and return it. The returned host
14219 * memory address can be NULL.
14220 */
14221static void __iomem *
14222lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14223{
14224        if (!phba->pcidev)
14225                return NULL;
14226
14227        switch (pci_barset) {
14228        case WQ_PCI_BAR_0_AND_1:
14229                return phba->pci_bar0_memmap_p;
14230        case WQ_PCI_BAR_2_AND_3:
14231                return phba->pci_bar2_memmap_p;
14232        case WQ_PCI_BAR_4_AND_5:
14233                return phba->pci_bar4_memmap_p;
14234        default:
14235                break;
14236        }
14237        return NULL;
14238}
14239
14240/**
14241 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
14242 * @phba: HBA structure that indicates port to create a queue on.
14243 * @startq: The starting FCP EQ to modify
14244 *
14245 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
14246 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14247 * updated in one mailbox command.
14248 *
14249 * The @phba struct is used to send mailbox command to HBA. The @startq
14250 * is used to get the starting FCP EQ to change.
14251 * This function is asynchronous and will wait for the mailbox
14252 * command to finish before continuing.
14253 *
14254 * On success this function will return a zero. If unable to allocate enough
14255 * memory this function will return -ENOMEM. If the queue create mailbox command
14256 * fails this function will return -ENXIO.
14257 **/
14258int
14259lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14260                         uint32_t numq, uint32_t imax)
14261{
14262        struct lpfc_mbx_modify_eq_delay *eq_delay;
14263        LPFC_MBOXQ_t *mbox;
14264        struct lpfc_queue *eq;
14265        int cnt, rc, length, status = 0;
14266        uint32_t shdr_status, shdr_add_status;
14267        uint32_t result, val;
14268        int qidx;
14269        union lpfc_sli4_cfg_shdr *shdr;
14270        uint16_t dmult;
14271
14272        if (startq >= phba->io_channel_irqs)
14273                return 0;
14274
14275        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14276        if (!mbox)
14277                return -ENOMEM;
14278        length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14279                  sizeof(struct lpfc_sli4_cfg_mhdr));
14280        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14281                         LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14282                         length, LPFC_SLI4_MBX_EMBED);
14283        eq_delay = &mbox->u.mqe.un.eq_delay;
14284
14285        /* Calculate delay multiper from maximum interrupt per second */
14286        result = imax / phba->io_channel_irqs;
14287        if (result > LPFC_DMULT_CONST || result == 0)
14288                dmult = 0;
14289        else
14290                dmult = LPFC_DMULT_CONST/result - 1;
14291        if (dmult > LPFC_DMULT_MAX)
14292                dmult = LPFC_DMULT_MAX;
14293
14294        cnt = 0;
14295        for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14296                eq = phba->sli4_hba.hba_eq[qidx];
14297                if (!eq)
14298                        continue;
14299                eq->q_mode = imax;
14300                eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14301                eq_delay->u.request.eq[cnt].phase = 0;
14302                eq_delay->u.request.eq[cnt].delay_multi = dmult;
14303                cnt++;
14304
14305                /* q_mode is only used for auto_imax */
14306                if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14307                        /* Use EQ Delay Register method for q_mode */
14308
14309                        /* Convert for EQ Delay register */
14310                        val =  phba->cfg_fcp_imax;
14311                        if (val) {
14312                                /* First, interrupts per sec per EQ */
14313                                val = phba->cfg_fcp_imax /
14314                                        phba->io_channel_irqs;
14315
14316                                /* us delay between each interrupt */
14317                                val = LPFC_SEC_TO_USEC / val;
14318                        }
14319                        eq->q_mode = val;
14320                } else {
14321                        eq->q_mode = imax;
14322                }
14323
14324                if (cnt >= numq)
14325                        break;
14326        }
14327        eq_delay->u.request.num_eq = cnt;
14328
14329        mbox->vport = phba->pport;
14330        mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14331        mbox->context1 = NULL;
14332        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14333        shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14334        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14335        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14336        if (shdr_status || shdr_add_status || rc) {
14337                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14338                                "2512 MODIFY_EQ_DELAY mailbox failed with "
14339                                "status x%x add_status x%x, mbx status x%x\n",
14340                                shdr_status, shdr_add_status, rc);
14341                status = -ENXIO;
14342        }
14343        mempool_free(mbox, phba->mbox_mem_pool);
14344        return status;
14345}
14346
14347/**
14348 * lpfc_eq_create - Create an Event Queue on the HBA
14349 * @phba: HBA structure that indicates port to create a queue on.
14350 * @eq: The queue structure to use to create the event queue.
14351 * @imax: The maximum interrupt per second limit.
14352 *
14353 * This function creates an event queue, as detailed in @eq, on a port,
14354 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14355 *
14356 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14357 * is used to get the entry count and entry size that are necessary to
14358 * determine the number of pages to allocate and use for this queue. This
14359 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14360 * event queue. This function is asynchronous and will wait for the mailbox
14361 * command to finish before continuing.
14362 *
14363 * On success this function will return a zero. If unable to allocate enough
14364 * memory this function will return -ENOMEM. If the queue create mailbox command
14365 * fails this function will return -ENXIO.
14366 **/
14367int
14368lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
14369{
14370        struct lpfc_mbx_eq_create *eq_create;
14371        LPFC_MBOXQ_t *mbox;
14372        int rc, length, status = 0;
14373        struct lpfc_dmabuf *dmabuf;
14374        uint32_t shdr_status, shdr_add_status;
14375        union lpfc_sli4_cfg_shdr *shdr;
14376        uint16_t dmult;
14377        uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14378
14379        /* sanity check on queue memory */
14380        if (!eq)
14381                return -ENODEV;
14382        if (!phba->sli4_hba.pc_sli4_params.supported)
14383                hw_page_size = SLI4_PAGE_SIZE;
14384
14385        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14386        if (!mbox)
14387                return -ENOMEM;
14388        length = (sizeof(struct lpfc_mbx_eq_create) -
14389                  sizeof(struct lpfc_sli4_cfg_mhdr));
14390        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14391                         LPFC_MBOX_OPCODE_EQ_CREATE,
14392                         length, LPFC_SLI4_MBX_EMBED);
14393        eq_create = &mbox->u.mqe.un.eq_create;
14394        shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14395        bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14396               eq->page_count);
14397        bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14398               LPFC_EQE_SIZE);
14399        bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
14400
14401        /* Use version 2 of CREATE_EQ if eqav is set */
14402        if (phba->sli4_hba.pc_sli4_params.eqav) {
14403                bf_set(lpfc_mbox_hdr_version, &shdr->request,
14404                       LPFC_Q_CREATE_VERSION_2);
14405                bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
14406                       phba->sli4_hba.pc_sli4_params.eqav);
14407        }
14408
14409        /* don't setup delay multiplier using EQ_CREATE */
14410        dmult = 0;
14411        bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14412               dmult);
14413        switch (eq->entry_count) {
14414        default:
14415                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14416                                "0360 Unsupported EQ count. (%d)\n",
14417                                eq->entry_count);
14418                if (eq->entry_count < 256)
14419                        return -EINVAL;
14420                /* otherwise default to smallest count (drop through) */
14421        case 256:
14422                bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14423                       LPFC_EQ_CNT_256);
14424                break;
14425        case 512:
14426                bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14427                       LPFC_EQ_CNT_512);
14428                break;
14429        case 1024:
14430                bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14431                       LPFC_EQ_CNT_1024);
14432                break;
14433        case 2048:
14434                bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14435                       LPFC_EQ_CNT_2048);
14436                break;
14437        case 4096:
14438                bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14439                       LPFC_EQ_CNT_4096);
14440                break;
14441        }
14442        list_for_each_entry(dmabuf, &eq->page_list, list) {
14443                memset(dmabuf->virt, 0, hw_page_size);
14444                eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14445                                        putPaddrLow(dmabuf->phys);
14446                eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14447                                        putPaddrHigh(dmabuf->phys);
14448        }
14449        mbox->vport = phba->pport;
14450        mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14451        mbox->context1 = NULL;
14452        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14453        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14454        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14455        if (shdr_status || shdr_add_status || rc) {
14456                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14457                                "2500 EQ_CREATE mailbox failed with "
14458                                "status x%x add_status x%x, mbx status x%x\n",
14459                                shdr_status, shdr_add_status, rc);
14460                status = -ENXIO;
14461        }
14462        eq->type = LPFC_EQ;
14463        eq->subtype = LPFC_NONE;
14464        eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14465        if (eq->queue_id == 0xFFFF)
14466                status = -ENXIO;
14467        eq->host_index = 0;
14468        eq->hba_index = 0;
14469        eq->entry_repost = LPFC_EQ_REPOST;
14470
14471        mempool_free(mbox, phba->mbox_mem_pool);
14472        return status;
14473}
14474
14475/**
14476 * lpfc_cq_create - Create a Completion Queue on the HBA
14477 * @phba: HBA structure that indicates port to create a queue on.
14478 * @cq: The queue structure to use to create the completion queue.
14479 * @eq: The event queue to bind this completion queue to.
14480 *
14481 * This function creates a completion queue, as detailed in @wq, on a port,
14482 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14483 *
14484 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14485 * is used to get the entry count and entry size that are necessary to
14486 * determine the number of pages to allocate and use for this queue. The @eq
14487 * is used to indicate which event queue to bind this completion queue to. This
14488 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14489 * completion queue. This function is asynchronous and will wait for the mailbox
14490 * command to finish before continuing.
14491 *
14492 * On success this function will return a zero. If unable to allocate enough
14493 * memory this function will return -ENOMEM. If the queue create mailbox command
14494 * fails this function will return -ENXIO.
14495 **/
14496int
14497lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14498               struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14499{
14500        struct lpfc_mbx_cq_create *cq_create;
14501        struct lpfc_dmabuf *dmabuf;
14502        LPFC_MBOXQ_t *mbox;
14503        int rc, length, status = 0;
14504        uint32_t shdr_status, shdr_add_status;
14505        union lpfc_sli4_cfg_shdr *shdr;
14506        uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14507
14508        /* sanity check on queue memory */
14509        if (!cq || !eq)
14510                return -ENODEV;
14511        if (!phba->sli4_hba.pc_sli4_params.supported)
14512                hw_page_size = cq->page_size;
14513
14514        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14515        if (!mbox)
14516                return -ENOMEM;
14517        length = (sizeof(struct lpfc_mbx_cq_create) -
14518                  sizeof(struct lpfc_sli4_cfg_mhdr));
14519        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14520                         LPFC_MBOX_OPCODE_CQ_CREATE,
14521                         length, LPFC_SLI4_MBX_EMBED);
14522        cq_create = &mbox->u.mqe.un.cq_create;
14523        shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
14524        bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14525                    cq->page_count);
14526        bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14527        bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
14528        bf_set(lpfc_mbox_hdr_version, &shdr->request,
14529               phba->sli4_hba.pc_sli4_params.cqv);
14530        if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
14531                bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14532                       (cq->page_size / SLI4_PAGE_SIZE));
14533                bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14534                       eq->queue_id);
14535                bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
14536                       phba->sli4_hba.pc_sli4_params.cqav);
14537        } else {
14538                bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14539                       eq->queue_id);
14540        }
14541        switch (cq->entry_count) {
14542        case 2048:
14543        case 4096:
14544                if (phba->sli4_hba.pc_sli4_params.cqv ==
14545                    LPFC_Q_CREATE_VERSION_2) {
14546                        cq_create->u.request.context.lpfc_cq_context_count =
14547                                cq->entry_count;
14548                        bf_set(lpfc_cq_context_count,
14549                               &cq_create->u.request.context,
14550                               LPFC_CQ_CNT_WORD7);
14551                        break;
14552                }
14553                /* Fall Thru */
14554        default:
14555                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14556                                "0361 Unsupported CQ count: "
14557                                "entry cnt %d sz %d pg cnt %d\n",
14558                                cq->entry_count, cq->entry_size,
14559                                cq->page_count);
14560                if (cq->entry_count < 256) {
14561                        status = -EINVAL;
14562                        goto out;
14563                }
14564                /* otherwise default to smallest count (drop through) */
14565        case 256:
14566                bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14567                       LPFC_CQ_CNT_256);
14568                break;
14569        case 512:
14570                bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14571                       LPFC_CQ_CNT_512);
14572                break;
14573        case 1024:
14574                bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14575                       LPFC_CQ_CNT_1024);
14576                break;
14577        }
14578        list_for_each_entry(dmabuf, &cq->page_list, list) {
14579                memset(dmabuf->virt, 0, cq->page_size);
14580                cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14581                                        putPaddrLow(dmabuf->phys);
14582                cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14583                                        putPaddrHigh(dmabuf->phys);
14584        }
14585        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14586
14587        /* The IOCTL status is embedded in the mailbox subheader. */
14588        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14589        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14590        if (shdr_status || shdr_add_status || rc) {
14591                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14592                                "2501 CQ_CREATE mailbox failed with "
14593                                "status x%x add_status x%x, mbx status x%x\n",
14594                                shdr_status, shdr_add_status, rc);
14595                status = -ENXIO;
14596                goto out;
14597        }
14598        cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14599        if (cq->queue_id == 0xFFFF) {
14600                status = -ENXIO;
14601                goto out;
14602        }
14603        /* link the cq onto the parent eq child list */
14604        list_add_tail(&cq->list, &eq->child_list);
14605        /* Set up completion queue's type and subtype */
14606        cq->type = type;
14607        cq->subtype = subtype;
14608        cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14609        cq->assoc_qid = eq->queue_id;
14610        cq->host_index = 0;
14611        cq->hba_index = 0;
14612        cq->entry_repost = LPFC_CQ_REPOST;
14613
14614out:
14615        mempool_free(mbox, phba->mbox_mem_pool);
14616        return status;
14617}
14618
14619/**
14620 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14621 * @phba: HBA structure that indicates port to create a queue on.
14622 * @cqp: The queue structure array to use to create the completion queues.
14623 * @eqp: The event queue array to bind these completion queues to.
14624 *
14625 * This function creates a set of  completion queue, s to support MRQ
14626 * as detailed in @cqp, on a port,
14627 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14628 *
14629 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14630 * is used to get the entry count and entry size that are necessary to
14631 * determine the number of pages to allocate and use for this queue. The @eq
14632 * is used to indicate which event queue to bind this completion queue to. This
14633 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14634 * completion queue. This function is asynchronous and will wait for the mailbox
14635 * command to finish before continuing.
14636 *
14637 * On success this function will return a zero. If unable to allocate enough
14638 * memory this function will return -ENOMEM. If the queue create mailbox command
14639 * fails this function will return -ENXIO.
14640 **/
14641int
14642lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14643                   struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14644{
14645        struct lpfc_queue *cq;
14646        struct lpfc_queue *eq;
14647        struct lpfc_mbx_cq_create_set *cq_set;
14648        struct lpfc_dmabuf *dmabuf;
14649        LPFC_MBOXQ_t *mbox;
14650        int rc, length, alloclen, status = 0;
14651        int cnt, idx, numcq, page_idx = 0;
14652        uint32_t shdr_status, shdr_add_status;
14653        union lpfc_sli4_cfg_shdr *shdr;
14654        uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14655
14656        /* sanity check on queue memory */
14657        numcq = phba->cfg_nvmet_mrq;
14658        if (!cqp || !eqp || !numcq)
14659                return -ENODEV;
14660
14661        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14662        if (!mbox)
14663                return -ENOMEM;
14664
14665        length = sizeof(struct lpfc_mbx_cq_create_set);
14666        length += ((numcq * cqp[0]->page_count) *
14667                   sizeof(struct dma_address));
14668        alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14669                        LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14670                        LPFC_SLI4_MBX_NEMBED);
14671        if (alloclen < length) {
14672                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14673                                "3098 Allocated DMA memory size (%d) is "
14674                                "less than the requested DMA memory size "
14675                                "(%d)\n", alloclen, length);
14676                status = -ENOMEM;
14677                goto out;
14678        }
14679        cq_set = mbox->sge_array->addr[0];
14680        shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14681        bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14682
14683        for (idx = 0; idx < numcq; idx++) {
14684                cq = cqp[idx];
14685                eq = eqp[idx];
14686                if (!cq || !eq) {
14687                        status = -ENOMEM;
14688                        goto out;
14689                }
14690                if (!phba->sli4_hba.pc_sli4_params.supported)
14691                        hw_page_size = cq->page_size;
14692
14693                switch (idx) {
14694                case 0:
14695                        bf_set(lpfc_mbx_cq_create_set_page_size,
14696                               &cq_set->u.request,
14697                               (hw_page_size / SLI4_PAGE_SIZE));
14698                        bf_set(lpfc_mbx_cq_create_set_num_pages,
14699                               &cq_set->u.request, cq->page_count);
14700                        bf_set(lpfc_mbx_cq_create_set_evt,
14701                               &cq_set->u.request, 1);
14702                        bf_set(lpfc_mbx_cq_create_set_valid,
14703                               &cq_set->u.request, 1);
14704                        bf_set(lpfc_mbx_cq_create_set_cqe_size,
14705                               &cq_set->u.request, 0);
14706                        bf_set(lpfc_mbx_cq_create_set_num_cq,
14707                               &cq_set->u.request, numcq);
14708                        bf_set(lpfc_mbx_cq_create_set_autovalid,
14709                               &cq_set->u.request,
14710                               phba->sli4_hba.pc_sli4_params.cqav);
14711                        switch (cq->entry_count) {
14712                        case 2048:
14713                        case 4096:
14714                                if (phba->sli4_hba.pc_sli4_params.cqv ==
14715                                    LPFC_Q_CREATE_VERSION_2) {
14716                                        bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14717                                               &cq_set->u.request,
14718                                                cq->entry_count);
14719                                        bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14720                                               &cq_set->u.request,
14721                                               LPFC_CQ_CNT_WORD7);
14722                                        break;
14723                                }
14724                                /* Fall Thru */
14725                        default:
14726                                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14727                                                "3118 Bad CQ count. (%d)\n",
14728                                                cq->entry_count);
14729                                if (cq->entry_count < 256) {
14730                                        status = -EINVAL;
14731                                        goto out;
14732                                }
14733                                /* otherwise default to smallest (drop thru) */
14734                        case 256:
14735                                bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14736                                       &cq_set->u.request, LPFC_CQ_CNT_256);
14737                                break;
14738                        case 512:
14739                                bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14740                                       &cq_set->u.request, LPFC_CQ_CNT_512);
14741                                break;
14742                        case 1024:
14743                                bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14744                                       &cq_set->u.request, LPFC_CQ_CNT_1024);
14745                                break;
14746                        }
14747                        bf_set(lpfc_mbx_cq_create_set_eq_id0,
14748                               &cq_set->u.request, eq->queue_id);
14749                        break;
14750                case 1:
14751                        bf_set(lpfc_mbx_cq_create_set_eq_id1,
14752                               &cq_set->u.request, eq->queue_id);
14753                        break;
14754                case 2:
14755                        bf_set(lpfc_mbx_cq_create_set_eq_id2,
14756                               &cq_set->u.request, eq->queue_id);
14757                        break;
14758                case 3:
14759                        bf_set(lpfc_mbx_cq_create_set_eq_id3,
14760                               &cq_set->u.request, eq->queue_id);
14761                        break;
14762                case 4:
14763                        bf_set(lpfc_mbx_cq_create_set_eq_id4,
14764                               &cq_set->u.request, eq->queue_id);
14765                        break;
14766                case 5:
14767                        bf_set(lpfc_mbx_cq_create_set_eq_id5,
14768                               &cq_set->u.request, eq->queue_id);
14769                        break;
14770                case 6:
14771                        bf_set(lpfc_mbx_cq_create_set_eq_id6,
14772                               &cq_set->u.request, eq->queue_id);
14773                        break;
14774                case 7:
14775                        bf_set(lpfc_mbx_cq_create_set_eq_id7,
14776                               &cq_set->u.request, eq->queue_id);
14777                        break;
14778                case 8:
14779                        bf_set(lpfc_mbx_cq_create_set_eq_id8,
14780                               &cq_set->u.request, eq->queue_id);
14781                        break;
14782                case 9:
14783                        bf_set(lpfc_mbx_cq_create_set_eq_id9,
14784                               &cq_set->u.request, eq->queue_id);
14785                        break;
14786                case 10:
14787                        bf_set(lpfc_mbx_cq_create_set_eq_id10,
14788                               &cq_set->u.request, eq->queue_id);
14789                        break;
14790                case 11:
14791                        bf_set(lpfc_mbx_cq_create_set_eq_id11,
14792                               &cq_set->u.request, eq->queue_id);
14793                        break;
14794                case 12:
14795                        bf_set(lpfc_mbx_cq_create_set_eq_id12,
14796                               &cq_set->u.request, eq->queue_id);
14797                        break;
14798                case 13:
14799                        bf_set(lpfc_mbx_cq_create_set_eq_id13,
14800                               &cq_set->u.request, eq->queue_id);
14801                        break;
14802                case 14:
14803                        bf_set(lpfc_mbx_cq_create_set_eq_id14,
14804                               &cq_set->u.request, eq->queue_id);
14805                        break;
14806                case 15:
14807                        bf_set(lpfc_mbx_cq_create_set_eq_id15,
14808                               &cq_set->u.request, eq->queue_id);
14809                        break;
14810                }
14811
14812                /* link the cq onto the parent eq child list */
14813                list_add_tail(&cq->list, &eq->child_list);
14814                /* Set up completion queue's type and subtype */
14815                cq->type = type;
14816                cq->subtype = subtype;
14817                cq->assoc_qid = eq->queue_id;
14818                cq->host_index = 0;
14819                cq->hba_index = 0;
14820                cq->entry_repost = LPFC_CQ_REPOST;
14821                cq->chann = idx;
14822
14823                rc = 0;
14824                list_for_each_entry(dmabuf, &cq->page_list, list) {
14825                        memset(dmabuf->virt, 0, hw_page_size);
14826                        cnt = page_idx + dmabuf->buffer_tag;
14827                        cq_set->u.request.page[cnt].addr_lo =
14828                                        putPaddrLow(dmabuf->phys);
14829                        cq_set->u.request.page[cnt].addr_hi =
14830                                        putPaddrHigh(dmabuf->phys);
14831                        rc++;
14832                }
14833                page_idx += rc;
14834        }
14835
14836        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14837
14838        /* The IOCTL status is embedded in the mailbox subheader. */
14839        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14840        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14841        if (shdr_status || shdr_add_status || rc) {
14842                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14843                                "3119 CQ_CREATE_SET mailbox failed with "
14844                                "status x%x add_status x%x, mbx status x%x\n",
14845                                shdr_status, shdr_add_status, rc);
14846                status = -ENXIO;
14847                goto out;
14848        }
14849        rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14850        if (rc == 0xFFFF) {
14851                status = -ENXIO;
14852                goto out;
14853        }
14854
14855        for (idx = 0; idx < numcq; idx++) {
14856                cq = cqp[idx];
14857                cq->queue_id = rc + idx;
14858        }
14859
14860out:
14861        lpfc_sli4_mbox_cmd_free(phba, mbox);
14862        return status;
14863}
14864
14865/**
14866 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
14867 * @phba: HBA structure that indicates port to create a queue on.
14868 * @mq: The queue structure to use to create the mailbox queue.
14869 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14870 * @cq: The completion queue to associate with this cq.
14871 *
14872 * This function provides failback (fb) functionality when the
14873 * mq_create_ext fails on older FW generations.  It's purpose is identical
14874 * to mq_create_ext otherwise.
14875 *
14876 * This routine cannot fail as all attributes were previously accessed and
14877 * initialized in mq_create_ext.
14878 **/
14879static void
14880lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14881                       LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
14882{
14883        struct lpfc_mbx_mq_create *mq_create;
14884        struct lpfc_dmabuf *dmabuf;
14885        int length;
14886
14887        length = (sizeof(struct lpfc_mbx_mq_create) -
14888                  sizeof(struct lpfc_sli4_cfg_mhdr));
14889        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14890                         LPFC_MBOX_OPCODE_MQ_CREATE,
14891                         length, LPFC_SLI4_MBX_EMBED);
14892        mq_create = &mbox->u.mqe.un.mq_create;
14893        bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
14894               mq->page_count);
14895        bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
14896               cq->queue_id);
14897        bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14898        switch (mq->entry_count) {
14899        case 16:
14900                bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14901                       LPFC_MQ_RING_SIZE_16);
14902                break;
14903        case 32:
14904                bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14905                       LPFC_MQ_RING_SIZE_32);
14906                break;
14907        case 64:
14908                bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14909                       LPFC_MQ_RING_SIZE_64);
14910                break;
14911        case 128:
14912                bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14913                       LPFC_MQ_RING_SIZE_128);
14914                break;
14915        }
14916        list_for_each_entry(dmabuf, &mq->page_list, list) {
14917                mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14918                        putPaddrLow(dmabuf->phys);
14919                mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14920                        putPaddrHigh(dmabuf->phys);
14921        }
14922}
14923
14924/**
14925 * lpfc_mq_create - Create a mailbox Queue on the HBA
14926 * @phba: HBA structure that indicates port to create a queue on.
14927 * @mq: The queue structure to use to create the mailbox queue.
14928 * @cq: The completion queue to associate with this cq.
14929 * @subtype: The queue's subtype.
14930 *
14931 * This function creates a mailbox queue, as detailed in @mq, on a port,
14932 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14933 *
14934 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14935 * is used to get the entry count and entry size that are necessary to
14936 * determine the number of pages to allocate and use for this queue. This
14937 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14938 * mailbox queue. This function is asynchronous and will wait for the mailbox
14939 * command to finish before continuing.
14940 *
14941 * On success this function will return a zero. If unable to allocate enough
14942 * memory this function will return -ENOMEM. If the queue create mailbox command
14943 * fails this function will return -ENXIO.
14944 **/
14945int32_t
14946lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14947               struct lpfc_queue *cq, uint32_t subtype)
14948{
14949        struct lpfc_mbx_mq_create *mq_create;
14950        struct lpfc_mbx_mq_create_ext *mq_create_ext;
14951        struct lpfc_dmabuf *dmabuf;
14952        LPFC_MBOXQ_t *mbox;
14953        int rc, length, status = 0;
14954        uint32_t shdr_status, shdr_add_status;
14955        union lpfc_sli4_cfg_shdr *shdr;
14956        uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14957
14958        /* sanity check on queue memory */
14959        if (!mq || !cq)
14960                return -ENODEV;
14961        if (!phba->sli4_hba.pc_sli4_params.supported)
14962                hw_page_size = SLI4_PAGE_SIZE;
14963
14964        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14965        if (!mbox)
14966                return -ENOMEM;
14967        length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14968                  sizeof(struct lpfc_sli4_cfg_mhdr));
14969        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14970                         LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14971                         length, LPFC_SLI4_MBX_EMBED);
14972
14973        mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
14974        shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
14975        bf_set(lpfc_mbx_mq_create_ext_num_pages,
14976               &mq_create_ext->u.request, mq->page_count);
14977        bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14978               &mq_create_ext->u.request, 1);
14979        bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
14980               &mq_create_ext->u.request, 1);
14981        bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14982               &mq_create_ext->u.request, 1);
14983        bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14984               &mq_create_ext->u.request, 1);
14985        bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14986               &mq_create_ext->u.request, 1);
14987        bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
14988        bf_set(lpfc_mbox_hdr_version, &shdr->request,
14989               phba->sli4_hba.pc_sli4_params.mqv);
14990        if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14991                bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14992                       cq->queue_id);
14993        else
14994                bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14995                       cq->queue_id);
14996        switch (mq->entry_count) {
14997        default:
14998                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14999                                "0362 Unsupported MQ count. (%d)\n",
15000                                mq->entry_count);
15001                if (mq->entry_count < 16) {
15002                        status = -EINVAL;
15003                        goto out;
15004                }
15005                /* otherwise default to smallest count (drop through) */
15006        case 16:
15007                bf_set(lpfc_mq_context_ring_size,
15008                       &mq_create_ext->u.request.context,
15009                       LPFC_MQ_RING_SIZE_16);
15010                break;
15011        case 32:
15012                bf_set(lpfc_mq_context_ring_size,
15013                       &mq_create_ext->u.request.context,
15014                       LPFC_MQ_RING_SIZE_32);
15015                break;
15016        case 64:
15017                bf_set(lpfc_mq_context_ring_size,
15018                       &mq_create_ext->u.request.context,
15019                       LPFC_MQ_RING_SIZE_64);
15020                break;
15021        case 128:
15022                bf_set(lpfc_mq_context_ring_size,
15023                       &mq_create_ext->u.request.context,
15024                       LPFC_MQ_RING_SIZE_128);
15025                break;
15026        }
15027        list_for_each_entry(dmabuf, &mq->page_list, list) {
15028                memset(dmabuf->virt, 0, hw_page_size);
15029                mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15030                                        putPaddrLow(dmabuf->phys);
15031                mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
15032                                        putPaddrHigh(dmabuf->phys);
15033        }
15034        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15035        mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15036                              &mq_create_ext->u.response);
15037        if (rc != MBX_SUCCESS) {
15038                lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15039                                "2795 MQ_CREATE_EXT failed with "
15040                                "status x%x. Failback to MQ_CREATE.\n",
15041                                rc);
15042                lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15043                mq_create = &mbox->u.mqe.un.mq_create;
15044                rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15045                shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15046                mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15047                                      &mq_create->u.response);
15048        }
15049
15050        /* The IOCTL status is embedded in the mailbox subheader. */
15051        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15052        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15053        if (shdr_status || shdr_add_status || rc) {
15054                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15055                                "2502 MQ_CREATE mailbox failed with "
15056                                "status x%x add_status x%x, mbx status x%x\n",
15057                                shdr_status, shdr_add_status, rc);
15058                status = -ENXIO;
15059                goto out;
15060        }
15061        if (mq->queue_id == 0xFFFF) {
15062                status = -ENXIO;
15063                goto out;
15064        }
15065        mq->type = LPFC_MQ;
15066        mq->assoc_qid = cq->queue_id;
15067        mq->subtype = subtype;
15068        mq->host_index = 0;
15069        mq->hba_index = 0;
15070        mq->entry_repost = LPFC_MQ_REPOST;
15071
15072        /* link the mq onto the parent cq child list */
15073        list_add_tail(&mq->list, &cq->child_list);
15074out:
15075        mempool_free(mbox, phba->mbox_mem_pool);
15076        return status;
15077}
15078
15079/**
15080 * lpfc_wq_create - Create a Work Queue on the HBA
15081 * @phba: HBA structure that indicates port to create a queue on.
15082 * @wq: The queue structure to use to create the work queue.
15083 * @cq: The completion queue to bind this work queue to.
15084 * @subtype: The subtype of the work queue indicating its functionality.
15085 *
15086 * This function creates a work queue, as detailed in @wq, on a port, described
15087 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15088 *
15089 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15090 * is used to get the entry count and entry size that are necessary to
15091 * determine the number of pages to allocate and use for this queue. The @cq
15092 * is used to indicate which completion queue to bind this work queue to. This
15093 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15094 * work queue. This function is asynchronous and will wait for the mailbox
15095 * command to finish before continuing.
15096 *
15097 * On success this function will return a zero. If unable to allocate enough
15098 * memory this function will return -ENOMEM. If the queue create mailbox command
15099 * fails this function will return -ENXIO.
15100 **/
15101int
15102lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15103               struct lpfc_queue *cq, uint32_t subtype)
15104{
15105        struct lpfc_mbx_wq_create *wq_create;
15106        struct lpfc_dmabuf *dmabuf;
15107        LPFC_MBOXQ_t *mbox;
15108        int rc, length, status = 0;
15109        uint32_t shdr_status, shdr_add_status;
15110        union lpfc_sli4_cfg_shdr *shdr;
15111        uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15112        struct dma_address *page;
15113        void __iomem *bar_memmap_p;
15114        uint32_t db_offset;
15115        uint16_t pci_barset;
15116        uint8_t dpp_barset;
15117        uint32_t dpp_offset;
15118        unsigned long pg_addr;
15119        uint8_t wq_create_version;
15120
15121        /* sanity check on queue memory */
15122        if (!wq || !cq)
15123                return -ENODEV;
15124        if (!phba->sli4_hba.pc_sli4_params.supported)
15125                hw_page_size = wq->page_size;
15126
15127        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15128        if (!mbox)
15129                return -ENOMEM;
15130        length = (sizeof(struct lpfc_mbx_wq_create) -
15131                  sizeof(struct lpfc_sli4_cfg_mhdr));
15132        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15133                         LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15134                         length, LPFC_SLI4_MBX_EMBED);
15135        wq_create = &mbox->u.mqe.un.wq_create;
15136        shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
15137        bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15138                    wq->page_count);
15139        bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15140                    cq->queue_id);
15141
15142        /* wqv is the earliest version supported, NOT the latest */
15143        bf_set(lpfc_mbox_hdr_version, &shdr->request,
15144               phba->sli4_hba.pc_sli4_params.wqv);
15145
15146        if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15147            (wq->page_size > SLI4_PAGE_SIZE))
15148                wq_create_version = LPFC_Q_CREATE_VERSION_1;
15149        else
15150                wq_create_version = LPFC_Q_CREATE_VERSION_0;
15151
15152
15153        if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15154                wq_create_version = LPFC_Q_CREATE_VERSION_1;
15155        else
15156                wq_create_version = LPFC_Q_CREATE_VERSION_0;
15157
15158        switch (wq_create_version) {
15159        case LPFC_Q_CREATE_VERSION_1:
15160                bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15161                       wq->entry_count);
15162                bf_set(lpfc_mbox_hdr_version, &shdr->request,
15163                       LPFC_Q_CREATE_VERSION_1);
15164
15165                switch (wq->entry_size) {
15166                default:
15167                case 64:
15168                        bf_set(lpfc_mbx_wq_create_wqe_size,
15169                               &wq_create->u.request_1,
15170                               LPFC_WQ_WQE_SIZE_64);
15171                        break;
15172                case 128:
15173                        bf_set(lpfc_mbx_wq_create_wqe_size,
15174                               &wq_create->u.request_1,
15175                               LPFC_WQ_WQE_SIZE_128);
15176                        break;
15177                }
15178                /* Request DPP by default */
15179                bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
15180                bf_set(lpfc_mbx_wq_create_page_size,
15181                       &wq_create->u.request_1,
15182                       (wq->page_size / SLI4_PAGE_SIZE));
15183                page = wq_create->u.request_1.page;
15184                break;
15185        default:
15186                page = wq_create->u.request.page;
15187                break;
15188        }
15189
15190        list_for_each_entry(dmabuf, &wq->page_list, list) {
15191                memset(dmabuf->virt, 0, hw_page_size);
15192                page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15193                page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
15194        }
15195
15196        if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15197                bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15198
15199        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15200        /* The IOCTL status is embedded in the mailbox subheader. */
15201        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15202        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15203        if (shdr_status || shdr_add_status || rc) {
15204                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15205                                "2503 WQ_CREATE mailbox failed with "
15206                                "status x%x add_status x%x, mbx status x%x\n",
15207                                shdr_status, shdr_add_status, rc);
15208                status = -ENXIO;
15209                goto out;
15210        }
15211
15212        if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15213                wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15214                                        &wq_create->u.response);
15215        else
15216                wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15217                                        &wq_create->u.response_1);
15218
15219        if (wq->queue_id == 0xFFFF) {
15220                status = -ENXIO;
15221                goto out;
15222        }
15223
15224        wq->db_format = LPFC_DB_LIST_FORMAT;
15225        if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15226                if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15227                        wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15228                                               &wq_create->u.response);
15229                        if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15230                            (wq->db_format != LPFC_DB_RING_FORMAT)) {
15231                                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15232                                                "3265 WQ[%d] doorbell format "
15233                                                "not supported: x%x\n",
15234                                                wq->queue_id, wq->db_format);
15235                                status = -EINVAL;
15236                                goto out;
15237                        }
15238                        pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15239                                            &wq_create->u.response);
15240                        bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15241                                                                   pci_barset);
15242                        if (!bar_memmap_p) {
15243                                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15244                                                "3263 WQ[%d] failed to memmap "
15245                                                "pci barset:x%x\n",
15246                                                wq->queue_id, pci_barset);
15247                                status = -ENOMEM;
15248                                goto out;
15249                        }
15250                        db_offset = wq_create->u.response.doorbell_offset;
15251                        if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15252                            (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15253                                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15254                                                "3252 WQ[%d] doorbell offset "
15255                                                "not supported: x%x\n",
15256                                                wq->queue_id, db_offset);
15257                                status = -EINVAL;
15258                                goto out;
15259                        }
15260                        wq->db_regaddr = bar_memmap_p + db_offset;
15261                        lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15262                                        "3264 WQ[%d]: barset:x%x, offset:x%x, "
15263                                        "format:x%x\n", wq->queue_id,
15264                                        pci_barset, db_offset, wq->db_format);
15265                } else
15266                        wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15267        } else {
15268                /* Check if DPP was honored by the firmware */
15269                wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15270                                    &wq_create->u.response_1);
15271                if (wq->dpp_enable) {
15272                        pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15273                                            &wq_create->u.response_1);
15274                        bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15275                                                                   pci_barset);
15276                        if (!bar_memmap_p) {
15277                                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15278                                                "3267 WQ[%d] failed to memmap "
15279                                                "pci barset:x%x\n",
15280                                                wq->queue_id, pci_barset);
15281                                status = -ENOMEM;
15282                                goto out;
15283                        }
15284                        db_offset = wq_create->u.response_1.doorbell_offset;
15285                        wq->db_regaddr = bar_memmap_p + db_offset;
15286                        wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15287                                            &wq_create->u.response_1);
15288                        dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15289                                            &wq_create->u.response_1);
15290                        bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15291                                                                   dpp_barset);
15292                        if (!bar_memmap_p) {
15293                                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15294                                                "3268 WQ[%d] failed to memmap "
15295                                                "pci barset:x%x\n",
15296                                                wq->queue_id, dpp_barset);
15297                                status = -ENOMEM;
15298                                goto out;
15299                        }
15300                        dpp_offset = wq_create->u.response_1.dpp_offset;
15301                        wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15302                        lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15303                                        "3271 WQ[%d]: barset:x%x, offset:x%x, "
15304                                        "dpp_id:x%x dpp_barset:x%x "
15305                                        "dpp_offset:x%x\n",
15306                                        wq->queue_id, pci_barset, db_offset,
15307                                        wq->dpp_id, dpp_barset, dpp_offset);
15308
15309                        /* Enable combined writes for DPP aperture */
15310                        pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
15311#ifdef CONFIG_X86
15312                        rc = set_memory_wc(pg_addr, 1);
15313                        if (rc) {
15314                                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15315                                        "3272 Cannot setup Combined "
15316                                        "Write on WQ[%d] - disable DPP\n",
15317                                        wq->queue_id);
15318                                phba->cfg_enable_dpp = 0;
15319                        }
15320#else
15321                        phba->cfg_enable_dpp = 0;
15322#endif
15323                } else
15324                        wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15325        }
15326        wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15327        if (wq->pring == NULL) {
15328                status = -ENOMEM;
15329                goto out;
15330        }
15331        wq->type = LPFC_WQ;
15332        wq->assoc_qid = cq->queue_id;
15333        wq->subtype = subtype;
15334        wq->host_index = 0;
15335        wq->hba_index = 0;
15336        wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
15337
15338        /* link the wq onto the parent cq child list */
15339        list_add_tail(&wq->list, &cq->child_list);
15340out:
15341        mempool_free(mbox, phba->mbox_mem_pool);
15342        return status;
15343}
15344
15345/**
15346 * lpfc_rq_create - Create a Receive Queue on the HBA
15347 * @phba: HBA structure that indicates port to create a queue on.
15348 * @hrq: The queue structure to use to create the header receive queue.
15349 * @drq: The queue structure to use to create the data receive queue.
15350 * @cq: The completion queue to bind this work queue to.
15351 *
15352 * This function creates a receive buffer queue pair , as detailed in @hrq and
15353 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15354 * to the HBA.
15355 *
15356 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15357 * struct is used to get the entry count that is necessary to determine the
15358 * number of pages to use for this queue. The @cq is used to indicate which
15359 * completion queue to bind received buffers that are posted to these queues to.
15360 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15361 * receive queue pair. This function is asynchronous and will wait for the
15362 * mailbox command to finish before continuing.
15363 *
15364 * On success this function will return a zero. If unable to allocate enough
15365 * memory this function will return -ENOMEM. If the queue create mailbox command
15366 * fails this function will return -ENXIO.
15367 **/
15368int
15369lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15370               struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15371{
15372        struct lpfc_mbx_rq_create *rq_create;
15373        struct lpfc_dmabuf *dmabuf;
15374        LPFC_MBOXQ_t *mbox;
15375        int rc, length, status = 0;
15376        uint32_t shdr_status, shdr_add_status;
15377        union lpfc_sli4_cfg_shdr *shdr;
15378        uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15379        void __iomem *bar_memmap_p;
15380        uint32_t db_offset;
15381        uint16_t pci_barset;
15382
15383        /* sanity check on queue memory */
15384        if (!hrq || !drq || !cq)
15385                return -ENODEV;
15386        if (!phba->sli4_hba.pc_sli4_params.supported)
15387                hw_page_size = SLI4_PAGE_SIZE;
15388
15389        if (hrq->entry_count != drq->entry_count)
15390                return -EINVAL;
15391        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15392        if (!mbox)
15393                return -ENOMEM;
15394        length = (sizeof(struct lpfc_mbx_rq_create) -
15395                  sizeof(struct lpfc_sli4_cfg_mhdr));
15396        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15397                         LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15398                         length, LPFC_SLI4_MBX_EMBED);
15399        rq_create = &mbox->u.mqe.un.rq_create;
15400        shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15401        bf_set(lpfc_mbox_hdr_version, &shdr->request,
15402               phba->sli4_hba.pc_sli4_params.rqv);
15403        if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15404                bf_set(lpfc_rq_context_rqe_count_1,
15405                       &rq_create->u.request.context,
15406                       hrq->entry_count);
15407                rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
15408                bf_set(lpfc_rq_context_rqe_size,
15409                       &rq_create->u.request.context,
15410                       LPFC_RQE_SIZE_8);
15411                bf_set(lpfc_rq_context_page_size,
15412                       &rq_create->u.request.context,
15413                       LPFC_RQ_PAGE_SIZE_4096);
15414        } else {
15415                switch (hrq->entry_count) {
15416                default:
15417                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15418                                        "2535 Unsupported RQ count. (%d)\n",
15419                                        hrq->entry_count);
15420                        if (hrq->entry_count < 512) {
15421                                status = -EINVAL;
15422                                goto out;
15423                        }
15424                        /* otherwise default to smallest count (drop through) */
15425                case 512:
15426                        bf_set(lpfc_rq_context_rqe_count,
15427                               &rq_create->u.request.context,
15428                               LPFC_RQ_RING_SIZE_512);
15429                        break;
15430                case 1024:
15431                        bf_set(lpfc_rq_context_rqe_count,
15432                               &rq_create->u.request.context,
15433                               LPFC_RQ_RING_SIZE_1024);
15434                        break;
15435                case 2048:
15436                        bf_set(lpfc_rq_context_rqe_count,
15437                               &rq_create->u.request.context,
15438                               LPFC_RQ_RING_SIZE_2048);
15439                        break;
15440                case 4096:
15441                        bf_set(lpfc_rq_context_rqe_count,
15442                               &rq_create->u.request.context,
15443                               LPFC_RQ_RING_SIZE_4096);
15444                        break;
15445                }
15446                bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15447                       LPFC_HDR_BUF_SIZE);
15448        }
15449        bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15450               cq->queue_id);
15451        bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15452               hrq->page_count);
15453        list_for_each_entry(dmabuf, &hrq->page_list, list) {
15454                memset(dmabuf->virt, 0, hw_page_size);
15455                rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15456                                        putPaddrLow(dmabuf->phys);
15457                rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15458                                        putPaddrHigh(dmabuf->phys);
15459        }
15460        if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15461                bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15462
15463        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15464        /* The IOCTL status is embedded in the mailbox subheader. */
15465        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15466        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15467        if (shdr_status || shdr_add_status || rc) {
15468                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15469                                "2504 RQ_CREATE mailbox failed with "
15470                                "status x%x add_status x%x, mbx status x%x\n",
15471                                shdr_status, shdr_add_status, rc);
15472                status = -ENXIO;
15473                goto out;
15474        }
15475        hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15476        if (hrq->queue_id == 0xFFFF) {
15477                status = -ENXIO;
15478                goto out;
15479        }
15480
15481        if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15482                hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15483                                        &rq_create->u.response);
15484                if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15485                    (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15486                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15487                                        "3262 RQ [%d] doorbell format not "
15488                                        "supported: x%x\n", hrq->queue_id,
15489                                        hrq->db_format);
15490                        status = -EINVAL;
15491                        goto out;
15492                }
15493
15494                pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15495                                    &rq_create->u.response);
15496                bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15497                if (!bar_memmap_p) {
15498                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15499                                        "3269 RQ[%d] failed to memmap pci "
15500                                        "barset:x%x\n", hrq->queue_id,
15501                                        pci_barset);
15502                        status = -ENOMEM;
15503                        goto out;
15504                }
15505
15506                db_offset = rq_create->u.response.doorbell_offset;
15507                if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15508                    (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15509                        lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15510                                        "3270 RQ[%d] doorbell offset not "
15511                                        "supported: x%x\n", hrq->queue_id,
15512                                        db_offset);
15513                        status = -EINVAL;
15514                        goto out;
15515                }
15516                hrq->db_regaddr = bar_memmap_p + db_offset;
15517                lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15518                                "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15519                                "format:x%x\n", hrq->queue_id, pci_barset,
15520                                db_offset, hrq->db_format);
15521        } else {
15522                hrq->db_format = LPFC_DB_RING_FORMAT;
15523                hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15524        }
15525        hrq->type = LPFC_HRQ;
15526        hrq->assoc_qid = cq->queue_id;
15527        hrq->subtype = subtype;
15528        hrq->host_index = 0;
15529        hrq->hba_index = 0;
15530        hrq->entry_repost = LPFC_RQ_REPOST;
15531
15532        /* now create the data queue */
15533        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15534                         LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15535                         length, LPFC_SLI4_MBX_EMBED);
15536        bf_set(lpfc_mbox_hdr_version, &shdr->request,
15537               phba->sli4_hba.pc_sli4_params.rqv);
15538        if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15539                bf_set(lpfc_rq_context_rqe_count_1,
15540                       &rq_create->u.request.context, hrq->entry_count);
15541                if (subtype == LPFC_NVMET)
15542                        rq_create->u.request.context.buffer_size =
15543                                LPFC_NVMET_DATA_BUF_SIZE;
15544                else
15545                        rq_create->u.request.context.buffer_size =
15546                                LPFC_DATA_BUF_SIZE;
15547                bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15548                       LPFC_RQE_SIZE_8);
15549                bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15550                       (PAGE_SIZE/SLI4_PAGE_SIZE));
15551        } else {
15552                switch (drq->entry_count) {
15553                default:
15554                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15555                                        "2536 Unsupported RQ count. (%d)\n",
15556                                        drq->entry_count);
15557                        if (drq->entry_count < 512) {
15558                                status = -EINVAL;
15559                                goto out;
15560                        }
15561                        /* otherwise default to smallest count (drop through) */
15562                case 512:
15563                        bf_set(lpfc_rq_context_rqe_count,
15564                               &rq_create->u.request.context,
15565                               LPFC_RQ_RING_SIZE_512);
15566                        break;
15567                case 1024:
15568                        bf_set(lpfc_rq_context_rqe_count,
15569                               &rq_create->u.request.context,
15570                               LPFC_RQ_RING_SIZE_1024);
15571                        break;
15572                case 2048:
15573                        bf_set(lpfc_rq_context_rqe_count,
15574                               &rq_create->u.request.context,
15575                               LPFC_RQ_RING_SIZE_2048);
15576                        break;
15577                case 4096:
15578                        bf_set(lpfc_rq_context_rqe_count,
15579                               &rq_create->u.request.context,
15580                               LPFC_RQ_RING_SIZE_4096);
15581                        break;
15582                }
15583                if (subtype == LPFC_NVMET)
15584                        bf_set(lpfc_rq_context_buf_size,
15585                               &rq_create->u.request.context,
15586                               LPFC_NVMET_DATA_BUF_SIZE);
15587                else
15588                        bf_set(lpfc_rq_context_buf_size,
15589                               &rq_create->u.request.context,
15590                               LPFC_DATA_BUF_SIZE);
15591        }
15592        bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15593               cq->queue_id);
15594        bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15595               drq->page_count);
15596        list_for_each_entry(dmabuf, &drq->page_list, list) {
15597                rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15598                                        putPaddrLow(dmabuf->phys);
15599                rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15600                                        putPaddrHigh(dmabuf->phys);
15601        }
15602        if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15603                bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15604        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15605        /* The IOCTL status is embedded in the mailbox subheader. */
15606        shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15607        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15608        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15609        if (shdr_status || shdr_add_status || rc) {
15610                status = -ENXIO;
15611                goto out;
15612        }
15613        drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15614        if (drq->queue_id == 0xFFFF) {
15615                status = -ENXIO;
15616                goto out;
15617        }
15618        drq->type = LPFC_DRQ;
15619        drq->assoc_qid = cq->queue_id;
15620        drq->subtype = subtype;
15621        drq->host_index = 0;
15622        drq->hba_index = 0;
15623        drq->entry_repost = LPFC_RQ_REPOST;
15624
15625        /* link the header and data RQs onto the parent cq child list */
15626        list_add_tail(&hrq->list, &cq->child_list);
15627        list_add_tail(&drq->list, &cq->child_list);
15628
15629out:
15630        mempool_free(mbox, phba->mbox_mem_pool);
15631        return status;
15632}
15633
15634/**
15635 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15636 * @phba: HBA structure that indicates port to create a queue on.
15637 * @hrqp: The queue structure array to use to create the header receive queues.
15638 * @drqp: The queue structure array to use to create the data receive queues.
15639 * @cqp: The completion queue array to bind these receive queues to.
15640 *
15641 * This function creates a receive buffer queue pair , as detailed in @hrq and
15642 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15643 * to the HBA.
15644 *
15645 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15646 * struct is used to get the entry count that is necessary to determine the
15647 * number of pages to use for this queue. The @cq is used to indicate which
15648 * completion queue to bind received buffers that are posted to these queues to.
15649 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15650 * receive queue pair. This function is asynchronous and will wait for the
15651 * mailbox command to finish before continuing.
15652 *
15653 * On success this function will return a zero. If unable to allocate enough
15654 * memory this function will return -ENOMEM. If the queue create mailbox command
15655 * fails this function will return -ENXIO.
15656 **/
15657int
15658lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15659                struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15660                uint32_t subtype)
15661{
15662        struct lpfc_queue *hrq, *drq, *cq;
15663        struct lpfc_mbx_rq_create_v2 *rq_create;
15664        struct lpfc_dmabuf *dmabuf;
15665        LPFC_MBOXQ_t *mbox;
15666        int rc, length, alloclen, status = 0;
15667        int cnt, idx, numrq, page_idx = 0;
15668        uint32_t shdr_status, shdr_add_status;
15669        union lpfc_sli4_cfg_shdr *shdr;
15670        uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15671
15672        numrq = phba->cfg_nvmet_mrq;
15673        /* sanity check on array memory */
15674        if (!hrqp || !drqp || !cqp || !numrq)
15675                return -ENODEV;
15676        if (!phba->sli4_hba.pc_sli4_params.supported)
15677                hw_page_size = SLI4_PAGE_SIZE;
15678
15679        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15680        if (!mbox)
15681                return -ENOMEM;
15682
15683        length = sizeof(struct lpfc_mbx_rq_create_v2);
15684        length += ((2 * numrq * hrqp[0]->page_count) *
15685                   sizeof(struct dma_address));
15686
15687        alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15688                                    LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15689                                    LPFC_SLI4_MBX_NEMBED);
15690        if (alloclen < length) {
15691                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15692                                "3099 Allocated DMA memory size (%d) is "
15693                                "less than the requested DMA memory size "
15694                                "(%d)\n", alloclen, length);
15695                status = -ENOMEM;
15696                goto out;
15697        }
15698
15699
15700
15701        rq_create = mbox->sge_array->addr[0];
15702        shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15703
15704        bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15705        cnt = 0;
15706
15707        for (idx = 0; idx < numrq; idx++) {
15708                hrq = hrqp[idx];
15709                drq = drqp[idx];
15710                cq  = cqp[idx];
15711
15712                /* sanity check on queue memory */
15713                if (!hrq || !drq || !cq) {
15714                        status = -ENODEV;
15715                        goto out;
15716                }
15717
15718                if (hrq->entry_count != drq->entry_count) {
15719                        status = -EINVAL;
15720                        goto out;
15721                }
15722
15723                if (idx == 0) {
15724                        bf_set(lpfc_mbx_rq_create_num_pages,
15725                               &rq_create->u.request,
15726                               hrq->page_count);
15727                        bf_set(lpfc_mbx_rq_create_rq_cnt,
15728                               &rq_create->u.request, (numrq * 2));
15729                        bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15730                               1);
15731                        bf_set(lpfc_rq_context_base_cq,
15732                               &rq_create->u.request.context,
15733                               cq->queue_id);
15734                        bf_set(lpfc_rq_context_data_size,
15735                               &rq_create->u.request.context,
15736                               LPFC_NVMET_DATA_BUF_SIZE);
15737                        bf_set(lpfc_rq_context_hdr_size,
15738                               &rq_create->u.request.context,
15739                               LPFC_HDR_BUF_SIZE);
15740                        bf_set(lpfc_rq_context_rqe_count_1,
15741                               &rq_create->u.request.context,
15742                               hrq->entry_count);
15743                        bf_set(lpfc_rq_context_rqe_size,
15744                               &rq_create->u.request.context,
15745                               LPFC_RQE_SIZE_8);
15746                        bf_set(lpfc_rq_context_page_size,
15747                               &rq_create->u.request.context,
15748                               (PAGE_SIZE/SLI4_PAGE_SIZE));
15749                }
15750                rc = 0;
15751                list_for_each_entry(dmabuf, &hrq->page_list, list) {
15752                        memset(dmabuf->virt, 0, hw_page_size);
15753                        cnt = page_idx + dmabuf->buffer_tag;
15754                        rq_create->u.request.page[cnt].addr_lo =
15755                                        putPaddrLow(dmabuf->phys);
15756                        rq_create->u.request.page[cnt].addr_hi =
15757                                        putPaddrHigh(dmabuf->phys);
15758                        rc++;
15759                }
15760                page_idx += rc;
15761
15762                rc = 0;
15763                list_for_each_entry(dmabuf, &drq->page_list, list) {
15764                        memset(dmabuf->virt, 0, hw_page_size);
15765                        cnt = page_idx + dmabuf->buffer_tag;
15766                        rq_create->u.request.page[cnt].addr_lo =
15767                                        putPaddrLow(dmabuf->phys);
15768                        rq_create->u.request.page[cnt].addr_hi =
15769                                        putPaddrHigh(dmabuf->phys);
15770                        rc++;
15771                }
15772                page_idx += rc;
15773
15774                hrq->db_format = LPFC_DB_RING_FORMAT;
15775                hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15776                hrq->type = LPFC_HRQ;
15777                hrq->assoc_qid = cq->queue_id;
15778                hrq->subtype = subtype;
15779                hrq->host_index = 0;
15780                hrq->hba_index = 0;
15781                hrq->entry_repost = LPFC_RQ_REPOST;
15782
15783                drq->db_format = LPFC_DB_RING_FORMAT;
15784                drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15785                drq->type = LPFC_DRQ;
15786                drq->assoc_qid = cq->queue_id;
15787                drq->subtype = subtype;
15788                drq->host_index = 0;
15789                drq->hba_index = 0;
15790                drq->entry_repost = LPFC_RQ_REPOST;
15791
15792                list_add_tail(&hrq->list, &cq->child_list);
15793                list_add_tail(&drq->list, &cq->child_list);
15794        }
15795
15796        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15797        /* The IOCTL status is embedded in the mailbox subheader. */
15798        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15799        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15800        if (shdr_status || shdr_add_status || rc) {
15801                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15802                                "3120 RQ_CREATE mailbox failed with "
15803                                "status x%x add_status x%x, mbx status x%x\n",
15804                                shdr_status, shdr_add_status, rc);
15805                status = -ENXIO;
15806                goto out;
15807        }
15808        rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15809        if (rc == 0xFFFF) {
15810                status = -ENXIO;
15811                goto out;
15812        }
15813
15814        /* Initialize all RQs with associated queue id */
15815        for (idx = 0; idx < numrq; idx++) {
15816                hrq = hrqp[idx];
15817                hrq->queue_id = rc + (2 * idx);
15818                drq = drqp[idx];
15819                drq->queue_id = rc + (2 * idx) + 1;
15820        }
15821
15822out:
15823        lpfc_sli4_mbox_cmd_free(phba, mbox);
15824        return status;
15825}
15826
15827/**
15828 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15829 * @eq: The queue structure associated with the queue to destroy.
15830 *
15831 * This function destroys a queue, as detailed in @eq by sending an mailbox
15832 * command, specific to the type of queue, to the HBA.
15833 *
15834 * The @eq struct is used to get the queue ID of the queue to destroy.
15835 *
15836 * On success this function will return a zero. If the queue destroy mailbox
15837 * command fails this function will return -ENXIO.
15838 **/
15839int
15840lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15841{
15842        LPFC_MBOXQ_t *mbox;
15843        int rc, length, status = 0;
15844        uint32_t shdr_status, shdr_add_status;
15845        union lpfc_sli4_cfg_shdr *shdr;
15846
15847        /* sanity check on queue memory */
15848        if (!eq)
15849                return -ENODEV;
15850        mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15851        if (!mbox)
15852                return -ENOMEM;
15853        length = (sizeof(struct lpfc_mbx_eq_destroy) -
15854                  sizeof(struct lpfc_sli4_cfg_mhdr));
15855        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15856                         LPFC_MBOX_OPCODE_EQ_DESTROY,
15857                         length, LPFC_SLI4_MBX_EMBED);
15858        bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15859               eq->queue_id);
15860        mbox->vport = eq->phba->pport;
15861        mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15862
15863        rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15864        /* The IOCTL status is embedded in the mailbox subheader. */
15865        shdr = (union lpfc_sli4_cfg_shdr *)
15866                &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15867        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15868        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15869        if (shdr_status || shdr_add_status || rc) {
15870                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15871                                "2505 EQ_DESTROY mailbox failed with "
15872                                "status x%x add_status x%x, mbx status x%x\n",
15873                                shdr_status, shdr_add_status, rc);
15874                status = -ENXIO;
15875        }
15876
15877        /* Remove eq from any list */
15878        list_del_init(&eq->list);
15879        mempool_free(mbox, eq->phba->mbox_mem_pool);
15880        return status;
15881}
15882
15883/**
15884 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15885 * @cq: The queue structure associated with the queue to destroy.
15886 *
15887 * This function destroys a queue, as detailed in @cq by sending an mailbox
15888 * command, specific to the type of queue, to the HBA.
15889 *
15890 * The @cq struct is used to get the queue ID of the queue to destroy.
15891 *
15892 * On success this function will return a zero. If the queue destroy mailbox
15893 * command fails this function will return -ENXIO.
15894 **/
15895int
15896lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15897{
15898        LPFC_MBOXQ_t *mbox;
15899        int rc, length, status = 0;
15900        uint32_t shdr_status, shdr_add_status;
15901        union lpfc_sli4_cfg_shdr *shdr;
15902
15903        /* sanity check on queue memory */
15904        if (!cq)
15905                return -ENODEV;
15906        mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15907        if (!mbox)
15908                return -ENOMEM;
15909        length = (sizeof(struct lpfc_mbx_cq_destroy) -
15910                  sizeof(struct lpfc_sli4_cfg_mhdr));
15911        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15912                         LPFC_MBOX_OPCODE_CQ_DESTROY,
15913                         length, LPFC_SLI4_MBX_EMBED);
15914        bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15915               cq->queue_id);
15916        mbox->vport = cq->phba->pport;
15917        mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15918        rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15919        /* The IOCTL status is embedded in the mailbox subheader. */
15920        shdr = (union lpfc_sli4_cfg_shdr *)
15921                &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15922        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15923        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15924        if (shdr_status || shdr_add_status || rc) {
15925                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15926                                "2506 CQ_DESTROY mailbox failed with "
15927                                "status x%x add_status x%x, mbx status x%x\n",
15928                                shdr_status, shdr_add_status, rc);
15929                status = -ENXIO;
15930        }
15931        /* Remove cq from any list */
15932        list_del_init(&cq->list);
15933        mempool_free(mbox, cq->phba->mbox_mem_pool);
15934        return status;
15935}
15936
15937/**
15938 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15939 * @qm: The queue structure associated with the queue to destroy.
15940 *
15941 * This function destroys a queue, as detailed in @mq by sending an mailbox
15942 * command, specific to the type of queue, to the HBA.
15943 *
15944 * The @mq struct is used to get the queue ID of the queue to destroy.
15945 *
15946 * On success this function will return a zero. If the queue destroy mailbox
15947 * command fails this function will return -ENXIO.
15948 **/
15949int
15950lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15951{
15952        LPFC_MBOXQ_t *mbox;
15953        int rc, length, status = 0;
15954        uint32_t shdr_status, shdr_add_status;
15955        union lpfc_sli4_cfg_shdr *shdr;
15956
15957        /* sanity check on queue memory */
15958        if (!mq)
15959                return -ENODEV;
15960        mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15961        if (!mbox)
15962                return -ENOMEM;
15963        length = (sizeof(struct lpfc_mbx_mq_destroy) -
15964                  sizeof(struct lpfc_sli4_cfg_mhdr));
15965        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15966                         LPFC_MBOX_OPCODE_MQ_DESTROY,
15967                         length, LPFC_SLI4_MBX_EMBED);
15968        bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15969               mq->queue_id);
15970        mbox->vport = mq->phba->pport;
15971        mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15972        rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15973        /* The IOCTL status is embedded in the mailbox subheader. */
15974        shdr = (union lpfc_sli4_cfg_shdr *)
15975                &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15976        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15977        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15978        if (shdr_status || shdr_add_status || rc) {
15979                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15980                                "2507 MQ_DESTROY mailbox failed with "
15981                                "status x%x add_status x%x, mbx status x%x\n",
15982                                shdr_status, shdr_add_status, rc);
15983                status = -ENXIO;
15984        }
15985        /* Remove mq from any list */
15986        list_del_init(&mq->list);
15987        mempool_free(mbox, mq->phba->mbox_mem_pool);
15988        return status;
15989}
15990
15991/**
15992 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15993 * @wq: The queue structure associated with the queue to destroy.
15994 *
15995 * This function destroys a queue, as detailed in @wq by sending an mailbox
15996 * command, specific to the type of queue, to the HBA.
15997 *
15998 * The @wq struct is used to get the queue ID of the queue to destroy.
15999 *
16000 * On success this function will return a zero. If the queue destroy mailbox
16001 * command fails this function will return -ENXIO.
16002 **/
16003int
16004lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16005{
16006        LPFC_MBOXQ_t *mbox;
16007        int rc, length, status = 0;
16008        uint32_t shdr_status, shdr_add_status;
16009        union lpfc_sli4_cfg_shdr *shdr;
16010
16011        /* sanity check on queue memory */
16012        if (!wq)
16013                return -ENODEV;
16014        mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16015        if (!mbox)
16016                return -ENOMEM;
16017        length = (sizeof(struct lpfc_mbx_wq_destroy) -
16018                  sizeof(struct lpfc_sli4_cfg_mhdr));
16019        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16020                         LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16021                         length, LPFC_SLI4_MBX_EMBED);
16022        bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16023               wq->queue_id);
16024        mbox->vport = wq->phba->pport;
16025        mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16026        rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16027        shdr = (union lpfc_sli4_cfg_shdr *)
16028                &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16029        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16030        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16031        if (shdr_status || shdr_add_status || rc) {
16032                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16033                                "2508 WQ_DESTROY mailbox failed with "
16034                                "status x%x add_status x%x, mbx status x%x\n",
16035                                shdr_status, shdr_add_status, rc);
16036                status = -ENXIO;
16037        }
16038        /* Remove wq from any list */
16039        list_del_init(&wq->list);
16040        kfree(wq->pring);
16041        wq->pring = NULL;
16042        mempool_free(mbox, wq->phba->mbox_mem_pool);
16043        return status;
16044}
16045
16046/**
16047 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16048 * @rq: The queue structure associated with the queue to destroy.
16049 *
16050 * This function destroys a queue, as detailed in @rq by sending an mailbox
16051 * command, specific to the type of queue, to the HBA.
16052 *
16053 * The @rq struct is used to get the queue ID of the queue to destroy.
16054 *
16055 * On success this function will return a zero. If the queue destroy mailbox
16056 * command fails this function will return -ENXIO.
16057 **/
16058int
16059lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16060                struct lpfc_queue *drq)
16061{
16062        LPFC_MBOXQ_t *mbox;
16063        int rc, length, status = 0;
16064        uint32_t shdr_status, shdr_add_status;
16065        union lpfc_sli4_cfg_shdr *shdr;
16066
16067        /* sanity check on queue memory */
16068        if (!hrq || !drq)
16069                return -ENODEV;
16070        mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16071        if (!mbox)
16072                return -ENOMEM;
16073        length = (sizeof(struct lpfc_mbx_rq_destroy) -
16074                  sizeof(struct lpfc_sli4_cfg_mhdr));
16075        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16076                         LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16077                         length, LPFC_SLI4_MBX_EMBED);
16078        bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16079               hrq->queue_id);
16080        mbox->vport = hrq->phba->pport;
16081        mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16082        rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16083        /* The IOCTL status is embedded in the mailbox subheader. */
16084        shdr = (union lpfc_sli4_cfg_shdr *)
16085                &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16086        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16087        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16088        if (shdr_status || shdr_add_status || rc) {
16089                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16090                                "2509 RQ_DESTROY mailbox failed with "
16091                                "status x%x add_status x%x, mbx status x%x\n",
16092                                shdr_status, shdr_add_status, rc);
16093                if (rc != MBX_TIMEOUT)
16094                        mempool_free(mbox, hrq->phba->mbox_mem_pool);
16095                return -ENXIO;
16096        }
16097        bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16098               drq->queue_id);
16099        rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16100        shdr = (union lpfc_sli4_cfg_shdr *)
16101                &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16102        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16103        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16104        if (shdr_status || shdr_add_status || rc) {
16105                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16106                                "2510 RQ_DESTROY mailbox failed with "
16107                                "status x%x add_status x%x, mbx status x%x\n",
16108                                shdr_status, shdr_add_status, rc);
16109                status = -ENXIO;
16110        }
16111        list_del_init(&hrq->list);
16112        list_del_init(&drq->list);
16113        mempool_free(mbox, hrq->phba->mbox_mem_pool);
16114        return status;
16115}
16116
16117/**
16118 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16119 * @phba: The virtual port for which this call being executed.
16120 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16121 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16122 * @xritag: the xritag that ties this io to the SGL pages.
16123 *
16124 * This routine will post the sgl pages for the IO that has the xritag
16125 * that is in the iocbq structure. The xritag is assigned during iocbq
16126 * creation and persists for as long as the driver is loaded.
16127 * if the caller has fewer than 256 scatter gather segments to map then
16128 * pdma_phys_addr1 should be 0.
16129 * If the caller needs to map more than 256 scatter gather segment then
16130 * pdma_phys_addr1 should be a valid physical address.
16131 * physical address for SGLs must be 64 byte aligned.
16132 * If you are going to map 2 SGL's then the first one must have 256 entries
16133 * the second sgl can have between 1 and 256 entries.
16134 *
16135 * Return codes:
16136 *      0 - Success
16137 *      -ENXIO, -ENOMEM - Failure
16138 **/
16139int
16140lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16141                dma_addr_t pdma_phys_addr0,
16142                dma_addr_t pdma_phys_addr1,
16143                uint16_t xritag)
16144{
16145        struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16146        LPFC_MBOXQ_t *mbox;
16147        int rc;
16148        uint32_t shdr_status, shdr_add_status;
16149        uint32_t mbox_tmo;
16150        union lpfc_sli4_cfg_shdr *shdr;
16151
16152        if (xritag == NO_XRI) {
16153                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16154                                "0364 Invalid param:\n");
16155                return -EINVAL;
16156        }
16157
16158        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16159        if (!mbox)
16160                return -ENOMEM;
16161
16162        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16163                        LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16164                        sizeof(struct lpfc_mbx_post_sgl_pages) -
16165                        sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16166
16167        post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16168                                &mbox->u.mqe.un.post_sgl_pages;
16169        bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16170        bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16171
16172        post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16173                                cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16174        post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16175                                cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16176
16177        post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16178                                cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16179        post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16180                                cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16181        if (!phba->sli4_hba.intr_enable)
16182                rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16183        else {
16184                mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16185                rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16186        }
16187        /* The IOCTL status is embedded in the mailbox subheader. */
16188        shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16189        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16190        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16191        if (rc != MBX_TIMEOUT)
16192                mempool_free(mbox, phba->mbox_mem_pool);
16193        if (shdr_status || shdr_add_status || rc) {
16194                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16195                                "2511 POST_SGL mailbox failed with "
16196                                "status x%x add_status x%x, mbx status x%x\n",
16197                                shdr_status, shdr_add_status, rc);
16198        }
16199        return 0;
16200}
16201
16202/**
16203 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
16204 * @phba: pointer to lpfc hba data structure.
16205 *
16206 * This routine is invoked to post rpi header templates to the
16207 * HBA consistent with the SLI-4 interface spec.  This routine
16208 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16209 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
16210 *
16211 * Returns
16212 *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16213 *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
16214 **/
16215static uint16_t
16216lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16217{
16218        unsigned long xri;
16219
16220        /*
16221         * Fetch the next logical xri.  Because this index is logical,
16222         * the driver starts at 0 each time.
16223         */
16224        spin_lock_irq(&phba->hbalock);
16225        xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16226                                 phba->sli4_hba.max_cfg_param.max_xri, 0);
16227        if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16228                spin_unlock_irq(&phba->hbalock);
16229                return NO_XRI;
16230        } else {
16231                set_bit(xri, phba->sli4_hba.xri_bmask);
16232                phba->sli4_hba.max_cfg_param.xri_used++;
16233        }
16234        spin_unlock_irq(&phba->hbalock);
16235        return xri;
16236}
16237
16238/**
16239 * lpfc_sli4_free_xri - Release an xri for reuse.
16240 * @phba: pointer to lpfc hba data structure.
16241 *
16242 * This routine is invoked to release an xri to the pool of
16243 * available rpis maintained by the driver.
16244 **/
16245static void
16246__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16247{
16248        if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
16249                phba->sli4_hba.max_cfg_param.xri_used--;
16250        }
16251}
16252
16253/**
16254 * lpfc_sli4_free_xri - Release an xri for reuse.
16255 * @phba: pointer to lpfc hba data structure.
16256 *
16257 * This routine is invoked to release an xri to the pool of
16258 * available rpis maintained by the driver.
16259 **/
16260void
16261lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16262{
16263        spin_lock_irq(&phba->hbalock);
16264        __lpfc_sli4_free_xri(phba, xri);
16265        spin_unlock_irq(&phba->hbalock);
16266}
16267
16268/**
16269 * lpfc_sli4_next_xritag - Get an xritag for the io
16270 * @phba: Pointer to HBA context object.
16271 *
16272 * This function gets an xritag for the iocb. If there is no unused xritag
16273 * it will return 0xffff.
16274 * The function returns the allocated xritag if successful, else returns zero.
16275 * Zero is not a valid xritag.
16276 * The caller is not required to hold any lock.
16277 **/
16278uint16_t
16279lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16280{
16281        uint16_t xri_index;
16282
16283        xri_index = lpfc_sli4_alloc_xri(phba);
16284        if (xri_index == NO_XRI)
16285                lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16286                                "2004 Failed to allocate XRI.last XRITAG is %d"
16287                                " Max XRI is %d, Used XRI is %d\n",
16288                                xri_index,
16289                                phba->sli4_hba.max_cfg_param.max_xri,
16290                                phba->sli4_hba.max_cfg_param.xri_used);
16291        return xri_index;
16292}
16293
16294/**
16295 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
16296 * @phba: pointer to lpfc hba data structure.
16297 * @post_sgl_list: pointer to els sgl entry list.
16298 * @count: number of els sgl entries on the list.
16299 *
16300 * This routine is invoked to post a block of driver's sgl pages to the
16301 * HBA using non-embedded mailbox command. No Lock is held. This routine
16302 * is only called when the driver is loading and after all IO has been
16303 * stopped.
16304 **/
16305static int
16306lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
16307                            struct list_head *post_sgl_list,
16308                            int post_cnt)
16309{
16310        struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
16311        struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16312        struct sgl_page_pairs *sgl_pg_pairs;
16313        void *viraddr;
16314        LPFC_MBOXQ_t *mbox;
16315        uint32_t reqlen, alloclen, pg_pairs;
16316        uint32_t mbox_tmo;
16317        uint16_t xritag_start = 0;
16318        int rc = 0;
16319        uint32_t shdr_status, shdr_add_status;
16320        union lpfc_sli4_cfg_shdr *shdr;
16321
16322        reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
16323                 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
16324        if (reqlen > SLI4_PAGE_SIZE) {
16325                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16326                                "2559 Block sgl registration required DMA "
16327                                "size (%d) great than a page\n", reqlen);
16328                return -ENOMEM;
16329        }
16330
16331        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16332        if (!mbox)
16333                return -ENOMEM;
16334
16335        /* Allocate DMA memory and set up the non-embedded mailbox command */
16336        alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16337                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16338                         LPFC_SLI4_MBX_NEMBED);
16339
16340        if (alloclen < reqlen) {
16341                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16342                                "0285 Allocated DMA memory size (%d) is "
16343                                "less than the requested DMA memory "
16344                                "size (%d)\n", alloclen, reqlen);
16345                lpfc_sli4_mbox_cmd_free(phba, mbox);
16346                return -ENOMEM;
16347        }
16348        /* Set up the SGL pages in the non-embedded DMA pages */
16349        viraddr = mbox->sge_array->addr[0];
16350        sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16351        sgl_pg_pairs = &sgl->sgl_pg_pairs;
16352
16353        pg_pairs = 0;
16354        list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
16355                /* Set up the sge entry */
16356                sgl_pg_pairs->sgl_pg0_addr_lo =
16357                                cpu_to_le32(putPaddrLow(sglq_entry->phys));
16358                sgl_pg_pairs->sgl_pg0_addr_hi =
16359                                cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16360                sgl_pg_pairs->sgl_pg1_addr_lo =
16361                                cpu_to_le32(putPaddrLow(0));
16362                sgl_pg_pairs->sgl_pg1_addr_hi =
16363                                cpu_to_le32(putPaddrHigh(0));
16364
16365                /* Keep the first xritag on the list */
16366                if (pg_pairs == 0)
16367                        xritag_start = sglq_entry->sli4_xritag;
16368                sgl_pg_pairs++;
16369                pg_pairs++;
16370        }
16371
16372        /* Complete initialization and perform endian conversion. */
16373        bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16374        bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
16375        sgl->word0 = cpu_to_le32(sgl->word0);
16376
16377        if (!phba->sli4_hba.intr_enable)
16378                rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16379        else {
16380                mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16381                rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16382        }
16383        shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16384        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16385        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16386        if (rc != MBX_TIMEOUT)
16387                lpfc_sli4_mbox_cmd_free(phba, mbox);
16388        if (shdr_status || shdr_add_status || rc) {
16389                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16390                                "2513 POST_SGL_BLOCK mailbox command failed "
16391                                "status x%x add_status x%x mbx status x%x\n",
16392                                shdr_status, shdr_add_status, rc);
16393                rc = -ENXIO;
16394        }
16395        return rc;
16396}
16397
16398/**
16399 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16400 * @phba: pointer to lpfc hba data structure.
16401 * @sblist: pointer to scsi buffer list.
16402 * @count: number of scsi buffers on the list.
16403 *
16404 * This routine is invoked to post a block of @count scsi sgl pages from a
16405 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16406 * No Lock is held.
16407 *
16408 **/
16409int
16410lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16411                              struct list_head *sblist,
16412                              int count)
16413{
16414        struct lpfc_scsi_buf *psb;
16415        struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16416        struct sgl_page_pairs *sgl_pg_pairs;
16417        void *viraddr;
16418        LPFC_MBOXQ_t *mbox;
16419        uint32_t reqlen, alloclen, pg_pairs;
16420        uint32_t mbox_tmo;
16421        uint16_t xritag_start = 0;
16422        int rc = 0;
16423        uint32_t shdr_status, shdr_add_status;
16424        dma_addr_t pdma_phys_bpl1;
16425        union lpfc_sli4_cfg_shdr *shdr;
16426
16427        /* Calculate the requested length of the dma memory */
16428        reqlen = count * sizeof(struct sgl_page_pairs) +
16429                 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
16430        if (reqlen > SLI4_PAGE_SIZE) {
16431                lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16432                                "0217 Block sgl registration required DMA "
16433                                "size (%d) great than a page\n", reqlen);
16434                return -ENOMEM;
16435        }
16436        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16437        if (!mbox) {
16438                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16439                                "0283 Failed to allocate mbox cmd memory\n");
16440                return -ENOMEM;
16441        }
16442
16443        /* Allocate DMA memory and set up the non-embedded mailbox command */
16444        alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16445                                LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16446                                LPFC_SLI4_MBX_NEMBED);
16447
16448        if (alloclen < reqlen) {
16449                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16450                                "2561 Allocated DMA memory size (%d) is "
16451                                "less than the requested DMA memory "
16452                                "size (%d)\n", alloclen, reqlen);
16453                lpfc_sli4_mbox_cmd_free(phba, mbox);
16454                return -ENOMEM;
16455        }
16456
16457        /* Get the first SGE entry from the non-embedded DMA memory */
16458        viraddr = mbox->sge_array->addr[0];
16459
16460        /* Set up the SGL pages in the non-embedded DMA pages */
16461        sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16462        sgl_pg_pairs = &sgl->sgl_pg_pairs;
16463
16464        pg_pairs = 0;
16465        list_for_each_entry(psb, sblist, list) {
16466                /* Set up the sge entry */
16467                sgl_pg_pairs->sgl_pg0_addr_lo =
16468                        cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16469                sgl_pg_pairs->sgl_pg0_addr_hi =
16470                        cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16471                if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16472                        pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16473                else
16474                        pdma_phys_bpl1 = 0;
16475                sgl_pg_pairs->sgl_pg1_addr_lo =
16476                        cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16477                sgl_pg_pairs->sgl_pg1_addr_hi =
16478                        cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16479                /* Keep the first xritag on the list */
16480                if (pg_pairs == 0)
16481                        xritag_start = psb->cur_iocbq.sli4_xritag;
16482                sgl_pg_pairs++;
16483                pg_pairs++;
16484        }
16485        bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16486        bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16487        /* Perform endian conversion if necessary */
16488        sgl->word0 = cpu_to_le32(sgl->word0);
16489
16490        if (!phba->sli4_hba.intr_enable)
16491                rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16492        else {
16493                mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16494                rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16495        }
16496        shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16497        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16498        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16499        if (rc != MBX_TIMEOUT)
16500                lpfc_sli4_mbox_cmd_free(phba, mbox);
16501        if (shdr_status || shdr_add_status || rc) {
16502                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16503                                "2564 POST_SGL_BLOCK mailbox command failed "
16504                                "status x%x add_status x%x mbx status x%x\n",
16505                                shdr_status, shdr_add_status, rc);
16506                rc = -ENXIO;
16507        }
16508        return rc;
16509}
16510
16511/**
16512 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16513 * @phba: pointer to lpfc_hba struct that the frame was received on
16514 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16515 *
16516 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16517 * valid type of frame that the LPFC driver will handle. This function will
16518 * return a zero if the frame is a valid frame or a non zero value when the
16519 * frame does not pass the check.
16520 **/
16521static int
16522lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16523{
16524        /*  make rctl_names static to save stack space */
16525        struct fc_vft_header *fc_vft_hdr;
16526        uint32_t *header = (uint32_t *) fc_hdr;
16527
16528#define FC_RCTL_MDS_DIAGS       0xF4
16529
16530        switch (fc_hdr->fh_r_ctl) {
16531        case FC_RCTL_DD_UNCAT:          /* uncategorized information */
16532        case FC_RCTL_DD_SOL_DATA:       /* solicited data */
16533        case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
16534        case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
16535        case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
16536        case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
16537        case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
16538        case FC_RCTL_DD_CMD_STATUS:     /* command status */
16539        case FC_RCTL_ELS_REQ:   /* extended link services request */
16540        case FC_RCTL_ELS_REP:   /* extended link services reply */
16541        case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
16542        case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
16543        case FC_RCTL_BA_NOP:    /* basic link service NOP */
16544        case FC_RCTL_BA_ABTS:   /* basic link service abort */
16545        case FC_RCTL_BA_RMC:    /* remove connection */
16546        case FC_RCTL_BA_ACC:    /* basic accept */
16547        case FC_RCTL_BA_RJT:    /* basic reject */
16548        case FC_RCTL_BA_PRMT:
16549        case FC_RCTL_ACK_1:     /* acknowledge_1 */
16550        case FC_RCTL_ACK_0:     /* acknowledge_0 */
16551        case FC_RCTL_P_RJT:     /* port reject */
16552        case FC_RCTL_F_RJT:     /* fabric reject */
16553        case FC_RCTL_P_BSY:     /* port busy */
16554        case FC_RCTL_F_BSY:     /* fabric busy to data frame */
16555        case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
16556        case FC_RCTL_LCR:       /* link credit reset */
16557        case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
16558        case FC_RCTL_END:       /* end */
16559                break;
16560        case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
16561                fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16562                fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16563                return lpfc_fc_frame_check(phba, fc_hdr);
16564        default:
16565                goto drop;
16566        }
16567
16568#define FC_TYPE_VENDOR_UNIQUE   0xFF
16569
16570        switch (fc_hdr->fh_type) {
16571        case FC_TYPE_BLS:
16572        case FC_TYPE_ELS:
16573        case FC_TYPE_FCP:
16574        case FC_TYPE_CT:
16575        case FC_TYPE_NVME:
16576        case FC_TYPE_VENDOR_UNIQUE:
16577                break;
16578        case FC_TYPE_IP:
16579        case FC_TYPE_ILS:
16580        default:
16581                goto drop;
16582        }
16583
16584        lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
16585                        "2538 Received frame rctl:x%x, type:x%x, "
16586                        "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
16587                        fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16588                        be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16589                        be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16590                        be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16591                        be32_to_cpu(header[6]));
16592        return 0;
16593drop:
16594        lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
16595                        "2539 Dropped frame rctl:x%x type:x%x\n",
16596                        fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16597        return 1;
16598}
16599
16600/**
16601 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16602 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16603 *
16604 * This function processes the FC header to retrieve the VFI from the VF
16605 * header, if one exists. This function will return the VFI if one exists
16606 * or 0 if no VSAN Header exists.
16607 **/
16608static uint32_t
16609lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16610{
16611        struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16612
16613        if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16614                return 0;
16615        return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16616}
16617
16618/**
16619 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16620 * @phba: Pointer to the HBA structure to search for the vport on
16621 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16622 * @fcfi: The FC Fabric ID that the frame came from
16623 *
16624 * This function searches the @phba for a vport that matches the content of the
16625 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16626 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16627 * returns the matching vport pointer or NULL if unable to match frame to a
16628 * vport.
16629 **/
16630static struct lpfc_vport *
16631lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
16632                       uint16_t fcfi, uint32_t did)
16633{
16634        struct lpfc_vport **vports;
16635        struct lpfc_vport *vport = NULL;
16636        int i;
16637
16638        if (did == Fabric_DID)
16639                return phba->pport;
16640        if ((phba->pport->fc_flag & FC_PT2PT) &&
16641                !(phba->link_state == LPFC_HBA_READY))
16642                return phba->pport;
16643
16644        vports = lpfc_create_vport_work_array(phba);
16645        if (vports != NULL) {
16646                for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16647                        if (phba->fcf.fcfi == fcfi &&
16648                            vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16649                            vports[i]->fc_myDID == did) {
16650                                vport = vports[i];
16651                                break;
16652                        }
16653                }
16654        }
16655        lpfc_destroy_vport_work_array(phba, vports);
16656        return vport;
16657}
16658
16659/**
16660 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16661 * @vport: The vport to work on.
16662 *
16663 * This function updates the receive sequence time stamp for this vport. The
16664 * receive sequence time stamp indicates the time that the last frame of the
16665 * the sequence that has been idle for the longest amount of time was received.
16666 * the driver uses this time stamp to indicate if any received sequences have
16667 * timed out.
16668 **/
16669static void
16670lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16671{
16672        struct lpfc_dmabuf *h_buf;
16673        struct hbq_dmabuf *dmabuf = NULL;
16674
16675        /* get the oldest sequence on the rcv list */
16676        h_buf = list_get_first(&vport->rcv_buffer_list,
16677                               struct lpfc_dmabuf, list);
16678        if (!h_buf)
16679                return;
16680        dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16681        vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16682}
16683
16684/**
16685 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16686 * @vport: The vport that the received sequences were sent to.
16687 *
16688 * This function cleans up all outstanding received sequences. This is called
16689 * by the driver when a link event or user action invalidates all the received
16690 * sequences.
16691 **/
16692void
16693lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16694{
16695        struct lpfc_dmabuf *h_buf, *hnext;
16696        struct lpfc_dmabuf *d_buf, *dnext;
16697        struct hbq_dmabuf *dmabuf = NULL;
16698
16699        /* start with the oldest sequence on the rcv list */
16700        list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16701                dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16702                list_del_init(&dmabuf->hbuf.list);
16703                list_for_each_entry_safe(d_buf, dnext,
16704                                         &dmabuf->dbuf.list, list) {
16705                        list_del_init(&d_buf->list);
16706                        lpfc_in_buf_free(vport->phba, d_buf);
16707                }
16708                lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16709        }
16710}
16711
16712/**
16713 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16714 * @vport: The vport that the received sequences were sent to.
16715 *
16716 * This function determines whether any received sequences have timed out by
16717 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16718 * indicates that there is at least one timed out sequence this routine will
16719 * go through the received sequences one at a time from most inactive to most
16720 * active to determine which ones need to be cleaned up. Once it has determined
16721 * that a sequence needs to be cleaned up it will simply free up the resources
16722 * without sending an abort.
16723 **/
16724void
16725lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16726{
16727        struct lpfc_dmabuf *h_buf, *hnext;
16728        struct lpfc_dmabuf *d_buf, *dnext;
16729        struct hbq_dmabuf *dmabuf = NULL;
16730        unsigned long timeout;
16731        int abort_count = 0;
16732
16733        timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16734                   vport->rcv_buffer_time_stamp);
16735        if (list_empty(&vport->rcv_buffer_list) ||
16736            time_before(jiffies, timeout))
16737                return;
16738        /* start with the oldest sequence on the rcv list */
16739        list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16740                dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16741                timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16742                           dmabuf->time_stamp);
16743                if (time_before(jiffies, timeout))
16744                        break;
16745                abort_count++;
16746                list_del_init(&dmabuf->hbuf.list);
16747                list_for_each_entry_safe(d_buf, dnext,
16748                                         &dmabuf->dbuf.list, list) {
16749                        list_del_init(&d_buf->list);
16750                        lpfc_in_buf_free(vport->phba, d_buf);
16751                }
16752                lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16753        }
16754        if (abort_count)
16755                lpfc_update_rcv_time_stamp(vport);
16756}
16757
16758/**
16759 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16760 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16761 *
16762 * This function searches through the existing incomplete sequences that have
16763 * been sent to this @vport. If the frame matches one of the incomplete
16764 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16765 * make up that sequence. If no sequence is found that matches this frame then
16766 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16767 * This function returns a pointer to the first dmabuf in the sequence list that
16768 * the frame was linked to.
16769 **/
16770static struct hbq_dmabuf *
16771lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16772{
16773        struct fc_frame_header *new_hdr;
16774        struct fc_frame_header *temp_hdr;
16775        struct lpfc_dmabuf *d_buf;
16776        struct lpfc_dmabuf *h_buf;
16777        struct hbq_dmabuf *seq_dmabuf = NULL;
16778        struct hbq_dmabuf *temp_dmabuf = NULL;
16779        uint8_t found = 0;
16780
16781        INIT_LIST_HEAD(&dmabuf->dbuf.list);
16782        dmabuf->time_stamp = jiffies;
16783        new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16784
16785        /* Use the hdr_buf to find the sequence that this frame belongs to */
16786        list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16787                temp_hdr = (struct fc_frame_header *)h_buf->virt;
16788                if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16789                    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16790                    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16791                        continue;
16792                /* found a pending sequence that matches this frame */
16793                seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16794                break;
16795        }
16796        if (!seq_dmabuf) {
16797                /*
16798                 * This indicates first frame received for this sequence.
16799                 * Queue the buffer on the vport's rcv_buffer_list.
16800                 */
16801                list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16802                lpfc_update_rcv_time_stamp(vport);
16803                return dmabuf;
16804        }
16805        temp_hdr = seq_dmabuf->hbuf.virt;
16806        if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16807                be16_to_cpu(temp_hdr->fh_seq_cnt)) {
16808                list_del_init(&seq_dmabuf->hbuf.list);
16809                list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16810                list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16811                lpfc_update_rcv_time_stamp(vport);
16812                return dmabuf;
16813        }
16814        /* move this sequence to the tail to indicate a young sequence */
16815        list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16816        seq_dmabuf->time_stamp = jiffies;
16817        lpfc_update_rcv_time_stamp(vport);
16818        if (list_empty(&seq_dmabuf->dbuf.list)) {
16819                temp_hdr = dmabuf->hbuf.virt;
16820                list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16821                return seq_dmabuf;
16822        }
16823        /* find the correct place in the sequence to insert this frame */
16824        d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16825        while (!found) {
16826                temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16827                temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16828                /*
16829                 * If the frame's sequence count is greater than the frame on
16830                 * the list then insert the frame right after this frame
16831                 */
16832                if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16833                        be16_to_cpu(temp_hdr->fh_seq_cnt)) {
16834                        list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
16835                        found = 1;
16836                        break;
16837                }
16838
16839                if (&d_buf->list == &seq_dmabuf->dbuf.list)
16840                        break;
16841                d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
16842        }
16843
16844        if (found)
16845                return seq_dmabuf;
16846        return NULL;
16847}
16848
16849/**
16850 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16851 * @vport: pointer to a vitural port
16852 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16853 *
16854 * This function tries to abort from the partially assembed sequence, described
16855 * by the information from basic abbort @dmabuf. It checks to see whether such
16856 * partially assembled sequence held by the driver. If so, it shall free up all
16857 * the frames from the partially assembled sequence.
16858 *
16859 * Return
16860 * true  -- if there is matching partially assembled sequence present and all
16861 *          the frames freed with the sequence;
16862 * false -- if there is no matching partially assembled sequence present so
16863 *          nothing got aborted in the lower layer driver
16864 **/
16865static bool
16866lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16867                            struct hbq_dmabuf *dmabuf)
16868{
16869        struct fc_frame_header *new_hdr;
16870        struct fc_frame_header *temp_hdr;
16871        struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16872        struct hbq_dmabuf *seq_dmabuf = NULL;
16873
16874        /* Use the hdr_buf to find the sequence that matches this frame */
16875        INIT_LIST_HEAD(&dmabuf->dbuf.list);
16876        INIT_LIST_HEAD(&dmabuf->hbuf.list);
16877        new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16878        list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16879                temp_hdr = (struct fc_frame_header *)h_buf->virt;
16880                if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16881                    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16882                    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16883                        continue;
16884                /* found a pending sequence that matches this frame */
16885                seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16886                break;
16887        }
16888
16889        /* Free up all the frames from the partially assembled sequence */
16890        if (seq_dmabuf) {
16891                list_for_each_entry_safe(d_buf, n_buf,
16892                                         &seq_dmabuf->dbuf.list, list) {
16893                        list_del_init(&d_buf->list);
16894                        lpfc_in_buf_free(vport->phba, d_buf);
16895                }
16896                return true;
16897        }
16898        return false;
16899}
16900
16901/**
16902 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16903 * @vport: pointer to a vitural port
16904 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16905 *
16906 * This function tries to abort from the assembed sequence from upper level
16907 * protocol, described by the information from basic abbort @dmabuf. It
16908 * checks to see whether such pending context exists at upper level protocol.
16909 * If so, it shall clean up the pending context.
16910 *
16911 * Return
16912 * true  -- if there is matching pending context of the sequence cleaned
16913 *          at ulp;
16914 * false -- if there is no matching pending context of the sequence present
16915 *          at ulp.
16916 **/
16917static bool
16918lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16919{
16920        struct lpfc_hba *phba = vport->phba;
16921        int handled;
16922
16923        /* Accepting abort at ulp with SLI4 only */
16924        if (phba->sli_rev < LPFC_SLI_REV4)
16925                return false;
16926
16927        /* Register all caring upper level protocols to attend abort */
16928        handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16929        if (handled)
16930                return true;
16931
16932        return false;
16933}
16934
16935/**
16936 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
16937 * @phba: Pointer to HBA context object.
16938 * @cmd_iocbq: pointer to the command iocbq structure.
16939 * @rsp_iocbq: pointer to the response iocbq structure.
16940 *
16941 * This function handles the sequence abort response iocb command complete
16942 * event. It properly releases the memory allocated to the sequence abort
16943 * accept iocb.
16944 **/
16945static void
16946lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
16947                             struct lpfc_iocbq *cmd_iocbq,
16948                             struct lpfc_iocbq *rsp_iocbq)
16949{
16950        struct lpfc_nodelist *ndlp;
16951
16952        if (cmd_iocbq) {
16953                ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16954                lpfc_nlp_put(ndlp);
16955                lpfc_nlp_not_used(ndlp);
16956                lpfc_sli_release_iocbq(phba, cmd_iocbq);
16957        }
16958
16959        /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16960        if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16961                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16962                        "3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
16963                        rsp_iocbq->iocb.ulpStatus,
16964                        rsp_iocbq->iocb.un.ulpWord[4]);
16965}
16966
16967/**
16968 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16969 * @phba: Pointer to HBA context object.
16970 * @xri: xri id in transaction.
16971 *
16972 * This function validates the xri maps to the known range of XRIs allocated an
16973 * used by the driver.
16974 **/
16975uint16_t
16976lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16977                      uint16_t xri)
16978{
16979        uint16_t i;
16980
16981        for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16982                if (xri == phba->sli4_hba.xri_ids[i])
16983                        return i;
16984        }
16985        return NO_XRI;
16986}
16987
16988/**
16989 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
16990 * @phba: Pointer to HBA context object.
16991 * @fc_hdr: pointer to a FC frame header.
16992 *
16993 * This function sends a basic response to a previous unsol sequence abort
16994 * event after aborting the sequence handling.
16995 **/
16996void
16997lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16998                        struct fc_frame_header *fc_hdr, bool aborted)
16999{
17000        struct lpfc_hba *phba = vport->phba;
17001        struct lpfc_iocbq *ctiocb = NULL;
17002        struct lpfc_nodelist *ndlp;
17003        uint16_t oxid, rxid, xri, lxri;
17004        uint32_t sid, fctl;
17005        IOCB_t *icmd;
17006        int rc;
17007
17008        if (!lpfc_is_link_up(phba))
17009                return;
17010
17011        sid = sli4_sid_from_fc_hdr(fc_hdr);
17012        oxid = be16_to_cpu(fc_hdr->fh_ox_id);
17013        rxid = be16_to_cpu(fc_hdr->fh_rx_id);
17014
17015        ndlp = lpfc_findnode_did(vport, sid);
17016        if (!ndlp) {
17017                ndlp = lpfc_nlp_init(vport, sid);
17018                if (!ndlp) {
17019                        lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17020                                         "1268 Failed to allocate ndlp for "
17021                                         "oxid:x%x SID:x%x\n", oxid, sid);
17022                        return;
17023                }
17024                /* Put ndlp onto pport node list */
17025                lpfc_enqueue_node(vport, ndlp);
17026        } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17027                /* re-setup ndlp without removing from node list */
17028                ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17029                if (!ndlp) {
17030                        lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17031                                         "3275 Failed to active ndlp found "
17032                                         "for oxid:x%x SID:x%x\n", oxid, sid);
17033                        return;
17034                }
17035        }
17036
17037        /* Allocate buffer for rsp iocb */
17038        ctiocb = lpfc_sli_get_iocbq(phba);
17039        if (!ctiocb)
17040                return;
17041
17042        /* Extract the F_CTL field from FC_HDR */
17043        fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17044
17045        icmd = &ctiocb->iocb;
17046        icmd->un.xseq64.bdl.bdeSize = 0;
17047        icmd->un.xseq64.bdl.ulpIoTag32 = 0;
17048        icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17049        icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17050        icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17051
17052        /* Fill in the rest of iocb fields */
17053        icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17054        icmd->ulpBdeCount = 0;
17055        icmd->ulpLe = 1;
17056        icmd->ulpClass = CLASS3;
17057        icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
17058        ctiocb->context1 = lpfc_nlp_get(ndlp);
17059
17060        ctiocb->iocb_cmpl = NULL;
17061        ctiocb->vport = phba->pport;
17062        ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
17063        ctiocb->sli4_lxritag = NO_XRI;
17064        ctiocb->sli4_xritag = NO_XRI;
17065
17066        if (fctl & FC_FC_EX_CTX)
17067                /* Exchange responder sent the abort so we
17068                 * own the oxid.
17069                 */
17070                xri = oxid;
17071        else
17072                xri = rxid;
17073        lxri = lpfc_sli4_xri_inrange(phba, xri);
17074        if (lxri != NO_XRI)
17075                lpfc_set_rrq_active(phba, ndlp, lxri,
17076                        (xri == oxid) ? rxid : oxid, 0);
17077        /* For BA_ABTS from exchange responder, if the logical xri with
17078         * the oxid maps to the FCP XRI range, the port no longer has
17079         * that exchange context, send a BLS_RJT. Override the IOCB for
17080         * a BA_RJT.
17081         */
17082        if ((fctl & FC_FC_EX_CTX) &&
17083            (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
17084                icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17085                bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17086                bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17087                bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17088        }
17089
17090        /* If BA_ABTS failed to abort a partially assembled receive sequence,
17091         * the driver no longer has that exchange, send a BLS_RJT. Override
17092         * the IOCB for a BA_RJT.
17093         */
17094        if (aborted == false) {
17095                icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17096                bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17097                bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17098                bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17099        }
17100
17101        if (fctl & FC_FC_EX_CTX) {
17102                /* ABTS sent by responder to CT exchange, construction
17103                 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17104                 * field and RX_ID from ABTS for RX_ID field.
17105                 */
17106                bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
17107        } else {
17108                /* ABTS sent by initiator to CT exchange, construction
17109                 * of BA_ACC will need to allocate a new XRI as for the
17110                 * XRI_TAG field.
17111                 */
17112                bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
17113        }
17114        bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
17115        bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
17116
17117        /* Xmit CT abts response on exchange <xid> */
17118        lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17119                         "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17120                         icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
17121
17122        rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17123        if (rc == IOCB_ERROR) {
17124                lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17125                                 "2925 Failed to issue CT ABTS RSP x%x on "
17126                                 "xri x%x, Data x%x\n",
17127                                 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17128                                 phba->link_state);
17129                lpfc_nlp_put(ndlp);
17130                ctiocb->context1 = NULL;
17131                lpfc_sli_release_iocbq(phba, ctiocb);
17132        }
17133}
17134
17135/**
17136 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17137 * @vport: Pointer to the vport on which this sequence was received
17138 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17139 *
17140 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17141 * receive sequence is only partially assembed by the driver, it shall abort
17142 * the partially assembled frames for the sequence. Otherwise, if the
17143 * unsolicited receive sequence has been completely assembled and passed to
17144 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17145 * unsolicited sequence has been aborted. After that, it will issue a basic
17146 * accept to accept the abort.
17147 **/
17148static void
17149lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17150                             struct hbq_dmabuf *dmabuf)
17151{
17152        struct lpfc_hba *phba = vport->phba;
17153        struct fc_frame_header fc_hdr;
17154        uint32_t fctl;
17155        bool aborted;
17156
17157        /* Make a copy of fc_hdr before the dmabuf being released */
17158        memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
17159        fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
17160
17161        if (fctl & FC_FC_EX_CTX) {
17162                /* ABTS by responder to exchange, no cleanup needed */
17163                aborted = true;
17164        } else {
17165                /* ABTS by initiator to exchange, need to do cleanup */
17166                aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17167                if (aborted == false)
17168                        aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
17169        }
17170        lpfc_in_buf_free(phba, &dmabuf->dbuf);
17171
17172        if (phba->nvmet_support) {
17173                lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17174                return;
17175        }
17176
17177        /* Respond with BA_ACC or BA_RJT accordingly */
17178        lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
17179}
17180
17181/**
17182 * lpfc_seq_complete - Indicates if a sequence is complete
17183 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17184 *
17185 * This function checks the sequence, starting with the frame described by
17186 * @dmabuf, to see if all the frames associated with this sequence are present.
17187 * the frames associated with this sequence are linked to the @dmabuf using the
17188 * dbuf list. This function looks for two major things. 1) That the first frame
17189 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17190 * set. 3) That there are no holes in the sequence count. The function will
17191 * return 1 when the sequence is complete, otherwise it will return 0.
17192 **/
17193static int
17194lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17195{
17196        struct fc_frame_header *hdr;
17197        struct lpfc_dmabuf *d_buf;
17198        struct hbq_dmabuf *seq_dmabuf;
17199        uint32_t fctl;
17200        int seq_count = 0;
17201
17202        hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17203        /* make sure first fame of sequence has a sequence count of zero */
17204        if (hdr->fh_seq_cnt != seq_count)
17205                return 0;
17206        fctl = (hdr->fh_f_ctl[0] << 16 |
17207                hdr->fh_f_ctl[1] << 8 |
17208                hdr->fh_f_ctl[2]);
17209        /* If last frame of sequence we can return success. */
17210        if (fctl & FC_FC_END_SEQ)
17211                return 1;
17212        list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17213                seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17214                hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17215                /* If there is a hole in the sequence count then fail. */
17216                if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
17217                        return 0;
17218                fctl = (hdr->fh_f_ctl[0] << 16 |
17219                        hdr->fh_f_ctl[1] << 8 |
17220                        hdr->fh_f_ctl[2]);
17221                /* If last frame of sequence we can return success. */
17222                if (fctl & FC_FC_END_SEQ)
17223                        return 1;
17224        }
17225        return 0;
17226}
17227
17228/**
17229 * lpfc_prep_seq - Prep sequence for ULP processing
17230 * @vport: Pointer to the vport on which this sequence was received
17231 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17232 *
17233 * This function takes a sequence, described by a list of frames, and creates
17234 * a list of iocbq structures to describe the sequence. This iocbq list will be
17235 * used to issue to the generic unsolicited sequence handler. This routine
17236 * returns a pointer to the first iocbq in the list. If the function is unable
17237 * to allocate an iocbq then it throw out the received frames that were not
17238 * able to be described and return a pointer to the first iocbq. If unable to
17239 * allocate any iocbqs (including the first) this function will return NULL.
17240 **/
17241static struct lpfc_iocbq *
17242lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17243{
17244        struct hbq_dmabuf *hbq_buf;
17245        struct lpfc_dmabuf *d_buf, *n_buf;
17246        struct lpfc_iocbq *first_iocbq, *iocbq;
17247        struct fc_frame_header *fc_hdr;
17248        uint32_t sid;
17249        uint32_t len, tot_len;
17250        struct ulp_bde64 *pbde;
17251
17252        fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17253        /* remove from receive buffer list */
17254        list_del_init(&seq_dmabuf->hbuf.list);
17255        lpfc_update_rcv_time_stamp(vport);
17256        /* get the Remote Port's SID */
17257        sid = sli4_sid_from_fc_hdr(fc_hdr);
17258        tot_len = 0;
17259        /* Get an iocbq struct to fill in. */
17260        first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17261        if (first_iocbq) {
17262                /* Initialize the first IOCB. */
17263                first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
17264                first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
17265                first_iocbq->vport = vport;
17266
17267                /* Check FC Header to see what TYPE of frame we are rcv'ing */
17268                if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17269                        first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17270                        first_iocbq->iocb.un.rcvels.parmRo =
17271                                sli4_did_from_fc_hdr(fc_hdr);
17272                        first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17273                } else
17274                        first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
17275                first_iocbq->iocb.ulpContext = NO_XRI;
17276                first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17277                        be16_to_cpu(fc_hdr->fh_ox_id);
17278                /* iocbq is prepped for internal consumption.  Physical vpi. */
17279                first_iocbq->iocb.unsli3.rcvsli3.vpi =
17280                        vport->phba->vpi_ids[vport->vpi];
17281                /* put the first buffer into the first IOCBq */
17282                tot_len = bf_get(lpfc_rcqe_length,
17283                                       &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17284
17285                first_iocbq->context2 = &seq_dmabuf->dbuf;
17286                first_iocbq->context3 = NULL;
17287                first_iocbq->iocb.ulpBdeCount = 1;
17288                if (tot_len > LPFC_DATA_BUF_SIZE)
17289                        first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17290                                                        LPFC_DATA_BUF_SIZE;
17291                else
17292                        first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17293
17294                first_iocbq->iocb.un.rcvels.remoteID = sid;
17295
17296                first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17297        }
17298        iocbq = first_iocbq;
17299        /*
17300         * Each IOCBq can have two Buffers assigned, so go through the list
17301         * of buffers for this sequence and save two buffers in each IOCBq
17302         */
17303        list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17304                if (!iocbq) {
17305                        lpfc_in_buf_free(vport->phba, d_buf);
17306                        continue;
17307                }
17308                if (!iocbq->context3) {
17309                        iocbq->context3 = d_buf;
17310                        iocbq->iocb.ulpBdeCount++;
17311                        /* We need to get the size out of the right CQE */
17312                        hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17313                        len = bf_get(lpfc_rcqe_length,
17314                                       &hbq_buf->cq_event.cqe.rcqe_cmpl);
17315                        pbde = (struct ulp_bde64 *)
17316                                        &iocbq->iocb.unsli3.sli3Words[4];
17317                        if (len > LPFC_DATA_BUF_SIZE)
17318                                pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17319                        else
17320                                pbde->tus.f.bdeSize = len;
17321
17322                        iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17323                        tot_len += len;
17324                } else {
17325                        iocbq = lpfc_sli_get_iocbq(vport->phba);
17326                        if (!iocbq) {
17327                                if (first_iocbq) {
17328                                        first_iocbq->iocb.ulpStatus =
17329                                                        IOSTAT_FCP_RSP_ERROR;
17330                                        first_iocbq->iocb.un.ulpWord[4] =
17331                                                        IOERR_NO_RESOURCES;
17332                                }
17333                                lpfc_in_buf_free(vport->phba, d_buf);
17334                                continue;
17335                        }
17336                        /* We need to get the size out of the right CQE */
17337                        hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17338                        len = bf_get(lpfc_rcqe_length,
17339                                       &hbq_buf->cq_event.cqe.rcqe_cmpl);
17340                        iocbq->context2 = d_buf;
17341                        iocbq->context3 = NULL;
17342                        iocbq->iocb.ulpBdeCount = 1;
17343                        if (len > LPFC_DATA_BUF_SIZE)
17344                                iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17345                                                        LPFC_DATA_BUF_SIZE;
17346                        else
17347                                iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17348
17349                        tot_len += len;
17350                        iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17351
17352                        iocbq->iocb.un.rcvels.remoteID = sid;
17353                        list_add_tail(&iocbq->list, &first_iocbq->list);
17354                }
17355        }
17356        return first_iocbq;
17357}
17358
17359static void
17360lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17361                          struct hbq_dmabuf *seq_dmabuf)
17362{
17363        struct fc_frame_header *fc_hdr;
17364        struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17365        struct lpfc_hba *phba = vport->phba;
17366
17367        fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17368        iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17369        if (!iocbq) {
17370                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17371                                "2707 Ring %d handler: Failed to allocate "
17372                                "iocb Rctl x%x Type x%x received\n",
17373                                LPFC_ELS_RING,
17374                                fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17375                return;
17376        }
17377        if (!lpfc_complete_unsol_iocb(phba,
17378                                      phba->sli4_hba.els_wq->pring,
17379                                      iocbq, fc_hdr->fh_r_ctl,
17380                                      fc_hdr->fh_type))
17381                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17382                                "2540 Ring %d handler: unexpected Rctl "
17383                                "x%x Type x%x received\n",
17384                                LPFC_ELS_RING,
17385                                fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17386
17387        /* Free iocb created in lpfc_prep_seq */
17388        list_for_each_entry_safe(curr_iocb, next_iocb,
17389                &iocbq->list, list) {
17390                list_del_init(&curr_iocb->list);
17391                lpfc_sli_release_iocbq(phba, curr_iocb);
17392        }
17393        lpfc_sli_release_iocbq(phba, iocbq);
17394}
17395
17396static void
17397lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17398                            struct lpfc_iocbq *rspiocb)
17399{
17400        struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17401
17402        if (pcmd && pcmd->virt)
17403                dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
17404        kfree(pcmd);
17405        lpfc_sli_release_iocbq(phba, cmdiocb);
17406}
17407
17408static void
17409lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17410                              struct hbq_dmabuf *dmabuf)
17411{
17412        struct fc_frame_header *fc_hdr;
17413        struct lpfc_hba *phba = vport->phba;
17414        struct lpfc_iocbq *iocbq = NULL;
17415        union  lpfc_wqe *wqe;
17416        struct lpfc_dmabuf *pcmd = NULL;
17417        uint32_t frame_len;
17418        int rc;
17419
17420        fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17421        frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17422
17423        /* Send the received frame back */
17424        iocbq = lpfc_sli_get_iocbq(phba);
17425        if (!iocbq)
17426                goto exit;
17427
17428        /* Allocate buffer for command payload */
17429        pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17430        if (pcmd)
17431                pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
17432                                            &pcmd->phys);
17433        if (!pcmd || !pcmd->virt)
17434                goto exit;
17435
17436        INIT_LIST_HEAD(&pcmd->list);
17437
17438        /* copyin the payload */
17439        memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17440
17441        /* fill in BDE's for command */
17442        iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17443        iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17444        iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17445        iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17446
17447        iocbq->context2 = pcmd;
17448        iocbq->vport = vport;
17449        iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17450        iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17451
17452        /*
17453         * Setup rest of the iocb as though it were a WQE
17454         * Build the SEND_FRAME WQE
17455         */
17456        wqe = (union lpfc_wqe *)&iocbq->iocb;
17457
17458        wqe->send_frame.frame_len = frame_len;
17459        wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17460        wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17461        wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17462        wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17463        wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17464        wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17465
17466        iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17467        iocbq->iocb.ulpLe = 1;
17468        iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17469        rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17470        if (rc == IOCB_ERROR)
17471                goto exit;
17472
17473        lpfc_in_buf_free(phba, &dmabuf->dbuf);
17474        return;
17475
17476exit:
17477        lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17478                        "2023 Unable to process MDS loopback frame\n");
17479        if (pcmd && pcmd->virt)
17480                dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
17481        kfree(pcmd);
17482        if (iocbq)
17483                lpfc_sli_release_iocbq(phba, iocbq);
17484        lpfc_in_buf_free(phba, &dmabuf->dbuf);
17485}
17486
17487/**
17488 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17489 * @phba: Pointer to HBA context object.
17490 *
17491 * This function is called with no lock held. This function processes all
17492 * the received buffers and gives it to upper layers when a received buffer
17493 * indicates that it is the final frame in the sequence. The interrupt
17494 * service routine processes received buffers at interrupt contexts.
17495 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17496 * appropriate receive function when the final frame in a sequence is received.
17497 **/
17498void
17499lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17500                                 struct hbq_dmabuf *dmabuf)
17501{
17502        struct hbq_dmabuf *seq_dmabuf;
17503        struct fc_frame_header *fc_hdr;
17504        struct lpfc_vport *vport;
17505        uint32_t fcfi;
17506        uint32_t did;
17507
17508        /* Process each received buffer */
17509        fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17510
17511        /* check to see if this a valid type of frame */
17512        if (lpfc_fc_frame_check(phba, fc_hdr)) {
17513                lpfc_in_buf_free(phba, &dmabuf->dbuf);
17514                return;
17515        }
17516
17517        if ((bf_get(lpfc_cqe_code,
17518                    &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17519                fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17520                              &dmabuf->cq_event.cqe.rcqe_cmpl);
17521        else
17522                fcfi = bf_get(lpfc_rcqe_fcf_id,
17523                              &dmabuf->cq_event.cqe.rcqe_cmpl);
17524
17525        if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17526                vport = phba->pport;
17527                /* Handle MDS Loopback frames */
17528                lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17529                return;
17530        }
17531
17532        /* d_id this frame is directed to */
17533        did = sli4_did_from_fc_hdr(fc_hdr);
17534
17535        vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
17536        if (!vport) {
17537                /* throw out the frame */
17538                lpfc_in_buf_free(phba, &dmabuf->dbuf);
17539                return;
17540        }
17541
17542        /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17543        if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17544                (did != Fabric_DID)) {
17545                /*
17546                 * Throw out the frame if we are not pt2pt.
17547                 * The pt2pt protocol allows for discovery frames
17548                 * to be received without a registered VPI.
17549                 */
17550                if (!(vport->fc_flag & FC_PT2PT) ||
17551                        (phba->link_state == LPFC_HBA_READY)) {
17552                        lpfc_in_buf_free(phba, &dmabuf->dbuf);
17553                        return;
17554                }
17555        }
17556
17557        /* Handle the basic abort sequence (BA_ABTS) event */
17558        if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17559                lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17560                return;
17561        }
17562
17563        /* Link this frame */
17564        seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17565        if (!seq_dmabuf) {
17566                /* unable to add frame to vport - throw it out */
17567                lpfc_in_buf_free(phba, &dmabuf->dbuf);
17568                return;
17569        }
17570        /* If not last frame in sequence continue processing frames. */
17571        if (!lpfc_seq_complete(seq_dmabuf))
17572                return;
17573
17574        /* Send the complete sequence to the upper layer protocol */
17575        lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
17576}
17577
17578/**
17579 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17580 * @phba: pointer to lpfc hba data structure.
17581 *
17582 * This routine is invoked to post rpi header templates to the
17583 * HBA consistent with the SLI-4 interface spec.  This routine
17584 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17585 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17586 *
17587 * This routine does not require any locks.  It's usage is expected
17588 * to be driver load or reset recovery when the driver is
17589 * sequential.
17590 *
17591 * Return codes
17592 *      0 - successful
17593 *      -EIO - The mailbox failed to complete successfully.
17594 *      When this error occurs, the driver is not guaranteed
17595 *      to have any rpi regions posted to the device and
17596 *      must either attempt to repost the regions or take a
17597 *      fatal error.
17598 **/
17599int
17600lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17601{
17602        struct lpfc_rpi_hdr *rpi_page;
17603        uint32_t rc = 0;
17604        uint16_t lrpi = 0;
17605
17606        /* SLI4 ports that support extents do not require RPI headers. */
17607        if (!phba->sli4_hba.rpi_hdrs_in_use)
17608                goto exit;
17609        if (phba->sli4_hba.extents_in_use)
17610                return -EIO;
17611
17612        list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
17613                /*
17614                 * Assign the rpi headers a physical rpi only if the driver
17615                 * has not initialized those resources.  A port reset only
17616                 * needs the headers posted.
17617                 */
17618                if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17619                    LPFC_RPI_RSRC_RDY)
17620                        rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17621
17622                rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17623                if (rc != MBX_SUCCESS) {
17624                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17625                                        "2008 Error %d posting all rpi "
17626                                        "headers\n", rc);
17627                        rc = -EIO;
17628                        break;
17629                }
17630        }
17631
17632 exit:
17633        bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17634               LPFC_RPI_RSRC_RDY);
17635        return rc;
17636}
17637
17638/**
17639 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17640 * @phba: pointer to lpfc hba data structure.
17641 * @rpi_page:  pointer to the rpi memory region.
17642 *
17643 * This routine is invoked to post a single rpi header to the
17644 * HBA consistent with the SLI-4 interface spec.  This memory region
17645 * maps up to 64 rpi context regions.
17646 *
17647 * Return codes
17648 *      0 - successful
17649 *      -ENOMEM - No available memory
17650 *      -EIO - The mailbox failed to complete successfully.
17651 **/
17652int
17653lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17654{
17655        LPFC_MBOXQ_t *mboxq;
17656        struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17657        uint32_t rc = 0;
17658        uint32_t shdr_status, shdr_add_status;
17659        union lpfc_sli4_cfg_shdr *shdr;
17660
17661        /* SLI4 ports that support extents do not require RPI headers. */
17662        if (!phba->sli4_hba.rpi_hdrs_in_use)
17663                return rc;
17664        if (phba->sli4_hba.extents_in_use)
17665                return -EIO;
17666
17667        /* The port is notified of the header region via a mailbox command. */
17668        mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17669        if (!mboxq) {
17670                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17671                                "2001 Unable to allocate memory for issuing "
17672                                "SLI_CONFIG_SPECIAL mailbox command\n");
17673                return -ENOMEM;
17674        }
17675
17676        /* Post all rpi memory regions to the port. */
17677        hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
17678        lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17679                         LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17680                         sizeof(struct lpfc_mbx_post_hdr_tmpl) -
17681                         sizeof(struct lpfc_sli4_cfg_mhdr),
17682                         LPFC_SLI4_MBX_EMBED);
17683
17684
17685        /* Post the physical rpi to the port for this rpi header. */
17686        bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17687               rpi_page->start_rpi);
17688        bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17689               hdr_tmpl, rpi_page->page_count);
17690
17691        hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17692        hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
17693        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
17694        shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17695        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17696        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17697        if (rc != MBX_TIMEOUT)
17698                mempool_free(mboxq, phba->mbox_mem_pool);
17699        if (shdr_status || shdr_add_status || rc) {
17700                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17701                                "2514 POST_RPI_HDR mailbox failed with "
17702                                "status x%x add_status x%x, mbx status x%x\n",
17703                                shdr_status, shdr_add_status, rc);
17704                rc = -ENXIO;
17705        } else {
17706                /*
17707                 * The next_rpi stores the next logical module-64 rpi value used
17708                 * to post physical rpis in subsequent rpi postings.
17709                 */
17710                spin_lock_irq(&phba->hbalock);
17711                phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17712                spin_unlock_irq(&phba->hbalock);
17713        }
17714        return rc;
17715}
17716
17717/**
17718 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17719 * @phba: pointer to lpfc hba data structure.
17720 *
17721 * This routine is invoked to post rpi header templates to the
17722 * HBA consistent with the SLI-4 interface spec.  This routine
17723 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17724 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
17725 *
17726 * Returns
17727 *      A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
17728 *      LPFC_RPI_ALLOC_ERROR if no rpis are available.
17729 **/
17730int
17731lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17732{
17733        unsigned long rpi;
17734        uint16_t max_rpi, rpi_limit;
17735        uint16_t rpi_remaining, lrpi = 0;
17736        struct lpfc_rpi_hdr *rpi_hdr;
17737        unsigned long iflag;
17738
17739        /*
17740         * Fetch the next logical rpi.  Because this index is logical,
17741         * the  driver starts at 0 each time.
17742         */
17743        spin_lock_irqsave(&phba->hbalock, iflag);
17744        max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17745        rpi_limit = phba->sli4_hba.next_rpi;
17746
17747        rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17748        if (rpi >= rpi_limit)
17749                rpi = LPFC_RPI_ALLOC_ERROR;
17750        else {
17751                set_bit(rpi, phba->sli4_hba.rpi_bmask);
17752                phba->sli4_hba.max_cfg_param.rpi_used++;
17753                phba->sli4_hba.rpi_count++;
17754        }
17755        lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17756                        "0001 rpi:%x max:%x lim:%x\n",
17757                        (int) rpi, max_rpi, rpi_limit);
17758
17759        /*
17760         * Don't try to allocate more rpi header regions if the device limit
17761         * has been exhausted.
17762         */
17763        if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17764            (phba->sli4_hba.rpi_count >= max_rpi)) {
17765                spin_unlock_irqrestore(&phba->hbalock, iflag);
17766                return rpi;
17767        }
17768
17769        /*
17770         * RPI header postings are not required for SLI4 ports capable of
17771         * extents.
17772         */
17773        if (!phba->sli4_hba.rpi_hdrs_in_use) {
17774                spin_unlock_irqrestore(&phba->hbalock, iflag);
17775                return rpi;
17776        }
17777
17778        /*
17779         * If the driver is running low on rpi resources, allocate another
17780         * page now.  Note that the next_rpi value is used because
17781         * it represents how many are actually in use whereas max_rpi notes
17782         * how many are supported max by the device.
17783         */
17784        rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
17785        spin_unlock_irqrestore(&phba->hbalock, iflag);
17786        if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17787                rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17788                if (!rpi_hdr) {
17789                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17790                                        "2002 Error Could not grow rpi "
17791                                        "count\n");
17792                } else {
17793                        lrpi = rpi_hdr->start_rpi;
17794                        rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17795                        lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17796                }
17797        }
17798
17799        return rpi;
17800}
17801
17802/**
17803 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17804 * @phba: pointer to lpfc hba data structure.
17805 *
17806 * This routine is invoked to release an rpi to the pool of
17807 * available rpis maintained by the driver.
17808 **/
17809static void
17810__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17811{
17812        if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17813                phba->sli4_hba.rpi_count--;
17814                phba->sli4_hba.max_cfg_param.rpi_used--;
17815        }
17816}
17817
17818/**
17819 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17820 * @phba: pointer to lpfc hba data structure.
17821 *
17822 * This routine is invoked to release an rpi to the pool of
17823 * available rpis maintained by the driver.
17824 **/
17825void
17826lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17827{
17828        spin_lock_irq(&phba->hbalock);
17829        __lpfc_sli4_free_rpi(phba, rpi);
17830        spin_unlock_irq(&phba->hbalock);
17831}
17832
17833/**
17834 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17835 * @phba: pointer to lpfc hba data structure.
17836 *
17837 * This routine is invoked to remove the memory region that
17838 * provided rpi via a bitmask.
17839 **/
17840void
17841lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17842{
17843        kfree(phba->sli4_hba.rpi_bmask);
17844        kfree(phba->sli4_hba.rpi_ids);
17845        bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
17846}
17847
17848/**
17849 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17850 * @phba: pointer to lpfc hba data structure.
17851 *
17852 * This routine is invoked to remove the memory region that
17853 * provided rpi via a bitmask.
17854 **/
17855int
17856lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17857        void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
17858{
17859        LPFC_MBOXQ_t *mboxq;
17860        struct lpfc_hba *phba = ndlp->phba;
17861        int rc;
17862
17863        /* The port is notified of the header region via a mailbox command. */
17864        mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17865        if (!mboxq)
17866                return -ENOMEM;
17867
17868        /* Post all rpi memory regions to the port. */
17869        lpfc_resume_rpi(mboxq, ndlp);
17870        if (cmpl) {
17871                mboxq->mbox_cmpl = cmpl;
17872                mboxq->context1 = arg;
17873                mboxq->context2 = ndlp;
17874        } else
17875                mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17876        mboxq->vport = ndlp->vport;
17877        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17878        if (rc == MBX_NOT_FINISHED) {
17879                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17880                                "2010 Resume RPI Mailbox failed "
17881                                "status %d, mbxStatus x%x\n", rc,
17882                                bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17883                mempool_free(mboxq, phba->mbox_mem_pool);
17884                return -EIO;
17885        }
17886        return 0;
17887}
17888
17889/**
17890 * lpfc_sli4_init_vpi - Initialize a vpi with the port
17891 * @vport: Pointer to the vport for which the vpi is being initialized
17892 *
17893 * This routine is invoked to activate a vpi with the port.
17894 *
17895 * Returns:
17896 *    0 success
17897 *    -Evalue otherwise
17898 **/
17899int
17900lpfc_sli4_init_vpi(struct lpfc_vport *vport)
17901{
17902        LPFC_MBOXQ_t *mboxq;
17903        int rc = 0;
17904        int retval = MBX_SUCCESS;
17905        uint32_t mbox_tmo;
17906        struct lpfc_hba *phba = vport->phba;
17907        mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17908        if (!mboxq)
17909                return -ENOMEM;
17910        lpfc_init_vpi(phba, mboxq, vport->vpi);
17911        mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
17912        rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
17913        if (rc != MBX_SUCCESS) {
17914                lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
17915                                "2022 INIT VPI Mailbox failed "
17916                                "status %d, mbxStatus x%x\n", rc,
17917                                bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17918                retval = -EIO;
17919        }
17920        if (rc != MBX_TIMEOUT)
17921                mempool_free(mboxq, vport->phba->mbox_mem_pool);
17922
17923        return retval;
17924}
17925
17926/**
17927 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17928 * @phba: pointer to lpfc hba data structure.
17929 * @mboxq: Pointer to mailbox object.
17930 *
17931 * This routine is invoked to manually add a single FCF record. The caller
17932 * must pass a completely initialized FCF_Record.  This routine takes
17933 * care of the nonembedded mailbox operations.
17934 **/
17935static void
17936lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17937{
17938        void *virt_addr;
17939        union lpfc_sli4_cfg_shdr *shdr;
17940        uint32_t shdr_status, shdr_add_status;
17941
17942        virt_addr = mboxq->sge_array->addr[0];
17943        /* The IOCTL status is embedded in the mailbox subheader. */
17944        shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17945        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17946        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17947
17948        if ((shdr_status || shdr_add_status) &&
17949                (shdr_status != STATUS_FCF_IN_USE))
17950                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17951                        "2558 ADD_FCF_RECORD mailbox failed with "
17952                        "status x%x add_status x%x\n",
17953                        shdr_status, shdr_add_status);
17954
17955        lpfc_sli4_mbox_cmd_free(phba, mboxq);
17956}
17957
17958/**
17959 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17960 * @phba: pointer to lpfc hba data structure.
17961 * @fcf_record:  pointer to the initialized fcf record to add.
17962 *
17963 * This routine is invoked to manually add a single FCF record. The caller
17964 * must pass a completely initialized FCF_Record.  This routine takes
17965 * care of the nonembedded mailbox operations.
17966 **/
17967int
17968lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17969{
17970        int rc = 0;
17971        LPFC_MBOXQ_t *mboxq;
17972        uint8_t *bytep;
17973        void *virt_addr;
17974        struct lpfc_mbx_sge sge;
17975        uint32_t alloc_len, req_len;
17976        uint32_t fcfindex;
17977
17978        mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17979        if (!mboxq) {
17980                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17981                        "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17982                return -ENOMEM;
17983        }
17984
17985        req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17986                  sizeof(uint32_t);
17987
17988        /* Allocate DMA memory and set up the non-embedded mailbox command */
17989        alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17990                                     LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17991                                     req_len, LPFC_SLI4_MBX_NEMBED);
17992        if (alloc_len < req_len) {
17993                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17994                        "2523 Allocated DMA memory size (x%x) is "
17995                        "less than the requested DMA memory "
17996                        "size (x%x)\n", alloc_len, req_len);
17997                lpfc_sli4_mbox_cmd_free(phba, mboxq);
17998                return -ENOMEM;
17999        }
18000
18001        /*
18002         * Get the first SGE entry from the non-embedded DMA memory.  This
18003         * routine only uses a single SGE.
18004         */
18005        lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
18006        virt_addr = mboxq->sge_array->addr[0];
18007        /*
18008         * Configure the FCF record for FCFI 0.  This is the driver's
18009         * hardcoded default and gets used in nonFIP mode.
18010         */
18011        fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18012        bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18013        lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18014
18015        /*
18016         * Copy the fcf_index and the FCF Record Data. The data starts after
18017         * the FCoE header plus word10. The data copy needs to be endian
18018         * correct.
18019         */
18020        bytep += sizeof(uint32_t);
18021        lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18022        mboxq->vport = phba->pport;
18023        mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18024        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18025        if (rc == MBX_NOT_FINISHED) {
18026                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18027                        "2515 ADD_FCF_RECORD mailbox failed with "
18028                        "status 0x%x\n", rc);
18029                lpfc_sli4_mbox_cmd_free(phba, mboxq);
18030                rc = -EIO;
18031        } else
18032                rc = 0;
18033
18034        return rc;
18035}
18036
18037/**
18038 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18039 * @phba: pointer to lpfc hba data structure.
18040 * @fcf_record:  pointer to the fcf record to write the default data.
18041 * @fcf_index: FCF table entry index.
18042 *
18043 * This routine is invoked to build the driver's default FCF record.  The
18044 * values used are hardcoded.  This routine handles memory initialization.
18045 *
18046 **/
18047void
18048lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18049                                struct fcf_record *fcf_record,
18050                                uint16_t fcf_index)
18051{
18052        memset(fcf_record, 0, sizeof(struct fcf_record));
18053        fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18054        fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18055        fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18056        bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18057        bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18058        bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18059        bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18060        bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18061        bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18062        bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18063        bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18064        bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18065        bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
18066        bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
18067        bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18068        bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18069                LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18070        /* Set the VLAN bit map */
18071        if (phba->valid_vlan) {
18072                fcf_record->vlan_bitmap[phba->vlan_id / 8]
18073                        = 1 << (phba->vlan_id % 8);
18074        }
18075}
18076
18077/**
18078 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
18079 * @phba: pointer to lpfc hba data structure.
18080 * @fcf_index: FCF table entry offset.
18081 *
18082 * This routine is invoked to scan the entire FCF table by reading FCF
18083 * record and processing it one at a time starting from the @fcf_index
18084 * for initial FCF discovery or fast FCF failover rediscovery.
18085 *
18086 * Return 0 if the mailbox command is submitted successfully, none 0
18087 * otherwise.
18088 **/
18089int
18090lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18091{
18092        int rc = 0, error;
18093        LPFC_MBOXQ_t *mboxq;
18094
18095        phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
18096        phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
18097        mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18098        if (!mboxq) {
18099                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18100                                "2000 Failed to allocate mbox for "
18101                                "READ_FCF cmd\n");
18102                error = -ENOMEM;
18103                goto fail_fcf_scan;
18104        }
18105        /* Construct the read FCF record mailbox command */
18106        rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18107        if (rc) {
18108                error = -EINVAL;
18109                goto fail_fcf_scan;
18110        }
18111        /* Issue the mailbox command asynchronously */
18112        mboxq->vport = phba->pport;
18113        mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
18114
18115        spin_lock_irq(&phba->hbalock);
18116        phba->hba_flag |= FCF_TS_INPROG;
18117        spin_unlock_irq(&phba->hbalock);
18118
18119        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18120        if (rc == MBX_NOT_FINISHED)
18121                error = -EIO;
18122        else {
18123                /* Reset eligible FCF count for new scan */
18124                if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
18125                        phba->fcf.eligible_fcf_cnt = 0;
18126                error = 0;
18127        }
18128fail_fcf_scan:
18129        if (error) {
18130                if (mboxq)
18131                        lpfc_sli4_mbox_cmd_free(phba, mboxq);
18132                /* FCF scan failed, clear FCF_TS_INPROG flag */
18133                spin_lock_irq(&phba->hbalock);
18134                phba->hba_flag &= ~FCF_TS_INPROG;
18135                spin_unlock_irq(&phba->hbalock);
18136        }
18137        return error;
18138}
18139
18140/**
18141 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
18142 * @phba: pointer to lpfc hba data structure.
18143 * @fcf_index: FCF table entry offset.
18144 *
18145 * This routine is invoked to read an FCF record indicated by @fcf_index
18146 * and to use it for FLOGI roundrobin FCF failover.
18147 *
18148 * Return 0 if the mailbox command is submitted successfully, none 0
18149 * otherwise.
18150 **/
18151int
18152lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18153{
18154        int rc = 0, error;
18155        LPFC_MBOXQ_t *mboxq;
18156
18157        mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18158        if (!mboxq) {
18159                lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18160                                "2763 Failed to allocate mbox for "
18161                                "READ_FCF cmd\n");
18162                error = -ENOMEM;
18163                goto fail_fcf_read;
18164        }
18165        /* Construct the read FCF record mailbox command */
18166        rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18167        if (rc) {
18168                error = -EINVAL;
18169                goto fail_fcf_read;
18170        }
18171        /* Issue the mailbox command asynchronously */
18172        mboxq->vport = phba->pport;
18173        mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18174        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18175        if (rc == MBX_NOT_FINISHED)
18176                error = -EIO;
18177        else
18178                error = 0;
18179
18180fail_fcf_read:
18181        if (error && mboxq)
18182                lpfc_sli4_mbox_cmd_free(phba, mboxq);
18183        return error;
18184}
18185
18186/**
18187 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18188 * @phba: pointer to lpfc hba data structure.
18189 * @fcf_index: FCF table entry offset.
18190 *
18191 * This routine is invoked to read an FCF record indicated by @fcf_index to
18192 * determine whether it's eligible for FLOGI roundrobin failover list.
18193 *
18194 * Return 0 if the mailbox command is submitted successfully, none 0
18195 * otherwise.
18196 **/
18197int
18198lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18199{
18200        int rc = 0, error;
18201        LPFC_MBOXQ_t *mboxq;
18202
18203        mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18204        if (!mboxq) {
18205                lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18206                                "2758 Failed to allocate mbox for "
18207                                "READ_FCF cmd\n");
18208                                error = -ENOMEM;
18209                                goto fail_fcf_read;
18210        }
18211        /* Construct the read FCF record mailbox command */
18212        rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18213        if (rc) {
18214                error = -EINVAL;
18215                goto fail_fcf_read;
18216        }
18217        /* Issue the mailbox command asynchronously */
18218        mboxq->vport = phba->pport;
18219        mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18220        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18221        if (rc == MBX_NOT_FINISHED)
18222                error = -EIO;
18223        else
18224                error = 0;
18225
18226fail_fcf_read:
18227        if (error && mboxq)
18228                lpfc_sli4_mbox_cmd_free(phba, mboxq);
18229        return error;
18230}
18231
18232/**
18233 * lpfc_check_next_fcf_pri_level
18234 * phba pointer to the lpfc_hba struct for this port.
18235 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18236 * routine when the rr_bmask is empty. The FCF indecies are put into the
18237 * rr_bmask based on their priority level. Starting from the highest priority
18238 * to the lowest. The most likely FCF candidate will be in the highest
18239 * priority group. When this routine is called it searches the fcf_pri list for
18240 * next lowest priority group and repopulates the rr_bmask with only those
18241 * fcf_indexes.
18242 * returns:
18243 * 1=success 0=failure
18244 **/
18245static int
18246lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18247{
18248        uint16_t next_fcf_pri;
18249        uint16_t last_index;
18250        struct lpfc_fcf_pri *fcf_pri;
18251        int rc;
18252        int ret = 0;
18253
18254        last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18255                        LPFC_SLI4_FCF_TBL_INDX_MAX);
18256        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18257                        "3060 Last IDX %d\n", last_index);
18258
18259        /* Verify the priority list has 2 or more entries */
18260        spin_lock_irq(&phba->hbalock);
18261        if (list_empty(&phba->fcf.fcf_pri_list) ||
18262            list_is_singular(&phba->fcf.fcf_pri_list)) {
18263                spin_unlock_irq(&phba->hbalock);
18264                lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18265                        "3061 Last IDX %d\n", last_index);
18266                return 0; /* Empty rr list */
18267        }
18268        spin_unlock_irq(&phba->hbalock);
18269
18270        next_fcf_pri = 0;
18271        /*
18272         * Clear the rr_bmask and set all of the bits that are at this
18273         * priority.
18274         */
18275        memset(phba->fcf.fcf_rr_bmask, 0,
18276                        sizeof(*phba->fcf.fcf_rr_bmask));
18277        spin_lock_irq(&phba->hbalock);
18278        list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18279                if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18280                        continue;
18281                /*
18282                 * the 1st priority that has not FLOGI failed
18283                 * will be the highest.
18284                 */
18285                if (!next_fcf_pri)
18286                        next_fcf_pri = fcf_pri->fcf_rec.priority;
18287                spin_unlock_irq(&phba->hbalock);
18288                if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18289                        rc = lpfc_sli4_fcf_rr_index_set(phba,
18290                                                fcf_pri->fcf_rec.fcf_index);
18291                        if (rc)
18292                                return 0;
18293                }
18294                spin_lock_irq(&phba->hbalock);
18295        }
18296        /*
18297         * if next_fcf_pri was not set above and the list is not empty then
18298         * we have failed flogis on all of them. So reset flogi failed
18299         * and start at the beginning.
18300         */
18301        if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18302                list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18303                        fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18304                        /*
18305                         * the 1st priority that has not FLOGI failed
18306                         * will be the highest.
18307                         */
18308                        if (!next_fcf_pri)
18309                                next_fcf_pri = fcf_pri->fcf_rec.priority;
18310                        spin_unlock_irq(&phba->hbalock);
18311                        if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18312                                rc = lpfc_sli4_fcf_rr_index_set(phba,
18313                                                fcf_pri->fcf_rec.fcf_index);
18314                                if (rc)
18315                                        return 0;
18316                        }
18317                        spin_lock_irq(&phba->hbalock);
18318                }
18319        } else
18320                ret = 1;
18321        spin_unlock_irq(&phba->hbalock);
18322
18323        return ret;
18324}
18325/**
18326 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18327 * @phba: pointer to lpfc hba data structure.
18328 *
18329 * This routine is to get the next eligible FCF record index in a round
18330 * robin fashion. If the next eligible FCF record index equals to the
18331 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
18332 * shall be returned, otherwise, the next eligible FCF record's index
18333 * shall be returned.
18334 **/
18335uint16_t
18336lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18337{
18338        uint16_t next_fcf_index;
18339
18340initial_priority:
18341        /* Search start from next bit of currently registered FCF index */
18342        next_fcf_index = phba->fcf.current_rec.fcf_indx;
18343
18344next_priority:
18345        /* Determine the next fcf index to check */
18346        next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
18347        next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18348                                       LPFC_SLI4_FCF_TBL_INDX_MAX,
18349                                       next_fcf_index);
18350
18351        /* Wrap around condition on phba->fcf.fcf_rr_bmask */
18352        if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18353                /*
18354                 * If we have wrapped then we need to clear the bits that
18355                 * have been tested so that we can detect when we should
18356                 * change the priority level.
18357                 */
18358                next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18359                                               LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
18360        }
18361
18362
18363        /* Check roundrobin failover list empty condition */
18364        if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18365                next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18366                /*
18367                 * If next fcf index is not found check if there are lower
18368                 * Priority level fcf's in the fcf_priority list.
18369                 * Set up the rr_bmask with all of the avaiable fcf bits
18370                 * at that level and continue the selection process.
18371                 */
18372                if (lpfc_check_next_fcf_pri_level(phba))
18373                        goto initial_priority;
18374                lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18375                                "2844 No roundrobin failover FCF available\n");
18376                if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18377                        return LPFC_FCOE_FCF_NEXT_NONE;
18378                else {
18379                        lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18380                                "3063 Only FCF available idx %d, flag %x\n",
18381                                next_fcf_index,
18382                        phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18383                        return next_fcf_index;
18384                }
18385        }
18386
18387        if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18388                phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
18389                LPFC_FCF_FLOGI_FAILED) {
18390                if (list_is_singular(&phba->fcf.fcf_pri_list))
18391                        return LPFC_FCOE_FCF_NEXT_NONE;
18392
18393                goto next_priority;
18394        }
18395
18396        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18397                        "2845 Get next roundrobin failover FCF (x%x)\n",
18398                        next_fcf_index);
18399
18400        return next_fcf_index;
18401}
18402
18403/**
18404 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18405 * @phba: pointer to lpfc hba data structure.
18406 *
18407 * This routine sets the FCF record index in to the eligible bmask for
18408 * roundrobin failover search. It checks to make sure that the index
18409 * does not go beyond the range of the driver allocated bmask dimension
18410 * before setting the bit.
18411 *
18412 * Returns 0 if the index bit successfully set, otherwise, it returns
18413 * -EINVAL.
18414 **/
18415int
18416lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18417{
18418        if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18419                lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18420                                "2610 FCF (x%x) reached driver's book "
18421                                "keeping dimension:x%x\n",
18422                                fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18423                return -EINVAL;
18424        }
18425        /* Set the eligible FCF record index bmask */
18426        set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18427
18428        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18429                        "2790 Set FCF (x%x) to roundrobin FCF failover "
18430                        "bmask\n", fcf_index);
18431
18432        return 0;
18433}
18434
18435/**
18436 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
18437 * @phba: pointer to lpfc hba data structure.
18438 *
18439 * This routine clears the FCF record index from the eligible bmask for
18440 * roundrobin failover search. It checks to make sure that the index
18441 * does not go beyond the range of the driver allocated bmask dimension
18442 * before clearing the bit.
18443 **/
18444void
18445lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18446{
18447        struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
18448        if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18449                lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18450                                "2762 FCF (x%x) reached driver's book "
18451                                "keeping dimension:x%x\n",
18452                                fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18453                return;
18454        }
18455        /* Clear the eligible FCF record index bmask */
18456        spin_lock_irq(&phba->hbalock);
18457        list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18458                                 list) {
18459                if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18460                        list_del_init(&fcf_pri->list);
18461                        break;
18462                }
18463        }
18464        spin_unlock_irq(&phba->hbalock);
18465        clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18466
18467        lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18468                        "2791 Clear FCF (x%x) from roundrobin failover "
18469                        "bmask\n", fcf_index);
18470}
18471
18472/**
18473 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18474 * @phba: pointer to lpfc hba data structure.
18475 *
18476 * This routine is the completion routine for the rediscover FCF table mailbox
18477 * command. If the mailbox command returned failure, it will try to stop the
18478 * FCF rediscover wait timer.
18479 **/
18480static void
18481lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18482{
18483        struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18484        uint32_t shdr_status, shdr_add_status;
18485
18486        redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18487
18488        shdr_status = bf_get(lpfc_mbox_hdr_status,
18489                             &redisc_fcf->header.cfg_shdr.response);
18490        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18491                             &redisc_fcf->header.cfg_shdr.response);
18492        if (shdr_status || shdr_add_status) {
18493                lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18494                                "2746 Requesting for FCF rediscovery failed "
18495                                "status x%x add_status x%x\n",
18496                                shdr_status, shdr_add_status);
18497                if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
18498                        spin_lock_irq(&phba->hbalock);
18499                        phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
18500                        spin_unlock_irq(&phba->hbalock);
18501                        /*
18502                         * CVL event triggered FCF rediscover request failed,
18503                         * last resort to re-try current registered FCF entry.
18504                         */
18505                        lpfc_retry_pport_discovery(phba);
18506                } else {
18507                        spin_lock_irq(&phba->hbalock);
18508                        phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
18509                        spin_unlock_irq(&phba->hbalock);
18510                        /*
18511                         * DEAD FCF event triggered FCF rediscover request
18512                         * failed, last resort to fail over as a link down
18513                         * to FCF registration.
18514                         */
18515                        lpfc_sli4_fcf_dead_failthrough(phba);
18516                }
18517        } else {
18518                lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18519                                "2775 Start FCF rediscover quiescent timer\n");
18520                /*
18521                 * Start FCF rediscovery wait timer for pending FCF
18522                 * before rescan FCF record table.
18523                 */
18524                lpfc_fcf_redisc_wait_start_timer(phba);
18525        }
18526
18527        mempool_free(mbox, phba->mbox_mem_pool);
18528}
18529
18530/**
18531 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
18532 * @phba: pointer to lpfc hba data structure.
18533 *
18534 * This routine is invoked to request for rediscovery of the entire FCF table
18535 * by the port.
18536 **/
18537int
18538lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18539{
18540        LPFC_MBOXQ_t *mbox;
18541        struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18542        int rc, length;
18543
18544        /* Cancel retry delay timers to all vports before FCF rediscover */
18545        lpfc_cancel_all_vport_retry_delay_timer(phba);
18546
18547        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18548        if (!mbox) {
18549                lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18550                                "2745 Failed to allocate mbox for "
18551                                "requesting FCF rediscover.\n");
18552                return -ENOMEM;
18553        }
18554
18555        length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18556                  sizeof(struct lpfc_sli4_cfg_mhdr));
18557        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18558                         LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18559                         length, LPFC_SLI4_MBX_EMBED);
18560
18561        redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18562        /* Set count to 0 for invalidating the entire FCF database */
18563        bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18564
18565        /* Issue the mailbox command asynchronously */
18566        mbox->vport = phba->pport;
18567        mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18568        rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18569
18570        if (rc == MBX_NOT_FINISHED) {
18571                mempool_free(mbox, phba->mbox_mem_pool);
18572                return -EIO;
18573        }
18574        return 0;
18575}
18576
18577/**
18578 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18579 * @phba: pointer to lpfc hba data structure.
18580 *
18581 * This function is the failover routine as a last resort to the FCF DEAD
18582 * event when driver failed to perform fast FCF failover.
18583 **/
18584void
18585lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18586{
18587        uint32_t link_state;
18588
18589        /*
18590         * Last resort as FCF DEAD event failover will treat this as
18591         * a link down, but save the link state because we don't want
18592         * it to be changed to Link Down unless it is already down.
18593         */
18594        link_state = phba->link_state;
18595        lpfc_linkdown(phba);
18596        phba->link_state = link_state;
18597
18598        /* Unregister FCF if no devices connected to it */
18599        lpfc_unregister_unused_fcf(phba);
18600}
18601
18602/**
18603 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
18604 * @phba: pointer to lpfc hba data structure.
18605 * @rgn23_data: pointer to configure region 23 data.
18606 *
18607 * This function gets SLI3 port configure region 23 data through memory dump
18608 * mailbox command. When it successfully retrieves data, the size of the data
18609 * will be returned, otherwise, 0 will be returned.
18610 **/
18611static uint32_t
18612lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18613{
18614        LPFC_MBOXQ_t *pmb = NULL;
18615        MAILBOX_t *mb;
18616        uint32_t offset = 0;
18617        int rc;
18618
18619        if (!rgn23_data)
18620                return 0;
18621
18622        pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18623        if (!pmb) {
18624                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18625                                "2600 failed to allocate mailbox memory\n");
18626                return 0;
18627        }
18628        mb = &pmb->u.mb;
18629
18630        do {
18631                lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18632                rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18633
18634                if (rc != MBX_SUCCESS) {
18635                        lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
18636                                        "2601 failed to read config "
18637                                        "region 23, rc 0x%x Status 0x%x\n",
18638                                        rc, mb->mbxStatus);
18639                        mb->un.varDmp.word_cnt = 0;
18640                }
18641                /*
18642                 * dump mem may return a zero when finished or we got a
18643                 * mailbox error, either way we are done.
18644                 */
18645                if (mb->un.varDmp.word_cnt == 0)
18646                        break;
18647                if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18648                        mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18649
18650                lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
18651                                       rgn23_data + offset,
18652                                       mb->un.varDmp.word_cnt);
18653                offset += mb->un.varDmp.word_cnt;
18654        } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18655
18656        mempool_free(pmb, phba->mbox_mem_pool);
18657        return offset;
18658}
18659
18660/**
18661 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18662 * @phba: pointer to lpfc hba data structure.
18663 * @rgn23_data: pointer to configure region 23 data.
18664 *
18665 * This function gets SLI4 port configure region 23 data through memory dump
18666 * mailbox command. When it successfully retrieves data, the size of the data
18667 * will be returned, otherwise, 0 will be returned.
18668 **/
18669static uint32_t
18670lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18671{
18672        LPFC_MBOXQ_t *mboxq = NULL;
18673        struct lpfc_dmabuf *mp = NULL;
18674        struct lpfc_mqe *mqe;
18675        uint32_t data_length = 0;
18676        int rc;
18677
18678        if (!rgn23_data)
18679                return 0;
18680
18681        mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18682        if (!mboxq) {
18683                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18684                                "3105 failed to allocate mailbox memory\n");
18685                return 0;
18686        }
18687
18688        if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18689                goto out;
18690        mqe = &mboxq->u.mqe;
18691        mp = (struct lpfc_dmabuf *) mboxq->context1;
18692        rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18693        if (rc)
18694                goto out;
18695        data_length = mqe->un.mb_words[5];
18696        if (data_length == 0)
18697                goto out;
18698        if (data_length > DMP_RGN23_SIZE) {
18699                data_length = 0;
18700                goto out;
18701        }
18702        lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18703out:
18704        mempool_free(mboxq, phba->mbox_mem_pool);
18705        if (mp) {
18706                lpfc_mbuf_free(phba, mp->virt, mp->phys);
18707                kfree(mp);
18708        }
18709        return data_length;
18710}
18711
18712/**
18713 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18714 * @phba: pointer to lpfc hba data structure.
18715 *
18716 * This function read region 23 and parse TLV for port status to
18717 * decide if the user disaled the port. If the TLV indicates the
18718 * port is disabled, the hba_flag is set accordingly.
18719 **/
18720void
18721lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18722{
18723        uint8_t *rgn23_data = NULL;
18724        uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18725        uint32_t offset = 0;
18726
18727        /* Get adapter Region 23 data */
18728        rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18729        if (!rgn23_data)
18730                goto out;
18731
18732        if (phba->sli_rev < LPFC_SLI_REV4)
18733                data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18734        else {
18735                if_type = bf_get(lpfc_sli_intf_if_type,
18736                                 &phba->sli4_hba.sli_intf);
18737                if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18738                        goto out;
18739                data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18740        }
18741
18742        if (!data_size)
18743                goto out;
18744
18745        /* Check the region signature first */
18746        if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18747                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18748                        "2619 Config region 23 has bad signature\n");
18749                        goto out;
18750        }
18751        offset += 4;
18752
18753        /* Check the data structure version */
18754        if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18755                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18756                        "2620 Config region 23 has bad version\n");
18757                goto out;
18758        }
18759        offset += 4;
18760
18761        /* Parse TLV entries in the region */
18762        while (offset < data_size) {
18763                if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18764                        break;
18765                /*
18766                 * If the TLV is not driver specific TLV or driver id is
18767                 * not linux driver id, skip the record.
18768                 */
18769                if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18770                    (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18771                    (rgn23_data[offset + 3] != 0)) {
18772                        offset += rgn23_data[offset + 1] * 4 + 4;
18773                        continue;
18774                }
18775
18776                /* Driver found a driver specific TLV in the config region */
18777                sub_tlv_len = rgn23_data[offset + 1] * 4;
18778                offset += 4;
18779                tlv_offset = 0;
18780
18781                /*
18782                 * Search for configured port state sub-TLV.
18783                 */
18784                while ((offset < data_size) &&
18785                        (tlv_offset < sub_tlv_len)) {
18786                        if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18787                                offset += 4;
18788                                tlv_offset += 4;
18789                                break;
18790                        }
18791                        if (rgn23_data[offset] != PORT_STE_TYPE) {
18792                                offset += rgn23_data[offset + 1] * 4 + 4;
18793                                tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18794                                continue;
18795                        }
18796
18797                        /* This HBA contains PORT_STE configured */
18798                        if (!rgn23_data[offset + 2])
18799                                phba->hba_flag |= LINK_DISABLED;
18800
18801                        goto out;
18802                }
18803        }
18804
18805out:
18806        kfree(rgn23_data);
18807        return;
18808}
18809
18810/**
18811 * lpfc_wr_object - write an object to the firmware
18812 * @phba: HBA structure that indicates port to create a queue on.
18813 * @dmabuf_list: list of dmabufs to write to the port.
18814 * @size: the total byte value of the objects to write to the port.
18815 * @offset: the current offset to be used to start the transfer.
18816 *
18817 * This routine will create a wr_object mailbox command to send to the port.
18818 * the mailbox command will be constructed using the dma buffers described in
18819 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18820 * BDEs that the imbedded mailbox can support. The @offset variable will be
18821 * used to indicate the starting offset of the transfer and will also return
18822 * the offset after the write object mailbox has completed. @size is used to
18823 * determine the end of the object and whether the eof bit should be set.
18824 *
18825 * Return 0 is successful and offset will contain the the new offset to use
18826 * for the next write.
18827 * Return negative value for error cases.
18828 **/
18829int
18830lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18831               uint32_t size, uint32_t *offset)
18832{
18833        struct lpfc_mbx_wr_object *wr_object;
18834        LPFC_MBOXQ_t *mbox;
18835        int rc = 0, i = 0;
18836        uint32_t shdr_status, shdr_add_status;
18837        uint32_t mbox_tmo;
18838        union lpfc_sli4_cfg_shdr *shdr;
18839        struct lpfc_dmabuf *dmabuf;
18840        uint32_t written = 0;
18841
18842        mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18843        if (!mbox)
18844                return -ENOMEM;
18845
18846        lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18847                        LPFC_MBOX_OPCODE_WRITE_OBJECT,
18848                        sizeof(struct lpfc_mbx_wr_object) -
18849                        sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18850
18851        wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18852        wr_object->u.request.write_offset = *offset;
18853        sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18854        wr_object->u.request.object_name[0] =
18855                cpu_to_le32(wr_object->u.request.object_name[0]);
18856        bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18857        list_for_each_entry(dmabuf, dmabuf_list, list) {
18858                if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18859                        break;
18860                wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18861                wr_object->u.request.bde[i].addrHigh =
18862                        putPaddrHigh(dmabuf->phys);
18863                if (written + SLI4_PAGE_SIZE >= size) {
18864                        wr_object->u.request.bde[i].tus.f.bdeSize =
18865                                (size - written);
18866                        written += (size - written);
18867                        bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18868                } else {
18869                        wr_object->u.request.bde[i].tus.f.bdeSize =
18870                                SLI4_PAGE_SIZE;
18871                        written += SLI4_PAGE_SIZE;
18872                }
18873                i++;
18874        }
18875        wr_object->u.request.bde_count = i;
18876        bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18877        if (!phba->sli4_hba.intr_enable)
18878                rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18879        else {
18880                mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18881                rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18882        }
18883        /* The IOCTL status is embedded in the mailbox subheader. */
18884        shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18885        shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18886        shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18887        if (rc != MBX_TIMEOUT)
18888                mempool_free(mbox, phba->mbox_mem_pool);
18889        if (shdr_status || shdr_add_status || rc) {
18890                lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18891                                "3025 Write Object mailbox failed with "
18892                                "status x%x add_status x%x, mbx status x%x\n",
18893                                shdr_status, shdr_add_status, rc);
18894                rc = -ENXIO;
18895                *offset = shdr_add_status;
18896        } else
18897                *offset += wr_object->u.response.actual_write_length;
18898        return rc;
18899}
18900
18901/**
18902 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18903 * @vport: pointer to vport data structure.
18904 *
18905 * This function iterate through the mailboxq and clean up all REG_LOGIN
18906 * and REG_VPI mailbox commands associated with the vport. This function
18907 * is called when driver want to restart discovery of the vport due to
18908 * a Clear Virtual Link event.
18909 **/
18910void
18911lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18912{
18913        struct lpfc_hba *phba = vport->phba;
18914        LPFC_MBOXQ_t *mb, *nextmb;
18915        struct lpfc_dmabuf *mp;
18916        struct lpfc_nodelist *ndlp;
18917        struct lpfc_nodelist *act_mbx_ndlp = NULL;
18918        struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
18919        LIST_HEAD(mbox_cmd_list);
18920        uint8_t restart_loop;
18921
18922        /* Clean up internally queued mailbox commands with the vport */
18923        spin_lock_irq(&phba->hbalock);
18924        list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18925                if (mb->vport != vport)
18926                        continue;
18927
18928                if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18929                        (mb->u.mb.mbxCommand != MBX_REG_VPI))
18930                        continue;
18931
18932                list_del(&mb->list);
18933                list_add_tail(&mb->list, &mbox_cmd_list);
18934        }
18935        /* Clean up active mailbox command with the vport */
18936        mb = phba->sli.mbox_active;
18937        if (mb && (mb->vport == vport)) {
18938                if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18939                        (mb->u.mb.mbxCommand == MBX_REG_VPI))
18940                        mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18941                if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18942                        act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18943                        /* Put reference count for delayed processing */
18944                        act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18945                        /* Unregister the RPI when mailbox complete */
18946                        mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18947                }
18948        }
18949        /* Cleanup any mailbox completions which are not yet processed */
18950        do {
18951                restart_loop = 0;
18952                list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18953                        /*
18954                         * If this mailox is already processed or it is
18955                         * for another vport ignore it.
18956                         */
18957                        if ((mb->vport != vport) ||
18958                                (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18959                                continue;
18960
18961                        if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18962                                (mb->u.mb.mbxCommand != MBX_REG_VPI))
18963                                continue;
18964
18965                        mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18966                        if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18967                                ndlp = (struct lpfc_nodelist *)mb->context2;
18968                                /* Unregister the RPI when mailbox complete */
18969                                mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18970                                restart_loop = 1;
18971                                spin_unlock_irq(&phba->hbalock);
18972                                spin_lock(shost->host_lock);
18973                                ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18974                                spin_unlock(shost->host_lock);
18975                                spin_lock_irq(&phba->hbalock);
18976                                break;
18977                        }
18978                }
18979        } while (restart_loop);
18980
18981        spin_unlock_irq(&phba->hbalock);
18982
18983        /* Release the cleaned-up mailbox commands */
18984        while (!list_empty(&mbox_cmd_list)) {
18985                list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
18986                if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18987                        mp = (struct lpfc_dmabuf *) (mb->context1);
18988                        if (mp) {
18989                                __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18990                                kfree(mp);
18991                        }
18992                        ndlp = (struct lpfc_nodelist *) mb->context2;
18993                        mb->context2 = NULL;
18994                        if (ndlp) {
18995                                spin_lock(shost->host_lock);
18996                                ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18997                                spin_unlock(shost->host_lock);
18998                                lpfc_nlp_put(ndlp);
18999                        }
19000                }
19001                mempool_free(mb, phba->mbox_mem_pool);
19002        }
19003
19004        /* Release the ndlp with the cleaned-up active mailbox command */
19005        if (act_mbx_ndlp) {
19006                spin_lock(shost->host_lock);
19007                act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19008                spin_unlock(shost->host_lock);
19009                lpfc_nlp_put(act_mbx_ndlp);
19010        }
19011}
19012
19013/**
19014 * lpfc_drain_txq - Drain the txq
19015 * @phba: Pointer to HBA context object.
19016 *
19017 * This function attempt to submit IOCBs on the txq
19018 * to the adapter.  For SLI4 adapters, the txq contains
19019 * ELS IOCBs that have been deferred because the there
19020 * are no SGLs.  This congestion can occur with large
19021 * vport counts during node discovery.
19022 **/
19023
19024uint32_t
19025lpfc_drain_txq(struct lpfc_hba *phba)
19026{
19027        LIST_HEAD(completions);
19028        struct lpfc_sli_ring *pring;
19029        struct lpfc_iocbq *piocbq = NULL;
19030        unsigned long iflags = 0;
19031        char *fail_msg = NULL;
19032        struct lpfc_sglq *sglq;
19033        union lpfc_wqe128 wqe;
19034        uint32_t txq_cnt = 0;
19035
19036        pring = lpfc_phba_elsring(phba);
19037        if (unlikely(!pring))
19038                return 0;
19039
19040        spin_lock_irqsave(&pring->ring_lock, iflags);
19041        list_for_each_entry(piocbq, &pring->txq, list) {
19042                txq_cnt++;
19043        }
19044
19045        if (txq_cnt > pring->txq_max)
19046                pring->txq_max = txq_cnt;
19047
19048        spin_unlock_irqrestore(&pring->ring_lock, iflags);
19049
19050        while (!list_empty(&pring->txq)) {
19051                spin_lock_irqsave(&pring->ring_lock, iflags);
19052
19053                piocbq = lpfc_sli_ringtx_get(phba, pring);
19054                if (!piocbq) {
19055                        spin_unlock_irqrestore(&pring->ring_lock, iflags);
19056                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19057                                "2823 txq empty and txq_cnt is %d\n ",
19058                                txq_cnt);
19059                        break;
19060                }
19061                sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
19062                if (!sglq) {
19063                        __lpfc_sli_ringtx_put(phba, pring, piocbq);
19064                        spin_unlock_irqrestore(&pring->ring_lock, iflags);
19065                        break;
19066                }
19067                txq_cnt--;
19068
19069                /* The xri and iocb resources secured,
19070                 * attempt to issue request
19071                 */
19072                piocbq->sli4_lxritag = sglq->sli4_lxritag;
19073                piocbq->sli4_xritag = sglq->sli4_xritag;
19074                if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19075                        fail_msg = "to convert bpl to sgl";
19076                else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
19077                        fail_msg = "to convert iocb to wqe";
19078                else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
19079                        fail_msg = " - Wq is full";
19080                else
19081                        lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19082
19083                if (fail_msg) {
19084                        /* Failed means we can't issue and need to cancel */
19085                        lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19086                                        "2822 IOCB failed %s iotag 0x%x "
19087                                        "xri 0x%x\n",
19088                                        fail_msg,
19089                                        piocbq->iotag, piocbq->sli4_xritag);
19090                        list_add_tail(&piocbq->list, &completions);
19091                }
19092                spin_unlock_irqrestore(&pring->ring_lock, iflags);
19093        }
19094
19095        /* Cancel all the IOCBs that cannot be issued */
19096        lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
19097                                IOERR_SLI_ABORTED);
19098
19099        return txq_cnt;
19100}
19101
19102/**
19103 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
19104 * @phba: Pointer to HBA context object.
19105 * @pwqe: Pointer to command WQE.
19106 * @sglq: Pointer to the scatter gather queue object.
19107 *
19108 * This routine converts the bpl or bde that is in the WQE
19109 * to a sgl list for the sli4 hardware. The physical address
19110 * of the bpl/bde is converted back to a virtual address.
19111 * If the WQE contains a BPL then the list of BDE's is
19112 * converted to sli4_sge's. If the WQE contains a single
19113 * BDE then it is converted to a single sli_sge.
19114 * The WQE is still in cpu endianness so the contents of
19115 * the bpl can be used without byte swapping.
19116 *
19117 * Returns valid XRI = Success, NO_XRI = Failure.
19118 */
19119static uint16_t
19120lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
19121                 struct lpfc_sglq *sglq)
19122{
19123        uint16_t xritag = NO_XRI;
19124        struct ulp_bde64 *bpl = NULL;
19125        struct ulp_bde64 bde;
19126        struct sli4_sge *sgl  = NULL;
19127        struct lpfc_dmabuf *dmabuf;
19128        union lpfc_wqe128 *wqe;
19129        int numBdes = 0;
19130        int i = 0;
19131        uint32_t offset = 0; /* accumulated offset in the sg request list */
19132        int inbound = 0; /* number of sg reply entries inbound from firmware */
19133        uint32_t cmd;
19134
19135        if (!pwqeq || !sglq)
19136                return xritag;
19137
19138        sgl  = (struct sli4_sge *)sglq->sgl;
19139        wqe = &pwqeq->wqe;
19140        pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19141
19142        cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19143        if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19144                return sglq->sli4_xritag;
19145        numBdes = pwqeq->rsvd2;
19146        if (numBdes) {
19147                /* The addrHigh and addrLow fields within the WQE
19148                 * have not been byteswapped yet so there is no
19149                 * need to swap them back.
19150                 */
19151                if (pwqeq->context3)
19152                        dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19153                else
19154                        return xritag;
19155
19156                bpl  = (struct ulp_bde64 *)dmabuf->virt;
19157                if (!bpl)
19158                        return xritag;
19159
19160                for (i = 0; i < numBdes; i++) {
19161                        /* Should already be byte swapped. */
19162                        sgl->addr_hi = bpl->addrHigh;
19163                        sgl->addr_lo = bpl->addrLow;
19164
19165                        sgl->word2 = le32_to_cpu(sgl->word2);
19166                        if ((i+1) == numBdes)
19167                                bf_set(lpfc_sli4_sge_last, sgl, 1);
19168                        else
19169                                bf_set(lpfc_sli4_sge_last, sgl, 0);
19170                        /* swap the size field back to the cpu so we
19171                         * can assign it to the sgl.
19172                         */
19173                        bde.tus.w = le32_to_cpu(bpl->tus.w);
19174                        sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19175                        /* The offsets in the sgl need to be accumulated
19176                         * separately for the request and reply lists.
19177                         * The request is always first, the reply follows.
19178                         */
19179                        switch (cmd) {
19180                        case CMD_GEN_REQUEST64_WQE:
19181                                /* add up the reply sg entries */
19182                                if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19183                                        inbound++;
19184                                /* first inbound? reset the offset */
19185                                if (inbound == 1)
19186                                        offset = 0;
19187                                bf_set(lpfc_sli4_sge_offset, sgl, offset);
19188                                bf_set(lpfc_sli4_sge_type, sgl,
19189                                        LPFC_SGE_TYPE_DATA);
19190                                offset += bde.tus.f.bdeSize;
19191                                break;
19192                        case CMD_FCP_TRSP64_WQE:
19193                                bf_set(lpfc_sli4_sge_offset, sgl, 0);
19194                                bf_set(lpfc_sli4_sge_type, sgl,
19195                                        LPFC_SGE_TYPE_DATA);
19196                                break;
19197                        case CMD_FCP_TSEND64_WQE:
19198                        case CMD_FCP_TRECEIVE64_WQE:
19199                                bf_set(lpfc_sli4_sge_type, sgl,
19200                                        bpl->tus.f.bdeFlags);
19201                                if (i < 3)
19202                                        offset = 0;
19203                                else
19204                                        offset += bde.tus.f.bdeSize;
19205                                bf_set(lpfc_sli4_sge_offset, sgl, offset);
19206                                break;
19207                        }
19208                        sgl->word2 = cpu_to_le32(sgl->word2);
19209                        bpl++;
19210                        sgl++;
19211                }
19212        } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19213                /* The addrHigh and addrLow fields of the BDE have not
19214                 * been byteswapped yet so they need to be swapped
19215                 * before putting them in the sgl.
19216                 */
19217                sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19218                sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19219                sgl->word2 = le32_to_cpu(sgl->word2);
19220                bf_set(lpfc_sli4_sge_last, sgl, 1);
19221                sgl->word2 = cpu_to_le32(sgl->word2);
19222                sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19223        }
19224        return sglq->sli4_xritag;
19225}
19226
19227/**
19228 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19229 * @phba: Pointer to HBA context object.
19230 * @ring_number: Base sli ring number
19231 * @pwqe: Pointer to command WQE.
19232 **/
19233int
19234lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19235                    struct lpfc_iocbq *pwqe)
19236{
19237        union lpfc_wqe128 *wqe = &pwqe->wqe;
19238        struct lpfc_nvmet_rcv_ctx *ctxp;
19239        struct lpfc_queue *wq;
19240        struct lpfc_sglq *sglq;
19241        struct lpfc_sli_ring *pring;
19242        unsigned long iflags;
19243        uint32_t ret = 0;
19244
19245        /* NVME_LS and NVME_LS ABTS requests. */
19246        if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19247                pring =  phba->sli4_hba.nvmels_wq->pring;
19248                spin_lock_irqsave(&pring->ring_lock, iflags);
19249                sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19250                if (!sglq) {
19251                        spin_unlock_irqrestore(&pring->ring_lock, iflags);
19252                        return WQE_BUSY;
19253                }
19254                pwqe->sli4_lxritag = sglq->sli4_lxritag;
19255                pwqe->sli4_xritag = sglq->sli4_xritag;
19256                if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19257                        spin_unlock_irqrestore(&pring->ring_lock, iflags);
19258                        return WQE_ERROR;
19259                }
19260                bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19261                       pwqe->sli4_xritag);
19262                ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19263                if (ret) {
19264                        spin_unlock_irqrestore(&pring->ring_lock, iflags);
19265                        return ret;
19266                }
19267
19268                lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19269                spin_unlock_irqrestore(&pring->ring_lock, iflags);
19270                return 0;
19271        }
19272
19273        /* NVME_FCREQ and NVME_ABTS requests */
19274        if (pwqe->iocb_flag & LPFC_IO_NVME) {
19275                /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19276                pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19277
19278                spin_lock_irqsave(&pring->ring_lock, iflags);
19279                wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19280                bf_set(wqe_cqid, &wqe->generic.wqe_com,
19281                      phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
19282                ret = lpfc_sli4_wq_put(wq, wqe);
19283                if (ret) {
19284                        spin_unlock_irqrestore(&pring->ring_lock, iflags);
19285                        return ret;
19286                }
19287                lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19288                spin_unlock_irqrestore(&pring->ring_lock, iflags);
19289                return 0;
19290        }
19291
19292        /* NVMET requests */
19293        if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19294                /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19295                pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19296
19297                spin_lock_irqsave(&pring->ring_lock, iflags);
19298                ctxp = pwqe->context2;
19299                sglq = ctxp->ctxbuf->sglq;
19300                if (pwqe->sli4_xritag ==  NO_XRI) {
19301                        pwqe->sli4_lxritag = sglq->sli4_lxritag;
19302                        pwqe->sli4_xritag = sglq->sli4_xritag;
19303                }
19304                bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19305                       pwqe->sli4_xritag);
19306                wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19307                bf_set(wqe_cqid, &wqe->generic.wqe_com,
19308                      phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
19309                ret = lpfc_sli4_wq_put(wq, wqe);
19310                if (ret) {
19311                        spin_unlock_irqrestore(&pring->ring_lock, iflags);
19312                        return ret;
19313                }
19314                lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19315                spin_unlock_irqrestore(&pring->ring_lock, iflags);
19316                return 0;
19317        }
19318        return WQE_ERROR;
19319}
19320