File Index
Files and directories
Sign in
FILE

lpfc_sli.c

File preview with syntax highlighting.

linux-6.19.14/drivers/scsi/lpfc/lpfc_sli.c C
    1/*******************************************************************
    2 * This file is part of the Emulex Linux Device Driver for         *
    3 * Fibre Channel Host Bus Adapters.                                *
    4 * Copyright (C) 2017-2025 Broadcom. All Rights Reserved. The term *
    5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
    6 * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
    7 * EMULEX and SLI are trademarks of Emulex.                        *
    8 * www.broadcom.com                                                *
    9 * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
   10 *                                                                 *
   11 * This program is free software; you can redistribute it and/or   *
   12 * modify it under the terms of version 2 of the GNU General       *
   13 * Public License as published by the Free Software Foundation.    *
   14 * This program is distributed in the hope that it will be useful. *
   15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
   16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
   17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
   18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
   19 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
   20 * more details, a copy of which can be found in the file COPYING  *
   21 * included with this package.                                     *
   22 *******************************************************************/
   23
   24#include <linux/blkdev.h>
   25#include <linux/pci.h>
   26#include <linux/interrupt.h>
   27#include <linux/delay.h>
   28#include <linux/slab.h>
   29#include <linux/lockdep.h>
   30#include <linux/dmi.h>
   31#include <linux/of.h>
   32
   33#include <scsi/scsi.h>
   34#include <scsi/scsi_cmnd.h>
   35#include <scsi/scsi_device.h>
   36#include <scsi/scsi_host.h>
   37#include <scsi/scsi_transport_fc.h>
   38#include <scsi/fc/fc_fs.h>
   39#include <linux/crash_dump.h>
   40#ifdef CONFIG_X86
   41#include <asm/set_memory.h>
   42#endif
   43
   44#include "lpfc_hw4.h"
   45#include "lpfc_hw.h"
   46#include "lpfc_sli.h"
   47#include "lpfc_sli4.h"
   48#include "lpfc_nl.h"
   49#include "lpfc_disc.h"
   50#include "lpfc.h"
   51#include "lpfc_scsi.h"
   52#include "lpfc_nvme.h"
   53#include "lpfc_crtn.h"
   54#include "lpfc_logmsg.h"
   55#include "lpfc_compat.h"
   56#include "lpfc_debugfs.h"
   57#include "lpfc_vport.h"
   58#include "lpfc_version.h"
   59
   60/* There are only four IOCB completion types. */
   61typedef enum _lpfc_iocb_type {
   62	LPFC_UNKNOWN_IOCB,
   63	LPFC_UNSOL_IOCB,
   64	LPFC_SOL_IOCB,
   65	LPFC_ABORT_IOCB
   66} lpfc_iocb_type;
   67
   68
   69/* Provide function prototypes local to this module. */
   70static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
   71				  uint32_t);
   72static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
   73			      uint8_t *, uint32_t *);
   74static struct lpfc_iocbq *
   75lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba *phba,
   76				  struct lpfc_iocbq *rspiocbq);
   77static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
   78				      struct hbq_dmabuf *);
   79static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
   80					  struct hbq_dmabuf *dmabuf);
   81static bool lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba,
   82				   struct lpfc_queue *cq, struct lpfc_cqe *cqe);
   83static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
   84				       int);
   85static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
   86				     struct lpfc_queue *eq,
   87				     struct lpfc_eqe *eqe,
   88				     enum lpfc_poll_mode poll_mode);
   89static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
   90static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
   91static struct lpfc_cqe *lpfc_sli4_cq_get(struct lpfc_queue *q);
   92static void __lpfc_sli4_consume_cqe(struct lpfc_hba *phba,
   93				    struct lpfc_queue *cq,
   94				    struct lpfc_cqe *cqe);
   95static uint16_t lpfc_wqe_bpl2sgl(struct lpfc_hba *phba,
   96				 struct lpfc_iocbq *pwqeq,
   97				 struct lpfc_sglq *sglq);
   98
   99union lpfc_wqe128 lpfc_iread_cmd_template;
  100union lpfc_wqe128 lpfc_iwrite_cmd_template;
  101union lpfc_wqe128 lpfc_icmnd_cmd_template;
  102
  103/* Setup WQE templates for IOs */
  104void lpfc_wqe_cmd_template(void)
  105{
  106	union lpfc_wqe128 *wqe;
  107
  108	/* IREAD template */
  109	wqe = &lpfc_iread_cmd_template;
  110	memset(wqe, 0, sizeof(union lpfc_wqe128));
  111
  112	/* Word 0, 1, 2 - BDE is variable */
  113
  114	/* Word 3 - cmd_buff_len, payload_offset_len is zero */
  115
  116	/* Word 4 - total_xfer_len is variable */
  117
  118	/* Word 5 - is zero */
  119
  120	/* Word 6 - ctxt_tag, xri_tag is variable */
  121
  122	/* Word 7 */
  123	bf_set(wqe_cmnd, &wqe->fcp_iread.wqe_com, CMD_FCP_IREAD64_WQE);
  124	bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, PARM_READ_CHECK);
  125	bf_set(wqe_class, &wqe->fcp_iread.wqe_com, CLASS3);
  126	bf_set(wqe_ct, &wqe->fcp_iread.wqe_com, SLI4_CT_RPI);
  127
  128	/* Word 8 - abort_tag is variable */
  129
  130	/* Word 9  - reqtag is variable */
  131
  132	/* Word 10 - dbde, wqes is variable */
  133	bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
  134	bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  135	bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com, LPFC_WQE_LENLOC_WORD4);
  136	bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
  137	bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
  138
  139	/* Word 11 - pbde is variable */
  140	bf_set(wqe_cmd_type, &wqe->fcp_iread.wqe_com, COMMAND_DATA_IN);
  141	bf_set(wqe_cqid, &wqe->fcp_iread.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  142	bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
  143
  144	/* Word 12 - is zero */
  145
  146	/* Word 13, 14, 15 - PBDE is variable */
  147
  148	/* IWRITE template */
  149	wqe = &lpfc_iwrite_cmd_template;
  150	memset(wqe, 0, sizeof(union lpfc_wqe128));
  151
  152	/* Word 0, 1, 2 - BDE is variable */
  153
  154	/* Word 3 - cmd_buff_len, payload_offset_len is zero */
  155
  156	/* Word 4 - total_xfer_len is variable */
  157
  158	/* Word 5 - initial_xfer_len is variable */
  159
  160	/* Word 6 - ctxt_tag, xri_tag is variable */
  161
  162	/* Word 7 */
  163	bf_set(wqe_cmnd, &wqe->fcp_iwrite.wqe_com, CMD_FCP_IWRITE64_WQE);
  164	bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, PARM_READ_CHECK);
  165	bf_set(wqe_class, &wqe->fcp_iwrite.wqe_com, CLASS3);
  166	bf_set(wqe_ct, &wqe->fcp_iwrite.wqe_com, SLI4_CT_RPI);
  167
  168	/* Word 8 - abort_tag is variable */
  169
  170	/* Word 9  - reqtag is variable */
  171
  172	/* Word 10 - dbde, wqes is variable */
  173	bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
  174	bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  175	bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_LENLOC_WORD4);
  176	bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
  177	bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
  178
  179	/* Word 11 - pbde is variable */
  180	bf_set(wqe_cmd_type, &wqe->fcp_iwrite.wqe_com, COMMAND_DATA_OUT);
  181	bf_set(wqe_cqid, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  182	bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
  183
  184	/* Word 12 - is zero */
  185
  186	/* Word 13, 14, 15 - PBDE is variable */
  187
  188	/* ICMND template */
  189	wqe = &lpfc_icmnd_cmd_template;
  190	memset(wqe, 0, sizeof(union lpfc_wqe128));
  191
  192	/* Word 0, 1, 2 - BDE is variable */
  193
  194	/* Word 3 - payload_offset_len is variable */
  195
  196	/* Word 4, 5 - is zero */
  197
  198	/* Word 6 - ctxt_tag, xri_tag is variable */
  199
  200	/* Word 7 */
  201	bf_set(wqe_cmnd, &wqe->fcp_icmd.wqe_com, CMD_FCP_ICMND64_WQE);
  202	bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  203	bf_set(wqe_class, &wqe->fcp_icmd.wqe_com, CLASS3);
  204	bf_set(wqe_ct, &wqe->fcp_icmd.wqe_com, SLI4_CT_RPI);
  205
  206	/* Word 8 - abort_tag is variable */
  207
  208	/* Word 9  - reqtag is variable */
  209
  210	/* Word 10 - dbde, wqes is variable */
  211	bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  212	bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_NONE);
  213	bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com, LPFC_WQE_LENLOC_NONE);
  214	bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
  215	bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
  216
  217	/* Word 11 */
  218	bf_set(wqe_cmd_type, &wqe->fcp_icmd.wqe_com, COMMAND_DATA_IN);
  219	bf_set(wqe_cqid, &wqe->fcp_icmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  220	bf_set(wqe_pbde, &wqe->fcp_icmd.wqe_com, 0);
  221
  222	/* Word 12, 13, 14, 15 - is zero */
  223}
  224
  225#if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
  226/**
  227 * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
  228 * @srcp: Source memory pointer.
  229 * @destp: Destination memory pointer.
  230 * @cnt: Number of words required to be copied.
  231 *       Must be a multiple of sizeof(uint64_t)
  232 *
  233 * This function is used for copying data between driver memory
  234 * and the SLI WQ. This function also changes the endianness
  235 * of each word if native endianness is different from SLI
  236 * endianness. This function can be called with or without
  237 * lock.
  238 **/
  239static void
  240lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  241{
  242	uint64_t *src = srcp;
  243	uint64_t *dest = destp;
  244	int i;
  245
  246	for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
  247		*dest++ = *src++;
  248}
  249#else
  250#define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
  251#endif
  252
  253/**
  254 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  255 * @q: The Work Queue to operate on.
  256 * @wqe: The work Queue Entry to put on the Work queue.
  257 *
  258 * This routine will copy the contents of @wqe to the next available entry on
  259 * the @q. This function will then ring the Work Queue Doorbell to signal the
  260 * HBA to start processing the Work Queue Entry. This function returns 0 if
  261 * successful. If no entries are available on @q then this function will return
  262 * -ENOMEM.
  263 * The caller is expected to hold the hbalock when calling this routine.
  264 **/
  265static int
  266lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
  267{
  268	union lpfc_wqe *temp_wqe;
  269	struct lpfc_register doorbell;
  270	uint32_t host_index;
  271	uint32_t idx;
  272	uint32_t i = 0;
  273	uint8_t *tmp;
  274	u32 if_type;
  275
  276	/* sanity check on queue memory */
  277	if (unlikely(!q))
  278		return -ENOMEM;
  279
  280	temp_wqe = lpfc_sli4_qe(q, q->host_index);
  281
  282	/* If the host has not yet processed the next entry then we are done */
  283	idx = ((q->host_index + 1) % q->entry_count);
  284	if (idx == q->hba_index) {
  285		q->WQ_overflow++;
  286		return -EBUSY;
  287	}
  288	q->WQ_posted++;
  289	/* set consumption flag every once in a while */
  290	if (!((q->host_index + 1) % q->notify_interval))
  291		bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  292	else
  293		bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
  294	if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  295		bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  296	lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  297	if (q->dpp_enable && q->phba->cfg_enable_dpp) {
  298		/* write to DPP aperture taking advatage of Combined Writes */
  299		tmp = (uint8_t *)temp_wqe;
  300#ifdef __raw_writeq
  301		for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
  302			__raw_writeq(*((uint64_t *)(tmp + i)),
  303					q->dpp_regaddr + i);
  304#else
  305		for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
  306			__raw_writel(*((uint32_t *)(tmp + i)),
  307					q->dpp_regaddr + i);
  308#endif
  309	}
  310	/* ensure WQE bcopy and DPP flushed before doorbell write */
  311	wmb();
  312
  313	/* Update the host index before invoking device */
  314	host_index = q->host_index;
  315
  316	q->host_index = idx;
  317
  318	/* Ring Doorbell */
  319	doorbell.word0 = 0;
  320	if (q->db_format == LPFC_DB_LIST_FORMAT) {
  321		if (q->dpp_enable && q->phba->cfg_enable_dpp) {
  322			bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
  323			bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
  324			bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
  325			    q->dpp_id);
  326			bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
  327			    q->queue_id);
  328		} else {
  329			bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
  330			bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
  331
  332			/* Leave bits <23:16> clear for if_type 6 dpp */
  333			if_type = bf_get(lpfc_sli_intf_if_type,
  334					 &q->phba->sli4_hba.sli_intf);
  335			if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
  336				bf_set(lpfc_wq_db_list_fm_index, &doorbell,
  337				       host_index);
  338		}
  339	} else if (q->db_format == LPFC_DB_RING_FORMAT) {
  340		bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
  341		bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
  342	} else {
  343		return -EINVAL;
  344	}
  345	writel(doorbell.word0, q->db_regaddr);
  346
  347	return 0;
  348}
  349
  350/**
  351 * lpfc_sli4_wq_release - Updates internal hba index for WQ
  352 * @q: The Work Queue to operate on.
  353 * @index: The index to advance the hba index to.
  354 *
  355 * This routine will update the HBA index of a queue to reflect consumption of
  356 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  357 * an entry the host calls this function to update the queue's internal
  358 * pointers.
  359 **/
  360static void
  361lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  362{
  363	/* sanity check on queue memory */
  364	if (unlikely(!q))
  365		return;
  366
  367	q->hba_index = index;
  368}
  369
  370/**
  371 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  372 * @q: The Mailbox Queue to operate on.
  373 * @mqe: The Mailbox Queue Entry to put on the Work queue.
  374 *
  375 * This routine will copy the contents of @mqe to the next available entry on
  376 * the @q. This function will then ring the Work Queue Doorbell to signal the
  377 * HBA to start processing the Work Queue Entry. This function returns 0 if
  378 * successful. If no entries are available on @q then this function will return
  379 * -ENOMEM.
  380 * The caller is expected to hold the hbalock when calling this routine.
  381 **/
  382static uint32_t
  383lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  384{
  385	struct lpfc_mqe *temp_mqe;
  386	struct lpfc_register doorbell;
  387
  388	/* sanity check on queue memory */
  389	if (unlikely(!q))
  390		return -ENOMEM;
  391	temp_mqe = lpfc_sli4_qe(q, q->host_index);
  392
  393	/* If the host has not yet processed the next entry then we are done */
  394	if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  395		return -ENOMEM;
  396	lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  397	/* Save off the mailbox pointer for completion */
  398	q->phba->mbox = (MAILBOX_t *)temp_mqe;
  399
  400	/* Update the host index before invoking device */
  401	q->host_index = ((q->host_index + 1) % q->entry_count);
  402
  403	/* Ring Doorbell */
  404	doorbell.word0 = 0;
  405	bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  406	bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  407	writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  408	return 0;
  409}
  410
  411/**
  412 * lpfc_sli4_mq_release - Updates internal hba index for MQ
  413 * @q: The Mailbox Queue to operate on.
  414 *
  415 * This routine will update the HBA index of a queue to reflect consumption of
  416 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  417 * an entry the host calls this function to update the queue's internal
  418 * pointers. This routine returns the number of entries that were consumed by
  419 * the HBA.
  420 **/
  421static uint32_t
  422lpfc_sli4_mq_release(struct lpfc_queue *q)
  423{
  424	/* sanity check on queue memory */
  425	if (unlikely(!q))
  426		return 0;
  427
  428	/* Clear the mailbox pointer for completion */
  429	q->phba->mbox = NULL;
  430	q->hba_index = ((q->hba_index + 1) % q->entry_count);
  431	return 1;
  432}
  433
  434/**
  435 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  436 * @q: The Event Queue to get the first valid EQE from
  437 *
  438 * This routine will get the first valid Event Queue Entry from @q, update
  439 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  440 * the Queue (no more work to do), or the Queue is full of EQEs that have been
  441 * processed, but not popped back to the HBA then this routine will return NULL.
  442 **/
  443static struct lpfc_eqe *
  444lpfc_sli4_eq_get(struct lpfc_queue *q)
  445{
  446	struct lpfc_eqe *eqe;
  447
  448	/* sanity check on queue memory */
  449	if (unlikely(!q))
  450		return NULL;
  451	eqe = lpfc_sli4_qe(q, q->host_index);
  452
  453	/* If the next EQE is not valid then we are done */
  454	if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
  455		return NULL;
  456
  457	/*
  458	 * insert barrier for instruction interlock : data from the hardware
  459	 * must have the valid bit checked before it can be copied and acted
  460	 * upon. Speculative instructions were allowing a bcopy at the start
  461	 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
  462	 * after our return, to copy data before the valid bit check above
  463	 * was done. As such, some of the copied data was stale. The barrier
  464	 * ensures the check is before any data is copied.
  465	 */
  466	mb();
  467	return eqe;
  468}
  469
  470/**
  471 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
  472 * @q: The Event Queue to disable interrupts
  473 *
  474 **/
  475void
  476lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
  477{
  478	struct lpfc_register doorbell;
  479
  480	doorbell.word0 = 0;
  481	bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  482	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  483	bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  484		(q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  485	bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  486	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
  487}
  488
  489/**
  490 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
  491 * @q: The Event Queue to disable interrupts
  492 *
  493 **/
  494void
  495lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
  496{
  497	struct lpfc_register doorbell;
  498
  499	doorbell.word0 = 0;
  500	bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
  501	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
  502}
  503
  504/**
  505 * lpfc_sli4_write_eq_db - write EQ DB for eqe's consumed or arm state
  506 * @phba: adapter with EQ
  507 * @q: The Event Queue that the host has completed processing for.
  508 * @count: Number of elements that have been consumed
  509 * @arm: Indicates whether the host wants to arms this CQ.
  510 *
  511 * This routine will notify the HBA, by ringing the doorbell, that count
  512 * number of EQEs have been processed. The @arm parameter indicates whether
  513 * the queue should be rearmed when ringing the doorbell.
  514 **/
  515void
  516lpfc_sli4_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
  517		     uint32_t count, bool arm)
  518{
  519	struct lpfc_register doorbell;
  520
  521	/* sanity check on queue memory */
  522	if (unlikely(!q || (count == 0 && !arm)))
  523		return;
  524
  525	/* ring doorbell for number popped */
  526	doorbell.word0 = 0;
  527	if (arm) {
  528		bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  529		bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  530	}
  531	bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
  532	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  533	bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  534			(q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  535	bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  536	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
  537	/* PCI read to flush PCI pipeline on re-arming for INTx mode */
  538	if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  539		readl(q->phba->sli4_hba.EQDBregaddr);
  540}
  541
  542/**
  543 * lpfc_sli4_if6_write_eq_db - write EQ DB for eqe's consumed or arm state
  544 * @phba: adapter with EQ
  545 * @q: The Event Queue that the host has completed processing for.
  546 * @count: Number of elements that have been consumed
  547 * @arm: Indicates whether the host wants to arms this CQ.
  548 *
  549 * This routine will notify the HBA, by ringing the doorbell, that count
  550 * number of EQEs have been processed. The @arm parameter indicates whether
  551 * the queue should be rearmed when ringing the doorbell.
  552 **/
  553void
  554lpfc_sli4_if6_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
  555			  uint32_t count, bool arm)
  556{
  557	struct lpfc_register doorbell;
  558
  559	/* sanity check on queue memory */
  560	if (unlikely(!q || (count == 0 && !arm)))
  561		return;
  562
  563	/* ring doorbell for number popped */
  564	doorbell.word0 = 0;
  565	if (arm)
  566		bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
  567	bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, count);
  568	bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
  569	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
  570	/* PCI read to flush PCI pipeline on re-arming for INTx mode */
  571	if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  572		readl(q->phba->sli4_hba.EQDBregaddr);
  573}
  574
  575static void
  576__lpfc_sli4_consume_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
  577			struct lpfc_eqe *eqe)
  578{
  579	if (!phba->sli4_hba.pc_sli4_params.eqav)
  580		bf_set_le32(lpfc_eqe_valid, eqe, 0);
  581
  582	eq->host_index = ((eq->host_index + 1) % eq->entry_count);
  583
  584	/* if the index wrapped around, toggle the valid bit */
  585	if (phba->sli4_hba.pc_sli4_params.eqav && !eq->host_index)
  586		eq->qe_valid = (eq->qe_valid) ? 0 : 1;
  587}
  588
  589static void
  590lpfc_sli4_eqcq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  591{
  592	struct lpfc_eqe *eqe = NULL;
  593	u32 eq_count = 0, cq_count = 0;
  594	struct lpfc_cqe *cqe = NULL;
  595	struct lpfc_queue *cq = NULL, *childq = NULL;
  596	int cqid = 0;
  597
  598	/* walk all the EQ entries and drop on the floor */
  599	eqe = lpfc_sli4_eq_get(eq);
  600	while (eqe) {
  601		/* Get the reference to the corresponding CQ */
  602		cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  603		cq = NULL;
  604
  605		list_for_each_entry(childq, &eq->child_list, list) {
  606			if (childq->queue_id == cqid) {
  607				cq = childq;
  608				break;
  609			}
  610		}
  611		/* If CQ is valid, iterate through it and drop all the CQEs */
  612		if (cq) {
  613			cqe = lpfc_sli4_cq_get(cq);
  614			while (cqe) {
  615				__lpfc_sli4_consume_cqe(phba, cq, cqe);
  616				cq_count++;
  617				cqe = lpfc_sli4_cq_get(cq);
  618			}
  619			/* Clear and re-arm the CQ */
  620			phba->sli4_hba.sli4_write_cq_db(phba, cq, cq_count,
  621			    LPFC_QUEUE_REARM);
  622			cq_count = 0;
  623		}
  624		__lpfc_sli4_consume_eqe(phba, eq, eqe);
  625		eq_count++;
  626		eqe = lpfc_sli4_eq_get(eq);
  627	}
  628
  629	/* Clear and re-arm the EQ */
  630	phba->sli4_hba.sli4_write_eq_db(phba, eq, eq_count, LPFC_QUEUE_REARM);
  631}
  632
  633static int
  634lpfc_sli4_process_eq(struct lpfc_hba *phba, struct lpfc_queue *eq,
  635		     u8 rearm, enum lpfc_poll_mode poll_mode)
  636{
  637	struct lpfc_eqe *eqe;
  638	int count = 0, consumed = 0;
  639
  640	if (cmpxchg(&eq->queue_claimed, 0, 1) != 0)
  641		goto rearm_and_exit;
  642
  643	eqe = lpfc_sli4_eq_get(eq);
  644	while (eqe) {
  645		lpfc_sli4_hba_handle_eqe(phba, eq, eqe, poll_mode);
  646		__lpfc_sli4_consume_eqe(phba, eq, eqe);
  647
  648		consumed++;
  649		if (!(++count % eq->max_proc_limit))
  650			break;
  651
  652		if (!(count % eq->notify_interval)) {
  653			phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed,
  654							LPFC_QUEUE_NOARM);
  655			consumed = 0;
  656		}
  657
  658		eqe = lpfc_sli4_eq_get(eq);
  659	}
  660	eq->EQ_processed += count;
  661
  662	/* Track the max number of EQEs processed in 1 intr */
  663	if (count > eq->EQ_max_eqe)
  664		eq->EQ_max_eqe = count;
  665
  666	xchg(&eq->queue_claimed, 0);
  667
  668rearm_and_exit:
  669	/* Always clear the EQ. */
  670	phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed, rearm);
  671
  672	return count;
  673}
  674
  675/**
  676 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  677 * @q: The Completion Queue to get the first valid CQE from
  678 *
  679 * This routine will get the first valid Completion Queue Entry from @q, update
  680 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  681 * the Queue (no more work to do), or the Queue is full of CQEs that have been
  682 * processed, but not popped back to the HBA then this routine will return NULL.
  683 **/
  684static struct lpfc_cqe *
  685lpfc_sli4_cq_get(struct lpfc_queue *q)
  686{
  687	struct lpfc_cqe *cqe;
  688
  689	/* sanity check on queue memory */
  690	if (unlikely(!q))
  691		return NULL;
  692	cqe = lpfc_sli4_qe(q, q->host_index);
  693
  694	/* If the next CQE is not valid then we are done */
  695	if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
  696		return NULL;
  697
  698	/*
  699	 * insert barrier for instruction interlock : data from the hardware
  700	 * must have the valid bit checked before it can be copied and acted
  701	 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
  702	 * instructions allowing action on content before valid bit checked,
  703	 * add barrier here as well. May not be needed as "content" is a
  704	 * single 32-bit entity here (vs multi word structure for cq's).
  705	 */
  706	mb();
  707	return cqe;
  708}
  709
  710static void
  711__lpfc_sli4_consume_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  712			struct lpfc_cqe *cqe)
  713{
  714	if (!phba->sli4_hba.pc_sli4_params.cqav)
  715		bf_set_le32(lpfc_cqe_valid, cqe, 0);
  716
  717	cq->host_index = ((cq->host_index + 1) % cq->entry_count);
  718
  719	/* if the index wrapped around, toggle the valid bit */
  720	if (phba->sli4_hba.pc_sli4_params.cqav && !cq->host_index)
  721		cq->qe_valid = (cq->qe_valid) ? 0 : 1;
  722}
  723
  724/**
  725 * lpfc_sli4_write_cq_db - write cq DB for entries consumed or arm state.
  726 * @phba: the adapter with the CQ
  727 * @q: The Completion Queue that the host has completed processing for.
  728 * @count: the number of elements that were consumed
  729 * @arm: Indicates whether the host wants to arms this CQ.
  730 *
  731 * This routine will notify the HBA, by ringing the doorbell, that the
  732 * CQEs have been processed. The @arm parameter specifies whether the
  733 * queue should be rearmed when ringing the doorbell.
  734 **/
  735void
  736lpfc_sli4_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
  737		     uint32_t count, bool arm)
  738{
  739	struct lpfc_register doorbell;
  740
  741	/* sanity check on queue memory */
  742	if (unlikely(!q || (count == 0 && !arm)))
  743		return;
  744
  745	/* ring doorbell for number popped */
  746	doorbell.word0 = 0;
  747	if (arm)
  748		bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  749	bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
  750	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  751	bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  752			(q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  753	bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  754	writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
  755}
  756
  757/**
  758 * lpfc_sli4_if6_write_cq_db - write cq DB for entries consumed or arm state.
  759 * @phba: the adapter with the CQ
  760 * @q: The Completion Queue that the host has completed processing for.
  761 * @count: the number of elements that were consumed
  762 * @arm: Indicates whether the host wants to arms this CQ.
  763 *
  764 * This routine will notify the HBA, by ringing the doorbell, that the
  765 * CQEs have been processed. The @arm parameter specifies whether the
  766 * queue should be rearmed when ringing the doorbell.
  767 **/
  768void
  769lpfc_sli4_if6_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
  770			 uint32_t count, bool arm)
  771{
  772	struct lpfc_register doorbell;
  773
  774	/* sanity check on queue memory */
  775	if (unlikely(!q || (count == 0 && !arm)))
  776		return;
  777
  778	/* ring doorbell for number popped */
  779	doorbell.word0 = 0;
  780	if (arm)
  781		bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
  782	bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, count);
  783	bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
  784	writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
  785}
  786
  787/*
  788 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  789 *
  790 * This routine will copy the contents of @wqe to the next available entry on
  791 * the @q. This function will then ring the Receive Queue Doorbell to signal the
  792 * HBA to start processing the Receive Queue Entry. This function returns the
  793 * index that the rqe was copied to if successful. If no entries are available
  794 * on @q then this function will return -ENOMEM.
  795 * The caller is expected to hold the hbalock when calling this routine.
  796 **/
  797int
  798lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  799		 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  800{
  801	struct lpfc_rqe *temp_hrqe;
  802	struct lpfc_rqe *temp_drqe;
  803	struct lpfc_register doorbell;
  804	int hq_put_index;
  805	int dq_put_index;
  806
  807	/* sanity check on queue memory */
  808	if (unlikely(!hq) || unlikely(!dq))
  809		return -ENOMEM;
  810	hq_put_index = hq->host_index;
  811	dq_put_index = dq->host_index;
  812	temp_hrqe = lpfc_sli4_qe(hq, hq_put_index);
  813	temp_drqe = lpfc_sli4_qe(dq, dq_put_index);
  814
  815	if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  816		return -EINVAL;
  817	if (hq_put_index != dq_put_index)
  818		return -EINVAL;
  819	/* If the host has not yet processed the next entry then we are done */
  820	if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
  821		return -EBUSY;
  822	lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  823	lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  824
  825	/* Update the host index to point to the next slot */
  826	hq->host_index = ((hq_put_index + 1) % hq->entry_count);
  827	dq->host_index = ((dq_put_index + 1) % dq->entry_count);
  828	hq->RQ_buf_posted++;
  829
  830	/* Ring The Header Receive Queue Doorbell */
  831	if (!(hq->host_index % hq->notify_interval)) {
  832		doorbell.word0 = 0;
  833		if (hq->db_format == LPFC_DB_RING_FORMAT) {
  834			bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
  835			       hq->notify_interval);
  836			bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
  837		} else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
  838			bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
  839			       hq->notify_interval);
  840			bf_set(lpfc_rq_db_list_fm_index, &doorbell,
  841			       hq->host_index);
  842			bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
  843		} else {
  844			return -EINVAL;
  845		}
  846		writel(doorbell.word0, hq->db_regaddr);
  847	}
  848	return hq_put_index;
  849}
  850
  851/*
  852 * lpfc_sli4_rq_release - Updates internal hba index for RQ
  853 *
  854 * This routine will update the HBA index of a queue to reflect consumption of
  855 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  856 * consumed an entry the host calls this function to update the queue's
  857 * internal pointers. This routine returns the number of entries that were
  858 * consumed by the HBA.
  859 **/
  860static uint32_t
  861lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  862{
  863	/* sanity check on queue memory */
  864	if (unlikely(!hq) || unlikely(!dq))
  865		return 0;
  866
  867	if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  868		return 0;
  869	hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  870	dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  871	return 1;
  872}
  873
  874/**
  875 * lpfc_cmd_iocb - Get next command iocb entry in the ring
  876 * @phba: Pointer to HBA context object.
  877 * @pring: Pointer to driver SLI ring object.
  878 *
  879 * This function returns pointer to next command iocb entry
  880 * in the command ring. The caller must hold hbalock to prevent
  881 * other threads consume the next command iocb.
  882 * SLI-2/SLI-3 provide different sized iocbs.
  883 **/
  884static inline IOCB_t *
  885lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  886{
  887	return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
  888			   pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
  889}
  890
  891/**
  892 * lpfc_resp_iocb - Get next response iocb entry in the ring
  893 * @phba: Pointer to HBA context object.
  894 * @pring: Pointer to driver SLI ring object.
  895 *
  896 * This function returns pointer to next response iocb entry
  897 * in the response ring. The caller must hold hbalock to make sure
  898 * that no other thread consume the next response iocb.
  899 * SLI-2/SLI-3 provide different sized iocbs.
  900 **/
  901static inline IOCB_t *
  902lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  903{
  904	return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
  905			   pring->sli.sli3.rspidx * phba->iocb_rsp_size);
  906}
  907
  908/**
  909 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  910 * @phba: Pointer to HBA context object.
  911 *
  912 * This function is called with hbalock held. This function
  913 * allocates a new driver iocb object from the iocb pool. If the
  914 * allocation is successful, it returns pointer to the newly
  915 * allocated iocb object else it returns NULL.
  916 **/
  917struct lpfc_iocbq *
  918__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  919{
  920	struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  921	struct lpfc_iocbq * iocbq = NULL;
  922
  923	lockdep_assert_held(&phba->hbalock);
  924
  925	list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  926	if (iocbq)
  927		phba->iocb_cnt++;
  928	if (phba->iocb_cnt > phba->iocb_max)
  929		phba->iocb_max = phba->iocb_cnt;
  930	return iocbq;
  931}
  932
  933/**
  934 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  935 * @phba: Pointer to HBA context object.
  936 * @xritag: XRI value.
  937 *
  938 * This function clears the sglq pointer from the array of active
  939 * sglq's. The xritag that is passed in is used to index into the
  940 * array. Before the xritag can be used it needs to be adjusted
  941 * by subtracting the xribase.
  942 *
  943 * Returns sglq ponter = success, NULL = Failure.
  944 **/
  945struct lpfc_sglq *
  946__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  947{
  948	struct lpfc_sglq *sglq;
  949
  950	sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  951	phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  952	return sglq;
  953}
  954
  955/**
  956 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  957 * @phba: Pointer to HBA context object.
  958 * @xritag: XRI value.
  959 *
  960 * This function returns the sglq pointer from the array of active
  961 * sglq's. The xritag that is passed in is used to index into the
  962 * array. Before the xritag can be used it needs to be adjusted
  963 * by subtracting the xribase.
  964 *
  965 * Returns sglq ponter = success, NULL = Failure.
  966 **/
  967struct lpfc_sglq *
  968__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  969{
  970	struct lpfc_sglq *sglq;
  971
  972	sglq =  phba->sli4_hba.lpfc_sglq_active_list[xritag];
  973	return sglq;
  974}
  975
  976/**
  977 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  978 * @phba: Pointer to HBA context object.
  979 * @xritag: xri used in this exchange.
  980 * @rrq: The RRQ to be cleared.
  981 *
  982 **/
  983void
  984lpfc_clr_rrq_active(struct lpfc_hba *phba,
  985		    uint16_t xritag,
  986		    struct lpfc_node_rrq *rrq)
  987{
  988	struct lpfc_nodelist *ndlp = NULL;
  989
  990	/* Lookup did to verify if did is still active on this vport */
  991	if (rrq->vport)
  992		ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  993
  994	if (!ndlp)
  995		goto out;
  996
  997	if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
  998		rrq->send_rrq = 0;
  999		rrq->xritag = 0;
 1000		rrq->rrq_stop_time = 0;
 1001	}
 1002out:
 1003	mempool_free(rrq, phba->rrq_pool);
 1004}
 1005
 1006/**
 1007 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
 1008 * @phba: Pointer to HBA context object.
 1009 *
 1010 * This function is called with hbalock held. This function
 1011 * Checks if stop_time (ratov from setting rrq active) has
 1012 * been reached, if it has and the send_rrq flag is set then
 1013 * it will call lpfc_send_rrq. If the send_rrq flag is not set
 1014 * then it will just call the routine to clear the rrq and
 1015 * free the rrq resource.
 1016 * The timer is set to the next rrq that is going to expire before
 1017 * leaving the routine.
 1018 *
 1019 **/
 1020void
 1021lpfc_handle_rrq_active(struct lpfc_hba *phba)
 1022{
 1023	struct lpfc_node_rrq *rrq;
 1024	struct lpfc_node_rrq *nextrrq;
 1025	unsigned long next_time;
 1026	unsigned long iflags;
 1027	LIST_HEAD(send_rrq);
 1028
 1029	clear_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);
 1030	next_time = jiffies + secs_to_jiffies(phba->fc_ratov + 1);
 1031	spin_lock_irqsave(&phba->rrq_list_lock, iflags);
 1032	list_for_each_entry_safe(rrq, nextrrq,
 1033				 &phba->active_rrq_list, list) {
 1034		if (time_after(jiffies, rrq->rrq_stop_time))
 1035			list_move(&rrq->list, &send_rrq);
 1036		else if (time_before(rrq->rrq_stop_time, next_time))
 1037			next_time = rrq->rrq_stop_time;
 1038	}
 1039	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);
 1040	if ((!list_empty(&phba->active_rrq_list)) &&
 1041	    (!test_bit(FC_UNLOADING, &phba->pport->load_flag)))
 1042		mod_timer(&phba->rrq_tmr, next_time);
 1043	list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
 1044		list_del(&rrq->list);
 1045		if (!rrq->send_rrq) {
 1046			/* this call will free the rrq */
 1047			lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
 1048		} else if (lpfc_send_rrq(phba, rrq)) {
 1049			/* if we send the rrq then the completion handler
 1050			*  will clear the bit in the xribitmap.
 1051			*/
 1052			lpfc_clr_rrq_active(phba, rrq->xritag,
 1053					    rrq);
 1054		}
 1055	}
 1056}
 1057
 1058/**
 1059 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
 1060 * @vport: Pointer to vport context object.
 1061 * @xri: The xri used in the exchange.
 1062 * @did: The targets DID for this exchange.
 1063 *
 1064 * returns NULL = rrq not found in the phba->active_rrq_list.
 1065 *         rrq = rrq for this xri and target.
 1066 **/
 1067struct lpfc_node_rrq *
 1068lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
 1069{
 1070	struct lpfc_hba *phba = vport->phba;
 1071	struct lpfc_node_rrq *rrq;
 1072	struct lpfc_node_rrq *nextrrq;
 1073	unsigned long iflags;
 1074
 1075	if (phba->sli_rev != LPFC_SLI_REV4)
 1076		return NULL;
 1077	spin_lock_irqsave(&phba->rrq_list_lock, iflags);
 1078	list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
 1079		if (rrq->vport == vport && rrq->xritag == xri &&
 1080				rrq->nlp_DID == did){
 1081			list_del(&rrq->list);
 1082			spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);
 1083			return rrq;
 1084		}
 1085	}
 1086	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);
 1087	return NULL;
 1088}
 1089
 1090/**
 1091 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
 1092 * @vport: Pointer to vport context object.
 1093 * @ndlp: Pointer to the lpfc_node_list structure.
 1094 * If ndlp is NULL Remove all active RRQs for this vport from the
 1095 * phba->active_rrq_list and clear the rrq.
 1096 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
 1097 **/
 1098void
 1099lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
 1100
 1101{
 1102	struct lpfc_hba *phba = vport->phba;
 1103	struct lpfc_node_rrq *rrq;
 1104	struct lpfc_node_rrq *nextrrq;
 1105	unsigned long iflags;
 1106	LIST_HEAD(rrq_list);
 1107
 1108	if (phba->sli_rev != LPFC_SLI_REV4)
 1109		return;
 1110	if (!ndlp) {
 1111		lpfc_sli4_vport_delete_els_xri_aborted(vport);
 1112		lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
 1113	}
 1114	spin_lock_irqsave(&phba->rrq_list_lock, iflags);
 1115	list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
 1116		if (rrq->vport != vport)
 1117			continue;
 1118
 1119		if (!ndlp || ndlp == lpfc_findnode_did(vport, rrq->nlp_DID))
 1120			list_move(&rrq->list, &rrq_list);
 1121
 1122	}
 1123	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);
 1124
 1125	list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
 1126		list_del(&rrq->list);
 1127		lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
 1128	}
 1129}
 1130
 1131/**
 1132 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
 1133 * @phba: Pointer to HBA context object.
 1134 * @ndlp: Targets nodelist pointer for this exchange.
 1135 * @xritag: the xri in the bitmap to test.
 1136 *
 1137 * This function returns:
 1138 * 0 = rrq not active for this xri
 1139 * 1 = rrq is valid for this xri.
 1140 **/
 1141int
 1142lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
 1143			uint16_t  xritag)
 1144{
 1145	if (!ndlp)
 1146		return 0;
 1147	if (!ndlp->active_rrqs_xri_bitmap)
 1148		return 0;
 1149	if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
 1150		return 1;
 1151	else
 1152		return 0;
 1153}
 1154
 1155/**
 1156 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
 1157 * @phba: Pointer to HBA context object.
 1158 * @ndlp: nodelist pointer for this target.
 1159 * @xritag: xri used in this exchange.
 1160 * @rxid: Remote Exchange ID.
 1161 * @send_rrq: Flag used to determine if we should send rrq els cmd.
 1162 *
 1163 * This function takes the hbalock.
 1164 * The active bit is always set in the active rrq xri_bitmap even
 1165 * if there is no slot avaiable for the other rrq information.
 1166 *
 1167 * returns 0 rrq actived for this xri
 1168 *         < 0 No memory or invalid ndlp.
 1169 **/
 1170int
 1171lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
 1172		    uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
 1173{
 1174	unsigned long iflags;
 1175	struct lpfc_node_rrq *rrq;
 1176	int empty;
 1177
 1178	if (!ndlp)
 1179		return -EINVAL;
 1180
 1181	if (!phba->cfg_enable_rrq)
 1182		return -EINVAL;
 1183
 1184	if (test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
 1185		clear_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);
 1186		goto outnl;
 1187	}
 1188
 1189	spin_lock_irqsave(&phba->hbalock, iflags);
 1190	if (ndlp->vport && test_bit(FC_UNLOADING, &ndlp->vport->load_flag))
 1191		goto out;
 1192
 1193	if (!ndlp->active_rrqs_xri_bitmap)
 1194		goto out;
 1195
 1196	if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
 1197		goto out;
 1198
 1199	spin_unlock_irqrestore(&phba->hbalock, iflags);
 1200	rrq = mempool_alloc(phba->rrq_pool, GFP_ATOMIC);
 1201	if (!rrq) {
 1202		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 1203				"3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
 1204				" DID:0x%x Send:%d\n",
 1205				xritag, rxid, ndlp->nlp_DID, send_rrq);
 1206		return -EINVAL;
 1207	}
 1208	if (phba->cfg_enable_rrq == 1)
 1209		rrq->send_rrq = send_rrq;
 1210	else
 1211		rrq->send_rrq = 0;
 1212	rrq->xritag = xritag;
 1213	rrq->rrq_stop_time = jiffies + secs_to_jiffies(phba->fc_ratov + 1);
 1214	rrq->nlp_DID = ndlp->nlp_DID;
 1215	rrq->vport = ndlp->vport;
 1216	rrq->rxid = rxid;
 1217
 1218	spin_lock_irqsave(&phba->rrq_list_lock, iflags);
 1219	empty = list_empty(&phba->active_rrq_list);
 1220	list_add_tail(&rrq->list, &phba->active_rrq_list);
 1221	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);
 1222	set_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);
 1223	if (empty)
 1224		lpfc_worker_wake_up(phba);
 1225	return 0;
 1226out:
 1227	spin_unlock_irqrestore(&phba->hbalock, iflags);
 1228outnl:
 1229	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 1230			"2921 Can't set rrq active xri:0x%x rxid:0x%x"
 1231			" DID:0x%x Send:%d\n",
 1232			xritag, rxid, ndlp->nlp_DID, send_rrq);
 1233	return -EINVAL;
 1234}
 1235
 1236/**
 1237 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
 1238 * @phba: Pointer to HBA context object.
 1239 * @piocbq: Pointer to the iocbq.
 1240 *
 1241 * The driver calls this function with either the nvme ls ring lock
 1242 * or the fc els ring lock held depending on the iocb usage.  This function
 1243 * gets a new driver sglq object from the sglq list. If the list is not empty
 1244 * then it is successful, it returns pointer to the newly allocated sglq
 1245 * object else it returns NULL.
 1246 **/
 1247static struct lpfc_sglq *
 1248__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
 1249{
 1250	struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
 1251	struct lpfc_sglq *sglq = NULL;
 1252	struct lpfc_sglq *start_sglq = NULL;
 1253	struct lpfc_io_buf *lpfc_cmd;
 1254	struct lpfc_nodelist *ndlp;
 1255	int found = 0;
 1256	u8 cmnd;
 1257
 1258	cmnd = get_job_cmnd(phba, piocbq);
 1259
 1260	if (piocbq->cmd_flag & LPFC_IO_FCP) {
 1261		lpfc_cmd = piocbq->io_buf;
 1262		ndlp = lpfc_cmd->rdata->pnode;
 1263	} else  if ((cmnd == CMD_GEN_REQUEST64_CR) &&
 1264			!(piocbq->cmd_flag & LPFC_IO_LIBDFC)) {
 1265		ndlp = piocbq->ndlp;
 1266	} else  if (piocbq->cmd_flag & LPFC_IO_LIBDFC) {
 1267		if (piocbq->cmd_flag & LPFC_IO_LOOPBACK)
 1268			ndlp = NULL;
 1269		else
 1270			ndlp = piocbq->ndlp;
 1271	} else {
 1272		ndlp = piocbq->ndlp;
 1273	}
 1274
 1275	spin_lock(&phba->sli4_hba.sgl_list_lock);
 1276	list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
 1277	start_sglq = sglq;
 1278	while (!found) {
 1279		if (!sglq)
 1280			break;
 1281		if (ndlp && ndlp->active_rrqs_xri_bitmap &&
 1282		    test_bit(sglq->sli4_lxritag,
 1283		    ndlp->active_rrqs_xri_bitmap)) {
 1284			/* This xri has an rrq outstanding for this DID.
 1285			 * put it back in the list and get another xri.
 1286			 */
 1287			list_add_tail(&sglq->list, lpfc_els_sgl_list);
 1288			sglq = NULL;
 1289			list_remove_head(lpfc_els_sgl_list, sglq,
 1290						struct lpfc_sglq, list);
 1291			if (sglq == start_sglq) {
 1292				list_add_tail(&sglq->list, lpfc_els_sgl_list);
 1293				sglq = NULL;
 1294				break;
 1295			} else
 1296				continue;
 1297		}
 1298		sglq->ndlp = ndlp;
 1299		found = 1;
 1300		phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
 1301		sglq->state = SGL_ALLOCATED;
 1302	}
 1303	spin_unlock(&phba->sli4_hba.sgl_list_lock);
 1304	return sglq;
 1305}
 1306
 1307/**
 1308 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
 1309 * @phba: Pointer to HBA context object.
 1310 * @piocbq: Pointer to the iocbq.
 1311 *
 1312 * This function is called with the sgl_list lock held. This function
 1313 * gets a new driver sglq object from the sglq list. If the
 1314 * list is not empty then it is successful, it returns pointer to the newly
 1315 * allocated sglq object else it returns NULL.
 1316 **/
 1317struct lpfc_sglq *
 1318__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
 1319{
 1320	struct list_head *lpfc_nvmet_sgl_list;
 1321	struct lpfc_sglq *sglq = NULL;
 1322
 1323	lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
 1324
 1325	lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
 1326
 1327	list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
 1328	if (!sglq)
 1329		return NULL;
 1330	phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
 1331	sglq->state = SGL_ALLOCATED;
 1332	return sglq;
 1333}
 1334
 1335/**
 1336 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
 1337 * @phba: Pointer to HBA context object.
 1338 *
 1339 * This function is called with no lock held. This function
 1340 * allocates a new driver iocb object from the iocb pool. If the
 1341 * allocation is successful, it returns pointer to the newly
 1342 * allocated iocb object else it returns NULL.
 1343 **/
 1344struct lpfc_iocbq *
 1345lpfc_sli_get_iocbq(struct lpfc_hba *phba)
 1346{
 1347	struct lpfc_iocbq * iocbq = NULL;
 1348	unsigned long iflags;
 1349
 1350	spin_lock_irqsave(&phba->hbalock, iflags);
 1351	iocbq = __lpfc_sli_get_iocbq(phba);
 1352	spin_unlock_irqrestore(&phba->hbalock, iflags);
 1353	return iocbq;
 1354}
 1355
 1356/**
 1357 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
 1358 * @phba: Pointer to HBA context object.
 1359 * @iocbq: Pointer to driver iocb object.
 1360 *
 1361 * This function is called to release the driver iocb object
 1362 * to the iocb pool. The iotag in the iocb object
 1363 * does not change for each use of the iocb object. This function
 1364 * clears all other fields of the iocb object when it is freed.
 1365 * The sqlq structure that holds the xritag and phys and virtual
 1366 * mappings for the scatter gather list is retrieved from the
 1367 * active array of sglq. The get of the sglq pointer also clears
 1368 * the entry in the array. If the status of the IO indiactes that
 1369 * this IO was aborted then the sglq entry it put on the
 1370 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
 1371 * IO has good status or fails for any other reason then the sglq
 1372 * entry is added to the free list (lpfc_els_sgl_list). The hbalock is
 1373 *  asserted held in the code path calling this routine.
 1374 **/
 1375static void
 1376__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
 1377{
 1378	struct lpfc_sglq *sglq;
 1379	unsigned long iflag = 0;
 1380	struct lpfc_sli_ring *pring;
 1381
 1382	if (iocbq->sli4_xritag == NO_XRI)
 1383		sglq = NULL;
 1384	else
 1385		sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
 1386
 1387
 1388	if (sglq)  {
 1389		if (iocbq->cmd_flag & LPFC_IO_NVMET) {
 1390			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
 1391					  iflag);
 1392			sglq->state = SGL_FREED;
 1393			sglq->ndlp = NULL;
 1394			list_add_tail(&sglq->list,
 1395				      &phba->sli4_hba.lpfc_nvmet_sgl_list);
 1396			spin_unlock_irqrestore(
 1397				&phba->sli4_hba.sgl_list_lock, iflag);
 1398			goto out;
 1399		}
 1400
 1401		if ((iocbq->cmd_flag & LPFC_EXCHANGE_BUSY) &&
 1402		    (!(unlikely(pci_channel_offline(phba->pcidev)))) &&
 1403		    sglq->state != SGL_XRI_ABORTED) {
 1404			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
 1405					  iflag);
 1406
 1407			/* Check if we can get a reference on ndlp */
 1408			if (sglq->ndlp && !lpfc_nlp_get(sglq->ndlp))
 1409				sglq->ndlp = NULL;
 1410
 1411			list_add(&sglq->list,
 1412				 &phba->sli4_hba.lpfc_abts_els_sgl_list);
 1413			spin_unlock_irqrestore(
 1414				&phba->sli4_hba.sgl_list_lock, iflag);
 1415		} else {
 1416			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
 1417					  iflag);
 1418			sglq->state = SGL_FREED;
 1419			sglq->ndlp = NULL;
 1420			list_add_tail(&sglq->list,
 1421				      &phba->sli4_hba.lpfc_els_sgl_list);
 1422			spin_unlock_irqrestore(
 1423				&phba->sli4_hba.sgl_list_lock, iflag);
 1424			pring = lpfc_phba_elsring(phba);
 1425			/* Check if TXQ queue needs to be serviced */
 1426			if (pring && (!list_empty(&pring->txq)))
 1427				lpfc_worker_wake_up(phba);
 1428		}
 1429	}
 1430
 1431out:
 1432	/*
 1433	 * Clean all volatile data fields, preserve iotag and node struct.
 1434	 */
 1435	memset_startat(iocbq, 0, wqe);
 1436	iocbq->sli4_lxritag = NO_XRI;
 1437	iocbq->sli4_xritag = NO_XRI;
 1438	iocbq->cmd_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET | LPFC_IO_CMF |
 1439			      LPFC_IO_NVME_LS);
 1440	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
 1441}
 1442
 1443
 1444/**
 1445 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
 1446 * @phba: Pointer to HBA context object.
 1447 * @iocbq: Pointer to driver iocb object.
 1448 *
 1449 * This function is called to release the driver iocb object to the
 1450 * iocb pool. The iotag in the iocb object does not change for each
 1451 * use of the iocb object. This function clears all other fields of
 1452 * the iocb object when it is freed. The hbalock is asserted held in
 1453 * the code path calling this routine.
 1454 **/
 1455static void
 1456__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
 1457{
 1458
 1459	/*
 1460	 * Clean all volatile data fields, preserve iotag and node struct.
 1461	 */
 1462	memset_startat(iocbq, 0, iocb);
 1463	iocbq->sli4_xritag = NO_XRI;
 1464	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
 1465}
 1466
 1467/**
 1468 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
 1469 * @phba: Pointer to HBA context object.
 1470 * @iocbq: Pointer to driver iocb object.
 1471 *
 1472 * This function is called with hbalock held to release driver
 1473 * iocb object to the iocb pool. The iotag in the iocb object
 1474 * does not change for each use of the iocb object. This function
 1475 * clears all other fields of the iocb object when it is freed.
 1476 **/
 1477static void
 1478__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
 1479{
 1480	lockdep_assert_held(&phba->hbalock);
 1481
 1482	phba->__lpfc_sli_release_iocbq(phba, iocbq);
 1483	phba->iocb_cnt--;
 1484}
 1485
 1486/**
 1487 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
 1488 * @phba: Pointer to HBA context object.
 1489 * @iocbq: Pointer to driver iocb object.
 1490 *
 1491 * This function is called with no lock held to release the iocb to
 1492 * iocb pool.
 1493 **/
 1494void
 1495lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
 1496{
 1497	unsigned long iflags;
 1498
 1499	/*
 1500	 * Clean all volatile data fields, preserve iotag and node struct.
 1501	 */
 1502	spin_lock_irqsave(&phba->hbalock, iflags);
 1503	__lpfc_sli_release_iocbq(phba, iocbq);
 1504	spin_unlock_irqrestore(&phba->hbalock, iflags);
 1505}
 1506
 1507/**
 1508 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
 1509 * @phba: Pointer to HBA context object.
 1510 * @iocblist: List of IOCBs.
 1511 * @ulpstatus: ULP status in IOCB command field.
 1512 * @ulpWord4: ULP word-4 in IOCB command field.
 1513 *
 1514 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
 1515 * on the list by invoking the complete callback function associated with the
 1516 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
 1517 * fields.
 1518 **/
 1519void
 1520lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
 1521		      uint32_t ulpstatus, uint32_t ulpWord4)
 1522{
 1523	struct lpfc_iocbq *piocb;
 1524
 1525	while (!list_empty(iocblist)) {
 1526		list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
 1527		if (piocb->cmd_cmpl) {
 1528			if (piocb->cmd_flag & LPFC_IO_NVME) {
 1529				lpfc_nvme_cancel_iocb(phba, piocb,
 1530						      ulpstatus, ulpWord4);
 1531			} else {
 1532				if (phba->sli_rev == LPFC_SLI_REV4) {
 1533					bf_set(lpfc_wcqe_c_status,
 1534					       &piocb->wcqe_cmpl, ulpstatus);
 1535					piocb->wcqe_cmpl.parameter = ulpWord4;
 1536				} else {
 1537					piocb->iocb.ulpStatus = ulpstatus;
 1538					piocb->iocb.un.ulpWord[4] = ulpWord4;
 1539				}
 1540				(piocb->cmd_cmpl) (phba, piocb, piocb);
 1541			}
 1542		} else {
 1543			lpfc_sli_release_iocbq(phba, piocb);
 1544		}
 1545	}
 1546	return;
 1547}
 1548
 1549/**
 1550 * lpfc_sli_iocb_cmd_type - Get the iocb type
 1551 * @iocb_cmnd: iocb command code.
 1552 *
 1553 * This function is called by ring event handler function to get the iocb type.
 1554 * This function translates the iocb command to an iocb command type used to
 1555 * decide the final disposition of each completed IOCB.
 1556 * The function returns
 1557 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
 1558 * LPFC_SOL_IOCB     if it is a solicited iocb completion
 1559 * LPFC_ABORT_IOCB   if it is an abort iocb
 1560 * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
 1561 *
 1562 * The caller is not required to hold any lock.
 1563 **/
 1564static lpfc_iocb_type
 1565lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
 1566{
 1567	lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
 1568
 1569	if (iocb_cmnd > CMD_MAX_IOCB_CMD)
 1570		return 0;
 1571
 1572	switch (iocb_cmnd) {
 1573	case CMD_XMIT_SEQUENCE_CR:
 1574	case CMD_XMIT_SEQUENCE_CX:
 1575	case CMD_XMIT_BCAST_CN:
 1576	case CMD_XMIT_BCAST_CX:
 1577	case CMD_ELS_REQUEST_CR:
 1578	case CMD_ELS_REQUEST_CX:
 1579	case CMD_CREATE_XRI_CR:
 1580	case CMD_CREATE_XRI_CX:
 1581	case CMD_GET_RPI_CN:
 1582	case CMD_XMIT_ELS_RSP_CX:
 1583	case CMD_GET_RPI_CR:
 1584	case CMD_FCP_IWRITE_CR:
 1585	case CMD_FCP_IWRITE_CX:
 1586	case CMD_FCP_IREAD_CR:
 1587	case CMD_FCP_IREAD_CX:
 1588	case CMD_FCP_ICMND_CR:
 1589	case CMD_FCP_ICMND_CX:
 1590	case CMD_FCP_TSEND_CX:
 1591	case CMD_FCP_TRSP_CX:
 1592	case CMD_FCP_TRECEIVE_CX:
 1593	case CMD_FCP_AUTO_TRSP_CX:
 1594	case CMD_ADAPTER_MSG:
 1595	case CMD_ADAPTER_DUMP:
 1596	case CMD_XMIT_SEQUENCE64_CR:
 1597	case CMD_XMIT_SEQUENCE64_CX:
 1598	case CMD_XMIT_BCAST64_CN:
 1599	case CMD_XMIT_BCAST64_CX:
 1600	case CMD_ELS_REQUEST64_CR:
 1601	case CMD_ELS_REQUEST64_CX:
 1602	case CMD_FCP_IWRITE64_CR:
 1603	case CMD_FCP_IWRITE64_CX:
 1604	case CMD_FCP_IREAD64_CR:
 1605	case CMD_FCP_IREAD64_CX:
 1606	case CMD_FCP_ICMND64_CR:
 1607	case CMD_FCP_ICMND64_CX:
 1608	case CMD_FCP_TSEND64_CX:
 1609	case CMD_FCP_TRSP64_CX:
 1610	case CMD_FCP_TRECEIVE64_CX:
 1611	case CMD_GEN_REQUEST64_CR:
 1612	case CMD_GEN_REQUEST64_CX:
 1613	case CMD_XMIT_ELS_RSP64_CX:
 1614	case DSSCMD_IWRITE64_CR:
 1615	case DSSCMD_IWRITE64_CX:
 1616	case DSSCMD_IREAD64_CR:
 1617	case DSSCMD_IREAD64_CX:
 1618	case CMD_SEND_FRAME:
 1619		type = LPFC_SOL_IOCB;
 1620		break;
 1621	case CMD_ABORT_XRI_CN:
 1622	case CMD_ABORT_XRI_CX:
 1623	case CMD_CLOSE_XRI_CN:
 1624	case CMD_CLOSE_XRI_CX:
 1625	case CMD_XRI_ABORTED_CX:
 1626	case CMD_ABORT_MXRI64_CN:
 1627	case CMD_XMIT_BLS_RSP64_CX:
 1628		type = LPFC_ABORT_IOCB;
 1629		break;
 1630	case CMD_RCV_SEQUENCE_CX:
 1631	case CMD_RCV_ELS_REQ_CX:
 1632	case CMD_RCV_SEQUENCE64_CX:
 1633	case CMD_RCV_ELS_REQ64_CX:
 1634	case CMD_ASYNC_STATUS:
 1635	case CMD_IOCB_RCV_SEQ64_CX:
 1636	case CMD_IOCB_RCV_ELS64_CX:
 1637	case CMD_IOCB_RCV_CONT64_CX:
 1638	case CMD_IOCB_RET_XRI64_CX:
 1639		type = LPFC_UNSOL_IOCB;
 1640		break;
 1641	case CMD_IOCB_XMIT_MSEQ64_CR:
 1642	case CMD_IOCB_XMIT_MSEQ64_CX:
 1643	case CMD_IOCB_RCV_SEQ_LIST64_CX:
 1644	case CMD_IOCB_RCV_ELS_LIST64_CX:
 1645	case CMD_IOCB_CLOSE_EXTENDED_CN:
 1646	case CMD_IOCB_ABORT_EXTENDED_CN:
 1647	case CMD_IOCB_RET_HBQE64_CN:
 1648	case CMD_IOCB_FCP_IBIDIR64_CR:
 1649	case CMD_IOCB_FCP_IBIDIR64_CX:
 1650	case CMD_IOCB_FCP_ITASKMGT64_CX:
 1651	case CMD_IOCB_LOGENTRY_CN:
 1652	case CMD_IOCB_LOGENTRY_ASYNC_CN:
 1653		printk("%s - Unhandled SLI-3 Command x%x\n",
 1654				__func__, iocb_cmnd);
 1655		type = LPFC_UNKNOWN_IOCB;
 1656		break;
 1657	default:
 1658		type = LPFC_UNKNOWN_IOCB;
 1659		break;
 1660	}
 1661
 1662	return type;
 1663}
 1664
 1665/**
 1666 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
 1667 * @phba: Pointer to HBA context object.
 1668 *
 1669 * This function is called from SLI initialization code
 1670 * to configure every ring of the HBA's SLI interface. The
 1671 * caller is not required to hold any lock. This function issues
 1672 * a config_ring mailbox command for each ring.
 1673 * This function returns zero if successful else returns a negative
 1674 * error code.
 1675 **/
 1676static int
 1677lpfc_sli_ring_map(struct lpfc_hba *phba)
 1678{
 1679	struct lpfc_sli *psli = &phba->sli;
 1680	LPFC_MBOXQ_t *pmb;
 1681	MAILBOX_t *pmbox;
 1682	int i, rc, ret = 0;
 1683
 1684	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 1685	if (!pmb)
 1686		return -ENOMEM;
 1687	pmbox = &pmb->u.mb;
 1688	phba->link_state = LPFC_INIT_MBX_CMDS;
 1689	for (i = 0; i < psli->num_rings; i++) {
 1690		lpfc_config_ring(phba, i, pmb);
 1691		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
 1692		if (rc != MBX_SUCCESS) {
 1693			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 1694					"0446 Adapter failed to init (%d), "
 1695					"mbxCmd x%x CFG_RING, mbxStatus x%x, "
 1696					"ring %d\n",
 1697					rc, pmbox->mbxCommand,
 1698					pmbox->mbxStatus, i);
 1699			phba->link_state = LPFC_HBA_ERROR;
 1700			ret = -ENXIO;
 1701			break;
 1702		}
 1703	}
 1704	mempool_free(pmb, phba->mbox_mem_pool);
 1705	return ret;
 1706}
 1707
 1708/**
 1709 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
 1710 * @phba: Pointer to HBA context object.
 1711 * @pring: Pointer to driver SLI ring object.
 1712 * @piocb: Pointer to the driver iocb object.
 1713 *
 1714 * The driver calls this function with the hbalock held for SLI3 ports or
 1715 * the ring lock held for SLI4 ports. The function adds the
 1716 * new iocb to txcmplq of the given ring. This function always returns
 1717 * 0. If this function is called for ELS ring, this function checks if
 1718 * there is a vport associated with the ELS command. This function also
 1719 * starts els_tmofunc timer if this is an ELS command.
 1720 **/
 1721static int
 1722lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 1723			struct lpfc_iocbq *piocb)
 1724{
 1725	u32 ulp_command = 0;
 1726
 1727	BUG_ON(!piocb);
 1728	ulp_command = get_job_cmnd(phba, piocb);
 1729
 1730	list_add_tail(&piocb->list, &pring->txcmplq);
 1731	piocb->cmd_flag |= LPFC_IO_ON_TXCMPLQ;
 1732	pring->txcmplq_cnt++;
 1733	if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
 1734	   (ulp_command != CMD_ABORT_XRI_WQE) &&
 1735	   (ulp_command != CMD_ABORT_XRI_CN) &&
 1736	   (ulp_command != CMD_CLOSE_XRI_CN)) {
 1737		BUG_ON(!piocb->vport);
 1738		if (!test_bit(FC_UNLOADING, &piocb->vport->load_flag))
 1739			mod_timer(&piocb->vport->els_tmofunc,
 1740				  jiffies + secs_to_jiffies(phba->fc_ratov << 1));
 1741	}
 1742
 1743	return 0;
 1744}
 1745
 1746/**
 1747 * lpfc_sli_ringtx_get - Get first element of the txq
 1748 * @phba: Pointer to HBA context object.
 1749 * @pring: Pointer to driver SLI ring object.
 1750 *
 1751 * This function is called with hbalock held to get next
 1752 * iocb in txq of the given ring. If there is any iocb in
 1753 * the txq, the function returns first iocb in the list after
 1754 * removing the iocb from the list, else it returns NULL.
 1755 **/
 1756struct lpfc_iocbq *
 1757lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 1758{
 1759	struct lpfc_iocbq *cmd_iocb;
 1760
 1761	lockdep_assert_held(&phba->hbalock);
 1762
 1763	list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
 1764	return cmd_iocb;
 1765}
 1766
 1767/**
 1768 * lpfc_cmf_sync_cmpl - Process a CMF_SYNC_WQE cmpl
 1769 * @phba: Pointer to HBA context object.
 1770 * @cmdiocb: Pointer to driver command iocb object.
 1771 * @rspiocb: Pointer to driver response iocb object.
 1772 *
 1773 * This routine will inform the driver of any BW adjustments we need
 1774 * to make. These changes will be picked up during the next CMF
 1775 * timer interrupt. In addition, any BW changes will be logged
 1776 * with LOG_CGN_MGMT.
 1777 **/
 1778static void
 1779lpfc_cmf_sync_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
 1780		   struct lpfc_iocbq *rspiocb)
 1781{
 1782	union lpfc_wqe128 *wqe;
 1783	uint32_t status, info;
 1784	struct lpfc_wcqe_complete *wcqe = &rspiocb->wcqe_cmpl;
 1785	uint64_t bw, bwdif, slop;
 1786	uint64_t pcent, bwpcent;
 1787	int asig, afpin, sigcnt, fpincnt;
 1788	int wsigmax, wfpinmax, cg, tdp;
 1789	char *s;
 1790
 1791	/* First check for error */
 1792	status = bf_get(lpfc_wcqe_c_status, wcqe);
 1793	if (status) {
 1794		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 1795				"6211 CMF_SYNC_WQE Error "
 1796				"req_tag x%x status x%x hwstatus x%x "
 1797				"tdatap x%x parm x%x\n",
 1798				bf_get(lpfc_wcqe_c_request_tag, wcqe),
 1799				bf_get(lpfc_wcqe_c_status, wcqe),
 1800				bf_get(lpfc_wcqe_c_hw_status, wcqe),
 1801				wcqe->total_data_placed,
 1802				wcqe->parameter);
 1803		goto out;
 1804	}
 1805
 1806	/* Gather congestion information on a successful cmpl */
 1807	info = wcqe->parameter;
 1808	phba->cmf_active_info = info;
 1809
 1810	/* See if firmware info count is valid or has changed */
 1811	if (info > LPFC_MAX_CMF_INFO || phba->cmf_info_per_interval == info)
 1812		info = 0;
 1813	else
 1814		phba->cmf_info_per_interval = info;
 1815
 1816	tdp = bf_get(lpfc_wcqe_c_cmf_bw, wcqe);
 1817	cg = bf_get(lpfc_wcqe_c_cmf_cg, wcqe);
 1818
 1819	/* Get BW requirement from firmware */
 1820	bw = (uint64_t)tdp * LPFC_CMF_BLK_SIZE;
 1821	if (!bw) {
 1822		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 1823				"6212 CMF_SYNC_WQE x%x: NULL bw\n",
 1824				bf_get(lpfc_wcqe_c_request_tag, wcqe));
 1825		goto out;
 1826	}
 1827
 1828	/* Gather information needed for logging if a BW change is required */
 1829	wqe = &cmdiocb->wqe;
 1830	asig = bf_get(cmf_sync_asig, &wqe->cmf_sync);
 1831	afpin = bf_get(cmf_sync_afpin, &wqe->cmf_sync);
 1832	fpincnt = bf_get(cmf_sync_wfpincnt, &wqe->cmf_sync);
 1833	sigcnt = bf_get(cmf_sync_wsigcnt, &wqe->cmf_sync);
 1834	if (phba->cmf_max_bytes_per_interval != bw ||
 1835	    (asig || afpin || sigcnt || fpincnt)) {
 1836		/* Are we increasing or decreasing BW */
 1837		if (phba->cmf_max_bytes_per_interval <  bw) {
 1838			bwdif = bw - phba->cmf_max_bytes_per_interval;
 1839			s = "Increase";
 1840		} else {
 1841			bwdif = phba->cmf_max_bytes_per_interval - bw;
 1842			s = "Decrease";
 1843		}
 1844
 1845		/* What is the change percentage */
 1846		slop = div_u64(phba->cmf_link_byte_count, 200); /*For rounding*/
 1847		pcent = div64_u64(bwdif * 100 + slop,
 1848				  phba->cmf_link_byte_count);
 1849		bwpcent = div64_u64(bw * 100 + slop,
 1850				    phba->cmf_link_byte_count);
 1851		/* Because of bytes adjustment due to shorter timer in
 1852		 * lpfc_cmf_timer() the cmf_link_byte_count can be shorter and
 1853		 * may seem like BW is above 100%.
 1854		 */
 1855		if (bwpcent > 100)
 1856			bwpcent = 100;
 1857
 1858		if (phba->cmf_max_bytes_per_interval < bw &&
 1859		    bwpcent > 95)
 1860			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 1861					"6208 Congestion bandwidth "
 1862					"limits removed\n");
 1863		else if ((phba->cmf_max_bytes_per_interval > bw) &&
 1864			 ((bwpcent + pcent) <= 100) && ((bwpcent + pcent) > 95))
 1865			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 1866					"6209 Congestion bandwidth "
 1867					"limits in effect\n");
 1868
 1869		if (asig) {
 1870			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 1871					"6237 BW Threshold %lld%% (%lld): "
 1872					"%lld%% %s: Signal Alarm: cg:%d "
 1873					"Info:%u\n",
 1874					bwpcent, bw, pcent, s, cg,
 1875					phba->cmf_active_info);
 1876		} else if (afpin) {
 1877			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 1878					"6238 BW Threshold %lld%% (%lld): "
 1879					"%lld%% %s: FPIN Alarm: cg:%d "
 1880					"Info:%u\n",
 1881					bwpcent, bw, pcent, s, cg,
 1882					phba->cmf_active_info);
 1883		} else if (sigcnt) {
 1884			wsigmax = bf_get(cmf_sync_wsigmax, &wqe->cmf_sync);
 1885			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 1886					"6239 BW Threshold %lld%% (%lld): "
 1887					"%lld%% %s: Signal Warning: "
 1888					"Cnt %d Max %d: cg:%d Info:%u\n",
 1889					bwpcent, bw, pcent, s, sigcnt,
 1890					wsigmax, cg, phba->cmf_active_info);
 1891		} else if (fpincnt) {
 1892			wfpinmax = bf_get(cmf_sync_wfpinmax, &wqe->cmf_sync);
 1893			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 1894					"6240 BW Threshold %lld%% (%lld): "
 1895					"%lld%% %s: FPIN Warning: "
 1896					"Cnt %d Max %d: cg:%d Info:%u\n",
 1897					bwpcent, bw, pcent, s, fpincnt,
 1898					wfpinmax, cg, phba->cmf_active_info);
 1899		} else {
 1900			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 1901					"6241 BW Threshold %lld%% (%lld): "
 1902					"CMF %lld%% %s: cg:%d Info:%u\n",
 1903					bwpcent, bw, pcent, s, cg,
 1904					phba->cmf_active_info);
 1905		}
 1906	} else if (info) {
 1907		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 1908				"6246 Info Threshold %u\n", info);
 1909	}
 1910
 1911	/* Save BW change to be picked up during next timer interrupt */
 1912	phba->cmf_last_sync_bw = bw;
 1913out:
 1914	lpfc_sli_release_iocbq(phba, cmdiocb);
 1915}
 1916
 1917/**
 1918 * lpfc_issue_cmf_sync_wqe - Issue a CMF_SYNC_WQE
 1919 * @phba: Pointer to HBA context object.
 1920 * @ms:   ms to set in WQE interval, 0 means use init op
 1921 * @total: Total rcv bytes for this interval
 1922 *
 1923 * This routine is called every CMF timer interrupt. Its purpose is
 1924 * to issue a CMF_SYNC_WQE to the firmware to inform it of any events
 1925 * that may indicate we have congestion (FPINs or Signals). Upon
 1926 * completion, the firmware will indicate any BW restrictions the
 1927 * driver may need to take.
 1928 **/
 1929int
 1930lpfc_issue_cmf_sync_wqe(struct lpfc_hba *phba, u32 ms, u64 total)
 1931{
 1932	union lpfc_wqe128 *wqe;
 1933	struct lpfc_iocbq *sync_buf;
 1934	unsigned long iflags;
 1935	u32 ret_val, cgn_sig_freq;
 1936	u32 atot, wtot, max;
 1937	u8 warn_sync_period = 0;
 1938
 1939	/* First address any alarm / warning activity */
 1940	atot = atomic_xchg(&phba->cgn_sync_alarm_cnt, 0);
 1941	wtot = atomic_xchg(&phba->cgn_sync_warn_cnt, 0);
 1942
 1943	spin_lock_irqsave(&phba->hbalock, iflags);
 1944
 1945	/* ONLY Managed mode will send the CMF_SYNC_WQE to the HBA */
 1946	if (phba->cmf_active_mode != LPFC_CFG_MANAGED ||
 1947	    phba->link_state < LPFC_LINK_UP) {
 1948		ret_val = 0;
 1949		goto out_unlock;
 1950	}
 1951
 1952	sync_buf = __lpfc_sli_get_iocbq(phba);
 1953	if (!sync_buf) {
 1954		lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT,
 1955				"6244 No available WQEs for CMF_SYNC_WQE\n");
 1956		ret_val = ENOMEM;
 1957		goto out_unlock;
 1958	}
 1959
 1960	wqe = &sync_buf->wqe;
 1961
 1962	/* WQEs are reused.  Clear stale data and set key fields to zero */
 1963	memset(wqe, 0, sizeof(*wqe));
 1964
 1965	/* If this is the very first CMF_SYNC_WQE, issue an init operation */
 1966	if (!ms) {
 1967		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 1968				"6441 CMF Init %d - CMF_SYNC_WQE\n",
 1969				phba->fc_eventTag);
 1970		bf_set(cmf_sync_op, &wqe->cmf_sync, 1); /* 1=init */
 1971		bf_set(cmf_sync_interval, &wqe->cmf_sync, LPFC_CMF_INTERVAL);
 1972		goto initpath;
 1973	}
 1974
 1975	bf_set(cmf_sync_op, &wqe->cmf_sync, 0); /* 0=recalc */
 1976	bf_set(cmf_sync_interval, &wqe->cmf_sync, ms);
 1977
 1978	/* Check for alarms / warnings */
 1979	if (atot) {
 1980		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
 1981			/* We hit an Signal alarm condition */
 1982			bf_set(cmf_sync_asig, &wqe->cmf_sync, 1);
 1983		} else {
 1984			/* We hit a FPIN alarm condition */
 1985			bf_set(cmf_sync_afpin, &wqe->cmf_sync, 1);
 1986		}
 1987	} else if (wtot) {
 1988		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY ||
 1989		    phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
 1990			cgn_sig_freq = phba->cgn_sig_freq ? phba->cgn_sig_freq :
 1991					lpfc_fabric_cgn_frequency;
 1992			/* We hit an Signal warning condition */
 1993			max = LPFC_SEC_TO_MSEC / cgn_sig_freq *
 1994				lpfc_acqe_cgn_frequency;
 1995			bf_set(cmf_sync_wsigmax, &wqe->cmf_sync, max);
 1996			bf_set(cmf_sync_wsigcnt, &wqe->cmf_sync, wtot);
 1997			warn_sync_period = lpfc_acqe_cgn_frequency;
 1998		} else {
 1999			/* We hit a FPIN warning condition */
 2000			bf_set(cmf_sync_wfpinmax, &wqe->cmf_sync, 1);
 2001			bf_set(cmf_sync_wfpincnt, &wqe->cmf_sync, 1);
 2002			if (phba->cgn_fpin_frequency != LPFC_FPIN_INIT_FREQ)
 2003				warn_sync_period =
 2004				LPFC_MSECS_TO_SECS(phba->cgn_fpin_frequency);
 2005		}
 2006	}
 2007
 2008	/* Update total read blocks during previous timer interval */
 2009	wqe->cmf_sync.read_bytes = (u32)(total / LPFC_CMF_BLK_SIZE);
 2010
 2011initpath:
 2012	bf_set(cmf_sync_ver, &wqe->cmf_sync, LPFC_CMF_SYNC_VER);
 2013	wqe->cmf_sync.event_tag = phba->fc_eventTag;
 2014	bf_set(cmf_sync_cmnd, &wqe->cmf_sync, CMD_CMF_SYNC_WQE);
 2015
 2016	/* Setup reqtag to match the wqe completion. */
 2017	bf_set(cmf_sync_reqtag, &wqe->cmf_sync, sync_buf->iotag);
 2018
 2019	bf_set(cmf_sync_qosd, &wqe->cmf_sync, 1);
 2020	bf_set(cmf_sync_period, &wqe->cmf_sync, warn_sync_period);
 2021
 2022	bf_set(cmf_sync_cmd_type, &wqe->cmf_sync, CMF_SYNC_COMMAND);
 2023	bf_set(cmf_sync_wqec, &wqe->cmf_sync, 1);
 2024	bf_set(cmf_sync_cqid, &wqe->cmf_sync, LPFC_WQE_CQ_ID_DEFAULT);
 2025
 2026	sync_buf->vport = phba->pport;
 2027	sync_buf->cmd_cmpl = lpfc_cmf_sync_cmpl;
 2028	sync_buf->cmd_dmabuf = NULL;
 2029	sync_buf->rsp_dmabuf = NULL;
 2030	sync_buf->bpl_dmabuf = NULL;
 2031	sync_buf->sli4_xritag = NO_XRI;
 2032
 2033	sync_buf->cmd_flag |= LPFC_IO_CMF;
 2034	ret_val = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], sync_buf);
 2035	if (ret_val) {
 2036		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 2037				"6214 Cannot issue CMF_SYNC_WQE: x%x\n",
 2038				ret_val);
 2039		__lpfc_sli_release_iocbq(phba, sync_buf);
 2040	}
 2041out_unlock:
 2042	spin_unlock_irqrestore(&phba->hbalock, iflags);
 2043	return ret_val;
 2044}
 2045
 2046/**
 2047 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
 2048 * @phba: Pointer to HBA context object.
 2049 * @pring: Pointer to driver SLI ring object.
 2050 *
 2051 * This function is called with hbalock held and the caller must post the
 2052 * iocb without releasing the lock. If the caller releases the lock,
 2053 * iocb slot returned by the function is not guaranteed to be available.
 2054 * The function returns pointer to the next available iocb slot if there
 2055 * is available slot in the ring, else it returns NULL.
 2056 * If the get index of the ring is ahead of the put index, the function
 2057 * will post an error attention event to the worker thread to take the
 2058 * HBA to offline state.
 2059 **/
 2060static IOCB_t *
 2061lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 2062{
 2063	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
 2064	uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;
 2065
 2066	lockdep_assert_held(&phba->hbalock);
 2067
 2068	if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
 2069	   (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
 2070		pring->sli.sli3.next_cmdidx = 0;
 2071
 2072	if (unlikely(pring->sli.sli3.local_getidx ==
 2073		pring->sli.sli3.next_cmdidx)) {
 2074
 2075		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
 2076
 2077		if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
 2078			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 2079					"0315 Ring %d issue: portCmdGet %d "
 2080					"is bigger than cmd ring %d\n",
 2081					pring->ringno,
 2082					pring->sli.sli3.local_getidx,
 2083					max_cmd_idx);
 2084
 2085			phba->link_state = LPFC_HBA_ERROR;
 2086			/*
 2087			 * All error attention handlers are posted to
 2088			 * worker thread
 2089			 */
 2090			phba->work_ha |= HA_ERATT;
 2091			phba->work_hs = HS_FFER3;
 2092
 2093			lpfc_worker_wake_up(phba);
 2094
 2095			return NULL;
 2096		}
 2097
 2098		if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
 2099			return NULL;
 2100	}
 2101
 2102	return lpfc_cmd_iocb(phba, pring);
 2103}
 2104
 2105/**
 2106 * lpfc_sli_next_iotag - Get an iotag for the iocb
 2107 * @phba: Pointer to HBA context object.
 2108 * @iocbq: Pointer to driver iocb object.
 2109 *
 2110 * This function gets an iotag for the iocb. If there is no unused iotag and
 2111 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
 2112 * array and assigns a new iotag.
 2113 * The function returns the allocated iotag if successful, else returns zero.
 2114 * Zero is not a valid iotag.
 2115 * The caller is not required to hold any lock.
 2116 **/
 2117uint16_t
 2118lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
 2119{
 2120	struct lpfc_iocbq **new_arr;
 2121	struct lpfc_iocbq **old_arr;
 2122	size_t new_len;
 2123	struct lpfc_sli *psli = &phba->sli;
 2124	uint16_t iotag;
 2125
 2126	spin_lock_irq(&phba->hbalock);
 2127	iotag = psli->last_iotag;
 2128	if(++iotag < psli->iocbq_lookup_len) {
 2129		psli->last_iotag = iotag;
 2130		psli->iocbq_lookup[iotag] = iocbq;
 2131		spin_unlock_irq(&phba->hbalock);
 2132		iocbq->iotag = iotag;
 2133		return iotag;
 2134	} else if (psli->iocbq_lookup_len < (0xffff
 2135					   - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
 2136		new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
 2137		spin_unlock_irq(&phba->hbalock);
 2138		new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
 2139				  GFP_KERNEL);
 2140		if (new_arr) {
 2141			spin_lock_irq(&phba->hbalock);
 2142			old_arr = psli->iocbq_lookup;
 2143			if (new_len <= psli->iocbq_lookup_len) {
 2144				/* highly unprobable case */
 2145				kfree(new_arr);
 2146				iotag = psli->last_iotag;
 2147				if(++iotag < psli->iocbq_lookup_len) {
 2148					psli->last_iotag = iotag;
 2149					psli->iocbq_lookup[iotag] = iocbq;
 2150					spin_unlock_irq(&phba->hbalock);
 2151					iocbq->iotag = iotag;
 2152					return iotag;
 2153				}
 2154				spin_unlock_irq(&phba->hbalock);
 2155				return 0;
 2156			}
 2157			if (psli->iocbq_lookup)
 2158				memcpy(new_arr, old_arr,
 2159				       ((psli->last_iotag  + 1) *
 2160					sizeof (struct lpfc_iocbq *)));
 2161			psli->iocbq_lookup = new_arr;
 2162			psli->iocbq_lookup_len = new_len;
 2163			psli->last_iotag = iotag;
 2164			psli->iocbq_lookup[iotag] = iocbq;
 2165			spin_unlock_irq(&phba->hbalock);
 2166			iocbq->iotag = iotag;
 2167			kfree(old_arr);
 2168			return iotag;
 2169		}
 2170	} else
 2171		spin_unlock_irq(&phba->hbalock);
 2172
 2173	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
 2174			"0318 Failed to allocate IOTAG.last IOTAG is %d\n",
 2175			psli->last_iotag);
 2176
 2177	return 0;
 2178}
 2179
 2180/**
 2181 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
 2182 * @phba: Pointer to HBA context object.
 2183 * @pring: Pointer to driver SLI ring object.
 2184 * @iocb: Pointer to iocb slot in the ring.
 2185 * @nextiocb: Pointer to driver iocb object which need to be
 2186 *            posted to firmware.
 2187 *
 2188 * This function is called to post a new iocb to the firmware. This
 2189 * function copies the new iocb to ring iocb slot and updates the
 2190 * ring pointers. It adds the new iocb to txcmplq if there is
 2191 * a completion call back for this iocb else the function will free the
 2192 * iocb object.  The hbalock is asserted held in the code path calling
 2193 * this routine.
 2194 **/
 2195static void
 2196lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 2197		IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
 2198{
 2199	/*
 2200	 * Set up an iotag
 2201	 */
 2202	nextiocb->iocb.ulpIoTag = (nextiocb->cmd_cmpl) ? nextiocb->iotag : 0;
 2203
 2204
 2205	if (pring->ringno == LPFC_ELS_RING) {
 2206		lpfc_debugfs_slow_ring_trc(phba,
 2207			"IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
 2208			*(((uint32_t *) &nextiocb->iocb) + 4),
 2209			*(((uint32_t *) &nextiocb->iocb) + 6),
 2210			*(((uint32_t *) &nextiocb->iocb) + 7));
 2211	}
 2212
 2213	/*
 2214	 * Issue iocb command to adapter
 2215	 */
 2216	lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
 2217	wmb();
 2218	pring->stats.iocb_cmd++;
 2219
 2220	/*
 2221	 * If there is no completion routine to call, we can release the
 2222	 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
 2223	 * that have no rsp ring completion, cmd_cmpl MUST be NULL.
 2224	 */
 2225	if (nextiocb->cmd_cmpl)
 2226		lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
 2227	else
 2228		__lpfc_sli_release_iocbq(phba, nextiocb);
 2229
 2230	/*
 2231	 * Let the HBA know what IOCB slot will be the next one the
 2232	 * driver will put a command into.
 2233	 */
 2234	pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
 2235	writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
 2236}
 2237
 2238/**
 2239 * lpfc_sli_update_full_ring - Update the chip attention register
 2240 * @phba: Pointer to HBA context object.
 2241 * @pring: Pointer to driver SLI ring object.
 2242 *
 2243 * The caller is not required to hold any lock for calling this function.
 2244 * This function updates the chip attention bits for the ring to inform firmware
 2245 * that there are pending work to be done for this ring and requests an
 2246 * interrupt when there is space available in the ring. This function is
 2247 * called when the driver is unable to post more iocbs to the ring due
 2248 * to unavailability of space in the ring.
 2249 **/
 2250static void
 2251lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 2252{
 2253	int ringno = pring->ringno;
 2254
 2255	pring->flag |= LPFC_CALL_RING_AVAILABLE;
 2256
 2257	wmb();
 2258
 2259	/*
 2260	 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
 2261	 * The HBA will tell us when an IOCB entry is available.
 2262	 */
 2263	writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
 2264	readl(phba->CAregaddr); /* flush */
 2265
 2266	pring->stats.iocb_cmd_full++;
 2267}
 2268
 2269/**
 2270 * lpfc_sli_update_ring - Update chip attention register
 2271 * @phba: Pointer to HBA context object.
 2272 * @pring: Pointer to driver SLI ring object.
 2273 *
 2274 * This function updates the chip attention register bit for the
 2275 * given ring to inform HBA that there is more work to be done
 2276 * in this ring. The caller is not required to hold any lock.
 2277 **/
 2278static void
 2279lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 2280{
 2281	int ringno = pring->ringno;
 2282
 2283	/*
 2284	 * Tell the HBA that there is work to do in this ring.
 2285	 */
 2286	if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
 2287		wmb();
 2288		writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
 2289		readl(phba->CAregaddr); /* flush */
 2290	}
 2291}
 2292
 2293/**
 2294 * lpfc_sli_resume_iocb - Process iocbs in the txq
 2295 * @phba: Pointer to HBA context object.
 2296 * @pring: Pointer to driver SLI ring object.
 2297 *
 2298 * This function is called with hbalock held to post pending iocbs
 2299 * in the txq to the firmware. This function is called when driver
 2300 * detects space available in the ring.
 2301 **/
 2302static void
 2303lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 2304{
 2305	IOCB_t *iocb;
 2306	struct lpfc_iocbq *nextiocb;
 2307
 2308	lockdep_assert_held(&phba->hbalock);
 2309
 2310	/*
 2311	 * Check to see if:
 2312	 *  (a) there is anything on the txq to send
 2313	 *  (b) link is up
 2314	 *  (c) link attention events can be processed (fcp ring only)
 2315	 *  (d) IOCB processing is not blocked by the outstanding mbox command.
 2316	 */
 2317
 2318	if (lpfc_is_link_up(phba) &&
 2319	    (!list_empty(&pring->txq)) &&
 2320	    (pring->ringno != LPFC_FCP_RING ||
 2321	     phba->sli.sli_flag & LPFC_PROCESS_LA)) {
 2322
 2323		while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
 2324		       (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
 2325			lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
 2326
 2327		if (iocb)
 2328			lpfc_sli_update_ring(phba, pring);
 2329		else
 2330			lpfc_sli_update_full_ring(phba, pring);
 2331	}
 2332
 2333	return;
 2334}
 2335
 2336/**
 2337 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
 2338 * @phba: Pointer to HBA context object.
 2339 * @hbqno: HBQ number.
 2340 *
 2341 * This function is called with hbalock held to get the next
 2342 * available slot for the given HBQ. If there is free slot
 2343 * available for the HBQ it will return pointer to the next available
 2344 * HBQ entry else it will return NULL.
 2345 **/
 2346static struct lpfc_hbq_entry *
 2347lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
 2348{
 2349	struct hbq_s *hbqp = &phba->hbqs[hbqno];
 2350
 2351	lockdep_assert_held(&phba->hbalock);
 2352
 2353	if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
 2354	    ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
 2355		hbqp->next_hbqPutIdx = 0;
 2356
 2357	if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
 2358		uint32_t raw_index = phba->hbq_get[hbqno];
 2359		uint32_t getidx = le32_to_cpu(raw_index);
 2360
 2361		hbqp->local_hbqGetIdx = getidx;
 2362
 2363		if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
 2364			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 2365					"1802 HBQ %d: local_hbqGetIdx "
 2366					"%u is > than hbqp->entry_count %u\n",
 2367					hbqno, hbqp->local_hbqGetIdx,
 2368					hbqp->entry_count);
 2369
 2370			phba->link_state = LPFC_HBA_ERROR;
 2371			return NULL;
 2372		}
 2373
 2374		if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
 2375			return NULL;
 2376	}
 2377
 2378	return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
 2379			hbqp->hbqPutIdx;
 2380}
 2381
 2382/**
 2383 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
 2384 * @phba: Pointer to HBA context object.
 2385 *
 2386 * This function is called with no lock held to free all the
 2387 * hbq buffers while uninitializing the SLI interface. It also
 2388 * frees the HBQ buffers returned by the firmware but not yet
 2389 * processed by the upper layers.
 2390 **/
 2391void
 2392lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
 2393{
 2394	struct lpfc_dmabuf *dmabuf, *next_dmabuf;
 2395	struct hbq_dmabuf *hbq_buf;
 2396	unsigned long flags;
 2397	int i, hbq_count;
 2398
 2399	hbq_count = lpfc_sli_hbq_count();
 2400	/* Return all memory used by all HBQs */
 2401	spin_lock_irqsave(&phba->hbalock, flags);
 2402	for (i = 0; i < hbq_count; ++i) {
 2403		list_for_each_entry_safe(dmabuf, next_dmabuf,
 2404				&phba->hbqs[i].hbq_buffer_list, list) {
 2405			hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
 2406			list_del(&hbq_buf->dbuf.list);
 2407			(phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
 2408		}
 2409		phba->hbqs[i].buffer_count = 0;
 2410	}
 2411
 2412	/* Mark the HBQs not in use */
 2413	phba->hbq_in_use = 0;
 2414	spin_unlock_irqrestore(&phba->hbalock, flags);
 2415}
 2416
 2417/**
 2418 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
 2419 * @phba: Pointer to HBA context object.
 2420 * @hbqno: HBQ number.
 2421 * @hbq_buf: Pointer to HBQ buffer.
 2422 *
 2423 * This function is called with the hbalock held to post a
 2424 * hbq buffer to the firmware. If the function finds an empty
 2425 * slot in the HBQ, it will post the buffer. The function will return
 2426 * pointer to the hbq entry if it successfully post the buffer
 2427 * else it will return NULL.
 2428 **/
 2429static int
 2430lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
 2431			 struct hbq_dmabuf *hbq_buf)
 2432{
 2433	lockdep_assert_held(&phba->hbalock);
 2434	return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
 2435}
 2436
 2437/**
 2438 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
 2439 * @phba: Pointer to HBA context object.
 2440 * @hbqno: HBQ number.
 2441 * @hbq_buf: Pointer to HBQ buffer.
 2442 *
 2443 * This function is called with the hbalock held to post a hbq buffer to the
 2444 * firmware. If the function finds an empty slot in the HBQ, it will post the
 2445 * buffer and place it on the hbq_buffer_list. The function will return zero if
 2446 * it successfully post the buffer else it will return an error.
 2447 **/
 2448static int
 2449lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
 2450			    struct hbq_dmabuf *hbq_buf)
 2451{
 2452	struct lpfc_hbq_entry *hbqe;
 2453	dma_addr_t physaddr = hbq_buf->dbuf.phys;
 2454
 2455	lockdep_assert_held(&phba->hbalock);
 2456	/* Get next HBQ entry slot to use */
 2457	hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
 2458	if (hbqe) {
 2459		struct hbq_s *hbqp = &phba->hbqs[hbqno];
 2460
 2461		hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
 2462		hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
 2463		hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
 2464		hbqe->bde.tus.f.bdeFlags = 0;
 2465		hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
 2466		hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
 2467				/* Sync SLIM */
 2468		hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
 2469		writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
 2470				/* flush */
 2471		readl(phba->hbq_put + hbqno);
 2472		list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
 2473		return 0;
 2474	} else
 2475		return -ENOMEM;
 2476}
 2477
 2478/**
 2479 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
 2480 * @phba: Pointer to HBA context object.
 2481 * @hbqno: HBQ number.
 2482 * @hbq_buf: Pointer to HBQ buffer.
 2483 *
 2484 * This function is called with the hbalock held to post an RQE to the SLI4
 2485 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
 2486 * the hbq_buffer_list and return zero, otherwise it will return an error.
 2487 **/
 2488static int
 2489lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
 2490			    struct hbq_dmabuf *hbq_buf)
 2491{
 2492	int rc;
 2493	struct lpfc_rqe hrqe;
 2494	struct lpfc_rqe drqe;
 2495	struct lpfc_queue *hrq;
 2496	struct lpfc_queue *drq;
 2497
 2498	if (hbqno != LPFC_ELS_HBQ)
 2499		return 1;
 2500	hrq = phba->sli4_hba.hdr_rq;
 2501	drq = phba->sli4_hba.dat_rq;
 2502
 2503	lockdep_assert_held(&phba->hbalock);
 2504	hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
 2505	hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
 2506	drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
 2507	drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
 2508	rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
 2509	if (rc < 0)
 2510		return rc;
 2511	hbq_buf->tag = (rc | (hbqno << 16));
 2512	list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
 2513	return 0;
 2514}
 2515
 2516/* HBQ for ELS and CT traffic. */
 2517static struct lpfc_hbq_init lpfc_els_hbq = {
 2518	.rn = 1,
 2519	.entry_count = 256,
 2520	.mask_count = 0,
 2521	.profile = 0,
 2522	.ring_mask = (1 << LPFC_ELS_RING),
 2523	.buffer_count = 0,
 2524	.init_count = 40,
 2525	.add_count = 40,
 2526};
 2527
 2528/* Array of HBQs */
 2529struct lpfc_hbq_init *lpfc_hbq_defs[] = {
 2530	&lpfc_els_hbq,
 2531};
 2532
 2533/**
 2534 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
 2535 * @phba: Pointer to HBA context object.
 2536 * @hbqno: HBQ number.
 2537 * @count: Number of HBQ buffers to be posted.
 2538 *
 2539 * This function is called with no lock held to post more hbq buffers to the
 2540 * given HBQ. The function returns the number of HBQ buffers successfully
 2541 * posted.
 2542 **/
 2543static int
 2544lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
 2545{
 2546	uint32_t i, posted = 0;
 2547	unsigned long flags;
 2548	struct hbq_dmabuf *hbq_buffer;
 2549	LIST_HEAD(hbq_buf_list);
 2550	if (!phba->hbqs[hbqno].hbq_alloc_buffer)
 2551		return 0;
 2552
 2553	if ((phba->hbqs[hbqno].buffer_count + count) >
 2554	    lpfc_hbq_defs[hbqno]->entry_count)
 2555		count = lpfc_hbq_defs[hbqno]->entry_count -
 2556					phba->hbqs[hbqno].buffer_count;
 2557	if (!count)
 2558		return 0;
 2559	/* Allocate HBQ entries */
 2560	for (i = 0; i < count; i++) {
 2561		hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
 2562		if (!hbq_buffer)
 2563			break;
 2564		list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
 2565	}
 2566	/* Check whether HBQ is still in use */
 2567	spin_lock_irqsave(&phba->hbalock, flags);
 2568	if (!phba->hbq_in_use)
 2569		goto err;
 2570	while (!list_empty(&hbq_buf_list)) {
 2571		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
 2572				 dbuf.list);
 2573		hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
 2574				      (hbqno << 16));
 2575		if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
 2576			phba->hbqs[hbqno].buffer_count++;
 2577			posted++;
 2578		} else
 2579			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
 2580	}
 2581	spin_unlock_irqrestore(&phba->hbalock, flags);
 2582	return posted;
 2583err:
 2584	spin_unlock_irqrestore(&phba->hbalock, flags);
 2585	while (!list_empty(&hbq_buf_list)) {
 2586		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
 2587				 dbuf.list);
 2588		(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
 2589	}
 2590	return 0;
 2591}
 2592
 2593/**
 2594 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
 2595 * @phba: Pointer to HBA context object.
 2596 * @qno: HBQ number.
 2597 *
 2598 * This function posts more buffers to the HBQ. This function
 2599 * is called with no lock held. The function returns the number of HBQ entries
 2600 * successfully allocated.
 2601 **/
 2602int
 2603lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
 2604{
 2605	if (phba->sli_rev == LPFC_SLI_REV4)
 2606		return 0;
 2607	else
 2608		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
 2609					 lpfc_hbq_defs[qno]->add_count);
 2610}
 2611
 2612/**
 2613 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
 2614 * @phba: Pointer to HBA context object.
 2615 * @qno:  HBQ queue number.
 2616 *
 2617 * This function is called from SLI initialization code path with
 2618 * no lock held to post initial HBQ buffers to firmware. The
 2619 * function returns the number of HBQ entries successfully allocated.
 2620 **/
 2621static int
 2622lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
 2623{
 2624	if (phba->sli_rev == LPFC_SLI_REV4)
 2625		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
 2626					lpfc_hbq_defs[qno]->entry_count);
 2627	else
 2628		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
 2629					 lpfc_hbq_defs[qno]->init_count);
 2630}
 2631
 2632/*
 2633 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
 2634 *
 2635 * This function removes the first hbq buffer on an hbq list and returns a
 2636 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
 2637 **/
 2638static struct hbq_dmabuf *
 2639lpfc_sli_hbqbuf_get(struct list_head *rb_list)
 2640{
 2641	struct lpfc_dmabuf *d_buf;
 2642
 2643	list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
 2644	if (!d_buf)
 2645		return NULL;
 2646	return container_of(d_buf, struct hbq_dmabuf, dbuf);
 2647}
 2648
 2649/**
 2650 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
 2651 * @phba: Pointer to HBA context object.
 2652 * @hrq: HBQ number.
 2653 *
 2654 * This function removes the first RQ buffer on an RQ buffer list and returns a
 2655 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
 2656 **/
 2657static struct rqb_dmabuf *
 2658lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
 2659{
 2660	struct lpfc_dmabuf *h_buf;
 2661	struct lpfc_rqb *rqbp;
 2662
 2663	rqbp = hrq->rqbp;
 2664	list_remove_head(&rqbp->rqb_buffer_list, h_buf,
 2665			 struct lpfc_dmabuf, list);
 2666	if (!h_buf)
 2667		return NULL;
 2668	rqbp->buffer_count--;
 2669	return container_of(h_buf, struct rqb_dmabuf, hbuf);
 2670}
 2671
 2672/**
 2673 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
 2674 * @phba: Pointer to HBA context object.
 2675 * @tag: Tag of the hbq buffer.
 2676 *
 2677 * This function searches for the hbq buffer associated with the given tag in
 2678 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
 2679 * otherwise it returns NULL.
 2680 **/
 2681static struct hbq_dmabuf *
 2682lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
 2683{
 2684	struct lpfc_dmabuf *d_buf;
 2685	struct hbq_dmabuf *hbq_buf;
 2686	uint32_t hbqno;
 2687
 2688	hbqno = tag >> 16;
 2689	if (hbqno >= LPFC_MAX_HBQS)
 2690		return NULL;
 2691
 2692	spin_lock_irq(&phba->hbalock);
 2693	list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
 2694		hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
 2695		if (hbq_buf->tag == tag) {
 2696			spin_unlock_irq(&phba->hbalock);
 2697			return hbq_buf;
 2698		}
 2699	}
 2700	spin_unlock_irq(&phba->hbalock);
 2701	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 2702			"1803 Bad hbq tag. Data: x%x x%x\n",
 2703			tag, phba->hbqs[tag >> 16].buffer_count);
 2704	return NULL;
 2705}
 2706
 2707/**
 2708 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
 2709 * @phba: Pointer to HBA context object.
 2710 * @hbq_buffer: Pointer to HBQ buffer.
 2711 *
 2712 * This function is called with hbalock. This function gives back
 2713 * the hbq buffer to firmware. If the HBQ does not have space to
 2714 * post the buffer, it will free the buffer.
 2715 **/
 2716void
 2717lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
 2718{
 2719	uint32_t hbqno;
 2720
 2721	if (hbq_buffer) {
 2722		hbqno = hbq_buffer->tag >> 16;
 2723		if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
 2724			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
 2725	}
 2726}
 2727
 2728/**
 2729 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
 2730 * @mbxCommand: mailbox command code.
 2731 *
 2732 * This function is called by the mailbox event handler function to verify
 2733 * that the completed mailbox command is a legitimate mailbox command. If the
 2734 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
 2735 * and the mailbox event handler will take the HBA offline.
 2736 **/
 2737static int
 2738lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
 2739{
 2740	uint8_t ret;
 2741
 2742	switch (mbxCommand) {
 2743	case MBX_LOAD_SM:
 2744	case MBX_READ_NV:
 2745	case MBX_WRITE_NV:
 2746	case MBX_WRITE_VPARMS:
 2747	case MBX_RUN_BIU_DIAG:
 2748	case MBX_INIT_LINK:
 2749	case MBX_DOWN_LINK:
 2750	case MBX_CONFIG_LINK:
 2751	case MBX_CONFIG_RING:
 2752	case MBX_RESET_RING:
 2753	case MBX_READ_CONFIG:
 2754	case MBX_READ_RCONFIG:
 2755	case MBX_READ_SPARM:
 2756	case MBX_READ_STATUS:
 2757	case MBX_READ_RPI:
 2758	case MBX_READ_XRI:
 2759	case MBX_READ_REV:
 2760	case MBX_READ_LNK_STAT:
 2761	case MBX_REG_LOGIN:
 2762	case MBX_UNREG_LOGIN:
 2763	case MBX_CLEAR_LA:
 2764	case MBX_DUMP_MEMORY:
 2765	case MBX_DUMP_CONTEXT:
 2766	case MBX_RUN_DIAGS:
 2767	case MBX_RESTART:
 2768	case MBX_UPDATE_CFG:
 2769	case MBX_DOWN_LOAD:
 2770	case MBX_DEL_LD_ENTRY:
 2771	case MBX_RUN_PROGRAM:
 2772	case MBX_SET_MASK:
 2773	case MBX_SET_VARIABLE:
 2774	case MBX_UNREG_D_ID:
 2775	case MBX_KILL_BOARD:
 2776	case MBX_CONFIG_FARP:
 2777	case MBX_BEACON:
 2778	case MBX_LOAD_AREA:
 2779	case MBX_RUN_BIU_DIAG64:
 2780	case MBX_CONFIG_PORT:
 2781	case MBX_READ_SPARM64:
 2782	case MBX_READ_RPI64:
 2783	case MBX_REG_LOGIN64:
 2784	case MBX_READ_TOPOLOGY:
 2785	case MBX_WRITE_WWN:
 2786	case MBX_SET_DEBUG:
 2787	case MBX_LOAD_EXP_ROM:
 2788	case MBX_ASYNCEVT_ENABLE:
 2789	case MBX_REG_VPI:
 2790	case MBX_UNREG_VPI:
 2791	case MBX_HEARTBEAT:
 2792	case MBX_PORT_CAPABILITIES:
 2793	case MBX_PORT_IOV_CONTROL:
 2794	case MBX_SLI4_CONFIG:
 2795	case MBX_SLI4_REQ_FTRS:
 2796	case MBX_REG_FCFI:
 2797	case MBX_UNREG_FCFI:
 2798	case MBX_REG_VFI:
 2799	case MBX_UNREG_VFI:
 2800	case MBX_INIT_VPI:
 2801	case MBX_INIT_VFI:
 2802	case MBX_RESUME_RPI:
 2803	case MBX_READ_EVENT_LOG_STATUS:
 2804	case MBX_READ_EVENT_LOG:
 2805	case MBX_SECURITY_MGMT:
 2806	case MBX_AUTH_PORT:
 2807	case MBX_ACCESS_VDATA:
 2808		ret = mbxCommand;
 2809		break;
 2810	default:
 2811		ret = MBX_SHUTDOWN;
 2812		break;
 2813	}
 2814	return ret;
 2815}
 2816
 2817/**
 2818 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
 2819 * @phba: Pointer to HBA context object.
 2820 * @pmboxq: Pointer to mailbox command.
 2821 *
 2822 * This is completion handler function for mailbox commands issued from
 2823 * lpfc_sli_issue_mbox_wait function. This function is called by the
 2824 * mailbox event handler function with no lock held. This function
 2825 * will wake up thread waiting on the wait queue pointed by context1
 2826 * of the mailbox.
 2827 **/
 2828void
 2829lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
 2830{
 2831	unsigned long drvr_flag;
 2832	struct completion *pmbox_done;
 2833
 2834	/*
 2835	 * If pmbox_done is empty, the driver thread gave up waiting and
 2836	 * continued running.
 2837	 */
 2838	pmboxq->mbox_flag |= LPFC_MBX_WAKE;
 2839	spin_lock_irqsave(&phba->hbalock, drvr_flag);
 2840	pmbox_done = pmboxq->ctx_u.mbox_wait;
 2841	if (pmbox_done)
 2842		complete(pmbox_done);
 2843	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 2844	return;
 2845}
 2846
 2847/**
 2848 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
 2849 * @phba: Pointer to HBA context object.
 2850 * @pmb: Pointer to mailbox object.
 2851 *
 2852 * This function is the default mailbox completion handler. It
 2853 * frees the memory resources associated with the completed mailbox
 2854 * command. If the completed command is a REG_LOGIN mailbox command,
 2855 * this function will issue a UREG_LOGIN to re-claim the RPI.
 2856 **/
 2857void
 2858lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 2859{
 2860	struct lpfc_vport  *vport = pmb->vport;
 2861	struct lpfc_dmabuf *mp;
 2862	struct lpfc_nodelist *ndlp;
 2863	struct Scsi_Host *shost;
 2864	uint16_t rpi, vpi;
 2865	int rc;
 2866
 2867	/*
 2868	 * If a REG_LOGIN succeeded  after node is destroyed or node
 2869	 * is in re-discovery driver need to cleanup the RPI.
 2870	 */
 2871	if (!test_bit(FC_UNLOADING, &phba->pport->load_flag) &&
 2872	    pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
 2873	    !pmb->u.mb.mbxStatus) {
 2874		mp = pmb->ctx_buf;
 2875		if (mp) {
 2876			pmb->ctx_buf = NULL;
 2877			lpfc_mbuf_free(phba, mp->virt, mp->phys);
 2878			kfree(mp);
 2879		}
 2880		rpi = pmb->u.mb.un.varWords[0];
 2881		vpi = pmb->u.mb.un.varRegLogin.vpi;
 2882		if (phba->sli_rev == LPFC_SLI_REV4)
 2883			vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
 2884		lpfc_unreg_login(phba, vpi, rpi, pmb);
 2885		pmb->vport = vport;
 2886		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
 2887		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
 2888		if (rc != MBX_NOT_FINISHED)
 2889			return;
 2890	}
 2891
 2892	if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
 2893		!test_bit(FC_UNLOADING, &phba->pport->load_flag) &&
 2894		!pmb->u.mb.mbxStatus) {
 2895		shost = lpfc_shost_from_vport(vport);
 2896		spin_lock_irq(shost->host_lock);
 2897		vport->vpi_state |= LPFC_VPI_REGISTERED;
 2898		spin_unlock_irq(shost->host_lock);
 2899		clear_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);
 2900	}
 2901
 2902	if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
 2903		ndlp = pmb->ctx_ndlp;
 2904		lpfc_nlp_put(ndlp);
 2905	}
 2906
 2907	if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
 2908		ndlp = pmb->ctx_ndlp;
 2909
 2910		/* Check to see if there are any deferred events to process */
 2911		if (ndlp) {
 2912			lpfc_printf_vlog(
 2913				vport,
 2914				KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
 2915				"1438 UNREG cmpl deferred mbox x%x "
 2916				"on NPort x%x Data: x%lx x%x x%px x%lx x%x\n",
 2917				ndlp->nlp_rpi, ndlp->nlp_DID,
 2918				ndlp->nlp_flag, ndlp->nlp_defer_did,
 2919				ndlp, vport->load_flag, kref_read(&ndlp->kref));
 2920
 2921			if (test_bit(NLP_UNREG_INP, &ndlp->nlp_flag) &&
 2922			    ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING) {
 2923				clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
 2924				ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
 2925				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
 2926			} else {
 2927				clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
 2928			}
 2929
 2930			/* The unreg_login mailbox is complete and had a
 2931			 * reference that has to be released.  The PLOGI
 2932			 * got its own ref.
 2933			 */
 2934			lpfc_nlp_put(ndlp);
 2935			pmb->ctx_ndlp = NULL;
 2936		}
 2937	}
 2938
 2939	/* This nlp_put pairs with lpfc_sli4_resume_rpi */
 2940	if (pmb->u.mb.mbxCommand == MBX_RESUME_RPI) {
 2941		ndlp = pmb->ctx_ndlp;
 2942		lpfc_nlp_put(ndlp);
 2943	}
 2944
 2945	/* Check security permission status on INIT_LINK mailbox command */
 2946	if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
 2947	    (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
 2948		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 2949				"2860 SLI authentication is required "
 2950				"for INIT_LINK but has not done yet\n");
 2951
 2952	if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
 2953		lpfc_sli4_mbox_cmd_free(phba, pmb);
 2954	else
 2955		lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
 2956}
 2957 /**
 2958 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
 2959 * @phba: Pointer to HBA context object.
 2960 * @pmb: Pointer to mailbox object.
 2961 *
 2962 * This function is the unreg rpi mailbox completion handler. It
 2963 * frees the memory resources associated with the completed mailbox
 2964 * command. An additional reference is put on the ndlp to prevent
 2965 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
 2966 * the unreg mailbox command completes, this routine puts the
 2967 * reference back.
 2968 *
 2969 **/
 2970void
 2971lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 2972{
 2973	struct lpfc_vport  *vport = pmb->vport;
 2974	struct lpfc_nodelist *ndlp;
 2975	bool unreg_inp;
 2976
 2977	ndlp = pmb->ctx_ndlp;
 2978	if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
 2979		if (phba->sli_rev == LPFC_SLI_REV4 &&
 2980		    (bf_get(lpfc_sli_intf_if_type,
 2981		     &phba->sli4_hba.sli_intf) >=
 2982		     LPFC_SLI_INTF_IF_TYPE_2)) {
 2983			if (ndlp) {
 2984				lpfc_printf_vlog(
 2985					 vport, KERN_INFO,
 2986					 LOG_MBOX | LOG_SLI | LOG_NODE,
 2987					 "0010 UNREG_LOGIN vpi:x%x "
 2988					 "rpi:%x DID:%x defer x%x flg x%lx "
 2989					 "x%px\n",
 2990					 vport->vpi, ndlp->nlp_rpi,
 2991					 ndlp->nlp_DID, ndlp->nlp_defer_did,
 2992					 ndlp->nlp_flag,
 2993					 ndlp);
 2994
 2995				/* Cleanup the nlp_flag now that the UNREG RPI
 2996				 * has completed.
 2997				 */
 2998				unreg_inp = test_and_clear_bit(NLP_UNREG_INP,
 2999							       &ndlp->nlp_flag);
 3000				clear_bit(NLP_LOGO_ACC, &ndlp->nlp_flag);
 3001
 3002				/* Check to see if there are any deferred
 3003				 * events to process
 3004				 */
 3005				if (unreg_inp &&
 3006				    ndlp->nlp_defer_did !=
 3007				    NLP_EVT_NOTHING_PENDING) {
 3008					lpfc_printf_vlog(
 3009						vport, KERN_INFO,
 3010						LOG_MBOX | LOG_SLI | LOG_NODE,
 3011						"4111 UNREG cmpl deferred "
 3012						"clr x%x on "
 3013						"NPort x%x Data: x%x x%px\n",
 3014						ndlp->nlp_rpi, ndlp->nlp_DID,
 3015						ndlp->nlp_defer_did, ndlp);
 3016					ndlp->nlp_defer_did =
 3017						NLP_EVT_NOTHING_PENDING;
 3018					lpfc_issue_els_plogi(
 3019						vport, ndlp->nlp_DID, 0);
 3020				}
 3021
 3022				lpfc_nlp_put(ndlp);
 3023			}
 3024		}
 3025	}
 3026
 3027	mempool_free(pmb, phba->mbox_mem_pool);
 3028}
 3029
 3030/**
 3031 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
 3032 * @phba: Pointer to HBA context object.
 3033 *
 3034 * This function is called with no lock held. This function processes all
 3035 * the completed mailbox commands and gives it to upper layers. The interrupt
 3036 * service routine processes mailbox completion interrupt and adds completed
 3037 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
 3038 * Worker thread call lpfc_sli_handle_mb_event, which will return the
 3039 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
 3040 * function returns the mailbox commands to the upper layer by calling the
 3041 * completion handler function of each mailbox.
 3042 **/
 3043int
 3044lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
 3045{
 3046	MAILBOX_t *pmbox;
 3047	LPFC_MBOXQ_t *pmb;
 3048	int rc;
 3049	LIST_HEAD(cmplq);
 3050
 3051	phba->sli.slistat.mbox_event++;
 3052
 3053	/* Get all completed mailboxe buffers into the cmplq */
 3054	spin_lock_irq(&phba->hbalock);
 3055	list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
 3056	spin_unlock_irq(&phba->hbalock);
 3057
 3058	/* Get a Mailbox buffer to setup mailbox commands for callback */
 3059	do {
 3060		list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
 3061		if (pmb == NULL)
 3062			break;
 3063
 3064		pmbox = &pmb->u.mb;
 3065
 3066		if (pmbox->mbxCommand != MBX_HEARTBEAT) {
 3067			if (pmb->vport) {
 3068				lpfc_debugfs_disc_trc(pmb->vport,
 3069					LPFC_DISC_TRC_MBOX_VPORT,
 3070					"MBOX cmpl vport: cmd:x%x mb:x%x x%x",
 3071					(uint32_t)pmbox->mbxCommand,
 3072					pmbox->un.varWords[0],
 3073					pmbox->un.varWords[1]);
 3074			}
 3075			else {
 3076				lpfc_debugfs_disc_trc(phba->pport,
 3077					LPFC_DISC_TRC_MBOX,
 3078					"MBOX cmpl:       cmd:x%x mb:x%x x%x",
 3079					(uint32_t)pmbox->mbxCommand,
 3080					pmbox->un.varWords[0],
 3081					pmbox->un.varWords[1]);
 3082			}
 3083		}
 3084
 3085		/*
 3086		 * It is a fatal error if unknown mbox command completion.
 3087		 */
 3088		if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
 3089		    MBX_SHUTDOWN) {
 3090			/* Unknown mailbox command compl */
 3091			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 3092					"(%d):0323 Unknown Mailbox command "
 3093					"x%x (x%x/x%x) Cmpl\n",
 3094					pmb->vport ? pmb->vport->vpi :
 3095					LPFC_VPORT_UNKNOWN,
 3096					pmbox->mbxCommand,
 3097					lpfc_sli_config_mbox_subsys_get(phba,
 3098									pmb),
 3099					lpfc_sli_config_mbox_opcode_get(phba,
 3100									pmb));
 3101			phba->link_state = LPFC_HBA_ERROR;
 3102			phba->work_hs = HS_FFER3;
 3103			lpfc_handle_eratt(phba);
 3104			continue;
 3105		}
 3106
 3107		if (pmbox->mbxStatus) {
 3108			phba->sli.slistat.mbox_stat_err++;
 3109			if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
 3110				/* Mbox cmd cmpl error - RETRYing */
 3111				lpfc_printf_log(phba, KERN_INFO,
 3112					LOG_MBOX | LOG_SLI,
 3113					"(%d):0305 Mbox cmd cmpl "
 3114					"error - RETRYing Data: x%x "
 3115					"(x%x/x%x) x%x x%x x%x\n",
 3116					pmb->vport ? pmb->vport->vpi :
 3117					LPFC_VPORT_UNKNOWN,
 3118					pmbox->mbxCommand,
 3119					lpfc_sli_config_mbox_subsys_get(phba,
 3120									pmb),
 3121					lpfc_sli_config_mbox_opcode_get(phba,
 3122									pmb),
 3123					pmbox->mbxStatus,
 3124					pmbox->un.varWords[0],
 3125					pmb->vport ? pmb->vport->port_state :
 3126					LPFC_VPORT_UNKNOWN);
 3127				pmbox->mbxStatus = 0;
 3128				pmbox->mbxOwner = OWN_HOST;
 3129				rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
 3130				if (rc != MBX_NOT_FINISHED)
 3131					continue;
 3132			}
 3133		}
 3134
 3135		/* Mailbox cmd <cmd> Cmpl <cmpl> */
 3136		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
 3137				"(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
 3138				"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
 3139				"x%x x%x x%x\n",
 3140				pmb->vport ? pmb->vport->vpi : 0,
 3141				pmbox->mbxCommand,
 3142				lpfc_sli_config_mbox_subsys_get(phba, pmb),
 3143				lpfc_sli_config_mbox_opcode_get(phba, pmb),
 3144				pmb->mbox_cmpl,
 3145				*((uint32_t *) pmbox),
 3146				pmbox->un.varWords[0],
 3147				pmbox->un.varWords[1],
 3148				pmbox->un.varWords[2],
 3149				pmbox->un.varWords[3],
 3150				pmbox->un.varWords[4],
 3151				pmbox->un.varWords[5],
 3152				pmbox->un.varWords[6],
 3153				pmbox->un.varWords[7],
 3154				pmbox->un.varWords[8],
 3155				pmbox->un.varWords[9],
 3156				pmbox->un.varWords[10]);
 3157
 3158		if (pmb->mbox_cmpl)
 3159			pmb->mbox_cmpl(phba,pmb);
 3160	} while (1);
 3161	return 0;
 3162}
 3163
 3164/**
 3165 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
 3166 * @phba: Pointer to HBA context object.
 3167 * @pring: Pointer to driver SLI ring object.
 3168 * @tag: buffer tag.
 3169 *
 3170 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
 3171 * is set in the tag the buffer is posted for a particular exchange,
 3172 * the function will return the buffer without replacing the buffer.
 3173 * If the buffer is for unsolicited ELS or CT traffic, this function
 3174 * returns the buffer and also posts another buffer to the firmware.
 3175 **/
 3176static struct lpfc_dmabuf *
 3177lpfc_sli_get_buff(struct lpfc_hba *phba,
 3178		  struct lpfc_sli_ring *pring,
 3179		  uint32_t tag)
 3180{
 3181	struct hbq_dmabuf *hbq_entry;
 3182
 3183	if (tag & QUE_BUFTAG_BIT)
 3184		return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
 3185	hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
 3186	if (!hbq_entry)
 3187		return NULL;
 3188	return &hbq_entry->dbuf;
 3189}
 3190
 3191/**
 3192 * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer
 3193 *                              containing a NVME LS request.
 3194 * @phba: pointer to lpfc hba data structure.
 3195 * @piocb: pointer to the iocbq struct representing the sequence starting
 3196 *        frame.
 3197 *
 3198 * This routine initially validates the NVME LS, validates there is a login
 3199 * with the port that sent the LS, and then calls the appropriate nvme host
 3200 * or target LS request handler.
 3201 **/
 3202static void
 3203lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
 3204{
 3205	struct lpfc_nodelist *ndlp;
 3206	struct lpfc_dmabuf *d_buf;
 3207	struct hbq_dmabuf *nvmebuf;
 3208	struct fc_frame_header *fc_hdr;
 3209	struct lpfc_async_xchg_ctx *axchg = NULL;
 3210	char *failwhy = NULL;
 3211	uint32_t oxid, sid, did, fctl, size;
 3212	int ret = 1;
 3213
 3214	d_buf = piocb->cmd_dmabuf;
 3215
 3216	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
 3217	fc_hdr = nvmebuf->hbuf.virt;
 3218	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
 3219	sid = sli4_sid_from_fc_hdr(fc_hdr);
 3220	did = sli4_did_from_fc_hdr(fc_hdr);
 3221	fctl = (fc_hdr->fh_f_ctl[0] << 16 |
 3222		fc_hdr->fh_f_ctl[1] << 8 |
 3223		fc_hdr->fh_f_ctl[2]);
 3224	size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
 3225
 3226	lpfc_nvmeio_data(phba, "NVME LS    RCV: xri x%x sz %d from %06x\n",
 3227			 oxid, size, sid);
 3228
 3229	if (test_bit(FC_UNLOADING, &phba->pport->load_flag)) {
 3230		failwhy = "Driver Unloading";
 3231	} else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
 3232		failwhy = "NVME FC4 Disabled";
 3233	} else if (!phba->nvmet_support && !phba->pport->localport) {
 3234		failwhy = "No Localport";
 3235	} else if (phba->nvmet_support && !phba->targetport) {
 3236		failwhy = "No Targetport";
 3237	} else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {
 3238		failwhy = "Bad NVME LS R_CTL";
 3239	} else if (unlikely((fctl & 0x00FF0000) !=
 3240			(FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {
 3241		failwhy = "Bad NVME LS F_CTL";
 3242	} else {
 3243		axchg = kzalloc(sizeof(*axchg), GFP_ATOMIC);
 3244		if (!axchg)
 3245			failwhy = "No CTX memory";
 3246	}
 3247
 3248	if (unlikely(failwhy)) {
 3249		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 3250				"6154 Drop NVME LS: SID %06X OXID x%X: %s\n",
 3251				sid, oxid, failwhy);
 3252		goto out_fail;
 3253	}
 3254
 3255	/* validate the source of the LS is logged in */
 3256	ndlp = lpfc_findnode_did(phba->pport, sid);
 3257	if (!ndlp ||
 3258	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
 3259	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
 3260		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
 3261				"6216 NVME Unsol rcv: No ndlp: "
 3262				"NPort_ID x%x oxid x%x\n",
 3263				sid, oxid);
 3264		goto out_fail;
 3265	}
 3266
 3267	axchg->phba = phba;
 3268	axchg->ndlp = ndlp;
 3269	axchg->size = size;
 3270	axchg->oxid = oxid;
 3271	axchg->sid = sid;
 3272	axchg->wqeq = NULL;
 3273	axchg->state = LPFC_NVME_STE_LS_RCV;
 3274	axchg->entry_cnt = 1;
 3275	axchg->rqb_buffer = (void *)nvmebuf;
 3276	axchg->hdwq = &phba->sli4_hba.hdwq[0];
 3277	axchg->payload = nvmebuf->dbuf.virt;
 3278	INIT_LIST_HEAD(&axchg->list);
 3279
 3280	if (phba->nvmet_support) {
 3281		ret = lpfc_nvmet_handle_lsreq(phba, axchg);
 3282		spin_lock_irq(&ndlp->lock);
 3283		if (!ret && !(ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH)) {
 3284			ndlp->fc4_xpt_flags |= NLP_XPT_HAS_HH;
 3285			spin_unlock_irq(&ndlp->lock);
 3286
 3287			/* This reference is a single occurrence to hold the
 3288			 * node valid until the nvmet transport calls
 3289			 * host_release.
 3290			 */
 3291			if (!lpfc_nlp_get(ndlp))
 3292				goto out_fail;
 3293
 3294			lpfc_printf_log(phba, KERN_ERR, LOG_NODE,
 3295					"6206 NVMET unsol ls_req ndlp x%px "
 3296					"DID x%x xflags x%x refcnt %d\n",
 3297					ndlp, ndlp->nlp_DID,
 3298					ndlp->fc4_xpt_flags,
 3299					kref_read(&ndlp->kref));
 3300		} else {
 3301			spin_unlock_irq(&ndlp->lock);
 3302		}
 3303	} else {
 3304		ret = lpfc_nvme_handle_lsreq(phba, axchg);
 3305	}
 3306
 3307	/* if zero, LS was successfully handled. If non-zero, LS not handled */
 3308	if (!ret)
 3309		return;
 3310
 3311out_fail:
 3312	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 3313			"6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "
 3314			"NVMe%s handler failed %d\n",
 3315			did, sid, oxid,
 3316			(phba->nvmet_support) ? "T" : "I", ret);
 3317
 3318	/* recycle receive buffer */
 3319	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
 3320
 3321	/* If start of new exchange, abort it */
 3322	if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))
 3323		ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);
 3324
 3325	if (ret)
 3326		kfree(axchg);
 3327}
 3328
 3329/**
 3330 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
 3331 * @phba: Pointer to HBA context object.
 3332 * @pring: Pointer to driver SLI ring object.
 3333 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
 3334 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
 3335 * @fch_type: the type for the first frame of the sequence.
 3336 *
 3337 * This function is called with no lock held. This function uses the r_ctl and
 3338 * type of the received sequence to find the correct callback function to call
 3339 * to process the sequence.
 3340 **/
 3341static int
 3342lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 3343			 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
 3344			 uint32_t fch_type)
 3345{
 3346	int i;
 3347
 3348	switch (fch_type) {
 3349	case FC_TYPE_NVME:
 3350		lpfc_nvme_unsol_ls_handler(phba, saveq);
 3351		return 1;
 3352	default:
 3353		break;
 3354	}
 3355
 3356	/* unSolicited Responses */
 3357	if (pring->prt[0].profile) {
 3358		if (pring->prt[0].lpfc_sli_rcv_unsol_event)
 3359			(pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
 3360									saveq);
 3361		return 1;
 3362	}
 3363	/* We must search, based on rctl / type
 3364	   for the right routine */
 3365	for (i = 0; i < pring->num_mask; i++) {
 3366		if ((pring->prt[i].rctl == fch_r_ctl) &&
 3367		    (pring->prt[i].type == fch_type)) {
 3368			if (pring->prt[i].lpfc_sli_rcv_unsol_event)
 3369				(pring->prt[i].lpfc_sli_rcv_unsol_event)
 3370						(phba, pring, saveq);
 3371			return 1;
 3372		}
 3373	}
 3374	return 0;
 3375}
 3376
 3377static void
 3378lpfc_sli_prep_unsol_wqe(struct lpfc_hba *phba,
 3379			struct lpfc_iocbq *saveq)
 3380{
 3381	IOCB_t *irsp;
 3382	union lpfc_wqe128 *wqe;
 3383	u16 i = 0;
 3384
 3385	irsp = &saveq->iocb;
 3386	wqe = &saveq->wqe;
 3387
 3388	/* Fill wcqe with the IOCB status fields */
 3389	bf_set(lpfc_wcqe_c_status, &saveq->wcqe_cmpl, irsp->ulpStatus);
 3390	saveq->wcqe_cmpl.word3 = irsp->ulpBdeCount;
 3391	saveq->wcqe_cmpl.parameter = irsp->un.ulpWord[4];
 3392	saveq->wcqe_cmpl.total_data_placed = irsp->unsli3.rcvsli3.acc_len;
 3393
 3394	/* Source ID */
 3395	bf_set(els_rsp64_sid, &wqe->xmit_els_rsp, irsp->un.rcvels.parmRo);
 3396
 3397	/* rx-id of the response frame */
 3398	bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com, irsp->ulpContext);
 3399
 3400	/* ox-id of the frame */
 3401	bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
 3402	       irsp->unsli3.rcvsli3.ox_id);
 3403
 3404	/* DID */
 3405	bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
 3406	       irsp->un.rcvels.remoteID);
 3407
 3408	/* unsol data len */
 3409	for (i = 0; i < irsp->ulpBdeCount; i++) {
 3410		struct lpfc_hbq_entry *hbqe = NULL;
 3411
 3412		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
 3413			if (i == 0) {
 3414				hbqe = (struct lpfc_hbq_entry *)
 3415					&irsp->un.ulpWord[0];
 3416				saveq->wqe.gen_req.bde.tus.f.bdeSize =
 3417					hbqe->bde.tus.f.bdeSize;
 3418			} else if (i == 1) {
 3419				hbqe = (struct lpfc_hbq_entry *)
 3420					&irsp->unsli3.sli3Words[4];
 3421				saveq->unsol_rcv_len = hbqe->bde.tus.f.bdeSize;
 3422			}
 3423		}
 3424	}
 3425}
 3426
 3427/**
 3428 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
 3429 * @phba: Pointer to HBA context object.
 3430 * @pring: Pointer to driver SLI ring object.
 3431 * @saveq: Pointer to the unsolicited iocb.
 3432 *
 3433 * This function is called with no lock held by the ring event handler
 3434 * when there is an unsolicited iocb posted to the response ring by the
 3435 * firmware. This function gets the buffer associated with the iocbs
 3436 * and calls the event handler for the ring. This function handles both
 3437 * qring buffers and hbq buffers.
 3438 * When the function returns 1 the caller can free the iocb object otherwise
 3439 * upper layer functions will free the iocb objects.
 3440 **/
 3441static int
 3442lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 3443			    struct lpfc_iocbq *saveq)
 3444{
 3445	IOCB_t           * irsp;
 3446	WORD5            * w5p;
 3447	dma_addr_t	 paddr;
 3448	uint32_t           Rctl, Type;
 3449	struct lpfc_iocbq *iocbq;
 3450	struct lpfc_dmabuf *dmzbuf;
 3451
 3452	irsp = &saveq->iocb;
 3453	saveq->vport = phba->pport;
 3454
 3455	if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
 3456		if (pring->lpfc_sli_rcv_async_status)
 3457			pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
 3458		else
 3459			lpfc_printf_log(phba,
 3460					KERN_WARNING,
 3461					LOG_SLI,
 3462					"0316 Ring %d handler: unexpected "
 3463					"ASYNC_STATUS iocb received evt_code "
 3464					"0x%x\n",
 3465					pring->ringno,
 3466					irsp->un.asyncstat.evt_code);
 3467		return 1;
 3468	}
 3469
 3470	if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
 3471	    (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
 3472		if (irsp->ulpBdeCount > 0) {
 3473			dmzbuf = lpfc_sli_get_buff(phba, pring,
 3474						   irsp->un.ulpWord[3]);
 3475			lpfc_in_buf_free(phba, dmzbuf);
 3476		}
 3477
 3478		if (irsp->ulpBdeCount > 1) {
 3479			dmzbuf = lpfc_sli_get_buff(phba, pring,
 3480						   irsp->unsli3.sli3Words[3]);
 3481			lpfc_in_buf_free(phba, dmzbuf);
 3482		}
 3483
 3484		if (irsp->ulpBdeCount > 2) {
 3485			dmzbuf = lpfc_sli_get_buff(phba, pring,
 3486						   irsp->unsli3.sli3Words[7]);
 3487			lpfc_in_buf_free(phba, dmzbuf);
 3488		}
 3489
 3490		return 1;
 3491	}
 3492
 3493	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
 3494		if (irsp->ulpBdeCount != 0) {
 3495			saveq->cmd_dmabuf = lpfc_sli_get_buff(phba, pring,
 3496						irsp->un.ulpWord[3]);
 3497			if (!saveq->cmd_dmabuf)
 3498				lpfc_printf_log(phba,
 3499					KERN_ERR,
 3500					LOG_SLI,
 3501					"0341 Ring %d Cannot find buffer for "
 3502					"an unsolicited iocb. tag 0x%x\n",
 3503					pring->ringno,
 3504					irsp->un.ulpWord[3]);
 3505		}
 3506		if (irsp->ulpBdeCount == 2) {
 3507			saveq->bpl_dmabuf = lpfc_sli_get_buff(phba, pring,
 3508						irsp->unsli3.sli3Words[7]);
 3509			if (!saveq->bpl_dmabuf)
 3510				lpfc_printf_log(phba,
 3511					KERN_ERR,
 3512					LOG_SLI,
 3513					"0342 Ring %d Cannot find buffer for an"
 3514					" unsolicited iocb. tag 0x%x\n",
 3515					pring->ringno,
 3516					irsp->unsli3.sli3Words[7]);
 3517		}
 3518		list_for_each_entry(iocbq, &saveq->list, list) {
 3519			irsp = &iocbq->iocb;
 3520			if (irsp->ulpBdeCount != 0) {
 3521				iocbq->cmd_dmabuf = lpfc_sli_get_buff(phba,
 3522							pring,
 3523							irsp->un.ulpWord[3]);
 3524				if (!iocbq->cmd_dmabuf)
 3525					lpfc_printf_log(phba,
 3526						KERN_ERR,
 3527						LOG_SLI,
 3528						"0343 Ring %d Cannot find "
 3529						"buffer for an unsolicited iocb"
 3530						". tag 0x%x\n", pring->ringno,
 3531						irsp->un.ulpWord[3]);
 3532			}
 3533			if (irsp->ulpBdeCount == 2) {
 3534				iocbq->bpl_dmabuf = lpfc_sli_get_buff(phba,
 3535						pring,
 3536						irsp->unsli3.sli3Words[7]);
 3537				if (!iocbq->bpl_dmabuf)
 3538					lpfc_printf_log(phba,
 3539						KERN_ERR,
 3540						LOG_SLI,
 3541						"0344 Ring %d Cannot find "
 3542						"buffer for an unsolicited "
 3543						"iocb. tag 0x%x\n",
 3544						pring->ringno,
 3545						irsp->unsli3.sli3Words[7]);
 3546			}
 3547		}
 3548	} else {
 3549		paddr = getPaddr(irsp->un.cont64[0].addrHigh,
 3550				 irsp->un.cont64[0].addrLow);
 3551		saveq->cmd_dmabuf = lpfc_sli_ringpostbuf_get(phba, pring,
 3552							     paddr);
 3553		if (irsp->ulpBdeCount == 2) {
 3554			paddr = getPaddr(irsp->un.cont64[1].addrHigh,
 3555					 irsp->un.cont64[1].addrLow);
 3556			saveq->bpl_dmabuf = lpfc_sli_ringpostbuf_get(phba,
 3557								   pring,
 3558								   paddr);
 3559		}
 3560	}
 3561
 3562	if (irsp->ulpBdeCount != 0 &&
 3563	    (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
 3564	     irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
 3565		int found = 0;
 3566
 3567		/* search continue save q for same XRI */
 3568		list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
 3569			if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
 3570				saveq->iocb.unsli3.rcvsli3.ox_id) {
 3571				list_add_tail(&saveq->list, &iocbq->list);
 3572				found = 1;
 3573				break;
 3574			}
 3575		}
 3576		if (!found)
 3577			list_add_tail(&saveq->clist,
 3578				      &pring->iocb_continue_saveq);
 3579
 3580		if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
 3581			list_del_init(&iocbq->clist);
 3582			saveq = iocbq;
 3583			irsp = &saveq->iocb;
 3584		} else {
 3585			return 0;
 3586		}
 3587	}
 3588	if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
 3589	    (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
 3590	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
 3591		Rctl = FC_RCTL_ELS_REQ;
 3592		Type = FC_TYPE_ELS;
 3593	} else {
 3594		w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
 3595		Rctl = w5p->hcsw.Rctl;
 3596		Type = w5p->hcsw.Type;
 3597
 3598		/* Firmware Workaround */
 3599		if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
 3600			(irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
 3601			 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
 3602			Rctl = FC_RCTL_ELS_REQ;
 3603			Type = FC_TYPE_ELS;
 3604			w5p->hcsw.Rctl = Rctl;
 3605			w5p->hcsw.Type = Type;
 3606		}
 3607	}
 3608
 3609	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
 3610	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
 3611	    irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
 3612		if (irsp->unsli3.rcvsli3.vpi == 0xffff)
 3613			saveq->vport = phba->pport;
 3614		else
 3615			saveq->vport = lpfc_find_vport_by_vpid(phba,
 3616					       irsp->unsli3.rcvsli3.vpi);
 3617	}
 3618
 3619	/* Prepare WQE with Unsol frame */
 3620	lpfc_sli_prep_unsol_wqe(phba, saveq);
 3621
 3622	if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
 3623		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
 3624				"0313 Ring %d handler: unexpected Rctl x%x "
 3625				"Type x%x received\n",
 3626				pring->ringno, Rctl, Type);
 3627
 3628	return 1;
 3629}
 3630
 3631/**
 3632 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
 3633 * @phba: Pointer to HBA context object.
 3634 * @pring: Pointer to driver SLI ring object.
 3635 * @prspiocb: Pointer to response iocb object.
 3636 *
 3637 * This function looks up the iocb_lookup table to get the command iocb
 3638 * corresponding to the given response iocb using the iotag of the
 3639 * response iocb. The driver calls this function with the hbalock held
 3640 * for SLI3 ports or the ring lock held for SLI4 ports.
 3641 * This function returns the command iocb object if it finds the command
 3642 * iocb else returns NULL.
 3643 **/
 3644static struct lpfc_iocbq *
 3645lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
 3646		      struct lpfc_sli_ring *pring,
 3647		      struct lpfc_iocbq *prspiocb)
 3648{
 3649	struct lpfc_iocbq *cmd_iocb = NULL;
 3650	u16 iotag;
 3651
 3652	if (phba->sli_rev == LPFC_SLI_REV4)
 3653		iotag = get_wqe_reqtag(prspiocb);
 3654	else
 3655		iotag = prspiocb->iocb.ulpIoTag;
 3656
 3657	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
 3658		cmd_iocb = phba->sli.iocbq_lookup[iotag];
 3659		if (cmd_iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ) {
 3660			/* remove from txcmpl queue list */
 3661			list_del_init(&cmd_iocb->list);
 3662			cmd_iocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
 3663			pring->txcmplq_cnt--;
 3664			return cmd_iocb;
 3665		}
 3666	}
 3667
 3668	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 3669			"0317 iotag x%x is out of "
 3670			"range: max iotag x%x\n",
 3671			iotag, phba->sli.last_iotag);
 3672	return NULL;
 3673}
 3674
 3675/**
 3676 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
 3677 * @phba: Pointer to HBA context object.
 3678 * @pring: Pointer to driver SLI ring object.
 3679 * @iotag: IOCB tag.
 3680 *
 3681 * This function looks up the iocb_lookup table to get the command iocb
 3682 * corresponding to the given iotag. The driver calls this function with
 3683 * the ring lock held because this function is an SLI4 port only helper.
 3684 * This function returns the command iocb object if it finds the command
 3685 * iocb else returns NULL.
 3686 **/
 3687static struct lpfc_iocbq *
 3688lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
 3689			     struct lpfc_sli_ring *pring, uint16_t iotag)
 3690{
 3691	struct lpfc_iocbq *cmd_iocb = NULL;
 3692
 3693	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
 3694		cmd_iocb = phba->sli.iocbq_lookup[iotag];
 3695		if (cmd_iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ) {
 3696			/* remove from txcmpl queue list */
 3697			list_del_init(&cmd_iocb->list);
 3698			cmd_iocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
 3699			pring->txcmplq_cnt--;
 3700			return cmd_iocb;
 3701		}
 3702	}
 3703
 3704	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 3705			"0372 iotag x%x lookup error: max iotag (x%x) "
 3706			"cmd_flag x%x\n",
 3707			iotag, phba->sli.last_iotag,
 3708			cmd_iocb ? cmd_iocb->cmd_flag : 0xffff);
 3709	return NULL;
 3710}
 3711
 3712/**
 3713 * lpfc_sli_process_sol_iocb - process solicited iocb completion
 3714 * @phba: Pointer to HBA context object.
 3715 * @pring: Pointer to driver SLI ring object.
 3716 * @saveq: Pointer to the response iocb to be processed.
 3717 *
 3718 * This function is called by the ring event handler for non-fcp
 3719 * rings when there is a new response iocb in the response ring.
 3720 * The caller is not required to hold any locks. This function
 3721 * gets the command iocb associated with the response iocb and
 3722 * calls the completion handler for the command iocb. If there
 3723 * is no completion handler, the function will free the resources
 3724 * associated with command iocb. If the response iocb is for
 3725 * an already aborted command iocb, the status of the completion
 3726 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
 3727 * This function always returns 1.
 3728 **/
 3729static int
 3730lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 3731			  struct lpfc_iocbq *saveq)
 3732{
 3733	struct lpfc_iocbq *cmdiocbp;
 3734	unsigned long iflag;
 3735	u32 ulp_command, ulp_status, ulp_word4, ulp_context, iotag;
 3736
 3737	if (phba->sli_rev == LPFC_SLI_REV4)
 3738		spin_lock_irqsave(&pring->ring_lock, iflag);
 3739	else
 3740		spin_lock_irqsave(&phba->hbalock, iflag);
 3741	cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
 3742	if (phba->sli_rev == LPFC_SLI_REV4)
 3743		spin_unlock_irqrestore(&pring->ring_lock, iflag);
 3744	else
 3745		spin_unlock_irqrestore(&phba->hbalock, iflag);
 3746
 3747	ulp_command = get_job_cmnd(phba, saveq);
 3748	ulp_status = get_job_ulpstatus(phba, saveq);
 3749	ulp_word4 = get_job_word4(phba, saveq);
 3750	ulp_context = get_job_ulpcontext(phba, saveq);
 3751	if (phba->sli_rev == LPFC_SLI_REV4)
 3752		iotag = get_wqe_reqtag(saveq);
 3753	else
 3754		iotag = saveq->iocb.ulpIoTag;
 3755
 3756	if (cmdiocbp) {
 3757		ulp_command = get_job_cmnd(phba, cmdiocbp);
 3758		if (cmdiocbp->cmd_cmpl) {
 3759			/*
 3760			 * If an ELS command failed send an event to mgmt
 3761			 * application.
 3762			 */
 3763			if (ulp_status &&
 3764			     (pring->ringno == LPFC_ELS_RING) &&
 3765			     (ulp_command == CMD_ELS_REQUEST64_CR))
 3766				lpfc_send_els_failure_event(phba,
 3767					cmdiocbp, saveq);
 3768
 3769			/*
 3770			 * Post all ELS completions to the worker thread.
 3771			 * All other are passed to the completion callback.
 3772			 */
 3773			if (pring->ringno == LPFC_ELS_RING) {
 3774				if ((phba->sli_rev < LPFC_SLI_REV4) &&
 3775				    (cmdiocbp->cmd_flag &
 3776							LPFC_DRIVER_ABORTED)) {
 3777					spin_lock_irqsave(&phba->hbalock,
 3778							  iflag);
 3779					cmdiocbp->cmd_flag &=
 3780						~LPFC_DRIVER_ABORTED;
 3781					spin_unlock_irqrestore(&phba->hbalock,
 3782							       iflag);
 3783					saveq->iocb.ulpStatus =
 3784						IOSTAT_LOCAL_REJECT;
 3785					saveq->iocb.un.ulpWord[4] =
 3786						IOERR_SLI_ABORTED;
 3787
 3788					/* Firmware could still be in progress
 3789					 * of DMAing payload, so don't free data
 3790					 * buffer till after a hbeat.
 3791					 */
 3792					spin_lock_irqsave(&phba->hbalock,
 3793							  iflag);
 3794					saveq->cmd_flag |= LPFC_DELAY_MEM_FREE;
 3795					spin_unlock_irqrestore(&phba->hbalock,
 3796							       iflag);
 3797				}
 3798				if (phba->sli_rev == LPFC_SLI_REV4) {
 3799					if (saveq->cmd_flag &
 3800					    LPFC_EXCHANGE_BUSY) {
 3801						/* Set cmdiocb flag for the
 3802						 * exchange busy so sgl (xri)
 3803						 * will not be released until
 3804						 * the abort xri is received
 3805						 * from hba.
 3806						 */
 3807						spin_lock_irqsave(
 3808							&phba->hbalock, iflag);
 3809						cmdiocbp->cmd_flag |=
 3810							LPFC_EXCHANGE_BUSY;
 3811						spin_unlock_irqrestore(
 3812							&phba->hbalock, iflag);
 3813					}
 3814					if (cmdiocbp->cmd_flag &
 3815					    LPFC_DRIVER_ABORTED) {
 3816						/*
 3817						 * Clear LPFC_DRIVER_ABORTED
 3818						 * bit in case it was driver
 3819						 * initiated abort.
 3820						 */
 3821						spin_lock_irqsave(
 3822							&phba->hbalock, iflag);
 3823						cmdiocbp->cmd_flag &=
 3824							~LPFC_DRIVER_ABORTED;
 3825						spin_unlock_irqrestore(
 3826							&phba->hbalock, iflag);
 3827						set_job_ulpstatus(cmdiocbp,
 3828								  IOSTAT_LOCAL_REJECT);
 3829						set_job_ulpword4(cmdiocbp,
 3830								 IOERR_ABORT_REQUESTED);
 3831						/*
 3832						 * For SLI4, irspiocb contains
 3833						 * NO_XRI in sli_xritag, it
 3834						 * shall not affect releasing
 3835						 * sgl (xri) process.
 3836						 */
 3837						set_job_ulpstatus(saveq,
 3838								  IOSTAT_LOCAL_REJECT);
 3839						set_job_ulpword4(saveq,
 3840								 IOERR_SLI_ABORTED);
 3841						spin_lock_irqsave(
 3842							&phba->hbalock, iflag);
 3843						saveq->cmd_flag |=
 3844							LPFC_DELAY_MEM_FREE;
 3845						spin_unlock_irqrestore(
 3846							&phba->hbalock, iflag);
 3847					}
 3848				}
 3849			}
 3850			cmdiocbp->cmd_cmpl(phba, cmdiocbp, saveq);
 3851		} else
 3852			lpfc_sli_release_iocbq(phba, cmdiocbp);
 3853	} else {
 3854		/*
 3855		 * Unknown initiating command based on the response iotag.
 3856		 * This could be the case on the ELS ring because of
 3857		 * lpfc_els_abort().
 3858		 */
 3859		if (pring->ringno != LPFC_ELS_RING) {
 3860			/*
 3861			 * Ring <ringno> handler: unexpected completion IoTag
 3862			 * <IoTag>
 3863			 */
 3864			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
 3865					 "0322 Ring %d handler: "
 3866					 "unexpected completion IoTag x%x "
 3867					 "Data: x%x x%x x%x x%x\n",
 3868					 pring->ringno, iotag, ulp_status,
 3869					 ulp_word4, ulp_command, ulp_context);
 3870		}
 3871	}
 3872
 3873	return 1;
 3874}
 3875
 3876/**
 3877 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
 3878 * @phba: Pointer to HBA context object.
 3879 * @pring: Pointer to driver SLI ring object.
 3880 *
 3881 * This function is called from the iocb ring event handlers when
 3882 * put pointer is ahead of the get pointer for a ring. This function signal
 3883 * an error attention condition to the worker thread and the worker
 3884 * thread will transition the HBA to offline state.
 3885 **/
 3886static void
 3887lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 3888{
 3889	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
 3890	/*
 3891	 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
 3892	 * rsp ring <portRspMax>
 3893	 */
 3894	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 3895			"0312 Ring %d handler: portRspPut %d "
 3896			"is bigger than rsp ring %d\n",
 3897			pring->ringno, le32_to_cpu(pgp->rspPutInx),
 3898			pring->sli.sli3.numRiocb);
 3899
 3900	phba->link_state = LPFC_HBA_ERROR;
 3901
 3902	/*
 3903	 * All error attention handlers are posted to
 3904	 * worker thread
 3905	 */
 3906	phba->work_ha |= HA_ERATT;
 3907	phba->work_hs = HS_FFER3;
 3908
 3909	lpfc_worker_wake_up(phba);
 3910
 3911	return;
 3912}
 3913
 3914/**
 3915 * lpfc_poll_eratt - Error attention polling timer timeout handler
 3916 * @t: Context to fetch pointer to address of HBA context object from.
 3917 *
 3918 * This function is invoked by the Error Attention polling timer when the
 3919 * timer times out. It will check the SLI Error Attention register for
 3920 * possible attention events. If so, it will post an Error Attention event
 3921 * and wake up worker thread to process it. Otherwise, it will set up the
 3922 * Error Attention polling timer for the next poll.
 3923 **/
 3924void lpfc_poll_eratt(struct timer_list *t)
 3925{
 3926	struct lpfc_hba *phba;
 3927	uint32_t eratt = 0;
 3928	uint64_t sli_intr, cnt;
 3929
 3930	phba = timer_container_of(phba, t, eratt_poll);
 3931
 3932	if (test_bit(FC_UNLOADING, &phba->pport->load_flag))
 3933		return;
 3934
 3935	if (phba->sli_rev == LPFC_SLI_REV4 &&
 3936	    !test_bit(HBA_SETUP, &phba->hba_flag)) {
 3937		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 3938				"0663 HBA still initializing 0x%lx, restart "
 3939				"timer\n",
 3940				phba->hba_flag);
 3941		goto restart_timer;
 3942	}
 3943
 3944	/* Here we will also keep track of interrupts per sec of the hba */
 3945	sli_intr = phba->sli.slistat.sli_intr;
 3946
 3947	if (phba->sli.slistat.sli_prev_intr > sli_intr)
 3948		cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
 3949			sli_intr);
 3950	else
 3951		cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
 3952
 3953	/* 64-bit integer division not supported on 32-bit x86 - use do_div */
 3954	do_div(cnt, phba->eratt_poll_interval);
 3955	phba->sli.slistat.sli_ips = cnt;
 3956
 3957	phba->sli.slistat.sli_prev_intr = sli_intr;
 3958
 3959	/* Check chip HA register for error event */
 3960	eratt = lpfc_sli_check_eratt(phba);
 3961
 3962	if (eratt) {
 3963		/* Tell the worker thread there is work to do */
 3964		lpfc_worker_wake_up(phba);
 3965		return;
 3966	}
 3967
 3968restart_timer:
 3969	/* Restart the timer for next eratt poll */
 3970	mod_timer(&phba->eratt_poll,
 3971		  jiffies + secs_to_jiffies(phba->eratt_poll_interval));
 3972	return;
 3973}
 3974
 3975
 3976/**
 3977 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
 3978 * @phba: Pointer to HBA context object.
 3979 * @pring: Pointer to driver SLI ring object.
 3980 * @mask: Host attention register mask for this ring.
 3981 *
 3982 * This function is called from the interrupt context when there is a ring
 3983 * event for the fcp ring. The caller does not hold any lock.
 3984 * The function processes each response iocb in the response ring until it
 3985 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
 3986 * LE bit set. The function will call the completion handler of the command iocb
 3987 * if the response iocb indicates a completion for a command iocb or it is
 3988 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
 3989 * function if this is an unsolicited iocb.
 3990 * This routine presumes LPFC_FCP_RING handling and doesn't bother
 3991 * to check it explicitly.
 3992 */
 3993int
 3994lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
 3995				struct lpfc_sli_ring *pring, uint32_t mask)
 3996{
 3997	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
 3998	IOCB_t *irsp = NULL;
 3999	IOCB_t *entry = NULL;
 4000	struct lpfc_iocbq *cmdiocbq = NULL;
 4001	struct lpfc_iocbq rspiocbq;
 4002	uint32_t status;
 4003	uint32_t portRspPut, portRspMax;
 4004	int rc = 1;
 4005	lpfc_iocb_type type;
 4006	unsigned long iflag;
 4007	uint32_t rsp_cmpl = 0;
 4008
 4009	spin_lock_irqsave(&phba->hbalock, iflag);
 4010	pring->stats.iocb_event++;
 4011
 4012	/*
 4013	 * The next available response entry should never exceed the maximum
 4014	 * entries.  If it does, treat it as an adapter hardware error.
 4015	 */
 4016	portRspMax = pring->sli.sli3.numRiocb;
 4017	portRspPut = le32_to_cpu(pgp->rspPutInx);
 4018	if (unlikely(portRspPut >= portRspMax)) {
 4019		lpfc_sli_rsp_pointers_error(phba, pring);
 4020		spin_unlock_irqrestore(&phba->hbalock, iflag);
 4021		return 1;
 4022	}
 4023	if (phba->fcp_ring_in_use) {
 4024		spin_unlock_irqrestore(&phba->hbalock, iflag);
 4025		return 1;
 4026	} else
 4027		phba->fcp_ring_in_use = 1;
 4028
 4029	rmb();
 4030	while (pring->sli.sli3.rspidx != portRspPut) {
 4031		/*
 4032		 * Fetch an entry off the ring and copy it into a local data
 4033		 * structure.  The copy involves a byte-swap since the
 4034		 * network byte order and pci byte orders are different.
 4035		 */
 4036		entry = lpfc_resp_iocb(phba, pring);
 4037		phba->last_completion_time = jiffies;
 4038
 4039		if (++pring->sli.sli3.rspidx >= portRspMax)
 4040			pring->sli.sli3.rspidx = 0;
 4041
 4042		lpfc_sli_pcimem_bcopy((uint32_t *) entry,
 4043				      (uint32_t *) &rspiocbq.iocb,
 4044				      phba->iocb_rsp_size);
 4045		INIT_LIST_HEAD(&(rspiocbq.list));
 4046		irsp = &rspiocbq.iocb;
 4047
 4048		type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
 4049		pring->stats.iocb_rsp++;
 4050		rsp_cmpl++;
 4051
 4052		if (unlikely(irsp->ulpStatus)) {
 4053			/*
 4054			 * If resource errors reported from HBA, reduce
 4055			 * queuedepths of the SCSI device.
 4056			 */
 4057			if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
 4058			    ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
 4059			     IOERR_NO_RESOURCES)) {
 4060				spin_unlock_irqrestore(&phba->hbalock, iflag);
 4061				phba->lpfc_rampdown_queue_depth(phba);
 4062				spin_lock_irqsave(&phba->hbalock, iflag);
 4063			}
 4064
 4065			/* Rsp ring <ringno> error: IOCB */
 4066			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
 4067					"0336 Rsp Ring %d error: IOCB Data: "
 4068					"x%x x%x x%x x%x x%x x%x x%x x%x\n",
 4069					pring->ringno,
 4070					irsp->un.ulpWord[0],
 4071					irsp->un.ulpWord[1],
 4072					irsp->un.ulpWord[2],
 4073					irsp->un.ulpWord[3],
 4074					irsp->un.ulpWord[4],
 4075					irsp->un.ulpWord[5],
 4076					*(uint32_t *)&irsp->un1,
 4077					*((uint32_t *)&irsp->un1 + 1));
 4078		}
 4079
 4080		switch (type) {
 4081		case LPFC_ABORT_IOCB:
 4082		case LPFC_SOL_IOCB:
 4083			/*
 4084			 * Idle exchange closed via ABTS from port.  No iocb
 4085			 * resources need to be recovered.
 4086			 */
 4087			if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
 4088				lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 4089						"0333 IOCB cmd 0x%x"
 4090						" processed. Skipping"
 4091						" completion\n",
 4092						irsp->ulpCommand);
 4093				break;
 4094			}
 4095
 4096			cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
 4097							 &rspiocbq);
 4098			if (unlikely(!cmdiocbq))
 4099				break;
 4100			if (cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED)
 4101				cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
 4102			if (cmdiocbq->cmd_cmpl) {
 4103				spin_unlock_irqrestore(&phba->hbalock, iflag);
 4104				cmdiocbq->cmd_cmpl(phba, cmdiocbq, &rspiocbq);
 4105				spin_lock_irqsave(&phba->hbalock, iflag);
 4106			}
 4107			break;
 4108		case LPFC_UNSOL_IOCB:
 4109			spin_unlock_irqrestore(&phba->hbalock, iflag);
 4110			lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
 4111			spin_lock_irqsave(&phba->hbalock, iflag);
 4112			break;
 4113		default:
 4114			if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
 4115				char adaptermsg[LPFC_MAX_ADPTMSG];
 4116				memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
 4117				memcpy(&adaptermsg[0], (uint8_t *) irsp,
 4118				       MAX_MSG_DATA);
 4119				dev_warn(&((phba->pcidev)->dev),
 4120					 "lpfc%d: %s\n",
 4121					 phba->brd_no, adaptermsg);
 4122			} else {
 4123				/* Unknown IOCB command */
 4124				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 4125						"0334 Unknown IOCB command "
 4126						"Data: x%x, x%x x%x x%x x%x\n",
 4127						type, irsp->ulpCommand,
 4128						irsp->ulpStatus,
 4129						irsp->ulpIoTag,
 4130						irsp->ulpContext);
 4131			}
 4132			break;
 4133		}
 4134
 4135		/*
 4136		 * The response IOCB has been processed.  Update the ring
 4137		 * pointer in SLIM.  If the port response put pointer has not
 4138		 * been updated, sync the pgp->rspPutInx and fetch the new port
 4139		 * response put pointer.
 4140		 */
 4141		writel(pring->sli.sli3.rspidx,
 4142			&phba->host_gp[pring->ringno].rspGetInx);
 4143
 4144		if (pring->sli.sli3.rspidx == portRspPut)
 4145			portRspPut = le32_to_cpu(pgp->rspPutInx);
 4146	}
 4147
 4148	if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
 4149		pring->stats.iocb_rsp_full++;
 4150		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
 4151		writel(status, phba->CAregaddr);
 4152		readl(phba->CAregaddr);
 4153	}
 4154	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
 4155		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
 4156		pring->stats.iocb_cmd_empty++;
 4157
 4158		/* Force update of the local copy of cmdGetInx */
 4159		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
 4160		lpfc_sli_resume_iocb(phba, pring);
 4161
 4162		if ((pring->lpfc_sli_cmd_available))
 4163			(pring->lpfc_sli_cmd_available) (phba, pring);
 4164
 4165	}
 4166
 4167	phba->fcp_ring_in_use = 0;
 4168	spin_unlock_irqrestore(&phba->hbalock, iflag);
 4169	return rc;
 4170}
 4171
 4172/**
 4173 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
 4174 * @phba: Pointer to HBA context object.
 4175 * @pring: Pointer to driver SLI ring object.
 4176 * @rspiocbp: Pointer to driver response IOCB object.
 4177 *
 4178 * This function is called from the worker thread when there is a slow-path
 4179 * response IOCB to process. This function chains all the response iocbs until
 4180 * seeing the iocb with the LE bit set. The function will call
 4181 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
 4182 * completion of a command iocb. The function will call the
 4183 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
 4184 * The function frees the resources or calls the completion handler if this
 4185 * iocb is an abort completion. The function returns NULL when the response
 4186 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
 4187 * this function shall chain the iocb on to the iocb_continueq and return the
 4188 * response iocb passed in.
 4189 **/
 4190static struct lpfc_iocbq *
 4191lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
 4192			struct lpfc_iocbq *rspiocbp)
 4193{
 4194	struct lpfc_iocbq *saveq;
 4195	struct lpfc_iocbq *cmdiocb;
 4196	struct lpfc_iocbq *next_iocb;
 4197	IOCB_t *irsp;
 4198	uint32_t free_saveq;
 4199	u8 cmd_type;
 4200	lpfc_iocb_type type;
 4201	unsigned long iflag;
 4202	u32 ulp_status = get_job_ulpstatus(phba, rspiocbp);
 4203	u32 ulp_word4 = get_job_word4(phba, rspiocbp);
 4204	u32 ulp_command = get_job_cmnd(phba, rspiocbp);
 4205	int rc;
 4206
 4207	spin_lock_irqsave(&phba->hbalock, iflag);
 4208	/* First add the response iocb to the countinueq list */
 4209	list_add_tail(&rspiocbp->list, &pring->iocb_continueq);
 4210	pring->iocb_continueq_cnt++;
 4211
 4212	/*
 4213	 * By default, the driver expects to free all resources
 4214	 * associated with this iocb completion.
 4215	 */
 4216	free_saveq = 1;
 4217	saveq = list_get_first(&pring->iocb_continueq,
 4218			       struct lpfc_iocbq, list);
 4219	list_del_init(&pring->iocb_continueq);
 4220	pring->iocb_continueq_cnt = 0;
 4221
 4222	pring->stats.iocb_rsp++;
 4223
 4224	/*
 4225	 * If resource errors reported from HBA, reduce
 4226	 * queuedepths of the SCSI device.
 4227	 */
 4228	if (ulp_status == IOSTAT_LOCAL_REJECT &&
 4229	    ((ulp_word4 & IOERR_PARAM_MASK) ==
 4230	     IOERR_NO_RESOURCES)) {
 4231		spin_unlock_irqrestore(&phba->hbalock, iflag);
 4232		phba->lpfc_rampdown_queue_depth(phba);
 4233		spin_lock_irqsave(&phba->hbalock, iflag);
 4234	}
 4235
 4236	if (ulp_status) {
 4237		/* Rsp ring <ringno> error: IOCB */
 4238		if (phba->sli_rev < LPFC_SLI_REV4) {
 4239			irsp = &rspiocbp->iocb;
 4240			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
 4241					"0328 Rsp Ring %d error: ulp_status x%x "
 4242					"IOCB Data: "
 4243					"x%08x x%08x x%08x x%08x "
 4244					"x%08x x%08x x%08x x%08x "
 4245					"x%08x x%08x x%08x x%08x "
 4246					"x%08x x%08x x%08x x%08x\n",
 4247					pring->ringno, ulp_status,
 4248					get_job_ulpword(rspiocbp, 0),
 4249					get_job_ulpword(rspiocbp, 1),
 4250					get_job_ulpword(rspiocbp, 2),
 4251					get_job_ulpword(rspiocbp, 3),
 4252					get_job_ulpword(rspiocbp, 4),
 4253					get_job_ulpword(rspiocbp, 5),
 4254					*(((uint32_t *)irsp) + 6),
 4255					*(((uint32_t *)irsp) + 7),
 4256					*(((uint32_t *)irsp) + 8),
 4257					*(((uint32_t *)irsp) + 9),
 4258					*(((uint32_t *)irsp) + 10),
 4259					*(((uint32_t *)irsp) + 11),
 4260					*(((uint32_t *)irsp) + 12),
 4261					*(((uint32_t *)irsp) + 13),
 4262					*(((uint32_t *)irsp) + 14),
 4263					*(((uint32_t *)irsp) + 15));
 4264		} else {
 4265			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
 4266					"0321 Rsp Ring %d error: "
 4267					"IOCB Data: "
 4268					"x%x x%x x%x x%x\n",
 4269					pring->ringno,
 4270					rspiocbp->wcqe_cmpl.word0,
 4271					rspiocbp->wcqe_cmpl.total_data_placed,
 4272					rspiocbp->wcqe_cmpl.parameter,
 4273					rspiocbp->wcqe_cmpl.word3);
 4274		}
 4275	}
 4276
 4277
 4278	/*
 4279	 * Fetch the iocb command type and call the correct completion
 4280	 * routine. Solicited and Unsolicited IOCBs on the ELS ring
 4281	 * get freed back to the lpfc_iocb_list by the discovery
 4282	 * kernel thread.
 4283	 */
 4284	cmd_type = ulp_command & CMD_IOCB_MASK;
 4285	type = lpfc_sli_iocb_cmd_type(cmd_type);
 4286	switch (type) {
 4287	case LPFC_SOL_IOCB:
 4288		spin_unlock_irqrestore(&phba->hbalock, iflag);
 4289		rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
 4290		spin_lock_irqsave(&phba->hbalock, iflag);
 4291		break;
 4292	case LPFC_UNSOL_IOCB:
 4293		spin_unlock_irqrestore(&phba->hbalock, iflag);
 4294		rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
 4295		spin_lock_irqsave(&phba->hbalock, iflag);
 4296		if (!rc)
 4297			free_saveq = 0;
 4298		break;
 4299	case LPFC_ABORT_IOCB:
 4300		cmdiocb = NULL;
 4301		if (ulp_command != CMD_XRI_ABORTED_CX)
 4302			cmdiocb = lpfc_sli_iocbq_lookup(phba, pring,
 4303							saveq);
 4304		if (cmdiocb) {
 4305			/* Call the specified completion routine */
 4306			if (cmdiocb->cmd_cmpl) {
 4307				spin_unlock_irqrestore(&phba->hbalock, iflag);
 4308				cmdiocb->cmd_cmpl(phba, cmdiocb, saveq);
 4309				spin_lock_irqsave(&phba->hbalock, iflag);
 4310			} else {
 4311				__lpfc_sli_release_iocbq(phba, cmdiocb);
 4312			}
 4313		}
 4314		break;
 4315	case LPFC_UNKNOWN_IOCB:
 4316		if (ulp_command == CMD_ADAPTER_MSG) {
 4317			char adaptermsg[LPFC_MAX_ADPTMSG];
 4318
 4319			memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
 4320			memcpy(&adaptermsg[0], (uint8_t *)&rspiocbp->wqe,
 4321			       MAX_MSG_DATA);
 4322			dev_warn(&((phba->pcidev)->dev),
 4323				 "lpfc%d: %s\n",
 4324				 phba->brd_no, adaptermsg);
 4325		} else {
 4326			/* Unknown command */
 4327			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 4328					"0335 Unknown IOCB "
 4329					"command Data: x%x "
 4330					"x%x x%x x%x\n",
 4331					ulp_command,
 4332					ulp_status,
 4333					get_wqe_reqtag(rspiocbp),
 4334					get_job_ulpcontext(phba, rspiocbp));
 4335		}
 4336		break;
 4337	}
 4338
 4339	if (free_saveq) {
 4340		list_for_each_entry_safe(rspiocbp, next_iocb,
 4341					 &saveq->list, list) {
 4342			list_del_init(&rspiocbp->list);
 4343			__lpfc_sli_release_iocbq(phba, rspiocbp);
 4344		}
 4345		__lpfc_sli_release_iocbq(phba, saveq);
 4346	}
 4347	rspiocbp = NULL;
 4348	spin_unlock_irqrestore(&phba->hbalock, iflag);
 4349	return rspiocbp;
 4350}
 4351
 4352/**
 4353 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
 4354 * @phba: Pointer to HBA context object.
 4355 * @pring: Pointer to driver SLI ring object.
 4356 * @mask: Host attention register mask for this ring.
 4357 *
 4358 * This routine wraps the actual slow_ring event process routine from the
 4359 * API jump table function pointer from the lpfc_hba struct.
 4360 **/
 4361void
 4362lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
 4363				struct lpfc_sli_ring *pring, uint32_t mask)
 4364{
 4365	phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
 4366}
 4367
 4368/**
 4369 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
 4370 * @phba: Pointer to HBA context object.
 4371 * @pring: Pointer to driver SLI ring object.
 4372 * @mask: Host attention register mask for this ring.
 4373 *
 4374 * This function is called from the worker thread when there is a ring event
 4375 * for non-fcp rings. The caller does not hold any lock. The function will
 4376 * remove each response iocb in the response ring and calls the handle
 4377 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
 4378 **/
 4379static void
 4380lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
 4381				   struct lpfc_sli_ring *pring, uint32_t mask)
 4382{
 4383	struct lpfc_pgp *pgp;
 4384	IOCB_t *entry;
 4385	IOCB_t *irsp = NULL;
 4386	struct lpfc_iocbq *rspiocbp = NULL;
 4387	uint32_t portRspPut, portRspMax;
 4388	unsigned long iflag;
 4389	uint32_t status;
 4390
 4391	pgp = &phba->port_gp[pring->ringno];
 4392	spin_lock_irqsave(&phba->hbalock, iflag);
 4393	pring->stats.iocb_event++;
 4394
 4395	/*
 4396	 * The next available response entry should never exceed the maximum
 4397	 * entries.  If it does, treat it as an adapter hardware error.
 4398	 */
 4399	portRspMax = pring->sli.sli3.numRiocb;
 4400	portRspPut = le32_to_cpu(pgp->rspPutInx);
 4401	if (portRspPut >= portRspMax) {
 4402		/*
 4403		 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
 4404		 * rsp ring <portRspMax>
 4405		 */
 4406		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 4407				"0303 Ring %d handler: portRspPut %d "
 4408				"is bigger than rsp ring %d\n",
 4409				pring->ringno, portRspPut, portRspMax);
 4410
 4411		phba->link_state = LPFC_HBA_ERROR;
 4412		spin_unlock_irqrestore(&phba->hbalock, iflag);
 4413
 4414		phba->work_hs = HS_FFER3;
 4415		lpfc_handle_eratt(phba);
 4416
 4417		return;
 4418	}
 4419
 4420	rmb();
 4421	while (pring->sli.sli3.rspidx != portRspPut) {
 4422		/*
 4423		 * Build a completion list and call the appropriate handler.
 4424		 * The process is to get the next available response iocb, get
 4425		 * a free iocb from the list, copy the response data into the
 4426		 * free iocb, insert to the continuation list, and update the
 4427		 * next response index to slim.  This process makes response
 4428		 * iocb's in the ring available to DMA as fast as possible but
 4429		 * pays a penalty for a copy operation.  Since the iocb is
 4430		 * only 32 bytes, this penalty is considered small relative to
 4431		 * the PCI reads for register values and a slim write.  When
 4432		 * the ulpLe field is set, the entire Command has been
 4433		 * received.
 4434		 */
 4435		entry = lpfc_resp_iocb(phba, pring);
 4436
 4437		phba->last_completion_time = jiffies;
 4438		rspiocbp = __lpfc_sli_get_iocbq(phba);
 4439		if (rspiocbp == NULL) {
 4440			printk(KERN_ERR "%s: out of buffers! Failing "
 4441			       "completion.\n", __func__);
 4442			break;
 4443		}
 4444
 4445		lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
 4446				      phba->iocb_rsp_size);
 4447		irsp = &rspiocbp->iocb;
 4448
 4449		if (++pring->sli.sli3.rspidx >= portRspMax)
 4450			pring->sli.sli3.rspidx = 0;
 4451
 4452		if (pring->ringno == LPFC_ELS_RING) {
 4453			lpfc_debugfs_slow_ring_trc(phba,
 4454			"IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
 4455				*(((uint32_t *) irsp) + 4),
 4456				*(((uint32_t *) irsp) + 6),
 4457				*(((uint32_t *) irsp) + 7));
 4458		}
 4459
 4460		writel(pring->sli.sli3.rspidx,
 4461			&phba->host_gp[pring->ringno].rspGetInx);
 4462
 4463		spin_unlock_irqrestore(&phba->hbalock, iflag);
 4464		/* Handle the response IOCB */
 4465		rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
 4466		spin_lock_irqsave(&phba->hbalock, iflag);
 4467
 4468		/*
 4469		 * If the port response put pointer has not been updated, sync
 4470		 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
 4471		 * response put pointer.
 4472		 */
 4473		if (pring->sli.sli3.rspidx == portRspPut) {
 4474			portRspPut = le32_to_cpu(pgp->rspPutInx);
 4475		}
 4476	} /* while (pring->sli.sli3.rspidx != portRspPut) */
 4477
 4478	if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
 4479		/* At least one response entry has been freed */
 4480		pring->stats.iocb_rsp_full++;
 4481		/* SET RxRE_RSP in Chip Att register */
 4482		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
 4483		writel(status, phba->CAregaddr);
 4484		readl(phba->CAregaddr); /* flush */
 4485	}
 4486	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
 4487		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
 4488		pring->stats.iocb_cmd_empty++;
 4489
 4490		/* Force update of the local copy of cmdGetInx */
 4491		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
 4492		lpfc_sli_resume_iocb(phba, pring);
 4493
 4494		if ((pring->lpfc_sli_cmd_available))
 4495			(pring->lpfc_sli_cmd_available) (phba, pring);
 4496
 4497	}
 4498
 4499	spin_unlock_irqrestore(&phba->hbalock, iflag);
 4500	return;
 4501}
 4502
 4503/**
 4504 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
 4505 * @phba: Pointer to HBA context object.
 4506 * @pring: Pointer to driver SLI ring object.
 4507 * @mask: Host attention register mask for this ring.
 4508 *
 4509 * This function is called from the worker thread when there is a pending
 4510 * ELS response iocb on the driver internal slow-path response iocb worker
 4511 * queue. The caller does not hold any lock. The function will remove each
 4512 * response iocb from the response worker queue and calls the handle
 4513 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
 4514 **/
 4515static void
 4516lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
 4517				   struct lpfc_sli_ring *pring, uint32_t mask)
 4518{
 4519	struct lpfc_iocbq *irspiocbq;
 4520	struct hbq_dmabuf *dmabuf;
 4521	struct lpfc_cq_event *cq_event;
 4522	unsigned long iflag;
 4523	int count = 0;
 4524
 4525	clear_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
 4526	while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
 4527		/* Get the response iocb from the head of work queue */
 4528		spin_lock_irqsave(&phba->hbalock, iflag);
 4529		list_remove_head(&phba->sli4_hba.sp_queue_event,
 4530				 cq_event, struct lpfc_cq_event, list);
 4531		spin_unlock_irqrestore(&phba->hbalock, iflag);
 4532
 4533		switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
 4534		case CQE_CODE_COMPL_WQE:
 4535			irspiocbq = container_of(cq_event, struct lpfc_iocbq,
 4536						 cq_event);
 4537			/* Translate ELS WCQE to response IOCBQ */
 4538			irspiocbq = lpfc_sli4_els_preprocess_rspiocbq(phba,
 4539								      irspiocbq);
 4540			if (irspiocbq)
 4541				lpfc_sli_sp_handle_rspiocb(phba, pring,
 4542							   irspiocbq);
 4543			count++;
 4544			break;
 4545		case CQE_CODE_RECEIVE:
 4546		case CQE_CODE_RECEIVE_V1:
 4547			dmabuf = container_of(cq_event, struct hbq_dmabuf,
 4548					      cq_event);
 4549			lpfc_sli4_handle_received_buffer(phba, dmabuf);
 4550			count++;
 4551			break;
 4552		default:
 4553			break;
 4554		}
 4555
 4556		/* Limit the number of events to 64 to avoid soft lockups */
 4557		if (count == 64)
 4558			break;
 4559	}
 4560}
 4561
 4562/**
 4563 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
 4564 * @phba: Pointer to HBA context object.
 4565 * @pring: Pointer to driver SLI ring object.
 4566 *
 4567 * This function aborts all iocbs in the given ring and frees all the iocb
 4568 * objects in txq. This function issues an abort iocb for all the iocb commands
 4569 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
 4570 * the return of this function. The caller is not required to hold any locks.
 4571 **/
 4572void
 4573lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
 4574{
 4575	LIST_HEAD(tx_completions);
 4576	LIST_HEAD(txcmplq_completions);
 4577	struct lpfc_iocbq *iocb, *next_iocb;
 4578	int offline;
 4579
 4580	if (pring->ringno == LPFC_ELS_RING) {
 4581		lpfc_fabric_abort_hba(phba);
 4582	}
 4583	offline = pci_channel_offline(phba->pcidev);
 4584
 4585	/* Error everything on txq and txcmplq
 4586	 * First do the txq.
 4587	 */
 4588	if (phba->sli_rev >= LPFC_SLI_REV4) {
 4589		spin_lock_irq(&pring->ring_lock);
 4590		list_splice_init(&pring->txq, &tx_completions);
 4591		pring->txq_cnt = 0;
 4592
 4593		if (offline) {
 4594			list_splice_init(&pring->txcmplq,
 4595					 &txcmplq_completions);
 4596		} else {
 4597			/* Next issue ABTS for everything on the txcmplq */
 4598			list_for_each_entry_safe(iocb, next_iocb,
 4599						 &pring->txcmplq, list)
 4600				lpfc_sli_issue_abort_iotag(phba, pring,
 4601							   iocb, NULL);
 4602		}
 4603		spin_unlock_irq(&pring->ring_lock);
 4604	} else {
 4605		spin_lock_irq(&phba->hbalock);
 4606		list_splice_init(&pring->txq, &tx_completions);
 4607		pring->txq_cnt = 0;
 4608
 4609		if (offline) {
 4610			list_splice_init(&pring->txcmplq, &txcmplq_completions);
 4611		} else {
 4612			/* Next issue ABTS for everything on the txcmplq */
 4613			list_for_each_entry_safe(iocb, next_iocb,
 4614						 &pring->txcmplq, list)
 4615				lpfc_sli_issue_abort_iotag(phba, pring,
 4616							   iocb, NULL);
 4617		}
 4618		spin_unlock_irq(&phba->hbalock);
 4619	}
 4620
 4621	if (offline) {
 4622		/* Cancel all the IOCBs from the completions list */
 4623		lpfc_sli_cancel_iocbs(phba, &txcmplq_completions,
 4624				      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
 4625	} else {
 4626		/* Make sure HBA is alive */
 4627		lpfc_issue_hb_tmo(phba);
 4628	}
 4629	/* Cancel all the IOCBs from the completions list */
 4630	lpfc_sli_cancel_iocbs(phba, &tx_completions, IOSTAT_LOCAL_REJECT,
 4631			      IOERR_SLI_ABORTED);
 4632}
 4633
 4634/**
 4635 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
 4636 * @phba: Pointer to HBA context object.
 4637 *
 4638 * This function aborts all iocbs in FCP rings and frees all the iocb
 4639 * objects in txq. This function issues an abort iocb for all the iocb commands
 4640 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
 4641 * the return of this function. The caller is not required to hold any locks.
 4642 **/
 4643void
 4644lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
 4645{
 4646	struct lpfc_sli *psli = &phba->sli;
 4647	struct lpfc_sli_ring  *pring;
 4648	uint32_t i;
 4649
 4650	/* Look on all the FCP Rings for the iotag */
 4651	if (phba->sli_rev >= LPFC_SLI_REV4) {
 4652		for (i = 0; i < phba->cfg_hdw_queue; i++) {
 4653			pring = phba->sli4_hba.hdwq[i].io_wq->pring;
 4654			lpfc_sli_abort_iocb_ring(phba, pring);
 4655		}
 4656	} else {
 4657		pring = &psli->sli3_ring[LPFC_FCP_RING];
 4658		lpfc_sli_abort_iocb_ring(phba, pring);
 4659	}
 4660}
 4661
 4662/**
 4663 * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
 4664 * @phba: Pointer to HBA context object.
 4665 *
 4666 * This function flushes all iocbs in the IO ring and frees all the iocb
 4667 * objects in txq and txcmplq. This function will not issue abort iocbs
 4668 * for all the iocb commands in txcmplq, they will just be returned with
 4669 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
 4670 * slot has been permanently disabled.
 4671 **/
 4672void
 4673lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
 4674{
 4675	LIST_HEAD(txq);
 4676	LIST_HEAD(txcmplq);
 4677	struct lpfc_sli *psli = &phba->sli;
 4678	struct lpfc_sli_ring  *pring;
 4679	uint32_t i;
 4680	struct lpfc_iocbq *piocb, *next_iocb;
 4681
 4682	/* Indicate the I/O queues are flushed */
 4683	set_bit(HBA_IOQ_FLUSH, &phba->hba_flag);
 4684
 4685	/* Look on all the FCP Rings for the iotag */
 4686	if (phba->sli_rev >= LPFC_SLI_REV4) {
 4687		for (i = 0; i < phba->cfg_hdw_queue; i++) {
 4688			if (!phba->sli4_hba.hdwq ||
 4689			    !phba->sli4_hba.hdwq[i].io_wq) {
 4690				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
 4691						"7777 hdwq's deleted %lx "
 4692						"%lx %x %x\n",
 4693						phba->pport->load_flag,
 4694						phba->hba_flag,
 4695						phba->link_state,
 4696						phba->sli.sli_flag);
 4697				return;
 4698			}
 4699			pring = phba->sli4_hba.hdwq[i].io_wq->pring;
 4700
 4701			spin_lock_irq(&pring->ring_lock);
 4702			/* Retrieve everything on txq */
 4703			list_splice_init(&pring->txq, &txq);
 4704			list_for_each_entry_safe(piocb, next_iocb,
 4705						 &pring->txcmplq, list)
 4706				piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
 4707			/* Retrieve everything on the txcmplq */
 4708			list_splice_init(&pring->txcmplq, &txcmplq);
 4709			pring->txq_cnt = 0;
 4710			pring->txcmplq_cnt = 0;
 4711			spin_unlock_irq(&pring->ring_lock);
 4712
 4713			/* Flush the txq */
 4714			lpfc_sli_cancel_iocbs(phba, &txq,
 4715					      IOSTAT_LOCAL_REJECT,
 4716					      IOERR_SLI_DOWN);
 4717			/* Flush the txcmplq */
 4718			lpfc_sli_cancel_iocbs(phba, &txcmplq,
 4719					      IOSTAT_LOCAL_REJECT,
 4720					      IOERR_SLI_DOWN);
 4721			if (unlikely(pci_channel_offline(phba->pcidev)))
 4722				lpfc_sli4_io_xri_aborted(phba, NULL, 0);
 4723		}
 4724	} else {
 4725		pring = &psli->sli3_ring[LPFC_FCP_RING];
 4726
 4727		spin_lock_irq(&phba->hbalock);
 4728		/* Retrieve everything on txq */
 4729		list_splice_init(&pring->txq, &txq);
 4730		list_for_each_entry_safe(piocb, next_iocb,
 4731					 &pring->txcmplq, list)
 4732			piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;
 4733		/* Retrieve everything on the txcmplq */
 4734		list_splice_init(&pring->txcmplq, &txcmplq);
 4735		pring->txq_cnt = 0;
 4736		pring->txcmplq_cnt = 0;
 4737		spin_unlock_irq(&phba->hbalock);
 4738
 4739		/* Flush the txq */
 4740		lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
 4741				      IOERR_SLI_DOWN);
 4742		/* Flush the txcmpq */
 4743		lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
 4744				      IOERR_SLI_DOWN);
 4745	}
 4746}
 4747
 4748/**
 4749 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
 4750 * @phba: Pointer to HBA context object.
 4751 * @mask: Bit mask to be checked.
 4752 *
 4753 * This function reads the host status register and compares
 4754 * with the provided bit mask to check if HBA completed
 4755 * the restart. This function will wait in a loop for the
 4756 * HBA to complete restart. If the HBA does not restart within
 4757 * 15 iterations, the function will reset the HBA again. The
 4758 * function returns 1 when HBA fail to restart otherwise returns
 4759 * zero.
 4760 **/
 4761static int
 4762lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
 4763{
 4764	uint32_t status;
 4765	int i = 0;
 4766	int retval = 0;
 4767
 4768	/* Read the HBA Host Status Register */
 4769	if (lpfc_readl(phba->HSregaddr, &status))
 4770		return 1;
 4771
 4772	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
 4773
 4774	/*
 4775	 * Check status register every 100ms for 5 retries, then every
 4776	 * 500ms for 5, then every 2.5 sec for 5, then reset board and
 4777	 * every 2.5 sec for 4.
 4778	 * Break our of the loop if errors occurred during init.
 4779	 */
 4780	while (((status & mask) != mask) &&
 4781	       !(status & HS_FFERM) &&
 4782	       i++ < 20) {
 4783
 4784		if (i <= 5)
 4785			msleep(10);
 4786		else if (i <= 10)
 4787			msleep(500);
 4788		else
 4789			msleep(2500);
 4790
 4791		if (i == 15) {
 4792				/* Do post */
 4793			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
 4794			lpfc_sli_brdrestart(phba);
 4795		}
 4796		/* Read the HBA Host Status Register */
 4797		if (lpfc_readl(phba->HSregaddr, &status)) {
 4798			retval = 1;
 4799			break;
 4800		}
 4801	}
 4802
 4803	/* Check to see if any errors occurred during init */
 4804	if ((status & HS_FFERM) || (i >= 20)) {
 4805		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 4806				"2751 Adapter failed to restart, "
 4807				"status reg x%x, FW Data: A8 x%x AC x%x\n",
 4808				status,
 4809				readl(phba->MBslimaddr + 0xa8),
 4810				readl(phba->MBslimaddr + 0xac));
 4811		phba->link_state = LPFC_HBA_ERROR;
 4812		retval = 1;
 4813	}
 4814
 4815	return retval;
 4816}
 4817
 4818/**
 4819 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
 4820 * @phba: Pointer to HBA context object.
 4821 * @mask: Bit mask to be checked.
 4822 *
 4823 * This function checks the host status register to check if HBA is
 4824 * ready. This function will wait in a loop for the HBA to be ready
 4825 * If the HBA is not ready , the function will will reset the HBA PCI
 4826 * function again. The function returns 1 when HBA fail to be ready
 4827 * otherwise returns zero.
 4828 **/
 4829static int
 4830lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
 4831{
 4832	uint32_t status;
 4833	int retval = 0;
 4834
 4835	/* Read the HBA Host Status Register */
 4836	status = lpfc_sli4_post_status_check(phba);
 4837
 4838	if (status) {
 4839		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
 4840		lpfc_sli_brdrestart(phba);
 4841		status = lpfc_sli4_post_status_check(phba);
 4842	}
 4843
 4844	/* Check to see if any errors occurred during init */
 4845	if (status) {
 4846		phba->link_state = LPFC_HBA_ERROR;
 4847		retval = 1;
 4848	} else
 4849		phba->sli4_hba.intr_enable = 0;
 4850
 4851	clear_bit(HBA_SETUP, &phba->hba_flag);
 4852	return retval;
 4853}
 4854
 4855/**
 4856 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
 4857 * @phba: Pointer to HBA context object.
 4858 * @mask: Bit mask to be checked.
 4859 *
 4860 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
 4861 * from the API jump table function pointer from the lpfc_hba struct.
 4862 **/
 4863int
 4864lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
 4865{
 4866	return phba->lpfc_sli_brdready(phba, mask);
 4867}
 4868
 4869#define BARRIER_TEST_PATTERN (0xdeadbeef)
 4870
 4871/**
 4872 * lpfc_reset_barrier - Make HBA ready for HBA reset
 4873 * @phba: Pointer to HBA context object.
 4874 *
 4875 * This function is called before resetting an HBA. This function is called
 4876 * with hbalock held and requests HBA to quiesce DMAs before a reset.
 4877 **/
 4878void lpfc_reset_barrier(struct lpfc_hba *phba)
 4879{
 4880	uint32_t __iomem *resp_buf;
 4881	uint32_t __iomem *mbox_buf;
 4882	volatile struct MAILBOX_word0 mbox;
 4883	uint32_t hc_copy, ha_copy, resp_data;
 4884	int  i;
 4885	uint8_t hdrtype;
 4886
 4887	lockdep_assert_held(&phba->hbalock);
 4888
 4889	pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
 4890	if (hdrtype != PCI_HEADER_TYPE_MFD ||
 4891	    (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
 4892	     FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
 4893		return;
 4894
 4895	/*
 4896	 * Tell the other part of the chip to suspend temporarily all
 4897	 * its DMA activity.
 4898	 */
 4899	resp_buf = phba->MBslimaddr;
 4900
 4901	/* Disable the error attention */
 4902	if (lpfc_readl(phba->HCregaddr, &hc_copy))
 4903		return;
 4904	writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
 4905	readl(phba->HCregaddr); /* flush */
 4906	phba->link_flag |= LS_IGNORE_ERATT;
 4907
 4908	if (lpfc_readl(phba->HAregaddr, &ha_copy))
 4909		return;
 4910	if (ha_copy & HA_ERATT) {
 4911		/* Clear Chip error bit */
 4912		writel(HA_ERATT, phba->HAregaddr);
 4913		phba->pport->stopped = 1;
 4914	}
 4915
 4916	mbox.word0 = 0;
 4917	mbox.mbxCommand = MBX_KILL_BOARD;
 4918	mbox.mbxOwner = OWN_CHIP;
 4919
 4920	writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
 4921	mbox_buf = phba->MBslimaddr;
 4922	writel(mbox.word0, mbox_buf);
 4923
 4924	for (i = 0; i < 50; i++) {
 4925		if (lpfc_readl((resp_buf + 1), &resp_data))
 4926			return;
 4927		if (resp_data != ~(BARRIER_TEST_PATTERN))
 4928			mdelay(1);
 4929		else
 4930			break;
 4931	}
 4932	resp_data = 0;
 4933	if (lpfc_readl((resp_buf + 1), &resp_data))
 4934		return;
 4935	if (resp_data  != ~(BARRIER_TEST_PATTERN)) {
 4936		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
 4937		    phba->pport->stopped)
 4938			goto restore_hc;
 4939		else
 4940			goto clear_errat;
 4941	}
 4942
 4943	mbox.mbxOwner = OWN_HOST;
 4944	resp_data = 0;
 4945	for (i = 0; i < 500; i++) {
 4946		if (lpfc_readl(resp_buf, &resp_data))
 4947			return;
 4948		if (resp_data != mbox.word0)
 4949			mdelay(1);
 4950		else
 4951			break;
 4952	}
 4953
 4954clear_errat:
 4955
 4956	while (++i < 500) {
 4957		if (lpfc_readl(phba->HAregaddr, &ha_copy))
 4958			return;
 4959		if (!(ha_copy & HA_ERATT))
 4960			mdelay(1);
 4961		else
 4962			break;
 4963	}
 4964
 4965	if (readl(phba->HAregaddr) & HA_ERATT) {
 4966		writel(HA_ERATT, phba->HAregaddr);
 4967		phba->pport->stopped = 1;
 4968	}
 4969
 4970restore_hc:
 4971	phba->link_flag &= ~LS_IGNORE_ERATT;
 4972	writel(hc_copy, phba->HCregaddr);
 4973	readl(phba->HCregaddr); /* flush */
 4974}
 4975
 4976/**
 4977 * lpfc_sli_brdkill - Issue a kill_board mailbox command
 4978 * @phba: Pointer to HBA context object.
 4979 *
 4980 * This function issues a kill_board mailbox command and waits for
 4981 * the error attention interrupt. This function is called for stopping
 4982 * the firmware processing. The caller is not required to hold any
 4983 * locks. This function calls lpfc_hba_down_post function to free
 4984 * any pending commands after the kill. The function will return 1 when it
 4985 * fails to kill the board else will return 0.
 4986 **/
 4987int
 4988lpfc_sli_brdkill(struct lpfc_hba *phba)
 4989{
 4990	struct lpfc_sli *psli;
 4991	LPFC_MBOXQ_t *pmb;
 4992	uint32_t status;
 4993	uint32_t ha_copy;
 4994	int retval;
 4995	int i = 0;
 4996
 4997	psli = &phba->sli;
 4998
 4999	/* Kill HBA */
 5000	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 5001			"0329 Kill HBA Data: x%x x%x\n",
 5002			phba->pport->port_state, psli->sli_flag);
 5003
 5004	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 5005	if (!pmb)
 5006		return 1;
 5007
 5008	/* Disable the error attention */
 5009	spin_lock_irq(&phba->hbalock);
 5010	if (lpfc_readl(phba->HCregaddr, &status)) {
 5011		spin_unlock_irq(&phba->hbalock);
 5012		mempool_free(pmb, phba->mbox_mem_pool);
 5013		return 1;
 5014	}
 5015	status &= ~HC_ERINT_ENA;
 5016	writel(status, phba->HCregaddr);
 5017	readl(phba->HCregaddr); /* flush */
 5018	phba->link_flag |= LS_IGNORE_ERATT;
 5019	spin_unlock_irq(&phba->hbalock);
 5020
 5021	lpfc_kill_board(phba, pmb);
 5022	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
 5023	retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
 5024
 5025	if (retval != MBX_SUCCESS) {
 5026		if (retval != MBX_BUSY)
 5027			mempool_free(pmb, phba->mbox_mem_pool);
 5028		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 5029				"2752 KILL_BOARD command failed retval %d\n",
 5030				retval);
 5031		spin_lock_irq(&phba->hbalock);
 5032		phba->link_flag &= ~LS_IGNORE_ERATT;
 5033		spin_unlock_irq(&phba->hbalock);
 5034		return 1;
 5035	}
 5036
 5037	spin_lock_irq(&phba->hbalock);
 5038	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
 5039	spin_unlock_irq(&phba->hbalock);
 5040
 5041	mempool_free(pmb, phba->mbox_mem_pool);
 5042
 5043	/* There is no completion for a KILL_BOARD mbox cmd. Check for an error
 5044	 * attention every 100ms for 3 seconds. If we don't get ERATT after
 5045	 * 3 seconds we still set HBA_ERROR state because the status of the
 5046	 * board is now undefined.
 5047	 */
 5048	if (lpfc_readl(phba->HAregaddr, &ha_copy))
 5049		return 1;
 5050	while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
 5051		mdelay(100);
 5052		if (lpfc_readl(phba->HAregaddr, &ha_copy))
 5053			return 1;
 5054	}
 5055
 5056	timer_delete_sync(&psli->mbox_tmo);
 5057	if (ha_copy & HA_ERATT) {
 5058		writel(HA_ERATT, phba->HAregaddr);
 5059		phba->pport->stopped = 1;
 5060	}
 5061	spin_lock_irq(&phba->hbalock);
 5062	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 5063	psli->mbox_active = NULL;
 5064	phba->link_flag &= ~LS_IGNORE_ERATT;
 5065	spin_unlock_irq(&phba->hbalock);
 5066
 5067	lpfc_hba_down_post(phba);
 5068	phba->link_state = LPFC_HBA_ERROR;
 5069
 5070	return ha_copy & HA_ERATT ? 0 : 1;
 5071}
 5072
 5073/**
 5074 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
 5075 * @phba: Pointer to HBA context object.
 5076 *
 5077 * This function resets the HBA by writing HC_INITFF to the control
 5078 * register. After the HBA resets, this function resets all the iocb ring
 5079 * indices. This function disables PCI layer parity checking during
 5080 * the reset.
 5081 * This function returns 0 always.
 5082 * The caller is not required to hold any locks.
 5083 **/
 5084int
 5085lpfc_sli_brdreset(struct lpfc_hba *phba)
 5086{
 5087	struct lpfc_sli *psli;
 5088	struct lpfc_sli_ring *pring;
 5089	uint16_t cfg_value;
 5090	int i;
 5091
 5092	psli = &phba->sli;
 5093
 5094	/* Reset HBA */
 5095	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 5096			"0325 Reset HBA Data: x%x x%x\n",
 5097			(phba->pport) ? phba->pport->port_state : 0,
 5098			psli->sli_flag);
 5099
 5100	/* perform board reset */
 5101	phba->fc_eventTag = 0;
 5102	phba->link_events = 0;
 5103	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
 5104	if (phba->pport) {
 5105		phba->pport->fc_myDID = 0;
 5106		phba->pport->fc_prevDID = 0;
 5107	}
 5108
 5109	/* Turn off parity checking and serr during the physical reset */
 5110	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
 5111		return -EIO;
 5112
 5113	pci_write_config_word(phba->pcidev, PCI_COMMAND,
 5114			      (cfg_value &
 5115			       ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
 5116
 5117	psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
 5118
 5119	/* Now toggle INITFF bit in the Host Control Register */
 5120	writel(HC_INITFF, phba->HCregaddr);
 5121	mdelay(1);
 5122	readl(phba->HCregaddr); /* flush */
 5123	writel(0, phba->HCregaddr);
 5124	readl(phba->HCregaddr); /* flush */
 5125
 5126	/* Restore PCI cmd register */
 5127	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
 5128
 5129	/* Initialize relevant SLI info */
 5130	for (i = 0; i < psli->num_rings; i++) {
 5131		pring = &psli->sli3_ring[i];
 5132		pring->flag = 0;
 5133		pring->sli.sli3.rspidx = 0;
 5134		pring->sli.sli3.next_cmdidx  = 0;
 5135		pring->sli.sli3.local_getidx = 0;
 5136		pring->sli.sli3.cmdidx = 0;
 5137		pring->missbufcnt = 0;
 5138	}
 5139
 5140	phba->link_state = LPFC_WARM_START;
 5141	return 0;
 5142}
 5143
 5144/**
 5145 * lpfc_sli4_brdreset - Reset a sli-4 HBA
 5146 * @phba: Pointer to HBA context object.
 5147 *
 5148 * This function resets a SLI4 HBA. This function disables PCI layer parity
 5149 * checking during resets the device. The caller is not required to hold
 5150 * any locks.
 5151 *
 5152 * This function returns 0 on success else returns negative error code.
 5153 **/
 5154int
 5155lpfc_sli4_brdreset(struct lpfc_hba *phba)
 5156{
 5157	struct lpfc_sli *psli = &phba->sli;
 5158	uint16_t cfg_value;
 5159	int rc = 0;
 5160
 5161	/* Reset HBA */
 5162	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 5163			"0295 Reset HBA Data: x%x x%x x%lx\n",
 5164			phba->pport->port_state, psli->sli_flag,
 5165			phba->hba_flag);
 5166
 5167	/* perform board reset */
 5168	phba->fc_eventTag = 0;
 5169	phba->link_events = 0;
 5170	phba->pport->fc_myDID = 0;
 5171	phba->pport->fc_prevDID = 0;
 5172
 5173	spin_lock_irq(&phba->hbalock);
 5174	psli->sli_flag &= ~(LPFC_PROCESS_LA);
 5175	phba->fcf.fcf_flag = 0;
 5176	spin_unlock_irq(&phba->hbalock);
 5177
 5178	/* Now physically reset the device */
 5179	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
 5180			"0389 Performing PCI function reset!\n");
 5181
 5182	/* Turn off parity checking and serr during the physical reset */
 5183	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
 5184		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
 5185				"3205 PCI read Config failed\n");
 5186		return -EIO;
 5187	}
 5188
 5189	pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
 5190			      ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
 5191
 5192	/* Perform FCoE PCI function reset before freeing queue memory */
 5193	rc = lpfc_pci_function_reset(phba);
 5194
 5195	/* Restore PCI cmd register */
 5196	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
 5197
 5198	return rc;
 5199}
 5200
 5201/**
 5202 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
 5203 * @phba: Pointer to HBA context object.
 5204 *
 5205 * This function is called in the SLI initialization code path to
 5206 * restart the HBA. The caller is not required to hold any lock.
 5207 * This function writes MBX_RESTART mailbox command to the SLIM and
 5208 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
 5209 * function to free any pending commands. The function enables
 5210 * POST only during the first initialization. The function returns zero.
 5211 * The function does not guarantee completion of MBX_RESTART mailbox
 5212 * command before the return of this function.
 5213 **/
 5214static int
 5215lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
 5216{
 5217	volatile struct MAILBOX_word0 mb;
 5218	struct lpfc_sli *psli;
 5219	void __iomem *to_slim;
 5220
 5221	spin_lock_irq(&phba->hbalock);
 5222
 5223	psli = &phba->sli;
 5224
 5225	/* Restart HBA */
 5226	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 5227			"0337 Restart HBA Data: x%x x%x\n",
 5228			(phba->pport) ? phba->pport->port_state : 0,
 5229			psli->sli_flag);
 5230
 5231	mb.word0 = 0;
 5232	mb.mbxCommand = MBX_RESTART;
 5233	mb.mbxHc = 1;
 5234
 5235	lpfc_reset_barrier(phba);
 5236
 5237	to_slim = phba->MBslimaddr;
 5238	writel(mb.word0, to_slim);
 5239	readl(to_slim); /* flush */
 5240
 5241	/* Only skip post after fc_ffinit is completed */
 5242	if (phba->pport && phba->pport->port_state)
 5243		mb.word0 = 1;	/* This is really setting up word1 */
 5244	else
 5245		mb.word0 = 0;	/* This is really setting up word1 */
 5246	to_slim = phba->MBslimaddr + sizeof (uint32_t);
 5247	writel(mb.word0, to_slim);
 5248	readl(to_slim); /* flush */
 5249
 5250	lpfc_sli_brdreset(phba);
 5251	if (phba->pport)
 5252		phba->pport->stopped = 0;
 5253	phba->link_state = LPFC_INIT_START;
 5254	phba->hba_flag = 0;
 5255	spin_unlock_irq(&phba->hbalock);
 5256
 5257	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
 5258	psli->stats_start = ktime_get_seconds();
 5259
 5260	/* Give the INITFF and Post time to settle. */
 5261	mdelay(100);
 5262
 5263	lpfc_hba_down_post(phba);
 5264
 5265	return 0;
 5266}
 5267
 5268/**
 5269 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
 5270 * @phba: Pointer to HBA context object.
 5271 *
 5272 * This function is called in the SLI initialization code path to restart
 5273 * a SLI4 HBA. The caller is not required to hold any lock.
 5274 * At the end of the function, it calls lpfc_hba_down_post function to
 5275 * free any pending commands.
 5276 **/
 5277static int
 5278lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
 5279{
 5280	struct lpfc_sli *psli = &phba->sli;
 5281	int rc;
 5282
 5283	/* Restart HBA */
 5284	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 5285			"0296 Restart HBA Data: x%x x%x\n",
 5286			phba->pport->port_state, psli->sli_flag);
 5287
 5288	clear_bit(HBA_SETUP, &phba->hba_flag);
 5289	lpfc_sli4_queue_unset(phba);
 5290
 5291	rc = lpfc_sli4_brdreset(phba);
 5292	if (rc) {
 5293		phba->link_state = LPFC_HBA_ERROR;
 5294		goto hba_down_queue;
 5295	}
 5296
 5297	spin_lock_irq(&phba->hbalock);
 5298	phba->pport->stopped = 0;
 5299	phba->link_state = LPFC_INIT_START;
 5300	phba->hba_flag = 0;
 5301	/* Preserve FA-PWWN expectation */
 5302	phba->sli4_hba.fawwpn_flag &= LPFC_FAWWPN_FABRIC;
 5303	spin_unlock_irq(&phba->hbalock);
 5304
 5305	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
 5306	psli->stats_start = ktime_get_seconds();
 5307
 5308hba_down_queue:
 5309	lpfc_hba_down_post(phba);
 5310	lpfc_sli4_queue_destroy(phba);
 5311
 5312	return rc;
 5313}
 5314
 5315/**
 5316 * lpfc_sli_brdrestart - Wrapper func for restarting hba
 5317 * @phba: Pointer to HBA context object.
 5318 *
 5319 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
 5320 * API jump table function pointer from the lpfc_hba struct.
 5321**/
 5322int
 5323lpfc_sli_brdrestart(struct lpfc_hba *phba)
 5324{
 5325	return phba->lpfc_sli_brdrestart(phba);
 5326}
 5327
 5328/**
 5329 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
 5330 * @phba: Pointer to HBA context object.
 5331 *
 5332 * This function is called after a HBA restart to wait for successful
 5333 * restart of the HBA. Successful restart of the HBA is indicated by
 5334 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
 5335 * iteration, the function will restart the HBA again. The function returns
 5336 * zero if HBA successfully restarted else returns negative error code.
 5337 **/
 5338int
 5339lpfc_sli_chipset_init(struct lpfc_hba *phba)
 5340{
 5341	uint32_t status, i = 0;
 5342
 5343	/* Read the HBA Host Status Register */
 5344	if (lpfc_readl(phba->HSregaddr, &status))
 5345		return -EIO;
 5346
 5347	/* Check status register to see what current state is */
 5348	i = 0;
 5349	while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
 5350
 5351		/* Check every 10ms for 10 retries, then every 100ms for 90
 5352		 * retries, then every 1 sec for 50 retires for a total of
 5353		 * ~60 seconds before reset the board again and check every
 5354		 * 1 sec for 50 retries. The up to 60 seconds before the
 5355		 * board ready is required by the Falcon FIPS zeroization
 5356		 * complete, and any reset the board in between shall cause
 5357		 * restart of zeroization, further delay the board ready.
 5358		 */
 5359		if (i++ >= 200) {
 5360			/* Adapter failed to init, timeout, status reg
 5361			   <status> */
 5362			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 5363					"0436 Adapter failed to init, "
 5364					"timeout, status reg x%x, "
 5365					"FW Data: A8 x%x AC x%x\n", status,
 5366					readl(phba->MBslimaddr + 0xa8),
 5367					readl(phba->MBslimaddr + 0xac));
 5368			phba->link_state = LPFC_HBA_ERROR;
 5369			return -ETIMEDOUT;
 5370		}
 5371
 5372		/* Check to see if any errors occurred during init */
 5373		if (status & HS_FFERM) {
 5374			/* ERROR: During chipset initialization */
 5375			/* Adapter failed to init, chipset, status reg
 5376			   <status> */
 5377			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 5378					"0437 Adapter failed to init, "
 5379					"chipset, status reg x%x, "
 5380					"FW Data: A8 x%x AC x%x\n", status,
 5381					readl(phba->MBslimaddr + 0xa8),
 5382					readl(phba->MBslimaddr + 0xac));
 5383			phba->link_state = LPFC_HBA_ERROR;
 5384			return -EIO;
 5385		}
 5386
 5387		if (i <= 10)
 5388			msleep(10);
 5389		else if (i <= 100)
 5390			msleep(100);
 5391		else
 5392			msleep(1000);
 5393
 5394		if (i == 150) {
 5395			/* Do post */
 5396			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
 5397			lpfc_sli_brdrestart(phba);
 5398		}
 5399		/* Read the HBA Host Status Register */
 5400		if (lpfc_readl(phba->HSregaddr, &status))
 5401			return -EIO;
 5402	}
 5403
 5404	/* Check to see if any errors occurred during init */
 5405	if (status & HS_FFERM) {
 5406		/* ERROR: During chipset initialization */
 5407		/* Adapter failed to init, chipset, status reg <status> */
 5408		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 5409				"0438 Adapter failed to init, chipset, "
 5410				"status reg x%x, "
 5411				"FW Data: A8 x%x AC x%x\n", status,
 5412				readl(phba->MBslimaddr + 0xa8),
 5413				readl(phba->MBslimaddr + 0xac));
 5414		phba->link_state = LPFC_HBA_ERROR;
 5415		return -EIO;
 5416	}
 5417
 5418	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
 5419
 5420	/* Clear all interrupt enable conditions */
 5421	writel(0, phba->HCregaddr);
 5422	readl(phba->HCregaddr); /* flush */
 5423
 5424	/* setup host attn register */
 5425	writel(0xffffffff, phba->HAregaddr);
 5426	readl(phba->HAregaddr); /* flush */
 5427	return 0;
 5428}
 5429
 5430/**
 5431 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
 5432 *
 5433 * This function calculates and returns the number of HBQs required to be
 5434 * configured.
 5435 **/
 5436int
 5437lpfc_sli_hbq_count(void)
 5438{
 5439	return ARRAY_SIZE(lpfc_hbq_defs);
 5440}
 5441
 5442/**
 5443 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
 5444 *
 5445 * This function adds the number of hbq entries in every HBQ to get
 5446 * the total number of hbq entries required for the HBA and returns
 5447 * the total count.
 5448 **/
 5449static int
 5450lpfc_sli_hbq_entry_count(void)
 5451{
 5452	int  hbq_count = lpfc_sli_hbq_count();
 5453	int  count = 0;
 5454	int  i;
 5455
 5456	for (i = 0; i < hbq_count; ++i)
 5457		count += lpfc_hbq_defs[i]->entry_count;
 5458	return count;
 5459}
 5460
 5461/**
 5462 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
 5463 *
 5464 * This function calculates amount of memory required for all hbq entries
 5465 * to be configured and returns the total memory required.
 5466 **/
 5467int
 5468lpfc_sli_hbq_size(void)
 5469{
 5470	return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
 5471}
 5472
 5473/**
 5474 * lpfc_sli_hbq_setup - configure and initialize HBQs
 5475 * @phba: Pointer to HBA context object.
 5476 *
 5477 * This function is called during the SLI initialization to configure
 5478 * all the HBQs and post buffers to the HBQ. The caller is not
 5479 * required to hold any locks. This function will return zero if successful
 5480 * else it will return negative error code.
 5481 **/
 5482static int
 5483lpfc_sli_hbq_setup(struct lpfc_hba *phba)
 5484{
 5485	int  hbq_count = lpfc_sli_hbq_count();
 5486	LPFC_MBOXQ_t *pmb;
 5487	MAILBOX_t *pmbox;
 5488	uint32_t hbqno;
 5489	uint32_t hbq_entry_index;
 5490
 5491				/* Get a Mailbox buffer to setup mailbox
 5492				 * commands for HBA initialization
 5493				 */
 5494	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 5495
 5496	if (!pmb)
 5497		return -ENOMEM;
 5498
 5499	pmbox = &pmb->u.mb;
 5500
 5501	/* Initialize the struct lpfc_sli_hbq structure for each hbq */
 5502	phba->link_state = LPFC_INIT_MBX_CMDS;
 5503	phba->hbq_in_use = 1;
 5504
 5505	hbq_entry_index = 0;
 5506	for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
 5507		phba->hbqs[hbqno].next_hbqPutIdx = 0;
 5508		phba->hbqs[hbqno].hbqPutIdx      = 0;
 5509		phba->hbqs[hbqno].local_hbqGetIdx   = 0;
 5510		phba->hbqs[hbqno].entry_count =
 5511			lpfc_hbq_defs[hbqno]->entry_count;
 5512		lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
 5513			hbq_entry_index, pmb);
 5514		hbq_entry_index += phba->hbqs[hbqno].entry_count;
 5515
 5516		if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
 5517			/* Adapter failed to init, mbxCmd <cmd> CFG_RING,
 5518			   mbxStatus <status>, ring <num> */
 5519
 5520			lpfc_printf_log(phba, KERN_ERR,
 5521					LOG_SLI | LOG_VPORT,
 5522					"1805 Adapter failed to init. "
 5523					"Data: x%x x%x x%x\n",
 5524					pmbox->mbxCommand,
 5525					pmbox->mbxStatus, hbqno);
 5526
 5527			phba->link_state = LPFC_HBA_ERROR;
 5528			mempool_free(pmb, phba->mbox_mem_pool);
 5529			return -ENXIO;
 5530		}
 5531	}
 5532	phba->hbq_count = hbq_count;
 5533
 5534	mempool_free(pmb, phba->mbox_mem_pool);
 5535
 5536	/* Initially populate or replenish the HBQs */
 5537	for (hbqno = 0; hbqno < hbq_count; ++hbqno)
 5538		lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
 5539	return 0;
 5540}
 5541
 5542/**
 5543 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
 5544 * @phba: Pointer to HBA context object.
 5545 *
 5546 * This function is called during the SLI initialization to configure
 5547 * all the HBQs and post buffers to the HBQ. The caller is not
 5548 * required to hold any locks. This function will return zero if successful
 5549 * else it will return negative error code.
 5550 **/
 5551static int
 5552lpfc_sli4_rb_setup(struct lpfc_hba *phba)
 5553{
 5554	phba->hbq_in_use = 1;
 5555	/**
 5556	 * Specific case when the MDS diagnostics is enabled and supported.
 5557	 * The receive buffer count is truncated to manage the incoming
 5558	 * traffic.
 5559	 **/
 5560	if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
 5561		phba->hbqs[LPFC_ELS_HBQ].entry_count =
 5562			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
 5563	else
 5564		phba->hbqs[LPFC_ELS_HBQ].entry_count =
 5565			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
 5566	phba->hbq_count = 1;
 5567	lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
 5568	/* Initially populate or replenish the HBQs */
 5569	return 0;
 5570}
 5571
 5572/**
 5573 * lpfc_sli_config_port - Issue config port mailbox command
 5574 * @phba: Pointer to HBA context object.
 5575 * @sli_mode: sli mode - 2/3
 5576 *
 5577 * This function is called by the sli initialization code path
 5578 * to issue config_port mailbox command. This function restarts the
 5579 * HBA firmware and issues a config_port mailbox command to configure
 5580 * the SLI interface in the sli mode specified by sli_mode
 5581 * variable. The caller is not required to hold any locks.
 5582 * The function returns 0 if successful, else returns negative error
 5583 * code.
 5584 **/
 5585int
 5586lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
 5587{
 5588	LPFC_MBOXQ_t *pmb;
 5589	uint32_t resetcount = 0, rc = 0, done = 0;
 5590
 5591	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 5592	if (!pmb) {
 5593		phba->link_state = LPFC_HBA_ERROR;
 5594		return -ENOMEM;
 5595	}
 5596
 5597	phba->sli_rev = sli_mode;
 5598	while (resetcount < 2 && !done) {
 5599		spin_lock_irq(&phba->hbalock);
 5600		phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
 5601		spin_unlock_irq(&phba->hbalock);
 5602		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
 5603		lpfc_sli_brdrestart(phba);
 5604		rc = lpfc_sli_chipset_init(phba);
 5605		if (rc)
 5606			break;
 5607
 5608		spin_lock_irq(&phba->hbalock);
 5609		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 5610		spin_unlock_irq(&phba->hbalock);
 5611		resetcount++;
 5612
 5613		/* Call pre CONFIG_PORT mailbox command initialization.  A
 5614		 * value of 0 means the call was successful.  Any other
 5615		 * nonzero value is a failure, but if ERESTART is returned,
 5616		 * the driver may reset the HBA and try again.
 5617		 */
 5618		rc = lpfc_config_port_prep(phba);
 5619		if (rc == -ERESTART) {
 5620			phba->link_state = LPFC_LINK_UNKNOWN;
 5621			continue;
 5622		} else if (rc)
 5623			break;
 5624
 5625		phba->link_state = LPFC_INIT_MBX_CMDS;
 5626		lpfc_config_port(phba, pmb);
 5627		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
 5628		phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
 5629					LPFC_SLI3_HBQ_ENABLED |
 5630					LPFC_SLI3_CRP_ENABLED |
 5631					LPFC_SLI3_DSS_ENABLED);
 5632		if (rc != MBX_SUCCESS) {
 5633			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 5634				"0442 Adapter failed to init, mbxCmd x%x "
 5635				"CONFIG_PORT, mbxStatus x%x Data: x%x\n",
 5636				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
 5637			spin_lock_irq(&phba->hbalock);
 5638			phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
 5639			spin_unlock_irq(&phba->hbalock);
 5640			rc = -ENXIO;
 5641		} else {
 5642			/* Allow asynchronous mailbox command to go through */
 5643			spin_lock_irq(&phba->hbalock);
 5644			phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
 5645			spin_unlock_irq(&phba->hbalock);
 5646			done = 1;
 5647
 5648			if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
 5649			    (pmb->u.mb.un.varCfgPort.gasabt == 0))
 5650				lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
 5651					"3110 Port did not grant ASABT\n");
 5652		}
 5653	}
 5654	if (!done) {
 5655		rc = -EINVAL;
 5656		goto do_prep_failed;
 5657	}
 5658	if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
 5659		if (!pmb->u.mb.un.varCfgPort.cMA) {
 5660			rc = -ENXIO;
 5661			goto do_prep_failed;
 5662		}
 5663		if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
 5664			phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
 5665			phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
 5666			phba->max_vports = (phba->max_vpi > phba->max_vports) ?
 5667				phba->max_vpi : phba->max_vports;
 5668
 5669		} else
 5670			phba->max_vpi = 0;
 5671		if (pmb->u.mb.un.varCfgPort.gerbm)
 5672			phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
 5673		if (pmb->u.mb.un.varCfgPort.gcrp)
 5674			phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
 5675
 5676		phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
 5677		phba->port_gp = phba->mbox->us.s3_pgp.port;
 5678
 5679		if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
 5680			if (pmb->u.mb.un.varCfgPort.gbg == 0) {
 5681				phba->cfg_enable_bg = 0;
 5682				phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
 5683				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 5684						"0443 Adapter did not grant "
 5685						"BlockGuard\n");
 5686			}
 5687		}
 5688	} else {
 5689		phba->hbq_get = NULL;
 5690		phba->port_gp = phba->mbox->us.s2.port;
 5691		phba->max_vpi = 0;
 5692	}
 5693do_prep_failed:
 5694	mempool_free(pmb, phba->mbox_mem_pool);
 5695	return rc;
 5696}
 5697
 5698
 5699/**
 5700 * lpfc_sli_hba_setup - SLI initialization function
 5701 * @phba: Pointer to HBA context object.
 5702 *
 5703 * This function is the main SLI initialization function. This function
 5704 * is called by the HBA initialization code, HBA reset code and HBA
 5705 * error attention handler code. Caller is not required to hold any
 5706 * locks. This function issues config_port mailbox command to configure
 5707 * the SLI, setup iocb rings and HBQ rings. In the end the function
 5708 * calls the config_port_post function to issue init_link mailbox
 5709 * command and to start the discovery. The function will return zero
 5710 * if successful, else it will return negative error code.
 5711 **/
 5712int
 5713lpfc_sli_hba_setup(struct lpfc_hba *phba)
 5714{
 5715	uint32_t rc;
 5716	int  i;
 5717	int longs;
 5718
 5719	/* Enable ISR already does config_port because of config_msi mbx */
 5720	if (test_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag)) {
 5721		rc = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
 5722		if (rc)
 5723			return -EIO;
 5724		clear_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);
 5725	}
 5726	phba->fcp_embed_io = 0;	/* SLI4 FC support only */
 5727
 5728	if (phba->sli_rev == 3) {
 5729		phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
 5730		phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
 5731	} else {
 5732		phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
 5733		phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
 5734		phba->sli3_options = 0;
 5735	}
 5736
 5737	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
 5738			"0444 Firmware in SLI %x mode. Max_vpi %d\n",
 5739			phba->sli_rev, phba->max_vpi);
 5740	rc = lpfc_sli_ring_map(phba);
 5741
 5742	if (rc)
 5743		goto lpfc_sli_hba_setup_error;
 5744
 5745	/* Initialize VPIs. */
 5746	if (phba->sli_rev == LPFC_SLI_REV3) {
 5747		/*
 5748		 * The VPI bitmask and physical ID array are allocated
 5749		 * and initialized once only - at driver load.  A port
 5750		 * reset doesn't need to reinitialize this memory.
 5751		 */
 5752		if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
 5753			longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
 5754			phba->vpi_bmask = kcalloc(longs,
 5755						  sizeof(unsigned long),
 5756						  GFP_KERNEL);
 5757			if (!phba->vpi_bmask) {
 5758				rc = -ENOMEM;
 5759				goto lpfc_sli_hba_setup_error;
 5760			}
 5761
 5762			phba->vpi_ids = kcalloc(phba->max_vpi + 1,
 5763						sizeof(uint16_t),
 5764						GFP_KERNEL);
 5765			if (!phba->vpi_ids) {
 5766				kfree(phba->vpi_bmask);
 5767				rc = -ENOMEM;
 5768				goto lpfc_sli_hba_setup_error;
 5769			}
 5770			for (i = 0; i < phba->max_vpi; i++)
 5771				phba->vpi_ids[i] = i;
 5772		}
 5773	}
 5774
 5775	/* Init HBQs */
 5776	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
 5777		rc = lpfc_sli_hbq_setup(phba);
 5778		if (rc)
 5779			goto lpfc_sli_hba_setup_error;
 5780	}
 5781	spin_lock_irq(&phba->hbalock);
 5782	phba->sli.sli_flag |= LPFC_PROCESS_LA;
 5783	spin_unlock_irq(&phba->hbalock);
 5784
 5785	rc = lpfc_config_port_post(phba);
 5786	if (rc)
 5787		goto lpfc_sli_hba_setup_error;
 5788
 5789	return rc;
 5790
 5791lpfc_sli_hba_setup_error:
 5792	phba->link_state = LPFC_HBA_ERROR;
 5793	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 5794			"0445 Firmware initialization failed\n");
 5795	return rc;
 5796}
 5797
 5798/**
 5799 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
 5800 * @phba: Pointer to HBA context object.
 5801 *
 5802 * This function issue a dump mailbox command to read config region
 5803 * 23 and parse the records in the region and populate driver
 5804 * data structure.
 5805 **/
 5806static int
 5807lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
 5808{
 5809	LPFC_MBOXQ_t *mboxq;
 5810	struct lpfc_dmabuf *mp;
 5811	struct lpfc_mqe *mqe;
 5812	uint32_t data_length;
 5813	int rc;
 5814
 5815	/* Program the default value of vlan_id and fc_map */
 5816	phba->valid_vlan = 0;
 5817	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
 5818	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
 5819	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
 5820
 5821	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 5822	if (!mboxq)
 5823		return -ENOMEM;
 5824
 5825	mqe = &mboxq->u.mqe;
 5826	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
 5827		rc = -ENOMEM;
 5828		goto out_free_mboxq;
 5829	}
 5830
 5831	mp = mboxq->ctx_buf;
 5832	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 5833
 5834	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
 5835			"(%d):2571 Mailbox cmd x%x Status x%x "
 5836			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
 5837			"x%x x%x x%x x%x x%x x%x x%x x%x x%x "
 5838			"CQ: x%x x%x x%x x%x\n",
 5839			mboxq->vport ? mboxq->vport->vpi : 0,
 5840			bf_get(lpfc_mqe_command, mqe),
 5841			bf_get(lpfc_mqe_status, mqe),
 5842			mqe->un.mb_words[0], mqe->un.mb_words[1],
 5843			mqe->un.mb_words[2], mqe->un.mb_words[3],
 5844			mqe->un.mb_words[4], mqe->un.mb_words[5],
 5845			mqe->un.mb_words[6], mqe->un.mb_words[7],
 5846			mqe->un.mb_words[8], mqe->un.mb_words[9],
 5847			mqe->un.mb_words[10], mqe->un.mb_words[11],
 5848			mqe->un.mb_words[12], mqe->un.mb_words[13],
 5849			mqe->un.mb_words[14], mqe->un.mb_words[15],
 5850			mqe->un.mb_words[16], mqe->un.mb_words[50],
 5851			mboxq->mcqe.word0,
 5852			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
 5853			mboxq->mcqe.trailer);
 5854
 5855	if (rc) {
 5856		rc = -EIO;
 5857		goto out_free_mboxq;
 5858	}
 5859	data_length = mqe->un.mb_words[5];
 5860	if (data_length > DMP_RGN23_SIZE) {
 5861		rc = -EIO;
 5862		goto out_free_mboxq;
 5863	}
 5864
 5865	lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
 5866	rc = 0;
 5867
 5868out_free_mboxq:
 5869	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
 5870	return rc;
 5871}
 5872
 5873/**
 5874 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
 5875 * @phba: pointer to lpfc hba data structure.
 5876 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
 5877 * @vpd: pointer to the memory to hold resulting port vpd data.
 5878 * @vpd_size: On input, the number of bytes allocated to @vpd.
 5879 *	      On output, the number of data bytes in @vpd.
 5880 *
 5881 * This routine executes a READ_REV SLI4 mailbox command.  In
 5882 * addition, this routine gets the port vpd data.
 5883 *
 5884 * Return codes
 5885 * 	0 - successful
 5886 * 	-ENOMEM - could not allocated memory.
 5887 **/
 5888static int
 5889lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
 5890		    uint8_t *vpd, uint32_t *vpd_size)
 5891{
 5892	int rc = 0;
 5893	uint32_t dma_size;
 5894	struct lpfc_dmabuf *dmabuf;
 5895	struct lpfc_mqe *mqe;
 5896
 5897	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
 5898	if (!dmabuf)
 5899		return -ENOMEM;
 5900
 5901	/*
 5902	 * Get a DMA buffer for the vpd data resulting from the READ_REV
 5903	 * mailbox command.
 5904	 */
 5905	dma_size = *vpd_size;
 5906	dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
 5907					  &dmabuf->phys, GFP_KERNEL);
 5908	if (!dmabuf->virt) {
 5909		kfree(dmabuf);
 5910		return -ENOMEM;
 5911	}
 5912
 5913	/*
 5914	 * The SLI4 implementation of READ_REV conflicts at word1,
 5915	 * bits 31:16 and SLI4 adds vpd functionality not present
 5916	 * in SLI3.  This code corrects the conflicts.
 5917	 */
 5918	lpfc_read_rev(phba, mboxq);
 5919	mqe = &mboxq->u.mqe;
 5920	mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
 5921	mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
 5922	mqe->un.read_rev.word1 &= 0x0000FFFF;
 5923	bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
 5924	bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
 5925
 5926	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 5927	if (rc) {
 5928		dma_free_coherent(&phba->pcidev->dev, dma_size,
 5929				  dmabuf->virt, dmabuf->phys);
 5930		kfree(dmabuf);
 5931		return -EIO;
 5932	}
 5933
 5934	/*
 5935	 * The available vpd length cannot be bigger than the
 5936	 * DMA buffer passed to the port.  Catch the less than
 5937	 * case and update the caller's size.
 5938	 */
 5939	if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
 5940		*vpd_size = mqe->un.read_rev.avail_vpd_len;
 5941
 5942	memcpy(vpd, dmabuf->virt, *vpd_size);
 5943
 5944	dma_free_coherent(&phba->pcidev->dev, dma_size,
 5945			  dmabuf->virt, dmabuf->phys);
 5946	kfree(dmabuf);
 5947	return 0;
 5948}
 5949
 5950/**
 5951 * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
 5952 * @phba: pointer to lpfc hba data structure.
 5953 *
 5954 * This routine retrieves SLI4 device physical port name this PCI function
 5955 * is attached to.
 5956 *
 5957 * Return codes
 5958 *      0 - successful
 5959 *      otherwise - failed to retrieve controller attributes
 5960 **/
 5961static int
 5962lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
 5963{
 5964	LPFC_MBOXQ_t *mboxq;
 5965	struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
 5966	struct lpfc_controller_attribute *cntl_attr;
 5967	void *virtaddr = NULL;
 5968	uint32_t alloclen, reqlen;
 5969	uint32_t shdr_status, shdr_add_status;
 5970	union lpfc_sli4_cfg_shdr *shdr;
 5971	int rc;
 5972
 5973	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 5974	if (!mboxq)
 5975		return -ENOMEM;
 5976
 5977	/* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
 5978	reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
 5979	alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
 5980			LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
 5981			LPFC_SLI4_MBX_NEMBED);
 5982
 5983	if (alloclen < reqlen) {
 5984		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 5985				"3084 Allocated DMA memory size (%d) is "
 5986				"less than the requested DMA memory size "
 5987				"(%d)\n", alloclen, reqlen);
 5988		rc = -ENOMEM;
 5989		goto out_free_mboxq;
 5990	}
 5991	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 5992	virtaddr = mboxq->sge_array->addr[0];
 5993	mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
 5994	shdr = &mbx_cntl_attr->cfg_shdr;
 5995	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
 5996	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
 5997	if (shdr_status || shdr_add_status || rc) {
 5998		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
 5999				"3085 Mailbox x%x (x%x/x%x) failed, "
 6000				"rc:x%x, status:x%x, add_status:x%x\n",
 6001				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
 6002				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
 6003				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
 6004				rc, shdr_status, shdr_add_status);
 6005		rc = -ENXIO;
 6006		goto out_free_mboxq;
 6007	}
 6008
 6009	cntl_attr = &mbx_cntl_attr->cntl_attr;
 6010	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
 6011	phba->sli4_hba.lnk_info.lnk_tp =
 6012		bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
 6013	phba->sli4_hba.lnk_info.lnk_no =
 6014		bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
 6015	phba->sli4_hba.flash_id = bf_get(lpfc_cntl_attr_flash_id, cntl_attr);
 6016	phba->sli4_hba.asic_rev = bf_get(lpfc_cntl_attr_asic_rev, cntl_attr);
 6017
 6018	memcpy(phba->BIOSVersion, cntl_attr->bios_ver_str,
 6019		sizeof(phba->BIOSVersion));
 6020	phba->BIOSVersion[sizeof(phba->BIOSVersion) - 1] = '\0';
 6021
 6022	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 6023			"3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s, "
 6024			"flash_id: x%02x, asic_rev: x%02x\n",
 6025			phba->sli4_hba.lnk_info.lnk_tp,
 6026			phba->sli4_hba.lnk_info.lnk_no,
 6027			phba->BIOSVersion, phba->sli4_hba.flash_id,
 6028			phba->sli4_hba.asic_rev);
 6029out_free_mboxq:
 6030	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
 6031		lpfc_sli4_mbox_cmd_free(phba, mboxq);
 6032	else
 6033		mempool_free(mboxq, phba->mbox_mem_pool);
 6034	return rc;
 6035}
 6036
 6037/**
 6038 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
 6039 * @phba: pointer to lpfc hba data structure.
 6040 *
 6041 * This routine retrieves SLI4 device physical port name this PCI function
 6042 * is attached to.
 6043 *
 6044 * Return codes
 6045 *      0 - successful
 6046 *      otherwise - failed to retrieve physical port name
 6047 **/
 6048static int
 6049lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
 6050{
 6051	LPFC_MBOXQ_t *mboxq;
 6052	struct lpfc_mbx_get_port_name *get_port_name;
 6053	uint32_t shdr_status, shdr_add_status;
 6054	union lpfc_sli4_cfg_shdr *shdr;
 6055	char cport_name = 0;
 6056	int rc;
 6057
 6058	/* We assume nothing at this point */
 6059	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
 6060	phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
 6061
 6062	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 6063	if (!mboxq)
 6064		return -ENOMEM;
 6065	/* obtain link type and link number via READ_CONFIG */
 6066	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
 6067	lpfc_sli4_read_config(phba);
 6068
 6069	if (phba->sli4_hba.fawwpn_flag & LPFC_FAWWPN_CONFIG)
 6070		phba->sli4_hba.fawwpn_flag |= LPFC_FAWWPN_FABRIC;
 6071
 6072	if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
 6073		goto retrieve_ppname;
 6074
 6075	/* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
 6076	rc = lpfc_sli4_get_ctl_attr(phba);
 6077	if (rc)
 6078		goto out_free_mboxq;
 6079
 6080retrieve_ppname:
 6081	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
 6082		LPFC_MBOX_OPCODE_GET_PORT_NAME,
 6083		sizeof(struct lpfc_mbx_get_port_name) -
 6084		sizeof(struct lpfc_sli4_cfg_mhdr),
 6085		LPFC_SLI4_MBX_EMBED);
 6086	get_port_name = &mboxq->u.mqe.un.get_port_name;
 6087	shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
 6088	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
 6089	bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
 6090		phba->sli4_hba.lnk_info.lnk_tp);
 6091	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 6092	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
 6093	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
 6094	if (shdr_status || shdr_add_status || rc) {
 6095		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
 6096				"3087 Mailbox x%x (x%x/x%x) failed: "
 6097				"rc:x%x, status:x%x, add_status:x%x\n",
 6098				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
 6099				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
 6100				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
 6101				rc, shdr_status, shdr_add_status);
 6102		rc = -ENXIO;
 6103		goto out_free_mboxq;
 6104	}
 6105	switch (phba->sli4_hba.lnk_info.lnk_no) {
 6106	case LPFC_LINK_NUMBER_0:
 6107		cport_name = bf_get(lpfc_mbx_get_port_name_name0,
 6108				&get_port_name->u.response);
 6109		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
 6110		break;
 6111	case LPFC_LINK_NUMBER_1:
 6112		cport_name = bf_get(lpfc_mbx_get_port_name_name1,
 6113				&get_port_name->u.response);
 6114		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
 6115		break;
 6116	case LPFC_LINK_NUMBER_2:
 6117		cport_name = bf_get(lpfc_mbx_get_port_name_name2,
 6118				&get_port_name->u.response);
 6119		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
 6120		break;
 6121	case LPFC_LINK_NUMBER_3:
 6122		cport_name = bf_get(lpfc_mbx_get_port_name_name3,
 6123				&get_port_name->u.response);
 6124		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
 6125		break;
 6126	default:
 6127		break;
 6128	}
 6129
 6130	if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
 6131		phba->Port[0] = cport_name;
 6132		phba->Port[1] = '\0';
 6133		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 6134				"3091 SLI get port name: %s\n", phba->Port);
 6135	}
 6136
 6137out_free_mboxq:
 6138	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
 6139		lpfc_sli4_mbox_cmd_free(phba, mboxq);
 6140	else
 6141		mempool_free(mboxq, phba->mbox_mem_pool);
 6142	return rc;
 6143}
 6144
 6145/**
 6146 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
 6147 * @phba: pointer to lpfc hba data structure.
 6148 *
 6149 * This routine is called to explicitly arm the SLI4 device's completion and
 6150 * event queues
 6151 **/
 6152static void
 6153lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
 6154{
 6155	int qidx;
 6156	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
 6157	struct lpfc_sli4_hdw_queue *qp;
 6158	struct lpfc_queue *eq;
 6159
 6160	sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
 6161	sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
 6162	if (sli4_hba->nvmels_cq)
 6163		sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
 6164					   LPFC_QUEUE_REARM);
 6165
 6166	if (sli4_hba->hdwq) {
 6167		/* Loop thru all Hardware Queues */
 6168		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
 6169			qp = &sli4_hba->hdwq[qidx];
 6170			/* ARM the corresponding CQ */
 6171			sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
 6172						LPFC_QUEUE_REARM);
 6173		}
 6174
 6175		/* Loop thru all IRQ vectors */
 6176		for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
 6177			eq = sli4_hba->hba_eq_hdl[qidx].eq;
 6178			/* ARM the corresponding EQ */
 6179			sli4_hba->sli4_write_eq_db(phba, eq,
 6180						   0, LPFC_QUEUE_REARM);
 6181		}
 6182	}
 6183
 6184	if (phba->nvmet_support) {
 6185		for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
 6186			sli4_hba->sli4_write_cq_db(phba,
 6187				sli4_hba->nvmet_cqset[qidx], 0,
 6188				LPFC_QUEUE_REARM);
 6189		}
 6190	}
 6191}
 6192
 6193/**
 6194 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
 6195 * @phba: Pointer to HBA context object.
 6196 * @type: The resource extent type.
 6197 * @extnt_count: buffer to hold port available extent count.
 6198 * @extnt_size: buffer to hold element count per extent.
 6199 *
 6200 * This function calls the port and retrievs the number of available
 6201 * extents and their size for a particular extent type.
 6202 *
 6203 * Returns: 0 if successful.  Nonzero otherwise.
 6204 **/
 6205int
 6206lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
 6207			       uint16_t *extnt_count, uint16_t *extnt_size)
 6208{
 6209	int rc = 0;
 6210	uint32_t length;
 6211	uint32_t mbox_tmo;
 6212	struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
 6213	LPFC_MBOXQ_t *mbox;
 6214
 6215	*extnt_count = 0;
 6216	*extnt_size = 0;
 6217
 6218	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 6219	if (!mbox)
 6220		return -ENOMEM;
 6221
 6222	/* Find out how many extents are available for this resource type */
 6223	length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
 6224		  sizeof(struct lpfc_sli4_cfg_mhdr));
 6225	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
 6226			 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
 6227			 length, LPFC_SLI4_MBX_EMBED);
 6228
 6229	/* Send an extents count of 0 - the GET doesn't use it. */
 6230	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
 6231					LPFC_SLI4_MBX_EMBED);
 6232	if (unlikely(rc)) {
 6233		rc = -EIO;
 6234		goto err_exit;
 6235	}
 6236
 6237	if (!phba->sli4_hba.intr_enable)
 6238		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
 6239	else {
 6240		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
 6241		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
 6242	}
 6243	if (unlikely(rc)) {
 6244		rc = -EIO;
 6245		goto err_exit;
 6246	}
 6247
 6248	rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
 6249	if (bf_get(lpfc_mbox_hdr_status,
 6250		   &rsrc_info->header.cfg_shdr.response)) {
 6251		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 6252				"2930 Failed to get resource extents "
 6253				"Status 0x%x Add'l Status 0x%x\n",
 6254				bf_get(lpfc_mbox_hdr_status,
 6255				       &rsrc_info->header.cfg_shdr.response),
 6256				bf_get(lpfc_mbox_hdr_add_status,
 6257				       &rsrc_info->header.cfg_shdr.response));
 6258		rc = -EIO;
 6259		goto err_exit;
 6260	}
 6261
 6262	*extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
 6263			      &rsrc_info->u.rsp);
 6264	*extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
 6265			     &rsrc_info->u.rsp);
 6266
 6267	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
 6268			"3162 Retrieved extents type-%d from port: count:%d, "
 6269			"size:%d\n", type, *extnt_count, *extnt_size);
 6270
 6271err_exit:
 6272	mempool_free(mbox, phba->mbox_mem_pool);
 6273	return rc;
 6274}
 6275
 6276/**
 6277 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
 6278 * @phba: Pointer to HBA context object.
 6279 * @type: The extent type to check.
 6280 *
 6281 * This function reads the current available extents from the port and checks
 6282 * if the extent count or extent size has changed since the last access.
 6283 * Callers use this routine post port reset to understand if there is a
 6284 * extent reprovisioning requirement.
 6285 *
 6286 * Returns:
 6287 *   -Error: error indicates problem.
 6288 *   1: Extent count or size has changed.
 6289 *   0: No changes.
 6290 **/
 6291static int
 6292lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
 6293{
 6294	uint16_t curr_ext_cnt, rsrc_ext_cnt;
 6295	uint16_t size_diff, rsrc_ext_size;
 6296	int rc = 0;
 6297	struct lpfc_rsrc_blks *rsrc_entry;
 6298	struct list_head *rsrc_blk_list = NULL;
 6299
 6300	size_diff = 0;
 6301	curr_ext_cnt = 0;
 6302	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
 6303					    &rsrc_ext_cnt,
 6304					    &rsrc_ext_size);
 6305	if (unlikely(rc))
 6306		return -EIO;
 6307
 6308	switch (type) {
 6309	case LPFC_RSC_TYPE_FCOE_RPI:
 6310		rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
 6311		break;
 6312	case LPFC_RSC_TYPE_FCOE_VPI:
 6313		rsrc_blk_list = &phba->lpfc_vpi_blk_list;
 6314		break;
 6315	case LPFC_RSC_TYPE_FCOE_XRI:
 6316		rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
 6317		break;
 6318	case LPFC_RSC_TYPE_FCOE_VFI:
 6319		rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
 6320		break;
 6321	default:
 6322		break;
 6323	}
 6324
 6325	list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
 6326		curr_ext_cnt++;
 6327		if (rsrc_entry->rsrc_size != rsrc_ext_size)
 6328			size_diff++;
 6329	}
 6330
 6331	if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
 6332		rc = 1;
 6333
 6334	return rc;
 6335}
 6336
 6337/**
 6338 * lpfc_sli4_cfg_post_extnts -
 6339 * @phba: Pointer to HBA context object.
 6340 * @extnt_cnt: number of available extents.
 6341 * @type: the extent type (rpi, xri, vfi, vpi).
 6342 * @emb: buffer to hold either MBX_EMBED or MBX_NEMBED operation.
 6343 * @mbox: pointer to the caller's allocated mailbox structure.
 6344 *
 6345 * This function executes the extents allocation request.  It also
 6346 * takes care of the amount of memory needed to allocate or get the
 6347 * allocated extents. It is the caller's responsibility to evaluate
 6348 * the response.
 6349 *
 6350 * Returns:
 6351 *   -Error:  Error value describes the condition found.
 6352 *   0: if successful
 6353 **/
 6354static int
 6355lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
 6356			  uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
 6357{
 6358	int rc = 0;
 6359	uint32_t req_len;
 6360	uint32_t emb_len;
 6361	uint32_t alloc_len, mbox_tmo;
 6362
 6363	/* Calculate the total requested length of the dma memory */
 6364	req_len = extnt_cnt * sizeof(uint16_t);
 6365
 6366	/*
 6367	 * Calculate the size of an embedded mailbox.  The uint32_t
 6368	 * accounts for extents-specific word.
 6369	 */
 6370	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
 6371		sizeof(uint32_t);
 6372
 6373	/*
 6374	 * Presume the allocation and response will fit into an embedded
 6375	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.
 6376	 */
 6377	*emb = LPFC_SLI4_MBX_EMBED;
 6378	if (req_len > emb_len) {
 6379		req_len = extnt_cnt * sizeof(uint16_t) +
 6380			sizeof(union lpfc_sli4_cfg_shdr) +
 6381			sizeof(uint32_t);
 6382		*emb = LPFC_SLI4_MBX_NEMBED;
 6383	}
 6384
 6385	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
 6386				     LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
 6387				     req_len, *emb);
 6388	if (alloc_len < req_len) {
 6389		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 6390			"2982 Allocated DMA memory size (x%x) is "
 6391			"less than the requested DMA memory "
 6392			"size (x%x)\n", alloc_len, req_len);
 6393		return -ENOMEM;
 6394	}
 6395	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
 6396	if (unlikely(rc))
 6397		return -EIO;
 6398
 6399	if (!phba->sli4_hba.intr_enable)
 6400		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
 6401	else {
 6402		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
 6403		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
 6404	}
 6405
 6406	if (unlikely(rc))
 6407		rc = -EIO;
 6408	return rc;
 6409}
 6410
 6411/**
 6412 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
 6413 * @phba: Pointer to HBA context object.
 6414 * @type:  The resource extent type to allocate.
 6415 *
 6416 * This function allocates the number of elements for the specified
 6417 * resource type.
 6418 **/
 6419static int
 6420lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
 6421{
 6422	bool emb = false;
 6423	uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
 6424	uint16_t rsrc_id, rsrc_start, j, k;
 6425	uint16_t *ids;
 6426	int i, rc;
 6427	unsigned long longs;
 6428	unsigned long *bmask;
 6429	struct lpfc_rsrc_blks *rsrc_blks;
 6430	LPFC_MBOXQ_t *mbox;
 6431	uint32_t length;
 6432	struct lpfc_id_range *id_array = NULL;
 6433	void *virtaddr = NULL;
 6434	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
 6435	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
 6436	struct list_head *ext_blk_list;
 6437
 6438	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
 6439					    &rsrc_cnt,
 6440					    &rsrc_size);
 6441	if (unlikely(rc))
 6442		return -EIO;
 6443
 6444	if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
 6445		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 6446			"3009 No available Resource Extents "
 6447			"for resource type 0x%x: Count: 0x%x, "
 6448			"Size 0x%x\n", type, rsrc_cnt,
 6449			rsrc_size);
 6450		return -ENOMEM;
 6451	}
 6452
 6453	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
 6454			"2903 Post resource extents type-0x%x: "
 6455			"count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
 6456
 6457	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 6458	if (!mbox)
 6459		return -ENOMEM;
 6460
 6461	rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
 6462	if (unlikely(rc)) {
 6463		rc = -EIO;
 6464		goto err_exit;
 6465	}
 6466
 6467	/*
 6468	 * Figure out where the response is located.  Then get local pointers
 6469	 * to the response data.  The port does not guarantee to respond to
 6470	 * all extents counts request so update the local variable with the
 6471	 * allocated count from the port.
 6472	 */
 6473	if (emb == LPFC_SLI4_MBX_EMBED) {
 6474		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
 6475		id_array = &rsrc_ext->u.rsp.id[0];
 6476		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
 6477	} else {
 6478		virtaddr = mbox->sge_array->addr[0];
 6479		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
 6480		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
 6481		id_array = &n_rsrc->id;
 6482	}
 6483
 6484	longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
 6485	rsrc_id_cnt = rsrc_cnt * rsrc_size;
 6486
 6487	/*
 6488	 * Based on the resource size and count, correct the base and max
 6489	 * resource values.
 6490	 */
 6491	length = sizeof(struct lpfc_rsrc_blks);
 6492	switch (type) {
 6493	case LPFC_RSC_TYPE_FCOE_RPI:
 6494		phba->sli4_hba.rpi_bmask = kcalloc(longs,
 6495						   sizeof(unsigned long),
 6496						   GFP_KERNEL);
 6497		if (unlikely(!phba->sli4_hba.rpi_bmask)) {
 6498			rc = -ENOMEM;
 6499			goto err_exit;
 6500		}
 6501		phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
 6502						 sizeof(uint16_t),
 6503						 GFP_KERNEL);
 6504		if (unlikely(!phba->sli4_hba.rpi_ids)) {
 6505			kfree(phba->sli4_hba.rpi_bmask);
 6506			rc = -ENOMEM;
 6507			goto err_exit;
 6508		}
 6509
 6510		/*
 6511		 * The next_rpi was initialized with the maximum available
 6512		 * count but the port may allocate a smaller number.  Catch
 6513		 * that case and update the next_rpi.
 6514		 */
 6515		phba->sli4_hba.next_rpi = rsrc_id_cnt;
 6516
 6517		/* Initialize local ptrs for common extent processing later. */
 6518		bmask = phba->sli4_hba.rpi_bmask;
 6519		ids = phba->sli4_hba.rpi_ids;
 6520		ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
 6521		break;
 6522	case LPFC_RSC_TYPE_FCOE_VPI:
 6523		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
 6524					  GFP_KERNEL);
 6525		if (unlikely(!phba->vpi_bmask)) {
 6526			rc = -ENOMEM;
 6527			goto err_exit;
 6528		}
 6529		phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
 6530					 GFP_KERNEL);
 6531		if (unlikely(!phba->vpi_ids)) {
 6532			kfree(phba->vpi_bmask);
 6533			rc = -ENOMEM;
 6534			goto err_exit;
 6535		}
 6536
 6537		/* Initialize local ptrs for common extent processing later. */
 6538		bmask = phba->vpi_bmask;
 6539		ids = phba->vpi_ids;
 6540		ext_blk_list = &phba->lpfc_vpi_blk_list;
 6541		break;
 6542	case LPFC_RSC_TYPE_FCOE_XRI:
 6543		phba->sli4_hba.xri_bmask = kcalloc(longs,
 6544						   sizeof(unsigned long),
 6545						   GFP_KERNEL);
 6546		if (unlikely(!phba->sli4_hba.xri_bmask)) {
 6547			rc = -ENOMEM;
 6548			goto err_exit;
 6549		}
 6550		phba->sli4_hba.max_cfg_param.xri_used = 0;
 6551		phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
 6552						 sizeof(uint16_t),
 6553						 GFP_KERNEL);
 6554		if (unlikely(!phba->sli4_hba.xri_ids)) {
 6555			kfree(phba->sli4_hba.xri_bmask);
 6556			rc = -ENOMEM;
 6557			goto err_exit;
 6558		}
 6559
 6560		/* Initialize local ptrs for common extent processing later. */
 6561		bmask = phba->sli4_hba.xri_bmask;
 6562		ids = phba->sli4_hba.xri_ids;
 6563		ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
 6564		break;
 6565	case LPFC_RSC_TYPE_FCOE_VFI:
 6566		phba->sli4_hba.vfi_bmask = kcalloc(longs,
 6567						   sizeof(unsigned long),
 6568						   GFP_KERNEL);
 6569		if (unlikely(!phba->sli4_hba.vfi_bmask)) {
 6570			rc = -ENOMEM;
 6571			goto err_exit;
 6572		}
 6573		phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
 6574						 sizeof(uint16_t),
 6575						 GFP_KERNEL);
 6576		if (unlikely(!phba->sli4_hba.vfi_ids)) {
 6577			kfree(phba->sli4_hba.vfi_bmask);
 6578			rc = -ENOMEM;
 6579			goto err_exit;
 6580		}
 6581
 6582		/* Initialize local ptrs for common extent processing later. */
 6583		bmask = phba->sli4_hba.vfi_bmask;
 6584		ids = phba->sli4_hba.vfi_ids;
 6585		ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
 6586		break;
 6587	default:
 6588		/* Unsupported Opcode.  Fail call. */
 6589		id_array = NULL;
 6590		bmask = NULL;
 6591		ids = NULL;
 6592		ext_blk_list = NULL;
 6593		goto err_exit;
 6594	}
 6595
 6596	/*
 6597	 * Complete initializing the extent configuration with the
 6598	 * allocated ids assigned to this function.  The bitmask serves
 6599	 * as an index into the array and manages the available ids.  The
 6600	 * array just stores the ids communicated to the port via the wqes.
 6601	 */
 6602	for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
 6603		if ((i % 2) == 0)
 6604			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
 6605					 &id_array[k]);
 6606		else
 6607			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
 6608					 &id_array[k]);
 6609
 6610		rsrc_blks = kzalloc(length, GFP_KERNEL);
 6611		if (unlikely(!rsrc_blks)) {
 6612			rc = -ENOMEM;
 6613			kfree(bmask);
 6614			kfree(ids);
 6615			goto err_exit;
 6616		}
 6617		rsrc_blks->rsrc_start = rsrc_id;
 6618		rsrc_blks->rsrc_size = rsrc_size;
 6619		list_add_tail(&rsrc_blks->list, ext_blk_list);
 6620		rsrc_start = rsrc_id;
 6621		if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
 6622			phba->sli4_hba.io_xri_start = rsrc_start +
 6623				lpfc_sli4_get_iocb_cnt(phba);
 6624		}
 6625
 6626		while (rsrc_id < (rsrc_start + rsrc_size)) {
 6627			ids[j] = rsrc_id;
 6628			rsrc_id++;
 6629			j++;
 6630		}
 6631		/* Entire word processed.  Get next word.*/
 6632		if ((i % 2) == 1)
 6633			k++;
 6634	}
 6635 err_exit:
 6636	lpfc_sli4_mbox_cmd_free(phba, mbox);
 6637	return rc;
 6638}
 6639
 6640
 6641
 6642/**
 6643 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
 6644 * @phba: Pointer to HBA context object.
 6645 * @type: the extent's type.
 6646 *
 6647 * This function deallocates all extents of a particular resource type.
 6648 * SLI4 does not allow for deallocating a particular extent range.  It
 6649 * is the caller's responsibility to release all kernel memory resources.
 6650 **/
 6651static int
 6652lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
 6653{
 6654	int rc;
 6655	uint32_t length, mbox_tmo = 0;
 6656	LPFC_MBOXQ_t *mbox;
 6657	struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
 6658	struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
 6659
 6660	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 6661	if (!mbox)
 6662		return -ENOMEM;
 6663
 6664	/*
 6665	 * This function sends an embedded mailbox because it only sends the
 6666	 * the resource type.  All extents of this type are released by the
 6667	 * port.
 6668	 */
 6669	length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
 6670		  sizeof(struct lpfc_sli4_cfg_mhdr));
 6671	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
 6672			 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
 6673			 length, LPFC_SLI4_MBX_EMBED);
 6674
 6675	/* Send an extents count of 0 - the dealloc doesn't use it. */
 6676	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
 6677					LPFC_SLI4_MBX_EMBED);
 6678	if (unlikely(rc)) {
 6679		rc = -EIO;
 6680		goto out_free_mbox;
 6681	}
 6682	if (!phba->sli4_hba.intr_enable)
 6683		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
 6684	else {
 6685		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
 6686		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
 6687	}
 6688	if (unlikely(rc)) {
 6689		rc = -EIO;
 6690		goto out_free_mbox;
 6691	}
 6692
 6693	dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
 6694	if (bf_get(lpfc_mbox_hdr_status,
 6695		   &dealloc_rsrc->header.cfg_shdr.response)) {
 6696		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 6697				"2919 Failed to release resource extents "
 6698				"for type %d - Status 0x%x Add'l Status 0x%x. "
 6699				"Resource memory not released.\n",
 6700				type,
 6701				bf_get(lpfc_mbox_hdr_status,
 6702				    &dealloc_rsrc->header.cfg_shdr.response),
 6703				bf_get(lpfc_mbox_hdr_add_status,
 6704				    &dealloc_rsrc->header.cfg_shdr.response));
 6705		rc = -EIO;
 6706		goto out_free_mbox;
 6707	}
 6708
 6709	/* Release kernel memory resources for the specific type. */
 6710	switch (type) {
 6711	case LPFC_RSC_TYPE_FCOE_VPI:
 6712		kfree(phba->vpi_bmask);
 6713		kfree(phba->vpi_ids);
 6714		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
 6715		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
 6716				    &phba->lpfc_vpi_blk_list, list) {
 6717			list_del_init(&rsrc_blk->list);
 6718			kfree(rsrc_blk);
 6719		}
 6720		phba->sli4_hba.max_cfg_param.vpi_used = 0;
 6721		break;
 6722	case LPFC_RSC_TYPE_FCOE_XRI:
 6723		kfree(phba->sli4_hba.xri_bmask);
 6724		kfree(phba->sli4_hba.xri_ids);
 6725		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
 6726				    &phba->sli4_hba.lpfc_xri_blk_list, list) {
 6727			list_del_init(&rsrc_blk->list);
 6728			kfree(rsrc_blk);
 6729		}
 6730		break;
 6731	case LPFC_RSC_TYPE_FCOE_VFI:
 6732		kfree(phba->sli4_hba.vfi_bmask);
 6733		kfree(phba->sli4_hba.vfi_ids);
 6734		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
 6735		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
 6736				    &phba->sli4_hba.lpfc_vfi_blk_list, list) {
 6737			list_del_init(&rsrc_blk->list);
 6738			kfree(rsrc_blk);
 6739		}
 6740		break;
 6741	case LPFC_RSC_TYPE_FCOE_RPI:
 6742		/* RPI bitmask and physical id array are cleaned up earlier. */
 6743		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
 6744				    &phba->sli4_hba.lpfc_rpi_blk_list, list) {
 6745			list_del_init(&rsrc_blk->list);
 6746			kfree(rsrc_blk);
 6747		}
 6748		break;
 6749	default:
 6750		break;
 6751	}
 6752
 6753	bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
 6754
 6755 out_free_mbox:
 6756	mempool_free(mbox, phba->mbox_mem_pool);
 6757	return rc;
 6758}
 6759
 6760static void
 6761lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
 6762		  uint32_t feature)
 6763{
 6764	uint32_t len;
 6765	u32 sig_freq = 0;
 6766
 6767	len = sizeof(struct lpfc_mbx_set_feature) -
 6768		sizeof(struct lpfc_sli4_cfg_mhdr);
 6769	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
 6770			 LPFC_MBOX_OPCODE_SET_FEATURES, len,
 6771			 LPFC_SLI4_MBX_EMBED);
 6772
 6773	switch (feature) {
 6774	case LPFC_SET_UE_RECOVERY:
 6775		bf_set(lpfc_mbx_set_feature_UER,
 6776		       &mbox->u.mqe.un.set_feature, 1);
 6777		mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
 6778		mbox->u.mqe.un.set_feature.param_len = 8;
 6779		break;
 6780	case LPFC_SET_MDS_DIAGS:
 6781		bf_set(lpfc_mbx_set_feature_mds,
 6782		       &mbox->u.mqe.un.set_feature, 1);
 6783		bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
 6784		       &mbox->u.mqe.un.set_feature, 1);
 6785		mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
 6786		mbox->u.mqe.un.set_feature.param_len = 8;
 6787		break;
 6788	case LPFC_SET_CGN_SIGNAL:
 6789		if (phba->cmf_active_mode == LPFC_CFG_OFF)
 6790			sig_freq = 0;
 6791		else
 6792			sig_freq = phba->cgn_sig_freq;
 6793
 6794		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
 6795			bf_set(lpfc_mbx_set_feature_CGN_alarm_freq,
 6796			       &mbox->u.mqe.un.set_feature, sig_freq);
 6797			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
 6798			       &mbox->u.mqe.un.set_feature, sig_freq);
 6799		}
 6800
 6801		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY)
 6802			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
 6803			       &mbox->u.mqe.un.set_feature, sig_freq);
 6804
 6805		if (phba->cmf_active_mode == LPFC_CFG_OFF ||
 6806		    phba->cgn_reg_signal == EDC_CG_SIG_NOTSUPPORTED)
 6807			sig_freq = 0;
 6808		else
 6809			sig_freq = lpfc_acqe_cgn_frequency;
 6810
 6811		bf_set(lpfc_mbx_set_feature_CGN_acqe_freq,
 6812		       &mbox->u.mqe.un.set_feature, sig_freq);
 6813
 6814		mbox->u.mqe.un.set_feature.feature = LPFC_SET_CGN_SIGNAL;
 6815		mbox->u.mqe.un.set_feature.param_len = 12;
 6816		break;
 6817	case LPFC_SET_DUAL_DUMP:
 6818		bf_set(lpfc_mbx_set_feature_dd,
 6819		       &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
 6820		bf_set(lpfc_mbx_set_feature_ddquery,
 6821		       &mbox->u.mqe.un.set_feature, 0);
 6822		mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
 6823		mbox->u.mqe.un.set_feature.param_len = 4;
 6824		break;
 6825	case LPFC_SET_ENABLE_MI:
 6826		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_MI;
 6827		mbox->u.mqe.un.set_feature.param_len = 4;
 6828		bf_set(lpfc_mbx_set_feature_milunq, &mbox->u.mqe.un.set_feature,
 6829		       phba->pport->cfg_lun_queue_depth);
 6830		bf_set(lpfc_mbx_set_feature_mi, &mbox->u.mqe.un.set_feature,
 6831		       phba->sli4_hba.pc_sli4_params.mi_ver);
 6832		break;
 6833	case LPFC_SET_LD_SIGNAL:
 6834		mbox->u.mqe.un.set_feature.feature = LPFC_SET_LD_SIGNAL;
 6835		mbox->u.mqe.un.set_feature.param_len = 16;
 6836		bf_set(lpfc_mbx_set_feature_lds_qry,
 6837		       &mbox->u.mqe.un.set_feature, LPFC_QUERY_LDS_OP);
 6838		break;
 6839	case LPFC_SET_ENABLE_CMF:
 6840		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_CMF;
 6841		mbox->u.mqe.un.set_feature.param_len = 4;
 6842		bf_set(lpfc_mbx_set_feature_cmf,
 6843		       &mbox->u.mqe.un.set_feature, 1);
 6844		break;
 6845	}
 6846	return;
 6847}
 6848
 6849/**
 6850 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
 6851 * @phba: Pointer to HBA context object.
 6852 *
 6853 * Disable FW logging into host memory on the adapter. To
 6854 * be done before reading logs from the host memory.
 6855 **/
 6856void
 6857lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
 6858{
 6859	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
 6860
 6861	spin_lock_irq(&phba->ras_fwlog_lock);
 6862	ras_fwlog->state = INACTIVE;
 6863	spin_unlock_irq(&phba->ras_fwlog_lock);
 6864
 6865	/* Disable FW logging to host memory */
 6866	writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
 6867	       phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
 6868
 6869	/* Wait 10ms for firmware to stop using DMA buffer */
 6870	usleep_range(10 * 1000, 20 * 1000);
 6871}
 6872
 6873/**
 6874 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
 6875 * @phba: Pointer to HBA context object.
 6876 *
 6877 * This function is called to free memory allocated for RAS FW logging
 6878 * support in the driver.
 6879 **/
 6880void
 6881lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
 6882{
 6883	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
 6884	struct lpfc_dmabuf *dmabuf, *next;
 6885
 6886	if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
 6887		list_for_each_entry_safe(dmabuf, next,
 6888				    &ras_fwlog->fwlog_buff_list,
 6889				    list) {
 6890			list_del(&dmabuf->list);
 6891			dma_free_coherent(&phba->pcidev->dev,
 6892					  LPFC_RAS_MAX_ENTRY_SIZE,
 6893					  dmabuf->virt, dmabuf->phys);
 6894			kfree(dmabuf);
 6895		}
 6896	}
 6897
 6898	if (ras_fwlog->lwpd.virt) {
 6899		dma_free_coherent(&phba->pcidev->dev,
 6900				  sizeof(uint32_t) * 2,
 6901				  ras_fwlog->lwpd.virt,
 6902				  ras_fwlog->lwpd.phys);
 6903		ras_fwlog->lwpd.virt = NULL;
 6904	}
 6905
 6906	spin_lock_irq(&phba->ras_fwlog_lock);
 6907	ras_fwlog->state = INACTIVE;
 6908	spin_unlock_irq(&phba->ras_fwlog_lock);
 6909}
 6910
 6911/**
 6912 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
 6913 * @phba: Pointer to HBA context object.
 6914 * @fwlog_buff_count: Count of buffers to be created.
 6915 *
 6916 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
 6917 * to update FW log is posted to the adapter.
 6918 * Buffer count is calculated based on module param ras_fwlog_buffsize
 6919 * Size of each buffer posted to FW is 64K.
 6920 **/
 6921
 6922static int
 6923lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
 6924			uint32_t fwlog_buff_count)
 6925{
 6926	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
 6927	struct lpfc_dmabuf *dmabuf;
 6928	int rc = 0, i = 0;
 6929
 6930	/* Initialize List */
 6931	INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
 6932
 6933	/* Allocate memory for the LWPD */
 6934	ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
 6935					    sizeof(uint32_t) * 2,
 6936					    &ras_fwlog->lwpd.phys,
 6937					    GFP_KERNEL);
 6938	if (!ras_fwlog->lwpd.virt) {
 6939		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 6940				"6185 LWPD Memory Alloc Failed\n");
 6941
 6942		return -ENOMEM;
 6943	}
 6944
 6945	ras_fwlog->fw_buffcount = fwlog_buff_count;
 6946	for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
 6947		dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
 6948				 GFP_KERNEL);
 6949		if (!dmabuf) {
 6950			rc = -ENOMEM;
 6951			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
 6952					"6186 Memory Alloc failed FW logging");
 6953			goto free_mem;
 6954		}
 6955
 6956		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
 6957						  LPFC_RAS_MAX_ENTRY_SIZE,
 6958						  &dmabuf->phys, GFP_KERNEL);
 6959		if (!dmabuf->virt) {
 6960			kfree(dmabuf);
 6961			rc = -ENOMEM;
 6962			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
 6963					"6187 DMA Alloc Failed FW logging");
 6964			goto free_mem;
 6965		}
 6966		dmabuf->buffer_tag = i;
 6967		list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
 6968	}
 6969
 6970free_mem:
 6971	if (rc)
 6972		lpfc_sli4_ras_dma_free(phba);
 6973
 6974	return rc;
 6975}
 6976
 6977/**
 6978 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
 6979 * @phba: pointer to lpfc hba data structure.
 6980 * @pmb: pointer to the driver internal queue element for mailbox command.
 6981 *
 6982 * Completion handler for driver's RAS MBX command to the device.
 6983 **/
 6984static void
 6985lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 6986{
 6987	MAILBOX_t *mb;
 6988	union lpfc_sli4_cfg_shdr *shdr;
 6989	uint32_t shdr_status, shdr_add_status;
 6990	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
 6991
 6992	mb = &pmb->u.mb;
 6993
 6994	shdr = (union lpfc_sli4_cfg_shdr *)
 6995		&pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
 6996	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
 6997	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
 6998
 6999	if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
 7000		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 7001				"6188 FW LOG mailbox "
 7002				"completed with status x%x add_status x%x,"
 7003				" mbx status x%x\n",
 7004				shdr_status, shdr_add_status, mb->mbxStatus);
 7005
 7006		ras_fwlog->ras_hwsupport = false;
 7007		goto disable_ras;
 7008	}
 7009
 7010	spin_lock_irq(&phba->ras_fwlog_lock);
 7011	ras_fwlog->state = ACTIVE;
 7012	spin_unlock_irq(&phba->ras_fwlog_lock);
 7013	mempool_free(pmb, phba->mbox_mem_pool);
 7014
 7015	return;
 7016
 7017disable_ras:
 7018	/* Free RAS DMA memory */
 7019	lpfc_sli4_ras_dma_free(phba);
 7020	mempool_free(pmb, phba->mbox_mem_pool);
 7021}
 7022
 7023/**
 7024 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
 7025 * @phba: pointer to lpfc hba data structure.
 7026 * @fwlog_level: Logging verbosity level.
 7027 * @fwlog_enable: Enable/Disable logging.
 7028 *
 7029 * Initialize memory and post mailbox command to enable FW logging in host
 7030 * memory.
 7031 **/
 7032int
 7033lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
 7034			 uint32_t fwlog_level,
 7035			 uint32_t fwlog_enable)
 7036{
 7037	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
 7038	struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
 7039	struct lpfc_dmabuf *dmabuf;
 7040	LPFC_MBOXQ_t *mbox;
 7041	uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
 7042	int rc = 0;
 7043
 7044	spin_lock_irq(&phba->ras_fwlog_lock);
 7045	ras_fwlog->state = INACTIVE;
 7046	spin_unlock_irq(&phba->ras_fwlog_lock);
 7047
 7048	fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
 7049			  phba->cfg_ras_fwlog_buffsize);
 7050	fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
 7051
 7052	/*
 7053	 * If re-enabling FW logging support use earlier allocated
 7054	 * DMA buffers while posting MBX command.
 7055	 **/
 7056	if (!ras_fwlog->lwpd.virt) {
 7057		rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
 7058		if (rc) {
 7059			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
 7060					"6189 FW Log Memory Allocation Failed");
 7061			return rc;
 7062		}
 7063	}
 7064
 7065	/* Setup Mailbox command */
 7066	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 7067	if (!mbox) {
 7068		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 7069				"6190 RAS MBX Alloc Failed");
 7070		rc = -ENOMEM;
 7071		goto mem_free;
 7072	}
 7073
 7074	ras_fwlog->fw_loglevel = fwlog_level;
 7075	len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
 7076		sizeof(struct lpfc_sli4_cfg_mhdr));
 7077
 7078	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
 7079			 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
 7080			 len, LPFC_SLI4_MBX_EMBED);
 7081
 7082	mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
 7083	bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
 7084	       fwlog_enable);
 7085	bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
 7086	       ras_fwlog->fw_loglevel);
 7087	bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
 7088	       ras_fwlog->fw_buffcount);
 7089	bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
 7090	       LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
 7091
 7092	/* Update DMA buffer address */
 7093	list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
 7094		memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
 7095
 7096		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
 7097			putPaddrLow(dmabuf->phys);
 7098
 7099		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
 7100			putPaddrHigh(dmabuf->phys);
 7101	}
 7102
 7103	/* Update LPWD address */
 7104	mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
 7105	mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
 7106
 7107	spin_lock_irq(&phba->ras_fwlog_lock);
 7108	ras_fwlog->state = REG_INPROGRESS;
 7109	spin_unlock_irq(&phba->ras_fwlog_lock);
 7110	mbox->vport = phba->pport;
 7111	mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
 7112
 7113	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
 7114
 7115	if (rc == MBX_NOT_FINISHED) {
 7116		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 7117				"6191 FW-Log Mailbox failed. "
 7118				"status %d mbxStatus : x%x", rc,
 7119				bf_get(lpfc_mqe_status, &mbox->u.mqe));
 7120		mempool_free(mbox, phba->mbox_mem_pool);
 7121		rc = -EIO;
 7122		goto mem_free;
 7123	} else
 7124		rc = 0;
 7125mem_free:
 7126	if (rc)
 7127		lpfc_sli4_ras_dma_free(phba);
 7128
 7129	return rc;
 7130}
 7131
 7132/**
 7133 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
 7134 * @phba: Pointer to HBA context object.
 7135 *
 7136 * Check if RAS is supported on the adapter and initialize it.
 7137 **/
 7138void
 7139lpfc_sli4_ras_setup(struct lpfc_hba *phba)
 7140{
 7141	/* Check RAS FW Log needs to be enabled or not */
 7142	if (lpfc_check_fwlog_support(phba))
 7143		return;
 7144
 7145	lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
 7146				 LPFC_RAS_ENABLE_LOGGING);
 7147}
 7148
 7149/**
 7150 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
 7151 * @phba: Pointer to HBA context object.
 7152 *
 7153 * This function allocates all SLI4 resource identifiers.
 7154 **/
 7155int
 7156lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
 7157{
 7158	int i, rc, error = 0;
 7159	uint16_t count, base;
 7160	unsigned long longs;
 7161
 7162	if (!phba->sli4_hba.rpi_hdrs_in_use)
 7163		phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
 7164	if (phba->sli4_hba.extents_in_use) {
 7165		/*
 7166		 * The port supports resource extents. The XRI, VPI, VFI, RPI
 7167		 * resource extent count must be read and allocated before
 7168		 * provisioning the resource id arrays.
 7169		 */
 7170		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
 7171		    LPFC_IDX_RSRC_RDY) {
 7172			/*
 7173			 * Extent-based resources are set - the driver could
 7174			 * be in a port reset. Figure out if any corrective
 7175			 * actions need to be taken.
 7176			 */
 7177			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
 7178						 LPFC_RSC_TYPE_FCOE_VFI);
 7179			if (rc != 0)
 7180				error++;
 7181			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
 7182						 LPFC_RSC_TYPE_FCOE_VPI);
 7183			if (rc != 0)
 7184				error++;
 7185			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
 7186						 LPFC_RSC_TYPE_FCOE_XRI);
 7187			if (rc != 0)
 7188				error++;
 7189			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
 7190						 LPFC_RSC_TYPE_FCOE_RPI);
 7191			if (rc != 0)
 7192				error++;
 7193
 7194			/*
 7195			 * It's possible that the number of resources
 7196			 * provided to this port instance changed between
 7197			 * resets.  Detect this condition and reallocate
 7198			 * resources.  Otherwise, there is no action.
 7199			 */
 7200			if (error) {
 7201				lpfc_printf_log(phba, KERN_INFO,
 7202						LOG_MBOX | LOG_INIT,
 7203						"2931 Detected extent resource "
 7204						"change.  Reallocating all "
 7205						"extents.\n");
 7206				rc = lpfc_sli4_dealloc_extent(phba,
 7207						 LPFC_RSC_TYPE_FCOE_VFI);
 7208				rc = lpfc_sli4_dealloc_extent(phba,
 7209						 LPFC_RSC_TYPE_FCOE_VPI);
 7210				rc = lpfc_sli4_dealloc_extent(phba,
 7211						 LPFC_RSC_TYPE_FCOE_XRI);
 7212				rc = lpfc_sli4_dealloc_extent(phba,
 7213						 LPFC_RSC_TYPE_FCOE_RPI);
 7214			} else
 7215				return 0;
 7216		}
 7217
 7218		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
 7219		if (unlikely(rc))
 7220			goto err_exit;
 7221
 7222		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
 7223		if (unlikely(rc))
 7224			goto err_exit;
 7225
 7226		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
 7227		if (unlikely(rc))
 7228			goto err_exit;
 7229
 7230		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
 7231		if (unlikely(rc))
 7232			goto err_exit;
 7233		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
 7234		       LPFC_IDX_RSRC_RDY);
 7235		return rc;
 7236	} else {
 7237		/*
 7238		 * The port does not support resource extents.  The XRI, VPI,
 7239		 * VFI, RPI resource ids were determined from READ_CONFIG.
 7240		 * Just allocate the bitmasks and provision the resource id
 7241		 * arrays.  If a port reset is active, the resources don't
 7242		 * need any action - just exit.
 7243		 */
 7244		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
 7245		    LPFC_IDX_RSRC_RDY) {
 7246			lpfc_sli4_dealloc_resource_identifiers(phba);
 7247			lpfc_sli4_remove_rpis(phba);
 7248		}
 7249		/* RPIs. */
 7250		count = phba->sli4_hba.max_cfg_param.max_rpi;
 7251		if (count <= 0) {
 7252			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 7253					"3279 Invalid provisioning of "
 7254					"rpi:%d\n", count);
 7255			rc = -EINVAL;
 7256			goto err_exit;
 7257		}
 7258		base = phba->sli4_hba.max_cfg_param.rpi_base;
 7259		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
 7260		phba->sli4_hba.rpi_bmask = kcalloc(longs,
 7261						   sizeof(unsigned long),
 7262						   GFP_KERNEL);
 7263		if (unlikely(!phba->sli4_hba.rpi_bmask)) {
 7264			rc = -ENOMEM;
 7265			goto err_exit;
 7266		}
 7267		phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
 7268						 GFP_KERNEL);
 7269		if (unlikely(!phba->sli4_hba.rpi_ids)) {
 7270			rc = -ENOMEM;
 7271			goto free_rpi_bmask;
 7272		}
 7273
 7274		for (i = 0; i < count; i++)
 7275			phba->sli4_hba.rpi_ids[i] = base + i;
 7276
 7277		/* VPIs. */
 7278		count = phba->sli4_hba.max_cfg_param.max_vpi;
 7279		if (count <= 0) {
 7280			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 7281					"3280 Invalid provisioning of "
 7282					"vpi:%d\n", count);
 7283			rc = -EINVAL;
 7284			goto free_rpi_ids;
 7285		}
 7286		base = phba->sli4_hba.max_cfg_param.vpi_base;
 7287		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
 7288		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
 7289					  GFP_KERNEL);
 7290		if (unlikely(!phba->vpi_bmask)) {
 7291			rc = -ENOMEM;
 7292			goto free_rpi_ids;
 7293		}
 7294		phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
 7295					GFP_KERNEL);
 7296		if (unlikely(!phba->vpi_ids)) {
 7297			rc = -ENOMEM;
 7298			goto free_vpi_bmask;
 7299		}
 7300
 7301		for (i = 0; i < count; i++)
 7302			phba->vpi_ids[i] = base + i;
 7303
 7304		/* XRIs. */
 7305		count = phba->sli4_hba.max_cfg_param.max_xri;
 7306		if (count <= 0) {
 7307			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 7308					"3281 Invalid provisioning of "
 7309					"xri:%d\n", count);
 7310			rc = -EINVAL;
 7311			goto free_vpi_ids;
 7312		}
 7313		base = phba->sli4_hba.max_cfg_param.xri_base;
 7314		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
 7315		phba->sli4_hba.xri_bmask = kcalloc(longs,
 7316						   sizeof(unsigned long),
 7317						   GFP_KERNEL);
 7318		if (unlikely(!phba->sli4_hba.xri_bmask)) {
 7319			rc = -ENOMEM;
 7320			goto free_vpi_ids;
 7321		}
 7322		phba->sli4_hba.max_cfg_param.xri_used = 0;
 7323		phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
 7324						 GFP_KERNEL);
 7325		if (unlikely(!phba->sli4_hba.xri_ids)) {
 7326			rc = -ENOMEM;
 7327			goto free_xri_bmask;
 7328		}
 7329
 7330		for (i = 0; i < count; i++)
 7331			phba->sli4_hba.xri_ids[i] = base + i;
 7332
 7333		/* VFIs. */
 7334		count = phba->sli4_hba.max_cfg_param.max_vfi;
 7335		if (count <= 0) {
 7336			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 7337					"3282 Invalid provisioning of "
 7338					"vfi:%d\n", count);
 7339			rc = -EINVAL;
 7340			goto free_xri_ids;
 7341		}
 7342		base = phba->sli4_hba.max_cfg_param.vfi_base;
 7343		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
 7344		phba->sli4_hba.vfi_bmask = kcalloc(longs,
 7345						   sizeof(unsigned long),
 7346						   GFP_KERNEL);
 7347		if (unlikely(!phba->sli4_hba.vfi_bmask)) {
 7348			rc = -ENOMEM;
 7349			goto free_xri_ids;
 7350		}
 7351		phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
 7352						 GFP_KERNEL);
 7353		if (unlikely(!phba->sli4_hba.vfi_ids)) {
 7354			rc = -ENOMEM;
 7355			goto free_vfi_bmask;
 7356		}
 7357
 7358		for (i = 0; i < count; i++)
 7359			phba->sli4_hba.vfi_ids[i] = base + i;
 7360
 7361		/*
 7362		 * Mark all resources ready.  An HBA reset doesn't need
 7363		 * to reset the initialization.
 7364		 */
 7365		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
 7366		       LPFC_IDX_RSRC_RDY);
 7367		return 0;
 7368	}
 7369
 7370 free_vfi_bmask:
 7371	kfree(phba->sli4_hba.vfi_bmask);
 7372	phba->sli4_hba.vfi_bmask = NULL;
 7373 free_xri_ids:
 7374	kfree(phba->sli4_hba.xri_ids);
 7375	phba->sli4_hba.xri_ids = NULL;
 7376 free_xri_bmask:
 7377	kfree(phba->sli4_hba.xri_bmask);
 7378	phba->sli4_hba.xri_bmask = NULL;
 7379 free_vpi_ids:
 7380	kfree(phba->vpi_ids);
 7381	phba->vpi_ids = NULL;
 7382 free_vpi_bmask:
 7383	kfree(phba->vpi_bmask);
 7384	phba->vpi_bmask = NULL;
 7385 free_rpi_ids:
 7386	kfree(phba->sli4_hba.rpi_ids);
 7387	phba->sli4_hba.rpi_ids = NULL;
 7388 free_rpi_bmask:
 7389	kfree(phba->sli4_hba.rpi_bmask);
 7390	phba->sli4_hba.rpi_bmask = NULL;
 7391 err_exit:
 7392	return rc;
 7393}
 7394
 7395/**
 7396 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
 7397 * @phba: Pointer to HBA context object.
 7398 *
 7399 * This function allocates the number of elements for the specified
 7400 * resource type.
 7401 **/
 7402int
 7403lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
 7404{
 7405	if (phba->sli4_hba.extents_in_use) {
 7406		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
 7407		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
 7408		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
 7409		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
 7410	} else {
 7411		kfree(phba->vpi_bmask);
 7412		phba->sli4_hba.max_cfg_param.vpi_used = 0;
 7413		kfree(phba->vpi_ids);
 7414		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
 7415		kfree(phba->sli4_hba.xri_bmask);
 7416		kfree(phba->sli4_hba.xri_ids);
 7417		kfree(phba->sli4_hba.vfi_bmask);
 7418		kfree(phba->sli4_hba.vfi_ids);
 7419		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
 7420		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
 7421	}
 7422
 7423	return 0;
 7424}
 7425
 7426/**
 7427 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
 7428 * @phba: Pointer to HBA context object.
 7429 * @type: The resource extent type.
 7430 * @extnt_cnt: buffer to hold port extent count response
 7431 * @extnt_size: buffer to hold port extent size response.
 7432 *
 7433 * This function calls the port to read the host allocated extents
 7434 * for a particular type.
 7435 **/
 7436int
 7437lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
 7438			       uint16_t *extnt_cnt, uint16_t *extnt_size)
 7439{
 7440	bool emb;
 7441	int rc = 0;
 7442	uint16_t curr_blks = 0;
 7443	uint32_t req_len, emb_len;
 7444	uint32_t alloc_len, mbox_tmo;
 7445	struct list_head *blk_list_head;
 7446	struct lpfc_rsrc_blks *rsrc_blk;
 7447	LPFC_MBOXQ_t *mbox;
 7448	void *virtaddr = NULL;
 7449	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
 7450	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
 7451	union  lpfc_sli4_cfg_shdr *shdr;
 7452
 7453	switch (type) {
 7454	case LPFC_RSC_TYPE_FCOE_VPI:
 7455		blk_list_head = &phba->lpfc_vpi_blk_list;
 7456		break;
 7457	case LPFC_RSC_TYPE_FCOE_XRI:
 7458		blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
 7459		break;
 7460	case LPFC_RSC_TYPE_FCOE_VFI:
 7461		blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
 7462		break;
 7463	case LPFC_RSC_TYPE_FCOE_RPI:
 7464		blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
 7465		break;
 7466	default:
 7467		return -EIO;
 7468	}
 7469
 7470	/* Count the number of extents currently allocatd for this type. */
 7471	list_for_each_entry(rsrc_blk, blk_list_head, list) {
 7472		if (curr_blks == 0) {
 7473			/*
 7474			 * The GET_ALLOCATED mailbox does not return the size,
 7475			 * just the count.  The size should be just the size
 7476			 * stored in the current allocated block and all sizes
 7477			 * for an extent type are the same so set the return
 7478			 * value now.
 7479			 */
 7480			*extnt_size = rsrc_blk->rsrc_size;
 7481		}
 7482		curr_blks++;
 7483	}
 7484
 7485	/*
 7486	 * Calculate the size of an embedded mailbox.  The uint32_t
 7487	 * accounts for extents-specific word.
 7488	 */
 7489	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
 7490		sizeof(uint32_t);
 7491
 7492	/*
 7493	 * Presume the allocation and response will fit into an embedded
 7494	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.
 7495	 */
 7496	emb = LPFC_SLI4_MBX_EMBED;
 7497	req_len = emb_len;
 7498	if (req_len > emb_len) {
 7499		req_len = curr_blks * sizeof(uint16_t) +
 7500			sizeof(union lpfc_sli4_cfg_shdr) +
 7501			sizeof(uint32_t);
 7502		emb = LPFC_SLI4_MBX_NEMBED;
 7503	}
 7504
 7505	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 7506	if (!mbox)
 7507		return -ENOMEM;
 7508	memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
 7509
 7510	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
 7511				     LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
 7512				     req_len, emb);
 7513	if (alloc_len < req_len) {
 7514		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 7515			"2983 Allocated DMA memory size (x%x) is "
 7516			"less than the requested DMA memory "
 7517			"size (x%x)\n", alloc_len, req_len);
 7518		rc = -ENOMEM;
 7519		goto err_exit;
 7520	}
 7521	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
 7522	if (unlikely(rc)) {
 7523		rc = -EIO;
 7524		goto err_exit;
 7525	}
 7526
 7527	if (!phba->sli4_hba.intr_enable)
 7528		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
 7529	else {
 7530		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
 7531		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
 7532	}
 7533
 7534	if (unlikely(rc)) {
 7535		rc = -EIO;
 7536		goto err_exit;
 7537	}
 7538
 7539	/*
 7540	 * Figure out where the response is located.  Then get local pointers
 7541	 * to the response data.  The port does not guarantee to respond to
 7542	 * all extents counts request so update the local variable with the
 7543	 * allocated count from the port.
 7544	 */
 7545	if (emb == LPFC_SLI4_MBX_EMBED) {
 7546		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
 7547		shdr = &rsrc_ext->header.cfg_shdr;
 7548		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
 7549	} else {
 7550		virtaddr = mbox->sge_array->addr[0];
 7551		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
 7552		shdr = &n_rsrc->cfg_shdr;
 7553		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
 7554	}
 7555
 7556	if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
 7557		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 7558			"2984 Failed to read allocated resources "
 7559			"for type %d - Status 0x%x Add'l Status 0x%x.\n",
 7560			type,
 7561			bf_get(lpfc_mbox_hdr_status, &shdr->response),
 7562			bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
 7563		rc = -EIO;
 7564		goto err_exit;
 7565	}
 7566 err_exit:
 7567	lpfc_sli4_mbox_cmd_free(phba, mbox);
 7568	return rc;
 7569}
 7570
 7571/**
 7572 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
 7573 * @phba: pointer to lpfc hba data structure.
 7574 * @sgl_list: linked link of sgl buffers to post
 7575 * @cnt: number of linked list buffers
 7576 *
 7577 * This routine walks the list of buffers that have been allocated and
 7578 * repost them to the port by using SGL block post. This is needed after a
 7579 * pci_function_reset/warm_start or start. It attempts to construct blocks
 7580 * of buffer sgls which contains contiguous xris and uses the non-embedded
 7581 * SGL block post mailbox commands to post them to the port. For single
 7582 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
 7583 * mailbox command for posting.
 7584 *
 7585 * Returns: 0 = success, non-zero failure.
 7586 **/
 7587static int
 7588lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
 7589			  struct list_head *sgl_list, int cnt)
 7590{
 7591	struct lpfc_sglq *sglq_entry = NULL;
 7592	struct lpfc_sglq *sglq_entry_next = NULL;
 7593	struct lpfc_sglq *sglq_entry_first = NULL;
 7594	int status = 0, total_cnt;
 7595	int post_cnt = 0, num_posted = 0, block_cnt = 0;
 7596	int last_xritag = NO_XRI;
 7597	LIST_HEAD(prep_sgl_list);
 7598	LIST_HEAD(blck_sgl_list);
 7599	LIST_HEAD(allc_sgl_list);
 7600	LIST_HEAD(post_sgl_list);
 7601	LIST_HEAD(free_sgl_list);
 7602
 7603	spin_lock_irq(&phba->hbalock);
 7604	spin_lock(&phba->sli4_hba.sgl_list_lock);
 7605	list_splice_init(sgl_list, &allc_sgl_list);
 7606	spin_unlock(&phba->sli4_hba.sgl_list_lock);
 7607	spin_unlock_irq(&phba->hbalock);
 7608
 7609	total_cnt = cnt;
 7610	list_for_each_entry_safe(sglq_entry, sglq_entry_next,
 7611				 &allc_sgl_list, list) {
 7612		list_del_init(&sglq_entry->list);
 7613		block_cnt++;
 7614		if ((last_xritag != NO_XRI) &&
 7615		    (sglq_entry->sli4_xritag != last_xritag + 1)) {
 7616			/* a hole in xri block, form a sgl posting block */
 7617			list_splice_init(&prep_sgl_list, &blck_sgl_list);
 7618			post_cnt = block_cnt - 1;
 7619			/* prepare list for next posting block */
 7620			list_add_tail(&sglq_entry->list, &prep_sgl_list);
 7621			block_cnt = 1;
 7622		} else {
 7623			/* prepare list for next posting block */
 7624			list_add_tail(&sglq_entry->list, &prep_sgl_list);
 7625			/* enough sgls for non-embed sgl mbox command */
 7626			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
 7627				list_splice_init(&prep_sgl_list,
 7628						 &blck_sgl_list);
 7629				post_cnt = block_cnt;
 7630				block_cnt = 0;
 7631			}
 7632		}
 7633		num_posted++;
 7634
 7635		/* keep track of last sgl's xritag */
 7636		last_xritag = sglq_entry->sli4_xritag;
 7637
 7638		/* end of repost sgl list condition for buffers */
 7639		if (num_posted == total_cnt) {
 7640			if (post_cnt == 0) {
 7641				list_splice_init(&prep_sgl_list,
 7642						 &blck_sgl_list);
 7643				post_cnt = block_cnt;
 7644			} else if (block_cnt == 1) {
 7645				status = lpfc_sli4_post_sgl(phba,
 7646						sglq_entry->phys, 0,
 7647						sglq_entry->sli4_xritag);
 7648				if (!status) {
 7649					/* successful, put sgl to posted list */
 7650					list_add_tail(&sglq_entry->list,
 7651						      &post_sgl_list);
 7652				} else {
 7653					/* Failure, put sgl to free list */
 7654					lpfc_printf_log(phba, KERN_WARNING,
 7655						LOG_SLI,
 7656						"3159 Failed to post "
 7657						"sgl, xritag:x%x\n",
 7658						sglq_entry->sli4_xritag);
 7659					list_add_tail(&sglq_entry->list,
 7660						      &free_sgl_list);
 7661					total_cnt--;
 7662				}
 7663			}
 7664		}
 7665
 7666		/* continue until a nembed page worth of sgls */
 7667		if (post_cnt == 0)
 7668			continue;
 7669
 7670		/* post the buffer list sgls as a block */
 7671		status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
 7672						 post_cnt);
 7673
 7674		if (!status) {
 7675			/* success, put sgl list to posted sgl list */
 7676			list_splice_init(&blck_sgl_list, &post_sgl_list);
 7677		} else {
 7678			/* Failure, put sgl list to free sgl list */
 7679			sglq_entry_first = list_first_entry(&blck_sgl_list,
 7680							    struct lpfc_sglq,
 7681							    list);
 7682			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
 7683					"3160 Failed to post sgl-list, "
 7684					"xritag:x%x-x%x\n",
 7685					sglq_entry_first->sli4_xritag,
 7686					(sglq_entry_first->sli4_xritag +
 7687					 post_cnt - 1));
 7688			list_splice_init(&blck_sgl_list, &free_sgl_list);
 7689			total_cnt -= post_cnt;
 7690		}
 7691
 7692		/* don't reset xirtag due to hole in xri block */
 7693		if (block_cnt == 0)
 7694			last_xritag = NO_XRI;
 7695
 7696		/* reset sgl post count for next round of posting */
 7697		post_cnt = 0;
 7698	}
 7699
 7700	/* free the sgls failed to post */
 7701	lpfc_free_sgl_list(phba, &free_sgl_list);
 7702
 7703	/* push sgls posted to the available list */
 7704	if (!list_empty(&post_sgl_list)) {
 7705		spin_lock_irq(&phba->hbalock);
 7706		spin_lock(&phba->sli4_hba.sgl_list_lock);
 7707		list_splice_init(&post_sgl_list, sgl_list);
 7708		spin_unlock(&phba->sli4_hba.sgl_list_lock);
 7709		spin_unlock_irq(&phba->hbalock);
 7710	} else {
 7711		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 7712				"3161 Failure to post sgl to port,status %x "
 7713				"blkcnt %d totalcnt %d postcnt %d\n",
 7714				status, block_cnt, total_cnt, post_cnt);
 7715		return -EIO;
 7716	}
 7717
 7718	/* return the number of XRIs actually posted */
 7719	return total_cnt;
 7720}
 7721
 7722/**
 7723 * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
 7724 * @phba: pointer to lpfc hba data structure.
 7725 *
 7726 * This routine walks the list of nvme buffers that have been allocated and
 7727 * repost them to the port by using SGL block post. This is needed after a
 7728 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
 7729 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
 7730 * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
 7731 *
 7732 * Returns: 0 = success, non-zero failure.
 7733 **/
 7734static int
 7735lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
 7736{
 7737	LIST_HEAD(post_nblist);
 7738	int num_posted, rc = 0;
 7739
 7740	/* get all NVME buffers need to repost to a local list */
 7741	lpfc_io_buf_flush(phba, &post_nblist);
 7742
 7743	/* post the list of nvme buffer sgls to port if available */
 7744	if (!list_empty(&post_nblist)) {
 7745		num_posted = lpfc_sli4_post_io_sgl_list(
 7746			phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
 7747		/* failed to post any nvme buffer, return error */
 7748		if (num_posted == 0)
 7749			rc = -EIO;
 7750	}
 7751	return rc;
 7752}
 7753
 7754static void
 7755lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
 7756{
 7757	uint32_t len;
 7758
 7759	len = sizeof(struct lpfc_mbx_set_host_data) -
 7760		sizeof(struct lpfc_sli4_cfg_mhdr);
 7761	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
 7762			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
 7763			 LPFC_SLI4_MBX_EMBED);
 7764
 7765	mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
 7766	mbox->u.mqe.un.set_host_data.param_len =
 7767					LPFC_HOST_OS_DRIVER_VERSION_SIZE;
 7768	snprintf(mbox->u.mqe.un.set_host_data.un.data,
 7769		 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
 7770		 "Linux %s v"LPFC_DRIVER_VERSION,
 7771		 test_bit(HBA_FCOE_MODE, &phba->hba_flag) ? "FCoE" : "FC");
 7772}
 7773
 7774int
 7775lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
 7776		    struct lpfc_queue *drq, int count, int idx)
 7777{
 7778	int rc, i;
 7779	struct lpfc_rqe hrqe;
 7780	struct lpfc_rqe drqe;
 7781	struct lpfc_rqb *rqbp;
 7782	unsigned long flags;
 7783	struct rqb_dmabuf *rqb_buffer;
 7784	LIST_HEAD(rqb_buf_list);
 7785
 7786	rqbp = hrq->rqbp;
 7787	for (i = 0; i < count; i++) {
 7788		spin_lock_irqsave(&phba->hbalock, flags);
 7789		/* IF RQ is already full, don't bother */
 7790		if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {
 7791			spin_unlock_irqrestore(&phba->hbalock, flags);
 7792			break;
 7793		}
 7794		spin_unlock_irqrestore(&phba->hbalock, flags);
 7795
 7796		rqb_buffer = rqbp->rqb_alloc_buffer(phba);
 7797		if (!rqb_buffer)
 7798			break;
 7799		rqb_buffer->hrq = hrq;
 7800		rqb_buffer->drq = drq;
 7801		rqb_buffer->idx = idx;
 7802		list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
 7803	}
 7804
 7805	spin_lock_irqsave(&phba->hbalock, flags);
 7806	while (!list_empty(&rqb_buf_list)) {
 7807		list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
 7808				 hbuf.list);
 7809
 7810		hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
 7811		hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
 7812		drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
 7813		drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
 7814		rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
 7815		if (rc < 0) {
 7816			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 7817					"6421 Cannot post to HRQ %d: %x %x %x "
 7818					"DRQ %x %x\n",
 7819					hrq->queue_id,
 7820					hrq->host_index,
 7821					hrq->hba_index,
 7822					hrq->entry_count,
 7823					drq->host_index,
 7824					drq->hba_index);
 7825			rqbp->rqb_free_buffer(phba, rqb_buffer);
 7826		} else {
 7827			list_add_tail(&rqb_buffer->hbuf.list,
 7828				      &rqbp->rqb_buffer_list);
 7829			rqbp->buffer_count++;
 7830		}
 7831	}
 7832	spin_unlock_irqrestore(&phba->hbalock, flags);
 7833	return 1;
 7834}
 7835
 7836static void
 7837lpfc_mbx_cmpl_read_lds_params(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 7838{
 7839	union lpfc_sli4_cfg_shdr *shdr;
 7840	u32 shdr_status, shdr_add_status;
 7841
 7842	shdr = (union lpfc_sli4_cfg_shdr *)
 7843		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
 7844	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
 7845	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
 7846	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
 7847		lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT | LOG_MBOX,
 7848				"4622 SET_FEATURE (x%x) mbox failed, "
 7849				"status x%x add_status x%x, mbx status x%x\n",
 7850				LPFC_SET_LD_SIGNAL, shdr_status,
 7851				shdr_add_status, pmb->u.mb.mbxStatus);
 7852		phba->degrade_activate_threshold = 0;
 7853		phba->degrade_deactivate_threshold = 0;
 7854		phba->fec_degrade_interval = 0;
 7855		goto out;
 7856	}
 7857
 7858	phba->degrade_activate_threshold = pmb->u.mqe.un.set_feature.word7;
 7859	phba->degrade_deactivate_threshold = pmb->u.mqe.un.set_feature.word8;
 7860	phba->fec_degrade_interval = pmb->u.mqe.un.set_feature.word10;
 7861
 7862	lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT,
 7863			"4624 Success: da x%x dd x%x interval x%x\n",
 7864			phba->degrade_activate_threshold,
 7865			phba->degrade_deactivate_threshold,
 7866			phba->fec_degrade_interval);
 7867out:
 7868	mempool_free(pmb, phba->mbox_mem_pool);
 7869}
 7870
 7871int
 7872lpfc_read_lds_params(struct lpfc_hba *phba)
 7873{
 7874	LPFC_MBOXQ_t *mboxq;
 7875	int rc;
 7876
 7877	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 7878	if (!mboxq)
 7879		return -ENOMEM;
 7880
 7881	lpfc_set_features(phba, mboxq, LPFC_SET_LD_SIGNAL);
 7882	mboxq->vport = phba->pport;
 7883	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_lds_params;
 7884	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
 7885	if (rc == MBX_NOT_FINISHED) {
 7886		mempool_free(mboxq, phba->mbox_mem_pool);
 7887		return -EIO;
 7888	}
 7889	return 0;
 7890}
 7891
 7892static void
 7893lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 7894{
 7895	struct lpfc_vport *vport = pmb->vport;
 7896	union lpfc_sli4_cfg_shdr *shdr;
 7897	u32 shdr_status, shdr_add_status;
 7898	u32 sig, acqe;
 7899
 7900	/* Two outcomes. (1) Set featurs was successul and EDC negotiation
 7901	 * is done. (2) Mailbox failed and send FPIN support only.
 7902	 */
 7903	shdr = (union lpfc_sli4_cfg_shdr *)
 7904		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;
 7905	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
 7906	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
 7907	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
 7908		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
 7909				"2516 CGN SET_FEATURE mbox failed with "
 7910				"status x%x add_status x%x, mbx status x%x "
 7911				"Reset Congestion to FPINs only\n",
 7912				shdr_status, shdr_add_status,
 7913				pmb->u.mb.mbxStatus);
 7914		/* If there is a mbox error, move on to RDF */
 7915		phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
 7916		phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
 7917		goto out;
 7918	}
 7919
 7920	/* Zero out Congestion Signal ACQE counter */
 7921	phba->cgn_acqe_cnt = 0;
 7922
 7923	acqe = bf_get(lpfc_mbx_set_feature_CGN_acqe_freq,
 7924		      &pmb->u.mqe.un.set_feature);
 7925	sig = bf_get(lpfc_mbx_set_feature_CGN_warn_freq,
 7926		     &pmb->u.mqe.un.set_feature);
 7927	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 7928			"4620 SET_FEATURES Success: Freq: %ds %dms "
 7929			" Reg: x%x x%x\n", acqe, sig,
 7930			phba->cgn_reg_signal, phba->cgn_reg_fpin);
 7931out:
 7932	mempool_free(pmb, phba->mbox_mem_pool);
 7933
 7934	/* Register for FPIN events from the fabric now that the
 7935	 * EDC common_set_features has completed.
 7936	 */
 7937	lpfc_issue_els_rdf(vport, 0);
 7938}
 7939
 7940int
 7941lpfc_config_cgn_signal(struct lpfc_hba *phba)
 7942{
 7943	LPFC_MBOXQ_t *mboxq;
 7944	u32 rc;
 7945
 7946	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 7947	if (!mboxq)
 7948		goto out_rdf;
 7949
 7950	lpfc_set_features(phba, mboxq, LPFC_SET_CGN_SIGNAL);
 7951	mboxq->vport = phba->pport;
 7952	mboxq->mbox_cmpl = lpfc_mbx_cmpl_cgn_set_ftrs;
 7953
 7954	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 7955			"4621 SET_FEATURES: FREQ sig x%x acqe x%x: "
 7956			"Reg: x%x x%x\n",
 7957			phba->cgn_sig_freq, lpfc_acqe_cgn_frequency,
 7958			phba->cgn_reg_signal, phba->cgn_reg_fpin);
 7959
 7960	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
 7961	if (rc == MBX_NOT_FINISHED)
 7962		goto out;
 7963	return 0;
 7964
 7965out:
 7966	mempool_free(mboxq, phba->mbox_mem_pool);
 7967out_rdf:
 7968	/* If there is a mbox error, move on to RDF */
 7969	phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
 7970	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
 7971	lpfc_issue_els_rdf(phba->pport, 0);
 7972	return -EIO;
 7973}
 7974
 7975/**
 7976 * lpfc_init_idle_stat_hb - Initialize idle_stat tracking
 7977 * @phba: pointer to lpfc hba data structure.
 7978 *
 7979 * This routine initializes the per-eq idle_stat to dynamically dictate
 7980 * polling decisions.
 7981 *
 7982 * Return codes:
 7983 *   None
 7984 **/
 7985static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)
 7986{
 7987	int i;
 7988	struct lpfc_sli4_hdw_queue *hdwq;
 7989	struct lpfc_queue *eq;
 7990	struct lpfc_idle_stat *idle_stat;
 7991	u64 wall;
 7992
 7993	for_each_present_cpu(i) {
 7994		hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
 7995		eq = hdwq->hba_eq;
 7996
 7997		/* Skip if we've already handled this eq's primary CPU */
 7998		if (eq->chann != i)
 7999			continue;
 8000
 8001		idle_stat = &phba->sli4_hba.idle_stat[i];
 8002
 8003		idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);
 8004		idle_stat->prev_wall = wall;
 8005
 8006		if (phba->nvmet_support ||
 8007		    phba->cmf_active_mode != LPFC_CFG_OFF ||
 8008		    phba->intr_type != MSIX)
 8009			eq->poll_mode = LPFC_QUEUE_WORK;
 8010		else
 8011			eq->poll_mode = LPFC_THREADED_IRQ;
 8012	}
 8013
 8014	if (!phba->nvmet_support && phba->intr_type == MSIX)
 8015		schedule_delayed_work(&phba->idle_stat_delay_work,
 8016				      msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
 8017}
 8018
 8019static void lpfc_sli4_dip(struct lpfc_hba *phba)
 8020{
 8021	uint32_t if_type;
 8022
 8023	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
 8024	if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
 8025	    if_type == LPFC_SLI_INTF_IF_TYPE_6) {
 8026		struct lpfc_register reg_data;
 8027
 8028		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
 8029			       &reg_data.word0))
 8030			return;
 8031
 8032		if (bf_get(lpfc_sliport_status_dip, &reg_data))
 8033			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
 8034					"2904 Firmware Dump Image Present"
 8035					" on Adapter");
 8036	}
 8037}
 8038
 8039/**
 8040 * lpfc_rx_monitor_create_ring - Initialize ring buffer for rx_monitor
 8041 * @rx_monitor: Pointer to lpfc_rx_info_monitor object
 8042 * @entries: Number of rx_info_entry objects to allocate in ring
 8043 *
 8044 * Return:
 8045 * 0 - Success
 8046 * ENOMEM - Failure to kmalloc
 8047 **/
 8048int lpfc_rx_monitor_create_ring(struct lpfc_rx_info_monitor *rx_monitor,
 8049				u32 entries)
 8050{
 8051	rx_monitor->ring = kmalloc_array(entries, sizeof(struct rx_info_entry),
 8052					 GFP_KERNEL);
 8053	if (!rx_monitor->ring)
 8054		return -ENOMEM;
 8055
 8056	rx_monitor->head_idx = 0;
 8057	rx_monitor->tail_idx = 0;
 8058	spin_lock_init(&rx_monitor->lock);
 8059	rx_monitor->entries = entries;
 8060
 8061	return 0;
 8062}
 8063
 8064/**
 8065 * lpfc_rx_monitor_destroy_ring - Free ring buffer for rx_monitor
 8066 * @rx_monitor: Pointer to lpfc_rx_info_monitor object
 8067 *
 8068 * Called after cancellation of cmf_timer.
 8069 **/
 8070void lpfc_rx_monitor_destroy_ring(struct lpfc_rx_info_monitor *rx_monitor)
 8071{
 8072	kfree(rx_monitor->ring);
 8073	rx_monitor->ring = NULL;
 8074	rx_monitor->entries = 0;
 8075	rx_monitor->head_idx = 0;
 8076	rx_monitor->tail_idx = 0;
 8077}
 8078
 8079/**
 8080 * lpfc_rx_monitor_record - Insert an entry into rx_monitor's ring
 8081 * @rx_monitor: Pointer to lpfc_rx_info_monitor object
 8082 * @entry: Pointer to rx_info_entry
 8083 *
 8084 * Used to insert an rx_info_entry into rx_monitor's ring.  Note that this is a
 8085 * deep copy of rx_info_entry not a shallow copy of the rx_info_entry ptr.
 8086 *
 8087 * This is called from lpfc_cmf_timer, which is in timer/softirq context.
 8088 *
 8089 * In cases of old data overflow, we do a best effort of FIFO order.
 8090 **/
 8091void lpfc_rx_monitor_record(struct lpfc_rx_info_monitor *rx_monitor,
 8092			    struct rx_info_entry *entry)
 8093{
 8094	struct rx_info_entry *ring = rx_monitor->ring;
 8095	u32 *head_idx = &rx_monitor->head_idx;
 8096	u32 *tail_idx = &rx_monitor->tail_idx;
 8097	spinlock_t *ring_lock = &rx_monitor->lock;
 8098	u32 ring_size = rx_monitor->entries;
 8099
 8100	spin_lock(ring_lock);
 8101	memcpy(&ring[*tail_idx], entry, sizeof(*entry));
 8102	*tail_idx = (*tail_idx + 1) % ring_size;
 8103
 8104	/* Best effort of FIFO saved data */
 8105	if (*tail_idx == *head_idx)
 8106		*head_idx = (*head_idx + 1) % ring_size;
 8107
 8108	spin_unlock(ring_lock);
 8109}
 8110
 8111/**
 8112 * lpfc_rx_monitor_report - Read out rx_monitor's ring
 8113 * @phba: Pointer to lpfc_hba object
 8114 * @rx_monitor: Pointer to lpfc_rx_info_monitor object
 8115 * @buf: Pointer to char buffer that will contain rx monitor info data
 8116 * @buf_len: Length buf including null char
 8117 * @max_read_entries: Maximum number of entries to read out of ring
 8118 *
 8119 * Used to dump/read what's in rx_monitor's ring buffer.
 8120 *
 8121 * If buf is NULL || buf_len == 0, then it is implied that we want to log the
 8122 * information to kmsg instead of filling out buf.
 8123 *
 8124 * Return:
 8125 * Number of entries read out of the ring
 8126 **/
 8127u32 lpfc_rx_monitor_report(struct lpfc_hba *phba,
 8128			   struct lpfc_rx_info_monitor *rx_monitor, char *buf,
 8129			   u32 buf_len, u32 max_read_entries)
 8130{
 8131	struct rx_info_entry *ring = rx_monitor->ring;
 8132	struct rx_info_entry *entry;
 8133	u32 *head_idx = &rx_monitor->head_idx;
 8134	u32 *tail_idx = &rx_monitor->tail_idx;
 8135	spinlock_t *ring_lock = &rx_monitor->lock;
 8136	u32 ring_size = rx_monitor->entries;
 8137	u32 cnt = 0;
 8138	char tmp[DBG_LOG_STR_SZ] = {0};
 8139	bool log_to_kmsg = (!buf || !buf_len) ? true : false;
 8140
 8141	if (!log_to_kmsg) {
 8142		/* clear the buffer to be sure */
 8143		memset(buf, 0, buf_len);
 8144
 8145		scnprintf(buf, buf_len, "\t%-16s%-16s%-16s%-16s%-8s%-8s%-8s"
 8146					"%-8s%-8s%-8s%-16s\n",
 8147					"MaxBPI", "Tot_Data_CMF",
 8148					"Tot_Data_Cmd", "Tot_Data_Cmpl",
 8149					"Lat(us)", "Avg_IO", "Max_IO", "Bsy",
 8150					"IO_cnt", "Info", "BWutil(ms)");
 8151	}
 8152
 8153	/* Needs to be _irq because record is called from timer interrupt
 8154	 * context
 8155	 */
 8156	spin_lock_irq(ring_lock);
 8157	while (*head_idx != *tail_idx) {
 8158		entry = &ring[*head_idx];
 8159
 8160		/* Read out this entry's data. */
 8161		if (!log_to_kmsg) {
 8162			/* If !log_to_kmsg, then store to buf. */
 8163			scnprintf(tmp, sizeof(tmp),
 8164				  "%03d:\t%-16llu%-16llu%-16llu%-16llu%-8llu"
 8165				  "%-8llu%-8llu%-8u%-8u%-8u%u(%u)\n",
 8166				  *head_idx, entry->max_bytes_per_interval,
 8167				  entry->cmf_bytes, entry->total_bytes,
 8168				  entry->rcv_bytes, entry->avg_io_latency,
 8169				  entry->avg_io_size, entry->max_read_cnt,
 8170				  entry->cmf_busy, entry->io_cnt,
 8171				  entry->cmf_info, entry->timer_utilization,
 8172				  entry->timer_interval);
 8173
 8174			/* Check for buffer overflow */
 8175			if ((strlen(buf) + strlen(tmp)) >= buf_len)
 8176				break;
 8177
 8178			/* Append entry's data to buffer */
 8179			strlcat(buf, tmp, buf_len);
 8180		} else {
 8181			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
 8182					"4410 %02u: MBPI %llu Xmit %llu "
 8183					"Cmpl %llu Lat %llu ASz %llu Info %02u "
 8184					"BWUtil %u Int %u slot %u\n",
 8185					cnt, entry->max_bytes_per_interval,
 8186					entry->total_bytes, entry->rcv_bytes,
 8187					entry->avg_io_latency,
 8188					entry->avg_io_size, entry->cmf_info,
 8189					entry->timer_utilization,
 8190					entry->timer_interval, *head_idx);
 8191		}
 8192
 8193		*head_idx = (*head_idx + 1) % ring_size;
 8194
 8195		/* Don't feed more than max_read_entries */
 8196		cnt++;
 8197		if (cnt >= max_read_entries)
 8198			break;
 8199	}
 8200	spin_unlock_irq(ring_lock);
 8201
 8202	return cnt;
 8203}
 8204
 8205/**
 8206 * lpfc_cmf_setup - Initialize idle_stat tracking
 8207 * @phba: Pointer to HBA context object.
 8208 *
 8209 * This is called from HBA setup during driver load or when the HBA
 8210 * comes online. this does all the initialization to support CMF and MI.
 8211 **/
 8212static int
 8213lpfc_cmf_setup(struct lpfc_hba *phba)
 8214{
 8215	LPFC_MBOXQ_t *mboxq;
 8216	struct lpfc_dmabuf *mp;
 8217	struct lpfc_pc_sli4_params *sli4_params;
 8218	int rc, cmf, mi_ver;
 8219
 8220	rc = lpfc_sli4_refresh_params(phba);
 8221	if (unlikely(rc))
 8222		return rc;
 8223
 8224	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 8225	if (!mboxq)
 8226		return -ENOMEM;
 8227
 8228	sli4_params = &phba->sli4_hba.pc_sli4_params;
 8229
 8230	/* Always try to enable MI feature if we can */
 8231	if (sli4_params->mi_ver) {
 8232		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_MI);
 8233		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 8234		mi_ver = bf_get(lpfc_mbx_set_feature_mi,
 8235				 &mboxq->u.mqe.un.set_feature);
 8236
 8237		if (rc == MBX_SUCCESS) {
 8238			if (mi_ver) {
 8239				lpfc_printf_log(phba,
 8240						KERN_WARNING, LOG_CGN_MGMT,
 8241						"6215 MI is enabled\n");
 8242				sli4_params->mi_ver = mi_ver;
 8243			} else {
 8244				lpfc_printf_log(phba,
 8245						KERN_WARNING, LOG_CGN_MGMT,
 8246						"6338 MI is disabled\n");
 8247				sli4_params->mi_ver = 0;
 8248			}
 8249		} else {
 8250			/* mi_ver is already set from GET_SLI4_PARAMETERS */
 8251			lpfc_printf_log(phba, KERN_INFO,
 8252					LOG_CGN_MGMT | LOG_INIT,
 8253					"6245 Enable MI Mailbox x%x (x%x/x%x) "
 8254					"failed, rc:x%x mi:x%x\n",
 8255					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
 8256					lpfc_sli_config_mbox_subsys_get
 8257						(phba, mboxq),
 8258					lpfc_sli_config_mbox_opcode_get
 8259						(phba, mboxq),
 8260					rc, sli4_params->mi_ver);
 8261		}
 8262	} else {
 8263		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
 8264				"6217 MI is disabled\n");
 8265	}
 8266
 8267	/* Ensure FDMI is enabled for MI if enable_mi is set */
 8268	if (sli4_params->mi_ver)
 8269		phba->cfg_fdmi_on = LPFC_FDMI_SUPPORT;
 8270
 8271	/* Always try to enable CMF feature if we can */
 8272	if (sli4_params->cmf) {
 8273		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_CMF);
 8274		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 8275		cmf = bf_get(lpfc_mbx_set_feature_cmf,
 8276			     &mboxq->u.mqe.un.set_feature);
 8277		if (rc == MBX_SUCCESS && cmf) {
 8278			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
 8279					"6218 CMF is enabled: mode %d\n",
 8280					phba->cmf_active_mode);
 8281		} else {
 8282			lpfc_printf_log(phba, KERN_WARNING,
 8283					LOG_CGN_MGMT | LOG_INIT,
 8284					"6219 Enable CMF Mailbox x%x (x%x/x%x) "
 8285					"failed, rc:x%x dd:x%x\n",
 8286					bf_get(lpfc_mqe_command, &mboxq->u.mqe),
 8287					lpfc_sli_config_mbox_subsys_get
 8288						(phba, mboxq),
 8289					lpfc_sli_config_mbox_opcode_get
 8290						(phba, mboxq),
 8291					rc, cmf);
 8292			sli4_params->cmf = 0;
 8293			phba->cmf_active_mode = LPFC_CFG_OFF;
 8294			goto no_cmf;
 8295		}
 8296
 8297		/* Allocate Congestion Information Buffer */
 8298		if (!phba->cgn_i) {
 8299			mp = kmalloc(sizeof(*mp), GFP_KERNEL);
 8300			if (mp)
 8301				mp->virt = dma_alloc_coherent
 8302						(&phba->pcidev->dev,
 8303						sizeof(struct lpfc_cgn_info),
 8304						&mp->phys, GFP_KERNEL);
 8305			if (!mp || !mp->virt) {
 8306				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
 8307						"2640 Failed to alloc memory "
 8308						"for Congestion Info\n");
 8309				kfree(mp);
 8310				sli4_params->cmf = 0;
 8311				phba->cmf_active_mode = LPFC_CFG_OFF;
 8312				goto no_cmf;
 8313			}
 8314			phba->cgn_i = mp;
 8315
 8316			/* initialize congestion buffer info */
 8317			lpfc_init_congestion_buf(phba);
 8318			lpfc_init_congestion_stat(phba);
 8319
 8320			/* Zero out Congestion Signal counters */
 8321			atomic64_set(&phba->cgn_acqe_stat.alarm, 0);
 8322			atomic64_set(&phba->cgn_acqe_stat.warn, 0);
 8323		}
 8324
 8325		rc = lpfc_sli4_cgn_params_read(phba);
 8326		if (rc < 0) {
 8327			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
 8328					"6242 Error reading Cgn Params (%d)\n",
 8329					rc);
 8330			/* Ensure CGN Mode is off */
 8331			sli4_params->cmf = 0;
 8332		} else if (!rc) {
 8333			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
 8334					"6243 CGN Event empty object.\n");
 8335			/* Ensure CGN Mode is off */
 8336			sli4_params->cmf = 0;
 8337		}
 8338	} else {
 8339no_cmf:
 8340		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
 8341				"6220 CMF is disabled\n");
 8342	}
 8343
 8344	/* Only register congestion buffer with firmware if BOTH
 8345	 * CMF and E2E are enabled.
 8346	 */
 8347	if (sli4_params->cmf && sli4_params->mi_ver) {
 8348		rc = lpfc_reg_congestion_buf(phba);
 8349		if (rc) {
 8350			dma_free_coherent(&phba->pcidev->dev,
 8351					  sizeof(struct lpfc_cgn_info),
 8352					  phba->cgn_i->virt, phba->cgn_i->phys);
 8353			kfree(phba->cgn_i);
 8354			phba->cgn_i = NULL;
 8355			/* Ensure CGN Mode is off */
 8356			phba->cmf_active_mode = LPFC_CFG_OFF;
 8357			sli4_params->cmf = 0;
 8358			return 0;
 8359		}
 8360	}
 8361	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
 8362			"6470 Setup MI version %d CMF %d mode %d\n",
 8363			sli4_params->mi_ver, sli4_params->cmf,
 8364			phba->cmf_active_mode);
 8365
 8366	mempool_free(mboxq, phba->mbox_mem_pool);
 8367
 8368	/* Initialize atomic counters */
 8369	atomic_set(&phba->cgn_fabric_warn_cnt, 0);
 8370	atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
 8371	atomic_set(&phba->cgn_sync_alarm_cnt, 0);
 8372	atomic_set(&phba->cgn_sync_warn_cnt, 0);
 8373	atomic_set(&phba->cgn_driver_evt_cnt, 0);
 8374	atomic_set(&phba->cgn_latency_evt_cnt, 0);
 8375	atomic64_set(&phba->cgn_latency_evt, 0);
 8376
 8377	phba->cmf_interval_rate = LPFC_CMF_INTERVAL;
 8378
 8379	/* Allocate RX Monitor Buffer */
 8380	if (!phba->rx_monitor) {
 8381		phba->rx_monitor = kzalloc(sizeof(*phba->rx_monitor),
 8382					   GFP_KERNEL);
 8383
 8384		if (!phba->rx_monitor) {
 8385			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
 8386					"2644 Failed to alloc memory "
 8387					"for RX Monitor Buffer\n");
 8388			return -ENOMEM;
 8389		}
 8390
 8391		/* Instruct the rx_monitor object to instantiate its ring */
 8392		if (lpfc_rx_monitor_create_ring(phba->rx_monitor,
 8393						LPFC_MAX_RXMONITOR_ENTRY)) {
 8394			kfree(phba->rx_monitor);
 8395			phba->rx_monitor = NULL;
 8396			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
 8397					"2645 Failed to alloc memory "
 8398					"for RX Monitor's Ring\n");
 8399			return -ENOMEM;
 8400		}
 8401	}
 8402
 8403	return 0;
 8404}
 8405
 8406static int
 8407lpfc_set_host_tm(struct lpfc_hba *phba)
 8408{
 8409	LPFC_MBOXQ_t *mboxq;
 8410	uint32_t len, rc;
 8411	struct timespec64 cur_time;
 8412	struct tm broken;
 8413	uint32_t month, day, year;
 8414	uint32_t hour, minute, second;
 8415	struct lpfc_mbx_set_host_date_time *tm;
 8416
 8417	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 8418	if (!mboxq)
 8419		return -ENOMEM;
 8420
 8421	len = sizeof(struct lpfc_mbx_set_host_data) -
 8422		sizeof(struct lpfc_sli4_cfg_mhdr);
 8423	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
 8424			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
 8425			 LPFC_SLI4_MBX_EMBED);
 8426
 8427	mboxq->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_DATE_TIME;
 8428	mboxq->u.mqe.un.set_host_data.param_len =
 8429			sizeof(struct lpfc_mbx_set_host_date_time);
 8430	tm = &mboxq->u.mqe.un.set_host_data.un.tm;
 8431	ktime_get_real_ts64(&cur_time);
 8432	time64_to_tm(cur_time.tv_sec, 0, &broken);
 8433	month = broken.tm_mon + 1;
 8434	day = broken.tm_mday;
 8435	year = broken.tm_year - 100;
 8436	hour = broken.tm_hour;
 8437	minute = broken.tm_min;
 8438	second = broken.tm_sec;
 8439	bf_set(lpfc_mbx_set_host_month, tm, month);
 8440	bf_set(lpfc_mbx_set_host_day, tm, day);
 8441	bf_set(lpfc_mbx_set_host_year, tm, year);
 8442	bf_set(lpfc_mbx_set_host_hour, tm, hour);
 8443	bf_set(lpfc_mbx_set_host_min, tm, minute);
 8444	bf_set(lpfc_mbx_set_host_sec, tm, second);
 8445
 8446	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 8447	mempool_free(mboxq, phba->mbox_mem_pool);
 8448	return rc;
 8449}
 8450
 8451/**
 8452 * lpfc_get_platform_uuid - Attempts to extract a platform uuid
 8453 * @phba: pointer to lpfc hba data structure.
 8454 *
 8455 * This routine attempts to first read SMBIOS DMI data for the System
 8456 * Information structure offset 08h called System UUID.  Else, no platform
 8457 * UUID will be advertised.
 8458 **/
 8459static void
 8460lpfc_get_platform_uuid(struct lpfc_hba *phba)
 8461{
 8462	int rc;
 8463	const char *uuid;
 8464	char pni[17] = {0}; /* 16 characters + '\0' */
 8465	bool is_ff = true, is_00 = true;
 8466	u8 i;
 8467
 8468	/* First attempt SMBIOS DMI */
 8469	uuid = dmi_get_system_info(DMI_PRODUCT_UUID);
 8470	if (uuid) {
 8471		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
 8472				"2088 SMBIOS UUID %s\n",
 8473				uuid);
 8474	} else {
 8475		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
 8476				"2099 Could not extract UUID\n");
 8477	}
 8478
 8479	if (uuid && uuid_is_valid(uuid)) {
 8480		/* Generate PNI from UUID format.
 8481		 *
 8482		 * 1.) Extract lower 64 bits from UUID format.
 8483		 * 2.) Set 3h for NAA Locally Assigned Name Identifier format.
 8484		 *
 8485		 * e.g. xxxxxxxx-xxxx-xxxx-yyyy-yyyyyyyyyyyy
 8486		 *
 8487		 * extract the yyyy-yyyyyyyyyyyy portion
 8488		 * final PNI   3yyyyyyyyyyyyyyy
 8489		 */
 8490		scnprintf(pni, sizeof(pni), "3%c%c%c%s",
 8491			  uuid[20], uuid[21], uuid[22], &uuid[24]);
 8492
 8493		/* Sanitize the converted PNI */
 8494		for (i = 1; i < 16 && (is_ff || is_00); i++) {
 8495			if (pni[i] != '0')
 8496				is_00 = false;
 8497			if (pni[i] != 'f' && pni[i] != 'F')
 8498				is_ff = false;
 8499		}
 8500
 8501		/* Convert from char* to unsigned long */
 8502		rc = kstrtoul(pni, 16, &phba->pni);
 8503		if (!rc && !is_ff && !is_00) {
 8504			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
 8505					"2100 PNI 0x%016lx\n", phba->pni);
 8506		} else {
 8507			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
 8508					"2101 PNI %s generation status %d\n",
 8509					pni, rc);
 8510			phba->pni = 0;
 8511		}
 8512	}
 8513}
 8514
 8515/**
 8516 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
 8517 * @phba: Pointer to HBA context object.
 8518 *
 8519 * This function is the main SLI4 device initialization PCI function. This
 8520 * function is called by the HBA initialization code, HBA reset code and
 8521 * HBA error attention handler code. Caller is not required to hold any
 8522 * locks.
 8523 **/
 8524int
 8525lpfc_sli4_hba_setup(struct lpfc_hba *phba)
 8526{
 8527	int rc, i, cnt, len, dd;
 8528	LPFC_MBOXQ_t *mboxq;
 8529	struct lpfc_mqe *mqe;
 8530	uint8_t *vpd;
 8531	uint32_t vpd_size;
 8532	uint32_t ftr_rsp = 0;
 8533	struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
 8534	struct lpfc_vport *vport = phba->pport;
 8535	struct lpfc_dmabuf *mp;
 8536	struct lpfc_rqb *rqbp;
 8537	u32 flg;
 8538
 8539	/* Perform a PCI function reset to start from clean */
 8540	rc = lpfc_pci_function_reset(phba);
 8541	if (unlikely(rc))
 8542		return -ENODEV;
 8543
 8544	/* Check the HBA Host Status Register for readyness */
 8545	rc = lpfc_sli4_post_status_check(phba);
 8546	if (unlikely(rc))
 8547		return -ENODEV;
 8548	else {
 8549		spin_lock_irq(&phba->hbalock);
 8550		phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
 8551		flg = phba->sli.sli_flag;
 8552		spin_unlock_irq(&phba->hbalock);
 8553		/* Allow a little time after setting SLI_ACTIVE for any polled
 8554		 * MBX commands to complete via BSG.
 8555		 */
 8556		for (i = 0; i < 50 && (flg & LPFC_SLI_MBOX_ACTIVE); i++) {
 8557			msleep(20);
 8558			spin_lock_irq(&phba->hbalock);
 8559			flg = phba->sli.sli_flag;
 8560			spin_unlock_irq(&phba->hbalock);
 8561		}
 8562	}
 8563	clear_bit(HBA_SETUP, &phba->hba_flag);
 8564
 8565	lpfc_sli4_dip(phba);
 8566
 8567	/*
 8568	 * Allocate a single mailbox container for initializing the
 8569	 * port.
 8570	 */
 8571	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
 8572	if (!mboxq)
 8573		return -ENOMEM;
 8574
 8575	/* Issue READ_REV to collect vpd and FW information. */
 8576	vpd_size = SLI4_PAGE_SIZE;
 8577	vpd = kzalloc(vpd_size, GFP_KERNEL);
 8578	if (!vpd) {
 8579		rc = -ENOMEM;
 8580		goto out_free_mbox;
 8581	}
 8582
 8583	rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
 8584	if (unlikely(rc)) {
 8585		kfree(vpd);
 8586		goto out_free_mbox;
 8587	}
 8588
 8589	mqe = &mboxq->u.mqe;
 8590	phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
 8591	if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
 8592		set_bit(HBA_FCOE_MODE, &phba->hba_flag);
 8593		phba->fcp_embed_io = 0;	/* SLI4 FC support only */
 8594	} else {
 8595		clear_bit(HBA_FCOE_MODE, &phba->hba_flag);
 8596	}
 8597
 8598	/* Obtain platform UUID, only for SLI4 FC adapters */
 8599	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag))
 8600		lpfc_get_platform_uuid(phba);
 8601
 8602	if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
 8603		LPFC_DCBX_CEE_MODE)
 8604		set_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
 8605	else
 8606		clear_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
 8607
 8608	clear_bit(HBA_IOQ_FLUSH, &phba->hba_flag);
 8609
 8610	if (phba->sli_rev != LPFC_SLI_REV4) {
 8611		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 8612			"0376 READ_REV Error. SLI Level %d "
 8613			"FCoE enabled %d\n",
 8614			phba->sli_rev,
 8615			test_bit(HBA_FCOE_MODE, &phba->hba_flag) ? 1 : 0);
 8616		rc = -EIO;
 8617		kfree(vpd);
 8618		goto out_free_mbox;
 8619	}
 8620
 8621	rc = lpfc_set_host_tm(phba);
 8622	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
 8623			"6468 Set host date / time: Status x%x:\n", rc);
 8624
 8625	/*
 8626	 * Continue initialization with default values even if driver failed
 8627	 * to read FCoE param config regions, only read parameters if the
 8628	 * board is FCoE
 8629	 */
 8630	if (test_bit(HBA_FCOE_MODE, &phba->hba_flag) &&
 8631	    lpfc_sli4_read_fcoe_params(phba))
 8632		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
 8633			"2570 Failed to read FCoE parameters\n");
 8634
 8635	/*
 8636	 * Retrieve sli4 device physical port name, failure of doing it
 8637	 * is considered as non-fatal.
 8638	 */
 8639	rc = lpfc_sli4_retrieve_pport_name(phba);
 8640	if (!rc)
 8641		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
 8642				"3080 Successful retrieving SLI4 device "
 8643				"physical port name: %s.\n", phba->Port);
 8644
 8645	rc = lpfc_sli4_get_ctl_attr(phba);
 8646	if (!rc)
 8647		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
 8648				"8351 Successful retrieving SLI4 device "
 8649				"CTL ATTR\n");
 8650
 8651	/*
 8652	 * Evaluate the read rev and vpd data. Populate the driver
 8653	 * state with the results. If this routine fails, the failure
 8654	 * is not fatal as the driver will use generic values.
 8655	 */
 8656	rc = lpfc_parse_vpd(phba, vpd, vpd_size);
 8657	if (unlikely(!rc))
 8658		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 8659				"0377 Error %d parsing vpd. "
 8660				"Using defaults.\n", rc);
 8661	kfree(vpd);
 8662
 8663	/* Save information as VPD data */
 8664	phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
 8665	phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
 8666
 8667	/*
 8668	 * This is because first G7 ASIC doesn't support the standard
 8669	 * 0x5a NVME cmd descriptor type/subtype
 8670	 */
 8671	if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
 8672			LPFC_SLI_INTF_IF_TYPE_6) &&
 8673	    (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
 8674	    (phba->vpd.rev.smRev == 0) &&
 8675	    (phba->cfg_nvme_embed_cmd == 1))
 8676		phba->cfg_nvme_embed_cmd = 0;
 8677
 8678	phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
 8679	phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
 8680					 &mqe->un.read_rev);
 8681	phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
 8682				       &mqe->un.read_rev);
 8683	phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
 8684					    &mqe->un.read_rev);
 8685	phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
 8686					   &mqe->un.read_rev);
 8687	phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
 8688	memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
 8689	phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
 8690	memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
 8691	phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
 8692	memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
 8693	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
 8694			"(%d):0380 READ_REV Status x%x "
 8695			"fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
 8696			mboxq->vport ? mboxq->vport->vpi : 0,
 8697			bf_get(lpfc_mqe_status, mqe),
 8698			phba->vpd.rev.opFwName,
 8699			phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
 8700			phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
 8701
 8702	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
 8703	    LPFC_SLI_INTF_IF_TYPE_0) {
 8704		lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
 8705		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 8706		if (rc == MBX_SUCCESS) {
 8707			set_bit(HBA_RECOVERABLE_UE, &phba->hba_flag);
 8708			/* Set 1Sec interval to detect UE */
 8709			phba->eratt_poll_interval = 1;
 8710			phba->sli4_hba.ue_to_sr = bf_get(
 8711					lpfc_mbx_set_feature_UESR,
 8712					&mboxq->u.mqe.un.set_feature);
 8713			phba->sli4_hba.ue_to_rp = bf_get(
 8714					lpfc_mbx_set_feature_UERP,
 8715					&mboxq->u.mqe.un.set_feature);
 8716		}
 8717	}
 8718
 8719	if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
 8720		/* Enable MDS Diagnostics only if the SLI Port supports it */
 8721		lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
 8722		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 8723		if (rc != MBX_SUCCESS)
 8724			phba->mds_diags_support = 0;
 8725	}
 8726
 8727	/*
 8728	 * Discover the port's supported feature set and match it against the
 8729	 * hosts requests.
 8730	 */
 8731	lpfc_request_features(phba, mboxq);
 8732	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 8733	if (unlikely(rc)) {
 8734		rc = -EIO;
 8735		goto out_free_mbox;
 8736	}
 8737
 8738	/* Disable VMID if app header is not supported */
 8739	if (phba->cfg_vmid_app_header && !(bf_get(lpfc_mbx_rq_ftr_rsp_ashdr,
 8740						  &mqe->un.req_ftrs))) {
 8741		bf_set(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags, 0);
 8742		phba->cfg_vmid_app_header = 0;
 8743		lpfc_printf_log(phba, KERN_DEBUG, LOG_SLI,
 8744				"1242 vmid feature not supported\n");
 8745	}
 8746
 8747	/*
 8748	 * The port must support FCP initiator mode as this is the
 8749	 * only mode running in the host.
 8750	 */
 8751	if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
 8752		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
 8753				"0378 No support for fcpi mode.\n");
 8754		ftr_rsp++;
 8755	}
 8756
 8757	/* Performance Hints are ONLY for FCoE */
 8758	if (test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
 8759		if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
 8760			phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
 8761		else
 8762			phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
 8763	}
 8764
 8765	/*
 8766	 * If the port cannot support the host's requested features
 8767	 * then turn off the global config parameters to disable the
 8768	 * feature in the driver.  This is not a fatal error.
 8769	 */
 8770	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
 8771		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
 8772			phba->cfg_enable_bg = 0;
 8773			phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
 8774			ftr_rsp++;
 8775		}
 8776	}
 8777
 8778	if (phba->max_vpi && phba->cfg_enable_npiv &&
 8779	    !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
 8780		ftr_rsp++;
 8781
 8782	if (ftr_rsp) {
 8783		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
 8784				"0379 Feature Mismatch Data: x%08x %08x "
 8785				"x%x x%x x%x\n", mqe->un.req_ftrs.word2,
 8786				mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
 8787				phba->cfg_enable_npiv, phba->max_vpi);
 8788		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
 8789			phba->cfg_enable_bg = 0;
 8790		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
 8791			phba->cfg_enable_npiv = 0;
 8792	}
 8793
 8794	/* These SLI3 features are assumed in SLI4 */
 8795	spin_lock_irq(&phba->hbalock);
 8796	phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
 8797	spin_unlock_irq(&phba->hbalock);
 8798
 8799	/* Always try to enable dual dump feature if we can */
 8800	lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
 8801	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 8802	dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
 8803	if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
 8804		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
 8805				"6448 Dual Dump is enabled\n");
 8806	else
 8807		lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
 8808				"6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
 8809				"rc:x%x dd:x%x\n",
 8810				bf_get(lpfc_mqe_command, &mboxq->u.mqe),
 8811				lpfc_sli_config_mbox_subsys_get(
 8812					phba, mboxq),
 8813				lpfc_sli_config_mbox_opcode_get(
 8814					phba, mboxq),
 8815				rc, dd);
 8816
 8817	/*
 8818	 * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent
 8819	 * calls depends on these resources to complete port setup.
 8820	 */
 8821	rc = lpfc_sli4_alloc_resource_identifiers(phba);
 8822	if (rc) {
 8823		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 8824				"2920 Failed to alloc Resource IDs "
 8825				"rc = x%x\n", rc);
 8826		goto out_free_mbox;
 8827	}
 8828
 8829	lpfc_sli4_node_rpi_restore(phba);
 8830
 8831	lpfc_set_host_data(phba, mboxq);
 8832
 8833	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 8834	if (rc) {
 8835		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
 8836				"2134 Failed to set host os driver version %x",
 8837				rc);
 8838	}
 8839
 8840	/* Read the port's service parameters. */
 8841	rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
 8842	if (rc) {
 8843		phba->link_state = LPFC_HBA_ERROR;
 8844		rc = -ENOMEM;
 8845		goto out_free_mbox;
 8846	}
 8847
 8848	mboxq->vport = vport;
 8849	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 8850	mp = mboxq->ctx_buf;
 8851	if (rc == MBX_SUCCESS) {
 8852		memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
 8853		rc = 0;
 8854	}
 8855
 8856	/*
 8857	 * This memory was allocated by the lpfc_read_sparam routine but is
 8858	 * no longer needed.  It is released and ctx_buf NULLed to prevent
 8859	 * unintended pointer access as the mbox is reused.
 8860	 */
 8861	lpfc_mbuf_free(phba, mp->virt, mp->phys);
 8862	kfree(mp);
 8863	mboxq->ctx_buf = NULL;
 8864	if (unlikely(rc)) {
 8865		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 8866				"0382 READ_SPARAM command failed "
 8867				"status %d, mbxStatus x%x\n",
 8868				rc, bf_get(lpfc_mqe_status, mqe));
 8869		phba->link_state = LPFC_HBA_ERROR;
 8870		rc = -EIO;
 8871		goto out_free_mbox;
 8872	}
 8873
 8874	lpfc_update_vport_wwn(vport);
 8875
 8876	/* Update the fc_host data structures with new wwn. */
 8877	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
 8878	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
 8879
 8880	/* Create all the SLI4 queues */
 8881	rc = lpfc_sli4_queue_create(phba);
 8882	if (rc) {
 8883		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 8884				"3089 Failed to allocate queues\n");
 8885		rc = -ENODEV;
 8886		goto out_free_mbox;
 8887	}
 8888	/* Set up all the queues to the device */
 8889	rc = lpfc_sli4_queue_setup(phba);
 8890	if (unlikely(rc)) {
 8891		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 8892				"0381 Error %d during queue setup.\n", rc);
 8893		goto out_destroy_queue;
 8894	}
 8895	/* Initialize the driver internal SLI layer lists. */
 8896	lpfc_sli4_setup(phba);
 8897	lpfc_sli4_queue_init(phba);
 8898
 8899	/* update host els xri-sgl sizes and mappings */
 8900	rc = lpfc_sli4_els_sgl_update(phba);
 8901	if (unlikely(rc)) {
 8902		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 8903				"1400 Failed to update xri-sgl size and "
 8904				"mapping: %d\n", rc);
 8905		goto out_destroy_queue;
 8906	}
 8907
 8908	/* register the els sgl pool to the port */
 8909	rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
 8910				       phba->sli4_hba.els_xri_cnt);
 8911	if (unlikely(rc < 0)) {
 8912		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 8913				"0582 Error %d during els sgl post "
 8914				"operation\n", rc);
 8915		rc = -ENODEV;
 8916		goto out_destroy_queue;
 8917	}
 8918	phba->sli4_hba.els_xri_cnt = rc;
 8919
 8920	if (phba->nvmet_support) {
 8921		/* update host nvmet xri-sgl sizes and mappings */
 8922		rc = lpfc_sli4_nvmet_sgl_update(phba);
 8923		if (unlikely(rc)) {
 8924			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 8925					"6308 Failed to update nvmet-sgl size "
 8926					"and mapping: %d\n", rc);
 8927			goto out_destroy_queue;
 8928		}
 8929
 8930		/* register the nvmet sgl pool to the port */
 8931		rc = lpfc_sli4_repost_sgl_list(
 8932			phba,
 8933			&phba->sli4_hba.lpfc_nvmet_sgl_list,
 8934			phba->sli4_hba.nvmet_xri_cnt);
 8935		if (unlikely(rc < 0)) {
 8936			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 8937					"3117 Error %d during nvmet "
 8938					"sgl post\n", rc);
 8939			rc = -ENODEV;
 8940			goto out_destroy_queue;
 8941		}
 8942		phba->sli4_hba.nvmet_xri_cnt = rc;
 8943
 8944		/* We allocate an iocbq for every receive context SGL.
 8945		 * The additional allocation is for abort and ls handling.
 8946		 */
 8947		cnt = phba->sli4_hba.nvmet_xri_cnt +
 8948			phba->sli4_hba.max_cfg_param.max_xri;
 8949	} else {
 8950		/* update host common xri-sgl sizes and mappings */
 8951		rc = lpfc_sli4_io_sgl_update(phba);
 8952		if (unlikely(rc)) {
 8953			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 8954					"6082 Failed to update nvme-sgl size "
 8955					"and mapping: %d\n", rc);
 8956			goto out_destroy_queue;
 8957		}
 8958
 8959		/* register the allocated common sgl pool to the port */
 8960		rc = lpfc_sli4_repost_io_sgl_list(phba);
 8961		if (unlikely(rc)) {
 8962			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 8963					"6116 Error %d during nvme sgl post "
 8964					"operation\n", rc);
 8965			/* Some NVME buffers were moved to abort nvme list */
 8966			/* A pci function reset will repost them */
 8967			rc = -ENODEV;
 8968			goto out_destroy_queue;
 8969		}
 8970		/* Each lpfc_io_buf job structure has an iocbq element.
 8971		 * This cnt provides for abort, els, ct and ls requests.
 8972		 */
 8973		cnt = phba->sli4_hba.max_cfg_param.max_xri;
 8974	}
 8975
 8976	if (!phba->sli.iocbq_lookup) {
 8977		/* Initialize and populate the iocb list per host */
 8978		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
 8979				"2821 initialize iocb list with %d entries\n",
 8980				cnt);
 8981		rc = lpfc_init_iocb_list(phba, cnt);
 8982		if (rc) {
 8983			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 8984					"1413 Failed to init iocb list.\n");
 8985			goto out_destroy_queue;
 8986		}
 8987	}
 8988
 8989	if (phba->nvmet_support)
 8990		lpfc_nvmet_create_targetport(phba);
 8991
 8992	if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
 8993		/* Post initial buffers to all RQs created */
 8994		for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
 8995			rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
 8996			INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
 8997			rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
 8998			rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
 8999			rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
 9000			rqbp->buffer_count = 0;
 9001
 9002			lpfc_post_rq_buffer(
 9003				phba, phba->sli4_hba.nvmet_mrq_hdr[i],
 9004				phba->sli4_hba.nvmet_mrq_data[i],
 9005				phba->cfg_nvmet_mrq_post, i);
 9006		}
 9007	}
 9008
 9009	/* Post the rpi header region to the device. */
 9010	rc = lpfc_sli4_post_all_rpi_hdrs(phba);
 9011	if (unlikely(rc)) {
 9012		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 9013				"0393 Error %d during rpi post operation\n",
 9014				rc);
 9015		rc = -ENODEV;
 9016		goto out_free_iocblist;
 9017	}
 9018
 9019	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {
 9020		if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
 9021			/*
 9022			 * The FC Port needs to register FCFI (index 0)
 9023			 */
 9024			lpfc_reg_fcfi(phba, mboxq);
 9025			mboxq->vport = phba->pport;
 9026			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 9027			if (rc != MBX_SUCCESS)
 9028				goto out_unset_queue;
 9029			rc = 0;
 9030			phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
 9031						&mboxq->u.mqe.un.reg_fcfi);
 9032		} else {
 9033			/* We are a NVME Target mode with MRQ > 1 */
 9034
 9035			/* First register the FCFI */
 9036			lpfc_reg_fcfi_mrq(phba, mboxq, 0);
 9037			mboxq->vport = phba->pport;
 9038			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 9039			if (rc != MBX_SUCCESS)
 9040				goto out_unset_queue;
 9041			rc = 0;
 9042			phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
 9043						&mboxq->u.mqe.un.reg_fcfi_mrq);
 9044
 9045			/* Next register the MRQs */
 9046			lpfc_reg_fcfi_mrq(phba, mboxq, 1);
 9047			mboxq->vport = phba->pport;
 9048			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 9049			if (rc != MBX_SUCCESS)
 9050				goto out_unset_queue;
 9051			rc = 0;
 9052		}
 9053		/* Check if the port is configured to be disabled */
 9054		lpfc_sli_read_link_ste(phba);
 9055	}
 9056
 9057	/* Don't post more new bufs if repost already recovered
 9058	 * the nvme sgls.
 9059	 */
 9060	if (phba->nvmet_support == 0) {
 9061		if (phba->sli4_hba.io_xri_cnt == 0) {
 9062			len = lpfc_new_io_buf(
 9063					      phba, phba->sli4_hba.io_xri_max);
 9064			if (len == 0) {
 9065				rc = -ENOMEM;
 9066				goto out_unset_queue;
 9067			}
 9068
 9069			if (phba->cfg_xri_rebalancing)
 9070				lpfc_create_multixri_pools(phba);
 9071		}
 9072	} else {
 9073		phba->cfg_xri_rebalancing = 0;
 9074	}
 9075
 9076	/* Allow asynchronous mailbox command to go through */
 9077	spin_lock_irq(&phba->hbalock);
 9078	phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
 9079	spin_unlock_irq(&phba->hbalock);
 9080
 9081	/* Post receive buffers to the device */
 9082	lpfc_sli4_rb_setup(phba);
 9083
 9084	/* Reset HBA FCF states after HBA reset */
 9085	phba->fcf.fcf_flag = 0;
 9086	phba->fcf.current_rec.flag = 0;
 9087
 9088	/* Start the ELS watchdog timer */
 9089	mod_timer(&vport->els_tmofunc,
 9090			jiffies + secs_to_jiffies(phba->fc_ratov * 2));
 9091
 9092	/* Start heart beat timer */
 9093	mod_timer(&phba->hb_tmofunc,
 9094		  jiffies + secs_to_jiffies(LPFC_HB_MBOX_INTERVAL));
 9095	clear_bit(HBA_HBEAT_INP, &phba->hba_flag);
 9096	clear_bit(HBA_HBEAT_TMO, &phba->hba_flag);
 9097	phba->last_completion_time = jiffies;
 9098
 9099	/* start eq_delay heartbeat */
 9100	if (phba->cfg_auto_imax)
 9101		queue_delayed_work(phba->wq, &phba->eq_delay_work,
 9102				   msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
 9103
 9104	/* start per phba idle_stat_delay heartbeat */
 9105	lpfc_init_idle_stat_hb(phba);
 9106
 9107	/* Start error attention (ERATT) polling timer */
 9108	mod_timer(&phba->eratt_poll,
 9109		  jiffies + secs_to_jiffies(phba->eratt_poll_interval));
 9110
 9111	/*
 9112	 * The port is ready, set the host's link state to LINK_DOWN
 9113	 * in preparation for link interrupts.
 9114	 */
 9115	spin_lock_irq(&phba->hbalock);
 9116	phba->link_state = LPFC_LINK_DOWN;
 9117
 9118	/* Check if physical ports are trunked */
 9119	if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
 9120		phba->trunk_link.link0.state = LPFC_LINK_DOWN;
 9121	if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
 9122		phba->trunk_link.link1.state = LPFC_LINK_DOWN;
 9123	if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
 9124		phba->trunk_link.link2.state = LPFC_LINK_DOWN;
 9125	if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
 9126		phba->trunk_link.link3.state = LPFC_LINK_DOWN;
 9127	spin_unlock_irq(&phba->hbalock);
 9128
 9129	/* Arm the CQs and then EQs on device */
 9130	lpfc_sli4_arm_cqeq_intr(phba);
 9131
 9132	/* Indicate device interrupt mode */
 9133	phba->sli4_hba.intr_enable = 1;
 9134
 9135	/* Setup CMF after HBA is initialized */
 9136	lpfc_cmf_setup(phba);
 9137
 9138	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag) &&
 9139	    test_bit(LINK_DISABLED, &phba->hba_flag)) {
 9140		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 9141				"3103 Adapter Link is disabled.\n");
 9142		lpfc_down_link(phba, mboxq);
 9143		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
 9144		if (rc != MBX_SUCCESS) {
 9145			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 9146					"3104 Adapter failed to issue "
 9147					"DOWN_LINK mbox cmd, rc:x%x\n", rc);
 9148			goto out_io_buff_free;
 9149		}
 9150	} else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
 9151		/* don't perform init_link on SLI4 FC port loopback test */
 9152		if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
 9153			rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
 9154			if (rc)
 9155				goto out_io_buff_free;
 9156		}
 9157	}
 9158	mempool_free(mboxq, phba->mbox_mem_pool);
 9159
 9160	/* Enable RAS FW log support */
 9161	lpfc_sli4_ras_setup(phba);
 9162
 9163	set_bit(HBA_SETUP, &phba->hba_flag);
 9164	return rc;
 9165
 9166out_io_buff_free:
 9167	/* Free allocated IO Buffers */
 9168	lpfc_io_free(phba);
 9169out_unset_queue:
 9170	/* Unset all the queues set up in this routine when error out */
 9171	lpfc_sli4_queue_unset(phba);
 9172out_free_iocblist:
 9173	lpfc_free_iocb_list(phba);
 9174out_destroy_queue:
 9175	lpfc_sli4_queue_destroy(phba);
 9176	lpfc_stop_hba_timers(phba);
 9177out_free_mbox:
 9178	mempool_free(mboxq, phba->mbox_mem_pool);
 9179	return rc;
 9180}
 9181
 9182/**
 9183 * lpfc_mbox_timeout - Timeout call back function for mbox timer
 9184 * @t: Context to fetch pointer to hba structure from.
 9185 *
 9186 * This is the callback function for mailbox timer. The mailbox
 9187 * timer is armed when a new mailbox command is issued and the timer
 9188 * is deleted when the mailbox complete. The function is called by
 9189 * the kernel timer code when a mailbox does not complete within
 9190 * expected time. This function wakes up the worker thread to
 9191 * process the mailbox timeout and returns. All the processing is
 9192 * done by the worker thread function lpfc_mbox_timeout_handler.
 9193 **/
 9194void
 9195lpfc_mbox_timeout(struct timer_list *t)
 9196{
 9197	struct lpfc_hba  *phba = timer_container_of(phba, t, sli.mbox_tmo);
 9198	unsigned long iflag;
 9199	uint32_t tmo_posted;
 9200
 9201	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
 9202	tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
 9203	if (!tmo_posted)
 9204		phba->pport->work_port_events |= WORKER_MBOX_TMO;
 9205	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
 9206
 9207	if (!tmo_posted)
 9208		lpfc_worker_wake_up(phba);
 9209	return;
 9210}
 9211
 9212/**
 9213 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
 9214 *                                    are pending
 9215 * @phba: Pointer to HBA context object.
 9216 *
 9217 * This function checks if any mailbox completions are present on the mailbox
 9218 * completion queue.
 9219 **/
 9220static bool
 9221lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
 9222{
 9223
 9224	uint32_t idx;
 9225	struct lpfc_queue *mcq;
 9226	struct lpfc_mcqe *mcqe;
 9227	bool pending_completions = false;
 9228	uint8_t	qe_valid;
 9229
 9230	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
 9231		return false;
 9232
 9233	/* Check for completions on mailbox completion queue */
 9234
 9235	mcq = phba->sli4_hba.mbx_cq;
 9236	idx = mcq->hba_index;
 9237	qe_valid = mcq->qe_valid;
 9238	while (bf_get_le32(lpfc_cqe_valid,
 9239	       (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
 9240		mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
 9241		if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
 9242		    (!bf_get_le32(lpfc_trailer_async, mcqe))) {
 9243			pending_completions = true;
 9244			break;
 9245		}
 9246		idx = (idx + 1) % mcq->entry_count;
 9247		if (mcq->hba_index == idx)
 9248			break;
 9249
 9250		/* if the index wrapped around, toggle the valid bit */
 9251		if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
 9252			qe_valid = (qe_valid) ? 0 : 1;
 9253	}
 9254	return pending_completions;
 9255
 9256}
 9257
 9258/**
 9259 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
 9260 *					      that were missed.
 9261 * @phba: Pointer to HBA context object.
 9262 *
 9263 * For sli4, it is possible to miss an interrupt. As such mbox completions
 9264 * maybe missed causing erroneous mailbox timeouts to occur. This function
 9265 * checks to see if mbox completions are on the mailbox completion queue
 9266 * and will process all the completions associated with the eq for the
 9267 * mailbox completion queue.
 9268 **/
 9269static bool
 9270lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
 9271{
 9272	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
 9273	uint32_t eqidx;
 9274	struct lpfc_queue *fpeq = NULL;
 9275	struct lpfc_queue *eq;
 9276	bool mbox_pending;
 9277
 9278	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
 9279		return false;
 9280
 9281	/* Find the EQ associated with the mbox CQ */
 9282	if (sli4_hba->hdwq) {
 9283		for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
 9284			eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
 9285			if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
 9286				fpeq = eq;
 9287				break;
 9288			}
 9289		}
 9290	}
 9291	if (!fpeq)
 9292		return false;
 9293
 9294	/* Turn off interrupts from this EQ */
 9295
 9296	sli4_hba->sli4_eq_clr_intr(fpeq);
 9297
 9298	/* Check to see if a mbox completion is pending */
 9299
 9300	mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
 9301
 9302	/*
 9303	 * If a mbox completion is pending, process all the events on EQ
 9304	 * associated with the mbox completion queue (this could include
 9305	 * mailbox commands, async events, els commands, receive queue data
 9306	 * and fcp commands)
 9307	 */
 9308
 9309	if (mbox_pending)
 9310		/* process and rearm the EQ */
 9311		lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
 9312				     LPFC_QUEUE_WORK);
 9313	else
 9314		/* Always clear and re-arm the EQ */
 9315		sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
 9316
 9317	return mbox_pending;
 9318
 9319}
 9320
 9321/**
 9322 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
 9323 * @phba: Pointer to HBA context object.
 9324 *
 9325 * This function is called from worker thread when a mailbox command times out.
 9326 * The caller is not required to hold any locks. This function will reset the
 9327 * HBA and recover all the pending commands.
 9328 **/
 9329void
 9330lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
 9331{
 9332	LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
 9333	MAILBOX_t *mb = NULL;
 9334
 9335	struct lpfc_sli *psli = &phba->sli;
 9336
 9337	/* If the mailbox completed, process the completion */
 9338	lpfc_sli4_process_missed_mbox_completions(phba);
 9339
 9340	if (!(psli->sli_flag & LPFC_SLI_ACTIVE))
 9341		return;
 9342
 9343	if (pmbox != NULL)
 9344		mb = &pmbox->u.mb;
 9345	/* Check the pmbox pointer first.  There is a race condition
 9346	 * between the mbox timeout handler getting executed in the
 9347	 * worklist and the mailbox actually completing. When this
 9348	 * race condition occurs, the mbox_active will be NULL.
 9349	 */
 9350	spin_lock_irq(&phba->hbalock);
 9351	if (pmbox == NULL) {
 9352		lpfc_printf_log(phba, KERN_WARNING,
 9353				LOG_MBOX | LOG_SLI,
 9354				"0353 Active Mailbox cleared - mailbox timeout "
 9355				"exiting\n");
 9356		spin_unlock_irq(&phba->hbalock);
 9357		return;
 9358	}
 9359
 9360	/* Mbox cmd <mbxCommand> timeout */
 9361	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 9362			"0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
 9363			mb->mbxCommand,
 9364			phba->pport->port_state,
 9365			phba->sli.sli_flag,
 9366			phba->sli.mbox_active);
 9367	spin_unlock_irq(&phba->hbalock);
 9368
 9369	/* Setting state unknown so lpfc_sli_abort_iocb_ring
 9370	 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
 9371	 * it to fail all outstanding SCSI IO.
 9372	 */
 9373	set_bit(MBX_TMO_ERR, &phba->bit_flags);
 9374	spin_lock_irq(&phba->pport->work_port_lock);
 9375	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
 9376	spin_unlock_irq(&phba->pport->work_port_lock);
 9377	spin_lock_irq(&phba->hbalock);
 9378	phba->link_state = LPFC_LINK_UNKNOWN;
 9379	psli->sli_flag &= ~LPFC_SLI_ACTIVE;
 9380	spin_unlock_irq(&phba->hbalock);
 9381
 9382	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 9383			"0345 Resetting board due to mailbox timeout\n");
 9384
 9385	/* Reset the HBA device */
 9386	lpfc_reset_hba(phba);
 9387}
 9388
 9389/**
 9390 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
 9391 * @phba: Pointer to HBA context object.
 9392 * @pmbox: Pointer to mailbox object.
 9393 * @flag: Flag indicating how the mailbox need to be processed.
 9394 *
 9395 * This function is called by discovery code and HBA management code
 9396 * to submit a mailbox command to firmware with SLI-3 interface spec. This
 9397 * function gets the hbalock to protect the data structures.
 9398 * The mailbox command can be submitted in polling mode, in which case
 9399 * this function will wait in a polling loop for the completion of the
 9400 * mailbox.
 9401 * If the mailbox is submitted in no_wait mode (not polling) the
 9402 * function will submit the command and returns immediately without waiting
 9403 * for the mailbox completion. The no_wait is supported only when HBA
 9404 * is in SLI2/SLI3 mode - interrupts are enabled.
 9405 * The SLI interface allows only one mailbox pending at a time. If the
 9406 * mailbox is issued in polling mode and there is already a mailbox
 9407 * pending, then the function will return an error. If the mailbox is issued
 9408 * in NO_WAIT mode and there is a mailbox pending already, the function
 9409 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
 9410 * The sli layer owns the mailbox object until the completion of mailbox
 9411 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
 9412 * return codes the caller owns the mailbox command after the return of
 9413 * the function.
 9414 **/
 9415static int
 9416lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
 9417		       uint32_t flag)
 9418{
 9419	MAILBOX_t *mbx;
 9420	struct lpfc_sli *psli = &phba->sli;
 9421	uint32_t status, evtctr;
 9422	uint32_t ha_copy, hc_copy;
 9423	int i;
 9424	unsigned long timeout;
 9425	unsigned long drvr_flag = 0;
 9426	uint32_t word0, ldata;
 9427	void __iomem *to_slim;
 9428	int processing_queue = 0;
 9429
 9430	spin_lock_irqsave(&phba->hbalock, drvr_flag);
 9431	if (!pmbox) {
 9432		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 9433		/* processing mbox queue from intr_handler */
 9434		if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
 9435			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 9436			return MBX_SUCCESS;
 9437		}
 9438		processing_queue = 1;
 9439		pmbox = lpfc_mbox_get(phba);
 9440		if (!pmbox) {
 9441			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 9442			return MBX_SUCCESS;
 9443		}
 9444	}
 9445
 9446	if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
 9447		pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
 9448		if(!pmbox->vport) {
 9449			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 9450			lpfc_printf_log(phba, KERN_ERR,
 9451					LOG_MBOX | LOG_VPORT,
 9452					"1806 Mbox x%x failed. No vport\n",
 9453					pmbox->u.mb.mbxCommand);
 9454			dump_stack();
 9455			goto out_not_finished;
 9456		}
 9457	}
 9458
 9459	/* If the PCI channel is in offline state, do not post mbox. */
 9460	if (unlikely(pci_channel_offline(phba->pcidev))) {
 9461		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 9462		goto out_not_finished;
 9463	}
 9464
 9465	/* If HBA has a deferred error attention, fail the iocb. */
 9466	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
 9467		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 9468		goto out_not_finished;
 9469	}
 9470
 9471	psli = &phba->sli;
 9472
 9473	mbx = &pmbox->u.mb;
 9474	status = MBX_SUCCESS;
 9475
 9476	if (phba->link_state == LPFC_HBA_ERROR) {
 9477		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 9478
 9479		/* Mbox command <mbxCommand> cannot issue */
 9480		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 9481				"(%d):0311 Mailbox command x%x cannot "
 9482				"issue Data: x%x x%x\n",
 9483				pmbox->vport ? pmbox->vport->vpi : 0,
 9484				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
 9485		goto out_not_finished;
 9486	}
 9487
 9488	if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
 9489		if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
 9490			!(hc_copy & HC_MBINT_ENA)) {
 9491			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 9492			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 9493				"(%d):2528 Mailbox command x%x cannot "
 9494				"issue Data: x%x x%x\n",
 9495				pmbox->vport ? pmbox->vport->vpi : 0,
 9496				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
 9497			goto out_not_finished;
 9498		}
 9499	}
 9500
 9501	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
 9502		/* Polling for a mbox command when another one is already active
 9503		 * is not allowed in SLI. Also, the driver must have established
 9504		 * SLI2 mode to queue and process multiple mbox commands.
 9505		 */
 9506
 9507		if (flag & MBX_POLL) {
 9508			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 9509
 9510			/* Mbox command <mbxCommand> cannot issue */
 9511			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 9512					"(%d):2529 Mailbox command x%x "
 9513					"cannot issue Data: x%x x%x\n",
 9514					pmbox->vport ? pmbox->vport->vpi : 0,
 9515					pmbox->u.mb.mbxCommand,
 9516					psli->sli_flag, flag);
 9517			goto out_not_finished;
 9518		}
 9519
 9520		if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
 9521			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 9522			/* Mbox command <mbxCommand> cannot issue */
 9523			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 9524					"(%d):2530 Mailbox command x%x "
 9525					"cannot issue Data: x%x x%x\n",
 9526					pmbox->vport ? pmbox->vport->vpi : 0,
 9527					pmbox->u.mb.mbxCommand,
 9528					psli->sli_flag, flag);
 9529			goto out_not_finished;
 9530		}
 9531
 9532		/* Another mailbox command is still being processed, queue this
 9533		 * command to be processed later.
 9534		 */
 9535		lpfc_mbox_put(phba, pmbox);
 9536
 9537		/* Mbox cmd issue - BUSY */
 9538		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
 9539				"(%d):0308 Mbox cmd issue - BUSY Data: "
 9540				"x%x x%x x%x x%x\n",
 9541				pmbox->vport ? pmbox->vport->vpi : 0xffffff,
 9542				mbx->mbxCommand,
 9543				phba->pport ? phba->pport->port_state : 0xff,
 9544				psli->sli_flag, flag);
 9545
 9546		psli->slistat.mbox_busy++;
 9547		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 9548
 9549		if (pmbox->vport) {
 9550			lpfc_debugfs_disc_trc(pmbox->vport,
 9551				LPFC_DISC_TRC_MBOX_VPORT,
 9552				"MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
 9553				(uint32_t)mbx->mbxCommand,
 9554				mbx->un.varWords[0], mbx->un.varWords[1]);
 9555		}
 9556		else {
 9557			lpfc_debugfs_disc_trc(phba->pport,
 9558				LPFC_DISC_TRC_MBOX,
 9559				"MBOX Bsy:        cmd:x%x mb:x%x x%x",
 9560				(uint32_t)mbx->mbxCommand,
 9561				mbx->un.varWords[0], mbx->un.varWords[1]);
 9562		}
 9563
 9564		return MBX_BUSY;
 9565	}
 9566
 9567	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
 9568
 9569	/* If we are not polling, we MUST be in SLI2 mode */
 9570	if (flag != MBX_POLL) {
 9571		if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
 9572		    (mbx->mbxCommand != MBX_KILL_BOARD)) {
 9573			psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 9574			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 9575			/* Mbox command <mbxCommand> cannot issue */
 9576			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 9577					"(%d):2531 Mailbox command x%x "
 9578					"cannot issue Data: x%x x%x\n",
 9579					pmbox->vport ? pmbox->vport->vpi : 0,
 9580					pmbox->u.mb.mbxCommand,
 9581					psli->sli_flag, flag);
 9582			goto out_not_finished;
 9583		}
 9584		/* timeout active mbox command */
 9585		timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox));
 9586		mod_timer(&psli->mbox_tmo, jiffies + timeout);
 9587	}
 9588
 9589	/* Mailbox cmd <cmd> issue */
 9590	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
 9591			"(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
 9592			"x%x\n",
 9593			pmbox->vport ? pmbox->vport->vpi : 0,
 9594			mbx->mbxCommand,
 9595			phba->pport ? phba->pport->port_state : 0xff,
 9596			psli->sli_flag, flag);
 9597
 9598	if (mbx->mbxCommand != MBX_HEARTBEAT) {
 9599		if (pmbox->vport) {
 9600			lpfc_debugfs_disc_trc(pmbox->vport,
 9601				LPFC_DISC_TRC_MBOX_VPORT,
 9602				"MBOX Send vport: cmd:x%x mb:x%x x%x",
 9603				(uint32_t)mbx->mbxCommand,
 9604				mbx->un.varWords[0], mbx->un.varWords[1]);
 9605		}
 9606		else {
 9607			lpfc_debugfs_disc_trc(phba->pport,
 9608				LPFC_DISC_TRC_MBOX,
 9609				"MBOX Send:       cmd:x%x mb:x%x x%x",
 9610				(uint32_t)mbx->mbxCommand,
 9611				mbx->un.varWords[0], mbx->un.varWords[1]);
 9612		}
 9613	}
 9614
 9615	psli->slistat.mbox_cmd++;
 9616	evtctr = psli->slistat.mbox_event;
 9617
 9618	/* next set own bit for the adapter and copy over command word */
 9619	mbx->mbxOwner = OWN_CHIP;
 9620
 9621	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
 9622		/* Populate mbox extension offset word. */
 9623		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
 9624			*(((uint32_t *)mbx) + pmbox->mbox_offset_word)
 9625				= (uint8_t *)phba->mbox_ext
 9626				  - (uint8_t *)phba->mbox;
 9627		}
 9628
 9629		/* Copy the mailbox extension data */
 9630		if (pmbox->in_ext_byte_len && pmbox->ext_buf) {
 9631			lpfc_sli_pcimem_bcopy(pmbox->ext_buf,
 9632					      (uint8_t *)phba->mbox_ext,
 9633					      pmbox->in_ext_byte_len);
 9634		}
 9635		/* Copy command data to host SLIM area */
 9636		lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
 9637	} else {
 9638		/* Populate mbox extension offset word. */
 9639		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
 9640			*(((uint32_t *)mbx) + pmbox->mbox_offset_word)
 9641				= MAILBOX_HBA_EXT_OFFSET;
 9642
 9643		/* Copy the mailbox extension data */
 9644		if (pmbox->in_ext_byte_len && pmbox->ext_buf)
 9645			lpfc_memcpy_to_slim(phba->MBslimaddr +
 9646				MAILBOX_HBA_EXT_OFFSET,
 9647				pmbox->ext_buf, pmbox->in_ext_byte_len);
 9648
 9649		if (mbx->mbxCommand == MBX_CONFIG_PORT)
 9650			/* copy command data into host mbox for cmpl */
 9651			lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
 9652					      MAILBOX_CMD_SIZE);
 9653
 9654		/* First copy mbox command data to HBA SLIM, skip past first
 9655		   word */
 9656		to_slim = phba->MBslimaddr + sizeof (uint32_t);
 9657		lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
 9658			    MAILBOX_CMD_SIZE - sizeof (uint32_t));
 9659
 9660		/* Next copy over first word, with mbxOwner set */
 9661		ldata = *((uint32_t *)mbx);
 9662		to_slim = phba->MBslimaddr;
 9663		writel(ldata, to_slim);
 9664		readl(to_slim); /* flush */
 9665
 9666		if (mbx->mbxCommand == MBX_CONFIG_PORT)
 9667			/* switch over to host mailbox */
 9668			psli->sli_flag |= LPFC_SLI_ACTIVE;
 9669	}
 9670
 9671	wmb();
 9672
 9673	switch (flag) {
 9674	case MBX_NOWAIT:
 9675		/* Set up reference to mailbox command */
 9676		psli->mbox_active = pmbox;
 9677		/* Interrupt board to do it */
 9678		writel(CA_MBATT, phba->CAregaddr);
 9679		readl(phba->CAregaddr); /* flush */
 9680		/* Don't wait for it to finish, just return */
 9681		break;
 9682
 9683	case MBX_POLL:
 9684		/* Set up null reference to mailbox command */
 9685		psli->mbox_active = NULL;
 9686		/* Interrupt board to do it */
 9687		writel(CA_MBATT, phba->CAregaddr);
 9688		readl(phba->CAregaddr); /* flush */
 9689
 9690		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
 9691			/* First read mbox status word */
 9692			word0 = *((uint32_t *)phba->mbox);
 9693			word0 = le32_to_cpu(word0);
 9694		} else {
 9695			/* First read mbox status word */
 9696			if (lpfc_readl(phba->MBslimaddr, &word0)) {
 9697				spin_unlock_irqrestore(&phba->hbalock,
 9698						       drvr_flag);
 9699				goto out_not_finished;
 9700			}
 9701		}
 9702
 9703		/* Read the HBA Host Attention Register */
 9704		if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
 9705			spin_unlock_irqrestore(&phba->hbalock,
 9706						       drvr_flag);
 9707			goto out_not_finished;
 9708		}
 9709		timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox)) + jiffies;
 9710		i = 0;
 9711		/* Wait for command to complete */
 9712		while (((word0 & OWN_CHIP) == OWN_CHIP) ||
 9713		       (!(ha_copy & HA_MBATT) &&
 9714			(phba->link_state > LPFC_WARM_START))) {
 9715			if (time_after(jiffies, timeout)) {
 9716				psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 9717				spin_unlock_irqrestore(&phba->hbalock,
 9718						       drvr_flag);
 9719				goto out_not_finished;
 9720			}
 9721
 9722			/* Check if we took a mbox interrupt while we were
 9723			   polling */
 9724			if (((word0 & OWN_CHIP) != OWN_CHIP)
 9725			    && (evtctr != psli->slistat.mbox_event))
 9726				break;
 9727
 9728			if (i++ > 10) {
 9729				spin_unlock_irqrestore(&phba->hbalock,
 9730						       drvr_flag);
 9731				msleep(1);
 9732				spin_lock_irqsave(&phba->hbalock, drvr_flag);
 9733			}
 9734
 9735			if (psli->sli_flag & LPFC_SLI_ACTIVE) {
 9736				/* First copy command data */
 9737				word0 = *((uint32_t *)phba->mbox);
 9738				word0 = le32_to_cpu(word0);
 9739				if (mbx->mbxCommand == MBX_CONFIG_PORT) {
 9740					MAILBOX_t *slimmb;
 9741					uint32_t slimword0;
 9742					/* Check real SLIM for any errors */
 9743					slimword0 = readl(phba->MBslimaddr);
 9744					slimmb = (MAILBOX_t *) & slimword0;
 9745					if (((slimword0 & OWN_CHIP) != OWN_CHIP)
 9746					    && slimmb->mbxStatus) {
 9747						psli->sli_flag &=
 9748						    ~LPFC_SLI_ACTIVE;
 9749						word0 = slimword0;
 9750					}
 9751				}
 9752			} else {
 9753				/* First copy command data */
 9754				word0 = readl(phba->MBslimaddr);
 9755			}
 9756			/* Read the HBA Host Attention Register */
 9757			if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
 9758				spin_unlock_irqrestore(&phba->hbalock,
 9759						       drvr_flag);
 9760				goto out_not_finished;
 9761			}
 9762		}
 9763
 9764		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
 9765			/* copy results back to user */
 9766			lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
 9767						MAILBOX_CMD_SIZE);
 9768			/* Copy the mailbox extension data */
 9769			if (pmbox->out_ext_byte_len && pmbox->ext_buf) {
 9770				lpfc_sli_pcimem_bcopy(phba->mbox_ext,
 9771						      pmbox->ext_buf,
 9772						      pmbox->out_ext_byte_len);
 9773			}
 9774		} else {
 9775			/* First copy command data */
 9776			lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
 9777						MAILBOX_CMD_SIZE);
 9778			/* Copy the mailbox extension data */
 9779			if (pmbox->out_ext_byte_len && pmbox->ext_buf) {
 9780				lpfc_memcpy_from_slim(
 9781					pmbox->ext_buf,
 9782					phba->MBslimaddr +
 9783					MAILBOX_HBA_EXT_OFFSET,
 9784					pmbox->out_ext_byte_len);
 9785			}
 9786		}
 9787
 9788		writel(HA_MBATT, phba->HAregaddr);
 9789		readl(phba->HAregaddr); /* flush */
 9790
 9791		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
 9792		status = mbx->mbxStatus;
 9793	}
 9794
 9795	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
 9796	return status;
 9797
 9798out_not_finished:
 9799	if (processing_queue) {
 9800		pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
 9801		lpfc_mbox_cmpl_put(phba, pmbox);
 9802	}
 9803	return MBX_NOT_FINISHED;
 9804}
 9805
 9806/**
 9807 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
 9808 * @phba: Pointer to HBA context object.
 9809 *
 9810 * The function blocks the posting of SLI4 asynchronous mailbox commands from
 9811 * the driver internal pending mailbox queue. It will then try to wait out the
 9812 * possible outstanding mailbox command before return.
 9813 *
 9814 * Returns:
 9815 * 	0 - the outstanding mailbox command completed; otherwise, the wait for
 9816 * 	the outstanding mailbox command timed out.
 9817 **/
 9818static int
 9819lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
 9820{
 9821	struct lpfc_sli *psli = &phba->sli;
 9822	LPFC_MBOXQ_t *mboxq;
 9823	int rc = 0;
 9824	unsigned long timeout = 0;
 9825	u32 sli_flag;
 9826	u8 cmd, subsys, opcode;
 9827
 9828	/* Mark the asynchronous mailbox command posting as blocked */
 9829	spin_lock_irq(&phba->hbalock);
 9830	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
 9831	/* Determine how long we might wait for the active mailbox
 9832	 * command to be gracefully completed by firmware.
 9833	 */
 9834	if (phba->sli.mbox_active)
 9835		timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba,
 9836						phba->sli.mbox_active)) + jiffies;
 9837	spin_unlock_irq(&phba->hbalock);
 9838
 9839	/* Make sure the mailbox is really active */
 9840	if (timeout)
 9841		lpfc_sli4_process_missed_mbox_completions(phba);
 9842
 9843	/* Wait for the outstanding mailbox command to complete */
 9844	while (phba->sli.mbox_active) {
 9845		/* Check active mailbox complete status every 2ms */
 9846		msleep(2);
 9847		if (time_after(jiffies, timeout)) {
 9848			/* Timeout, mark the outstanding cmd not complete */
 9849
 9850			/* Sanity check sli.mbox_active has not completed or
 9851			 * cancelled from another context during last 2ms sleep,
 9852			 * so take hbalock to be sure before logging.
 9853			 */
 9854			spin_lock_irq(&phba->hbalock);
 9855			if (phba->sli.mbox_active) {
 9856				mboxq = phba->sli.mbox_active;
 9857				cmd = mboxq->u.mb.mbxCommand;
 9858				subsys = lpfc_sli_config_mbox_subsys_get(phba,
 9859									 mboxq);
 9860				opcode = lpfc_sli_config_mbox_opcode_get(phba,
 9861									 mboxq);
 9862				sli_flag = psli->sli_flag;
 9863				spin_unlock_irq(&phba->hbalock);
 9864				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
 9865						"2352 Mailbox command x%x "
 9866						"(x%x/x%x) sli_flag x%x could "
 9867						"not complete\n",
 9868						cmd, subsys, opcode,
 9869						sli_flag);
 9870			} else {
 9871				spin_unlock_irq(&phba->hbalock);
 9872			}
 9873
 9874			rc = 1;
 9875			break;
 9876		}
 9877	}
 9878
 9879	/* Can not cleanly block async mailbox command, fails it */
 9880	if (rc) {
 9881		spin_lock_irq(&phba->hbalock);
 9882		psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
 9883		spin_unlock_irq(&phba->hbalock);
 9884	}
 9885	return rc;
 9886}
 9887
 9888/**
 9889 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
 9890 * @phba: Pointer to HBA context object.
 9891 *
 9892 * The function unblocks and resume posting of SLI4 asynchronous mailbox
 9893 * commands from the driver internal pending mailbox queue. It makes sure
 9894 * that there is no outstanding mailbox command before resuming posting
 9895 * asynchronous mailbox commands. If, for any reason, there is outstanding
 9896 * mailbox command, it will try to wait it out before resuming asynchronous
 9897 * mailbox command posting.
 9898 **/
 9899static void
 9900lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
 9901{
 9902	struct lpfc_sli *psli = &phba->sli;
 9903
 9904	spin_lock_irq(&phba->hbalock);
 9905	if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
 9906		/* Asynchronous mailbox posting is not blocked, do nothing */
 9907		spin_unlock_irq(&phba->hbalock);
 9908		return;
 9909	}
 9910
 9911	/* Outstanding synchronous mailbox command is guaranteed to be done,
 9912	 * successful or timeout, after timing-out the outstanding mailbox
 9913	 * command shall always be removed, so just unblock posting async
 9914	 * mailbox command and resume
 9915	 */
 9916	psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
 9917	spin_unlock_irq(&phba->hbalock);
 9918
 9919	/* wake up worker thread to post asynchronous mailbox command */
 9920	lpfc_worker_wake_up(phba);
 9921}
 9922
 9923/**
 9924 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
 9925 * @phba: Pointer to HBA context object.
 9926 * @mboxq: Pointer to mailbox object.
 9927 *
 9928 * The function waits for the bootstrap mailbox register ready bit from
 9929 * port for twice the regular mailbox command timeout value.
 9930 *
 9931 *      0 - no timeout on waiting for bootstrap mailbox register ready.
 9932 *      MBXERR_ERROR - wait for bootstrap mailbox register timed out or port
 9933 *                     is in an unrecoverable state.
 9934 **/
 9935static int
 9936lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
 9937{
 9938	uint32_t db_ready;
 9939	unsigned long timeout;
 9940	struct lpfc_register bmbx_reg;
 9941	struct lpfc_register portstat_reg = {-1};
 9942
 9943	/* Sanity check - there is no point to wait if the port is in an
 9944	 * unrecoverable state.
 9945	 */
 9946	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
 9947	    LPFC_SLI_INTF_IF_TYPE_2) {
 9948		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
 9949			       &portstat_reg.word0) ||
 9950		    lpfc_sli4_unrecoverable_port(&portstat_reg)) {
 9951			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
 9952					"3858 Skipping bmbx ready because "
 9953					"Port Status x%x\n",
 9954					portstat_reg.word0);
 9955			return MBXERR_ERROR;
 9956		}
 9957	}
 9958
 9959	timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)) + jiffies;
 9960
 9961	do {
 9962		bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
 9963		db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
 9964		if (!db_ready)
 9965			mdelay(2);
 9966
 9967		if (time_after(jiffies, timeout))
 9968			return MBXERR_ERROR;
 9969	} while (!db_ready);
 9970
 9971	return 0;
 9972}
 9973
 9974/**
 9975 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
 9976 * @phba: Pointer to HBA context object.
 9977 * @mboxq: Pointer to mailbox object.
 9978 *
 9979 * The function posts a mailbox to the port.  The mailbox is expected
 9980 * to be comletely filled in and ready for the port to operate on it.
 9981 * This routine executes a synchronous completion operation on the
 9982 * mailbox by polling for its completion.
 9983 *
 9984 * The caller must not be holding any locks when calling this routine.
 9985 *
 9986 * Returns:
 9987 *	MBX_SUCCESS - mailbox posted successfully
 9988 *	Any of the MBX error values.
 9989 **/
 9990static int
 9991lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
 9992{
 9993	int rc = MBX_SUCCESS;
 9994	unsigned long iflag;
 9995	uint32_t mcqe_status;
 9996	uint32_t mbx_cmnd;
 9997	struct lpfc_sli *psli = &phba->sli;
 9998	struct lpfc_mqe *mb = &mboxq->u.mqe;
 9999	struct lpfc_bmbx_create *mbox_rgn;
10000	struct dma_address *dma_address;
10001
10002	/*
10003	 * Only one mailbox can be active to the bootstrap mailbox region
10004	 * at a time and there is no queueing provided.
10005	 */
10006	spin_lock_irqsave(&phba->hbalock, iflag);
10007	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10008		spin_unlock_irqrestore(&phba->hbalock, iflag);
10009		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10010				"(%d):2532 Mailbox command x%x (x%x/x%x) "
10011				"cannot issue Data: x%x x%x\n",
10012				mboxq->vport ? mboxq->vport->vpi : 0,
10013				mboxq->u.mb.mbxCommand,
10014				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10015				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10016				psli->sli_flag, MBX_POLL);
10017		return MBXERR_ERROR;
10018	}
10019	/* The server grabs the token and owns it until release */
10020	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
10021	phba->sli.mbox_active = mboxq;
10022	spin_unlock_irqrestore(&phba->hbalock, iflag);
10023
10024	/* wait for bootstrap mbox register for readyness */
10025	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
10026	if (rc)
10027		goto exit;
10028	/*
10029	 * Initialize the bootstrap memory region to avoid stale data areas
10030	 * in the mailbox post.  Then copy the caller's mailbox contents to
10031	 * the bmbx mailbox region.
10032	 */
10033	mbx_cmnd = bf_get(lpfc_mqe_command, mb);
10034	memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
10035	lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
10036			       sizeof(struct lpfc_mqe));
10037
10038	/* Post the high mailbox dma address to the port and wait for ready. */
10039	dma_address = &phba->sli4_hba.bmbx.dma_address;
10040	writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
10041
10042	/* wait for bootstrap mbox register for hi-address write done */
10043	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
10044	if (rc)
10045		goto exit;
10046
10047	/* Post the low mailbox dma address to the port. */
10048	writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
10049
10050	/* wait for bootstrap mbox register for low address write done */
10051	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
10052	if (rc)
10053		goto exit;
10054
10055	/*
10056	 * Read the CQ to ensure the mailbox has completed.
10057	 * If so, update the mailbox status so that the upper layers
10058	 * can complete the request normally.
10059	 */
10060	lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
10061			       sizeof(struct lpfc_mqe));
10062	mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
10063	lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
10064			       sizeof(struct lpfc_mcqe));
10065	mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
10066	/*
10067	 * When the CQE status indicates a failure and the mailbox status
10068	 * indicates success then copy the CQE status into the mailbox status
10069	 * (and prefix it with x4000).
10070	 */
10071	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10072		if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
10073			bf_set(lpfc_mqe_status, mb,
10074			       (LPFC_MBX_ERROR_RANGE | mcqe_status));
10075		rc = MBXERR_ERROR;
10076	} else
10077		lpfc_sli4_swap_str(phba, mboxq);
10078
10079	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10080			"(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
10081			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
10082			" x%x x%x CQ: x%x x%x x%x x%x\n",
10083			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
10084			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10085			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10086			bf_get(lpfc_mqe_status, mb),
10087			mb->un.mb_words[0], mb->un.mb_words[1],
10088			mb->un.mb_words[2], mb->un.mb_words[3],
10089			mb->un.mb_words[4], mb->un.mb_words[5],
10090			mb->un.mb_words[6], mb->un.mb_words[7],
10091			mb->un.mb_words[8], mb->un.mb_words[9],
10092			mb->un.mb_words[10], mb->un.mb_words[11],
10093			mb->un.mb_words[12], mboxq->mcqe.word0,
10094			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
10095			mboxq->mcqe.trailer);
10096exit:
10097	/* We are holding the token, no needed for lock when release */
10098	spin_lock_irqsave(&phba->hbalock, iflag);
10099	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10100	phba->sli.mbox_active = NULL;
10101	spin_unlock_irqrestore(&phba->hbalock, iflag);
10102	return rc;
10103}
10104
10105/**
10106 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
10107 * @phba: Pointer to HBA context object.
10108 * @mboxq: Pointer to mailbox object.
10109 * @flag: Flag indicating how the mailbox need to be processed.
10110 *
10111 * This function is called by discovery code and HBA management code to submit
10112 * a mailbox command to firmware with SLI-4 interface spec.
10113 *
10114 * Return codes the caller owns the mailbox command after the return of the
10115 * function.
10116 **/
10117static int
10118lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
10119		       uint32_t flag)
10120{
10121	struct lpfc_sli *psli = &phba->sli;
10122	unsigned long iflags;
10123	int rc;
10124
10125	/* dump from issue mailbox command if setup */
10126	lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
10127
10128	rc = lpfc_mbox_dev_check(phba);
10129	if (unlikely(rc)) {
10130		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10131				"(%d):2544 Mailbox command x%x (x%x/x%x) "
10132				"cannot issue Data: x%x x%x\n",
10133				mboxq->vport ? mboxq->vport->vpi : 0,
10134				mboxq->u.mb.mbxCommand,
10135				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10136				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10137				psli->sli_flag, flag);
10138		goto out_not_finished;
10139	}
10140
10141	/* Detect polling mode and jump to a handler */
10142	if (!phba->sli4_hba.intr_enable) {
10143		if (flag == MBX_POLL)
10144			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
10145		else
10146			rc = -EIO;
10147		if (rc != MBX_SUCCESS)
10148			lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
10149					"(%d):2541 Mailbox command x%x "
10150					"(x%x/x%x) failure: "
10151					"mqe_sta: x%x mcqe_sta: x%x/x%x "
10152					"Data: x%x x%x\n",
10153					mboxq->vport ? mboxq->vport->vpi : 0,
10154					mboxq->u.mb.mbxCommand,
10155					lpfc_sli_config_mbox_subsys_get(phba,
10156									mboxq),
10157					lpfc_sli_config_mbox_opcode_get(phba,
10158									mboxq),
10159					bf_get(lpfc_mqe_status, &mboxq->u.mqe),
10160					bf_get(lpfc_mcqe_status, &mboxq->mcqe),
10161					bf_get(lpfc_mcqe_ext_status,
10162					       &mboxq->mcqe),
10163					psli->sli_flag, flag);
10164		return rc;
10165	} else if (flag == MBX_POLL) {
10166		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
10167				"(%d):2542 Try to issue mailbox command "
10168				"x%x (x%x/x%x) synchronously ahead of async "
10169				"mailbox command queue: x%x x%x\n",
10170				mboxq->vport ? mboxq->vport->vpi : 0,
10171				mboxq->u.mb.mbxCommand,
10172				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10173				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10174				psli->sli_flag, flag);
10175		/* Try to block the asynchronous mailbox posting */
10176		rc = lpfc_sli4_async_mbox_block(phba);
10177		if (!rc) {
10178			/* Successfully blocked, now issue sync mbox cmd */
10179			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
10180			if (rc != MBX_SUCCESS)
10181				lpfc_printf_log(phba, KERN_WARNING,
10182					LOG_MBOX | LOG_SLI,
10183					"(%d):2597 Sync Mailbox command "
10184					"x%x (x%x/x%x) failure: "
10185					"mqe_sta: x%x mcqe_sta: x%x/x%x "
10186					"Data: x%x x%x\n",
10187					mboxq->vport ? mboxq->vport->vpi : 0,
10188					mboxq->u.mb.mbxCommand,
10189					lpfc_sli_config_mbox_subsys_get(phba,
10190									mboxq),
10191					lpfc_sli_config_mbox_opcode_get(phba,
10192									mboxq),
10193					bf_get(lpfc_mqe_status, &mboxq->u.mqe),
10194					bf_get(lpfc_mcqe_status, &mboxq->mcqe),
10195					bf_get(lpfc_mcqe_ext_status,
10196					       &mboxq->mcqe),
10197					psli->sli_flag, flag);
10198			/* Unblock the async mailbox posting afterward */
10199			lpfc_sli4_async_mbox_unblock(phba);
10200		}
10201		return rc;
10202	}
10203
10204	/* Now, interrupt mode asynchronous mailbox command */
10205	rc = lpfc_mbox_cmd_check(phba, mboxq);
10206	if (rc) {
10207		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10208				"(%d):2543 Mailbox command x%x (x%x/x%x) "
10209				"cannot issue Data: x%x x%x\n",
10210				mboxq->vport ? mboxq->vport->vpi : 0,
10211				mboxq->u.mb.mbxCommand,
10212				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10213				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10214				psli->sli_flag, flag);
10215		goto out_not_finished;
10216	}
10217
10218	/* Put the mailbox command to the driver internal FIFO */
10219	psli->slistat.mbox_busy++;
10220	spin_lock_irqsave(&phba->hbalock, iflags);
10221	lpfc_mbox_put(phba, mboxq);
10222	spin_unlock_irqrestore(&phba->hbalock, iflags);
10223	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10224			"(%d):0354 Mbox cmd issue - Enqueue Data: "
10225			"x%x (x%x/x%x) x%x x%x x%x x%x\n",
10226			mboxq->vport ? mboxq->vport->vpi : 0xffffff,
10227			bf_get(lpfc_mqe_command, &mboxq->u.mqe),
10228			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10229			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10230			mboxq->u.mb.un.varUnregLogin.rpi,
10231			phba->pport->port_state,
10232			psli->sli_flag, MBX_NOWAIT);
10233	/* Wake up worker thread to transport mailbox command from head */
10234	lpfc_worker_wake_up(phba);
10235
10236	return MBX_BUSY;
10237
10238out_not_finished:
10239	return MBX_NOT_FINISHED;
10240}
10241
10242/**
10243 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
10244 * @phba: Pointer to HBA context object.
10245 *
10246 * This function is called by worker thread to send a mailbox command to
10247 * SLI4 HBA firmware.
10248 *
10249 **/
10250int
10251lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
10252{
10253	struct lpfc_sli *psli = &phba->sli;
10254	LPFC_MBOXQ_t *mboxq;
10255	int rc = MBX_SUCCESS;
10256	unsigned long iflags;
10257	struct lpfc_mqe *mqe;
10258	uint32_t mbx_cmnd;
10259
10260	/* Check interrupt mode before post async mailbox command */
10261	if (unlikely(!phba->sli4_hba.intr_enable))
10262		return MBX_NOT_FINISHED;
10263
10264	/* Check for mailbox command service token */
10265	spin_lock_irqsave(&phba->hbalock, iflags);
10266	if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
10267		spin_unlock_irqrestore(&phba->hbalock, iflags);
10268		return MBX_NOT_FINISHED;
10269	}
10270	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
10271		spin_unlock_irqrestore(&phba->hbalock, iflags);
10272		return MBX_NOT_FINISHED;
10273	}
10274	if (unlikely(phba->sli.mbox_active)) {
10275		spin_unlock_irqrestore(&phba->hbalock, iflags);
10276		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10277				"0384 There is pending active mailbox cmd\n");
10278		return MBX_NOT_FINISHED;
10279	}
10280	/* Take the mailbox command service token */
10281	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
10282
10283	/* Get the next mailbox command from head of queue */
10284	mboxq = lpfc_mbox_get(phba);
10285
10286	/* If no more mailbox command waiting for post, we're done */
10287	if (!mboxq) {
10288		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10289		spin_unlock_irqrestore(&phba->hbalock, iflags);
10290		return MBX_SUCCESS;
10291	}
10292	phba->sli.mbox_active = mboxq;
10293	spin_unlock_irqrestore(&phba->hbalock, iflags);
10294
10295	/* Check device readiness for posting mailbox command */
10296	rc = lpfc_mbox_dev_check(phba);
10297	if (unlikely(rc))
10298		/* Driver clean routine will clean up pending mailbox */
10299		goto out_not_finished;
10300
10301	/* Prepare the mbox command to be posted */
10302	mqe = &mboxq->u.mqe;
10303	mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
10304
10305	/* Start timer for the mbox_tmo and log some mailbox post messages */
10306	mod_timer(&psli->mbox_tmo, (jiffies +
10307		  secs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq))));
10308
10309	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
10310			"(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
10311			"x%x x%x\n",
10312			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
10313			lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10314			lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10315			phba->pport->port_state, psli->sli_flag);
10316
10317	if (mbx_cmnd != MBX_HEARTBEAT) {
10318		if (mboxq->vport) {
10319			lpfc_debugfs_disc_trc(mboxq->vport,
10320				LPFC_DISC_TRC_MBOX_VPORT,
10321				"MBOX Send vport: cmd:x%x mb:x%x x%x",
10322				mbx_cmnd, mqe->un.mb_words[0],
10323				mqe->un.mb_words[1]);
10324		} else {
10325			lpfc_debugfs_disc_trc(phba->pport,
10326				LPFC_DISC_TRC_MBOX,
10327				"MBOX Send: cmd:x%x mb:x%x x%x",
10328				mbx_cmnd, mqe->un.mb_words[0],
10329				mqe->un.mb_words[1]);
10330		}
10331	}
10332	psli->slistat.mbox_cmd++;
10333
10334	/* Post the mailbox command to the port */
10335	rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
10336	if (rc != MBX_SUCCESS) {
10337		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10338				"(%d):2533 Mailbox command x%x (x%x/x%x) "
10339				"cannot issue Data: x%x x%x\n",
10340				mboxq->vport ? mboxq->vport->vpi : 0,
10341				mboxq->u.mb.mbxCommand,
10342				lpfc_sli_config_mbox_subsys_get(phba, mboxq),
10343				lpfc_sli_config_mbox_opcode_get(phba, mboxq),
10344				psli->sli_flag, MBX_NOWAIT);
10345		goto out_not_finished;
10346	}
10347
10348	return rc;
10349
10350out_not_finished:
10351	spin_lock_irqsave(&phba->hbalock, iflags);
10352	if (phba->sli.mbox_active) {
10353		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
10354		__lpfc_mbox_cmpl_put(phba, mboxq);
10355		/* Release the token */
10356		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10357		phba->sli.mbox_active = NULL;
10358	}
10359	spin_unlock_irqrestore(&phba->hbalock, iflags);
10360
10361	return MBX_NOT_FINISHED;
10362}
10363
10364/**
10365 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
10366 * @phba: Pointer to HBA context object.
10367 * @pmbox: Pointer to mailbox object.
10368 * @flag: Flag indicating how the mailbox need to be processed.
10369 *
10370 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
10371 * the API jump table function pointer from the lpfc_hba struct.
10372 *
10373 * Return codes the caller owns the mailbox command after the return of the
10374 * function.
10375 **/
10376int
10377lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
10378{
10379	return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
10380}
10381
10382/**
10383 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
10384 * @phba: The hba struct for which this call is being executed.
10385 * @dev_grp: The HBA PCI-Device group number.
10386 *
10387 * This routine sets up the mbox interface API function jump table in @phba
10388 * struct.
10389 * Returns: 0 - success, -ENODEV - failure.
10390 **/
10391int
10392lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
10393{
10394
10395	switch (dev_grp) {
10396	case LPFC_PCI_DEV_LP:
10397		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
10398		phba->lpfc_sli_handle_slow_ring_event =
10399				lpfc_sli_handle_slow_ring_event_s3;
10400		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
10401		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
10402		phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
10403		break;
10404	case LPFC_PCI_DEV_OC:
10405		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
10406		phba->lpfc_sli_handle_slow_ring_event =
10407				lpfc_sli_handle_slow_ring_event_s4;
10408		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
10409		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
10410		phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
10411		break;
10412	default:
10413		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10414				"1420 Invalid HBA PCI-device group: 0x%x\n",
10415				dev_grp);
10416		return -ENODEV;
10417	}
10418	return 0;
10419}
10420
10421/**
10422 * __lpfc_sli_ringtx_put - Add an iocb to the txq
10423 * @phba: Pointer to HBA context object.
10424 * @pring: Pointer to driver SLI ring object.
10425 * @piocb: Pointer to address of newly added command iocb.
10426 *
10427 * This function is called with hbalock held for SLI3 ports or
10428 * the ring lock held for SLI4 ports to add a command
10429 * iocb to the txq when SLI layer cannot submit the command iocb
10430 * to the ring.
10431 **/
10432void
10433__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10434		    struct lpfc_iocbq *piocb)
10435{
10436	if (phba->sli_rev == LPFC_SLI_REV4)
10437		lockdep_assert_held(&pring->ring_lock);
10438	else
10439		lockdep_assert_held(&phba->hbalock);
10440	/* Insert the caller's iocb in the txq tail for later processing. */
10441	list_add_tail(&piocb->list, &pring->txq);
10442}
10443
10444/**
10445 * lpfc_sli_next_iocb - Get the next iocb in the txq
10446 * @phba: Pointer to HBA context object.
10447 * @pring: Pointer to driver SLI ring object.
10448 * @piocb: Pointer to address of newly added command iocb.
10449 *
10450 * This function is called with hbalock held before a new
10451 * iocb is submitted to the firmware. This function checks
10452 * txq to flush the iocbs in txq to Firmware before
10453 * submitting new iocbs to the Firmware.
10454 * If there are iocbs in the txq which need to be submitted
10455 * to firmware, lpfc_sli_next_iocb returns the first element
10456 * of the txq after dequeuing it from txq.
10457 * If there is no iocb in the txq then the function will return
10458 * *piocb and *piocb is set to NULL. Caller needs to check
10459 * *piocb to find if there are more commands in the txq.
10460 **/
10461static struct lpfc_iocbq *
10462lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10463		   struct lpfc_iocbq **piocb)
10464{
10465	struct lpfc_iocbq * nextiocb;
10466
10467	lockdep_assert_held(&phba->hbalock);
10468
10469	nextiocb = lpfc_sli_ringtx_get(phba, pring);
10470	if (!nextiocb) {
10471		nextiocb = *piocb;
10472		*piocb = NULL;
10473	}
10474
10475	return nextiocb;
10476}
10477
10478/**
10479 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
10480 * @phba: Pointer to HBA context object.
10481 * @ring_number: SLI ring number to issue iocb on.
10482 * @piocb: Pointer to command iocb.
10483 * @flag: Flag indicating if this command can be put into txq.
10484 *
10485 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
10486 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
10487 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
10488 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
10489 * this function allows only iocbs for posting buffers. This function finds
10490 * next available slot in the command ring and posts the command to the
10491 * available slot and writes the port attention register to request HBA start
10492 * processing new iocb. If there is no slot available in the ring and
10493 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
10494 * the function returns IOCB_BUSY.
10495 *
10496 * This function is called with hbalock held. The function will return success
10497 * after it successfully submit the iocb to firmware or after adding to the
10498 * txq.
10499 **/
10500static int
10501__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
10502		    struct lpfc_iocbq *piocb, uint32_t flag)
10503{
10504	struct lpfc_iocbq *nextiocb;
10505	IOCB_t *iocb;
10506	struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
10507
10508	lockdep_assert_held(&phba->hbalock);
10509
10510	if (piocb->cmd_cmpl && (!piocb->vport) &&
10511	   (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
10512	   (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
10513		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10514				"1807 IOCB x%x failed. No vport\n",
10515				piocb->iocb.ulpCommand);
10516		dump_stack();
10517		return IOCB_ERROR;
10518	}
10519
10520
10521	/* If the PCI channel is in offline state, do not post iocbs. */
10522	if (unlikely(pci_channel_offline(phba->pcidev)))
10523		return IOCB_ERROR;
10524
10525	/* If HBA has a deferred error attention, fail the iocb. */
10526	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
10527		return IOCB_ERROR;
10528
10529	/*
10530	 * We should never get an IOCB if we are in a < LINK_DOWN state
10531	 */
10532	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10533		return IOCB_ERROR;
10534
10535	/*
10536	 * Check to see if we are blocking IOCB processing because of a
10537	 * outstanding event.
10538	 */
10539	if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
10540		goto iocb_busy;
10541
10542	if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
10543		/*
10544		 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
10545		 * can be issued if the link is not up.
10546		 */
10547		switch (piocb->iocb.ulpCommand) {
10548		case CMD_QUE_RING_BUF_CN:
10549		case CMD_QUE_RING_BUF64_CN:
10550			/*
10551			 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
10552			 * completion, cmd_cmpl MUST be 0.
10553			 */
10554			if (piocb->cmd_cmpl)
10555				piocb->cmd_cmpl = NULL;
10556			fallthrough;
10557		case CMD_CREATE_XRI_CR:
10558		case CMD_CLOSE_XRI_CN:
10559		case CMD_CLOSE_XRI_CX:
10560			break;
10561		default:
10562			goto iocb_busy;
10563		}
10564
10565	/*
10566	 * For FCP commands, we must be in a state where we can process link
10567	 * attention events.
10568	 */
10569	} else if (unlikely(pring->ringno == LPFC_FCP_RING &&
10570			    !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
10571		goto iocb_busy;
10572	}
10573
10574	while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
10575	       (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
10576		lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
10577
10578	if (iocb)
10579		lpfc_sli_update_ring(phba, pring);
10580	else
10581		lpfc_sli_update_full_ring(phba, pring);
10582
10583	if (!piocb)
10584		return IOCB_SUCCESS;
10585
10586	goto out_busy;
10587
10588 iocb_busy:
10589	pring->stats.iocb_cmd_delay++;
10590
10591 out_busy:
10592
10593	if (!(flag & SLI_IOCB_RET_IOCB)) {
10594		__lpfc_sli_ringtx_put(phba, pring, piocb);
10595		return IOCB_SUCCESS;
10596	}
10597
10598	return IOCB_BUSY;
10599}
10600
10601/**
10602 * __lpfc_sli_issue_fcp_io_s3 - SLI3 device for sending fcp io iocb
10603 * @phba: Pointer to HBA context object.
10604 * @ring_number: SLI ring number to issue wqe on.
10605 * @piocb: Pointer to command iocb.
10606 * @flag: Flag indicating if this command can be put into txq.
10607 *
10608 * __lpfc_sli_issue_fcp_io_s3 is wrapper function to invoke lockless func to
10609 * send  an iocb command to an HBA with SLI-3 interface spec.
10610 *
10611 * This function takes the hbalock before invoking the lockless version.
10612 * The function will return success after it successfully submit the wqe to
10613 * firmware or after adding to the txq.
10614 **/
10615static int
10616__lpfc_sli_issue_fcp_io_s3(struct lpfc_hba *phba, uint32_t ring_number,
10617			   struct lpfc_iocbq *piocb, uint32_t flag)
10618{
10619	unsigned long iflags;
10620	int rc;
10621
10622	spin_lock_irqsave(&phba->hbalock, iflags);
10623	rc = __lpfc_sli_issue_iocb_s3(phba, ring_number, piocb, flag);
10624	spin_unlock_irqrestore(&phba->hbalock, iflags);
10625
10626	return rc;
10627}
10628
10629/**
10630 * __lpfc_sli_issue_fcp_io_s4 - SLI4 device for sending fcp io wqe
10631 * @phba: Pointer to HBA context object.
10632 * @ring_number: SLI ring number to issue wqe on.
10633 * @piocb: Pointer to command iocb.
10634 * @flag: Flag indicating if this command can be put into txq.
10635 *
10636 * __lpfc_sli_issue_fcp_io_s4 is used by other functions in the driver to issue
10637 * an wqe command to an HBA with SLI-4 interface spec.
10638 *
10639 * This function is a lockless version. The function will return success
10640 * after it successfully submit the wqe to firmware or after adding to the
10641 * txq.
10642 **/
10643static int
10644__lpfc_sli_issue_fcp_io_s4(struct lpfc_hba *phba, uint32_t ring_number,
10645			   struct lpfc_iocbq *piocb, uint32_t flag)
10646{
10647	struct lpfc_io_buf *lpfc_cmd = piocb->io_buf;
10648
10649	lpfc_prep_embed_io(phba, lpfc_cmd);
10650	return lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, piocb);
10651}
10652
10653void
10654lpfc_prep_embed_io(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
10655{
10656	struct lpfc_iocbq *piocb = &lpfc_cmd->cur_iocbq;
10657	union lpfc_wqe128 *wqe = &lpfc_cmd->cur_iocbq.wqe;
10658	struct sli4_sge_le *sgl;
10659	u32 type_size;
10660
10661	/* 128 byte wqe support here */
10662	sgl = (struct sli4_sge_le *)lpfc_cmd->dma_sgl;
10663
10664	if (phba->fcp_embed_io) {
10665		struct fcp_cmnd *fcp_cmnd;
10666		u32 *ptr;
10667
10668		fcp_cmnd = lpfc_cmd->fcp_cmnd;
10669
10670		/* Word 0-2 - FCP_CMND */
10671		type_size = le32_to_cpu(sgl->sge_len);
10672		type_size |= ULP_BDE64_TYPE_BDE_IMMED;
10673		wqe->generic.bde.tus.w = type_size;
10674		wqe->generic.bde.addrHigh = 0;
10675		wqe->generic.bde.addrLow =  72;  /* Word 18 */
10676
10677		bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10678		bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
10679
10680		/* Word 18-29  FCP CMND Payload */
10681		ptr = &wqe->words[18];
10682		lpfc_sli_pcimem_bcopy(fcp_cmnd, ptr, le32_to_cpu(sgl->sge_len));
10683	} else {
10684		/* Word 0-2 - Inline BDE */
10685		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
10686		wqe->generic.bde.tus.f.bdeSize = le32_to_cpu(sgl->sge_len);
10687		wqe->generic.bde.addrHigh = le32_to_cpu(sgl->addr_hi);
10688		wqe->generic.bde.addrLow = le32_to_cpu(sgl->addr_lo);
10689
10690		/* Word 10 */
10691		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10692		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
10693	}
10694
10695	/* add the VMID tags as per switch response */
10696	if (unlikely(piocb->cmd_flag & LPFC_IO_VMID)) {
10697		if (phba->pport->vmid_flag & LPFC_VMID_TYPE_PRIO) {
10698			bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
10699			bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
10700					(piocb->vmid_tag.cs_ctl_vmid));
10701		} else if (phba->cfg_vmid_app_header) {
10702			bf_set(wqe_appid, &wqe->fcp_iwrite.wqe_com, 1);
10703			bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10704			wqe->words[31] = piocb->vmid_tag.app_id;
10705		}
10706	}
10707}
10708
10709/**
10710 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
10711 * @phba: Pointer to HBA context object.
10712 * @ring_number: SLI ring number to issue iocb on.
10713 * @piocb: Pointer to command iocb.
10714 * @flag: Flag indicating if this command can be put into txq.
10715 *
10716 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
10717 * an iocb command to an HBA with SLI-4 interface spec.
10718 *
10719 * This function is called with ringlock held. The function will return success
10720 * after it successfully submit the iocb to firmware or after adding to the
10721 * txq.
10722 **/
10723static int
10724__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
10725			 struct lpfc_iocbq *piocb, uint32_t flag)
10726{
10727	struct lpfc_sglq *sglq;
10728	union lpfc_wqe128 *wqe;
10729	struct lpfc_queue *wq;
10730	struct lpfc_sli_ring *pring;
10731	u32 ulp_command = get_job_cmnd(phba, piocb);
10732
10733	/* Get the WQ */
10734	if ((piocb->cmd_flag & LPFC_IO_FCP) ||
10735	    (piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {
10736		wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
10737	} else {
10738		wq = phba->sli4_hba.els_wq;
10739	}
10740
10741	/* Get corresponding ring */
10742	pring = wq->pring;
10743
10744	/*
10745	 * The WQE can be either 64 or 128 bytes,
10746	 */
10747
10748	lockdep_assert_held(&pring->ring_lock);
10749	wqe = &piocb->wqe;
10750	if (piocb->sli4_xritag == NO_XRI) {
10751		if (ulp_command == CMD_ABORT_XRI_CX)
10752			sglq = NULL;
10753		else {
10754			sglq = __lpfc_sli_get_els_sglq(phba, piocb);
10755			if (!sglq) {
10756				if (!(flag & SLI_IOCB_RET_IOCB)) {
10757					__lpfc_sli_ringtx_put(phba,
10758							pring,
10759							piocb);
10760					return IOCB_SUCCESS;
10761				} else {
10762					return IOCB_BUSY;
10763				}
10764			}
10765		}
10766	} else if (piocb->cmd_flag &  LPFC_IO_FCP) {
10767		/* These IO's already have an XRI and a mapped sgl. */
10768		sglq = NULL;
10769	}
10770	else {
10771		/*
10772		 * This is a continuation of a commandi,(CX) so this
10773		 * sglq is on the active list
10774		 */
10775		sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
10776		if (!sglq)
10777			return IOCB_ERROR;
10778	}
10779
10780	if (sglq) {
10781		piocb->sli4_lxritag = sglq->sli4_lxritag;
10782		piocb->sli4_xritag = sglq->sli4_xritag;
10783
10784		/* ABTS sent by initiator to CT exchange, the
10785		 * RX_ID field will be filled with the newly
10786		 * allocated responder XRI.
10787		 */
10788		if (ulp_command == CMD_XMIT_BLS_RSP64_CX &&
10789		    piocb->abort_bls == LPFC_ABTS_UNSOL_INT)
10790			bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10791			       piocb->sli4_xritag);
10792
10793		bf_set(wqe_xri_tag, &wqe->generic.wqe_com,
10794		       piocb->sli4_xritag);
10795
10796		if (lpfc_wqe_bpl2sgl(phba, piocb, sglq) == NO_XRI)
10797			return IOCB_ERROR;
10798	}
10799
10800	if (lpfc_sli4_wq_put(wq, wqe))
10801		return IOCB_ERROR;
10802
10803	lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
10804
10805	return 0;
10806}
10807
10808/*
10809 * lpfc_sli_issue_fcp_io - Wrapper func for issuing fcp i/o
10810 *
10811 * This routine wraps the actual fcp i/o function for issusing WQE for sli-4
10812 * or IOCB for sli-3  function.
10813 * pointer from the lpfc_hba struct.
10814 *
10815 * Return codes:
10816 * IOCB_ERROR - Error
10817 * IOCB_SUCCESS - Success
10818 * IOCB_BUSY - Busy
10819 **/
10820int
10821lpfc_sli_issue_fcp_io(struct lpfc_hba *phba, uint32_t ring_number,
10822		      struct lpfc_iocbq *piocb, uint32_t flag)
10823{
10824	return phba->__lpfc_sli_issue_fcp_io(phba, ring_number, piocb, flag);
10825}
10826
10827/*
10828 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
10829 *
10830 * This routine wraps the actual lockless version for issusing IOCB function
10831 * pointer from the lpfc_hba struct.
10832 *
10833 * Return codes:
10834 * IOCB_ERROR - Error
10835 * IOCB_SUCCESS - Success
10836 * IOCB_BUSY - Busy
10837 **/
10838int
10839__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10840		struct lpfc_iocbq *piocb, uint32_t flag)
10841{
10842	return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10843}
10844
10845static void
10846__lpfc_sli_prep_els_req_rsp_s3(struct lpfc_iocbq *cmdiocbq,
10847			       struct lpfc_vport *vport,
10848			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,
10849			       u32 elscmd, u8 tmo, u8 expect_rsp)
10850{
10851	struct lpfc_hba *phba = vport->phba;
10852	IOCB_t *cmd;
10853
10854	cmd = &cmdiocbq->iocb;
10855	memset(cmd, 0, sizeof(*cmd));
10856
10857	cmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
10858	cmd->un.elsreq64.bdl.addrLow = putPaddrLow(bmp->phys);
10859	cmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
10860
10861	if (expect_rsp) {
10862		cmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
10863		cmd->un.elsreq64.remoteID = did; /* DID */
10864		cmd->ulpCommand = CMD_ELS_REQUEST64_CR;
10865		cmd->ulpTimeout = tmo;
10866	} else {
10867		cmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
10868		cmd->un.genreq64.xmit_els_remoteID = did; /* DID */
10869		cmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
10870		cmd->ulpPU = PARM_NPIV_DID;
10871	}
10872	cmd->ulpBdeCount = 1;
10873	cmd->ulpLe = 1;
10874	cmd->ulpClass = CLASS3;
10875
10876	/* If we have NPIV enabled, we want to send ELS traffic by VPI. */
10877	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
10878		if (expect_rsp) {
10879			cmd->un.elsreq64.myID = vport->fc_myDID;
10880
10881			/* For ELS_REQUEST64_CR, use the VPI by default */
10882			cmd->ulpContext = phba->vpi_ids[vport->vpi];
10883		}
10884
10885		cmd->ulpCt_h = 0;
10886		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
10887		if (elscmd == ELS_CMD_ECHO)
10888			cmd->ulpCt_l = 0; /* context = invalid RPI */
10889		else
10890			cmd->ulpCt_l = 1; /* context = VPI */
10891	}
10892}
10893
10894static void
10895__lpfc_sli_prep_els_req_rsp_s4(struct lpfc_iocbq *cmdiocbq,
10896			       struct lpfc_vport *vport,
10897			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,
10898			       u32 elscmd, u8 tmo, u8 expect_rsp)
10899{
10900	struct lpfc_hba  *phba = vport->phba;
10901	union lpfc_wqe128 *wqe;
10902	struct ulp_bde64_le *bde;
10903	u8 els_id;
10904
10905	wqe = &cmdiocbq->wqe;
10906	memset(wqe, 0, sizeof(*wqe));
10907
10908	/* Word 0 - 2 BDE */
10909	bde = (struct ulp_bde64_le *)&wqe->generic.bde;
10910	bde->addr_low = cpu_to_le32(putPaddrLow(bmp->phys));
10911	bde->addr_high = cpu_to_le32(putPaddrHigh(bmp->phys));
10912	bde->type_size = cpu_to_le32(cmd_size);
10913	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
10914
10915	if (expect_rsp) {
10916		bf_set(wqe_cmnd, &wqe->els_req.wqe_com, CMD_ELS_REQUEST64_WQE);
10917
10918		/* Transfer length */
10919		wqe->els_req.payload_len = cmd_size;
10920		wqe->els_req.max_response_payload_len = FCELSSIZE;
10921
10922		/* DID */
10923		bf_set(wqe_els_did, &wqe->els_req.wqe_dest, did);
10924
10925		/* Word 11 - ELS_ID */
10926		switch (elscmd) {
10927		case ELS_CMD_PLOGI:
10928			els_id = LPFC_ELS_ID_PLOGI;
10929			break;
10930		case ELS_CMD_FLOGI:
10931			els_id = LPFC_ELS_ID_FLOGI;
10932			break;
10933		case ELS_CMD_LOGO:
10934			els_id = LPFC_ELS_ID_LOGO;
10935			break;
10936		case ELS_CMD_FDISC:
10937			if (!vport->fc_myDID) {
10938				els_id = LPFC_ELS_ID_FDISC;
10939				break;
10940			}
10941			fallthrough;
10942		default:
10943			els_id = LPFC_ELS_ID_DEFAULT;
10944			break;
10945		}
10946
10947		bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
10948	} else {
10949		/* DID */
10950		bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest, did);
10951
10952		/* Transfer length */
10953		wqe->xmit_els_rsp.response_payload_len = cmd_size;
10954
10955		bf_set(wqe_cmnd, &wqe->xmit_els_rsp.wqe_com,
10956		       CMD_XMIT_ELS_RSP64_WQE);
10957	}
10958
10959	bf_set(wqe_tmo, &wqe->generic.wqe_com, tmo);
10960	bf_set(wqe_reqtag, &wqe->generic.wqe_com, cmdiocbq->iotag);
10961	bf_set(wqe_class, &wqe->generic.wqe_com, CLASS3);
10962
10963	/* If we have NPIV enabled, we want to send ELS traffic by VPI.
10964	 * For SLI4, since the driver controls VPIs we also want to include
10965	 * all ELS pt2pt protocol traffic as well.
10966	 */
10967	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||
10968	    test_bit(FC_PT2PT, &vport->fc_flag)) {
10969		if (expect_rsp) {
10970			bf_set(els_req64_sid, &wqe->els_req, vport->fc_myDID);
10971
10972			/* For ELS_REQUEST64_WQE, use the VPI by default */
10973			bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
10974			       phba->vpi_ids[vport->vpi]);
10975		}
10976
10977		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
10978		if (elscmd == ELS_CMD_ECHO)
10979			bf_set(wqe_ct, &wqe->generic.wqe_com, 0);
10980		else
10981			bf_set(wqe_ct, &wqe->generic.wqe_com, 1);
10982	}
10983}
10984
10985void
10986lpfc_sli_prep_els_req_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
10987			  struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
10988			  u16 cmd_size, u32 did, u32 elscmd, u8 tmo,
10989			  u8 expect_rsp)
10990{
10991	phba->__lpfc_sli_prep_els_req_rsp(cmdiocbq, vport, bmp, cmd_size, did,
10992					  elscmd, tmo, expect_rsp);
10993}
10994
10995static void
10996__lpfc_sli_prep_gen_req_s3(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,
10997			   u16 rpi, u32 num_entry, u8 tmo)
10998{
10999	IOCB_t *cmd;
11000
11001	cmd = &cmdiocbq->iocb;
11002	memset(cmd, 0, sizeof(*cmd));
11003
11004	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
11005	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
11006	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
11007	cmd->un.genreq64.bdl.bdeSize = num_entry * sizeof(struct ulp_bde64);
11008
11009	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
11010	cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
11011	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
11012
11013	cmd->ulpContext = rpi;
11014	cmd->ulpClass = CLASS3;
11015	cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
11016	cmd->ulpBdeCount = 1;
11017	cmd->ulpLe = 1;
11018	cmd->ulpOwner = OWN_CHIP;
11019	cmd->ulpTimeout = tmo;
11020}
11021
11022static void
11023__lpfc_sli_prep_gen_req_s4(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,
11024			   u16 rpi, u32 num_entry, u8 tmo)
11025{
11026	union lpfc_wqe128 *cmdwqe;
11027	struct ulp_bde64_le *bde, *bpl;
11028	u32 xmit_len = 0, total_len = 0, size, type, i;
11029
11030	cmdwqe = &cmdiocbq->wqe;
11031	memset(cmdwqe, 0, sizeof(*cmdwqe));
11032
11033	/* Calculate total_len and xmit_len */
11034	bpl = (struct ulp_bde64_le *)bmp->virt;
11035	for (i = 0; i < num_entry; i++) {
11036		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;
11037		total_len += size;
11038	}
11039	for (i = 0; i < num_entry; i++) {
11040		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;
11041		type = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_TYPE_MASK;
11042		if (type != ULP_BDE64_TYPE_BDE_64)
11043			break;
11044		xmit_len += size;
11045	}
11046
11047	/* Words 0 - 2 */
11048	bde = (struct ulp_bde64_le *)&cmdwqe->generic.bde;
11049	bde->addr_low = bpl->addr_low;
11050	bde->addr_high = bpl->addr_high;
11051	bde->type_size = cpu_to_le32(xmit_len);
11052	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);
11053
11054	/* Word 3 */
11055	cmdwqe->gen_req.request_payload_len = xmit_len;
11056
11057	/* Word 5 */
11058	bf_set(wqe_type, &cmdwqe->gen_req.wge_ctl, FC_TYPE_CT);
11059	bf_set(wqe_rctl, &cmdwqe->gen_req.wge_ctl, FC_RCTL_DD_UNSOL_CTL);
11060	bf_set(wqe_si, &cmdwqe->gen_req.wge_ctl, 1);
11061	bf_set(wqe_la, &cmdwqe->gen_req.wge_ctl, 1);
11062
11063	/* Word 6 */
11064	bf_set(wqe_ctxt_tag, &cmdwqe->gen_req.wqe_com, rpi);
11065
11066	/* Word 7 */
11067	bf_set(wqe_tmo, &cmdwqe->gen_req.wqe_com, tmo);
11068	bf_set(wqe_class, &cmdwqe->gen_req.wqe_com, CLASS3);
11069	bf_set(wqe_cmnd, &cmdwqe->gen_req.wqe_com, CMD_GEN_REQUEST64_CR);
11070	bf_set(wqe_ct, &cmdwqe->gen_req.wqe_com, SLI4_CT_RPI);
11071
11072	/* Word 12 */
11073	cmdwqe->gen_req.max_response_payload_len = total_len - xmit_len;
11074}
11075
11076void
11077lpfc_sli_prep_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11078		      struct lpfc_dmabuf *bmp, u16 rpi, u32 num_entry, u8 tmo)
11079{
11080	phba->__lpfc_sli_prep_gen_req(cmdiocbq, bmp, rpi, num_entry, tmo);
11081}
11082
11083static void
11084__lpfc_sli_prep_xmit_seq64_s3(struct lpfc_iocbq *cmdiocbq,
11085			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11086			      u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11087{
11088	IOCB_t *icmd;
11089
11090	icmd = &cmdiocbq->iocb;
11091	memset(icmd, 0, sizeof(*icmd));
11092
11093	icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
11094	icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
11095	icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
11096	icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
11097	icmd->un.xseq64.w5.hcsw.Fctl = LA;
11098	if (last_seq)
11099		icmd->un.xseq64.w5.hcsw.Fctl |= LS;
11100	icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11101	icmd->un.xseq64.w5.hcsw.Rctl = rctl;
11102	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
11103
11104	icmd->ulpBdeCount = 1;
11105	icmd->ulpLe = 1;
11106	icmd->ulpClass = CLASS3;
11107
11108	switch (cr_cx_cmd) {
11109	case CMD_XMIT_SEQUENCE64_CR:
11110		icmd->ulpContext = rpi;
11111		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
11112		break;
11113	case CMD_XMIT_SEQUENCE64_CX:
11114		icmd->ulpContext = ox_id;
11115		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
11116		break;
11117	default:
11118		break;
11119	}
11120}
11121
11122static void
11123__lpfc_sli_prep_xmit_seq64_s4(struct lpfc_iocbq *cmdiocbq,
11124			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11125			      u32 full_size, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11126{
11127	union lpfc_wqe128 *wqe;
11128	struct ulp_bde64 *bpl;
11129
11130	wqe = &cmdiocbq->wqe;
11131	memset(wqe, 0, sizeof(*wqe));
11132
11133	/* Words 0 - 2 */
11134	bpl = (struct ulp_bde64 *)bmp->virt;
11135	wqe->xmit_sequence.bde.addrHigh = bpl->addrHigh;
11136	wqe->xmit_sequence.bde.addrLow = bpl->addrLow;
11137	wqe->xmit_sequence.bde.tus.w = bpl->tus.w;
11138
11139	/* Word 5 */
11140	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, last_seq);
11141	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 1);
11142	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
11143	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, rctl);
11144	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_CT);
11145
11146	/* Word 6 */
11147	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com, rpi);
11148
11149	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
11150	       CMD_XMIT_SEQUENCE64_WQE);
11151
11152	/* Word 7 */
11153	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
11154
11155	/* Word 9 */
11156	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ox_id);
11157
11158	if (cmdiocbq->cmd_flag & (LPFC_IO_LIBDFC | LPFC_IO_LOOPBACK)) {
11159		/* Word 10 */
11160		if (cmdiocbq->cmd_flag & LPFC_IO_VMID) {
11161			bf_set(wqe_appid, &wqe->xmit_sequence.wqe_com, 1);
11162			bf_set(wqe_wqes, &wqe->xmit_sequence.wqe_com, 1);
11163			wqe->words[31] = LOOPBACK_SRC_APPID;
11164		}
11165
11166		/* Word 12 */
11167		wqe->xmit_sequence.xmit_len = full_size;
11168	}
11169	else
11170		wqe->xmit_sequence.xmit_len =
11171			wqe->xmit_sequence.bde.tus.f.bdeSize;
11172}
11173
11174void
11175lpfc_sli_prep_xmit_seq64(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11176			 struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,
11177			 u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)
11178{
11179	phba->__lpfc_sli_prep_xmit_seq64(cmdiocbq, bmp, rpi, ox_id, num_entry,
11180					 rctl, last_seq, cr_cx_cmd);
11181}
11182
11183static void
11184__lpfc_sli_prep_abort_xri_s3(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,
11185			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,
11186			     bool wqec)
11187{
11188	IOCB_t *icmd = NULL;
11189
11190	icmd = &cmdiocbq->iocb;
11191	memset(icmd, 0, sizeof(*icmd));
11192
11193	/* Word 5 */
11194	icmd->un.acxri.abortContextTag = ulp_context;
11195	icmd->un.acxri.abortIoTag = iotag;
11196
11197	if (ia) {
11198		/* Word 7 */
11199		icmd->ulpCommand = CMD_CLOSE_XRI_CN;
11200	} else {
11201		/* Word 3 */
11202		icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
11203
11204		/* Word 7 */
11205		icmd->ulpClass = ulp_class;
11206		icmd->ulpCommand = CMD_ABORT_XRI_CN;
11207	}
11208
11209	/* Word 7 */
11210	icmd->ulpLe = 1;
11211}
11212
11213static void
11214__lpfc_sli_prep_abort_xri_s4(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,
11215			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,
11216			     bool wqec)
11217{
11218	union lpfc_wqe128 *wqe;
11219
11220	wqe = &cmdiocbq->wqe;
11221	memset(wqe, 0, sizeof(*wqe));
11222
11223	/* Word 3 */
11224	bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
11225	if (ia)
11226		bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
11227	else
11228		bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
11229
11230	/* Word 7 */
11231	bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_WQE);
11232
11233	/* Word 8 */
11234	wqe->abort_cmd.wqe_com.abort_tag = ulp_context;
11235
11236	/* Word 9 */
11237	bf_set(wqe_reqtag, &wqe->abort_cmd.wqe_com, iotag);
11238
11239	/* Word 10 */
11240	bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
11241
11242	/* Word 11 */
11243	if (wqec)
11244		bf_set(wqe_wqec, &wqe->abort_cmd.wqe_com, 1);
11245	bf_set(wqe_cqid, &wqe->abort_cmd.wqe_com, cqid);
11246	bf_set(wqe_cmd_type, &wqe->abort_cmd.wqe_com, OTHER_COMMAND);
11247}
11248
11249void
11250lpfc_sli_prep_abort_xri(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,
11251			u16 ulp_context, u16 iotag, u8 ulp_class, u16 cqid,
11252			bool ia, bool wqec)
11253{
11254	phba->__lpfc_sli_prep_abort_xri(cmdiocbq, ulp_context, iotag, ulp_class,
11255					cqid, ia, wqec);
11256}
11257
11258/**
11259 * lpfc_sli_api_table_setup - Set up sli api function jump table
11260 * @phba: The hba struct for which this call is being executed.
11261 * @dev_grp: The HBA PCI-Device group number.
11262 *
11263 * This routine sets up the SLI interface API function jump table in @phba
11264 * struct.
11265 * Returns: 0 - success, -ENODEV - failure.
11266 **/
11267int
11268lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
11269{
11270
11271	switch (dev_grp) {
11272	case LPFC_PCI_DEV_LP:
11273		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
11274		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
11275		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s3;
11276		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s3;
11277		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s3;
11278		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s3;
11279		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s3;
11280		break;
11281	case LPFC_PCI_DEV_OC:
11282		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
11283		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
11284		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s4;
11285		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s4;
11286		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s4;
11287		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s4;
11288		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s4;
11289		break;
11290	default:
11291		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11292				"1419 Invalid HBA PCI-device group: 0x%x\n",
11293				dev_grp);
11294		return -ENODEV;
11295	}
11296	return 0;
11297}
11298
11299/**
11300 * lpfc_sli4_calc_ring - Calculates which ring to use
11301 * @phba: Pointer to HBA context object.
11302 * @piocb: Pointer to command iocb.
11303 *
11304 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
11305 * hba_wqidx, thus we need to calculate the corresponding ring.
11306 * Since ABORTS must go on the same WQ of the command they are
11307 * aborting, we use command's hba_wqidx.
11308 */
11309struct lpfc_sli_ring *
11310lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
11311{
11312	struct lpfc_io_buf *lpfc_cmd;
11313
11314	if (piocb->cmd_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
11315		if (unlikely(!phba->sli4_hba.hdwq))
11316			return NULL;
11317		/*
11318		 * for abort iocb hba_wqidx should already
11319		 * be setup based on what work queue we used.
11320		 */
11321		if (!(piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {
11322			lpfc_cmd = piocb->io_buf;
11323			piocb->hba_wqidx = lpfc_cmd->hdwq_no;
11324		}
11325		return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
11326	} else {
11327		if (unlikely(!phba->sli4_hba.els_wq))
11328			return NULL;
11329		piocb->hba_wqidx = 0;
11330		return phba->sli4_hba.els_wq->pring;
11331	}
11332}
11333
11334inline void lpfc_sli4_poll_eq(struct lpfc_queue *eq)
11335{
11336	struct lpfc_hba *phba = eq->phba;
11337
11338	/*
11339	 * Unlocking an irq is one of the entry point to check
11340	 * for re-schedule, but we are good for io submission
11341	 * path as midlayer does a get_cpu to glue us in. Flush
11342	 * out the invalidate queue so we can see the updated
11343	 * value for flag.
11344	 */
11345	smp_rmb();
11346
11347	if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
11348		/* We will not likely get the completion for the caller
11349		 * during this iteration but i guess that's fine.
11350		 * Future io's coming on this eq should be able to
11351		 * pick it up.  As for the case of single io's, they
11352		 * will be handled through a sched from polling timer
11353		 * function which is currently triggered every 1msec.
11354		 */
11355		lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM,
11356				     LPFC_QUEUE_WORK);
11357}
11358
11359/**
11360 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
11361 * @phba: Pointer to HBA context object.
11362 * @ring_number: Ring number
11363 * @piocb: Pointer to command iocb.
11364 * @flag: Flag indicating if this command can be put into txq.
11365 *
11366 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
11367 * function. This function gets the hbalock and calls
11368 * __lpfc_sli_issue_iocb function and will return the error returned
11369 * by __lpfc_sli_issue_iocb function. This wrapper is used by
11370 * functions which do not hold hbalock.
11371 **/
11372int
11373lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
11374		    struct lpfc_iocbq *piocb, uint32_t flag)
11375{
11376	struct lpfc_sli_ring *pring;
11377	struct lpfc_queue *eq;
11378	unsigned long iflags;
11379	int rc;
11380
11381	/* If the PCI channel is in offline state, do not post iocbs. */
11382	if (unlikely(pci_channel_offline(phba->pcidev)))
11383		return IOCB_ERROR;
11384
11385	if (phba->sli_rev == LPFC_SLI_REV4) {
11386		lpfc_sli_prep_wqe(phba, piocb);
11387
11388		eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
11389
11390		pring = lpfc_sli4_calc_ring(phba, piocb);
11391		if (unlikely(pring == NULL))
11392			return IOCB_ERROR;
11393
11394		spin_lock_irqsave(&pring->ring_lock, iflags);
11395		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11396		spin_unlock_irqrestore(&pring->ring_lock, iflags);
11397
11398		lpfc_sli4_poll_eq(eq);
11399	} else {
11400		/* For now, SLI2/3 will still use hbalock */
11401		spin_lock_irqsave(&phba->hbalock, iflags);
11402		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11403		spin_unlock_irqrestore(&phba->hbalock, iflags);
11404	}
11405	return rc;
11406}
11407
11408/**
11409 * lpfc_extra_ring_setup - Extra ring setup function
11410 * @phba: Pointer to HBA context object.
11411 *
11412 * This function is called while driver attaches with the
11413 * HBA to setup the extra ring. The extra ring is used
11414 * only when driver needs to support target mode functionality
11415 * or IP over FC functionalities.
11416 *
11417 * This function is called with no lock held. SLI3 only.
11418 **/
11419static int
11420lpfc_extra_ring_setup( struct lpfc_hba *phba)
11421{
11422	struct lpfc_sli *psli;
11423	struct lpfc_sli_ring *pring;
11424
11425	psli = &phba->sli;
11426
11427	/* Adjust cmd/rsp ring iocb entries more evenly */
11428
11429	/* Take some away from the FCP ring */
11430	pring = &psli->sli3_ring[LPFC_FCP_RING];
11431	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11432	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11433	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11434	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11435
11436	/* and give them to the extra ring */
11437	pring = &psli->sli3_ring[LPFC_EXTRA_RING];
11438
11439	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11440	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11441	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11442	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11443
11444	/* Setup default profile for this ring */
11445	pring->iotag_max = 4096;
11446	pring->num_mask = 1;
11447	pring->prt[0].profile = 0;      /* Mask 0 */
11448	pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
11449	pring->prt[0].type = phba->cfg_multi_ring_type;
11450	pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
11451	return 0;
11452}
11453
11454static void
11455lpfc_sli_post_recovery_event(struct lpfc_hba *phba,
11456			     struct lpfc_nodelist *ndlp)
11457{
11458	unsigned long iflags;
11459	struct lpfc_work_evt  *evtp = &ndlp->recovery_evt;
11460
11461	/* Hold a node reference for outstanding queued work */
11462	if (!lpfc_nlp_get(ndlp))
11463		return;
11464
11465	spin_lock_irqsave(&phba->hbalock, iflags);
11466	if (!list_empty(&evtp->evt_listp)) {
11467		spin_unlock_irqrestore(&phba->hbalock, iflags);
11468		lpfc_nlp_put(ndlp);
11469		return;
11470	}
11471
11472	evtp->evt_arg1 = ndlp;
11473	evtp->evt = LPFC_EVT_RECOVER_PORT;
11474	list_add_tail(&evtp->evt_listp, &phba->work_list);
11475	spin_unlock_irqrestore(&phba->hbalock, iflags);
11476
11477	lpfc_worker_wake_up(phba);
11478}
11479
11480/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
11481 * @phba: Pointer to HBA context object.
11482 * @iocbq: Pointer to iocb object.
11483 *
11484 * The async_event handler calls this routine when it receives
11485 * an ASYNC_STATUS_CN event from the port.  The port generates
11486 * this event when an Abort Sequence request to an rport fails
11487 * twice in succession.  The abort could be originated by the
11488 * driver or by the port.  The ABTS could have been for an ELS
11489 * or FCP IO.  The port only generates this event when an ABTS
11490 * fails to complete after one retry.
11491 */
11492static void
11493lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
11494			  struct lpfc_iocbq *iocbq)
11495{
11496	struct lpfc_nodelist *ndlp = NULL;
11497	uint16_t rpi = 0, vpi = 0;
11498	struct lpfc_vport *vport = NULL;
11499
11500	/* The rpi in the ulpContext is vport-sensitive. */
11501	vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
11502	rpi = iocbq->iocb.ulpContext;
11503
11504	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11505			"3092 Port generated ABTS async event "
11506			"on vpi %d rpi %d status 0x%x\n",
11507			vpi, rpi, iocbq->iocb.ulpStatus);
11508
11509	vport = lpfc_find_vport_by_vpid(phba, vpi);
11510	if (!vport)
11511		goto err_exit;
11512	ndlp = lpfc_findnode_rpi(vport, rpi);
11513	if (!ndlp)
11514		goto err_exit;
11515
11516	if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
11517		lpfc_sli_abts_recover_port(vport, ndlp);
11518	return;
11519
11520 err_exit:
11521	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11522			"3095 Event Context not found, no "
11523			"action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
11524			vpi, rpi, iocbq->iocb.ulpStatus,
11525			iocbq->iocb.ulpContext);
11526}
11527
11528/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
11529 * @phba: pointer to HBA context object.
11530 * @ndlp: nodelist pointer for the impacted rport.
11531 * @axri: pointer to the wcqe containing the failed exchange.
11532 *
11533 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
11534 * port.  The port generates this event when an abort exchange request to an
11535 * rport fails twice in succession with no reply.  The abort could be originated
11536 * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.
11537 */
11538void
11539lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
11540			   struct lpfc_nodelist *ndlp,
11541			   struct sli4_wcqe_xri_aborted *axri)
11542{
11543	uint32_t ext_status = 0;
11544
11545	if (!ndlp) {
11546		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11547				"3115 Node Context not found, driver "
11548				"ignoring abts err event\n");
11549		return;
11550	}
11551
11552	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11553			"3116 Port generated FCP XRI ABORT event on "
11554			"vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
11555			ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
11556			bf_get(lpfc_wcqe_xa_xri, axri),
11557			bf_get(lpfc_wcqe_xa_status, axri),
11558			axri->parameter);
11559
11560	/*
11561	 * Catch the ABTS protocol failure case.  Older OCe FW releases returned
11562	 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
11563	 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
11564	 */
11565	ext_status = axri->parameter & IOERR_PARAM_MASK;
11566	if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
11567	    ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
11568		lpfc_sli_post_recovery_event(phba, ndlp);
11569}
11570
11571/**
11572 * lpfc_sli_async_event_handler - ASYNC iocb handler function
11573 * @phba: Pointer to HBA context object.
11574 * @pring: Pointer to driver SLI ring object.
11575 * @iocbq: Pointer to iocb object.
11576 *
11577 * This function is called by the slow ring event handler
11578 * function when there is an ASYNC event iocb in the ring.
11579 * This function is called with no lock held.
11580 * Currently this function handles only temperature related
11581 * ASYNC events. The function decodes the temperature sensor
11582 * event message and posts events for the management applications.
11583 **/
11584static void
11585lpfc_sli_async_event_handler(struct lpfc_hba * phba,
11586	struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
11587{
11588	IOCB_t *icmd;
11589	uint16_t evt_code;
11590	struct temp_event temp_event_data;
11591	struct Scsi_Host *shost;
11592	uint32_t *iocb_w;
11593
11594	icmd = &iocbq->iocb;
11595	evt_code = icmd->un.asyncstat.evt_code;
11596
11597	switch (evt_code) {
11598	case ASYNC_TEMP_WARN:
11599	case ASYNC_TEMP_SAFE:
11600		temp_event_data.data = (uint32_t) icmd->ulpContext;
11601		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
11602		if (evt_code == ASYNC_TEMP_WARN) {
11603			temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
11604			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11605				"0347 Adapter is very hot, please take "
11606				"corrective action. temperature : %d Celsius\n",
11607				(uint32_t) icmd->ulpContext);
11608		} else {
11609			temp_event_data.event_code = LPFC_NORMAL_TEMP;
11610			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11611				"0340 Adapter temperature is OK now. "
11612				"temperature : %d Celsius\n",
11613				(uint32_t) icmd->ulpContext);
11614		}
11615
11616		/* Send temperature change event to applications */
11617		shost = lpfc_shost_from_vport(phba->pport);
11618		fc_host_post_vendor_event(shost, fc_get_event_number(),
11619			sizeof(temp_event_data), (char *) &temp_event_data,
11620			LPFC_NL_VENDOR_ID);
11621		break;
11622	case ASYNC_STATUS_CN:
11623		lpfc_sli_abts_err_handler(phba, iocbq);
11624		break;
11625	default:
11626		iocb_w = (uint32_t *) icmd;
11627		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
11628			"0346 Ring %d handler: unexpected ASYNC_STATUS"
11629			" evt_code 0x%x\n"
11630			"W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
11631			"W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
11632			"W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
11633			"W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
11634			pring->ringno, icmd->un.asyncstat.evt_code,
11635			iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
11636			iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
11637			iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
11638			iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
11639
11640		break;
11641	}
11642}
11643
11644
11645/**
11646 * lpfc_sli4_setup - SLI ring setup function
11647 * @phba: Pointer to HBA context object.
11648 *
11649 * lpfc_sli_setup sets up rings of the SLI interface with
11650 * number of iocbs per ring and iotags. This function is
11651 * called while driver attach to the HBA and before the
11652 * interrupts are enabled. So there is no need for locking.
11653 *
11654 * This function always returns 0.
11655 **/
11656int
11657lpfc_sli4_setup(struct lpfc_hba *phba)
11658{
11659	struct lpfc_sli_ring *pring;
11660
11661	pring = phba->sli4_hba.els_wq->pring;
11662	pring->num_mask = LPFC_MAX_RING_MASK;
11663	pring->prt[0].profile = 0;	/* Mask 0 */
11664	pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11665	pring->prt[0].type = FC_TYPE_ELS;
11666	pring->prt[0].lpfc_sli_rcv_unsol_event =
11667	    lpfc_els_unsol_event;
11668	pring->prt[1].profile = 0;	/* Mask 1 */
11669	pring->prt[1].rctl = FC_RCTL_ELS_REP;
11670	pring->prt[1].type = FC_TYPE_ELS;
11671	pring->prt[1].lpfc_sli_rcv_unsol_event =
11672	    lpfc_els_unsol_event;
11673	pring->prt[2].profile = 0;	/* Mask 2 */
11674	/* NameServer Inquiry */
11675	pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11676	/* NameServer */
11677	pring->prt[2].type = FC_TYPE_CT;
11678	pring->prt[2].lpfc_sli_rcv_unsol_event =
11679	    lpfc_ct_unsol_event;
11680	pring->prt[3].profile = 0;	/* Mask 3 */
11681	/* NameServer response */
11682	pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11683	/* NameServer */
11684	pring->prt[3].type = FC_TYPE_CT;
11685	pring->prt[3].lpfc_sli_rcv_unsol_event =
11686	    lpfc_ct_unsol_event;
11687	return 0;
11688}
11689
11690/**
11691 * lpfc_sli_setup - SLI ring setup function
11692 * @phba: Pointer to HBA context object.
11693 *
11694 * lpfc_sli_setup sets up rings of the SLI interface with
11695 * number of iocbs per ring and iotags. This function is
11696 * called while driver attach to the HBA and before the
11697 * interrupts are enabled. So there is no need for locking.
11698 *
11699 * This function always returns 0. SLI3 only.
11700 **/
11701int
11702lpfc_sli_setup(struct lpfc_hba *phba)
11703{
11704	int i, totiocbsize = 0;
11705	struct lpfc_sli *psli = &phba->sli;
11706	struct lpfc_sli_ring *pring;
11707
11708	psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
11709	psli->sli_flag = 0;
11710
11711	psli->iocbq_lookup = NULL;
11712	psli->iocbq_lookup_len = 0;
11713	psli->last_iotag = 0;
11714
11715	for (i = 0; i < psli->num_rings; i++) {
11716		pring = &psli->sli3_ring[i];
11717		switch (i) {
11718		case LPFC_FCP_RING:	/* ring 0 - FCP */
11719			/* numCiocb and numRiocb are used in config_port */
11720			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
11721			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
11722			pring->sli.sli3.numCiocb +=
11723				SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11724			pring->sli.sli3.numRiocb +=
11725				SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11726			pring->sli.sli3.numCiocb +=
11727				SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11728			pring->sli.sli3.numRiocb +=
11729				SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11730			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11731							SLI3_IOCB_CMD_SIZE :
11732							SLI2_IOCB_CMD_SIZE;
11733			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11734							SLI3_IOCB_RSP_SIZE :
11735							SLI2_IOCB_RSP_SIZE;
11736			pring->iotag_ctr = 0;
11737			pring->iotag_max =
11738			    (phba->cfg_hba_queue_depth * 2);
11739			pring->fast_iotag = pring->iotag_max;
11740			pring->num_mask = 0;
11741			break;
11742		case LPFC_EXTRA_RING:	/* ring 1 - EXTRA */
11743			/* numCiocb and numRiocb are used in config_port */
11744			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
11745			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
11746			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11747							SLI3_IOCB_CMD_SIZE :
11748							SLI2_IOCB_CMD_SIZE;
11749			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11750							SLI3_IOCB_RSP_SIZE :
11751							SLI2_IOCB_RSP_SIZE;
11752			pring->iotag_max = phba->cfg_hba_queue_depth;
11753			pring->num_mask = 0;
11754			break;
11755		case LPFC_ELS_RING:	/* ring 2 - ELS / CT */
11756			/* numCiocb and numRiocb are used in config_port */
11757			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
11758			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
11759			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
11760							SLI3_IOCB_CMD_SIZE :
11761							SLI2_IOCB_CMD_SIZE;
11762			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
11763							SLI3_IOCB_RSP_SIZE :
11764							SLI2_IOCB_RSP_SIZE;
11765			pring->fast_iotag = 0;
11766			pring->iotag_ctr = 0;
11767			pring->iotag_max = 4096;
11768			pring->lpfc_sli_rcv_async_status =
11769				lpfc_sli_async_event_handler;
11770			pring->num_mask = LPFC_MAX_RING_MASK;
11771			pring->prt[0].profile = 0;	/* Mask 0 */
11772			pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11773			pring->prt[0].type = FC_TYPE_ELS;
11774			pring->prt[0].lpfc_sli_rcv_unsol_event =
11775			    lpfc_els_unsol_event;
11776			pring->prt[1].profile = 0;	/* Mask 1 */
11777			pring->prt[1].rctl = FC_RCTL_ELS_REP;
11778			pring->prt[1].type = FC_TYPE_ELS;
11779			pring->prt[1].lpfc_sli_rcv_unsol_event =
11780			    lpfc_els_unsol_event;
11781			pring->prt[2].profile = 0;	/* Mask 2 */
11782			/* NameServer Inquiry */
11783			pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11784			/* NameServer */
11785			pring->prt[2].type = FC_TYPE_CT;
11786			pring->prt[2].lpfc_sli_rcv_unsol_event =
11787			    lpfc_ct_unsol_event;
11788			pring->prt[3].profile = 0;	/* Mask 3 */
11789			/* NameServer response */
11790			pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11791			/* NameServer */
11792			pring->prt[3].type = FC_TYPE_CT;
11793			pring->prt[3].lpfc_sli_rcv_unsol_event =
11794			    lpfc_ct_unsol_event;
11795			break;
11796		}
11797		totiocbsize += (pring->sli.sli3.numCiocb *
11798			pring->sli.sli3.sizeCiocb) +
11799			(pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
11800	}
11801	if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
11802		/* Too many cmd / rsp ring entries in SLI2 SLIM */
11803		printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
11804		       "SLI2 SLIM Data: x%x x%lx\n",
11805		       phba->brd_no, totiocbsize,
11806		       (unsigned long) MAX_SLIM_IOCB_SIZE);
11807	}
11808	if (phba->cfg_multi_ring_support == 2)
11809		lpfc_extra_ring_setup(phba);
11810
11811	return 0;
11812}
11813
11814/**
11815 * lpfc_sli4_queue_init - Queue initialization function
11816 * @phba: Pointer to HBA context object.
11817 *
11818 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
11819 * ring. This function also initializes ring indices of each ring.
11820 * This function is called during the initialization of the SLI
11821 * interface of an HBA.
11822 * This function is called with no lock held and always returns
11823 * 1.
11824 **/
11825void
11826lpfc_sli4_queue_init(struct lpfc_hba *phba)
11827{
11828	struct lpfc_sli *psli;
11829	struct lpfc_sli_ring *pring;
11830	int i;
11831
11832	psli = &phba->sli;
11833	spin_lock_irq(&phba->hbalock);
11834	INIT_LIST_HEAD(&psli->mboxq);
11835	INIT_LIST_HEAD(&psli->mboxq_cmpl);
11836	/* Initialize list headers for txq and txcmplq as double linked lists */
11837	for (i = 0; i < phba->cfg_hdw_queue; i++) {
11838		pring = phba->sli4_hba.hdwq[i].io_wq->pring;
11839		pring->flag = 0;
11840		pring->ringno = LPFC_FCP_RING;
11841		pring->txcmplq_cnt = 0;
11842		INIT_LIST_HEAD(&pring->txq);
11843		INIT_LIST_HEAD(&pring->txcmplq);
11844		INIT_LIST_HEAD(&pring->iocb_continueq);
11845		spin_lock_init(&pring->ring_lock);
11846	}
11847	pring = phba->sli4_hba.els_wq->pring;
11848	pring->flag = 0;
11849	pring->ringno = LPFC_ELS_RING;
11850	pring->txcmplq_cnt = 0;
11851	INIT_LIST_HEAD(&pring->txq);
11852	INIT_LIST_HEAD(&pring->txcmplq);
11853	INIT_LIST_HEAD(&pring->iocb_continueq);
11854	spin_lock_init(&pring->ring_lock);
11855
11856	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
11857		pring = phba->sli4_hba.nvmels_wq->pring;
11858		pring->flag = 0;
11859		pring->ringno = LPFC_ELS_RING;
11860		pring->txcmplq_cnt = 0;
11861		INIT_LIST_HEAD(&pring->txq);
11862		INIT_LIST_HEAD(&pring->txcmplq);
11863		INIT_LIST_HEAD(&pring->iocb_continueq);
11864		spin_lock_init(&pring->ring_lock);
11865	}
11866
11867	spin_unlock_irq(&phba->hbalock);
11868}
11869
11870/**
11871 * lpfc_sli_queue_init - Queue initialization function
11872 * @phba: Pointer to HBA context object.
11873 *
11874 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
11875 * ring. This function also initializes ring indices of each ring.
11876 * This function is called during the initialization of the SLI
11877 * interface of an HBA.
11878 * This function is called with no lock held and always returns
11879 * 1.
11880 **/
11881void
11882lpfc_sli_queue_init(struct lpfc_hba *phba)
11883{
11884	struct lpfc_sli *psli;
11885	struct lpfc_sli_ring *pring;
11886	int i;
11887
11888	psli = &phba->sli;
11889	spin_lock_irq(&phba->hbalock);
11890	INIT_LIST_HEAD(&psli->mboxq);
11891	INIT_LIST_HEAD(&psli->mboxq_cmpl);
11892	/* Initialize list headers for txq and txcmplq as double linked lists */
11893	for (i = 0; i < psli->num_rings; i++) {
11894		pring = &psli->sli3_ring[i];
11895		pring->ringno = i;
11896		pring->sli.sli3.next_cmdidx  = 0;
11897		pring->sli.sli3.local_getidx = 0;
11898		pring->sli.sli3.cmdidx = 0;
11899		INIT_LIST_HEAD(&pring->iocb_continueq);
11900		INIT_LIST_HEAD(&pring->iocb_continue_saveq);
11901		INIT_LIST_HEAD(&pring->postbufq);
11902		pring->flag = 0;
11903		INIT_LIST_HEAD(&pring->txq);
11904		INIT_LIST_HEAD(&pring->txcmplq);
11905		spin_lock_init(&pring->ring_lock);
11906	}
11907	spin_unlock_irq(&phba->hbalock);
11908}
11909
11910/**
11911 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
11912 * @phba: Pointer to HBA context object.
11913 *
11914 * This routine flushes the mailbox command subsystem. It will unconditionally
11915 * flush all the mailbox commands in the three possible stages in the mailbox
11916 * command sub-system: pending mailbox command queue; the outstanding mailbox
11917 * command; and completed mailbox command queue. It is caller's responsibility
11918 * to make sure that the driver is in the proper state to flush the mailbox
11919 * command sub-system. Namely, the posting of mailbox commands into the
11920 * pending mailbox command queue from the various clients must be stopped;
11921 * either the HBA is in a state that it will never works on the outstanding
11922 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
11923 * mailbox command has been completed.
11924 **/
11925static void
11926lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
11927{
11928	LIST_HEAD(completions);
11929	struct lpfc_sli *psli = &phba->sli;
11930	LPFC_MBOXQ_t *pmb;
11931	unsigned long iflag;
11932
11933	/* Disable softirqs, including timers from obtaining phba->hbalock */
11934	local_bh_disable();
11935
11936	/* Flush all the mailbox commands in the mbox system */
11937	spin_lock_irqsave(&phba->hbalock, iflag);
11938
11939	/* The pending mailbox command queue */
11940	list_splice_init(&phba->sli.mboxq, &completions);
11941	/* The outstanding active mailbox command */
11942	if (psli->mbox_active) {
11943		list_add_tail(&psli->mbox_active->list, &completions);
11944		psli->mbox_active = NULL;
11945		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11946	}
11947	/* The completed mailbox command queue */
11948	list_splice_init(&phba->sli.mboxq_cmpl, &completions);
11949	spin_unlock_irqrestore(&phba->hbalock, iflag);
11950
11951	/* Enable softirqs again, done with phba->hbalock */
11952	local_bh_enable();
11953
11954	/* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
11955	while (!list_empty(&completions)) {
11956		list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
11957		pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
11958		if (pmb->mbox_cmpl)
11959			pmb->mbox_cmpl(phba, pmb);
11960	}
11961}
11962
11963/**
11964 * lpfc_sli_host_down - Vport cleanup function
11965 * @vport: Pointer to virtual port object.
11966 *
11967 * lpfc_sli_host_down is called to clean up the resources
11968 * associated with a vport before destroying virtual
11969 * port data structures.
11970 * This function does following operations:
11971 * - Free discovery resources associated with this virtual
11972 *   port.
11973 * - Free iocbs associated with this virtual port in
11974 *   the txq.
11975 * - Send abort for all iocb commands associated with this
11976 *   vport in txcmplq.
11977 *
11978 * This function is called with no lock held and always returns 1.
11979 **/
11980int
11981lpfc_sli_host_down(struct lpfc_vport *vport)
11982{
11983	LIST_HEAD(completions);
11984	struct lpfc_hba *phba = vport->phba;
11985	struct lpfc_sli *psli = &phba->sli;
11986	struct lpfc_queue *qp = NULL;
11987	struct lpfc_sli_ring *pring;
11988	struct lpfc_iocbq *iocb, *next_iocb;
11989	int i;
11990	unsigned long flags = 0;
11991	uint16_t prev_pring_flag;
11992
11993	lpfc_cleanup_discovery_resources(vport);
11994
11995	spin_lock_irqsave(&phba->hbalock, flags);
11996
11997	/*
11998	 * Error everything on the txq since these iocbs
11999	 * have not been given to the FW yet.
12000	 * Also issue ABTS for everything on the txcmplq
12001	 */
12002	if (phba->sli_rev != LPFC_SLI_REV4) {
12003		for (i = 0; i < psli->num_rings; i++) {
12004			pring = &psli->sli3_ring[i];
12005			prev_pring_flag = pring->flag;
12006			/* Only slow rings */
12007			if (pring->ringno == LPFC_ELS_RING) {
12008				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12009				/* Set the lpfc data pending flag */
12010				set_bit(LPFC_DATA_READY, &phba->data_flags);
12011			}
12012			list_for_each_entry_safe(iocb, next_iocb,
12013						 &pring->txq, list) {
12014				if (iocb->vport != vport)
12015					continue;
12016				list_move_tail(&iocb->list, &completions);
12017			}
12018			list_for_each_entry_safe(iocb, next_iocb,
12019						 &pring->txcmplq, list) {
12020				if (iocb->vport != vport)
12021					continue;
12022				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
12023							   NULL);
12024			}
12025			pring->flag = prev_pring_flag;
12026		}
12027	} else {
12028		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12029			pring = qp->pring;
12030			if (!pring)
12031				continue;
12032			if (pring == phba->sli4_hba.els_wq->pring) {
12033				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12034				/* Set the lpfc data pending flag */
12035				set_bit(LPFC_DATA_READY, &phba->data_flags);
12036			}
12037			prev_pring_flag = pring->flag;
12038			spin_lock(&pring->ring_lock);
12039			list_for_each_entry_safe(iocb, next_iocb,
12040						 &pring->txq, list) {
12041				if (iocb->vport != vport)
12042					continue;
12043				list_move_tail(&iocb->list, &completions);
12044			}
12045			spin_unlock(&pring->ring_lock);
12046			list_for_each_entry_safe(iocb, next_iocb,
12047						 &pring->txcmplq, list) {
12048				if (iocb->vport != vport)
12049					continue;
12050				lpfc_sli_issue_abort_iotag(phba, pring, iocb,
12051							   NULL);
12052			}
12053			pring->flag = prev_pring_flag;
12054		}
12055	}
12056	spin_unlock_irqrestore(&phba->hbalock, flags);
12057
12058	/* Make sure HBA is alive */
12059	lpfc_issue_hb_tmo(phba);
12060
12061	/* Cancel all the IOCBs from the completions list */
12062	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12063			      IOERR_SLI_DOWN);
12064	return 1;
12065}
12066
12067/**
12068 * lpfc_sli_hba_down - Resource cleanup function for the HBA
12069 * @phba: Pointer to HBA context object.
12070 *
12071 * This function cleans up all iocb, buffers, mailbox commands
12072 * while shutting down the HBA. This function is called with no
12073 * lock held and always returns 1.
12074 * This function does the following to cleanup driver resources:
12075 * - Free discovery resources for each virtual port
12076 * - Cleanup any pending fabric iocbs
12077 * - Iterate through the iocb txq and free each entry
12078 *   in the list.
12079 * - Free up any buffer posted to the HBA
12080 * - Free mailbox commands in the mailbox queue.
12081 **/
12082int
12083lpfc_sli_hba_down(struct lpfc_hba *phba)
12084{
12085	LIST_HEAD(completions);
12086	struct lpfc_sli *psli = &phba->sli;
12087	struct lpfc_queue *qp = NULL;
12088	struct lpfc_sli_ring *pring;
12089	struct lpfc_dmabuf *buf_ptr;
12090	unsigned long flags = 0;
12091	int i;
12092
12093	/* Shutdown the mailbox command sub-system */
12094	lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
12095
12096	lpfc_hba_down_prep(phba);
12097
12098	/* Disable softirqs, including timers from obtaining phba->hbalock */
12099	local_bh_disable();
12100
12101	lpfc_fabric_abort_hba(phba);
12102
12103	spin_lock_irqsave(&phba->hbalock, flags);
12104
12105	/*
12106	 * Error everything on the txq since these iocbs
12107	 * have not been given to the FW yet.
12108	 */
12109	if (phba->sli_rev != LPFC_SLI_REV4) {
12110		for (i = 0; i < psli->num_rings; i++) {
12111			pring = &psli->sli3_ring[i];
12112			/* Only slow rings */
12113			if (pring->ringno == LPFC_ELS_RING) {
12114				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12115				/* Set the lpfc data pending flag */
12116				set_bit(LPFC_DATA_READY, &phba->data_flags);
12117			}
12118			list_splice_init(&pring->txq, &completions);
12119		}
12120	} else {
12121		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12122			pring = qp->pring;
12123			if (!pring)
12124				continue;
12125			spin_lock(&pring->ring_lock);
12126			list_splice_init(&pring->txq, &completions);
12127			spin_unlock(&pring->ring_lock);
12128			if (pring == phba->sli4_hba.els_wq->pring) {
12129				pring->flag |= LPFC_DEFERRED_RING_EVENT;
12130				/* Set the lpfc data pending flag */
12131				set_bit(LPFC_DATA_READY, &phba->data_flags);
12132			}
12133		}
12134	}
12135	spin_unlock_irqrestore(&phba->hbalock, flags);
12136
12137	/* Cancel all the IOCBs from the completions list */
12138	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
12139			      IOERR_SLI_DOWN);
12140
12141	spin_lock_irqsave(&phba->hbalock, flags);
12142	list_splice_init(&phba->elsbuf, &completions);
12143	phba->elsbuf_cnt = 0;
12144	phba->elsbuf_prev_cnt = 0;
12145	spin_unlock_irqrestore(&phba->hbalock, flags);
12146
12147	while (!list_empty(&completions)) {
12148		list_remove_head(&completions, buf_ptr,
12149			struct lpfc_dmabuf, list);
12150		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
12151		kfree(buf_ptr);
12152	}
12153
12154	/* Enable softirqs again, done with phba->hbalock */
12155	local_bh_enable();
12156
12157	/* Return any active mbox cmds */
12158	timer_delete_sync(&psli->mbox_tmo);
12159
12160	spin_lock_irqsave(&phba->pport->work_port_lock, flags);
12161	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12162	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
12163
12164	return 1;
12165}
12166
12167/**
12168 * lpfc_sli_pcimem_bcopy - SLI memory copy function
12169 * @srcp: Source memory pointer.
12170 * @destp: Destination memory pointer.
12171 * @cnt: Number of words required to be copied.
12172 *
12173 * This function is used for copying data between driver memory
12174 * and the SLI memory. This function also changes the endianness
12175 * of each word if native endianness is different from SLI
12176 * endianness. This function can be called with or without
12177 * lock.
12178 **/
12179void
12180lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
12181{
12182	uint32_t *src = srcp;
12183	uint32_t *dest = destp;
12184	uint32_t ldata;
12185	int i;
12186
12187	for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
12188		ldata = *src;
12189		ldata = le32_to_cpu(ldata);
12190		*dest = ldata;
12191		src++;
12192		dest++;
12193	}
12194}
12195
12196
12197/**
12198 * lpfc_sli_bemem_bcopy - SLI memory copy function
12199 * @srcp: Source memory pointer.
12200 * @destp: Destination memory pointer.
12201 * @cnt: Number of words required to be copied.
12202 *
12203 * This function is used for copying data between a data structure
12204 * with big endian representation to local endianness.
12205 * This function can be called with or without lock.
12206 **/
12207void
12208lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
12209{
12210	uint32_t *src = srcp;
12211	uint32_t *dest = destp;
12212	uint32_t ldata;
12213	int i;
12214
12215	for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
12216		ldata = *src;
12217		ldata = be32_to_cpu(ldata);
12218		*dest = ldata;
12219		src++;
12220		dest++;
12221	}
12222}
12223
12224/**
12225 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
12226 * @phba: Pointer to HBA context object.
12227 * @pring: Pointer to driver SLI ring object.
12228 * @mp: Pointer to driver buffer object.
12229 *
12230 * This function is called with no lock held.
12231 * It always return zero after adding the buffer to the postbufq
12232 * buffer list.
12233 **/
12234int
12235lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12236			 struct lpfc_dmabuf *mp)
12237{
12238	/* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
12239	   later */
12240	spin_lock_irq(&phba->hbalock);
12241	list_add_tail(&mp->list, &pring->postbufq);
12242	pring->postbufq_cnt++;
12243	spin_unlock_irq(&phba->hbalock);
12244	return 0;
12245}
12246
12247/**
12248 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
12249 * @phba: Pointer to HBA context object.
12250 *
12251 * When HBQ is enabled, buffers are searched based on tags. This function
12252 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
12253 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
12254 * does not conflict with tags of buffer posted for unsolicited events.
12255 * The function returns the allocated tag. The function is called with
12256 * no locks held.
12257 **/
12258uint32_t
12259lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
12260{
12261	spin_lock_irq(&phba->hbalock);
12262	phba->buffer_tag_count++;
12263	/*
12264	 * Always set the QUE_BUFTAG_BIT to distiguish between
12265	 * a tag assigned by HBQ.
12266	 */
12267	phba->buffer_tag_count |= QUE_BUFTAG_BIT;
12268	spin_unlock_irq(&phba->hbalock);
12269	return phba->buffer_tag_count;
12270}
12271
12272/**
12273 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
12274 * @phba: Pointer to HBA context object.
12275 * @pring: Pointer to driver SLI ring object.
12276 * @tag: Buffer tag.
12277 *
12278 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
12279 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
12280 * iocb is posted to the response ring with the tag of the buffer.
12281 * This function searches the pring->postbufq list using the tag
12282 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
12283 * iocb. If the buffer is found then lpfc_dmabuf object of the
12284 * buffer is returned to the caller else NULL is returned.
12285 * This function is called with no lock held.
12286 **/
12287struct lpfc_dmabuf *
12288lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12289			uint32_t tag)
12290{
12291	struct lpfc_dmabuf *mp, *next_mp;
12292	struct list_head *slp = &pring->postbufq;
12293
12294	/* Search postbufq, from the beginning, looking for a match on tag */
12295	spin_lock_irq(&phba->hbalock);
12296	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12297		if (mp->buffer_tag == tag) {
12298			list_del_init(&mp->list);
12299			pring->postbufq_cnt--;
12300			spin_unlock_irq(&phba->hbalock);
12301			return mp;
12302		}
12303	}
12304
12305	spin_unlock_irq(&phba->hbalock);
12306	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12307			"0402 Cannot find virtual addr for buffer tag on "
12308			"ring %d Data x%lx x%px x%px x%x\n",
12309			pring->ringno, (unsigned long) tag,
12310			slp->next, slp->prev, pring->postbufq_cnt);
12311
12312	return NULL;
12313}
12314
12315/**
12316 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
12317 * @phba: Pointer to HBA context object.
12318 * @pring: Pointer to driver SLI ring object.
12319 * @phys: DMA address of the buffer.
12320 *
12321 * This function searches the buffer list using the dma_address
12322 * of unsolicited event to find the driver's lpfc_dmabuf object
12323 * corresponding to the dma_address. The function returns the
12324 * lpfc_dmabuf object if a buffer is found else it returns NULL.
12325 * This function is called by the ct and els unsolicited event
12326 * handlers to get the buffer associated with the unsolicited
12327 * event.
12328 *
12329 * This function is called with no lock held.
12330 **/
12331struct lpfc_dmabuf *
12332lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12333			 dma_addr_t phys)
12334{
12335	struct lpfc_dmabuf *mp, *next_mp;
12336	struct list_head *slp = &pring->postbufq;
12337
12338	/* Search postbufq, from the beginning, looking for a match on phys */
12339	spin_lock_irq(&phba->hbalock);
12340	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12341		if (mp->phys == phys) {
12342			list_del_init(&mp->list);
12343			pring->postbufq_cnt--;
12344			spin_unlock_irq(&phba->hbalock);
12345			return mp;
12346		}
12347	}
12348
12349	spin_unlock_irq(&phba->hbalock);
12350	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
12351			"0410 Cannot find virtual addr for mapped buf on "
12352			"ring %d Data x%llx x%px x%px x%x\n",
12353			pring->ringno, (unsigned long long)phys,
12354			slp->next, slp->prev, pring->postbufq_cnt);
12355	return NULL;
12356}
12357
12358/**
12359 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
12360 * @phba: Pointer to HBA context object.
12361 * @cmdiocb: Pointer to driver command iocb object.
12362 * @rspiocb: Pointer to driver response iocb object.
12363 *
12364 * This function is the completion handler for the abort iocbs for
12365 * ELS commands. This function is called from the ELS ring event
12366 * handler with no lock held. This function frees memory resources
12367 * associated with the abort iocb.
12368 **/
12369static void
12370lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12371			struct lpfc_iocbq *rspiocb)
12372{
12373	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);
12374	u32 ulp_word4 = get_job_word4(phba, rspiocb);
12375	u8 cmnd = get_job_cmnd(phba, cmdiocb);
12376
12377	if (ulp_status) {
12378		/*
12379		 * Assume that the port already completed and returned, or
12380		 * will return the iocb. Just Log the message.
12381		 */
12382		if (phba->sli_rev < LPFC_SLI_REV4) {
12383			if (cmnd == CMD_ABORT_XRI_CX &&
12384			    ulp_status == IOSTAT_LOCAL_REJECT &&
12385			    ulp_word4 == IOERR_ABORT_REQUESTED) {
12386				goto release_iocb;
12387			}
12388		}
12389	}
12390
12391	lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
12392			"0327 Abort els iocb complete x%px with io cmd xri %x "
12393			"abort tag x%x abort status %x abort code %x\n",
12394			cmdiocb, get_job_abtsiotag(phba, cmdiocb),
12395			(phba->sli_rev == LPFC_SLI_REV4) ?
12396			get_wqe_reqtag(cmdiocb) :
12397			cmdiocb->iocb.ulpIoTag,
12398			ulp_status, ulp_word4);
12399release_iocb:
12400	lpfc_sli_release_iocbq(phba, cmdiocb);
12401	return;
12402}
12403
12404/**
12405 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
12406 * @phba: Pointer to HBA context object.
12407 * @cmdiocb: Pointer to driver command iocb object.
12408 * @rspiocb: Pointer to driver response iocb object.
12409 *
12410 * The function is called from SLI ring event handler with no
12411 * lock held. This function is the completion handler for ELS commands
12412 * which are aborted. The function frees memory resources used for
12413 * the aborted ELS commands.
12414 **/
12415void
12416lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12417		     struct lpfc_iocbq *rspiocb)
12418{
12419	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;
12420	IOCB_t *irsp;
12421	LPFC_MBOXQ_t *mbox;
12422	u32 ulp_command, ulp_status, ulp_word4, iotag;
12423
12424	ulp_command = get_job_cmnd(phba, cmdiocb);
12425	ulp_status = get_job_ulpstatus(phba, rspiocb);
12426	ulp_word4 = get_job_word4(phba, rspiocb);
12427
12428	if (phba->sli_rev == LPFC_SLI_REV4) {
12429		iotag = get_wqe_reqtag(cmdiocb);
12430	} else {
12431		irsp = &rspiocb->iocb;
12432		iotag = irsp->ulpIoTag;
12433
12434		/* It is possible a PLOGI_RJT for NPIV ports to get aborted.
12435		 * The MBX_REG_LOGIN64 mbox command is freed back to the
12436		 * mbox_mem_pool here.
12437		 */
12438		if (cmdiocb->context_un.mbox) {
12439			mbox = cmdiocb->context_un.mbox;
12440			lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);
12441			cmdiocb->context_un.mbox = NULL;
12442		}
12443	}
12444
12445	/* ELS cmd tag <ulpIoTag> completes */
12446	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
12447			"0139 Ignoring ELS cmd code x%x ref cnt x%x Data: "
12448			"x%x x%x x%x x%px\n",
12449			ulp_command, kref_read(&cmdiocb->ndlp->kref),
12450			ulp_status, ulp_word4, iotag, cmdiocb->ndlp);
12451	/*
12452	 * Deref the ndlp after free_iocb. sli_release_iocb will access the ndlp
12453	 * if exchange is busy.
12454	 */
12455	if (ulp_command == CMD_GEN_REQUEST64_CR)
12456		lpfc_ct_free_iocb(phba, cmdiocb);
12457	else
12458		lpfc_els_free_iocb(phba, cmdiocb);
12459
12460	lpfc_nlp_put(ndlp);
12461}
12462
12463/**
12464 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
12465 * @phba: Pointer to HBA context object.
12466 * @pring: Pointer to driver SLI ring object.
12467 * @cmdiocb: Pointer to driver command iocb object.
12468 * @cmpl: completion function.
12469 *
12470 * This function issues an abort iocb for the provided command iocb. In case
12471 * of unloading, the abort iocb will not be issued to commands on the ELS
12472 * ring. Instead, the callback function shall be changed to those commands
12473 * so that nothing happens when them finishes. This function is called with
12474 * hbalock held andno ring_lock held (SLI4). The function returns IOCB_SUCCESS
12475 * when the command iocb is an abort request.
12476 *
12477 **/
12478int
12479lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12480			   struct lpfc_iocbq *cmdiocb, void *cmpl)
12481{
12482	struct lpfc_vport *vport = cmdiocb->vport;
12483	struct lpfc_iocbq *abtsiocbp;
12484	int retval = IOCB_ERROR;
12485	unsigned long iflags;
12486	struct lpfc_nodelist *ndlp = NULL;
12487	u32 ulp_command = get_job_cmnd(phba, cmdiocb);
12488	u16 ulp_context, iotag;
12489	bool ia;
12490
12491	/*
12492	 * There are certain command types we don't want to abort.  And we
12493	 * don't want to abort commands that are already in the process of
12494	 * being aborted.
12495	 */
12496	if (ulp_command == CMD_ABORT_XRI_WQE ||
12497	    ulp_command == CMD_ABORT_XRI_CN ||
12498	    ulp_command == CMD_CLOSE_XRI_CN ||
12499	    cmdiocb->cmd_flag & LPFC_DRIVER_ABORTED)
12500		return IOCB_ABORTING;
12501
12502	if (!pring) {
12503		if (cmdiocb->cmd_flag & LPFC_IO_FABRIC)
12504			cmdiocb->fabric_cmd_cmpl = lpfc_ignore_els_cmpl;
12505		else
12506			cmdiocb->cmd_cmpl = lpfc_ignore_els_cmpl;
12507		return retval;
12508	}
12509
12510	/*
12511	 * Always abort the outstanding WQE and set the IA bit correctly
12512	 * for the context.  This is necessary for correctly removing
12513	 * outstanding ndlp reference counts when the CQE completes with
12514	 * the XB bit set.
12515	 */
12516	abtsiocbp = __lpfc_sli_get_iocbq(phba);
12517	if (abtsiocbp == NULL)
12518		return IOCB_NORESOURCE;
12519
12520	/* This signals the response to set the correct status
12521	 * before calling the completion handler
12522	 */
12523	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;
12524
12525	if (phba->sli_rev == LPFC_SLI_REV4) {
12526		ulp_context = cmdiocb->sli4_xritag;
12527		iotag = abtsiocbp->iotag;
12528	} else {
12529		iotag = cmdiocb->iocb.ulpIoTag;
12530		if (pring->ringno == LPFC_ELS_RING) {
12531			ndlp = cmdiocb->ndlp;
12532			ulp_context = ndlp->nlp_rpi;
12533		} else {
12534			ulp_context = cmdiocb->iocb.ulpContext;
12535		}
12536	}
12537
12538	/* Just close the exchange under certain conditions. */
12539	if (test_bit(FC_UNLOADING, &vport->load_flag) ||
12540	    phba->link_state < LPFC_LINK_UP ||
12541	    (phba->sli_rev == LPFC_SLI_REV4 &&
12542	     phba->sli4_hba.link_state.status == LPFC_FC_LA_TYPE_LINK_DOWN) ||
12543	    (phba->link_flag & LS_EXTERNAL_LOOPBACK))
12544		ia = true;
12545	else
12546		ia = false;
12547
12548	lpfc_sli_prep_abort_xri(phba, abtsiocbp, ulp_context, iotag,
12549				cmdiocb->iocb.ulpClass,
12550				LPFC_WQE_CQ_ID_DEFAULT, ia, false);
12551
12552	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
12553	abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
12554	if (cmdiocb->cmd_flag & LPFC_IO_FCP)
12555		abtsiocbp->cmd_flag |= (LPFC_IO_FCP | LPFC_USE_FCPWQIDX);
12556
12557	if (cmdiocb->cmd_flag & LPFC_IO_FOF)
12558		abtsiocbp->cmd_flag |= LPFC_IO_FOF;
12559
12560	if (cmpl)
12561		abtsiocbp->cmd_cmpl = cmpl;
12562	else
12563		abtsiocbp->cmd_cmpl = lpfc_sli_abort_els_cmpl;
12564	abtsiocbp->vport = vport;
12565
12566	if (phba->sli_rev == LPFC_SLI_REV4) {
12567		pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
12568		if (unlikely(pring == NULL))
12569			goto abort_iotag_exit;
12570		/* Note: both hbalock and ring_lock need to be set here */
12571		spin_lock_irqsave(&pring->ring_lock, iflags);
12572		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12573			abtsiocbp, 0);
12574		spin_unlock_irqrestore(&pring->ring_lock, iflags);
12575	} else {
12576		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12577			abtsiocbp, 0);
12578	}
12579
12580abort_iotag_exit:
12581
12582	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
12583			 "0339 Abort IO XRI x%x, Original iotag x%x, "
12584			 "abort tag x%x Cmdjob : x%px Abortjob : x%px "
12585			 "retval x%x : IA %d cmd_cmpl %ps\n",
12586			 ulp_context, (phba->sli_rev == LPFC_SLI_REV4) ?
12587			 cmdiocb->iotag : iotag, iotag, cmdiocb, abtsiocbp,
12588			 retval, ia, abtsiocbp->cmd_cmpl);
12589	if (retval) {
12590		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;
12591		__lpfc_sli_release_iocbq(phba, abtsiocbp);
12592	}
12593
12594	/*
12595	 * Caller to this routine should check for IOCB_ERROR
12596	 * and handle it properly.  This routine no longer removes
12597	 * iocb off txcmplq and call compl in case of IOCB_ERROR.
12598	 */
12599	return retval;
12600}
12601
12602/**
12603 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
12604 * @phba: pointer to lpfc HBA data structure.
12605 *
12606 * This routine will abort all pending and outstanding iocbs to an HBA.
12607 **/
12608void
12609lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
12610{
12611	struct lpfc_sli *psli = &phba->sli;
12612	struct lpfc_sli_ring *pring;
12613	struct lpfc_queue *qp = NULL;
12614	int i;
12615
12616	if (phba->sli_rev != LPFC_SLI_REV4) {
12617		for (i = 0; i < psli->num_rings; i++) {
12618			pring = &psli->sli3_ring[i];
12619			lpfc_sli_abort_iocb_ring(phba, pring);
12620		}
12621		return;
12622	}
12623	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12624		pring = qp->pring;
12625		if (!pring)
12626			continue;
12627		lpfc_sli_abort_iocb_ring(phba, pring);
12628	}
12629}
12630
12631/**
12632 * lpfc_sli_validate_fcp_iocb_for_abort - filter iocbs appropriate for FCP aborts
12633 * @iocbq: Pointer to iocb object.
12634 * @vport: Pointer to driver virtual port object.
12635 *
12636 * This function acts as an iocb filter for functions which abort FCP iocbs.
12637 *
12638 * Return values
12639 * -ENODEV, if a null iocb or vport ptr is encountered
12640 * -EINVAL, if the iocb is not an FCP I/O, not on the TX cmpl queue, premarked as
12641 *          driver already started the abort process, or is an abort iocb itself
12642 * 0, passes criteria for aborting the FCP I/O iocb
12643 **/
12644static int
12645lpfc_sli_validate_fcp_iocb_for_abort(struct lpfc_iocbq *iocbq,
12646				     struct lpfc_vport *vport)
12647{
12648	u8 ulp_command;
12649
12650	/* No null ptr vports */
12651	if (!iocbq || iocbq->vport != vport)
12652		return -ENODEV;
12653
12654	/* iocb must be for FCP IO, already exists on the TX cmpl queue,
12655	 * can't be premarked as driver aborted, nor be an ABORT iocb itself
12656	 */
12657	ulp_command = get_job_cmnd(vport->phba, iocbq);
12658	if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
12659	    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ) ||
12660	    (iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||
12661	    (ulp_command == CMD_ABORT_XRI_CN ||
12662	     ulp_command == CMD_CLOSE_XRI_CN ||
12663	     ulp_command == CMD_ABORT_XRI_WQE))
12664		return -EINVAL;
12665
12666	return 0;
12667}
12668
12669/**
12670 * lpfc_sli_validate_fcp_iocb - validate commands associated with a SCSI target
12671 * @iocbq: Pointer to driver iocb object.
12672 * @vport: Pointer to driver virtual port object.
12673 * @tgt_id: SCSI ID of the target.
12674 * @lun_id: LUN ID of the scsi device.
12675 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
12676 *
12677 * This function acts as an iocb filter for validating a lun/SCSI target/SCSI
12678 * host.
12679 *
12680 * It will return
12681 * 0 if the filtering criteria is met for the given iocb and will return
12682 * 1 if the filtering criteria is not met.
12683 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
12684 * given iocb is for the SCSI device specified by vport, tgt_id and
12685 * lun_id parameter.
12686 * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
12687 * given iocb is for the SCSI target specified by vport and tgt_id
12688 * parameters.
12689 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
12690 * given iocb is for the SCSI host associated with the given vport.
12691 * This function is called with no locks held.
12692 **/
12693static int
12694lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
12695			   uint16_t tgt_id, uint64_t lun_id,
12696			   lpfc_ctx_cmd ctx_cmd)
12697{
12698	struct lpfc_io_buf *lpfc_cmd;
12699	int rc = 1;
12700
12701	lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12702
12703	if (lpfc_cmd->pCmd == NULL)
12704		return rc;
12705
12706	switch (ctx_cmd) {
12707	case LPFC_CTX_LUN:
12708		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12709		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
12710		    (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
12711			rc = 0;
12712		break;
12713	case LPFC_CTX_TGT:
12714		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
12715		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
12716			rc = 0;
12717		break;
12718	case LPFC_CTX_HOST:
12719		rc = 0;
12720		break;
12721	default:
12722		printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
12723			__func__, ctx_cmd);
12724		break;
12725	}
12726
12727	return rc;
12728}
12729
12730/**
12731 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
12732 * @vport: Pointer to virtual port.
12733 * @tgt_id: SCSI ID of the target.
12734 * @lun_id: LUN ID of the scsi device.
12735 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12736 *
12737 * This function returns number of FCP commands pending for the vport.
12738 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
12739 * commands pending on the vport associated with SCSI device specified
12740 * by tgt_id and lun_id parameters.
12741 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
12742 * commands pending on the vport associated with SCSI target specified
12743 * by tgt_id parameter.
12744 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
12745 * commands pending on the vport.
12746 * This function returns the number of iocbs which satisfy the filter.
12747 * This function is called without any lock held.
12748 **/
12749int
12750lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
12751		  lpfc_ctx_cmd ctx_cmd)
12752{
12753	struct lpfc_hba *phba = vport->phba;
12754	struct lpfc_iocbq *iocbq;
12755	int sum, i;
12756	unsigned long iflags;
12757	u8 ulp_command;
12758
12759	spin_lock_irqsave(&phba->hbalock, iflags);
12760	for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
12761		iocbq = phba->sli.iocbq_lookup[i];
12762
12763		if (!iocbq || iocbq->vport != vport)
12764			continue;
12765		if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
12766		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ))
12767			continue;
12768
12769		/* Include counting outstanding aborts */
12770		ulp_command = get_job_cmnd(phba, iocbq);
12771		if (ulp_command == CMD_ABORT_XRI_CN ||
12772		    ulp_command == CMD_CLOSE_XRI_CN ||
12773		    ulp_command == CMD_ABORT_XRI_WQE) {
12774			sum++;
12775			continue;
12776		}
12777
12778		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12779					       ctx_cmd) == 0)
12780			sum++;
12781	}
12782	spin_unlock_irqrestore(&phba->hbalock, iflags);
12783
12784	return sum;
12785}
12786
12787/**
12788 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
12789 * @phba: Pointer to HBA context object
12790 * @cmdiocb: Pointer to command iocb object.
12791 * @rspiocb: Pointer to response iocb object.
12792 *
12793 * This function is called when an aborted FCP iocb completes. This
12794 * function is called by the ring event handler with no lock held.
12795 * This function frees the iocb.
12796 **/
12797void
12798lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12799			struct lpfc_iocbq *rspiocb)
12800{
12801	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12802			"3096 ABORT_XRI_CX completing on rpi x%x "
12803			"original iotag x%x, abort cmd iotag x%x "
12804			"status 0x%x, reason 0x%x\n",
12805			(phba->sli_rev == LPFC_SLI_REV4) ?
12806			cmdiocb->sli4_xritag :
12807			cmdiocb->iocb.un.acxri.abortContextTag,
12808			get_job_abtsiotag(phba, cmdiocb),
12809			cmdiocb->iotag, get_job_ulpstatus(phba, rspiocb),
12810			get_job_word4(phba, rspiocb));
12811	lpfc_sli_release_iocbq(phba, cmdiocb);
12812	return;
12813}
12814
12815/**
12816 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
12817 * @vport: Pointer to virtual port.
12818 * @tgt_id: SCSI ID of the target.
12819 * @lun_id: LUN ID of the scsi device.
12820 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12821 *
12822 * This function sends an abort command for every SCSI command
12823 * associated with the given virtual port pending on the ring
12824 * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12825 * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before
12826 * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12827 * followed by lpfc_sli_validate_fcp_iocb.
12828 *
12829 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
12830 * FCP iocbs associated with lun specified by tgt_id and lun_id
12831 * parameters
12832 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
12833 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12834 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
12835 * FCP iocbs associated with virtual port.
12836 * The pring used for SLI3 is sli3_ring[LPFC_FCP_RING], for SLI4
12837 * lpfc_sli4_calc_ring is used.
12838 * This function returns number of iocbs it failed to abort.
12839 * This function is called with no locks held.
12840 **/
12841int
12842lpfc_sli_abort_iocb(struct lpfc_vport *vport, u16 tgt_id, u64 lun_id,
12843		    lpfc_ctx_cmd abort_cmd)
12844{
12845	struct lpfc_hba *phba = vport->phba;
12846	struct lpfc_sli_ring *pring = NULL;
12847	struct lpfc_iocbq *iocbq;
12848	int errcnt = 0, ret_val = 0;
12849	unsigned long iflags;
12850	int i;
12851
12852	/* all I/Os are in process of being flushed */
12853	if (test_bit(HBA_IOQ_FLUSH, &phba->hba_flag))
12854		return errcnt;
12855
12856	for (i = 1; i <= phba->sli.last_iotag; i++) {
12857		iocbq = phba->sli.iocbq_lookup[i];
12858
12859		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12860			continue;
12861
12862		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12863					       abort_cmd) != 0)
12864			continue;
12865
12866		spin_lock_irqsave(&phba->hbalock, iflags);
12867		if (phba->sli_rev == LPFC_SLI_REV3) {
12868			pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12869		} else if (phba->sli_rev == LPFC_SLI_REV4) {
12870			pring = lpfc_sli4_calc_ring(phba, iocbq);
12871		}
12872		ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocbq,
12873						     lpfc_sli_abort_fcp_cmpl);
12874		spin_unlock_irqrestore(&phba->hbalock, iflags);
12875		if (ret_val != IOCB_SUCCESS)
12876			errcnt++;
12877	}
12878
12879	return errcnt;
12880}
12881
12882/**
12883 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
12884 * @vport: Pointer to virtual port.
12885 * @pring: Pointer to driver SLI ring object.
12886 * @tgt_id: SCSI ID of the target.
12887 * @lun_id: LUN ID of the scsi device.
12888 * @cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12889 *
12890 * This function sends an abort command for every SCSI command
12891 * associated with the given virtual port pending on the ring
12892 * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12893 * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before
12894 * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12895 * followed by lpfc_sli_validate_fcp_iocb.
12896 *
12897 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
12898 * FCP iocbs associated with lun specified by tgt_id and lun_id
12899 * parameters
12900 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
12901 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12902 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
12903 * FCP iocbs associated with virtual port.
12904 * This function returns number of iocbs it aborted .
12905 * This function is called with no locks held right after a taskmgmt
12906 * command is sent.
12907 **/
12908int
12909lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
12910			uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
12911{
12912	struct lpfc_hba *phba = vport->phba;
12913	struct lpfc_io_buf *lpfc_cmd;
12914	struct lpfc_iocbq *abtsiocbq;
12915	struct lpfc_nodelist *ndlp = NULL;
12916	struct lpfc_iocbq *iocbq;
12917	int sum, i, ret_val;
12918	unsigned long iflags;
12919	struct lpfc_sli_ring *pring_s4 = NULL;
12920	u16 ulp_context, iotag, cqid = LPFC_WQE_CQ_ID_DEFAULT;
12921	bool ia;
12922
12923	/* all I/Os are in process of being flushed */
12924	if (test_bit(HBA_IOQ_FLUSH, &phba->hba_flag))
12925		return 0;
12926
12927	sum = 0;
12928
12929	spin_lock_irqsave(&phba->hbalock, iflags);
12930	for (i = 1; i <= phba->sli.last_iotag; i++) {
12931		iocbq = phba->sli.iocbq_lookup[i];
12932
12933		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12934			continue;
12935
12936		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12937					       cmd) != 0)
12938			continue;
12939
12940		/* Guard against IO completion being called at same time */
12941		lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12942		spin_lock(&lpfc_cmd->buf_lock);
12943
12944		if (!lpfc_cmd->pCmd) {
12945			spin_unlock(&lpfc_cmd->buf_lock);
12946			continue;
12947		}
12948
12949		if (phba->sli_rev == LPFC_SLI_REV4) {
12950			pring_s4 =
12951			    phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
12952			if (!pring_s4) {
12953				spin_unlock(&lpfc_cmd->buf_lock);
12954				continue;
12955			}
12956			/* Note: both hbalock and ring_lock must be set here */
12957			spin_lock(&pring_s4->ring_lock);
12958		}
12959
12960		/*
12961		 * If the iocbq is already being aborted, don't take a second
12962		 * action, but do count it.
12963		 */
12964		if ((iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||
12965		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ)) {
12966			if (phba->sli_rev == LPFC_SLI_REV4)
12967				spin_unlock(&pring_s4->ring_lock);
12968			spin_unlock(&lpfc_cmd->buf_lock);
12969			continue;
12970		}
12971
12972		/* issue ABTS for this IOCB based on iotag */
12973		abtsiocbq = __lpfc_sli_get_iocbq(phba);
12974		if (!abtsiocbq) {
12975			if (phba->sli_rev == LPFC_SLI_REV4)
12976				spin_unlock(&pring_s4->ring_lock);
12977			spin_unlock(&lpfc_cmd->buf_lock);
12978			continue;
12979		}
12980
12981		if (phba->sli_rev == LPFC_SLI_REV4) {
12982			iotag = abtsiocbq->iotag;
12983			ulp_context = iocbq->sli4_xritag;
12984			cqid = lpfc_cmd->hdwq->io_cq_map;
12985		} else {
12986			iotag = iocbq->iocb.ulpIoTag;
12987			if (pring->ringno == LPFC_ELS_RING) {
12988				ndlp = iocbq->ndlp;
12989				ulp_context = ndlp->nlp_rpi;
12990			} else {
12991				ulp_context = iocbq->iocb.ulpContext;
12992			}
12993		}
12994
12995		ndlp = lpfc_cmd->rdata->pnode;
12996
12997		if (lpfc_is_link_up(phba) &&
12998		    (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE) &&
12999		    !(phba->link_flag & LS_EXTERNAL_LOOPBACK))
13000			ia = false;
13001		else
13002			ia = true;
13003
13004		lpfc_sli_prep_abort_xri(phba, abtsiocbq, ulp_context, iotag,
13005					iocbq->iocb.ulpClass, cqid,
13006					ia, false);
13007
13008		abtsiocbq->vport = vport;
13009
13010		/* ABTS WQE must go to the same WQ as the WQE to be aborted */
13011		abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
13012		if (iocbq->cmd_flag & LPFC_IO_FCP)
13013			abtsiocbq->cmd_flag |= LPFC_USE_FCPWQIDX;
13014		if (iocbq->cmd_flag & LPFC_IO_FOF)
13015			abtsiocbq->cmd_flag |= LPFC_IO_FOF;
13016
13017		/* Setup callback routine and issue the command. */
13018		abtsiocbq->cmd_cmpl = lpfc_sli_abort_fcp_cmpl;
13019
13020		/*
13021		 * Indicate the IO is being aborted by the driver and set
13022		 * the caller's flag into the aborted IO.
13023		 */
13024		iocbq->cmd_flag |= LPFC_DRIVER_ABORTED;
13025
13026		if (phba->sli_rev == LPFC_SLI_REV4) {
13027			ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
13028							abtsiocbq, 0);
13029			spin_unlock(&pring_s4->ring_lock);
13030		} else {
13031			ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
13032							abtsiocbq, 0);
13033		}
13034
13035		spin_unlock(&lpfc_cmd->buf_lock);
13036
13037		if (ret_val == IOCB_ERROR)
13038			__lpfc_sli_release_iocbq(phba, abtsiocbq);
13039		else
13040			sum++;
13041	}
13042	spin_unlock_irqrestore(&phba->hbalock, iflags);
13043	return sum;
13044}
13045
13046/**
13047 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
13048 * @phba: Pointer to HBA context object.
13049 * @cmdiocbq: Pointer to command iocb.
13050 * @rspiocbq: Pointer to response iocb.
13051 *
13052 * This function is the completion handler for iocbs issued using
13053 * lpfc_sli_issue_iocb_wait function. This function is called by the
13054 * ring event handler function without any lock held. This function
13055 * can be called from both worker thread context and interrupt
13056 * context. This function also can be called from other thread which
13057 * cleans up the SLI layer objects.
13058 * This function copy the contents of the response iocb to the
13059 * response iocb memory object provided by the caller of
13060 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
13061 * sleeps for the iocb completion.
13062 **/
13063static void
13064lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
13065			struct lpfc_iocbq *cmdiocbq,
13066			struct lpfc_iocbq *rspiocbq)
13067{
13068	wait_queue_head_t *pdone_q;
13069	unsigned long iflags;
13070	struct lpfc_io_buf *lpfc_cmd;
13071	size_t offset = offsetof(struct lpfc_iocbq, wqe);
13072
13073	spin_lock_irqsave(&phba->hbalock, iflags);
13074	if (cmdiocbq->cmd_flag & LPFC_IO_WAKE_TMO) {
13075
13076		/*
13077		 * A time out has occurred for the iocb.  If a time out
13078		 * completion handler has been supplied, call it.  Otherwise,
13079		 * just free the iocbq.
13080		 */
13081
13082		spin_unlock_irqrestore(&phba->hbalock, iflags);
13083		cmdiocbq->cmd_cmpl = cmdiocbq->wait_cmd_cmpl;
13084		cmdiocbq->wait_cmd_cmpl = NULL;
13085		if (cmdiocbq->cmd_cmpl)
13086			cmdiocbq->cmd_cmpl(phba, cmdiocbq, NULL);
13087		else
13088			lpfc_sli_release_iocbq(phba, cmdiocbq);
13089		return;
13090	}
13091
13092	/* Copy the contents of the local rspiocb into the caller's buffer. */
13093	cmdiocbq->cmd_flag |= LPFC_IO_WAKE;
13094	if (cmdiocbq->rsp_iocb && rspiocbq)
13095		memcpy((char *)cmdiocbq->rsp_iocb + offset,
13096		       (char *)rspiocbq + offset, sizeof(*rspiocbq) - offset);
13097
13098	/* Set the exchange busy flag for task management commands */
13099	if ((cmdiocbq->cmd_flag & LPFC_IO_FCP) &&
13100	    !(cmdiocbq->cmd_flag & LPFC_IO_LIBDFC)) {
13101		lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
13102					cur_iocbq);
13103		if (rspiocbq && (rspiocbq->cmd_flag & LPFC_EXCHANGE_BUSY))
13104			lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
13105		else
13106			lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
13107	}
13108
13109	pdone_q = cmdiocbq->context_un.wait_queue;
13110	if (pdone_q)
13111		wake_up(pdone_q);
13112	spin_unlock_irqrestore(&phba->hbalock, iflags);
13113	return;
13114}
13115
13116/**
13117 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
13118 * @phba: Pointer to HBA context object..
13119 * @piocbq: Pointer to command iocb.
13120 * @flag: Flag to test.
13121 *
13122 * This routine grabs the hbalock and then test the cmd_flag to
13123 * see if the passed in flag is set.
13124 * Returns:
13125 * 1 if flag is set.
13126 * 0 if flag is not set.
13127 **/
13128static int
13129lpfc_chk_iocb_flg(struct lpfc_hba *phba,
13130		 struct lpfc_iocbq *piocbq, uint32_t flag)
13131{
13132	unsigned long iflags;
13133	int ret;
13134
13135	spin_lock_irqsave(&phba->hbalock, iflags);
13136	ret = piocbq->cmd_flag & flag;
13137	spin_unlock_irqrestore(&phba->hbalock, iflags);
13138	return ret;
13139
13140}
13141
13142/**
13143 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
13144 * @phba: Pointer to HBA context object..
13145 * @ring_number: Ring number
13146 * @piocb: Pointer to command iocb.
13147 * @prspiocbq: Pointer to response iocb.
13148 * @timeout: Timeout in number of seconds.
13149 *
13150 * This function issues the iocb to firmware and waits for the
13151 * iocb to complete. The cmd_cmpl field of the shall be used
13152 * to handle iocbs which time out. If the field is NULL, the
13153 * function shall free the iocbq structure.  If more clean up is
13154 * needed, the caller is expected to provide a completion function
13155 * that will provide the needed clean up.  If the iocb command is
13156 * not completed within timeout seconds, the function will either
13157 * free the iocbq structure (if cmd_cmpl == NULL) or execute the
13158 * completion function set in the cmd_cmpl field and then return
13159 * a status of IOCB_TIMEDOUT.  The caller should not free the iocb
13160 * resources if this function returns IOCB_TIMEDOUT.
13161 * The function waits for the iocb completion using an
13162 * non-interruptible wait.
13163 * This function will sleep while waiting for iocb completion.
13164 * So, this function should not be called from any context which
13165 * does not allow sleeping. Due to the same reason, this function
13166 * cannot be called with interrupt disabled.
13167 * This function assumes that the iocb completions occur while
13168 * this function sleep. So, this function cannot be called from
13169 * the thread which process iocb completion for this ring.
13170 * This function clears the cmd_flag of the iocb object before
13171 * issuing the iocb and the iocb completion handler sets this
13172 * flag and wakes this thread when the iocb completes.
13173 * The contents of the response iocb will be copied to prspiocbq
13174 * by the completion handler when the command completes.
13175 * This function returns IOCB_SUCCESS when success.
13176 * This function is called with no lock held.
13177 **/
13178int
13179lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
13180			 uint32_t ring_number,
13181			 struct lpfc_iocbq *piocb,
13182			 struct lpfc_iocbq *prspiocbq,
13183			 uint32_t timeout)
13184{
13185	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
13186	long timeleft, timeout_req = 0;
13187	int retval = IOCB_SUCCESS;
13188	uint32_t creg_val;
13189	struct lpfc_iocbq *iocb;
13190	int txq_cnt = 0;
13191	int txcmplq_cnt = 0;
13192	struct lpfc_sli_ring *pring;
13193	unsigned long iflags;
13194	bool iocb_completed = true;
13195
13196	if (phba->sli_rev >= LPFC_SLI_REV4) {
13197		lpfc_sli_prep_wqe(phba, piocb);
13198
13199		pring = lpfc_sli4_calc_ring(phba, piocb);
13200	} else
13201		pring = &phba->sli.sli3_ring[ring_number];
13202	/*
13203	 * If the caller has provided a response iocbq buffer, then rsp_iocb
13204	 * is NULL or its an error.
13205	 */
13206	if (prspiocbq) {
13207		if (piocb->rsp_iocb)
13208			return IOCB_ERROR;
13209		piocb->rsp_iocb = prspiocbq;
13210	}
13211
13212	piocb->wait_cmd_cmpl = piocb->cmd_cmpl;
13213	piocb->cmd_cmpl = lpfc_sli_wake_iocb_wait;
13214	piocb->context_un.wait_queue = &done_q;
13215	piocb->cmd_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
13216
13217	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13218		if (lpfc_readl(phba->HCregaddr, &creg_val))
13219			return IOCB_ERROR;
13220		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
13221		writel(creg_val, phba->HCregaddr);
13222		readl(phba->HCregaddr); /* flush */
13223	}
13224
13225	retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
13226				     SLI_IOCB_RET_IOCB);
13227	if (retval == IOCB_SUCCESS) {
13228		timeout_req = secs_to_jiffies(timeout);
13229		timeleft = wait_event_timeout(done_q,
13230				lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
13231				timeout_req);
13232		spin_lock_irqsave(&phba->hbalock, iflags);
13233		if (!(piocb->cmd_flag & LPFC_IO_WAKE)) {
13234
13235			/*
13236			 * IOCB timed out.  Inform the wake iocb wait
13237			 * completion function and set local status
13238			 */
13239
13240			iocb_completed = false;
13241			piocb->cmd_flag |= LPFC_IO_WAKE_TMO;
13242		}
13243		spin_unlock_irqrestore(&phba->hbalock, iflags);
13244		if (iocb_completed) {
13245			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13246					"0331 IOCB wake signaled\n");
13247			/* Note: we are not indicating if the IOCB has a success
13248			 * status or not - that's for the caller to check.
13249			 * IOCB_SUCCESS means just that the command was sent and
13250			 * completed. Not that it completed successfully.
13251			 * */
13252		} else if (timeleft == 0) {
13253			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13254					"0338 IOCB wait timeout error - no "
13255					"wake response Data x%x\n", timeout);
13256			retval = IOCB_TIMEDOUT;
13257		} else {
13258			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13259					"0330 IOCB wake NOT set, "
13260					"Data x%x x%lx\n",
13261					timeout, (timeleft / jiffies));
13262			retval = IOCB_TIMEDOUT;
13263		}
13264	} else if (retval == IOCB_BUSY) {
13265		if (phba->cfg_log_verbose & LOG_SLI) {
13266			list_for_each_entry(iocb, &pring->txq, list) {
13267				txq_cnt++;
13268			}
13269			list_for_each_entry(iocb, &pring->txcmplq, list) {
13270				txcmplq_cnt++;
13271			}
13272			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13273				"2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
13274				phba->iocb_cnt, txq_cnt, txcmplq_cnt);
13275		}
13276		return retval;
13277	} else {
13278		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13279				"0332 IOCB wait issue failed, Data x%x\n",
13280				retval);
13281		retval = IOCB_ERROR;
13282	}
13283
13284	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13285		if (lpfc_readl(phba->HCregaddr, &creg_val))
13286			return IOCB_ERROR;
13287		creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
13288		writel(creg_val, phba->HCregaddr);
13289		readl(phba->HCregaddr); /* flush */
13290	}
13291
13292	if (prspiocbq)
13293		piocb->rsp_iocb = NULL;
13294
13295	piocb->context_un.wait_queue = NULL;
13296	piocb->cmd_cmpl = NULL;
13297	return retval;
13298}
13299
13300/**
13301 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
13302 * @phba: Pointer to HBA context object.
13303 * @pmboxq: Pointer to driver mailbox object.
13304 * @timeout: Timeout in number of seconds.
13305 *
13306 * This function issues the mailbox to firmware and waits for the
13307 * mailbox command to complete. If the mailbox command is not
13308 * completed within timeout seconds, it returns MBX_TIMEOUT.
13309 * The function waits for the mailbox completion using an
13310 * interruptible wait. If the thread is woken up due to a
13311 * signal, MBX_TIMEOUT error is returned to the caller. Caller
13312 * should not free the mailbox resources, if this function returns
13313 * MBX_TIMEOUT.
13314 * This function will sleep while waiting for mailbox completion.
13315 * So, this function should not be called from any context which
13316 * does not allow sleeping. Due to the same reason, this function
13317 * cannot be called with interrupt disabled.
13318 * This function assumes that the mailbox completion occurs while
13319 * this function sleep. So, this function cannot be called from
13320 * the worker thread which processes mailbox completion.
13321 * This function is called in the context of HBA management
13322 * applications.
13323 * This function returns MBX_SUCCESS when successful.
13324 * This function is called with no lock held.
13325 **/
13326int
13327lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
13328			 uint32_t timeout)
13329{
13330	struct completion mbox_done;
13331	int retval;
13332	unsigned long flag;
13333
13334	pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
13335	/* setup wake call as IOCB callback */
13336	pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
13337
13338	/* setup ctx_u field to pass wait_queue pointer to wake function  */
13339	init_completion(&mbox_done);
13340	pmboxq->ctx_u.mbox_wait = &mbox_done;
13341	/* now issue the command */
13342	retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
13343	if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
13344		wait_for_completion_timeout(&mbox_done, secs_to_jiffies(timeout));
13345
13346		spin_lock_irqsave(&phba->hbalock, flag);
13347		pmboxq->ctx_u.mbox_wait = NULL;
13348		/*
13349		 * if LPFC_MBX_WAKE flag is set the mailbox is completed
13350		 * else do not free the resources.
13351		 */
13352		if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
13353			retval = MBX_SUCCESS;
13354		} else {
13355			retval = MBX_TIMEOUT;
13356			pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13357		}
13358		spin_unlock_irqrestore(&phba->hbalock, flag);
13359	}
13360	return retval;
13361}
13362
13363/**
13364 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
13365 * @phba: Pointer to HBA context.
13366 * @mbx_action: Mailbox shutdown options.
13367 *
13368 * This function is called to shutdown the driver's mailbox sub-system.
13369 * It first marks the mailbox sub-system is in a block state to prevent
13370 * the asynchronous mailbox command from issued off the pending mailbox
13371 * command queue. If the mailbox command sub-system shutdown is due to
13372 * HBA error conditions such as EEH or ERATT, this routine shall invoke
13373 * the mailbox sub-system flush routine to forcefully bring down the
13374 * mailbox sub-system. Otherwise, if it is due to normal condition (such
13375 * as with offline or HBA function reset), this routine will wait for the
13376 * outstanding mailbox command to complete before invoking the mailbox
13377 * sub-system flush routine to gracefully bring down mailbox sub-system.
13378 **/
13379void
13380lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
13381{
13382	struct lpfc_sli *psli = &phba->sli;
13383	unsigned long timeout;
13384
13385	if (mbx_action == LPFC_MBX_NO_WAIT) {
13386		/* delay 100ms for port state */
13387		msleep(100);
13388		lpfc_sli_mbox_sys_flush(phba);
13389		return;
13390	}
13391	timeout = secs_to_jiffies(LPFC_MBOX_TMO) + jiffies;
13392
13393	/* Disable softirqs, including timers from obtaining phba->hbalock */
13394	local_bh_disable();
13395
13396	spin_lock_irq(&phba->hbalock);
13397	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
13398
13399	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
13400		/* Determine how long we might wait for the active mailbox
13401		 * command to be gracefully completed by firmware.
13402		 */
13403		if (phba->sli.mbox_active)
13404			timeout = secs_to_jiffies(lpfc_mbox_tmo_val(phba,
13405						phba->sli.mbox_active)) + jiffies;
13406		spin_unlock_irq(&phba->hbalock);
13407
13408		/* Enable softirqs again, done with phba->hbalock */
13409		local_bh_enable();
13410
13411		while (phba->sli.mbox_active) {
13412			/* Check active mailbox complete status every 2ms */
13413			msleep(2);
13414			if (time_after(jiffies, timeout))
13415				/* Timeout, let the mailbox flush routine to
13416				 * forcefully release active mailbox command
13417				 */
13418				break;
13419		}
13420	} else {
13421		spin_unlock_irq(&phba->hbalock);
13422
13423		/* Enable softirqs again, done with phba->hbalock */
13424		local_bh_enable();
13425	}
13426
13427	lpfc_sli_mbox_sys_flush(phba);
13428}
13429
13430/**
13431 * lpfc_sli_eratt_read - read sli-3 error attention events
13432 * @phba: Pointer to HBA context.
13433 *
13434 * This function is called to read the SLI3 device error attention registers
13435 * for possible error attention events. The caller must hold the hostlock
13436 * with spin_lock_irq().
13437 *
13438 * This function returns 1 when there is Error Attention in the Host Attention
13439 * Register and returns 0 otherwise.
13440 **/
13441static int
13442lpfc_sli_eratt_read(struct lpfc_hba *phba)
13443{
13444	uint32_t ha_copy;
13445
13446	/* Read chip Host Attention (HA) register */
13447	if (lpfc_readl(phba->HAregaddr, &ha_copy))
13448		goto unplug_err;
13449
13450	if (ha_copy & HA_ERATT) {
13451		/* Read host status register to retrieve error event */
13452		if (lpfc_sli_read_hs(phba))
13453			goto unplug_err;
13454
13455		/* Check if there is a deferred error condition is active */
13456		if ((HS_FFER1 & phba->work_hs) &&
13457		    ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13458		      HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
13459			set_bit(DEFER_ERATT, &phba->hba_flag);
13460			/* Clear all interrupt enable conditions */
13461			writel(0, phba->HCregaddr);
13462			readl(phba->HCregaddr);
13463		}
13464
13465		/* Set the driver HA work bitmap */
13466		phba->work_ha |= HA_ERATT;
13467		/* Indicate polling handles this ERATT */
13468		set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13469		return 1;
13470	}
13471	return 0;
13472
13473unplug_err:
13474	/* Set the driver HS work bitmap */
13475	phba->work_hs |= UNPLUG_ERR;
13476	/* Set the driver HA work bitmap */
13477	phba->work_ha |= HA_ERATT;
13478	/* Indicate polling handles this ERATT */
13479	set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13480	return 1;
13481}
13482
13483/**
13484 * lpfc_sli4_eratt_read - read sli-4 error attention events
13485 * @phba: Pointer to HBA context.
13486 *
13487 * This function is called to read the SLI4 device error attention registers
13488 * for possible error attention events. The caller must hold the hostlock
13489 * with spin_lock_irq().
13490 *
13491 * This function returns 1 when there is Error Attention in the Host Attention
13492 * Register and returns 0 otherwise.
13493 **/
13494static int
13495lpfc_sli4_eratt_read(struct lpfc_hba *phba)
13496{
13497	uint32_t uerr_sta_hi, uerr_sta_lo;
13498	uint32_t if_type, portsmphr;
13499	struct lpfc_register portstat_reg;
13500	u32 logmask;
13501
13502	/*
13503	 * For now, use the SLI4 device internal unrecoverable error
13504	 * registers for error attention. This can be changed later.
13505	 */
13506	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
13507	switch (if_type) {
13508	case LPFC_SLI_INTF_IF_TYPE_0:
13509		if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
13510			&uerr_sta_lo) ||
13511			lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
13512			&uerr_sta_hi)) {
13513			phba->work_hs |= UNPLUG_ERR;
13514			phba->work_ha |= HA_ERATT;
13515			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13516			return 1;
13517		}
13518		if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
13519		    (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
13520			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13521					"1423 HBA Unrecoverable error: "
13522					"uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
13523					"ue_mask_lo_reg=0x%x, "
13524					"ue_mask_hi_reg=0x%x\n",
13525					uerr_sta_lo, uerr_sta_hi,
13526					phba->sli4_hba.ue_mask_lo,
13527					phba->sli4_hba.ue_mask_hi);
13528			phba->work_status[0] = uerr_sta_lo;
13529			phba->work_status[1] = uerr_sta_hi;
13530			phba->work_ha |= HA_ERATT;
13531			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13532			return 1;
13533		}
13534		break;
13535	case LPFC_SLI_INTF_IF_TYPE_2:
13536	case LPFC_SLI_INTF_IF_TYPE_6:
13537		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
13538			&portstat_reg.word0) ||
13539			lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
13540			&portsmphr)){
13541			phba->work_hs |= UNPLUG_ERR;
13542			phba->work_ha |= HA_ERATT;
13543			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13544			return 1;
13545		}
13546		if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
13547			phba->work_status[0] =
13548				readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
13549			phba->work_status[1] =
13550				readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
13551			logmask = LOG_TRACE_EVENT;
13552			if (phba->work_status[0] ==
13553				SLIPORT_ERR1_REG_ERR_CODE_2 &&
13554			    phba->work_status[1] == SLIPORT_ERR2_REG_FW_RESTART)
13555				logmask = LOG_SLI;
13556			lpfc_printf_log(phba, KERN_ERR, logmask,
13557					"2885 Port Status Event: "
13558					"port status reg 0x%x, "
13559					"port smphr reg 0x%x, "
13560					"error 1=0x%x, error 2=0x%x\n",
13561					portstat_reg.word0,
13562					portsmphr,
13563					phba->work_status[0],
13564					phba->work_status[1]);
13565			phba->work_ha |= HA_ERATT;
13566			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);
13567			return 1;
13568		}
13569		break;
13570	case LPFC_SLI_INTF_IF_TYPE_1:
13571	default:
13572		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13573				"2886 HBA Error Attention on unsupported "
13574				"if type %d.", if_type);
13575		return 1;
13576	}
13577
13578	return 0;
13579}
13580
13581/**
13582 * lpfc_sli_check_eratt - check error attention events
13583 * @phba: Pointer to HBA context.
13584 *
13585 * This function is called from timer soft interrupt context to check HBA's
13586 * error attention register bit for error attention events.
13587 *
13588 * This function returns 1 when there is Error Attention in the Host Attention
13589 * Register and returns 0 otherwise.
13590 **/
13591int
13592lpfc_sli_check_eratt(struct lpfc_hba *phba)
13593{
13594	uint32_t ha_copy;
13595
13596	/* If somebody is waiting to handle an eratt, don't process it
13597	 * here. The brdkill function will do this.
13598	 */
13599	if (phba->link_flag & LS_IGNORE_ERATT)
13600		return 0;
13601
13602	/* Check if interrupt handler handles this ERATT */
13603	if (test_bit(HBA_ERATT_HANDLED, &phba->hba_flag))
13604		/* Interrupt handler has handled ERATT */
13605		return 0;
13606
13607	/*
13608	 * If there is deferred error attention, do not check for error
13609	 * attention
13610	 */
13611	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
13612		return 0;
13613
13614	spin_lock_irq(&phba->hbalock);
13615	/* If PCI channel is offline, don't process it */
13616	if (unlikely(pci_channel_offline(phba->pcidev))) {
13617		spin_unlock_irq(&phba->hbalock);
13618		return 0;
13619	}
13620
13621	switch (phba->sli_rev) {
13622	case LPFC_SLI_REV2:
13623	case LPFC_SLI_REV3:
13624		/* Read chip Host Attention (HA) register */
13625		ha_copy = lpfc_sli_eratt_read(phba);
13626		break;
13627	case LPFC_SLI_REV4:
13628		/* Read device Uncoverable Error (UERR) registers */
13629		ha_copy = lpfc_sli4_eratt_read(phba);
13630		break;
13631	default:
13632		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13633				"0299 Invalid SLI revision (%d)\n",
13634				phba->sli_rev);
13635		ha_copy = 0;
13636		break;
13637	}
13638	spin_unlock_irq(&phba->hbalock);
13639
13640	return ha_copy;
13641}
13642
13643/**
13644 * lpfc_intr_state_check - Check device state for interrupt handling
13645 * @phba: Pointer to HBA context.
13646 *
13647 * This inline routine checks whether a device or its PCI slot is in a state
13648 * that the interrupt should be handled.
13649 *
13650 * This function returns 0 if the device or the PCI slot is in a state that
13651 * interrupt should be handled, otherwise -EIO.
13652 */
13653static inline int
13654lpfc_intr_state_check(struct lpfc_hba *phba)
13655{
13656	/* If the pci channel is offline, ignore all the interrupts */
13657	if (unlikely(pci_channel_offline(phba->pcidev)))
13658		return -EIO;
13659
13660	/* Update device level interrupt statistics */
13661	phba->sli.slistat.sli_intr++;
13662
13663	/* Ignore all interrupts during initialization. */
13664	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
13665		return -EIO;
13666
13667	return 0;
13668}
13669
13670/**
13671 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
13672 * @irq: Interrupt number.
13673 * @dev_id: The device context pointer.
13674 *
13675 * This function is directly called from the PCI layer as an interrupt
13676 * service routine when device with SLI-3 interface spec is enabled with
13677 * MSI-X multi-message interrupt mode and there are slow-path events in
13678 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
13679 * interrupt mode, this function is called as part of the device-level
13680 * interrupt handler. When the PCI slot is in error recovery or the HBA
13681 * is undergoing initialization, the interrupt handler will not process
13682 * the interrupt. The link attention and ELS ring attention events are
13683 * handled by the worker thread. The interrupt handler signals the worker
13684 * thread and returns for these events. This function is called without
13685 * any lock held. It gets the hbalock to access and update SLI data
13686 * structures.
13687 *
13688 * This function returns IRQ_HANDLED when interrupt is handled else it
13689 * returns IRQ_NONE.
13690 **/
13691irqreturn_t
13692lpfc_sli_sp_intr_handler(int irq, void *dev_id)
13693{
13694	struct lpfc_hba  *phba;
13695	uint32_t ha_copy, hc_copy;
13696	uint32_t work_ha_copy;
13697	unsigned long status;
13698	unsigned long iflag;
13699	uint32_t control;
13700
13701	MAILBOX_t *mbox, *pmbox;
13702	struct lpfc_vport *vport;
13703	struct lpfc_nodelist *ndlp;
13704	struct lpfc_dmabuf *mp;
13705	LPFC_MBOXQ_t *pmb;
13706	int rc;
13707
13708	/*
13709	 * Get the driver's phba structure from the dev_id and
13710	 * assume the HBA is not interrupting.
13711	 */
13712	phba = (struct lpfc_hba *)dev_id;
13713
13714	if (unlikely(!phba))
13715		return IRQ_NONE;
13716
13717	/*
13718	 * Stuff needs to be attented to when this function is invoked as an
13719	 * individual interrupt handler in MSI-X multi-message interrupt mode
13720	 */
13721	if (phba->intr_type == MSIX) {
13722		/* Check device state for handling interrupt */
13723		if (lpfc_intr_state_check(phba))
13724			return IRQ_NONE;
13725		/* Need to read HA REG for slow-path events */
13726		spin_lock_irqsave(&phba->hbalock, iflag);
13727		if (lpfc_readl(phba->HAregaddr, &ha_copy))
13728			goto unplug_error;
13729		/* If somebody is waiting to handle an eratt don't process it
13730		 * here. The brdkill function will do this.
13731		 */
13732		if (phba->link_flag & LS_IGNORE_ERATT)
13733			ha_copy &= ~HA_ERATT;
13734		/* Check the need for handling ERATT in interrupt handler */
13735		if (ha_copy & HA_ERATT) {
13736			if (test_and_set_bit(HBA_ERATT_HANDLED,
13737					     &phba->hba_flag))
13738				/* ERATT polling has handled ERATT */
13739				ha_copy &= ~HA_ERATT;
13740		}
13741
13742		/*
13743		 * If there is deferred error attention, do not check for any
13744		 * interrupt.
13745		 */
13746		if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
13747			spin_unlock_irqrestore(&phba->hbalock, iflag);
13748			return IRQ_NONE;
13749		}
13750
13751		/* Clear up only attention source related to slow-path */
13752		if (lpfc_readl(phba->HCregaddr, &hc_copy))
13753			goto unplug_error;
13754
13755		writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
13756			HC_LAINT_ENA | HC_ERINT_ENA),
13757			phba->HCregaddr);
13758		writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
13759			phba->HAregaddr);
13760		writel(hc_copy, phba->HCregaddr);
13761		readl(phba->HAregaddr); /* flush */
13762		spin_unlock_irqrestore(&phba->hbalock, iflag);
13763	} else
13764		ha_copy = phba->ha_copy;
13765
13766	work_ha_copy = ha_copy & phba->work_ha_mask;
13767
13768	if (work_ha_copy) {
13769		if (work_ha_copy & HA_LATT) {
13770			if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
13771				/*
13772				 * Turn off Link Attention interrupts
13773				 * until CLEAR_LA done
13774				 */
13775				spin_lock_irqsave(&phba->hbalock, iflag);
13776				phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
13777				if (lpfc_readl(phba->HCregaddr, &control))
13778					goto unplug_error;
13779				control &= ~HC_LAINT_ENA;
13780				writel(control, phba->HCregaddr);
13781				readl(phba->HCregaddr); /* flush */
13782				spin_unlock_irqrestore(&phba->hbalock, iflag);
13783			}
13784			else
13785				work_ha_copy &= ~HA_LATT;
13786		}
13787
13788		if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
13789			/*
13790			 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
13791			 * the only slow ring.
13792			 */
13793			status = (work_ha_copy &
13794				(HA_RXMASK  << (4*LPFC_ELS_RING)));
13795			status >>= (4*LPFC_ELS_RING);
13796			if (status & HA_RXMASK) {
13797				spin_lock_irqsave(&phba->hbalock, iflag);
13798				if (lpfc_readl(phba->HCregaddr, &control))
13799					goto unplug_error;
13800
13801				lpfc_debugfs_slow_ring_trc(phba,
13802				"ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
13803				control, status,
13804				(uint32_t)phba->sli.slistat.sli_intr);
13805
13806				if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
13807					lpfc_debugfs_slow_ring_trc(phba,
13808						"ISR Disable ring:"
13809						"pwork:x%x hawork:x%x wait:x%x",
13810						phba->work_ha, work_ha_copy,
13811						(uint32_t)((unsigned long)
13812						&phba->work_waitq));
13813
13814					control &=
13815					    ~(HC_R0INT_ENA << LPFC_ELS_RING);
13816					writel(control, phba->HCregaddr);
13817					readl(phba->HCregaddr); /* flush */
13818				}
13819				else {
13820					lpfc_debugfs_slow_ring_trc(phba,
13821						"ISR slow ring:   pwork:"
13822						"x%x hawork:x%x wait:x%x",
13823						phba->work_ha, work_ha_copy,
13824						(uint32_t)((unsigned long)
13825						&phba->work_waitq));
13826				}
13827				spin_unlock_irqrestore(&phba->hbalock, iflag);
13828			}
13829		}
13830		spin_lock_irqsave(&phba->hbalock, iflag);
13831		if (work_ha_copy & HA_ERATT) {
13832			if (lpfc_sli_read_hs(phba))
13833				goto unplug_error;
13834			/*
13835			 * Check if there is a deferred error condition
13836			 * is active
13837			 */
13838			if ((HS_FFER1 & phba->work_hs) &&
13839				((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
13840				  HS_FFER6 | HS_FFER7 | HS_FFER8) &
13841				  phba->work_hs)) {
13842				set_bit(DEFER_ERATT, &phba->hba_flag);
13843				/* Clear all interrupt enable conditions */
13844				writel(0, phba->HCregaddr);
13845				readl(phba->HCregaddr);
13846			}
13847		}
13848
13849		if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
13850			pmb = phba->sli.mbox_active;
13851			pmbox = &pmb->u.mb;
13852			mbox = phba->mbox;
13853			vport = pmb->vport;
13854
13855			/* First check out the status word */
13856			lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
13857			if (pmbox->mbxOwner != OWN_HOST) {
13858				spin_unlock_irqrestore(&phba->hbalock, iflag);
13859				/*
13860				 * Stray Mailbox Interrupt, mbxCommand <cmd>
13861				 * mbxStatus <status>
13862				 */
13863				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
13864						"(%d):0304 Stray Mailbox "
13865						"Interrupt mbxCommand x%x "
13866						"mbxStatus x%x\n",
13867						(vport ? vport->vpi : 0),
13868						pmbox->mbxCommand,
13869						pmbox->mbxStatus);
13870				/* clear mailbox attention bit */
13871				work_ha_copy &= ~HA_MBATT;
13872			} else {
13873				phba->sli.mbox_active = NULL;
13874				spin_unlock_irqrestore(&phba->hbalock, iflag);
13875				phba->last_completion_time = jiffies;
13876				timer_delete(&phba->sli.mbox_tmo);
13877				if (pmb->mbox_cmpl) {
13878					lpfc_sli_pcimem_bcopy(mbox, pmbox,
13879							MAILBOX_CMD_SIZE);
13880					if (pmb->out_ext_byte_len &&
13881						pmb->ext_buf)
13882						lpfc_sli_pcimem_bcopy(
13883						phba->mbox_ext,
13884						pmb->ext_buf,
13885						pmb->out_ext_byte_len);
13886				}
13887				if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13888					pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13889
13890					lpfc_debugfs_disc_trc(vport,
13891						LPFC_DISC_TRC_MBOX_VPORT,
13892						"MBOX dflt rpi: : "
13893						"status:x%x rpi:x%x",
13894						(uint32_t)pmbox->mbxStatus,
13895						pmbox->un.varWords[0], 0);
13896
13897					if (!pmbox->mbxStatus) {
13898						mp = pmb->ctx_buf;
13899						ndlp = pmb->ctx_ndlp;
13900
13901						/* Reg_LOGIN of dflt RPI was
13902						 * successful. new lets get
13903						 * rid of the RPI using the
13904						 * same mbox buffer.
13905						 */
13906						lpfc_unreg_login(phba,
13907							vport->vpi,
13908							pmbox->un.varWords[0],
13909							pmb);
13910						pmb->mbox_cmpl =
13911							lpfc_mbx_cmpl_dflt_rpi;
13912						pmb->ctx_buf = mp;
13913						pmb->ctx_ndlp = ndlp;
13914						pmb->vport = vport;
13915						rc = lpfc_sli_issue_mbox(phba,
13916								pmb,
13917								MBX_NOWAIT);
13918						if (rc != MBX_BUSY)
13919							lpfc_printf_log(phba,
13920							KERN_ERR,
13921							LOG_TRACE_EVENT,
13922							"0350 rc should have"
13923							"been MBX_BUSY\n");
13924						if (rc != MBX_NOT_FINISHED)
13925							goto send_current_mbox;
13926					}
13927				}
13928				spin_lock_irqsave(
13929						&phba->pport->work_port_lock,
13930						iflag);
13931				phba->pport->work_port_events &=
13932					~WORKER_MBOX_TMO;
13933				spin_unlock_irqrestore(
13934						&phba->pport->work_port_lock,
13935						iflag);
13936
13937				/* Do NOT queue MBX_HEARTBEAT to the worker
13938				 * thread for processing.
13939				 */
13940				if (pmbox->mbxCommand == MBX_HEARTBEAT) {
13941					/* Process mbox now */
13942					phba->sli.mbox_active = NULL;
13943					phba->sli.sli_flag &=
13944						~LPFC_SLI_MBOX_ACTIVE;
13945					if (pmb->mbox_cmpl)
13946						pmb->mbox_cmpl(phba, pmb);
13947				} else {
13948					/* Queue to worker thread to process */
13949					lpfc_mbox_cmpl_put(phba, pmb);
13950				}
13951			}
13952		} else
13953			spin_unlock_irqrestore(&phba->hbalock, iflag);
13954
13955		if ((work_ha_copy & HA_MBATT) &&
13956		    (phba->sli.mbox_active == NULL)) {
13957send_current_mbox:
13958			/* Process next mailbox command if there is one */
13959			do {
13960				rc = lpfc_sli_issue_mbox(phba, NULL,
13961							 MBX_NOWAIT);
13962			} while (rc == MBX_NOT_FINISHED);
13963			if (rc != MBX_SUCCESS)
13964				lpfc_printf_log(phba, KERN_ERR,
13965						LOG_TRACE_EVENT,
13966						"0349 rc should be "
13967						"MBX_SUCCESS\n");
13968		}
13969
13970		spin_lock_irqsave(&phba->hbalock, iflag);
13971		phba->work_ha |= work_ha_copy;
13972		spin_unlock_irqrestore(&phba->hbalock, iflag);
13973		lpfc_worker_wake_up(phba);
13974	}
13975	return IRQ_HANDLED;
13976unplug_error:
13977	spin_unlock_irqrestore(&phba->hbalock, iflag);
13978	return IRQ_HANDLED;
13979
13980} /* lpfc_sli_sp_intr_handler */
13981
13982/**
13983 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
13984 * @irq: Interrupt number.
13985 * @dev_id: The device context pointer.
13986 *
13987 * This function is directly called from the PCI layer as an interrupt
13988 * service routine when device with SLI-3 interface spec is enabled with
13989 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13990 * ring event in the HBA. However, when the device is enabled with either
13991 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13992 * device-level interrupt handler. When the PCI slot is in error recovery
13993 * or the HBA is undergoing initialization, the interrupt handler will not
13994 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13995 * the intrrupt context. This function is called without any lock held.
13996 * It gets the hbalock to access and update SLI data structures.
13997 *
13998 * This function returns IRQ_HANDLED when interrupt is handled else it
13999 * returns IRQ_NONE.
14000 **/
14001irqreturn_t
14002lpfc_sli_fp_intr_handler(int irq, void *dev_id)
14003{
14004	struct lpfc_hba  *phba;
14005	uint32_t ha_copy;
14006	unsigned long status;
14007	unsigned long iflag;
14008	struct lpfc_sli_ring *pring;
14009
14010	/* Get the driver's phba structure from the dev_id and
14011	 * assume the HBA is not interrupting.
14012	 */
14013	phba = (struct lpfc_hba *) dev_id;
14014
14015	if (unlikely(!phba))
14016		return IRQ_NONE;
14017
14018	/*
14019	 * Stuff needs to be attented to when this function is invoked as an
14020	 * individual interrupt handler in MSI-X multi-message interrupt mode
14021	 */
14022	if (phba->intr_type == MSIX) {
14023		/* Check device state for handling interrupt */
14024		if (lpfc_intr_state_check(phba))
14025			return IRQ_NONE;
14026		/* Need to read HA REG for FCP ring and other ring events */
14027		if (lpfc_readl(phba->HAregaddr, &ha_copy))
14028			return IRQ_HANDLED;
14029
14030		/*
14031		 * If there is deferred error attention, do not check for
14032		 * any interrupt.
14033		 */
14034		if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))
14035			return IRQ_NONE;
14036
14037		/* Clear up only attention source related to fast-path */
14038		spin_lock_irqsave(&phba->hbalock, iflag);
14039		writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
14040			phba->HAregaddr);
14041		readl(phba->HAregaddr); /* flush */
14042		spin_unlock_irqrestore(&phba->hbalock, iflag);
14043	} else
14044		ha_copy = phba->ha_copy;
14045
14046	/*
14047	 * Process all events on FCP ring. Take the optimized path for FCP IO.
14048	 */
14049	ha_copy &= ~(phba->work_ha_mask);
14050
14051	status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
14052	status >>= (4*LPFC_FCP_RING);
14053	pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
14054	if (status & HA_RXMASK)
14055		lpfc_sli_handle_fast_ring_event(phba, pring, status);
14056
14057	if (phba->cfg_multi_ring_support == 2) {
14058		/*
14059		 * Process all events on extra ring. Take the optimized path
14060		 * for extra ring IO.
14061		 */
14062		status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
14063		status >>= (4*LPFC_EXTRA_RING);
14064		if (status & HA_RXMASK) {
14065			lpfc_sli_handle_fast_ring_event(phba,
14066					&phba->sli.sli3_ring[LPFC_EXTRA_RING],
14067					status);
14068		}
14069	}
14070	return IRQ_HANDLED;
14071}  /* lpfc_sli_fp_intr_handler */
14072
14073/**
14074 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
14075 * @irq: Interrupt number.
14076 * @dev_id: The device context pointer.
14077 *
14078 * This function is the HBA device-level interrupt handler to device with
14079 * SLI-3 interface spec, called from the PCI layer when either MSI or
14080 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
14081 * requires driver attention. This function invokes the slow-path interrupt
14082 * attention handling function and fast-path interrupt attention handling
14083 * function in turn to process the relevant HBA attention events. This
14084 * function is called without any lock held. It gets the hbalock to access
14085 * and update SLI data structures.
14086 *
14087 * This function returns IRQ_HANDLED when interrupt is handled, else it
14088 * returns IRQ_NONE.
14089 **/
14090irqreturn_t
14091lpfc_sli_intr_handler(int irq, void *dev_id)
14092{
14093	struct lpfc_hba  *phba;
14094	irqreturn_t sp_irq_rc, fp_irq_rc;
14095	unsigned long status1, status2;
14096	uint32_t hc_copy;
14097
14098	/*
14099	 * Get the driver's phba structure from the dev_id and
14100	 * assume the HBA is not interrupting.
14101	 */
14102	phba = (struct lpfc_hba *) dev_id;
14103
14104	if (unlikely(!phba))
14105		return IRQ_NONE;
14106
14107	/* Check device state for handling interrupt */
14108	if (lpfc_intr_state_check(phba))
14109		return IRQ_NONE;
14110
14111	spin_lock(&phba->hbalock);
14112	if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
14113		spin_unlock(&phba->hbalock);
14114		return IRQ_HANDLED;
14115	}
14116
14117	if (unlikely(!phba->ha_copy)) {
14118		spin_unlock(&phba->hbalock);
14119		return IRQ_NONE;
14120	} else if (phba->ha_copy & HA_ERATT) {
14121		if (test_and_set_bit(HBA_ERATT_HANDLED, &phba->hba_flag))
14122			/* ERATT polling has handled ERATT */
14123			phba->ha_copy &= ~HA_ERATT;
14124	}
14125
14126	/*
14127	 * If there is deferred error attention, do not check for any interrupt.
14128	 */
14129	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {
14130		spin_unlock(&phba->hbalock);
14131		return IRQ_NONE;
14132	}
14133
14134	/* Clear attention sources except link and error attentions */
14135	if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
14136		spin_unlock(&phba->hbalock);
14137		return IRQ_HANDLED;
14138	}
14139	writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
14140		| HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
14141		phba->HCregaddr);
14142	writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
14143	writel(hc_copy, phba->HCregaddr);
14144	readl(phba->HAregaddr); /* flush */
14145	spin_unlock(&phba->hbalock);
14146
14147	/*
14148	 * Invokes slow-path host attention interrupt handling as appropriate.
14149	 */
14150
14151	/* status of events with mailbox and link attention */
14152	status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
14153
14154	/* status of events with ELS ring */
14155	status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
14156	status2 >>= (4*LPFC_ELS_RING);
14157
14158	if (status1 || (status2 & HA_RXMASK))
14159		sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
14160	else
14161		sp_irq_rc = IRQ_NONE;
14162
14163	/*
14164	 * Invoke fast-path host attention interrupt handling as appropriate.
14165	 */
14166
14167	/* status of events with FCP ring */
14168	status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
14169	status1 >>= (4*LPFC_FCP_RING);
14170
14171	/* status of events with extra ring */
14172	if (phba->cfg_multi_ring_support == 2) {
14173		status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
14174		status2 >>= (4*LPFC_EXTRA_RING);
14175	} else
14176		status2 = 0;
14177
14178	if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
14179		fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
14180	else
14181		fp_irq_rc = IRQ_NONE;
14182
14183	/* Return device-level interrupt handling status */
14184	return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
14185}  /* lpfc_sli_intr_handler */
14186
14187/**
14188 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
14189 * @phba: pointer to lpfc hba data structure.
14190 *
14191 * This routine is invoked by the worker thread to process all the pending
14192 * SLI4 els abort xri events.
14193 **/
14194void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
14195{
14196	struct lpfc_cq_event *cq_event;
14197	unsigned long iflags;
14198
14199	/* First, declare the els xri abort event has been handled */
14200	clear_bit(ELS_XRI_ABORT_EVENT, &phba->hba_flag);
14201
14202	/* Now, handle all the els xri abort events */
14203	spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14204	while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
14205		/* Get the first event from the head of the event queue */
14206		list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
14207				 cq_event, struct lpfc_cq_event, list);
14208		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14209				       iflags);
14210		/* Notify aborted XRI for ELS work queue */
14211		lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
14212
14213		/* Free the event processed back to the free pool */
14214		lpfc_sli4_cq_event_release(phba, cq_event);
14215		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14216				  iflags);
14217	}
14218	spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
14219}
14220
14221/**
14222 * lpfc_sli4_els_preprocess_rspiocbq - Get response iocbq from els wcqe
14223 * @phba: Pointer to HBA context object.
14224 * @irspiocbq: Pointer to work-queue completion queue entry.
14225 *
14226 * This routine handles an ELS work-queue completion event and construct
14227 * a pseudo response ELS IOCBQ from the SLI4 ELS WCQE for the common
14228 * discovery engine to handle.
14229 *
14230 * Return: Pointer to the receive IOCBQ, NULL otherwise.
14231 **/
14232static struct lpfc_iocbq *
14233lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba *phba,
14234				  struct lpfc_iocbq *irspiocbq)
14235{
14236	struct lpfc_sli_ring *pring;
14237	struct lpfc_iocbq *cmdiocbq;
14238	struct lpfc_wcqe_complete *wcqe;
14239	unsigned long iflags;
14240
14241	pring = lpfc_phba_elsring(phba);
14242	if (unlikely(!pring))
14243		return NULL;
14244
14245	wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
14246	spin_lock_irqsave(&pring->ring_lock, iflags);
14247	pring->stats.iocb_event++;
14248	/* Look up the ELS command IOCB and create pseudo response IOCB */
14249	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
14250				bf_get(lpfc_wcqe_c_request_tag, wcqe));
14251	if (unlikely(!cmdiocbq)) {
14252		spin_unlock_irqrestore(&pring->ring_lock, iflags);
14253		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14254				"0386 ELS complete with no corresponding "
14255				"cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
14256				wcqe->word0, wcqe->total_data_placed,
14257				wcqe->parameter, wcqe->word3);
14258		lpfc_sli_release_iocbq(phba, irspiocbq);
14259		return NULL;
14260	}
14261
14262	memcpy(&irspiocbq->wqe, &cmdiocbq->wqe, sizeof(union lpfc_wqe128));
14263	memcpy(&irspiocbq->wcqe_cmpl, wcqe, sizeof(*wcqe));
14264
14265	/* Put the iocb back on the txcmplq */
14266	lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
14267	spin_unlock_irqrestore(&pring->ring_lock, iflags);
14268
14269	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
14270		spin_lock_irqsave(&phba->hbalock, iflags);
14271		irspiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;
14272		spin_unlock_irqrestore(&phba->hbalock, iflags);
14273	}
14274
14275	return irspiocbq;
14276}
14277
14278inline struct lpfc_cq_event *
14279lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
14280{
14281	struct lpfc_cq_event *cq_event;
14282
14283	/* Allocate a new internal CQ_EVENT entry */
14284	cq_event = lpfc_sli4_cq_event_alloc(phba);
14285	if (!cq_event) {
14286		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14287				"0602 Failed to alloc CQ_EVENT entry\n");
14288		return NULL;
14289	}
14290
14291	/* Move the CQE into the event */
14292	memcpy(&cq_event->cqe, entry, size);
14293	return cq_event;
14294}
14295
14296/**
14297 * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
14298 * @phba: Pointer to HBA context object.
14299 * @mcqe: Pointer to mailbox completion queue entry.
14300 *
14301 * This routine process a mailbox completion queue entry with asynchronous
14302 * event.
14303 *
14304 * Return: true if work posted to worker thread, otherwise false.
14305 **/
14306static bool
14307lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14308{
14309	struct lpfc_cq_event *cq_event;
14310	unsigned long iflags;
14311
14312	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14313			"0392 Async Event: word0:x%x, word1:x%x, "
14314			"word2:x%x, word3:x%x\n", mcqe->word0,
14315			mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
14316
14317	cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
14318	if (!cq_event)
14319		return false;
14320
14321	spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
14322	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
14323	spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
14324
14325	/* Set the async event flag */
14326	set_bit(ASYNC_EVENT, &phba->hba_flag);
14327
14328	return true;
14329}
14330
14331/**
14332 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
14333 * @phba: Pointer to HBA context object.
14334 * @mcqe: Pointer to mailbox completion queue entry.
14335 *
14336 * This routine process a mailbox completion queue entry with mailbox
14337 * completion event.
14338 *
14339 * Return: true if work posted to worker thread, otherwise false.
14340 **/
14341static bool
14342lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14343{
14344	uint32_t mcqe_status;
14345	MAILBOX_t *mbox, *pmbox;
14346	struct lpfc_mqe *mqe;
14347	struct lpfc_vport *vport;
14348	struct lpfc_nodelist *ndlp;
14349	struct lpfc_dmabuf *mp;
14350	unsigned long iflags;
14351	LPFC_MBOXQ_t *pmb;
14352	bool workposted = false;
14353	int rc;
14354
14355	/* If not a mailbox complete MCQE, out by checking mailbox consume */
14356	if (!bf_get(lpfc_trailer_completed, mcqe))
14357		goto out_no_mqe_complete;
14358
14359	/* Get the reference to the active mbox command */
14360	spin_lock_irqsave(&phba->hbalock, iflags);
14361	pmb = phba->sli.mbox_active;
14362	if (unlikely(!pmb)) {
14363		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14364				"1832 No pending MBOX command to handle\n");
14365		spin_unlock_irqrestore(&phba->hbalock, iflags);
14366		goto out_no_mqe_complete;
14367	}
14368	spin_unlock_irqrestore(&phba->hbalock, iflags);
14369	mqe = &pmb->u.mqe;
14370	pmbox = (MAILBOX_t *)&pmb->u.mqe;
14371	mbox = phba->mbox;
14372	vport = pmb->vport;
14373
14374	/* Reset heartbeat timer */
14375	phba->last_completion_time = jiffies;
14376	timer_delete(&phba->sli.mbox_tmo);
14377
14378	/* Move mbox data to caller's mailbox region, do endian swapping */
14379	if (pmb->mbox_cmpl && mbox)
14380		lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
14381
14382	/*
14383	 * For mcqe errors, conditionally move a modified error code to
14384	 * the mbox so that the error will not be missed.
14385	 */
14386	mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
14387	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
14388		if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
14389			bf_set(lpfc_mqe_status, mqe,
14390			       (LPFC_MBX_ERROR_RANGE | mcqe_status));
14391	}
14392	if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
14393		pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
14394		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
14395				      "MBOX dflt rpi: status:x%x rpi:x%x",
14396				      mcqe_status,
14397				      pmbox->un.varWords[0], 0);
14398		if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
14399			mp = pmb->ctx_buf;
14400			ndlp = pmb->ctx_ndlp;
14401
14402			/* Reg_LOGIN of dflt RPI was successful. Mark the
14403			 * node as having an UNREG_LOGIN in progress to stop
14404			 * an unsolicited PLOGI from the same NPortId from
14405			 * starting another mailbox transaction.
14406			 */
14407			set_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
14408			lpfc_unreg_login(phba, vport->vpi,
14409					 pmbox->un.varWords[0], pmb);
14410			pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
14411			pmb->ctx_buf = mp;
14412
14413			/* No reference taken here.  This is a default
14414			 * RPI reg/immediate unreg cycle. The reference was
14415			 * taken in the reg rpi path and is released when
14416			 * this mailbox completes.
14417			 */
14418			pmb->ctx_ndlp = ndlp;
14419			pmb->vport = vport;
14420			rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
14421			if (rc != MBX_BUSY)
14422				lpfc_printf_log(phba, KERN_ERR,
14423						LOG_TRACE_EVENT,
14424						"0385 rc should "
14425						"have been MBX_BUSY\n");
14426			if (rc != MBX_NOT_FINISHED)
14427				goto send_current_mbox;
14428		}
14429	}
14430	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
14431	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
14432	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
14433
14434	/* Do NOT queue MBX_HEARTBEAT to the worker thread for processing. */
14435	if (pmbox->mbxCommand == MBX_HEARTBEAT) {
14436		spin_lock_irqsave(&phba->hbalock, iflags);
14437		/* Release the mailbox command posting token */
14438		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14439		phba->sli.mbox_active = NULL;
14440		if (bf_get(lpfc_trailer_consumed, mcqe))
14441			lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14442		spin_unlock_irqrestore(&phba->hbalock, iflags);
14443
14444		/* Post the next mbox command, if there is one */
14445		lpfc_sli4_post_async_mbox(phba);
14446
14447		/* Process cmpl now */
14448		if (pmb->mbox_cmpl)
14449			pmb->mbox_cmpl(phba, pmb);
14450		return false;
14451	}
14452
14453	/* There is mailbox completion work to queue to the worker thread */
14454	spin_lock_irqsave(&phba->hbalock, iflags);
14455	__lpfc_mbox_cmpl_put(phba, pmb);
14456	phba->work_ha |= HA_MBATT;
14457	spin_unlock_irqrestore(&phba->hbalock, iflags);
14458	workposted = true;
14459
14460send_current_mbox:
14461	spin_lock_irqsave(&phba->hbalock, iflags);
14462	/* Release the mailbox command posting token */
14463	phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14464	/* Setting active mailbox pointer need to be in sync to flag clear */
14465	phba->sli.mbox_active = NULL;
14466	if (bf_get(lpfc_trailer_consumed, mcqe))
14467		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14468	spin_unlock_irqrestore(&phba->hbalock, iflags);
14469	/* Wake up worker thread to post the next pending mailbox command */
14470	lpfc_worker_wake_up(phba);
14471	return workposted;
14472
14473out_no_mqe_complete:
14474	spin_lock_irqsave(&phba->hbalock, iflags);
14475	if (bf_get(lpfc_trailer_consumed, mcqe))
14476		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14477	spin_unlock_irqrestore(&phba->hbalock, iflags);
14478	return false;
14479}
14480
14481/**
14482 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
14483 * @phba: Pointer to HBA context object.
14484 * @cq: Pointer to associated CQ
14485 * @cqe: Pointer to mailbox completion queue entry.
14486 *
14487 * This routine process a mailbox completion queue entry, it invokes the
14488 * proper mailbox complete handling or asynchronous event handling routine
14489 * according to the MCQE's async bit.
14490 *
14491 * Return: true if work posted to worker thread, otherwise false.
14492 **/
14493static bool
14494lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14495			 struct lpfc_cqe *cqe)
14496{
14497	struct lpfc_mcqe mcqe;
14498	bool workposted;
14499
14500	cq->CQ_mbox++;
14501
14502	/* Copy the mailbox MCQE and convert endian order as needed */
14503	lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
14504
14505	/* Invoke the proper event handling routine */
14506	if (!bf_get(lpfc_trailer_async, &mcqe))
14507		workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
14508	else
14509		workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
14510	return workposted;
14511}
14512
14513/**
14514 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
14515 * @phba: Pointer to HBA context object.
14516 * @cq: Pointer to associated CQ
14517 * @wcqe: Pointer to work-queue completion queue entry.
14518 *
14519 * This routine handles an ELS work-queue completion event.
14520 *
14521 * Return: true if work posted to worker thread, otherwise false.
14522 **/
14523static bool
14524lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14525			     struct lpfc_wcqe_complete *wcqe)
14526{
14527	struct lpfc_iocbq *irspiocbq;
14528	unsigned long iflags;
14529	struct lpfc_sli_ring *pring = cq->pring;
14530	int txq_cnt = 0;
14531	int txcmplq_cnt = 0;
14532
14533	/* Check for response status */
14534	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14535		/* Log the error status */
14536		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14537				"0357 ELS CQE error: status=x%x: "
14538				"CQE: %08x %08x %08x %08x\n",
14539				bf_get(lpfc_wcqe_c_status, wcqe),
14540				wcqe->word0, wcqe->total_data_placed,
14541				wcqe->parameter, wcqe->word3);
14542	}
14543
14544	/* Get an irspiocbq for later ELS response processing use */
14545	irspiocbq = lpfc_sli_get_iocbq(phba);
14546	if (!irspiocbq) {
14547		if (!list_empty(&pring->txq))
14548			txq_cnt++;
14549		if (!list_empty(&pring->txcmplq))
14550			txcmplq_cnt++;
14551		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14552			"0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
14553			"els_txcmplq_cnt=%d\n",
14554			txq_cnt, phba->iocb_cnt,
14555			txcmplq_cnt);
14556		return false;
14557	}
14558
14559	/* Save off the slow-path queue event for work thread to process */
14560	memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
14561	spin_lock_irqsave(&phba->hbalock, iflags);
14562	list_add_tail(&irspiocbq->cq_event.list,
14563		      &phba->sli4_hba.sp_queue_event);
14564	spin_unlock_irqrestore(&phba->hbalock, iflags);
14565	set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
14566
14567	return true;
14568}
14569
14570/**
14571 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
14572 * @phba: Pointer to HBA context object.
14573 * @wcqe: Pointer to work-queue completion queue entry.
14574 *
14575 * This routine handles slow-path WQ entry consumed event by invoking the
14576 * proper WQ release routine to the slow-path WQ.
14577 **/
14578static void
14579lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
14580			     struct lpfc_wcqe_release *wcqe)
14581{
14582	/* sanity check on queue memory */
14583	if (unlikely(!phba->sli4_hba.els_wq))
14584		return;
14585	/* Check for the slow-path ELS work queue */
14586	if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
14587		lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
14588				     bf_get(lpfc_wcqe_r_wqe_index, wcqe));
14589	else
14590		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14591				"2579 Slow-path wqe consume event carries "
14592				"miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
14593				bf_get(lpfc_wcqe_r_wqe_index, wcqe),
14594				phba->sli4_hba.els_wq->queue_id);
14595}
14596
14597/**
14598 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
14599 * @phba: Pointer to HBA context object.
14600 * @cq: Pointer to a WQ completion queue.
14601 * @wcqe: Pointer to work-queue completion queue entry.
14602 *
14603 * This routine handles an XRI abort event.
14604 *
14605 * Return: true if work posted to worker thread, otherwise false.
14606 **/
14607static bool
14608lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
14609				   struct lpfc_queue *cq,
14610				   struct sli4_wcqe_xri_aborted *wcqe)
14611{
14612	bool workposted = false;
14613	struct lpfc_cq_event *cq_event;
14614	unsigned long iflags;
14615
14616	switch (cq->subtype) {
14617	case LPFC_IO:
14618		lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
14619		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
14620			/* Notify aborted XRI for NVME work queue */
14621			if (phba->nvmet_support)
14622				lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
14623		}
14624		workposted = false;
14625		break;
14626	case LPFC_NVME_LS: /* NVME LS uses ELS resources */
14627	case LPFC_ELS:
14628		cq_event = lpfc_cq_event_setup(phba, wcqe, sizeof(*wcqe));
14629		if (!cq_event) {
14630			workposted = false;
14631			break;
14632		}
14633		cq_event->hdwq = cq->hdwq;
14634		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14635				  iflags);
14636		list_add_tail(&cq_event->list,
14637			      &phba->sli4_hba.sp_els_xri_aborted_work_queue);
14638		/* Set the els xri abort event flag */
14639		set_bit(ELS_XRI_ABORT_EVENT, &phba->hba_flag);
14640		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14641				       iflags);
14642		workposted = true;
14643		break;
14644	default:
14645		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14646				"0603 Invalid CQ subtype %d: "
14647				"%08x %08x %08x %08x\n",
14648				cq->subtype, wcqe->word0, wcqe->parameter,
14649				wcqe->word2, wcqe->word3);
14650		workposted = false;
14651		break;
14652	}
14653	return workposted;
14654}
14655
14656#define FC_RCTL_MDS_DIAGS	0xF4
14657
14658/**
14659 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
14660 * @phba: Pointer to HBA context object.
14661 * @rcqe: Pointer to receive-queue completion queue entry.
14662 *
14663 * This routine process a receive-queue completion queue entry.
14664 *
14665 * Return: true if work posted to worker thread, otherwise false.
14666 **/
14667static bool
14668lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
14669{
14670	bool workposted = false;
14671	struct fc_frame_header *fc_hdr;
14672	struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
14673	struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
14674	struct lpfc_nvmet_tgtport *tgtp;
14675	struct hbq_dmabuf *dma_buf;
14676	uint32_t status, rq_id;
14677	unsigned long iflags;
14678
14679	/* sanity check on queue memory */
14680	if (unlikely(!hrq) || unlikely(!drq))
14681		return workposted;
14682
14683	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14684		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14685	else
14686		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14687	if (rq_id != hrq->queue_id)
14688		goto out;
14689
14690	status = bf_get(lpfc_rcqe_status, rcqe);
14691	switch (status) {
14692	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
14693		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14694				"2537 Receive Frame Truncated!!\n");
14695		fallthrough;
14696	case FC_STATUS_RQ_SUCCESS:
14697		spin_lock_irqsave(&phba->hbalock, iflags);
14698		lpfc_sli4_rq_release(hrq, drq);
14699		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14700		if (!dma_buf) {
14701			hrq->RQ_no_buf_found++;
14702			spin_unlock_irqrestore(&phba->hbalock, iflags);
14703			goto out;
14704		}
14705		hrq->RQ_rcv_buf++;
14706		hrq->RQ_buf_posted--;
14707		memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
14708
14709		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14710
14711		if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
14712		    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
14713			spin_unlock_irqrestore(&phba->hbalock, iflags);
14714			/* Handle MDS Loopback frames */
14715			if  (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
14716				lpfc_sli4_handle_mds_loopback(phba->pport,
14717							      dma_buf);
14718			else
14719				lpfc_in_buf_free(phba, &dma_buf->dbuf);
14720			break;
14721		}
14722
14723		/* save off the frame for the work thread to process */
14724		list_add_tail(&dma_buf->cq_event.list,
14725			      &phba->sli4_hba.sp_queue_event);
14726		spin_unlock_irqrestore(&phba->hbalock, iflags);
14727		/* Frame received */
14728		set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
14729		workposted = true;
14730		break;
14731	case FC_STATUS_INSUFF_BUF_FRM_DISC:
14732		if (phba->nvmet_support) {
14733			tgtp = phba->targetport->private;
14734			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14735					"6402 RQE Error x%x, posted %d err_cnt "
14736					"%d: %x %x %x\n",
14737					status, hrq->RQ_buf_posted,
14738					hrq->RQ_no_posted_buf,
14739					atomic_read(&tgtp->rcv_fcp_cmd_in),
14740					atomic_read(&tgtp->rcv_fcp_cmd_out),
14741					atomic_read(&tgtp->xmt_fcp_release));
14742		}
14743		fallthrough;
14744
14745	case FC_STATUS_INSUFF_BUF_NEED_BUF:
14746		hrq->RQ_no_posted_buf++;
14747		/* Post more buffers if possible */
14748		set_bit(HBA_POST_RECEIVE_BUFFER, &phba->hba_flag);
14749		workposted = true;
14750		break;
14751	case FC_STATUS_RQ_DMA_FAILURE:
14752		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14753				"2564 RQE DMA Error x%x, x%08x x%08x x%08x "
14754				"x%08x\n",
14755				status, rcqe->word0, rcqe->word1,
14756				rcqe->word2, rcqe->word3);
14757
14758		/* If IV set, no further recovery */
14759		if (bf_get(lpfc_rcqe_iv, rcqe))
14760			break;
14761
14762		/* recycle consumed resource */
14763		spin_lock_irqsave(&phba->hbalock, iflags);
14764		lpfc_sli4_rq_release(hrq, drq);
14765		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14766		if (!dma_buf) {
14767			hrq->RQ_no_buf_found++;
14768			spin_unlock_irqrestore(&phba->hbalock, iflags);
14769			break;
14770		}
14771		hrq->RQ_rcv_buf++;
14772		hrq->RQ_buf_posted--;
14773		spin_unlock_irqrestore(&phba->hbalock, iflags);
14774		lpfc_in_buf_free(phba, &dma_buf->dbuf);
14775		break;
14776	default:
14777		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14778				"2565 Unexpected RQE Status x%x, w0-3 x%08x "
14779				"x%08x x%08x x%08x\n",
14780				status, rcqe->word0, rcqe->word1,
14781				rcqe->word2, rcqe->word3);
14782		break;
14783	}
14784out:
14785	return workposted;
14786}
14787
14788/**
14789 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
14790 * @phba: Pointer to HBA context object.
14791 * @cq: Pointer to the completion queue.
14792 * @cqe: Pointer to a completion queue entry.
14793 *
14794 * This routine process a slow-path work-queue or receive queue completion queue
14795 * entry.
14796 *
14797 * Return: true if work posted to worker thread, otherwise false.
14798 **/
14799static bool
14800lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14801			 struct lpfc_cqe *cqe)
14802{
14803	struct lpfc_cqe cqevt;
14804	bool workposted = false;
14805
14806	/* Copy the work queue CQE and convert endian order if needed */
14807	lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
14808
14809	/* Check and process for different type of WCQE and dispatch */
14810	switch (bf_get(lpfc_cqe_code, &cqevt)) {
14811	case CQE_CODE_COMPL_WQE:
14812		/* Process the WQ/RQ complete event */
14813		phba->last_completion_time = jiffies;
14814		workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
14815				(struct lpfc_wcqe_complete *)&cqevt);
14816		break;
14817	case CQE_CODE_RELEASE_WQE:
14818		/* Process the WQ release event */
14819		lpfc_sli4_sp_handle_rel_wcqe(phba,
14820				(struct lpfc_wcqe_release *)&cqevt);
14821		break;
14822	case CQE_CODE_XRI_ABORTED:
14823		/* Process the WQ XRI abort event */
14824		phba->last_completion_time = jiffies;
14825		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14826				(struct sli4_wcqe_xri_aborted *)&cqevt);
14827		break;
14828	case CQE_CODE_RECEIVE:
14829	case CQE_CODE_RECEIVE_V1:
14830		/* Process the RQ event */
14831		phba->last_completion_time = jiffies;
14832		workposted = lpfc_sli4_sp_handle_rcqe(phba,
14833				(struct lpfc_rcqe *)&cqevt);
14834		break;
14835	default:
14836		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14837				"0388 Not a valid WCQE code: x%x\n",
14838				bf_get(lpfc_cqe_code, &cqevt));
14839		break;
14840	}
14841	return workposted;
14842}
14843
14844/**
14845 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
14846 * @phba: Pointer to HBA context object.
14847 * @eqe: Pointer to fast-path event queue entry.
14848 * @speq: Pointer to slow-path event queue.
14849 *
14850 * This routine process a event queue entry from the slow-path event queue.
14851 * It will check the MajorCode and MinorCode to determine this is for a
14852 * completion event on a completion queue, if not, an error shall be logged
14853 * and just return. Otherwise, it will get to the corresponding completion
14854 * queue and process all the entries on that completion queue, rearm the
14855 * completion queue, and then return.
14856 *
14857 **/
14858static void
14859lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14860	struct lpfc_queue *speq)
14861{
14862	struct lpfc_queue *cq = NULL, *childq;
14863	uint16_t cqid;
14864	int ret = 0;
14865
14866	/* Get the reference to the corresponding CQ */
14867	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14868
14869	list_for_each_entry(childq, &speq->child_list, list) {
14870		if (childq->queue_id == cqid) {
14871			cq = childq;
14872			break;
14873		}
14874	}
14875	if (unlikely(!cq)) {
14876		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
14877			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14878					"0365 Slow-path CQ identifier "
14879					"(%d) does not exist\n", cqid);
14880		return;
14881	}
14882
14883	/* Save EQ associated with this CQ */
14884	cq->assoc_qp = speq;
14885
14886	if (is_kdump_kernel())
14887		ret = queue_work(phba->wq, &cq->spwork);
14888	else
14889		ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
14890
14891	if (!ret)
14892		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
14893				"0390 Cannot schedule queue work "
14894				"for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14895				cqid, cq->queue_id, raw_smp_processor_id());
14896}
14897
14898/**
14899 * __lpfc_sli4_process_cq - Process elements of a CQ
14900 * @phba: Pointer to HBA context object.
14901 * @cq: Pointer to CQ to be processed
14902 * @handler: Routine to process each cqe
14903 * @delay: Pointer to usdelay to set in case of rescheduling of the handler
14904 *
14905 * This routine processes completion queue entries in a CQ. While a valid
14906 * queue element is found, the handler is called. During processing checks
14907 * are made for periodic doorbell writes to let the hardware know of
14908 * element consumption.
14909 *
14910 * If the max limit on cqes to process is hit, or there are no more valid
14911 * entries, the loop stops. If we processed a sufficient number of elements,
14912 * meaning there is sufficient load, rather than rearming and generating
14913 * another interrupt, a cq rescheduling delay will be set. A delay of 0
14914 * indicates no rescheduling.
14915 *
14916 * Returns True if work scheduled, False otherwise.
14917 **/
14918static bool
14919__lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
14920	bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
14921			struct lpfc_cqe *), unsigned long *delay)
14922{
14923	struct lpfc_cqe *cqe;
14924	bool workposted = false;
14925	int count = 0, consumed = 0;
14926	bool arm = true;
14927
14928	/* default - no reschedule */
14929	*delay = 0;
14930
14931	if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
14932		goto rearm_and_exit;
14933
14934	/* Process all the entries to the CQ */
14935	cq->q_flag = 0;
14936	cqe = lpfc_sli4_cq_get(cq);
14937	while (cqe) {
14938		workposted |= handler(phba, cq, cqe);
14939		__lpfc_sli4_consume_cqe(phba, cq, cqe);
14940
14941		consumed++;
14942		if (!(++count % cq->max_proc_limit))
14943			break;
14944
14945		if (!(count % cq->notify_interval)) {
14946			phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14947						LPFC_QUEUE_NOARM);
14948			consumed = 0;
14949			cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
14950		}
14951
14952		if (count == LPFC_NVMET_CQ_NOTIFY)
14953			cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
14954
14955		cqe = lpfc_sli4_cq_get(cq);
14956	}
14957	if (count >= phba->cfg_cq_poll_threshold) {
14958		*delay = 1;
14959		arm = false;
14960	}
14961
14962	/* Track the max number of CQEs processed in 1 EQ */
14963	if (count > cq->CQ_max_cqe)
14964		cq->CQ_max_cqe = count;
14965
14966	cq->assoc_qp->EQ_cqe_cnt += count;
14967
14968	/* Catch the no cq entry condition */
14969	if (unlikely(count == 0))
14970		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14971				"0369 No entry from completion queue "
14972				"qid=%d\n", cq->queue_id);
14973
14974	xchg(&cq->queue_claimed, 0);
14975
14976rearm_and_exit:
14977	phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14978			arm ?  LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
14979
14980	return workposted;
14981}
14982
14983/**
14984 * __lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
14985 * @cq: pointer to CQ to process
14986 *
14987 * This routine calls the cq processing routine with a handler specific
14988 * to the type of queue bound to it.
14989 *
14990 * The CQ routine returns two values: the first is the calling status,
14991 * which indicates whether work was queued to the  background discovery
14992 * thread. If true, the routine should wakeup the discovery thread;
14993 * the second is the delay parameter. If non-zero, rather than rearming
14994 * the CQ and yet another interrupt, the CQ handler should be queued so
14995 * that it is processed in a subsequent polling action. The value of
14996 * the delay indicates when to reschedule it.
14997 **/
14998static void
14999__lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
15000{
15001	struct lpfc_hba *phba = cq->phba;
15002	unsigned long delay;
15003	bool workposted = false;
15004	int ret = 0;
15005
15006	/* Process and rearm the CQ */
15007	switch (cq->type) {
15008	case LPFC_MCQ:
15009		workposted |= __lpfc_sli4_process_cq(phba, cq,
15010						lpfc_sli4_sp_handle_mcqe,
15011						&delay);
15012		break;
15013	case LPFC_WCQ:
15014		if (cq->subtype == LPFC_IO)
15015			workposted |= __lpfc_sli4_process_cq(phba, cq,
15016						lpfc_sli4_fp_handle_cqe,
15017						&delay);
15018		else
15019			workposted |= __lpfc_sli4_process_cq(phba, cq,
15020						lpfc_sli4_sp_handle_cqe,
15021						&delay);
15022		break;
15023	default:
15024		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15025				"0370 Invalid completion queue type (%d)\n",
15026				cq->type);
15027		return;
15028	}
15029
15030	if (delay) {
15031		if (is_kdump_kernel())
15032			ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
15033						delay);
15034		else
15035			ret = queue_delayed_work_on(cq->chann, phba->wq,
15036						&cq->sched_spwork, delay);
15037		if (!ret)
15038			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15039				"0394 Cannot schedule queue work "
15040				"for cqid=%d on CPU %d\n",
15041				cq->queue_id, cq->chann);
15042	}
15043
15044	/* wake up worker thread if there are works to be done */
15045	if (workposted)
15046		lpfc_worker_wake_up(phba);
15047}
15048
15049/**
15050 * lpfc_sli4_sp_process_cq - slow-path work handler when started by
15051 *   interrupt
15052 * @work: pointer to work element
15053 *
15054 * translates from the work handler and calls the slow-path handler.
15055 **/
15056static void
15057lpfc_sli4_sp_process_cq(struct work_struct *work)
15058{
15059	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
15060
15061	__lpfc_sli4_sp_process_cq(cq);
15062}
15063
15064/**
15065 * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
15066 * @work: pointer to work element
15067 *
15068 * translates from the work handler and calls the slow-path handler.
15069 **/
15070static void
15071lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
15072{
15073	struct lpfc_queue *cq = container_of(to_delayed_work(work),
15074					struct lpfc_queue, sched_spwork);
15075
15076	__lpfc_sli4_sp_process_cq(cq);
15077}
15078
15079/**
15080 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
15081 * @phba: Pointer to HBA context object.
15082 * @cq: Pointer to associated CQ
15083 * @wcqe: Pointer to work-queue completion queue entry.
15084 *
15085 * This routine process a fast-path work queue completion entry from fast-path
15086 * event queue for FCP command response completion.
15087 **/
15088static void
15089lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15090			     struct lpfc_wcqe_complete *wcqe)
15091{
15092	struct lpfc_sli_ring *pring = cq->pring;
15093	struct lpfc_iocbq *cmdiocbq;
15094	unsigned long iflags;
15095
15096	/* Check for response status */
15097	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
15098		/* If resource errors reported from HBA, reduce queue
15099		 * depth of the SCSI device.
15100		 */
15101		if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
15102		     IOSTAT_LOCAL_REJECT)) &&
15103		    ((wcqe->parameter & IOERR_PARAM_MASK) ==
15104		     IOERR_NO_RESOURCES))
15105			phba->lpfc_rampdown_queue_depth(phba);
15106
15107		/* Log the cmpl status */
15108		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15109				"0373 FCP CQE cmpl: status=x%x: "
15110				"CQE: %08x %08x %08x %08x\n",
15111				bf_get(lpfc_wcqe_c_status, wcqe),
15112				wcqe->word0, wcqe->total_data_placed,
15113				wcqe->parameter, wcqe->word3);
15114	}
15115
15116	/* Look up the FCP command IOCB and create pseudo response IOCB */
15117	spin_lock_irqsave(&pring->ring_lock, iflags);
15118	pring->stats.iocb_event++;
15119	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
15120				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15121	spin_unlock_irqrestore(&pring->ring_lock, iflags);
15122	if (unlikely(!cmdiocbq)) {
15123		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15124				"0374 FCP complete with no corresponding "
15125				"cmdiocb: iotag (%d)\n",
15126				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15127		return;
15128	}
15129#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
15130	cmdiocbq->isr_timestamp = cq->isr_timestamp;
15131#endif
15132	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
15133		spin_lock_irqsave(&phba->hbalock, iflags);
15134		cmdiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;
15135		spin_unlock_irqrestore(&phba->hbalock, iflags);
15136	}
15137
15138	if (cmdiocbq->cmd_cmpl) {
15139		/* For FCP the flag is cleared in cmd_cmpl */
15140		if (!(cmdiocbq->cmd_flag & LPFC_IO_FCP) &&
15141		    cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED) {
15142			spin_lock_irqsave(&phba->hbalock, iflags);
15143			cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
15144			spin_unlock_irqrestore(&phba->hbalock, iflags);
15145		}
15146
15147		/* Pass the cmd_iocb and the wcqe to the upper layer */
15148		memcpy(&cmdiocbq->wcqe_cmpl, wcqe,
15149		       sizeof(struct lpfc_wcqe_complete));
15150		cmdiocbq->cmd_cmpl(phba, cmdiocbq, cmdiocbq);
15151	} else {
15152		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15153				"0375 FCP cmdiocb not callback function "
15154				"iotag: (%d)\n",
15155				bf_get(lpfc_wcqe_c_request_tag, wcqe));
15156	}
15157}
15158
15159/**
15160 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
15161 * @phba: Pointer to HBA context object.
15162 * @cq: Pointer to completion queue.
15163 * @wcqe: Pointer to work-queue completion queue entry.
15164 *
15165 * This routine handles an fast-path WQ entry consumed event by invoking the
15166 * proper WQ release routine to the slow-path WQ.
15167 **/
15168static void
15169lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15170			     struct lpfc_wcqe_release *wcqe)
15171{
15172	struct lpfc_queue *childwq;
15173	bool wqid_matched = false;
15174	uint16_t hba_wqid;
15175
15176	/* Check for fast-path FCP work queue release */
15177	hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
15178	list_for_each_entry(childwq, &cq->child_list, list) {
15179		if (childwq->queue_id == hba_wqid) {
15180			lpfc_sli4_wq_release(childwq,
15181					bf_get(lpfc_wcqe_r_wqe_index, wcqe));
15182			if (childwq->q_flag & HBA_NVMET_WQFULL)
15183				lpfc_nvmet_wqfull_process(phba, childwq);
15184			wqid_matched = true;
15185			break;
15186		}
15187	}
15188	/* Report warning log message if no match found */
15189	if (wqid_matched != true)
15190		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15191				"2580 Fast-path wqe consume event carries "
15192				"miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
15193}
15194
15195/**
15196 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
15197 * @phba: Pointer to HBA context object.
15198 * @cq: Pointer to completion queue.
15199 * @rcqe: Pointer to receive-queue completion queue entry.
15200 *
15201 * This routine process a receive-queue completion queue entry.
15202 *
15203 * Return: true if work posted to worker thread, otherwise false.
15204 **/
15205static bool
15206lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15207			    struct lpfc_rcqe *rcqe)
15208{
15209	bool workposted = false;
15210	struct lpfc_queue *hrq;
15211	struct lpfc_queue *drq;
15212	struct rqb_dmabuf *dma_buf;
15213	struct fc_frame_header *fc_hdr;
15214	struct lpfc_nvmet_tgtport *tgtp;
15215	uint32_t status, rq_id;
15216	unsigned long iflags;
15217	uint32_t fctl, idx;
15218
15219	if ((phba->nvmet_support == 0) ||
15220	    (phba->sli4_hba.nvmet_cqset == NULL))
15221		return workposted;
15222
15223	idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
15224	hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
15225	drq = phba->sli4_hba.nvmet_mrq_data[idx];
15226
15227	/* sanity check on queue memory */
15228	if (unlikely(!hrq) || unlikely(!drq))
15229		return workposted;
15230
15231	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
15232		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
15233	else
15234		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
15235
15236	if ((phba->nvmet_support == 0) ||
15237	    (rq_id != hrq->queue_id))
15238		return workposted;
15239
15240	status = bf_get(lpfc_rcqe_status, rcqe);
15241	switch (status) {
15242	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
15243		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15244				"6126 Receive Frame Truncated!!\n");
15245		fallthrough;
15246	case FC_STATUS_RQ_SUCCESS:
15247		spin_lock_irqsave(&phba->hbalock, iflags);
15248		lpfc_sli4_rq_release(hrq, drq);
15249		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15250		if (!dma_buf) {
15251			hrq->RQ_no_buf_found++;
15252			spin_unlock_irqrestore(&phba->hbalock, iflags);
15253			goto out;
15254		}
15255		spin_unlock_irqrestore(&phba->hbalock, iflags);
15256		hrq->RQ_rcv_buf++;
15257		hrq->RQ_buf_posted--;
15258		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
15259
15260		/* Just some basic sanity checks on FCP Command frame */
15261		fctl = (fc_hdr->fh_f_ctl[0] << 16 |
15262			fc_hdr->fh_f_ctl[1] << 8 |
15263			fc_hdr->fh_f_ctl[2]);
15264		if (((fctl &
15265		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
15266		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
15267		    (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
15268			goto drop;
15269
15270		if (fc_hdr->fh_type == FC_TYPE_FCP) {
15271			dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
15272			lpfc_nvmet_unsol_fcp_event(
15273				phba, idx, dma_buf, cq->isr_timestamp,
15274				cq->q_flag & HBA_NVMET_CQ_NOTIFY);
15275			return false;
15276		}
15277drop:
15278		lpfc_rq_buf_free(phba, &dma_buf->hbuf);
15279		break;
15280	case FC_STATUS_INSUFF_BUF_FRM_DISC:
15281		if (phba->nvmet_support) {
15282			tgtp = phba->targetport->private;
15283			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15284					"6401 RQE Error x%x, posted %d err_cnt "
15285					"%d: %x %x %x\n",
15286					status, hrq->RQ_buf_posted,
15287					hrq->RQ_no_posted_buf,
15288					atomic_read(&tgtp->rcv_fcp_cmd_in),
15289					atomic_read(&tgtp->rcv_fcp_cmd_out),
15290					atomic_read(&tgtp->xmt_fcp_release));
15291		}
15292		fallthrough;
15293
15294	case FC_STATUS_INSUFF_BUF_NEED_BUF:
15295		hrq->RQ_no_posted_buf++;
15296		/* Post more buffers if possible */
15297		break;
15298	case FC_STATUS_RQ_DMA_FAILURE:
15299		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15300				"2575 RQE DMA Error x%x, x%08x x%08x x%08x "
15301				"x%08x\n",
15302				status, rcqe->word0, rcqe->word1,
15303				rcqe->word2, rcqe->word3);
15304
15305		/* If IV set, no further recovery */
15306		if (bf_get(lpfc_rcqe_iv, rcqe))
15307			break;
15308
15309		/* recycle consumed resource */
15310		spin_lock_irqsave(&phba->hbalock, iflags);
15311		lpfc_sli4_rq_release(hrq, drq);
15312		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15313		if (!dma_buf) {
15314			hrq->RQ_no_buf_found++;
15315			spin_unlock_irqrestore(&phba->hbalock, iflags);
15316			break;
15317		}
15318		hrq->RQ_rcv_buf++;
15319		hrq->RQ_buf_posted--;
15320		spin_unlock_irqrestore(&phba->hbalock, iflags);
15321		lpfc_rq_buf_free(phba, &dma_buf->hbuf);
15322		break;
15323	default:
15324		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15325				"2576 Unexpected RQE Status x%x, w0-3 x%08x "
15326				"x%08x x%08x x%08x\n",
15327				status, rcqe->word0, rcqe->word1,
15328				rcqe->word2, rcqe->word3);
15329		break;
15330	}
15331out:
15332	return workposted;
15333}
15334
15335/**
15336 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
15337 * @phba: adapter with cq
15338 * @cq: Pointer to the completion queue.
15339 * @cqe: Pointer to fast-path completion queue entry.
15340 *
15341 * This routine process a fast-path work queue completion entry from fast-path
15342 * event queue for FCP command response completion.
15343 *
15344 * Return: true if work posted to worker thread, otherwise false.
15345 **/
15346static bool
15347lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15348			 struct lpfc_cqe *cqe)
15349{
15350	struct lpfc_wcqe_release wcqe;
15351	bool workposted = false;
15352
15353	/* Copy the work queue CQE and convert endian order if needed */
15354	lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
15355
15356	/* Check and process for different type of WCQE and dispatch */
15357	switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
15358	case CQE_CODE_COMPL_WQE:
15359	case CQE_CODE_NVME_ERSP:
15360		cq->CQ_wq++;
15361		/* Process the WQ complete event */
15362		phba->last_completion_time = jiffies;
15363		if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
15364			lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
15365				(struct lpfc_wcqe_complete *)&wcqe);
15366		break;
15367	case CQE_CODE_RELEASE_WQE:
15368		cq->CQ_release_wqe++;
15369		/* Process the WQ release event */
15370		lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
15371				(struct lpfc_wcqe_release *)&wcqe);
15372		break;
15373	case CQE_CODE_XRI_ABORTED:
15374		cq->CQ_xri_aborted++;
15375		/* Process the WQ XRI abort event */
15376		phba->last_completion_time = jiffies;
15377		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
15378				(struct sli4_wcqe_xri_aborted *)&wcqe);
15379		break;
15380	case CQE_CODE_RECEIVE_V1:
15381	case CQE_CODE_RECEIVE:
15382		phba->last_completion_time = jiffies;
15383		if (cq->subtype == LPFC_NVMET) {
15384			workposted = lpfc_sli4_nvmet_handle_rcqe(
15385				phba, cq, (struct lpfc_rcqe *)&wcqe);
15386		}
15387		break;
15388	default:
15389		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15390				"0144 Not a valid CQE code: x%x\n",
15391				bf_get(lpfc_wcqe_c_code, &wcqe));
15392		break;
15393	}
15394	return workposted;
15395}
15396
15397/**
15398 * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
15399 * @cq: Pointer to CQ to be processed
15400 *
15401 * This routine calls the cq processing routine with the handler for
15402 * fast path CQEs.
15403 *
15404 * The CQ routine returns two values: the first is the calling status,
15405 * which indicates whether work was queued to the  background discovery
15406 * thread. If true, the routine should wakeup the discovery thread;
15407 * the second is the delay parameter. If non-zero, rather than rearming
15408 * the CQ and yet another interrupt, the CQ handler should be queued so
15409 * that it is processed in a subsequent polling action. The value of
15410 * the delay indicates when to reschedule it.
15411 **/
15412static void
15413__lpfc_sli4_hba_process_cq(struct lpfc_queue *cq)
15414{
15415	struct lpfc_hba *phba = cq->phba;
15416	unsigned long delay;
15417	bool workposted = false;
15418	int ret;
15419
15420	/* process and rearm the CQ */
15421	workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
15422					     &delay);
15423
15424	if (delay) {
15425		if (is_kdump_kernel())
15426			ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
15427						delay);
15428		else
15429			ret = queue_delayed_work_on(cq->chann, phba->wq,
15430						&cq->sched_irqwork, delay);
15431		if (!ret)
15432			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15433					"0367 Cannot schedule queue work "
15434					"for cqid=%d on CPU %d\n",
15435					cq->queue_id, cq->chann);
15436	}
15437
15438	/* wake up worker thread if there are works to be done */
15439	if (workposted)
15440		lpfc_worker_wake_up(phba);
15441}
15442
15443/**
15444 * lpfc_sli4_hba_process_cq - fast-path work handler when started by
15445 *   interrupt
15446 * @work: pointer to work element
15447 *
15448 * translates from the work handler and calls the fast-path handler.
15449 **/
15450static void
15451lpfc_sli4_hba_process_cq(struct work_struct *work)
15452{
15453	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
15454
15455	__lpfc_sli4_hba_process_cq(cq);
15456}
15457
15458/**
15459 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
15460 * @phba: Pointer to HBA context object.
15461 * @eq: Pointer to the queue structure.
15462 * @eqe: Pointer to fast-path event queue entry.
15463 * @poll_mode: poll_mode to execute processing the cq.
15464 *
15465 * This routine process a event queue entry from the fast-path event queue.
15466 * It will check the MajorCode and MinorCode to determine this is for a
15467 * completion event on a completion queue, if not, an error shall be logged
15468 * and just return. Otherwise, it will get to the corresponding completion
15469 * queue and process all the entries on the completion queue, rearm the
15470 * completion queue, and then return.
15471 **/
15472static void
15473lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
15474			 struct lpfc_eqe *eqe, enum lpfc_poll_mode poll_mode)
15475{
15476	struct lpfc_queue *cq = NULL;
15477	uint32_t qidx = eq->hdwq;
15478	uint16_t cqid, id;
15479	int ret;
15480
15481	if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
15482		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15483				"0366 Not a valid completion "
15484				"event: majorcode=x%x, minorcode=x%x\n",
15485				bf_get_le32(lpfc_eqe_major_code, eqe),
15486				bf_get_le32(lpfc_eqe_minor_code, eqe));
15487		return;
15488	}
15489
15490	/* Get the reference to the corresponding CQ */
15491	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
15492
15493	/* Use the fast lookup method first */
15494	if (cqid <= phba->sli4_hba.cq_max) {
15495		cq = phba->sli4_hba.cq_lookup[cqid];
15496		if (cq)
15497			goto  work_cq;
15498	}
15499
15500	/* Next check for NVMET completion */
15501	if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
15502		id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
15503		if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
15504			/* Process NVMET unsol rcv */
15505			cq = phba->sli4_hba.nvmet_cqset[cqid - id];
15506			goto  process_cq;
15507		}
15508	}
15509
15510	if (phba->sli4_hba.nvmels_cq &&
15511	    (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
15512		/* Process NVME unsol rcv */
15513		cq = phba->sli4_hba.nvmels_cq;
15514	}
15515
15516	/* Otherwise this is a Slow path event */
15517	if (cq == NULL) {
15518		lpfc_sli4_sp_handle_eqe(phba, eqe,
15519					phba->sli4_hba.hdwq[qidx].hba_eq);
15520		return;
15521	}
15522
15523process_cq:
15524	if (unlikely(cqid != cq->queue_id)) {
15525		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15526				"0368 Miss-matched fast-path completion "
15527				"queue identifier: eqcqid=%d, fcpcqid=%d\n",
15528				cqid, cq->queue_id);
15529		return;
15530	}
15531
15532work_cq:
15533#if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
15534	if (phba->ktime_on)
15535		cq->isr_timestamp = ktime_get_ns();
15536	else
15537		cq->isr_timestamp = 0;
15538#endif
15539
15540	switch (poll_mode) {
15541	case LPFC_THREADED_IRQ:
15542		__lpfc_sli4_hba_process_cq(cq);
15543		break;
15544	case LPFC_QUEUE_WORK:
15545	default:
15546		if (is_kdump_kernel())
15547			ret = queue_work(phba->wq, &cq->irqwork);
15548		else
15549			ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
15550		if (!ret)
15551			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15552					"0383 Cannot schedule queue work "
15553					"for CQ eqcqid=%d, cqid=%d on CPU %d\n",
15554					cqid, cq->queue_id,
15555					raw_smp_processor_id());
15556		break;
15557	}
15558}
15559
15560/**
15561 * lpfc_sli4_dly_hba_process_cq - fast-path work handler when started by timer
15562 * @work: pointer to work element
15563 *
15564 * translates from the work handler and calls the fast-path handler.
15565 **/
15566static void
15567lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
15568{
15569	struct lpfc_queue *cq = container_of(to_delayed_work(work),
15570					struct lpfc_queue, sched_irqwork);
15571
15572	__lpfc_sli4_hba_process_cq(cq);
15573}
15574
15575/**
15576 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
15577 * @irq: Interrupt number.
15578 * @dev_id: The device context pointer.
15579 *
15580 * This function is directly called from the PCI layer as an interrupt
15581 * service routine when device with SLI-4 interface spec is enabled with
15582 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
15583 * ring event in the HBA. However, when the device is enabled with either
15584 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
15585 * device-level interrupt handler. When the PCI slot is in error recovery
15586 * or the HBA is undergoing initialization, the interrupt handler will not
15587 * process the interrupt. The SCSI FCP fast-path ring event are handled in
15588 * the intrrupt context. This function is called without any lock held.
15589 * It gets the hbalock to access and update SLI data structures. Note that,
15590 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
15591 * equal to that of FCP CQ index.
15592 *
15593 * The link attention and ELS ring attention events are handled
15594 * by the worker thread. The interrupt handler signals the worker thread
15595 * and returns for these events. This function is called without any lock
15596 * held. It gets the hbalock to access and update SLI data structures.
15597 *
15598 * This function returns IRQ_HANDLED when interrupt is handled, IRQ_WAKE_THREAD
15599 * when interrupt is scheduled to be handled from a threaded irq context, or
15600 * else returns IRQ_NONE.
15601 **/
15602irqreturn_t
15603lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
15604{
15605	struct lpfc_hba *phba;
15606	struct lpfc_hba_eq_hdl *hba_eq_hdl;
15607	struct lpfc_queue *fpeq;
15608	unsigned long iflag;
15609	int hba_eqidx;
15610	int ecount = 0;
15611	struct lpfc_eq_intr_info *eqi;
15612
15613	/* Get the driver's phba structure from the dev_id */
15614	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
15615	phba = hba_eq_hdl->phba;
15616	hba_eqidx = hba_eq_hdl->idx;
15617
15618	if (unlikely(!phba))
15619		return IRQ_NONE;
15620	if (unlikely(!phba->sli4_hba.hdwq))
15621		return IRQ_NONE;
15622
15623	/* Get to the EQ struct associated with this vector */
15624	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
15625	if (unlikely(!fpeq))
15626		return IRQ_NONE;
15627
15628	/* Check device state for handling interrupt */
15629	if (unlikely(lpfc_intr_state_check(phba))) {
15630		/* Check again for link_state with lock held */
15631		spin_lock_irqsave(&phba->hbalock, iflag);
15632		if (phba->link_state < LPFC_LINK_DOWN)
15633			/* Flush, clear interrupt, and rearm the EQ */
15634			lpfc_sli4_eqcq_flush(phba, fpeq);
15635		spin_unlock_irqrestore(&phba->hbalock, iflag);
15636		return IRQ_NONE;
15637	}
15638
15639	switch (fpeq->poll_mode) {
15640	case LPFC_THREADED_IRQ:
15641		/* CGN mgmt is mutually exclusive from irq processing */
15642		if (phba->cmf_active_mode == LPFC_CFG_OFF)
15643			return IRQ_WAKE_THREAD;
15644		fallthrough;
15645	case LPFC_QUEUE_WORK:
15646	default:
15647		eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
15648		eqi->icnt++;
15649
15650		fpeq->last_cpu = raw_smp_processor_id();
15651
15652		if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
15653		    fpeq->q_flag & HBA_EQ_DELAY_CHK &&
15654		    phba->cfg_auto_imax &&
15655		    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
15656		    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
15657			lpfc_sli4_mod_hba_eq_delay(phba, fpeq,
15658						   LPFC_MAX_AUTO_EQ_DELAY);
15659
15660		/* process and rearm the EQ */
15661		ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
15662					      LPFC_QUEUE_WORK);
15663
15664		if (unlikely(ecount == 0)) {
15665			fpeq->EQ_no_entry++;
15666			if (phba->intr_type == MSIX)
15667				/* MSI-X treated interrupt served as no EQ share INT */
15668				lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15669						"0358 MSI-X interrupt with no EQE\n");
15670			else
15671				/* Non MSI-X treated on interrupt as EQ share INT */
15672				return IRQ_NONE;
15673		}
15674	}
15675
15676	return IRQ_HANDLED;
15677} /* lpfc_sli4_hba_intr_handler */
15678
15679/**
15680 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
15681 * @irq: Interrupt number.
15682 * @dev_id: The device context pointer.
15683 *
15684 * This function is the device-level interrupt handler to device with SLI-4
15685 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
15686 * interrupt mode is enabled and there is an event in the HBA which requires
15687 * driver attention. This function invokes the slow-path interrupt attention
15688 * handling function and fast-path interrupt attention handling function in
15689 * turn to process the relevant HBA attention events. This function is called
15690 * without any lock held. It gets the hbalock to access and update SLI data
15691 * structures.
15692 *
15693 * This function returns IRQ_HANDLED when interrupt is handled, else it
15694 * returns IRQ_NONE.
15695 **/
15696irqreturn_t
15697lpfc_sli4_intr_handler(int irq, void *dev_id)
15698{
15699	struct lpfc_hba  *phba;
15700	irqreturn_t hba_irq_rc;
15701	bool hba_handled = false;
15702	int qidx;
15703
15704	/* Get the driver's phba structure from the dev_id */
15705	phba = (struct lpfc_hba *)dev_id;
15706
15707	if (unlikely(!phba))
15708		return IRQ_NONE;
15709
15710	/*
15711	 * Invoke fast-path host attention interrupt handling as appropriate.
15712	 */
15713	for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
15714		hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
15715					&phba->sli4_hba.hba_eq_hdl[qidx]);
15716		if (hba_irq_rc == IRQ_HANDLED)
15717			hba_handled |= true;
15718	}
15719
15720	return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
15721} /* lpfc_sli4_intr_handler */
15722
15723void lpfc_sli4_poll_hbtimer(struct timer_list *t)
15724{
15725	struct lpfc_hba *phba = timer_container_of(phba, t, cpuhp_poll_timer);
15726	struct lpfc_queue *eq;
15727
15728	rcu_read_lock();
15729
15730	list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
15731		lpfc_sli4_poll_eq(eq);
15732	if (!list_empty(&phba->poll_list))
15733		mod_timer(&phba->cpuhp_poll_timer,
15734			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15735
15736	rcu_read_unlock();
15737}
15738
15739static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
15740{
15741	struct lpfc_hba *phba = eq->phba;
15742
15743	/* kickstart slowpath processing if needed */
15744	if (list_empty(&phba->poll_list))
15745		mod_timer(&phba->cpuhp_poll_timer,
15746			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15747
15748	list_add_rcu(&eq->_poll_list, &phba->poll_list);
15749	synchronize_rcu();
15750}
15751
15752static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
15753{
15754	struct lpfc_hba *phba = eq->phba;
15755
15756	/* Disable slowpath processing for this eq.  Kick start the eq
15757	 * by RE-ARMING the eq's ASAP
15758	 */
15759	list_del_rcu(&eq->_poll_list);
15760	synchronize_rcu();
15761
15762	if (list_empty(&phba->poll_list))
15763		timer_delete_sync(&phba->cpuhp_poll_timer);
15764}
15765
15766void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
15767{
15768	struct lpfc_queue *eq, *next;
15769
15770	list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
15771		list_del(&eq->_poll_list);
15772
15773	INIT_LIST_HEAD(&phba->poll_list);
15774	synchronize_rcu();
15775}
15776
15777static inline void
15778__lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
15779{
15780	if (mode == eq->mode)
15781		return;
15782	/*
15783	 * currently this function is only called during a hotplug
15784	 * event and the cpu on which this function is executing
15785	 * is going offline.  By now the hotplug has instructed
15786	 * the scheduler to remove this cpu from cpu active mask.
15787	 * So we don't need to work about being put aside by the
15788	 * scheduler for a high priority process.  Yes, the inte-
15789	 * rrupts could come but they are known to retire ASAP.
15790	 */
15791
15792	/* Disable polling in the fastpath */
15793	WRITE_ONCE(eq->mode, mode);
15794	/* flush out the store buffer */
15795	smp_wmb();
15796
15797	/*
15798	 * Add this eq to the polling list and start polling. For
15799	 * a grace period both interrupt handler and poller will
15800	 * try to process the eq _but_ that's fine.  We have a
15801	 * synchronization mechanism in place (queue_claimed) to
15802	 * deal with it.  This is just a draining phase for int-
15803	 * errupt handler (not eq's) as we have guranteed through
15804	 * barrier that all the CPUs have seen the new CQ_POLLED
15805	 * state. which will effectively disable the REARMING of
15806	 * the EQ.  The whole idea is eq's die off eventually as
15807	 * we are not rearming EQ's anymore.
15808	 */
15809	mode ? lpfc_sli4_add_to_poll_list(eq) :
15810	       lpfc_sli4_remove_from_poll_list(eq);
15811}
15812
15813void lpfc_sli4_start_polling(struct lpfc_queue *eq)
15814{
15815	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
15816}
15817
15818void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
15819{
15820	struct lpfc_hba *phba = eq->phba;
15821
15822	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
15823
15824	/* Kick start for the pending io's in h/w.
15825	 * Once we switch back to interrupt processing on a eq
15826	 * the io path completion will only arm eq's when it
15827	 * receives a completion.  But since eq's are in disa-
15828	 * rmed state it doesn't receive a completion.  This
15829	 * creates a deadlock scenaro.
15830	 */
15831	phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
15832}
15833
15834/**
15835 * lpfc_sli4_queue_free - free a queue structure and associated memory
15836 * @queue: The queue structure to free.
15837 *
15838 * This function frees a queue structure and the DMAable memory used for
15839 * the host resident queue. This function must be called after destroying the
15840 * queue on the HBA.
15841 **/
15842void
15843lpfc_sli4_queue_free(struct lpfc_queue *queue)
15844{
15845	struct lpfc_dmabuf *dmabuf;
15846
15847	if (!queue)
15848		return;
15849
15850	if (!list_empty(&queue->wq_list))
15851		list_del(&queue->wq_list);
15852
15853	while (!list_empty(&queue->page_list)) {
15854		list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
15855				 list);
15856		dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
15857				  dmabuf->virt, dmabuf->phys);
15858		kfree(dmabuf);
15859	}
15860	if (queue->rqbp) {
15861		lpfc_free_rq_buffer(queue->phba, queue);
15862		kfree(queue->rqbp);
15863	}
15864
15865	if (!list_empty(&queue->cpu_list))
15866		list_del(&queue->cpu_list);
15867
15868	kfree(queue);
15869	return;
15870}
15871
15872/**
15873 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
15874 * @phba: The HBA that this queue is being created on.
15875 * @page_size: The size of a queue page
15876 * @entry_size: The size of each queue entry for this queue.
15877 * @entry_count: The number of entries that this queue will handle.
15878 * @cpu: The cpu that will primarily utilize this queue.
15879 *
15880 * This function allocates a queue structure and the DMAable memory used for
15881 * the host resident queue. This function must be called before creating the
15882 * queue on the HBA.
15883 **/
15884struct lpfc_queue *
15885lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
15886		      uint32_t entry_size, uint32_t entry_count, int cpu)
15887{
15888	struct lpfc_queue *queue;
15889	struct lpfc_dmabuf *dmabuf;
15890	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15891	uint16_t x, pgcnt;
15892
15893	if (!phba->sli4_hba.pc_sli4_params.supported)
15894		hw_page_size = page_size;
15895
15896	pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
15897
15898	/* If needed, Adjust page count to match the max the adapter supports */
15899	if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
15900		pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
15901
15902	queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
15903			     GFP_KERNEL, cpu_to_node(cpu));
15904	if (!queue)
15905		return NULL;
15906
15907	INIT_LIST_HEAD(&queue->list);
15908	INIT_LIST_HEAD(&queue->_poll_list);
15909	INIT_LIST_HEAD(&queue->wq_list);
15910	INIT_LIST_HEAD(&queue->wqfull_list);
15911	INIT_LIST_HEAD(&queue->page_list);
15912	INIT_LIST_HEAD(&queue->child_list);
15913	INIT_LIST_HEAD(&queue->cpu_list);
15914
15915	/* Set queue parameters now.  If the system cannot provide memory
15916	 * resources, the free routine needs to know what was allocated.
15917	 */
15918	queue->page_count = pgcnt;
15919	queue->q_pgs = (void **)&queue[1];
15920	queue->entry_cnt_per_pg = hw_page_size / entry_size;
15921	queue->entry_size = entry_size;
15922	queue->entry_count = entry_count;
15923	queue->page_size = hw_page_size;
15924	queue->phba = phba;
15925
15926	for (x = 0; x < queue->page_count; x++) {
15927		dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
15928				      dev_to_node(&phba->pcidev->dev));
15929		if (!dmabuf)
15930			goto out_fail;
15931		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
15932						  hw_page_size, &dmabuf->phys,
15933						  GFP_KERNEL);
15934		if (!dmabuf->virt) {
15935			kfree(dmabuf);
15936			goto out_fail;
15937		}
15938		dmabuf->buffer_tag = x;
15939		list_add_tail(&dmabuf->list, &queue->page_list);
15940		/* use lpfc_sli4_qe to index a paritcular entry in this page */
15941		queue->q_pgs[x] = dmabuf->virt;
15942	}
15943	INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
15944	INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
15945	INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
15946	INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
15947
15948	/* notify_interval will be set during q creation */
15949
15950	return queue;
15951out_fail:
15952	lpfc_sli4_queue_free(queue);
15953	return NULL;
15954}
15955
15956/**
15957 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
15958 * @phba: HBA structure that indicates port to create a queue on.
15959 * @pci_barset: PCI BAR set flag.
15960 *
15961 * This function shall perform iomap of the specified PCI BAR address to host
15962 * memory address if not already done so and return it. The returned host
15963 * memory address can be NULL.
15964 */
15965static void __iomem *
15966lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
15967{
15968	if (!phba->pcidev)
15969		return NULL;
15970
15971	switch (pci_barset) {
15972	case WQ_PCI_BAR_0_AND_1:
15973		return phba->pci_bar0_memmap_p;
15974	case WQ_PCI_BAR_2_AND_3:
15975		return phba->pci_bar2_memmap_p;
15976	case WQ_PCI_BAR_4_AND_5:
15977		return phba->pci_bar4_memmap_p;
15978	default:
15979		break;
15980	}
15981	return NULL;
15982}
15983
15984static __maybe_unused void __iomem *
15985lpfc_dpp_wc_map(struct lpfc_hba *phba, uint8_t dpp_barset)
15986{
15987
15988	/* DPP region is supposed to cover 64-bit BAR2 */
15989	if (dpp_barset != WQ_PCI_BAR_4_AND_5) {
15990		lpfc_log_msg(phba, KERN_WARNING, LOG_INIT,
15991			     "3273 dpp_barset x%x != WQ_PCI_BAR_4_AND_5\n",
15992			     dpp_barset);
15993		return NULL;
15994	}
15995
15996	if (!phba->sli4_hba.dpp_regs_memmap_wc_p) {
15997		void __iomem *dpp_map;
15998
15999		dpp_map = ioremap_wc(phba->pci_bar2_map,
16000				     pci_resource_len(phba->pcidev,
16001						      PCI_64BIT_BAR4));
16002
16003		if (dpp_map)
16004			phba->sli4_hba.dpp_regs_memmap_wc_p = dpp_map;
16005	}
16006
16007	return phba->sli4_hba.dpp_regs_memmap_wc_p;
16008}
16009
16010/**
16011 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
16012 * @phba: HBA structure that EQs are on.
16013 * @startq: The starting EQ index to modify
16014 * @numq: The number of EQs (consecutive indexes) to modify
16015 * @usdelay: amount of delay
16016 *
16017 * This function revises the EQ delay on 1 or more EQs. The EQ delay
16018 * is set either by writing to a register (if supported by the SLI Port)
16019 * or by mailbox command. The mailbox command allows several EQs to be
16020 * updated at once.
16021 *
16022 * The @phba struct is used to send a mailbox command to HBA. The @startq
16023 * is used to get the starting EQ index to change. The @numq value is
16024 * used to specify how many consecutive EQ indexes, starting at EQ index,
16025 * are to be changed. This function is asynchronous and will wait for any
16026 * mailbox commands to finish before returning.
16027 *
16028 * On success this function will return a zero. If unable to allocate
16029 * enough memory this function will return -ENOMEM. If a mailbox command
16030 * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
16031 * have had their delay multipler changed.
16032 **/
16033void
16034lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
16035			 uint32_t numq, uint32_t usdelay)
16036{
16037	struct lpfc_mbx_modify_eq_delay *eq_delay;
16038	LPFC_MBOXQ_t *mbox;
16039	struct lpfc_queue *eq;
16040	int cnt = 0, rc, length;
16041	uint32_t shdr_status, shdr_add_status;
16042	uint32_t dmult;
16043	int qidx;
16044	union lpfc_sli4_cfg_shdr *shdr;
16045
16046	if (startq >= phba->cfg_irq_chann)
16047		return;
16048
16049	if (usdelay > 0xFFFF) {
16050		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
16051				"6429 usdelay %d too large. Scaled down to "
16052				"0xFFFF.\n", usdelay);
16053		usdelay = 0xFFFF;
16054	}
16055
16056	/* set values by EQ_DELAY register if supported */
16057	if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
16058		for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
16059			eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
16060			if (!eq)
16061				continue;
16062
16063			lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
16064
16065			if (++cnt >= numq)
16066				break;
16067		}
16068		return;
16069	}
16070
16071	/* Otherwise, set values by mailbox cmd */
16072
16073	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16074	if (!mbox) {
16075		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16076				"6428 Failed allocating mailbox cmd buffer."
16077				" EQ delay was not set.\n");
16078		return;
16079	}
16080	length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
16081		  sizeof(struct lpfc_sli4_cfg_mhdr));
16082	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16083			 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
16084			 length, LPFC_SLI4_MBX_EMBED);
16085	eq_delay = &mbox->u.mqe.un.eq_delay;
16086
16087	/* Calculate delay multiper from maximum interrupt per second */
16088	dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
16089	if (dmult)
16090		dmult--;
16091	if (dmult > LPFC_DMULT_MAX)
16092		dmult = LPFC_DMULT_MAX;
16093
16094	for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
16095		eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
16096		if (!eq)
16097			continue;
16098		eq->q_mode = usdelay;
16099		eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
16100		eq_delay->u.request.eq[cnt].phase = 0;
16101		eq_delay->u.request.eq[cnt].delay_multi = dmult;
16102
16103		if (++cnt >= numq)
16104			break;
16105	}
16106	eq_delay->u.request.num_eq = cnt;
16107
16108	mbox->vport = phba->pport;
16109	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16110	mbox->ctx_ndlp = NULL;
16111	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16112	shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
16113	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16114	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16115	if (shdr_status || shdr_add_status || rc) {
16116		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16117				"2512 MODIFY_EQ_DELAY mailbox failed with "
16118				"status x%x add_status x%x, mbx status x%x\n",
16119				shdr_status, shdr_add_status, rc);
16120	}
16121	mempool_free(mbox, phba->mbox_mem_pool);
16122	return;
16123}
16124
16125/**
16126 * lpfc_eq_create - Create an Event Queue on the HBA
16127 * @phba: HBA structure that indicates port to create a queue on.
16128 * @eq: The queue structure to use to create the event queue.
16129 * @imax: The maximum interrupt per second limit.
16130 *
16131 * This function creates an event queue, as detailed in @eq, on a port,
16132 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
16133 *
16134 * The @phba struct is used to send mailbox command to HBA. The @eq struct
16135 * is used to get the entry count and entry size that are necessary to
16136 * determine the number of pages to allocate and use for this queue. This
16137 * function will send the EQ_CREATE mailbox command to the HBA to setup the
16138 * event queue. This function is asynchronous and will wait for the mailbox
16139 * command to finish before continuing.
16140 *
16141 * On success this function will return a zero. If unable to allocate enough
16142 * memory this function will return -ENOMEM. If the queue create mailbox command
16143 * fails this function will return -ENXIO.
16144 **/
16145int
16146lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
16147{
16148	struct lpfc_mbx_eq_create *eq_create;
16149	LPFC_MBOXQ_t *mbox;
16150	int rc, length, status = 0;
16151	struct lpfc_dmabuf *dmabuf;
16152	uint32_t shdr_status, shdr_add_status;
16153	union lpfc_sli4_cfg_shdr *shdr;
16154	uint16_t dmult;
16155	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16156
16157	/* sanity check on queue memory */
16158	if (!eq)
16159		return -ENODEV;
16160	if (!phba->sli4_hba.pc_sli4_params.supported)
16161		hw_page_size = SLI4_PAGE_SIZE;
16162
16163	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16164	if (!mbox)
16165		return -ENOMEM;
16166	length = (sizeof(struct lpfc_mbx_eq_create) -
16167		  sizeof(struct lpfc_sli4_cfg_mhdr));
16168	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16169			 LPFC_MBOX_OPCODE_EQ_CREATE,
16170			 length, LPFC_SLI4_MBX_EMBED);
16171	eq_create = &mbox->u.mqe.un.eq_create;
16172	shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
16173	bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
16174	       eq->page_count);
16175	bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
16176	       LPFC_EQE_SIZE);
16177	bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
16178
16179	/* Use version 2 of CREATE_EQ if eqav is set */
16180	if (phba->sli4_hba.pc_sli4_params.eqav) {
16181		bf_set(lpfc_mbox_hdr_version, &shdr->request,
16182		       LPFC_Q_CREATE_VERSION_2);
16183		bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
16184		       phba->sli4_hba.pc_sli4_params.eqav);
16185	}
16186
16187	/* don't setup delay multiplier using EQ_CREATE */
16188	dmult = 0;
16189	bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
16190	       dmult);
16191	switch (eq->entry_count) {
16192	default:
16193		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16194				"0360 Unsupported EQ count. (%d)\n",
16195				eq->entry_count);
16196		if (eq->entry_count < 256) {
16197			status = -EINVAL;
16198			goto out;
16199		}
16200		fallthrough;	/* otherwise default to smallest count */
16201	case 256:
16202		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16203		       LPFC_EQ_CNT_256);
16204		break;
16205	case 512:
16206		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16207		       LPFC_EQ_CNT_512);
16208		break;
16209	case 1024:
16210		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16211		       LPFC_EQ_CNT_1024);
16212		break;
16213	case 2048:
16214		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16215		       LPFC_EQ_CNT_2048);
16216		break;
16217	case 4096:
16218		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16219		       LPFC_EQ_CNT_4096);
16220		break;
16221	}
16222	list_for_each_entry(dmabuf, &eq->page_list, list) {
16223		memset(dmabuf->virt, 0, hw_page_size);
16224		eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16225					putPaddrLow(dmabuf->phys);
16226		eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16227					putPaddrHigh(dmabuf->phys);
16228	}
16229	mbox->vport = phba->pport;
16230	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16231	mbox->ctx_buf = NULL;
16232	mbox->ctx_ndlp = NULL;
16233	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16234	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16235	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16236	if (shdr_status || shdr_add_status || rc) {
16237		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16238				"2500 EQ_CREATE mailbox failed with "
16239				"status x%x add_status x%x, mbx status x%x\n",
16240				shdr_status, shdr_add_status, rc);
16241		status = -ENXIO;
16242	}
16243	eq->type = LPFC_EQ;
16244	eq->subtype = LPFC_NONE;
16245	eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
16246	if (eq->queue_id == 0xFFFF)
16247		status = -ENXIO;
16248	eq->host_index = 0;
16249	eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
16250	eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
16251out:
16252	mempool_free(mbox, phba->mbox_mem_pool);
16253	return status;
16254}
16255
16256/**
16257 * lpfc_sli4_hba_intr_handler_th - SLI4 HBA threaded interrupt handler
16258 * @irq: Interrupt number.
16259 * @dev_id: The device context pointer.
16260 *
16261 * This routine is a mirror of lpfc_sli4_hba_intr_handler, but executed within
16262 * threaded irq context.
16263 *
16264 * Returns
16265 * IRQ_HANDLED - interrupt is handled
16266 * IRQ_NONE - otherwise
16267 **/
16268irqreturn_t lpfc_sli4_hba_intr_handler_th(int irq, void *dev_id)
16269{
16270	struct lpfc_hba *phba;
16271	struct lpfc_hba_eq_hdl *hba_eq_hdl;
16272	struct lpfc_queue *fpeq;
16273	int ecount = 0;
16274	int hba_eqidx;
16275	struct lpfc_eq_intr_info *eqi;
16276
16277	/* Get the driver's phba structure from the dev_id */
16278	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
16279	phba = hba_eq_hdl->phba;
16280	hba_eqidx = hba_eq_hdl->idx;
16281
16282	if (unlikely(!phba))
16283		return IRQ_NONE;
16284	if (unlikely(!phba->sli4_hba.hdwq))
16285		return IRQ_NONE;
16286
16287	/* Get to the EQ struct associated with this vector */
16288	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
16289	if (unlikely(!fpeq))
16290		return IRQ_NONE;
16291
16292	eqi = per_cpu_ptr(phba->sli4_hba.eq_info, raw_smp_processor_id());
16293	eqi->icnt++;
16294
16295	fpeq->last_cpu = raw_smp_processor_id();
16296
16297	if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
16298	    fpeq->q_flag & HBA_EQ_DELAY_CHK &&
16299	    phba->cfg_auto_imax &&
16300	    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
16301	    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
16302		lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
16303
16304	/* process and rearm the EQ */
16305	ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,
16306				      LPFC_THREADED_IRQ);
16307
16308	if (unlikely(ecount == 0)) {
16309		fpeq->EQ_no_entry++;
16310		if (phba->intr_type == MSIX)
16311			/* MSI-X treated interrupt served as no EQ share INT */
16312			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16313					"3358 MSI-X interrupt with no EQE\n");
16314		else
16315			/* Non MSI-X treated on interrupt as EQ share INT */
16316			return IRQ_NONE;
16317	}
16318	return IRQ_HANDLED;
16319}
16320
16321/**
16322 * lpfc_cq_create - Create a Completion Queue on the HBA
16323 * @phba: HBA structure that indicates port to create a queue on.
16324 * @cq: The queue structure to use to create the completion queue.
16325 * @eq: The event queue to bind this completion queue to.
16326 * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16327 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16328 *
16329 * This function creates a completion queue, as detailed in @wq, on a port,
16330 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
16331 *
16332 * The @phba struct is used to send mailbox command to HBA. The @cq struct
16333 * is used to get the entry count and entry size that are necessary to
16334 * determine the number of pages to allocate and use for this queue. The @eq
16335 * is used to indicate which event queue to bind this completion queue to. This
16336 * function will send the CQ_CREATE mailbox command to the HBA to setup the
16337 * completion queue. This function is asynchronous and will wait for the mailbox
16338 * command to finish before continuing.
16339 *
16340 * On success this function will return a zero. If unable to allocate enough
16341 * memory this function will return -ENOMEM. If the queue create mailbox command
16342 * fails this function will return -ENXIO.
16343 **/
16344int
16345lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
16346	       struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
16347{
16348	struct lpfc_mbx_cq_create *cq_create;
16349	struct lpfc_dmabuf *dmabuf;
16350	LPFC_MBOXQ_t *mbox;
16351	int rc, length, status = 0;
16352	uint32_t shdr_status, shdr_add_status;
16353	union lpfc_sli4_cfg_shdr *shdr;
16354
16355	/* sanity check on queue memory */
16356	if (!cq || !eq)
16357		return -ENODEV;
16358
16359	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16360	if (!mbox)
16361		return -ENOMEM;
16362	length = (sizeof(struct lpfc_mbx_cq_create) -
16363		  sizeof(struct lpfc_sli4_cfg_mhdr));
16364	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16365			 LPFC_MBOX_OPCODE_CQ_CREATE,
16366			 length, LPFC_SLI4_MBX_EMBED);
16367	cq_create = &mbox->u.mqe.un.cq_create;
16368	shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
16369	bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
16370		    cq->page_count);
16371	bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
16372	bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
16373	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16374	       phba->sli4_hba.pc_sli4_params.cqv);
16375	if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
16376		bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
16377		       (cq->page_size / SLI4_PAGE_SIZE));
16378		bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
16379		       eq->queue_id);
16380		bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
16381		       phba->sli4_hba.pc_sli4_params.cqav);
16382	} else {
16383		bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
16384		       eq->queue_id);
16385	}
16386	switch (cq->entry_count) {
16387	case 2048:
16388	case 4096:
16389		if (phba->sli4_hba.pc_sli4_params.cqv ==
16390		    LPFC_Q_CREATE_VERSION_2) {
16391			cq_create->u.request.context.lpfc_cq_context_count =
16392				cq->entry_count;
16393			bf_set(lpfc_cq_context_count,
16394			       &cq_create->u.request.context,
16395			       LPFC_CQ_CNT_WORD7);
16396			break;
16397		}
16398		fallthrough;
16399	default:
16400		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16401				"0361 Unsupported CQ count: "
16402				"entry cnt %d sz %d pg cnt %d\n",
16403				cq->entry_count, cq->entry_size,
16404				cq->page_count);
16405		if (cq->entry_count < 256) {
16406			status = -EINVAL;
16407			goto out;
16408		}
16409		fallthrough;	/* otherwise default to smallest count */
16410	case 256:
16411		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16412		       LPFC_CQ_CNT_256);
16413		break;
16414	case 512:
16415		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16416		       LPFC_CQ_CNT_512);
16417		break;
16418	case 1024:
16419		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16420		       LPFC_CQ_CNT_1024);
16421		break;
16422	}
16423	list_for_each_entry(dmabuf, &cq->page_list, list) {
16424		memset(dmabuf->virt, 0, cq->page_size);
16425		cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16426					putPaddrLow(dmabuf->phys);
16427		cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16428					putPaddrHigh(dmabuf->phys);
16429	}
16430	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16431
16432	/* The IOCTL status is embedded in the mailbox subheader. */
16433	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16434	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16435	if (shdr_status || shdr_add_status || rc) {
16436		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16437				"2501 CQ_CREATE mailbox failed with "
16438				"status x%x add_status x%x, mbx status x%x\n",
16439				shdr_status, shdr_add_status, rc);
16440		status = -ENXIO;
16441		goto out;
16442	}
16443	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16444	if (cq->queue_id == 0xFFFF) {
16445		status = -ENXIO;
16446		goto out;
16447	}
16448	/* link the cq onto the parent eq child list */
16449	list_add_tail(&cq->list, &eq->child_list);
16450	/* Set up completion queue's type and subtype */
16451	cq->type = type;
16452	cq->subtype = subtype;
16453	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16454	cq->assoc_qid = eq->queue_id;
16455	cq->assoc_qp = eq;
16456	cq->host_index = 0;
16457	cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16458	cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
16459
16460	if (cq->queue_id > phba->sli4_hba.cq_max)
16461		phba->sli4_hba.cq_max = cq->queue_id;
16462out:
16463	mempool_free(mbox, phba->mbox_mem_pool);
16464	return status;
16465}
16466
16467/**
16468 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
16469 * @phba: HBA structure that indicates port to create a queue on.
16470 * @cqp: The queue structure array to use to create the completion queues.
16471 * @hdwq: The hardware queue array  with the EQ to bind completion queues to.
16472 * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16473 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
16474 *
16475 * This function creates a set of  completion queue, s to support MRQ
16476 * as detailed in @cqp, on a port,
16477 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
16478 *
16479 * The @phba struct is used to send mailbox command to HBA. The @cq struct
16480 * is used to get the entry count and entry size that are necessary to
16481 * determine the number of pages to allocate and use for this queue. The @eq
16482 * is used to indicate which event queue to bind this completion queue to. This
16483 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
16484 * completion queue. This function is asynchronous and will wait for the mailbox
16485 * command to finish before continuing.
16486 *
16487 * On success this function will return a zero. If unable to allocate enough
16488 * memory this function will return -ENOMEM. If the queue create mailbox command
16489 * fails this function will return -ENXIO.
16490 **/
16491int
16492lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
16493		   struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
16494		   uint32_t subtype)
16495{
16496	struct lpfc_queue *cq;
16497	struct lpfc_queue *eq;
16498	struct lpfc_mbx_cq_create_set *cq_set;
16499	struct lpfc_dmabuf *dmabuf;
16500	LPFC_MBOXQ_t *mbox;
16501	int rc, length, alloclen, status = 0;
16502	int cnt, idx, numcq, page_idx = 0;
16503	uint32_t shdr_status, shdr_add_status;
16504	union lpfc_sli4_cfg_shdr *shdr;
16505	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16506
16507	/* sanity check on queue memory */
16508	numcq = phba->cfg_nvmet_mrq;
16509	if (!cqp || !hdwq || !numcq)
16510		return -ENODEV;
16511
16512	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16513	if (!mbox)
16514		return -ENOMEM;
16515
16516	length = sizeof(struct lpfc_mbx_cq_create_set);
16517	length += ((numcq * cqp[0]->page_count) *
16518		   sizeof(struct dma_address));
16519	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16520			LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
16521			LPFC_SLI4_MBX_NEMBED);
16522	if (alloclen < length) {
16523		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16524				"3098 Allocated DMA memory size (%d) is "
16525				"less than the requested DMA memory size "
16526				"(%d)\n", alloclen, length);
16527		status = -ENOMEM;
16528		goto out;
16529	}
16530	cq_set = mbox->sge_array->addr[0];
16531	shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
16532	bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
16533
16534	for (idx = 0; idx < numcq; idx++) {
16535		cq = cqp[idx];
16536		eq = hdwq[idx].hba_eq;
16537		if (!cq || !eq) {
16538			status = -ENOMEM;
16539			goto out;
16540		}
16541		if (!phba->sli4_hba.pc_sli4_params.supported)
16542			hw_page_size = cq->page_size;
16543
16544		switch (idx) {
16545		case 0:
16546			bf_set(lpfc_mbx_cq_create_set_page_size,
16547			       &cq_set->u.request,
16548			       (hw_page_size / SLI4_PAGE_SIZE));
16549			bf_set(lpfc_mbx_cq_create_set_num_pages,
16550			       &cq_set->u.request, cq->page_count);
16551			bf_set(lpfc_mbx_cq_create_set_evt,
16552			       &cq_set->u.request, 1);
16553			bf_set(lpfc_mbx_cq_create_set_valid,
16554			       &cq_set->u.request, 1);
16555			bf_set(lpfc_mbx_cq_create_set_cqe_size,
16556			       &cq_set->u.request, 0);
16557			bf_set(lpfc_mbx_cq_create_set_num_cq,
16558			       &cq_set->u.request, numcq);
16559			bf_set(lpfc_mbx_cq_create_set_autovalid,
16560			       &cq_set->u.request,
16561			       phba->sli4_hba.pc_sli4_params.cqav);
16562			switch (cq->entry_count) {
16563			case 2048:
16564			case 4096:
16565				if (phba->sli4_hba.pc_sli4_params.cqv ==
16566				    LPFC_Q_CREATE_VERSION_2) {
16567					bf_set(lpfc_mbx_cq_create_set_cqe_cnt_lo,
16568					       &cq_set->u.request,
16569					       cq->entry_count);
16570					bf_set(lpfc_mbx_cq_create_set_cqecnt,
16571					       &cq_set->u.request,
16572					       LPFC_CQ_CNT_WORD7);
16573					break;
16574				}
16575				fallthrough;
16576			default:
16577				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16578						"3118 Bad CQ count. (%d)\n",
16579						cq->entry_count);
16580				if (cq->entry_count < 256) {
16581					status = -EINVAL;
16582					goto out;
16583				}
16584				fallthrough;	/* otherwise default to smallest */
16585			case 256:
16586				bf_set(lpfc_mbx_cq_create_set_cqecnt,
16587				       &cq_set->u.request, LPFC_CQ_CNT_256);
16588				break;
16589			case 512:
16590				bf_set(lpfc_mbx_cq_create_set_cqecnt,
16591				       &cq_set->u.request, LPFC_CQ_CNT_512);
16592				break;
16593			case 1024:
16594				bf_set(lpfc_mbx_cq_create_set_cqecnt,
16595				       &cq_set->u.request, LPFC_CQ_CNT_1024);
16596				break;
16597			}
16598			bf_set(lpfc_mbx_cq_create_set_eq_id0,
16599			       &cq_set->u.request, eq->queue_id);
16600			break;
16601		case 1:
16602			bf_set(lpfc_mbx_cq_create_set_eq_id1,
16603			       &cq_set->u.request, eq->queue_id);
16604			break;
16605		case 2:
16606			bf_set(lpfc_mbx_cq_create_set_eq_id2,
16607			       &cq_set->u.request, eq->queue_id);
16608			break;
16609		case 3:
16610			bf_set(lpfc_mbx_cq_create_set_eq_id3,
16611			       &cq_set->u.request, eq->queue_id);
16612			break;
16613		case 4:
16614			bf_set(lpfc_mbx_cq_create_set_eq_id4,
16615			       &cq_set->u.request, eq->queue_id);
16616			break;
16617		case 5:
16618			bf_set(lpfc_mbx_cq_create_set_eq_id5,
16619			       &cq_set->u.request, eq->queue_id);
16620			break;
16621		case 6:
16622			bf_set(lpfc_mbx_cq_create_set_eq_id6,
16623			       &cq_set->u.request, eq->queue_id);
16624			break;
16625		case 7:
16626			bf_set(lpfc_mbx_cq_create_set_eq_id7,
16627			       &cq_set->u.request, eq->queue_id);
16628			break;
16629		case 8:
16630			bf_set(lpfc_mbx_cq_create_set_eq_id8,
16631			       &cq_set->u.request, eq->queue_id);
16632			break;
16633		case 9:
16634			bf_set(lpfc_mbx_cq_create_set_eq_id9,
16635			       &cq_set->u.request, eq->queue_id);
16636			break;
16637		case 10:
16638			bf_set(lpfc_mbx_cq_create_set_eq_id10,
16639			       &cq_set->u.request, eq->queue_id);
16640			break;
16641		case 11:
16642			bf_set(lpfc_mbx_cq_create_set_eq_id11,
16643			       &cq_set->u.request, eq->queue_id);
16644			break;
16645		case 12:
16646			bf_set(lpfc_mbx_cq_create_set_eq_id12,
16647			       &cq_set->u.request, eq->queue_id);
16648			break;
16649		case 13:
16650			bf_set(lpfc_mbx_cq_create_set_eq_id13,
16651			       &cq_set->u.request, eq->queue_id);
16652			break;
16653		case 14:
16654			bf_set(lpfc_mbx_cq_create_set_eq_id14,
16655			       &cq_set->u.request, eq->queue_id);
16656			break;
16657		case 15:
16658			bf_set(lpfc_mbx_cq_create_set_eq_id15,
16659			       &cq_set->u.request, eq->queue_id);
16660			break;
16661		}
16662
16663		/* link the cq onto the parent eq child list */
16664		list_add_tail(&cq->list, &eq->child_list);
16665		/* Set up completion queue's type and subtype */
16666		cq->type = type;
16667		cq->subtype = subtype;
16668		cq->assoc_qid = eq->queue_id;
16669		cq->assoc_qp = eq;
16670		cq->host_index = 0;
16671		cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16672		cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
16673					 cq->entry_count);
16674		cq->chann = idx;
16675
16676		rc = 0;
16677		list_for_each_entry(dmabuf, &cq->page_list, list) {
16678			memset(dmabuf->virt, 0, hw_page_size);
16679			cnt = page_idx + dmabuf->buffer_tag;
16680			cq_set->u.request.page[cnt].addr_lo =
16681					putPaddrLow(dmabuf->phys);
16682			cq_set->u.request.page[cnt].addr_hi =
16683					putPaddrHigh(dmabuf->phys);
16684			rc++;
16685		}
16686		page_idx += rc;
16687	}
16688
16689	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16690
16691	/* The IOCTL status is embedded in the mailbox subheader. */
16692	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16693	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16694	if (shdr_status || shdr_add_status || rc) {
16695		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16696				"3119 CQ_CREATE_SET mailbox failed with "
16697				"status x%x add_status x%x, mbx status x%x\n",
16698				shdr_status, shdr_add_status, rc);
16699		status = -ENXIO;
16700		goto out;
16701	}
16702	rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
16703	if (rc == 0xFFFF) {
16704		status = -ENXIO;
16705		goto out;
16706	}
16707
16708	for (idx = 0; idx < numcq; idx++) {
16709		cq = cqp[idx];
16710		cq->queue_id = rc + idx;
16711		if (cq->queue_id > phba->sli4_hba.cq_max)
16712			phba->sli4_hba.cq_max = cq->queue_id;
16713	}
16714
16715out:
16716	lpfc_sli4_mbox_cmd_free(phba, mbox);
16717	return status;
16718}
16719
16720/**
16721 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
16722 * @phba: HBA structure that indicates port to create a queue on.
16723 * @mq: The queue structure to use to create the mailbox queue.
16724 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
16725 * @cq: The completion queue to associate with this cq.
16726 *
16727 * This function provides failback (fb) functionality when the
16728 * mq_create_ext fails on older FW generations.  It's purpose is identical
16729 * to mq_create_ext otherwise.
16730 *
16731 * This routine cannot fail as all attributes were previously accessed and
16732 * initialized in mq_create_ext.
16733 **/
16734static void
16735lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
16736		       LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
16737{
16738	struct lpfc_mbx_mq_create *mq_create;
16739	struct lpfc_dmabuf *dmabuf;
16740	int length;
16741
16742	length = (sizeof(struct lpfc_mbx_mq_create) -
16743		  sizeof(struct lpfc_sli4_cfg_mhdr));
16744	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16745			 LPFC_MBOX_OPCODE_MQ_CREATE,
16746			 length, LPFC_SLI4_MBX_EMBED);
16747	mq_create = &mbox->u.mqe.un.mq_create;
16748	bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
16749	       mq->page_count);
16750	bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
16751	       cq->queue_id);
16752	bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
16753	switch (mq->entry_count) {
16754	case 16:
16755		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16756		       LPFC_MQ_RING_SIZE_16);
16757		break;
16758	case 32:
16759		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16760		       LPFC_MQ_RING_SIZE_32);
16761		break;
16762	case 64:
16763		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16764		       LPFC_MQ_RING_SIZE_64);
16765		break;
16766	case 128:
16767		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16768		       LPFC_MQ_RING_SIZE_128);
16769		break;
16770	}
16771	list_for_each_entry(dmabuf, &mq->page_list, list) {
16772		mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16773			putPaddrLow(dmabuf->phys);
16774		mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16775			putPaddrHigh(dmabuf->phys);
16776	}
16777}
16778
16779/**
16780 * lpfc_mq_create - Create a mailbox Queue on the HBA
16781 * @phba: HBA structure that indicates port to create a queue on.
16782 * @mq: The queue structure to use to create the mailbox queue.
16783 * @cq: The completion queue to associate with this cq.
16784 * @subtype: The queue's subtype.
16785 *
16786 * This function creates a mailbox queue, as detailed in @mq, on a port,
16787 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
16788 *
16789 * The @phba struct is used to send mailbox command to HBA. The @cq struct
16790 * is used to get the entry count and entry size that are necessary to
16791 * determine the number of pages to allocate and use for this queue. This
16792 * function will send the MQ_CREATE mailbox command to the HBA to setup the
16793 * mailbox queue. This function is asynchronous and will wait for the mailbox
16794 * command to finish before continuing.
16795 *
16796 * On success this function will return a zero. If unable to allocate enough
16797 * memory this function will return -ENOMEM. If the queue create mailbox command
16798 * fails this function will return -ENXIO.
16799 **/
16800int32_t
16801lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
16802	       struct lpfc_queue *cq, uint32_t subtype)
16803{
16804	struct lpfc_mbx_mq_create *mq_create;
16805	struct lpfc_mbx_mq_create_ext *mq_create_ext;
16806	struct lpfc_dmabuf *dmabuf;
16807	LPFC_MBOXQ_t *mbox;
16808	int rc, length, status = 0;
16809	uint32_t shdr_status, shdr_add_status;
16810	union lpfc_sli4_cfg_shdr *shdr;
16811	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16812
16813	/* sanity check on queue memory */
16814	if (!mq || !cq)
16815		return -ENODEV;
16816	if (!phba->sli4_hba.pc_sli4_params.supported)
16817		hw_page_size = SLI4_PAGE_SIZE;
16818
16819	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16820	if (!mbox)
16821		return -ENOMEM;
16822	length = (sizeof(struct lpfc_mbx_mq_create_ext) -
16823		  sizeof(struct lpfc_sli4_cfg_mhdr));
16824	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16825			 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
16826			 length, LPFC_SLI4_MBX_EMBED);
16827
16828	mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
16829	shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
16830	bf_set(lpfc_mbx_mq_create_ext_num_pages,
16831	       &mq_create_ext->u.request, mq->page_count);
16832	bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
16833	       &mq_create_ext->u.request, 1);
16834	bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
16835	       &mq_create_ext->u.request, 1);
16836	bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
16837	       &mq_create_ext->u.request, 1);
16838	bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
16839	       &mq_create_ext->u.request, 1);
16840	bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
16841	       &mq_create_ext->u.request, 1);
16842	bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
16843	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16844	       phba->sli4_hba.pc_sli4_params.mqv);
16845	if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
16846		bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
16847		       cq->queue_id);
16848	else
16849		bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
16850		       cq->queue_id);
16851	switch (mq->entry_count) {
16852	default:
16853		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16854				"0362 Unsupported MQ count. (%d)\n",
16855				mq->entry_count);
16856		if (mq->entry_count < 16) {
16857			status = -EINVAL;
16858			goto out;
16859		}
16860		fallthrough;	/* otherwise default to smallest count */
16861	case 16:
16862		bf_set(lpfc_mq_context_ring_size,
16863		       &mq_create_ext->u.request.context,
16864		       LPFC_MQ_RING_SIZE_16);
16865		break;
16866	case 32:
16867		bf_set(lpfc_mq_context_ring_size,
16868		       &mq_create_ext->u.request.context,
16869		       LPFC_MQ_RING_SIZE_32);
16870		break;
16871	case 64:
16872		bf_set(lpfc_mq_context_ring_size,
16873		       &mq_create_ext->u.request.context,
16874		       LPFC_MQ_RING_SIZE_64);
16875		break;
16876	case 128:
16877		bf_set(lpfc_mq_context_ring_size,
16878		       &mq_create_ext->u.request.context,
16879		       LPFC_MQ_RING_SIZE_128);
16880		break;
16881	}
16882	list_for_each_entry(dmabuf, &mq->page_list, list) {
16883		memset(dmabuf->virt, 0, hw_page_size);
16884		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
16885					putPaddrLow(dmabuf->phys);
16886		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
16887					putPaddrHigh(dmabuf->phys);
16888	}
16889	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16890	mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16891			      &mq_create_ext->u.response);
16892	if (rc != MBX_SUCCESS) {
16893		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16894				"2795 MQ_CREATE_EXT failed with "
16895				"status x%x. Failback to MQ_CREATE.\n",
16896				rc);
16897		lpfc_mq_create_fb_init(phba, mq, mbox, cq);
16898		mq_create = &mbox->u.mqe.un.mq_create;
16899		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16900		shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
16901		mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16902				      &mq_create->u.response);
16903	}
16904
16905	/* The IOCTL status is embedded in the mailbox subheader. */
16906	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16907	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16908	if (shdr_status || shdr_add_status || rc) {
16909		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16910				"2502 MQ_CREATE mailbox failed with "
16911				"status x%x add_status x%x, mbx status x%x\n",
16912				shdr_status, shdr_add_status, rc);
16913		status = -ENXIO;
16914		goto out;
16915	}
16916	if (mq->queue_id == 0xFFFF) {
16917		status = -ENXIO;
16918		goto out;
16919	}
16920	mq->type = LPFC_MQ;
16921	mq->assoc_qid = cq->queue_id;
16922	mq->subtype = subtype;
16923	mq->host_index = 0;
16924	mq->hba_index = 0;
16925
16926	/* link the mq onto the parent cq child list */
16927	list_add_tail(&mq->list, &cq->child_list);
16928out:
16929	mempool_free(mbox, phba->mbox_mem_pool);
16930	return status;
16931}
16932
16933/**
16934 * lpfc_wq_create - Create a Work Queue on the HBA
16935 * @phba: HBA structure that indicates port to create a queue on.
16936 * @wq: The queue structure to use to create the work queue.
16937 * @cq: The completion queue to bind this work queue to.
16938 * @subtype: The subtype of the work queue indicating its functionality.
16939 *
16940 * This function creates a work queue, as detailed in @wq, on a port, described
16941 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
16942 *
16943 * The @phba struct is used to send mailbox command to HBA. The @wq struct
16944 * is used to get the entry count and entry size that are necessary to
16945 * determine the number of pages to allocate and use for this queue. The @cq
16946 * is used to indicate which completion queue to bind this work queue to. This
16947 * function will send the WQ_CREATE mailbox command to the HBA to setup the
16948 * work queue. This function is asynchronous and will wait for the mailbox
16949 * command to finish before continuing.
16950 *
16951 * On success this function will return a zero. If unable to allocate enough
16952 * memory this function will return -ENOMEM. If the queue create mailbox command
16953 * fails this function will return -ENXIO.
16954 **/
16955int
16956lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
16957	       struct lpfc_queue *cq, uint32_t subtype)
16958{
16959	struct lpfc_mbx_wq_create *wq_create;
16960	struct lpfc_dmabuf *dmabuf;
16961	LPFC_MBOXQ_t *mbox;
16962	int rc, length, status = 0;
16963	uint32_t shdr_status, shdr_add_status;
16964	union lpfc_sli4_cfg_shdr *shdr;
16965	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16966	struct dma_address *page;
16967	void __iomem *bar_memmap_p;
16968	uint32_t db_offset;
16969	uint16_t pci_barset;
16970	uint8_t dpp_barset;
16971	uint32_t dpp_offset;
16972	uint8_t wq_create_version;
16973
16974	/* sanity check on queue memory */
16975	if (!wq || !cq)
16976		return -ENODEV;
16977	if (!phba->sli4_hba.pc_sli4_params.supported)
16978		hw_page_size = wq->page_size;
16979
16980	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16981	if (!mbox)
16982		return -ENOMEM;
16983	length = (sizeof(struct lpfc_mbx_wq_create) -
16984		  sizeof(struct lpfc_sli4_cfg_mhdr));
16985	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16986			 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
16987			 length, LPFC_SLI4_MBX_EMBED);
16988	wq_create = &mbox->u.mqe.un.wq_create;
16989	shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
16990	bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
16991		    wq->page_count);
16992	bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
16993		    cq->queue_id);
16994
16995	/* wqv is the earliest version supported, NOT the latest */
16996	bf_set(lpfc_mbox_hdr_version, &shdr->request,
16997	       phba->sli4_hba.pc_sli4_params.wqv);
16998
16999	if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
17000	    (wq->page_size > SLI4_PAGE_SIZE))
17001		wq_create_version = LPFC_Q_CREATE_VERSION_1;
17002	else
17003		wq_create_version = LPFC_Q_CREATE_VERSION_0;
17004
17005	switch (wq_create_version) {
17006	case LPFC_Q_CREATE_VERSION_1:
17007		bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
17008		       wq->entry_count);
17009		bf_set(lpfc_mbox_hdr_version, &shdr->request,
17010		       LPFC_Q_CREATE_VERSION_1);
17011
17012		switch (wq->entry_size) {
17013		default:
17014		case 64:
17015			bf_set(lpfc_mbx_wq_create_wqe_size,
17016			       &wq_create->u.request_1,
17017			       LPFC_WQ_WQE_SIZE_64);
17018			break;
17019		case 128:
17020			bf_set(lpfc_mbx_wq_create_wqe_size,
17021			       &wq_create->u.request_1,
17022			       LPFC_WQ_WQE_SIZE_128);
17023			break;
17024		}
17025		/* Request DPP by default */
17026		bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
17027		bf_set(lpfc_mbx_wq_create_page_size,
17028		       &wq_create->u.request_1,
17029		       (wq->page_size / SLI4_PAGE_SIZE));
17030		page = wq_create->u.request_1.page;
17031		break;
17032	default:
17033		page = wq_create->u.request.page;
17034		break;
17035	}
17036
17037	list_for_each_entry(dmabuf, &wq->page_list, list) {
17038		memset(dmabuf->virt, 0, hw_page_size);
17039		page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
17040		page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
17041	}
17042
17043	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17044		bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
17045
17046	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17047	/* The IOCTL status is embedded in the mailbox subheader. */
17048	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17049	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17050	if (shdr_status || shdr_add_status || rc) {
17051		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17052				"2503 WQ_CREATE mailbox failed with "
17053				"status x%x add_status x%x, mbx status x%x\n",
17054				shdr_status, shdr_add_status, rc);
17055		status = -ENXIO;
17056		goto out;
17057	}
17058
17059	if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
17060		wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
17061					&wq_create->u.response);
17062	else
17063		wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
17064					&wq_create->u.response_1);
17065
17066	if (wq->queue_id == 0xFFFF) {
17067		status = -ENXIO;
17068		goto out;
17069	}
17070
17071	wq->db_format = LPFC_DB_LIST_FORMAT;
17072	if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
17073		if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
17074			wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
17075					       &wq_create->u.response);
17076			if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
17077			    (wq->db_format != LPFC_DB_RING_FORMAT)) {
17078				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17079						"3265 WQ[%d] doorbell format "
17080						"not supported: x%x\n",
17081						wq->queue_id, wq->db_format);
17082				status = -EINVAL;
17083				goto out;
17084			}
17085			pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
17086					    &wq_create->u.response);
17087			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17088								   pci_barset);
17089			if (!bar_memmap_p) {
17090				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17091						"3263 WQ[%d] failed to memmap "
17092						"pci barset:x%x\n",
17093						wq->queue_id, pci_barset);
17094				status = -ENOMEM;
17095				goto out;
17096			}
17097			db_offset = wq_create->u.response.doorbell_offset;
17098			if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
17099			    (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
17100				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17101						"3252 WQ[%d] doorbell offset "
17102						"not supported: x%x\n",
17103						wq->queue_id, db_offset);
17104				status = -EINVAL;
17105				goto out;
17106			}
17107			wq->db_regaddr = bar_memmap_p + db_offset;
17108			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17109					"3264 WQ[%d]: barset:x%x, offset:x%x, "
17110					"format:x%x\n", wq->queue_id,
17111					pci_barset, db_offset, wq->db_format);
17112		} else
17113			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
17114	} else {
17115		/* Check if DPP was honored by the firmware */
17116		wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
17117				    &wq_create->u.response_1);
17118		if (wq->dpp_enable) {
17119			pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
17120					    &wq_create->u.response_1);
17121			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17122								   pci_barset);
17123			if (!bar_memmap_p) {
17124				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17125						"3267 WQ[%d] failed to memmap "
17126						"pci barset:x%x\n",
17127						wq->queue_id, pci_barset);
17128				status = -ENOMEM;
17129				goto out;
17130			}
17131			db_offset = wq_create->u.response_1.doorbell_offset;
17132			wq->db_regaddr = bar_memmap_p + db_offset;
17133			wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
17134					    &wq_create->u.response_1);
17135			dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
17136					    &wq_create->u.response_1);
17137			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17138								   dpp_barset);
17139			if (!bar_memmap_p) {
17140				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17141						"3268 WQ[%d] failed to memmap "
17142						"pci barset:x%x\n",
17143						wq->queue_id, dpp_barset);
17144				status = -ENOMEM;
17145				goto out;
17146			}
17147			dpp_offset = wq_create->u.response_1.dpp_offset;
17148			wq->dpp_regaddr = bar_memmap_p + dpp_offset;
17149			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17150					"3271 WQ[%d]: barset:x%x, offset:x%x, "
17151					"dpp_id:x%x dpp_barset:x%x "
17152					"dpp_offset:x%x\n",
17153					wq->queue_id, pci_barset, db_offset,
17154					wq->dpp_id, dpp_barset, dpp_offset);
17155
17156#ifdef CONFIG_X86
17157			/* Enable combined writes for DPP aperture */
17158			bar_memmap_p = lpfc_dpp_wc_map(phba, dpp_barset);
17159			if (!bar_memmap_p) {
17160				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17161					"3272 Cannot setup Combined "
17162					"Write on WQ[%d] - disable DPP\n",
17163					wq->queue_id);
17164				phba->cfg_enable_dpp = 0;
17165			} else {
17166				wq->dpp_regaddr = bar_memmap_p + dpp_offset;
17167			}
17168#else
17169			phba->cfg_enable_dpp = 0;
17170#endif
17171		} else
17172			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
17173	}
17174	wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
17175	if (wq->pring == NULL) {
17176		status = -ENOMEM;
17177		goto out;
17178	}
17179	wq->type = LPFC_WQ;
17180	wq->assoc_qid = cq->queue_id;
17181	wq->subtype = subtype;
17182	wq->host_index = 0;
17183	wq->hba_index = 0;
17184	wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
17185
17186	/* link the wq onto the parent cq child list */
17187	list_add_tail(&wq->list, &cq->child_list);
17188out:
17189	mempool_free(mbox, phba->mbox_mem_pool);
17190	return status;
17191}
17192
17193/**
17194 * lpfc_rq_create - Create a Receive Queue on the HBA
17195 * @phba: HBA structure that indicates port to create a queue on.
17196 * @hrq: The queue structure to use to create the header receive queue.
17197 * @drq: The queue structure to use to create the data receive queue.
17198 * @cq: The completion queue to bind this work queue to.
17199 * @subtype: The subtype of the work queue indicating its functionality.
17200 *
17201 * This function creates a receive buffer queue pair , as detailed in @hrq and
17202 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17203 * to the HBA.
17204 *
17205 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17206 * struct is used to get the entry count that is necessary to determine the
17207 * number of pages to use for this queue. The @cq is used to indicate which
17208 * completion queue to bind received buffers that are posted to these queues to.
17209 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17210 * receive queue pair. This function is asynchronous and will wait for the
17211 * mailbox command to finish before continuing.
17212 *
17213 * On success this function will return a zero. If unable to allocate enough
17214 * memory this function will return -ENOMEM. If the queue create mailbox command
17215 * fails this function will return -ENXIO.
17216 **/
17217int
17218lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17219	       struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
17220{
17221	struct lpfc_mbx_rq_create *rq_create;
17222	struct lpfc_dmabuf *dmabuf;
17223	LPFC_MBOXQ_t *mbox;
17224	int rc, length, status = 0;
17225	uint32_t shdr_status, shdr_add_status;
17226	union lpfc_sli4_cfg_shdr *shdr;
17227	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17228	void __iomem *bar_memmap_p;
17229	uint32_t db_offset;
17230	uint16_t pci_barset;
17231
17232	/* sanity check on queue memory */
17233	if (!hrq || !drq || !cq)
17234		return -ENODEV;
17235	if (!phba->sli4_hba.pc_sli4_params.supported)
17236		hw_page_size = SLI4_PAGE_SIZE;
17237
17238	if (hrq->entry_count != drq->entry_count)
17239		return -EINVAL;
17240	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17241	if (!mbox)
17242		return -ENOMEM;
17243	length = (sizeof(struct lpfc_mbx_rq_create) -
17244		  sizeof(struct lpfc_sli4_cfg_mhdr));
17245	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17246			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17247			 length, LPFC_SLI4_MBX_EMBED);
17248	rq_create = &mbox->u.mqe.un.rq_create;
17249	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17250	bf_set(lpfc_mbox_hdr_version, &shdr->request,
17251	       phba->sli4_hba.pc_sli4_params.rqv);
17252	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17253		bf_set(lpfc_rq_context_rqe_count_1,
17254		       &rq_create->u.request.context,
17255		       hrq->entry_count);
17256		rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
17257		bf_set(lpfc_rq_context_rqe_size,
17258		       &rq_create->u.request.context,
17259		       LPFC_RQE_SIZE_8);
17260		bf_set(lpfc_rq_context_page_size,
17261		       &rq_create->u.request.context,
17262		       LPFC_RQ_PAGE_SIZE_4096);
17263	} else {
17264		switch (hrq->entry_count) {
17265		default:
17266			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17267					"2535 Unsupported RQ count. (%d)\n",
17268					hrq->entry_count);
17269			if (hrq->entry_count < 512) {
17270				status = -EINVAL;
17271				goto out;
17272			}
17273			fallthrough;	/* otherwise default to smallest count */
17274		case 512:
17275			bf_set(lpfc_rq_context_rqe_count,
17276			       &rq_create->u.request.context,
17277			       LPFC_RQ_RING_SIZE_512);
17278			break;
17279		case 1024:
17280			bf_set(lpfc_rq_context_rqe_count,
17281			       &rq_create->u.request.context,
17282			       LPFC_RQ_RING_SIZE_1024);
17283			break;
17284		case 2048:
17285			bf_set(lpfc_rq_context_rqe_count,
17286			       &rq_create->u.request.context,
17287			       LPFC_RQ_RING_SIZE_2048);
17288			break;
17289		case 4096:
17290			bf_set(lpfc_rq_context_rqe_count,
17291			       &rq_create->u.request.context,
17292			       LPFC_RQ_RING_SIZE_4096);
17293			break;
17294		}
17295		bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
17296		       LPFC_HDR_BUF_SIZE);
17297	}
17298	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17299	       cq->queue_id);
17300	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17301	       hrq->page_count);
17302	list_for_each_entry(dmabuf, &hrq->page_list, list) {
17303		memset(dmabuf->virt, 0, hw_page_size);
17304		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17305					putPaddrLow(dmabuf->phys);
17306		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17307					putPaddrHigh(dmabuf->phys);
17308	}
17309	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17310		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17311
17312	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17313	/* The IOCTL status is embedded in the mailbox subheader. */
17314	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17315	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17316	if (shdr_status || shdr_add_status || rc) {
17317		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17318				"2504 RQ_CREATE mailbox failed with "
17319				"status x%x add_status x%x, mbx status x%x\n",
17320				shdr_status, shdr_add_status, rc);
17321		status = -ENXIO;
17322		goto out;
17323	}
17324	hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17325	if (hrq->queue_id == 0xFFFF) {
17326		status = -ENXIO;
17327		goto out;
17328	}
17329
17330	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
17331		hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
17332					&rq_create->u.response);
17333		if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
17334		    (hrq->db_format != LPFC_DB_RING_FORMAT)) {
17335			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17336					"3262 RQ [%d] doorbell format not "
17337					"supported: x%x\n", hrq->queue_id,
17338					hrq->db_format);
17339			status = -EINVAL;
17340			goto out;
17341		}
17342
17343		pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
17344				    &rq_create->u.response);
17345		bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
17346		if (!bar_memmap_p) {
17347			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17348					"3269 RQ[%d] failed to memmap pci "
17349					"barset:x%x\n", hrq->queue_id,
17350					pci_barset);
17351			status = -ENOMEM;
17352			goto out;
17353		}
17354
17355		db_offset = rq_create->u.response.doorbell_offset;
17356		if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
17357		    (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
17358			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17359					"3270 RQ[%d] doorbell offset not "
17360					"supported: x%x\n", hrq->queue_id,
17361					db_offset);
17362			status = -EINVAL;
17363			goto out;
17364		}
17365		hrq->db_regaddr = bar_memmap_p + db_offset;
17366		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17367				"3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
17368				"format:x%x\n", hrq->queue_id, pci_barset,
17369				db_offset, hrq->db_format);
17370	} else {
17371		hrq->db_format = LPFC_DB_RING_FORMAT;
17372		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17373	}
17374	hrq->type = LPFC_HRQ;
17375	hrq->assoc_qid = cq->queue_id;
17376	hrq->subtype = subtype;
17377	hrq->host_index = 0;
17378	hrq->hba_index = 0;
17379	hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17380
17381	/* now create the data queue */
17382	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17383			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17384			 length, LPFC_SLI4_MBX_EMBED);
17385	bf_set(lpfc_mbox_hdr_version, &shdr->request,
17386	       phba->sli4_hba.pc_sli4_params.rqv);
17387	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17388		bf_set(lpfc_rq_context_rqe_count_1,
17389		       &rq_create->u.request.context, hrq->entry_count);
17390		if (subtype == LPFC_NVMET)
17391			rq_create->u.request.context.buffer_size =
17392				LPFC_NVMET_DATA_BUF_SIZE;
17393		else
17394			rq_create->u.request.context.buffer_size =
17395				LPFC_DATA_BUF_SIZE;
17396		bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
17397		       LPFC_RQE_SIZE_8);
17398		bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
17399		       (PAGE_SIZE/SLI4_PAGE_SIZE));
17400	} else {
17401		switch (drq->entry_count) {
17402		default:
17403			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17404					"2536 Unsupported RQ count. (%d)\n",
17405					drq->entry_count);
17406			if (drq->entry_count < 512) {
17407				status = -EINVAL;
17408				goto out;
17409			}
17410			fallthrough;	/* otherwise default to smallest count */
17411		case 512:
17412			bf_set(lpfc_rq_context_rqe_count,
17413			       &rq_create->u.request.context,
17414			       LPFC_RQ_RING_SIZE_512);
17415			break;
17416		case 1024:
17417			bf_set(lpfc_rq_context_rqe_count,
17418			       &rq_create->u.request.context,
17419			       LPFC_RQ_RING_SIZE_1024);
17420			break;
17421		case 2048:
17422			bf_set(lpfc_rq_context_rqe_count,
17423			       &rq_create->u.request.context,
17424			       LPFC_RQ_RING_SIZE_2048);
17425			break;
17426		case 4096:
17427			bf_set(lpfc_rq_context_rqe_count,
17428			       &rq_create->u.request.context,
17429			       LPFC_RQ_RING_SIZE_4096);
17430			break;
17431		}
17432		if (subtype == LPFC_NVMET)
17433			bf_set(lpfc_rq_context_buf_size,
17434			       &rq_create->u.request.context,
17435			       LPFC_NVMET_DATA_BUF_SIZE);
17436		else
17437			bf_set(lpfc_rq_context_buf_size,
17438			       &rq_create->u.request.context,
17439			       LPFC_DATA_BUF_SIZE);
17440	}
17441	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17442	       cq->queue_id);
17443	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17444	       drq->page_count);
17445	list_for_each_entry(dmabuf, &drq->page_list, list) {
17446		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17447					putPaddrLow(dmabuf->phys);
17448		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17449					putPaddrHigh(dmabuf->phys);
17450	}
17451	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17452		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17453	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17454	/* The IOCTL status is embedded in the mailbox subheader. */
17455	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17456	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17457	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17458	if (shdr_status || shdr_add_status || rc) {
17459		status = -ENXIO;
17460		goto out;
17461	}
17462	drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17463	if (drq->queue_id == 0xFFFF) {
17464		status = -ENXIO;
17465		goto out;
17466	}
17467	drq->type = LPFC_DRQ;
17468	drq->assoc_qid = cq->queue_id;
17469	drq->subtype = subtype;
17470	drq->host_index = 0;
17471	drq->hba_index = 0;
17472	drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17473
17474	/* link the header and data RQs onto the parent cq child list */
17475	list_add_tail(&hrq->list, &cq->child_list);
17476	list_add_tail(&drq->list, &cq->child_list);
17477
17478out:
17479	mempool_free(mbox, phba->mbox_mem_pool);
17480	return status;
17481}
17482
17483/**
17484 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
17485 * @phba: HBA structure that indicates port to create a queue on.
17486 * @hrqp: The queue structure array to use to create the header receive queues.
17487 * @drqp: The queue structure array to use to create the data receive queues.
17488 * @cqp: The completion queue array to bind these receive queues to.
17489 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
17490 *
17491 * This function creates a receive buffer queue pair , as detailed in @hrq and
17492 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17493 * to the HBA.
17494 *
17495 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17496 * struct is used to get the entry count that is necessary to determine the
17497 * number of pages to use for this queue. The @cq is used to indicate which
17498 * completion queue to bind received buffers that are posted to these queues to.
17499 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17500 * receive queue pair. This function is asynchronous and will wait for the
17501 * mailbox command to finish before continuing.
17502 *
17503 * On success this function will return a zero. If unable to allocate enough
17504 * memory this function will return -ENOMEM. If the queue create mailbox command
17505 * fails this function will return -ENXIO.
17506 **/
17507int
17508lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
17509		struct lpfc_queue **drqp, struct lpfc_queue **cqp,
17510		uint32_t subtype)
17511{
17512	struct lpfc_queue *hrq, *drq, *cq;
17513	struct lpfc_mbx_rq_create_v2 *rq_create;
17514	struct lpfc_dmabuf *dmabuf;
17515	LPFC_MBOXQ_t *mbox;
17516	int rc, length, alloclen, status = 0;
17517	int cnt, idx, numrq, page_idx = 0;
17518	uint32_t shdr_status, shdr_add_status;
17519	union lpfc_sli4_cfg_shdr *shdr;
17520	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17521
17522	numrq = phba->cfg_nvmet_mrq;
17523	/* sanity check on array memory */
17524	if (!hrqp || !drqp || !cqp || !numrq)
17525		return -ENODEV;
17526	if (!phba->sli4_hba.pc_sli4_params.supported)
17527		hw_page_size = SLI4_PAGE_SIZE;
17528
17529	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17530	if (!mbox)
17531		return -ENOMEM;
17532
17533	length = sizeof(struct lpfc_mbx_rq_create_v2);
17534	length += ((2 * numrq * hrqp[0]->page_count) *
17535		   sizeof(struct dma_address));
17536
17537	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17538				    LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
17539				    LPFC_SLI4_MBX_NEMBED);
17540	if (alloclen < length) {
17541		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17542				"3099 Allocated DMA memory size (%d) is "
17543				"less than the requested DMA memory size "
17544				"(%d)\n", alloclen, length);
17545		status = -ENOMEM;
17546		goto out;
17547	}
17548
17549
17550
17551	rq_create = mbox->sge_array->addr[0];
17552	shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
17553
17554	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
17555	cnt = 0;
17556
17557	for (idx = 0; idx < numrq; idx++) {
17558		hrq = hrqp[idx];
17559		drq = drqp[idx];
17560		cq  = cqp[idx];
17561
17562		/* sanity check on queue memory */
17563		if (!hrq || !drq || !cq) {
17564			status = -ENODEV;
17565			goto out;
17566		}
17567
17568		if (hrq->entry_count != drq->entry_count) {
17569			status = -EINVAL;
17570			goto out;
17571		}
17572
17573		if (idx == 0) {
17574			bf_set(lpfc_mbx_rq_create_num_pages,
17575			       &rq_create->u.request,
17576			       hrq->page_count);
17577			bf_set(lpfc_mbx_rq_create_rq_cnt,
17578			       &rq_create->u.request, (numrq * 2));
17579			bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
17580			       1);
17581			bf_set(lpfc_rq_context_base_cq,
17582			       &rq_create->u.request.context,
17583			       cq->queue_id);
17584			bf_set(lpfc_rq_context_data_size,
17585			       &rq_create->u.request.context,
17586			       LPFC_NVMET_DATA_BUF_SIZE);
17587			bf_set(lpfc_rq_context_hdr_size,
17588			       &rq_create->u.request.context,
17589			       LPFC_HDR_BUF_SIZE);
17590			bf_set(lpfc_rq_context_rqe_count_1,
17591			       &rq_create->u.request.context,
17592			       hrq->entry_count);
17593			bf_set(lpfc_rq_context_rqe_size,
17594			       &rq_create->u.request.context,
17595			       LPFC_RQE_SIZE_8);
17596			bf_set(lpfc_rq_context_page_size,
17597			       &rq_create->u.request.context,
17598			       (PAGE_SIZE/SLI4_PAGE_SIZE));
17599		}
17600		rc = 0;
17601		list_for_each_entry(dmabuf, &hrq->page_list, list) {
17602			memset(dmabuf->virt, 0, hw_page_size);
17603			cnt = page_idx + dmabuf->buffer_tag;
17604			rq_create->u.request.page[cnt].addr_lo =
17605					putPaddrLow(dmabuf->phys);
17606			rq_create->u.request.page[cnt].addr_hi =
17607					putPaddrHigh(dmabuf->phys);
17608			rc++;
17609		}
17610		page_idx += rc;
17611
17612		rc = 0;
17613		list_for_each_entry(dmabuf, &drq->page_list, list) {
17614			memset(dmabuf->virt, 0, hw_page_size);
17615			cnt = page_idx + dmabuf->buffer_tag;
17616			rq_create->u.request.page[cnt].addr_lo =
17617					putPaddrLow(dmabuf->phys);
17618			rq_create->u.request.page[cnt].addr_hi =
17619					putPaddrHigh(dmabuf->phys);
17620			rc++;
17621		}
17622		page_idx += rc;
17623
17624		hrq->db_format = LPFC_DB_RING_FORMAT;
17625		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17626		hrq->type = LPFC_HRQ;
17627		hrq->assoc_qid = cq->queue_id;
17628		hrq->subtype = subtype;
17629		hrq->host_index = 0;
17630		hrq->hba_index = 0;
17631		hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17632
17633		drq->db_format = LPFC_DB_RING_FORMAT;
17634		drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17635		drq->type = LPFC_DRQ;
17636		drq->assoc_qid = cq->queue_id;
17637		drq->subtype = subtype;
17638		drq->host_index = 0;
17639		drq->hba_index = 0;
17640		drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
17641
17642		list_add_tail(&hrq->list, &cq->child_list);
17643		list_add_tail(&drq->list, &cq->child_list);
17644	}
17645
17646	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17647	/* The IOCTL status is embedded in the mailbox subheader. */
17648	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17649	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17650	if (shdr_status || shdr_add_status || rc) {
17651		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17652				"3120 RQ_CREATE mailbox failed with "
17653				"status x%x add_status x%x, mbx status x%x\n",
17654				shdr_status, shdr_add_status, rc);
17655		status = -ENXIO;
17656		goto out;
17657	}
17658	rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17659	if (rc == 0xFFFF) {
17660		status = -ENXIO;
17661		goto out;
17662	}
17663
17664	/* Initialize all RQs with associated queue id */
17665	for (idx = 0; idx < numrq; idx++) {
17666		hrq = hrqp[idx];
17667		hrq->queue_id = rc + (2 * idx);
17668		drq = drqp[idx];
17669		drq->queue_id = rc + (2 * idx) + 1;
17670	}
17671
17672out:
17673	lpfc_sli4_mbox_cmd_free(phba, mbox);
17674	return status;
17675}
17676
17677/**
17678 * lpfc_eq_destroy - Destroy an event Queue on the HBA
17679 * @phba: HBA structure that indicates port to destroy a queue on.
17680 * @eq: The queue structure associated with the queue to destroy.
17681 *
17682 * This function destroys a queue, as detailed in @eq by sending an mailbox
17683 * command, specific to the type of queue, to the HBA.
17684 *
17685 * The @eq struct is used to get the queue ID of the queue to destroy.
17686 *
17687 * On success this function will return a zero. If the queue destroy mailbox
17688 * command fails this function will return -ENXIO.
17689 **/
17690int
17691lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
17692{
17693	LPFC_MBOXQ_t *mbox;
17694	int rc, length, status = 0;
17695	uint32_t shdr_status, shdr_add_status;
17696	union lpfc_sli4_cfg_shdr *shdr;
17697
17698	/* sanity check on queue memory */
17699	if (!eq)
17700		return -ENODEV;
17701
17702	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17703		goto list_remove;
17704
17705	mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
17706	if (!mbox)
17707		return -ENOMEM;
17708	length = (sizeof(struct lpfc_mbx_eq_destroy) -
17709		  sizeof(struct lpfc_sli4_cfg_mhdr));
17710	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17711			 LPFC_MBOX_OPCODE_EQ_DESTROY,
17712			 length, LPFC_SLI4_MBX_EMBED);
17713	bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
17714	       eq->queue_id);
17715	mbox->vport = eq->phba->pport;
17716	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17717
17718	rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
17719	/* The IOCTL status is embedded in the mailbox subheader. */
17720	shdr = (union lpfc_sli4_cfg_shdr *)
17721		&mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
17722	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17723	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17724	if (shdr_status || shdr_add_status || rc) {
17725		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17726				"2505 EQ_DESTROY mailbox failed with "
17727				"status x%x add_status x%x, mbx status x%x\n",
17728				shdr_status, shdr_add_status, rc);
17729		status = -ENXIO;
17730	}
17731	mempool_free(mbox, eq->phba->mbox_mem_pool);
17732
17733list_remove:
17734	/* Remove eq from any list */
17735	list_del_init(&eq->list);
17736
17737	return status;
17738}
17739
17740/**
17741 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
17742 * @phba: HBA structure that indicates port to destroy a queue on.
17743 * @cq: The queue structure associated with the queue to destroy.
17744 *
17745 * This function destroys a queue, as detailed in @cq by sending an mailbox
17746 * command, specific to the type of queue, to the HBA.
17747 *
17748 * The @cq struct is used to get the queue ID of the queue to destroy.
17749 *
17750 * On success this function will return a zero. If the queue destroy mailbox
17751 * command fails this function will return -ENXIO.
17752 **/
17753int
17754lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
17755{
17756	LPFC_MBOXQ_t *mbox;
17757	int rc, length, status = 0;
17758	uint32_t shdr_status, shdr_add_status;
17759	union lpfc_sli4_cfg_shdr *shdr;
17760
17761	/* sanity check on queue memory */
17762	if (!cq)
17763		return -ENODEV;
17764
17765	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17766		goto list_remove;
17767
17768	mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
17769	if (!mbox)
17770		return -ENOMEM;
17771	length = (sizeof(struct lpfc_mbx_cq_destroy) -
17772		  sizeof(struct lpfc_sli4_cfg_mhdr));
17773	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17774			 LPFC_MBOX_OPCODE_CQ_DESTROY,
17775			 length, LPFC_SLI4_MBX_EMBED);
17776	bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
17777	       cq->queue_id);
17778	mbox->vport = cq->phba->pport;
17779	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17780	rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
17781	/* The IOCTL status is embedded in the mailbox subheader. */
17782	shdr = (union lpfc_sli4_cfg_shdr *)
17783		&mbox->u.mqe.un.wq_create.header.cfg_shdr;
17784	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17785	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17786	if (shdr_status || shdr_add_status || rc) {
17787		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17788				"2506 CQ_DESTROY mailbox failed with "
17789				"status x%x add_status x%x, mbx status x%x\n",
17790				shdr_status, shdr_add_status, rc);
17791		status = -ENXIO;
17792	}
17793	mempool_free(mbox, cq->phba->mbox_mem_pool);
17794
17795list_remove:
17796	/* Remove cq from any list */
17797	list_del_init(&cq->list);
17798	return status;
17799}
17800
17801/**
17802 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
17803 * @phba: HBA structure that indicates port to destroy a queue on.
17804 * @mq: The queue structure associated with the queue to destroy.
17805 *
17806 * This function destroys a queue, as detailed in @mq by sending an mailbox
17807 * command, specific to the type of queue, to the HBA.
17808 *
17809 * The @mq struct is used to get the queue ID of the queue to destroy.
17810 *
17811 * On success this function will return a zero. If the queue destroy mailbox
17812 * command fails this function will return -ENXIO.
17813 **/
17814int
17815lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
17816{
17817	LPFC_MBOXQ_t *mbox;
17818	int rc, length, status = 0;
17819	uint32_t shdr_status, shdr_add_status;
17820	union lpfc_sli4_cfg_shdr *shdr;
17821
17822	/* sanity check on queue memory */
17823	if (!mq)
17824		return -ENODEV;
17825
17826	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17827		goto list_remove;
17828
17829	mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
17830	if (!mbox)
17831		return -ENOMEM;
17832	length = (sizeof(struct lpfc_mbx_mq_destroy) -
17833		  sizeof(struct lpfc_sli4_cfg_mhdr));
17834	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17835			 LPFC_MBOX_OPCODE_MQ_DESTROY,
17836			 length, LPFC_SLI4_MBX_EMBED);
17837	bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
17838	       mq->queue_id);
17839	mbox->vport = mq->phba->pport;
17840	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17841	rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
17842	/* The IOCTL status is embedded in the mailbox subheader. */
17843	shdr = (union lpfc_sli4_cfg_shdr *)
17844		&mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
17845	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17846	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17847	if (shdr_status || shdr_add_status || rc) {
17848		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17849				"2507 MQ_DESTROY mailbox failed with "
17850				"status x%x add_status x%x, mbx status x%x\n",
17851				shdr_status, shdr_add_status, rc);
17852		status = -ENXIO;
17853	}
17854	mempool_free(mbox, mq->phba->mbox_mem_pool);
17855
17856list_remove:
17857	/* Remove mq from any list */
17858	list_del_init(&mq->list);
17859	return status;
17860}
17861
17862/**
17863 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
17864 * @phba: HBA structure that indicates port to destroy a queue on.
17865 * @wq: The queue structure associated with the queue to destroy.
17866 *
17867 * This function destroys a queue, as detailed in @wq by sending an mailbox
17868 * command, specific to the type of queue, to the HBA.
17869 *
17870 * The @wq struct is used to get the queue ID of the queue to destroy.
17871 *
17872 * On success this function will return a zero. If the queue destroy mailbox
17873 * command fails this function will return -ENXIO.
17874 **/
17875int
17876lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
17877{
17878	LPFC_MBOXQ_t *mbox;
17879	int rc, length, status = 0;
17880	uint32_t shdr_status, shdr_add_status;
17881	union lpfc_sli4_cfg_shdr *shdr;
17882
17883	/* sanity check on queue memory */
17884	if (!wq)
17885		return -ENODEV;
17886
17887	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17888		goto list_remove;
17889
17890	mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
17891	if (!mbox)
17892		return -ENOMEM;
17893	length = (sizeof(struct lpfc_mbx_wq_destroy) -
17894		  sizeof(struct lpfc_sli4_cfg_mhdr));
17895	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17896			 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
17897			 length, LPFC_SLI4_MBX_EMBED);
17898	bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
17899	       wq->queue_id);
17900	mbox->vport = wq->phba->pport;
17901	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17902	rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
17903	shdr = (union lpfc_sli4_cfg_shdr *)
17904		&mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
17905	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17906	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17907	if (shdr_status || shdr_add_status || rc) {
17908		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17909				"2508 WQ_DESTROY mailbox failed with "
17910				"status x%x add_status x%x, mbx status x%x\n",
17911				shdr_status, shdr_add_status, rc);
17912		status = -ENXIO;
17913	}
17914	mempool_free(mbox, wq->phba->mbox_mem_pool);
17915
17916list_remove:
17917	/* Remove wq from any list */
17918	list_del_init(&wq->list);
17919	kfree(wq->pring);
17920	wq->pring = NULL;
17921	return status;
17922}
17923
17924/**
17925 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
17926 * @phba: HBA structure that indicates port to destroy a queue on.
17927 * @hrq: The queue structure associated with the queue to destroy.
17928 * @drq: The queue structure associated with the queue to destroy.
17929 *
17930 * This function destroys a queue, as detailed in @rq by sending an mailbox
17931 * command, specific to the type of queue, to the HBA.
17932 *
17933 * The @rq struct is used to get the queue ID of the queue to destroy.
17934 *
17935 * On success this function will return a zero. If the queue destroy mailbox
17936 * command fails this function will return -ENXIO.
17937 **/
17938int
17939lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17940		struct lpfc_queue *drq)
17941{
17942	LPFC_MBOXQ_t *mbox;
17943	int rc, length, status = 0;
17944	uint32_t shdr_status, shdr_add_status;
17945	union lpfc_sli4_cfg_shdr *shdr;
17946
17947	/* sanity check on queue memory */
17948	if (!hrq || !drq)
17949		return -ENODEV;
17950
17951	if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))
17952		goto list_remove;
17953
17954	mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
17955	if (!mbox)
17956		return -ENOMEM;
17957	length = (sizeof(struct lpfc_mbx_rq_destroy) -
17958		  sizeof(struct lpfc_sli4_cfg_mhdr));
17959	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17960			 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
17961			 length, LPFC_SLI4_MBX_EMBED);
17962	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17963	       hrq->queue_id);
17964	mbox->vport = hrq->phba->pport;
17965	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17966	rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
17967	/* The IOCTL status is embedded in the mailbox subheader. */
17968	shdr = (union lpfc_sli4_cfg_shdr *)
17969		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17970	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17971	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17972	if (shdr_status || shdr_add_status || rc) {
17973		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17974				"2509 RQ_DESTROY mailbox failed with "
17975				"status x%x add_status x%x, mbx status x%x\n",
17976				shdr_status, shdr_add_status, rc);
17977		mempool_free(mbox, hrq->phba->mbox_mem_pool);
17978		return -ENXIO;
17979	}
17980	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17981	       drq->queue_id);
17982	rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
17983	shdr = (union lpfc_sli4_cfg_shdr *)
17984		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17985	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17986	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17987	if (shdr_status || shdr_add_status || rc) {
17988		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
17989				"2510 RQ_DESTROY mailbox failed with "
17990				"status x%x add_status x%x, mbx status x%x\n",
17991				shdr_status, shdr_add_status, rc);
17992		status = -ENXIO;
17993	}
17994	mempool_free(mbox, hrq->phba->mbox_mem_pool);
17995
17996list_remove:
17997	list_del_init(&hrq->list);
17998	list_del_init(&drq->list);
17999	return status;
18000}
18001
18002/**
18003 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
18004 * @phba: The virtual port for which this call being executed.
18005 * @pdma_phys_addr0: Physical address of the 1st SGL page.
18006 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
18007 * @xritag: the xritag that ties this io to the SGL pages.
18008 *
18009 * This routine will post the sgl pages for the IO that has the xritag
18010 * that is in the iocbq structure. The xritag is assigned during iocbq
18011 * creation and persists for as long as the driver is loaded.
18012 * if the caller has fewer than 256 scatter gather segments to map then
18013 * pdma_phys_addr1 should be 0.
18014 * If the caller needs to map more than 256 scatter gather segment then
18015 * pdma_phys_addr1 should be a valid physical address.
18016 * physical address for SGLs must be 64 byte aligned.
18017 * If you are going to map 2 SGL's then the first one must have 256 entries
18018 * the second sgl can have between 1 and 256 entries.
18019 *
18020 * Return codes:
18021 * 	0 - Success
18022 * 	-ENXIO, -ENOMEM - Failure
18023 **/
18024int
18025lpfc_sli4_post_sgl(struct lpfc_hba *phba,
18026		dma_addr_t pdma_phys_addr0,
18027		dma_addr_t pdma_phys_addr1,
18028		uint16_t xritag)
18029{
18030	struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
18031	LPFC_MBOXQ_t *mbox;
18032	int rc;
18033	uint32_t shdr_status, shdr_add_status;
18034	uint32_t mbox_tmo;
18035	union lpfc_sli4_cfg_shdr *shdr;
18036
18037	if (xritag == NO_XRI) {
18038		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18039				"0364 Invalid param:\n");
18040		return -EINVAL;
18041	}
18042
18043	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18044	if (!mbox)
18045		return -ENOMEM;
18046
18047	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18048			LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
18049			sizeof(struct lpfc_mbx_post_sgl_pages) -
18050			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18051
18052	post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
18053				&mbox->u.mqe.un.post_sgl_pages;
18054	bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
18055	bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
18056
18057	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo	=
18058				cpu_to_le32(putPaddrLow(pdma_phys_addr0));
18059	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
18060				cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
18061
18062	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo	=
18063				cpu_to_le32(putPaddrLow(pdma_phys_addr1));
18064	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
18065				cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
18066	if (!phba->sli4_hba.intr_enable)
18067		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18068	else {
18069		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18070		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18071	}
18072	/* The IOCTL status is embedded in the mailbox subheader. */
18073	shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
18074	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18075	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18076	if (!phba->sli4_hba.intr_enable)
18077		mempool_free(mbox, phba->mbox_mem_pool);
18078	else if (rc != MBX_TIMEOUT)
18079		mempool_free(mbox, phba->mbox_mem_pool);
18080	if (shdr_status || shdr_add_status || rc) {
18081		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18082				"2511 POST_SGL mailbox failed with "
18083				"status x%x add_status x%x, mbx status x%x\n",
18084				shdr_status, shdr_add_status, rc);
18085	}
18086	return 0;
18087}
18088
18089/**
18090 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
18091 * @phba: pointer to lpfc hba data structure.
18092 *
18093 * This routine is invoked to post rpi header templates to the
18094 * HBA consistent with the SLI-4 interface spec.  This routine
18095 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18096 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
18097 *
18098 * Returns
18099 *	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
18100 *	LPFC_RPI_ALLOC_ERROR if no rpis are available.
18101 **/
18102static uint16_t
18103lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
18104{
18105	unsigned long xri;
18106
18107	/*
18108	 * Fetch the next logical xri.  Because this index is logical,
18109	 * the driver starts at 0 each time.
18110	 */
18111	spin_lock_irq(&phba->hbalock);
18112	xri = find_first_zero_bit(phba->sli4_hba.xri_bmask,
18113				 phba->sli4_hba.max_cfg_param.max_xri);
18114	if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
18115		spin_unlock_irq(&phba->hbalock);
18116		return NO_XRI;
18117	} else {
18118		set_bit(xri, phba->sli4_hba.xri_bmask);
18119		phba->sli4_hba.max_cfg_param.xri_used++;
18120	}
18121	spin_unlock_irq(&phba->hbalock);
18122	return xri;
18123}
18124
18125/**
18126 * __lpfc_sli4_free_xri - Release an xri for reuse.
18127 * @phba: pointer to lpfc hba data structure.
18128 * @xri: xri to release.
18129 *
18130 * This routine is invoked to release an xri to the pool of
18131 * available rpis maintained by the driver.
18132 **/
18133static void
18134__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18135{
18136	if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
18137		phba->sli4_hba.max_cfg_param.xri_used--;
18138	}
18139}
18140
18141/**
18142 * lpfc_sli4_free_xri - Release an xri for reuse.
18143 * @phba: pointer to lpfc hba data structure.
18144 * @xri: xri to release.
18145 *
18146 * This routine is invoked to release an xri to the pool of
18147 * available rpis maintained by the driver.
18148 **/
18149void
18150lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18151{
18152	spin_lock_irq(&phba->hbalock);
18153	__lpfc_sli4_free_xri(phba, xri);
18154	spin_unlock_irq(&phba->hbalock);
18155}
18156
18157/**
18158 * lpfc_sli4_next_xritag - Get an xritag for the io
18159 * @phba: Pointer to HBA context object.
18160 *
18161 * This function gets an xritag for the iocb. If there is no unused xritag
18162 * it will return 0xffff.
18163 * The function returns the allocated xritag if successful, else returns zero.
18164 * Zero is not a valid xritag.
18165 * The caller is not required to hold any lock.
18166 **/
18167uint16_t
18168lpfc_sli4_next_xritag(struct lpfc_hba *phba)
18169{
18170	uint16_t xri_index;
18171
18172	xri_index = lpfc_sli4_alloc_xri(phba);
18173	if (xri_index == NO_XRI)
18174		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
18175				"2004 Failed to allocate XRI.last XRITAG is %d"
18176				" Max XRI is %d, Used XRI is %d\n",
18177				xri_index,
18178				phba->sli4_hba.max_cfg_param.max_xri,
18179				phba->sli4_hba.max_cfg_param.xri_used);
18180	return xri_index;
18181}
18182
18183/**
18184 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
18185 * @phba: pointer to lpfc hba data structure.
18186 * @post_sgl_list: pointer to els sgl entry list.
18187 * @post_cnt: number of els sgl entries on the list.
18188 *
18189 * This routine is invoked to post a block of driver's sgl pages to the
18190 * HBA using non-embedded mailbox command. No Lock is held. This routine
18191 * is only called when the driver is loading and after all IO has been
18192 * stopped.
18193 **/
18194static int
18195lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
18196			    struct list_head *post_sgl_list,
18197			    int post_cnt)
18198{
18199	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
18200	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18201	struct sgl_page_pairs *sgl_pg_pairs;
18202	void *viraddr;
18203	LPFC_MBOXQ_t *mbox;
18204	uint32_t reqlen, alloclen, pg_pairs;
18205	uint32_t mbox_tmo;
18206	uint16_t xritag_start = 0;
18207	int rc = 0;
18208	uint32_t shdr_status, shdr_add_status;
18209	union lpfc_sli4_cfg_shdr *shdr;
18210
18211	reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
18212		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18213	if (reqlen > SLI4_PAGE_SIZE) {
18214		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18215				"2559 Block sgl registration required DMA "
18216				"size (%d) great than a page\n", reqlen);
18217		return -ENOMEM;
18218	}
18219
18220	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18221	if (!mbox)
18222		return -ENOMEM;
18223
18224	/* Allocate DMA memory and set up the non-embedded mailbox command */
18225	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18226			 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
18227			 LPFC_SLI4_MBX_NEMBED);
18228
18229	if (alloclen < reqlen) {
18230		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18231				"0285 Allocated DMA memory size (%d) is "
18232				"less than the requested DMA memory "
18233				"size (%d)\n", alloclen, reqlen);
18234		lpfc_sli4_mbox_cmd_free(phba, mbox);
18235		return -ENOMEM;
18236	}
18237	/* Set up the SGL pages in the non-embedded DMA pages */
18238	viraddr = mbox->sge_array->addr[0];
18239	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18240	sgl_pg_pairs = &sgl->sgl_pg_pairs;
18241
18242	pg_pairs = 0;
18243	list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
18244		/* Set up the sge entry */
18245		sgl_pg_pairs->sgl_pg0_addr_lo =
18246				cpu_to_le32(putPaddrLow(sglq_entry->phys));
18247		sgl_pg_pairs->sgl_pg0_addr_hi =
18248				cpu_to_le32(putPaddrHigh(sglq_entry->phys));
18249		sgl_pg_pairs->sgl_pg1_addr_lo =
18250				cpu_to_le32(putPaddrLow(0));
18251		sgl_pg_pairs->sgl_pg1_addr_hi =
18252				cpu_to_le32(putPaddrHigh(0));
18253
18254		/* Keep the first xritag on the list */
18255		if (pg_pairs == 0)
18256			xritag_start = sglq_entry->sli4_xritag;
18257		sgl_pg_pairs++;
18258		pg_pairs++;
18259	}
18260
18261	/* Complete initialization and perform endian conversion. */
18262	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18263	bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
18264	sgl->word0 = cpu_to_le32(sgl->word0);
18265
18266	if (!phba->sli4_hba.intr_enable)
18267		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18268	else {
18269		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18270		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18271	}
18272	shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
18273	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18274	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18275	if (!phba->sli4_hba.intr_enable)
18276		lpfc_sli4_mbox_cmd_free(phba, mbox);
18277	else if (rc != MBX_TIMEOUT)
18278		lpfc_sli4_mbox_cmd_free(phba, mbox);
18279	if (shdr_status || shdr_add_status || rc) {
18280		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18281				"2513 POST_SGL_BLOCK mailbox command failed "
18282				"status x%x add_status x%x mbx status x%x\n",
18283				shdr_status, shdr_add_status, rc);
18284		rc = -ENXIO;
18285	}
18286	return rc;
18287}
18288
18289/**
18290 * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
18291 * @phba: pointer to lpfc hba data structure.
18292 * @nblist: pointer to nvme buffer list.
18293 * @count: number of scsi buffers on the list.
18294 *
18295 * This routine is invoked to post a block of @count scsi sgl pages from a
18296 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
18297 * No Lock is held.
18298 *
18299 **/
18300static int
18301lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
18302			    int count)
18303{
18304	struct lpfc_io_buf *lpfc_ncmd;
18305	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18306	struct sgl_page_pairs *sgl_pg_pairs;
18307	void *viraddr;
18308	LPFC_MBOXQ_t *mbox;
18309	uint32_t reqlen, alloclen, pg_pairs;
18310	uint32_t mbox_tmo;
18311	uint16_t xritag_start = 0;
18312	int rc = 0;
18313	uint32_t shdr_status, shdr_add_status;
18314	dma_addr_t pdma_phys_bpl1;
18315	union lpfc_sli4_cfg_shdr *shdr;
18316
18317	/* Calculate the requested length of the dma memory */
18318	reqlen = count * sizeof(struct sgl_page_pairs) +
18319		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
18320	if (reqlen > SLI4_PAGE_SIZE) {
18321		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
18322				"6118 Block sgl registration required DMA "
18323				"size (%d) great than a page\n", reqlen);
18324		return -ENOMEM;
18325	}
18326	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18327	if (!mbox) {
18328		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18329				"6119 Failed to allocate mbox cmd memory\n");
18330		return -ENOMEM;
18331	}
18332
18333	/* Allocate DMA memory and set up the non-embedded mailbox command */
18334	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18335				    LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
18336				    reqlen, LPFC_SLI4_MBX_NEMBED);
18337
18338	if (alloclen < reqlen) {
18339		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18340				"6120 Allocated DMA memory size (%d) is "
18341				"less than the requested DMA memory "
18342				"size (%d)\n", alloclen, reqlen);
18343		lpfc_sli4_mbox_cmd_free(phba, mbox);
18344		return -ENOMEM;
18345	}
18346
18347	/* Get the first SGE entry from the non-embedded DMA memory */
18348	viraddr = mbox->sge_array->addr[0];
18349
18350	/* Set up the SGL pages in the non-embedded DMA pages */
18351	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18352	sgl_pg_pairs = &sgl->sgl_pg_pairs;
18353
18354	pg_pairs = 0;
18355	list_for_each_entry(lpfc_ncmd, nblist, list) {
18356		/* Set up the sge entry */
18357		sgl_pg_pairs->sgl_pg0_addr_lo =
18358			cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
18359		sgl_pg_pairs->sgl_pg0_addr_hi =
18360			cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
18361		if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
18362			pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
18363						SGL_PAGE_SIZE;
18364		else
18365			pdma_phys_bpl1 = 0;
18366		sgl_pg_pairs->sgl_pg1_addr_lo =
18367			cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
18368		sgl_pg_pairs->sgl_pg1_addr_hi =
18369			cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
18370		/* Keep the first xritag on the list */
18371		if (pg_pairs == 0)
18372			xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
18373		sgl_pg_pairs++;
18374		pg_pairs++;
18375	}
18376	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18377	bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
18378	/* Perform endian conversion if necessary */
18379	sgl->word0 = cpu_to_le32(sgl->word0);
18380
18381	if (!phba->sli4_hba.intr_enable) {
18382		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18383	} else {
18384		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
18385		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18386	}
18387	shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
18388	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18389	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18390	if (!phba->sli4_hba.intr_enable)
18391		lpfc_sli4_mbox_cmd_free(phba, mbox);
18392	else if (rc != MBX_TIMEOUT)
18393		lpfc_sli4_mbox_cmd_free(phba, mbox);
18394	if (shdr_status || shdr_add_status || rc) {
18395		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
18396				"6125 POST_SGL_BLOCK mailbox command failed "
18397				"status x%x add_status x%x mbx status x%x\n",
18398				shdr_status, shdr_add_status, rc);
18399		rc = -ENXIO;
18400	}
18401	return rc;
18402}
18403
18404/**
18405 * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
18406 * @phba: pointer to lpfc hba data structure.
18407 * @post_nblist: pointer to the nvme buffer list.
18408 * @sb_count: number of nvme buffers.
18409 *
18410 * This routine walks a list of nvme buffers that was passed in. It attempts
18411 * to construct blocks of nvme buffer sgls which contains contiguous xris and
18412 * uses the non-embedded SGL block post mailbox commands to post to the port.
18413 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
18414 * embedded SGL post mailbox command for posting. The @post_nblist passed in
18415 * must be local list, thus no lock is needed when manipulate the list.
18416 *
18417 * Returns: 0 = failure, non-zero number of successfully posted buffers.
18418 **/
18419int
18420lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
18421			   struct list_head *post_nblist, int sb_count)
18422{
18423	struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
18424	int status, sgl_size;
18425	int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
18426	dma_addr_t pdma_phys_sgl1;
18427	int last_xritag = NO_XRI;
18428	int cur_xritag;
18429	LIST_HEAD(prep_nblist);
18430	LIST_HEAD(blck_nblist);
18431	LIST_HEAD(nvme_nblist);
18432
18433	/* sanity check */
18434	if (sb_count <= 0)
18435		return -EINVAL;
18436
18437	sgl_size = phba->cfg_sg_dma_buf_size;
18438	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
18439		list_del_init(&lpfc_ncmd->list);
18440		block_cnt++;
18441		if ((last_xritag != NO_XRI) &&
18442		    (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
18443			/* a hole in xri block, form a sgl posting block */
18444			list_splice_init(&prep_nblist, &blck_nblist);
18445			post_cnt = block_cnt - 1;
18446			/* prepare list for next posting block */
18447			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18448			block_cnt = 1;
18449		} else {
18450			/* prepare list for next posting block */
18451			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18452			/* enough sgls for non-embed sgl mbox command */
18453			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
18454				list_splice_init(&prep_nblist, &blck_nblist);
18455				post_cnt = block_cnt;
18456				block_cnt = 0;
18457			}
18458		}
18459		num_posting++;
18460		last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18461
18462		/* end of repost sgl list condition for NVME buffers */
18463		if (num_posting == sb_count) {
18464			if (post_cnt == 0) {
18465				/* last sgl posting block */
18466				list_splice_init(&prep_nblist, &blck_nblist);
18467				post_cnt = block_cnt;
18468			} else if (block_cnt == 1) {
18469				/* last single sgl with non-contiguous xri */
18470				if (sgl_size > SGL_PAGE_SIZE)
18471					pdma_phys_sgl1 =
18472						lpfc_ncmd->dma_phys_sgl +
18473						SGL_PAGE_SIZE;
18474				else
18475					pdma_phys_sgl1 = 0;
18476				cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18477				status = lpfc_sli4_post_sgl(
18478						phba, lpfc_ncmd->dma_phys_sgl,
18479						pdma_phys_sgl1, cur_xritag);
18480				if (status) {
18481					/* Post error.  Buffer unavailable. */
18482					lpfc_ncmd->flags |=
18483						LPFC_SBUF_NOT_POSTED;
18484				} else {
18485					/* Post success. Bffer available. */
18486					lpfc_ncmd->flags &=
18487						~LPFC_SBUF_NOT_POSTED;
18488					lpfc_ncmd->status = IOSTAT_SUCCESS;
18489					num_posted++;
18490				}
18491				/* success, put on NVME buffer sgl list */
18492				list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18493			}
18494		}
18495
18496		/* continue until a nembed page worth of sgls */
18497		if (post_cnt == 0)
18498			continue;
18499
18500		/* post block of NVME buffer list sgls */
18501		status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
18502						     post_cnt);
18503
18504		/* don't reset xirtag due to hole in xri block */
18505		if (block_cnt == 0)
18506			last_xritag = NO_XRI;
18507
18508		/* reset NVME buffer post count for next round of posting */
18509		post_cnt = 0;
18510
18511		/* put posted NVME buffer-sgl posted on NVME buffer sgl list */
18512		while (!list_empty(&blck_nblist)) {
18513			list_remove_head(&blck_nblist, lpfc_ncmd,
18514					 struct lpfc_io_buf, list);
18515			if (status) {
18516				/* Post error.  Mark buffer unavailable. */
18517				lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
18518			} else {
18519				/* Post success, Mark buffer available. */
18520				lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
18521				lpfc_ncmd->status = IOSTAT_SUCCESS;
18522				num_posted++;
18523			}
18524			list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18525		}
18526	}
18527	/* Push NVME buffers with sgl posted to the available list */
18528	lpfc_io_buf_replenish(phba, &nvme_nblist);
18529
18530	return num_posted;
18531}
18532
18533/**
18534 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
18535 * @phba: pointer to lpfc_hba struct that the frame was received on
18536 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18537 *
18538 * This function checks the fields in the @fc_hdr to see if the FC frame is a
18539 * valid type of frame that the LPFC driver will handle. This function will
18540 * return a zero if the frame is a valid frame or a non zero value when the
18541 * frame does not pass the check.
18542 **/
18543static int
18544lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
18545{
18546	/*  make rctl_names static to save stack space */
18547	struct fc_vft_header *fc_vft_hdr;
18548	struct fc_app_header *fc_app_hdr;
18549	uint32_t *header = (uint32_t *) fc_hdr;
18550
18551#define FC_RCTL_MDS_DIAGS	0xF4
18552
18553	switch (fc_hdr->fh_r_ctl) {
18554	case FC_RCTL_DD_UNCAT:		/* uncategorized information */
18555	case FC_RCTL_DD_SOL_DATA:	/* solicited data */
18556	case FC_RCTL_DD_UNSOL_CTL:	/* unsolicited control */
18557	case FC_RCTL_DD_SOL_CTL:	/* solicited control or reply */
18558	case FC_RCTL_DD_UNSOL_DATA:	/* unsolicited data */
18559	case FC_RCTL_DD_DATA_DESC:	/* data descriptor */
18560	case FC_RCTL_DD_UNSOL_CMD:	/* unsolicited command */
18561	case FC_RCTL_DD_CMD_STATUS:	/* command status */
18562	case FC_RCTL_ELS_REQ:	/* extended link services request */
18563	case FC_RCTL_ELS_REP:	/* extended link services reply */
18564	case FC_RCTL_ELS4_REQ:	/* FC-4 ELS request */
18565	case FC_RCTL_ELS4_REP:	/* FC-4 ELS reply */
18566	case FC_RCTL_BA_ABTS: 	/* basic link service abort */
18567	case FC_RCTL_BA_RMC: 	/* remove connection */
18568	case FC_RCTL_BA_ACC:	/* basic accept */
18569	case FC_RCTL_BA_RJT:	/* basic reject */
18570	case FC_RCTL_BA_PRMT:
18571	case FC_RCTL_ACK_1:	/* acknowledge_1 */
18572	case FC_RCTL_ACK_0:	/* acknowledge_0 */
18573	case FC_RCTL_P_RJT:	/* port reject */
18574	case FC_RCTL_F_RJT:	/* fabric reject */
18575	case FC_RCTL_P_BSY:	/* port busy */
18576	case FC_RCTL_F_BSY:	/* fabric busy to data frame */
18577	case FC_RCTL_F_BSYL:	/* fabric busy to link control frame */
18578	case FC_RCTL_LCR:	/* link credit reset */
18579	case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
18580	case FC_RCTL_END:	/* end */
18581		break;
18582	case FC_RCTL_VFTH:	/* Virtual Fabric tagging Header */
18583		fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18584		fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
18585		return lpfc_fc_frame_check(phba, fc_hdr);
18586	case FC_RCTL_BA_NOP:	/* basic link service NOP */
18587	default:
18588		goto drop;
18589	}
18590
18591	switch (fc_hdr->fh_type) {
18592	case FC_TYPE_BLS:
18593	case FC_TYPE_ELS:
18594	case FC_TYPE_FCP:
18595	case FC_TYPE_CT:
18596	case FC_TYPE_NVME:
18597		break;
18598	case FC_TYPE_IP:
18599	case FC_TYPE_ILS:
18600	default:
18601		goto drop;
18602	}
18603
18604	if (unlikely(phba->link_flag == LS_LOOPBACK_MODE &&
18605				phba->cfg_vmid_app_header)) {
18606		/* Application header is 16B device header */
18607		if (fc_hdr->fh_df_ctl & LPFC_FC_16B_DEVICE_HEADER) {
18608			fc_app_hdr = (struct fc_app_header *) (fc_hdr + 1);
18609			if (be32_to_cpu(fc_app_hdr->src_app_id) !=
18610					LOOPBACK_SRC_APPID) {
18611				lpfc_printf_log(phba, KERN_WARNING,
18612						LOG_ELS | LOG_LIBDFC,
18613						"1932 Loopback src app id "
18614						"not matched, app_id:x%x\n",
18615						be32_to_cpu(fc_app_hdr->src_app_id));
18616
18617				goto drop;
18618			}
18619		} else {
18620			lpfc_printf_log(phba, KERN_WARNING,
18621					LOG_ELS | LOG_LIBDFC,
18622					"1933 Loopback df_ctl bit not set, "
18623					"df_ctl:x%x\n",
18624					fc_hdr->fh_df_ctl);
18625
18626			goto drop;
18627		}
18628	}
18629
18630	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
18631			"2538 Received frame rctl:x%x, type:x%x, "
18632			"frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
18633			fc_hdr->fh_r_ctl, fc_hdr->fh_type,
18634			be32_to_cpu(header[0]), be32_to_cpu(header[1]),
18635			be32_to_cpu(header[2]), be32_to_cpu(header[3]),
18636			be32_to_cpu(header[4]), be32_to_cpu(header[5]),
18637			be32_to_cpu(header[6]));
18638	return 0;
18639drop:
18640	lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
18641			"2539 Dropped frame rctl:x%x type:x%x\n",
18642			fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18643	return 1;
18644}
18645
18646/**
18647 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
18648 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18649 *
18650 * This function processes the FC header to retrieve the VFI from the VF
18651 * header, if one exists. This function will return the VFI if one exists
18652 * or 0 if no VSAN Header exists.
18653 **/
18654static uint32_t
18655lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
18656{
18657	struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18658
18659	if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
18660		return 0;
18661	return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
18662}
18663
18664/**
18665 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
18666 * @phba: Pointer to the HBA structure to search for the vport on
18667 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18668 * @fcfi: The FC Fabric ID that the frame came from
18669 * @did: Destination ID to match against
18670 *
18671 * This function searches the @phba for a vport that matches the content of the
18672 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
18673 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
18674 * returns the matching vport pointer or NULL if unable to match frame to a
18675 * vport.
18676 **/
18677static struct lpfc_vport *
18678lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
18679		       uint16_t fcfi, uint32_t did)
18680{
18681	struct lpfc_vport **vports;
18682	struct lpfc_vport *vport = NULL;
18683	int i;
18684
18685	if (did == Fabric_DID)
18686		return phba->pport;
18687	if (test_bit(FC_PT2PT, &phba->pport->fc_flag) &&
18688	    phba->link_state != LPFC_HBA_READY)
18689		return phba->pport;
18690
18691	vports = lpfc_create_vport_work_array(phba);
18692	if (vports != NULL) {
18693		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
18694			if (phba->fcf.fcfi == fcfi &&
18695			    vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
18696			    vports[i]->fc_myDID == did) {
18697				vport = vports[i];
18698				break;
18699			}
18700		}
18701	}
18702	lpfc_destroy_vport_work_array(phba, vports);
18703	return vport;
18704}
18705
18706/**
18707 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
18708 * @vport: The vport to work on.
18709 *
18710 * This function updates the receive sequence time stamp for this vport. The
18711 * receive sequence time stamp indicates the time that the last frame of the
18712 * the sequence that has been idle for the longest amount of time was received.
18713 * the driver uses this time stamp to indicate if any received sequences have
18714 * timed out.
18715 **/
18716static void
18717lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
18718{
18719	struct lpfc_dmabuf *h_buf;
18720	struct hbq_dmabuf *dmabuf = NULL;
18721
18722	/* get the oldest sequence on the rcv list */
18723	h_buf = list_get_first(&vport->rcv_buffer_list,
18724			       struct lpfc_dmabuf, list);
18725	if (!h_buf)
18726		return;
18727	dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18728	vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
18729}
18730
18731/**
18732 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
18733 * @vport: The vport that the received sequences were sent to.
18734 *
18735 * This function cleans up all outstanding received sequences. This is called
18736 * by the driver when a link event or user action invalidates all the received
18737 * sequences.
18738 **/
18739void
18740lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
18741{
18742	struct lpfc_dmabuf *h_buf, *hnext;
18743	struct lpfc_dmabuf *d_buf, *dnext;
18744	struct hbq_dmabuf *dmabuf = NULL;
18745
18746	/* start with the oldest sequence on the rcv list */
18747	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18748		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18749		list_del_init(&dmabuf->hbuf.list);
18750		list_for_each_entry_safe(d_buf, dnext,
18751					 &dmabuf->dbuf.list, list) {
18752			list_del_init(&d_buf->list);
18753			lpfc_in_buf_free(vport->phba, d_buf);
18754		}
18755		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18756	}
18757}
18758
18759/**
18760 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
18761 * @vport: The vport that the received sequences were sent to.
18762 *
18763 * This function determines whether any received sequences have timed out by
18764 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
18765 * indicates that there is at least one timed out sequence this routine will
18766 * go through the received sequences one at a time from most inactive to most
18767 * active to determine which ones need to be cleaned up. Once it has determined
18768 * that a sequence needs to be cleaned up it will simply free up the resources
18769 * without sending an abort.
18770 **/
18771void
18772lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
18773{
18774	struct lpfc_dmabuf *h_buf, *hnext;
18775	struct lpfc_dmabuf *d_buf, *dnext;
18776	struct hbq_dmabuf *dmabuf = NULL;
18777	unsigned long timeout;
18778	int abort_count = 0;
18779
18780	timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18781		   vport->rcv_buffer_time_stamp);
18782	if (list_empty(&vport->rcv_buffer_list) ||
18783	    time_before(jiffies, timeout))
18784		return;
18785	/* start with the oldest sequence on the rcv list */
18786	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18787		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18788		timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18789			   dmabuf->time_stamp);
18790		if (time_before(jiffies, timeout))
18791			break;
18792		abort_count++;
18793		list_del_init(&dmabuf->hbuf.list);
18794		list_for_each_entry_safe(d_buf, dnext,
18795					 &dmabuf->dbuf.list, list) {
18796			list_del_init(&d_buf->list);
18797			lpfc_in_buf_free(vport->phba, d_buf);
18798		}
18799		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18800	}
18801	if (abort_count)
18802		lpfc_update_rcv_time_stamp(vport);
18803}
18804
18805/**
18806 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
18807 * @vport: pointer to a vitural port
18808 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
18809 *
18810 * This function searches through the existing incomplete sequences that have
18811 * been sent to this @vport. If the frame matches one of the incomplete
18812 * sequences then the dbuf in the @dmabuf is added to the list of frames that
18813 * make up that sequence. If no sequence is found that matches this frame then
18814 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
18815 * This function returns a pointer to the first dmabuf in the sequence list that
18816 * the frame was linked to.
18817 **/
18818static struct hbq_dmabuf *
18819lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18820{
18821	struct fc_frame_header *new_hdr;
18822	struct fc_frame_header *temp_hdr;
18823	struct lpfc_dmabuf *d_buf;
18824	struct lpfc_dmabuf *h_buf;
18825	struct hbq_dmabuf *seq_dmabuf = NULL;
18826	struct hbq_dmabuf *temp_dmabuf = NULL;
18827	uint8_t	found = 0;
18828
18829	INIT_LIST_HEAD(&dmabuf->dbuf.list);
18830	dmabuf->time_stamp = jiffies;
18831	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18832
18833	/* Use the hdr_buf to find the sequence that this frame belongs to */
18834	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18835		temp_hdr = (struct fc_frame_header *)h_buf->virt;
18836		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18837		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18838		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18839			continue;
18840		/* found a pending sequence that matches this frame */
18841		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18842		break;
18843	}
18844	if (!seq_dmabuf) {
18845		/*
18846		 * This indicates first frame received for this sequence.
18847		 * Queue the buffer on the vport's rcv_buffer_list.
18848		 */
18849		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18850		lpfc_update_rcv_time_stamp(vport);
18851		return dmabuf;
18852	}
18853	temp_hdr = seq_dmabuf->hbuf.virt;
18854	if (be16_to_cpu(new_hdr->fh_seq_cnt) <
18855		be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18856		list_del_init(&seq_dmabuf->hbuf.list);
18857		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18858		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18859		lpfc_update_rcv_time_stamp(vport);
18860		return dmabuf;
18861	}
18862	/* move this sequence to the tail to indicate a young sequence */
18863	list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
18864	seq_dmabuf->time_stamp = jiffies;
18865	lpfc_update_rcv_time_stamp(vport);
18866	if (list_empty(&seq_dmabuf->dbuf.list)) {
18867		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18868		return seq_dmabuf;
18869	}
18870	/* find the correct place in the sequence to insert this frame */
18871	d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
18872	while (!found) {
18873		temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18874		temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
18875		/*
18876		 * If the frame's sequence count is greater than the frame on
18877		 * the list then insert the frame right after this frame
18878		 */
18879		if (be16_to_cpu(new_hdr->fh_seq_cnt) >
18880			be16_to_cpu(temp_hdr->fh_seq_cnt)) {
18881			list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
18882			found = 1;
18883			break;
18884		}
18885
18886		if (&d_buf->list == &seq_dmabuf->dbuf.list)
18887			break;
18888		d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
18889	}
18890
18891	if (found)
18892		return seq_dmabuf;
18893	return NULL;
18894}
18895
18896/**
18897 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
18898 * @vport: pointer to a vitural port
18899 * @dmabuf: pointer to a dmabuf that describes the FC sequence
18900 *
18901 * This function tries to abort from the partially assembed sequence, described
18902 * by the information from basic abbort @dmabuf. It checks to see whether such
18903 * partially assembled sequence held by the driver. If so, it shall free up all
18904 * the frames from the partially assembled sequence.
18905 *
18906 * Return
18907 * true  -- if there is matching partially assembled sequence present and all
18908 *          the frames freed with the sequence;
18909 * false -- if there is no matching partially assembled sequence present so
18910 *          nothing got aborted in the lower layer driver
18911 **/
18912static bool
18913lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
18914			    struct hbq_dmabuf *dmabuf)
18915{
18916	struct fc_frame_header *new_hdr;
18917	struct fc_frame_header *temp_hdr;
18918	struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
18919	struct hbq_dmabuf *seq_dmabuf = NULL;
18920
18921	/* Use the hdr_buf to find the sequence that matches this frame */
18922	INIT_LIST_HEAD(&dmabuf->dbuf.list);
18923	INIT_LIST_HEAD(&dmabuf->hbuf.list);
18924	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18925	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18926		temp_hdr = (struct fc_frame_header *)h_buf->virt;
18927		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18928		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18929		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18930			continue;
18931		/* found a pending sequence that matches this frame */
18932		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18933		break;
18934	}
18935
18936	/* Free up all the frames from the partially assembled sequence */
18937	if (seq_dmabuf) {
18938		list_for_each_entry_safe(d_buf, n_buf,
18939					 &seq_dmabuf->dbuf.list, list) {
18940			list_del_init(&d_buf->list);
18941			lpfc_in_buf_free(vport->phba, d_buf);
18942		}
18943		return true;
18944	}
18945	return false;
18946}
18947
18948/**
18949 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
18950 * @vport: pointer to a vitural port
18951 * @dmabuf: pointer to a dmabuf that describes the FC sequence
18952 *
18953 * This function tries to abort from the assembed sequence from upper level
18954 * protocol, described by the information from basic abbort @dmabuf. It
18955 * checks to see whether such pending context exists at upper level protocol.
18956 * If so, it shall clean up the pending context.
18957 *
18958 * Return
18959 * true  -- if there is matching pending context of the sequence cleaned
18960 *          at ulp;
18961 * false -- if there is no matching pending context of the sequence present
18962 *          at ulp.
18963 **/
18964static bool
18965lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18966{
18967	struct lpfc_hba *phba = vport->phba;
18968	int handled;
18969
18970	/* Accepting abort at ulp with SLI4 only */
18971	if (phba->sli_rev < LPFC_SLI_REV4)
18972		return false;
18973
18974	/* Register all caring upper level protocols to attend abort */
18975	handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
18976	if (handled)
18977		return true;
18978
18979	return false;
18980}
18981
18982/**
18983 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
18984 * @phba: Pointer to HBA context object.
18985 * @cmd_iocbq: pointer to the command iocbq structure.
18986 * @rsp_iocbq: pointer to the response iocbq structure.
18987 *
18988 * This function handles the sequence abort response iocb command complete
18989 * event. It properly releases the memory allocated to the sequence abort
18990 * accept iocb.
18991 **/
18992static void
18993lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
18994			     struct lpfc_iocbq *cmd_iocbq,
18995			     struct lpfc_iocbq *rsp_iocbq)
18996{
18997	if (cmd_iocbq) {
18998		lpfc_nlp_put(cmd_iocbq->ndlp);
18999		lpfc_sli_release_iocbq(phba, cmd_iocbq);
19000	}
19001
19002	/* Failure means BLS ABORT RSP did not get delivered to remote node*/
19003	if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
19004		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19005			"3154 BLS ABORT RSP failed, data:  x%x/x%x\n",
19006			get_job_ulpstatus(phba, rsp_iocbq),
19007			get_job_word4(phba, rsp_iocbq));
19008}
19009
19010/**
19011 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
19012 * @phba: Pointer to HBA context object.
19013 * @xri: xri id in transaction.
19014 *
19015 * This function validates the xri maps to the known range of XRIs allocated an
19016 * used by the driver.
19017 **/
19018uint16_t
19019lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
19020		      uint16_t xri)
19021{
19022	uint16_t i;
19023
19024	for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
19025		if (xri == phba->sli4_hba.xri_ids[i])
19026			return i;
19027	}
19028	return NO_XRI;
19029}
19030
19031/**
19032 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
19033 * @vport: pointer to a virtual port.
19034 * @fc_hdr: pointer to a FC frame header.
19035 * @aborted: was the partially assembled receive sequence successfully aborted
19036 *
19037 * This function sends a basic response to a previous unsol sequence abort
19038 * event after aborting the sequence handling.
19039 **/
19040void
19041lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
19042			struct fc_frame_header *fc_hdr, bool aborted)
19043{
19044	struct lpfc_hba *phba = vport->phba;
19045	struct lpfc_iocbq *ctiocb = NULL;
19046	struct lpfc_nodelist *ndlp;
19047	uint16_t oxid, rxid, xri, lxri;
19048	uint32_t sid, fctl;
19049	union lpfc_wqe128 *icmd;
19050	int rc;
19051
19052	if (!lpfc_is_link_up(phba))
19053		return;
19054
19055	sid = sli4_sid_from_fc_hdr(fc_hdr);
19056	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
19057	rxid = be16_to_cpu(fc_hdr->fh_rx_id);
19058
19059	ndlp = lpfc_findnode_did(vport, sid);
19060	if (!ndlp) {
19061		ndlp = lpfc_nlp_init(vport, sid);
19062		if (!ndlp) {
19063			lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
19064					 "1268 Failed to allocate ndlp for "
19065					 "oxid:x%x SID:x%x\n", oxid, sid);
19066			return;
19067		}
19068		/* Put ndlp onto vport node list */
19069		lpfc_enqueue_node(vport, ndlp);
19070	}
19071
19072	/* Allocate buffer for rsp iocb */
19073	ctiocb = lpfc_sli_get_iocbq(phba);
19074	if (!ctiocb)
19075		return;
19076
19077	icmd = &ctiocb->wqe;
19078
19079	/* Extract the F_CTL field from FC_HDR */
19080	fctl = sli4_fctl_from_fc_hdr(fc_hdr);
19081
19082	ctiocb->ndlp = lpfc_nlp_get(ndlp);
19083	if (!ctiocb->ndlp) {
19084		lpfc_sli_release_iocbq(phba, ctiocb);
19085		return;
19086	}
19087
19088	ctiocb->vport = vport;
19089	ctiocb->cmd_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
19090	ctiocb->sli4_lxritag = NO_XRI;
19091	ctiocb->sli4_xritag = NO_XRI;
19092	ctiocb->abort_rctl = FC_RCTL_BA_ACC;
19093
19094	if (fctl & FC_FC_EX_CTX)
19095		/* Exchange responder sent the abort so we
19096		 * own the oxid.
19097		 */
19098		xri = oxid;
19099	else
19100		xri = rxid;
19101	lxri = lpfc_sli4_xri_inrange(phba, xri);
19102	if (lxri != NO_XRI)
19103		lpfc_set_rrq_active(phba, ndlp, lxri,
19104			(xri == oxid) ? rxid : oxid, 0);
19105	/* For BA_ABTS from exchange responder, if the logical xri with
19106	 * the oxid maps to the FCP XRI range, the port no longer has
19107	 * that exchange context, send a BLS_RJT. Override the IOCB for
19108	 * a BA_RJT.
19109	 */
19110	if ((fctl & FC_FC_EX_CTX) &&
19111	    (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
19112		ctiocb->abort_rctl = FC_RCTL_BA_RJT;
19113		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);
19114		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,
19115		       FC_BA_RJT_INV_XID);
19116		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,
19117		       FC_BA_RJT_UNABLE);
19118	}
19119
19120	/* If BA_ABTS failed to abort a partially assembled receive sequence,
19121	 * the driver no longer has that exchange, send a BLS_RJT. Override
19122	 * the IOCB for a BA_RJT.
19123	 */
19124	if (aborted == false) {
19125		ctiocb->abort_rctl = FC_RCTL_BA_RJT;
19126		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);
19127		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,
19128		       FC_BA_RJT_INV_XID);
19129		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,
19130		       FC_BA_RJT_UNABLE);
19131	}
19132
19133	if (fctl & FC_FC_EX_CTX) {
19134		/* ABTS sent by responder to CT exchange, construction
19135		 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
19136		 * field and RX_ID from ABTS for RX_ID field.
19137		 */
19138		ctiocb->abort_bls = LPFC_ABTS_UNSOL_RSP;
19139		bf_set(xmit_bls_rsp64_rxid, &icmd->xmit_bls_rsp, rxid);
19140	} else {
19141		/* ABTS sent by initiator to CT exchange, construction
19142		 * of BA_ACC will need to allocate a new XRI as for the
19143		 * XRI_TAG field.
19144		 */
19145		ctiocb->abort_bls = LPFC_ABTS_UNSOL_INT;
19146	}
19147
19148	/* OX_ID is invariable to who sent ABTS to CT exchange */
19149	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, oxid);
19150	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, rxid);
19151
19152	/* Use CT=VPI */
19153	bf_set(wqe_els_did, &icmd->xmit_bls_rsp.wqe_dest,
19154	       ndlp->nlp_DID);
19155	bf_set(xmit_bls_rsp64_temprpi, &icmd->xmit_bls_rsp,
19156	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
19157	bf_set(wqe_cmnd, &icmd->generic.wqe_com, CMD_XMIT_BLS_RSP64_CX);
19158
19159	/* Xmit CT abts response on exchange <xid> */
19160	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
19161			 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
19162			 ctiocb->abort_rctl, oxid, phba->link_state);
19163
19164	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
19165	if (rc == IOCB_ERROR) {
19166		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
19167				 "2925 Failed to issue CT ABTS RSP x%x on "
19168				 "xri x%x, Data x%x\n",
19169				 ctiocb->abort_rctl, oxid,
19170				 phba->link_state);
19171		lpfc_nlp_put(ndlp);
19172		ctiocb->ndlp = NULL;
19173		lpfc_sli_release_iocbq(phba, ctiocb);
19174	}
19175
19176	/* if only usage of this nodelist is BLS response, release initial ref
19177	 * to free ndlp when transmit completes
19178	 */
19179	if (ndlp->nlp_state == NLP_STE_UNUSED_NODE &&
19180	    !test_bit(NLP_DROPPED, &ndlp->nlp_flag) &&
19181	    !(ndlp->fc4_xpt_flags & (NVME_XPT_REGD | SCSI_XPT_REGD))) {
19182		set_bit(NLP_DROPPED, &ndlp->nlp_flag);
19183		lpfc_nlp_put(ndlp);
19184	}
19185}
19186
19187/**
19188 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
19189 * @vport: Pointer to the vport on which this sequence was received
19190 * @dmabuf: pointer to a dmabuf that describes the FC sequence
19191 *
19192 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
19193 * receive sequence is only partially assembed by the driver, it shall abort
19194 * the partially assembled frames for the sequence. Otherwise, if the
19195 * unsolicited receive sequence has been completely assembled and passed to
19196 * the Upper Layer Protocol (ULP), it then mark the per oxid status for the
19197 * unsolicited sequence has been aborted. After that, it will issue a basic
19198 * accept to accept the abort.
19199 **/
19200static void
19201lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
19202			     struct hbq_dmabuf *dmabuf)
19203{
19204	struct lpfc_hba *phba = vport->phba;
19205	struct fc_frame_header fc_hdr;
19206	uint32_t fctl;
19207	bool aborted;
19208
19209	/* Make a copy of fc_hdr before the dmabuf being released */
19210	memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
19211	fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
19212
19213	if (fctl & FC_FC_EX_CTX) {
19214		/* ABTS by responder to exchange, no cleanup needed */
19215		aborted = true;
19216	} else {
19217		/* ABTS by initiator to exchange, need to do cleanup */
19218		aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
19219		if (aborted == false)
19220			aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
19221	}
19222	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19223
19224	if (phba->nvmet_support) {
19225		lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
19226		return;
19227	}
19228
19229	/* Respond with BA_ACC or BA_RJT accordingly */
19230	lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
19231}
19232
19233/**
19234 * lpfc_seq_complete - Indicates if a sequence is complete
19235 * @dmabuf: pointer to a dmabuf that describes the FC sequence
19236 *
19237 * This function checks the sequence, starting with the frame described by
19238 * @dmabuf, to see if all the frames associated with this sequence are present.
19239 * the frames associated with this sequence are linked to the @dmabuf using the
19240 * dbuf list. This function looks for two major things. 1) That the first frame
19241 * has a sequence count of zero. 2) There is a frame with last frame of sequence
19242 * set. 3) That there are no holes in the sequence count. The function will
19243 * return 1 when the sequence is complete, otherwise it will return 0.
19244 **/
19245static int
19246lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
19247{
19248	struct fc_frame_header *hdr;
19249	struct lpfc_dmabuf *d_buf;
19250	struct hbq_dmabuf *seq_dmabuf;
19251	uint32_t fctl;
19252	int seq_count = 0;
19253
19254	hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19255	/* make sure first fame of sequence has a sequence count of zero */
19256	if (hdr->fh_seq_cnt != seq_count)
19257		return 0;
19258	fctl = (hdr->fh_f_ctl[0] << 16 |
19259		hdr->fh_f_ctl[1] << 8 |
19260		hdr->fh_f_ctl[2]);
19261	/* If last frame of sequence we can return success. */
19262	if (fctl & FC_FC_END_SEQ)
19263		return 1;
19264	list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
19265		seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19266		hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19267		/* If there is a hole in the sequence count then fail. */
19268		if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
19269			return 0;
19270		fctl = (hdr->fh_f_ctl[0] << 16 |
19271			hdr->fh_f_ctl[1] << 8 |
19272			hdr->fh_f_ctl[2]);
19273		/* If last frame of sequence we can return success. */
19274		if (fctl & FC_FC_END_SEQ)
19275			return 1;
19276	}
19277	return 0;
19278}
19279
19280/**
19281 * lpfc_prep_seq - Prep sequence for ULP processing
19282 * @vport: Pointer to the vport on which this sequence was received
19283 * @seq_dmabuf: pointer to a dmabuf that describes the FC sequence
19284 *
19285 * This function takes a sequence, described by a list of frames, and creates
19286 * a list of iocbq structures to describe the sequence. This iocbq list will be
19287 * used to issue to the generic unsolicited sequence handler. This routine
19288 * returns a pointer to the first iocbq in the list. If the function is unable
19289 * to allocate an iocbq then it throw out the received frames that were not
19290 * able to be described and return a pointer to the first iocbq. If unable to
19291 * allocate any iocbqs (including the first) this function will return NULL.
19292 **/
19293static struct lpfc_iocbq *
19294lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
19295{
19296	struct hbq_dmabuf *hbq_buf;
19297	struct lpfc_dmabuf *d_buf, *n_buf;
19298	struct lpfc_iocbq *first_iocbq, *iocbq;
19299	struct fc_frame_header *fc_hdr;
19300	uint32_t sid;
19301	uint32_t len, tot_len;
19302
19303	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19304	/* remove from receive buffer list */
19305	list_del_init(&seq_dmabuf->hbuf.list);
19306	lpfc_update_rcv_time_stamp(vport);
19307	/* get the Remote Port's SID */
19308	sid = sli4_sid_from_fc_hdr(fc_hdr);
19309	tot_len = 0;
19310	/* Get an iocbq struct to fill in. */
19311	first_iocbq = lpfc_sli_get_iocbq(vport->phba);
19312	if (first_iocbq) {
19313		/* Initialize the first IOCB. */
19314		first_iocbq->wcqe_cmpl.total_data_placed = 0;
19315		bf_set(lpfc_wcqe_c_status, &first_iocbq->wcqe_cmpl,
19316		       IOSTAT_SUCCESS);
19317		first_iocbq->vport = vport;
19318
19319		/* Check FC Header to see what TYPE of frame we are rcv'ing */
19320		if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
19321			bf_set(els_rsp64_sid, &first_iocbq->wqe.xmit_els_rsp,
19322			       sli4_did_from_fc_hdr(fc_hdr));
19323		}
19324
19325		bf_set(wqe_ctxt_tag, &first_iocbq->wqe.xmit_els_rsp.wqe_com,
19326		       NO_XRI);
19327		bf_set(wqe_rcvoxid, &first_iocbq->wqe.xmit_els_rsp.wqe_com,
19328		       be16_to_cpu(fc_hdr->fh_ox_id));
19329
19330		/* put the first buffer into the first iocb */
19331		tot_len = bf_get(lpfc_rcqe_length,
19332				 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
19333
19334		first_iocbq->cmd_dmabuf = &seq_dmabuf->dbuf;
19335		first_iocbq->bpl_dmabuf = NULL;
19336		/* Keep track of the BDE count */
19337		first_iocbq->wcqe_cmpl.word3 = 1;
19338
19339		if (tot_len > LPFC_DATA_BUF_SIZE)
19340			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize =
19341				LPFC_DATA_BUF_SIZE;
19342		else
19343			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize = tot_len;
19344
19345		first_iocbq->wcqe_cmpl.total_data_placed = tot_len;
19346		bf_set(wqe_els_did, &first_iocbq->wqe.xmit_els_rsp.wqe_dest,
19347		       sid);
19348	}
19349	iocbq = first_iocbq;
19350	/*
19351	 * Each IOCBq can have two Buffers assigned, so go through the list
19352	 * of buffers for this sequence and save two buffers in each IOCBq
19353	 */
19354	list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
19355		if (!iocbq) {
19356			lpfc_in_buf_free(vport->phba, d_buf);
19357			continue;
19358		}
19359		if (!iocbq->bpl_dmabuf) {
19360			iocbq->bpl_dmabuf = d_buf;
19361			iocbq->wcqe_cmpl.word3++;
19362			/* We need to get the size out of the right CQE */
19363			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19364			len = bf_get(lpfc_rcqe_length,
19365				       &hbq_buf->cq_event.cqe.rcqe_cmpl);
19366			iocbq->unsol_rcv_len = len;
19367			iocbq->wcqe_cmpl.total_data_placed += len;
19368			tot_len += len;
19369		} else {
19370			iocbq = lpfc_sli_get_iocbq(vport->phba);
19371			if (!iocbq) {
19372				if (first_iocbq) {
19373					bf_set(lpfc_wcqe_c_status,
19374					       &first_iocbq->wcqe_cmpl,
19375					       IOSTAT_SUCCESS);
19376					first_iocbq->wcqe_cmpl.parameter =
19377						IOERR_NO_RESOURCES;
19378				}
19379				lpfc_in_buf_free(vport->phba, d_buf);
19380				continue;
19381			}
19382			/* We need to get the size out of the right CQE */
19383			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19384			len = bf_get(lpfc_rcqe_length,
19385				       &hbq_buf->cq_event.cqe.rcqe_cmpl);
19386			iocbq->cmd_dmabuf = d_buf;
19387			iocbq->bpl_dmabuf = NULL;
19388			iocbq->wcqe_cmpl.word3 = 1;
19389
19390			if (len > LPFC_DATA_BUF_SIZE)
19391				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =
19392					LPFC_DATA_BUF_SIZE;
19393			else
19394				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =
19395					len;
19396
19397			tot_len += len;
19398			iocbq->wcqe_cmpl.total_data_placed = tot_len;
19399			bf_set(wqe_els_did, &iocbq->wqe.xmit_els_rsp.wqe_dest,
19400			       sid);
19401			list_add_tail(&iocbq->list, &first_iocbq->list);
19402		}
19403	}
19404	/* Free the sequence's header buffer */
19405	if (!first_iocbq)
19406		lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
19407
19408	return first_iocbq;
19409}
19410
19411static void
19412lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
19413			  struct hbq_dmabuf *seq_dmabuf)
19414{
19415	struct fc_frame_header *fc_hdr;
19416	struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
19417	struct lpfc_hba *phba = vport->phba;
19418
19419	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19420	iocbq = lpfc_prep_seq(vport, seq_dmabuf);
19421	if (!iocbq) {
19422		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19423				"2707 Ring %d handler: Failed to allocate "
19424				"iocb Rctl x%x Type x%x received\n",
19425				LPFC_ELS_RING,
19426				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19427		return;
19428	}
19429	if (!lpfc_complete_unsol_iocb(phba,
19430				      phba->sli4_hba.els_wq->pring,
19431				      iocbq, fc_hdr->fh_r_ctl,
19432				      fc_hdr->fh_type)) {
19433		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19434				"2540 Ring %d handler: unexpected Rctl "
19435				"x%x Type x%x received\n",
19436				LPFC_ELS_RING,
19437				fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19438		lpfc_in_buf_free(phba, &seq_dmabuf->dbuf);
19439	}
19440
19441	/* Free iocb created in lpfc_prep_seq */
19442	list_for_each_entry_safe(curr_iocb, next_iocb,
19443				 &iocbq->list, list) {
19444		list_del_init(&curr_iocb->list);
19445		lpfc_sli_release_iocbq(phba, curr_iocb);
19446	}
19447	lpfc_sli_release_iocbq(phba, iocbq);
19448}
19449
19450static void
19451lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
19452			    struct lpfc_iocbq *rspiocb)
19453{
19454	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;
19455
19456	if (pcmd && pcmd->virt)
19457		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19458	kfree(pcmd);
19459	lpfc_sli_release_iocbq(phba, cmdiocb);
19460	lpfc_drain_txq(phba);
19461}
19462
19463static void
19464lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
19465			      struct hbq_dmabuf *dmabuf)
19466{
19467	struct fc_frame_header *fc_hdr;
19468	struct lpfc_hba *phba = vport->phba;
19469	struct lpfc_iocbq *iocbq = NULL;
19470	union  lpfc_wqe128 *pwqe;
19471	struct lpfc_dmabuf *pcmd = NULL;
19472	uint32_t frame_len;
19473	int rc;
19474	unsigned long iflags;
19475
19476	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19477	frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
19478
19479	/* Send the received frame back */
19480	iocbq = lpfc_sli_get_iocbq(phba);
19481	if (!iocbq) {
19482		/* Queue cq event and wakeup worker thread to process it */
19483		spin_lock_irqsave(&phba->hbalock, iflags);
19484		list_add_tail(&dmabuf->cq_event.list,
19485			      &phba->sli4_hba.sp_queue_event);
19486		spin_unlock_irqrestore(&phba->hbalock, iflags);
19487		set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);
19488		lpfc_worker_wake_up(phba);
19489		return;
19490	}
19491
19492	/* Allocate buffer for command payload */
19493	pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
19494	if (pcmd)
19495		pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
19496					    &pcmd->phys);
19497	if (!pcmd || !pcmd->virt)
19498		goto exit;
19499
19500	INIT_LIST_HEAD(&pcmd->list);
19501
19502	/* copyin the payload */
19503	memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
19504
19505	iocbq->cmd_dmabuf = pcmd;
19506	iocbq->vport = vport;
19507	iocbq->cmd_flag &= ~LPFC_FIP_ELS_ID_MASK;
19508	iocbq->cmd_flag |= LPFC_USE_FCPWQIDX;
19509	iocbq->num_bdes = 0;
19510
19511	pwqe = &iocbq->wqe;
19512	/* fill in BDE's for command */
19513	pwqe->gen_req.bde.addrHigh = putPaddrHigh(pcmd->phys);
19514	pwqe->gen_req.bde.addrLow = putPaddrLow(pcmd->phys);
19515	pwqe->gen_req.bde.tus.f.bdeSize = frame_len;
19516	pwqe->gen_req.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
19517
19518	pwqe->send_frame.frame_len = frame_len;
19519	pwqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((__be32 *)fc_hdr));
19520	pwqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((__be32 *)fc_hdr + 1));
19521	pwqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((__be32 *)fc_hdr + 2));
19522	pwqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((__be32 *)fc_hdr + 3));
19523	pwqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((__be32 *)fc_hdr + 4));
19524	pwqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((__be32 *)fc_hdr + 5));
19525
19526	pwqe->generic.wqe_com.word7 = 0;
19527	pwqe->generic.wqe_com.word10 = 0;
19528
19529	bf_set(wqe_cmnd, &pwqe->generic.wqe_com, CMD_SEND_FRAME);
19530	bf_set(wqe_sof, &pwqe->generic.wqe_com, 0x2E); /* SOF byte */
19531	bf_set(wqe_eof, &pwqe->generic.wqe_com, 0x41); /* EOF byte */
19532	bf_set(wqe_lenloc, &pwqe->generic.wqe_com, 1);
19533	bf_set(wqe_xbl, &pwqe->generic.wqe_com, 1);
19534	bf_set(wqe_dbde, &pwqe->generic.wqe_com, 1);
19535	bf_set(wqe_xc, &pwqe->generic.wqe_com, 1);
19536	bf_set(wqe_cmd_type, &pwqe->generic.wqe_com, 0xA);
19537	bf_set(wqe_cqid, &pwqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
19538	bf_set(wqe_xri_tag, &pwqe->generic.wqe_com, iocbq->sli4_xritag);
19539	bf_set(wqe_reqtag, &pwqe->generic.wqe_com, iocbq->iotag);
19540	bf_set(wqe_class, &pwqe->generic.wqe_com, CLASS3);
19541	pwqe->generic.wqe_com.abort_tag = iocbq->iotag;
19542
19543	iocbq->cmd_cmpl = lpfc_sli4_mds_loopback_cmpl;
19544
19545	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
19546	if (rc == IOCB_ERROR)
19547		goto exit;
19548
19549	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19550	return;
19551
19552exit:
19553	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
19554			"2023 Unable to process MDS loopback frame\n");
19555	if (pcmd && pcmd->virt)
19556		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
19557	kfree(pcmd);
19558	if (iocbq)
19559		lpfc_sli_release_iocbq(phba, iocbq);
19560	lpfc_in_buf_free(phba, &dmabuf->dbuf);
19561}
19562
19563/**
19564 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
19565 * @phba: Pointer to HBA context object.
19566 * @dmabuf: Pointer to a dmabuf that describes the FC sequence.
19567 *
19568 * This function is called with no lock held. This function processes all
19569 * the received buffers and gives it to upper layers when a received buffer
19570 * indicates that it is the final frame in the sequence. The interrupt
19571 * service routine processes received buffers at interrupt contexts.
19572 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
19573 * appropriate receive function when the final frame in a sequence is received.
19574 **/
19575void
19576lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
19577				 struct hbq_dmabuf *dmabuf)
19578{
19579	struct hbq_dmabuf *seq_dmabuf;
19580	struct fc_frame_header *fc_hdr;
19581	struct lpfc_vport *vport;
19582	uint32_t fcfi;
19583	uint32_t did;
19584
19585	/* Process each received buffer */
19586	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19587
19588	if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
19589	    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
19590		vport = phba->pport;
19591		/* Handle MDS Loopback frames */
19592		if  (!test_bit(FC_UNLOADING, &phba->pport->load_flag))
19593			lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19594		else
19595			lpfc_in_buf_free(phba, &dmabuf->dbuf);
19596		return;
19597	}
19598
19599	/* check to see if this a valid type of frame */
19600	if (lpfc_fc_frame_check(phba, fc_hdr)) {
19601		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19602		return;
19603	}
19604
19605	if ((bf_get(lpfc_cqe_code,
19606		    &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
19607		fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
19608			      &dmabuf->cq_event.cqe.rcqe_cmpl);
19609	else
19610		fcfi = bf_get(lpfc_rcqe_fcf_id,
19611			      &dmabuf->cq_event.cqe.rcqe_cmpl);
19612
19613	if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
19614		vport = phba->pport;
19615		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
19616				"2023 MDS Loopback %d bytes\n",
19617				bf_get(lpfc_rcqe_length,
19618				       &dmabuf->cq_event.cqe.rcqe_cmpl));
19619		/* Handle MDS Loopback frames */
19620		lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19621		return;
19622	}
19623
19624	/* d_id this frame is directed to */
19625	did = sli4_did_from_fc_hdr(fc_hdr);
19626
19627	vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
19628	if (!vport) {
19629		/* throw out the frame */
19630		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19631		return;
19632	}
19633
19634	/* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
19635	if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
19636		(did != Fabric_DID)) {
19637		/*
19638		 * Throw out the frame if we are not pt2pt.
19639		 * The pt2pt protocol allows for discovery frames
19640		 * to be received without a registered VPI.
19641		 */
19642		if (!test_bit(FC_PT2PT, &vport->fc_flag) ||
19643		    phba->link_state == LPFC_HBA_READY) {
19644			lpfc_in_buf_free(phba, &dmabuf->dbuf);
19645			return;
19646		}
19647	}
19648
19649	/* Handle the basic abort sequence (BA_ABTS) event */
19650	if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
19651		lpfc_sli4_handle_unsol_abort(vport, dmabuf);
19652		return;
19653	}
19654
19655	/* Link this frame */
19656	seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
19657	if (!seq_dmabuf) {
19658		/* unable to add frame to vport - throw it out */
19659		lpfc_in_buf_free(phba, &dmabuf->dbuf);
19660		return;
19661	}
19662	/* If not last frame in sequence continue processing frames. */
19663	if (!lpfc_seq_complete(seq_dmabuf))
19664		return;
19665
19666	/* Send the complete sequence to the upper layer protocol */
19667	lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
19668}
19669
19670/**
19671 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
19672 * @phba: pointer to lpfc hba data structure.
19673 *
19674 * This routine is invoked to post rpi header templates to the
19675 * HBA consistent with the SLI-4 interface spec.  This routine
19676 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19677 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19678 *
19679 * This routine does not require any locks.  It's usage is expected
19680 * to be driver load or reset recovery when the driver is
19681 * sequential.
19682 *
19683 * Return codes
19684 * 	0 - successful
19685 *      -EIO - The mailbox failed to complete successfully.
19686 * 	When this error occurs, the driver is not guaranteed
19687 *	to have any rpi regions posted to the device and
19688 *	must either attempt to repost the regions or take a
19689 *	fatal error.
19690 **/
19691int
19692lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
19693{
19694	struct lpfc_rpi_hdr *rpi_page;
19695	uint32_t rc = 0;
19696	uint16_t lrpi = 0;
19697
19698	/* SLI4 ports that support extents do not require RPI headers. */
19699	if (!phba->sli4_hba.rpi_hdrs_in_use)
19700		goto exit;
19701	if (phba->sli4_hba.extents_in_use)
19702		return -EIO;
19703
19704	list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
19705		/*
19706		 * Assign the rpi headers a physical rpi only if the driver
19707		 * has not initialized those resources.  A port reset only
19708		 * needs the headers posted.
19709		 */
19710		if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
19711		    LPFC_RPI_RSRC_RDY)
19712			rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19713
19714		rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
19715		if (rc != MBX_SUCCESS) {
19716			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19717					"2008 Error %d posting all rpi "
19718					"headers\n", rc);
19719			rc = -EIO;
19720			break;
19721		}
19722	}
19723
19724 exit:
19725	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
19726	       LPFC_RPI_RSRC_RDY);
19727	return rc;
19728}
19729
19730/**
19731 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
19732 * @phba: pointer to lpfc hba data structure.
19733 * @rpi_page:  pointer to the rpi memory region.
19734 *
19735 * This routine is invoked to post a single rpi header to the
19736 * HBA consistent with the SLI-4 interface spec.  This memory region
19737 * maps up to 64 rpi context regions.
19738 *
19739 * Return codes
19740 * 	0 - successful
19741 * 	-ENOMEM - No available memory
19742 *      -EIO - The mailbox failed to complete successfully.
19743 **/
19744int
19745lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
19746{
19747	LPFC_MBOXQ_t *mboxq;
19748	struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
19749	uint32_t rc = 0;
19750	uint32_t shdr_status, shdr_add_status;
19751	union lpfc_sli4_cfg_shdr *shdr;
19752
19753	/* SLI4 ports that support extents do not require RPI headers. */
19754	if (!phba->sli4_hba.rpi_hdrs_in_use)
19755		return rc;
19756	if (phba->sli4_hba.extents_in_use)
19757		return -EIO;
19758
19759	/* The port is notified of the header region via a mailbox command. */
19760	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19761	if (!mboxq) {
19762		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19763				"2001 Unable to allocate memory for issuing "
19764				"SLI_CONFIG_SPECIAL mailbox command\n");
19765		return -ENOMEM;
19766	}
19767
19768	/* Post all rpi memory regions to the port. */
19769	hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
19770	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19771			 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
19772			 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
19773			 sizeof(struct lpfc_sli4_cfg_mhdr),
19774			 LPFC_SLI4_MBX_EMBED);
19775
19776
19777	/* Post the physical rpi to the port for this rpi header. */
19778	bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
19779	       rpi_page->start_rpi);
19780	bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
19781	       hdr_tmpl, rpi_page->page_count);
19782
19783	hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
19784	hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
19785	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19786	shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
19787	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19788	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19789	mempool_free(mboxq, phba->mbox_mem_pool);
19790	if (shdr_status || shdr_add_status || rc) {
19791		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19792				"2514 POST_RPI_HDR mailbox failed with "
19793				"status x%x add_status x%x, mbx status x%x\n",
19794				shdr_status, shdr_add_status, rc);
19795		rc = -ENXIO;
19796	} else {
19797		/*
19798		 * The next_rpi stores the next logical module-64 rpi value used
19799		 * to post physical rpis in subsequent rpi postings.
19800		 */
19801		spin_lock_irq(&phba->hbalock);
19802		phba->sli4_hba.next_rpi = rpi_page->next_rpi;
19803		spin_unlock_irq(&phba->hbalock);
19804	}
19805	return rc;
19806}
19807
19808/**
19809 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
19810 * @phba: pointer to lpfc hba data structure.
19811 *
19812 * This routine is invoked to post rpi header templates to the
19813 * HBA consistent with the SLI-4 interface spec.  This routine
19814 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19815 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
19816 *
19817 * Returns
19818 * 	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
19819 * 	LPFC_RPI_ALLOC_ERROR if no rpis are available.
19820 **/
19821int
19822lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
19823{
19824	unsigned long rpi;
19825	uint16_t max_rpi, rpi_limit;
19826	uint16_t rpi_remaining, lrpi = 0;
19827	struct lpfc_rpi_hdr *rpi_hdr;
19828	unsigned long iflag;
19829
19830	/*
19831	 * Fetch the next logical rpi.  Because this index is logical,
19832	 * the  driver starts at 0 each time.
19833	 */
19834	spin_lock_irqsave(&phba->hbalock, iflag);
19835	max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
19836	rpi_limit = phba->sli4_hba.next_rpi;
19837
19838	rpi = find_first_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit);
19839	if (rpi >= rpi_limit)
19840		rpi = LPFC_RPI_ALLOC_ERROR;
19841	else {
19842		set_bit(rpi, phba->sli4_hba.rpi_bmask);
19843		phba->sli4_hba.max_cfg_param.rpi_used++;
19844		phba->sli4_hba.rpi_count++;
19845	}
19846	lpfc_printf_log(phba, KERN_INFO,
19847			LOG_NODE | LOG_DISCOVERY,
19848			"0001 Allocated rpi:x%x max:x%x lim:x%x\n",
19849			(int) rpi, max_rpi, rpi_limit);
19850
19851	/*
19852	 * Don't try to allocate more rpi header regions if the device limit
19853	 * has been exhausted.
19854	 */
19855	if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
19856	    (phba->sli4_hba.rpi_count >= max_rpi)) {
19857		spin_unlock_irqrestore(&phba->hbalock, iflag);
19858		return rpi;
19859	}
19860
19861	/*
19862	 * RPI header postings are not required for SLI4 ports capable of
19863	 * extents.
19864	 */
19865	if (!phba->sli4_hba.rpi_hdrs_in_use) {
19866		spin_unlock_irqrestore(&phba->hbalock, iflag);
19867		return rpi;
19868	}
19869
19870	/*
19871	 * If the driver is running low on rpi resources, allocate another
19872	 * page now.  Note that the next_rpi value is used because
19873	 * it represents how many are actually in use whereas max_rpi notes
19874	 * how many are supported max by the device.
19875	 */
19876	rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
19877	spin_unlock_irqrestore(&phba->hbalock, iflag);
19878	if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
19879		rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
19880		if (!rpi_hdr) {
19881			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19882					"2002 Error Could not grow rpi "
19883					"count\n");
19884		} else {
19885			lrpi = rpi_hdr->start_rpi;
19886			rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19887			lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
19888		}
19889	}
19890
19891	return rpi;
19892}
19893
19894/**
19895 * __lpfc_sli4_free_rpi - Release an rpi for reuse.
19896 * @phba: pointer to lpfc hba data structure.
19897 * @rpi: rpi to free
19898 *
19899 * This routine is invoked to release an rpi to the pool of
19900 * available rpis maintained by the driver.
19901 **/
19902static void
19903__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19904{
19905	/*
19906	 * if the rpi value indicates a prior unreg has already
19907	 * been done, skip the unreg.
19908	 */
19909	if (rpi == LPFC_RPI_ALLOC_ERROR)
19910		return;
19911
19912	if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
19913		phba->sli4_hba.rpi_count--;
19914		phba->sli4_hba.max_cfg_param.rpi_used--;
19915	} else {
19916		lpfc_printf_log(phba, KERN_INFO,
19917				LOG_NODE | LOG_DISCOVERY,
19918				"2016 rpi %x not inuse\n",
19919				rpi);
19920	}
19921}
19922
19923/**
19924 * lpfc_sli4_free_rpi - Release an rpi for reuse.
19925 * @phba: pointer to lpfc hba data structure.
19926 * @rpi: rpi to free
19927 *
19928 * This routine is invoked to release an rpi to the pool of
19929 * available rpis maintained by the driver.
19930 **/
19931void
19932lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19933{
19934	spin_lock_irq(&phba->hbalock);
19935	__lpfc_sli4_free_rpi(phba, rpi);
19936	spin_unlock_irq(&phba->hbalock);
19937}
19938
19939/**
19940 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
19941 * @phba: pointer to lpfc hba data structure.
19942 *
19943 * This routine is invoked to remove the memory region that
19944 * provided rpi via a bitmask.
19945 **/
19946void
19947lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
19948{
19949	kfree(phba->sli4_hba.rpi_bmask);
19950	kfree(phba->sli4_hba.rpi_ids);
19951	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
19952}
19953
19954/**
19955 * lpfc_sli4_resume_rpi - Resume traffic relative to an RPI
19956 * @ndlp: pointer to lpfc nodelist data structure.
19957 * @cmpl: completion call-back.
19958 * @iocbq: data to load as mbox ctx_u information
19959 *
19960 * Return codes
19961 *	0 - successful
19962 *	-ENOMEM - No available memory
19963 *	-EIO - The mailbox failed to complete successfully.
19964 **/
19965int
19966lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
19967		     void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *),
19968		     struct lpfc_iocbq *iocbq)
19969{
19970	LPFC_MBOXQ_t *mboxq;
19971	struct lpfc_hba *phba = ndlp->phba;
19972	int rc;
19973
19974	/* The port is notified of the header region via a mailbox command. */
19975	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19976	if (!mboxq)
19977		return -ENOMEM;
19978
19979	/* If cmpl assigned, then this nlp_get pairs with
19980	 * lpfc_mbx_cmpl_resume_rpi.
19981	 *
19982	 * Else cmpl is NULL, then this nlp_get pairs with
19983	 * lpfc_sli_def_mbox_cmpl.
19984	 */
19985	if (!lpfc_nlp_get(ndlp)) {
19986		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19987				"2122 %s: Failed to get nlp ref\n",
19988				__func__);
19989		mempool_free(mboxq, phba->mbox_mem_pool);
19990		return -EIO;
19991	}
19992
19993	lpfc_resume_rpi(mboxq, ndlp);
19994	if (cmpl) {
19995		mboxq->mbox_cmpl = cmpl;
19996		mboxq->ctx_u.save_iocb = iocbq;
19997	} else
19998		mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19999	mboxq->ctx_ndlp = ndlp;
20000	mboxq->vport = ndlp->vport;
20001	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20002	if (rc == MBX_NOT_FINISHED) {
20003		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20004				"2010 Resume RPI Mailbox failed "
20005				"status %d, mbxStatus x%x\n", rc,
20006				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
20007		lpfc_nlp_put(ndlp);
20008		mempool_free(mboxq, phba->mbox_mem_pool);
20009		return -EIO;
20010	}
20011	return 0;
20012}
20013
20014/**
20015 * lpfc_sli4_init_vpi - Initialize a vpi with the port
20016 * @vport: Pointer to the vport for which the vpi is being initialized
20017 *
20018 * This routine is invoked to activate a vpi with the port.
20019 *
20020 * Returns:
20021 *    0 success
20022 *    -Evalue otherwise
20023 **/
20024int
20025lpfc_sli4_init_vpi(struct lpfc_vport *vport)
20026{
20027	LPFC_MBOXQ_t *mboxq;
20028	int rc = 0;
20029	int retval = MBX_SUCCESS;
20030	uint32_t mbox_tmo;
20031	struct lpfc_hba *phba = vport->phba;
20032	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20033	if (!mboxq)
20034		return -ENOMEM;
20035	lpfc_init_vpi(phba, mboxq, vport->vpi);
20036	mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
20037	rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
20038	if (rc != MBX_SUCCESS) {
20039		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
20040				"2022 INIT VPI Mailbox failed "
20041				"status %d, mbxStatus x%x\n", rc,
20042				bf_get(lpfc_mqe_status, &mboxq->u.mqe));
20043		retval = -EIO;
20044	}
20045	if (rc != MBX_TIMEOUT)
20046		mempool_free(mboxq, vport->phba->mbox_mem_pool);
20047
20048	return retval;
20049}
20050
20051/**
20052 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
20053 * @phba: pointer to lpfc hba data structure.
20054 * @mboxq: Pointer to mailbox object.
20055 *
20056 * This routine is invoked to manually add a single FCF record. The caller
20057 * must pass a completely initialized FCF_Record.  This routine takes
20058 * care of the nonembedded mailbox operations.
20059 **/
20060static void
20061lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
20062{
20063	void *virt_addr;
20064	union lpfc_sli4_cfg_shdr *shdr;
20065	uint32_t shdr_status, shdr_add_status;
20066
20067	virt_addr = mboxq->sge_array->addr[0];
20068	/* The IOCTL status is embedded in the mailbox subheader. */
20069	shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
20070	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
20071	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
20072
20073	if ((shdr_status || shdr_add_status) &&
20074		(shdr_status != STATUS_FCF_IN_USE))
20075		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20076			"2558 ADD_FCF_RECORD mailbox failed with "
20077			"status x%x add_status x%x\n",
20078			shdr_status, shdr_add_status);
20079
20080	lpfc_sli4_mbox_cmd_free(phba, mboxq);
20081}
20082
20083/**
20084 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
20085 * @phba: pointer to lpfc hba data structure.
20086 * @fcf_record:  pointer to the initialized fcf record to add.
20087 *
20088 * This routine is invoked to manually add a single FCF record. The caller
20089 * must pass a completely initialized FCF_Record.  This routine takes
20090 * care of the nonembedded mailbox operations.
20091 **/
20092int
20093lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
20094{
20095	int rc = 0;
20096	LPFC_MBOXQ_t *mboxq;
20097	uint8_t *bytep;
20098	void *virt_addr;
20099	struct lpfc_mbx_sge sge;
20100	uint32_t alloc_len, req_len;
20101	uint32_t fcfindex;
20102
20103	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20104	if (!mboxq) {
20105		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20106			"2009 Failed to allocate mbox for ADD_FCF cmd\n");
20107		return -ENOMEM;
20108	}
20109
20110	req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
20111		  sizeof(uint32_t);
20112
20113	/* Allocate DMA memory and set up the non-embedded mailbox command */
20114	alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
20115				     LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
20116				     req_len, LPFC_SLI4_MBX_NEMBED);
20117	if (alloc_len < req_len) {
20118		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20119			"2523 Allocated DMA memory size (x%x) is "
20120			"less than the requested DMA memory "
20121			"size (x%x)\n", alloc_len, req_len);
20122		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20123		return -ENOMEM;
20124	}
20125
20126	/*
20127	 * Get the first SGE entry from the non-embedded DMA memory.  This
20128	 * routine only uses a single SGE.
20129	 */
20130	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
20131	virt_addr = mboxq->sge_array->addr[0];
20132	/*
20133	 * Configure the FCF record for FCFI 0.  This is the driver's
20134	 * hardcoded default and gets used in nonFIP mode.
20135	 */
20136	fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
20137	bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
20138	lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
20139
20140	/*
20141	 * Copy the fcf_index and the FCF Record Data. The data starts after
20142	 * the FCoE header plus word10. The data copy needs to be endian
20143	 * correct.
20144	 */
20145	bytep += sizeof(uint32_t);
20146	lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
20147	mboxq->vport = phba->pport;
20148	mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
20149	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20150	if (rc == MBX_NOT_FINISHED) {
20151		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20152			"2515 ADD_FCF_RECORD mailbox failed with "
20153			"status 0x%x\n", rc);
20154		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20155		rc = -EIO;
20156	} else
20157		rc = 0;
20158
20159	return rc;
20160}
20161
20162/**
20163 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
20164 * @phba: pointer to lpfc hba data structure.
20165 * @fcf_record:  pointer to the fcf record to write the default data.
20166 * @fcf_index: FCF table entry index.
20167 *
20168 * This routine is invoked to build the driver's default FCF record.  The
20169 * values used are hardcoded.  This routine handles memory initialization.
20170 *
20171 **/
20172void
20173lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
20174				struct fcf_record *fcf_record,
20175				uint16_t fcf_index)
20176{
20177	memset(fcf_record, 0, sizeof(struct fcf_record));
20178	fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
20179	fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
20180	fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
20181	bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
20182	bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
20183	bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
20184	bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
20185	bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
20186	bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
20187	bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
20188	bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
20189	bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
20190	bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
20191	bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
20192	bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
20193	bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
20194		LPFC_FCF_FPMA | LPFC_FCF_SPMA);
20195	/* Set the VLAN bit map */
20196	if (phba->valid_vlan) {
20197		fcf_record->vlan_bitmap[phba->vlan_id / 8]
20198			= 1 << (phba->vlan_id % 8);
20199	}
20200}
20201
20202/**
20203 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
20204 * @phba: pointer to lpfc hba data structure.
20205 * @fcf_index: FCF table entry offset.
20206 *
20207 * This routine is invoked to scan the entire FCF table by reading FCF
20208 * record and processing it one at a time starting from the @fcf_index
20209 * for initial FCF discovery or fast FCF failover rediscovery.
20210 *
20211 * Return 0 if the mailbox command is submitted successfully, none 0
20212 * otherwise.
20213 **/
20214int
20215lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20216{
20217	int rc = 0, error;
20218	LPFC_MBOXQ_t *mboxq;
20219
20220	phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
20221	phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
20222	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20223	if (!mboxq) {
20224		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20225				"2000 Failed to allocate mbox for "
20226				"READ_FCF cmd\n");
20227		error = -ENOMEM;
20228		goto fail_fcf_scan;
20229	}
20230	/* Construct the read FCF record mailbox command */
20231	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20232	if (rc) {
20233		error = -EINVAL;
20234		goto fail_fcf_scan;
20235	}
20236	/* Issue the mailbox command asynchronously */
20237	mboxq->vport = phba->pport;
20238	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
20239
20240	set_bit(FCF_TS_INPROG, &phba->hba_flag);
20241
20242	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20243	if (rc == MBX_NOT_FINISHED)
20244		error = -EIO;
20245	else {
20246		/* Reset eligible FCF count for new scan */
20247		if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
20248			phba->fcf.eligible_fcf_cnt = 0;
20249		error = 0;
20250	}
20251fail_fcf_scan:
20252	if (error) {
20253		if (mboxq)
20254			lpfc_sli4_mbox_cmd_free(phba, mboxq);
20255		/* FCF scan failed, clear FCF_TS_INPROG flag */
20256		clear_bit(FCF_TS_INPROG, &phba->hba_flag);
20257	}
20258	return error;
20259}
20260
20261/**
20262 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
20263 * @phba: pointer to lpfc hba data structure.
20264 * @fcf_index: FCF table entry offset.
20265 *
20266 * This routine is invoked to read an FCF record indicated by @fcf_index
20267 * and to use it for FLOGI roundrobin FCF failover.
20268 *
20269 * Return 0 if the mailbox command is submitted successfully, none 0
20270 * otherwise.
20271 **/
20272int
20273lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20274{
20275	int rc = 0, error;
20276	LPFC_MBOXQ_t *mboxq;
20277
20278	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20279	if (!mboxq) {
20280		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20281				"2763 Failed to allocate mbox for "
20282				"READ_FCF cmd\n");
20283		error = -ENOMEM;
20284		goto fail_fcf_read;
20285	}
20286	/* Construct the read FCF record mailbox command */
20287	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20288	if (rc) {
20289		error = -EINVAL;
20290		goto fail_fcf_read;
20291	}
20292	/* Issue the mailbox command asynchronously */
20293	mboxq->vport = phba->pport;
20294	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
20295	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20296	if (rc == MBX_NOT_FINISHED)
20297		error = -EIO;
20298	else
20299		error = 0;
20300
20301fail_fcf_read:
20302	if (error && mboxq)
20303		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20304	return error;
20305}
20306
20307/**
20308 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
20309 * @phba: pointer to lpfc hba data structure.
20310 * @fcf_index: FCF table entry offset.
20311 *
20312 * This routine is invoked to read an FCF record indicated by @fcf_index to
20313 * determine whether it's eligible for FLOGI roundrobin failover list.
20314 *
20315 * Return 0 if the mailbox command is submitted successfully, none 0
20316 * otherwise.
20317 **/
20318int
20319lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20320{
20321	int rc = 0, error;
20322	LPFC_MBOXQ_t *mboxq;
20323
20324	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20325	if (!mboxq) {
20326		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20327				"2758 Failed to allocate mbox for "
20328				"READ_FCF cmd\n");
20329				error = -ENOMEM;
20330				goto fail_fcf_read;
20331	}
20332	/* Construct the read FCF record mailbox command */
20333	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20334	if (rc) {
20335		error = -EINVAL;
20336		goto fail_fcf_read;
20337	}
20338	/* Issue the mailbox command asynchronously */
20339	mboxq->vport = phba->pport;
20340	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
20341	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20342	if (rc == MBX_NOT_FINISHED)
20343		error = -EIO;
20344	else
20345		error = 0;
20346
20347fail_fcf_read:
20348	if (error && mboxq)
20349		lpfc_sli4_mbox_cmd_free(phba, mboxq);
20350	return error;
20351}
20352
20353/**
20354 * lpfc_check_next_fcf_pri_level
20355 * @phba: pointer to the lpfc_hba struct for this port.
20356 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
20357 * routine when the rr_bmask is empty. The FCF indecies are put into the
20358 * rr_bmask based on their priority level. Starting from the highest priority
20359 * to the lowest. The most likely FCF candidate will be in the highest
20360 * priority group. When this routine is called it searches the fcf_pri list for
20361 * next lowest priority group and repopulates the rr_bmask with only those
20362 * fcf_indexes.
20363 * returns:
20364 * 1=success 0=failure
20365 **/
20366static int
20367lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
20368{
20369	uint16_t next_fcf_pri;
20370	uint16_t last_index;
20371	struct lpfc_fcf_pri *fcf_pri;
20372	int rc;
20373	int ret = 0;
20374
20375	last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20376			LPFC_SLI4_FCF_TBL_INDX_MAX);
20377	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20378			"3060 Last IDX %d\n", last_index);
20379
20380	/* Verify the priority list has 2 or more entries */
20381	spin_lock_irq(&phba->hbalock);
20382	if (list_empty(&phba->fcf.fcf_pri_list) ||
20383	    list_is_singular(&phba->fcf.fcf_pri_list)) {
20384		spin_unlock_irq(&phba->hbalock);
20385		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20386			"3061 Last IDX %d\n", last_index);
20387		return 0; /* Empty rr list */
20388	}
20389	spin_unlock_irq(&phba->hbalock);
20390
20391	next_fcf_pri = 0;
20392	/*
20393	 * Clear the rr_bmask and set all of the bits that are at this
20394	 * priority.
20395	 */
20396	memset(phba->fcf.fcf_rr_bmask, 0,
20397			sizeof(*phba->fcf.fcf_rr_bmask));
20398	spin_lock_irq(&phba->hbalock);
20399	list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20400		if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
20401			continue;
20402		/*
20403		 * the 1st priority that has not FLOGI failed
20404		 * will be the highest.
20405		 */
20406		if (!next_fcf_pri)
20407			next_fcf_pri = fcf_pri->fcf_rec.priority;
20408		spin_unlock_irq(&phba->hbalock);
20409		if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20410			rc = lpfc_sli4_fcf_rr_index_set(phba,
20411						fcf_pri->fcf_rec.fcf_index);
20412			if (rc)
20413				return 0;
20414		}
20415		spin_lock_irq(&phba->hbalock);
20416	}
20417	/*
20418	 * if next_fcf_pri was not set above and the list is not empty then
20419	 * we have failed flogis on all of them. So reset flogi failed
20420	 * and start at the beginning.
20421	 */
20422	if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
20423		list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20424			fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
20425			/*
20426			 * the 1st priority that has not FLOGI failed
20427			 * will be the highest.
20428			 */
20429			if (!next_fcf_pri)
20430				next_fcf_pri = fcf_pri->fcf_rec.priority;
20431			spin_unlock_irq(&phba->hbalock);
20432			if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20433				rc = lpfc_sli4_fcf_rr_index_set(phba,
20434						fcf_pri->fcf_rec.fcf_index);
20435				if (rc)
20436					return 0;
20437			}
20438			spin_lock_irq(&phba->hbalock);
20439		}
20440	} else
20441		ret = 1;
20442	spin_unlock_irq(&phba->hbalock);
20443
20444	return ret;
20445}
20446/**
20447 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
20448 * @phba: pointer to lpfc hba data structure.
20449 *
20450 * This routine is to get the next eligible FCF record index in a round
20451 * robin fashion. If the next eligible FCF record index equals to the
20452 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
20453 * shall be returned, otherwise, the next eligible FCF record's index
20454 * shall be returned.
20455 **/
20456uint16_t
20457lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
20458{
20459	uint16_t next_fcf_index;
20460
20461initial_priority:
20462	/* Search start from next bit of currently registered FCF index */
20463	next_fcf_index = phba->fcf.current_rec.fcf_indx;
20464
20465next_priority:
20466	/* Determine the next fcf index to check */
20467	next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
20468	next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
20469				       LPFC_SLI4_FCF_TBL_INDX_MAX,
20470				       next_fcf_index);
20471
20472	/* Wrap around condition on phba->fcf.fcf_rr_bmask */
20473	if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20474		/*
20475		 * If we have wrapped then we need to clear the bits that
20476		 * have been tested so that we can detect when we should
20477		 * change the priority level.
20478		 */
20479		next_fcf_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20480					       LPFC_SLI4_FCF_TBL_INDX_MAX);
20481	}
20482
20483
20484	/* Check roundrobin failover list empty condition */
20485	if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
20486		next_fcf_index == phba->fcf.current_rec.fcf_indx) {
20487		/*
20488		 * If next fcf index is not found check if there are lower
20489		 * Priority level fcf's in the fcf_priority list.
20490		 * Set up the rr_bmask with all of the avaiable fcf bits
20491		 * at that level and continue the selection process.
20492		 */
20493		if (lpfc_check_next_fcf_pri_level(phba))
20494			goto initial_priority;
20495		lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
20496				"2844 No roundrobin failover FCF available\n");
20497
20498		return LPFC_FCOE_FCF_NEXT_NONE;
20499	}
20500
20501	if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
20502		phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
20503		LPFC_FCF_FLOGI_FAILED) {
20504		if (list_is_singular(&phba->fcf.fcf_pri_list))
20505			return LPFC_FCOE_FCF_NEXT_NONE;
20506
20507		goto next_priority;
20508	}
20509
20510	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20511			"2845 Get next roundrobin failover FCF (x%x)\n",
20512			next_fcf_index);
20513
20514	return next_fcf_index;
20515}
20516
20517/**
20518 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
20519 * @phba: pointer to lpfc hba data structure.
20520 * @fcf_index: index into the FCF table to 'set'
20521 *
20522 * This routine sets the FCF record index in to the eligible bmask for
20523 * roundrobin failover search. It checks to make sure that the index
20524 * does not go beyond the range of the driver allocated bmask dimension
20525 * before setting the bit.
20526 *
20527 * Returns 0 if the index bit successfully set, otherwise, it returns
20528 * -EINVAL.
20529 **/
20530int
20531lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
20532{
20533	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20534		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20535				"2610 FCF (x%x) reached driver's book "
20536				"keeping dimension:x%x\n",
20537				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20538		return -EINVAL;
20539	}
20540	/* Set the eligible FCF record index bmask */
20541	set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20542
20543	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20544			"2790 Set FCF (x%x) to roundrobin FCF failover "
20545			"bmask\n", fcf_index);
20546
20547	return 0;
20548}
20549
20550/**
20551 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
20552 * @phba: pointer to lpfc hba data structure.
20553 * @fcf_index: index into the FCF table to 'clear'
20554 *
20555 * This routine clears the FCF record index from the eligible bmask for
20556 * roundrobin failover search. It checks to make sure that the index
20557 * does not go beyond the range of the driver allocated bmask dimension
20558 * before clearing the bit.
20559 **/
20560void
20561lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
20562{
20563	struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
20564	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20565		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20566				"2762 FCF (x%x) reached driver's book "
20567				"keeping dimension:x%x\n",
20568				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20569		return;
20570	}
20571	/* Clear the eligible FCF record index bmask */
20572	spin_lock_irq(&phba->hbalock);
20573	list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
20574				 list) {
20575		if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
20576			list_del_init(&fcf_pri->list);
20577			break;
20578		}
20579	}
20580	spin_unlock_irq(&phba->hbalock);
20581	clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20582
20583	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20584			"2791 Clear FCF (x%x) from roundrobin failover "
20585			"bmask\n", fcf_index);
20586}
20587
20588/**
20589 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
20590 * @phba: pointer to lpfc hba data structure.
20591 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
20592 *
20593 * This routine is the completion routine for the rediscover FCF table mailbox
20594 * command. If the mailbox command returned failure, it will try to stop the
20595 * FCF rediscover wait timer.
20596 **/
20597static void
20598lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
20599{
20600	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20601	uint32_t shdr_status, shdr_add_status;
20602
20603	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20604
20605	shdr_status = bf_get(lpfc_mbox_hdr_status,
20606			     &redisc_fcf->header.cfg_shdr.response);
20607	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20608			     &redisc_fcf->header.cfg_shdr.response);
20609	if (shdr_status || shdr_add_status) {
20610		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20611				"2746 Requesting for FCF rediscovery failed "
20612				"status x%x add_status x%x\n",
20613				shdr_status, shdr_add_status);
20614		if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
20615			spin_lock_irq(&phba->hbalock);
20616			phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
20617			spin_unlock_irq(&phba->hbalock);
20618			/*
20619			 * CVL event triggered FCF rediscover request failed,
20620			 * last resort to re-try current registered FCF entry.
20621			 */
20622			lpfc_retry_pport_discovery(phba);
20623		} else {
20624			spin_lock_irq(&phba->hbalock);
20625			phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
20626			spin_unlock_irq(&phba->hbalock);
20627			/*
20628			 * DEAD FCF event triggered FCF rediscover request
20629			 * failed, last resort to fail over as a link down
20630			 * to FCF registration.
20631			 */
20632			lpfc_sli4_fcf_dead_failthrough(phba);
20633		}
20634	} else {
20635		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20636				"2775 Start FCF rediscover quiescent timer\n");
20637		/*
20638		 * Start FCF rediscovery wait timer for pending FCF
20639		 * before rescan FCF record table.
20640		 */
20641		lpfc_fcf_redisc_wait_start_timer(phba);
20642	}
20643
20644	mempool_free(mbox, phba->mbox_mem_pool);
20645}
20646
20647/**
20648 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
20649 * @phba: pointer to lpfc hba data structure.
20650 *
20651 * This routine is invoked to request for rediscovery of the entire FCF table
20652 * by the port.
20653 **/
20654int
20655lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
20656{
20657	LPFC_MBOXQ_t *mbox;
20658	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20659	int rc, length;
20660
20661	/* Cancel retry delay timers to all vports before FCF rediscover */
20662	lpfc_cancel_all_vport_retry_delay_timer(phba);
20663
20664	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20665	if (!mbox) {
20666		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20667				"2745 Failed to allocate mbox for "
20668				"requesting FCF rediscover.\n");
20669		return -ENOMEM;
20670	}
20671
20672	length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
20673		  sizeof(struct lpfc_sli4_cfg_mhdr));
20674	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
20675			 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
20676			 length, LPFC_SLI4_MBX_EMBED);
20677
20678	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20679	/* Set count to 0 for invalidating the entire FCF database */
20680	bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
20681
20682	/* Issue the mailbox command asynchronously */
20683	mbox->vport = phba->pport;
20684	mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
20685	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
20686
20687	if (rc == MBX_NOT_FINISHED) {
20688		mempool_free(mbox, phba->mbox_mem_pool);
20689		return -EIO;
20690	}
20691	return 0;
20692}
20693
20694/**
20695 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
20696 * @phba: pointer to lpfc hba data structure.
20697 *
20698 * This function is the failover routine as a last resort to the FCF DEAD
20699 * event when driver failed to perform fast FCF failover.
20700 **/
20701void
20702lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
20703{
20704	uint32_t link_state;
20705
20706	/*
20707	 * Last resort as FCF DEAD event failover will treat this as
20708	 * a link down, but save the link state because we don't want
20709	 * it to be changed to Link Down unless it is already down.
20710	 */
20711	link_state = phba->link_state;
20712	lpfc_linkdown(phba);
20713	phba->link_state = link_state;
20714
20715	/* Unregister FCF if no devices connected to it */
20716	lpfc_unregister_unused_fcf(phba);
20717}
20718
20719/**
20720 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
20721 * @phba: pointer to lpfc hba data structure.
20722 * @rgn23_data: pointer to configure region 23 data.
20723 *
20724 * This function gets SLI3 port configure region 23 data through memory dump
20725 * mailbox command. When it successfully retrieves data, the size of the data
20726 * will be returned, otherwise, 0 will be returned.
20727 **/
20728static uint32_t
20729lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20730{
20731	LPFC_MBOXQ_t *pmb = NULL;
20732	MAILBOX_t *mb;
20733	uint32_t offset = 0;
20734	int rc;
20735
20736	if (!rgn23_data)
20737		return 0;
20738
20739	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20740	if (!pmb) {
20741		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20742				"2600 failed to allocate mailbox memory\n");
20743		return 0;
20744	}
20745	mb = &pmb->u.mb;
20746
20747	do {
20748		lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
20749		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
20750
20751		if (rc != MBX_SUCCESS) {
20752			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20753					"2601 failed to read config "
20754					"region 23, rc 0x%x Status 0x%x\n",
20755					rc, mb->mbxStatus);
20756			mb->un.varDmp.word_cnt = 0;
20757		}
20758		/*
20759		 * dump mem may return a zero when finished or we got a
20760		 * mailbox error, either way we are done.
20761		 */
20762		if (mb->un.varDmp.word_cnt == 0)
20763			break;
20764
20765		if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
20766			mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
20767
20768		lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
20769				       rgn23_data + offset,
20770				       mb->un.varDmp.word_cnt);
20771		offset += mb->un.varDmp.word_cnt;
20772	} while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
20773
20774	mempool_free(pmb, phba->mbox_mem_pool);
20775	return offset;
20776}
20777
20778/**
20779 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
20780 * @phba: pointer to lpfc hba data structure.
20781 * @rgn23_data: pointer to configure region 23 data.
20782 *
20783 * This function gets SLI4 port configure region 23 data through memory dump
20784 * mailbox command. When it successfully retrieves data, the size of the data
20785 * will be returned, otherwise, 0 will be returned.
20786 **/
20787static uint32_t
20788lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20789{
20790	LPFC_MBOXQ_t *mboxq = NULL;
20791	struct lpfc_dmabuf *mp = NULL;
20792	struct lpfc_mqe *mqe;
20793	uint32_t data_length = 0;
20794	int rc;
20795
20796	if (!rgn23_data)
20797		return 0;
20798
20799	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20800	if (!mboxq) {
20801		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20802				"3105 failed to allocate mailbox memory\n");
20803		return 0;
20804	}
20805
20806	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
20807		goto out;
20808	mqe = &mboxq->u.mqe;
20809	mp = mboxq->ctx_buf;
20810	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
20811	if (rc)
20812		goto out;
20813	data_length = mqe->un.mb_words[5];
20814	if (data_length == 0)
20815		goto out;
20816	if (data_length > DMP_RGN23_SIZE) {
20817		data_length = 0;
20818		goto out;
20819	}
20820	lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
20821out:
20822	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
20823	return data_length;
20824}
20825
20826/**
20827 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
20828 * @phba: pointer to lpfc hba data structure.
20829 *
20830 * This function read region 23 and parse TLV for port status to
20831 * decide if the user disaled the port. If the TLV indicates the
20832 * port is disabled, the hba_flag is set accordingly.
20833 **/
20834void
20835lpfc_sli_read_link_ste(struct lpfc_hba *phba)
20836{
20837	uint8_t *rgn23_data = NULL;
20838	uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
20839	uint32_t offset = 0;
20840
20841	/* Get adapter Region 23 data */
20842	rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
20843	if (!rgn23_data)
20844		goto out;
20845
20846	if (phba->sli_rev < LPFC_SLI_REV4)
20847		data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
20848	else {
20849		if_type = bf_get(lpfc_sli_intf_if_type,
20850				 &phba->sli4_hba.sli_intf);
20851		if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
20852			goto out;
20853		data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
20854	}
20855
20856	if (!data_size)
20857		goto out;
20858
20859	/* Check the region signature first */
20860	if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
20861		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20862			"2619 Config region 23 has bad signature\n");
20863			goto out;
20864	}
20865	offset += 4;
20866
20867	/* Check the data structure version */
20868	if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
20869		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
20870			"2620 Config region 23 has bad version\n");
20871		goto out;
20872	}
20873	offset += 4;
20874
20875	/* Parse TLV entries in the region */
20876	while (offset < data_size) {
20877		if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
20878			break;
20879		/*
20880		 * If the TLV is not driver specific TLV or driver id is
20881		 * not linux driver id, skip the record.
20882		 */
20883		if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
20884		    (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
20885		    (rgn23_data[offset + 3] != 0)) {
20886			offset += rgn23_data[offset + 1] * 4 + 4;
20887			continue;
20888		}
20889
20890		/* Driver found a driver specific TLV in the config region */
20891		sub_tlv_len = rgn23_data[offset + 1] * 4;
20892		offset += 4;
20893		tlv_offset = 0;
20894
20895		/*
20896		 * Search for configured port state sub-TLV.
20897		 */
20898		while ((offset < data_size) &&
20899			(tlv_offset < sub_tlv_len)) {
20900			if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
20901				offset += 4;
20902				tlv_offset += 4;
20903				break;
20904			}
20905			if (rgn23_data[offset] != PORT_STE_TYPE) {
20906				offset += rgn23_data[offset + 1] * 4 + 4;
20907				tlv_offset += rgn23_data[offset + 1] * 4 + 4;
20908				continue;
20909			}
20910
20911			/* This HBA contains PORT_STE configured */
20912			if (!rgn23_data[offset + 2])
20913				set_bit(LINK_DISABLED, &phba->hba_flag);
20914
20915			goto out;
20916		}
20917	}
20918
20919out:
20920	kfree(rgn23_data);
20921	return;
20922}
20923
20924/**
20925 * lpfc_log_fw_write_cmpl - logs firmware write completion status
20926 * @phba: pointer to lpfc hba data structure
20927 * @shdr_status: wr_object rsp's status field
20928 * @shdr_add_status: wr_object rsp's add_status field
20929 * @shdr_add_status_2: wr_object rsp's add_status_2 field
20930 * @shdr_change_status: wr_object rsp's change_status field
20931 * @shdr_csf: wr_object rsp's csf bit
20932 *
20933 * This routine is intended to be called after a firmware write completes.
20934 * It will log next action items to be performed by the user to instantiate
20935 * the newly downloaded firmware or reason for incompatibility.
20936 **/
20937static void
20938lpfc_log_fw_write_cmpl(struct lpfc_hba *phba, u32 shdr_status,
20939		       u32 shdr_add_status, u32 shdr_add_status_2,
20940		       u32 shdr_change_status, u32 shdr_csf)
20941{
20942	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20943			"4198 %s: flash_id x%02x, asic_rev x%02x, "
20944			"status x%02x, add_status x%02x, add_status_2 x%02x, "
20945			"change_status x%02x, csf %01x\n", __func__,
20946			phba->sli4_hba.flash_id, phba->sli4_hba.asic_rev,
20947			shdr_status, shdr_add_status, shdr_add_status_2,
20948			shdr_change_status, shdr_csf);
20949
20950	if (shdr_add_status == LPFC_ADD_STATUS_INCOMPAT_OBJ) {
20951		switch (shdr_add_status_2) {
20952		case LPFC_ADD_STATUS_2_INCOMPAT_FLASH:
20953			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20954				     "4199 Firmware write failed: "
20955				     "image incompatible with flash x%02x\n",
20956				     phba->sli4_hba.flash_id);
20957			break;
20958		case LPFC_ADD_STATUS_2_INCORRECT_ASIC:
20959			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20960				     "4200 Firmware write failed: "
20961				     "image incompatible with ASIC "
20962				     "architecture x%02x\n",
20963				     phba->sli4_hba.asic_rev);
20964			break;
20965		default:
20966			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20967				     "4210 Firmware write failed: "
20968				     "add_status_2 x%02x\n",
20969				     shdr_add_status_2);
20970			break;
20971		}
20972	} else if (!shdr_status && !shdr_add_status) {
20973		if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
20974		    shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
20975			if (shdr_csf)
20976				shdr_change_status =
20977						   LPFC_CHANGE_STATUS_PCI_RESET;
20978		}
20979
20980		switch (shdr_change_status) {
20981		case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
20982			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20983				     "3198 Firmware write complete: System "
20984				     "reboot required to instantiate\n");
20985			break;
20986		case (LPFC_CHANGE_STATUS_FW_RESET):
20987			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20988				     "3199 Firmware write complete: "
20989				     "Firmware reset required to "
20990				     "instantiate\n");
20991			break;
20992		case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
20993			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
20994				     "3200 Firmware write complete: Port "
20995				     "Migration or PCI Reset required to "
20996				     "instantiate\n");
20997			break;
20998		case (LPFC_CHANGE_STATUS_PCI_RESET):
20999			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,
21000				     "3201 Firmware write complete: PCI "
21001				     "Reset required to instantiate\n");
21002			break;
21003		default:
21004			break;
21005		}
21006	}
21007}
21008
21009/**
21010 * lpfc_wr_object - write an object to the firmware
21011 * @phba: HBA structure that indicates port to create a queue on.
21012 * @dmabuf_list: list of dmabufs to write to the port.
21013 * @size: the total byte value of the objects to write to the port.
21014 * @offset: the current offset to be used to start the transfer.
21015 *
21016 * This routine will create a wr_object mailbox command to send to the port.
21017 * the mailbox command will be constructed using the dma buffers described in
21018 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
21019 * BDEs that the imbedded mailbox can support. The @offset variable will be
21020 * used to indicate the starting offset of the transfer and will also return
21021 * the offset after the write object mailbox has completed. @size is used to
21022 * determine the end of the object and whether the eof bit should be set.
21023 *
21024 * Return 0 is successful and offset will contain the new offset to use
21025 * for the next write.
21026 * Return negative value for error cases.
21027 **/
21028int
21029lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
21030	       uint32_t size, uint32_t *offset)
21031{
21032	struct lpfc_mbx_wr_object *wr_object;
21033	LPFC_MBOXQ_t *mbox;
21034	int rc = 0, i = 0;
21035	int mbox_status = 0;
21036	uint32_t shdr_status, shdr_add_status, shdr_add_status_2;
21037	uint32_t shdr_change_status = 0, shdr_csf = 0;
21038	uint32_t mbox_tmo;
21039	struct lpfc_dmabuf *dmabuf;
21040	uint32_t written = 0;
21041	bool check_change_status = false;
21042
21043	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
21044	if (!mbox)
21045		return -ENOMEM;
21046
21047	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
21048			LPFC_MBOX_OPCODE_WRITE_OBJECT,
21049			sizeof(struct lpfc_mbx_wr_object) -
21050			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
21051
21052	wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
21053	wr_object->u.request.write_offset = *offset;
21054	sprintf((uint8_t *)wr_object->u.request.object_name, "/");
21055	wr_object->u.request.object_name[0] =
21056		cpu_to_le32(wr_object->u.request.object_name[0]);
21057	bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
21058	list_for_each_entry(dmabuf, dmabuf_list, list) {
21059		if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
21060			break;
21061		wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
21062		wr_object->u.request.bde[i].addrHigh =
21063			putPaddrHigh(dmabuf->phys);
21064		if (written + SLI4_PAGE_SIZE >= size) {
21065			wr_object->u.request.bde[i].tus.f.bdeSize =
21066				(size - written);
21067			written += (size - written);
21068			bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
21069			bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
21070			check_change_status = true;
21071		} else {
21072			wr_object->u.request.bde[i].tus.f.bdeSize =
21073				SLI4_PAGE_SIZE;
21074			written += SLI4_PAGE_SIZE;
21075		}
21076		i++;
21077	}
21078	wr_object->u.request.bde_count = i;
21079	bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
21080	if (!phba->sli4_hba.intr_enable)
21081		mbox_status = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
21082	else {
21083		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
21084		mbox_status = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
21085	}
21086
21087	/* The mbox status needs to be maintained to detect MBOX_TIMEOUT. */
21088	rc = mbox_status;
21089
21090	/* The IOCTL status is embedded in the mailbox subheader. */
21091	shdr_status = bf_get(lpfc_mbox_hdr_status,
21092			     &wr_object->header.cfg_shdr.response);
21093	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
21094				 &wr_object->header.cfg_shdr.response);
21095	shdr_add_status_2 = bf_get(lpfc_mbox_hdr_add_status_2,
21096				   &wr_object->header.cfg_shdr.response);
21097	if (check_change_status) {
21098		shdr_change_status = bf_get(lpfc_wr_object_change_status,
21099					    &wr_object->u.response);
21100		shdr_csf = bf_get(lpfc_wr_object_csf,
21101				  &wr_object->u.response);
21102	}
21103
21104	if (shdr_status || shdr_add_status || shdr_add_status_2 || rc) {
21105		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21106				"3025 Write Object mailbox failed with "
21107				"status x%x add_status x%x, add_status_2 x%x, "
21108				"mbx status x%x\n",
21109				shdr_status, shdr_add_status, shdr_add_status_2,
21110				rc);
21111		rc = -ENXIO;
21112		*offset = shdr_add_status;
21113	} else {
21114		*offset += wr_object->u.response.actual_write_length;
21115	}
21116
21117	if (rc || check_change_status)
21118		lpfc_log_fw_write_cmpl(phba, shdr_status, shdr_add_status,
21119				       shdr_add_status_2, shdr_change_status,
21120				       shdr_csf);
21121
21122	if (!phba->sli4_hba.intr_enable)
21123		mempool_free(mbox, phba->mbox_mem_pool);
21124	else if (mbox_status != MBX_TIMEOUT)
21125		mempool_free(mbox, phba->mbox_mem_pool);
21126
21127	return rc;
21128}
21129
21130/**
21131 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
21132 * @vport: pointer to vport data structure.
21133 *
21134 * This function iterate through the mailboxq and clean up all REG_LOGIN
21135 * and REG_VPI mailbox commands associated with the vport. This function
21136 * is called when driver want to restart discovery of the vport due to
21137 * a Clear Virtual Link event.
21138 **/
21139void
21140lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
21141{
21142	struct lpfc_hba *phba = vport->phba;
21143	LPFC_MBOXQ_t *mb, *nextmb;
21144	struct lpfc_nodelist *ndlp;
21145	struct lpfc_nodelist *act_mbx_ndlp = NULL;
21146	LIST_HEAD(mbox_cmd_list);
21147	uint8_t restart_loop;
21148
21149	/* Clean up internally queued mailbox commands with the vport */
21150	spin_lock_irq(&phba->hbalock);
21151	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
21152		if (mb->vport != vport)
21153			continue;
21154
21155		if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
21156			(mb->u.mb.mbxCommand != MBX_REG_VPI))
21157			continue;
21158
21159		list_move_tail(&mb->list, &mbox_cmd_list);
21160	}
21161	/* Clean up active mailbox command with the vport */
21162	mb = phba->sli.mbox_active;
21163	if (mb && (mb->vport == vport)) {
21164		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
21165			(mb->u.mb.mbxCommand == MBX_REG_VPI))
21166			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
21167		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21168			act_mbx_ndlp = mb->ctx_ndlp;
21169
21170			/* This reference is local to this routine.  The
21171			 * reference is removed at routine exit.
21172			 */
21173			act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
21174
21175			/* Unregister the RPI when mailbox complete */
21176			mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21177		}
21178	}
21179	/* Cleanup any mailbox completions which are not yet processed */
21180	do {
21181		restart_loop = 0;
21182		list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
21183			/*
21184			 * If this mailox is already processed or it is
21185			 * for another vport ignore it.
21186			 */
21187			if ((mb->vport != vport) ||
21188				(mb->mbox_flag & LPFC_MBX_IMED_UNREG))
21189				continue;
21190
21191			if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
21192				(mb->u.mb.mbxCommand != MBX_REG_VPI))
21193				continue;
21194
21195			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
21196			if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21197				ndlp = mb->ctx_ndlp;
21198				/* Unregister the RPI when mailbox complete */
21199				mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21200				restart_loop = 1;
21201				clear_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag);
21202				break;
21203			}
21204		}
21205	} while (restart_loop);
21206
21207	spin_unlock_irq(&phba->hbalock);
21208
21209	/* Release the cleaned-up mailbox commands */
21210	while (!list_empty(&mbox_cmd_list)) {
21211		list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
21212		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
21213			ndlp = mb->ctx_ndlp;
21214			mb->ctx_ndlp = NULL;
21215			if (ndlp) {
21216				clear_bit(NLP_IGNR_REG_CMPL, &ndlp->nlp_flag);
21217				lpfc_nlp_put(ndlp);
21218			}
21219		}
21220		lpfc_mbox_rsrc_cleanup(phba, mb, MBOX_THD_UNLOCKED);
21221	}
21222
21223	/* Release the ndlp with the cleaned-up active mailbox command */
21224	if (act_mbx_ndlp) {
21225		clear_bit(NLP_IGNR_REG_CMPL, &act_mbx_ndlp->nlp_flag);
21226		lpfc_nlp_put(act_mbx_ndlp);
21227	}
21228}
21229
21230/**
21231 * lpfc_drain_txq - Drain the txq
21232 * @phba: Pointer to HBA context object.
21233 *
21234 * This function attempt to submit IOCBs on the txq
21235 * to the adapter.  For SLI4 adapters, the txq contains
21236 * ELS IOCBs that have been deferred because the there
21237 * are no SGLs.  This congestion can occur with large
21238 * vport counts during node discovery.
21239 **/
21240
21241uint32_t
21242lpfc_drain_txq(struct lpfc_hba *phba)
21243{
21244	LIST_HEAD(completions);
21245	struct lpfc_sli_ring *pring;
21246	struct lpfc_iocbq *piocbq = NULL;
21247	unsigned long iflags = 0;
21248	char *fail_msg = NULL;
21249	uint32_t txq_cnt = 0;
21250	struct lpfc_queue *wq;
21251	int ret = 0;
21252
21253	if (phba->link_flag & LS_MDS_LOOPBACK) {
21254		/* MDS WQE are posted only to first WQ*/
21255		wq = phba->sli4_hba.hdwq[0].io_wq;
21256		if (unlikely(!wq))
21257			return 0;
21258		pring = wq->pring;
21259	} else {
21260		wq = phba->sli4_hba.els_wq;
21261		if (unlikely(!wq))
21262			return 0;
21263		pring = lpfc_phba_elsring(phba);
21264	}
21265
21266	if (unlikely(!pring) || list_empty(&pring->txq))
21267		return 0;
21268
21269	spin_lock_irqsave(&pring->ring_lock, iflags);
21270	list_for_each_entry(piocbq, &pring->txq, list) {
21271		txq_cnt++;
21272	}
21273
21274	if (txq_cnt > pring->txq_max)
21275		pring->txq_max = txq_cnt;
21276
21277	spin_unlock_irqrestore(&pring->ring_lock, iflags);
21278
21279	while (!list_empty(&pring->txq)) {
21280		spin_lock_irqsave(&pring->ring_lock, iflags);
21281
21282		piocbq = lpfc_sli_ringtx_get(phba, pring);
21283		if (!piocbq) {
21284			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21285			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21286				"2823 txq empty and txq_cnt is %d\n",
21287				txq_cnt);
21288			break;
21289		}
21290		txq_cnt--;
21291
21292		ret = __lpfc_sli_issue_iocb(phba, pring->ringno, piocbq, 0);
21293
21294		if (ret && ret != IOCB_BUSY) {
21295			fail_msg = " - Cannot send IO ";
21296			piocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;
21297		}
21298		if (fail_msg) {
21299			piocbq->cmd_flag |= LPFC_DRIVER_ABORTED;
21300			/* Failed means we can't issue and need to cancel */
21301			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
21302					"2822 IOCB failed %s iotag 0x%x "
21303					"xri 0x%x %d flg x%x\n",
21304					fail_msg, piocbq->iotag,
21305					piocbq->sli4_xritag, ret,
21306					piocbq->cmd_flag);
21307			list_add_tail(&piocbq->list, &completions);
21308			fail_msg = NULL;
21309		}
21310		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21311		if (txq_cnt == 0 || ret == IOCB_BUSY)
21312			break;
21313	}
21314	/* Cancel all the IOCBs that cannot be issued */
21315	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
21316			      IOERR_SLI_ABORTED);
21317
21318	return txq_cnt;
21319}
21320
21321/**
21322 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
21323 * @phba: Pointer to HBA context object.
21324 * @pwqeq: Pointer to command WQE.
21325 * @sglq: Pointer to the scatter gather queue object.
21326 *
21327 * This routine converts the bpl or bde that is in the WQE
21328 * to a sgl list for the sli4 hardware. The physical address
21329 * of the bpl/bde is converted back to a virtual address.
21330 * If the WQE contains a BPL then the list of BDE's is
21331 * converted to sli4_sge's. If the WQE contains a single
21332 * BDE then it is converted to a single sli_sge.
21333 * The WQE is still in cpu endianness so the contents of
21334 * the bpl can be used without byte swapping.
21335 *
21336 * Returns valid XRI = Success, NO_XRI = Failure.
21337 */
21338static uint16_t
21339lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
21340		 struct lpfc_sglq *sglq)
21341{
21342	uint16_t xritag = NO_XRI;
21343	struct ulp_bde64 *bpl = NULL;
21344	struct ulp_bde64 bde;
21345	struct sli4_sge *sgl  = NULL;
21346	struct lpfc_dmabuf *dmabuf;
21347	union lpfc_wqe128 *wqe;
21348	int numBdes = 0;
21349	int i = 0;
21350	uint32_t offset = 0; /* accumulated offset in the sg request list */
21351	int inbound = 0; /* number of sg reply entries inbound from firmware */
21352	uint32_t cmd;
21353
21354	if (!pwqeq || !sglq)
21355		return xritag;
21356
21357	sgl  = (struct sli4_sge *)sglq->sgl;
21358	wqe = &pwqeq->wqe;
21359	pwqeq->iocb.ulpIoTag = pwqeq->iotag;
21360
21361	cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
21362	if (cmd == CMD_XMIT_BLS_RSP64_WQE)
21363		return sglq->sli4_xritag;
21364	numBdes = pwqeq->num_bdes;
21365	if (numBdes) {
21366		/* The addrHigh and addrLow fields within the WQE
21367		 * have not been byteswapped yet so there is no
21368		 * need to swap them back.
21369		 */
21370		if (pwqeq->bpl_dmabuf)
21371			dmabuf = pwqeq->bpl_dmabuf;
21372		else
21373			return xritag;
21374
21375		bpl  = (struct ulp_bde64 *)dmabuf->virt;
21376		if (!bpl)
21377			return xritag;
21378
21379		for (i = 0; i < numBdes; i++) {
21380			/* Should already be byte swapped. */
21381			sgl->addr_hi = bpl->addrHigh;
21382			sgl->addr_lo = bpl->addrLow;
21383
21384			sgl->word2 = le32_to_cpu(sgl->word2);
21385			if ((i+1) == numBdes)
21386				bf_set(lpfc_sli4_sge_last, sgl, 1);
21387			else
21388				bf_set(lpfc_sli4_sge_last, sgl, 0);
21389			/* swap the size field back to the cpu so we
21390			 * can assign it to the sgl.
21391			 */
21392			bde.tus.w = le32_to_cpu(bpl->tus.w);
21393			sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
21394			/* The offsets in the sgl need to be accumulated
21395			 * separately for the request and reply lists.
21396			 * The request is always first, the reply follows.
21397			 */
21398			switch (cmd) {
21399			case CMD_GEN_REQUEST64_WQE:
21400				/* add up the reply sg entries */
21401				if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
21402					inbound++;
21403				/* first inbound? reset the offset */
21404				if (inbound == 1)
21405					offset = 0;
21406				bf_set(lpfc_sli4_sge_offset, sgl, offset);
21407				bf_set(lpfc_sli4_sge_type, sgl,
21408					LPFC_SGE_TYPE_DATA);
21409				offset += bde.tus.f.bdeSize;
21410				break;
21411			case CMD_FCP_TRSP64_WQE:
21412				bf_set(lpfc_sli4_sge_offset, sgl, 0);
21413				bf_set(lpfc_sli4_sge_type, sgl,
21414					LPFC_SGE_TYPE_DATA);
21415				break;
21416			case CMD_FCP_TSEND64_WQE:
21417			case CMD_FCP_TRECEIVE64_WQE:
21418				bf_set(lpfc_sli4_sge_type, sgl,
21419					bpl->tus.f.bdeFlags);
21420				if (i < 3)
21421					offset = 0;
21422				else
21423					offset += bde.tus.f.bdeSize;
21424				bf_set(lpfc_sli4_sge_offset, sgl, offset);
21425				break;
21426			}
21427			sgl->word2 = cpu_to_le32(sgl->word2);
21428			bpl++;
21429			sgl++;
21430		}
21431	} else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
21432		/* The addrHigh and addrLow fields of the BDE have not
21433		 * been byteswapped yet so they need to be swapped
21434		 * before putting them in the sgl.
21435		 */
21436		sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
21437		sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
21438		sgl->word2 = le32_to_cpu(sgl->word2);
21439		bf_set(lpfc_sli4_sge_last, sgl, 1);
21440		sgl->word2 = cpu_to_le32(sgl->word2);
21441		sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
21442	}
21443	return sglq->sli4_xritag;
21444}
21445
21446/**
21447 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
21448 * @phba: Pointer to HBA context object.
21449 * @qp: Pointer to HDW queue.
21450 * @pwqe: Pointer to command WQE.
21451 **/
21452int
21453lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21454		    struct lpfc_iocbq *pwqe)
21455{
21456	union lpfc_wqe128 *wqe = &pwqe->wqe;
21457	struct lpfc_async_xchg_ctx *ctxp;
21458	struct lpfc_queue *wq;
21459	struct lpfc_sglq *sglq;
21460	struct lpfc_sli_ring *pring;
21461	unsigned long iflags;
21462	int ret = 0;
21463
21464	/* NVME_LS and NVME_LS ABTS requests. */
21465	if (pwqe->cmd_flag & LPFC_IO_NVME_LS) {
21466		pring =  phba->sli4_hba.nvmels_wq->pring;
21467		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21468					  qp, wq_access);
21469		sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
21470		if (!sglq) {
21471			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21472			return WQE_BUSY;
21473		}
21474		pwqe->sli4_lxritag = sglq->sli4_lxritag;
21475		pwqe->sli4_xritag = sglq->sli4_xritag;
21476		if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
21477			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21478			return WQE_ERROR;
21479		}
21480		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21481		       pwqe->sli4_xritag);
21482		ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
21483		if (ret) {
21484			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21485			return ret;
21486		}
21487
21488		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21489		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21490
21491		lpfc_sli4_poll_eq(qp->hba_eq);
21492		return 0;
21493	}
21494
21495	/* NVME_FCREQ and NVME_ABTS requests */
21496	if (pwqe->cmd_flag & (LPFC_IO_NVME | LPFC_IO_FCP | LPFC_IO_CMF)) {
21497		/* Get the IO distribution (hba_wqidx) for WQ assignment. */
21498		wq = qp->io_wq;
21499		pring = wq->pring;
21500
21501		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21502
21503		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21504					  qp, wq_access);
21505		ret = lpfc_sli4_wq_put(wq, wqe);
21506		if (ret) {
21507			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21508			return ret;
21509		}
21510		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21511		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21512
21513		lpfc_sli4_poll_eq(qp->hba_eq);
21514		return 0;
21515	}
21516
21517	/* NVMET requests */
21518	if (pwqe->cmd_flag & LPFC_IO_NVMET) {
21519		/* Get the IO distribution (hba_wqidx) for WQ assignment. */
21520		wq = qp->io_wq;
21521		pring = wq->pring;
21522
21523		ctxp = pwqe->context_un.axchg;
21524		sglq = ctxp->ctxbuf->sglq;
21525		if (pwqe->sli4_xritag ==  NO_XRI) {
21526			pwqe->sli4_lxritag = sglq->sli4_lxritag;
21527			pwqe->sli4_xritag = sglq->sli4_xritag;
21528		}
21529		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21530		       pwqe->sli4_xritag);
21531		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
21532
21533		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21534					  qp, wq_access);
21535		ret = lpfc_sli4_wq_put(wq, wqe);
21536		if (ret) {
21537			spin_unlock_irqrestore(&pring->ring_lock, iflags);
21538			return ret;
21539		}
21540		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21541		spin_unlock_irqrestore(&pring->ring_lock, iflags);
21542
21543		lpfc_sli4_poll_eq(qp->hba_eq);
21544		return 0;
21545	}
21546	return WQE_ERROR;
21547}
21548
21549/**
21550 * lpfc_sli4_issue_abort_iotag - SLI-4 WQE init & issue for the Abort
21551 * @phba: Pointer to HBA context object.
21552 * @cmdiocb: Pointer to driver command iocb object.
21553 * @cmpl: completion function.
21554 *
21555 * Fill the appropriate fields for the abort WQE and call
21556 * internal routine lpfc_sli4_issue_wqe to send the WQE
21557 * This function is called with hbalock held and no ring_lock held.
21558 *
21559 * RETURNS 0 - SUCCESS
21560 **/
21561
21562int
21563lpfc_sli4_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
21564			    void *cmpl)
21565{
21566	struct lpfc_vport *vport = cmdiocb->vport;
21567	struct lpfc_iocbq *abtsiocb = NULL;
21568	union lpfc_wqe128 *abtswqe;
21569	struct lpfc_io_buf *lpfc_cmd;
21570	int retval = IOCB_ERROR;
21571	u16 xritag = cmdiocb->sli4_xritag;
21572
21573	/*
21574	 * The scsi command can not be in txq and it is in flight because the
21575	 * pCmd is still pointing at the SCSI command we have to abort. There
21576	 * is no need to search the txcmplq. Just send an abort to the FW.
21577	 */
21578
21579	abtsiocb = __lpfc_sli_get_iocbq(phba);
21580	if (!abtsiocb)
21581		return WQE_NORESOURCE;
21582
21583	/* Indicate the IO is being aborted by the driver. */
21584	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;
21585
21586	abtswqe = &abtsiocb->wqe;
21587	memset(abtswqe, 0, sizeof(*abtswqe));
21588
21589	if (!lpfc_is_link_up(phba) || (phba->link_flag & LS_EXTERNAL_LOOPBACK))
21590		bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 1);
21591	bf_set(abort_cmd_criteria, &abtswqe->abort_cmd, T_XRI_TAG);
21592	abtswqe->abort_cmd.rsrvd5 = 0;
21593	abtswqe->abort_cmd.wqe_com.abort_tag = xritag;
21594	bf_set(wqe_reqtag, &abtswqe->abort_cmd.wqe_com, abtsiocb->iotag);
21595	bf_set(wqe_cmnd, &abtswqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
21596	bf_set(wqe_xri_tag, &abtswqe->generic.wqe_com, 0);
21597	bf_set(wqe_qosd, &abtswqe->abort_cmd.wqe_com, 1);
21598	bf_set(wqe_lenloc, &abtswqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
21599	bf_set(wqe_cmd_type, &abtswqe->abort_cmd.wqe_com, OTHER_COMMAND);
21600
21601	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
21602	abtsiocb->hba_wqidx = cmdiocb->hba_wqidx;
21603	abtsiocb->cmd_flag |= LPFC_USE_FCPWQIDX;
21604	if (cmdiocb->cmd_flag & LPFC_IO_FCP)
21605		abtsiocb->cmd_flag |= LPFC_IO_FCP;
21606	if (cmdiocb->cmd_flag & LPFC_IO_NVME)
21607		abtsiocb->cmd_flag |= LPFC_IO_NVME;
21608	if (cmdiocb->cmd_flag & LPFC_IO_FOF)
21609		abtsiocb->cmd_flag |= LPFC_IO_FOF;
21610	abtsiocb->vport = vport;
21611	abtsiocb->cmd_cmpl = cmpl;
21612
21613	lpfc_cmd = container_of(cmdiocb, struct lpfc_io_buf, cur_iocbq);
21614	retval = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, abtsiocb);
21615
21616	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
21617			 "0359 Abort xri x%x, original iotag x%x, "
21618			 "abort cmd iotag x%x retval x%x\n",
21619			 xritag, cmdiocb->iotag, abtsiocb->iotag, retval);
21620
21621	if (retval) {
21622		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;
21623		__lpfc_sli_release_iocbq(phba, abtsiocb);
21624	}
21625
21626	return retval;
21627}
21628
21629#ifdef LPFC_MXP_STAT
21630/**
21631 * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
21632 * @phba: pointer to lpfc hba data structure.
21633 * @hwqid: belong to which HWQ.
21634 *
21635 * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
21636 * 15 seconds after a test case is running.
21637 *
21638 * The user should call lpfc_debugfs_multixripools_write before running a test
21639 * case to clear stat_snapshot_taken. Then the user starts a test case. During
21640 * test case is running, stat_snapshot_taken is incremented by 1 every time when
21641 * this routine is called from heartbeat timer. When stat_snapshot_taken is
21642 * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
21643 **/
21644void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
21645{
21646	struct lpfc_sli4_hdw_queue *qp;
21647	struct lpfc_multixri_pool *multixri_pool;
21648	struct lpfc_pvt_pool *pvt_pool;
21649	struct lpfc_pbl_pool *pbl_pool;
21650	u32 txcmplq_cnt;
21651
21652	qp = &phba->sli4_hba.hdwq[hwqid];
21653	multixri_pool = qp->p_multixri_pool;
21654	if (!multixri_pool)
21655		return;
21656
21657	if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
21658		pvt_pool = &qp->p_multixri_pool->pvt_pool;
21659		pbl_pool = &qp->p_multixri_pool->pbl_pool;
21660		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21661
21662		multixri_pool->stat_pbl_count = pbl_pool->count;
21663		multixri_pool->stat_pvt_count = pvt_pool->count;
21664		multixri_pool->stat_busy_count = txcmplq_cnt;
21665	}
21666
21667	multixri_pool->stat_snapshot_taken++;
21668}
21669#endif
21670
21671/**
21672 * lpfc_adjust_pvt_pool_count - Adjust private pool count
21673 * @phba: pointer to lpfc hba data structure.
21674 * @hwqid: belong to which HWQ.
21675 *
21676 * This routine moves some XRIs from private to public pool when private pool
21677 * is not busy.
21678 **/
21679void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
21680{
21681	struct lpfc_multixri_pool *multixri_pool;
21682	u32 io_req_count;
21683	u32 prev_io_req_count;
21684
21685	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21686	if (!multixri_pool)
21687		return;
21688	io_req_count = multixri_pool->io_req_count;
21689	prev_io_req_count = multixri_pool->prev_io_req_count;
21690
21691	if (prev_io_req_count != io_req_count) {
21692		/* Private pool is busy */
21693		multixri_pool->prev_io_req_count = io_req_count;
21694	} else {
21695		/* Private pool is not busy.
21696		 * Move XRIs from private to public pool.
21697		 */
21698		lpfc_move_xri_pvt_to_pbl(phba, hwqid);
21699	}
21700}
21701
21702/**
21703 * lpfc_adjust_high_watermark - Adjust high watermark
21704 * @phba: pointer to lpfc hba data structure.
21705 * @hwqid: belong to which HWQ.
21706 *
21707 * This routine sets high watermark as number of outstanding XRIs,
21708 * but make sure the new value is between xri_limit/2 and xri_limit.
21709 **/
21710void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
21711{
21712	u32 new_watermark;
21713	u32 watermark_max;
21714	u32 watermark_min;
21715	u32 xri_limit;
21716	u32 txcmplq_cnt;
21717	u32 abts_io_bufs;
21718	struct lpfc_multixri_pool *multixri_pool;
21719	struct lpfc_sli4_hdw_queue *qp;
21720
21721	qp = &phba->sli4_hba.hdwq[hwqid];
21722	multixri_pool = qp->p_multixri_pool;
21723	if (!multixri_pool)
21724		return;
21725	xri_limit = multixri_pool->xri_limit;
21726
21727	watermark_max = xri_limit;
21728	watermark_min = xri_limit / 2;
21729
21730	txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
21731	abts_io_bufs = qp->abts_scsi_io_bufs;
21732	abts_io_bufs += qp->abts_nvme_io_bufs;
21733
21734	new_watermark = txcmplq_cnt + abts_io_bufs;
21735	new_watermark = min(watermark_max, new_watermark);
21736	new_watermark = max(watermark_min, new_watermark);
21737	multixri_pool->pvt_pool.high_watermark = new_watermark;
21738
21739#ifdef LPFC_MXP_STAT
21740	multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
21741					  new_watermark);
21742#endif
21743}
21744
21745/**
21746 * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
21747 * @phba: pointer to lpfc hba data structure.
21748 * @hwqid: belong to which HWQ.
21749 *
21750 * This routine is called from hearbeat timer when pvt_pool is idle.
21751 * All free XRIs are moved from private to public pool on hwqid with 2 steps.
21752 * The first step moves (all - low_watermark) amount of XRIs.
21753 * The second step moves the rest of XRIs.
21754 **/
21755void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
21756{
21757	struct lpfc_pbl_pool *pbl_pool;
21758	struct lpfc_pvt_pool *pvt_pool;
21759	struct lpfc_sli4_hdw_queue *qp;
21760	struct lpfc_io_buf *lpfc_ncmd;
21761	struct lpfc_io_buf *lpfc_ncmd_next;
21762	unsigned long iflag;
21763	struct list_head tmp_list;
21764	u32 tmp_count;
21765
21766	qp = &phba->sli4_hba.hdwq[hwqid];
21767	pbl_pool = &qp->p_multixri_pool->pbl_pool;
21768	pvt_pool = &qp->p_multixri_pool->pvt_pool;
21769	tmp_count = 0;
21770
21771	lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
21772	lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
21773
21774	if (pvt_pool->count > pvt_pool->low_watermark) {
21775		/* Step 1: move (all - low_watermark) from pvt_pool
21776		 * to pbl_pool
21777		 */
21778
21779		/* Move low watermark of bufs from pvt_pool to tmp_list */
21780		INIT_LIST_HEAD(&tmp_list);
21781		list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21782					 &pvt_pool->list, list) {
21783			list_move_tail(&lpfc_ncmd->list, &tmp_list);
21784			tmp_count++;
21785			if (tmp_count >= pvt_pool->low_watermark)
21786				break;
21787		}
21788
21789		/* Move all bufs from pvt_pool to pbl_pool */
21790		list_splice_init(&pvt_pool->list, &pbl_pool->list);
21791
21792		/* Move all bufs from tmp_list to pvt_pool */
21793		list_splice(&tmp_list, &pvt_pool->list);
21794
21795		pbl_pool->count += (pvt_pool->count - tmp_count);
21796		pvt_pool->count = tmp_count;
21797	} else {
21798		/* Step 2: move the rest from pvt_pool to pbl_pool */
21799		list_splice_init(&pvt_pool->list, &pbl_pool->list);
21800		pbl_pool->count += pvt_pool->count;
21801		pvt_pool->count = 0;
21802	}
21803
21804	spin_unlock(&pvt_pool->lock);
21805	spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21806}
21807
21808/**
21809 * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21810 * @phba: pointer to lpfc hba data structure
21811 * @qp: pointer to HDW queue
21812 * @pbl_pool: specified public free XRI pool
21813 * @pvt_pool: specified private free XRI pool
21814 * @count: number of XRIs to move
21815 *
21816 * This routine tries to move some free common bufs from the specified pbl_pool
21817 * to the specified pvt_pool. It might move less than count XRIs if there's not
21818 * enough in public pool.
21819 *
21820 * Return:
21821 *   true - if XRIs are successfully moved from the specified pbl_pool to the
21822 *          specified pvt_pool
21823 *   false - if the specified pbl_pool is empty or locked by someone else
21824 **/
21825static bool
21826_lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21827			  struct lpfc_pbl_pool *pbl_pool,
21828			  struct lpfc_pvt_pool *pvt_pool, u32 count)
21829{
21830	struct lpfc_io_buf *lpfc_ncmd;
21831	struct lpfc_io_buf *lpfc_ncmd_next;
21832	unsigned long iflag;
21833	int ret;
21834
21835	ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
21836	if (ret) {
21837		if (pbl_pool->count) {
21838			/* Move a batch of XRIs from public to private pool */
21839			lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
21840			list_for_each_entry_safe(lpfc_ncmd,
21841						 lpfc_ncmd_next,
21842						 &pbl_pool->list,
21843						 list) {
21844				list_move_tail(&lpfc_ncmd->list,
21845					       &pvt_pool->list);
21846				pvt_pool->count++;
21847				pbl_pool->count--;
21848				count--;
21849				if (count == 0)
21850					break;
21851			}
21852
21853			spin_unlock(&pvt_pool->lock);
21854			spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21855			return true;
21856		}
21857		spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21858	}
21859
21860	return false;
21861}
21862
21863/**
21864 * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21865 * @phba: pointer to lpfc hba data structure.
21866 * @hwqid: belong to which HWQ.
21867 * @count: number of XRIs to move
21868 *
21869 * This routine tries to find some free common bufs in one of public pools with
21870 * Round Robin method. The search always starts from local hwqid, then the next
21871 * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
21872 * a batch of free common bufs are moved to private pool on hwqid.
21873 * It might move less than count XRIs if there's not enough in public pool.
21874 **/
21875void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
21876{
21877	struct lpfc_multixri_pool *multixri_pool;
21878	struct lpfc_multixri_pool *next_multixri_pool;
21879	struct lpfc_pvt_pool *pvt_pool;
21880	struct lpfc_pbl_pool *pbl_pool;
21881	struct lpfc_sli4_hdw_queue *qp;
21882	u32 next_hwqid;
21883	u32 hwq_count;
21884	int ret;
21885
21886	qp = &phba->sli4_hba.hdwq[hwqid];
21887	multixri_pool = qp->p_multixri_pool;
21888	pvt_pool = &multixri_pool->pvt_pool;
21889	pbl_pool = &multixri_pool->pbl_pool;
21890
21891	/* Check if local pbl_pool is available */
21892	ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
21893	if (ret) {
21894#ifdef LPFC_MXP_STAT
21895		multixri_pool->local_pbl_hit_count++;
21896#endif
21897		return;
21898	}
21899
21900	hwq_count = phba->cfg_hdw_queue;
21901
21902	/* Get the next hwqid which was found last time */
21903	next_hwqid = multixri_pool->rrb_next_hwqid;
21904
21905	do {
21906		/* Go to next hwq */
21907		next_hwqid = (next_hwqid + 1) % hwq_count;
21908
21909		next_multixri_pool =
21910			phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
21911		pbl_pool = &next_multixri_pool->pbl_pool;
21912
21913		/* Check if the public free xri pool is available */
21914		ret = _lpfc_move_xri_pbl_to_pvt(
21915			phba, qp, pbl_pool, pvt_pool, count);
21916
21917		/* Exit while-loop if success or all hwqid are checked */
21918	} while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
21919
21920	/* Starting point for the next time */
21921	multixri_pool->rrb_next_hwqid = next_hwqid;
21922
21923	if (!ret) {
21924		/* stats: all public pools are empty*/
21925		multixri_pool->pbl_empty_count++;
21926	}
21927
21928#ifdef LPFC_MXP_STAT
21929	if (ret) {
21930		if (next_hwqid == hwqid)
21931			multixri_pool->local_pbl_hit_count++;
21932		else
21933			multixri_pool->other_pbl_hit_count++;
21934	}
21935#endif
21936}
21937
21938/**
21939 * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
21940 * @phba: pointer to lpfc hba data structure.
21941 * @hwqid: belong to which HWQ.
21942 *
21943 * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
21944 * low watermark.
21945 **/
21946void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
21947{
21948	struct lpfc_multixri_pool *multixri_pool;
21949	struct lpfc_pvt_pool *pvt_pool;
21950
21951	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21952	pvt_pool = &multixri_pool->pvt_pool;
21953
21954	if (pvt_pool->count < pvt_pool->low_watermark)
21955		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21956}
21957
21958/**
21959 * lpfc_release_io_buf - Return one IO buf back to free pool
21960 * @phba: pointer to lpfc hba data structure.
21961 * @lpfc_ncmd: IO buf to be returned.
21962 * @qp: belong to which HWQ.
21963 *
21964 * This routine returns one IO buf back to free pool. If this is an urgent IO,
21965 * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
21966 * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
21967 * xri_limit.  If cfg_xri_rebalancing==0, the IO buf is returned to
21968 * lpfc_io_buf_list_put.
21969 **/
21970void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
21971			 struct lpfc_sli4_hdw_queue *qp)
21972{
21973	unsigned long iflag;
21974	struct lpfc_pbl_pool *pbl_pool;
21975	struct lpfc_pvt_pool *pvt_pool;
21976	struct lpfc_epd_pool *epd_pool;
21977	u32 txcmplq_cnt;
21978	u32 xri_owned;
21979	u32 xri_limit;
21980	u32 abts_io_bufs;
21981
21982	/* MUST zero fields if buffer is reused by another protocol */
21983	lpfc_ncmd->nvmeCmd = NULL;
21984	lpfc_ncmd->cur_iocbq.cmd_cmpl = NULL;
21985
21986	if (phba->cfg_xpsgl && !phba->nvmet_support &&
21987	    !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
21988		lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
21989
21990	if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
21991		lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
21992
21993	if (phba->cfg_xri_rebalancing) {
21994		if (lpfc_ncmd->expedite) {
21995			/* Return to expedite pool */
21996			epd_pool = &phba->epd_pool;
21997			spin_lock_irqsave(&epd_pool->lock, iflag);
21998			list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
21999			epd_pool->count++;
22000			spin_unlock_irqrestore(&epd_pool->lock, iflag);
22001			return;
22002		}
22003
22004		/* Avoid invalid access if an IO sneaks in and is being rejected
22005		 * just _after_ xri pools are destroyed in lpfc_offline.
22006		 * Nothing much can be done at this point.
22007		 */
22008		if (!qp->p_multixri_pool)
22009			return;
22010
22011		pbl_pool = &qp->p_multixri_pool->pbl_pool;
22012		pvt_pool = &qp->p_multixri_pool->pvt_pool;
22013
22014		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
22015		abts_io_bufs = qp->abts_scsi_io_bufs;
22016		abts_io_bufs += qp->abts_nvme_io_bufs;
22017
22018		xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
22019		xri_limit = qp->p_multixri_pool->xri_limit;
22020
22021#ifdef LPFC_MXP_STAT
22022		if (xri_owned <= xri_limit)
22023			qp->p_multixri_pool->below_limit_count++;
22024		else
22025			qp->p_multixri_pool->above_limit_count++;
22026#endif
22027
22028		/* XRI goes to either public or private free xri pool
22029		 *     based on watermark and xri_limit
22030		 */
22031		if ((pvt_pool->count < pvt_pool->low_watermark) ||
22032		    (xri_owned < xri_limit &&
22033		     pvt_pool->count < pvt_pool->high_watermark)) {
22034			lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
22035						  qp, free_pvt_pool);
22036			list_add_tail(&lpfc_ncmd->list,
22037				      &pvt_pool->list);
22038			pvt_pool->count++;
22039			spin_unlock_irqrestore(&pvt_pool->lock, iflag);
22040		} else {
22041			lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
22042						  qp, free_pub_pool);
22043			list_add_tail(&lpfc_ncmd->list,
22044				      &pbl_pool->list);
22045			pbl_pool->count++;
22046			spin_unlock_irqrestore(&pbl_pool->lock, iflag);
22047		}
22048	} else {
22049		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
22050					  qp, free_xri);
22051		list_add_tail(&lpfc_ncmd->list,
22052			      &qp->lpfc_io_buf_list_put);
22053		qp->put_io_bufs++;
22054		spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
22055				       iflag);
22056	}
22057}
22058
22059/**
22060 * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
22061 * @phba: pointer to lpfc hba data structure.
22062 * @qp: pointer to HDW queue
22063 * @pvt_pool: pointer to private pool data structure.
22064 * @ndlp: pointer to lpfc nodelist data structure.
22065 *
22066 * This routine tries to get one free IO buf from private pool.
22067 *
22068 * Return:
22069 *   pointer to one free IO buf - if private pool is not empty
22070 *   NULL - if private pool is empty
22071 **/
22072static struct lpfc_io_buf *
22073lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
22074				  struct lpfc_sli4_hdw_queue *qp,
22075				  struct lpfc_pvt_pool *pvt_pool,
22076				  struct lpfc_nodelist *ndlp)
22077{
22078	struct lpfc_io_buf *lpfc_ncmd;
22079	struct lpfc_io_buf *lpfc_ncmd_next;
22080	unsigned long iflag;
22081
22082	lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
22083	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
22084				 &pvt_pool->list, list) {
22085		if (lpfc_test_rrq_active(
22086			phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
22087			continue;
22088		list_del(&lpfc_ncmd->list);
22089		pvt_pool->count--;
22090		spin_unlock_irqrestore(&pvt_pool->lock, iflag);
22091		return lpfc_ncmd;
22092	}
22093	spin_unlock_irqrestore(&pvt_pool->lock, iflag);
22094
22095	return NULL;
22096}
22097
22098/**
22099 * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
22100 * @phba: pointer to lpfc hba data structure.
22101 *
22102 * This routine tries to get one free IO buf from expedite pool.
22103 *
22104 * Return:
22105 *   pointer to one free IO buf - if expedite pool is not empty
22106 *   NULL - if expedite pool is empty
22107 **/
22108static struct lpfc_io_buf *
22109lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
22110{
22111	struct lpfc_io_buf *lpfc_ncmd = NULL, *iter;
22112	struct lpfc_io_buf *lpfc_ncmd_next;
22113	unsigned long iflag;
22114	struct lpfc_epd_pool *epd_pool;
22115
22116	epd_pool = &phba->epd_pool;
22117
22118	spin_lock_irqsave(&epd_pool->lock, iflag);
22119	if (epd_pool->count > 0) {
22120		list_for_each_entry_safe(iter, lpfc_ncmd_next,
22121					 &epd_pool->list, list) {
22122			list_del(&iter->list);
22123			epd_pool->count--;
22124			lpfc_ncmd = iter;
22125			break;
22126		}
22127	}
22128	spin_unlock_irqrestore(&epd_pool->lock, iflag);
22129
22130	return lpfc_ncmd;
22131}
22132
22133/**
22134 * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
22135 * @phba: pointer to lpfc hba data structure.
22136 * @ndlp: pointer to lpfc nodelist data structure.
22137 * @hwqid: belong to which HWQ
22138 * @expedite: 1 means this request is urgent.
22139 *
22140 * This routine will do the following actions and then return a pointer to
22141 * one free IO buf.
22142 *
22143 * 1. If private free xri count is empty, move some XRIs from public to
22144 *    private pool.
22145 * 2. Get one XRI from private free xri pool.
22146 * 3. If we fail to get one from pvt_pool and this is an expedite request,
22147 *    get one free xri from expedite pool.
22148 *
22149 * Note: ndlp is only used on SCSI side for RRQ testing.
22150 *       The caller should pass NULL for ndlp on NVME side.
22151 *
22152 * Return:
22153 *   pointer to one free IO buf - if private pool is not empty
22154 *   NULL - if private pool is empty
22155 **/
22156static struct lpfc_io_buf *
22157lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
22158				    struct lpfc_nodelist *ndlp,
22159				    int hwqid, int expedite)
22160{
22161	struct lpfc_sli4_hdw_queue *qp;
22162	struct lpfc_multixri_pool *multixri_pool;
22163	struct lpfc_pvt_pool *pvt_pool;
22164	struct lpfc_io_buf *lpfc_ncmd;
22165
22166	qp = &phba->sli4_hba.hdwq[hwqid];
22167	lpfc_ncmd = NULL;
22168	if (!qp) {
22169		lpfc_printf_log(phba, KERN_INFO,
22170				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22171				"5556 NULL qp for hwqid  x%x\n", hwqid);
22172		return lpfc_ncmd;
22173	}
22174	multixri_pool = qp->p_multixri_pool;
22175	if (!multixri_pool) {
22176		lpfc_printf_log(phba, KERN_INFO,
22177				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22178				"5557 NULL multixri for hwqid  x%x\n", hwqid);
22179		return lpfc_ncmd;
22180	}
22181	pvt_pool = &multixri_pool->pvt_pool;
22182	if (!pvt_pool) {
22183		lpfc_printf_log(phba, KERN_INFO,
22184				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22185				"5558 NULL pvt_pool for hwqid  x%x\n", hwqid);
22186		return lpfc_ncmd;
22187	}
22188	multixri_pool->io_req_count++;
22189
22190	/* If pvt_pool is empty, move some XRIs from public to private pool */
22191	if (pvt_pool->count == 0)
22192		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
22193
22194	/* Get one XRI from private free xri pool */
22195	lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
22196
22197	if (lpfc_ncmd) {
22198		lpfc_ncmd->hdwq = qp;
22199		lpfc_ncmd->hdwq_no = hwqid;
22200	} else if (expedite) {
22201		/* If we fail to get one from pvt_pool and this is an expedite
22202		 * request, get one free xri from expedite pool.
22203		 */
22204		lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
22205	}
22206
22207	return lpfc_ncmd;
22208}
22209
22210static inline struct lpfc_io_buf *
22211lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
22212{
22213	struct lpfc_sli4_hdw_queue *qp;
22214	struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
22215
22216	qp = &phba->sli4_hba.hdwq[idx];
22217	list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
22218				 &qp->lpfc_io_buf_list_get, list) {
22219		if (lpfc_test_rrq_active(phba, ndlp,
22220					 lpfc_cmd->cur_iocbq.sli4_lxritag))
22221			continue;
22222
22223		if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
22224			continue;
22225
22226		list_del_init(&lpfc_cmd->list);
22227		qp->get_io_bufs--;
22228		lpfc_cmd->hdwq = qp;
22229		lpfc_cmd->hdwq_no = idx;
22230		return lpfc_cmd;
22231	}
22232	return NULL;
22233}
22234
22235/**
22236 * lpfc_get_io_buf - Get one IO buffer from free pool
22237 * @phba: The HBA for which this call is being executed.
22238 * @ndlp: pointer to lpfc nodelist data structure.
22239 * @hwqid: belong to which HWQ
22240 * @expedite: 1 means this request is urgent.
22241 *
22242 * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
22243 * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
22244 * a IO buffer from head of @hdwq io_buf_list and returns to caller.
22245 *
22246 * Note: ndlp is only used on SCSI side for RRQ testing.
22247 *       The caller should pass NULL for ndlp on NVME side.
22248 *
22249 * Return codes:
22250 *   NULL - Error
22251 *   Pointer to lpfc_io_buf - Success
22252 **/
22253struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
22254				    struct lpfc_nodelist *ndlp,
22255				    u32 hwqid, int expedite)
22256{
22257	struct lpfc_sli4_hdw_queue *qp;
22258	unsigned long iflag;
22259	struct lpfc_io_buf *lpfc_cmd;
22260
22261	qp = &phba->sli4_hba.hdwq[hwqid];
22262	lpfc_cmd = NULL;
22263	if (!qp) {
22264		lpfc_printf_log(phba, KERN_WARNING,
22265				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22266				"5555 NULL qp for hwqid  x%x\n", hwqid);
22267		return lpfc_cmd;
22268	}
22269
22270	if (phba->cfg_xri_rebalancing)
22271		lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
22272			phba, ndlp, hwqid, expedite);
22273	else {
22274		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
22275					  qp, alloc_xri_get);
22276		if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
22277			lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22278		if (!lpfc_cmd) {
22279			lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
22280					  qp, alloc_xri_put);
22281			list_splice(&qp->lpfc_io_buf_list_put,
22282				    &qp->lpfc_io_buf_list_get);
22283			qp->get_io_bufs += qp->put_io_bufs;
22284			INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
22285			qp->put_io_bufs = 0;
22286			spin_unlock(&qp->io_buf_list_put_lock);
22287			if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
22288			    expedite)
22289				lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22290		}
22291		spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
22292	}
22293
22294	return lpfc_cmd;
22295}
22296
22297/**
22298 * lpfc_read_object - Retrieve object data from HBA
22299 * @phba: The HBA for which this call is being executed.
22300 * @rdobject: Pathname of object data we want to read.
22301 * @datap: Pointer to where data will be copied to.
22302 * @datasz: size of data area
22303 *
22304 * This routine is limited to object sizes of LPFC_BPL_SIZE (1024) or less.
22305 * The data will be truncated if datasz is not large enough.
22306 * Version 1 is not supported with Embedded mbox cmd, so we must use version 0.
22307 * Returns the actual bytes read from the object.
22308 *
22309 * This routine is hard coded to use a poll completion.  Unlike other
22310 * sli4_config mailboxes, it uses lpfc_mbuf memory which is not
22311 * cleaned up in lpfc_sli4_cmd_mbox_free.  If this routine is modified
22312 * to use interrupt-based completions, code is needed to fully cleanup
22313 * the memory.
22314 */
22315int
22316lpfc_read_object(struct lpfc_hba *phba, char *rdobject, uint32_t *datap,
22317		 uint32_t datasz)
22318{
22319	struct lpfc_mbx_read_object *read_object;
22320	LPFC_MBOXQ_t *mbox;
22321	int rc, length, eof, j, byte_cnt = 0;
22322	uint32_t shdr_status, shdr_add_status;
22323	union lpfc_sli4_cfg_shdr *shdr;
22324	struct lpfc_dmabuf *pcmd;
22325	u32 rd_object_name[LPFC_MBX_OBJECT_NAME_LEN_DW] = {0};
22326
22327	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
22328	if (!mbox)
22329		return -ENOMEM;
22330	length = (sizeof(struct lpfc_mbx_read_object) -
22331		  sizeof(struct lpfc_sli4_cfg_mhdr));
22332	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
22333			 LPFC_MBOX_OPCODE_READ_OBJECT,
22334			 length, LPFC_SLI4_MBX_EMBED);
22335	read_object = &mbox->u.mqe.un.read_object;
22336	shdr = (union lpfc_sli4_cfg_shdr *)&read_object->header.cfg_shdr;
22337
22338	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_0);
22339	bf_set(lpfc_mbx_rd_object_rlen, &read_object->u.request, datasz);
22340	read_object->u.request.rd_object_offset = 0;
22341	read_object->u.request.rd_object_cnt = 1;
22342
22343	memset((void *)read_object->u.request.rd_object_name, 0,
22344	       LPFC_OBJ_NAME_SZ);
22345	scnprintf((char *)rd_object_name, sizeof(rd_object_name), rdobject);
22346	for (j = 0; j < strlen(rdobject); j++)
22347		read_object->u.request.rd_object_name[j] =
22348			cpu_to_le32(rd_object_name[j]);
22349
22350	pcmd = kmalloc(sizeof(*pcmd), GFP_KERNEL);
22351	if (pcmd)
22352		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
22353	if (!pcmd || !pcmd->virt) {
22354		kfree(pcmd);
22355		mempool_free(mbox, phba->mbox_mem_pool);
22356		return -ENOMEM;
22357	}
22358	memset((void *)pcmd->virt, 0, LPFC_BPL_SIZE);
22359	read_object->u.request.rd_object_hbuf[0].pa_lo =
22360		putPaddrLow(pcmd->phys);
22361	read_object->u.request.rd_object_hbuf[0].pa_hi =
22362		putPaddrHigh(pcmd->phys);
22363	read_object->u.request.rd_object_hbuf[0].length = LPFC_BPL_SIZE;
22364
22365	mbox->vport = phba->pport;
22366	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
22367	mbox->ctx_ndlp = NULL;
22368
22369	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
22370	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
22371	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
22372
22373	if (shdr_status == STATUS_FAILED &&
22374	    shdr_add_status == ADD_STATUS_INVALID_OBJECT_NAME) {
22375		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22376				"4674 No port cfg file in FW.\n");
22377		byte_cnt = -ENOENT;
22378	} else if (shdr_status || shdr_add_status || rc) {
22379		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22380				"2625 READ_OBJECT mailbox failed with "
22381				"status x%x add_status x%x, mbx status x%x\n",
22382				shdr_status, shdr_add_status, rc);
22383		byte_cnt = -ENXIO;
22384	} else {
22385		/* Success */
22386		length = read_object->u.response.rd_object_actual_rlen;
22387		eof = bf_get(lpfc_mbx_rd_object_eof, &read_object->u.response);
22388		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_CGN_MGMT,
22389				"2626 READ_OBJECT Success len %d:%d, EOF %d\n",
22390				length, datasz, eof);
22391
22392		/* Detect the port config file exists but is empty */
22393		if (!length && eof) {
22394			byte_cnt = 0;
22395			goto exit;
22396		}
22397
22398		byte_cnt = length;
22399		lpfc_sli_pcimem_bcopy(pcmd->virt, datap, byte_cnt);
22400	}
22401
22402 exit:
22403	/* This is an embedded SLI4 mailbox with an external buffer allocated.
22404	 * Free the pcmd and then cleanup with the correct routine.
22405	 */
22406	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
22407	kfree(pcmd);
22408	lpfc_sli4_mbox_cmd_free(phba, mbox);
22409	return byte_cnt;
22410}
22411
22412/**
22413 * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
22414 * @phba: The HBA for which this call is being executed.
22415 * @lpfc_buf: IO buf structure to append the SGL chunk
22416 *
22417 * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
22418 * and will allocate an SGL chunk if the pool is empty.
22419 *
22420 * Return codes:
22421 *   NULL - Error
22422 *   Pointer to sli4_hybrid_sgl - Success
22423 **/
22424struct sli4_hybrid_sgl *
22425lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22426{
22427	struct sli4_hybrid_sgl *list_entry = NULL;
22428	struct sli4_hybrid_sgl *tmp = NULL;
22429	struct sli4_hybrid_sgl *allocated_sgl = NULL;
22430	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22431	struct list_head *buf_list = &hdwq->sgl_list;
22432	unsigned long iflags;
22433
22434	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22435
22436	if (likely(!list_empty(buf_list))) {
22437		/* break off 1 chunk from the sgl_list */
22438		list_for_each_entry_safe(list_entry, tmp,
22439					 buf_list, list_node) {
22440			list_move_tail(&list_entry->list_node,
22441				       &lpfc_buf->dma_sgl_xtra_list);
22442			break;
22443		}
22444	} else {
22445		/* allocate more */
22446		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22447		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22448				   cpu_to_node(hdwq->io_wq->chann));
22449		if (!tmp) {
22450			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22451					"8353 error kmalloc memory for HDWQ "
22452					"%d %s\n",
22453					lpfc_buf->hdwq_no, __func__);
22454			return NULL;
22455		}
22456
22457		tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
22458					      GFP_ATOMIC, &tmp->dma_phys_sgl);
22459		if (!tmp->dma_sgl) {
22460			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22461					"8354 error pool_alloc memory for HDWQ "
22462					"%d %s\n",
22463					lpfc_buf->hdwq_no, __func__);
22464			kfree(tmp);
22465			return NULL;
22466		}
22467
22468		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22469		list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
22470	}
22471
22472	allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
22473					struct sli4_hybrid_sgl,
22474					list_node);
22475
22476	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22477
22478	return allocated_sgl;
22479}
22480
22481/**
22482 * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
22483 * @phba: The HBA for which this call is being executed.
22484 * @lpfc_buf: IO buf structure with the SGL chunk
22485 *
22486 * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
22487 *
22488 * Return codes:
22489 *   0 - Success
22490 *   -EINVAL - Error
22491 **/
22492int
22493lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22494{
22495	int rc = 0;
22496	struct sli4_hybrid_sgl *list_entry = NULL;
22497	struct sli4_hybrid_sgl *tmp = NULL;
22498	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22499	struct list_head *buf_list = &hdwq->sgl_list;
22500	unsigned long iflags;
22501
22502	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22503
22504	if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
22505		list_for_each_entry_safe(list_entry, tmp,
22506					 &lpfc_buf->dma_sgl_xtra_list,
22507					 list_node) {
22508			list_move_tail(&list_entry->list_node,
22509				       buf_list);
22510		}
22511	} else {
22512		rc = -EINVAL;
22513	}
22514
22515	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22516	return rc;
22517}
22518
22519/**
22520 * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
22521 * @phba: phba object
22522 * @hdwq: hdwq to cleanup sgl buff resources on
22523 *
22524 * This routine frees all SGL chunks of hdwq SGL chunk pool.
22525 *
22526 * Return codes:
22527 *   None
22528 **/
22529void
22530lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
22531		       struct lpfc_sli4_hdw_queue *hdwq)
22532{
22533	struct list_head *buf_list = &hdwq->sgl_list;
22534	struct sli4_hybrid_sgl *list_entry = NULL;
22535	struct sli4_hybrid_sgl *tmp = NULL;
22536	unsigned long iflags;
22537
22538	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22539
22540	/* Free sgl pool */
22541	list_for_each_entry_safe(list_entry, tmp,
22542				 buf_list, list_node) {
22543		list_del(&list_entry->list_node);
22544		dma_pool_free(phba->lpfc_sg_dma_buf_pool,
22545			      list_entry->dma_sgl,
22546			      list_entry->dma_phys_sgl);
22547		kfree(list_entry);
22548	}
22549
22550	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22551}
22552
22553/**
22554 * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
22555 * @phba: The HBA for which this call is being executed.
22556 * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
22557 *
22558 * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
22559 * and will allocate an CMD/RSP buffer if the pool is empty.
22560 *
22561 * Return codes:
22562 *   NULL - Error
22563 *   Pointer to fcp_cmd_rsp_buf - Success
22564 **/
22565struct fcp_cmd_rsp_buf *
22566lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22567			      struct lpfc_io_buf *lpfc_buf)
22568{
22569	struct fcp_cmd_rsp_buf *list_entry = NULL;
22570	struct fcp_cmd_rsp_buf *tmp = NULL;
22571	struct fcp_cmd_rsp_buf *allocated_buf = NULL;
22572	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22573	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22574	unsigned long iflags;
22575
22576	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22577
22578	if (likely(!list_empty(buf_list))) {
22579		/* break off 1 chunk from the list */
22580		list_for_each_entry_safe(list_entry, tmp,
22581					 buf_list,
22582					 list_node) {
22583			list_move_tail(&list_entry->list_node,
22584				       &lpfc_buf->dma_cmd_rsp_list);
22585			break;
22586		}
22587	} else {
22588		/* allocate more */
22589		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22590		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
22591				   cpu_to_node(hdwq->io_wq->chann));
22592		if (!tmp) {
22593			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22594					"8355 error kmalloc memory for HDWQ "
22595					"%d %s\n",
22596					lpfc_buf->hdwq_no, __func__);
22597			return NULL;
22598		}
22599
22600		tmp->fcp_cmnd = dma_pool_zalloc(phba->lpfc_cmd_rsp_buf_pool,
22601						GFP_ATOMIC,
22602						&tmp->fcp_cmd_rsp_dma_handle);
22603
22604		if (!tmp->fcp_cmnd) {
22605			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22606					"8356 error pool_alloc memory for HDWQ "
22607					"%d %s\n",
22608					lpfc_buf->hdwq_no, __func__);
22609			kfree(tmp);
22610			return NULL;
22611		}
22612
22613		tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
22614				sizeof(struct fcp_cmnd32));
22615
22616		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22617		list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
22618	}
22619
22620	allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
22621					struct fcp_cmd_rsp_buf,
22622					list_node);
22623
22624	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22625
22626	return allocated_buf;
22627}
22628
22629/**
22630 * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
22631 * @phba: The HBA for which this call is being executed.
22632 * @lpfc_buf: IO buf structure with the CMD/RSP buf
22633 *
22634 * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
22635 *
22636 * Return codes:
22637 *   0 - Success
22638 *   -EINVAL - Error
22639 **/
22640int
22641lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22642			      struct lpfc_io_buf *lpfc_buf)
22643{
22644	int rc = 0;
22645	struct fcp_cmd_rsp_buf *list_entry = NULL;
22646	struct fcp_cmd_rsp_buf *tmp = NULL;
22647	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22648	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22649	unsigned long iflags;
22650
22651	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22652
22653	if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
22654		list_for_each_entry_safe(list_entry, tmp,
22655					 &lpfc_buf->dma_cmd_rsp_list,
22656					 list_node) {
22657			list_move_tail(&list_entry->list_node,
22658				       buf_list);
22659		}
22660	} else {
22661		rc = -EINVAL;
22662	}
22663
22664	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22665	return rc;
22666}
22667
22668/**
22669 * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
22670 * @phba: phba object
22671 * @hdwq: hdwq to cleanup cmd rsp buff resources on
22672 *
22673 * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
22674 *
22675 * Return codes:
22676 *   None
22677 **/
22678void
22679lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22680			       struct lpfc_sli4_hdw_queue *hdwq)
22681{
22682	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22683	struct fcp_cmd_rsp_buf *list_entry = NULL;
22684	struct fcp_cmd_rsp_buf *tmp = NULL;
22685	unsigned long iflags;
22686
22687	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
22688
22689	/* Free cmd_rsp buf pool */
22690	list_for_each_entry_safe(list_entry, tmp,
22691				 buf_list,
22692				 list_node) {
22693		list_del(&list_entry->list_node);
22694		dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
22695			      list_entry->fcp_cmnd,
22696			      list_entry->fcp_cmd_rsp_dma_handle);
22697		kfree(list_entry);
22698	}
22699
22700	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
22701}
22702
22703/**
22704 * lpfc_sli_prep_wqe - Prepare WQE for the command to be posted
22705 * @phba: phba object
22706 * @job: job entry of the command to be posted.
22707 *
22708 * Fill the common fields of the wqe for each of the command.
22709 *
22710 * Return codes:
22711 *	None
22712 **/
22713void
22714lpfc_sli_prep_wqe(struct lpfc_hba *phba, struct lpfc_iocbq *job)
22715{
22716	u8 cmnd;
22717	u32 *pcmd;
22718	u32 if_type = 0;
22719	u32 abort_tag;
22720	bool fip;
22721	struct lpfc_nodelist *ndlp = NULL;
22722	union lpfc_wqe128 *wqe = &job->wqe;
22723	u8 command_type = ELS_COMMAND_NON_FIP;
22724
22725	fip = test_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
22726	/* The fcp commands will set command type */
22727	if (job->cmd_flag &  LPFC_IO_FCP)
22728		command_type = FCP_COMMAND;
22729	else if (fip && (job->cmd_flag & LPFC_FIP_ELS_ID_MASK))
22730		command_type = ELS_COMMAND_FIP;
22731	else
22732		command_type = ELS_COMMAND_NON_FIP;
22733
22734	abort_tag = job->iotag;
22735	cmnd = bf_get(wqe_cmnd, &wqe->els_req.wqe_com);
22736
22737	switch (cmnd) {
22738	case CMD_ELS_REQUEST64_WQE:
22739		ndlp = job->ndlp;
22740
22741		if_type = bf_get(lpfc_sli_intf_if_type,
22742				 &phba->sli4_hba.sli_intf);
22743		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
22744			pcmd = (u32 *)job->cmd_dmabuf->virt;
22745			if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
22746				     *pcmd == ELS_CMD_SCR ||
22747				     *pcmd == ELS_CMD_RDF ||
22748				     *pcmd == ELS_CMD_EDC ||
22749				     *pcmd == ELS_CMD_RSCN_XMT ||
22750				     *pcmd == ELS_CMD_FDISC ||
22751				     *pcmd == ELS_CMD_LOGO ||
22752				     *pcmd == ELS_CMD_QFPA ||
22753				     *pcmd == ELS_CMD_UVEM ||
22754				     *pcmd == ELS_CMD_PLOGI)) {
22755				bf_set(els_req64_sp, &wqe->els_req, 1);
22756				bf_set(els_req64_sid, &wqe->els_req,
22757				       job->vport->fc_myDID);
22758
22759				if ((*pcmd == ELS_CMD_FLOGI) &&
22760				    !(phba->fc_topology ==
22761				      LPFC_TOPOLOGY_LOOP))
22762					bf_set(els_req64_sid, &wqe->els_req, 0);
22763
22764				bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
22765				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
22766				       phba->vpi_ids[job->vport->vpi]);
22767			} else if (pcmd) {
22768				bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
22769				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
22770				       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22771			}
22772		}
22773
22774		bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
22775		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22776
22777		bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
22778		bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
22779		bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
22780		bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
22781		bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
22782		break;
22783	case CMD_XMIT_ELS_RSP64_WQE:
22784		ndlp = job->ndlp;
22785
22786		/* word4 */
22787		wqe->xmit_els_rsp.word4 = 0;
22788
22789		if_type = bf_get(lpfc_sli_intf_if_type,
22790				 &phba->sli4_hba.sli_intf);
22791		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
22792			if (test_bit(FC_PT2PT, &job->vport->fc_flag)) {
22793				bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
22794				bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
22795				       job->vport->fc_myDID);
22796				if (job->vport->fc_myDID == Fabric_DID) {
22797					bf_set(wqe_els_did,
22798					       &wqe->xmit_els_rsp.wqe_dest, 0);
22799				}
22800			}
22801		}
22802
22803		bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
22804		bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
22805		bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
22806		bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
22807		       LPFC_WQE_LENLOC_WORD3);
22808		bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
22809
22810		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
22811			bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
22812			bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
22813			       job->vport->fc_myDID);
22814			bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
22815		}
22816
22817		if (phba->sli_rev == LPFC_SLI_REV4) {
22818			bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
22819			       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
22820
22821			if (bf_get(wqe_ct, &wqe->xmit_els_rsp.wqe_com))
22822				bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
22823				       phba->vpi_ids[job->vport->vpi]);
22824		}
22825		command_type = OTHER_COMMAND;
22826		break;
22827	case CMD_GEN_REQUEST64_WQE:
22828		/* Word 10 */
22829		bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
22830		bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
22831		bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
22832		bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
22833		bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
22834		command_type = OTHER_COMMAND;
22835		break;
22836	case CMD_XMIT_SEQUENCE64_WQE:
22837		if (phba->link_flag & LS_LOOPBACK_MODE)
22838			bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
22839
22840		wqe->xmit_sequence.rsvd3 = 0;
22841		bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
22842		bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
22843		bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
22844		       LPFC_WQE_IOD_WRITE);
22845		bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
22846		       LPFC_WQE_LENLOC_WORD12);
22847		bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
22848		command_type = OTHER_COMMAND;
22849		break;
22850	case CMD_XMIT_BLS_RSP64_WQE:
22851		bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
22852		bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
22853		bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
22854		bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
22855		       phba->vpi_ids[phba->pport->vpi]);
22856		bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
22857		bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
22858		       LPFC_WQE_LENLOC_NONE);
22859		/* Overwrite the pre-set comnd type with OTHER_COMMAND */
22860		command_type = OTHER_COMMAND;
22861		break;
22862	case CMD_FCP_ICMND64_WQE:	/* task mgmt commands */
22863	case CMD_ABORT_XRI_WQE:		/* abort iotag */
22864	case CMD_SEND_FRAME:		/* mds loopback */
22865		/* cases already formatted for sli4 wqe - no chgs necessary */
22866		return;
22867	default:
22868		dump_stack();
22869		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
22870				"6207 Invalid command 0x%x\n",
22871				cmnd);
22872		break;
22873	}
22874
22875	wqe->generic.wqe_com.abort_tag = abort_tag;
22876	bf_set(wqe_reqtag, &wqe->generic.wqe_com, job->iotag);
22877	bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
22878	bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
22879}
~/ Read-only file preview.