1 /*******************************************************************
 
   2  * This file is part of the Emulex Linux Device Driver for         *
 
   3  * Fibre Channel Host Bus Adapters.                                *
 
   4  * Copyright (C) 2004-2008 Emulex.  All rights reserved.           *
 
   5  * EMULEX and SLI are trademarks of Emulex.                        *
 
   7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
 
   9  * This program is free software; you can redistribute it and/or   *
 
  10  * modify it under the terms of version 2 of the GNU General       *
 
  11  * Public License as published by the Free Software Foundation.    *
 
  12  * This program is distributed in the hope that it will be useful. *
 
  13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
 
  14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
 
  15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
 
  16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
 
  17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
 
  18  * more details, a copy of which can be found in the file COPYING  *
 
  19  * included with this package.                                     *
 
  20  *******************************************************************/
 
  22 #include <linux/blkdev.h>
 
  23 #include <linux/pci.h>
 
  24 #include <linux/interrupt.h>
 
  25 #include <linux/delay.h>
 
  27 #include <scsi/scsi.h>
 
  28 #include <scsi/scsi_cmnd.h>
 
  29 #include <scsi/scsi_device.h>
 
  30 #include <scsi/scsi_host.h>
 
  31 #include <scsi/scsi_transport_fc.h>
 
  35 #include "lpfc_disc.h"
 
  36 #include "lpfc_scsi.h"
 
  38 #include "lpfc_crtn.h"
 
  39 #include "lpfc_logmsg.h"
 
  40 #include "lpfc_compat.h"
 
  41 #include "lpfc_debugfs.h"
 
  44  * Define macro to log: Mailbox command x%x cannot issue Data
 
  45  * This allows multiple uses of lpfc_msgBlk0311
 
  46  * w/o perturbing log msg utility.
 
  48 #define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
 
  49                         lpfc_printf_log(phba, \
 
  52                                 "(%d):0311 Mailbox command x%x cannot " \
 
  53                                 "issue Data: x%x x%x x%x\n", \
 
  54                                 pmbox->vport ? pmbox->vport->vpi : 0, \
 
  55                                 pmbox->mb.mbxCommand,           \
 
  56                                 phba->pport->port_state,        \
 
  61 /* There are only four IOCB completion types. */
 
  62 typedef enum _lpfc_iocb_type {
 
  69                 /* SLI-2/SLI-3 provide different sized iocbs.  Given a pointer
 
  70                  * to the start of the ring, and the slot number of the
 
  71                  * desired iocb entry, calc a pointer to that entry.
 
  73 static inline IOCB_t *
 
  74 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 
  76         return (IOCB_t *) (((char *) pring->cmdringaddr) +
 
  77                            pring->cmdidx * phba->iocb_cmd_size);
 
  80 static inline IOCB_t *
 
  81 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 
  83         return (IOCB_t *) (((char *) pring->rspringaddr) +
 
  84                            pring->rspidx * phba->iocb_rsp_size);
 
  87 static struct lpfc_iocbq *
 
  88 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
 
  90         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
 
  91         struct lpfc_iocbq * iocbq = NULL;
 
  93         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
 
  98 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
 
 100         struct lpfc_iocbq * iocbq = NULL;
 
 101         unsigned long iflags;
 
 103         spin_lock_irqsave(&phba->hbalock, iflags);
 
 104         iocbq = __lpfc_sli_get_iocbq(phba);
 
 105         spin_unlock_irqrestore(&phba->hbalock, iflags);
 
 110 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
 
 112         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
 
 115          * Clean all volatile data fields, preserve iotag and node struct.
 
 117         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
 
 118         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
 
 122 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
 
 124         unsigned long iflags;
 
 127          * Clean all volatile data fields, preserve iotag and node struct.
 
 129         spin_lock_irqsave(&phba->hbalock, iflags);
 
 130         __lpfc_sli_release_iocbq(phba, iocbq);
 
 131         spin_unlock_irqrestore(&phba->hbalock, iflags);
 
 135  * Translate the iocb command to an iocb command type used to decide the final
 
 136  * disposition of each completed IOCB.
 
 138 static lpfc_iocb_type
 
 139 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
 
 141         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
 
 143         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
 
 147         case CMD_XMIT_SEQUENCE_CR:
 
 148         case CMD_XMIT_SEQUENCE_CX:
 
 149         case CMD_XMIT_BCAST_CN:
 
 150         case CMD_XMIT_BCAST_CX:
 
 151         case CMD_ELS_REQUEST_CR:
 
 152         case CMD_ELS_REQUEST_CX:
 
 153         case CMD_CREATE_XRI_CR:
 
 154         case CMD_CREATE_XRI_CX:
 
 156         case CMD_XMIT_ELS_RSP_CX:
 
 158         case CMD_FCP_IWRITE_CR:
 
 159         case CMD_FCP_IWRITE_CX:
 
 160         case CMD_FCP_IREAD_CR:
 
 161         case CMD_FCP_IREAD_CX:
 
 162         case CMD_FCP_ICMND_CR:
 
 163         case CMD_FCP_ICMND_CX:
 
 164         case CMD_FCP_TSEND_CX:
 
 165         case CMD_FCP_TRSP_CX:
 
 166         case CMD_FCP_TRECEIVE_CX:
 
 167         case CMD_FCP_AUTO_TRSP_CX:
 
 168         case CMD_ADAPTER_MSG:
 
 169         case CMD_ADAPTER_DUMP:
 
 170         case CMD_XMIT_SEQUENCE64_CR:
 
 171         case CMD_XMIT_SEQUENCE64_CX:
 
 172         case CMD_XMIT_BCAST64_CN:
 
 173         case CMD_XMIT_BCAST64_CX:
 
 174         case CMD_ELS_REQUEST64_CR:
 
 175         case CMD_ELS_REQUEST64_CX:
 
 176         case CMD_FCP_IWRITE64_CR:
 
 177         case CMD_FCP_IWRITE64_CX:
 
 178         case CMD_FCP_IREAD64_CR:
 
 179         case CMD_FCP_IREAD64_CX:
 
 180         case CMD_FCP_ICMND64_CR:
 
 181         case CMD_FCP_ICMND64_CX:
 
 182         case CMD_FCP_TSEND64_CX:
 
 183         case CMD_FCP_TRSP64_CX:
 
 184         case CMD_FCP_TRECEIVE64_CX:
 
 185         case CMD_GEN_REQUEST64_CR:
 
 186         case CMD_GEN_REQUEST64_CX:
 
 187         case CMD_XMIT_ELS_RSP64_CX:
 
 188                 type = LPFC_SOL_IOCB;
 
 190         case CMD_ABORT_XRI_CN:
 
 191         case CMD_ABORT_XRI_CX:
 
 192         case CMD_CLOSE_XRI_CN:
 
 193         case CMD_CLOSE_XRI_CX:
 
 194         case CMD_XRI_ABORTED_CX:
 
 195         case CMD_ABORT_MXRI64_CN:
 
 196                 type = LPFC_ABORT_IOCB;
 
 198         case CMD_RCV_SEQUENCE_CX:
 
 199         case CMD_RCV_ELS_REQ_CX:
 
 200         case CMD_RCV_SEQUENCE64_CX:
 
 201         case CMD_RCV_ELS_REQ64_CX:
 
 202         case CMD_ASYNC_STATUS:
 
 203         case CMD_IOCB_RCV_SEQ64_CX:
 
 204         case CMD_IOCB_RCV_ELS64_CX:
 
 205         case CMD_IOCB_RCV_CONT64_CX:
 
 206         case CMD_IOCB_RET_XRI64_CX:
 
 207                 type = LPFC_UNSOL_IOCB;
 
 209         case CMD_IOCB_XMIT_MSEQ64_CR:
 
 210         case CMD_IOCB_XMIT_MSEQ64_CX:
 
 211         case CMD_IOCB_RCV_SEQ_LIST64_CX:
 
 212         case CMD_IOCB_RCV_ELS_LIST64_CX:
 
 213         case CMD_IOCB_CLOSE_EXTENDED_CN:
 
 214         case CMD_IOCB_ABORT_EXTENDED_CN:
 
 215         case CMD_IOCB_RET_HBQE64_CN:
 
 216         case CMD_IOCB_FCP_IBIDIR64_CR:
 
 217         case CMD_IOCB_FCP_IBIDIR64_CX:
 
 218         case CMD_IOCB_FCP_ITASKMGT64_CX:
 
 219         case CMD_IOCB_LOGENTRY_CN:
 
 220         case CMD_IOCB_LOGENTRY_ASYNC_CN:
 
 221                 printk("%s - Unhandled SLI-3 Command x%x\n",
 
 222                                 __FUNCTION__, iocb_cmnd);
 
 223                 type = LPFC_UNKNOWN_IOCB;
 
 226                 type = LPFC_UNKNOWN_IOCB;
 
 234 lpfc_sli_ring_map(struct lpfc_hba *phba)
 
 236         struct lpfc_sli *psli = &phba->sli;
 
 241         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 
 245         phba->link_state = LPFC_INIT_MBX_CMDS;
 
 246         for (i = 0; i < psli->num_rings; i++) {
 
 247                 lpfc_config_ring(phba, i, pmb);
 
 248                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
 
 249                 if (rc != MBX_SUCCESS) {
 
 250                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
 
 251                                         "0446 Adapter failed to init (%d), "
 
 252                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
 
 254                                         rc, pmbox->mbxCommand,
 
 255                                         pmbox->mbxStatus, i);
 
 256                         phba->link_state = LPFC_HBA_ERROR;
 
 261         mempool_free(pmb, phba->mbox_mem_pool);
 
 266 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 
 267                         struct lpfc_iocbq *piocb)
 
 269         list_add_tail(&piocb->list, &pring->txcmplq);
 
 270         pring->txcmplq_cnt++;
 
 271         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
 
 272            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
 
 273            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
 
 277                         mod_timer(&piocb->vport->els_tmofunc,
 
 278                                   jiffies + HZ * (phba->fc_ratov << 1));
 
 285 static struct lpfc_iocbq *
 
 286 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 
 288         struct lpfc_iocbq *cmd_iocb;
 
 290         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
 
 291         if (cmd_iocb != NULL)
 
 297 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 
 299         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
 
 300                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
 
 301                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
 
 302         uint32_t  max_cmd_idx = pring->numCiocb;
 
 304         if ((pring->next_cmdidx == pring->cmdidx) &&
 
 305            (++pring->next_cmdidx >= max_cmd_idx))
 
 306                 pring->next_cmdidx = 0;
 
 308         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
 
 310                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
 
 312                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
 
 313                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
 
 314                                         "0315 Ring %d issue: portCmdGet %d "
 
 315                                         "is bigger then cmd ring %d\n",
 
 317                                         pring->local_getidx, max_cmd_idx);
 
 319                         phba->link_state = LPFC_HBA_ERROR;
 
 321                          * All error attention handlers are posted to
 
 324                         phba->work_ha |= HA_ERATT;
 
 325                         phba->work_hs = HS_FFER3;
 
 327                         /* hbalock should already be held */
 
 329                                 lpfc_worker_wake_up(phba);
 
 334                 if (pring->local_getidx == pring->next_cmdidx)
 
 338         return lpfc_cmd_iocb(phba, pring);
 
 342 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
 
 344         struct lpfc_iocbq **new_arr;
 
 345         struct lpfc_iocbq **old_arr;
 
 347         struct lpfc_sli *psli = &phba->sli;
 
 350         spin_lock_irq(&phba->hbalock);
 
 351         iotag = psli->last_iotag;
 
 352         if(++iotag < psli->iocbq_lookup_len) {
 
 353                 psli->last_iotag = iotag;
 
 354                 psli->iocbq_lookup[iotag] = iocbq;
 
 355                 spin_unlock_irq(&phba->hbalock);
 
 356                 iocbq->iotag = iotag;
 
 358         } else if (psli->iocbq_lookup_len < (0xffff
 
 359                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
 
 360                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
 
 361                 spin_unlock_irq(&phba->hbalock);
 
 362                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
 
 365                         spin_lock_irq(&phba->hbalock);
 
 366                         old_arr = psli->iocbq_lookup;
 
 367                         if (new_len <= psli->iocbq_lookup_len) {
 
 368                                 /* highly unprobable case */
 
 370                                 iotag = psli->last_iotag;
 
 371                                 if(++iotag < psli->iocbq_lookup_len) {
 
 372                                         psli->last_iotag = iotag;
 
 373                                         psli->iocbq_lookup[iotag] = iocbq;
 
 374                                         spin_unlock_irq(&phba->hbalock);
 
 375                                         iocbq->iotag = iotag;
 
 378                                 spin_unlock_irq(&phba->hbalock);
 
 381                         if (psli->iocbq_lookup)
 
 382                                 memcpy(new_arr, old_arr,
 
 383                                        ((psli->last_iotag  + 1) *
 
 384                                         sizeof (struct lpfc_iocbq *)));
 
 385                         psli->iocbq_lookup = new_arr;
 
 386                         psli->iocbq_lookup_len = new_len;
 
 387                         psli->last_iotag = iotag;
 
 388                         psli->iocbq_lookup[iotag] = iocbq;
 
 389                         spin_unlock_irq(&phba->hbalock);
 
 390                         iocbq->iotag = iotag;
 
 395                 spin_unlock_irq(&phba->hbalock);
 
 397         lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
 
 398                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
 
 405 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 
 406                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
 
 411         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
 
 413         if (pring->ringno == LPFC_ELS_RING) {
 
 414                 lpfc_debugfs_slow_ring_trc(phba,
 
 415                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
 
 416                         *(((uint32_t *) &nextiocb->iocb) + 4),
 
 417                         *(((uint32_t *) &nextiocb->iocb) + 6),
 
 418                         *(((uint32_t *) &nextiocb->iocb) + 7));
 
 422          * Issue iocb command to adapter
 
 424         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
 
 426         pring->stats.iocb_cmd++;
 
 429          * If there is no completion routine to call, we can release the
 
 430          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
 
 431          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
 
 433         if (nextiocb->iocb_cmpl)
 
 434                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
 
 436                 __lpfc_sli_release_iocbq(phba, nextiocb);
 
 439          * Let the HBA know what IOCB slot will be the next one the
 
 440          * driver will put a command into.
 
 442         pring->cmdidx = pring->next_cmdidx;
 
 443         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
 
 447 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 
 449         int ringno = pring->ringno;
 
 451         pring->flag |= LPFC_CALL_RING_AVAILABLE;
 
 456          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
 
 457          * The HBA will tell us when an IOCB entry is available.
 
 459         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
 
 460         readl(phba->CAregaddr); /* flush */
 
 462         pring->stats.iocb_cmd_full++;
 
 466 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 
 468         int ringno = pring->ringno;
 
 471          * Tell the HBA that there is work to do in this ring.
 
 474         writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
 
 475         readl(phba->CAregaddr); /* flush */
 
 479 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 
 482         struct lpfc_iocbq *nextiocb;
 
 486          *  (a) there is anything on the txq to send
 
 488          *  (c) link attention events can be processed (fcp ring only)
 
 489          *  (d) IOCB processing is not blocked by the outstanding mbox command.
 
 491         if (pring->txq_cnt &&
 
 492             lpfc_is_link_up(phba) &&
 
 493             (pring->ringno != phba->sli.fcp_ring ||
 
 494              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
 
 496                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
 
 497                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
 
 498                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
 
 501                         lpfc_sli_update_ring(phba, pring);
 
 503                         lpfc_sli_update_full_ring(phba, pring);
 
 509 static struct lpfc_hbq_entry *
 
 510 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
 
 512         struct hbq_s *hbqp = &phba->hbqs[hbqno];
 
 514         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
 
 515             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
 
 516                 hbqp->next_hbqPutIdx = 0;
 
 518         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
 
 519                 uint32_t raw_index = phba->hbq_get[hbqno];
 
 520                 uint32_t getidx = le32_to_cpu(raw_index);
 
 522                 hbqp->local_hbqGetIdx = getidx;
 
 524                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
 
 525                         lpfc_printf_log(phba, KERN_ERR,
 
 527                                         "1802 HBQ %d: local_hbqGetIdx "
 
 528                                         "%u is > than hbqp->entry_count %u\n",
 
 529                                         hbqno, hbqp->local_hbqGetIdx,
 
 532                         phba->link_state = LPFC_HBA_ERROR;
 
 536                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
 
 540         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
 
 545 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
 
 547         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
 
 548         struct hbq_dmabuf *hbq_buf;
 
 553         hbq_count = lpfc_sli_hbq_count();
 
 554         /* Return all memory used by all HBQs */
 
 555         spin_lock_irqsave(&phba->hbalock, flags);
 
 556         for (i = 0; i < hbq_count; ++i) {
 
 557                 list_for_each_entry_safe(dmabuf, next_dmabuf,
 
 558                                 &phba->hbqs[i].hbq_buffer_list, list) {
 
 559                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
 
 560                         list_del(&hbq_buf->dbuf.list);
 
 561                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
 
 563                 phba->hbqs[i].buffer_count = 0;
 
 565         /* Return all HBQ buffer that are in-fly */
 
 566         list_for_each_entry_safe(dmabuf, next_dmabuf,
 
 567                         &phba->hbqbuf_in_list, list) {
 
 568                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
 
 569                 list_del(&hbq_buf->dbuf.list);
 
 570                 if (hbq_buf->tag == -1) {
 
 571                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
 
 574                         hbqno = hbq_buf->tag >> 16;
 
 575                         if (hbqno >= LPFC_MAX_HBQS)
 
 576                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
 
 579                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
 
 584         /* Mark the HBQs not in use */
 
 585         phba->hbq_in_use = 0;
 
 586         spin_unlock_irqrestore(&phba->hbalock, flags);
 
 589 static struct lpfc_hbq_entry *
 
 590 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
 
 591                          struct hbq_dmabuf *hbq_buf)
 
 593         struct lpfc_hbq_entry *hbqe;
 
 594         dma_addr_t physaddr = hbq_buf->dbuf.phys;
 
 596         /* Get next HBQ entry slot to use */
 
 597         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
 
 599                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
 
 601                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
 
 602                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
 
 603                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
 
 604                 hbqe->bde.tus.f.bdeFlags = 0;
 
 605                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
 
 606                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
 
 608                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
 
 609                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
 
 611                 readl(phba->hbq_put + hbqno);
 
 612                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
 
 617 static struct lpfc_hbq_init lpfc_els_hbq = {
 
 622         .ring_mask = (1 << LPFC_ELS_RING),
 
 628 static struct lpfc_hbq_init lpfc_extra_hbq = {
 
 633         .ring_mask = (1 << LPFC_EXTRA_RING),
 
 639 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
 
 645 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
 
 647         uint32_t i, start, end;
 
 649         struct hbq_dmabuf *hbq_buffer;
 
 651         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
 
 654         start = phba->hbqs[hbqno].buffer_count;
 
 656         if (end > lpfc_hbq_defs[hbqno]->entry_count)
 
 657                 end = lpfc_hbq_defs[hbqno]->entry_count;
 
 659         /* Check whether HBQ is still in use */
 
 660         spin_lock_irqsave(&phba->hbalock, flags);
 
 661         if (!phba->hbq_in_use)
 
 664         /* Populate HBQ entries */
 
 665         for (i = start; i < end; i++) {
 
 666                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
 
 669                 hbq_buffer->tag = (i | (hbqno << 16));
 
 670                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
 
 671                         phba->hbqs[hbqno].buffer_count++;
 
 673                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
 
 677         spin_unlock_irqrestore(&phba->hbalock, flags);
 
 680         spin_unlock_irqrestore(&phba->hbalock, flags);
 
 685 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
 
 687         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
 
 688                                          lpfc_hbq_defs[qno]->add_count));
 
 692 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
 
 694         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
 
 695                                          lpfc_hbq_defs[qno]->init_count));
 
 698 static struct hbq_dmabuf *
 
 699 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
 
 701         struct lpfc_dmabuf *d_buf;
 
 702         struct hbq_dmabuf *hbq_buf;
 
 706         if (hbqno >= LPFC_MAX_HBQS)
 
 709         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
 
 710                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
 
 711                 if (hbq_buf->tag == tag) {
 
 715         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
 
 716                         "1803 Bad hbq tag. Data: x%x x%x\n",
 
 717                         tag, phba->hbqs[tag >> 16].buffer_count);
 
 722 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
 
 727                 hbqno = hbq_buffer->tag >> 16;
 
 728                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
 
 729                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
 
 735 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
 
 739         switch (mbxCommand) {
 
 743         case MBX_WRITE_VPARMS:
 
 744         case MBX_RUN_BIU_DIAG:
 
 747         case MBX_CONFIG_LINK:
 
 748         case MBX_CONFIG_RING:
 
 750         case MBX_READ_CONFIG:
 
 751         case MBX_READ_RCONFIG:
 
 753         case MBX_READ_STATUS:
 
 757         case MBX_READ_LNK_STAT:
 
 759         case MBX_UNREG_LOGIN:
 
 762         case MBX_DUMP_MEMORY:
 
 763         case MBX_DUMP_CONTEXT:
 
 768         case MBX_DEL_LD_ENTRY:
 
 769         case MBX_RUN_PROGRAM:
 
 771         case MBX_SET_VARIABLE:
 
 774         case MBX_CONFIG_FARP:
 
 777         case MBX_RUN_BIU_DIAG64:
 
 778         case MBX_CONFIG_PORT:
 
 779         case MBX_READ_SPARM64:
 
 781         case MBX_REG_LOGIN64:
 
 785         case MBX_LOAD_EXP_ROM:
 
 786         case MBX_ASYNCEVT_ENABLE:
 
 799 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
 
 801         wait_queue_head_t *pdone_q;
 
 802         unsigned long drvr_flag;
 
 805          * If pdone_q is empty, the driver thread gave up waiting and
 
 808         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
 
 809         spin_lock_irqsave(&phba->hbalock, drvr_flag);
 
 810         pdone_q = (wait_queue_head_t *) pmboxq->context1;
 
 812                 wake_up_interruptible(pdone_q);
 
 813         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 
 818 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 
 820         struct lpfc_dmabuf *mp;
 
 824         mp = (struct lpfc_dmabuf *) (pmb->context1);
 
 827                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
 
 832          * If a REG_LOGIN succeeded  after node is destroyed or node
 
 833          * is in re-discovery driver need to cleanup the RPI.
 
 835         if (!(phba->pport->load_flag & FC_UNLOADING) &&
 
 836             pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
 
 837             !pmb->mb.mbxStatus) {
 
 839                 rpi = pmb->mb.un.varWords[0];
 
 840                 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
 
 841                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
 
 842                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
 
 843                 if (rc != MBX_NOT_FINISHED)
 
 847         mempool_free(pmb, phba->mbox_mem_pool);
 
 852 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
 
 859         phba->sli.slistat.mbox_event++;
 
 861         /* Get all completed mailboxe buffers into the cmplq */
 
 862         spin_lock_irq(&phba->hbalock);
 
 863         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
 
 864         spin_unlock_irq(&phba->hbalock);
 
 866         /* Get a Mailbox buffer to setup mailbox commands for callback */
 
 868                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
 
 874                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
 
 876                                 lpfc_debugfs_disc_trc(pmb->vport,
 
 877                                         LPFC_DISC_TRC_MBOX_VPORT,
 
 878                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
 
 879                                         (uint32_t)pmbox->mbxCommand,
 
 880                                         pmbox->un.varWords[0],
 
 881                                         pmbox->un.varWords[1]);
 
 884                                 lpfc_debugfs_disc_trc(phba->pport,
 
 886                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
 
 887                                         (uint32_t)pmbox->mbxCommand,
 
 888                                         pmbox->un.varWords[0],
 
 889                                         pmbox->un.varWords[1]);
 
 894                  * It is a fatal error if unknown mbox command completion.
 
 896                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
 
 898                         /* Unknow mailbox command compl */
 
 899                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
 
 900                                         "(%d):0323 Unknown Mailbox command "
 
 902                                         pmb->vport ? pmb->vport->vpi : 0,
 
 904                         phba->link_state = LPFC_HBA_ERROR;
 
 905                         phba->work_hs = HS_FFER3;
 
 906                         lpfc_handle_eratt(phba);
 
 910                 if (pmbox->mbxStatus) {
 
 911                         phba->sli.slistat.mbox_stat_err++;
 
 912                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
 
 913                                 /* Mbox cmd cmpl error - RETRYing */
 
 914                                 lpfc_printf_log(phba, KERN_INFO,
 
 916                                                 "(%d):0305 Mbox cmd cmpl "
 
 917                                                 "error - RETRYing Data: x%x "
 
 919                                                 pmb->vport ? pmb->vport->vpi :0,
 
 922                                                 pmbox->un.varWords[0],
 
 923                                                 pmb->vport->port_state);
 
 924                                 pmbox->mbxStatus = 0;
 
 925                                 pmbox->mbxOwner = OWN_HOST;
 
 926                                 spin_lock_irq(&phba->hbalock);
 
 927                                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 
 928                                 spin_unlock_irq(&phba->hbalock);
 
 929                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
 
 930                                 if (rc == MBX_SUCCESS)
 
 935                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
 
 936                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
 
 937                                 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
 
 938                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
 
 939                                 pmb->vport ? pmb->vport->vpi : 0,
 
 942                                 *((uint32_t *) pmbox),
 
 943                                 pmbox->un.varWords[0],
 
 944                                 pmbox->un.varWords[1],
 
 945                                 pmbox->un.varWords[2],
 
 946                                 pmbox->un.varWords[3],
 
 947                                 pmbox->un.varWords[4],
 
 948                                 pmbox->un.varWords[5],
 
 949                                 pmbox->un.varWords[6],
 
 950                                 pmbox->un.varWords[7]);
 
 953                         pmb->mbox_cmpl(phba,pmb);
 
 958 static struct lpfc_dmabuf *
 
 959 lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
 
 961         struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
 
 963         void *virt;             /* virtual address ptr */
 
 964         dma_addr_t phys;        /* mapped address */
 
 967         /* Check whether HBQ is still in use */
 
 968         spin_lock_irqsave(&phba->hbalock, flags);
 
 969         if (!phba->hbq_in_use) {
 
 970                 spin_unlock_irqrestore(&phba->hbalock, flags);
 
 974         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
 
 975         if (hbq_entry == NULL) {
 
 976                 spin_unlock_irqrestore(&phba->hbalock, flags);
 
 979         list_del(&hbq_entry->dbuf.list);
 
 982         new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
 
 983         if (new_hbq_entry == NULL) {
 
 984                 list_add_tail(&hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
 
 985                 spin_unlock_irqrestore(&phba->hbalock, flags);
 
 986                 return &hbq_entry->dbuf;
 
 988         new_hbq_entry->tag = -1;
 
 989         phys = new_hbq_entry->dbuf.phys;
 
 990         virt = new_hbq_entry->dbuf.virt;
 
 991         new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
 
 992         new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
 
 993         hbq_entry->dbuf.phys = phys;
 
 994         hbq_entry->dbuf.virt = virt;
 
 995         lpfc_sli_free_hbq(phba, hbq_entry);
 
 996         list_add_tail(&new_hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
 
 997         spin_unlock_irqrestore(&phba->hbalock, flags);
 
 999         return &new_hbq_entry->dbuf;
 
1002 static struct lpfc_dmabuf *
 
1003 lpfc_sli_get_buff(struct lpfc_hba *phba,
 
1004                         struct lpfc_sli_ring *pring,
 
1007         if (tag & QUE_BUFTAG_BIT)
 
1008                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
 
1010                 return lpfc_sli_replace_hbqbuff(phba, tag);
 
1014 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 
1015                             struct lpfc_iocbq *saveq)
 
1019         uint32_t           Rctl, Type;
 
1021         struct lpfc_iocbq *iocbq;
 
1022         struct lpfc_dmabuf *dmzbuf;
 
1025         irsp = &(saveq->iocb);
 
1027         if (irsp->ulpStatus == IOSTAT_NEED_BUFFER)
 
1029         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
 
1030                 if (pring->lpfc_sli_rcv_async_status)
 
1031                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
 
1033                         lpfc_printf_log(phba,
 
1036                                         "0316 Ring %d handler: unexpected "
 
1037                                         "ASYNC_STATUS iocb received evt_code "
 
1040                                         irsp->un.asyncstat.evt_code);
 
1044         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
 
1045                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
 
1046                 if (irsp->ulpBdeCount > 0) {
 
1047                         dmzbuf = lpfc_sli_get_buff(phba, pring,
 
1048                                         irsp->un.ulpWord[3]);
 
1049                         lpfc_in_buf_free(phba, dmzbuf);
 
1052                 if (irsp->ulpBdeCount > 1) {
 
1053                         dmzbuf = lpfc_sli_get_buff(phba, pring,
 
1054                                         irsp->unsli3.sli3Words[3]);
 
1055                         lpfc_in_buf_free(phba, dmzbuf);
 
1058                 if (irsp->ulpBdeCount > 2) {
 
1059                         dmzbuf = lpfc_sli_get_buff(phba, pring,
 
1060                                 irsp->unsli3.sli3Words[7]);
 
1061                         lpfc_in_buf_free(phba, dmzbuf);
 
1067         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
 
1068                 if (irsp->ulpBdeCount != 0) {
 
1069                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
 
1070                                                 irsp->un.ulpWord[3]);
 
1071                         if (!saveq->context2)
 
1072                                 lpfc_printf_log(phba,
 
1075                                         "0341 Ring %d Cannot find buffer for "
 
1076                                         "an unsolicited iocb. tag 0x%x\n",
 
1078                                         irsp->un.ulpWord[3]);
 
1080                 if (irsp->ulpBdeCount == 2) {
 
1081                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
 
1082                                                 irsp->unsli3.sli3Words[7]);
 
1083                         if (!saveq->context3)
 
1084                                 lpfc_printf_log(phba,
 
1087                                         "0342 Ring %d Cannot find buffer for an"
 
1088                                         " unsolicited iocb. tag 0x%x\n",
 
1090                                         irsp->unsli3.sli3Words[7]);
 
1092                 list_for_each_entry(iocbq, &saveq->list, list) {
 
1093                         irsp = &(iocbq->iocb);
 
1094                         if (irsp->ulpBdeCount != 0) {
 
1095                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
 
1096                                                         irsp->un.ulpWord[3]);
 
1097                                 if (!iocbq->context2)
 
1098                                         lpfc_printf_log(phba,
 
1101                                                 "0343 Ring %d Cannot find "
 
1102                                                 "buffer for an unsolicited iocb"
 
1103                                                 ". tag 0x%x\n", pring->ringno,
 
1104                                                 irsp->un.ulpWord[3]);
 
1106                         if (irsp->ulpBdeCount == 2) {
 
1107                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
 
1108                                                 irsp->unsli3.sli3Words[7]);
 
1109                                 if (!iocbq->context3)
 
1110                                         lpfc_printf_log(phba,
 
1113                                                 "0344 Ring %d Cannot find "
 
1114                                                 "buffer for an unsolicited "
 
1117                                                 irsp->unsli3.sli3Words[7]);
 
1121         if (irsp->ulpBdeCount != 0 &&
 
1122             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
 
1123              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
 
1126                 /* search continue save q for same XRI */
 
1127                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
 
1128                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
 
1129                                 list_add_tail(&saveq->list, &iocbq->list);
 
1135                         list_add_tail(&saveq->clist,
 
1136                                       &pring->iocb_continue_saveq);
 
1137                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
 
1138                         list_del_init(&iocbq->clist);
 
1140                         irsp = &(saveq->iocb);
 
1144         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
 
1145             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
 
1146             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
 
1150                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
 
1151                 Rctl = w5p->hcsw.Rctl;
 
1152                 Type = w5p->hcsw.Type;
 
1154                 /* Firmware Workaround */
 
1155                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
 
1156                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
 
1157                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
 
1160                         w5p->hcsw.Rctl = Rctl;
 
1161                         w5p->hcsw.Type = Type;
 
1165         /* unSolicited Responses */
 
1166         if (pring->prt[0].profile) {
 
1167                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
 
1168                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
 
1172                 /* We must search, based on rctl / type
 
1173                    for the right routine */
 
1174                 for (i = 0; i < pring->num_mask; i++) {
 
1175                         if ((pring->prt[i].rctl == Rctl)
 
1176                             && (pring->prt[i].type == Type)) {
 
1177                                 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
 
1178                                         (pring->prt[i].lpfc_sli_rcv_unsol_event)
 
1179                                                         (phba, pring, saveq);
 
1186                 /* Unexpected Rctl / Type received */
 
1187                 /* Ring <ringno> handler: unexpected
 
1188                    Rctl <Rctl> Type <Type> received */
 
1189                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
 
1190                                 "0313 Ring %d handler: unexpected Rctl x%x "
 
1191                                 "Type x%x received\n",
 
1192                                 pring->ringno, Rctl, Type);
 
1197 static struct lpfc_iocbq *
 
1198 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
 
1199                       struct lpfc_sli_ring *pring,
 
1200                       struct lpfc_iocbq *prspiocb)
 
1202         struct lpfc_iocbq *cmd_iocb = NULL;
 
1205         iotag = prspiocb->iocb.ulpIoTag;
 
1207         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
 
1208                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
 
1209                 list_del_init(&cmd_iocb->list);
 
1210                 pring->txcmplq_cnt--;
 
1214         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
 
1215                         "0317 iotag x%x is out off "
 
1216                         "range: max iotag x%x wd0 x%x\n",
 
1217                         iotag, phba->sli.last_iotag,
 
1218                         *(((uint32_t *) &prspiocb->iocb) + 7));
 
1223 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 
1224                           struct lpfc_iocbq *saveq)
 
1226         struct lpfc_iocbq *cmdiocbp;
 
1228         unsigned long iflag;
 
1230         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
 
1231         spin_lock_irqsave(&phba->hbalock, iflag);
 
1232         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
 
1233         spin_unlock_irqrestore(&phba->hbalock, iflag);
 
1236                 if (cmdiocbp->iocb_cmpl) {
 
1238                          * Post all ELS completions to the worker thread.
 
1239                          * All other are passed to the completion callback.
 
1241                         if (pring->ringno == LPFC_ELS_RING) {
 
1242                                 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
 
1243                                         cmdiocbp->iocb_flag &=
 
1244                                                 ~LPFC_DRIVER_ABORTED;
 
1245                                         saveq->iocb.ulpStatus =
 
1246                                                 IOSTAT_LOCAL_REJECT;
 
1247                                         saveq->iocb.un.ulpWord[4] =
 
1250                                         /* Firmware could still be in progress
 
1251                                          * of DMAing payload, so don't free data
 
1252                                          * buffer till after a hbeat.
 
1254                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
 
1257                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
 
1259                         lpfc_sli_release_iocbq(phba, cmdiocbp);
 
1262                  * Unknown initiating command based on the response iotag.
 
1263                  * This could be the case on the ELS ring because of
 
1266                 if (pring->ringno != LPFC_ELS_RING) {
 
1268                          * Ring <ringno> handler: unexpected completion IoTag
 
1271                         lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
 
1272                                          "0322 Ring %d handler: "
 
1273                                          "unexpected completion IoTag x%x "
 
1274                                          "Data: x%x x%x x%x x%x\n",
 
1276                                          saveq->iocb.ulpIoTag,
 
1277                                          saveq->iocb.ulpStatus,
 
1278                                          saveq->iocb.un.ulpWord[4],
 
1279                                          saveq->iocb.ulpCommand,
 
1280                                          saveq->iocb.ulpContext);
 
1288 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 
1290         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
 
1291                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
 
1292                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
 
1294          * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
 
1295          * rsp ring <portRspMax>
 
1297         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
 
1298                         "0312 Ring %d handler: portRspPut %d "
 
1299                         "is bigger then rsp ring %d\n",
 
1300                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
 
1303         phba->link_state = LPFC_HBA_ERROR;
 
1306          * All error attention handlers are posted to
 
1309         phba->work_ha |= HA_ERATT;
 
1310         phba->work_hs = HS_FFER3;
 
1312         /* hbalock should already be held */
 
1313         if (phba->work_wait)
 
1314                 lpfc_worker_wake_up(phba);
 
1319 void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
 
1321         struct lpfc_sli      *psli  = &phba->sli;
 
1322         struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
 
1323         IOCB_t *irsp = NULL;
 
1324         IOCB_t *entry = NULL;
 
1325         struct lpfc_iocbq *cmdiocbq = NULL;
 
1326         struct lpfc_iocbq rspiocbq;
 
1327         struct lpfc_pgp *pgp;
 
1329         uint32_t portRspPut, portRspMax;
 
1331         uint32_t rsp_cmpl = 0;
 
1333         unsigned long iflags;
 
1335         pring->stats.iocb_event++;
 
1337         pgp = (phba->sli_rev == 3) ?
 
1338                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
 
1339                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
 
1343          * The next available response entry should never exceed the maximum
 
1344          * entries.  If it does, treat it as an adapter hardware error.
 
1346         portRspMax = pring->numRiocb;
 
1347         portRspPut = le32_to_cpu(pgp->rspPutInx);
 
1348         if (unlikely(portRspPut >= portRspMax)) {
 
1349                 lpfc_sli_rsp_pointers_error(phba, pring);
 
1354         while (pring->rspidx != portRspPut) {
 
1355                 entry = lpfc_resp_iocb(phba, pring);
 
1356                 if (++pring->rspidx >= portRspMax)
 
1359                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
 
1360                                       (uint32_t *) &rspiocbq.iocb,
 
1361                                       phba->iocb_rsp_size);
 
1362                 irsp = &rspiocbq.iocb;
 
1363                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
 
1364                 pring->stats.iocb_rsp++;
 
1367                 if (unlikely(irsp->ulpStatus)) {
 
1368                         /* Rsp ring <ringno> error: IOCB */
 
1369                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
 
1370                                         "0326 Rsp Ring %d error: IOCB Data: "
 
1371                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
 
1373                                         irsp->un.ulpWord[0],
 
1374                                         irsp->un.ulpWord[1],
 
1375                                         irsp->un.ulpWord[2],
 
1376                                         irsp->un.ulpWord[3],
 
1377                                         irsp->un.ulpWord[4],
 
1378                                         irsp->un.ulpWord[5],
 
1379                                         *(((uint32_t *) irsp) + 6),
 
1380                                         *(((uint32_t *) irsp) + 7));
 
1384                 case LPFC_ABORT_IOCB:
 
1387                          * Idle exchange closed via ABTS from port.  No iocb
 
1388                          * resources need to be recovered.
 
1390                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
 
1391                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 
1392                                                 "0314 IOCB cmd 0x%x "
 
1393                                                 "processed. Skipping "
 
1399                         spin_lock_irqsave(&phba->hbalock, iflags);
 
1400                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
 
1402                         spin_unlock_irqrestore(&phba->hbalock, iflags);
 
1403                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
 
1404                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
 
1409                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
 
1410                                 char adaptermsg[LPFC_MAX_ADPTMSG];
 
1411                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
 
1412                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
 
1414                                 dev_warn(&((phba->pcidev)->dev),
 
1416                                          phba->brd_no, adaptermsg);
 
1418                                 /* Unknown IOCB command */
 
1419                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
 
1420                                                 "0321 Unknown IOCB command "
 
1421                                                 "Data: x%x, x%x x%x x%x x%x\n",
 
1422                                                 type, irsp->ulpCommand,
 
1431                  * The response IOCB has been processed.  Update the ring
 
1432                  * pointer in SLIM.  If the port response put pointer has not
 
1433                  * been updated, sync the pgp->rspPutInx and fetch the new port
 
1434                  * response put pointer.
 
1436                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
 
1438                 if (pring->rspidx == portRspPut)
 
1439                         portRspPut = le32_to_cpu(pgp->rspPutInx);
 
1442         ha_copy = readl(phba->HAregaddr);
 
1443         ha_copy >>= (LPFC_FCP_RING * 4);
 
1445         if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
 
1446                 spin_lock_irqsave(&phba->hbalock, iflags);
 
1447                 pring->stats.iocb_rsp_full++;
 
1448                 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
 
1449                 writel(status, phba->CAregaddr);
 
1450                 readl(phba->CAregaddr);
 
1451                 spin_unlock_irqrestore(&phba->hbalock, iflags);
 
1453         if ((ha_copy & HA_R0CE_RSP) &&
 
1454             (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
 
1455                 spin_lock_irqsave(&phba->hbalock, iflags);
 
1456                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
 
1457                 pring->stats.iocb_cmd_empty++;
 
1459                 /* Force update of the local copy of cmdGetInx */
 
1460                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
 
1461                 lpfc_sli_resume_iocb(phba, pring);
 
1463                 if ((pring->lpfc_sli_cmd_available))
 
1464                         (pring->lpfc_sli_cmd_available) (phba, pring);
 
1466                 spin_unlock_irqrestore(&phba->hbalock, iflags);
 
1473  * This routine presumes LPFC_FCP_RING handling and doesn't bother
 
1474  * to check it explicitly.
 
1477 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
 
1478                                 struct lpfc_sli_ring *pring, uint32_t mask)
 
1480         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
 
1481                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
 
1482                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
 
1483         IOCB_t *irsp = NULL;
 
1484         IOCB_t *entry = NULL;
 
1485         struct lpfc_iocbq *cmdiocbq = NULL;
 
1486         struct lpfc_iocbq rspiocbq;
 
1488         uint32_t portRspPut, portRspMax;
 
1490         lpfc_iocb_type type;
 
1491         unsigned long iflag;
 
1492         uint32_t rsp_cmpl = 0;
 
1494         spin_lock_irqsave(&phba->hbalock, iflag);
 
1495         pring->stats.iocb_event++;
 
1498          * The next available response entry should never exceed the maximum
 
1499          * entries.  If it does, treat it as an adapter hardware error.
 
1501         portRspMax = pring->numRiocb;
 
1502         portRspPut = le32_to_cpu(pgp->rspPutInx);
 
1503         if (unlikely(portRspPut >= portRspMax)) {
 
1504                 lpfc_sli_rsp_pointers_error(phba, pring);
 
1505                 spin_unlock_irqrestore(&phba->hbalock, iflag);
 
1510         while (pring->rspidx != portRspPut) {
 
1512                  * Fetch an entry off the ring and copy it into a local data
 
1513                  * structure.  The copy involves a byte-swap since the
 
1514                  * network byte order and pci byte orders are different.
 
1516                 entry = lpfc_resp_iocb(phba, pring);
 
1517                 phba->last_completion_time = jiffies;
 
1519                 if (++pring->rspidx >= portRspMax)
 
1522                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
 
1523                                       (uint32_t *) &rspiocbq.iocb,
 
1524                                       phba->iocb_rsp_size);
 
1525                 INIT_LIST_HEAD(&(rspiocbq.list));
 
1526                 irsp = &rspiocbq.iocb;
 
1528                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
 
1529                 pring->stats.iocb_rsp++;
 
1532                 if (unlikely(irsp->ulpStatus)) {
 
1534                          * If resource errors reported from HBA, reduce
 
1535                          * queuedepths of the SCSI device.
 
1537                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
 
1538                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
 
1539                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
 
1540                                 lpfc_adjust_queue_depth(phba);
 
1541                                 spin_lock_irqsave(&phba->hbalock, iflag);
 
1544                         /* Rsp ring <ringno> error: IOCB */
 
1545                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
 
1546                                         "0336 Rsp Ring %d error: IOCB Data: "
 
1547                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
 
1549                                         irsp->un.ulpWord[0],
 
1550                                         irsp->un.ulpWord[1],
 
1551                                         irsp->un.ulpWord[2],
 
1552                                         irsp->un.ulpWord[3],
 
1553                                         irsp->un.ulpWord[4],
 
1554                                         irsp->un.ulpWord[5],
 
1555                                         *(((uint32_t *) irsp) + 6),
 
1556                                         *(((uint32_t *) irsp) + 7));
 
1560                 case LPFC_ABORT_IOCB:
 
1563                          * Idle exchange closed via ABTS from port.  No iocb
 
1564                          * resources need to be recovered.
 
1566                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
 
1567                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 
1568                                                 "0333 IOCB cmd 0x%x"
 
1569                                                 " processed. Skipping"
 
1575                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
 
1577                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
 
1578                                 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
 
1579                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
 
1582                                         spin_unlock_irqrestore(&phba->hbalock,
 
1584                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
 
1586                                         spin_lock_irqsave(&phba->hbalock,
 
1591                 case LPFC_UNSOL_IOCB:
 
1592                         spin_unlock_irqrestore(&phba->hbalock, iflag);
 
1593                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
 
1594                         spin_lock_irqsave(&phba->hbalock, iflag);
 
1597                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
 
1598                                 char adaptermsg[LPFC_MAX_ADPTMSG];
 
1599                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
 
1600                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
 
1602                                 dev_warn(&((phba->pcidev)->dev),
 
1604                                          phba->brd_no, adaptermsg);
 
1606                                 /* Unknown IOCB command */
 
1607                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
 
1608                                                 "0334 Unknown IOCB command "
 
1609                                                 "Data: x%x, x%x x%x x%x x%x\n",
 
1610                                                 type, irsp->ulpCommand,
 
1619                  * The response IOCB has been processed.  Update the ring
 
1620                  * pointer in SLIM.  If the port response put pointer has not
 
1621                  * been updated, sync the pgp->rspPutInx and fetch the new port
 
1622                  * response put pointer.
 
1624                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
 
1626                 if (pring->rspidx == portRspPut)
 
1627                         portRspPut = le32_to_cpu(pgp->rspPutInx);
 
1630         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
 
1631                 pring->stats.iocb_rsp_full++;
 
1632                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
 
1633                 writel(status, phba->CAregaddr);
 
1634                 readl(phba->CAregaddr);
 
1636         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
 
1637                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
 
1638                 pring->stats.iocb_cmd_empty++;
 
1640                 /* Force update of the local copy of cmdGetInx */
 
1641                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
 
1642                 lpfc_sli_resume_iocb(phba, pring);
 
1644                 if ((pring->lpfc_sli_cmd_available))
 
1645                         (pring->lpfc_sli_cmd_available) (phba, pring);
 
1649         spin_unlock_irqrestore(&phba->hbalock, iflag);
 
1654 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
 
1655                                 struct lpfc_sli_ring *pring, uint32_t mask)
 
1657         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
 
1658                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
 
1659                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
 
1661         IOCB_t *irsp = NULL;
 
1662         struct lpfc_iocbq *rspiocbp = NULL;
 
1663         struct lpfc_iocbq *next_iocb;
 
1664         struct lpfc_iocbq *cmdiocbp;
 
1665         struct lpfc_iocbq *saveq;
 
1666         uint8_t iocb_cmd_type;
 
1667         lpfc_iocb_type type;
 
1668         uint32_t status, free_saveq;
 
1669         uint32_t portRspPut, portRspMax;
 
1671         unsigned long iflag;
 
1673         spin_lock_irqsave(&phba->hbalock, iflag);
 
1674         pring->stats.iocb_event++;
 
1677          * The next available response entry should never exceed the maximum
 
1678          * entries.  If it does, treat it as an adapter hardware error.
 
1680         portRspMax = pring->numRiocb;
 
1681         portRspPut = le32_to_cpu(pgp->rspPutInx);
 
1682         if (portRspPut >= portRspMax) {
 
1684                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
 
1685                  * rsp ring <portRspMax>
 
1687                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
 
1688                                 "0303 Ring %d handler: portRspPut %d "
 
1689                                 "is bigger then rsp ring %d\n",
 
1690                                 pring->ringno, portRspPut, portRspMax);
 
1692                 phba->link_state = LPFC_HBA_ERROR;
 
1693                 spin_unlock_irqrestore(&phba->hbalock, iflag);
 
1695                 phba->work_hs = HS_FFER3;
 
1696                 lpfc_handle_eratt(phba);
 
1702         while (pring->rspidx != portRspPut) {
 
1704                  * Build a completion list and call the appropriate handler.
 
1705                  * The process is to get the next available response iocb, get
 
1706                  * a free iocb from the list, copy the response data into the
 
1707                  * free iocb, insert to the continuation list, and update the
 
1708                  * next response index to slim.  This process makes response
 
1709                  * iocb's in the ring available to DMA as fast as possible but
 
1710                  * pays a penalty for a copy operation.  Since the iocb is
 
1711                  * only 32 bytes, this penalty is considered small relative to
 
1712                  * the PCI reads for register values and a slim write.  When
 
1713                  * the ulpLe field is set, the entire Command has been
 
1716                 entry = lpfc_resp_iocb(phba, pring);
 
1718                 phba->last_completion_time = jiffies;
 
1719                 rspiocbp = __lpfc_sli_get_iocbq(phba);
 
1720                 if (rspiocbp == NULL) {
 
1721                         printk(KERN_ERR "%s: out of buffers! Failing "
 
1722                                "completion.\n", __FUNCTION__);
 
1726                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
 
1727                                       phba->iocb_rsp_size);
 
1728                 irsp = &rspiocbp->iocb;
 
1730                 if (++pring->rspidx >= portRspMax)
 
1733                 if (pring->ringno == LPFC_ELS_RING) {
 
1734                         lpfc_debugfs_slow_ring_trc(phba,
 
1735                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
 
1736                                 *(((uint32_t *) irsp) + 4),
 
1737                                 *(((uint32_t *) irsp) + 6),
 
1738                                 *(((uint32_t *) irsp) + 7));
 
1741                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
 
1743                 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
 
1745                 pring->iocb_continueq_cnt++;
 
1748                          * By default, the driver expects to free all resources
 
1749                          * associated with this iocb completion.
 
1752                         saveq = list_get_first(&pring->iocb_continueq,
 
1753                                                struct lpfc_iocbq, list);
 
1754                         irsp = &(saveq->iocb);
 
1755                         list_del_init(&pring->iocb_continueq);
 
1756                         pring->iocb_continueq_cnt = 0;
 
1758                         pring->stats.iocb_rsp++;
 
1761                          * If resource errors reported from HBA, reduce
 
1762                          * queuedepths of the SCSI device.
 
1764                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
 
1765                              (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
 
1766                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
 
1767                                 lpfc_adjust_queue_depth(phba);
 
1768                                 spin_lock_irqsave(&phba->hbalock, iflag);
 
1771                         if (irsp->ulpStatus) {
 
1772                                 /* Rsp ring <ringno> error: IOCB */
 
1773                                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
 
1774                                                 "0328 Rsp Ring %d error: "
 
1779                                                 "x%x x%x x%x x%x\n",
 
1781                                                 irsp->un.ulpWord[0],
 
1782                                                 irsp->un.ulpWord[1],
 
1783                                                 irsp->un.ulpWord[2],
 
1784                                                 irsp->un.ulpWord[3],
 
1785                                                 irsp->un.ulpWord[4],
 
1786                                                 irsp->un.ulpWord[5],
 
1787                                                 *(((uint32_t *) irsp) + 6),
 
1788                                                 *(((uint32_t *) irsp) + 7),
 
1789                                                 *(((uint32_t *) irsp) + 8),
 
1790                                                 *(((uint32_t *) irsp) + 9),
 
1791                                                 *(((uint32_t *) irsp) + 10),
 
1792                                                 *(((uint32_t *) irsp) + 11),
 
1793                                                 *(((uint32_t *) irsp) + 12),
 
1794                                                 *(((uint32_t *) irsp) + 13),
 
1795                                                 *(((uint32_t *) irsp) + 14),
 
1796                                                 *(((uint32_t *) irsp) + 15));
 
1800                          * Fetch the IOCB command type and call the correct
 
1801                          * completion routine.  Solicited and Unsolicited
 
1802                          * IOCBs on the ELS ring get freed back to the
 
1803                          * lpfc_iocb_list by the discovery kernel thread.
 
1805                         iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
 
1806                         type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
 
1807                         if (type == LPFC_SOL_IOCB) {
 
1808                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
 
1809                                 rc = lpfc_sli_process_sol_iocb(phba, pring,
 
1811                                 spin_lock_irqsave(&phba->hbalock, iflag);
 
1812                         } else if (type == LPFC_UNSOL_IOCB) {
 
1813                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
 
1814                                 rc = lpfc_sli_process_unsol_iocb(phba, pring,
 
1816                                 spin_lock_irqsave(&phba->hbalock, iflag);
 
1819                         } else if (type == LPFC_ABORT_IOCB) {
 
1820                                 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
 
1822                                       lpfc_sli_iocbq_lookup(phba, pring,
 
1824                                         /* Call the specified completion
 
1826                                         if (cmdiocbp->iocb_cmpl) {
 
1827                                                 spin_unlock_irqrestore(
 
1830                                                 (cmdiocbp->iocb_cmpl) (phba,
 
1836                                                 __lpfc_sli_release_iocbq(phba,
 
1839                         } else if (type == LPFC_UNKNOWN_IOCB) {
 
1840                                 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
 
1842                                         char adaptermsg[LPFC_MAX_ADPTMSG];
 
1844                                         memset(adaptermsg, 0,
 
1846                                         memcpy(&adaptermsg[0], (uint8_t *) irsp,
 
1848                                         dev_warn(&((phba->pcidev)->dev),
 
1850                                                  phba->brd_no, adaptermsg);
 
1852                                         /* Unknown IOCB command */
 
1853                                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
 
1854                                                         "0335 Unknown IOCB "
 
1855                                                         "command Data: x%x "
 
1865                                 list_for_each_entry_safe(rspiocbp, next_iocb,
 
1866                                                          &saveq->list, list) {
 
1867                                         list_del(&rspiocbp->list);
 
1868                                         __lpfc_sli_release_iocbq(phba,
 
1871                                 __lpfc_sli_release_iocbq(phba, saveq);
 
1877                  * If the port response put pointer has not been updated, sync
 
1878                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
 
1879                  * response put pointer.
 
1881                 if (pring->rspidx == portRspPut) {
 
1882                         portRspPut = le32_to_cpu(pgp->rspPutInx);
 
1884         } /* while (pring->rspidx != portRspPut) */
 
1886         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
 
1887                 /* At least one response entry has been freed */
 
1888                 pring->stats.iocb_rsp_full++;
 
1889                 /* SET RxRE_RSP in Chip Att register */
 
1890                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
 
1891                 writel(status, phba->CAregaddr);
 
1892                 readl(phba->CAregaddr); /* flush */
 
1894         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
 
1895                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
 
1896                 pring->stats.iocb_cmd_empty++;
 
1898                 /* Force update of the local copy of cmdGetInx */
 
1899                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
 
1900                 lpfc_sli_resume_iocb(phba, pring);
 
1902                 if ((pring->lpfc_sli_cmd_available))
 
1903                         (pring->lpfc_sli_cmd_available) (phba, pring);
 
1907         spin_unlock_irqrestore(&phba->hbalock, iflag);
 
1912 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 
1914         LIST_HEAD(completions);
 
1915         struct lpfc_iocbq *iocb, *next_iocb;
 
1918         if (pring->ringno == LPFC_ELS_RING) {
 
1919                 lpfc_fabric_abort_hba(phba);
 
1922         /* Error everything on txq and txcmplq
 
1925         spin_lock_irq(&phba->hbalock);
 
1926         list_splice_init(&pring->txq, &completions);
 
1929         /* Next issue ABTS for everything on the txcmplq */
 
1930         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
 
1931                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
 
1933         spin_unlock_irq(&phba->hbalock);
 
1935         while (!list_empty(&completions)) {
 
1936                 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
 
1938                 list_del_init(&iocb->list);
 
1940                 if (!iocb->iocb_cmpl)
 
1941                         lpfc_sli_release_iocbq(phba, iocb);
 
1943                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
 
1944                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
 
1945                         (iocb->iocb_cmpl) (phba, iocb, iocb);
 
1951 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
 
1957         /* Read the HBA Host Status Register */
 
1958         status = readl(phba->HSregaddr);
 
1961          * Check status register every 100ms for 5 retries, then every
 
1962          * 500ms for 5, then every 2.5 sec for 5, then reset board and
 
1963          * every 2.5 sec for 4.
 
1964          * Break our of the loop if errors occurred during init.
 
1966         while (((status & mask) != mask) &&
 
1967                !(status & HS_FFERM) &&
 
1979                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
 
1980                         lpfc_sli_brdrestart(phba);
 
1982                 /* Read the HBA Host Status Register */
 
1983                 status = readl(phba->HSregaddr);
 
1986         /* Check to see if any errors occurred during init */
 
1987         if ((status & HS_FFERM) || (i >= 20)) {
 
1988                 phba->link_state = LPFC_HBA_ERROR;
 
1995 #define BARRIER_TEST_PATTERN (0xdeadbeef)
 
1997 void lpfc_reset_barrier(struct lpfc_hba *phba)
 
1999         uint32_t __iomem *resp_buf;
 
2000         uint32_t __iomem *mbox_buf;
 
2001         volatile uint32_t mbox;
 
2006         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
 
2007         if (hdrtype != 0x80 ||
 
2008             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
 
2009              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
 
2013          * Tell the other part of the chip to suspend temporarily all
 
2016         resp_buf = phba->MBslimaddr;
 
2018         /* Disable the error attention */
 
2019         hc_copy = readl(phba->HCregaddr);
 
2020         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
 
2021         readl(phba->HCregaddr); /* flush */
 
2022         phba->link_flag |= LS_IGNORE_ERATT;
 
2024         if (readl(phba->HAregaddr) & HA_ERATT) {
 
2025                 /* Clear Chip error bit */
 
2026                 writel(HA_ERATT, phba->HAregaddr);
 
2027                 phba->pport->stopped = 1;
 
2031         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
 
2032         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
 
2034         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
 
2035         mbox_buf = phba->MBslimaddr;
 
2036         writel(mbox, mbox_buf);
 
2039              readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
 
2042         if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
 
2043                 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
 
2044                     phba->pport->stopped)
 
2050         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
 
2051         for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
 
2056         while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
 
2059         if (readl(phba->HAregaddr) & HA_ERATT) {
 
2060                 writel(HA_ERATT, phba->HAregaddr);
 
2061                 phba->pport->stopped = 1;
 
2065         phba->link_flag &= ~LS_IGNORE_ERATT;
 
2066         writel(hc_copy, phba->HCregaddr);
 
2067         readl(phba->HCregaddr); /* flush */
 
2071 lpfc_sli_brdkill(struct lpfc_hba *phba)
 
2073         struct lpfc_sli *psli;
 
2083         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 
2084                         "0329 Kill HBA Data: x%x x%x\n",
 
2085                         phba->pport->port_state, psli->sli_flag);
 
2087         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 
2091         /* Disable the error attention */
 
2092         spin_lock_irq(&phba->hbalock);
 
2093         status = readl(phba->HCregaddr);
 
2094         status &= ~HC_ERINT_ENA;
 
2095         writel(status, phba->HCregaddr);
 
2096         readl(phba->HCregaddr); /* flush */
 
2097         phba->link_flag |= LS_IGNORE_ERATT;
 
2098         spin_unlock_irq(&phba->hbalock);
 
2100         lpfc_kill_board(phba, pmb);
 
2101         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
 
2102         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
 
2104         if (retval != MBX_SUCCESS) {
 
2105                 if (retval != MBX_BUSY)
 
2106                         mempool_free(pmb, phba->mbox_mem_pool);
 
2107                 spin_lock_irq(&phba->hbalock);
 
2108                 phba->link_flag &= ~LS_IGNORE_ERATT;
 
2109                 spin_unlock_irq(&phba->hbalock);
 
2113         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
 
2115         mempool_free(pmb, phba->mbox_mem_pool);
 
2117         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
 
2118          * attention every 100ms for 3 seconds. If we don't get ERATT after
 
2119          * 3 seconds we still set HBA_ERROR state because the status of the
 
2120          * board is now undefined.
 
2122         ha_copy = readl(phba->HAregaddr);
 
2124         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
 
2126                 ha_copy = readl(phba->HAregaddr);
 
2129         del_timer_sync(&psli->mbox_tmo);
 
2130         if (ha_copy & HA_ERATT) {
 
2131                 writel(HA_ERATT, phba->HAregaddr);
 
2132                 phba->pport->stopped = 1;
 
2134         spin_lock_irq(&phba->hbalock);
 
2135         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 
2136         phba->link_flag &= ~LS_IGNORE_ERATT;
 
2137         spin_unlock_irq(&phba->hbalock);
 
2139         psli->mbox_active = NULL;
 
2140         lpfc_hba_down_post(phba);
 
2141         phba->link_state = LPFC_HBA_ERROR;
 
2143         return ha_copy & HA_ERATT ? 0 : 1;
 
2147 lpfc_sli_brdreset(struct lpfc_hba *phba)
 
2149         struct lpfc_sli *psli;
 
2150         struct lpfc_sli_ring *pring;
 
2157         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 
2158                         "0325 Reset HBA Data: x%x x%x\n",
 
2159                         phba->pport->port_state, psli->sli_flag);
 
2161         /* perform board reset */
 
2162         phba->fc_eventTag = 0;
 
2163         phba->pport->fc_myDID = 0;
 
2164         phba->pport->fc_prevDID = 0;
 
2166         /* Turn off parity checking and serr during the physical reset */
 
2167         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
 
2168         pci_write_config_word(phba->pcidev, PCI_COMMAND,
 
2170                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
 
2172         psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
 
2173         /* Now toggle INITFF bit in the Host Control Register */
 
2174         writel(HC_INITFF, phba->HCregaddr);
 
2176         readl(phba->HCregaddr); /* flush */
 
2177         writel(0, phba->HCregaddr);
 
2178         readl(phba->HCregaddr); /* flush */
 
2180         /* Restore PCI cmd register */
 
2181         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
 
2183         /* Initialize relevant SLI info */
 
2184         for (i = 0; i < psli->num_rings; i++) {
 
2185                 pring = &psli->ring[i];
 
2188                 pring->next_cmdidx  = 0;
 
2189                 pring->local_getidx = 0;
 
2191                 pring->missbufcnt = 0;
 
2194         phba->link_state = LPFC_WARM_START;
 
2199 lpfc_sli_brdrestart(struct lpfc_hba *phba)
 
2202         struct lpfc_sli *psli;
 
2204         volatile uint32_t word0;
 
2205         void __iomem *to_slim;
 
2207         spin_lock_irq(&phba->hbalock);
 
2212         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 
2213                         "0337 Restart HBA Data: x%x x%x\n",
 
2214                         phba->pport->port_state, psli->sli_flag);
 
2217         mb = (MAILBOX_t *) &word0;
 
2218         mb->mbxCommand = MBX_RESTART;
 
2221         lpfc_reset_barrier(phba);
 
2223         to_slim = phba->MBslimaddr;
 
2224         writel(*(uint32_t *) mb, to_slim);
 
2225         readl(to_slim); /* flush */
 
2227         /* Only skip post after fc_ffinit is completed */
 
2228         if (phba->pport->port_state) {
 
2230                 word0 = 1;      /* This is really setting up word1 */
 
2233                 word0 = 0;      /* This is really setting up word1 */
 
2235         to_slim = phba->MBslimaddr + sizeof (uint32_t);
 
2236         writel(*(uint32_t *) mb, to_slim);
 
2237         readl(to_slim); /* flush */
 
2239         lpfc_sli_brdreset(phba);
 
2240         phba->pport->stopped = 0;
 
2241         phba->link_state = LPFC_INIT_START;
 
2243         spin_unlock_irq(&phba->hbalock);
 
2245         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
 
2246         psli->stats_start = get_seconds();
 
2253         lpfc_hba_down_post(phba);
 
2259 lpfc_sli_chipset_init(struct lpfc_hba *phba)
 
2261         uint32_t status, i = 0;
 
2263         /* Read the HBA Host Status Register */
 
2264         status = readl(phba->HSregaddr);
 
2266         /* Check status register to see what current state is */
 
2268         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
 
2270                 /* Check every 100ms for 5 retries, then every 500ms for 5, then
 
2271                  * every 2.5 sec for 5, then reset board and every 2.5 sec for
 
2275                         /* Adapter failed to init, timeout, status reg
 
2277                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
 
2278                                         "0436 Adapter failed to init, "
 
2279                                         "timeout, status reg x%x, "
 
2280                                         "FW Data: A8 x%x AC x%x\n", status,
 
2281                                         readl(phba->MBslimaddr + 0xa8),
 
2282                                         readl(phba->MBslimaddr + 0xac));
 
2283                         phba->link_state = LPFC_HBA_ERROR;
 
2287                 /* Check to see if any errors occurred during init */
 
2288                 if (status & HS_FFERM) {
 
2289                         /* ERROR: During chipset initialization */
 
2290                         /* Adapter failed to init, chipset, status reg
 
2292                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
 
2293                                         "0437 Adapter failed to init, "
 
2294                                         "chipset, status reg x%x, "
 
2295                                         "FW Data: A8 x%x AC x%x\n", status,
 
2296                                         readl(phba->MBslimaddr + 0xa8),
 
2297                                         readl(phba->MBslimaddr + 0xac));
 
2298                         phba->link_state = LPFC_HBA_ERROR;
 
2304                 } else if (i <= 10) {
 
2312                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
 
2313                         lpfc_sli_brdrestart(phba);
 
2315                 /* Read the HBA Host Status Register */
 
2316                 status = readl(phba->HSregaddr);
 
2319         /* Check to see if any errors occurred during init */
 
2320         if (status & HS_FFERM) {
 
2321                 /* ERROR: During chipset initialization */
 
2322                 /* Adapter failed to init, chipset, status reg <status> */
 
2323                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
 
2324                                 "0438 Adapter failed to init, chipset, "
 
2326                                 "FW Data: A8 x%x AC x%x\n", status,
 
2327                                 readl(phba->MBslimaddr + 0xa8),
 
2328                                 readl(phba->MBslimaddr + 0xac));
 
2329                 phba->link_state = LPFC_HBA_ERROR;
 
2333         /* Clear all interrupt enable conditions */
 
2334         writel(0, phba->HCregaddr);
 
2335         readl(phba->HCregaddr); /* flush */
 
2337         /* setup host attn register */
 
2338         writel(0xffffffff, phba->HAregaddr);
 
2339         readl(phba->HAregaddr); /* flush */
 
2344 lpfc_sli_hbq_count(void)
 
2346         return ARRAY_SIZE(lpfc_hbq_defs);
 
2350 lpfc_sli_hbq_entry_count(void)
 
2352         int  hbq_count = lpfc_sli_hbq_count();
 
2356         for (i = 0; i < hbq_count; ++i)
 
2357                 count += lpfc_hbq_defs[i]->entry_count;
 
2362 lpfc_sli_hbq_size(void)
 
2364         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
 
2368 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
 
2370         int  hbq_count = lpfc_sli_hbq_count();
 
2374         uint32_t hbq_entry_index;
 
2376                                 /* Get a Mailbox buffer to setup mailbox
 
2377                                  * commands for HBA initialization
 
2379         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 
2386         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
 
2387         phba->link_state = LPFC_INIT_MBX_CMDS;
 
2388         phba->hbq_in_use = 1;
 
2390         hbq_entry_index = 0;
 
2391         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
 
2392                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
 
2393                 phba->hbqs[hbqno].hbqPutIdx      = 0;
 
2394                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
 
2395                 phba->hbqs[hbqno].entry_count =
 
2396                         lpfc_hbq_defs[hbqno]->entry_count;
 
2397                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
 
2398                         hbq_entry_index, pmb);
 
2399                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
 
2401                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
 
2402                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
 
2403                            mbxStatus <status>, ring <num> */
 
2405                         lpfc_printf_log(phba, KERN_ERR,
 
2406                                         LOG_SLI | LOG_VPORT,
 
2407                                         "1805 Adapter failed to init. "
 
2408                                         "Data: x%x x%x x%x\n",
 
2410                                         pmbox->mbxStatus, hbqno);
 
2412                         phba->link_state = LPFC_HBA_ERROR;
 
2413                         mempool_free(pmb, phba->mbox_mem_pool);
 
2417         phba->hbq_count = hbq_count;
 
2419         mempool_free(pmb, phba->mbox_mem_pool);
 
2421         /* Initially populate or replenish the HBQs */
 
2422         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
 
2423                 if (lpfc_sli_hbqbuf_init_hbqs(phba, hbqno))
 
2430 lpfc_do_config_port(struct lpfc_hba *phba, int sli_mode)
 
2433         uint32_t resetcount = 0, rc = 0, done = 0;
 
2435         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 
2437                 phba->link_state = LPFC_HBA_ERROR;
 
2441         phba->sli_rev = sli_mode;
 
2442         while (resetcount < 2 && !done) {
 
2443                 spin_lock_irq(&phba->hbalock);
 
2444                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
 
2445                 spin_unlock_irq(&phba->hbalock);
 
2446                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
 
2447                 lpfc_sli_brdrestart(phba);
 
2449                 rc = lpfc_sli_chipset_init(phba);
 
2453                 spin_lock_irq(&phba->hbalock);
 
2454                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 
2455                 spin_unlock_irq(&phba->hbalock);
 
2458                 /* Call pre CONFIG_PORT mailbox command initialization.  A
 
2459                  * value of 0 means the call was successful.  Any other
 
2460                  * nonzero value is a failure, but if ERESTART is returned,
 
2461                  * the driver may reset the HBA and try again.
 
2463                 rc = lpfc_config_port_prep(phba);
 
2464                 if (rc == -ERESTART) {
 
2465                         phba->link_state = LPFC_LINK_UNKNOWN;
 
2471                 phba->link_state = LPFC_INIT_MBX_CMDS;
 
2472                 lpfc_config_port(phba, pmb);
 
2473                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
 
2474                 if (rc != MBX_SUCCESS) {
 
2475                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
 
2476                                 "0442 Adapter failed to init, mbxCmd x%x "
 
2477                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
 
2478                                 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
 
2479                         spin_lock_irq(&phba->hbalock);
 
2480                         phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
 
2481                         spin_unlock_irq(&phba->hbalock);
 
2485                         phba->max_vpi = (phba->max_vpi &&
 
2486                                          pmb->mb.un.varCfgPort.gmv) != 0
 
2487                                 ? pmb->mb.un.varCfgPort.max_vpi
 
2494                 goto do_prep_failed;
 
2497         if ((pmb->mb.un.varCfgPort.sli_mode == 3) &&
 
2498                 (!pmb->mb.un.varCfgPort.cMA)) {
 
2503         mempool_free(pmb, phba->mbox_mem_pool);
 
2508 lpfc_sli_hba_setup(struct lpfc_hba *phba)
 
2513         switch (lpfc_sli_mode) {
 
2515                 if (phba->cfg_enable_npiv) {
 
2516                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
 
2517                                 "1824 NPIV enabled: Override lpfc_sli_mode "
 
2518                                 "parameter (%d) to auto (0).\n",
 
2528                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
 
2529                                 "1819 Unrecognized lpfc_sli_mode "
 
2530                                 "parameter: %d.\n", lpfc_sli_mode);
 
2535         rc = lpfc_do_config_port(phba, mode);
 
2536         if (rc && lpfc_sli_mode == 3)
 
2537                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
 
2538                                 "1820 Unable to select SLI-3.  "
 
2539                                 "Not supported by adapter.\n");
 
2540         if (rc && mode != 2)
 
2541                 rc = lpfc_do_config_port(phba, 2);
 
2543                 goto lpfc_sli_hba_setup_error;
 
2545         if (phba->sli_rev == 3) {
 
2546                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
 
2547                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
 
2548                 phba->sli3_options |= LPFC_SLI3_ENABLED;
 
2549                 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
 
2552                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
 
2553                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
 
2554                 phba->sli3_options = 0;
 
2557         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
 
2558                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
 
2559                         phba->sli_rev, phba->max_vpi);
 
2560         rc = lpfc_sli_ring_map(phba);
 
2563                 goto lpfc_sli_hba_setup_error;
 
2567         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
 
2568                 rc = lpfc_sli_hbq_setup(phba);
 
2570                         goto lpfc_sli_hba_setup_error;
 
2573         phba->sli.sli_flag |= LPFC_PROCESS_LA;
 
2575         rc = lpfc_config_port_post(phba);
 
2577                 goto lpfc_sli_hba_setup_error;
 
2581 lpfc_sli_hba_setup_error:
 
2582         phba->link_state = LPFC_HBA_ERROR;
 
2583         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
 
2584                         "0445 Firmware initialization failed\n");
 
2588 /*! lpfc_mbox_timeout
 
2592  * \param hba Pointer to per struct lpfc_hba structure
 
2593  * \param l1  Pointer to the driver's mailbox queue.
 
2599  * This routine handles mailbox timeout events at timer interrupt context.
 
2602 lpfc_mbox_timeout(unsigned long ptr)
 
2604         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
 
2605         unsigned long iflag;
 
2606         uint32_t tmo_posted;
 
2608         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
 
2609         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
 
2611                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
 
2612         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
 
2615                 spin_lock_irqsave(&phba->hbalock, iflag);
 
2616                 if (phba->work_wait)
 
2617                         lpfc_worker_wake_up(phba);
 
2618                 spin_unlock_irqrestore(&phba->hbalock, iflag);
 
2623 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
 
2625         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
 
2626         MAILBOX_t *mb = &pmbox->mb;
 
2627         struct lpfc_sli *psli = &phba->sli;
 
2628         struct lpfc_sli_ring *pring;
 
2630         if (!(phba->pport->work_port_events & WORKER_MBOX_TMO)) {
 
2634         /* Mbox cmd <mbxCommand> timeout */
 
2635         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
 
2636                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
 
2638                         phba->pport->port_state,
 
2640                         phba->sli.mbox_active);
 
2642         /* Setting state unknown so lpfc_sli_abort_iocb_ring
 
2643          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
 
2644          * it to fail all oustanding SCSI IO.
 
2646         spin_lock_irq(&phba->pport->work_port_lock);
 
2647         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
 
2648         spin_unlock_irq(&phba->pport->work_port_lock);
 
2649         spin_lock_irq(&phba->hbalock);
 
2650         phba->link_state = LPFC_LINK_UNKNOWN;
 
2651         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
 
2652         spin_unlock_irq(&phba->hbalock);
 
2654         pring = &psli->ring[psli->fcp_ring];
 
2655         lpfc_sli_abort_iocb_ring(phba, pring);
 
2657         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
 
2658                         "0345 Resetting board due to mailbox timeout\n");
 
2660          * lpfc_offline calls lpfc_sli_hba_down which will clean up
 
2661          * on oustanding mailbox commands.
 
2663         /* If resets are disabled then set error state and return. */
 
2664         if (!phba->cfg_enable_hba_reset) {
 
2665                 phba->link_state = LPFC_HBA_ERROR;
 
2668         lpfc_offline_prep(phba);
 
2670         lpfc_sli_brdrestart(phba);
 
2672         lpfc_unblock_mgmt_io(phba);
 
2677 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
 
2680         struct lpfc_sli *psli = &phba->sli;
 
2681         uint32_t status, evtctr;
 
2684         unsigned long timeout;
 
2685         unsigned long drvr_flag = 0;
 
2686         volatile uint32_t word0, ldata;
 
2687         void __iomem *to_slim;
 
2688         int processing_queue = 0;
 
2690         spin_lock_irqsave(&phba->hbalock, drvr_flag);
 
2692                 /* processing mbox queue from intr_handler */
 
2693                 processing_queue = 1;
 
2694                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 
2695                 pmbox = lpfc_mbox_get(phba);
 
2697                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 
2702         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
 
2703                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
 
2705                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 
2706                         lpfc_printf_log(phba, KERN_ERR,
 
2707                                         LOG_MBOX | LOG_VPORT,
 
2708                                         "1806 Mbox x%x failed. No vport\n",
 
2709                                         pmbox->mb.mbxCommand);
 
2711                         goto out_not_finished;
 
2715         /* If the PCI channel is in offline state, do not post mbox. */
 
2716         if (unlikely(pci_channel_offline(phba->pcidev))) {
 
2717                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 
2718                 goto out_not_finished;
 
2724         status = MBX_SUCCESS;
 
2726         if (phba->link_state == LPFC_HBA_ERROR) {
 
2727                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 
2729                 /* Mbox command <mbxCommand> cannot issue */
 
2730                 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
 
2731                 goto out_not_finished;
 
2734         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
 
2735             !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
 
2736                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 
2737                 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
 
2738                 goto out_not_finished;
 
2741         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
 
2742                 /* Polling for a mbox command when another one is already active
 
2743                  * is not allowed in SLI. Also, the driver must have established
 
2744                  * SLI2 mode to queue and process multiple mbox commands.
 
2747                 if (flag & MBX_POLL) {
 
2748                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 
2750                         /* Mbox command <mbxCommand> cannot issue */
 
2751                         LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
 
2752                         goto out_not_finished;
 
2755                 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
 
2756                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 
2757                         /* Mbox command <mbxCommand> cannot issue */
 
2758                         LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
 
2759                         goto out_not_finished;
 
2762                 /* Another mailbox command is still being processed, queue this
 
2763                  * command to be processed later.
 
2765                 lpfc_mbox_put(phba, pmbox);
 
2767                 /* Mbox cmd issue - BUSY */
 
2768                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
 
2769                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
 
2770                                 "x%x x%x x%x x%x\n",
 
2771                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
 
2772                                 mb->mbxCommand, phba->pport->port_state,
 
2773                                 psli->sli_flag, flag);
 
2775                 psli->slistat.mbox_busy++;
 
2776                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 
2779                         lpfc_debugfs_disc_trc(pmbox->vport,
 
2780                                 LPFC_DISC_TRC_MBOX_VPORT,
 
2781                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
 
2782                                 (uint32_t)mb->mbxCommand,
 
2783                                 mb->un.varWords[0], mb->un.varWords[1]);
 
2786                         lpfc_debugfs_disc_trc(phba->pport,
 
2788                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
 
2789                                 (uint32_t)mb->mbxCommand,
 
2790                                 mb->un.varWords[0], mb->un.varWords[1]);
 
2796         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
 
2798         /* If we are not polling, we MUST be in SLI2 mode */
 
2799         if (flag != MBX_POLL) {
 
2800                 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
 
2801                     (mb->mbxCommand != MBX_KILL_BOARD)) {
 
2802                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 
2803                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 
2804                         /* Mbox command <mbxCommand> cannot issue */
 
2805                         LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
 
2806                         goto out_not_finished;
 
2808                 /* timeout active mbox command */
 
2809                 mod_timer(&psli->mbox_tmo, (jiffies +
 
2810                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
 
2813         /* Mailbox cmd <cmd> issue */
 
2814         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
 
2815                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
 
2817                         pmbox->vport ? pmbox->vport->vpi : 0,
 
2818                         mb->mbxCommand, phba->pport->port_state,
 
2819                         psli->sli_flag, flag);
 
2821         if (mb->mbxCommand != MBX_HEARTBEAT) {
 
2823                         lpfc_debugfs_disc_trc(pmbox->vport,
 
2824                                 LPFC_DISC_TRC_MBOX_VPORT,
 
2825                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
 
2826                                 (uint32_t)mb->mbxCommand,
 
2827                                 mb->un.varWords[0], mb->un.varWords[1]);
 
2830                         lpfc_debugfs_disc_trc(phba->pport,
 
2832                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
 
2833                                 (uint32_t)mb->mbxCommand,
 
2834                                 mb->un.varWords[0], mb->un.varWords[1]);
 
2838         psli->slistat.mbox_cmd++;
 
2839         evtctr = psli->slistat.mbox_event;
 
2841         /* next set own bit for the adapter and copy over command word */
 
2842         mb->mbxOwner = OWN_CHIP;
 
2844         if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
 
2845                 /* First copy command data to host SLIM area */
 
2846                 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
 
2848                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
 
2849                         /* copy command data into host mbox for cmpl */
 
2850                         lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
 
2854                 /* First copy mbox command data to HBA SLIM, skip past first
 
2856                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
 
2857                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
 
2858                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
 
2860                 /* Next copy over first word, with mbxOwner set */
 
2861                 ldata = *((volatile uint32_t *)mb);
 
2862                 to_slim = phba->MBslimaddr;
 
2863                 writel(ldata, to_slim);
 
2864                 readl(to_slim); /* flush */
 
2866                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
 
2867                         /* switch over to host mailbox */
 
2868                         psli->sli_flag |= LPFC_SLI2_ACTIVE;
 
2876                 /* Set up reference to mailbox command */
 
2877                 psli->mbox_active = pmbox;
 
2878                 /* Interrupt board to do it */
 
2879                 writel(CA_MBATT, phba->CAregaddr);
 
2880                 readl(phba->CAregaddr); /* flush */
 
2881                 /* Don't wait for it to finish, just return */
 
2885                 /* Set up null reference to mailbox command */
 
2886                 psli->mbox_active = NULL;
 
2887                 /* Interrupt board to do it */
 
2888                 writel(CA_MBATT, phba->CAregaddr);
 
2889                 readl(phba->CAregaddr); /* flush */
 
2891                 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
 
2892                         /* First read mbox status word */
 
2893                         word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
 
2894                         word0 = le32_to_cpu(word0);
 
2896                         /* First read mbox status word */
 
2897                         word0 = readl(phba->MBslimaddr);
 
2900                 /* Read the HBA Host Attention Register */
 
2901                 ha_copy = readl(phba->HAregaddr);
 
2902                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
 
2906                 /* Wait for command to complete */
 
2907                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
 
2908                        (!(ha_copy & HA_MBATT) &&
 
2909                         (phba->link_state > LPFC_WARM_START))) {
 
2910                         if (time_after(jiffies, timeout)) {
 
2911                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 
2912                                 spin_unlock_irqrestore(&phba->hbalock,
 
2914                                 goto out_not_finished;
 
2917                         /* Check if we took a mbox interrupt while we were
 
2919                         if (((word0 & OWN_CHIP) != OWN_CHIP)
 
2920                             && (evtctr != psli->slistat.mbox_event))
 
2924                                 spin_unlock_irqrestore(&phba->hbalock,
 
2927                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
 
2930                         if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
 
2931                                 /* First copy command data */
 
2932                                 word0 = *((volatile uint32_t *)
 
2933                                                 &phba->slim2p->mbx);
 
2934                                 word0 = le32_to_cpu(word0);
 
2935                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
 
2937                                         volatile uint32_t slimword0;
 
2938                                         /* Check real SLIM for any errors */
 
2939                                         slimword0 = readl(phba->MBslimaddr);
 
2940                                         slimmb = (MAILBOX_t *) & slimword0;
 
2941                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
 
2942                                             && slimmb->mbxStatus) {
 
2949                                 /* First copy command data */
 
2950                                 word0 = readl(phba->MBslimaddr);
 
2952                         /* Read the HBA Host Attention Register */
 
2953                         ha_copy = readl(phba->HAregaddr);
 
2956                 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
 
2957                         /* copy results back to user */
 
2958                         lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
 
2961                         /* First copy command data */
 
2962                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
 
2964                         if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
 
2966                                 lpfc_memcpy_from_slim((void *)pmbox->context2,
 
2967                                       phba->MBslimaddr + DMP_RSP_OFFSET,
 
2968                                                       mb->un.varDmp.word_cnt);
 
2972                 writel(HA_MBATT, phba->HAregaddr);
 
2973                 readl(phba->HAregaddr); /* flush */
 
2975                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 
2976                 status = mb->mbxStatus;
 
2979         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 
2983         if (processing_queue) {
 
2984                 pmbox->mb.mbxStatus = MBX_NOT_FINISHED;
 
2985                 lpfc_mbox_cmpl_put(phba, pmbox);
 
2987         return MBX_NOT_FINISHED;
 
2991  * Caller needs to hold lock.
 
2994 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 
2995                     struct lpfc_iocbq *piocb)
 
2997         /* Insert the caller's iocb in the txq tail for later processing. */
 
2998         list_add_tail(&piocb->list, &pring->txq);
 
3002 static struct lpfc_iocbq *
 
3003 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 
3004                    struct lpfc_iocbq **piocb)
 
3006         struct lpfc_iocbq * nextiocb;
 
3008         nextiocb = lpfc_sli_ringtx_get(phba, pring);
 
3018  * Lockless version of lpfc_sli_issue_iocb.
 
3021 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 
3022                     struct lpfc_iocbq *piocb, uint32_t flag)
 
3024         struct lpfc_iocbq *nextiocb;
 
3027         if (piocb->iocb_cmpl && (!piocb->vport) &&
 
3028            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
 
3029            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
 
3030                 lpfc_printf_log(phba, KERN_ERR,
 
3031                                 LOG_SLI | LOG_VPORT,
 
3032                                 "1807 IOCB x%x failed. No vport\n",
 
3033                                 piocb->iocb.ulpCommand);
 
3039         /* If the PCI channel is in offline state, do not post iocbs. */
 
3040         if (unlikely(pci_channel_offline(phba->pcidev)))
 
3044          * We should never get an IOCB if we are in a < LINK_DOWN state
 
3046         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
 
3050          * Check to see if we are blocking IOCB processing because of a
 
3051          * outstanding event.
 
3053         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
 
3056         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
 
3058                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
 
3059                  * can be issued if the link is not up.
 
3061                 switch (piocb->iocb.ulpCommand) {
 
3062                 case CMD_QUE_RING_BUF_CN:
 
3063                 case CMD_QUE_RING_BUF64_CN:
 
3065                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
 
3066                          * completion, iocb_cmpl MUST be 0.
 
3068                         if (piocb->iocb_cmpl)
 
3069                                 piocb->iocb_cmpl = NULL;
 
3071                 case CMD_CREATE_XRI_CR:
 
3072                 case CMD_CLOSE_XRI_CN:
 
3073                 case CMD_CLOSE_XRI_CX:
 
3080          * For FCP commands, we must be in a state where we can process link
 
3083         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
 
3084                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
 
3088         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
 
3089                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
 
3090                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
 
3093                 lpfc_sli_update_ring(phba, pring);
 
3095                 lpfc_sli_update_full_ring(phba, pring);
 
3098                 return IOCB_SUCCESS;
 
3103         pring->stats.iocb_cmd_delay++;
 
3107         if (!(flag & SLI_IOCB_RET_IOCB)) {
 
3108                 __lpfc_sli_ringtx_put(phba, pring, piocb);
 
3109                 return IOCB_SUCCESS;
 
3117 lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 
3118                     struct lpfc_iocbq *piocb, uint32_t flag)
 
3120         unsigned long iflags;
 
3123         spin_lock_irqsave(&phba->hbalock, iflags);
 
3124         rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
 
3125         spin_unlock_irqrestore(&phba->hbalock, iflags);
 
3131 lpfc_extra_ring_setup( struct lpfc_hba *phba)
 
3133         struct lpfc_sli *psli;
 
3134         struct lpfc_sli_ring *pring;
 
3138         /* Adjust cmd/rsp ring iocb entries more evenly */
 
3140         /* Take some away from the FCP ring */
 
3141         pring = &psli->ring[psli->fcp_ring];
 
3142         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
 
3143         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
 
3144         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
 
3145         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
 
3147         /* and give them to the extra ring */
 
3148         pring = &psli->ring[psli->extra_ring];
 
3150         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
 
3151         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
 
3152         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
 
3153         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
 
3155         /* Setup default profile for this ring */
 
3156         pring->iotag_max = 4096;
 
3157         pring->num_mask = 1;
 
3158         pring->prt[0].profile = 0;      /* Mask 0 */
 
3159         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
 
3160         pring->prt[0].type = phba->cfg_multi_ring_type;
 
3161         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
 
3166 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
 
3167         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
 
3172         struct temp_event temp_event_data;
 
3173         struct Scsi_Host *shost;
 
3175         icmd = &iocbq->iocb;
 
3176         evt_code = icmd->un.asyncstat.evt_code;
 
3177         temp = icmd->ulpContext;
 
3179         if ((evt_code != ASYNC_TEMP_WARN) &&
 
3180                 (evt_code != ASYNC_TEMP_SAFE)) {
 
3181                 lpfc_printf_log(phba,
 
3184                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
 
3187                         icmd->un.asyncstat.evt_code);
 
3190         temp_event_data.data = (uint32_t)temp;
 
3191         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
 
3192         if (evt_code == ASYNC_TEMP_WARN) {
 
3193                 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
 
3194                 lpfc_printf_log(phba,
 
3197                                 "0347 Adapter is very hot, please take "
 
3198                                 "corrective action. temperature : %d Celsius\n",
 
3201         if (evt_code == ASYNC_TEMP_SAFE) {
 
3202                 temp_event_data.event_code = LPFC_NORMAL_TEMP;
 
3203                 lpfc_printf_log(phba,
 
3206                                 "0340 Adapter temperature is OK now. "
 
3207                                 "temperature : %d Celsius\n",
 
3211         /* Send temperature change event to applications */
 
3212         shost = lpfc_shost_from_vport(phba->pport);
 
3213         fc_host_post_vendor_event(shost, fc_get_event_number(),
 
3214                 sizeof(temp_event_data), (char *) &temp_event_data,
 
3215                 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
 
3221 lpfc_sli_setup(struct lpfc_hba *phba)
 
3223         int i, totiocbsize = 0;
 
3224         struct lpfc_sli *psli = &phba->sli;
 
3225         struct lpfc_sli_ring *pring;
 
3227         psli->num_rings = MAX_CONFIGURED_RINGS;
 
3229         psli->fcp_ring = LPFC_FCP_RING;
 
3230         psli->next_ring = LPFC_FCP_NEXT_RING;
 
3231         psli->extra_ring = LPFC_EXTRA_RING;
 
3233         psli->iocbq_lookup = NULL;
 
3234         psli->iocbq_lookup_len = 0;
 
3235         psli->last_iotag = 0;
 
3237         for (i = 0; i < psli->num_rings; i++) {
 
3238                 pring = &psli->ring[i];
 
3240                 case LPFC_FCP_RING:     /* ring 0 - FCP */
 
3241                         /* numCiocb and numRiocb are used in config_port */
 
3242                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
 
3243                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
 
3244                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
 
3245                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
 
3246                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
 
3247                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
 
3248                         pring->sizeCiocb = (phba->sli_rev == 3) ?
 
3249                                                         SLI3_IOCB_CMD_SIZE :
 
3251                         pring->sizeRiocb = (phba->sli_rev == 3) ?
 
3252                                                         SLI3_IOCB_RSP_SIZE :
 
3254                         pring->iotag_ctr = 0;
 
3256                             (phba->cfg_hba_queue_depth * 2);
 
3257                         pring->fast_iotag = pring->iotag_max;
 
3258                         pring->num_mask = 0;
 
3260                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
 
3261                         /* numCiocb and numRiocb are used in config_port */
 
3262                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
 
3263                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
 
3264                         pring->sizeCiocb = (phba->sli_rev == 3) ?
 
3265                                                         SLI3_IOCB_CMD_SIZE :
 
3267                         pring->sizeRiocb = (phba->sli_rev == 3) ?
 
3268                                                         SLI3_IOCB_RSP_SIZE :
 
3270                         pring->iotag_max = phba->cfg_hba_queue_depth;
 
3271                         pring->num_mask = 0;
 
3273                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
 
3274                         /* numCiocb and numRiocb are used in config_port */
 
3275                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
 
3276                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
 
3277                         pring->sizeCiocb = (phba->sli_rev == 3) ?
 
3278                                                         SLI3_IOCB_CMD_SIZE :
 
3280                         pring->sizeRiocb = (phba->sli_rev == 3) ?
 
3281                                                         SLI3_IOCB_RSP_SIZE :
 
3283                         pring->fast_iotag = 0;
 
3284                         pring->iotag_ctr = 0;
 
3285                         pring->iotag_max = 4096;
 
3286                         pring->lpfc_sli_rcv_async_status =
 
3287                                 lpfc_sli_async_event_handler;
 
3288                         pring->num_mask = 4;
 
3289                         pring->prt[0].profile = 0;      /* Mask 0 */
 
3290                         pring->prt[0].rctl = FC_ELS_REQ;
 
3291                         pring->prt[0].type = FC_ELS_DATA;
 
3292                         pring->prt[0].lpfc_sli_rcv_unsol_event =
 
3293                             lpfc_els_unsol_event;
 
3294                         pring->prt[1].profile = 0;      /* Mask 1 */
 
3295                         pring->prt[1].rctl = FC_ELS_RSP;
 
3296                         pring->prt[1].type = FC_ELS_DATA;
 
3297                         pring->prt[1].lpfc_sli_rcv_unsol_event =
 
3298                             lpfc_els_unsol_event;
 
3299                         pring->prt[2].profile = 0;      /* Mask 2 */
 
3300                         /* NameServer Inquiry */
 
3301                         pring->prt[2].rctl = FC_UNSOL_CTL;
 
3303                         pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
 
3304                         pring->prt[2].lpfc_sli_rcv_unsol_event =
 
3305                             lpfc_ct_unsol_event;
 
3306                         pring->prt[3].profile = 0;      /* Mask 3 */
 
3307                         /* NameServer response */
 
3308                         pring->prt[3].rctl = FC_SOL_CTL;
 
3310                         pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
 
3311                         pring->prt[3].lpfc_sli_rcv_unsol_event =
 
3312                             lpfc_ct_unsol_event;
 
3315                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
 
3316                                 (pring->numRiocb * pring->sizeRiocb);
 
3318         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
 
3319                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
 
3320                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
 
3321                        "SLI2 SLIM Data: x%x x%lx\n",
 
3322                        phba->brd_no, totiocbsize,
 
3323                        (unsigned long) MAX_SLIM_IOCB_SIZE);
 
3325         if (phba->cfg_multi_ring_support == 2)
 
3326                 lpfc_extra_ring_setup(phba);
 
3332 lpfc_sli_queue_setup(struct lpfc_hba *phba)
 
3334         struct lpfc_sli *psli;
 
3335         struct lpfc_sli_ring *pring;
 
3339         spin_lock_irq(&phba->hbalock);
 
3340         INIT_LIST_HEAD(&psli->mboxq);
 
3341         INIT_LIST_HEAD(&psli->mboxq_cmpl);
 
3342         /* Initialize list headers for txq and txcmplq as double linked lists */
 
3343         for (i = 0; i < psli->num_rings; i++) {
 
3344                 pring = &psli->ring[i];
 
3346                 pring->next_cmdidx  = 0;
 
3347                 pring->local_getidx = 0;
 
3349                 INIT_LIST_HEAD(&pring->txq);
 
3350                 INIT_LIST_HEAD(&pring->txcmplq);
 
3351                 INIT_LIST_HEAD(&pring->iocb_continueq);
 
3352                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
 
3353                 INIT_LIST_HEAD(&pring->postbufq);
 
3355         spin_unlock_irq(&phba->hbalock);
 
3360 lpfc_sli_host_down(struct lpfc_vport *vport)
 
3362         LIST_HEAD(completions);
 
3363         struct lpfc_hba *phba = vport->phba;
 
3364         struct lpfc_sli *psli = &phba->sli;
 
3365         struct lpfc_sli_ring *pring;
 
3366         struct lpfc_iocbq *iocb, *next_iocb;
 
3368         unsigned long flags = 0;
 
3369         uint16_t prev_pring_flag;
 
3371         lpfc_cleanup_discovery_resources(vport);
 
3373         spin_lock_irqsave(&phba->hbalock, flags);
 
3374         for (i = 0; i < psli->num_rings; i++) {
 
3375                 pring = &psli->ring[i];
 
3376                 prev_pring_flag = pring->flag;
 
3377                 if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
 
3378                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
 
3380                  * Error everything on the txq since these iocbs have not been
 
3381                  * given to the FW yet.
 
3383                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
 
3384                         if (iocb->vport != vport)
 
3386                         list_move_tail(&iocb->list, &completions);
 
3390                 /* Next issue ABTS for everything on the txcmplq */
 
3391                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
 
3393                         if (iocb->vport != vport)
 
3395                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
 
3398                 pring->flag = prev_pring_flag;
 
3401         spin_unlock_irqrestore(&phba->hbalock, flags);
 
3403         while (!list_empty(&completions)) {
 
3404                 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
 
3406                 if (!iocb->iocb_cmpl)
 
3407                         lpfc_sli_release_iocbq(phba, iocb);
 
3409                         iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
 
3410                         iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
 
3411                         (iocb->iocb_cmpl) (phba, iocb, iocb);
 
3418 lpfc_sli_hba_down(struct lpfc_hba *phba)
 
3420         LIST_HEAD(completions);
 
3421         struct lpfc_sli *psli = &phba->sli;
 
3422         struct lpfc_sli_ring *pring;
 
3423         struct lpfc_dmabuf *buf_ptr;
 
3425         struct lpfc_iocbq *iocb;
 
3428         unsigned long flags = 0;
 
3430         lpfc_hba_down_prep(phba);
 
3432         lpfc_fabric_abort_hba(phba);
 
3434         spin_lock_irqsave(&phba->hbalock, flags);
 
3435         for (i = 0; i < psli->num_rings; i++) {
 
3436                 pring = &psli->ring[i];
 
3437                 if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
 
3438                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
 
3441                  * Error everything on the txq since these iocbs have not been
 
3442                  * given to the FW yet.
 
3444                 list_splice_init(&pring->txq, &completions);
 
3448         spin_unlock_irqrestore(&phba->hbalock, flags);
 
3450         while (!list_empty(&completions)) {
 
3451                 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
 
3454                 if (!iocb->iocb_cmpl)
 
3455                         lpfc_sli_release_iocbq(phba, iocb);
 
3457                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
 
3458                         cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
 
3459                         (iocb->iocb_cmpl) (phba, iocb, iocb);
 
3463         spin_lock_irqsave(&phba->hbalock, flags);
 
3464         list_splice_init(&phba->elsbuf, &completions);
 
3465         phba->elsbuf_cnt = 0;
 
3466         phba->elsbuf_prev_cnt = 0;
 
3467         spin_unlock_irqrestore(&phba->hbalock, flags);
 
3469         while (!list_empty(&completions)) {
 
3470                 list_remove_head(&completions, buf_ptr,
 
3471                         struct lpfc_dmabuf, list);
 
3472                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
 
3476         /* Return any active mbox cmds */
 
3477         del_timer_sync(&psli->mbox_tmo);
 
3478         spin_lock_irqsave(&phba->hbalock, flags);
 
3480         spin_lock(&phba->pport->work_port_lock);
 
3481         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
 
3482         spin_unlock(&phba->pport->work_port_lock);
 
3484         /* Return any pending or completed mbox cmds */
 
3485         list_splice_init(&phba->sli.mboxq, &completions);
 
3486         if (psli->mbox_active) {
 
3487                 list_add_tail(&psli->mbox_active->list, &completions);
 
3488                 psli->mbox_active = NULL;
 
3489                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 
3491         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
 
3492         spin_unlock_irqrestore(&phba->hbalock, flags);
 
3494         while (!list_empty(&completions)) {
 
3495                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
 
3496                 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
 
3498                         pmb->mbox_cmpl(phba,pmb);
 
3504 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
 
3506         uint32_t *src = srcp;
 
3507         uint32_t *dest = destp;
 
3511         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
 
3513                 ldata = le32_to_cpu(ldata);
 
3521 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 
3522                          struct lpfc_dmabuf *mp)
 
3524         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
 
3526         spin_lock_irq(&phba->hbalock);
 
3527         list_add_tail(&mp->list, &pring->postbufq);
 
3528         pring->postbufq_cnt++;
 
3529         spin_unlock_irq(&phba->hbalock);
 
3534 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
 
3536         spin_lock_irq(&phba->hbalock);
 
3537         phba->buffer_tag_count++;
 
3539          * Always set the QUE_BUFTAG_BIT to distiguish between
 
3540          * a tag assigned by HBQ.
 
3542         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
 
3543         spin_unlock_irq(&phba->hbalock);
 
3544         return phba->buffer_tag_count;
 
3547 struct lpfc_dmabuf *
 
3548 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 
3551         struct lpfc_dmabuf *mp, *next_mp;
 
3552         struct list_head *slp = &pring->postbufq;
 
3554         /* Search postbufq, from the begining, looking for a match on tag */
 
3555         spin_lock_irq(&phba->hbalock);
 
3556         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
 
3557                 if (mp->buffer_tag == tag) {
 
3558                         list_del_init(&mp->list);
 
3559                         pring->postbufq_cnt--;
 
3560                         spin_unlock_irq(&phba->hbalock);
 
3565         spin_unlock_irq(&phba->hbalock);
 
3566         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
 
3567                         "0410 Cannot find virtual addr for buffer tag on "
 
3568                         "ring %d Data x%lx x%p x%p x%x\n",
 
3569                         pring->ringno, (unsigned long) tag,
 
3570                         slp->next, slp->prev, pring->postbufq_cnt);
 
3575 struct lpfc_dmabuf *
 
3576 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 
3579         struct lpfc_dmabuf *mp, *next_mp;
 
3580         struct list_head *slp = &pring->postbufq;
 
3582         /* Search postbufq, from the begining, looking for a match on phys */
 
3583         spin_lock_irq(&phba->hbalock);
 
3584         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
 
3585                 if (mp->phys == phys) {
 
3586                         list_del_init(&mp->list);
 
3587                         pring->postbufq_cnt--;
 
3588                         spin_unlock_irq(&phba->hbalock);
 
3593         spin_unlock_irq(&phba->hbalock);
 
3594         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
 
3595                         "0410 Cannot find virtual addr for mapped buf on "
 
3596                         "ring %d Data x%llx x%p x%p x%x\n",
 
3597                         pring->ringno, (unsigned long long)phys,
 
3598                         slp->next, slp->prev, pring->postbufq_cnt);
 
3603 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
3604                         struct lpfc_iocbq *rspiocb)
 
3606         IOCB_t *irsp = &rspiocb->iocb;
 
3607         uint16_t abort_iotag, abort_context;
 
3608         struct lpfc_iocbq *abort_iocb;
 
3609         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
 
3613         if (irsp->ulpStatus) {
 
3614                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
 
3615                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
 
3617                 spin_lock_irq(&phba->hbalock);
 
3618                 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
 
3619                         abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
 
3621                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
 
3622                                 "0327 Cannot abort els iocb %p "
 
3623                                 "with tag %x context %x, abort status %x, "
 
3625                                 abort_iocb, abort_iotag, abort_context,
 
3626                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
 
3629                  *  If the iocb is not found in Firmware queue the iocb
 
3630                  *  might have completed already. Do not free it again.
 
3632                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
 
3633                         spin_unlock_irq(&phba->hbalock);
 
3634                         lpfc_sli_release_iocbq(phba, cmdiocb);
 
3638                  * make sure we have the right iocbq before taking it
 
3639                  * off the txcmplq and try to call completion routine.
 
3642                     abort_iocb->iocb.ulpContext != abort_context ||
 
3643                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
 
3644                         spin_unlock_irq(&phba->hbalock);
 
3646                         list_del_init(&abort_iocb->list);
 
3647                         pring->txcmplq_cnt--;
 
3648                         spin_unlock_irq(&phba->hbalock);
 
3650                         /* Firmware could still be in progress of DMAing
 
3651                          * payload, so don't free data buffer till after
 
3654                         abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
 
3656                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
 
3657                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
 
3658                         abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
 
3659                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
 
3663         lpfc_sli_release_iocbq(phba, cmdiocb);
 
3668 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
3669                      struct lpfc_iocbq *rspiocb)
 
3671         IOCB_t *irsp = &rspiocb->iocb;
 
3673         /* ELS cmd tag <ulpIoTag> completes */
 
3674         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
 
3675                         "0133 Ignoring ELS cmd tag x%x completion Data: "
 
3677                         irsp->ulpIoTag, irsp->ulpStatus,
 
3678                         irsp->un.ulpWord[4], irsp->ulpTimeout);
 
3679         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
 
3680                 lpfc_ct_free_iocb(phba, cmdiocb);
 
3682                 lpfc_els_free_iocb(phba, cmdiocb);
 
3687 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 
3688                            struct lpfc_iocbq *cmdiocb)
 
3690         struct lpfc_vport *vport = cmdiocb->vport;
 
3691         struct lpfc_iocbq *abtsiocbp;
 
3692         IOCB_t *icmd = NULL;
 
3693         IOCB_t *iabt = NULL;
 
3694         int retval = IOCB_ERROR;
 
3697          * There are certain command types we don't want to abort.  And we
 
3698          * don't want to abort commands that are already in the process of
 
3701         icmd = &cmdiocb->iocb;
 
3702         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
 
3703             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
 
3704             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
 
3707         /* If we're unloading, don't abort iocb on the ELS ring, but change the
 
3708          * callback so that nothing happens when it finishes.
 
3710         if ((vport->load_flag & FC_UNLOADING) &&
 
3711             (pring->ringno == LPFC_ELS_RING)) {
 
3712                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
 
3713                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
 
3715                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
 
3716                 goto abort_iotag_exit;
 
3719         /* issue ABTS for this IOCB based on iotag */
 
3720         abtsiocbp = __lpfc_sli_get_iocbq(phba);
 
3721         if (abtsiocbp == NULL)
 
3724         /* This signals the response to set the correct status
 
3725          * before calling the completion handler.
 
3727         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
 
3729         iabt = &abtsiocbp->iocb;
 
3730         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
 
3731         iabt->un.acxri.abortContextTag = icmd->ulpContext;
 
3732         iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
 
3734         iabt->ulpClass = icmd->ulpClass;
 
3736         if (phba->link_state >= LPFC_LINK_UP)
 
3737                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
 
3739                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
 
3741         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
 
3743         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
 
3744                          "0339 Abort xri x%x, original iotag x%x, "
 
3745                          "abort cmd iotag x%x\n",
 
3746                          iabt->un.acxri.abortContextTag,
 
3747                          iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
 
3748         retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
 
3752          * Caller to this routine should check for IOCB_ERROR
 
3753          * and handle it properly.  This routine no longer removes
 
3754          * iocb off txcmplq and call compl in case of IOCB_ERROR.
 
3760 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
 
3761                            uint16_t tgt_id, uint64_t lun_id,
 
3762                            lpfc_ctx_cmd ctx_cmd)
 
3764         struct lpfc_scsi_buf *lpfc_cmd;
 
3765         struct scsi_cmnd *cmnd;
 
3768         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
 
3771         if (iocbq->vport != vport)
 
3774         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
 
3775         cmnd = lpfc_cmd->pCmd;
 
3782                 if ((cmnd->device->id == tgt_id) &&
 
3783                     (cmnd->device->lun == lun_id))
 
3787                 if (cmnd->device->id == tgt_id)
 
3794                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
 
3795                         __FUNCTION__, ctx_cmd);
 
3803 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
 
3804                   lpfc_ctx_cmd ctx_cmd)
 
3806         struct lpfc_hba *phba = vport->phba;
 
3807         struct lpfc_iocbq *iocbq;
 
3810         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
 
3811                 iocbq = phba->sli.iocbq_lookup[i];
 
3813                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
 
3822 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 
3823                         struct lpfc_iocbq *rspiocb)
 
3825         lpfc_sli_release_iocbq(phba, cmdiocb);
 
3830 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
 
3831                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
 
3833         struct lpfc_hba *phba = vport->phba;
 
3834         struct lpfc_iocbq *iocbq;
 
3835         struct lpfc_iocbq *abtsiocb;
 
3837         int errcnt = 0, ret_val = 0;
 
3840         for (i = 1; i <= phba->sli.last_iotag; i++) {
 
3841                 iocbq = phba->sli.iocbq_lookup[i];
 
3843                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
 
3847                 /* issue ABTS for this IOCB based on iotag */
 
3848                 abtsiocb = lpfc_sli_get_iocbq(phba);
 
3849                 if (abtsiocb == NULL) {
 
3855                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
 
3856                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
 
3857                 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
 
3858                 abtsiocb->iocb.ulpLe = 1;
 
3859                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
 
3860                 abtsiocb->vport = phba->pport;
 
3862                 if (lpfc_is_link_up(phba))
 
3863                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
 
3865                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
 
3867                 /* Setup callback routine and issue the command. */
 
3868                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
 
3869                 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
 
3870                 if (ret_val == IOCB_ERROR) {
 
3871                         lpfc_sli_release_iocbq(phba, abtsiocb);
 
3881 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
 
3882                         struct lpfc_iocbq *cmdiocbq,
 
3883                         struct lpfc_iocbq *rspiocbq)
 
3885         wait_queue_head_t *pdone_q;
 
3886         unsigned long iflags;
 
3888         spin_lock_irqsave(&phba->hbalock, iflags);
 
3889         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
 
3890         if (cmdiocbq->context2 && rspiocbq)
 
3891                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
 
3892                        &rspiocbq->iocb, sizeof(IOCB_t));
 
3894         pdone_q = cmdiocbq->context_un.wait_queue;
 
3897         spin_unlock_irqrestore(&phba->hbalock, iflags);
 
3902  * Issue the caller's iocb and wait for its completion, but no longer than the
 
3903  * caller's timeout.  Note that iocb_flags is cleared before the
 
3904  * lpfc_sli_issue_call since the wake routine sets a unique value and by
 
3905  * definition this is a wait function.
 
3909 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
 
3910                          struct lpfc_sli_ring *pring,
 
3911                          struct lpfc_iocbq *piocb,
 
3912                          struct lpfc_iocbq *prspiocbq,
 
3915         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
 
3916         long timeleft, timeout_req = 0;
 
3917         int retval = IOCB_SUCCESS;
 
3921          * If the caller has provided a response iocbq buffer, then context2
 
3922          * is NULL or its an error.
 
3925                 if (piocb->context2)
 
3927                 piocb->context2 = prspiocbq;
 
3930         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
 
3931         piocb->context_un.wait_queue = &done_q;
 
3932         piocb->iocb_flag &= ~LPFC_IO_WAKE;
 
3934         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
 
3935                 creg_val = readl(phba->HCregaddr);
 
3936                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
 
3937                 writel(creg_val, phba->HCregaddr);
 
3938                 readl(phba->HCregaddr); /* flush */
 
3941         retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
 
3942         if (retval == IOCB_SUCCESS) {
 
3943                 timeout_req = timeout * HZ;
 
3944                 timeleft = wait_event_timeout(done_q,
 
3945                                 piocb->iocb_flag & LPFC_IO_WAKE,
 
3948                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
 
3949                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 
3950                                         "0331 IOCB wake signaled\n");
 
3951                 } else if (timeleft == 0) {
 
3952                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
 
3953                                         "0338 IOCB wait timeout error - no "
 
3954                                         "wake response Data x%x\n", timeout);
 
3955                         retval = IOCB_TIMEDOUT;
 
3957                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
 
3958                                         "0330 IOCB wake NOT set, "
 
3960                                         timeout, (timeleft / jiffies));
 
3961                         retval = IOCB_TIMEDOUT;
 
3964                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 
3965                                 ":0332 IOCB wait issue failed, Data x%x\n",
 
3967                 retval = IOCB_ERROR;
 
3970         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
 
3971                 creg_val = readl(phba->HCregaddr);
 
3972                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
 
3973                 writel(creg_val, phba->HCregaddr);
 
3974                 readl(phba->HCregaddr); /* flush */
 
3978                 piocb->context2 = NULL;
 
3980         piocb->context_un.wait_queue = NULL;
 
3981         piocb->iocb_cmpl = NULL;
 
3986 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
 
3989         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
 
3993         /* The caller must leave context1 empty. */
 
3994         if (pmboxq->context1)
 
3995                 return MBX_NOT_FINISHED;
 
3997         /* setup wake call as IOCB callback */
 
3998         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
 
3999         /* setup context field to pass wait_queue pointer to wake function  */
 
4000         pmboxq->context1 = &done_q;
 
4002         /* now issue the command */
 
4003         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
 
4005         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
 
4006                 wait_event_interruptible_timeout(done_q,
 
4007                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
 
4010                 spin_lock_irqsave(&phba->hbalock, flag);
 
4011                 pmboxq->context1 = NULL;
 
4013                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
 
4014                  * else do not free the resources.
 
4016                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
 
4017                         retval = MBX_SUCCESS;
 
4019                         retval = MBX_TIMEOUT;
 
4020                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
 
4022                 spin_unlock_irqrestore(&phba->hbalock, flag);
 
4029 lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
 
4031         struct lpfc_vport *vport = phba->pport;
 
4035         while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
 
4036                 if (i++ > LPFC_MBOX_TMO * 1000)
 
4040                  * Call lpfc_sli_handle_mb_event only if a mailbox cmd
 
4041                  * did finish. This way we won't get the misleading
 
4042                  * "Stray Mailbox Interrupt" message.
 
4044                 spin_lock_irq(&phba->hbalock);
 
4045                 ha_copy = phba->work_ha;
 
4046                 phba->work_ha &= ~HA_MBATT;
 
4047                 spin_unlock_irq(&phba->hbalock);
 
4049                 if (ha_copy & HA_MBATT)
 
4050                         if (lpfc_sli_handle_mb_event(phba) == 0)
 
4056         return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
 
4060 lpfc_intr_handler(int irq, void *dev_id)
 
4062         struct lpfc_hba  *phba;
 
4064         uint32_t work_ha_copy;
 
4065         unsigned long status;
 
4068         MAILBOX_t *mbox, *pmbox;
 
4069         struct lpfc_vport *vport;
 
4070         struct lpfc_nodelist *ndlp;
 
4071         struct lpfc_dmabuf *mp;
 
4076          * Get the driver's phba structure from the dev_id and
 
4077          * assume the HBA is not interrupting.
 
4079         phba = (struct lpfc_hba *) dev_id;
 
4081         if (unlikely(!phba))
 
4084         /* If the pci channel is offline, ignore all the interrupts. */
 
4085         if (unlikely(pci_channel_offline(phba->pcidev)))
 
4088         phba->sli.slistat.sli_intr++;
 
4091          * Call the HBA to see if it is interrupting.  If not, don't claim
 
4095         /* Ignore all interrupts during initialization. */
 
4096         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
 
4100          * Read host attention register to determine interrupt source
 
4101          * Clear Attention Sources, except Error Attention (to
 
4102          * preserve status) and Link Attention
 
4104         spin_lock(&phba->hbalock);
 
4105         ha_copy = readl(phba->HAregaddr);
 
4106         /* If somebody is waiting to handle an eratt don't process it
 
4107          * here.  The brdkill function will do this.
 
4109         if (phba->link_flag & LS_IGNORE_ERATT)
 
4110                 ha_copy &= ~HA_ERATT;
 
4111         writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
 
4112         readl(phba->HAregaddr); /* flush */
 
4113         spin_unlock(&phba->hbalock);
 
4115         if (unlikely(!ha_copy))
 
4118         work_ha_copy = ha_copy & phba->work_ha_mask;
 
4120         if (unlikely(work_ha_copy)) {
 
4121                 if (work_ha_copy & HA_LATT) {
 
4122                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
 
4124                                  * Turn off Link Attention interrupts
 
4125                                  * until CLEAR_LA done
 
4127                                 spin_lock(&phba->hbalock);
 
4128                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
 
4129                                 control = readl(phba->HCregaddr);
 
4130                                 control &= ~HC_LAINT_ENA;
 
4131                                 writel(control, phba->HCregaddr);
 
4132                                 readl(phba->HCregaddr); /* flush */
 
4133                                 spin_unlock(&phba->hbalock);
 
4136                                 work_ha_copy &= ~HA_LATT;
 
4139                 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
 
4141                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
 
4142                          * the only slow ring.
 
4144                         status = (work_ha_copy &
 
4145                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
 
4146                         status >>= (4*LPFC_ELS_RING);
 
4147                         if (status & HA_RXMASK) {
 
4148                                 spin_lock(&phba->hbalock);
 
4149                                 control = readl(phba->HCregaddr);
 
4151                                 lpfc_debugfs_slow_ring_trc(phba,
 
4152                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
 
4154                                 (uint32_t)phba->sli.slistat.sli_intr);
 
4156                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
 
4157                                         lpfc_debugfs_slow_ring_trc(phba,
 
4159                                                 "pwork:x%x hawork:x%x wait:x%x",
 
4160                                                 phba->work_ha, work_ha_copy,
 
4161                                                 (uint32_t)((unsigned long)
 
4165                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
 
4166                                         writel(control, phba->HCregaddr);
 
4167                                         readl(phba->HCregaddr); /* flush */
 
4170                                         lpfc_debugfs_slow_ring_trc(phba,
 
4171                                                 "ISR slow ring:   pwork:"
 
4172                                                 "x%x hawork:x%x wait:x%x",
 
4173                                                 phba->work_ha, work_ha_copy,
 
4174                                                 (uint32_t)((unsigned long)
 
4177                                 spin_unlock(&phba->hbalock);
 
4181                 if (work_ha_copy & HA_ERATT) {
 
4183                          * There was a link/board error.  Read the
 
4184                          * status register to retrieve the error event
 
4187                         phba->sli.slistat.err_attn_event++;
 
4188                         /* Save status info */
 
4189                         phba->work_hs = readl(phba->HSregaddr);
 
4190                         phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
 
4191                         phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
 
4193                         /* Clear Chip error bit */
 
4194                         writel(HA_ERATT, phba->HAregaddr);
 
4195                         readl(phba->HAregaddr); /* flush */
 
4196                         phba->pport->stopped = 1;
 
4199                 spin_lock(&phba->hbalock);
 
4200                 if ((work_ha_copy & HA_MBATT) &&
 
4201                     (phba->sli.mbox_active)) {
 
4202                         pmb = phba->sli.mbox_active;
 
4204                         mbox = &phba->slim2p->mbx;
 
4207                         /* First check out the status word */
 
4208                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
 
4209                         if (pmbox->mbxOwner != OWN_HOST) {
 
4210                                 spin_unlock(&phba->hbalock);
 
4212                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
 
4213                                  * mbxStatus <status>
 
4215                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
 
4217                                                 "(%d):0304 Stray Mailbox "
 
4218                                                 "Interrupt mbxCommand x%x "
 
4220                                                 (vport ? vport->vpi : 0),
 
4223                                 /* clear mailbox attention bit */
 
4224                                 work_ha_copy &= ~HA_MBATT;
 
4226                                 phba->sli.mbox_active = NULL;
 
4227                                 spin_unlock(&phba->hbalock);
 
4228                                 phba->last_completion_time = jiffies;
 
4229                                 del_timer(&phba->sli.mbox_tmo);
 
4230                                 if (pmb->mbox_cmpl) {
 
4231                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
 
4234                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
 
4235                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
 
4237                                         lpfc_debugfs_disc_trc(vport,
 
4238                                                 LPFC_DISC_TRC_MBOX_VPORT,
 
4240                                                 "status:x%x rpi:x%x",
 
4241                                                 (uint32_t)pmbox->mbxStatus,
 
4242                                                 pmbox->un.varWords[0], 0);
 
4244                                         if (!pmbox->mbxStatus) {
 
4245                                                 mp = (struct lpfc_dmabuf *)
 
4247                                                 ndlp = (struct lpfc_nodelist *)
 
4250                                                 /* Reg_LOGIN of dflt RPI was
 
4251                                                  * successful. new lets get
 
4252                                                  * rid of the RPI using the
 
4255                                                 lpfc_unreg_login(phba,
 
4257                                                         pmbox->un.varWords[0],
 
4260                                                         lpfc_mbx_cmpl_dflt_rpi;
 
4262                                                 pmb->context2 = ndlp;
 
4264                                                 rc = lpfc_sli_issue_mbox(phba,
 
4268                                                         lpfc_printf_log(phba,
 
4271                                                         "0306 rc should have"
 
4273                                                 goto send_current_mbox;
 
4276                                 spin_lock(&phba->pport->work_port_lock);
 
4277                                 phba->pport->work_port_events &=
 
4279                                 spin_unlock(&phba->pport->work_port_lock);
 
4280                                 lpfc_mbox_cmpl_put(phba, pmb);
 
4283                         spin_unlock(&phba->hbalock);
 
4284                 if ((work_ha_copy & HA_MBATT) &&
 
4285                     (phba->sli.mbox_active == NULL)) {
 
4287                         /* Process next mailbox command if there is one */
 
4289                                 rc = lpfc_sli_issue_mbox(phba, NULL,
 
4291                         } while (rc == MBX_NOT_FINISHED);
 
4292                         if (rc != MBX_SUCCESS)
 
4293                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
 
4294                                                 LOG_SLI, "0349 rc should be "
 
4298                 spin_lock(&phba->hbalock);
 
4299                 phba->work_ha |= work_ha_copy;
 
4300                 if (phba->work_wait)
 
4301                         lpfc_worker_wake_up(phba);
 
4302                 spin_unlock(&phba->hbalock);
 
4305         ha_copy &= ~(phba->work_ha_mask);
 
4308          * Process all events on FCP ring.  Take the optimized path for
 
4309          * FCP IO.  Any other IO is slow path and is handled by
 
4310          * the worker thread.
 
4312         status = (ha_copy & (HA_RXMASK  << (4*LPFC_FCP_RING)));
 
4313         status >>= (4*LPFC_FCP_RING);
 
4314         if (status & HA_RXMASK)
 
4315                 lpfc_sli_handle_fast_ring_event(phba,
 
4316                                                 &phba->sli.ring[LPFC_FCP_RING],
 
4319         if (phba->cfg_multi_ring_support == 2) {
 
4321                  * Process all events on extra ring.  Take the optimized path
 
4322                  * for extra ring IO.  Any other IO is slow path and is handled
 
4323                  * by the worker thread.
 
4325                 status = (ha_copy & (HA_RXMASK  << (4*LPFC_EXTRA_RING)));
 
4326                 status >>= (4*LPFC_EXTRA_RING);
 
4327                 if (status & HA_RXMASK) {
 
4328                         lpfc_sli_handle_fast_ring_event(phba,
 
4329                                         &phba->sli.ring[LPFC_EXTRA_RING],
 
4335 } /* lpfc_intr_handler */