brintos

brintos / linux-shallow public Read only

0
0
Text · 684.2 KiB · 2ec6e55 Raw
22784 lines · c
1/*******************************************************************2 * This file is part of the Emulex Linux Device Driver for         *3 * Fibre Channel Host Bus Adapters.                                *4 * Copyright (C) 2017-2024 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 31#include <scsi/scsi.h>32#include <scsi/scsi_cmnd.h>33#include <scsi/scsi_device.h>34#include <scsi/scsi_host.h>35#include <scsi/scsi_transport_fc.h>36#include <scsi/fc/fc_fs.h>37#include <linux/crash_dump.h>38#ifdef CONFIG_X8639#include <asm/set_memory.h>40#endif41 42#include "lpfc_hw4.h"43#include "lpfc_hw.h"44#include "lpfc_sli.h"45#include "lpfc_sli4.h"46#include "lpfc_nl.h"47#include "lpfc_disc.h"48#include "lpfc.h"49#include "lpfc_scsi.h"50#include "lpfc_nvme.h"51#include "lpfc_crtn.h"52#include "lpfc_logmsg.h"53#include "lpfc_compat.h"54#include "lpfc_debugfs.h"55#include "lpfc_vport.h"56#include "lpfc_version.h"57 58/* There are only four IOCB completion types. */59typedef enum _lpfc_iocb_type {60	LPFC_UNKNOWN_IOCB,61	LPFC_UNSOL_IOCB,62	LPFC_SOL_IOCB,63	LPFC_ABORT_IOCB64} lpfc_iocb_type;65 66 67/* Provide function prototypes local to this module. */68static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,69				  uint32_t);70static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,71			      uint8_t *, uint32_t *);72static struct lpfc_iocbq *73lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba *phba,74				  struct lpfc_iocbq *rspiocbq);75static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,76				      struct hbq_dmabuf *);77static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,78					  struct hbq_dmabuf *dmabuf);79static bool lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba,80				   struct lpfc_queue *cq, struct lpfc_cqe *cqe);81static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,82				       int);83static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,84				     struct lpfc_queue *eq,85				     struct lpfc_eqe *eqe,86				     enum lpfc_poll_mode poll_mode);87static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);88static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);89static struct lpfc_cqe *lpfc_sli4_cq_get(struct lpfc_queue *q);90static void __lpfc_sli4_consume_cqe(struct lpfc_hba *phba,91				    struct lpfc_queue *cq,92				    struct lpfc_cqe *cqe);93static uint16_t lpfc_wqe_bpl2sgl(struct lpfc_hba *phba,94				 struct lpfc_iocbq *pwqeq,95				 struct lpfc_sglq *sglq);96 97union lpfc_wqe128 lpfc_iread_cmd_template;98union lpfc_wqe128 lpfc_iwrite_cmd_template;99union lpfc_wqe128 lpfc_icmnd_cmd_template;100 101/* Setup WQE templates for IOs */102void lpfc_wqe_cmd_template(void)103{104	union lpfc_wqe128 *wqe;105 106	/* IREAD template */107	wqe = &lpfc_iread_cmd_template;108	memset(wqe, 0, sizeof(union lpfc_wqe128));109 110	/* Word 0, 1, 2 - BDE is variable */111 112	/* Word 3 - cmd_buff_len, payload_offset_len is zero */113 114	/* Word 4 - total_xfer_len is variable */115 116	/* Word 5 - is zero */117 118	/* Word 6 - ctxt_tag, xri_tag is variable */119 120	/* Word 7 */121	bf_set(wqe_cmnd, &wqe->fcp_iread.wqe_com, CMD_FCP_IREAD64_WQE);122	bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, PARM_READ_CHECK);123	bf_set(wqe_class, &wqe->fcp_iread.wqe_com, CLASS3);124	bf_set(wqe_ct, &wqe->fcp_iread.wqe_com, SLI4_CT_RPI);125 126	/* Word 8 - abort_tag is variable */127 128	/* Word 9  - reqtag is variable */129 130	/* Word 10 - dbde, wqes is variable */131	bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);132	bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);133	bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com, LPFC_WQE_LENLOC_WORD4);134	bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);135	bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);136 137	/* Word 11 - pbde is variable */138	bf_set(wqe_cmd_type, &wqe->fcp_iread.wqe_com, COMMAND_DATA_IN);139	bf_set(wqe_cqid, &wqe->fcp_iread.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);140	bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);141 142	/* Word 12 - is zero */143 144	/* Word 13, 14, 15 - PBDE is variable */145 146	/* IWRITE template */147	wqe = &lpfc_iwrite_cmd_template;148	memset(wqe, 0, sizeof(union lpfc_wqe128));149 150	/* Word 0, 1, 2 - BDE is variable */151 152	/* Word 3 - cmd_buff_len, payload_offset_len is zero */153 154	/* Word 4 - total_xfer_len is variable */155 156	/* Word 5 - initial_xfer_len is variable */157 158	/* Word 6 - ctxt_tag, xri_tag is variable */159 160	/* Word 7 */161	bf_set(wqe_cmnd, &wqe->fcp_iwrite.wqe_com, CMD_FCP_IWRITE64_WQE);162	bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, PARM_READ_CHECK);163	bf_set(wqe_class, &wqe->fcp_iwrite.wqe_com, CLASS3);164	bf_set(wqe_ct, &wqe->fcp_iwrite.wqe_com, SLI4_CT_RPI);165 166	/* Word 8 - abort_tag is variable */167 168	/* Word 9  - reqtag is variable */169 170	/* Word 10 - dbde, wqes is variable */171	bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);172	bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);173	bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_LENLOC_WORD4);174	bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);175	bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);176 177	/* Word 11 - pbde is variable */178	bf_set(wqe_cmd_type, &wqe->fcp_iwrite.wqe_com, COMMAND_DATA_OUT);179	bf_set(wqe_cqid, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);180	bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);181 182	/* Word 12 - is zero */183 184	/* Word 13, 14, 15 - PBDE is variable */185 186	/* ICMND template */187	wqe = &lpfc_icmnd_cmd_template;188	memset(wqe, 0, sizeof(union lpfc_wqe128));189 190	/* Word 0, 1, 2 - BDE is variable */191 192	/* Word 3 - payload_offset_len is variable */193 194	/* Word 4, 5 - is zero */195 196	/* Word 6 - ctxt_tag, xri_tag is variable */197 198	/* Word 7 */199	bf_set(wqe_cmnd, &wqe->fcp_icmd.wqe_com, CMD_FCP_ICMND64_WQE);200	bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);201	bf_set(wqe_class, &wqe->fcp_icmd.wqe_com, CLASS3);202	bf_set(wqe_ct, &wqe->fcp_icmd.wqe_com, SLI4_CT_RPI);203 204	/* Word 8 - abort_tag is variable */205 206	/* Word 9  - reqtag is variable */207 208	/* Word 10 - dbde, wqes is variable */209	bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);210	bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_NONE);211	bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com, LPFC_WQE_LENLOC_NONE);212	bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);213	bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);214 215	/* Word 11 */216	bf_set(wqe_cmd_type, &wqe->fcp_icmd.wqe_com, COMMAND_DATA_IN);217	bf_set(wqe_cqid, &wqe->fcp_icmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);218	bf_set(wqe_pbde, &wqe->fcp_icmd.wqe_com, 0);219 220	/* Word 12, 13, 14, 15 - is zero */221}222 223#if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)224/**225 * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function226 * @srcp: Source memory pointer.227 * @destp: Destination memory pointer.228 * @cnt: Number of words required to be copied.229 *       Must be a multiple of sizeof(uint64_t)230 *231 * This function is used for copying data between driver memory232 * and the SLI WQ. This function also changes the endianness233 * of each word if native endianness is different from SLI234 * endianness. This function can be called with or without235 * lock.236 **/237static void238lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)239{240	uint64_t *src = srcp;241	uint64_t *dest = destp;242	int i;243 244	for (i = 0; i < (int)cnt; i += sizeof(uint64_t))245		*dest++ = *src++;246}247#else248#define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)249#endif250 251/**252 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue253 * @q: The Work Queue to operate on.254 * @wqe: The work Queue Entry to put on the Work queue.255 *256 * This routine will copy the contents of @wqe to the next available entry on257 * the @q. This function will then ring the Work Queue Doorbell to signal the258 * HBA to start processing the Work Queue Entry. This function returns 0 if259 * successful. If no entries are available on @q then this function will return260 * -ENOMEM.261 * The caller is expected to hold the hbalock when calling this routine.262 **/263static int264lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)265{266	union lpfc_wqe *temp_wqe;267	struct lpfc_register doorbell;268	uint32_t host_index;269	uint32_t idx;270	uint32_t i = 0;271	uint8_t *tmp;272	u32 if_type;273 274	/* sanity check on queue memory */275	if (unlikely(!q))276		return -ENOMEM;277 278	temp_wqe = lpfc_sli4_qe(q, q->host_index);279 280	/* If the host has not yet processed the next entry then we are done */281	idx = ((q->host_index + 1) % q->entry_count);282	if (idx == q->hba_index) {283		q->WQ_overflow++;284		return -EBUSY;285	}286	q->WQ_posted++;287	/* set consumption flag every once in a while */288	if (!((q->host_index + 1) % q->notify_interval))289		bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);290	else291		bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);292	if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)293		bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);294	lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);295	if (q->dpp_enable && q->phba->cfg_enable_dpp) {296		/* write to DPP aperture taking advatage of Combined Writes */297		tmp = (uint8_t *)temp_wqe;298#ifdef __raw_writeq299		for (i = 0; i < q->entry_size; i += sizeof(uint64_t))300			__raw_writeq(*((uint64_t *)(tmp + i)),301					q->dpp_regaddr + i);302#else303		for (i = 0; i < q->entry_size; i += sizeof(uint32_t))304			__raw_writel(*((uint32_t *)(tmp + i)),305					q->dpp_regaddr + i);306#endif307	}308	/* ensure WQE bcopy and DPP flushed before doorbell write */309	wmb();310 311	/* Update the host index before invoking device */312	host_index = q->host_index;313 314	q->host_index = idx;315 316	/* Ring Doorbell */317	doorbell.word0 = 0;318	if (q->db_format == LPFC_DB_LIST_FORMAT) {319		if (q->dpp_enable && q->phba->cfg_enable_dpp) {320			bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);321			bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);322			bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,323			    q->dpp_id);324			bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,325			    q->queue_id);326		} else {327			bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);328			bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);329 330			/* Leave bits <23:16> clear for if_type 6 dpp */331			if_type = bf_get(lpfc_sli_intf_if_type,332					 &q->phba->sli4_hba.sli_intf);333			if (if_type != LPFC_SLI_INTF_IF_TYPE_6)334				bf_set(lpfc_wq_db_list_fm_index, &doorbell,335				       host_index);336		}337	} else if (q->db_format == LPFC_DB_RING_FORMAT) {338		bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);339		bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);340	} else {341		return -EINVAL;342	}343	writel(doorbell.word0, q->db_regaddr);344 345	return 0;346}347 348/**349 * lpfc_sli4_wq_release - Updates internal hba index for WQ350 * @q: The Work Queue to operate on.351 * @index: The index to advance the hba index to.352 *353 * This routine will update the HBA index of a queue to reflect consumption of354 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed355 * an entry the host calls this function to update the queue's internal356 * pointers.357 **/358static void359lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)360{361	/* sanity check on queue memory */362	if (unlikely(!q))363		return;364 365	q->hba_index = index;366}367 368/**369 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue370 * @q: The Mailbox Queue to operate on.371 * @mqe: The Mailbox Queue Entry to put on the Work queue.372 *373 * This routine will copy the contents of @mqe to the next available entry on374 * the @q. This function will then ring the Work Queue Doorbell to signal the375 * HBA to start processing the Work Queue Entry. This function returns 0 if376 * successful. If no entries are available on @q then this function will return377 * -ENOMEM.378 * The caller is expected to hold the hbalock when calling this routine.379 **/380static uint32_t381lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)382{383	struct lpfc_mqe *temp_mqe;384	struct lpfc_register doorbell;385 386	/* sanity check on queue memory */387	if (unlikely(!q))388		return -ENOMEM;389	temp_mqe = lpfc_sli4_qe(q, q->host_index);390 391	/* If the host has not yet processed the next entry then we are done */392	if (((q->host_index + 1) % q->entry_count) == q->hba_index)393		return -ENOMEM;394	lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);395	/* Save off the mailbox pointer for completion */396	q->phba->mbox = (MAILBOX_t *)temp_mqe;397 398	/* Update the host index before invoking device */399	q->host_index = ((q->host_index + 1) % q->entry_count);400 401	/* Ring Doorbell */402	doorbell.word0 = 0;403	bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);404	bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);405	writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);406	return 0;407}408 409/**410 * lpfc_sli4_mq_release - Updates internal hba index for MQ411 * @q: The Mailbox Queue to operate on.412 *413 * This routine will update the HBA index of a queue to reflect consumption of414 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed415 * an entry the host calls this function to update the queue's internal416 * pointers. This routine returns the number of entries that were consumed by417 * the HBA.418 **/419static uint32_t420lpfc_sli4_mq_release(struct lpfc_queue *q)421{422	/* sanity check on queue memory */423	if (unlikely(!q))424		return 0;425 426	/* Clear the mailbox pointer for completion */427	q->phba->mbox = NULL;428	q->hba_index = ((q->hba_index + 1) % q->entry_count);429	return 1;430}431 432/**433 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ434 * @q: The Event Queue to get the first valid EQE from435 *436 * This routine will get the first valid Event Queue Entry from @q, update437 * the queue's internal hba index, and return the EQE. If no valid EQEs are in438 * the Queue (no more work to do), or the Queue is full of EQEs that have been439 * processed, but not popped back to the HBA then this routine will return NULL.440 **/441static struct lpfc_eqe *442lpfc_sli4_eq_get(struct lpfc_queue *q)443{444	struct lpfc_eqe *eqe;445 446	/* sanity check on queue memory */447	if (unlikely(!q))448		return NULL;449	eqe = lpfc_sli4_qe(q, q->host_index);450 451	/* If the next EQE is not valid then we are done */452	if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)453		return NULL;454 455	/*456	 * insert barrier for instruction interlock : data from the hardware457	 * must have the valid bit checked before it can be copied and acted458	 * upon. Speculative instructions were allowing a bcopy at the start459	 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately460	 * after our return, to copy data before the valid bit check above461	 * was done. As such, some of the copied data was stale. The barrier462	 * ensures the check is before any data is copied.463	 */464	mb();465	return eqe;466}467 468/**469 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ470 * @q: The Event Queue to disable interrupts471 *472 **/473void474lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)475{476	struct lpfc_register doorbell;477 478	doorbell.word0 = 0;479	bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);480	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);481	bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,482		(q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));483	bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);484	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);485}486 487/**488 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ489 * @q: The Event Queue to disable interrupts490 *491 **/492void493lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)494{495	struct lpfc_register doorbell;496 497	doorbell.word0 = 0;498	bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);499	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);500}501 502/**503 * lpfc_sli4_write_eq_db - write EQ DB for eqe's consumed or arm state504 * @phba: adapter with EQ505 * @q: The Event Queue that the host has completed processing for.506 * @count: Number of elements that have been consumed507 * @arm: Indicates whether the host wants to arms this CQ.508 *509 * This routine will notify the HBA, by ringing the doorbell, that count510 * number of EQEs have been processed. The @arm parameter indicates whether511 * the queue should be rearmed when ringing the doorbell.512 **/513void514lpfc_sli4_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,515		     uint32_t count, bool arm)516{517	struct lpfc_register doorbell;518 519	/* sanity check on queue memory */520	if (unlikely(!q || (count == 0 && !arm)))521		return;522 523	/* ring doorbell for number popped */524	doorbell.word0 = 0;525	if (arm) {526		bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);527		bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);528	}529	bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);530	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);531	bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,532			(q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));533	bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);534	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);535	/* PCI read to flush PCI pipeline on re-arming for INTx mode */536	if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))537		readl(q->phba->sli4_hba.EQDBregaddr);538}539 540/**541 * lpfc_sli4_if6_write_eq_db - write EQ DB for eqe's consumed or arm state542 * @phba: adapter with EQ543 * @q: The Event Queue that the host has completed processing for.544 * @count: Number of elements that have been consumed545 * @arm: Indicates whether the host wants to arms this CQ.546 *547 * This routine will notify the HBA, by ringing the doorbell, that count548 * number of EQEs have been processed. The @arm parameter indicates whether549 * the queue should be rearmed when ringing the doorbell.550 **/551void552lpfc_sli4_if6_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,553			  uint32_t count, bool arm)554{555	struct lpfc_register doorbell;556 557	/* sanity check on queue memory */558	if (unlikely(!q || (count == 0 && !arm)))559		return;560 561	/* ring doorbell for number popped */562	doorbell.word0 = 0;563	if (arm)564		bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);565	bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, count);566	bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);567	writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);568	/* PCI read to flush PCI pipeline on re-arming for INTx mode */569	if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))570		readl(q->phba->sli4_hba.EQDBregaddr);571}572 573static void574__lpfc_sli4_consume_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,575			struct lpfc_eqe *eqe)576{577	if (!phba->sli4_hba.pc_sli4_params.eqav)578		bf_set_le32(lpfc_eqe_valid, eqe, 0);579 580	eq->host_index = ((eq->host_index + 1) % eq->entry_count);581 582	/* if the index wrapped around, toggle the valid bit */583	if (phba->sli4_hba.pc_sli4_params.eqav && !eq->host_index)584		eq->qe_valid = (eq->qe_valid) ? 0 : 1;585}586 587static void588lpfc_sli4_eqcq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)589{590	struct lpfc_eqe *eqe = NULL;591	u32 eq_count = 0, cq_count = 0;592	struct lpfc_cqe *cqe = NULL;593	struct lpfc_queue *cq = NULL, *childq = NULL;594	int cqid = 0;595 596	/* walk all the EQ entries and drop on the floor */597	eqe = lpfc_sli4_eq_get(eq);598	while (eqe) {599		/* Get the reference to the corresponding CQ */600		cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);601		cq = NULL;602 603		list_for_each_entry(childq, &eq->child_list, list) {604			if (childq->queue_id == cqid) {605				cq = childq;606				break;607			}608		}609		/* If CQ is valid, iterate through it and drop all the CQEs */610		if (cq) {611			cqe = lpfc_sli4_cq_get(cq);612			while (cqe) {613				__lpfc_sli4_consume_cqe(phba, cq, cqe);614				cq_count++;615				cqe = lpfc_sli4_cq_get(cq);616			}617			/* Clear and re-arm the CQ */618			phba->sli4_hba.sli4_write_cq_db(phba, cq, cq_count,619			    LPFC_QUEUE_REARM);620			cq_count = 0;621		}622		__lpfc_sli4_consume_eqe(phba, eq, eqe);623		eq_count++;624		eqe = lpfc_sli4_eq_get(eq);625	}626 627	/* Clear and re-arm the EQ */628	phba->sli4_hba.sli4_write_eq_db(phba, eq, eq_count, LPFC_QUEUE_REARM);629}630 631static int632lpfc_sli4_process_eq(struct lpfc_hba *phba, struct lpfc_queue *eq,633		     u8 rearm, enum lpfc_poll_mode poll_mode)634{635	struct lpfc_eqe *eqe;636	int count = 0, consumed = 0;637 638	if (cmpxchg(&eq->queue_claimed, 0, 1) != 0)639		goto rearm_and_exit;640 641	eqe = lpfc_sli4_eq_get(eq);642	while (eqe) {643		lpfc_sli4_hba_handle_eqe(phba, eq, eqe, poll_mode);644		__lpfc_sli4_consume_eqe(phba, eq, eqe);645 646		consumed++;647		if (!(++count % eq->max_proc_limit))648			break;649 650		if (!(count % eq->notify_interval)) {651			phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed,652							LPFC_QUEUE_NOARM);653			consumed = 0;654		}655 656		eqe = lpfc_sli4_eq_get(eq);657	}658	eq->EQ_processed += count;659 660	/* Track the max number of EQEs processed in 1 intr */661	if (count > eq->EQ_max_eqe)662		eq->EQ_max_eqe = count;663 664	xchg(&eq->queue_claimed, 0);665 666rearm_and_exit:667	/* Always clear the EQ. */668	phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed, rearm);669 670	return count;671}672 673/**674 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ675 * @q: The Completion Queue to get the first valid CQE from676 *677 * This routine will get the first valid Completion Queue Entry from @q, update678 * the queue's internal hba index, and return the CQE. If no valid CQEs are in679 * the Queue (no more work to do), or the Queue is full of CQEs that have been680 * processed, but not popped back to the HBA then this routine will return NULL.681 **/682static struct lpfc_cqe *683lpfc_sli4_cq_get(struct lpfc_queue *q)684{685	struct lpfc_cqe *cqe;686 687	/* sanity check on queue memory */688	if (unlikely(!q))689		return NULL;690	cqe = lpfc_sli4_qe(q, q->host_index);691 692	/* If the next CQE is not valid then we are done */693	if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)694		return NULL;695 696	/*697	 * insert barrier for instruction interlock : data from the hardware698	 * must have the valid bit checked before it can be copied and acted699	 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative700	 * instructions allowing action on content before valid bit checked,701	 * add barrier here as well. May not be needed as "content" is a702	 * single 32-bit entity here (vs multi word structure for cq's).703	 */704	mb();705	return cqe;706}707 708static void709__lpfc_sli4_consume_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,710			struct lpfc_cqe *cqe)711{712	if (!phba->sli4_hba.pc_sli4_params.cqav)713		bf_set_le32(lpfc_cqe_valid, cqe, 0);714 715	cq->host_index = ((cq->host_index + 1) % cq->entry_count);716 717	/* if the index wrapped around, toggle the valid bit */718	if (phba->sli4_hba.pc_sli4_params.cqav && !cq->host_index)719		cq->qe_valid = (cq->qe_valid) ? 0 : 1;720}721 722/**723 * lpfc_sli4_write_cq_db - write cq DB for entries consumed or arm state.724 * @phba: the adapter with the CQ725 * @q: The Completion Queue that the host has completed processing for.726 * @count: the number of elements that were consumed727 * @arm: Indicates whether the host wants to arms this CQ.728 *729 * This routine will notify the HBA, by ringing the doorbell, that the730 * CQEs have been processed. The @arm parameter specifies whether the731 * queue should be rearmed when ringing the doorbell.732 **/733void734lpfc_sli4_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,735		     uint32_t count, bool arm)736{737	struct lpfc_register doorbell;738 739	/* sanity check on queue memory */740	if (unlikely(!q || (count == 0 && !arm)))741		return;742 743	/* ring doorbell for number popped */744	doorbell.word0 = 0;745	if (arm)746		bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);747	bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);748	bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);749	bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,750			(q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));751	bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);752	writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);753}754 755/**756 * lpfc_sli4_if6_write_cq_db - write cq DB for entries consumed or arm state.757 * @phba: the adapter with the CQ758 * @q: The Completion Queue that the host has completed processing for.759 * @count: the number of elements that were consumed760 * @arm: Indicates whether the host wants to arms this CQ.761 *762 * This routine will notify the HBA, by ringing the doorbell, that the763 * CQEs have been processed. The @arm parameter specifies whether the764 * queue should be rearmed when ringing the doorbell.765 **/766void767lpfc_sli4_if6_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,768			 uint32_t count, bool arm)769{770	struct lpfc_register doorbell;771 772	/* sanity check on queue memory */773	if (unlikely(!q || (count == 0 && !arm)))774		return;775 776	/* ring doorbell for number popped */777	doorbell.word0 = 0;778	if (arm)779		bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);780	bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, count);781	bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);782	writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);783}784 785/*786 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue787 *788 * This routine will copy the contents of @wqe to the next available entry on789 * the @q. This function will then ring the Receive Queue Doorbell to signal the790 * HBA to start processing the Receive Queue Entry. This function returns the791 * index that the rqe was copied to if successful. If no entries are available792 * on @q then this function will return -ENOMEM.793 * The caller is expected to hold the hbalock when calling this routine.794 **/795int796lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,797		 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)798{799	struct lpfc_rqe *temp_hrqe;800	struct lpfc_rqe *temp_drqe;801	struct lpfc_register doorbell;802	int hq_put_index;803	int dq_put_index;804 805	/* sanity check on queue memory */806	if (unlikely(!hq) || unlikely(!dq))807		return -ENOMEM;808	hq_put_index = hq->host_index;809	dq_put_index = dq->host_index;810	temp_hrqe = lpfc_sli4_qe(hq, hq_put_index);811	temp_drqe = lpfc_sli4_qe(dq, dq_put_index);812 813	if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)814		return -EINVAL;815	if (hq_put_index != dq_put_index)816		return -EINVAL;817	/* If the host has not yet processed the next entry then we are done */818	if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)819		return -EBUSY;820	lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);821	lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);822 823	/* Update the host index to point to the next slot */824	hq->host_index = ((hq_put_index + 1) % hq->entry_count);825	dq->host_index = ((dq_put_index + 1) % dq->entry_count);826	hq->RQ_buf_posted++;827 828	/* Ring The Header Receive Queue Doorbell */829	if (!(hq->host_index % hq->notify_interval)) {830		doorbell.word0 = 0;831		if (hq->db_format == LPFC_DB_RING_FORMAT) {832			bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,833			       hq->notify_interval);834			bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);835		} else if (hq->db_format == LPFC_DB_LIST_FORMAT) {836			bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,837			       hq->notify_interval);838			bf_set(lpfc_rq_db_list_fm_index, &doorbell,839			       hq->host_index);840			bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);841		} else {842			return -EINVAL;843		}844		writel(doorbell.word0, hq->db_regaddr);845	}846	return hq_put_index;847}848 849/*850 * lpfc_sli4_rq_release - Updates internal hba index for RQ851 *852 * This routine will update the HBA index of a queue to reflect consumption of853 * one Receive Queue Entry by the HBA. When the HBA indicates that it has854 * consumed an entry the host calls this function to update the queue's855 * internal pointers. This routine returns the number of entries that were856 * consumed by the HBA.857 **/858static uint32_t859lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)860{861	/* sanity check on queue memory */862	if (unlikely(!hq) || unlikely(!dq))863		return 0;864 865	if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))866		return 0;867	hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);868	dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);869	return 1;870}871 872/**873 * lpfc_cmd_iocb - Get next command iocb entry in the ring874 * @phba: Pointer to HBA context object.875 * @pring: Pointer to driver SLI ring object.876 *877 * This function returns pointer to next command iocb entry878 * in the command ring. The caller must hold hbalock to prevent879 * other threads consume the next command iocb.880 * SLI-2/SLI-3 provide different sized iocbs.881 **/882static inline IOCB_t *883lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)884{885	return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +886			   pring->sli.sli3.cmdidx * phba->iocb_cmd_size);887}888 889/**890 * lpfc_resp_iocb - Get next response iocb entry in the ring891 * @phba: Pointer to HBA context object.892 * @pring: Pointer to driver SLI ring object.893 *894 * This function returns pointer to next response iocb entry895 * in the response ring. The caller must hold hbalock to make sure896 * that no other thread consume the next response iocb.897 * SLI-2/SLI-3 provide different sized iocbs.898 **/899static inline IOCB_t *900lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)901{902	return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +903			   pring->sli.sli3.rspidx * phba->iocb_rsp_size);904}905 906/**907 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool908 * @phba: Pointer to HBA context object.909 *910 * This function is called with hbalock held. This function911 * allocates a new driver iocb object from the iocb pool. If the912 * allocation is successful, it returns pointer to the newly913 * allocated iocb object else it returns NULL.914 **/915struct lpfc_iocbq *916__lpfc_sli_get_iocbq(struct lpfc_hba *phba)917{918	struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;919	struct lpfc_iocbq * iocbq = NULL;920 921	lockdep_assert_held(&phba->hbalock);922 923	list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);924	if (iocbq)925		phba->iocb_cnt++;926	if (phba->iocb_cnt > phba->iocb_max)927		phba->iocb_max = phba->iocb_cnt;928	return iocbq;929}930 931/**932 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.933 * @phba: Pointer to HBA context object.934 * @xritag: XRI value.935 *936 * This function clears the sglq pointer from the array of active937 * sglq's. The xritag that is passed in is used to index into the938 * array. Before the xritag can be used it needs to be adjusted939 * by subtracting the xribase.940 *941 * Returns sglq ponter = success, NULL = Failure.942 **/943struct lpfc_sglq *944__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)945{946	struct lpfc_sglq *sglq;947 948	sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];949	phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;950	return sglq;951}952 953/**954 * __lpfc_get_active_sglq - Get the active sglq for this XRI.955 * @phba: Pointer to HBA context object.956 * @xritag: XRI value.957 *958 * This function returns the sglq pointer from the array of active959 * sglq's. The xritag that is passed in is used to index into the960 * array. Before the xritag can be used it needs to be adjusted961 * by subtracting the xribase.962 *963 * Returns sglq ponter = success, NULL = Failure.964 **/965struct lpfc_sglq *966__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)967{968	struct lpfc_sglq *sglq;969 970	sglq =  phba->sli4_hba.lpfc_sglq_active_list[xritag];971	return sglq;972}973 974/**975 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.976 * @phba: Pointer to HBA context object.977 * @xritag: xri used in this exchange.978 * @rrq: The RRQ to be cleared.979 *980 **/981void982lpfc_clr_rrq_active(struct lpfc_hba *phba,983		    uint16_t xritag,984		    struct lpfc_node_rrq *rrq)985{986	struct lpfc_nodelist *ndlp = NULL;987 988	/* Lookup did to verify if did is still active on this vport */989	if (rrq->vport)990		ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);991 992	if (!ndlp)993		goto out;994 995	if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {996		rrq->send_rrq = 0;997		rrq->xritag = 0;998		rrq->rrq_stop_time = 0;999	}1000out:1001	mempool_free(rrq, phba->rrq_pool);1002}1003 1004/**1005 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.1006 * @phba: Pointer to HBA context object.1007 *1008 * This function is called with hbalock held. This function1009 * Checks if stop_time (ratov from setting rrq active) has1010 * been reached, if it has and the send_rrq flag is set then1011 * it will call lpfc_send_rrq. If the send_rrq flag is not set1012 * then it will just call the routine to clear the rrq and1013 * free the rrq resource.1014 * The timer is set to the next rrq that is going to expire before1015 * leaving the routine.1016 *1017 **/1018void1019lpfc_handle_rrq_active(struct lpfc_hba *phba)1020{1021	struct lpfc_node_rrq *rrq;1022	struct lpfc_node_rrq *nextrrq;1023	unsigned long next_time;1024	unsigned long iflags;1025	LIST_HEAD(send_rrq);1026 1027	clear_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);1028	next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));1029	spin_lock_irqsave(&phba->rrq_list_lock, iflags);1030	list_for_each_entry_safe(rrq, nextrrq,1031				 &phba->active_rrq_list, list) {1032		if (time_after(jiffies, rrq->rrq_stop_time))1033			list_move(&rrq->list, &send_rrq);1034		else if (time_before(rrq->rrq_stop_time, next_time))1035			next_time = rrq->rrq_stop_time;1036	}1037	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);1038	if ((!list_empty(&phba->active_rrq_list)) &&1039	    (!test_bit(FC_UNLOADING, &phba->pport->load_flag)))1040		mod_timer(&phba->rrq_tmr, next_time);1041	list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {1042		list_del(&rrq->list);1043		if (!rrq->send_rrq) {1044			/* this call will free the rrq */1045			lpfc_clr_rrq_active(phba, rrq->xritag, rrq);1046		} else if (lpfc_send_rrq(phba, rrq)) {1047			/* if we send the rrq then the completion handler1048			*  will clear the bit in the xribitmap.1049			*/1050			lpfc_clr_rrq_active(phba, rrq->xritag,1051					    rrq);1052		}1053	}1054}1055 1056/**1057 * lpfc_get_active_rrq - Get the active RRQ for this exchange.1058 * @vport: Pointer to vport context object.1059 * @xri: The xri used in the exchange.1060 * @did: The targets DID for this exchange.1061 *1062 * returns NULL = rrq not found in the phba->active_rrq_list.1063 *         rrq = rrq for this xri and target.1064 **/1065struct lpfc_node_rrq *1066lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)1067{1068	struct lpfc_hba *phba = vport->phba;1069	struct lpfc_node_rrq *rrq;1070	struct lpfc_node_rrq *nextrrq;1071	unsigned long iflags;1072 1073	if (phba->sli_rev != LPFC_SLI_REV4)1074		return NULL;1075	spin_lock_irqsave(&phba->rrq_list_lock, iflags);1076	list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {1077		if (rrq->vport == vport && rrq->xritag == xri &&1078				rrq->nlp_DID == did){1079			list_del(&rrq->list);1080			spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);1081			return rrq;1082		}1083	}1084	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);1085	return NULL;1086}1087 1088/**1089 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.1090 * @vport: Pointer to vport context object.1091 * @ndlp: Pointer to the lpfc_node_list structure.1092 * If ndlp is NULL Remove all active RRQs for this vport from the1093 * phba->active_rrq_list and clear the rrq.1094 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.1095 **/1096void1097lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)1098 1099{1100	struct lpfc_hba *phba = vport->phba;1101	struct lpfc_node_rrq *rrq;1102	struct lpfc_node_rrq *nextrrq;1103	unsigned long iflags;1104	LIST_HEAD(rrq_list);1105 1106	if (phba->sli_rev != LPFC_SLI_REV4)1107		return;1108	if (!ndlp) {1109		lpfc_sli4_vport_delete_els_xri_aborted(vport);1110		lpfc_sli4_vport_delete_fcp_xri_aborted(vport);1111	}1112	spin_lock_irqsave(&phba->rrq_list_lock, iflags);1113	list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {1114		if (rrq->vport != vport)1115			continue;1116 1117		if (!ndlp || ndlp == lpfc_findnode_did(vport, rrq->nlp_DID))1118			list_move(&rrq->list, &rrq_list);1119 1120	}1121	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);1122 1123	list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {1124		list_del(&rrq->list);1125		lpfc_clr_rrq_active(phba, rrq->xritag, rrq);1126	}1127}1128 1129/**1130 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.1131 * @phba: Pointer to HBA context object.1132 * @ndlp: Targets nodelist pointer for this exchange.1133 * @xritag: the xri in the bitmap to test.1134 *1135 * This function returns:1136 * 0 = rrq not active for this xri1137 * 1 = rrq is valid for this xri.1138 **/1139int1140lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,1141			uint16_t  xritag)1142{1143	if (!ndlp)1144		return 0;1145	if (!ndlp->active_rrqs_xri_bitmap)1146		return 0;1147	if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))1148		return 1;1149	else1150		return 0;1151}1152 1153/**1154 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.1155 * @phba: Pointer to HBA context object.1156 * @ndlp: nodelist pointer for this target.1157 * @xritag: xri used in this exchange.1158 * @rxid: Remote Exchange ID.1159 * @send_rrq: Flag used to determine if we should send rrq els cmd.1160 *1161 * This function takes the hbalock.1162 * The active bit is always set in the active rrq xri_bitmap even1163 * if there is no slot avaiable for the other rrq information.1164 *1165 * returns 0 rrq actived for this xri1166 *         < 0 No memory or invalid ndlp.1167 **/1168int1169lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,1170		    uint16_t xritag, uint16_t rxid, uint16_t send_rrq)1171{1172	unsigned long iflags;1173	struct lpfc_node_rrq *rrq;1174	int empty;1175 1176	if (!ndlp)1177		return -EINVAL;1178 1179	if (!phba->cfg_enable_rrq)1180		return -EINVAL;1181 1182	if (test_bit(FC_UNLOADING, &phba->pport->load_flag)) {1183		clear_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);1184		goto outnl;1185	}1186 1187	spin_lock_irqsave(&phba->hbalock, iflags);1188	if (ndlp->vport && test_bit(FC_UNLOADING, &ndlp->vport->load_flag))1189		goto out;1190 1191	if (!ndlp->active_rrqs_xri_bitmap)1192		goto out;1193 1194	if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))1195		goto out;1196 1197	spin_unlock_irqrestore(&phba->hbalock, iflags);1198	rrq = mempool_alloc(phba->rrq_pool, GFP_ATOMIC);1199	if (!rrq) {1200		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,1201				"3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"1202				" DID:0x%x Send:%d\n",1203				xritag, rxid, ndlp->nlp_DID, send_rrq);1204		return -EINVAL;1205	}1206	if (phba->cfg_enable_rrq == 1)1207		rrq->send_rrq = send_rrq;1208	else1209		rrq->send_rrq = 0;1210	rrq->xritag = xritag;1211	rrq->rrq_stop_time = jiffies +1212				msecs_to_jiffies(1000 * (phba->fc_ratov + 1));1213	rrq->nlp_DID = ndlp->nlp_DID;1214	rrq->vport = ndlp->vport;1215	rrq->rxid = rxid;1216 1217	spin_lock_irqsave(&phba->rrq_list_lock, iflags);1218	empty = list_empty(&phba->active_rrq_list);1219	list_add_tail(&rrq->list, &phba->active_rrq_list);1220	spin_unlock_irqrestore(&phba->rrq_list_lock, iflags);1221	set_bit(HBA_RRQ_ACTIVE, &phba->hba_flag);1222	if (empty)1223		lpfc_worker_wake_up(phba);1224	return 0;1225out:1226	spin_unlock_irqrestore(&phba->hbalock, iflags);1227outnl:1228	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,1229			"2921 Can't set rrq active xri:0x%x rxid:0x%x"1230			" DID:0x%x Send:%d\n",1231			xritag, rxid, ndlp->nlp_DID, send_rrq);1232	return -EINVAL;1233}1234 1235/**1236 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool1237 * @phba: Pointer to HBA context object.1238 * @piocbq: Pointer to the iocbq.1239 *1240 * The driver calls this function with either the nvme ls ring lock1241 * or the fc els ring lock held depending on the iocb usage.  This function1242 * gets a new driver sglq object from the sglq list. If the list is not empty1243 * then it is successful, it returns pointer to the newly allocated sglq1244 * object else it returns NULL.1245 **/1246static struct lpfc_sglq *1247__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)1248{1249	struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;1250	struct lpfc_sglq *sglq = NULL;1251	struct lpfc_sglq *start_sglq = NULL;1252	struct lpfc_io_buf *lpfc_cmd;1253	struct lpfc_nodelist *ndlp;1254	int found = 0;1255	u8 cmnd;1256 1257	cmnd = get_job_cmnd(phba, piocbq);1258 1259	if (piocbq->cmd_flag & LPFC_IO_FCP) {1260		lpfc_cmd = piocbq->io_buf;1261		ndlp = lpfc_cmd->rdata->pnode;1262	} else  if ((cmnd == CMD_GEN_REQUEST64_CR) &&1263			!(piocbq->cmd_flag & LPFC_IO_LIBDFC)) {1264		ndlp = piocbq->ndlp;1265	} else  if (piocbq->cmd_flag & LPFC_IO_LIBDFC) {1266		if (piocbq->cmd_flag & LPFC_IO_LOOPBACK)1267			ndlp = NULL;1268		else1269			ndlp = piocbq->ndlp;1270	} else {1271		ndlp = piocbq->ndlp;1272	}1273 1274	spin_lock(&phba->sli4_hba.sgl_list_lock);1275	list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);1276	start_sglq = sglq;1277	while (!found) {1278		if (!sglq)1279			break;1280		if (ndlp && ndlp->active_rrqs_xri_bitmap &&1281		    test_bit(sglq->sli4_lxritag,1282		    ndlp->active_rrqs_xri_bitmap)) {1283			/* This xri has an rrq outstanding for this DID.1284			 * put it back in the list and get another xri.1285			 */1286			list_add_tail(&sglq->list, lpfc_els_sgl_list);1287			sglq = NULL;1288			list_remove_head(lpfc_els_sgl_list, sglq,1289						struct lpfc_sglq, list);1290			if (sglq == start_sglq) {1291				list_add_tail(&sglq->list, lpfc_els_sgl_list);1292				sglq = NULL;1293				break;1294			} else1295				continue;1296		}1297		sglq->ndlp = ndlp;1298		found = 1;1299		phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;1300		sglq->state = SGL_ALLOCATED;1301	}1302	spin_unlock(&phba->sli4_hba.sgl_list_lock);1303	return sglq;1304}1305 1306/**1307 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool1308 * @phba: Pointer to HBA context object.1309 * @piocbq: Pointer to the iocbq.1310 *1311 * This function is called with the sgl_list lock held. This function1312 * gets a new driver sglq object from the sglq list. If the1313 * list is not empty then it is successful, it returns pointer to the newly1314 * allocated sglq object else it returns NULL.1315 **/1316struct lpfc_sglq *1317__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)1318{1319	struct list_head *lpfc_nvmet_sgl_list;1320	struct lpfc_sglq *sglq = NULL;1321 1322	lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;1323 1324	lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);1325 1326	list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);1327	if (!sglq)1328		return NULL;1329	phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;1330	sglq->state = SGL_ALLOCATED;1331	return sglq;1332}1333 1334/**1335 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool1336 * @phba: Pointer to HBA context object.1337 *1338 * This function is called with no lock held. This function1339 * allocates a new driver iocb object from the iocb pool. If the1340 * allocation is successful, it returns pointer to the newly1341 * allocated iocb object else it returns NULL.1342 **/1343struct lpfc_iocbq *1344lpfc_sli_get_iocbq(struct lpfc_hba *phba)1345{1346	struct lpfc_iocbq * iocbq = NULL;1347	unsigned long iflags;1348 1349	spin_lock_irqsave(&phba->hbalock, iflags);1350	iocbq = __lpfc_sli_get_iocbq(phba);1351	spin_unlock_irqrestore(&phba->hbalock, iflags);1352	return iocbq;1353}1354 1355/**1356 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool1357 * @phba: Pointer to HBA context object.1358 * @iocbq: Pointer to driver iocb object.1359 *1360 * This function is called to release the driver iocb object1361 * to the iocb pool. The iotag in the iocb object1362 * does not change for each use of the iocb object. This function1363 * clears all other fields of the iocb object when it is freed.1364 * The sqlq structure that holds the xritag and phys and virtual1365 * mappings for the scatter gather list is retrieved from the1366 * active array of sglq. The get of the sglq pointer also clears1367 * the entry in the array. If the status of the IO indiactes that1368 * this IO was aborted then the sglq entry it put on the1369 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the1370 * IO has good status or fails for any other reason then the sglq1371 * entry is added to the free list (lpfc_els_sgl_list). The hbalock is1372 *  asserted held in the code path calling this routine.1373 **/1374static void1375__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)1376{1377	struct lpfc_sglq *sglq;1378	unsigned long iflag = 0;1379	struct lpfc_sli_ring *pring;1380 1381	if (iocbq->sli4_xritag == NO_XRI)1382		sglq = NULL;1383	else1384		sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);1385 1386 1387	if (sglq)  {1388		if (iocbq->cmd_flag & LPFC_IO_NVMET) {1389			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,1390					  iflag);1391			sglq->state = SGL_FREED;1392			sglq->ndlp = NULL;1393			list_add_tail(&sglq->list,1394				      &phba->sli4_hba.lpfc_nvmet_sgl_list);1395			spin_unlock_irqrestore(1396				&phba->sli4_hba.sgl_list_lock, iflag);1397			goto out;1398		}1399 1400		if ((iocbq->cmd_flag & LPFC_EXCHANGE_BUSY) &&1401		    (!(unlikely(pci_channel_offline(phba->pcidev)))) &&1402		    sglq->state != SGL_XRI_ABORTED) {1403			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,1404					  iflag);1405 1406			/* Check if we can get a reference on ndlp */1407			if (sglq->ndlp && !lpfc_nlp_get(sglq->ndlp))1408				sglq->ndlp = NULL;1409 1410			list_add(&sglq->list,1411				 &phba->sli4_hba.lpfc_abts_els_sgl_list);1412			spin_unlock_irqrestore(1413				&phba->sli4_hba.sgl_list_lock, iflag);1414		} else {1415			spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,1416					  iflag);1417			sglq->state = SGL_FREED;1418			sglq->ndlp = NULL;1419			list_add_tail(&sglq->list,1420				      &phba->sli4_hba.lpfc_els_sgl_list);1421			spin_unlock_irqrestore(1422				&phba->sli4_hba.sgl_list_lock, iflag);1423			pring = lpfc_phba_elsring(phba);1424			/* Check if TXQ queue needs to be serviced */1425			if (pring && (!list_empty(&pring->txq)))1426				lpfc_worker_wake_up(phba);1427		}1428	}1429 1430out:1431	/*1432	 * Clean all volatile data fields, preserve iotag and node struct.1433	 */1434	memset_startat(iocbq, 0, wqe);1435	iocbq->sli4_lxritag = NO_XRI;1436	iocbq->sli4_xritag = NO_XRI;1437	iocbq->cmd_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET | LPFC_IO_CMF |1438			      LPFC_IO_NVME_LS);1439	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);1440}1441 1442 1443/**1444 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool1445 * @phba: Pointer to HBA context object.1446 * @iocbq: Pointer to driver iocb object.1447 *1448 * This function is called to release the driver iocb object to the1449 * iocb pool. The iotag in the iocb object does not change for each1450 * use of the iocb object. This function clears all other fields of1451 * the iocb object when it is freed. The hbalock is asserted held in1452 * the code path calling this routine.1453 **/1454static void1455__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)1456{1457 1458	/*1459	 * Clean all volatile data fields, preserve iotag and node struct.1460	 */1461	memset_startat(iocbq, 0, iocb);1462	iocbq->sli4_xritag = NO_XRI;1463	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);1464}1465 1466/**1467 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool1468 * @phba: Pointer to HBA context object.1469 * @iocbq: Pointer to driver iocb object.1470 *1471 * This function is called with hbalock held to release driver1472 * iocb object to the iocb pool. The iotag in the iocb object1473 * does not change for each use of the iocb object. This function1474 * clears all other fields of the iocb object when it is freed.1475 **/1476static void1477__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)1478{1479	lockdep_assert_held(&phba->hbalock);1480 1481	phba->__lpfc_sli_release_iocbq(phba, iocbq);1482	phba->iocb_cnt--;1483}1484 1485/**1486 * lpfc_sli_release_iocbq - Release iocb to the iocb pool1487 * @phba: Pointer to HBA context object.1488 * @iocbq: Pointer to driver iocb object.1489 *1490 * This function is called with no lock held to release the iocb to1491 * iocb pool.1492 **/1493void1494lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)1495{1496	unsigned long iflags;1497 1498	/*1499	 * Clean all volatile data fields, preserve iotag and node struct.1500	 */1501	spin_lock_irqsave(&phba->hbalock, iflags);1502	__lpfc_sli_release_iocbq(phba, iocbq);1503	spin_unlock_irqrestore(&phba->hbalock, iflags);1504}1505 1506/**1507 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.1508 * @phba: Pointer to HBA context object.1509 * @iocblist: List of IOCBs.1510 * @ulpstatus: ULP status in IOCB command field.1511 * @ulpWord4: ULP word-4 in IOCB command field.1512 *1513 * This function is called with a list of IOCBs to cancel. It cancels the IOCB1514 * on the list by invoking the complete callback function associated with the1515 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond1516 * fields.1517 **/1518void1519lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,1520		      uint32_t ulpstatus, uint32_t ulpWord4)1521{1522	struct lpfc_iocbq *piocb;1523 1524	while (!list_empty(iocblist)) {1525		list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);1526		if (piocb->cmd_cmpl) {1527			if (piocb->cmd_flag & LPFC_IO_NVME) {1528				lpfc_nvme_cancel_iocb(phba, piocb,1529						      ulpstatus, ulpWord4);1530			} else {1531				if (phba->sli_rev == LPFC_SLI_REV4) {1532					bf_set(lpfc_wcqe_c_status,1533					       &piocb->wcqe_cmpl, ulpstatus);1534					piocb->wcqe_cmpl.parameter = ulpWord4;1535				} else {1536					piocb->iocb.ulpStatus = ulpstatus;1537					piocb->iocb.un.ulpWord[4] = ulpWord4;1538				}1539				(piocb->cmd_cmpl) (phba, piocb, piocb);1540			}1541		} else {1542			lpfc_sli_release_iocbq(phba, piocb);1543		}1544	}1545	return;1546}1547 1548/**1549 * lpfc_sli_iocb_cmd_type - Get the iocb type1550 * @iocb_cmnd: iocb command code.1551 *1552 * This function is called by ring event handler function to get the iocb type.1553 * This function translates the iocb command to an iocb command type used to1554 * decide the final disposition of each completed IOCB.1555 * The function returns1556 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb1557 * LPFC_SOL_IOCB     if it is a solicited iocb completion1558 * LPFC_ABORT_IOCB   if it is an abort iocb1559 * LPFC_UNSOL_IOCB   if it is an unsolicited iocb1560 *1561 * The caller is not required to hold any lock.1562 **/1563static lpfc_iocb_type1564lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)1565{1566	lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;1567 1568	if (iocb_cmnd > CMD_MAX_IOCB_CMD)1569		return 0;1570 1571	switch (iocb_cmnd) {1572	case CMD_XMIT_SEQUENCE_CR:1573	case CMD_XMIT_SEQUENCE_CX:1574	case CMD_XMIT_BCAST_CN:1575	case CMD_XMIT_BCAST_CX:1576	case CMD_ELS_REQUEST_CR:1577	case CMD_ELS_REQUEST_CX:1578	case CMD_CREATE_XRI_CR:1579	case CMD_CREATE_XRI_CX:1580	case CMD_GET_RPI_CN:1581	case CMD_XMIT_ELS_RSP_CX:1582	case CMD_GET_RPI_CR:1583	case CMD_FCP_IWRITE_CR:1584	case CMD_FCP_IWRITE_CX:1585	case CMD_FCP_IREAD_CR:1586	case CMD_FCP_IREAD_CX:1587	case CMD_FCP_ICMND_CR:1588	case CMD_FCP_ICMND_CX:1589	case CMD_FCP_TSEND_CX:1590	case CMD_FCP_TRSP_CX:1591	case CMD_FCP_TRECEIVE_CX:1592	case CMD_FCP_AUTO_TRSP_CX:1593	case CMD_ADAPTER_MSG:1594	case CMD_ADAPTER_DUMP:1595	case CMD_XMIT_SEQUENCE64_CR:1596	case CMD_XMIT_SEQUENCE64_CX:1597	case CMD_XMIT_BCAST64_CN:1598	case CMD_XMIT_BCAST64_CX:1599	case CMD_ELS_REQUEST64_CR:1600	case CMD_ELS_REQUEST64_CX:1601	case CMD_FCP_IWRITE64_CR:1602	case CMD_FCP_IWRITE64_CX:1603	case CMD_FCP_IREAD64_CR:1604	case CMD_FCP_IREAD64_CX:1605	case CMD_FCP_ICMND64_CR:1606	case CMD_FCP_ICMND64_CX:1607	case CMD_FCP_TSEND64_CX:1608	case CMD_FCP_TRSP64_CX:1609	case CMD_FCP_TRECEIVE64_CX:1610	case CMD_GEN_REQUEST64_CR:1611	case CMD_GEN_REQUEST64_CX:1612	case CMD_XMIT_ELS_RSP64_CX:1613	case DSSCMD_IWRITE64_CR:1614	case DSSCMD_IWRITE64_CX:1615	case DSSCMD_IREAD64_CR:1616	case DSSCMD_IREAD64_CX:1617	case CMD_SEND_FRAME:1618		type = LPFC_SOL_IOCB;1619		break;1620	case CMD_ABORT_XRI_CN:1621	case CMD_ABORT_XRI_CX:1622	case CMD_CLOSE_XRI_CN:1623	case CMD_CLOSE_XRI_CX:1624	case CMD_XRI_ABORTED_CX:1625	case CMD_ABORT_MXRI64_CN:1626	case CMD_XMIT_BLS_RSP64_CX:1627		type = LPFC_ABORT_IOCB;1628		break;1629	case CMD_RCV_SEQUENCE_CX:1630	case CMD_RCV_ELS_REQ_CX:1631	case CMD_RCV_SEQUENCE64_CX:1632	case CMD_RCV_ELS_REQ64_CX:1633	case CMD_ASYNC_STATUS:1634	case CMD_IOCB_RCV_SEQ64_CX:1635	case CMD_IOCB_RCV_ELS64_CX:1636	case CMD_IOCB_RCV_CONT64_CX:1637	case CMD_IOCB_RET_XRI64_CX:1638		type = LPFC_UNSOL_IOCB;1639		break;1640	case CMD_IOCB_XMIT_MSEQ64_CR:1641	case CMD_IOCB_XMIT_MSEQ64_CX:1642	case CMD_IOCB_RCV_SEQ_LIST64_CX:1643	case CMD_IOCB_RCV_ELS_LIST64_CX:1644	case CMD_IOCB_CLOSE_EXTENDED_CN:1645	case CMD_IOCB_ABORT_EXTENDED_CN:1646	case CMD_IOCB_RET_HBQE64_CN:1647	case CMD_IOCB_FCP_IBIDIR64_CR:1648	case CMD_IOCB_FCP_IBIDIR64_CX:1649	case CMD_IOCB_FCP_ITASKMGT64_CX:1650	case CMD_IOCB_LOGENTRY_CN:1651	case CMD_IOCB_LOGENTRY_ASYNC_CN:1652		printk("%s - Unhandled SLI-3 Command x%x\n",1653				__func__, iocb_cmnd);1654		type = LPFC_UNKNOWN_IOCB;1655		break;1656	default:1657		type = LPFC_UNKNOWN_IOCB;1658		break;1659	}1660 1661	return type;1662}1663 1664/**1665 * lpfc_sli_ring_map - Issue config_ring mbox for all rings1666 * @phba: Pointer to HBA context object.1667 *1668 * This function is called from SLI initialization code1669 * to configure every ring of the HBA's SLI interface. The1670 * caller is not required to hold any lock. This function issues1671 * a config_ring mailbox command for each ring.1672 * This function returns zero if successful else returns a negative1673 * error code.1674 **/1675static int1676lpfc_sli_ring_map(struct lpfc_hba *phba)1677{1678	struct lpfc_sli *psli = &phba->sli;1679	LPFC_MBOXQ_t *pmb;1680	MAILBOX_t *pmbox;1681	int i, rc, ret = 0;1682 1683	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);1684	if (!pmb)1685		return -ENOMEM;1686	pmbox = &pmb->u.mb;1687	phba->link_state = LPFC_INIT_MBX_CMDS;1688	for (i = 0; i < psli->num_rings; i++) {1689		lpfc_config_ring(phba, i, pmb);1690		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);1691		if (rc != MBX_SUCCESS) {1692			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,1693					"0446 Adapter failed to init (%d), "1694					"mbxCmd x%x CFG_RING, mbxStatus x%x, "1695					"ring %d\n",1696					rc, pmbox->mbxCommand,1697					pmbox->mbxStatus, i);1698			phba->link_state = LPFC_HBA_ERROR;1699			ret = -ENXIO;1700			break;1701		}1702	}1703	mempool_free(pmb, phba->mbox_mem_pool);1704	return ret;1705}1706 1707/**1708 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq1709 * @phba: Pointer to HBA context object.1710 * @pring: Pointer to driver SLI ring object.1711 * @piocb: Pointer to the driver iocb object.1712 *1713 * The driver calls this function with the hbalock held for SLI3 ports or1714 * the ring lock held for SLI4 ports. The function adds the1715 * new iocb to txcmplq of the given ring. This function always returns1716 * 0. If this function is called for ELS ring, this function checks if1717 * there is a vport associated with the ELS command. This function also1718 * starts els_tmofunc timer if this is an ELS command.1719 **/1720static int1721lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,1722			struct lpfc_iocbq *piocb)1723{1724	u32 ulp_command = 0;1725 1726	BUG_ON(!piocb);1727	ulp_command = get_job_cmnd(phba, piocb);1728 1729	list_add_tail(&piocb->list, &pring->txcmplq);1730	piocb->cmd_flag |= LPFC_IO_ON_TXCMPLQ;1731	pring->txcmplq_cnt++;1732	if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&1733	   (ulp_command != CMD_ABORT_XRI_WQE) &&1734	   (ulp_command != CMD_ABORT_XRI_CN) &&1735	   (ulp_command != CMD_CLOSE_XRI_CN)) {1736		BUG_ON(!piocb->vport);1737		if (!test_bit(FC_UNLOADING, &piocb->vport->load_flag))1738			mod_timer(&piocb->vport->els_tmofunc,1739				  jiffies +1740				  msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));1741	}1742 1743	return 0;1744}1745 1746/**1747 * lpfc_sli_ringtx_get - Get first element of the txq1748 * @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 next1752 * iocb in txq of the given ring. If there is any iocb in1753 * the txq, the function returns first iocb in the list after1754 * 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 cmpl1769 * @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 need1774 * to make. These changes will be picked up during the next CMF1775 * timer interrupt. In addition, any BW changes will be logged1776 * with LOG_CGN_MGMT.1777 **/1778static void1779lpfc_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	else1814		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 in1852		 * lpfc_cmf_timer() the cmf_link_byte_count can be shorter and1853		 * 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_WQE1919 * @phba: Pointer to HBA context object.1920 * @ms:   ms to set in WQE interval, 0 means use init op1921 * @total: Total rcv bytes for this interval1922 *1923 * This routine is called every CMF timer interrupt. Its purpose is1924 * to issue a CMF_SYNC_WQE to the firmware to inform it of any events1925 * that may indicate we have congestion (FPINs or Signals). Upon1926 * completion, the firmware will indicate any BW restrictions the1927 * driver may need to take.1928 **/1929int1930lpfc_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;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			/* We hit an Signal warning condition */1991			max = LPFC_SEC_TO_MSEC / lpfc_fabric_cgn_frequency *1992				lpfc_acqe_cgn_frequency;1993			bf_set(cmf_sync_wsigmax, &wqe->cmf_sync, max);1994			bf_set(cmf_sync_wsigcnt, &wqe->cmf_sync, wtot);1995			warn_sync_period = lpfc_acqe_cgn_frequency;1996		} else {1997			/* We hit a FPIN warning condition */1998			bf_set(cmf_sync_wfpinmax, &wqe->cmf_sync, 1);1999			bf_set(cmf_sync_wfpincnt, &wqe->cmf_sync, 1);2000			if (phba->cgn_fpin_frequency != LPFC_FPIN_INIT_FREQ)2001				warn_sync_period =2002				LPFC_MSECS_TO_SECS(phba->cgn_fpin_frequency);2003		}2004	}2005 2006	/* Update total read blocks during previous timer interval */2007	wqe->cmf_sync.read_bytes = (u32)(total / LPFC_CMF_BLK_SIZE);2008 2009initpath:2010	bf_set(cmf_sync_ver, &wqe->cmf_sync, LPFC_CMF_SYNC_VER);2011	wqe->cmf_sync.event_tag = phba->fc_eventTag;2012	bf_set(cmf_sync_cmnd, &wqe->cmf_sync, CMD_CMF_SYNC_WQE);2013 2014	/* Setup reqtag to match the wqe completion. */2015	bf_set(cmf_sync_reqtag, &wqe->cmf_sync, sync_buf->iotag);2016 2017	bf_set(cmf_sync_qosd, &wqe->cmf_sync, 1);2018	bf_set(cmf_sync_period, &wqe->cmf_sync, warn_sync_period);2019 2020	bf_set(cmf_sync_cmd_type, &wqe->cmf_sync, CMF_SYNC_COMMAND);2021	bf_set(cmf_sync_wqec, &wqe->cmf_sync, 1);2022	bf_set(cmf_sync_cqid, &wqe->cmf_sync, LPFC_WQE_CQ_ID_DEFAULT);2023 2024	sync_buf->vport = phba->pport;2025	sync_buf->cmd_cmpl = lpfc_cmf_sync_cmpl;2026	sync_buf->cmd_dmabuf = NULL;2027	sync_buf->rsp_dmabuf = NULL;2028	sync_buf->bpl_dmabuf = NULL;2029	sync_buf->sli4_xritag = NO_XRI;2030 2031	sync_buf->cmd_flag |= LPFC_IO_CMF;2032	ret_val = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], sync_buf);2033	if (ret_val) {2034		lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,2035				"6214 Cannot issue CMF_SYNC_WQE: x%x\n",2036				ret_val);2037		__lpfc_sli_release_iocbq(phba, sync_buf);2038	}2039out_unlock:2040	spin_unlock_irqrestore(&phba->hbalock, iflags);2041	return ret_val;2042}2043 2044/**2045 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring2046 * @phba: Pointer to HBA context object.2047 * @pring: Pointer to driver SLI ring object.2048 *2049 * This function is called with hbalock held and the caller must post the2050 * iocb without releasing the lock. If the caller releases the lock,2051 * iocb slot returned by the function is not guaranteed to be available.2052 * The function returns pointer to the next available iocb slot if there2053 * is available slot in the ring, else it returns NULL.2054 * If the get index of the ring is ahead of the put index, the function2055 * will post an error attention event to the worker thread to take the2056 * HBA to offline state.2057 **/2058static IOCB_t *2059lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)2060{2061	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];2062	uint32_t  max_cmd_idx = pring->sli.sli3.numCiocb;2063 2064	lockdep_assert_held(&phba->hbalock);2065 2066	if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&2067	   (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))2068		pring->sli.sli3.next_cmdidx = 0;2069 2070	if (unlikely(pring->sli.sli3.local_getidx ==2071		pring->sli.sli3.next_cmdidx)) {2072 2073		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);2074 2075		if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {2076			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,2077					"0315 Ring %d issue: portCmdGet %d "2078					"is bigger than cmd ring %d\n",2079					pring->ringno,2080					pring->sli.sli3.local_getidx,2081					max_cmd_idx);2082 2083			phba->link_state = LPFC_HBA_ERROR;2084			/*2085			 * All error attention handlers are posted to2086			 * worker thread2087			 */2088			phba->work_ha |= HA_ERATT;2089			phba->work_hs = HS_FFER3;2090 2091			lpfc_worker_wake_up(phba);2092 2093			return NULL;2094		}2095 2096		if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)2097			return NULL;2098	}2099 2100	return lpfc_cmd_iocb(phba, pring);2101}2102 2103/**2104 * lpfc_sli_next_iotag - Get an iotag for the iocb2105 * @phba: Pointer to HBA context object.2106 * @iocbq: Pointer to driver iocb object.2107 *2108 * This function gets an iotag for the iocb. If there is no unused iotag and2109 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup2110 * array and assigns a new iotag.2111 * The function returns the allocated iotag if successful, else returns zero.2112 * Zero is not a valid iotag.2113 * The caller is not required to hold any lock.2114 **/2115uint16_t2116lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)2117{2118	struct lpfc_iocbq **new_arr;2119	struct lpfc_iocbq **old_arr;2120	size_t new_len;2121	struct lpfc_sli *psli = &phba->sli;2122	uint16_t iotag;2123 2124	spin_lock_irq(&phba->hbalock);2125	iotag = psli->last_iotag;2126	if(++iotag < psli->iocbq_lookup_len) {2127		psli->last_iotag = iotag;2128		psli->iocbq_lookup[iotag] = iocbq;2129		spin_unlock_irq(&phba->hbalock);2130		iocbq->iotag = iotag;2131		return iotag;2132	} else if (psli->iocbq_lookup_len < (0xffff2133					   - LPFC_IOCBQ_LOOKUP_INCREMENT)) {2134		new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;2135		spin_unlock_irq(&phba->hbalock);2136		new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),2137				  GFP_KERNEL);2138		if (new_arr) {2139			spin_lock_irq(&phba->hbalock);2140			old_arr = psli->iocbq_lookup;2141			if (new_len <= psli->iocbq_lookup_len) {2142				/* highly unprobable case */2143				kfree(new_arr);2144				iotag = psli->last_iotag;2145				if(++iotag < psli->iocbq_lookup_len) {2146					psli->last_iotag = iotag;2147					psli->iocbq_lookup[iotag] = iocbq;2148					spin_unlock_irq(&phba->hbalock);2149					iocbq->iotag = iotag;2150					return iotag;2151				}2152				spin_unlock_irq(&phba->hbalock);2153				return 0;2154			}2155			if (psli->iocbq_lookup)2156				memcpy(new_arr, old_arr,2157				       ((psli->last_iotag  + 1) *2158					sizeof (struct lpfc_iocbq *)));2159			psli->iocbq_lookup = new_arr;2160			psli->iocbq_lookup_len = new_len;2161			psli->last_iotag = iotag;2162			psli->iocbq_lookup[iotag] = iocbq;2163			spin_unlock_irq(&phba->hbalock);2164			iocbq->iotag = iotag;2165			kfree(old_arr);2166			return iotag;2167		}2168	} else2169		spin_unlock_irq(&phba->hbalock);2170 2171	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,2172			"0318 Failed to allocate IOTAG.last IOTAG is %d\n",2173			psli->last_iotag);2174 2175	return 0;2176}2177 2178/**2179 * lpfc_sli_submit_iocb - Submit an iocb to the firmware2180 * @phba: Pointer to HBA context object.2181 * @pring: Pointer to driver SLI ring object.2182 * @iocb: Pointer to iocb slot in the ring.2183 * @nextiocb: Pointer to driver iocb object which need to be2184 *            posted to firmware.2185 *2186 * This function is called to post a new iocb to the firmware. This2187 * function copies the new iocb to ring iocb slot and updates the2188 * ring pointers. It adds the new iocb to txcmplq if there is2189 * a completion call back for this iocb else the function will free the2190 * iocb object.  The hbalock is asserted held in the code path calling2191 * this routine.2192 **/2193static void2194lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,2195		IOCB_t *iocb, struct lpfc_iocbq *nextiocb)2196{2197	/*2198	 * Set up an iotag2199	 */2200	nextiocb->iocb.ulpIoTag = (nextiocb->cmd_cmpl) ? nextiocb->iotag : 0;2201 2202 2203	if (pring->ringno == LPFC_ELS_RING) {2204		lpfc_debugfs_slow_ring_trc(phba,2205			"IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",2206			*(((uint32_t *) &nextiocb->iocb) + 4),2207			*(((uint32_t *) &nextiocb->iocb) + 6),2208			*(((uint32_t *) &nextiocb->iocb) + 7));2209	}2210 2211	/*2212	 * Issue iocb command to adapter2213	 */2214	lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);2215	wmb();2216	pring->stats.iocb_cmd++;2217 2218	/*2219	 * If there is no completion routine to call, we can release the2220	 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,2221	 * that have no rsp ring completion, cmd_cmpl MUST be NULL.2222	 */2223	if (nextiocb->cmd_cmpl)2224		lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);2225	else2226		__lpfc_sli_release_iocbq(phba, nextiocb);2227 2228	/*2229	 * Let the HBA know what IOCB slot will be the next one the2230	 * driver will put a command into.2231	 */2232	pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;2233	writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);2234}2235 2236/**2237 * lpfc_sli_update_full_ring - Update the chip attention register2238 * @phba: Pointer to HBA context object.2239 * @pring: Pointer to driver SLI ring object.2240 *2241 * The caller is not required to hold any lock for calling this function.2242 * This function updates the chip attention bits for the ring to inform firmware2243 * that there are pending work to be done for this ring and requests an2244 * interrupt when there is space available in the ring. This function is2245 * called when the driver is unable to post more iocbs to the ring due2246 * to unavailability of space in the ring.2247 **/2248static void2249lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)2250{2251	int ringno = pring->ringno;2252 2253	pring->flag |= LPFC_CALL_RING_AVAILABLE;2254 2255	wmb();2256 2257	/*2258	 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.2259	 * The HBA will tell us when an IOCB entry is available.2260	 */2261	writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);2262	readl(phba->CAregaddr); /* flush */2263 2264	pring->stats.iocb_cmd_full++;2265}2266 2267/**2268 * lpfc_sli_update_ring - Update chip attention register2269 * @phba: Pointer to HBA context object.2270 * @pring: Pointer to driver SLI ring object.2271 *2272 * This function updates the chip attention register bit for the2273 * given ring to inform HBA that there is more work to be done2274 * in this ring. The caller is not required to hold any lock.2275 **/2276static void2277lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)2278{2279	int ringno = pring->ringno;2280 2281	/*2282	 * Tell the HBA that there is work to do in this ring.2283	 */2284	if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {2285		wmb();2286		writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);2287		readl(phba->CAregaddr); /* flush */2288	}2289}2290 2291/**2292 * lpfc_sli_resume_iocb - Process iocbs in the txq2293 * @phba: Pointer to HBA context object.2294 * @pring: Pointer to driver SLI ring object.2295 *2296 * This function is called with hbalock held to post pending iocbs2297 * in the txq to the firmware. This function is called when driver2298 * detects space available in the ring.2299 **/2300static void2301lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)2302{2303	IOCB_t *iocb;2304	struct lpfc_iocbq *nextiocb;2305 2306	lockdep_assert_held(&phba->hbalock);2307 2308	/*2309	 * Check to see if:2310	 *  (a) there is anything on the txq to send2311	 *  (b) link is up2312	 *  (c) link attention events can be processed (fcp ring only)2313	 *  (d) IOCB processing is not blocked by the outstanding mbox command.2314	 */2315 2316	if (lpfc_is_link_up(phba) &&2317	    (!list_empty(&pring->txq)) &&2318	    (pring->ringno != LPFC_FCP_RING ||2319	     phba->sli.sli_flag & LPFC_PROCESS_LA)) {2320 2321		while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&2322		       (nextiocb = lpfc_sli_ringtx_get(phba, pring)))2323			lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);2324 2325		if (iocb)2326			lpfc_sli_update_ring(phba, pring);2327		else2328			lpfc_sli_update_full_ring(phba, pring);2329	}2330 2331	return;2332}2333 2334/**2335 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ2336 * @phba: Pointer to HBA context object.2337 * @hbqno: HBQ number.2338 *2339 * This function is called with hbalock held to get the next2340 * available slot for the given HBQ. If there is free slot2341 * available for the HBQ it will return pointer to the next available2342 * HBQ entry else it will return NULL.2343 **/2344static struct lpfc_hbq_entry *2345lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)2346{2347	struct hbq_s *hbqp = &phba->hbqs[hbqno];2348 2349	lockdep_assert_held(&phba->hbalock);2350 2351	if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&2352	    ++hbqp->next_hbqPutIdx >= hbqp->entry_count)2353		hbqp->next_hbqPutIdx = 0;2354 2355	if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {2356		uint32_t raw_index = phba->hbq_get[hbqno];2357		uint32_t getidx = le32_to_cpu(raw_index);2358 2359		hbqp->local_hbqGetIdx = getidx;2360 2361		if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {2362			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,2363					"1802 HBQ %d: local_hbqGetIdx "2364					"%u is > than hbqp->entry_count %u\n",2365					hbqno, hbqp->local_hbqGetIdx,2366					hbqp->entry_count);2367 2368			phba->link_state = LPFC_HBA_ERROR;2369			return NULL;2370		}2371 2372		if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)2373			return NULL;2374	}2375 2376	return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +2377			hbqp->hbqPutIdx;2378}2379 2380/**2381 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers2382 * @phba: Pointer to HBA context object.2383 *2384 * This function is called with no lock held to free all the2385 * hbq buffers while uninitializing the SLI interface. It also2386 * frees the HBQ buffers returned by the firmware but not yet2387 * processed by the upper layers.2388 **/2389void2390lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)2391{2392	struct lpfc_dmabuf *dmabuf, *next_dmabuf;2393	struct hbq_dmabuf *hbq_buf;2394	unsigned long flags;2395	int i, hbq_count;2396 2397	hbq_count = lpfc_sli_hbq_count();2398	/* Return all memory used by all HBQs */2399	spin_lock_irqsave(&phba->hbalock, flags);2400	for (i = 0; i < hbq_count; ++i) {2401		list_for_each_entry_safe(dmabuf, next_dmabuf,2402				&phba->hbqs[i].hbq_buffer_list, list) {2403			hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);2404			list_del(&hbq_buf->dbuf.list);2405			(phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);2406		}2407		phba->hbqs[i].buffer_count = 0;2408	}2409 2410	/* Mark the HBQs not in use */2411	phba->hbq_in_use = 0;2412	spin_unlock_irqrestore(&phba->hbalock, flags);2413}2414 2415/**2416 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware2417 * @phba: Pointer to HBA context object.2418 * @hbqno: HBQ number.2419 * @hbq_buf: Pointer to HBQ buffer.2420 *2421 * This function is called with the hbalock held to post a2422 * hbq buffer to the firmware. If the function finds an empty2423 * slot in the HBQ, it will post the buffer. The function will return2424 * pointer to the hbq entry if it successfully post the buffer2425 * else it will return NULL.2426 **/2427static int2428lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,2429			 struct hbq_dmabuf *hbq_buf)2430{2431	lockdep_assert_held(&phba->hbalock);2432	return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);2433}2434 2435/**2436 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware2437 * @phba: Pointer to HBA context object.2438 * @hbqno: HBQ number.2439 * @hbq_buf: Pointer to HBQ buffer.2440 *2441 * This function is called with the hbalock held to post a hbq buffer to the2442 * firmware. If the function finds an empty slot in the HBQ, it will post the2443 * buffer and place it on the hbq_buffer_list. The function will return zero if2444 * it successfully post the buffer else it will return an error.2445 **/2446static int2447lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,2448			    struct hbq_dmabuf *hbq_buf)2449{2450	struct lpfc_hbq_entry *hbqe;2451	dma_addr_t physaddr = hbq_buf->dbuf.phys;2452 2453	lockdep_assert_held(&phba->hbalock);2454	/* Get next HBQ entry slot to use */2455	hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);2456	if (hbqe) {2457		struct hbq_s *hbqp = &phba->hbqs[hbqno];2458 2459		hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));2460		hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));2461		hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;2462		hbqe->bde.tus.f.bdeFlags = 0;2463		hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);2464		hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);2465				/* Sync SLIM */2466		hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;2467		writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);2468				/* flush */2469		readl(phba->hbq_put + hbqno);2470		list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);2471		return 0;2472	} else2473		return -ENOMEM;2474}2475 2476/**2477 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware2478 * @phba: Pointer to HBA context object.2479 * @hbqno: HBQ number.2480 * @hbq_buf: Pointer to HBQ buffer.2481 *2482 * This function is called with the hbalock held to post an RQE to the SLI42483 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to2484 * the hbq_buffer_list and return zero, otherwise it will return an error.2485 **/2486static int2487lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,2488			    struct hbq_dmabuf *hbq_buf)2489{2490	int rc;2491	struct lpfc_rqe hrqe;2492	struct lpfc_rqe drqe;2493	struct lpfc_queue *hrq;2494	struct lpfc_queue *drq;2495 2496	if (hbqno != LPFC_ELS_HBQ)2497		return 1;2498	hrq = phba->sli4_hba.hdr_rq;2499	drq = phba->sli4_hba.dat_rq;2500 2501	lockdep_assert_held(&phba->hbalock);2502	hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);2503	hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);2504	drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);2505	drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);2506	rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);2507	if (rc < 0)2508		return rc;2509	hbq_buf->tag = (rc | (hbqno << 16));2510	list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);2511	return 0;2512}2513 2514/* HBQ for ELS and CT traffic. */2515static struct lpfc_hbq_init lpfc_els_hbq = {2516	.rn = 1,2517	.entry_count = 256,2518	.mask_count = 0,2519	.profile = 0,2520	.ring_mask = (1 << LPFC_ELS_RING),2521	.buffer_count = 0,2522	.init_count = 40,2523	.add_count = 40,2524};2525 2526/* Array of HBQs */2527struct lpfc_hbq_init *lpfc_hbq_defs[] = {2528	&lpfc_els_hbq,2529};2530 2531/**2532 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ2533 * @phba: Pointer to HBA context object.2534 * @hbqno: HBQ number.2535 * @count: Number of HBQ buffers to be posted.2536 *2537 * This function is called with no lock held to post more hbq buffers to the2538 * given HBQ. The function returns the number of HBQ buffers successfully2539 * posted.2540 **/2541static int2542lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)2543{2544	uint32_t i, posted = 0;2545	unsigned long flags;2546	struct hbq_dmabuf *hbq_buffer;2547	LIST_HEAD(hbq_buf_list);2548	if (!phba->hbqs[hbqno].hbq_alloc_buffer)2549		return 0;2550 2551	if ((phba->hbqs[hbqno].buffer_count + count) >2552	    lpfc_hbq_defs[hbqno]->entry_count)2553		count = lpfc_hbq_defs[hbqno]->entry_count -2554					phba->hbqs[hbqno].buffer_count;2555	if (!count)2556		return 0;2557	/* Allocate HBQ entries */2558	for (i = 0; i < count; i++) {2559		hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);2560		if (!hbq_buffer)2561			break;2562		list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);2563	}2564	/* Check whether HBQ is still in use */2565	spin_lock_irqsave(&phba->hbalock, flags);2566	if (!phba->hbq_in_use)2567		goto err;2568	while (!list_empty(&hbq_buf_list)) {2569		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,2570				 dbuf.list);2571		hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |2572				      (hbqno << 16));2573		if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {2574			phba->hbqs[hbqno].buffer_count++;2575			posted++;2576		} else2577			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);2578	}2579	spin_unlock_irqrestore(&phba->hbalock, flags);2580	return posted;2581err:2582	spin_unlock_irqrestore(&phba->hbalock, flags);2583	while (!list_empty(&hbq_buf_list)) {2584		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,2585				 dbuf.list);2586		(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);2587	}2588	return 0;2589}2590 2591/**2592 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware2593 * @phba: Pointer to HBA context object.2594 * @qno: HBQ number.2595 *2596 * This function posts more buffers to the HBQ. This function2597 * is called with no lock held. The function returns the number of HBQ entries2598 * successfully allocated.2599 **/2600int2601lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)2602{2603	if (phba->sli_rev == LPFC_SLI_REV4)2604		return 0;2605	else2606		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,2607					 lpfc_hbq_defs[qno]->add_count);2608}2609 2610/**2611 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ2612 * @phba: Pointer to HBA context object.2613 * @qno:  HBQ queue number.2614 *2615 * This function is called from SLI initialization code path with2616 * no lock held to post initial HBQ buffers to firmware. The2617 * function returns the number of HBQ entries successfully allocated.2618 **/2619static int2620lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)2621{2622	if (phba->sli_rev == LPFC_SLI_REV4)2623		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,2624					lpfc_hbq_defs[qno]->entry_count);2625	else2626		return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,2627					 lpfc_hbq_defs[qno]->init_count);2628}2629 2630/*2631 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list2632 *2633 * This function removes the first hbq buffer on an hbq list and returns a2634 * pointer to that buffer. If it finds no buffers on the list it returns NULL.2635 **/2636static struct hbq_dmabuf *2637lpfc_sli_hbqbuf_get(struct list_head *rb_list)2638{2639	struct lpfc_dmabuf *d_buf;2640 2641	list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);2642	if (!d_buf)2643		return NULL;2644	return container_of(d_buf, struct hbq_dmabuf, dbuf);2645}2646 2647/**2648 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list2649 * @phba: Pointer to HBA context object.2650 * @hrq: HBQ number.2651 *2652 * This function removes the first RQ buffer on an RQ buffer list and returns a2653 * pointer to that buffer. If it finds no buffers on the list it returns NULL.2654 **/2655static struct rqb_dmabuf *2656lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)2657{2658	struct lpfc_dmabuf *h_buf;2659	struct lpfc_rqb *rqbp;2660 2661	rqbp = hrq->rqbp;2662	list_remove_head(&rqbp->rqb_buffer_list, h_buf,2663			 struct lpfc_dmabuf, list);2664	if (!h_buf)2665		return NULL;2666	rqbp->buffer_count--;2667	return container_of(h_buf, struct rqb_dmabuf, hbuf);2668}2669 2670/**2671 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag2672 * @phba: Pointer to HBA context object.2673 * @tag: Tag of the hbq buffer.2674 *2675 * This function searches for the hbq buffer associated with the given tag in2676 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer2677 * otherwise it returns NULL.2678 **/2679static struct hbq_dmabuf *2680lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)2681{2682	struct lpfc_dmabuf *d_buf;2683	struct hbq_dmabuf *hbq_buf;2684	uint32_t hbqno;2685 2686	hbqno = tag >> 16;2687	if (hbqno >= LPFC_MAX_HBQS)2688		return NULL;2689 2690	spin_lock_irq(&phba->hbalock);2691	list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {2692		hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);2693		if (hbq_buf->tag == tag) {2694			spin_unlock_irq(&phba->hbalock);2695			return hbq_buf;2696		}2697	}2698	spin_unlock_irq(&phba->hbalock);2699	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,2700			"1803 Bad hbq tag. Data: x%x x%x\n",2701			tag, phba->hbqs[tag >> 16].buffer_count);2702	return NULL;2703}2704 2705/**2706 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware2707 * @phba: Pointer to HBA context object.2708 * @hbq_buffer: Pointer to HBQ buffer.2709 *2710 * This function is called with hbalock. This function gives back2711 * the hbq buffer to firmware. If the HBQ does not have space to2712 * post the buffer, it will free the buffer.2713 **/2714void2715lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)2716{2717	uint32_t hbqno;2718 2719	if (hbq_buffer) {2720		hbqno = hbq_buffer->tag >> 16;2721		if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))2722			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);2723	}2724}2725 2726/**2727 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox2728 * @mbxCommand: mailbox command code.2729 *2730 * This function is called by the mailbox event handler function to verify2731 * that the completed mailbox command is a legitimate mailbox command. If the2732 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN2733 * and the mailbox event handler will take the HBA offline.2734 **/2735static int2736lpfc_sli_chk_mbx_command(uint8_t mbxCommand)2737{2738	uint8_t ret;2739 2740	switch (mbxCommand) {2741	case MBX_LOAD_SM:2742	case MBX_READ_NV:2743	case MBX_WRITE_NV:2744	case MBX_WRITE_VPARMS:2745	case MBX_RUN_BIU_DIAG:2746	case MBX_INIT_LINK:2747	case MBX_DOWN_LINK:2748	case MBX_CONFIG_LINK:2749	case MBX_CONFIG_RING:2750	case MBX_RESET_RING:2751	case MBX_READ_CONFIG:2752	case MBX_READ_RCONFIG:2753	case MBX_READ_SPARM:2754	case MBX_READ_STATUS:2755	case MBX_READ_RPI:2756	case MBX_READ_XRI:2757	case MBX_READ_REV:2758	case MBX_READ_LNK_STAT:2759	case MBX_REG_LOGIN:2760	case MBX_UNREG_LOGIN:2761	case MBX_CLEAR_LA:2762	case MBX_DUMP_MEMORY:2763	case MBX_DUMP_CONTEXT:2764	case MBX_RUN_DIAGS:2765	case MBX_RESTART:2766	case MBX_UPDATE_CFG:2767	case MBX_DOWN_LOAD:2768	case MBX_DEL_LD_ENTRY:2769	case MBX_RUN_PROGRAM:2770	case MBX_SET_MASK:2771	case MBX_SET_VARIABLE:2772	case MBX_UNREG_D_ID:2773	case MBX_KILL_BOARD:2774	case MBX_CONFIG_FARP:2775	case MBX_BEACON:2776	case MBX_LOAD_AREA:2777	case MBX_RUN_BIU_DIAG64:2778	case MBX_CONFIG_PORT:2779	case MBX_READ_SPARM64:2780	case MBX_READ_RPI64:2781	case MBX_REG_LOGIN64:2782	case MBX_READ_TOPOLOGY:2783	case MBX_WRITE_WWN:2784	case MBX_SET_DEBUG:2785	case MBX_LOAD_EXP_ROM:2786	case MBX_ASYNCEVT_ENABLE:2787	case MBX_REG_VPI:2788	case MBX_UNREG_VPI:2789	case MBX_HEARTBEAT:2790	case MBX_PORT_CAPABILITIES:2791	case MBX_PORT_IOV_CONTROL:2792	case MBX_SLI4_CONFIG:2793	case MBX_SLI4_REQ_FTRS:2794	case MBX_REG_FCFI:2795	case MBX_UNREG_FCFI:2796	case MBX_REG_VFI:2797	case MBX_UNREG_VFI:2798	case MBX_INIT_VPI:2799	case MBX_INIT_VFI:2800	case MBX_RESUME_RPI:2801	case MBX_READ_EVENT_LOG_STATUS:2802	case MBX_READ_EVENT_LOG:2803	case MBX_SECURITY_MGMT:2804	case MBX_AUTH_PORT:2805	case MBX_ACCESS_VDATA:2806		ret = mbxCommand;2807		break;2808	default:2809		ret = MBX_SHUTDOWN;2810		break;2811	}2812	return ret;2813}2814 2815/**2816 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler2817 * @phba: Pointer to HBA context object.2818 * @pmboxq: Pointer to mailbox command.2819 *2820 * This is completion handler function for mailbox commands issued from2821 * lpfc_sli_issue_mbox_wait function. This function is called by the2822 * mailbox event handler function with no lock held. This function2823 * will wake up thread waiting on the wait queue pointed by context12824 * of the mailbox.2825 **/2826void2827lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)2828{2829	unsigned long drvr_flag;2830	struct completion *pmbox_done;2831 2832	/*2833	 * If pmbox_done is empty, the driver thread gave up waiting and2834	 * continued running.2835	 */2836	pmboxq->mbox_flag |= LPFC_MBX_WAKE;2837	spin_lock_irqsave(&phba->hbalock, drvr_flag);2838	pmbox_done = pmboxq->ctx_u.mbox_wait;2839	if (pmbox_done)2840		complete(pmbox_done);2841	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);2842	return;2843}2844 2845static void2846__lpfc_sli_rpi_release(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)2847{2848	unsigned long iflags;2849 2850	if (ndlp->nlp_flag & NLP_RELEASE_RPI) {2851		lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);2852		spin_lock_irqsave(&ndlp->lock, iflags);2853		ndlp->nlp_flag &= ~NLP_RELEASE_RPI;2854		ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;2855		spin_unlock_irqrestore(&ndlp->lock, iflags);2856	}2857	ndlp->nlp_flag &= ~NLP_UNREG_INP;2858}2859 2860void2861lpfc_sli_rpi_release(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)2862{2863	__lpfc_sli_rpi_release(vport, ndlp);2864}2865 2866/**2867 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler2868 * @phba: Pointer to HBA context object.2869 * @pmb: Pointer to mailbox object.2870 *2871 * This function is the default mailbox completion handler. It2872 * frees the memory resources associated with the completed mailbox2873 * command. If the completed command is a REG_LOGIN mailbox command,2874 * this function will issue a UREG_LOGIN to re-claim the RPI.2875 **/2876void2877lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)2878{2879	struct lpfc_vport  *vport = pmb->vport;2880	struct lpfc_dmabuf *mp;2881	struct lpfc_nodelist *ndlp;2882	struct Scsi_Host *shost;2883	uint16_t rpi, vpi;2884	int rc;2885 2886	/*2887	 * If a REG_LOGIN succeeded  after node is destroyed or node2888	 * is in re-discovery driver need to cleanup the RPI.2889	 */2890	if (!test_bit(FC_UNLOADING, &phba->pport->load_flag) &&2891	    pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&2892	    !pmb->u.mb.mbxStatus) {2893		mp = pmb->ctx_buf;2894		if (mp) {2895			pmb->ctx_buf = NULL;2896			lpfc_mbuf_free(phba, mp->virt, mp->phys);2897			kfree(mp);2898		}2899		rpi = pmb->u.mb.un.varWords[0];2900		vpi = pmb->u.mb.un.varRegLogin.vpi;2901		if (phba->sli_rev == LPFC_SLI_REV4)2902			vpi -= phba->sli4_hba.max_cfg_param.vpi_base;2903		lpfc_unreg_login(phba, vpi, rpi, pmb);2904		pmb->vport = vport;2905		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;2906		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);2907		if (rc != MBX_NOT_FINISHED)2908			return;2909	}2910 2911	if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&2912		!test_bit(FC_UNLOADING, &phba->pport->load_flag) &&2913		!pmb->u.mb.mbxStatus) {2914		shost = lpfc_shost_from_vport(vport);2915		spin_lock_irq(shost->host_lock);2916		vport->vpi_state |= LPFC_VPI_REGISTERED;2917		spin_unlock_irq(shost->host_lock);2918		clear_bit(FC_VPORT_NEEDS_REG_VPI, &vport->fc_flag);2919	}2920 2921	if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {2922		ndlp = pmb->ctx_ndlp;2923		lpfc_nlp_put(ndlp);2924	}2925 2926	if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {2927		ndlp = pmb->ctx_ndlp;2928 2929		/* Check to see if there are any deferred events to process */2930		if (ndlp) {2931			lpfc_printf_vlog(2932				vport,2933				KERN_INFO, LOG_MBOX | LOG_DISCOVERY,2934				"1438 UNREG cmpl deferred mbox x%x "2935				"on NPort x%x Data: x%x x%x x%px x%lx x%x\n",2936				ndlp->nlp_rpi, ndlp->nlp_DID,2937				ndlp->nlp_flag, ndlp->nlp_defer_did,2938				ndlp, vport->load_flag, kref_read(&ndlp->kref));2939 2940			if ((ndlp->nlp_flag & NLP_UNREG_INP) &&2941			    (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {2942				ndlp->nlp_flag &= ~NLP_UNREG_INP;2943				ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;2944				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);2945			} else {2946				__lpfc_sli_rpi_release(vport, ndlp);2947			}2948 2949			/* The unreg_login mailbox is complete and had a2950			 * reference that has to be released.  The PLOGI2951			 * got its own ref.2952			 */2953			lpfc_nlp_put(ndlp);2954			pmb->ctx_ndlp = NULL;2955		}2956	}2957 2958	/* This nlp_put pairs with lpfc_sli4_resume_rpi */2959	if (pmb->u.mb.mbxCommand == MBX_RESUME_RPI) {2960		ndlp = pmb->ctx_ndlp;2961		lpfc_nlp_put(ndlp);2962	}2963 2964	/* Check security permission status on INIT_LINK mailbox command */2965	if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&2966	    (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))2967		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,2968				"2860 SLI authentication is required "2969				"for INIT_LINK but has not done yet\n");2970 2971	if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)2972		lpfc_sli4_mbox_cmd_free(phba, pmb);2973	else2974		lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);2975}2976 /**2977 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler2978 * @phba: Pointer to HBA context object.2979 * @pmb: Pointer to mailbox object.2980 *2981 * This function is the unreg rpi mailbox completion handler. It2982 * frees the memory resources associated with the completed mailbox2983 * command. An additional reference is put on the ndlp to prevent2984 * lpfc_nlp_release from freeing the rpi bit in the bitmask before2985 * the unreg mailbox command completes, this routine puts the2986 * reference back.2987 *2988 **/2989void2990lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)2991{2992	struct lpfc_vport  *vport = pmb->vport;2993	struct lpfc_nodelist *ndlp;2994 2995	ndlp = pmb->ctx_ndlp;2996	if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {2997		if (phba->sli_rev == LPFC_SLI_REV4 &&2998		    (bf_get(lpfc_sli_intf_if_type,2999		     &phba->sli4_hba.sli_intf) >=3000		     LPFC_SLI_INTF_IF_TYPE_2)) {3001			if (ndlp) {3002				lpfc_printf_vlog(3003					 vport, KERN_INFO,3004					 LOG_MBOX | LOG_SLI | LOG_NODE,3005					 "0010 UNREG_LOGIN vpi:x%x "3006					 "rpi:%x DID:%x defer x%x flg x%x "3007					 "x%px\n",3008					 vport->vpi, ndlp->nlp_rpi,3009					 ndlp->nlp_DID, ndlp->nlp_defer_did,3010					 ndlp->nlp_flag,3011					 ndlp);3012				ndlp->nlp_flag &= ~NLP_LOGO_ACC;3013 3014				/* Check to see if there are any deferred3015				 * events to process3016				 */3017				if ((ndlp->nlp_flag & NLP_UNREG_INP) &&3018				    (ndlp->nlp_defer_did !=3019				    NLP_EVT_NOTHING_PENDING)) {3020					lpfc_printf_vlog(3021						vport, KERN_INFO,3022						LOG_MBOX | LOG_SLI | LOG_NODE,3023						"4111 UNREG cmpl deferred "3024						"clr x%x on "3025						"NPort x%x Data: x%x x%px\n",3026						ndlp->nlp_rpi, ndlp->nlp_DID,3027						ndlp->nlp_defer_did, ndlp);3028					ndlp->nlp_flag &= ~NLP_UNREG_INP;3029					ndlp->nlp_defer_did =3030						NLP_EVT_NOTHING_PENDING;3031					lpfc_issue_els_plogi(3032						vport, ndlp->nlp_DID, 0);3033				} else {3034					__lpfc_sli_rpi_release(vport, ndlp);3035				}3036				lpfc_nlp_put(ndlp);3037			}3038		}3039	}3040 3041	mempool_free(pmb, phba->mbox_mem_pool);3042}3043 3044/**3045 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware3046 * @phba: Pointer to HBA context object.3047 *3048 * This function is called with no lock held. This function processes all3049 * the completed mailbox commands and gives it to upper layers. The interrupt3050 * service routine processes mailbox completion interrupt and adds completed3051 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.3052 * Worker thread call lpfc_sli_handle_mb_event, which will return the3053 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This3054 * function returns the mailbox commands to the upper layer by calling the3055 * completion handler function of each mailbox.3056 **/3057int3058lpfc_sli_handle_mb_event(struct lpfc_hba *phba)3059{3060	MAILBOX_t *pmbox;3061	LPFC_MBOXQ_t *pmb;3062	int rc;3063	LIST_HEAD(cmplq);3064 3065	phba->sli.slistat.mbox_event++;3066 3067	/* Get all completed mailboxe buffers into the cmplq */3068	spin_lock_irq(&phba->hbalock);3069	list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);3070	spin_unlock_irq(&phba->hbalock);3071 3072	/* Get a Mailbox buffer to setup mailbox commands for callback */3073	do {3074		list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);3075		if (pmb == NULL)3076			break;3077 3078		pmbox = &pmb->u.mb;3079 3080		if (pmbox->mbxCommand != MBX_HEARTBEAT) {3081			if (pmb->vport) {3082				lpfc_debugfs_disc_trc(pmb->vport,3083					LPFC_DISC_TRC_MBOX_VPORT,3084					"MBOX cmpl vport: cmd:x%x mb:x%x x%x",3085					(uint32_t)pmbox->mbxCommand,3086					pmbox->un.varWords[0],3087					pmbox->un.varWords[1]);3088			}3089			else {3090				lpfc_debugfs_disc_trc(phba->pport,3091					LPFC_DISC_TRC_MBOX,3092					"MBOX cmpl:       cmd:x%x mb:x%x x%x",3093					(uint32_t)pmbox->mbxCommand,3094					pmbox->un.varWords[0],3095					pmbox->un.varWords[1]);3096			}3097		}3098 3099		/*3100		 * It is a fatal error if unknown mbox command completion.3101		 */3102		if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==3103		    MBX_SHUTDOWN) {3104			/* Unknown mailbox command compl */3105			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,3106					"(%d):0323 Unknown Mailbox command "3107					"x%x (x%x/x%x) Cmpl\n",3108					pmb->vport ? pmb->vport->vpi :3109					LPFC_VPORT_UNKNOWN,3110					pmbox->mbxCommand,3111					lpfc_sli_config_mbox_subsys_get(phba,3112									pmb),3113					lpfc_sli_config_mbox_opcode_get(phba,3114									pmb));3115			phba->link_state = LPFC_HBA_ERROR;3116			phba->work_hs = HS_FFER3;3117			lpfc_handle_eratt(phba);3118			continue;3119		}3120 3121		if (pmbox->mbxStatus) {3122			phba->sli.slistat.mbox_stat_err++;3123			if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {3124				/* Mbox cmd cmpl error - RETRYing */3125				lpfc_printf_log(phba, KERN_INFO,3126					LOG_MBOX | LOG_SLI,3127					"(%d):0305 Mbox cmd cmpl "3128					"error - RETRYing Data: x%x "3129					"(x%x/x%x) x%x x%x x%x\n",3130					pmb->vport ? pmb->vport->vpi :3131					LPFC_VPORT_UNKNOWN,3132					pmbox->mbxCommand,3133					lpfc_sli_config_mbox_subsys_get(phba,3134									pmb),3135					lpfc_sli_config_mbox_opcode_get(phba,3136									pmb),3137					pmbox->mbxStatus,3138					pmbox->un.varWords[0],3139					pmb->vport ? pmb->vport->port_state :3140					LPFC_VPORT_UNKNOWN);3141				pmbox->mbxStatus = 0;3142				pmbox->mbxOwner = OWN_HOST;3143				rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);3144				if (rc != MBX_NOT_FINISHED)3145					continue;3146			}3147		}3148 3149		/* Mailbox cmd <cmd> Cmpl <cmpl> */3150		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,3151				"(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "3152				"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "3153				"x%x x%x x%x\n",3154				pmb->vport ? pmb->vport->vpi : 0,3155				pmbox->mbxCommand,3156				lpfc_sli_config_mbox_subsys_get(phba, pmb),3157				lpfc_sli_config_mbox_opcode_get(phba, pmb),3158				pmb->mbox_cmpl,3159				*((uint32_t *) pmbox),3160				pmbox->un.varWords[0],3161				pmbox->un.varWords[1],3162				pmbox->un.varWords[2],3163				pmbox->un.varWords[3],3164				pmbox->un.varWords[4],3165				pmbox->un.varWords[5],3166				pmbox->un.varWords[6],3167				pmbox->un.varWords[7],3168				pmbox->un.varWords[8],3169				pmbox->un.varWords[9],3170				pmbox->un.varWords[10]);3171 3172		if (pmb->mbox_cmpl)3173			pmb->mbox_cmpl(phba,pmb);3174	} while (1);3175	return 0;3176}3177 3178/**3179 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag3180 * @phba: Pointer to HBA context object.3181 * @pring: Pointer to driver SLI ring object.3182 * @tag: buffer tag.3183 *3184 * This function is called with no lock held. When QUE_BUFTAG_BIT bit3185 * is set in the tag the buffer is posted for a particular exchange,3186 * the function will return the buffer without replacing the buffer.3187 * If the buffer is for unsolicited ELS or CT traffic, this function3188 * returns the buffer and also posts another buffer to the firmware.3189 **/3190static struct lpfc_dmabuf *3191lpfc_sli_get_buff(struct lpfc_hba *phba,3192		  struct lpfc_sli_ring *pring,3193		  uint32_t tag)3194{3195	struct hbq_dmabuf *hbq_entry;3196 3197	if (tag & QUE_BUFTAG_BIT)3198		return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);3199	hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);3200	if (!hbq_entry)3201		return NULL;3202	return &hbq_entry->dbuf;3203}3204 3205/**3206 * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer3207 *                              containing a NVME LS request.3208 * @phba: pointer to lpfc hba data structure.3209 * @piocb: pointer to the iocbq struct representing the sequence starting3210 *        frame.3211 *3212 * This routine initially validates the NVME LS, validates there is a login3213 * with the port that sent the LS, and then calls the appropriate nvme host3214 * or target LS request handler.3215 **/3216static void3217lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)3218{3219	struct lpfc_nodelist *ndlp;3220	struct lpfc_dmabuf *d_buf;3221	struct hbq_dmabuf *nvmebuf;3222	struct fc_frame_header *fc_hdr;3223	struct lpfc_async_xchg_ctx *axchg = NULL;3224	char *failwhy = NULL;3225	uint32_t oxid, sid, did, fctl, size;3226	int ret = 1;3227 3228	d_buf = piocb->cmd_dmabuf;3229 3230	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);3231	fc_hdr = nvmebuf->hbuf.virt;3232	oxid = be16_to_cpu(fc_hdr->fh_ox_id);3233	sid = sli4_sid_from_fc_hdr(fc_hdr);3234	did = sli4_did_from_fc_hdr(fc_hdr);3235	fctl = (fc_hdr->fh_f_ctl[0] << 16 |3236		fc_hdr->fh_f_ctl[1] << 8 |3237		fc_hdr->fh_f_ctl[2]);3238	size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);3239 3240	lpfc_nvmeio_data(phba, "NVME LS    RCV: xri x%x sz %d from %06x\n",3241			 oxid, size, sid);3242 3243	if (test_bit(FC_UNLOADING, &phba->pport->load_flag)) {3244		failwhy = "Driver Unloading";3245	} else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {3246		failwhy = "NVME FC4 Disabled";3247	} else if (!phba->nvmet_support && !phba->pport->localport) {3248		failwhy = "No Localport";3249	} else if (phba->nvmet_support && !phba->targetport) {3250		failwhy = "No Targetport";3251	} else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {3252		failwhy = "Bad NVME LS R_CTL";3253	} else if (unlikely((fctl & 0x00FF0000) !=3254			(FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {3255		failwhy = "Bad NVME LS F_CTL";3256	} else {3257		axchg = kzalloc(sizeof(*axchg), GFP_ATOMIC);3258		if (!axchg)3259			failwhy = "No CTX memory";3260	}3261 3262	if (unlikely(failwhy)) {3263		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,3264				"6154 Drop NVME LS: SID %06X OXID x%X: %s\n",3265				sid, oxid, failwhy);3266		goto out_fail;3267	}3268 3269	/* validate the source of the LS is logged in */3270	ndlp = lpfc_findnode_did(phba->pport, sid);3271	if (!ndlp ||3272	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&3273	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {3274		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,3275				"6216 NVME Unsol rcv: No ndlp: "3276				"NPort_ID x%x oxid x%x\n",3277				sid, oxid);3278		goto out_fail;3279	}3280 3281	axchg->phba = phba;3282	axchg->ndlp = ndlp;3283	axchg->size = size;3284	axchg->oxid = oxid;3285	axchg->sid = sid;3286	axchg->wqeq = NULL;3287	axchg->state = LPFC_NVME_STE_LS_RCV;3288	axchg->entry_cnt = 1;3289	axchg->rqb_buffer = (void *)nvmebuf;3290	axchg->hdwq = &phba->sli4_hba.hdwq[0];3291	axchg->payload = nvmebuf->dbuf.virt;3292	INIT_LIST_HEAD(&axchg->list);3293 3294	if (phba->nvmet_support) {3295		ret = lpfc_nvmet_handle_lsreq(phba, axchg);3296		spin_lock_irq(&ndlp->lock);3297		if (!ret && !(ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH)) {3298			ndlp->fc4_xpt_flags |= NLP_XPT_HAS_HH;3299			spin_unlock_irq(&ndlp->lock);3300 3301			/* This reference is a single occurrence to hold the3302			 * node valid until the nvmet transport calls3303			 * host_release.3304			 */3305			if (!lpfc_nlp_get(ndlp))3306				goto out_fail;3307 3308			lpfc_printf_log(phba, KERN_ERR, LOG_NODE,3309					"6206 NVMET unsol ls_req ndlp x%px "3310					"DID x%x xflags x%x refcnt %d\n",3311					ndlp, ndlp->nlp_DID,3312					ndlp->fc4_xpt_flags,3313					kref_read(&ndlp->kref));3314		} else {3315			spin_unlock_irq(&ndlp->lock);3316		}3317	} else {3318		ret = lpfc_nvme_handle_lsreq(phba, axchg);3319	}3320 3321	/* if zero, LS was successfully handled. If non-zero, LS not handled */3322	if (!ret)3323		return;3324 3325out_fail:3326	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,3327			"6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "3328			"NVMe%s handler failed %d\n",3329			did, sid, oxid,3330			(phba->nvmet_support) ? "T" : "I", ret);3331 3332	/* recycle receive buffer */3333	lpfc_in_buf_free(phba, &nvmebuf->dbuf);3334 3335	/* If start of new exchange, abort it */3336	if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))3337		ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);3338 3339	if (ret)3340		kfree(axchg);3341}3342 3343/**3344 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence3345 * @phba: Pointer to HBA context object.3346 * @pring: Pointer to driver SLI ring object.3347 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.3348 * @fch_r_ctl: the r_ctl for the first frame of the sequence.3349 * @fch_type: the type for the first frame of the sequence.3350 *3351 * This function is called with no lock held. This function uses the r_ctl and3352 * type of the received sequence to find the correct callback function to call3353 * to process the sequence.3354 **/3355static int3356lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,3357			 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,3358			 uint32_t fch_type)3359{3360	int i;3361 3362	switch (fch_type) {3363	case FC_TYPE_NVME:3364		lpfc_nvme_unsol_ls_handler(phba, saveq);3365		return 1;3366	default:3367		break;3368	}3369 3370	/* unSolicited Responses */3371	if (pring->prt[0].profile) {3372		if (pring->prt[0].lpfc_sli_rcv_unsol_event)3373			(pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,3374									saveq);3375		return 1;3376	}3377	/* We must search, based on rctl / type3378	   for the right routine */3379	for (i = 0; i < pring->num_mask; i++) {3380		if ((pring->prt[i].rctl == fch_r_ctl) &&3381		    (pring->prt[i].type == fch_type)) {3382			if (pring->prt[i].lpfc_sli_rcv_unsol_event)3383				(pring->prt[i].lpfc_sli_rcv_unsol_event)3384						(phba, pring, saveq);3385			return 1;3386		}3387	}3388	return 0;3389}3390 3391static void3392lpfc_sli_prep_unsol_wqe(struct lpfc_hba *phba,3393			struct lpfc_iocbq *saveq)3394{3395	IOCB_t *irsp;3396	union lpfc_wqe128 *wqe;3397	u16 i = 0;3398 3399	irsp = &saveq->iocb;3400	wqe = &saveq->wqe;3401 3402	/* Fill wcqe with the IOCB status fields */3403	bf_set(lpfc_wcqe_c_status, &saveq->wcqe_cmpl, irsp->ulpStatus);3404	saveq->wcqe_cmpl.word3 = irsp->ulpBdeCount;3405	saveq->wcqe_cmpl.parameter = irsp->un.ulpWord[4];3406	saveq->wcqe_cmpl.total_data_placed = irsp->unsli3.rcvsli3.acc_len;3407 3408	/* Source ID */3409	bf_set(els_rsp64_sid, &wqe->xmit_els_rsp, irsp->un.rcvels.parmRo);3410 3411	/* rx-id of the response frame */3412	bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com, irsp->ulpContext);3413 3414	/* ox-id of the frame */3415	bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,3416	       irsp->unsli3.rcvsli3.ox_id);3417 3418	/* DID */3419	bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,3420	       irsp->un.rcvels.remoteID);3421 3422	/* unsol data len */3423	for (i = 0; i < irsp->ulpBdeCount; i++) {3424		struct lpfc_hbq_entry *hbqe = NULL;3425 3426		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {3427			if (i == 0) {3428				hbqe = (struct lpfc_hbq_entry *)3429					&irsp->un.ulpWord[0];3430				saveq->wqe.gen_req.bde.tus.f.bdeSize =3431					hbqe->bde.tus.f.bdeSize;3432			} else if (i == 1) {3433				hbqe = (struct lpfc_hbq_entry *)3434					&irsp->unsli3.sli3Words[4];3435				saveq->unsol_rcv_len = hbqe->bde.tus.f.bdeSize;3436			}3437		}3438	}3439}3440 3441/**3442 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler3443 * @phba: Pointer to HBA context object.3444 * @pring: Pointer to driver SLI ring object.3445 * @saveq: Pointer to the unsolicited iocb.3446 *3447 * This function is called with no lock held by the ring event handler3448 * when there is an unsolicited iocb posted to the response ring by the3449 * firmware. This function gets the buffer associated with the iocbs3450 * and calls the event handler for the ring. This function handles both3451 * qring buffers and hbq buffers.3452 * When the function returns 1 the caller can free the iocb object otherwise3453 * upper layer functions will free the iocb objects.3454 **/3455static int3456lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,3457			    struct lpfc_iocbq *saveq)3458{3459	IOCB_t           * irsp;3460	WORD5            * w5p;3461	dma_addr_t	 paddr;3462	uint32_t           Rctl, Type;3463	struct lpfc_iocbq *iocbq;3464	struct lpfc_dmabuf *dmzbuf;3465 3466	irsp = &saveq->iocb;3467	saveq->vport = phba->pport;3468 3469	if (irsp->ulpCommand == CMD_ASYNC_STATUS) {3470		if (pring->lpfc_sli_rcv_async_status)3471			pring->lpfc_sli_rcv_async_status(phba, pring, saveq);3472		else3473			lpfc_printf_log(phba,3474					KERN_WARNING,3475					LOG_SLI,3476					"0316 Ring %d handler: unexpected "3477					"ASYNC_STATUS iocb received evt_code "3478					"0x%x\n",3479					pring->ringno,3480					irsp->un.asyncstat.evt_code);3481		return 1;3482	}3483 3484	if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&3485	    (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {3486		if (irsp->ulpBdeCount > 0) {3487			dmzbuf = lpfc_sli_get_buff(phba, pring,3488						   irsp->un.ulpWord[3]);3489			lpfc_in_buf_free(phba, dmzbuf);3490		}3491 3492		if (irsp->ulpBdeCount > 1) {3493			dmzbuf = lpfc_sli_get_buff(phba, pring,3494						   irsp->unsli3.sli3Words[3]);3495			lpfc_in_buf_free(phba, dmzbuf);3496		}3497 3498		if (irsp->ulpBdeCount > 2) {3499			dmzbuf = lpfc_sli_get_buff(phba, pring,3500						   irsp->unsli3.sli3Words[7]);3501			lpfc_in_buf_free(phba, dmzbuf);3502		}3503 3504		return 1;3505	}3506 3507	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {3508		if (irsp->ulpBdeCount != 0) {3509			saveq->cmd_dmabuf = lpfc_sli_get_buff(phba, pring,3510						irsp->un.ulpWord[3]);3511			if (!saveq->cmd_dmabuf)3512				lpfc_printf_log(phba,3513					KERN_ERR,3514					LOG_SLI,3515					"0341 Ring %d Cannot find buffer for "3516					"an unsolicited iocb. tag 0x%x\n",3517					pring->ringno,3518					irsp->un.ulpWord[3]);3519		}3520		if (irsp->ulpBdeCount == 2) {3521			saveq->bpl_dmabuf = lpfc_sli_get_buff(phba, pring,3522						irsp->unsli3.sli3Words[7]);3523			if (!saveq->bpl_dmabuf)3524				lpfc_printf_log(phba,3525					KERN_ERR,3526					LOG_SLI,3527					"0342 Ring %d Cannot find buffer for an"3528					" unsolicited iocb. tag 0x%x\n",3529					pring->ringno,3530					irsp->unsli3.sli3Words[7]);3531		}3532		list_for_each_entry(iocbq, &saveq->list, list) {3533			irsp = &iocbq->iocb;3534			if (irsp->ulpBdeCount != 0) {3535				iocbq->cmd_dmabuf = lpfc_sli_get_buff(phba,3536							pring,3537							irsp->un.ulpWord[3]);3538				if (!iocbq->cmd_dmabuf)3539					lpfc_printf_log(phba,3540						KERN_ERR,3541						LOG_SLI,3542						"0343 Ring %d Cannot find "3543						"buffer for an unsolicited iocb"3544						". tag 0x%x\n", pring->ringno,3545						irsp->un.ulpWord[3]);3546			}3547			if (irsp->ulpBdeCount == 2) {3548				iocbq->bpl_dmabuf = lpfc_sli_get_buff(phba,3549						pring,3550						irsp->unsli3.sli3Words[7]);3551				if (!iocbq->bpl_dmabuf)3552					lpfc_printf_log(phba,3553						KERN_ERR,3554						LOG_SLI,3555						"0344 Ring %d Cannot find "3556						"buffer for an unsolicited "3557						"iocb. tag 0x%x\n",3558						pring->ringno,3559						irsp->unsli3.sli3Words[7]);3560			}3561		}3562	} else {3563		paddr = getPaddr(irsp->un.cont64[0].addrHigh,3564				 irsp->un.cont64[0].addrLow);3565		saveq->cmd_dmabuf = lpfc_sli_ringpostbuf_get(phba, pring,3566							     paddr);3567		if (irsp->ulpBdeCount == 2) {3568			paddr = getPaddr(irsp->un.cont64[1].addrHigh,3569					 irsp->un.cont64[1].addrLow);3570			saveq->bpl_dmabuf = lpfc_sli_ringpostbuf_get(phba,3571								   pring,3572								   paddr);3573		}3574	}3575 3576	if (irsp->ulpBdeCount != 0 &&3577	    (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||3578	     irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {3579		int found = 0;3580 3581		/* search continue save q for same XRI */3582		list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {3583			if (iocbq->iocb.unsli3.rcvsli3.ox_id ==3584				saveq->iocb.unsli3.rcvsli3.ox_id) {3585				list_add_tail(&saveq->list, &iocbq->list);3586				found = 1;3587				break;3588			}3589		}3590		if (!found)3591			list_add_tail(&saveq->clist,3592				      &pring->iocb_continue_saveq);3593 3594		if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {3595			list_del_init(&iocbq->clist);3596			saveq = iocbq;3597			irsp = &saveq->iocb;3598		} else {3599			return 0;3600		}3601	}3602	if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||3603	    (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||3604	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {3605		Rctl = FC_RCTL_ELS_REQ;3606		Type = FC_TYPE_ELS;3607	} else {3608		w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);3609		Rctl = w5p->hcsw.Rctl;3610		Type = w5p->hcsw.Type;3611 3612		/* Firmware Workaround */3613		if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&3614			(irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||3615			 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {3616			Rctl = FC_RCTL_ELS_REQ;3617			Type = FC_TYPE_ELS;3618			w5p->hcsw.Rctl = Rctl;3619			w5p->hcsw.Type = Type;3620		}3621	}3622 3623	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&3624	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||3625	    irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {3626		if (irsp->unsli3.rcvsli3.vpi == 0xffff)3627			saveq->vport = phba->pport;3628		else3629			saveq->vport = lpfc_find_vport_by_vpid(phba,3630					       irsp->unsli3.rcvsli3.vpi);3631	}3632 3633	/* Prepare WQE with Unsol frame */3634	lpfc_sli_prep_unsol_wqe(phba, saveq);3635 3636	if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))3637		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,3638				"0313 Ring %d handler: unexpected Rctl x%x "3639				"Type x%x received\n",3640				pring->ringno, Rctl, Type);3641 3642	return 1;3643}3644 3645/**3646 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb3647 * @phba: Pointer to HBA context object.3648 * @pring: Pointer to driver SLI ring object.3649 * @prspiocb: Pointer to response iocb object.3650 *3651 * This function looks up the iocb_lookup table to get the command iocb3652 * corresponding to the given response iocb using the iotag of the3653 * response iocb. The driver calls this function with the hbalock held3654 * for SLI3 ports or the ring lock held for SLI4 ports.3655 * This function returns the command iocb object if it finds the command3656 * iocb else returns NULL.3657 **/3658static struct lpfc_iocbq *3659lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,3660		      struct lpfc_sli_ring *pring,3661		      struct lpfc_iocbq *prspiocb)3662{3663	struct lpfc_iocbq *cmd_iocb = NULL;3664	u16 iotag;3665 3666	if (phba->sli_rev == LPFC_SLI_REV4)3667		iotag = get_wqe_reqtag(prspiocb);3668	else3669		iotag = prspiocb->iocb.ulpIoTag;3670 3671	if (iotag != 0 && iotag <= phba->sli.last_iotag) {3672		cmd_iocb = phba->sli.iocbq_lookup[iotag];3673		if (cmd_iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ) {3674			/* remove from txcmpl queue list */3675			list_del_init(&cmd_iocb->list);3676			cmd_iocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;3677			pring->txcmplq_cnt--;3678			return cmd_iocb;3679		}3680	}3681 3682	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,3683			"0317 iotag x%x is out of "3684			"range: max iotag x%x\n",3685			iotag, phba->sli.last_iotag);3686	return NULL;3687}3688 3689/**3690 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag3691 * @phba: Pointer to HBA context object.3692 * @pring: Pointer to driver SLI ring object.3693 * @iotag: IOCB tag.3694 *3695 * This function looks up the iocb_lookup table to get the command iocb3696 * corresponding to the given iotag. The driver calls this function with3697 * the ring lock held because this function is an SLI4 port only helper.3698 * This function returns the command iocb object if it finds the command3699 * iocb else returns NULL.3700 **/3701static struct lpfc_iocbq *3702lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,3703			     struct lpfc_sli_ring *pring, uint16_t iotag)3704{3705	struct lpfc_iocbq *cmd_iocb = NULL;3706 3707	if (iotag != 0 && iotag <= phba->sli.last_iotag) {3708		cmd_iocb = phba->sli.iocbq_lookup[iotag];3709		if (cmd_iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ) {3710			/* remove from txcmpl queue list */3711			list_del_init(&cmd_iocb->list);3712			cmd_iocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;3713			pring->txcmplq_cnt--;3714			return cmd_iocb;3715		}3716	}3717 3718	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,3719			"0372 iotag x%x lookup error: max iotag (x%x) "3720			"cmd_flag x%x\n",3721			iotag, phba->sli.last_iotag,3722			cmd_iocb ? cmd_iocb->cmd_flag : 0xffff);3723	return NULL;3724}3725 3726/**3727 * lpfc_sli_process_sol_iocb - process solicited iocb completion3728 * @phba: Pointer to HBA context object.3729 * @pring: Pointer to driver SLI ring object.3730 * @saveq: Pointer to the response iocb to be processed.3731 *3732 * This function is called by the ring event handler for non-fcp3733 * rings when there is a new response iocb in the response ring.3734 * The caller is not required to hold any locks. This function3735 * gets the command iocb associated with the response iocb and3736 * calls the completion handler for the command iocb. If there3737 * is no completion handler, the function will free the resources3738 * associated with command iocb. If the response iocb is for3739 * an already aborted command iocb, the status of the completion3740 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.3741 * This function always returns 1.3742 **/3743static int3744lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,3745			  struct lpfc_iocbq *saveq)3746{3747	struct lpfc_iocbq *cmdiocbp;3748	unsigned long iflag;3749	u32 ulp_command, ulp_status, ulp_word4, ulp_context, iotag;3750 3751	if (phba->sli_rev == LPFC_SLI_REV4)3752		spin_lock_irqsave(&pring->ring_lock, iflag);3753	else3754		spin_lock_irqsave(&phba->hbalock, iflag);3755	cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);3756	if (phba->sli_rev == LPFC_SLI_REV4)3757		spin_unlock_irqrestore(&pring->ring_lock, iflag);3758	else3759		spin_unlock_irqrestore(&phba->hbalock, iflag);3760 3761	ulp_command = get_job_cmnd(phba, saveq);3762	ulp_status = get_job_ulpstatus(phba, saveq);3763	ulp_word4 = get_job_word4(phba, saveq);3764	ulp_context = get_job_ulpcontext(phba, saveq);3765	if (phba->sli_rev == LPFC_SLI_REV4)3766		iotag = get_wqe_reqtag(saveq);3767	else3768		iotag = saveq->iocb.ulpIoTag;3769 3770	if (cmdiocbp) {3771		ulp_command = get_job_cmnd(phba, cmdiocbp);3772		if (cmdiocbp->cmd_cmpl) {3773			/*3774			 * If an ELS command failed send an event to mgmt3775			 * application.3776			 */3777			if (ulp_status &&3778			     (pring->ringno == LPFC_ELS_RING) &&3779			     (ulp_command == CMD_ELS_REQUEST64_CR))3780				lpfc_send_els_failure_event(phba,3781					cmdiocbp, saveq);3782 3783			/*3784			 * Post all ELS completions to the worker thread.3785			 * All other are passed to the completion callback.3786			 */3787			if (pring->ringno == LPFC_ELS_RING) {3788				if ((phba->sli_rev < LPFC_SLI_REV4) &&3789				    (cmdiocbp->cmd_flag &3790							LPFC_DRIVER_ABORTED)) {3791					spin_lock_irqsave(&phba->hbalock,3792							  iflag);3793					cmdiocbp->cmd_flag &=3794						~LPFC_DRIVER_ABORTED;3795					spin_unlock_irqrestore(&phba->hbalock,3796							       iflag);3797					saveq->iocb.ulpStatus =3798						IOSTAT_LOCAL_REJECT;3799					saveq->iocb.un.ulpWord[4] =3800						IOERR_SLI_ABORTED;3801 3802					/* Firmware could still be in progress3803					 * of DMAing payload, so don't free data3804					 * buffer till after a hbeat.3805					 */3806					spin_lock_irqsave(&phba->hbalock,3807							  iflag);3808					saveq->cmd_flag |= LPFC_DELAY_MEM_FREE;3809					spin_unlock_irqrestore(&phba->hbalock,3810							       iflag);3811				}3812				if (phba->sli_rev == LPFC_SLI_REV4) {3813					if (saveq->cmd_flag &3814					    LPFC_EXCHANGE_BUSY) {3815						/* Set cmdiocb flag for the3816						 * exchange busy so sgl (xri)3817						 * will not be released until3818						 * the abort xri is received3819						 * from hba.3820						 */3821						spin_lock_irqsave(3822							&phba->hbalock, iflag);3823						cmdiocbp->cmd_flag |=3824							LPFC_EXCHANGE_BUSY;3825						spin_unlock_irqrestore(3826							&phba->hbalock, iflag);3827					}3828					if (cmdiocbp->cmd_flag &3829					    LPFC_DRIVER_ABORTED) {3830						/*3831						 * Clear LPFC_DRIVER_ABORTED3832						 * bit in case it was driver3833						 * initiated abort.3834						 */3835						spin_lock_irqsave(3836							&phba->hbalock, iflag);3837						cmdiocbp->cmd_flag &=3838							~LPFC_DRIVER_ABORTED;3839						spin_unlock_irqrestore(3840							&phba->hbalock, iflag);3841						set_job_ulpstatus(cmdiocbp,3842								  IOSTAT_LOCAL_REJECT);3843						set_job_ulpword4(cmdiocbp,3844								 IOERR_ABORT_REQUESTED);3845						/*3846						 * For SLI4, irspiocb contains3847						 * NO_XRI in sli_xritag, it3848						 * shall not affect releasing3849						 * sgl (xri) process.3850						 */3851						set_job_ulpstatus(saveq,3852								  IOSTAT_LOCAL_REJECT);3853						set_job_ulpword4(saveq,3854								 IOERR_SLI_ABORTED);3855						spin_lock_irqsave(3856							&phba->hbalock, iflag);3857						saveq->cmd_flag |=3858							LPFC_DELAY_MEM_FREE;3859						spin_unlock_irqrestore(3860							&phba->hbalock, iflag);3861					}3862				}3863			}3864			cmdiocbp->cmd_cmpl(phba, cmdiocbp, saveq);3865		} else3866			lpfc_sli_release_iocbq(phba, cmdiocbp);3867	} else {3868		/*3869		 * Unknown initiating command based on the response iotag.3870		 * This could be the case on the ELS ring because of3871		 * lpfc_els_abort().3872		 */3873		if (pring->ringno != LPFC_ELS_RING) {3874			/*3875			 * Ring <ringno> handler: unexpected completion IoTag3876			 * <IoTag>3877			 */3878			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,3879					 "0322 Ring %d handler: "3880					 "unexpected completion IoTag x%x "3881					 "Data: x%x x%x x%x x%x\n",3882					 pring->ringno, iotag, ulp_status,3883					 ulp_word4, ulp_command, ulp_context);3884		}3885	}3886 3887	return 1;3888}3889 3890/**3891 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler3892 * @phba: Pointer to HBA context object.3893 * @pring: Pointer to driver SLI ring object.3894 *3895 * This function is called from the iocb ring event handlers when3896 * put pointer is ahead of the get pointer for a ring. This function signal3897 * an error attention condition to the worker thread and the worker3898 * thread will transition the HBA to offline state.3899 **/3900static void3901lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)3902{3903	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];3904	/*3905	 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than3906	 * rsp ring <portRspMax>3907	 */3908	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,3909			"0312 Ring %d handler: portRspPut %d "3910			"is bigger than rsp ring %d\n",3911			pring->ringno, le32_to_cpu(pgp->rspPutInx),3912			pring->sli.sli3.numRiocb);3913 3914	phba->link_state = LPFC_HBA_ERROR;3915 3916	/*3917	 * All error attention handlers are posted to3918	 * worker thread3919	 */3920	phba->work_ha |= HA_ERATT;3921	phba->work_hs = HS_FFER3;3922 3923	lpfc_worker_wake_up(phba);3924 3925	return;3926}3927 3928/**3929 * lpfc_poll_eratt - Error attention polling timer timeout handler3930 * @t: Context to fetch pointer to address of HBA context object from.3931 *3932 * This function is invoked by the Error Attention polling timer when the3933 * timer times out. It will check the SLI Error Attention register for3934 * possible attention events. If so, it will post an Error Attention event3935 * and wake up worker thread to process it. Otherwise, it will set up the3936 * Error Attention polling timer for the next poll.3937 **/3938void lpfc_poll_eratt(struct timer_list *t)3939{3940	struct lpfc_hba *phba;3941	uint32_t eratt = 0;3942	uint64_t sli_intr, cnt;3943 3944	phba = from_timer(phba, t, eratt_poll);3945	if (!test_bit(HBA_SETUP, &phba->hba_flag))3946		return;3947 3948	if (test_bit(FC_UNLOADING, &phba->pport->load_flag))3949		return;3950 3951	/* Here we will also keep track of interrupts per sec of the hba */3952	sli_intr = phba->sli.slistat.sli_intr;3953 3954	if (phba->sli.slistat.sli_prev_intr > sli_intr)3955		cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +3956			sli_intr);3957	else3958		cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);3959 3960	/* 64-bit integer division not supported on 32-bit x86 - use do_div */3961	do_div(cnt, phba->eratt_poll_interval);3962	phba->sli.slistat.sli_ips = cnt;3963 3964	phba->sli.slistat.sli_prev_intr = sli_intr;3965 3966	/* Check chip HA register for error event */3967	eratt = lpfc_sli_check_eratt(phba);3968 3969	if (eratt)3970		/* Tell the worker thread there is work to do */3971		lpfc_worker_wake_up(phba);3972	else3973		/* Restart the timer for next eratt poll */3974		mod_timer(&phba->eratt_poll,3975			  jiffies +3976			  msecs_to_jiffies(1000 * phba->eratt_poll_interval));3977	return;3978}3979 3980 3981/**3982 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring3983 * @phba: Pointer to HBA context object.3984 * @pring: Pointer to driver SLI ring object.3985 * @mask: Host attention register mask for this ring.3986 *3987 * This function is called from the interrupt context when there is a ring3988 * event for the fcp ring. The caller does not hold any lock.3989 * The function processes each response iocb in the response ring until it3990 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with3991 * LE bit set. The function will call the completion handler of the command iocb3992 * if the response iocb indicates a completion for a command iocb or it is3993 * an abort completion. The function will call lpfc_sli_process_unsol_iocb3994 * function if this is an unsolicited iocb.3995 * This routine presumes LPFC_FCP_RING handling and doesn't bother3996 * to check it explicitly.3997 */3998int3999lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,4000				struct lpfc_sli_ring *pring, uint32_t mask)4001{4002	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];4003	IOCB_t *irsp = NULL;4004	IOCB_t *entry = NULL;4005	struct lpfc_iocbq *cmdiocbq = NULL;4006	struct lpfc_iocbq rspiocbq;4007	uint32_t status;4008	uint32_t portRspPut, portRspMax;4009	int rc = 1;4010	lpfc_iocb_type type;4011	unsigned long iflag;4012	uint32_t rsp_cmpl = 0;4013 4014	spin_lock_irqsave(&phba->hbalock, iflag);4015	pring->stats.iocb_event++;4016 4017	/*4018	 * The next available response entry should never exceed the maximum4019	 * entries.  If it does, treat it as an adapter hardware error.4020	 */4021	portRspMax = pring->sli.sli3.numRiocb;4022	portRspPut = le32_to_cpu(pgp->rspPutInx);4023	if (unlikely(portRspPut >= portRspMax)) {4024		lpfc_sli_rsp_pointers_error(phba, pring);4025		spin_unlock_irqrestore(&phba->hbalock, iflag);4026		return 1;4027	}4028	if (phba->fcp_ring_in_use) {4029		spin_unlock_irqrestore(&phba->hbalock, iflag);4030		return 1;4031	} else4032		phba->fcp_ring_in_use = 1;4033 4034	rmb();4035	while (pring->sli.sli3.rspidx != portRspPut) {4036		/*4037		 * Fetch an entry off the ring and copy it into a local data4038		 * structure.  The copy involves a byte-swap since the4039		 * network byte order and pci byte orders are different.4040		 */4041		entry = lpfc_resp_iocb(phba, pring);4042		phba->last_completion_time = jiffies;4043 4044		if (++pring->sli.sli3.rspidx >= portRspMax)4045			pring->sli.sli3.rspidx = 0;4046 4047		lpfc_sli_pcimem_bcopy((uint32_t *) entry,4048				      (uint32_t *) &rspiocbq.iocb,4049				      phba->iocb_rsp_size);4050		INIT_LIST_HEAD(&(rspiocbq.list));4051		irsp = &rspiocbq.iocb;4052 4053		type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);4054		pring->stats.iocb_rsp++;4055		rsp_cmpl++;4056 4057		if (unlikely(irsp->ulpStatus)) {4058			/*4059			 * If resource errors reported from HBA, reduce4060			 * queuedepths of the SCSI device.4061			 */4062			if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&4063			    ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==4064			     IOERR_NO_RESOURCES)) {4065				spin_unlock_irqrestore(&phba->hbalock, iflag);4066				phba->lpfc_rampdown_queue_depth(phba);4067				spin_lock_irqsave(&phba->hbalock, iflag);4068			}4069 4070			/* Rsp ring <ringno> error: IOCB */4071			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,4072					"0336 Rsp Ring %d error: IOCB Data: "4073					"x%x x%x x%x x%x x%x x%x x%x x%x\n",4074					pring->ringno,4075					irsp->un.ulpWord[0],4076					irsp->un.ulpWord[1],4077					irsp->un.ulpWord[2],4078					irsp->un.ulpWord[3],4079					irsp->un.ulpWord[4],4080					irsp->un.ulpWord[5],4081					*(uint32_t *)&irsp->un1,4082					*((uint32_t *)&irsp->un1 + 1));4083		}4084 4085		switch (type) {4086		case LPFC_ABORT_IOCB:4087		case LPFC_SOL_IOCB:4088			/*4089			 * Idle exchange closed via ABTS from port.  No iocb4090			 * resources need to be recovered.4091			 */4092			if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {4093				lpfc_printf_log(phba, KERN_INFO, LOG_SLI,4094						"0333 IOCB cmd 0x%x"4095						" processed. Skipping"4096						" completion\n",4097						irsp->ulpCommand);4098				break;4099			}4100 4101			cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,4102							 &rspiocbq);4103			if (unlikely(!cmdiocbq))4104				break;4105			if (cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED)4106				cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;4107			if (cmdiocbq->cmd_cmpl) {4108				spin_unlock_irqrestore(&phba->hbalock, iflag);4109				cmdiocbq->cmd_cmpl(phba, cmdiocbq, &rspiocbq);4110				spin_lock_irqsave(&phba->hbalock, iflag);4111			}4112			break;4113		case LPFC_UNSOL_IOCB:4114			spin_unlock_irqrestore(&phba->hbalock, iflag);4115			lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);4116			spin_lock_irqsave(&phba->hbalock, iflag);4117			break;4118		default:4119			if (irsp->ulpCommand == CMD_ADAPTER_MSG) {4120				char adaptermsg[LPFC_MAX_ADPTMSG];4121				memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);4122				memcpy(&adaptermsg[0], (uint8_t *) irsp,4123				       MAX_MSG_DATA);4124				dev_warn(&((phba->pcidev)->dev),4125					 "lpfc%d: %s\n",4126					 phba->brd_no, adaptermsg);4127			} else {4128				/* Unknown IOCB command */4129				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,4130						"0334 Unknown IOCB command "4131						"Data: x%x, x%x x%x x%x x%x\n",4132						type, irsp->ulpCommand,4133						irsp->ulpStatus,4134						irsp->ulpIoTag,4135						irsp->ulpContext);4136			}4137			break;4138		}4139 4140		/*4141		 * The response IOCB has been processed.  Update the ring4142		 * pointer in SLIM.  If the port response put pointer has not4143		 * been updated, sync the pgp->rspPutInx and fetch the new port4144		 * response put pointer.4145		 */4146		writel(pring->sli.sli3.rspidx,4147			&phba->host_gp[pring->ringno].rspGetInx);4148 4149		if (pring->sli.sli3.rspidx == portRspPut)4150			portRspPut = le32_to_cpu(pgp->rspPutInx);4151	}4152 4153	if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {4154		pring->stats.iocb_rsp_full++;4155		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));4156		writel(status, phba->CAregaddr);4157		readl(phba->CAregaddr);4158	}4159	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {4160		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;4161		pring->stats.iocb_cmd_empty++;4162 4163		/* Force update of the local copy of cmdGetInx */4164		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);4165		lpfc_sli_resume_iocb(phba, pring);4166 4167		if ((pring->lpfc_sli_cmd_available))4168			(pring->lpfc_sli_cmd_available) (phba, pring);4169 4170	}4171 4172	phba->fcp_ring_in_use = 0;4173	spin_unlock_irqrestore(&phba->hbalock, iflag);4174	return rc;4175}4176 4177/**4178 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb4179 * @phba: Pointer to HBA context object.4180 * @pring: Pointer to driver SLI ring object.4181 * @rspiocbp: Pointer to driver response IOCB object.4182 *4183 * This function is called from the worker thread when there is a slow-path4184 * response IOCB to process. This function chains all the response iocbs until4185 * seeing the iocb with the LE bit set. The function will call4186 * lpfc_sli_process_sol_iocb function if the response iocb indicates a4187 * completion of a command iocb. The function will call the4188 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.4189 * The function frees the resources or calls the completion handler if this4190 * iocb is an abort completion. The function returns NULL when the response4191 * iocb has the LE bit set and all the chained iocbs are processed, otherwise4192 * this function shall chain the iocb on to the iocb_continueq and return the4193 * response iocb passed in.4194 **/4195static struct lpfc_iocbq *4196lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,4197			struct lpfc_iocbq *rspiocbp)4198{4199	struct lpfc_iocbq *saveq;4200	struct lpfc_iocbq *cmdiocb;4201	struct lpfc_iocbq *next_iocb;4202	IOCB_t *irsp;4203	uint32_t free_saveq;4204	u8 cmd_type;4205	lpfc_iocb_type type;4206	unsigned long iflag;4207	u32 ulp_status = get_job_ulpstatus(phba, rspiocbp);4208	u32 ulp_word4 = get_job_word4(phba, rspiocbp);4209	u32 ulp_command = get_job_cmnd(phba, rspiocbp);4210	int rc;4211 4212	spin_lock_irqsave(&phba->hbalock, iflag);4213	/* First add the response iocb to the countinueq list */4214	list_add_tail(&rspiocbp->list, &pring->iocb_continueq);4215	pring->iocb_continueq_cnt++;4216 4217	/*4218	 * By default, the driver expects to free all resources4219	 * associated with this iocb completion.4220	 */4221	free_saveq = 1;4222	saveq = list_get_first(&pring->iocb_continueq,4223			       struct lpfc_iocbq, list);4224	list_del_init(&pring->iocb_continueq);4225	pring->iocb_continueq_cnt = 0;4226 4227	pring->stats.iocb_rsp++;4228 4229	/*4230	 * If resource errors reported from HBA, reduce4231	 * queuedepths of the SCSI device.4232	 */4233	if (ulp_status == IOSTAT_LOCAL_REJECT &&4234	    ((ulp_word4 & IOERR_PARAM_MASK) ==4235	     IOERR_NO_RESOURCES)) {4236		spin_unlock_irqrestore(&phba->hbalock, iflag);4237		phba->lpfc_rampdown_queue_depth(phba);4238		spin_lock_irqsave(&phba->hbalock, iflag);4239	}4240 4241	if (ulp_status) {4242		/* Rsp ring <ringno> error: IOCB */4243		if (phba->sli_rev < LPFC_SLI_REV4) {4244			irsp = &rspiocbp->iocb;4245			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,4246					"0328 Rsp Ring %d error: ulp_status x%x "4247					"IOCB Data: "4248					"x%08x x%08x x%08x x%08x "4249					"x%08x x%08x x%08x x%08x "4250					"x%08x x%08x x%08x x%08x "4251					"x%08x x%08x x%08x x%08x\n",4252					pring->ringno, ulp_status,4253					get_job_ulpword(rspiocbp, 0),4254					get_job_ulpword(rspiocbp, 1),4255					get_job_ulpword(rspiocbp, 2),4256					get_job_ulpword(rspiocbp, 3),4257					get_job_ulpword(rspiocbp, 4),4258					get_job_ulpword(rspiocbp, 5),4259					*(((uint32_t *)irsp) + 6),4260					*(((uint32_t *)irsp) + 7),4261					*(((uint32_t *)irsp) + 8),4262					*(((uint32_t *)irsp) + 9),4263					*(((uint32_t *)irsp) + 10),4264					*(((uint32_t *)irsp) + 11),4265					*(((uint32_t *)irsp) + 12),4266					*(((uint32_t *)irsp) + 13),4267					*(((uint32_t *)irsp) + 14),4268					*(((uint32_t *)irsp) + 15));4269		} else {4270			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,4271					"0321 Rsp Ring %d error: "4272					"IOCB Data: "4273					"x%x x%x x%x x%x\n",4274					pring->ringno,4275					rspiocbp->wcqe_cmpl.word0,4276					rspiocbp->wcqe_cmpl.total_data_placed,4277					rspiocbp->wcqe_cmpl.parameter,4278					rspiocbp->wcqe_cmpl.word3);4279		}4280	}4281 4282 4283	/*4284	 * Fetch the iocb command type and call the correct completion4285	 * routine. Solicited and Unsolicited IOCBs on the ELS ring4286	 * get freed back to the lpfc_iocb_list by the discovery4287	 * kernel thread.4288	 */4289	cmd_type = ulp_command & CMD_IOCB_MASK;4290	type = lpfc_sli_iocb_cmd_type(cmd_type);4291	switch (type) {4292	case LPFC_SOL_IOCB:4293		spin_unlock_irqrestore(&phba->hbalock, iflag);4294		rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);4295		spin_lock_irqsave(&phba->hbalock, iflag);4296		break;4297	case LPFC_UNSOL_IOCB:4298		spin_unlock_irqrestore(&phba->hbalock, iflag);4299		rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);4300		spin_lock_irqsave(&phba->hbalock, iflag);4301		if (!rc)4302			free_saveq = 0;4303		break;4304	case LPFC_ABORT_IOCB:4305		cmdiocb = NULL;4306		if (ulp_command != CMD_XRI_ABORTED_CX)4307			cmdiocb = lpfc_sli_iocbq_lookup(phba, pring,4308							saveq);4309		if (cmdiocb) {4310			/* Call the specified completion routine */4311			if (cmdiocb->cmd_cmpl) {4312				spin_unlock_irqrestore(&phba->hbalock, iflag);4313				cmdiocb->cmd_cmpl(phba, cmdiocb, saveq);4314				spin_lock_irqsave(&phba->hbalock, iflag);4315			} else {4316				__lpfc_sli_release_iocbq(phba, cmdiocb);4317			}4318		}4319		break;4320	case LPFC_UNKNOWN_IOCB:4321		if (ulp_command == CMD_ADAPTER_MSG) {4322			char adaptermsg[LPFC_MAX_ADPTMSG];4323 4324			memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);4325			memcpy(&adaptermsg[0], (uint8_t *)&rspiocbp->wqe,4326			       MAX_MSG_DATA);4327			dev_warn(&((phba->pcidev)->dev),4328				 "lpfc%d: %s\n",4329				 phba->brd_no, adaptermsg);4330		} else {4331			/* Unknown command */4332			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,4333					"0335 Unknown IOCB "4334					"command Data: x%x "4335					"x%x x%x x%x\n",4336					ulp_command,4337					ulp_status,4338					get_wqe_reqtag(rspiocbp),4339					get_job_ulpcontext(phba, rspiocbp));4340		}4341		break;4342	}4343 4344	if (free_saveq) {4345		list_for_each_entry_safe(rspiocbp, next_iocb,4346					 &saveq->list, list) {4347			list_del_init(&rspiocbp->list);4348			__lpfc_sli_release_iocbq(phba, rspiocbp);4349		}4350		__lpfc_sli_release_iocbq(phba, saveq);4351	}4352	rspiocbp = NULL;4353	spin_unlock_irqrestore(&phba->hbalock, iflag);4354	return rspiocbp;4355}4356 4357/**4358 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs4359 * @phba: Pointer to HBA context object.4360 * @pring: Pointer to driver SLI ring object.4361 * @mask: Host attention register mask for this ring.4362 *4363 * This routine wraps the actual slow_ring event process routine from the4364 * API jump table function pointer from the lpfc_hba struct.4365 **/4366void4367lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,4368				struct lpfc_sli_ring *pring, uint32_t mask)4369{4370	phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);4371}4372 4373/**4374 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings4375 * @phba: Pointer to HBA context object.4376 * @pring: Pointer to driver SLI ring object.4377 * @mask: Host attention register mask for this ring.4378 *4379 * This function is called from the worker thread when there is a ring event4380 * for non-fcp rings. The caller does not hold any lock. The function will4381 * remove each response iocb in the response ring and calls the handle4382 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.4383 **/4384static void4385lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,4386				   struct lpfc_sli_ring *pring, uint32_t mask)4387{4388	struct lpfc_pgp *pgp;4389	IOCB_t *entry;4390	IOCB_t *irsp = NULL;4391	struct lpfc_iocbq *rspiocbp = NULL;4392	uint32_t portRspPut, portRspMax;4393	unsigned long iflag;4394	uint32_t status;4395 4396	pgp = &phba->port_gp[pring->ringno];4397	spin_lock_irqsave(&phba->hbalock, iflag);4398	pring->stats.iocb_event++;4399 4400	/*4401	 * The next available response entry should never exceed the maximum4402	 * entries.  If it does, treat it as an adapter hardware error.4403	 */4404	portRspMax = pring->sli.sli3.numRiocb;4405	portRspPut = le32_to_cpu(pgp->rspPutInx);4406	if (portRspPut >= portRspMax) {4407		/*4408		 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than4409		 * rsp ring <portRspMax>4410		 */4411		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,4412				"0303 Ring %d handler: portRspPut %d "4413				"is bigger than rsp ring %d\n",4414				pring->ringno, portRspPut, portRspMax);4415 4416		phba->link_state = LPFC_HBA_ERROR;4417		spin_unlock_irqrestore(&phba->hbalock, iflag);4418 4419		phba->work_hs = HS_FFER3;4420		lpfc_handle_eratt(phba);4421 4422		return;4423	}4424 4425	rmb();4426	while (pring->sli.sli3.rspidx != portRspPut) {4427		/*4428		 * Build a completion list and call the appropriate handler.4429		 * The process is to get the next available response iocb, get4430		 * a free iocb from the list, copy the response data into the4431		 * free iocb, insert to the continuation list, and update the4432		 * next response index to slim.  This process makes response4433		 * iocb's in the ring available to DMA as fast as possible but4434		 * pays a penalty for a copy operation.  Since the iocb is4435		 * only 32 bytes, this penalty is considered small relative to4436		 * the PCI reads for register values and a slim write.  When4437		 * the ulpLe field is set, the entire Command has been4438		 * received.4439		 */4440		entry = lpfc_resp_iocb(phba, pring);4441 4442		phba->last_completion_time = jiffies;4443		rspiocbp = __lpfc_sli_get_iocbq(phba);4444		if (rspiocbp == NULL) {4445			printk(KERN_ERR "%s: out of buffers! Failing "4446			       "completion.\n", __func__);4447			break;4448		}4449 4450		lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,4451				      phba->iocb_rsp_size);4452		irsp = &rspiocbp->iocb;4453 4454		if (++pring->sli.sli3.rspidx >= portRspMax)4455			pring->sli.sli3.rspidx = 0;4456 4457		if (pring->ringno == LPFC_ELS_RING) {4458			lpfc_debugfs_slow_ring_trc(phba,4459			"IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",4460				*(((uint32_t *) irsp) + 4),4461				*(((uint32_t *) irsp) + 6),4462				*(((uint32_t *) irsp) + 7));4463		}4464 4465		writel(pring->sli.sli3.rspidx,4466			&phba->host_gp[pring->ringno].rspGetInx);4467 4468		spin_unlock_irqrestore(&phba->hbalock, iflag);4469		/* Handle the response IOCB */4470		rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);4471		spin_lock_irqsave(&phba->hbalock, iflag);4472 4473		/*4474		 * If the port response put pointer has not been updated, sync4475		 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port4476		 * response put pointer.4477		 */4478		if (pring->sli.sli3.rspidx == portRspPut) {4479			portRspPut = le32_to_cpu(pgp->rspPutInx);4480		}4481	} /* while (pring->sli.sli3.rspidx != portRspPut) */4482 4483	if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {4484		/* At least one response entry has been freed */4485		pring->stats.iocb_rsp_full++;4486		/* SET RxRE_RSP in Chip Att register */4487		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));4488		writel(status, phba->CAregaddr);4489		readl(phba->CAregaddr); /* flush */4490	}4491	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {4492		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;4493		pring->stats.iocb_cmd_empty++;4494 4495		/* Force update of the local copy of cmdGetInx */4496		pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);4497		lpfc_sli_resume_iocb(phba, pring);4498 4499		if ((pring->lpfc_sli_cmd_available))4500			(pring->lpfc_sli_cmd_available) (phba, pring);4501 4502	}4503 4504	spin_unlock_irqrestore(&phba->hbalock, iflag);4505	return;4506}4507 4508/**4509 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events4510 * @phba: Pointer to HBA context object.4511 * @pring: Pointer to driver SLI ring object.4512 * @mask: Host attention register mask for this ring.4513 *4514 * This function is called from the worker thread when there is a pending4515 * ELS response iocb on the driver internal slow-path response iocb worker4516 * queue. The caller does not hold any lock. The function will remove each4517 * response iocb from the response worker queue and calls the handle4518 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.4519 **/4520static void4521lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,4522				   struct lpfc_sli_ring *pring, uint32_t mask)4523{4524	struct lpfc_iocbq *irspiocbq;4525	struct hbq_dmabuf *dmabuf;4526	struct lpfc_cq_event *cq_event;4527	unsigned long iflag;4528	int count = 0;4529 4530	clear_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);4531	while (!list_empty(&phba->sli4_hba.sp_queue_event)) {4532		/* Get the response iocb from the head of work queue */4533		spin_lock_irqsave(&phba->hbalock, iflag);4534		list_remove_head(&phba->sli4_hba.sp_queue_event,4535				 cq_event, struct lpfc_cq_event, list);4536		spin_unlock_irqrestore(&phba->hbalock, iflag);4537 4538		switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {4539		case CQE_CODE_COMPL_WQE:4540			irspiocbq = container_of(cq_event, struct lpfc_iocbq,4541						 cq_event);4542			/* Translate ELS WCQE to response IOCBQ */4543			irspiocbq = lpfc_sli4_els_preprocess_rspiocbq(phba,4544								      irspiocbq);4545			if (irspiocbq)4546				lpfc_sli_sp_handle_rspiocb(phba, pring,4547							   irspiocbq);4548			count++;4549			break;4550		case CQE_CODE_RECEIVE:4551		case CQE_CODE_RECEIVE_V1:4552			dmabuf = container_of(cq_event, struct hbq_dmabuf,4553					      cq_event);4554			lpfc_sli4_handle_received_buffer(phba, dmabuf);4555			count++;4556			break;4557		default:4558			break;4559		}4560 4561		/* Limit the number of events to 64 to avoid soft lockups */4562		if (count == 64)4563			break;4564	}4565}4566 4567/**4568 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring4569 * @phba: Pointer to HBA context object.4570 * @pring: Pointer to driver SLI ring object.4571 *4572 * This function aborts all iocbs in the given ring and frees all the iocb4573 * objects in txq. This function issues an abort iocb for all the iocb commands4574 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before4575 * the return of this function. The caller is not required to hold any locks.4576 **/4577void4578lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)4579{4580	LIST_HEAD(tx_completions);4581	LIST_HEAD(txcmplq_completions);4582	struct lpfc_iocbq *iocb, *next_iocb;4583	int offline;4584 4585	if (pring->ringno == LPFC_ELS_RING) {4586		lpfc_fabric_abort_hba(phba);4587	}4588	offline = pci_channel_offline(phba->pcidev);4589 4590	/* Error everything on txq and txcmplq4591	 * First do the txq.4592	 */4593	if (phba->sli_rev >= LPFC_SLI_REV4) {4594		spin_lock_irq(&pring->ring_lock);4595		list_splice_init(&pring->txq, &tx_completions);4596		pring->txq_cnt = 0;4597 4598		if (offline) {4599			list_splice_init(&pring->txcmplq,4600					 &txcmplq_completions);4601		} else {4602			/* Next issue ABTS for everything on the txcmplq */4603			list_for_each_entry_safe(iocb, next_iocb,4604						 &pring->txcmplq, list)4605				lpfc_sli_issue_abort_iotag(phba, pring,4606							   iocb, NULL);4607		}4608		spin_unlock_irq(&pring->ring_lock);4609	} else {4610		spin_lock_irq(&phba->hbalock);4611		list_splice_init(&pring->txq, &tx_completions);4612		pring->txq_cnt = 0;4613 4614		if (offline) {4615			list_splice_init(&pring->txcmplq, &txcmplq_completions);4616		} else {4617			/* Next issue ABTS for everything on the txcmplq */4618			list_for_each_entry_safe(iocb, next_iocb,4619						 &pring->txcmplq, list)4620				lpfc_sli_issue_abort_iotag(phba, pring,4621							   iocb, NULL);4622		}4623		spin_unlock_irq(&phba->hbalock);4624	}4625 4626	if (offline) {4627		/* Cancel all the IOCBs from the completions list */4628		lpfc_sli_cancel_iocbs(phba, &txcmplq_completions,4629				      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);4630	} else {4631		/* Make sure HBA is alive */4632		lpfc_issue_hb_tmo(phba);4633	}4634	/* Cancel all the IOCBs from the completions list */4635	lpfc_sli_cancel_iocbs(phba, &tx_completions, IOSTAT_LOCAL_REJECT,4636			      IOERR_SLI_ABORTED);4637}4638 4639/**4640 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings4641 * @phba: Pointer to HBA context object.4642 *4643 * This function aborts all iocbs in FCP rings and frees all the iocb4644 * objects in txq. This function issues an abort iocb for all the iocb commands4645 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before4646 * the return of this function. The caller is not required to hold any locks.4647 **/4648void4649lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)4650{4651	struct lpfc_sli *psli = &phba->sli;4652	struct lpfc_sli_ring  *pring;4653	uint32_t i;4654 4655	/* Look on all the FCP Rings for the iotag */4656	if (phba->sli_rev >= LPFC_SLI_REV4) {4657		for (i = 0; i < phba->cfg_hdw_queue; i++) {4658			pring = phba->sli4_hba.hdwq[i].io_wq->pring;4659			lpfc_sli_abort_iocb_ring(phba, pring);4660		}4661	} else {4662		pring = &psli->sli3_ring[LPFC_FCP_RING];4663		lpfc_sli_abort_iocb_ring(phba, pring);4664	}4665}4666 4667/**4668 * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring4669 * @phba: Pointer to HBA context object.4670 *4671 * This function flushes all iocbs in the IO ring and frees all the iocb4672 * objects in txq and txcmplq. This function will not issue abort iocbs4673 * for all the iocb commands in txcmplq, they will just be returned with4674 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI4675 * slot has been permanently disabled.4676 **/4677void4678lpfc_sli_flush_io_rings(struct lpfc_hba *phba)4679{4680	LIST_HEAD(txq);4681	LIST_HEAD(txcmplq);4682	struct lpfc_sli *psli = &phba->sli;4683	struct lpfc_sli_ring  *pring;4684	uint32_t i;4685	struct lpfc_iocbq *piocb, *next_iocb;4686 4687	/* Indicate the I/O queues are flushed */4688	set_bit(HBA_IOQ_FLUSH, &phba->hba_flag);4689 4690	/* Look on all the FCP Rings for the iotag */4691	if (phba->sli_rev >= LPFC_SLI_REV4) {4692		for (i = 0; i < phba->cfg_hdw_queue; i++) {4693			if (!phba->sli4_hba.hdwq ||4694			    !phba->sli4_hba.hdwq[i].io_wq) {4695				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,4696						"7777 hdwq's deleted %lx "4697						"%lx %x %x\n",4698						phba->pport->load_flag,4699						phba->hba_flag,4700						phba->link_state,4701						phba->sli.sli_flag);4702				return;4703			}4704			pring = phba->sli4_hba.hdwq[i].io_wq->pring;4705 4706			spin_lock_irq(&pring->ring_lock);4707			/* Retrieve everything on txq */4708			list_splice_init(&pring->txq, &txq);4709			list_for_each_entry_safe(piocb, next_iocb,4710						 &pring->txcmplq, list)4711				piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;4712			/* Retrieve everything on the txcmplq */4713			list_splice_init(&pring->txcmplq, &txcmplq);4714			pring->txq_cnt = 0;4715			pring->txcmplq_cnt = 0;4716			spin_unlock_irq(&pring->ring_lock);4717 4718			/* Flush the txq */4719			lpfc_sli_cancel_iocbs(phba, &txq,4720					      IOSTAT_LOCAL_REJECT,4721					      IOERR_SLI_DOWN);4722			/* Flush the txcmplq */4723			lpfc_sli_cancel_iocbs(phba, &txcmplq,4724					      IOSTAT_LOCAL_REJECT,4725					      IOERR_SLI_DOWN);4726			if (unlikely(pci_channel_offline(phba->pcidev)))4727				lpfc_sli4_io_xri_aborted(phba, NULL, 0);4728		}4729	} else {4730		pring = &psli->sli3_ring[LPFC_FCP_RING];4731 4732		spin_lock_irq(&phba->hbalock);4733		/* Retrieve everything on txq */4734		list_splice_init(&pring->txq, &txq);4735		list_for_each_entry_safe(piocb, next_iocb,4736					 &pring->txcmplq, list)4737			piocb->cmd_flag &= ~LPFC_IO_ON_TXCMPLQ;4738		/* Retrieve everything on the txcmplq */4739		list_splice_init(&pring->txcmplq, &txcmplq);4740		pring->txq_cnt = 0;4741		pring->txcmplq_cnt = 0;4742		spin_unlock_irq(&phba->hbalock);4743 4744		/* Flush the txq */4745		lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,4746				      IOERR_SLI_DOWN);4747		/* Flush the txcmpq */4748		lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,4749				      IOERR_SLI_DOWN);4750	}4751}4752 4753/**4754 * lpfc_sli_brdready_s3 - Check for sli3 host ready status4755 * @phba: Pointer to HBA context object.4756 * @mask: Bit mask to be checked.4757 *4758 * This function reads the host status register and compares4759 * with the provided bit mask to check if HBA completed4760 * the restart. This function will wait in a loop for the4761 * HBA to complete restart. If the HBA does not restart within4762 * 15 iterations, the function will reset the HBA again. The4763 * function returns 1 when HBA fail to restart otherwise returns4764 * zero.4765 **/4766static int4767lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)4768{4769	uint32_t status;4770	int i = 0;4771	int retval = 0;4772 4773	/* Read the HBA Host Status Register */4774	if (lpfc_readl(phba->HSregaddr, &status))4775		return 1;4776 4777	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);4778 4779	/*4780	 * Check status register every 100ms for 5 retries, then every4781	 * 500ms for 5, then every 2.5 sec for 5, then reset board and4782	 * every 2.5 sec for 4.4783	 * Break our of the loop if errors occurred during init.4784	 */4785	while (((status & mask) != mask) &&4786	       !(status & HS_FFERM) &&4787	       i++ < 20) {4788 4789		if (i <= 5)4790			msleep(10);4791		else if (i <= 10)4792			msleep(500);4793		else4794			msleep(2500);4795 4796		if (i == 15) {4797				/* Do post */4798			phba->pport->port_state = LPFC_VPORT_UNKNOWN;4799			lpfc_sli_brdrestart(phba);4800		}4801		/* Read the HBA Host Status Register */4802		if (lpfc_readl(phba->HSregaddr, &status)) {4803			retval = 1;4804			break;4805		}4806	}4807 4808	/* Check to see if any errors occurred during init */4809	if ((status & HS_FFERM) || (i >= 20)) {4810		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,4811				"2751 Adapter failed to restart, "4812				"status reg x%x, FW Data: A8 x%x AC x%x\n",4813				status,4814				readl(phba->MBslimaddr + 0xa8),4815				readl(phba->MBslimaddr + 0xac));4816		phba->link_state = LPFC_HBA_ERROR;4817		retval = 1;4818	}4819 4820	return retval;4821}4822 4823/**4824 * lpfc_sli_brdready_s4 - Check for sli4 host ready status4825 * @phba: Pointer to HBA context object.4826 * @mask: Bit mask to be checked.4827 *4828 * This function checks the host status register to check if HBA is4829 * ready. This function will wait in a loop for the HBA to be ready4830 * If the HBA is not ready , the function will will reset the HBA PCI4831 * function again. The function returns 1 when HBA fail to be ready4832 * otherwise returns zero.4833 **/4834static int4835lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)4836{4837	uint32_t status;4838	int retval = 0;4839 4840	/* Read the HBA Host Status Register */4841	status = lpfc_sli4_post_status_check(phba);4842 4843	if (status) {4844		phba->pport->port_state = LPFC_VPORT_UNKNOWN;4845		lpfc_sli_brdrestart(phba);4846		status = lpfc_sli4_post_status_check(phba);4847	}4848 4849	/* Check to see if any errors occurred during init */4850	if (status) {4851		phba->link_state = LPFC_HBA_ERROR;4852		retval = 1;4853	} else4854		phba->sli4_hba.intr_enable = 0;4855 4856	clear_bit(HBA_SETUP, &phba->hba_flag);4857	return retval;4858}4859 4860/**4861 * lpfc_sli_brdready - Wrapper func for checking the hba readyness4862 * @phba: Pointer to HBA context object.4863 * @mask: Bit mask to be checked.4864 *4865 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine4866 * from the API jump table function pointer from the lpfc_hba struct.4867 **/4868int4869lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)4870{4871	return phba->lpfc_sli_brdready(phba, mask);4872}4873 4874#define BARRIER_TEST_PATTERN (0xdeadbeef)4875 4876/**4877 * lpfc_reset_barrier - Make HBA ready for HBA reset4878 * @phba: Pointer to HBA context object.4879 *4880 * This function is called before resetting an HBA. This function is called4881 * with hbalock held and requests HBA to quiesce DMAs before a reset.4882 **/4883void lpfc_reset_barrier(struct lpfc_hba *phba)4884{4885	uint32_t __iomem *resp_buf;4886	uint32_t __iomem *mbox_buf;4887	volatile struct MAILBOX_word0 mbox;4888	uint32_t hc_copy, ha_copy, resp_data;4889	int  i;4890	uint8_t hdrtype;4891 4892	lockdep_assert_held(&phba->hbalock);4893 4894	pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);4895	if (hdrtype != PCI_HEADER_TYPE_MFD ||4896	    (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&4897	     FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))4898		return;4899 4900	/*4901	 * Tell the other part of the chip to suspend temporarily all4902	 * its DMA activity.4903	 */4904	resp_buf = phba->MBslimaddr;4905 4906	/* Disable the error attention */4907	if (lpfc_readl(phba->HCregaddr, &hc_copy))4908		return;4909	writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);4910	readl(phba->HCregaddr); /* flush */4911	phba->link_flag |= LS_IGNORE_ERATT;4912 4913	if (lpfc_readl(phba->HAregaddr, &ha_copy))4914		return;4915	if (ha_copy & HA_ERATT) {4916		/* Clear Chip error bit */4917		writel(HA_ERATT, phba->HAregaddr);4918		phba->pport->stopped = 1;4919	}4920 4921	mbox.word0 = 0;4922	mbox.mbxCommand = MBX_KILL_BOARD;4923	mbox.mbxOwner = OWN_CHIP;4924 4925	writel(BARRIER_TEST_PATTERN, (resp_buf + 1));4926	mbox_buf = phba->MBslimaddr;4927	writel(mbox.word0, mbox_buf);4928 4929	for (i = 0; i < 50; i++) {4930		if (lpfc_readl((resp_buf + 1), &resp_data))4931			return;4932		if (resp_data != ~(BARRIER_TEST_PATTERN))4933			mdelay(1);4934		else4935			break;4936	}4937	resp_data = 0;4938	if (lpfc_readl((resp_buf + 1), &resp_data))4939		return;4940	if (resp_data  != ~(BARRIER_TEST_PATTERN)) {4941		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||4942		    phba->pport->stopped)4943			goto restore_hc;4944		else4945			goto clear_errat;4946	}4947 4948	mbox.mbxOwner = OWN_HOST;4949	resp_data = 0;4950	for (i = 0; i < 500; i++) {4951		if (lpfc_readl(resp_buf, &resp_data))4952			return;4953		if (resp_data != mbox.word0)4954			mdelay(1);4955		else4956			break;4957	}4958 4959clear_errat:4960 4961	while (++i < 500) {4962		if (lpfc_readl(phba->HAregaddr, &ha_copy))4963			return;4964		if (!(ha_copy & HA_ERATT))4965			mdelay(1);4966		else4967			break;4968	}4969 4970	if (readl(phba->HAregaddr) & HA_ERATT) {4971		writel(HA_ERATT, phba->HAregaddr);4972		phba->pport->stopped = 1;4973	}4974 4975restore_hc:4976	phba->link_flag &= ~LS_IGNORE_ERATT;4977	writel(hc_copy, phba->HCregaddr);4978	readl(phba->HCregaddr); /* flush */4979}4980 4981/**4982 * lpfc_sli_brdkill - Issue a kill_board mailbox command4983 * @phba: Pointer to HBA context object.4984 *4985 * This function issues a kill_board mailbox command and waits for4986 * the error attention interrupt. This function is called for stopping4987 * the firmware processing. The caller is not required to hold any4988 * locks. This function calls lpfc_hba_down_post function to free4989 * any pending commands after the kill. The function will return 1 when it4990 * fails to kill the board else will return 0.4991 **/4992int4993lpfc_sli_brdkill(struct lpfc_hba *phba)4994{4995	struct lpfc_sli *psli;4996	LPFC_MBOXQ_t *pmb;4997	uint32_t status;4998	uint32_t ha_copy;4999	int retval;5000	int i = 0;5001 5002	psli = &phba->sli;5003 5004	/* Kill HBA */5005	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,5006			"0329 Kill HBA Data: x%x x%x\n",5007			phba->pport->port_state, psli->sli_flag);5008 5009	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);5010	if (!pmb)5011		return 1;5012 5013	/* Disable the error attention */5014	spin_lock_irq(&phba->hbalock);5015	if (lpfc_readl(phba->HCregaddr, &status)) {5016		spin_unlock_irq(&phba->hbalock);5017		mempool_free(pmb, phba->mbox_mem_pool);5018		return 1;5019	}5020	status &= ~HC_ERINT_ENA;5021	writel(status, phba->HCregaddr);5022	readl(phba->HCregaddr); /* flush */5023	phba->link_flag |= LS_IGNORE_ERATT;5024	spin_unlock_irq(&phba->hbalock);5025 5026	lpfc_kill_board(phba, pmb);5027	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;5028	retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);5029 5030	if (retval != MBX_SUCCESS) {5031		if (retval != MBX_BUSY)5032			mempool_free(pmb, phba->mbox_mem_pool);5033		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,5034				"2752 KILL_BOARD command failed retval %d\n",5035				retval);5036		spin_lock_irq(&phba->hbalock);5037		phba->link_flag &= ~LS_IGNORE_ERATT;5038		spin_unlock_irq(&phba->hbalock);5039		return 1;5040	}5041 5042	spin_lock_irq(&phba->hbalock);5043	psli->sli_flag &= ~LPFC_SLI_ACTIVE;5044	spin_unlock_irq(&phba->hbalock);5045 5046	mempool_free(pmb, phba->mbox_mem_pool);5047 5048	/* There is no completion for a KILL_BOARD mbox cmd. Check for an error5049	 * attention every 100ms for 3 seconds. If we don't get ERATT after5050	 * 3 seconds we still set HBA_ERROR state because the status of the5051	 * board is now undefined.5052	 */5053	if (lpfc_readl(phba->HAregaddr, &ha_copy))5054		return 1;5055	while ((i++ < 30) && !(ha_copy & HA_ERATT)) {5056		mdelay(100);5057		if (lpfc_readl(phba->HAregaddr, &ha_copy))5058			return 1;5059	}5060 5061	del_timer_sync(&psli->mbox_tmo);5062	if (ha_copy & HA_ERATT) {5063		writel(HA_ERATT, phba->HAregaddr);5064		phba->pport->stopped = 1;5065	}5066	spin_lock_irq(&phba->hbalock);5067	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;5068	psli->mbox_active = NULL;5069	phba->link_flag &= ~LS_IGNORE_ERATT;5070	spin_unlock_irq(&phba->hbalock);5071 5072	lpfc_hba_down_post(phba);5073	phba->link_state = LPFC_HBA_ERROR;5074 5075	return ha_copy & HA_ERATT ? 0 : 1;5076}5077 5078/**5079 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA5080 * @phba: Pointer to HBA context object.5081 *5082 * This function resets the HBA by writing HC_INITFF to the control5083 * register. After the HBA resets, this function resets all the iocb ring5084 * indices. This function disables PCI layer parity checking during5085 * the reset.5086 * This function returns 0 always.5087 * The caller is not required to hold any locks.5088 **/5089int5090lpfc_sli_brdreset(struct lpfc_hba *phba)5091{5092	struct lpfc_sli *psli;5093	struct lpfc_sli_ring *pring;5094	uint16_t cfg_value;5095	int i;5096 5097	psli = &phba->sli;5098 5099	/* Reset HBA */5100	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,5101			"0325 Reset HBA Data: x%x x%x\n",5102			(phba->pport) ? phba->pport->port_state : 0,5103			psli->sli_flag);5104 5105	/* perform board reset */5106	phba->fc_eventTag = 0;5107	phba->link_events = 0;5108	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);5109	if (phba->pport) {5110		phba->pport->fc_myDID = 0;5111		phba->pport->fc_prevDID = 0;5112	}5113 5114	/* Turn off parity checking and serr during the physical reset */5115	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))5116		return -EIO;5117 5118	pci_write_config_word(phba->pcidev, PCI_COMMAND,5119			      (cfg_value &5120			       ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));5121 5122	psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);5123 5124	/* Now toggle INITFF bit in the Host Control Register */5125	writel(HC_INITFF, phba->HCregaddr);5126	mdelay(1);5127	readl(phba->HCregaddr); /* flush */5128	writel(0, phba->HCregaddr);5129	readl(phba->HCregaddr); /* flush */5130 5131	/* Restore PCI cmd register */5132	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);5133 5134	/* Initialize relevant SLI info */5135	for (i = 0; i < psli->num_rings; i++) {5136		pring = &psli->sli3_ring[i];5137		pring->flag = 0;5138		pring->sli.sli3.rspidx = 0;5139		pring->sli.sli3.next_cmdidx  = 0;5140		pring->sli.sli3.local_getidx = 0;5141		pring->sli.sli3.cmdidx = 0;5142		pring->missbufcnt = 0;5143	}5144 5145	phba->link_state = LPFC_WARM_START;5146	return 0;5147}5148 5149/**5150 * lpfc_sli4_brdreset - Reset a sli-4 HBA5151 * @phba: Pointer to HBA context object.5152 *5153 * This function resets a SLI4 HBA. This function disables PCI layer parity5154 * checking during resets the device. The caller is not required to hold5155 * any locks.5156 *5157 * This function returns 0 on success else returns negative error code.5158 **/5159int5160lpfc_sli4_brdreset(struct lpfc_hba *phba)5161{5162	struct lpfc_sli *psli = &phba->sli;5163	uint16_t cfg_value;5164	int rc = 0;5165 5166	/* Reset HBA */5167	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,5168			"0295 Reset HBA Data: x%x x%x x%lx\n",5169			phba->pport->port_state, psli->sli_flag,5170			phba->hba_flag);5171 5172	/* perform board reset */5173	phba->fc_eventTag = 0;5174	phba->link_events = 0;5175	phba->pport->fc_myDID = 0;5176	phba->pport->fc_prevDID = 0;5177	clear_bit(HBA_SETUP, &phba->hba_flag);5178 5179	spin_lock_irq(&phba->hbalock);5180	psli->sli_flag &= ~(LPFC_PROCESS_LA);5181	phba->fcf.fcf_flag = 0;5182	spin_unlock_irq(&phba->hbalock);5183 5184	/* Now physically reset the device */5185	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,5186			"0389 Performing PCI function reset!\n");5187 5188	/* Turn off parity checking and serr during the physical reset */5189	if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {5190		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,5191				"3205 PCI read Config failed\n");5192		return -EIO;5193	}5194 5195	pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &5196			      ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));5197 5198	/* Perform FCoE PCI function reset before freeing queue memory */5199	rc = lpfc_pci_function_reset(phba);5200 5201	/* Restore PCI cmd register */5202	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);5203 5204	return rc;5205}5206 5207/**5208 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba5209 * @phba: Pointer to HBA context object.5210 *5211 * This function is called in the SLI initialization code path to5212 * restart the HBA. The caller is not required to hold any lock.5213 * This function writes MBX_RESTART mailbox command to the SLIM and5214 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post5215 * function to free any pending commands. The function enables5216 * POST only during the first initialization. The function returns zero.5217 * The function does not guarantee completion of MBX_RESTART mailbox5218 * command before the return of this function.5219 **/5220static int5221lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)5222{5223	volatile struct MAILBOX_word0 mb;5224	struct lpfc_sli *psli;5225	void __iomem *to_slim;5226 5227	spin_lock_irq(&phba->hbalock);5228 5229	psli = &phba->sli;5230 5231	/* Restart HBA */5232	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,5233			"0337 Restart HBA Data: x%x x%x\n",5234			(phba->pport) ? phba->pport->port_state : 0,5235			psli->sli_flag);5236 5237	mb.word0 = 0;5238	mb.mbxCommand = MBX_RESTART;5239	mb.mbxHc = 1;5240 5241	lpfc_reset_barrier(phba);5242 5243	to_slim = phba->MBslimaddr;5244	writel(mb.word0, to_slim);5245	readl(to_slim); /* flush */5246 5247	/* Only skip post after fc_ffinit is completed */5248	if (phba->pport && phba->pport->port_state)5249		mb.word0 = 1;	/* This is really setting up word1 */5250	else5251		mb.word0 = 0;	/* This is really setting up word1 */5252	to_slim = phba->MBslimaddr + sizeof (uint32_t);5253	writel(mb.word0, to_slim);5254	readl(to_slim); /* flush */5255 5256	lpfc_sli_brdreset(phba);5257	if (phba->pport)5258		phba->pport->stopped = 0;5259	phba->link_state = LPFC_INIT_START;5260	phba->hba_flag = 0;5261	spin_unlock_irq(&phba->hbalock);5262 5263	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));5264	psli->stats_start = ktime_get_seconds();5265 5266	/* Give the INITFF and Post time to settle. */5267	mdelay(100);5268 5269	lpfc_hba_down_post(phba);5270 5271	return 0;5272}5273 5274/**5275 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba5276 * @phba: Pointer to HBA context object.5277 *5278 * This function is called in the SLI initialization code path to restart5279 * a SLI4 HBA. The caller is not required to hold any lock.5280 * At the end of the function, it calls lpfc_hba_down_post function to5281 * free any pending commands.5282 **/5283static int5284lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)5285{5286	struct lpfc_sli *psli = &phba->sli;5287	int rc;5288 5289	/* Restart HBA */5290	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,5291			"0296 Restart HBA Data: x%x x%x\n",5292			phba->pport->port_state, psli->sli_flag);5293 5294	rc = lpfc_sli4_brdreset(phba);5295	if (rc) {5296		phba->link_state = LPFC_HBA_ERROR;5297		goto hba_down_queue;5298	}5299 5300	spin_lock_irq(&phba->hbalock);5301	phba->pport->stopped = 0;5302	phba->link_state = LPFC_INIT_START;5303	phba->hba_flag = 0;5304	/* Preserve FA-PWWN expectation */5305	phba->sli4_hba.fawwpn_flag &= LPFC_FAWWPN_FABRIC;5306	spin_unlock_irq(&phba->hbalock);5307 5308	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));5309	psli->stats_start = ktime_get_seconds();5310 5311hba_down_queue:5312	lpfc_hba_down_post(phba);5313	lpfc_sli4_queue_destroy(phba);5314 5315	return rc;5316}5317 5318/**5319 * lpfc_sli_brdrestart - Wrapper func for restarting hba5320 * @phba: Pointer to HBA context object.5321 *5322 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the5323 * API jump table function pointer from the lpfc_hba struct.5324**/5325int5326lpfc_sli_brdrestart(struct lpfc_hba *phba)5327{5328	return phba->lpfc_sli_brdrestart(phba);5329}5330 5331/**5332 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart5333 * @phba: Pointer to HBA context object.5334 *5335 * This function is called after a HBA restart to wait for successful5336 * restart of the HBA. Successful restart of the HBA is indicated by5337 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 155338 * iteration, the function will restart the HBA again. The function returns5339 * zero if HBA successfully restarted else returns negative error code.5340 **/5341int5342lpfc_sli_chipset_init(struct lpfc_hba *phba)5343{5344	uint32_t status, i = 0;5345 5346	/* Read the HBA Host Status Register */5347	if (lpfc_readl(phba->HSregaddr, &status))5348		return -EIO;5349 5350	/* Check status register to see what current state is */5351	i = 0;5352	while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {5353 5354		/* Check every 10ms for 10 retries, then every 100ms for 905355		 * retries, then every 1 sec for 50 retires for a total of5356		 * ~60 seconds before reset the board again and check every5357		 * 1 sec for 50 retries. The up to 60 seconds before the5358		 * board ready is required by the Falcon FIPS zeroization5359		 * complete, and any reset the board in between shall cause5360		 * restart of zeroization, further delay the board ready.5361		 */5362		if (i++ >= 200) {5363			/* Adapter failed to init, timeout, status reg5364			   <status> */5365			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,5366					"0436 Adapter failed to init, "5367					"timeout, status reg x%x, "5368					"FW Data: A8 x%x AC x%x\n", status,5369					readl(phba->MBslimaddr + 0xa8),5370					readl(phba->MBslimaddr + 0xac));5371			phba->link_state = LPFC_HBA_ERROR;5372			return -ETIMEDOUT;5373		}5374 5375		/* Check to see if any errors occurred during init */5376		if (status & HS_FFERM) {5377			/* ERROR: During chipset initialization */5378			/* Adapter failed to init, chipset, status reg5379			   <status> */5380			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,5381					"0437 Adapter failed to init, "5382					"chipset, status reg x%x, "5383					"FW Data: A8 x%x AC x%x\n", status,5384					readl(phba->MBslimaddr + 0xa8),5385					readl(phba->MBslimaddr + 0xac));5386			phba->link_state = LPFC_HBA_ERROR;5387			return -EIO;5388		}5389 5390		if (i <= 10)5391			msleep(10);5392		else if (i <= 100)5393			msleep(100);5394		else5395			msleep(1000);5396 5397		if (i == 150) {5398			/* Do post */5399			phba->pport->port_state = LPFC_VPORT_UNKNOWN;5400			lpfc_sli_brdrestart(phba);5401		}5402		/* Read the HBA Host Status Register */5403		if (lpfc_readl(phba->HSregaddr, &status))5404			return -EIO;5405	}5406 5407	/* Check to see if any errors occurred during init */5408	if (status & HS_FFERM) {5409		/* ERROR: During chipset initialization */5410		/* Adapter failed to init, chipset, status reg <status> */5411		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,5412				"0438 Adapter failed to init, chipset, "5413				"status reg x%x, "5414				"FW Data: A8 x%x AC x%x\n", status,5415				readl(phba->MBslimaddr + 0xa8),5416				readl(phba->MBslimaddr + 0xac));5417		phba->link_state = LPFC_HBA_ERROR;5418		return -EIO;5419	}5420 5421	set_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);5422 5423	/* Clear all interrupt enable conditions */5424	writel(0, phba->HCregaddr);5425	readl(phba->HCregaddr); /* flush */5426 5427	/* setup host attn register */5428	writel(0xffffffff, phba->HAregaddr);5429	readl(phba->HAregaddr); /* flush */5430	return 0;5431}5432 5433/**5434 * lpfc_sli_hbq_count - Get the number of HBQs to be configured5435 *5436 * This function calculates and returns the number of HBQs required to be5437 * configured.5438 **/5439int5440lpfc_sli_hbq_count(void)5441{5442	return ARRAY_SIZE(lpfc_hbq_defs);5443}5444 5445/**5446 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries5447 *5448 * This function adds the number of hbq entries in every HBQ to get5449 * the total number of hbq entries required for the HBA and returns5450 * the total count.5451 **/5452static int5453lpfc_sli_hbq_entry_count(void)5454{5455	int  hbq_count = lpfc_sli_hbq_count();5456	int  count = 0;5457	int  i;5458 5459	for (i = 0; i < hbq_count; ++i)5460		count += lpfc_hbq_defs[i]->entry_count;5461	return count;5462}5463 5464/**5465 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries5466 *5467 * This function calculates amount of memory required for all hbq entries5468 * to be configured and returns the total memory required.5469 **/5470int5471lpfc_sli_hbq_size(void)5472{5473	return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);5474}5475 5476/**5477 * lpfc_sli_hbq_setup - configure and initialize HBQs5478 * @phba: Pointer to HBA context object.5479 *5480 * This function is called during the SLI initialization to configure5481 * all the HBQs and post buffers to the HBQ. The caller is not5482 * required to hold any locks. This function will return zero if successful5483 * else it will return negative error code.5484 **/5485static int5486lpfc_sli_hbq_setup(struct lpfc_hba *phba)5487{5488	int  hbq_count = lpfc_sli_hbq_count();5489	LPFC_MBOXQ_t *pmb;5490	MAILBOX_t *pmbox;5491	uint32_t hbqno;5492	uint32_t hbq_entry_index;5493 5494				/* Get a Mailbox buffer to setup mailbox5495				 * commands for HBA initialization5496				 */5497	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);5498 5499	if (!pmb)5500		return -ENOMEM;5501 5502	pmbox = &pmb->u.mb;5503 5504	/* Initialize the struct lpfc_sli_hbq structure for each hbq */5505	phba->link_state = LPFC_INIT_MBX_CMDS;5506	phba->hbq_in_use = 1;5507 5508	hbq_entry_index = 0;5509	for (hbqno = 0; hbqno < hbq_count; ++hbqno) {5510		phba->hbqs[hbqno].next_hbqPutIdx = 0;5511		phba->hbqs[hbqno].hbqPutIdx      = 0;5512		phba->hbqs[hbqno].local_hbqGetIdx   = 0;5513		phba->hbqs[hbqno].entry_count =5514			lpfc_hbq_defs[hbqno]->entry_count;5515		lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],5516			hbq_entry_index, pmb);5517		hbq_entry_index += phba->hbqs[hbqno].entry_count;5518 5519		if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {5520			/* Adapter failed to init, mbxCmd <cmd> CFG_RING,5521			   mbxStatus <status>, ring <num> */5522 5523			lpfc_printf_log(phba, KERN_ERR,5524					LOG_SLI | LOG_VPORT,5525					"1805 Adapter failed to init. "5526					"Data: x%x x%x x%x\n",5527					pmbox->mbxCommand,5528					pmbox->mbxStatus, hbqno);5529 5530			phba->link_state = LPFC_HBA_ERROR;5531			mempool_free(pmb, phba->mbox_mem_pool);5532			return -ENXIO;5533		}5534	}5535	phba->hbq_count = hbq_count;5536 5537	mempool_free(pmb, phba->mbox_mem_pool);5538 5539	/* Initially populate or replenish the HBQs */5540	for (hbqno = 0; hbqno < hbq_count; ++hbqno)5541		lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);5542	return 0;5543}5544 5545/**5546 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA5547 * @phba: Pointer to HBA context object.5548 *5549 * This function is called during the SLI initialization to configure5550 * all the HBQs and post buffers to the HBQ. The caller is not5551 * required to hold any locks. This function will return zero if successful5552 * else it will return negative error code.5553 **/5554static int5555lpfc_sli4_rb_setup(struct lpfc_hba *phba)5556{5557	phba->hbq_in_use = 1;5558	/**5559	 * Specific case when the MDS diagnostics is enabled and supported.5560	 * The receive buffer count is truncated to manage the incoming5561	 * traffic.5562	 **/5563	if (phba->cfg_enable_mds_diags && phba->mds_diags_support)5564		phba->hbqs[LPFC_ELS_HBQ].entry_count =5565			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;5566	else5567		phba->hbqs[LPFC_ELS_HBQ].entry_count =5568			lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;5569	phba->hbq_count = 1;5570	lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);5571	/* Initially populate or replenish the HBQs */5572	return 0;5573}5574 5575/**5576 * lpfc_sli_config_port - Issue config port mailbox command5577 * @phba: Pointer to HBA context object.5578 * @sli_mode: sli mode - 2/35579 *5580 * This function is called by the sli initialization code path5581 * to issue config_port mailbox command. This function restarts the5582 * HBA firmware and issues a config_port mailbox command to configure5583 * the SLI interface in the sli mode specified by sli_mode5584 * variable. The caller is not required to hold any locks.5585 * The function returns 0 if successful, else returns negative error5586 * code.5587 **/5588int5589lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)5590{5591	LPFC_MBOXQ_t *pmb;5592	uint32_t resetcount = 0, rc = 0, done = 0;5593 5594	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);5595	if (!pmb) {5596		phba->link_state = LPFC_HBA_ERROR;5597		return -ENOMEM;5598	}5599 5600	phba->sli_rev = sli_mode;5601	while (resetcount < 2 && !done) {5602		spin_lock_irq(&phba->hbalock);5603		phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;5604		spin_unlock_irq(&phba->hbalock);5605		phba->pport->port_state = LPFC_VPORT_UNKNOWN;5606		lpfc_sli_brdrestart(phba);5607		rc = lpfc_sli_chipset_init(phba);5608		if (rc)5609			break;5610 5611		spin_lock_irq(&phba->hbalock);5612		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;5613		spin_unlock_irq(&phba->hbalock);5614		resetcount++;5615 5616		/* Call pre CONFIG_PORT mailbox command initialization.  A5617		 * value of 0 means the call was successful.  Any other5618		 * nonzero value is a failure, but if ERESTART is returned,5619		 * the driver may reset the HBA and try again.5620		 */5621		rc = lpfc_config_port_prep(phba);5622		if (rc == -ERESTART) {5623			phba->link_state = LPFC_LINK_UNKNOWN;5624			continue;5625		} else if (rc)5626			break;5627 5628		phba->link_state = LPFC_INIT_MBX_CMDS;5629		lpfc_config_port(phba, pmb);5630		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);5631		phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |5632					LPFC_SLI3_HBQ_ENABLED |5633					LPFC_SLI3_CRP_ENABLED |5634					LPFC_SLI3_DSS_ENABLED);5635		if (rc != MBX_SUCCESS) {5636			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,5637				"0442 Adapter failed to init, mbxCmd x%x "5638				"CONFIG_PORT, mbxStatus x%x Data: x%x\n",5639				pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);5640			spin_lock_irq(&phba->hbalock);5641			phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;5642			spin_unlock_irq(&phba->hbalock);5643			rc = -ENXIO;5644		} else {5645			/* Allow asynchronous mailbox command to go through */5646			spin_lock_irq(&phba->hbalock);5647			phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;5648			spin_unlock_irq(&phba->hbalock);5649			done = 1;5650 5651			if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&5652			    (pmb->u.mb.un.varCfgPort.gasabt == 0))5653				lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,5654					"3110 Port did not grant ASABT\n");5655		}5656	}5657	if (!done) {5658		rc = -EINVAL;5659		goto do_prep_failed;5660	}5661	if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {5662		if (!pmb->u.mb.un.varCfgPort.cMA) {5663			rc = -ENXIO;5664			goto do_prep_failed;5665		}5666		if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {5667			phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;5668			phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;5669			phba->max_vports = (phba->max_vpi > phba->max_vports) ?5670				phba->max_vpi : phba->max_vports;5671 5672		} else5673			phba->max_vpi = 0;5674		if (pmb->u.mb.un.varCfgPort.gerbm)5675			phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;5676		if (pmb->u.mb.un.varCfgPort.gcrp)5677			phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;5678 5679		phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;5680		phba->port_gp = phba->mbox->us.s3_pgp.port;5681 5682		if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {5683			if (pmb->u.mb.un.varCfgPort.gbg == 0) {5684				phba->cfg_enable_bg = 0;5685				phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;5686				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,5687						"0443 Adapter did not grant "5688						"BlockGuard\n");5689			}5690		}5691	} else {5692		phba->hbq_get = NULL;5693		phba->port_gp = phba->mbox->us.s2.port;5694		phba->max_vpi = 0;5695	}5696do_prep_failed:5697	mempool_free(pmb, phba->mbox_mem_pool);5698	return rc;5699}5700 5701 5702/**5703 * lpfc_sli_hba_setup - SLI initialization function5704 * @phba: Pointer to HBA context object.5705 *5706 * This function is the main SLI initialization function. This function5707 * is called by the HBA initialization code, HBA reset code and HBA5708 * error attention handler code. Caller is not required to hold any5709 * locks. This function issues config_port mailbox command to configure5710 * the SLI, setup iocb rings and HBQ rings. In the end the function5711 * calls the config_port_post function to issue init_link mailbox5712 * command and to start the discovery. The function will return zero5713 * if successful, else it will return negative error code.5714 **/5715int5716lpfc_sli_hba_setup(struct lpfc_hba *phba)5717{5718	uint32_t rc;5719	int  i;5720	int longs;5721 5722	/* Enable ISR already does config_port because of config_msi mbx */5723	if (test_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag)) {5724		rc = lpfc_sli_config_port(phba, LPFC_SLI_REV3);5725		if (rc)5726			return -EIO;5727		clear_bit(HBA_NEEDS_CFG_PORT, &phba->hba_flag);5728	}5729	phba->fcp_embed_io = 0;	/* SLI4 FC support only */5730 5731	if (phba->sli_rev == 3) {5732		phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;5733		phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;5734	} else {5735		phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;5736		phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;5737		phba->sli3_options = 0;5738	}5739 5740	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,5741			"0444 Firmware in SLI %x mode. Max_vpi %d\n",5742			phba->sli_rev, phba->max_vpi);5743	rc = lpfc_sli_ring_map(phba);5744 5745	if (rc)5746		goto lpfc_sli_hba_setup_error;5747 5748	/* Initialize VPIs. */5749	if (phba->sli_rev == LPFC_SLI_REV3) {5750		/*5751		 * The VPI bitmask and physical ID array are allocated5752		 * and initialized once only - at driver load.  A port5753		 * reset doesn't need to reinitialize this memory.5754		 */5755		if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {5756			longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;5757			phba->vpi_bmask = kcalloc(longs,5758						  sizeof(unsigned long),5759						  GFP_KERNEL);5760			if (!phba->vpi_bmask) {5761				rc = -ENOMEM;5762				goto lpfc_sli_hba_setup_error;5763			}5764 5765			phba->vpi_ids = kcalloc(phba->max_vpi + 1,5766						sizeof(uint16_t),5767						GFP_KERNEL);5768			if (!phba->vpi_ids) {5769				kfree(phba->vpi_bmask);5770				rc = -ENOMEM;5771				goto lpfc_sli_hba_setup_error;5772			}5773			for (i = 0; i < phba->max_vpi; i++)5774				phba->vpi_ids[i] = i;5775		}5776	}5777 5778	/* Init HBQs */5779	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {5780		rc = lpfc_sli_hbq_setup(phba);5781		if (rc)5782			goto lpfc_sli_hba_setup_error;5783	}5784	spin_lock_irq(&phba->hbalock);5785	phba->sli.sli_flag |= LPFC_PROCESS_LA;5786	spin_unlock_irq(&phba->hbalock);5787 5788	rc = lpfc_config_port_post(phba);5789	if (rc)5790		goto lpfc_sli_hba_setup_error;5791 5792	return rc;5793 5794lpfc_sli_hba_setup_error:5795	phba->link_state = LPFC_HBA_ERROR;5796	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,5797			"0445 Firmware initialization failed\n");5798	return rc;5799}5800 5801/**5802 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region5803 * @phba: Pointer to HBA context object.5804 *5805 * This function issue a dump mailbox command to read config region5806 * 23 and parse the records in the region and populate driver5807 * data structure.5808 **/5809static int5810lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)5811{5812	LPFC_MBOXQ_t *mboxq;5813	struct lpfc_dmabuf *mp;5814	struct lpfc_mqe *mqe;5815	uint32_t data_length;5816	int rc;5817 5818	/* Program the default value of vlan_id and fc_map */5819	phba->valid_vlan = 0;5820	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;5821	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;5822	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;5823 5824	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);5825	if (!mboxq)5826		return -ENOMEM;5827 5828	mqe = &mboxq->u.mqe;5829	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {5830		rc = -ENOMEM;5831		goto out_free_mboxq;5832	}5833 5834	mp = mboxq->ctx_buf;5835	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);5836 5837	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,5838			"(%d):2571 Mailbox cmd x%x Status x%x "5839			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "5840			"x%x x%x x%x x%x x%x x%x x%x x%x x%x "5841			"CQ: x%x x%x x%x x%x\n",5842			mboxq->vport ? mboxq->vport->vpi : 0,5843			bf_get(lpfc_mqe_command, mqe),5844			bf_get(lpfc_mqe_status, mqe),5845			mqe->un.mb_words[0], mqe->un.mb_words[1],5846			mqe->un.mb_words[2], mqe->un.mb_words[3],5847			mqe->un.mb_words[4], mqe->un.mb_words[5],5848			mqe->un.mb_words[6], mqe->un.mb_words[7],5849			mqe->un.mb_words[8], mqe->un.mb_words[9],5850			mqe->un.mb_words[10], mqe->un.mb_words[11],5851			mqe->un.mb_words[12], mqe->un.mb_words[13],5852			mqe->un.mb_words[14], mqe->un.mb_words[15],5853			mqe->un.mb_words[16], mqe->un.mb_words[50],5854			mboxq->mcqe.word0,5855			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,5856			mboxq->mcqe.trailer);5857 5858	if (rc) {5859		rc = -EIO;5860		goto out_free_mboxq;5861	}5862	data_length = mqe->un.mb_words[5];5863	if (data_length > DMP_RGN23_SIZE) {5864		rc = -EIO;5865		goto out_free_mboxq;5866	}5867 5868	lpfc_parse_fcoe_conf(phba, mp->virt, data_length);5869	rc = 0;5870 5871out_free_mboxq:5872	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);5873	return rc;5874}5875 5876/**5877 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data5878 * @phba: pointer to lpfc hba data structure.5879 * @mboxq: pointer to the LPFC_MBOXQ_t structure.5880 * @vpd: pointer to the memory to hold resulting port vpd data.5881 * @vpd_size: On input, the number of bytes allocated to @vpd.5882 *	      On output, the number of data bytes in @vpd.5883 *5884 * This routine executes a READ_REV SLI4 mailbox command.  In5885 * addition, this routine gets the port vpd data.5886 *5887 * Return codes5888 * 	0 - successful5889 * 	-ENOMEM - could not allocated memory.5890 **/5891static int5892lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,5893		    uint8_t *vpd, uint32_t *vpd_size)5894{5895	int rc = 0;5896	uint32_t dma_size;5897	struct lpfc_dmabuf *dmabuf;5898	struct lpfc_mqe *mqe;5899 5900	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);5901	if (!dmabuf)5902		return -ENOMEM;5903 5904	/*5905	 * Get a DMA buffer for the vpd data resulting from the READ_REV5906	 * mailbox command.5907	 */5908	dma_size = *vpd_size;5909	dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,5910					  &dmabuf->phys, GFP_KERNEL);5911	if (!dmabuf->virt) {5912		kfree(dmabuf);5913		return -ENOMEM;5914	}5915 5916	/*5917	 * The SLI4 implementation of READ_REV conflicts at word1,5918	 * bits 31:16 and SLI4 adds vpd functionality not present5919	 * in SLI3.  This code corrects the conflicts.5920	 */5921	lpfc_read_rev(phba, mboxq);5922	mqe = &mboxq->u.mqe;5923	mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);5924	mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);5925	mqe->un.read_rev.word1 &= 0x0000FFFF;5926	bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);5927	bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);5928 5929	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);5930	if (rc) {5931		dma_free_coherent(&phba->pcidev->dev, dma_size,5932				  dmabuf->virt, dmabuf->phys);5933		kfree(dmabuf);5934		return -EIO;5935	}5936 5937	/*5938	 * The available vpd length cannot be bigger than the5939	 * DMA buffer passed to the port.  Catch the less than5940	 * case and update the caller's size.5941	 */5942	if (mqe->un.read_rev.avail_vpd_len < *vpd_size)5943		*vpd_size = mqe->un.read_rev.avail_vpd_len;5944 5945	memcpy(vpd, dmabuf->virt, *vpd_size);5946 5947	dma_free_coherent(&phba->pcidev->dev, dma_size,5948			  dmabuf->virt, dmabuf->phys);5949	kfree(dmabuf);5950	return 0;5951}5952 5953/**5954 * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes5955 * @phba: pointer to lpfc hba data structure.5956 *5957 * This routine retrieves SLI4 device physical port name this PCI function5958 * is attached to.5959 *5960 * Return codes5961 *      0 - successful5962 *      otherwise - failed to retrieve controller attributes5963 **/5964static int5965lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)5966{5967	LPFC_MBOXQ_t *mboxq;5968	struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;5969	struct lpfc_controller_attribute *cntl_attr;5970	void *virtaddr = NULL;5971	uint32_t alloclen, reqlen;5972	uint32_t shdr_status, shdr_add_status;5973	union lpfc_sli4_cfg_shdr *shdr;5974	int rc;5975 5976	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);5977	if (!mboxq)5978		return -ENOMEM;5979 5980	/* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */5981	reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);5982	alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,5983			LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,5984			LPFC_SLI4_MBX_NEMBED);5985 5986	if (alloclen < reqlen) {5987		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,5988				"3084 Allocated DMA memory size (%d) is "5989				"less than the requested DMA memory size "5990				"(%d)\n", alloclen, reqlen);5991		rc = -ENOMEM;5992		goto out_free_mboxq;5993	}5994	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);5995	virtaddr = mboxq->sge_array->addr[0];5996	mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;5997	shdr = &mbx_cntl_attr->cfg_shdr;5998	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);5999	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);6000	if (shdr_status || shdr_add_status || rc) {6001		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,6002				"3085 Mailbox x%x (x%x/x%x) failed, "6003				"rc:x%x, status:x%x, add_status:x%x\n",6004				bf_get(lpfc_mqe_command, &mboxq->u.mqe),6005				lpfc_sli_config_mbox_subsys_get(phba, mboxq),6006				lpfc_sli_config_mbox_opcode_get(phba, mboxq),6007				rc, shdr_status, shdr_add_status);6008		rc = -ENXIO;6009		goto out_free_mboxq;6010	}6011 6012	cntl_attr = &mbx_cntl_attr->cntl_attr;6013	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;6014	phba->sli4_hba.lnk_info.lnk_tp =6015		bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);6016	phba->sli4_hba.lnk_info.lnk_no =6017		bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);6018	phba->sli4_hba.flash_id = bf_get(lpfc_cntl_attr_flash_id, cntl_attr);6019	phba->sli4_hba.asic_rev = bf_get(lpfc_cntl_attr_asic_rev, cntl_attr);6020 6021	memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));6022	strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,6023		sizeof(phba->BIOSVersion));6024 6025	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,6026			"3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s, "6027			"flash_id: x%02x, asic_rev: x%02x\n",6028			phba->sli4_hba.lnk_info.lnk_tp,6029			phba->sli4_hba.lnk_info.lnk_no,6030			phba->BIOSVersion, phba->sli4_hba.flash_id,6031			phba->sli4_hba.asic_rev);6032out_free_mboxq:6033	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)6034		lpfc_sli4_mbox_cmd_free(phba, mboxq);6035	else6036		mempool_free(mboxq, phba->mbox_mem_pool);6037	return rc;6038}6039 6040/**6041 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name6042 * @phba: pointer to lpfc hba data structure.6043 *6044 * This routine retrieves SLI4 device physical port name this PCI function6045 * is attached to.6046 *6047 * Return codes6048 *      0 - successful6049 *      otherwise - failed to retrieve physical port name6050 **/6051static int6052lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)6053{6054	LPFC_MBOXQ_t *mboxq;6055	struct lpfc_mbx_get_port_name *get_port_name;6056	uint32_t shdr_status, shdr_add_status;6057	union lpfc_sli4_cfg_shdr *shdr;6058	char cport_name = 0;6059	int rc;6060 6061	/* We assume nothing at this point */6062	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;6063	phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;6064 6065	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);6066	if (!mboxq)6067		return -ENOMEM;6068	/* obtain link type and link number via READ_CONFIG */6069	phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;6070	lpfc_sli4_read_config(phba);6071 6072	if (phba->sli4_hba.fawwpn_flag & LPFC_FAWWPN_CONFIG)6073		phba->sli4_hba.fawwpn_flag |= LPFC_FAWWPN_FABRIC;6074 6075	if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)6076		goto retrieve_ppname;6077 6078	/* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */6079	rc = lpfc_sli4_get_ctl_attr(phba);6080	if (rc)6081		goto out_free_mboxq;6082 6083retrieve_ppname:6084	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,6085		LPFC_MBOX_OPCODE_GET_PORT_NAME,6086		sizeof(struct lpfc_mbx_get_port_name) -6087		sizeof(struct lpfc_sli4_cfg_mhdr),6088		LPFC_SLI4_MBX_EMBED);6089	get_port_name = &mboxq->u.mqe.un.get_port_name;6090	shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;6091	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);6092	bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,6093		phba->sli4_hba.lnk_info.lnk_tp);6094	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);6095	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);6096	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);6097	if (shdr_status || shdr_add_status || rc) {6098		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,6099				"3087 Mailbox x%x (x%x/x%x) failed: "6100				"rc:x%x, status:x%x, add_status:x%x\n",6101				bf_get(lpfc_mqe_command, &mboxq->u.mqe),6102				lpfc_sli_config_mbox_subsys_get(phba, mboxq),6103				lpfc_sli_config_mbox_opcode_get(phba, mboxq),6104				rc, shdr_status, shdr_add_status);6105		rc = -ENXIO;6106		goto out_free_mboxq;6107	}6108	switch (phba->sli4_hba.lnk_info.lnk_no) {6109	case LPFC_LINK_NUMBER_0:6110		cport_name = bf_get(lpfc_mbx_get_port_name_name0,6111				&get_port_name->u.response);6112		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;6113		break;6114	case LPFC_LINK_NUMBER_1:6115		cport_name = bf_get(lpfc_mbx_get_port_name_name1,6116				&get_port_name->u.response);6117		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;6118		break;6119	case LPFC_LINK_NUMBER_2:6120		cport_name = bf_get(lpfc_mbx_get_port_name_name2,6121				&get_port_name->u.response);6122		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;6123		break;6124	case LPFC_LINK_NUMBER_3:6125		cport_name = bf_get(lpfc_mbx_get_port_name_name3,6126				&get_port_name->u.response);6127		phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;6128		break;6129	default:6130		break;6131	}6132 6133	if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {6134		phba->Port[0] = cport_name;6135		phba->Port[1] = '\0';6136		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,6137				"3091 SLI get port name: %s\n", phba->Port);6138	}6139 6140out_free_mboxq:6141	if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)6142		lpfc_sli4_mbox_cmd_free(phba, mboxq);6143	else6144		mempool_free(mboxq, phba->mbox_mem_pool);6145	return rc;6146}6147 6148/**6149 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues6150 * @phba: pointer to lpfc hba data structure.6151 *6152 * This routine is called to explicitly arm the SLI4 device's completion and6153 * event queues6154 **/6155static void6156lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)6157{6158	int qidx;6159	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;6160	struct lpfc_sli4_hdw_queue *qp;6161	struct lpfc_queue *eq;6162 6163	sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);6164	sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);6165	if (sli4_hba->nvmels_cq)6166		sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,6167					   LPFC_QUEUE_REARM);6168 6169	if (sli4_hba->hdwq) {6170		/* Loop thru all Hardware Queues */6171		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {6172			qp = &sli4_hba->hdwq[qidx];6173			/* ARM the corresponding CQ */6174			sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,6175						LPFC_QUEUE_REARM);6176		}6177 6178		/* Loop thru all IRQ vectors */6179		for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {6180			eq = sli4_hba->hba_eq_hdl[qidx].eq;6181			/* ARM the corresponding EQ */6182			sli4_hba->sli4_write_eq_db(phba, eq,6183						   0, LPFC_QUEUE_REARM);6184		}6185	}6186 6187	if (phba->nvmet_support) {6188		for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {6189			sli4_hba->sli4_write_cq_db(phba,6190				sli4_hba->nvmet_cqset[qidx], 0,6191				LPFC_QUEUE_REARM);6192		}6193	}6194}6195 6196/**6197 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.6198 * @phba: Pointer to HBA context object.6199 * @type: The resource extent type.6200 * @extnt_count: buffer to hold port available extent count.6201 * @extnt_size: buffer to hold element count per extent.6202 *6203 * This function calls the port and retrievs the number of available6204 * extents and their size for a particular extent type.6205 *6206 * Returns: 0 if successful.  Nonzero otherwise.6207 **/6208int6209lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,6210			       uint16_t *extnt_count, uint16_t *extnt_size)6211{6212	int rc = 0;6213	uint32_t length;6214	uint32_t mbox_tmo;6215	struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;6216	LPFC_MBOXQ_t *mbox;6217 6218	*extnt_count = 0;6219	*extnt_size = 0;6220 6221	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);6222	if (!mbox)6223		return -ENOMEM;6224 6225	/* Find out how many extents are available for this resource type */6226	length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -6227		  sizeof(struct lpfc_sli4_cfg_mhdr));6228	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,6229			 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,6230			 length, LPFC_SLI4_MBX_EMBED);6231 6232	/* Send an extents count of 0 - the GET doesn't use it. */6233	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,6234					LPFC_SLI4_MBX_EMBED);6235	if (unlikely(rc)) {6236		rc = -EIO;6237		goto err_exit;6238	}6239 6240	if (!phba->sli4_hba.intr_enable)6241		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);6242	else {6243		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);6244		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);6245	}6246	if (unlikely(rc)) {6247		rc = -EIO;6248		goto err_exit;6249	}6250 6251	rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;6252	if (bf_get(lpfc_mbox_hdr_status,6253		   &rsrc_info->header.cfg_shdr.response)) {6254		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,6255				"2930 Failed to get resource extents "6256				"Status 0x%x Add'l Status 0x%x\n",6257				bf_get(lpfc_mbox_hdr_status,6258				       &rsrc_info->header.cfg_shdr.response),6259				bf_get(lpfc_mbox_hdr_add_status,6260				       &rsrc_info->header.cfg_shdr.response));6261		rc = -EIO;6262		goto err_exit;6263	}6264 6265	*extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,6266			      &rsrc_info->u.rsp);6267	*extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,6268			     &rsrc_info->u.rsp);6269 6270	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,6271			"3162 Retrieved extents type-%d from port: count:%d, "6272			"size:%d\n", type, *extnt_count, *extnt_size);6273 6274err_exit:6275	mempool_free(mbox, phba->mbox_mem_pool);6276	return rc;6277}6278 6279/**6280 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.6281 * @phba: Pointer to HBA context object.6282 * @type: The extent type to check.6283 *6284 * This function reads the current available extents from the port and checks6285 * if the extent count or extent size has changed since the last access.6286 * Callers use this routine post port reset to understand if there is a6287 * extent reprovisioning requirement.6288 *6289 * Returns:6290 *   -Error: error indicates problem.6291 *   1: Extent count or size has changed.6292 *   0: No changes.6293 **/6294static int6295lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)6296{6297	uint16_t curr_ext_cnt, rsrc_ext_cnt;6298	uint16_t size_diff, rsrc_ext_size;6299	int rc = 0;6300	struct lpfc_rsrc_blks *rsrc_entry;6301	struct list_head *rsrc_blk_list = NULL;6302 6303	size_diff = 0;6304	curr_ext_cnt = 0;6305	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,6306					    &rsrc_ext_cnt,6307					    &rsrc_ext_size);6308	if (unlikely(rc))6309		return -EIO;6310 6311	switch (type) {6312	case LPFC_RSC_TYPE_FCOE_RPI:6313		rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;6314		break;6315	case LPFC_RSC_TYPE_FCOE_VPI:6316		rsrc_blk_list = &phba->lpfc_vpi_blk_list;6317		break;6318	case LPFC_RSC_TYPE_FCOE_XRI:6319		rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;6320		break;6321	case LPFC_RSC_TYPE_FCOE_VFI:6322		rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;6323		break;6324	default:6325		break;6326	}6327 6328	list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {6329		curr_ext_cnt++;6330		if (rsrc_entry->rsrc_size != rsrc_ext_size)6331			size_diff++;6332	}6333 6334	if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)6335		rc = 1;6336 6337	return rc;6338}6339 6340/**6341 * lpfc_sli4_cfg_post_extnts -6342 * @phba: Pointer to HBA context object.6343 * @extnt_cnt: number of available extents.6344 * @type: the extent type (rpi, xri, vfi, vpi).6345 * @emb: buffer to hold either MBX_EMBED or MBX_NEMBED operation.6346 * @mbox: pointer to the caller's allocated mailbox structure.6347 *6348 * This function executes the extents allocation request.  It also6349 * takes care of the amount of memory needed to allocate or get the6350 * allocated extents. It is the caller's responsibility to evaluate6351 * the response.6352 *6353 * Returns:6354 *   -Error:  Error value describes the condition found.6355 *   0: if successful6356 **/6357static int6358lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,6359			  uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)6360{6361	int rc = 0;6362	uint32_t req_len;6363	uint32_t emb_len;6364	uint32_t alloc_len, mbox_tmo;6365 6366	/* Calculate the total requested length of the dma memory */6367	req_len = extnt_cnt * sizeof(uint16_t);6368 6369	/*6370	 * Calculate the size of an embedded mailbox.  The uint32_t6371	 * accounts for extents-specific word.6372	 */6373	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -6374		sizeof(uint32_t);6375 6376	/*6377	 * Presume the allocation and response will fit into an embedded6378	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.6379	 */6380	*emb = LPFC_SLI4_MBX_EMBED;6381	if (req_len > emb_len) {6382		req_len = extnt_cnt * sizeof(uint16_t) +6383			sizeof(union lpfc_sli4_cfg_shdr) +6384			sizeof(uint32_t);6385		*emb = LPFC_SLI4_MBX_NEMBED;6386	}6387 6388	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,6389				     LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,6390				     req_len, *emb);6391	if (alloc_len < req_len) {6392		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,6393			"2982 Allocated DMA memory size (x%x) is "6394			"less than the requested DMA memory "6395			"size (x%x)\n", alloc_len, req_len);6396		return -ENOMEM;6397	}6398	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);6399	if (unlikely(rc))6400		return -EIO;6401 6402	if (!phba->sli4_hba.intr_enable)6403		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);6404	else {6405		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);6406		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);6407	}6408 6409	if (unlikely(rc))6410		rc = -EIO;6411	return rc;6412}6413 6414/**6415 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.6416 * @phba: Pointer to HBA context object.6417 * @type:  The resource extent type to allocate.6418 *6419 * This function allocates the number of elements for the specified6420 * resource type.6421 **/6422static int6423lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)6424{6425	bool emb = false;6426	uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;6427	uint16_t rsrc_id, rsrc_start, j, k;6428	uint16_t *ids;6429	int i, rc;6430	unsigned long longs;6431	unsigned long *bmask;6432	struct lpfc_rsrc_blks *rsrc_blks;6433	LPFC_MBOXQ_t *mbox;6434	uint32_t length;6435	struct lpfc_id_range *id_array = NULL;6436	void *virtaddr = NULL;6437	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;6438	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;6439	struct list_head *ext_blk_list;6440 6441	rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,6442					    &rsrc_cnt,6443					    &rsrc_size);6444	if (unlikely(rc))6445		return -EIO;6446 6447	if ((rsrc_cnt == 0) || (rsrc_size == 0)) {6448		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,6449			"3009 No available Resource Extents "6450			"for resource type 0x%x: Count: 0x%x, "6451			"Size 0x%x\n", type, rsrc_cnt,6452			rsrc_size);6453		return -ENOMEM;6454	}6455 6456	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,6457			"2903 Post resource extents type-0x%x: "6458			"count:%d, size %d\n", type, rsrc_cnt, rsrc_size);6459 6460	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);6461	if (!mbox)6462		return -ENOMEM;6463 6464	rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);6465	if (unlikely(rc)) {6466		rc = -EIO;6467		goto err_exit;6468	}6469 6470	/*6471	 * Figure out where the response is located.  Then get local pointers6472	 * to the response data.  The port does not guarantee to respond to6473	 * all extents counts request so update the local variable with the6474	 * allocated count from the port.6475	 */6476	if (emb == LPFC_SLI4_MBX_EMBED) {6477		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;6478		id_array = &rsrc_ext->u.rsp.id[0];6479		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);6480	} else {6481		virtaddr = mbox->sge_array->addr[0];6482		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;6483		rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);6484		id_array = &n_rsrc->id;6485	}6486 6487	longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;6488	rsrc_id_cnt = rsrc_cnt * rsrc_size;6489 6490	/*6491	 * Based on the resource size and count, correct the base and max6492	 * resource values.6493	 */6494	length = sizeof(struct lpfc_rsrc_blks);6495	switch (type) {6496	case LPFC_RSC_TYPE_FCOE_RPI:6497		phba->sli4_hba.rpi_bmask = kcalloc(longs,6498						   sizeof(unsigned long),6499						   GFP_KERNEL);6500		if (unlikely(!phba->sli4_hba.rpi_bmask)) {6501			rc = -ENOMEM;6502			goto err_exit;6503		}6504		phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,6505						 sizeof(uint16_t),6506						 GFP_KERNEL);6507		if (unlikely(!phba->sli4_hba.rpi_ids)) {6508			kfree(phba->sli4_hba.rpi_bmask);6509			rc = -ENOMEM;6510			goto err_exit;6511		}6512 6513		/*6514		 * The next_rpi was initialized with the maximum available6515		 * count but the port may allocate a smaller number.  Catch6516		 * that case and update the next_rpi.6517		 */6518		phba->sli4_hba.next_rpi = rsrc_id_cnt;6519 6520		/* Initialize local ptrs for common extent processing later. */6521		bmask = phba->sli4_hba.rpi_bmask;6522		ids = phba->sli4_hba.rpi_ids;6523		ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;6524		break;6525	case LPFC_RSC_TYPE_FCOE_VPI:6526		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),6527					  GFP_KERNEL);6528		if (unlikely(!phba->vpi_bmask)) {6529			rc = -ENOMEM;6530			goto err_exit;6531		}6532		phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),6533					 GFP_KERNEL);6534		if (unlikely(!phba->vpi_ids)) {6535			kfree(phba->vpi_bmask);6536			rc = -ENOMEM;6537			goto err_exit;6538		}6539 6540		/* Initialize local ptrs for common extent processing later. */6541		bmask = phba->vpi_bmask;6542		ids = phba->vpi_ids;6543		ext_blk_list = &phba->lpfc_vpi_blk_list;6544		break;6545	case LPFC_RSC_TYPE_FCOE_XRI:6546		phba->sli4_hba.xri_bmask = kcalloc(longs,6547						   sizeof(unsigned long),6548						   GFP_KERNEL);6549		if (unlikely(!phba->sli4_hba.xri_bmask)) {6550			rc = -ENOMEM;6551			goto err_exit;6552		}6553		phba->sli4_hba.max_cfg_param.xri_used = 0;6554		phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,6555						 sizeof(uint16_t),6556						 GFP_KERNEL);6557		if (unlikely(!phba->sli4_hba.xri_ids)) {6558			kfree(phba->sli4_hba.xri_bmask);6559			rc = -ENOMEM;6560			goto err_exit;6561		}6562 6563		/* Initialize local ptrs for common extent processing later. */6564		bmask = phba->sli4_hba.xri_bmask;6565		ids = phba->sli4_hba.xri_ids;6566		ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;6567		break;6568	case LPFC_RSC_TYPE_FCOE_VFI:6569		phba->sli4_hba.vfi_bmask = kcalloc(longs,6570						   sizeof(unsigned long),6571						   GFP_KERNEL);6572		if (unlikely(!phba->sli4_hba.vfi_bmask)) {6573			rc = -ENOMEM;6574			goto err_exit;6575		}6576		phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,6577						 sizeof(uint16_t),6578						 GFP_KERNEL);6579		if (unlikely(!phba->sli4_hba.vfi_ids)) {6580			kfree(phba->sli4_hba.vfi_bmask);6581			rc = -ENOMEM;6582			goto err_exit;6583		}6584 6585		/* Initialize local ptrs for common extent processing later. */6586		bmask = phba->sli4_hba.vfi_bmask;6587		ids = phba->sli4_hba.vfi_ids;6588		ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;6589		break;6590	default:6591		/* Unsupported Opcode.  Fail call. */6592		id_array = NULL;6593		bmask = NULL;6594		ids = NULL;6595		ext_blk_list = NULL;6596		goto err_exit;6597	}6598 6599	/*6600	 * Complete initializing the extent configuration with the6601	 * allocated ids assigned to this function.  The bitmask serves6602	 * as an index into the array and manages the available ids.  The6603	 * array just stores the ids communicated to the port via the wqes.6604	 */6605	for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {6606		if ((i % 2) == 0)6607			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,6608					 &id_array[k]);6609		else6610			rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,6611					 &id_array[k]);6612 6613		rsrc_blks = kzalloc(length, GFP_KERNEL);6614		if (unlikely(!rsrc_blks)) {6615			rc = -ENOMEM;6616			kfree(bmask);6617			kfree(ids);6618			goto err_exit;6619		}6620		rsrc_blks->rsrc_start = rsrc_id;6621		rsrc_blks->rsrc_size = rsrc_size;6622		list_add_tail(&rsrc_blks->list, ext_blk_list);6623		rsrc_start = rsrc_id;6624		if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {6625			phba->sli4_hba.io_xri_start = rsrc_start +6626				lpfc_sli4_get_iocb_cnt(phba);6627		}6628 6629		while (rsrc_id < (rsrc_start + rsrc_size)) {6630			ids[j] = rsrc_id;6631			rsrc_id++;6632			j++;6633		}6634		/* Entire word processed.  Get next word.*/6635		if ((i % 2) == 1)6636			k++;6637	}6638 err_exit:6639	lpfc_sli4_mbox_cmd_free(phba, mbox);6640	return rc;6641}6642 6643 6644 6645/**6646 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.6647 * @phba: Pointer to HBA context object.6648 * @type: the extent's type.6649 *6650 * This function deallocates all extents of a particular resource type.6651 * SLI4 does not allow for deallocating a particular extent range.  It6652 * is the caller's responsibility to release all kernel memory resources.6653 **/6654static int6655lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)6656{6657	int rc;6658	uint32_t length, mbox_tmo = 0;6659	LPFC_MBOXQ_t *mbox;6660	struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;6661	struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;6662 6663	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);6664	if (!mbox)6665		return -ENOMEM;6666 6667	/*6668	 * This function sends an embedded mailbox because it only sends the6669	 * the resource type.  All extents of this type are released by the6670	 * port.6671	 */6672	length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -6673		  sizeof(struct lpfc_sli4_cfg_mhdr));6674	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,6675			 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,6676			 length, LPFC_SLI4_MBX_EMBED);6677 6678	/* Send an extents count of 0 - the dealloc doesn't use it. */6679	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,6680					LPFC_SLI4_MBX_EMBED);6681	if (unlikely(rc)) {6682		rc = -EIO;6683		goto out_free_mbox;6684	}6685	if (!phba->sli4_hba.intr_enable)6686		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);6687	else {6688		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);6689		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);6690	}6691	if (unlikely(rc)) {6692		rc = -EIO;6693		goto out_free_mbox;6694	}6695 6696	dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;6697	if (bf_get(lpfc_mbox_hdr_status,6698		   &dealloc_rsrc->header.cfg_shdr.response)) {6699		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,6700				"2919 Failed to release resource extents "6701				"for type %d - Status 0x%x Add'l Status 0x%x. "6702				"Resource memory not released.\n",6703				type,6704				bf_get(lpfc_mbox_hdr_status,6705				    &dealloc_rsrc->header.cfg_shdr.response),6706				bf_get(lpfc_mbox_hdr_add_status,6707				    &dealloc_rsrc->header.cfg_shdr.response));6708		rc = -EIO;6709		goto out_free_mbox;6710	}6711 6712	/* Release kernel memory resources for the specific type. */6713	switch (type) {6714	case LPFC_RSC_TYPE_FCOE_VPI:6715		kfree(phba->vpi_bmask);6716		kfree(phba->vpi_ids);6717		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);6718		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,6719				    &phba->lpfc_vpi_blk_list, list) {6720			list_del_init(&rsrc_blk->list);6721			kfree(rsrc_blk);6722		}6723		phba->sli4_hba.max_cfg_param.vpi_used = 0;6724		break;6725	case LPFC_RSC_TYPE_FCOE_XRI:6726		kfree(phba->sli4_hba.xri_bmask);6727		kfree(phba->sli4_hba.xri_ids);6728		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,6729				    &phba->sli4_hba.lpfc_xri_blk_list, list) {6730			list_del_init(&rsrc_blk->list);6731			kfree(rsrc_blk);6732		}6733		break;6734	case LPFC_RSC_TYPE_FCOE_VFI:6735		kfree(phba->sli4_hba.vfi_bmask);6736		kfree(phba->sli4_hba.vfi_ids);6737		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);6738		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,6739				    &phba->sli4_hba.lpfc_vfi_blk_list, list) {6740			list_del_init(&rsrc_blk->list);6741			kfree(rsrc_blk);6742		}6743		break;6744	case LPFC_RSC_TYPE_FCOE_RPI:6745		/* RPI bitmask and physical id array are cleaned up earlier. */6746		list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,6747				    &phba->sli4_hba.lpfc_rpi_blk_list, list) {6748			list_del_init(&rsrc_blk->list);6749			kfree(rsrc_blk);6750		}6751		break;6752	default:6753		break;6754	}6755 6756	bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);6757 6758 out_free_mbox:6759	mempool_free(mbox, phba->mbox_mem_pool);6760	return rc;6761}6762 6763static void6764lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,6765		  uint32_t feature)6766{6767	uint32_t len;6768	u32 sig_freq = 0;6769 6770	len = sizeof(struct lpfc_mbx_set_feature) -6771		sizeof(struct lpfc_sli4_cfg_mhdr);6772	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,6773			 LPFC_MBOX_OPCODE_SET_FEATURES, len,6774			 LPFC_SLI4_MBX_EMBED);6775 6776	switch (feature) {6777	case LPFC_SET_UE_RECOVERY:6778		bf_set(lpfc_mbx_set_feature_UER,6779		       &mbox->u.mqe.un.set_feature, 1);6780		mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;6781		mbox->u.mqe.un.set_feature.param_len = 8;6782		break;6783	case LPFC_SET_MDS_DIAGS:6784		bf_set(lpfc_mbx_set_feature_mds,6785		       &mbox->u.mqe.un.set_feature, 1);6786		bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,6787		       &mbox->u.mqe.un.set_feature, 1);6788		mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;6789		mbox->u.mqe.un.set_feature.param_len = 8;6790		break;6791	case LPFC_SET_CGN_SIGNAL:6792		if (phba->cmf_active_mode == LPFC_CFG_OFF)6793			sig_freq = 0;6794		else6795			sig_freq = phba->cgn_sig_freq;6796 6797		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {6798			bf_set(lpfc_mbx_set_feature_CGN_alarm_freq,6799			       &mbox->u.mqe.un.set_feature, sig_freq);6800			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,6801			       &mbox->u.mqe.un.set_feature, sig_freq);6802		}6803 6804		if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY)6805			bf_set(lpfc_mbx_set_feature_CGN_warn_freq,6806			       &mbox->u.mqe.un.set_feature, sig_freq);6807 6808		if (phba->cmf_active_mode == LPFC_CFG_OFF ||6809		    phba->cgn_reg_signal == EDC_CG_SIG_NOTSUPPORTED)6810			sig_freq = 0;6811		else6812			sig_freq = lpfc_acqe_cgn_frequency;6813 6814		bf_set(lpfc_mbx_set_feature_CGN_acqe_freq,6815		       &mbox->u.mqe.un.set_feature, sig_freq);6816 6817		mbox->u.mqe.un.set_feature.feature = LPFC_SET_CGN_SIGNAL;6818		mbox->u.mqe.un.set_feature.param_len = 12;6819		break;6820	case LPFC_SET_DUAL_DUMP:6821		bf_set(lpfc_mbx_set_feature_dd,6822		       &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);6823		bf_set(lpfc_mbx_set_feature_ddquery,6824		       &mbox->u.mqe.un.set_feature, 0);6825		mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;6826		mbox->u.mqe.un.set_feature.param_len = 4;6827		break;6828	case LPFC_SET_ENABLE_MI:6829		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_MI;6830		mbox->u.mqe.un.set_feature.param_len = 4;6831		bf_set(lpfc_mbx_set_feature_milunq, &mbox->u.mqe.un.set_feature,6832		       phba->pport->cfg_lun_queue_depth);6833		bf_set(lpfc_mbx_set_feature_mi, &mbox->u.mqe.un.set_feature,6834		       phba->sli4_hba.pc_sli4_params.mi_ver);6835		break;6836	case LPFC_SET_LD_SIGNAL:6837		mbox->u.mqe.un.set_feature.feature = LPFC_SET_LD_SIGNAL;6838		mbox->u.mqe.un.set_feature.param_len = 16;6839		bf_set(lpfc_mbx_set_feature_lds_qry,6840		       &mbox->u.mqe.un.set_feature, LPFC_QUERY_LDS_OP);6841		break;6842	case LPFC_SET_ENABLE_CMF:6843		mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_CMF;6844		mbox->u.mqe.un.set_feature.param_len = 4;6845		bf_set(lpfc_mbx_set_feature_cmf,6846		       &mbox->u.mqe.un.set_feature, 1);6847		break;6848	}6849	return;6850}6851 6852/**6853 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter6854 * @phba: Pointer to HBA context object.6855 *6856 * Disable FW logging into host memory on the adapter. To6857 * be done before reading logs from the host memory.6858 **/6859void6860lpfc_ras_stop_fwlog(struct lpfc_hba *phba)6861{6862	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;6863 6864	spin_lock_irq(&phba->ras_fwlog_lock);6865	ras_fwlog->state = INACTIVE;6866	spin_unlock_irq(&phba->ras_fwlog_lock);6867 6868	/* Disable FW logging to host memory */6869	writel(LPFC_CTL_PDEV_CTL_DDL_RAS,6870	       phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);6871 6872	/* Wait 10ms for firmware to stop using DMA buffer */6873	usleep_range(10 * 1000, 20 * 1000);6874}6875 6876/**6877 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.6878 * @phba: Pointer to HBA context object.6879 *6880 * This function is called to free memory allocated for RAS FW logging6881 * support in the driver.6882 **/6883void6884lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)6885{6886	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;6887	struct lpfc_dmabuf *dmabuf, *next;6888 6889	if (!list_empty(&ras_fwlog->fwlog_buff_list)) {6890		list_for_each_entry_safe(dmabuf, next,6891				    &ras_fwlog->fwlog_buff_list,6892				    list) {6893			list_del(&dmabuf->list);6894			dma_free_coherent(&phba->pcidev->dev,6895					  LPFC_RAS_MAX_ENTRY_SIZE,6896					  dmabuf->virt, dmabuf->phys);6897			kfree(dmabuf);6898		}6899	}6900 6901	if (ras_fwlog->lwpd.virt) {6902		dma_free_coherent(&phba->pcidev->dev,6903				  sizeof(uint32_t) * 2,6904				  ras_fwlog->lwpd.virt,6905				  ras_fwlog->lwpd.phys);6906		ras_fwlog->lwpd.virt = NULL;6907	}6908 6909	spin_lock_irq(&phba->ras_fwlog_lock);6910	ras_fwlog->state = INACTIVE;6911	spin_unlock_irq(&phba->ras_fwlog_lock);6912}6913 6914/**6915 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support6916 * @phba: Pointer to HBA context object.6917 * @fwlog_buff_count: Count of buffers to be created.6918 *6919 * This routine DMA memory for Log Write Position Data[LPWD] and buffer6920 * to update FW log is posted to the adapter.6921 * Buffer count is calculated based on module param ras_fwlog_buffsize6922 * Size of each buffer posted to FW is 64K.6923 **/6924 6925static int6926lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,6927			uint32_t fwlog_buff_count)6928{6929	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;6930	struct lpfc_dmabuf *dmabuf;6931	int rc = 0, i = 0;6932 6933	/* Initialize List */6934	INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);6935 6936	/* Allocate memory for the LWPD */6937	ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,6938					    sizeof(uint32_t) * 2,6939					    &ras_fwlog->lwpd.phys,6940					    GFP_KERNEL);6941	if (!ras_fwlog->lwpd.virt) {6942		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,6943				"6185 LWPD Memory Alloc Failed\n");6944 6945		return -ENOMEM;6946	}6947 6948	ras_fwlog->fw_buffcount = fwlog_buff_count;6949	for (i = 0; i < ras_fwlog->fw_buffcount; i++) {6950		dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),6951				 GFP_KERNEL);6952		if (!dmabuf) {6953			rc = -ENOMEM;6954			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,6955					"6186 Memory Alloc failed FW logging");6956			goto free_mem;6957		}6958 6959		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,6960						  LPFC_RAS_MAX_ENTRY_SIZE,6961						  &dmabuf->phys, GFP_KERNEL);6962		if (!dmabuf->virt) {6963			kfree(dmabuf);6964			rc = -ENOMEM;6965			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,6966					"6187 DMA Alloc Failed FW logging");6967			goto free_mem;6968		}6969		dmabuf->buffer_tag = i;6970		list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);6971	}6972 6973free_mem:6974	if (rc)6975		lpfc_sli4_ras_dma_free(phba);6976 6977	return rc;6978}6979 6980/**6981 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command6982 * @phba: pointer to lpfc hba data structure.6983 * @pmb: pointer to the driver internal queue element for mailbox command.6984 *6985 * Completion handler for driver's RAS MBX command to the device.6986 **/6987static void6988lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)6989{6990	MAILBOX_t *mb;6991	union lpfc_sli4_cfg_shdr *shdr;6992	uint32_t shdr_status, shdr_add_status;6993	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;6994 6995	mb = &pmb->u.mb;6996 6997	shdr = (union lpfc_sli4_cfg_shdr *)6998		&pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;6999	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);7000	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);7001 7002	if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {7003		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,7004				"6188 FW LOG mailbox "7005				"completed with status x%x add_status x%x,"7006				" mbx status x%x\n",7007				shdr_status, shdr_add_status, mb->mbxStatus);7008 7009		ras_fwlog->ras_hwsupport = false;7010		goto disable_ras;7011	}7012 7013	spin_lock_irq(&phba->ras_fwlog_lock);7014	ras_fwlog->state = ACTIVE;7015	spin_unlock_irq(&phba->ras_fwlog_lock);7016	mempool_free(pmb, phba->mbox_mem_pool);7017 7018	return;7019 7020disable_ras:7021	/* Free RAS DMA memory */7022	lpfc_sli4_ras_dma_free(phba);7023	mempool_free(pmb, phba->mbox_mem_pool);7024}7025 7026/**7027 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command7028 * @phba: pointer to lpfc hba data structure.7029 * @fwlog_level: Logging verbosity level.7030 * @fwlog_enable: Enable/Disable logging.7031 *7032 * Initialize memory and post mailbox command to enable FW logging in host7033 * memory.7034 **/7035int7036lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,7037			 uint32_t fwlog_level,7038			 uint32_t fwlog_enable)7039{7040	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;7041	struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;7042	struct lpfc_dmabuf *dmabuf;7043	LPFC_MBOXQ_t *mbox;7044	uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;7045	int rc = 0;7046 7047	spin_lock_irq(&phba->ras_fwlog_lock);7048	ras_fwlog->state = INACTIVE;7049	spin_unlock_irq(&phba->ras_fwlog_lock);7050 7051	fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *7052			  phba->cfg_ras_fwlog_buffsize);7053	fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);7054 7055	/*7056	 * If re-enabling FW logging support use earlier allocated7057	 * DMA buffers while posting MBX command.7058	 **/7059	if (!ras_fwlog->lwpd.virt) {7060		rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);7061		if (rc) {7062			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,7063					"6189 FW Log Memory Allocation Failed");7064			return rc;7065		}7066	}7067 7068	/* Setup Mailbox command */7069	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);7070	if (!mbox) {7071		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,7072				"6190 RAS MBX Alloc Failed");7073		rc = -ENOMEM;7074		goto mem_free;7075	}7076 7077	ras_fwlog->fw_loglevel = fwlog_level;7078	len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -7079		sizeof(struct lpfc_sli4_cfg_mhdr));7080 7081	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,7082			 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,7083			 len, LPFC_SLI4_MBX_EMBED);7084 7085	mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;7086	bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,7087	       fwlog_enable);7088	bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,7089	       ras_fwlog->fw_loglevel);7090	bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,7091	       ras_fwlog->fw_buffcount);7092	bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,7093	       LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);7094 7095	/* Update DMA buffer address */7096	list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {7097		memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);7098 7099		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =7100			putPaddrLow(dmabuf->phys);7101 7102		mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =7103			putPaddrHigh(dmabuf->phys);7104	}7105 7106	/* Update LPWD address */7107	mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);7108	mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);7109 7110	spin_lock_irq(&phba->ras_fwlog_lock);7111	ras_fwlog->state = REG_INPROGRESS;7112	spin_unlock_irq(&phba->ras_fwlog_lock);7113	mbox->vport = phba->pport;7114	mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;7115 7116	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);7117 7118	if (rc == MBX_NOT_FINISHED) {7119		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,7120				"6191 FW-Log Mailbox failed. "7121				"status %d mbxStatus : x%x", rc,7122				bf_get(lpfc_mqe_status, &mbox->u.mqe));7123		mempool_free(mbox, phba->mbox_mem_pool);7124		rc = -EIO;7125		goto mem_free;7126	} else7127		rc = 0;7128mem_free:7129	if (rc)7130		lpfc_sli4_ras_dma_free(phba);7131 7132	return rc;7133}7134 7135/**7136 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter7137 * @phba: Pointer to HBA context object.7138 *7139 * Check if RAS is supported on the adapter and initialize it.7140 **/7141void7142lpfc_sli4_ras_setup(struct lpfc_hba *phba)7143{7144	/* Check RAS FW Log needs to be enabled or not */7145	if (lpfc_check_fwlog_support(phba))7146		return;7147 7148	lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,7149				 LPFC_RAS_ENABLE_LOGGING);7150}7151 7152/**7153 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.7154 * @phba: Pointer to HBA context object.7155 *7156 * This function allocates all SLI4 resource identifiers.7157 **/7158int7159lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)7160{7161	int i, rc, error = 0;7162	uint16_t count, base;7163	unsigned long longs;7164 7165	if (!phba->sli4_hba.rpi_hdrs_in_use)7166		phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;7167	if (phba->sli4_hba.extents_in_use) {7168		/*7169		 * The port supports resource extents. The XRI, VPI, VFI, RPI7170		 * resource extent count must be read and allocated before7171		 * provisioning the resource id arrays.7172		 */7173		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==7174		    LPFC_IDX_RSRC_RDY) {7175			/*7176			 * Extent-based resources are set - the driver could7177			 * be in a port reset. Figure out if any corrective7178			 * actions need to be taken.7179			 */7180			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,7181						 LPFC_RSC_TYPE_FCOE_VFI);7182			if (rc != 0)7183				error++;7184			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,7185						 LPFC_RSC_TYPE_FCOE_VPI);7186			if (rc != 0)7187				error++;7188			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,7189						 LPFC_RSC_TYPE_FCOE_XRI);7190			if (rc != 0)7191				error++;7192			rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,7193						 LPFC_RSC_TYPE_FCOE_RPI);7194			if (rc != 0)7195				error++;7196 7197			/*7198			 * It's possible that the number of resources7199			 * provided to this port instance changed between7200			 * resets.  Detect this condition and reallocate7201			 * resources.  Otherwise, there is no action.7202			 */7203			if (error) {7204				lpfc_printf_log(phba, KERN_INFO,7205						LOG_MBOX | LOG_INIT,7206						"2931 Detected extent resource "7207						"change.  Reallocating all "7208						"extents.\n");7209				rc = lpfc_sli4_dealloc_extent(phba,7210						 LPFC_RSC_TYPE_FCOE_VFI);7211				rc = lpfc_sli4_dealloc_extent(phba,7212						 LPFC_RSC_TYPE_FCOE_VPI);7213				rc = lpfc_sli4_dealloc_extent(phba,7214						 LPFC_RSC_TYPE_FCOE_XRI);7215				rc = lpfc_sli4_dealloc_extent(phba,7216						 LPFC_RSC_TYPE_FCOE_RPI);7217			} else7218				return 0;7219		}7220 7221		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);7222		if (unlikely(rc))7223			goto err_exit;7224 7225		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);7226		if (unlikely(rc))7227			goto err_exit;7228 7229		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);7230		if (unlikely(rc))7231			goto err_exit;7232 7233		rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);7234		if (unlikely(rc))7235			goto err_exit;7236		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,7237		       LPFC_IDX_RSRC_RDY);7238		return rc;7239	} else {7240		/*7241		 * The port does not support resource extents.  The XRI, VPI,7242		 * VFI, RPI resource ids were determined from READ_CONFIG.7243		 * Just allocate the bitmasks and provision the resource id7244		 * arrays.  If a port reset is active, the resources don't7245		 * need any action - just exit.7246		 */7247		if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==7248		    LPFC_IDX_RSRC_RDY) {7249			lpfc_sli4_dealloc_resource_identifiers(phba);7250			lpfc_sli4_remove_rpis(phba);7251		}7252		/* RPIs. */7253		count = phba->sli4_hba.max_cfg_param.max_rpi;7254		if (count <= 0) {7255			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,7256					"3279 Invalid provisioning of "7257					"rpi:%d\n", count);7258			rc = -EINVAL;7259			goto err_exit;7260		}7261		base = phba->sli4_hba.max_cfg_param.rpi_base;7262		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;7263		phba->sli4_hba.rpi_bmask = kcalloc(longs,7264						   sizeof(unsigned long),7265						   GFP_KERNEL);7266		if (unlikely(!phba->sli4_hba.rpi_bmask)) {7267			rc = -ENOMEM;7268			goto err_exit;7269		}7270		phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),7271						 GFP_KERNEL);7272		if (unlikely(!phba->sli4_hba.rpi_ids)) {7273			rc = -ENOMEM;7274			goto free_rpi_bmask;7275		}7276 7277		for (i = 0; i < count; i++)7278			phba->sli4_hba.rpi_ids[i] = base + i;7279 7280		/* VPIs. */7281		count = phba->sli4_hba.max_cfg_param.max_vpi;7282		if (count <= 0) {7283			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,7284					"3280 Invalid provisioning of "7285					"vpi:%d\n", count);7286			rc = -EINVAL;7287			goto free_rpi_ids;7288		}7289		base = phba->sli4_hba.max_cfg_param.vpi_base;7290		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;7291		phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),7292					  GFP_KERNEL);7293		if (unlikely(!phba->vpi_bmask)) {7294			rc = -ENOMEM;7295			goto free_rpi_ids;7296		}7297		phba->vpi_ids = kcalloc(count, sizeof(uint16_t),7298					GFP_KERNEL);7299		if (unlikely(!phba->vpi_ids)) {7300			rc = -ENOMEM;7301			goto free_vpi_bmask;7302		}7303 7304		for (i = 0; i < count; i++)7305			phba->vpi_ids[i] = base + i;7306 7307		/* XRIs. */7308		count = phba->sli4_hba.max_cfg_param.max_xri;7309		if (count <= 0) {7310			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,7311					"3281 Invalid provisioning of "7312					"xri:%d\n", count);7313			rc = -EINVAL;7314			goto free_vpi_ids;7315		}7316		base = phba->sli4_hba.max_cfg_param.xri_base;7317		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;7318		phba->sli4_hba.xri_bmask = kcalloc(longs,7319						   sizeof(unsigned long),7320						   GFP_KERNEL);7321		if (unlikely(!phba->sli4_hba.xri_bmask)) {7322			rc = -ENOMEM;7323			goto free_vpi_ids;7324		}7325		phba->sli4_hba.max_cfg_param.xri_used = 0;7326		phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),7327						 GFP_KERNEL);7328		if (unlikely(!phba->sli4_hba.xri_ids)) {7329			rc = -ENOMEM;7330			goto free_xri_bmask;7331		}7332 7333		for (i = 0; i < count; i++)7334			phba->sli4_hba.xri_ids[i] = base + i;7335 7336		/* VFIs. */7337		count = phba->sli4_hba.max_cfg_param.max_vfi;7338		if (count <= 0) {7339			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,7340					"3282 Invalid provisioning of "7341					"vfi:%d\n", count);7342			rc = -EINVAL;7343			goto free_xri_ids;7344		}7345		base = phba->sli4_hba.max_cfg_param.vfi_base;7346		longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;7347		phba->sli4_hba.vfi_bmask = kcalloc(longs,7348						   sizeof(unsigned long),7349						   GFP_KERNEL);7350		if (unlikely(!phba->sli4_hba.vfi_bmask)) {7351			rc = -ENOMEM;7352			goto free_xri_ids;7353		}7354		phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),7355						 GFP_KERNEL);7356		if (unlikely(!phba->sli4_hba.vfi_ids)) {7357			rc = -ENOMEM;7358			goto free_vfi_bmask;7359		}7360 7361		for (i = 0; i < count; i++)7362			phba->sli4_hba.vfi_ids[i] = base + i;7363 7364		/*7365		 * Mark all resources ready.  An HBA reset doesn't need7366		 * to reset the initialization.7367		 */7368		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,7369		       LPFC_IDX_RSRC_RDY);7370		return 0;7371	}7372 7373 free_vfi_bmask:7374	kfree(phba->sli4_hba.vfi_bmask);7375	phba->sli4_hba.vfi_bmask = NULL;7376 free_xri_ids:7377	kfree(phba->sli4_hba.xri_ids);7378	phba->sli4_hba.xri_ids = NULL;7379 free_xri_bmask:7380	kfree(phba->sli4_hba.xri_bmask);7381	phba->sli4_hba.xri_bmask = NULL;7382 free_vpi_ids:7383	kfree(phba->vpi_ids);7384	phba->vpi_ids = NULL;7385 free_vpi_bmask:7386	kfree(phba->vpi_bmask);7387	phba->vpi_bmask = NULL;7388 free_rpi_ids:7389	kfree(phba->sli4_hba.rpi_ids);7390	phba->sli4_hba.rpi_ids = NULL;7391 free_rpi_bmask:7392	kfree(phba->sli4_hba.rpi_bmask);7393	phba->sli4_hba.rpi_bmask = NULL;7394 err_exit:7395	return rc;7396}7397 7398/**7399 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.7400 * @phba: Pointer to HBA context object.7401 *7402 * This function allocates the number of elements for the specified7403 * resource type.7404 **/7405int7406lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)7407{7408	if (phba->sli4_hba.extents_in_use) {7409		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);7410		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);7411		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);7412		lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);7413	} else {7414		kfree(phba->vpi_bmask);7415		phba->sli4_hba.max_cfg_param.vpi_used = 0;7416		kfree(phba->vpi_ids);7417		bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);7418		kfree(phba->sli4_hba.xri_bmask);7419		kfree(phba->sli4_hba.xri_ids);7420		kfree(phba->sli4_hba.vfi_bmask);7421		kfree(phba->sli4_hba.vfi_ids);7422		bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);7423		bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);7424	}7425 7426	return 0;7427}7428 7429/**7430 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.7431 * @phba: Pointer to HBA context object.7432 * @type: The resource extent type.7433 * @extnt_cnt: buffer to hold port extent count response7434 * @extnt_size: buffer to hold port extent size response.7435 *7436 * This function calls the port to read the host allocated extents7437 * for a particular type.7438 **/7439int7440lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,7441			       uint16_t *extnt_cnt, uint16_t *extnt_size)7442{7443	bool emb;7444	int rc = 0;7445	uint16_t curr_blks = 0;7446	uint32_t req_len, emb_len;7447	uint32_t alloc_len, mbox_tmo;7448	struct list_head *blk_list_head;7449	struct lpfc_rsrc_blks *rsrc_blk;7450	LPFC_MBOXQ_t *mbox;7451	void *virtaddr = NULL;7452	struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;7453	struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;7454	union  lpfc_sli4_cfg_shdr *shdr;7455 7456	switch (type) {7457	case LPFC_RSC_TYPE_FCOE_VPI:7458		blk_list_head = &phba->lpfc_vpi_blk_list;7459		break;7460	case LPFC_RSC_TYPE_FCOE_XRI:7461		blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;7462		break;7463	case LPFC_RSC_TYPE_FCOE_VFI:7464		blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;7465		break;7466	case LPFC_RSC_TYPE_FCOE_RPI:7467		blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;7468		break;7469	default:7470		return -EIO;7471	}7472 7473	/* Count the number of extents currently allocatd for this type. */7474	list_for_each_entry(rsrc_blk, blk_list_head, list) {7475		if (curr_blks == 0) {7476			/*7477			 * The GET_ALLOCATED mailbox does not return the size,7478			 * just the count.  The size should be just the size7479			 * stored in the current allocated block and all sizes7480			 * for an extent type are the same so set the return7481			 * value now.7482			 */7483			*extnt_size = rsrc_blk->rsrc_size;7484		}7485		curr_blks++;7486	}7487 7488	/*7489	 * Calculate the size of an embedded mailbox.  The uint32_t7490	 * accounts for extents-specific word.7491	 */7492	emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -7493		sizeof(uint32_t);7494 7495	/*7496	 * Presume the allocation and response will fit into an embedded7497	 * mailbox.  If not true, reconfigure to a non-embedded mailbox.7498	 */7499	emb = LPFC_SLI4_MBX_EMBED;7500	req_len = emb_len;7501	if (req_len > emb_len) {7502		req_len = curr_blks * sizeof(uint16_t) +7503			sizeof(union lpfc_sli4_cfg_shdr) +7504			sizeof(uint32_t);7505		emb = LPFC_SLI4_MBX_NEMBED;7506	}7507 7508	mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);7509	if (!mbox)7510		return -ENOMEM;7511	memset(mbox, 0, sizeof(LPFC_MBOXQ_t));7512 7513	alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,7514				     LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,7515				     req_len, emb);7516	if (alloc_len < req_len) {7517		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,7518			"2983 Allocated DMA memory size (x%x) is "7519			"less than the requested DMA memory "7520			"size (x%x)\n", alloc_len, req_len);7521		rc = -ENOMEM;7522		goto err_exit;7523	}7524	rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);7525	if (unlikely(rc)) {7526		rc = -EIO;7527		goto err_exit;7528	}7529 7530	if (!phba->sli4_hba.intr_enable)7531		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);7532	else {7533		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);7534		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);7535	}7536 7537	if (unlikely(rc)) {7538		rc = -EIO;7539		goto err_exit;7540	}7541 7542	/*7543	 * Figure out where the response is located.  Then get local pointers7544	 * to the response data.  The port does not guarantee to respond to7545	 * all extents counts request so update the local variable with the7546	 * allocated count from the port.7547	 */7548	if (emb == LPFC_SLI4_MBX_EMBED) {7549		rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;7550		shdr = &rsrc_ext->header.cfg_shdr;7551		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);7552	} else {7553		virtaddr = mbox->sge_array->addr[0];7554		n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;7555		shdr = &n_rsrc->cfg_shdr;7556		*extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);7557	}7558 7559	if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {7560		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,7561			"2984 Failed to read allocated resources "7562			"for type %d - Status 0x%x Add'l Status 0x%x.\n",7563			type,7564			bf_get(lpfc_mbox_hdr_status, &shdr->response),7565			bf_get(lpfc_mbox_hdr_add_status, &shdr->response));7566		rc = -EIO;7567		goto err_exit;7568	}7569 err_exit:7570	lpfc_sli4_mbox_cmd_free(phba, mbox);7571	return rc;7572}7573 7574/**7575 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block7576 * @phba: pointer to lpfc hba data structure.7577 * @sgl_list: linked link of sgl buffers to post7578 * @cnt: number of linked list buffers7579 *7580 * This routine walks the list of buffers that have been allocated and7581 * repost them to the port by using SGL block post. This is needed after a7582 * pci_function_reset/warm_start or start. It attempts to construct blocks7583 * of buffer sgls which contains contiguous xris and uses the non-embedded7584 * SGL block post mailbox commands to post them to the port. For single7585 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post7586 * mailbox command for posting.7587 *7588 * Returns: 0 = success, non-zero failure.7589 **/7590static int7591lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,7592			  struct list_head *sgl_list, int cnt)7593{7594	struct lpfc_sglq *sglq_entry = NULL;7595	struct lpfc_sglq *sglq_entry_next = NULL;7596	struct lpfc_sglq *sglq_entry_first = NULL;7597	int status = 0, total_cnt;7598	int post_cnt = 0, num_posted = 0, block_cnt = 0;7599	int last_xritag = NO_XRI;7600	LIST_HEAD(prep_sgl_list);7601	LIST_HEAD(blck_sgl_list);7602	LIST_HEAD(allc_sgl_list);7603	LIST_HEAD(post_sgl_list);7604	LIST_HEAD(free_sgl_list);7605 7606	spin_lock_irq(&phba->hbalock);7607	spin_lock(&phba->sli4_hba.sgl_list_lock);7608	list_splice_init(sgl_list, &allc_sgl_list);7609	spin_unlock(&phba->sli4_hba.sgl_list_lock);7610	spin_unlock_irq(&phba->hbalock);7611 7612	total_cnt = cnt;7613	list_for_each_entry_safe(sglq_entry, sglq_entry_next,7614				 &allc_sgl_list, list) {7615		list_del_init(&sglq_entry->list);7616		block_cnt++;7617		if ((last_xritag != NO_XRI) &&7618		    (sglq_entry->sli4_xritag != last_xritag + 1)) {7619			/* a hole in xri block, form a sgl posting block */7620			list_splice_init(&prep_sgl_list, &blck_sgl_list);7621			post_cnt = block_cnt - 1;7622			/* prepare list for next posting block */7623			list_add_tail(&sglq_entry->list, &prep_sgl_list);7624			block_cnt = 1;7625		} else {7626			/* prepare list for next posting block */7627			list_add_tail(&sglq_entry->list, &prep_sgl_list);7628			/* enough sgls for non-embed sgl mbox command */7629			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {7630				list_splice_init(&prep_sgl_list,7631						 &blck_sgl_list);7632				post_cnt = block_cnt;7633				block_cnt = 0;7634			}7635		}7636		num_posted++;7637 7638		/* keep track of last sgl's xritag */7639		last_xritag = sglq_entry->sli4_xritag;7640 7641		/* end of repost sgl list condition for buffers */7642		if (num_posted == total_cnt) {7643			if (post_cnt == 0) {7644				list_splice_init(&prep_sgl_list,7645						 &blck_sgl_list);7646				post_cnt = block_cnt;7647			} else if (block_cnt == 1) {7648				status = lpfc_sli4_post_sgl(phba,7649						sglq_entry->phys, 0,7650						sglq_entry->sli4_xritag);7651				if (!status) {7652					/* successful, put sgl to posted list */7653					list_add_tail(&sglq_entry->list,7654						      &post_sgl_list);7655				} else {7656					/* Failure, put sgl to free list */7657					lpfc_printf_log(phba, KERN_WARNING,7658						LOG_SLI,7659						"3159 Failed to post "7660						"sgl, xritag:x%x\n",7661						sglq_entry->sli4_xritag);7662					list_add_tail(&sglq_entry->list,7663						      &free_sgl_list);7664					total_cnt--;7665				}7666			}7667		}7668 7669		/* continue until a nembed page worth of sgls */7670		if (post_cnt == 0)7671			continue;7672 7673		/* post the buffer list sgls as a block */7674		status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,7675						 post_cnt);7676 7677		if (!status) {7678			/* success, put sgl list to posted sgl list */7679			list_splice_init(&blck_sgl_list, &post_sgl_list);7680		} else {7681			/* Failure, put sgl list to free sgl list */7682			sglq_entry_first = list_first_entry(&blck_sgl_list,7683							    struct lpfc_sglq,7684							    list);7685			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,7686					"3160 Failed to post sgl-list, "7687					"xritag:x%x-x%x\n",7688					sglq_entry_first->sli4_xritag,7689					(sglq_entry_first->sli4_xritag +7690					 post_cnt - 1));7691			list_splice_init(&blck_sgl_list, &free_sgl_list);7692			total_cnt -= post_cnt;7693		}7694 7695		/* don't reset xirtag due to hole in xri block */7696		if (block_cnt == 0)7697			last_xritag = NO_XRI;7698 7699		/* reset sgl post count for next round of posting */7700		post_cnt = 0;7701	}7702 7703	/* free the sgls failed to post */7704	lpfc_free_sgl_list(phba, &free_sgl_list);7705 7706	/* push sgls posted to the available list */7707	if (!list_empty(&post_sgl_list)) {7708		spin_lock_irq(&phba->hbalock);7709		spin_lock(&phba->sli4_hba.sgl_list_lock);7710		list_splice_init(&post_sgl_list, sgl_list);7711		spin_unlock(&phba->sli4_hba.sgl_list_lock);7712		spin_unlock_irq(&phba->hbalock);7713	} else {7714		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,7715				"3161 Failure to post sgl to port,status %x "7716				"blkcnt %d totalcnt %d postcnt %d\n",7717				status, block_cnt, total_cnt, post_cnt);7718		return -EIO;7719	}7720 7721	/* return the number of XRIs actually posted */7722	return total_cnt;7723}7724 7725/**7726 * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls7727 * @phba: pointer to lpfc hba data structure.7728 *7729 * This routine walks the list of nvme buffers that have been allocated and7730 * repost them to the port by using SGL block post. This is needed after a7731 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine7732 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list7733 * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.7734 *7735 * Returns: 0 = success, non-zero failure.7736 **/7737static int7738lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)7739{7740	LIST_HEAD(post_nblist);7741	int num_posted, rc = 0;7742 7743	/* get all NVME buffers need to repost to a local list */7744	lpfc_io_buf_flush(phba, &post_nblist);7745 7746	/* post the list of nvme buffer sgls to port if available */7747	if (!list_empty(&post_nblist)) {7748		num_posted = lpfc_sli4_post_io_sgl_list(7749			phba, &post_nblist, phba->sli4_hba.io_xri_cnt);7750		/* failed to post any nvme buffer, return error */7751		if (num_posted == 0)7752			rc = -EIO;7753	}7754	return rc;7755}7756 7757static void7758lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)7759{7760	uint32_t len;7761 7762	len = sizeof(struct lpfc_mbx_set_host_data) -7763		sizeof(struct lpfc_sli4_cfg_mhdr);7764	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,7765			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,7766			 LPFC_SLI4_MBX_EMBED);7767 7768	mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;7769	mbox->u.mqe.un.set_host_data.param_len =7770					LPFC_HOST_OS_DRIVER_VERSION_SIZE;7771	snprintf(mbox->u.mqe.un.set_host_data.un.data,7772		 LPFC_HOST_OS_DRIVER_VERSION_SIZE,7773		 "Linux %s v"LPFC_DRIVER_VERSION,7774		 test_bit(HBA_FCOE_MODE, &phba->hba_flag) ? "FCoE" : "FC");7775}7776 7777int7778lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,7779		    struct lpfc_queue *drq, int count, int idx)7780{7781	int rc, i;7782	struct lpfc_rqe hrqe;7783	struct lpfc_rqe drqe;7784	struct lpfc_rqb *rqbp;7785	unsigned long flags;7786	struct rqb_dmabuf *rqb_buffer;7787	LIST_HEAD(rqb_buf_list);7788 7789	rqbp = hrq->rqbp;7790	for (i = 0; i < count; i++) {7791		spin_lock_irqsave(&phba->hbalock, flags);7792		/* IF RQ is already full, don't bother */7793		if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {7794			spin_unlock_irqrestore(&phba->hbalock, flags);7795			break;7796		}7797		spin_unlock_irqrestore(&phba->hbalock, flags);7798 7799		rqb_buffer = rqbp->rqb_alloc_buffer(phba);7800		if (!rqb_buffer)7801			break;7802		rqb_buffer->hrq = hrq;7803		rqb_buffer->drq = drq;7804		rqb_buffer->idx = idx;7805		list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);7806	}7807 7808	spin_lock_irqsave(&phba->hbalock, flags);7809	while (!list_empty(&rqb_buf_list)) {7810		list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,7811				 hbuf.list);7812 7813		hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);7814		hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);7815		drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);7816		drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);7817		rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);7818		if (rc < 0) {7819			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,7820					"6421 Cannot post to HRQ %d: %x %x %x "7821					"DRQ %x %x\n",7822					hrq->queue_id,7823					hrq->host_index,7824					hrq->hba_index,7825					hrq->entry_count,7826					drq->host_index,7827					drq->hba_index);7828			rqbp->rqb_free_buffer(phba, rqb_buffer);7829		} else {7830			list_add_tail(&rqb_buffer->hbuf.list,7831				      &rqbp->rqb_buffer_list);7832			rqbp->buffer_count++;7833		}7834	}7835	spin_unlock_irqrestore(&phba->hbalock, flags);7836	return 1;7837}7838 7839static void7840lpfc_mbx_cmpl_read_lds_params(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)7841{7842	union lpfc_sli4_cfg_shdr *shdr;7843	u32 shdr_status, shdr_add_status;7844 7845	shdr = (union lpfc_sli4_cfg_shdr *)7846		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;7847	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);7848	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);7849	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {7850		lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT | LOG_MBOX,7851				"4622 SET_FEATURE (x%x) mbox failed, "7852				"status x%x add_status x%x, mbx status x%x\n",7853				LPFC_SET_LD_SIGNAL, shdr_status,7854				shdr_add_status, pmb->u.mb.mbxStatus);7855		phba->degrade_activate_threshold = 0;7856		phba->degrade_deactivate_threshold = 0;7857		phba->fec_degrade_interval = 0;7858		goto out;7859	}7860 7861	phba->degrade_activate_threshold = pmb->u.mqe.un.set_feature.word7;7862	phba->degrade_deactivate_threshold = pmb->u.mqe.un.set_feature.word8;7863	phba->fec_degrade_interval = pmb->u.mqe.un.set_feature.word10;7864 7865	lpfc_printf_log(phba, KERN_INFO, LOG_LDS_EVENT,7866			"4624 Success: da x%x dd x%x interval x%x\n",7867			phba->degrade_activate_threshold,7868			phba->degrade_deactivate_threshold,7869			phba->fec_degrade_interval);7870out:7871	mempool_free(pmb, phba->mbox_mem_pool);7872}7873 7874int7875lpfc_read_lds_params(struct lpfc_hba *phba)7876{7877	LPFC_MBOXQ_t *mboxq;7878	int rc;7879 7880	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);7881	if (!mboxq)7882		return -ENOMEM;7883 7884	lpfc_set_features(phba, mboxq, LPFC_SET_LD_SIGNAL);7885	mboxq->vport = phba->pport;7886	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_lds_params;7887	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);7888	if (rc == MBX_NOT_FINISHED) {7889		mempool_free(mboxq, phba->mbox_mem_pool);7890		return -EIO;7891	}7892	return 0;7893}7894 7895static void7896lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)7897{7898	struct lpfc_vport *vport = pmb->vport;7899	union lpfc_sli4_cfg_shdr *shdr;7900	u32 shdr_status, shdr_add_status;7901	u32 sig, acqe;7902 7903	/* Two outcomes. (1) Set featurs was successul and EDC negotiation7904	 * is done. (2) Mailbox failed and send FPIN support only.7905	 */7906	shdr = (union lpfc_sli4_cfg_shdr *)7907		&pmb->u.mqe.un.sli4_config.header.cfg_shdr;7908	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);7909	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);7910	if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {7911		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,7912				"2516 CGN SET_FEATURE mbox failed with "7913				"status x%x add_status x%x, mbx status x%x "7914				"Reset Congestion to FPINs only\n",7915				shdr_status, shdr_add_status,7916				pmb->u.mb.mbxStatus);7917		/* If there is a mbox error, move on to RDF */7918		phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;7919		phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;7920		goto out;7921	}7922 7923	/* Zero out Congestion Signal ACQE counter */7924	phba->cgn_acqe_cnt = 0;7925 7926	acqe = bf_get(lpfc_mbx_set_feature_CGN_acqe_freq,7927		      &pmb->u.mqe.un.set_feature);7928	sig = bf_get(lpfc_mbx_set_feature_CGN_warn_freq,7929		     &pmb->u.mqe.un.set_feature);7930	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,7931			"4620 SET_FEATURES Success: Freq: %ds %dms "7932			" Reg: x%x x%x\n", acqe, sig,7933			phba->cgn_reg_signal, phba->cgn_reg_fpin);7934out:7935	mempool_free(pmb, phba->mbox_mem_pool);7936 7937	/* Register for FPIN events from the fabric now that the7938	 * EDC common_set_features has completed.7939	 */7940	lpfc_issue_els_rdf(vport, 0);7941}7942 7943int7944lpfc_config_cgn_signal(struct lpfc_hba *phba)7945{7946	LPFC_MBOXQ_t *mboxq;7947	u32 rc;7948 7949	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);7950	if (!mboxq)7951		goto out_rdf;7952 7953	lpfc_set_features(phba, mboxq, LPFC_SET_CGN_SIGNAL);7954	mboxq->vport = phba->pport;7955	mboxq->mbox_cmpl = lpfc_mbx_cmpl_cgn_set_ftrs;7956 7957	lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,7958			"4621 SET_FEATURES: FREQ sig x%x acqe x%x: "7959			"Reg: x%x x%x\n",7960			phba->cgn_sig_freq, lpfc_acqe_cgn_frequency,7961			phba->cgn_reg_signal, phba->cgn_reg_fpin);7962 7963	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);7964	if (rc == MBX_NOT_FINISHED)7965		goto out;7966	return 0;7967 7968out:7969	mempool_free(mboxq, phba->mbox_mem_pool);7970out_rdf:7971	/* If there is a mbox error, move on to RDF */7972	phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;7973	phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;7974	lpfc_issue_els_rdf(phba->pport, 0);7975	return -EIO;7976}7977 7978/**7979 * lpfc_init_idle_stat_hb - Initialize idle_stat tracking7980 * @phba: pointer to lpfc hba data structure.7981 *7982 * This routine initializes the per-eq idle_stat to dynamically dictate7983 * polling decisions.7984 *7985 * Return codes:7986 *   None7987 **/7988static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)7989{7990	int i;7991	struct lpfc_sli4_hdw_queue *hdwq;7992	struct lpfc_queue *eq;7993	struct lpfc_idle_stat *idle_stat;7994	u64 wall;7995 7996	for_each_present_cpu(i) {7997		hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];7998		eq = hdwq->hba_eq;7999 8000		/* Skip if we've already handled this eq's primary CPU */8001		if (eq->chann != i)8002			continue;8003 8004		idle_stat = &phba->sli4_hba.idle_stat[i];8005 8006		idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);8007		idle_stat->prev_wall = wall;8008 8009		if (phba->nvmet_support ||8010		    phba->cmf_active_mode != LPFC_CFG_OFF ||8011		    phba->intr_type != MSIX)8012			eq->poll_mode = LPFC_QUEUE_WORK;8013		else8014			eq->poll_mode = LPFC_THREADED_IRQ;8015	}8016 8017	if (!phba->nvmet_support && phba->intr_type == MSIX)8018		schedule_delayed_work(&phba->idle_stat_delay_work,8019				      msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));8020}8021 8022static void lpfc_sli4_dip(struct lpfc_hba *phba)8023{8024	uint32_t if_type;8025 8026	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);8027	if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||8028	    if_type == LPFC_SLI_INTF_IF_TYPE_6) {8029		struct lpfc_register reg_data;8030 8031		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,8032			       &reg_data.word0))8033			return;8034 8035		if (bf_get(lpfc_sliport_status_dip, &reg_data))8036			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,8037					"2904 Firmware Dump Image Present"8038					" on Adapter");8039	}8040}8041 8042/**8043 * lpfc_rx_monitor_create_ring - Initialize ring buffer for rx_monitor8044 * @rx_monitor: Pointer to lpfc_rx_info_monitor object8045 * @entries: Number of rx_info_entry objects to allocate in ring8046 *8047 * Return:8048 * 0 - Success8049 * ENOMEM - Failure to kmalloc8050 **/8051int lpfc_rx_monitor_create_ring(struct lpfc_rx_info_monitor *rx_monitor,8052				u32 entries)8053{8054	rx_monitor->ring = kmalloc_array(entries, sizeof(struct rx_info_entry),8055					 GFP_KERNEL);8056	if (!rx_monitor->ring)8057		return -ENOMEM;8058 8059	rx_monitor->head_idx = 0;8060	rx_monitor->tail_idx = 0;8061	spin_lock_init(&rx_monitor->lock);8062	rx_monitor->entries = entries;8063 8064	return 0;8065}8066 8067/**8068 * lpfc_rx_monitor_destroy_ring - Free ring buffer for rx_monitor8069 * @rx_monitor: Pointer to lpfc_rx_info_monitor object8070 *8071 * Called after cancellation of cmf_timer.8072 **/8073void lpfc_rx_monitor_destroy_ring(struct lpfc_rx_info_monitor *rx_monitor)8074{8075	kfree(rx_monitor->ring);8076	rx_monitor->ring = NULL;8077	rx_monitor->entries = 0;8078	rx_monitor->head_idx = 0;8079	rx_monitor->tail_idx = 0;8080}8081 8082/**8083 * lpfc_rx_monitor_record - Insert an entry into rx_monitor's ring8084 * @rx_monitor: Pointer to lpfc_rx_info_monitor object8085 * @entry: Pointer to rx_info_entry8086 *8087 * Used to insert an rx_info_entry into rx_monitor's ring.  Note that this is a8088 * deep copy of rx_info_entry not a shallow copy of the rx_info_entry ptr.8089 *8090 * This is called from lpfc_cmf_timer, which is in timer/softirq context.8091 *8092 * In cases of old data overflow, we do a best effort of FIFO order.8093 **/8094void lpfc_rx_monitor_record(struct lpfc_rx_info_monitor *rx_monitor,8095			    struct rx_info_entry *entry)8096{8097	struct rx_info_entry *ring = rx_monitor->ring;8098	u32 *head_idx = &rx_monitor->head_idx;8099	u32 *tail_idx = &rx_monitor->tail_idx;8100	spinlock_t *ring_lock = &rx_monitor->lock;8101	u32 ring_size = rx_monitor->entries;8102 8103	spin_lock(ring_lock);8104	memcpy(&ring[*tail_idx], entry, sizeof(*entry));8105	*tail_idx = (*tail_idx + 1) % ring_size;8106 8107	/* Best effort of FIFO saved data */8108	if (*tail_idx == *head_idx)8109		*head_idx = (*head_idx + 1) % ring_size;8110 8111	spin_unlock(ring_lock);8112}8113 8114/**8115 * lpfc_rx_monitor_report - Read out rx_monitor's ring8116 * @phba: Pointer to lpfc_hba object8117 * @rx_monitor: Pointer to lpfc_rx_info_monitor object8118 * @buf: Pointer to char buffer that will contain rx monitor info data8119 * @buf_len: Length buf including null char8120 * @max_read_entries: Maximum number of entries to read out of ring8121 *8122 * Used to dump/read what's in rx_monitor's ring buffer.8123 *8124 * If buf is NULL || buf_len == 0, then it is implied that we want to log the8125 * information to kmsg instead of filling out buf.8126 *8127 * Return:8128 * Number of entries read out of the ring8129 **/8130u32 lpfc_rx_monitor_report(struct lpfc_hba *phba,8131			   struct lpfc_rx_info_monitor *rx_monitor, char *buf,8132			   u32 buf_len, u32 max_read_entries)8133{8134	struct rx_info_entry *ring = rx_monitor->ring;8135	struct rx_info_entry *entry;8136	u32 *head_idx = &rx_monitor->head_idx;8137	u32 *tail_idx = &rx_monitor->tail_idx;8138	spinlock_t *ring_lock = &rx_monitor->lock;8139	u32 ring_size = rx_monitor->entries;8140	u32 cnt = 0;8141	char tmp[DBG_LOG_STR_SZ] = {0};8142	bool log_to_kmsg = (!buf || !buf_len) ? true : false;8143 8144	if (!log_to_kmsg) {8145		/* clear the buffer to be sure */8146		memset(buf, 0, buf_len);8147 8148		scnprintf(buf, buf_len, "\t%-16s%-16s%-16s%-16s%-8s%-8s%-8s"8149					"%-8s%-8s%-8s%-16s\n",8150					"MaxBPI", "Tot_Data_CMF",8151					"Tot_Data_Cmd", "Tot_Data_Cmpl",8152					"Lat(us)", "Avg_IO", "Max_IO", "Bsy",8153					"IO_cnt", "Info", "BWutil(ms)");8154	}8155 8156	/* Needs to be _irq because record is called from timer interrupt8157	 * context8158	 */8159	spin_lock_irq(ring_lock);8160	while (*head_idx != *tail_idx) {8161		entry = &ring[*head_idx];8162 8163		/* Read out this entry's data. */8164		if (!log_to_kmsg) {8165			/* If !log_to_kmsg, then store to buf. */8166			scnprintf(tmp, sizeof(tmp),8167				  "%03d:\t%-16llu%-16llu%-16llu%-16llu%-8llu"8168				  "%-8llu%-8llu%-8u%-8u%-8u%u(%u)\n",8169				  *head_idx, entry->max_bytes_per_interval,8170				  entry->cmf_bytes, entry->total_bytes,8171				  entry->rcv_bytes, entry->avg_io_latency,8172				  entry->avg_io_size, entry->max_read_cnt,8173				  entry->cmf_busy, entry->io_cnt,8174				  entry->cmf_info, entry->timer_utilization,8175				  entry->timer_interval);8176 8177			/* Check for buffer overflow */8178			if ((strlen(buf) + strlen(tmp)) >= buf_len)8179				break;8180 8181			/* Append entry's data to buffer */8182			strlcat(buf, tmp, buf_len);8183		} else {8184			lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,8185					"4410 %02u: MBPI %llu Xmit %llu "8186					"Cmpl %llu Lat %llu ASz %llu Info %02u "8187					"BWUtil %u Int %u slot %u\n",8188					cnt, entry->max_bytes_per_interval,8189					entry->total_bytes, entry->rcv_bytes,8190					entry->avg_io_latency,8191					entry->avg_io_size, entry->cmf_info,8192					entry->timer_utilization,8193					entry->timer_interval, *head_idx);8194		}8195 8196		*head_idx = (*head_idx + 1) % ring_size;8197 8198		/* Don't feed more than max_read_entries */8199		cnt++;8200		if (cnt >= max_read_entries)8201			break;8202	}8203	spin_unlock_irq(ring_lock);8204 8205	return cnt;8206}8207 8208/**8209 * lpfc_cmf_setup - Initialize idle_stat tracking8210 * @phba: Pointer to HBA context object.8211 *8212 * This is called from HBA setup during driver load or when the HBA8213 * comes online. this does all the initialization to support CMF and MI.8214 **/8215static int8216lpfc_cmf_setup(struct lpfc_hba *phba)8217{8218	LPFC_MBOXQ_t *mboxq;8219	struct lpfc_dmabuf *mp;8220	struct lpfc_pc_sli4_params *sli4_params;8221	int rc, cmf, mi_ver;8222 8223	rc = lpfc_sli4_refresh_params(phba);8224	if (unlikely(rc))8225		return rc;8226 8227	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);8228	if (!mboxq)8229		return -ENOMEM;8230 8231	sli4_params = &phba->sli4_hba.pc_sli4_params;8232 8233	/* Always try to enable MI feature if we can */8234	if (sli4_params->mi_ver) {8235		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_MI);8236		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);8237		mi_ver = bf_get(lpfc_mbx_set_feature_mi,8238				 &mboxq->u.mqe.un.set_feature);8239 8240		if (rc == MBX_SUCCESS) {8241			if (mi_ver) {8242				lpfc_printf_log(phba,8243						KERN_WARNING, LOG_CGN_MGMT,8244						"6215 MI is enabled\n");8245				sli4_params->mi_ver = mi_ver;8246			} else {8247				lpfc_printf_log(phba,8248						KERN_WARNING, LOG_CGN_MGMT,8249						"6338 MI is disabled\n");8250				sli4_params->mi_ver = 0;8251			}8252		} else {8253			/* mi_ver is already set from GET_SLI4_PARAMETERS */8254			lpfc_printf_log(phba, KERN_INFO,8255					LOG_CGN_MGMT | LOG_INIT,8256					"6245 Enable MI Mailbox x%x (x%x/x%x) "8257					"failed, rc:x%x mi:x%x\n",8258					bf_get(lpfc_mqe_command, &mboxq->u.mqe),8259					lpfc_sli_config_mbox_subsys_get8260						(phba, mboxq),8261					lpfc_sli_config_mbox_opcode_get8262						(phba, mboxq),8263					rc, sli4_params->mi_ver);8264		}8265	} else {8266		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,8267				"6217 MI is disabled\n");8268	}8269 8270	/* Ensure FDMI is enabled for MI if enable_mi is set */8271	if (sli4_params->mi_ver)8272		phba->cfg_fdmi_on = LPFC_FDMI_SUPPORT;8273 8274	/* Always try to enable CMF feature if we can */8275	if (sli4_params->cmf) {8276		lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_CMF);8277		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);8278		cmf = bf_get(lpfc_mbx_set_feature_cmf,8279			     &mboxq->u.mqe.un.set_feature);8280		if (rc == MBX_SUCCESS && cmf) {8281			lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,8282					"6218 CMF is enabled: mode %d\n",8283					phba->cmf_active_mode);8284		} else {8285			lpfc_printf_log(phba, KERN_WARNING,8286					LOG_CGN_MGMT | LOG_INIT,8287					"6219 Enable CMF Mailbox x%x (x%x/x%x) "8288					"failed, rc:x%x dd:x%x\n",8289					bf_get(lpfc_mqe_command, &mboxq->u.mqe),8290					lpfc_sli_config_mbox_subsys_get8291						(phba, mboxq),8292					lpfc_sli_config_mbox_opcode_get8293						(phba, mboxq),8294					rc, cmf);8295			sli4_params->cmf = 0;8296			phba->cmf_active_mode = LPFC_CFG_OFF;8297			goto no_cmf;8298		}8299 8300		/* Allocate Congestion Information Buffer */8301		if (!phba->cgn_i) {8302			mp = kmalloc(sizeof(*mp), GFP_KERNEL);8303			if (mp)8304				mp->virt = dma_alloc_coherent8305						(&phba->pcidev->dev,8306						sizeof(struct lpfc_cgn_info),8307						&mp->phys, GFP_KERNEL);8308			if (!mp || !mp->virt) {8309				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,8310						"2640 Failed to alloc memory "8311						"for Congestion Info\n");8312				kfree(mp);8313				sli4_params->cmf = 0;8314				phba->cmf_active_mode = LPFC_CFG_OFF;8315				goto no_cmf;8316			}8317			phba->cgn_i = mp;8318 8319			/* initialize congestion buffer info */8320			lpfc_init_congestion_buf(phba);8321			lpfc_init_congestion_stat(phba);8322 8323			/* Zero out Congestion Signal counters */8324			atomic64_set(&phba->cgn_acqe_stat.alarm, 0);8325			atomic64_set(&phba->cgn_acqe_stat.warn, 0);8326		}8327 8328		rc = lpfc_sli4_cgn_params_read(phba);8329		if (rc < 0) {8330			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,8331					"6242 Error reading Cgn Params (%d)\n",8332					rc);8333			/* Ensure CGN Mode is off */8334			sli4_params->cmf = 0;8335		} else if (!rc) {8336			lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,8337					"6243 CGN Event empty object.\n");8338			/* Ensure CGN Mode is off */8339			sli4_params->cmf = 0;8340		}8341	} else {8342no_cmf:8343		lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,8344				"6220 CMF is disabled\n");8345	}8346 8347	/* Only register congestion buffer with firmware if BOTH8348	 * CMF and E2E are enabled.8349	 */8350	if (sli4_params->cmf && sli4_params->mi_ver) {8351		rc = lpfc_reg_congestion_buf(phba);8352		if (rc) {8353			dma_free_coherent(&phba->pcidev->dev,8354					  sizeof(struct lpfc_cgn_info),8355					  phba->cgn_i->virt, phba->cgn_i->phys);8356			kfree(phba->cgn_i);8357			phba->cgn_i = NULL;8358			/* Ensure CGN Mode is off */8359			phba->cmf_active_mode = LPFC_CFG_OFF;8360			sli4_params->cmf = 0;8361			return 0;8362		}8363	}8364	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,8365			"6470 Setup MI version %d CMF %d mode %d\n",8366			sli4_params->mi_ver, sli4_params->cmf,8367			phba->cmf_active_mode);8368 8369	mempool_free(mboxq, phba->mbox_mem_pool);8370 8371	/* Initialize atomic counters */8372	atomic_set(&phba->cgn_fabric_warn_cnt, 0);8373	atomic_set(&phba->cgn_fabric_alarm_cnt, 0);8374	atomic_set(&phba->cgn_sync_alarm_cnt, 0);8375	atomic_set(&phba->cgn_sync_warn_cnt, 0);8376	atomic_set(&phba->cgn_driver_evt_cnt, 0);8377	atomic_set(&phba->cgn_latency_evt_cnt, 0);8378	atomic64_set(&phba->cgn_latency_evt, 0);8379 8380	phba->cmf_interval_rate = LPFC_CMF_INTERVAL;8381 8382	/* Allocate RX Monitor Buffer */8383	if (!phba->rx_monitor) {8384		phba->rx_monitor = kzalloc(sizeof(*phba->rx_monitor),8385					   GFP_KERNEL);8386 8387		if (!phba->rx_monitor) {8388			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,8389					"2644 Failed to alloc memory "8390					"for RX Monitor Buffer\n");8391			return -ENOMEM;8392		}8393 8394		/* Instruct the rx_monitor object to instantiate its ring */8395		if (lpfc_rx_monitor_create_ring(phba->rx_monitor,8396						LPFC_MAX_RXMONITOR_ENTRY)) {8397			kfree(phba->rx_monitor);8398			phba->rx_monitor = NULL;8399			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,8400					"2645 Failed to alloc memory "8401					"for RX Monitor's Ring\n");8402			return -ENOMEM;8403		}8404	}8405 8406	return 0;8407}8408 8409static int8410lpfc_set_host_tm(struct lpfc_hba *phba)8411{8412	LPFC_MBOXQ_t *mboxq;8413	uint32_t len, rc;8414	struct timespec64 cur_time;8415	struct tm broken;8416	uint32_t month, day, year;8417	uint32_t hour, minute, second;8418	struct lpfc_mbx_set_host_date_time *tm;8419 8420	mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);8421	if (!mboxq)8422		return -ENOMEM;8423 8424	len = sizeof(struct lpfc_mbx_set_host_data) -8425		sizeof(struct lpfc_sli4_cfg_mhdr);8426	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,8427			 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,8428			 LPFC_SLI4_MBX_EMBED);8429 8430	mboxq->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_DATE_TIME;8431	mboxq->u.mqe.un.set_host_data.param_len =8432			sizeof(struct lpfc_mbx_set_host_date_time);8433	tm = &mboxq->u.mqe.un.set_host_data.un.tm;8434	ktime_get_real_ts64(&cur_time);8435	time64_to_tm(cur_time.tv_sec, 0, &broken);8436	month = broken.tm_mon + 1;8437	day = broken.tm_mday;8438	year = broken.tm_year - 100;8439	hour = broken.tm_hour;8440	minute = broken.tm_min;8441	second = broken.tm_sec;8442	bf_set(lpfc_mbx_set_host_month, tm, month);8443	bf_set(lpfc_mbx_set_host_day, tm, day);8444	bf_set(lpfc_mbx_set_host_year, tm, year);8445	bf_set(lpfc_mbx_set_host_hour, tm, hour);8446	bf_set(lpfc_mbx_set_host_min, tm, minute);8447	bf_set(lpfc_mbx_set_host_sec, tm, second);8448 8449	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);8450	mempool_free(mboxq, phba->mbox_mem_pool);8451	return rc;8452}8453 8454/**8455 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function8456 * @phba: Pointer to HBA context object.8457 *8458 * This function is the main SLI4 device initialization PCI function. This8459 * function is called by the HBA initialization code, HBA reset code and8460 * HBA error attention handler code. Caller is not required to hold any8461 * locks.8462 **/8463int8464lpfc_sli4_hba_setup(struct lpfc_hba *phba)8465{8466	int rc, i, cnt, len, dd;8467	LPFC_MBOXQ_t *mboxq;8468	struct lpfc_mqe *mqe;8469	uint8_t *vpd;8470	uint32_t vpd_size;8471	uint32_t ftr_rsp = 0;8472	struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);8473	struct lpfc_vport *vport = phba->pport;8474	struct lpfc_dmabuf *mp;8475	struct lpfc_rqb *rqbp;8476	u32 flg;8477 8478	/* Perform a PCI function reset to start from clean */8479	rc = lpfc_pci_function_reset(phba);8480	if (unlikely(rc))8481		return -ENODEV;8482 8483	/* Check the HBA Host Status Register for readyness */8484	rc = lpfc_sli4_post_status_check(phba);8485	if (unlikely(rc))8486		return -ENODEV;8487	else {8488		spin_lock_irq(&phba->hbalock);8489		phba->sli.sli_flag |= LPFC_SLI_ACTIVE;8490		flg = phba->sli.sli_flag;8491		spin_unlock_irq(&phba->hbalock);8492		/* Allow a little time after setting SLI_ACTIVE for any polled8493		 * MBX commands to complete via BSG.8494		 */8495		for (i = 0; i < 50 && (flg & LPFC_SLI_MBOX_ACTIVE); i++) {8496			msleep(20);8497			spin_lock_irq(&phba->hbalock);8498			flg = phba->sli.sli_flag;8499			spin_unlock_irq(&phba->hbalock);8500		}8501	}8502	clear_bit(HBA_SETUP, &phba->hba_flag);8503 8504	lpfc_sli4_dip(phba);8505 8506	/*8507	 * Allocate a single mailbox container for initializing the8508	 * port.8509	 */8510	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);8511	if (!mboxq)8512		return -ENOMEM;8513 8514	/* Issue READ_REV to collect vpd and FW information. */8515	vpd_size = SLI4_PAGE_SIZE;8516	vpd = kzalloc(vpd_size, GFP_KERNEL);8517	if (!vpd) {8518		rc = -ENOMEM;8519		goto out_free_mbox;8520	}8521 8522	rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);8523	if (unlikely(rc)) {8524		kfree(vpd);8525		goto out_free_mbox;8526	}8527 8528	mqe = &mboxq->u.mqe;8529	phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);8530	if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {8531		set_bit(HBA_FCOE_MODE, &phba->hba_flag);8532		phba->fcp_embed_io = 0;	/* SLI4 FC support only */8533	} else {8534		clear_bit(HBA_FCOE_MODE, &phba->hba_flag);8535	}8536 8537	if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==8538		LPFC_DCBX_CEE_MODE)8539		set_bit(HBA_FIP_SUPPORT, &phba->hba_flag);8540	else8541		clear_bit(HBA_FIP_SUPPORT, &phba->hba_flag);8542 8543	clear_bit(HBA_IOQ_FLUSH, &phba->hba_flag);8544 8545	if (phba->sli_rev != LPFC_SLI_REV4) {8546		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8547			"0376 READ_REV Error. SLI Level %d "8548			"FCoE enabled %d\n",8549			phba->sli_rev,8550			test_bit(HBA_FCOE_MODE, &phba->hba_flag) ? 1 : 0);8551		rc = -EIO;8552		kfree(vpd);8553		goto out_free_mbox;8554	}8555 8556	rc = lpfc_set_host_tm(phba);8557	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,8558			"6468 Set host date / time: Status x%x:\n", rc);8559 8560	/*8561	 * Continue initialization with default values even if driver failed8562	 * to read FCoE param config regions, only read parameters if the8563	 * board is FCoE8564	 */8565	if (test_bit(HBA_FCOE_MODE, &phba->hba_flag) &&8566	    lpfc_sli4_read_fcoe_params(phba))8567		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,8568			"2570 Failed to read FCoE parameters\n");8569 8570	/*8571	 * Retrieve sli4 device physical port name, failure of doing it8572	 * is considered as non-fatal.8573	 */8574	rc = lpfc_sli4_retrieve_pport_name(phba);8575	if (!rc)8576		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,8577				"3080 Successful retrieving SLI4 device "8578				"physical port name: %s.\n", phba->Port);8579 8580	rc = lpfc_sli4_get_ctl_attr(phba);8581	if (!rc)8582		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,8583				"8351 Successful retrieving SLI4 device "8584				"CTL ATTR\n");8585 8586	/*8587	 * Evaluate the read rev and vpd data. Populate the driver8588	 * state with the results. If this routine fails, the failure8589	 * is not fatal as the driver will use generic values.8590	 */8591	rc = lpfc_parse_vpd(phba, vpd, vpd_size);8592	if (unlikely(!rc))8593		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8594				"0377 Error %d parsing vpd. "8595				"Using defaults.\n", rc);8596	kfree(vpd);8597 8598	/* Save information as VPD data */8599	phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;8600	phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;8601 8602	/*8603	 * This is because first G7 ASIC doesn't support the standard8604	 * 0x5a NVME cmd descriptor type/subtype8605	 */8606	if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==8607			LPFC_SLI_INTF_IF_TYPE_6) &&8608	    (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&8609	    (phba->vpd.rev.smRev == 0) &&8610	    (phba->cfg_nvme_embed_cmd == 1))8611		phba->cfg_nvme_embed_cmd = 0;8612 8613	phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;8614	phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,8615					 &mqe->un.read_rev);8616	phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,8617				       &mqe->un.read_rev);8618	phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,8619					    &mqe->un.read_rev);8620	phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,8621					   &mqe->un.read_rev);8622	phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;8623	memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);8624	phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;8625	memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);8626	phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;8627	memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);8628	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,8629			"(%d):0380 READ_REV Status x%x "8630			"fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",8631			mboxq->vport ? mboxq->vport->vpi : 0,8632			bf_get(lpfc_mqe_status, mqe),8633			phba->vpd.rev.opFwName,8634			phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,8635			phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);8636 8637	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==8638	    LPFC_SLI_INTF_IF_TYPE_0) {8639		lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);8640		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);8641		if (rc == MBX_SUCCESS) {8642			set_bit(HBA_RECOVERABLE_UE, &phba->hba_flag);8643			/* Set 1Sec interval to detect UE */8644			phba->eratt_poll_interval = 1;8645			phba->sli4_hba.ue_to_sr = bf_get(8646					lpfc_mbx_set_feature_UESR,8647					&mboxq->u.mqe.un.set_feature);8648			phba->sli4_hba.ue_to_rp = bf_get(8649					lpfc_mbx_set_feature_UERP,8650					&mboxq->u.mqe.un.set_feature);8651		}8652	}8653 8654	if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {8655		/* Enable MDS Diagnostics only if the SLI Port supports it */8656		lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);8657		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);8658		if (rc != MBX_SUCCESS)8659			phba->mds_diags_support = 0;8660	}8661 8662	/*8663	 * Discover the port's supported feature set and match it against the8664	 * hosts requests.8665	 */8666	lpfc_request_features(phba, mboxq);8667	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);8668	if (unlikely(rc)) {8669		rc = -EIO;8670		goto out_free_mbox;8671	}8672 8673	/* Disable VMID if app header is not supported */8674	if (phba->cfg_vmid_app_header && !(bf_get(lpfc_mbx_rq_ftr_rsp_ashdr,8675						  &mqe->un.req_ftrs))) {8676		bf_set(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags, 0);8677		phba->cfg_vmid_app_header = 0;8678		lpfc_printf_log(phba, KERN_DEBUG, LOG_SLI,8679				"1242 vmid feature not supported\n");8680	}8681 8682	/*8683	 * The port must support FCP initiator mode as this is the8684	 * only mode running in the host.8685	 */8686	if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {8687		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,8688				"0378 No support for fcpi mode.\n");8689		ftr_rsp++;8690	}8691 8692	/* Performance Hints are ONLY for FCoE */8693	if (test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {8694		if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))8695			phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;8696		else8697			phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;8698	}8699 8700	/*8701	 * If the port cannot support the host's requested features8702	 * then turn off the global config parameters to disable the8703	 * feature in the driver.  This is not a fatal error.8704	 */8705	if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {8706		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {8707			phba->cfg_enable_bg = 0;8708			phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;8709			ftr_rsp++;8710		}8711	}8712 8713	if (phba->max_vpi && phba->cfg_enable_npiv &&8714	    !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))8715		ftr_rsp++;8716 8717	if (ftr_rsp) {8718		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,8719				"0379 Feature Mismatch Data: x%08x %08x "8720				"x%x x%x x%x\n", mqe->un.req_ftrs.word2,8721				mqe->un.req_ftrs.word3, phba->cfg_enable_bg,8722				phba->cfg_enable_npiv, phba->max_vpi);8723		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))8724			phba->cfg_enable_bg = 0;8725		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))8726			phba->cfg_enable_npiv = 0;8727	}8728 8729	/* These SLI3 features are assumed in SLI4 */8730	spin_lock_irq(&phba->hbalock);8731	phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);8732	spin_unlock_irq(&phba->hbalock);8733 8734	/* Always try to enable dual dump feature if we can */8735	lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);8736	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);8737	dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);8738	if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))8739		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,8740				"6448 Dual Dump is enabled\n");8741	else8742		lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,8743				"6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "8744				"rc:x%x dd:x%x\n",8745				bf_get(lpfc_mqe_command, &mboxq->u.mqe),8746				lpfc_sli_config_mbox_subsys_get(8747					phba, mboxq),8748				lpfc_sli_config_mbox_opcode_get(8749					phba, mboxq),8750				rc, dd);8751	/*8752	 * Allocate all resources (xri,rpi,vpi,vfi) now.  Subsequent8753	 * calls depends on these resources to complete port setup.8754	 */8755	rc = lpfc_sli4_alloc_resource_identifiers(phba);8756	if (rc) {8757		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8758				"2920 Failed to alloc Resource IDs "8759				"rc = x%x\n", rc);8760		goto out_free_mbox;8761	}8762 8763	lpfc_set_host_data(phba, mboxq);8764 8765	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);8766	if (rc) {8767		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,8768				"2134 Failed to set host os driver version %x",8769				rc);8770	}8771 8772	/* Read the port's service parameters. */8773	rc = lpfc_read_sparam(phba, mboxq, vport->vpi);8774	if (rc) {8775		phba->link_state = LPFC_HBA_ERROR;8776		rc = -ENOMEM;8777		goto out_free_mbox;8778	}8779 8780	mboxq->vport = vport;8781	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);8782	mp = mboxq->ctx_buf;8783	if (rc == MBX_SUCCESS) {8784		memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));8785		rc = 0;8786	}8787 8788	/*8789	 * This memory was allocated by the lpfc_read_sparam routine but is8790	 * no longer needed.  It is released and ctx_buf NULLed to prevent8791	 * unintended pointer access as the mbox is reused.8792	 */8793	lpfc_mbuf_free(phba, mp->virt, mp->phys);8794	kfree(mp);8795	mboxq->ctx_buf = NULL;8796	if (unlikely(rc)) {8797		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8798				"0382 READ_SPARAM command failed "8799				"status %d, mbxStatus x%x\n",8800				rc, bf_get(lpfc_mqe_status, mqe));8801		phba->link_state = LPFC_HBA_ERROR;8802		rc = -EIO;8803		goto out_free_mbox;8804	}8805 8806	lpfc_update_vport_wwn(vport);8807 8808	/* Update the fc_host data structures with new wwn. */8809	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);8810	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);8811 8812	/* Create all the SLI4 queues */8813	rc = lpfc_sli4_queue_create(phba);8814	if (rc) {8815		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8816				"3089 Failed to allocate queues\n");8817		rc = -ENODEV;8818		goto out_free_mbox;8819	}8820	/* Set up all the queues to the device */8821	rc = lpfc_sli4_queue_setup(phba);8822	if (unlikely(rc)) {8823		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8824				"0381 Error %d during queue setup.\n", rc);8825		goto out_stop_timers;8826	}8827	/* Initialize the driver internal SLI layer lists. */8828	lpfc_sli4_setup(phba);8829	lpfc_sli4_queue_init(phba);8830 8831	/* update host els xri-sgl sizes and mappings */8832	rc = lpfc_sli4_els_sgl_update(phba);8833	if (unlikely(rc)) {8834		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8835				"1400 Failed to update xri-sgl size and "8836				"mapping: %d\n", rc);8837		goto out_destroy_queue;8838	}8839 8840	/* register the els sgl pool to the port */8841	rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,8842				       phba->sli4_hba.els_xri_cnt);8843	if (unlikely(rc < 0)) {8844		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8845				"0582 Error %d during els sgl post "8846				"operation\n", rc);8847		rc = -ENODEV;8848		goto out_destroy_queue;8849	}8850	phba->sli4_hba.els_xri_cnt = rc;8851 8852	if (phba->nvmet_support) {8853		/* update host nvmet xri-sgl sizes and mappings */8854		rc = lpfc_sli4_nvmet_sgl_update(phba);8855		if (unlikely(rc)) {8856			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8857					"6308 Failed to update nvmet-sgl size "8858					"and mapping: %d\n", rc);8859			goto out_destroy_queue;8860		}8861 8862		/* register the nvmet sgl pool to the port */8863		rc = lpfc_sli4_repost_sgl_list(8864			phba,8865			&phba->sli4_hba.lpfc_nvmet_sgl_list,8866			phba->sli4_hba.nvmet_xri_cnt);8867		if (unlikely(rc < 0)) {8868			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8869					"3117 Error %d during nvmet "8870					"sgl post\n", rc);8871			rc = -ENODEV;8872			goto out_destroy_queue;8873		}8874		phba->sli4_hba.nvmet_xri_cnt = rc;8875 8876		/* We allocate an iocbq for every receive context SGL.8877		 * The additional allocation is for abort and ls handling.8878		 */8879		cnt = phba->sli4_hba.nvmet_xri_cnt +8880			phba->sli4_hba.max_cfg_param.max_xri;8881	} else {8882		/* update host common xri-sgl sizes and mappings */8883		rc = lpfc_sli4_io_sgl_update(phba);8884		if (unlikely(rc)) {8885			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8886					"6082 Failed to update nvme-sgl size "8887					"and mapping: %d\n", rc);8888			goto out_destroy_queue;8889		}8890 8891		/* register the allocated common sgl pool to the port */8892		rc = lpfc_sli4_repost_io_sgl_list(phba);8893		if (unlikely(rc)) {8894			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8895					"6116 Error %d during nvme sgl post "8896					"operation\n", rc);8897			/* Some NVME buffers were moved to abort nvme list */8898			/* A pci function reset will repost them */8899			rc = -ENODEV;8900			goto out_destroy_queue;8901		}8902		/* Each lpfc_io_buf job structure has an iocbq element.8903		 * This cnt provides for abort, els, ct and ls requests.8904		 */8905		cnt = phba->sli4_hba.max_cfg_param.max_xri;8906	}8907 8908	if (!phba->sli.iocbq_lookup) {8909		/* Initialize and populate the iocb list per host */8910		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,8911				"2821 initialize iocb list with %d entries\n",8912				cnt);8913		rc = lpfc_init_iocb_list(phba, cnt);8914		if (rc) {8915			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8916					"1413 Failed to init iocb list.\n");8917			goto out_destroy_queue;8918		}8919	}8920 8921	if (phba->nvmet_support)8922		lpfc_nvmet_create_targetport(phba);8923 8924	if (phba->nvmet_support && phba->cfg_nvmet_mrq) {8925		/* Post initial buffers to all RQs created */8926		for (i = 0; i < phba->cfg_nvmet_mrq; i++) {8927			rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;8928			INIT_LIST_HEAD(&rqbp->rqb_buffer_list);8929			rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;8930			rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;8931			rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;8932			rqbp->buffer_count = 0;8933 8934			lpfc_post_rq_buffer(8935				phba, phba->sli4_hba.nvmet_mrq_hdr[i],8936				phba->sli4_hba.nvmet_mrq_data[i],8937				phba->cfg_nvmet_mrq_post, i);8938		}8939	}8940 8941	/* Post the rpi header region to the device. */8942	rc = lpfc_sli4_post_all_rpi_hdrs(phba);8943	if (unlikely(rc)) {8944		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,8945				"0393 Error %d during rpi post operation\n",8946				rc);8947		rc = -ENODEV;8948		goto out_free_iocblist;8949	}8950	lpfc_sli4_node_prep(phba);8951 8952	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag)) {8953		if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {8954			/*8955			 * The FC Port needs to register FCFI (index 0)8956			 */8957			lpfc_reg_fcfi(phba, mboxq);8958			mboxq->vport = phba->pport;8959			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);8960			if (rc != MBX_SUCCESS)8961				goto out_unset_queue;8962			rc = 0;8963			phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,8964						&mboxq->u.mqe.un.reg_fcfi);8965		} else {8966			/* We are a NVME Target mode with MRQ > 1 */8967 8968			/* First register the FCFI */8969			lpfc_reg_fcfi_mrq(phba, mboxq, 0);8970			mboxq->vport = phba->pport;8971			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);8972			if (rc != MBX_SUCCESS)8973				goto out_unset_queue;8974			rc = 0;8975			phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,8976						&mboxq->u.mqe.un.reg_fcfi_mrq);8977 8978			/* Next register the MRQs */8979			lpfc_reg_fcfi_mrq(phba, mboxq, 1);8980			mboxq->vport = phba->pport;8981			rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);8982			if (rc != MBX_SUCCESS)8983				goto out_unset_queue;8984			rc = 0;8985		}8986		/* Check if the port is configured to be disabled */8987		lpfc_sli_read_link_ste(phba);8988	}8989 8990	/* Don't post more new bufs if repost already recovered8991	 * the nvme sgls.8992	 */8993	if (phba->nvmet_support == 0) {8994		if (phba->sli4_hba.io_xri_cnt == 0) {8995			len = lpfc_new_io_buf(8996					      phba, phba->sli4_hba.io_xri_max);8997			if (len == 0) {8998				rc = -ENOMEM;8999				goto out_unset_queue;9000			}9001 9002			if (phba->cfg_xri_rebalancing)9003				lpfc_create_multixri_pools(phba);9004		}9005	} else {9006		phba->cfg_xri_rebalancing = 0;9007	}9008 9009	/* Allow asynchronous mailbox command to go through */9010	spin_lock_irq(&phba->hbalock);9011	phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;9012	spin_unlock_irq(&phba->hbalock);9013 9014	/* Post receive buffers to the device */9015	lpfc_sli4_rb_setup(phba);9016 9017	/* Reset HBA FCF states after HBA reset */9018	phba->fcf.fcf_flag = 0;9019	phba->fcf.current_rec.flag = 0;9020 9021	/* Start the ELS watchdog timer */9022	mod_timer(&vport->els_tmofunc,9023		  jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));9024 9025	/* Start heart beat timer */9026	mod_timer(&phba->hb_tmofunc,9027		  jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));9028	clear_bit(HBA_HBEAT_INP, &phba->hba_flag);9029	clear_bit(HBA_HBEAT_TMO, &phba->hba_flag);9030	phba->last_completion_time = jiffies;9031 9032	/* start eq_delay heartbeat */9033	if (phba->cfg_auto_imax)9034		queue_delayed_work(phba->wq, &phba->eq_delay_work,9035				   msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));9036 9037	/* start per phba idle_stat_delay heartbeat */9038	lpfc_init_idle_stat_hb(phba);9039 9040	/* Start error attention (ERATT) polling timer */9041	mod_timer(&phba->eratt_poll,9042		  jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));9043 9044	/*9045	 * The port is ready, set the host's link state to LINK_DOWN9046	 * in preparation for link interrupts.9047	 */9048	spin_lock_irq(&phba->hbalock);9049	phba->link_state = LPFC_LINK_DOWN;9050 9051	/* Check if physical ports are trunked */9052	if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))9053		phba->trunk_link.link0.state = LPFC_LINK_DOWN;9054	if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))9055		phba->trunk_link.link1.state = LPFC_LINK_DOWN;9056	if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))9057		phba->trunk_link.link2.state = LPFC_LINK_DOWN;9058	if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))9059		phba->trunk_link.link3.state = LPFC_LINK_DOWN;9060	spin_unlock_irq(&phba->hbalock);9061 9062	/* Arm the CQs and then EQs on device */9063	lpfc_sli4_arm_cqeq_intr(phba);9064 9065	/* Indicate device interrupt mode */9066	phba->sli4_hba.intr_enable = 1;9067 9068	/* Setup CMF after HBA is initialized */9069	lpfc_cmf_setup(phba);9070 9071	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag) &&9072	    test_bit(LINK_DISABLED, &phba->hba_flag)) {9073		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,9074				"3103 Adapter Link is disabled.\n");9075		lpfc_down_link(phba, mboxq);9076		rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);9077		if (rc != MBX_SUCCESS) {9078			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,9079					"3104 Adapter failed to issue "9080					"DOWN_LINK mbox cmd, rc:x%x\n", rc);9081			goto out_io_buff_free;9082		}9083	} else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {9084		/* don't perform init_link on SLI4 FC port loopback test */9085		if (!(phba->link_flag & LS_LOOPBACK_MODE)) {9086			rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);9087			if (rc)9088				goto out_io_buff_free;9089		}9090	}9091	mempool_free(mboxq, phba->mbox_mem_pool);9092 9093	/* Enable RAS FW log support */9094	lpfc_sli4_ras_setup(phba);9095 9096	set_bit(HBA_SETUP, &phba->hba_flag);9097	return rc;9098 9099out_io_buff_free:9100	/* Free allocated IO Buffers */9101	lpfc_io_free(phba);9102out_unset_queue:9103	/* Unset all the queues set up in this routine when error out */9104	lpfc_sli4_queue_unset(phba);9105out_free_iocblist:9106	lpfc_free_iocb_list(phba);9107out_destroy_queue:9108	lpfc_sli4_queue_destroy(phba);9109out_stop_timers:9110	lpfc_stop_hba_timers(phba);9111out_free_mbox:9112	mempool_free(mboxq, phba->mbox_mem_pool);9113	return rc;9114}9115 9116/**9117 * lpfc_mbox_timeout - Timeout call back function for mbox timer9118 * @t: Context to fetch pointer to hba structure from.9119 *9120 * This is the callback function for mailbox timer. The mailbox9121 * timer is armed when a new mailbox command is issued and the timer9122 * is deleted when the mailbox complete. The function is called by9123 * the kernel timer code when a mailbox does not complete within9124 * expected time. This function wakes up the worker thread to9125 * process the mailbox timeout and returns. All the processing is9126 * done by the worker thread function lpfc_mbox_timeout_handler.9127 **/9128void9129lpfc_mbox_timeout(struct timer_list *t)9130{9131	struct lpfc_hba  *phba = from_timer(phba, t, sli.mbox_tmo);9132	unsigned long iflag;9133	uint32_t tmo_posted;9134 9135	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);9136	tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;9137	if (!tmo_posted)9138		phba->pport->work_port_events |= WORKER_MBOX_TMO;9139	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);9140 9141	if (!tmo_posted)9142		lpfc_worker_wake_up(phba);9143	return;9144}9145 9146/**9147 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions9148 *                                    are pending9149 * @phba: Pointer to HBA context object.9150 *9151 * This function checks if any mailbox completions are present on the mailbox9152 * completion queue.9153 **/9154static bool9155lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)9156{9157 9158	uint32_t idx;9159	struct lpfc_queue *mcq;9160	struct lpfc_mcqe *mcqe;9161	bool pending_completions = false;9162	uint8_t	qe_valid;9163 9164	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))9165		return false;9166 9167	/* Check for completions on mailbox completion queue */9168 9169	mcq = phba->sli4_hba.mbx_cq;9170	idx = mcq->hba_index;9171	qe_valid = mcq->qe_valid;9172	while (bf_get_le32(lpfc_cqe_valid,9173	       (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {9174		mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));9175		if (bf_get_le32(lpfc_trailer_completed, mcqe) &&9176		    (!bf_get_le32(lpfc_trailer_async, mcqe))) {9177			pending_completions = true;9178			break;9179		}9180		idx = (idx + 1) % mcq->entry_count;9181		if (mcq->hba_index == idx)9182			break;9183 9184		/* if the index wrapped around, toggle the valid bit */9185		if (phba->sli4_hba.pc_sli4_params.cqav && !idx)9186			qe_valid = (qe_valid) ? 0 : 1;9187	}9188	return pending_completions;9189 9190}9191 9192/**9193 * lpfc_sli4_process_missed_mbox_completions - process mbox completions9194 *					      that were missed.9195 * @phba: Pointer to HBA context object.9196 *9197 * For sli4, it is possible to miss an interrupt. As such mbox completions9198 * maybe missed causing erroneous mailbox timeouts to occur. This function9199 * checks to see if mbox completions are on the mailbox completion queue9200 * and will process all the completions associated with the eq for the9201 * mailbox completion queue.9202 **/9203static bool9204lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)9205{9206	struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;9207	uint32_t eqidx;9208	struct lpfc_queue *fpeq = NULL;9209	struct lpfc_queue *eq;9210	bool mbox_pending;9211 9212	if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))9213		return false;9214 9215	/* Find the EQ associated with the mbox CQ */9216	if (sli4_hba->hdwq) {9217		for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {9218			eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;9219			if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {9220				fpeq = eq;9221				break;9222			}9223		}9224	}9225	if (!fpeq)9226		return false;9227 9228	/* Turn off interrupts from this EQ */9229 9230	sli4_hba->sli4_eq_clr_intr(fpeq);9231 9232	/* Check to see if a mbox completion is pending */9233 9234	mbox_pending = lpfc_sli4_mbox_completions_pending(phba);9235 9236	/*9237	 * If a mbox completion is pending, process all the events on EQ9238	 * associated with the mbox completion queue (this could include9239	 * mailbox commands, async events, els commands, receive queue data9240	 * and fcp commands)9241	 */9242 9243	if (mbox_pending)9244		/* process and rearm the EQ */9245		lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,9246				     LPFC_QUEUE_WORK);9247	else9248		/* Always clear and re-arm the EQ */9249		sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);9250 9251	return mbox_pending;9252 9253}9254 9255/**9256 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout9257 * @phba: Pointer to HBA context object.9258 *9259 * This function is called from worker thread when a mailbox command times out.9260 * The caller is not required to hold any locks. This function will reset the9261 * HBA and recover all the pending commands.9262 **/9263void9264lpfc_mbox_timeout_handler(struct lpfc_hba *phba)9265{9266	LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;9267	MAILBOX_t *mb = NULL;9268 9269	struct lpfc_sli *psli = &phba->sli;9270 9271	/* If the mailbox completed, process the completion */9272	lpfc_sli4_process_missed_mbox_completions(phba);9273 9274	if (!(psli->sli_flag & LPFC_SLI_ACTIVE))9275		return;9276 9277	if (pmbox != NULL)9278		mb = &pmbox->u.mb;9279	/* Check the pmbox pointer first.  There is a race condition9280	 * between the mbox timeout handler getting executed in the9281	 * worklist and the mailbox actually completing. When this9282	 * race condition occurs, the mbox_active will be NULL.9283	 */9284	spin_lock_irq(&phba->hbalock);9285	if (pmbox == NULL) {9286		lpfc_printf_log(phba, KERN_WARNING,9287				LOG_MBOX | LOG_SLI,9288				"0353 Active Mailbox cleared - mailbox timeout "9289				"exiting\n");9290		spin_unlock_irq(&phba->hbalock);9291		return;9292	}9293 9294	/* Mbox cmd <mbxCommand> timeout */9295	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,9296			"0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",9297			mb->mbxCommand,9298			phba->pport->port_state,9299			phba->sli.sli_flag,9300			phba->sli.mbox_active);9301	spin_unlock_irq(&phba->hbalock);9302 9303	/* Setting state unknown so lpfc_sli_abort_iocb_ring9304	 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing9305	 * it to fail all outstanding SCSI IO.9306	 */9307	set_bit(MBX_TMO_ERR, &phba->bit_flags);9308	spin_lock_irq(&phba->pport->work_port_lock);9309	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;9310	spin_unlock_irq(&phba->pport->work_port_lock);9311	spin_lock_irq(&phba->hbalock);9312	phba->link_state = LPFC_LINK_UNKNOWN;9313	psli->sli_flag &= ~LPFC_SLI_ACTIVE;9314	spin_unlock_irq(&phba->hbalock);9315 9316	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,9317			"0345 Resetting board due to mailbox timeout\n");9318 9319	/* Reset the HBA device */9320	lpfc_reset_hba(phba);9321}9322 9323/**9324 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware9325 * @phba: Pointer to HBA context object.9326 * @pmbox: Pointer to mailbox object.9327 * @flag: Flag indicating how the mailbox need to be processed.9328 *9329 * This function is called by discovery code and HBA management code9330 * to submit a mailbox command to firmware with SLI-3 interface spec. This9331 * function gets the hbalock to protect the data structures.9332 * The mailbox command can be submitted in polling mode, in which case9333 * this function will wait in a polling loop for the completion of the9334 * mailbox.9335 * If the mailbox is submitted in no_wait mode (not polling) the9336 * function will submit the command and returns immediately without waiting9337 * for the mailbox completion. The no_wait is supported only when HBA9338 * is in SLI2/SLI3 mode - interrupts are enabled.9339 * The SLI interface allows only one mailbox pending at a time. If the9340 * mailbox is issued in polling mode and there is already a mailbox9341 * pending, then the function will return an error. If the mailbox is issued9342 * in NO_WAIT mode and there is a mailbox pending already, the function9343 * will return MBX_BUSY after queuing the mailbox into mailbox queue.9344 * The sli layer owns the mailbox object until the completion of mailbox9345 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other9346 * return codes the caller owns the mailbox command after the return of9347 * the function.9348 **/9349static int9350lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,9351		       uint32_t flag)9352{9353	MAILBOX_t *mbx;9354	struct lpfc_sli *psli = &phba->sli;9355	uint32_t status, evtctr;9356	uint32_t ha_copy, hc_copy;9357	int i;9358	unsigned long timeout;9359	unsigned long drvr_flag = 0;9360	uint32_t word0, ldata;9361	void __iomem *to_slim;9362	int processing_queue = 0;9363 9364	spin_lock_irqsave(&phba->hbalock, drvr_flag);9365	if (!pmbox) {9366		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;9367		/* processing mbox queue from intr_handler */9368		if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {9369			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);9370			return MBX_SUCCESS;9371		}9372		processing_queue = 1;9373		pmbox = lpfc_mbox_get(phba);9374		if (!pmbox) {9375			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);9376			return MBX_SUCCESS;9377		}9378	}9379 9380	if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&9381		pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {9382		if(!pmbox->vport) {9383			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);9384			lpfc_printf_log(phba, KERN_ERR,9385					LOG_MBOX | LOG_VPORT,9386					"1806 Mbox x%x failed. No vport\n",9387					pmbox->u.mb.mbxCommand);9388			dump_stack();9389			goto out_not_finished;9390		}9391	}9392 9393	/* If the PCI channel is in offline state, do not post mbox. */9394	if (unlikely(pci_channel_offline(phba->pcidev))) {9395		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);9396		goto out_not_finished;9397	}9398 9399	/* If HBA has a deferred error attention, fail the iocb. */9400	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {9401		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);9402		goto out_not_finished;9403	}9404 9405	psli = &phba->sli;9406 9407	mbx = &pmbox->u.mb;9408	status = MBX_SUCCESS;9409 9410	if (phba->link_state == LPFC_HBA_ERROR) {9411		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);9412 9413		/* Mbox command <mbxCommand> cannot issue */9414		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,9415				"(%d):0311 Mailbox command x%x cannot "9416				"issue Data: x%x x%x\n",9417				pmbox->vport ? pmbox->vport->vpi : 0,9418				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);9419		goto out_not_finished;9420	}9421 9422	if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {9423		if (lpfc_readl(phba->HCregaddr, &hc_copy) ||9424			!(hc_copy & HC_MBINT_ENA)) {9425			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);9426			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,9427				"(%d):2528 Mailbox command x%x cannot "9428				"issue Data: x%x x%x\n",9429				pmbox->vport ? pmbox->vport->vpi : 0,9430				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);9431			goto out_not_finished;9432		}9433	}9434 9435	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {9436		/* Polling for a mbox command when another one is already active9437		 * is not allowed in SLI. Also, the driver must have established9438		 * SLI2 mode to queue and process multiple mbox commands.9439		 */9440 9441		if (flag & MBX_POLL) {9442			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);9443 9444			/* Mbox command <mbxCommand> cannot issue */9445			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,9446					"(%d):2529 Mailbox command x%x "9447					"cannot issue Data: x%x x%x\n",9448					pmbox->vport ? pmbox->vport->vpi : 0,9449					pmbox->u.mb.mbxCommand,9450					psli->sli_flag, flag);9451			goto out_not_finished;9452		}9453 9454		if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {9455			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);9456			/* Mbox command <mbxCommand> cannot issue */9457			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,9458					"(%d):2530 Mailbox command x%x "9459					"cannot issue Data: x%x x%x\n",9460					pmbox->vport ? pmbox->vport->vpi : 0,9461					pmbox->u.mb.mbxCommand,9462					psli->sli_flag, flag);9463			goto out_not_finished;9464		}9465 9466		/* Another mailbox command is still being processed, queue this9467		 * command to be processed later.9468		 */9469		lpfc_mbox_put(phba, pmbox);9470 9471		/* Mbox cmd issue - BUSY */9472		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,9473				"(%d):0308 Mbox cmd issue - BUSY Data: "9474				"x%x x%x x%x x%x\n",9475				pmbox->vport ? pmbox->vport->vpi : 0xffffff,9476				mbx->mbxCommand,9477				phba->pport ? phba->pport->port_state : 0xff,9478				psli->sli_flag, flag);9479 9480		psli->slistat.mbox_busy++;9481		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);9482 9483		if (pmbox->vport) {9484			lpfc_debugfs_disc_trc(pmbox->vport,9485				LPFC_DISC_TRC_MBOX_VPORT,9486				"MBOX Bsy vport:  cmd:x%x mb:x%x x%x",9487				(uint32_t)mbx->mbxCommand,9488				mbx->un.varWords[0], mbx->un.varWords[1]);9489		}9490		else {9491			lpfc_debugfs_disc_trc(phba->pport,9492				LPFC_DISC_TRC_MBOX,9493				"MBOX Bsy:        cmd:x%x mb:x%x x%x",9494				(uint32_t)mbx->mbxCommand,9495				mbx->un.varWords[0], mbx->un.varWords[1]);9496		}9497 9498		return MBX_BUSY;9499	}9500 9501	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;9502 9503	/* If we are not polling, we MUST be in SLI2 mode */9504	if (flag != MBX_POLL) {9505		if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&9506		    (mbx->mbxCommand != MBX_KILL_BOARD)) {9507			psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;9508			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);9509			/* Mbox command <mbxCommand> cannot issue */9510			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,9511					"(%d):2531 Mailbox command x%x "9512					"cannot issue Data: x%x x%x\n",9513					pmbox->vport ? pmbox->vport->vpi : 0,9514					pmbox->u.mb.mbxCommand,9515					psli->sli_flag, flag);9516			goto out_not_finished;9517		}9518		/* timeout active mbox command */9519		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *9520					   1000);9521		mod_timer(&psli->mbox_tmo, jiffies + timeout);9522	}9523 9524	/* Mailbox cmd <cmd> issue */9525	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,9526			"(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "9527			"x%x\n",9528			pmbox->vport ? pmbox->vport->vpi : 0,9529			mbx->mbxCommand,9530			phba->pport ? phba->pport->port_state : 0xff,9531			psli->sli_flag, flag);9532 9533	if (mbx->mbxCommand != MBX_HEARTBEAT) {9534		if (pmbox->vport) {9535			lpfc_debugfs_disc_trc(pmbox->vport,9536				LPFC_DISC_TRC_MBOX_VPORT,9537				"MBOX Send vport: cmd:x%x mb:x%x x%x",9538				(uint32_t)mbx->mbxCommand,9539				mbx->un.varWords[0], mbx->un.varWords[1]);9540		}9541		else {9542			lpfc_debugfs_disc_trc(phba->pport,9543				LPFC_DISC_TRC_MBOX,9544				"MBOX Send:       cmd:x%x mb:x%x x%x",9545				(uint32_t)mbx->mbxCommand,9546				mbx->un.varWords[0], mbx->un.varWords[1]);9547		}9548	}9549 9550	psli->slistat.mbox_cmd++;9551	evtctr = psli->slistat.mbox_event;9552 9553	/* next set own bit for the adapter and copy over command word */9554	mbx->mbxOwner = OWN_CHIP;9555 9556	if (psli->sli_flag & LPFC_SLI_ACTIVE) {9557		/* Populate mbox extension offset word. */9558		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {9559			*(((uint32_t *)mbx) + pmbox->mbox_offset_word)9560				= (uint8_t *)phba->mbox_ext9561				  - (uint8_t *)phba->mbox;9562		}9563 9564		/* Copy the mailbox extension data */9565		if (pmbox->in_ext_byte_len && pmbox->ext_buf) {9566			lpfc_sli_pcimem_bcopy(pmbox->ext_buf,9567					      (uint8_t *)phba->mbox_ext,9568					      pmbox->in_ext_byte_len);9569		}9570		/* Copy command data to host SLIM area */9571		lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);9572	} else {9573		/* Populate mbox extension offset word. */9574		if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)9575			*(((uint32_t *)mbx) + pmbox->mbox_offset_word)9576				= MAILBOX_HBA_EXT_OFFSET;9577 9578		/* Copy the mailbox extension data */9579		if (pmbox->in_ext_byte_len && pmbox->ext_buf)9580			lpfc_memcpy_to_slim(phba->MBslimaddr +9581				MAILBOX_HBA_EXT_OFFSET,9582				pmbox->ext_buf, pmbox->in_ext_byte_len);9583 9584		if (mbx->mbxCommand == MBX_CONFIG_PORT)9585			/* copy command data into host mbox for cmpl */9586			lpfc_sli_pcimem_bcopy(mbx, phba->mbox,9587					      MAILBOX_CMD_SIZE);9588 9589		/* First copy mbox command data to HBA SLIM, skip past first9590		   word */9591		to_slim = phba->MBslimaddr + sizeof (uint32_t);9592		lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],9593			    MAILBOX_CMD_SIZE - sizeof (uint32_t));9594 9595		/* Next copy over first word, with mbxOwner set */9596		ldata = *((uint32_t *)mbx);9597		to_slim = phba->MBslimaddr;9598		writel(ldata, to_slim);9599		readl(to_slim); /* flush */9600 9601		if (mbx->mbxCommand == MBX_CONFIG_PORT)9602			/* switch over to host mailbox */9603			psli->sli_flag |= LPFC_SLI_ACTIVE;9604	}9605 9606	wmb();9607 9608	switch (flag) {9609	case MBX_NOWAIT:9610		/* Set up reference to mailbox command */9611		psli->mbox_active = pmbox;9612		/* Interrupt board to do it */9613		writel(CA_MBATT, phba->CAregaddr);9614		readl(phba->CAregaddr); /* flush */9615		/* Don't wait for it to finish, just return */9616		break;9617 9618	case MBX_POLL:9619		/* Set up null reference to mailbox command */9620		psli->mbox_active = NULL;9621		/* Interrupt board to do it */9622		writel(CA_MBATT, phba->CAregaddr);9623		readl(phba->CAregaddr); /* flush */9624 9625		if (psli->sli_flag & LPFC_SLI_ACTIVE) {9626			/* First read mbox status word */9627			word0 = *((uint32_t *)phba->mbox);9628			word0 = le32_to_cpu(word0);9629		} else {9630			/* First read mbox status word */9631			if (lpfc_readl(phba->MBslimaddr, &word0)) {9632				spin_unlock_irqrestore(&phba->hbalock,9633						       drvr_flag);9634				goto out_not_finished;9635			}9636		}9637 9638		/* Read the HBA Host Attention Register */9639		if (lpfc_readl(phba->HAregaddr, &ha_copy)) {9640			spin_unlock_irqrestore(&phba->hbalock,9641						       drvr_flag);9642			goto out_not_finished;9643		}9644		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *9645							1000) + jiffies;9646		i = 0;9647		/* Wait for command to complete */9648		while (((word0 & OWN_CHIP) == OWN_CHIP) ||9649		       (!(ha_copy & HA_MBATT) &&9650			(phba->link_state > LPFC_WARM_START))) {9651			if (time_after(jiffies, timeout)) {9652				psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;9653				spin_unlock_irqrestore(&phba->hbalock,9654						       drvr_flag);9655				goto out_not_finished;9656			}9657 9658			/* Check if we took a mbox interrupt while we were9659			   polling */9660			if (((word0 & OWN_CHIP) != OWN_CHIP)9661			    && (evtctr != psli->slistat.mbox_event))9662				break;9663 9664			if (i++ > 10) {9665				spin_unlock_irqrestore(&phba->hbalock,9666						       drvr_flag);9667				msleep(1);9668				spin_lock_irqsave(&phba->hbalock, drvr_flag);9669			}9670 9671			if (psli->sli_flag & LPFC_SLI_ACTIVE) {9672				/* First copy command data */9673				word0 = *((uint32_t *)phba->mbox);9674				word0 = le32_to_cpu(word0);9675				if (mbx->mbxCommand == MBX_CONFIG_PORT) {9676					MAILBOX_t *slimmb;9677					uint32_t slimword0;9678					/* Check real SLIM for any errors */9679					slimword0 = readl(phba->MBslimaddr);9680					slimmb = (MAILBOX_t *) & slimword0;9681					if (((slimword0 & OWN_CHIP) != OWN_CHIP)9682					    && slimmb->mbxStatus) {9683						psli->sli_flag &=9684						    ~LPFC_SLI_ACTIVE;9685						word0 = slimword0;9686					}9687				}9688			} else {9689				/* First copy command data */9690				word0 = readl(phba->MBslimaddr);9691			}9692			/* Read the HBA Host Attention Register */9693			if (lpfc_readl(phba->HAregaddr, &ha_copy)) {9694				spin_unlock_irqrestore(&phba->hbalock,9695						       drvr_flag);9696				goto out_not_finished;9697			}9698		}9699 9700		if (psli->sli_flag & LPFC_SLI_ACTIVE) {9701			/* copy results back to user */9702			lpfc_sli_pcimem_bcopy(phba->mbox, mbx,9703						MAILBOX_CMD_SIZE);9704			/* Copy the mailbox extension data */9705			if (pmbox->out_ext_byte_len && pmbox->ext_buf) {9706				lpfc_sli_pcimem_bcopy(phba->mbox_ext,9707						      pmbox->ext_buf,9708						      pmbox->out_ext_byte_len);9709			}9710		} else {9711			/* First copy command data */9712			lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,9713						MAILBOX_CMD_SIZE);9714			/* Copy the mailbox extension data */9715			if (pmbox->out_ext_byte_len && pmbox->ext_buf) {9716				lpfc_memcpy_from_slim(9717					pmbox->ext_buf,9718					phba->MBslimaddr +9719					MAILBOX_HBA_EXT_OFFSET,9720					pmbox->out_ext_byte_len);9721			}9722		}9723 9724		writel(HA_MBATT, phba->HAregaddr);9725		readl(phba->HAregaddr); /* flush */9726 9727		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;9728		status = mbx->mbxStatus;9729	}9730 9731	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);9732	return status;9733 9734out_not_finished:9735	if (processing_queue) {9736		pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;9737		lpfc_mbox_cmpl_put(phba, pmbox);9738	}9739	return MBX_NOT_FINISHED;9740}9741 9742/**9743 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command9744 * @phba: Pointer to HBA context object.9745 *9746 * The function blocks the posting of SLI4 asynchronous mailbox commands from9747 * the driver internal pending mailbox queue. It will then try to wait out the9748 * possible outstanding mailbox command before return.9749 *9750 * Returns:9751 * 	0 - the outstanding mailbox command completed; otherwise, the wait for9752 * 	the outstanding mailbox command timed out.9753 **/9754static int9755lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)9756{9757	struct lpfc_sli *psli = &phba->sli;9758	LPFC_MBOXQ_t *mboxq;9759	int rc = 0;9760	unsigned long timeout = 0;9761	u32 sli_flag;9762	u8 cmd, subsys, opcode;9763 9764	/* Mark the asynchronous mailbox command posting as blocked */9765	spin_lock_irq(&phba->hbalock);9766	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;9767	/* Determine how long we might wait for the active mailbox9768	 * command to be gracefully completed by firmware.9769	 */9770	if (phba->sli.mbox_active)9771		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,9772						phba->sli.mbox_active) *9773						1000) + jiffies;9774	spin_unlock_irq(&phba->hbalock);9775 9776	/* Make sure the mailbox is really active */9777	if (timeout)9778		lpfc_sli4_process_missed_mbox_completions(phba);9779 9780	/* Wait for the outstanding mailbox command to complete */9781	while (phba->sli.mbox_active) {9782		/* Check active mailbox complete status every 2ms */9783		msleep(2);9784		if (time_after(jiffies, timeout)) {9785			/* Timeout, mark the outstanding cmd not complete */9786 9787			/* Sanity check sli.mbox_active has not completed or9788			 * cancelled from another context during last 2ms sleep,9789			 * so take hbalock to be sure before logging.9790			 */9791			spin_lock_irq(&phba->hbalock);9792			if (phba->sli.mbox_active) {9793				mboxq = phba->sli.mbox_active;9794				cmd = mboxq->u.mb.mbxCommand;9795				subsys = lpfc_sli_config_mbox_subsys_get(phba,9796									 mboxq);9797				opcode = lpfc_sli_config_mbox_opcode_get(phba,9798									 mboxq);9799				sli_flag = psli->sli_flag;9800				spin_unlock_irq(&phba->hbalock);9801				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,9802						"2352 Mailbox command x%x "9803						"(x%x/x%x) sli_flag x%x could "9804						"not complete\n",9805						cmd, subsys, opcode,9806						sli_flag);9807			} else {9808				spin_unlock_irq(&phba->hbalock);9809			}9810 9811			rc = 1;9812			break;9813		}9814	}9815 9816	/* Can not cleanly block async mailbox command, fails it */9817	if (rc) {9818		spin_lock_irq(&phba->hbalock);9819		psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;9820		spin_unlock_irq(&phba->hbalock);9821	}9822	return rc;9823}9824 9825/**9826 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command9827 * @phba: Pointer to HBA context object.9828 *9829 * The function unblocks and resume posting of SLI4 asynchronous mailbox9830 * commands from the driver internal pending mailbox queue. It makes sure9831 * that there is no outstanding mailbox command before resuming posting9832 * asynchronous mailbox commands. If, for any reason, there is outstanding9833 * mailbox command, it will try to wait it out before resuming asynchronous9834 * mailbox command posting.9835 **/9836static void9837lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)9838{9839	struct lpfc_sli *psli = &phba->sli;9840 9841	spin_lock_irq(&phba->hbalock);9842	if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {9843		/* Asynchronous mailbox posting is not blocked, do nothing */9844		spin_unlock_irq(&phba->hbalock);9845		return;9846	}9847 9848	/* Outstanding synchronous mailbox command is guaranteed to be done,9849	 * successful or timeout, after timing-out the outstanding mailbox9850	 * command shall always be removed, so just unblock posting async9851	 * mailbox command and resume9852	 */9853	psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;9854	spin_unlock_irq(&phba->hbalock);9855 9856	/* wake up worker thread to post asynchronous mailbox command */9857	lpfc_worker_wake_up(phba);9858}9859 9860/**9861 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready9862 * @phba: Pointer to HBA context object.9863 * @mboxq: Pointer to mailbox object.9864 *9865 * The function waits for the bootstrap mailbox register ready bit from9866 * port for twice the regular mailbox command timeout value.9867 *9868 *      0 - no timeout on waiting for bootstrap mailbox register ready.9869 *      MBXERR_ERROR - wait for bootstrap mailbox register timed out or port9870 *                     is in an unrecoverable state.9871 **/9872static int9873lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)9874{9875	uint32_t db_ready;9876	unsigned long timeout;9877	struct lpfc_register bmbx_reg;9878	struct lpfc_register portstat_reg = {-1};9879 9880	/* Sanity check - there is no point to wait if the port is in an9881	 * unrecoverable state.9882	 */9883	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=9884	    LPFC_SLI_INTF_IF_TYPE_2) {9885		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,9886			       &portstat_reg.word0) ||9887		    lpfc_sli4_unrecoverable_port(&portstat_reg)) {9888			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,9889					"3858 Skipping bmbx ready because "9890					"Port Status x%x\n",9891					portstat_reg.word0);9892			return MBXERR_ERROR;9893		}9894	}9895 9896	timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)9897				   * 1000) + jiffies;9898 9899	do {9900		bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);9901		db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);9902		if (!db_ready)9903			mdelay(2);9904 9905		if (time_after(jiffies, timeout))9906			return MBXERR_ERROR;9907	} while (!db_ready);9908 9909	return 0;9910}9911 9912/**9913 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox9914 * @phba: Pointer to HBA context object.9915 * @mboxq: Pointer to mailbox object.9916 *9917 * The function posts a mailbox to the port.  The mailbox is expected9918 * to be comletely filled in and ready for the port to operate on it.9919 * This routine executes a synchronous completion operation on the9920 * mailbox by polling for its completion.9921 *9922 * The caller must not be holding any locks when calling this routine.9923 *9924 * Returns:9925 *	MBX_SUCCESS - mailbox posted successfully9926 *	Any of the MBX error values.9927 **/9928static int9929lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)9930{9931	int rc = MBX_SUCCESS;9932	unsigned long iflag;9933	uint32_t mcqe_status;9934	uint32_t mbx_cmnd;9935	struct lpfc_sli *psli = &phba->sli;9936	struct lpfc_mqe *mb = &mboxq->u.mqe;9937	struct lpfc_bmbx_create *mbox_rgn;9938	struct dma_address *dma_address;9939 9940	/*9941	 * Only one mailbox can be active to the bootstrap mailbox region9942	 * at a time and there is no queueing provided.9943	 */9944	spin_lock_irqsave(&phba->hbalock, iflag);9945	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {9946		spin_unlock_irqrestore(&phba->hbalock, iflag);9947		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,9948				"(%d):2532 Mailbox command x%x (x%x/x%x) "9949				"cannot issue Data: x%x x%x\n",9950				mboxq->vport ? mboxq->vport->vpi : 0,9951				mboxq->u.mb.mbxCommand,9952				lpfc_sli_config_mbox_subsys_get(phba, mboxq),9953				lpfc_sli_config_mbox_opcode_get(phba, mboxq),9954				psli->sli_flag, MBX_POLL);9955		return MBXERR_ERROR;9956	}9957	/* The server grabs the token and owns it until release */9958	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;9959	phba->sli.mbox_active = mboxq;9960	spin_unlock_irqrestore(&phba->hbalock, iflag);9961 9962	/* wait for bootstrap mbox register for readyness */9963	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);9964	if (rc)9965		goto exit;9966	/*9967	 * Initialize the bootstrap memory region to avoid stale data areas9968	 * in the mailbox post.  Then copy the caller's mailbox contents to9969	 * the bmbx mailbox region.9970	 */9971	mbx_cmnd = bf_get(lpfc_mqe_command, mb);9972	memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));9973	lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,9974			       sizeof(struct lpfc_mqe));9975 9976	/* Post the high mailbox dma address to the port and wait for ready. */9977	dma_address = &phba->sli4_hba.bmbx.dma_address;9978	writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);9979 9980	/* wait for bootstrap mbox register for hi-address write done */9981	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);9982	if (rc)9983		goto exit;9984 9985	/* Post the low mailbox dma address to the port. */9986	writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);9987 9988	/* wait for bootstrap mbox register for low address write done */9989	rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);9990	if (rc)9991		goto exit;9992 9993	/*9994	 * Read the CQ to ensure the mailbox has completed.9995	 * If so, update the mailbox status so that the upper layers9996	 * can complete the request normally.9997	 */9998	lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,9999			       sizeof(struct lpfc_mqe));10000	mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;10001	lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,10002			       sizeof(struct lpfc_mcqe));10003	mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);10004	/*10005	 * When the CQE status indicates a failure and the mailbox status10006	 * indicates success then copy the CQE status into the mailbox status10007	 * (and prefix it with x4000).10008	 */10009	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {10010		if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)10011			bf_set(lpfc_mqe_status, mb,10012			       (LPFC_MBX_ERROR_RANGE | mcqe_status));10013		rc = MBXERR_ERROR;10014	} else10015		lpfc_sli4_swap_str(phba, mboxq);10016 10017	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,10018			"(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "10019			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"10020			" x%x x%x CQ: x%x x%x x%x x%x\n",10021			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,10022			lpfc_sli_config_mbox_subsys_get(phba, mboxq),10023			lpfc_sli_config_mbox_opcode_get(phba, mboxq),10024			bf_get(lpfc_mqe_status, mb),10025			mb->un.mb_words[0], mb->un.mb_words[1],10026			mb->un.mb_words[2], mb->un.mb_words[3],10027			mb->un.mb_words[4], mb->un.mb_words[5],10028			mb->un.mb_words[6], mb->un.mb_words[7],10029			mb->un.mb_words[8], mb->un.mb_words[9],10030			mb->un.mb_words[10], mb->un.mb_words[11],10031			mb->un.mb_words[12], mboxq->mcqe.word0,10032			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,10033			mboxq->mcqe.trailer);10034exit:10035	/* We are holding the token, no needed for lock when release */10036	spin_lock_irqsave(&phba->hbalock, iflag);10037	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;10038	phba->sli.mbox_active = NULL;10039	spin_unlock_irqrestore(&phba->hbalock, iflag);10040	return rc;10041}10042 10043/**10044 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware10045 * @phba: Pointer to HBA context object.10046 * @mboxq: Pointer to mailbox object.10047 * @flag: Flag indicating how the mailbox need to be processed.10048 *10049 * This function is called by discovery code and HBA management code to submit10050 * a mailbox command to firmware with SLI-4 interface spec.10051 *10052 * Return codes the caller owns the mailbox command after the return of the10053 * function.10054 **/10055static int10056lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,10057		       uint32_t flag)10058{10059	struct lpfc_sli *psli = &phba->sli;10060	unsigned long iflags;10061	int rc;10062 10063	/* dump from issue mailbox command if setup */10064	lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);10065 10066	rc = lpfc_mbox_dev_check(phba);10067	if (unlikely(rc)) {10068		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,10069				"(%d):2544 Mailbox command x%x (x%x/x%x) "10070				"cannot issue Data: x%x x%x\n",10071				mboxq->vport ? mboxq->vport->vpi : 0,10072				mboxq->u.mb.mbxCommand,10073				lpfc_sli_config_mbox_subsys_get(phba, mboxq),10074				lpfc_sli_config_mbox_opcode_get(phba, mboxq),10075				psli->sli_flag, flag);10076		goto out_not_finished;10077	}10078 10079	/* Detect polling mode and jump to a handler */10080	if (!phba->sli4_hba.intr_enable) {10081		if (flag == MBX_POLL)10082			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);10083		else10084			rc = -EIO;10085		if (rc != MBX_SUCCESS)10086			lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,10087					"(%d):2541 Mailbox command x%x "10088					"(x%x/x%x) failure: "10089					"mqe_sta: x%x mcqe_sta: x%x/x%x "10090					"Data: x%x x%x\n",10091					mboxq->vport ? mboxq->vport->vpi : 0,10092					mboxq->u.mb.mbxCommand,10093					lpfc_sli_config_mbox_subsys_get(phba,10094									mboxq),10095					lpfc_sli_config_mbox_opcode_get(phba,10096									mboxq),10097					bf_get(lpfc_mqe_status, &mboxq->u.mqe),10098					bf_get(lpfc_mcqe_status, &mboxq->mcqe),10099					bf_get(lpfc_mcqe_ext_status,10100					       &mboxq->mcqe),10101					psli->sli_flag, flag);10102		return rc;10103	} else if (flag == MBX_POLL) {10104		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,10105				"(%d):2542 Try to issue mailbox command "10106				"x%x (x%x/x%x) synchronously ahead of async "10107				"mailbox command queue: x%x x%x\n",10108				mboxq->vport ? mboxq->vport->vpi : 0,10109				mboxq->u.mb.mbxCommand,10110				lpfc_sli_config_mbox_subsys_get(phba, mboxq),10111				lpfc_sli_config_mbox_opcode_get(phba, mboxq),10112				psli->sli_flag, flag);10113		/* Try to block the asynchronous mailbox posting */10114		rc = lpfc_sli4_async_mbox_block(phba);10115		if (!rc) {10116			/* Successfully blocked, now issue sync mbox cmd */10117			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);10118			if (rc != MBX_SUCCESS)10119				lpfc_printf_log(phba, KERN_WARNING,10120					LOG_MBOX | LOG_SLI,10121					"(%d):2597 Sync Mailbox command "10122					"x%x (x%x/x%x) failure: "10123					"mqe_sta: x%x mcqe_sta: x%x/x%x "10124					"Data: x%x x%x\n",10125					mboxq->vport ? mboxq->vport->vpi : 0,10126					mboxq->u.mb.mbxCommand,10127					lpfc_sli_config_mbox_subsys_get(phba,10128									mboxq),10129					lpfc_sli_config_mbox_opcode_get(phba,10130									mboxq),10131					bf_get(lpfc_mqe_status, &mboxq->u.mqe),10132					bf_get(lpfc_mcqe_status, &mboxq->mcqe),10133					bf_get(lpfc_mcqe_ext_status,10134					       &mboxq->mcqe),10135					psli->sli_flag, flag);10136			/* Unblock the async mailbox posting afterward */10137			lpfc_sli4_async_mbox_unblock(phba);10138		}10139		return rc;10140	}10141 10142	/* Now, interrupt mode asynchronous mailbox command */10143	rc = lpfc_mbox_cmd_check(phba, mboxq);10144	if (rc) {10145		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,10146				"(%d):2543 Mailbox command x%x (x%x/x%x) "10147				"cannot issue Data: x%x x%x\n",10148				mboxq->vport ? mboxq->vport->vpi : 0,10149				mboxq->u.mb.mbxCommand,10150				lpfc_sli_config_mbox_subsys_get(phba, mboxq),10151				lpfc_sli_config_mbox_opcode_get(phba, mboxq),10152				psli->sli_flag, flag);10153		goto out_not_finished;10154	}10155 10156	/* Put the mailbox command to the driver internal FIFO */10157	psli->slistat.mbox_busy++;10158	spin_lock_irqsave(&phba->hbalock, iflags);10159	lpfc_mbox_put(phba, mboxq);10160	spin_unlock_irqrestore(&phba->hbalock, iflags);10161	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,10162			"(%d):0354 Mbox cmd issue - Enqueue Data: "10163			"x%x (x%x/x%x) x%x x%x x%x x%x\n",10164			mboxq->vport ? mboxq->vport->vpi : 0xffffff,10165			bf_get(lpfc_mqe_command, &mboxq->u.mqe),10166			lpfc_sli_config_mbox_subsys_get(phba, mboxq),10167			lpfc_sli_config_mbox_opcode_get(phba, mboxq),10168			mboxq->u.mb.un.varUnregLogin.rpi,10169			phba->pport->port_state,10170			psli->sli_flag, MBX_NOWAIT);10171	/* Wake up worker thread to transport mailbox command from head */10172	lpfc_worker_wake_up(phba);10173 10174	return MBX_BUSY;10175 10176out_not_finished:10177	return MBX_NOT_FINISHED;10178}10179 10180/**10181 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device10182 * @phba: Pointer to HBA context object.10183 *10184 * This function is called by worker thread to send a mailbox command to10185 * SLI4 HBA firmware.10186 *10187 **/10188int10189lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)10190{10191	struct lpfc_sli *psli = &phba->sli;10192	LPFC_MBOXQ_t *mboxq;10193	int rc = MBX_SUCCESS;10194	unsigned long iflags;10195	struct lpfc_mqe *mqe;10196	uint32_t mbx_cmnd;10197 10198	/* Check interrupt mode before post async mailbox command */10199	if (unlikely(!phba->sli4_hba.intr_enable))10200		return MBX_NOT_FINISHED;10201 10202	/* Check for mailbox command service token */10203	spin_lock_irqsave(&phba->hbalock, iflags);10204	if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {10205		spin_unlock_irqrestore(&phba->hbalock, iflags);10206		return MBX_NOT_FINISHED;10207	}10208	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {10209		spin_unlock_irqrestore(&phba->hbalock, iflags);10210		return MBX_NOT_FINISHED;10211	}10212	if (unlikely(phba->sli.mbox_active)) {10213		spin_unlock_irqrestore(&phba->hbalock, iflags);10214		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,10215				"0384 There is pending active mailbox cmd\n");10216		return MBX_NOT_FINISHED;10217	}10218	/* Take the mailbox command service token */10219	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;10220 10221	/* Get the next mailbox command from head of queue */10222	mboxq = lpfc_mbox_get(phba);10223 10224	/* If no more mailbox command waiting for post, we're done */10225	if (!mboxq) {10226		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;10227		spin_unlock_irqrestore(&phba->hbalock, iflags);10228		return MBX_SUCCESS;10229	}10230	phba->sli.mbox_active = mboxq;10231	spin_unlock_irqrestore(&phba->hbalock, iflags);10232 10233	/* Check device readiness for posting mailbox command */10234	rc = lpfc_mbox_dev_check(phba);10235	if (unlikely(rc))10236		/* Driver clean routine will clean up pending mailbox */10237		goto out_not_finished;10238 10239	/* Prepare the mbox command to be posted */10240	mqe = &mboxq->u.mqe;10241	mbx_cmnd = bf_get(lpfc_mqe_command, mqe);10242 10243	/* Start timer for the mbox_tmo and log some mailbox post messages */10244	mod_timer(&psli->mbox_tmo, (jiffies +10245		  msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));10246 10247	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,10248			"(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "10249			"x%x x%x\n",10250			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,10251			lpfc_sli_config_mbox_subsys_get(phba, mboxq),10252			lpfc_sli_config_mbox_opcode_get(phba, mboxq),10253			phba->pport->port_state, psli->sli_flag);10254 10255	if (mbx_cmnd != MBX_HEARTBEAT) {10256		if (mboxq->vport) {10257			lpfc_debugfs_disc_trc(mboxq->vport,10258				LPFC_DISC_TRC_MBOX_VPORT,10259				"MBOX Send vport: cmd:x%x mb:x%x x%x",10260				mbx_cmnd, mqe->un.mb_words[0],10261				mqe->un.mb_words[1]);10262		} else {10263			lpfc_debugfs_disc_trc(phba->pport,10264				LPFC_DISC_TRC_MBOX,10265				"MBOX Send: cmd:x%x mb:x%x x%x",10266				mbx_cmnd, mqe->un.mb_words[0],10267				mqe->un.mb_words[1]);10268		}10269	}10270	psli->slistat.mbox_cmd++;10271 10272	/* Post the mailbox command to the port */10273	rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);10274	if (rc != MBX_SUCCESS) {10275		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,10276				"(%d):2533 Mailbox command x%x (x%x/x%x) "10277				"cannot issue Data: x%x x%x\n",10278				mboxq->vport ? mboxq->vport->vpi : 0,10279				mboxq->u.mb.mbxCommand,10280				lpfc_sli_config_mbox_subsys_get(phba, mboxq),10281				lpfc_sli_config_mbox_opcode_get(phba, mboxq),10282				psli->sli_flag, MBX_NOWAIT);10283		goto out_not_finished;10284	}10285 10286	return rc;10287 10288out_not_finished:10289	spin_lock_irqsave(&phba->hbalock, iflags);10290	if (phba->sli.mbox_active) {10291		mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;10292		__lpfc_mbox_cmpl_put(phba, mboxq);10293		/* Release the token */10294		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;10295		phba->sli.mbox_active = NULL;10296	}10297	spin_unlock_irqrestore(&phba->hbalock, iflags);10298 10299	return MBX_NOT_FINISHED;10300}10301 10302/**10303 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command10304 * @phba: Pointer to HBA context object.10305 * @pmbox: Pointer to mailbox object.10306 * @flag: Flag indicating how the mailbox need to be processed.10307 *10308 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from10309 * the API jump table function pointer from the lpfc_hba struct.10310 *10311 * Return codes the caller owns the mailbox command after the return of the10312 * function.10313 **/10314int10315lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)10316{10317	return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);10318}10319 10320/**10321 * lpfc_mbox_api_table_setup - Set up mbox api function jump table10322 * @phba: The hba struct for which this call is being executed.10323 * @dev_grp: The HBA PCI-Device group number.10324 *10325 * This routine sets up the mbox interface API function jump table in @phba10326 * struct.10327 * Returns: 0 - success, -ENODEV - failure.10328 **/10329int10330lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)10331{10332 10333	switch (dev_grp) {10334	case LPFC_PCI_DEV_LP:10335		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;10336		phba->lpfc_sli_handle_slow_ring_event =10337				lpfc_sli_handle_slow_ring_event_s3;10338		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;10339		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;10340		phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;10341		break;10342	case LPFC_PCI_DEV_OC:10343		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;10344		phba->lpfc_sli_handle_slow_ring_event =10345				lpfc_sli_handle_slow_ring_event_s4;10346		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;10347		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;10348		phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;10349		break;10350	default:10351		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,10352				"1420 Invalid HBA PCI-device group: 0x%x\n",10353				dev_grp);10354		return -ENODEV;10355	}10356	return 0;10357}10358 10359/**10360 * __lpfc_sli_ringtx_put - Add an iocb to the txq10361 * @phba: Pointer to HBA context object.10362 * @pring: Pointer to driver SLI ring object.10363 * @piocb: Pointer to address of newly added command iocb.10364 *10365 * This function is called with hbalock held for SLI3 ports or10366 * the ring lock held for SLI4 ports to add a command10367 * iocb to the txq when SLI layer cannot submit the command iocb10368 * to the ring.10369 **/10370void10371__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,10372		    struct lpfc_iocbq *piocb)10373{10374	if (phba->sli_rev == LPFC_SLI_REV4)10375		lockdep_assert_held(&pring->ring_lock);10376	else10377		lockdep_assert_held(&phba->hbalock);10378	/* Insert the caller's iocb in the txq tail for later processing. */10379	list_add_tail(&piocb->list, &pring->txq);10380}10381 10382/**10383 * lpfc_sli_next_iocb - Get the next iocb in the txq10384 * @phba: Pointer to HBA context object.10385 * @pring: Pointer to driver SLI ring object.10386 * @piocb: Pointer to address of newly added command iocb.10387 *10388 * This function is called with hbalock held before a new10389 * iocb is submitted to the firmware. This function checks10390 * txq to flush the iocbs in txq to Firmware before10391 * submitting new iocbs to the Firmware.10392 * If there are iocbs in the txq which need to be submitted10393 * to firmware, lpfc_sli_next_iocb returns the first element10394 * of the txq after dequeuing it from txq.10395 * If there is no iocb in the txq then the function will return10396 * *piocb and *piocb is set to NULL. Caller needs to check10397 * *piocb to find if there are more commands in the txq.10398 **/10399static struct lpfc_iocbq *10400lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,10401		   struct lpfc_iocbq **piocb)10402{10403	struct lpfc_iocbq * nextiocb;10404 10405	lockdep_assert_held(&phba->hbalock);10406 10407	nextiocb = lpfc_sli_ringtx_get(phba, pring);10408	if (!nextiocb) {10409		nextiocb = *piocb;10410		*piocb = NULL;10411	}10412 10413	return nextiocb;10414}10415 10416/**10417 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb10418 * @phba: Pointer to HBA context object.10419 * @ring_number: SLI ring number to issue iocb on.10420 * @piocb: Pointer to command iocb.10421 * @flag: Flag indicating if this command can be put into txq.10422 *10423 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue10424 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is10425 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT10426 * flag is turned on, the function returns IOCB_ERROR. When the link is down,10427 * this function allows only iocbs for posting buffers. This function finds10428 * next available slot in the command ring and posts the command to the10429 * available slot and writes the port attention register to request HBA start10430 * processing new iocb. If there is no slot available in the ring and10431 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise10432 * the function returns IOCB_BUSY.10433 *10434 * This function is called with hbalock held. The function will return success10435 * after it successfully submit the iocb to firmware or after adding to the10436 * txq.10437 **/10438static int10439__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,10440		    struct lpfc_iocbq *piocb, uint32_t flag)10441{10442	struct lpfc_iocbq *nextiocb;10443	IOCB_t *iocb;10444	struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];10445 10446	lockdep_assert_held(&phba->hbalock);10447 10448	if (piocb->cmd_cmpl && (!piocb->vport) &&10449	   (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&10450	   (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {10451		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,10452				"1807 IOCB x%x failed. No vport\n",10453				piocb->iocb.ulpCommand);10454		dump_stack();10455		return IOCB_ERROR;10456	}10457 10458 10459	/* If the PCI channel is in offline state, do not post iocbs. */10460	if (unlikely(pci_channel_offline(phba->pcidev)))10461		return IOCB_ERROR;10462 10463	/* If HBA has a deferred error attention, fail the iocb. */10464	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))10465		return IOCB_ERROR;10466 10467	/*10468	 * We should never get an IOCB if we are in a < LINK_DOWN state10469	 */10470	if (unlikely(phba->link_state < LPFC_LINK_DOWN))10471		return IOCB_ERROR;10472 10473	/*10474	 * Check to see if we are blocking IOCB processing because of a10475	 * outstanding event.10476	 */10477	if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))10478		goto iocb_busy;10479 10480	if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {10481		/*10482		 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF10483		 * can be issued if the link is not up.10484		 */10485		switch (piocb->iocb.ulpCommand) {10486		case CMD_QUE_RING_BUF_CN:10487		case CMD_QUE_RING_BUF64_CN:10488			/*10489			 * For IOCBs, like QUE_RING_BUF, that have no rsp ring10490			 * completion, cmd_cmpl MUST be 0.10491			 */10492			if (piocb->cmd_cmpl)10493				piocb->cmd_cmpl = NULL;10494			fallthrough;10495		case CMD_CREATE_XRI_CR:10496		case CMD_CLOSE_XRI_CN:10497		case CMD_CLOSE_XRI_CX:10498			break;10499		default:10500			goto iocb_busy;10501		}10502 10503	/*10504	 * For FCP commands, we must be in a state where we can process link10505	 * attention events.10506	 */10507	} else if (unlikely(pring->ringno == LPFC_FCP_RING &&10508			    !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {10509		goto iocb_busy;10510	}10511 10512	while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&10513	       (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))10514		lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);10515 10516	if (iocb)10517		lpfc_sli_update_ring(phba, pring);10518	else10519		lpfc_sli_update_full_ring(phba, pring);10520 10521	if (!piocb)10522		return IOCB_SUCCESS;10523 10524	goto out_busy;10525 10526 iocb_busy:10527	pring->stats.iocb_cmd_delay++;10528 10529 out_busy:10530 10531	if (!(flag & SLI_IOCB_RET_IOCB)) {10532		__lpfc_sli_ringtx_put(phba, pring, piocb);10533		return IOCB_SUCCESS;10534	}10535 10536	return IOCB_BUSY;10537}10538 10539/**10540 * __lpfc_sli_issue_fcp_io_s3 - SLI3 device for sending fcp io iocb10541 * @phba: Pointer to HBA context object.10542 * @ring_number: SLI ring number to issue wqe on.10543 * @piocb: Pointer to command iocb.10544 * @flag: Flag indicating if this command can be put into txq.10545 *10546 * __lpfc_sli_issue_fcp_io_s3 is wrapper function to invoke lockless func to10547 * send  an iocb command to an HBA with SLI-3 interface spec.10548 *10549 * This function takes the hbalock before invoking the lockless version.10550 * The function will return success after it successfully submit the wqe to10551 * firmware or after adding to the txq.10552 **/10553static int10554__lpfc_sli_issue_fcp_io_s3(struct lpfc_hba *phba, uint32_t ring_number,10555			   struct lpfc_iocbq *piocb, uint32_t flag)10556{10557	unsigned long iflags;10558	int rc;10559 10560	spin_lock_irqsave(&phba->hbalock, iflags);10561	rc = __lpfc_sli_issue_iocb_s3(phba, ring_number, piocb, flag);10562	spin_unlock_irqrestore(&phba->hbalock, iflags);10563 10564	return rc;10565}10566 10567/**10568 * __lpfc_sli_issue_fcp_io_s4 - SLI4 device for sending fcp io wqe10569 * @phba: Pointer to HBA context object.10570 * @ring_number: SLI ring number to issue wqe on.10571 * @piocb: Pointer to command iocb.10572 * @flag: Flag indicating if this command can be put into txq.10573 *10574 * __lpfc_sli_issue_fcp_io_s4 is used by other functions in the driver to issue10575 * an wqe command to an HBA with SLI-4 interface spec.10576 *10577 * This function is a lockless version. The function will return success10578 * after it successfully submit the wqe to firmware or after adding to the10579 * txq.10580 **/10581static int10582__lpfc_sli_issue_fcp_io_s4(struct lpfc_hba *phba, uint32_t ring_number,10583			   struct lpfc_iocbq *piocb, uint32_t flag)10584{10585	struct lpfc_io_buf *lpfc_cmd = piocb->io_buf;10586 10587	lpfc_prep_embed_io(phba, lpfc_cmd);10588	return lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, piocb);10589}10590 10591void10592lpfc_prep_embed_io(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)10593{10594	struct lpfc_iocbq *piocb = &lpfc_cmd->cur_iocbq;10595	union lpfc_wqe128 *wqe = &lpfc_cmd->cur_iocbq.wqe;10596	struct sli4_sge_le *sgl;10597	u32 type_size;10598 10599	/* 128 byte wqe support here */10600	sgl = (struct sli4_sge_le *)lpfc_cmd->dma_sgl;10601 10602	if (phba->fcp_embed_io) {10603		struct fcp_cmnd *fcp_cmnd;10604		u32 *ptr;10605 10606		fcp_cmnd = lpfc_cmd->fcp_cmnd;10607 10608		/* Word 0-2 - FCP_CMND */10609		type_size = le32_to_cpu(sgl->sge_len);10610		type_size |= ULP_BDE64_TYPE_BDE_IMMED;10611		wqe->generic.bde.tus.w = type_size;10612		wqe->generic.bde.addrHigh = 0;10613		wqe->generic.bde.addrLow =  72;  /* Word 18 */10614 10615		bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);10616		bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);10617 10618		/* Word 18-29  FCP CMND Payload */10619		ptr = &wqe->words[18];10620		lpfc_sli_pcimem_bcopy(fcp_cmnd, ptr, le32_to_cpu(sgl->sge_len));10621	} else {10622		/* Word 0-2 - Inline BDE */10623		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;10624		wqe->generic.bde.tus.f.bdeSize = le32_to_cpu(sgl->sge_len);10625		wqe->generic.bde.addrHigh = le32_to_cpu(sgl->addr_hi);10626		wqe->generic.bde.addrLow = le32_to_cpu(sgl->addr_lo);10627 10628		/* Word 10 */10629		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);10630		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);10631	}10632 10633	/* add the VMID tags as per switch response */10634	if (unlikely(piocb->cmd_flag & LPFC_IO_VMID)) {10635		if (phba->pport->vmid_flag & LPFC_VMID_TYPE_PRIO) {10636			bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);10637			bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,10638					(piocb->vmid_tag.cs_ctl_vmid));10639		} else if (phba->cfg_vmid_app_header) {10640			bf_set(wqe_appid, &wqe->fcp_iwrite.wqe_com, 1);10641			bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);10642			wqe->words[31] = piocb->vmid_tag.app_id;10643		}10644	}10645}10646 10647/**10648 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb10649 * @phba: Pointer to HBA context object.10650 * @ring_number: SLI ring number to issue iocb on.10651 * @piocb: Pointer to command iocb.10652 * @flag: Flag indicating if this command can be put into txq.10653 *10654 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue10655 * an iocb command to an HBA with SLI-4 interface spec.10656 *10657 * This function is called with ringlock held. The function will return success10658 * after it successfully submit the iocb to firmware or after adding to the10659 * txq.10660 **/10661static int10662__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,10663			 struct lpfc_iocbq *piocb, uint32_t flag)10664{10665	struct lpfc_sglq *sglq;10666	union lpfc_wqe128 *wqe;10667	struct lpfc_queue *wq;10668	struct lpfc_sli_ring *pring;10669	u32 ulp_command = get_job_cmnd(phba, piocb);10670 10671	/* Get the WQ */10672	if ((piocb->cmd_flag & LPFC_IO_FCP) ||10673	    (piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {10674		wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;10675	} else {10676		wq = phba->sli4_hba.els_wq;10677	}10678 10679	/* Get corresponding ring */10680	pring = wq->pring;10681 10682	/*10683	 * The WQE can be either 64 or 128 bytes,10684	 */10685 10686	lockdep_assert_held(&pring->ring_lock);10687	wqe = &piocb->wqe;10688	if (piocb->sli4_xritag == NO_XRI) {10689		if (ulp_command == CMD_ABORT_XRI_CX)10690			sglq = NULL;10691		else {10692			sglq = __lpfc_sli_get_els_sglq(phba, piocb);10693			if (!sglq) {10694				if (!(flag & SLI_IOCB_RET_IOCB)) {10695					__lpfc_sli_ringtx_put(phba,10696							pring,10697							piocb);10698					return IOCB_SUCCESS;10699				} else {10700					return IOCB_BUSY;10701				}10702			}10703		}10704	} else if (piocb->cmd_flag &  LPFC_IO_FCP) {10705		/* These IO's already have an XRI and a mapped sgl. */10706		sglq = NULL;10707	}10708	else {10709		/*10710		 * This is a continuation of a commandi,(CX) so this10711		 * sglq is on the active list10712		 */10713		sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);10714		if (!sglq)10715			return IOCB_ERROR;10716	}10717 10718	if (sglq) {10719		piocb->sli4_lxritag = sglq->sli4_lxritag;10720		piocb->sli4_xritag = sglq->sli4_xritag;10721 10722		/* ABTS sent by initiator to CT exchange, the10723		 * RX_ID field will be filled with the newly10724		 * allocated responder XRI.10725		 */10726		if (ulp_command == CMD_XMIT_BLS_RSP64_CX &&10727		    piocb->abort_bls == LPFC_ABTS_UNSOL_INT)10728			bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,10729			       piocb->sli4_xritag);10730 10731		bf_set(wqe_xri_tag, &wqe->generic.wqe_com,10732		       piocb->sli4_xritag);10733 10734		if (lpfc_wqe_bpl2sgl(phba, piocb, sglq) == NO_XRI)10735			return IOCB_ERROR;10736	}10737 10738	if (lpfc_sli4_wq_put(wq, wqe))10739		return IOCB_ERROR;10740 10741	lpfc_sli_ringtxcmpl_put(phba, pring, piocb);10742 10743	return 0;10744}10745 10746/*10747 * lpfc_sli_issue_fcp_io - Wrapper func for issuing fcp i/o10748 *10749 * This routine wraps the actual fcp i/o function for issusing WQE for sli-410750 * or IOCB for sli-3  function.10751 * pointer from the lpfc_hba struct.10752 *10753 * Return codes:10754 * IOCB_ERROR - Error10755 * IOCB_SUCCESS - Success10756 * IOCB_BUSY - Busy10757 **/10758int10759lpfc_sli_issue_fcp_io(struct lpfc_hba *phba, uint32_t ring_number,10760		      struct lpfc_iocbq *piocb, uint32_t flag)10761{10762	return phba->__lpfc_sli_issue_fcp_io(phba, ring_number, piocb, flag);10763}10764 10765/*10766 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb10767 *10768 * This routine wraps the actual lockless version for issusing IOCB function10769 * pointer from the lpfc_hba struct.10770 *10771 * Return codes:10772 * IOCB_ERROR - Error10773 * IOCB_SUCCESS - Success10774 * IOCB_BUSY - Busy10775 **/10776int10777__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,10778		struct lpfc_iocbq *piocb, uint32_t flag)10779{10780	return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);10781}10782 10783static void10784__lpfc_sli_prep_els_req_rsp_s3(struct lpfc_iocbq *cmdiocbq,10785			       struct lpfc_vport *vport,10786			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,10787			       u32 elscmd, u8 tmo, u8 expect_rsp)10788{10789	struct lpfc_hba *phba = vport->phba;10790	IOCB_t *cmd;10791 10792	cmd = &cmdiocbq->iocb;10793	memset(cmd, 0, sizeof(*cmd));10794 10795	cmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);10796	cmd->un.elsreq64.bdl.addrLow = putPaddrLow(bmp->phys);10797	cmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;10798 10799	if (expect_rsp) {10800		cmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));10801		cmd->un.elsreq64.remoteID = did; /* DID */10802		cmd->ulpCommand = CMD_ELS_REQUEST64_CR;10803		cmd->ulpTimeout = tmo;10804	} else {10805		cmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);10806		cmd->un.genreq64.xmit_els_remoteID = did; /* DID */10807		cmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;10808		cmd->ulpPU = PARM_NPIV_DID;10809	}10810	cmd->ulpBdeCount = 1;10811	cmd->ulpLe = 1;10812	cmd->ulpClass = CLASS3;10813 10814	/* If we have NPIV enabled, we want to send ELS traffic by VPI. */10815	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {10816		if (expect_rsp) {10817			cmd->un.elsreq64.myID = vport->fc_myDID;10818 10819			/* For ELS_REQUEST64_CR, use the VPI by default */10820			cmd->ulpContext = phba->vpi_ids[vport->vpi];10821		}10822 10823		cmd->ulpCt_h = 0;10824		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */10825		if (elscmd == ELS_CMD_ECHO)10826			cmd->ulpCt_l = 0; /* context = invalid RPI */10827		else10828			cmd->ulpCt_l = 1; /* context = VPI */10829	}10830}10831 10832static void10833__lpfc_sli_prep_els_req_rsp_s4(struct lpfc_iocbq *cmdiocbq,10834			       struct lpfc_vport *vport,10835			       struct lpfc_dmabuf *bmp, u16 cmd_size, u32 did,10836			       u32 elscmd, u8 tmo, u8 expect_rsp)10837{10838	struct lpfc_hba  *phba = vport->phba;10839	union lpfc_wqe128 *wqe;10840	struct ulp_bde64_le *bde;10841	u8 els_id;10842 10843	wqe = &cmdiocbq->wqe;10844	memset(wqe, 0, sizeof(*wqe));10845 10846	/* Word 0 - 2 BDE */10847	bde = (struct ulp_bde64_le *)&wqe->generic.bde;10848	bde->addr_low = cpu_to_le32(putPaddrLow(bmp->phys));10849	bde->addr_high = cpu_to_le32(putPaddrHigh(bmp->phys));10850	bde->type_size = cpu_to_le32(cmd_size);10851	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);10852 10853	if (expect_rsp) {10854		bf_set(wqe_cmnd, &wqe->els_req.wqe_com, CMD_ELS_REQUEST64_WQE);10855 10856		/* Transfer length */10857		wqe->els_req.payload_len = cmd_size;10858		wqe->els_req.max_response_payload_len = FCELSSIZE;10859 10860		/* DID */10861		bf_set(wqe_els_did, &wqe->els_req.wqe_dest, did);10862 10863		/* Word 11 - ELS_ID */10864		switch (elscmd) {10865		case ELS_CMD_PLOGI:10866			els_id = LPFC_ELS_ID_PLOGI;10867			break;10868		case ELS_CMD_FLOGI:10869			els_id = LPFC_ELS_ID_FLOGI;10870			break;10871		case ELS_CMD_LOGO:10872			els_id = LPFC_ELS_ID_LOGO;10873			break;10874		case ELS_CMD_FDISC:10875			if (!vport->fc_myDID) {10876				els_id = LPFC_ELS_ID_FDISC;10877				break;10878			}10879			fallthrough;10880		default:10881			els_id = LPFC_ELS_ID_DEFAULT;10882			break;10883		}10884 10885		bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);10886	} else {10887		/* DID */10888		bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest, did);10889 10890		/* Transfer length */10891		wqe->xmit_els_rsp.response_payload_len = cmd_size;10892 10893		bf_set(wqe_cmnd, &wqe->xmit_els_rsp.wqe_com,10894		       CMD_XMIT_ELS_RSP64_WQE);10895	}10896 10897	bf_set(wqe_tmo, &wqe->generic.wqe_com, tmo);10898	bf_set(wqe_reqtag, &wqe->generic.wqe_com, cmdiocbq->iotag);10899	bf_set(wqe_class, &wqe->generic.wqe_com, CLASS3);10900 10901	/* If we have NPIV enabled, we want to send ELS traffic by VPI.10902	 * For SLI4, since the driver controls VPIs we also want to include10903	 * all ELS pt2pt protocol traffic as well.10904	 */10905	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||10906	    test_bit(FC_PT2PT, &vport->fc_flag)) {10907		if (expect_rsp) {10908			bf_set(els_req64_sid, &wqe->els_req, vport->fc_myDID);10909 10910			/* For ELS_REQUEST64_WQE, use the VPI by default */10911			bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,10912			       phba->vpi_ids[vport->vpi]);10913		}10914 10915		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */10916		if (elscmd == ELS_CMD_ECHO)10917			bf_set(wqe_ct, &wqe->generic.wqe_com, 0);10918		else10919			bf_set(wqe_ct, &wqe->generic.wqe_com, 1);10920	}10921}10922 10923void10924lpfc_sli_prep_els_req_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,10925			  struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,10926			  u16 cmd_size, u32 did, u32 elscmd, u8 tmo,10927			  u8 expect_rsp)10928{10929	phba->__lpfc_sli_prep_els_req_rsp(cmdiocbq, vport, bmp, cmd_size, did,10930					  elscmd, tmo, expect_rsp);10931}10932 10933static void10934__lpfc_sli_prep_gen_req_s3(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,10935			   u16 rpi, u32 num_entry, u8 tmo)10936{10937	IOCB_t *cmd;10938 10939	cmd = &cmdiocbq->iocb;10940	memset(cmd, 0, sizeof(*cmd));10941 10942	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);10943	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);10944	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;10945	cmd->un.genreq64.bdl.bdeSize = num_entry * sizeof(struct ulp_bde64);10946 10947	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;10948	cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;10949	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);10950 10951	cmd->ulpContext = rpi;10952	cmd->ulpClass = CLASS3;10953	cmd->ulpCommand = CMD_GEN_REQUEST64_CR;10954	cmd->ulpBdeCount = 1;10955	cmd->ulpLe = 1;10956	cmd->ulpOwner = OWN_CHIP;10957	cmd->ulpTimeout = tmo;10958}10959 10960static void10961__lpfc_sli_prep_gen_req_s4(struct lpfc_iocbq *cmdiocbq, struct lpfc_dmabuf *bmp,10962			   u16 rpi, u32 num_entry, u8 tmo)10963{10964	union lpfc_wqe128 *cmdwqe;10965	struct ulp_bde64_le *bde, *bpl;10966	u32 xmit_len = 0, total_len = 0, size, type, i;10967 10968	cmdwqe = &cmdiocbq->wqe;10969	memset(cmdwqe, 0, sizeof(*cmdwqe));10970 10971	/* Calculate total_len and xmit_len */10972	bpl = (struct ulp_bde64_le *)bmp->virt;10973	for (i = 0; i < num_entry; i++) {10974		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;10975		total_len += size;10976	}10977	for (i = 0; i < num_entry; i++) {10978		size = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_SIZE_MASK;10979		type = le32_to_cpu(bpl[i].type_size) & ULP_BDE64_TYPE_MASK;10980		if (type != ULP_BDE64_TYPE_BDE_64)10981			break;10982		xmit_len += size;10983	}10984 10985	/* Words 0 - 2 */10986	bde = (struct ulp_bde64_le *)&cmdwqe->generic.bde;10987	bde->addr_low = bpl->addr_low;10988	bde->addr_high = bpl->addr_high;10989	bde->type_size = cpu_to_le32(xmit_len);10990	bde->type_size |= cpu_to_le32(ULP_BDE64_TYPE_BDE_64);10991 10992	/* Word 3 */10993	cmdwqe->gen_req.request_payload_len = xmit_len;10994 10995	/* Word 5 */10996	bf_set(wqe_type, &cmdwqe->gen_req.wge_ctl, FC_TYPE_CT);10997	bf_set(wqe_rctl, &cmdwqe->gen_req.wge_ctl, FC_RCTL_DD_UNSOL_CTL);10998	bf_set(wqe_si, &cmdwqe->gen_req.wge_ctl, 1);10999	bf_set(wqe_la, &cmdwqe->gen_req.wge_ctl, 1);11000 11001	/* Word 6 */11002	bf_set(wqe_ctxt_tag, &cmdwqe->gen_req.wqe_com, rpi);11003 11004	/* Word 7 */11005	bf_set(wqe_tmo, &cmdwqe->gen_req.wqe_com, tmo);11006	bf_set(wqe_class, &cmdwqe->gen_req.wqe_com, CLASS3);11007	bf_set(wqe_cmnd, &cmdwqe->gen_req.wqe_com, CMD_GEN_REQUEST64_CR);11008	bf_set(wqe_ct, &cmdwqe->gen_req.wqe_com, SLI4_CT_RPI);11009 11010	/* Word 12 */11011	cmdwqe->gen_req.max_response_payload_len = total_len - xmit_len;11012}11013 11014void11015lpfc_sli_prep_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,11016		      struct lpfc_dmabuf *bmp, u16 rpi, u32 num_entry, u8 tmo)11017{11018	phba->__lpfc_sli_prep_gen_req(cmdiocbq, bmp, rpi, num_entry, tmo);11019}11020 11021static void11022__lpfc_sli_prep_xmit_seq64_s3(struct lpfc_iocbq *cmdiocbq,11023			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,11024			      u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)11025{11026	IOCB_t *icmd;11027 11028	icmd = &cmdiocbq->iocb;11029	memset(icmd, 0, sizeof(*icmd));11030 11031	icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);11032	icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);11033	icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;11034	icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));11035	icmd->un.xseq64.w5.hcsw.Fctl = LA;11036	if (last_seq)11037		icmd->un.xseq64.w5.hcsw.Fctl |= LS;11038	icmd->un.xseq64.w5.hcsw.Dfctl = 0;11039	icmd->un.xseq64.w5.hcsw.Rctl = rctl;11040	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;11041 11042	icmd->ulpBdeCount = 1;11043	icmd->ulpLe = 1;11044	icmd->ulpClass = CLASS3;11045 11046	switch (cr_cx_cmd) {11047	case CMD_XMIT_SEQUENCE64_CR:11048		icmd->ulpContext = rpi;11049		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;11050		break;11051	case CMD_XMIT_SEQUENCE64_CX:11052		icmd->ulpContext = ox_id;11053		icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;11054		break;11055	default:11056		break;11057	}11058}11059 11060static void11061__lpfc_sli_prep_xmit_seq64_s4(struct lpfc_iocbq *cmdiocbq,11062			      struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,11063			      u32 full_size, u8 rctl, u8 last_seq, u8 cr_cx_cmd)11064{11065	union lpfc_wqe128 *wqe;11066	struct ulp_bde64 *bpl;11067 11068	wqe = &cmdiocbq->wqe;11069	memset(wqe, 0, sizeof(*wqe));11070 11071	/* Words 0 - 2 */11072	bpl = (struct ulp_bde64 *)bmp->virt;11073	wqe->xmit_sequence.bde.addrHigh = bpl->addrHigh;11074	wqe->xmit_sequence.bde.addrLow = bpl->addrLow;11075	wqe->xmit_sequence.bde.tus.w = bpl->tus.w;11076 11077	/* Word 5 */11078	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, last_seq);11079	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 1);11080	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);11081	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, rctl);11082	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_CT);11083 11084	/* Word 6 */11085	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com, rpi);11086 11087	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,11088	       CMD_XMIT_SEQUENCE64_WQE);11089 11090	/* Word 7 */11091	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);11092 11093	/* Word 9 */11094	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ox_id);11095 11096	if (cmdiocbq->cmd_flag & (LPFC_IO_LIBDFC | LPFC_IO_LOOPBACK)) {11097		/* Word 10 */11098		if (cmdiocbq->cmd_flag & LPFC_IO_VMID) {11099			bf_set(wqe_appid, &wqe->xmit_sequence.wqe_com, 1);11100			bf_set(wqe_wqes, &wqe->xmit_sequence.wqe_com, 1);11101			wqe->words[31] = LOOPBACK_SRC_APPID;11102		}11103 11104		/* Word 12 */11105		wqe->xmit_sequence.xmit_len = full_size;11106	}11107	else11108		wqe->xmit_sequence.xmit_len =11109			wqe->xmit_sequence.bde.tus.f.bdeSize;11110}11111 11112void11113lpfc_sli_prep_xmit_seq64(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,11114			 struct lpfc_dmabuf *bmp, u16 rpi, u16 ox_id,11115			 u32 num_entry, u8 rctl, u8 last_seq, u8 cr_cx_cmd)11116{11117	phba->__lpfc_sli_prep_xmit_seq64(cmdiocbq, bmp, rpi, ox_id, num_entry,11118					 rctl, last_seq, cr_cx_cmd);11119}11120 11121static void11122__lpfc_sli_prep_abort_xri_s3(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,11123			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,11124			     bool wqec)11125{11126	IOCB_t *icmd = NULL;11127 11128	icmd = &cmdiocbq->iocb;11129	memset(icmd, 0, sizeof(*icmd));11130 11131	/* Word 5 */11132	icmd->un.acxri.abortContextTag = ulp_context;11133	icmd->un.acxri.abortIoTag = iotag;11134 11135	if (ia) {11136		/* Word 7 */11137		icmd->ulpCommand = CMD_CLOSE_XRI_CN;11138	} else {11139		/* Word 3 */11140		icmd->un.acxri.abortType = ABORT_TYPE_ABTS;11141 11142		/* Word 7 */11143		icmd->ulpClass = ulp_class;11144		icmd->ulpCommand = CMD_ABORT_XRI_CN;11145	}11146 11147	/* Word 7 */11148	icmd->ulpLe = 1;11149}11150 11151static void11152__lpfc_sli_prep_abort_xri_s4(struct lpfc_iocbq *cmdiocbq, u16 ulp_context,11153			     u16 iotag, u8 ulp_class, u16 cqid, bool ia,11154			     bool wqec)11155{11156	union lpfc_wqe128 *wqe;11157 11158	wqe = &cmdiocbq->wqe;11159	memset(wqe, 0, sizeof(*wqe));11160 11161	/* Word 3 */11162	bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);11163	if (ia)11164		bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);11165	else11166		bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);11167 11168	/* Word 7 */11169	bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_WQE);11170 11171	/* Word 8 */11172	wqe->abort_cmd.wqe_com.abort_tag = ulp_context;11173 11174	/* Word 9 */11175	bf_set(wqe_reqtag, &wqe->abort_cmd.wqe_com, iotag);11176 11177	/* Word 10 */11178	bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);11179 11180	/* Word 11 */11181	if (wqec)11182		bf_set(wqe_wqec, &wqe->abort_cmd.wqe_com, 1);11183	bf_set(wqe_cqid, &wqe->abort_cmd.wqe_com, cqid);11184	bf_set(wqe_cmd_type, &wqe->abort_cmd.wqe_com, OTHER_COMMAND);11185}11186 11187void11188lpfc_sli_prep_abort_xri(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocbq,11189			u16 ulp_context, u16 iotag, u8 ulp_class, u16 cqid,11190			bool ia, bool wqec)11191{11192	phba->__lpfc_sli_prep_abort_xri(cmdiocbq, ulp_context, iotag, ulp_class,11193					cqid, ia, wqec);11194}11195 11196/**11197 * lpfc_sli_api_table_setup - Set up sli api function jump table11198 * @phba: The hba struct for which this call is being executed.11199 * @dev_grp: The HBA PCI-Device group number.11200 *11201 * This routine sets up the SLI interface API function jump table in @phba11202 * struct.11203 * Returns: 0 - success, -ENODEV - failure.11204 **/11205int11206lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)11207{11208 11209	switch (dev_grp) {11210	case LPFC_PCI_DEV_LP:11211		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;11212		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;11213		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s3;11214		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s3;11215		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s3;11216		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s3;11217		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s3;11218		break;11219	case LPFC_PCI_DEV_OC:11220		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;11221		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;11222		phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s4;11223		phba->__lpfc_sli_prep_els_req_rsp = __lpfc_sli_prep_els_req_rsp_s4;11224		phba->__lpfc_sli_prep_gen_req = __lpfc_sli_prep_gen_req_s4;11225		phba->__lpfc_sli_prep_xmit_seq64 = __lpfc_sli_prep_xmit_seq64_s4;11226		phba->__lpfc_sli_prep_abort_xri = __lpfc_sli_prep_abort_xri_s4;11227		break;11228	default:11229		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,11230				"1419 Invalid HBA PCI-device group: 0x%x\n",11231				dev_grp);11232		return -ENODEV;11233	}11234	return 0;11235}11236 11237/**11238 * lpfc_sli4_calc_ring - Calculates which ring to use11239 * @phba: Pointer to HBA context object.11240 * @piocb: Pointer to command iocb.11241 *11242 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on11243 * hba_wqidx, thus we need to calculate the corresponding ring.11244 * Since ABORTS must go on the same WQ of the command they are11245 * aborting, we use command's hba_wqidx.11246 */11247struct lpfc_sli_ring *11248lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)11249{11250	struct lpfc_io_buf *lpfc_cmd;11251 11252	if (piocb->cmd_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {11253		if (unlikely(!phba->sli4_hba.hdwq))11254			return NULL;11255		/*11256		 * for abort iocb hba_wqidx should already11257		 * be setup based on what work queue we used.11258		 */11259		if (!(piocb->cmd_flag & LPFC_USE_FCPWQIDX)) {11260			lpfc_cmd = piocb->io_buf;11261			piocb->hba_wqidx = lpfc_cmd->hdwq_no;11262		}11263		return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;11264	} else {11265		if (unlikely(!phba->sli4_hba.els_wq))11266			return NULL;11267		piocb->hba_wqidx = 0;11268		return phba->sli4_hba.els_wq->pring;11269	}11270}11271 11272inline void lpfc_sli4_poll_eq(struct lpfc_queue *eq)11273{11274	struct lpfc_hba *phba = eq->phba;11275 11276	/*11277	 * Unlocking an irq is one of the entry point to check11278	 * for re-schedule, but we are good for io submission11279	 * path as midlayer does a get_cpu to glue us in. Flush11280	 * out the invalidate queue so we can see the updated11281	 * value for flag.11282	 */11283	smp_rmb();11284 11285	if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)11286		/* We will not likely get the completion for the caller11287		 * during this iteration but i guess that's fine.11288		 * Future io's coming on this eq should be able to11289		 * pick it up.  As for the case of single io's, they11290		 * will be handled through a sched from polling timer11291		 * function which is currently triggered every 1msec.11292		 */11293		lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM,11294				     LPFC_QUEUE_WORK);11295}11296 11297/**11298 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb11299 * @phba: Pointer to HBA context object.11300 * @ring_number: Ring number11301 * @piocb: Pointer to command iocb.11302 * @flag: Flag indicating if this command can be put into txq.11303 *11304 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb11305 * function. This function gets the hbalock and calls11306 * __lpfc_sli_issue_iocb function and will return the error returned11307 * by __lpfc_sli_issue_iocb function. This wrapper is used by11308 * functions which do not hold hbalock.11309 **/11310int11311lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,11312		    struct lpfc_iocbq *piocb, uint32_t flag)11313{11314	struct lpfc_sli_ring *pring;11315	struct lpfc_queue *eq;11316	unsigned long iflags;11317	int rc;11318 11319	/* If the PCI channel is in offline state, do not post iocbs. */11320	if (unlikely(pci_channel_offline(phba->pcidev)))11321		return IOCB_ERROR;11322 11323	if (phba->sli_rev == LPFC_SLI_REV4) {11324		lpfc_sli_prep_wqe(phba, piocb);11325 11326		eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;11327 11328		pring = lpfc_sli4_calc_ring(phba, piocb);11329		if (unlikely(pring == NULL))11330			return IOCB_ERROR;11331 11332		spin_lock_irqsave(&pring->ring_lock, iflags);11333		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);11334		spin_unlock_irqrestore(&pring->ring_lock, iflags);11335 11336		lpfc_sli4_poll_eq(eq);11337	} else {11338		/* For now, SLI2/3 will still use hbalock */11339		spin_lock_irqsave(&phba->hbalock, iflags);11340		rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);11341		spin_unlock_irqrestore(&phba->hbalock, iflags);11342	}11343	return rc;11344}11345 11346/**11347 * lpfc_extra_ring_setup - Extra ring setup function11348 * @phba: Pointer to HBA context object.11349 *11350 * This function is called while driver attaches with the11351 * HBA to setup the extra ring. The extra ring is used11352 * only when driver needs to support target mode functionality11353 * or IP over FC functionalities.11354 *11355 * This function is called with no lock held. SLI3 only.11356 **/11357static int11358lpfc_extra_ring_setup( struct lpfc_hba *phba)11359{11360	struct lpfc_sli *psli;11361	struct lpfc_sli_ring *pring;11362 11363	psli = &phba->sli;11364 11365	/* Adjust cmd/rsp ring iocb entries more evenly */11366 11367	/* Take some away from the FCP ring */11368	pring = &psli->sli3_ring[LPFC_FCP_RING];11369	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;11370	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;11371	pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;11372	pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;11373 11374	/* and give them to the extra ring */11375	pring = &psli->sli3_ring[LPFC_EXTRA_RING];11376 11377	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;11378	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;11379	pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;11380	pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;11381 11382	/* Setup default profile for this ring */11383	pring->iotag_max = 4096;11384	pring->num_mask = 1;11385	pring->prt[0].profile = 0;      /* Mask 0 */11386	pring->prt[0].rctl = phba->cfg_multi_ring_rctl;11387	pring->prt[0].type = phba->cfg_multi_ring_type;11388	pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;11389	return 0;11390}11391 11392static void11393lpfc_sli_post_recovery_event(struct lpfc_hba *phba,11394			     struct lpfc_nodelist *ndlp)11395{11396	unsigned long iflags;11397	struct lpfc_work_evt  *evtp = &ndlp->recovery_evt;11398 11399	/* Hold a node reference for outstanding queued work */11400	if (!lpfc_nlp_get(ndlp))11401		return;11402 11403	spin_lock_irqsave(&phba->hbalock, iflags);11404	if (!list_empty(&evtp->evt_listp)) {11405		spin_unlock_irqrestore(&phba->hbalock, iflags);11406		lpfc_nlp_put(ndlp);11407		return;11408	}11409 11410	evtp->evt_arg1 = ndlp;11411	evtp->evt = LPFC_EVT_RECOVER_PORT;11412	list_add_tail(&evtp->evt_listp, &phba->work_list);11413	spin_unlock_irqrestore(&phba->hbalock, iflags);11414 11415	lpfc_worker_wake_up(phba);11416}11417 11418/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.11419 * @phba: Pointer to HBA context object.11420 * @iocbq: Pointer to iocb object.11421 *11422 * The async_event handler calls this routine when it receives11423 * an ASYNC_STATUS_CN event from the port.  The port generates11424 * this event when an Abort Sequence request to an rport fails11425 * twice in succession.  The abort could be originated by the11426 * driver or by the port.  The ABTS could have been for an ELS11427 * or FCP IO.  The port only generates this event when an ABTS11428 * fails to complete after one retry.11429 */11430static void11431lpfc_sli_abts_err_handler(struct lpfc_hba *phba,11432			  struct lpfc_iocbq *iocbq)11433{11434	struct lpfc_nodelist *ndlp = NULL;11435	uint16_t rpi = 0, vpi = 0;11436	struct lpfc_vport *vport = NULL;11437 11438	/* The rpi in the ulpContext is vport-sensitive. */11439	vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;11440	rpi = iocbq->iocb.ulpContext;11441 11442	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,11443			"3092 Port generated ABTS async event "11444			"on vpi %d rpi %d status 0x%x\n",11445			vpi, rpi, iocbq->iocb.ulpStatus);11446 11447	vport = lpfc_find_vport_by_vpid(phba, vpi);11448	if (!vport)11449		goto err_exit;11450	ndlp = lpfc_findnode_rpi(vport, rpi);11451	if (!ndlp)11452		goto err_exit;11453 11454	if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)11455		lpfc_sli_abts_recover_port(vport, ndlp);11456	return;11457 11458 err_exit:11459	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,11460			"3095 Event Context not found, no "11461			"action on vpi %d rpi %d status 0x%x, reason 0x%x\n",11462			vpi, rpi, iocbq->iocb.ulpStatus,11463			iocbq->iocb.ulpContext);11464}11465 11466/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.11467 * @phba: pointer to HBA context object.11468 * @ndlp: nodelist pointer for the impacted rport.11469 * @axri: pointer to the wcqe containing the failed exchange.11470 *11471 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the11472 * port.  The port generates this event when an abort exchange request to an11473 * rport fails twice in succession with no reply.  The abort could be originated11474 * by the driver or by the port.  The ABTS could have been for an ELS or FCP IO.11475 */11476void11477lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,11478			   struct lpfc_nodelist *ndlp,11479			   struct sli4_wcqe_xri_aborted *axri)11480{11481	uint32_t ext_status = 0;11482 11483	if (!ndlp) {11484		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,11485				"3115 Node Context not found, driver "11486				"ignoring abts err event\n");11487		return;11488	}11489 11490	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,11491			"3116 Port generated FCP XRI ABORT event on "11492			"vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",11493			ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],11494			bf_get(lpfc_wcqe_xa_xri, axri),11495			bf_get(lpfc_wcqe_xa_status, axri),11496			axri->parameter);11497 11498	/*11499	 * Catch the ABTS protocol failure case.  Older OCe FW releases returned11500	 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and11501	 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.11502	 */11503	ext_status = axri->parameter & IOERR_PARAM_MASK;11504	if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&11505	    ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))11506		lpfc_sli_post_recovery_event(phba, ndlp);11507}11508 11509/**11510 * lpfc_sli_async_event_handler - ASYNC iocb handler function11511 * @phba: Pointer to HBA context object.11512 * @pring: Pointer to driver SLI ring object.11513 * @iocbq: Pointer to iocb object.11514 *11515 * This function is called by the slow ring event handler11516 * function when there is an ASYNC event iocb in the ring.11517 * This function is called with no lock held.11518 * Currently this function handles only temperature related11519 * ASYNC events. The function decodes the temperature sensor11520 * event message and posts events for the management applications.11521 **/11522static void11523lpfc_sli_async_event_handler(struct lpfc_hba * phba,11524	struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)11525{11526	IOCB_t *icmd;11527	uint16_t evt_code;11528	struct temp_event temp_event_data;11529	struct Scsi_Host *shost;11530	uint32_t *iocb_w;11531 11532	icmd = &iocbq->iocb;11533	evt_code = icmd->un.asyncstat.evt_code;11534 11535	switch (evt_code) {11536	case ASYNC_TEMP_WARN:11537	case ASYNC_TEMP_SAFE:11538		temp_event_data.data = (uint32_t) icmd->ulpContext;11539		temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;11540		if (evt_code == ASYNC_TEMP_WARN) {11541			temp_event_data.event_code = LPFC_THRESHOLD_TEMP;11542			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,11543				"0347 Adapter is very hot, please take "11544				"corrective action. temperature : %d Celsius\n",11545				(uint32_t) icmd->ulpContext);11546		} else {11547			temp_event_data.event_code = LPFC_NORMAL_TEMP;11548			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,11549				"0340 Adapter temperature is OK now. "11550				"temperature : %d Celsius\n",11551				(uint32_t) icmd->ulpContext);11552		}11553 11554		/* Send temperature change event to applications */11555		shost = lpfc_shost_from_vport(phba->pport);11556		fc_host_post_vendor_event(shost, fc_get_event_number(),11557			sizeof(temp_event_data), (char *) &temp_event_data,11558			LPFC_NL_VENDOR_ID);11559		break;11560	case ASYNC_STATUS_CN:11561		lpfc_sli_abts_err_handler(phba, iocbq);11562		break;11563	default:11564		iocb_w = (uint32_t *) icmd;11565		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,11566			"0346 Ring %d handler: unexpected ASYNC_STATUS"11567			" evt_code 0x%x\n"11568			"W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"11569			"W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"11570			"W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"11571			"W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",11572			pring->ringno, icmd->un.asyncstat.evt_code,11573			iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],11574			iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],11575			iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],11576			iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);11577 11578		break;11579	}11580}11581 11582 11583/**11584 * lpfc_sli4_setup - SLI ring setup function11585 * @phba: Pointer to HBA context object.11586 *11587 * lpfc_sli_setup sets up rings of the SLI interface with11588 * number of iocbs per ring and iotags. This function is11589 * called while driver attach to the HBA and before the11590 * interrupts are enabled. So there is no need for locking.11591 *11592 * This function always returns 0.11593 **/11594int11595lpfc_sli4_setup(struct lpfc_hba *phba)11596{11597	struct lpfc_sli_ring *pring;11598 11599	pring = phba->sli4_hba.els_wq->pring;11600	pring->num_mask = LPFC_MAX_RING_MASK;11601	pring->prt[0].profile = 0;	/* Mask 0 */11602	pring->prt[0].rctl = FC_RCTL_ELS_REQ;11603	pring->prt[0].type = FC_TYPE_ELS;11604	pring->prt[0].lpfc_sli_rcv_unsol_event =11605	    lpfc_els_unsol_event;11606	pring->prt[1].profile = 0;	/* Mask 1 */11607	pring->prt[1].rctl = FC_RCTL_ELS_REP;11608	pring->prt[1].type = FC_TYPE_ELS;11609	pring->prt[1].lpfc_sli_rcv_unsol_event =11610	    lpfc_els_unsol_event;11611	pring->prt[2].profile = 0;	/* Mask 2 */11612	/* NameServer Inquiry */11613	pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;11614	/* NameServer */11615	pring->prt[2].type = FC_TYPE_CT;11616	pring->prt[2].lpfc_sli_rcv_unsol_event =11617	    lpfc_ct_unsol_event;11618	pring->prt[3].profile = 0;	/* Mask 3 */11619	/* NameServer response */11620	pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;11621	/* NameServer */11622	pring->prt[3].type = FC_TYPE_CT;11623	pring->prt[3].lpfc_sli_rcv_unsol_event =11624	    lpfc_ct_unsol_event;11625	return 0;11626}11627 11628/**11629 * lpfc_sli_setup - SLI ring setup function11630 * @phba: Pointer to HBA context object.11631 *11632 * lpfc_sli_setup sets up rings of the SLI interface with11633 * number of iocbs per ring and iotags. This function is11634 * called while driver attach to the HBA and before the11635 * interrupts are enabled. So there is no need for locking.11636 *11637 * This function always returns 0. SLI3 only.11638 **/11639int11640lpfc_sli_setup(struct lpfc_hba *phba)11641{11642	int i, totiocbsize = 0;11643	struct lpfc_sli *psli = &phba->sli;11644	struct lpfc_sli_ring *pring;11645 11646	psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;11647	psli->sli_flag = 0;11648 11649	psli->iocbq_lookup = NULL;11650	psli->iocbq_lookup_len = 0;11651	psli->last_iotag = 0;11652 11653	for (i = 0; i < psli->num_rings; i++) {11654		pring = &psli->sli3_ring[i];11655		switch (i) {11656		case LPFC_FCP_RING:	/* ring 0 - FCP */11657			/* numCiocb and numRiocb are used in config_port */11658			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;11659			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;11660			pring->sli.sli3.numCiocb +=11661				SLI2_IOCB_CMD_R1XTRA_ENTRIES;11662			pring->sli.sli3.numRiocb +=11663				SLI2_IOCB_RSP_R1XTRA_ENTRIES;11664			pring->sli.sli3.numCiocb +=11665				SLI2_IOCB_CMD_R3XTRA_ENTRIES;11666			pring->sli.sli3.numRiocb +=11667				SLI2_IOCB_RSP_R3XTRA_ENTRIES;11668			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?11669							SLI3_IOCB_CMD_SIZE :11670							SLI2_IOCB_CMD_SIZE;11671			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?11672							SLI3_IOCB_RSP_SIZE :11673							SLI2_IOCB_RSP_SIZE;11674			pring->iotag_ctr = 0;11675			pring->iotag_max =11676			    (phba->cfg_hba_queue_depth * 2);11677			pring->fast_iotag = pring->iotag_max;11678			pring->num_mask = 0;11679			break;11680		case LPFC_EXTRA_RING:	/* ring 1 - EXTRA */11681			/* numCiocb and numRiocb are used in config_port */11682			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;11683			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;11684			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?11685							SLI3_IOCB_CMD_SIZE :11686							SLI2_IOCB_CMD_SIZE;11687			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?11688							SLI3_IOCB_RSP_SIZE :11689							SLI2_IOCB_RSP_SIZE;11690			pring->iotag_max = phba->cfg_hba_queue_depth;11691			pring->num_mask = 0;11692			break;11693		case LPFC_ELS_RING:	/* ring 2 - ELS / CT */11694			/* numCiocb and numRiocb are used in config_port */11695			pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;11696			pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;11697			pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?11698							SLI3_IOCB_CMD_SIZE :11699							SLI2_IOCB_CMD_SIZE;11700			pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?11701							SLI3_IOCB_RSP_SIZE :11702							SLI2_IOCB_RSP_SIZE;11703			pring->fast_iotag = 0;11704			pring->iotag_ctr = 0;11705			pring->iotag_max = 4096;11706			pring->lpfc_sli_rcv_async_status =11707				lpfc_sli_async_event_handler;11708			pring->num_mask = LPFC_MAX_RING_MASK;11709			pring->prt[0].profile = 0;	/* Mask 0 */11710			pring->prt[0].rctl = FC_RCTL_ELS_REQ;11711			pring->prt[0].type = FC_TYPE_ELS;11712			pring->prt[0].lpfc_sli_rcv_unsol_event =11713			    lpfc_els_unsol_event;11714			pring->prt[1].profile = 0;	/* Mask 1 */11715			pring->prt[1].rctl = FC_RCTL_ELS_REP;11716			pring->prt[1].type = FC_TYPE_ELS;11717			pring->prt[1].lpfc_sli_rcv_unsol_event =11718			    lpfc_els_unsol_event;11719			pring->prt[2].profile = 0;	/* Mask 2 */11720			/* NameServer Inquiry */11721			pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;11722			/* NameServer */11723			pring->prt[2].type = FC_TYPE_CT;11724			pring->prt[2].lpfc_sli_rcv_unsol_event =11725			    lpfc_ct_unsol_event;11726			pring->prt[3].profile = 0;	/* Mask 3 */11727			/* NameServer response */11728			pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;11729			/* NameServer */11730			pring->prt[3].type = FC_TYPE_CT;11731			pring->prt[3].lpfc_sli_rcv_unsol_event =11732			    lpfc_ct_unsol_event;11733			break;11734		}11735		totiocbsize += (pring->sli.sli3.numCiocb *11736			pring->sli.sli3.sizeCiocb) +11737			(pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);11738	}11739	if (totiocbsize > MAX_SLIM_IOCB_SIZE) {11740		/* Too many cmd / rsp ring entries in SLI2 SLIM */11741		printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "11742		       "SLI2 SLIM Data: x%x x%lx\n",11743		       phba->brd_no, totiocbsize,11744		       (unsigned long) MAX_SLIM_IOCB_SIZE);11745	}11746	if (phba->cfg_multi_ring_support == 2)11747		lpfc_extra_ring_setup(phba);11748 11749	return 0;11750}11751 11752/**11753 * lpfc_sli4_queue_init - Queue initialization function11754 * @phba: Pointer to HBA context object.11755 *11756 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each11757 * ring. This function also initializes ring indices of each ring.11758 * This function is called during the initialization of the SLI11759 * interface of an HBA.11760 * This function is called with no lock held and always returns11761 * 1.11762 **/11763void11764lpfc_sli4_queue_init(struct lpfc_hba *phba)11765{11766	struct lpfc_sli *psli;11767	struct lpfc_sli_ring *pring;11768	int i;11769 11770	psli = &phba->sli;11771	spin_lock_irq(&phba->hbalock);11772	INIT_LIST_HEAD(&psli->mboxq);11773	INIT_LIST_HEAD(&psli->mboxq_cmpl);11774	/* Initialize list headers for txq and txcmplq as double linked lists */11775	for (i = 0; i < phba->cfg_hdw_queue; i++) {11776		pring = phba->sli4_hba.hdwq[i].io_wq->pring;11777		pring->flag = 0;11778		pring->ringno = LPFC_FCP_RING;11779		pring->txcmplq_cnt = 0;11780		INIT_LIST_HEAD(&pring->txq);11781		INIT_LIST_HEAD(&pring->txcmplq);11782		INIT_LIST_HEAD(&pring->iocb_continueq);11783		spin_lock_init(&pring->ring_lock);11784	}11785	pring = phba->sli4_hba.els_wq->pring;11786	pring->flag = 0;11787	pring->ringno = LPFC_ELS_RING;11788	pring->txcmplq_cnt = 0;11789	INIT_LIST_HEAD(&pring->txq);11790	INIT_LIST_HEAD(&pring->txcmplq);11791	INIT_LIST_HEAD(&pring->iocb_continueq);11792	spin_lock_init(&pring->ring_lock);11793 11794	if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {11795		pring = phba->sli4_hba.nvmels_wq->pring;11796		pring->flag = 0;11797		pring->ringno = LPFC_ELS_RING;11798		pring->txcmplq_cnt = 0;11799		INIT_LIST_HEAD(&pring->txq);11800		INIT_LIST_HEAD(&pring->txcmplq);11801		INIT_LIST_HEAD(&pring->iocb_continueq);11802		spin_lock_init(&pring->ring_lock);11803	}11804 11805	spin_unlock_irq(&phba->hbalock);11806}11807 11808/**11809 * lpfc_sli_queue_init - Queue initialization function11810 * @phba: Pointer to HBA context object.11811 *11812 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each11813 * ring. This function also initializes ring indices of each ring.11814 * This function is called during the initialization of the SLI11815 * interface of an HBA.11816 * This function is called with no lock held and always returns11817 * 1.11818 **/11819void11820lpfc_sli_queue_init(struct lpfc_hba *phba)11821{11822	struct lpfc_sli *psli;11823	struct lpfc_sli_ring *pring;11824	int i;11825 11826	psli = &phba->sli;11827	spin_lock_irq(&phba->hbalock);11828	INIT_LIST_HEAD(&psli->mboxq);11829	INIT_LIST_HEAD(&psli->mboxq_cmpl);11830	/* Initialize list headers for txq and txcmplq as double linked lists */11831	for (i = 0; i < psli->num_rings; i++) {11832		pring = &psli->sli3_ring[i];11833		pring->ringno = i;11834		pring->sli.sli3.next_cmdidx  = 0;11835		pring->sli.sli3.local_getidx = 0;11836		pring->sli.sli3.cmdidx = 0;11837		INIT_LIST_HEAD(&pring->iocb_continueq);11838		INIT_LIST_HEAD(&pring->iocb_continue_saveq);11839		INIT_LIST_HEAD(&pring->postbufq);11840		pring->flag = 0;11841		INIT_LIST_HEAD(&pring->txq);11842		INIT_LIST_HEAD(&pring->txcmplq);11843		spin_lock_init(&pring->ring_lock);11844	}11845	spin_unlock_irq(&phba->hbalock);11846}11847 11848/**11849 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system11850 * @phba: Pointer to HBA context object.11851 *11852 * This routine flushes the mailbox command subsystem. It will unconditionally11853 * flush all the mailbox commands in the three possible stages in the mailbox11854 * command sub-system: pending mailbox command queue; the outstanding mailbox11855 * command; and completed mailbox command queue. It is caller's responsibility11856 * to make sure that the driver is in the proper state to flush the mailbox11857 * command sub-system. Namely, the posting of mailbox commands into the11858 * pending mailbox command queue from the various clients must be stopped;11859 * either the HBA is in a state that it will never works on the outstanding11860 * mailbox command (such as in EEH or ERATT conditions) or the outstanding11861 * mailbox command has been completed.11862 **/11863static void11864lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)11865{11866	LIST_HEAD(completions);11867	struct lpfc_sli *psli = &phba->sli;11868	LPFC_MBOXQ_t *pmb;11869	unsigned long iflag;11870 11871	/* Disable softirqs, including timers from obtaining phba->hbalock */11872	local_bh_disable();11873 11874	/* Flush all the mailbox commands in the mbox system */11875	spin_lock_irqsave(&phba->hbalock, iflag);11876 11877	/* The pending mailbox command queue */11878	list_splice_init(&phba->sli.mboxq, &completions);11879	/* The outstanding active mailbox command */11880	if (psli->mbox_active) {11881		list_add_tail(&psli->mbox_active->list, &completions);11882		psli->mbox_active = NULL;11883		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;11884	}11885	/* The completed mailbox command queue */11886	list_splice_init(&phba->sli.mboxq_cmpl, &completions);11887	spin_unlock_irqrestore(&phba->hbalock, iflag);11888 11889	/* Enable softirqs again, done with phba->hbalock */11890	local_bh_enable();11891 11892	/* Return all flushed mailbox commands with MBX_NOT_FINISHED status */11893	while (!list_empty(&completions)) {11894		list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);11895		pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;11896		if (pmb->mbox_cmpl)11897			pmb->mbox_cmpl(phba, pmb);11898	}11899}11900 11901/**11902 * lpfc_sli_host_down - Vport cleanup function11903 * @vport: Pointer to virtual port object.11904 *11905 * lpfc_sli_host_down is called to clean up the resources11906 * associated with a vport before destroying virtual11907 * port data structures.11908 * This function does following operations:11909 * - Free discovery resources associated with this virtual11910 *   port.11911 * - Free iocbs associated with this virtual port in11912 *   the txq.11913 * - Send abort for all iocb commands associated with this11914 *   vport in txcmplq.11915 *11916 * This function is called with no lock held and always returns 1.11917 **/11918int11919lpfc_sli_host_down(struct lpfc_vport *vport)11920{11921	LIST_HEAD(completions);11922	struct lpfc_hba *phba = vport->phba;11923	struct lpfc_sli *psli = &phba->sli;11924	struct lpfc_queue *qp = NULL;11925	struct lpfc_sli_ring *pring;11926	struct lpfc_iocbq *iocb, *next_iocb;11927	int i;11928	unsigned long flags = 0;11929	uint16_t prev_pring_flag;11930 11931	lpfc_cleanup_discovery_resources(vport);11932 11933	spin_lock_irqsave(&phba->hbalock, flags);11934 11935	/*11936	 * Error everything on the txq since these iocbs11937	 * have not been given to the FW yet.11938	 * Also issue ABTS for everything on the txcmplq11939	 */11940	if (phba->sli_rev != LPFC_SLI_REV4) {11941		for (i = 0; i < psli->num_rings; i++) {11942			pring = &psli->sli3_ring[i];11943			prev_pring_flag = pring->flag;11944			/* Only slow rings */11945			if (pring->ringno == LPFC_ELS_RING) {11946				pring->flag |= LPFC_DEFERRED_RING_EVENT;11947				/* Set the lpfc data pending flag */11948				set_bit(LPFC_DATA_READY, &phba->data_flags);11949			}11950			list_for_each_entry_safe(iocb, next_iocb,11951						 &pring->txq, list) {11952				if (iocb->vport != vport)11953					continue;11954				list_move_tail(&iocb->list, &completions);11955			}11956			list_for_each_entry_safe(iocb, next_iocb,11957						 &pring->txcmplq, list) {11958				if (iocb->vport != vport)11959					continue;11960				lpfc_sli_issue_abort_iotag(phba, pring, iocb,11961							   NULL);11962			}11963			pring->flag = prev_pring_flag;11964		}11965	} else {11966		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {11967			pring = qp->pring;11968			if (!pring)11969				continue;11970			if (pring == phba->sli4_hba.els_wq->pring) {11971				pring->flag |= LPFC_DEFERRED_RING_EVENT;11972				/* Set the lpfc data pending flag */11973				set_bit(LPFC_DATA_READY, &phba->data_flags);11974			}11975			prev_pring_flag = pring->flag;11976			spin_lock(&pring->ring_lock);11977			list_for_each_entry_safe(iocb, next_iocb,11978						 &pring->txq, list) {11979				if (iocb->vport != vport)11980					continue;11981				list_move_tail(&iocb->list, &completions);11982			}11983			spin_unlock(&pring->ring_lock);11984			list_for_each_entry_safe(iocb, next_iocb,11985						 &pring->txcmplq, list) {11986				if (iocb->vport != vport)11987					continue;11988				lpfc_sli_issue_abort_iotag(phba, pring, iocb,11989							   NULL);11990			}11991			pring->flag = prev_pring_flag;11992		}11993	}11994	spin_unlock_irqrestore(&phba->hbalock, flags);11995 11996	/* Make sure HBA is alive */11997	lpfc_issue_hb_tmo(phba);11998 11999	/* Cancel all the IOCBs from the completions list */12000	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,12001			      IOERR_SLI_DOWN);12002	return 1;12003}12004 12005/**12006 * lpfc_sli_hba_down - Resource cleanup function for the HBA12007 * @phba: Pointer to HBA context object.12008 *12009 * This function cleans up all iocb, buffers, mailbox commands12010 * while shutting down the HBA. This function is called with no12011 * lock held and always returns 1.12012 * This function does the following to cleanup driver resources:12013 * - Free discovery resources for each virtual port12014 * - Cleanup any pending fabric iocbs12015 * - Iterate through the iocb txq and free each entry12016 *   in the list.12017 * - Free up any buffer posted to the HBA12018 * - Free mailbox commands in the mailbox queue.12019 **/12020int12021lpfc_sli_hba_down(struct lpfc_hba *phba)12022{12023	LIST_HEAD(completions);12024	struct lpfc_sli *psli = &phba->sli;12025	struct lpfc_queue *qp = NULL;12026	struct lpfc_sli_ring *pring;12027	struct lpfc_dmabuf *buf_ptr;12028	unsigned long flags = 0;12029	int i;12030 12031	/* Shutdown the mailbox command sub-system */12032	lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);12033 12034	lpfc_hba_down_prep(phba);12035 12036	/* Disable softirqs, including timers from obtaining phba->hbalock */12037	local_bh_disable();12038 12039	lpfc_fabric_abort_hba(phba);12040 12041	spin_lock_irqsave(&phba->hbalock, flags);12042 12043	/*12044	 * Error everything on the txq since these iocbs12045	 * have not been given to the FW yet.12046	 */12047	if (phba->sli_rev != LPFC_SLI_REV4) {12048		for (i = 0; i < psli->num_rings; i++) {12049			pring = &psli->sli3_ring[i];12050			/* Only slow rings */12051			if (pring->ringno == LPFC_ELS_RING) {12052				pring->flag |= LPFC_DEFERRED_RING_EVENT;12053				/* Set the lpfc data pending flag */12054				set_bit(LPFC_DATA_READY, &phba->data_flags);12055			}12056			list_splice_init(&pring->txq, &completions);12057		}12058	} else {12059		list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {12060			pring = qp->pring;12061			if (!pring)12062				continue;12063			spin_lock(&pring->ring_lock);12064			list_splice_init(&pring->txq, &completions);12065			spin_unlock(&pring->ring_lock);12066			if (pring == phba->sli4_hba.els_wq->pring) {12067				pring->flag |= LPFC_DEFERRED_RING_EVENT;12068				/* Set the lpfc data pending flag */12069				set_bit(LPFC_DATA_READY, &phba->data_flags);12070			}12071		}12072	}12073	spin_unlock_irqrestore(&phba->hbalock, flags);12074 12075	/* Cancel all the IOCBs from the completions list */12076	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,12077			      IOERR_SLI_DOWN);12078 12079	spin_lock_irqsave(&phba->hbalock, flags);12080	list_splice_init(&phba->elsbuf, &completions);12081	phba->elsbuf_cnt = 0;12082	phba->elsbuf_prev_cnt = 0;12083	spin_unlock_irqrestore(&phba->hbalock, flags);12084 12085	while (!list_empty(&completions)) {12086		list_remove_head(&completions, buf_ptr,12087			struct lpfc_dmabuf, list);12088		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);12089		kfree(buf_ptr);12090	}12091 12092	/* Enable softirqs again, done with phba->hbalock */12093	local_bh_enable();12094 12095	/* Return any active mbox cmds */12096	del_timer_sync(&psli->mbox_tmo);12097 12098	spin_lock_irqsave(&phba->pport->work_port_lock, flags);12099	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;12100	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);12101 12102	return 1;12103}12104 12105/**12106 * lpfc_sli_pcimem_bcopy - SLI memory copy function12107 * @srcp: Source memory pointer.12108 * @destp: Destination memory pointer.12109 * @cnt: Number of words required to be copied.12110 *12111 * This function is used for copying data between driver memory12112 * and the SLI memory. This function also changes the endianness12113 * of each word if native endianness is different from SLI12114 * endianness. This function can be called with or without12115 * lock.12116 **/12117void12118lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)12119{12120	uint32_t *src = srcp;12121	uint32_t *dest = destp;12122	uint32_t ldata;12123	int i;12124 12125	for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {12126		ldata = *src;12127		ldata = le32_to_cpu(ldata);12128		*dest = ldata;12129		src++;12130		dest++;12131	}12132}12133 12134 12135/**12136 * lpfc_sli_bemem_bcopy - SLI memory copy function12137 * @srcp: Source memory pointer.12138 * @destp: Destination memory pointer.12139 * @cnt: Number of words required to be copied.12140 *12141 * This function is used for copying data between a data structure12142 * with big endian representation to local endianness.12143 * This function can be called with or without lock.12144 **/12145void12146lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)12147{12148	uint32_t *src = srcp;12149	uint32_t *dest = destp;12150	uint32_t ldata;12151	int i;12152 12153	for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {12154		ldata = *src;12155		ldata = be32_to_cpu(ldata);12156		*dest = ldata;12157		src++;12158		dest++;12159	}12160}12161 12162/**12163 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq12164 * @phba: Pointer to HBA context object.12165 * @pring: Pointer to driver SLI ring object.12166 * @mp: Pointer to driver buffer object.12167 *12168 * This function is called with no lock held.12169 * It always return zero after adding the buffer to the postbufq12170 * buffer list.12171 **/12172int12173lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,12174			 struct lpfc_dmabuf *mp)12175{12176	/* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up12177	   later */12178	spin_lock_irq(&phba->hbalock);12179	list_add_tail(&mp->list, &pring->postbufq);12180	pring->postbufq_cnt++;12181	spin_unlock_irq(&phba->hbalock);12182	return 0;12183}12184 12185/**12186 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer12187 * @phba: Pointer to HBA context object.12188 *12189 * When HBQ is enabled, buffers are searched based on tags. This function12190 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The12191 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag12192 * does not conflict with tags of buffer posted for unsolicited events.12193 * The function returns the allocated tag. The function is called with12194 * no locks held.12195 **/12196uint32_t12197lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)12198{12199	spin_lock_irq(&phba->hbalock);12200	phba->buffer_tag_count++;12201	/*12202	 * Always set the QUE_BUFTAG_BIT to distiguish between12203	 * a tag assigned by HBQ.12204	 */12205	phba->buffer_tag_count |= QUE_BUFTAG_BIT;12206	spin_unlock_irq(&phba->hbalock);12207	return phba->buffer_tag_count;12208}12209 12210/**12211 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag12212 * @phba: Pointer to HBA context object.12213 * @pring: Pointer to driver SLI ring object.12214 * @tag: Buffer tag.12215 *12216 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq12217 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX12218 * iocb is posted to the response ring with the tag of the buffer.12219 * This function searches the pring->postbufq list using the tag12220 * to find buffer associated with CMD_IOCB_RET_XRI64_CX12221 * iocb. If the buffer is found then lpfc_dmabuf object of the12222 * buffer is returned to the caller else NULL is returned.12223 * This function is called with no lock held.12224 **/12225struct lpfc_dmabuf *12226lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,12227			uint32_t tag)12228{12229	struct lpfc_dmabuf *mp, *next_mp;12230	struct list_head *slp = &pring->postbufq;12231 12232	/* Search postbufq, from the beginning, looking for a match on tag */12233	spin_lock_irq(&phba->hbalock);12234	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {12235		if (mp->buffer_tag == tag) {12236			list_del_init(&mp->list);12237			pring->postbufq_cnt--;12238			spin_unlock_irq(&phba->hbalock);12239			return mp;12240		}12241	}12242 12243	spin_unlock_irq(&phba->hbalock);12244	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,12245			"0402 Cannot find virtual addr for buffer tag on "12246			"ring %d Data x%lx x%px x%px x%x\n",12247			pring->ringno, (unsigned long) tag,12248			slp->next, slp->prev, pring->postbufq_cnt);12249 12250	return NULL;12251}12252 12253/**12254 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events12255 * @phba: Pointer to HBA context object.12256 * @pring: Pointer to driver SLI ring object.12257 * @phys: DMA address of the buffer.12258 *12259 * This function searches the buffer list using the dma_address12260 * of unsolicited event to find the driver's lpfc_dmabuf object12261 * corresponding to the dma_address. The function returns the12262 * lpfc_dmabuf object if a buffer is found else it returns NULL.12263 * This function is called by the ct and els unsolicited event12264 * handlers to get the buffer associated with the unsolicited12265 * event.12266 *12267 * This function is called with no lock held.12268 **/12269struct lpfc_dmabuf *12270lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,12271			 dma_addr_t phys)12272{12273	struct lpfc_dmabuf *mp, *next_mp;12274	struct list_head *slp = &pring->postbufq;12275 12276	/* Search postbufq, from the beginning, looking for a match on phys */12277	spin_lock_irq(&phba->hbalock);12278	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {12279		if (mp->phys == phys) {12280			list_del_init(&mp->list);12281			pring->postbufq_cnt--;12282			spin_unlock_irq(&phba->hbalock);12283			return mp;12284		}12285	}12286 12287	spin_unlock_irq(&phba->hbalock);12288	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,12289			"0410 Cannot find virtual addr for mapped buf on "12290			"ring %d Data x%llx x%px x%px x%x\n",12291			pring->ringno, (unsigned long long)phys,12292			slp->next, slp->prev, pring->postbufq_cnt);12293	return NULL;12294}12295 12296/**12297 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs12298 * @phba: Pointer to HBA context object.12299 * @cmdiocb: Pointer to driver command iocb object.12300 * @rspiocb: Pointer to driver response iocb object.12301 *12302 * This function is the completion handler for the abort iocbs for12303 * ELS commands. This function is called from the ELS ring event12304 * handler with no lock held. This function frees memory resources12305 * associated with the abort iocb.12306 **/12307static void12308lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,12309			struct lpfc_iocbq *rspiocb)12310{12311	u32 ulp_status = get_job_ulpstatus(phba, rspiocb);12312	u32 ulp_word4 = get_job_word4(phba, rspiocb);12313	u8 cmnd = get_job_cmnd(phba, cmdiocb);12314 12315	if (ulp_status) {12316		/*12317		 * Assume that the port already completed and returned, or12318		 * will return the iocb. Just Log the message.12319		 */12320		if (phba->sli_rev < LPFC_SLI_REV4) {12321			if (cmnd == CMD_ABORT_XRI_CX &&12322			    ulp_status == IOSTAT_LOCAL_REJECT &&12323			    ulp_word4 == IOERR_ABORT_REQUESTED) {12324				goto release_iocb;12325			}12326		}12327	}12328 12329	lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,12330			"0327 Abort els iocb complete x%px with io cmd xri %x "12331			"abort tag x%x abort status %x abort code %x\n",12332			cmdiocb, get_job_abtsiotag(phba, cmdiocb),12333			(phba->sli_rev == LPFC_SLI_REV4) ?12334			get_wqe_reqtag(cmdiocb) :12335			cmdiocb->iocb.ulpIoTag,12336			ulp_status, ulp_word4);12337release_iocb:12338	lpfc_sli_release_iocbq(phba, cmdiocb);12339	return;12340}12341 12342/**12343 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command12344 * @phba: Pointer to HBA context object.12345 * @cmdiocb: Pointer to driver command iocb object.12346 * @rspiocb: Pointer to driver response iocb object.12347 *12348 * The function is called from SLI ring event handler with no12349 * lock held. This function is the completion handler for ELS commands12350 * which are aborted. The function frees memory resources used for12351 * the aborted ELS commands.12352 **/12353void12354lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,12355		     struct lpfc_iocbq *rspiocb)12356{12357	struct lpfc_nodelist *ndlp = cmdiocb->ndlp;12358	IOCB_t *irsp;12359	LPFC_MBOXQ_t *mbox;12360	u32 ulp_command, ulp_status, ulp_word4, iotag;12361 12362	ulp_command = get_job_cmnd(phba, cmdiocb);12363	ulp_status = get_job_ulpstatus(phba, rspiocb);12364	ulp_word4 = get_job_word4(phba, rspiocb);12365 12366	if (phba->sli_rev == LPFC_SLI_REV4) {12367		iotag = get_wqe_reqtag(cmdiocb);12368	} else {12369		irsp = &rspiocb->iocb;12370		iotag = irsp->ulpIoTag;12371 12372		/* It is possible a PLOGI_RJT for NPIV ports to get aborted.12373		 * The MBX_REG_LOGIN64 mbox command is freed back to the12374		 * mbox_mem_pool here.12375		 */12376		if (cmdiocb->context_un.mbox) {12377			mbox = cmdiocb->context_un.mbox;12378			lpfc_mbox_rsrc_cleanup(phba, mbox, MBOX_THD_UNLOCKED);12379			cmdiocb->context_un.mbox = NULL;12380		}12381	}12382 12383	/* ELS cmd tag <ulpIoTag> completes */12384	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,12385			"0139 Ignoring ELS cmd code x%x ref cnt x%x Data: "12386			"x%x x%x x%x x%px\n",12387			ulp_command, kref_read(&cmdiocb->ndlp->kref),12388			ulp_status, ulp_word4, iotag, cmdiocb->ndlp);12389	/*12390	 * Deref the ndlp after free_iocb. sli_release_iocb will access the ndlp12391	 * if exchange is busy.12392	 */12393	if (ulp_command == CMD_GEN_REQUEST64_CR)12394		lpfc_ct_free_iocb(phba, cmdiocb);12395	else12396		lpfc_els_free_iocb(phba, cmdiocb);12397 12398	lpfc_nlp_put(ndlp);12399}12400 12401/**12402 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb12403 * @phba: Pointer to HBA context object.12404 * @pring: Pointer to driver SLI ring object.12405 * @cmdiocb: Pointer to driver command iocb object.12406 * @cmpl: completion function.12407 *12408 * This function issues an abort iocb for the provided command iocb. In case12409 * of unloading, the abort iocb will not be issued to commands on the ELS12410 * ring. Instead, the callback function shall be changed to those commands12411 * so that nothing happens when them finishes. This function is called with12412 * hbalock held andno ring_lock held (SLI4). The function returns IOCB_SUCCESS12413 * when the command iocb is an abort request.12414 *12415 **/12416int12417lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,12418			   struct lpfc_iocbq *cmdiocb, void *cmpl)12419{12420	struct lpfc_vport *vport = cmdiocb->vport;12421	struct lpfc_iocbq *abtsiocbp;12422	int retval = IOCB_ERROR;12423	unsigned long iflags;12424	struct lpfc_nodelist *ndlp = NULL;12425	u32 ulp_command = get_job_cmnd(phba, cmdiocb);12426	u16 ulp_context, iotag;12427	bool ia;12428 12429	/*12430	 * There are certain command types we don't want to abort.  And we12431	 * don't want to abort commands that are already in the process of12432	 * being aborted.12433	 */12434	if (ulp_command == CMD_ABORT_XRI_WQE ||12435	    ulp_command == CMD_ABORT_XRI_CN ||12436	    ulp_command == CMD_CLOSE_XRI_CN ||12437	    cmdiocb->cmd_flag & LPFC_DRIVER_ABORTED)12438		return IOCB_ABORTING;12439 12440	if (!pring) {12441		if (cmdiocb->cmd_flag & LPFC_IO_FABRIC)12442			cmdiocb->fabric_cmd_cmpl = lpfc_ignore_els_cmpl;12443		else12444			cmdiocb->cmd_cmpl = lpfc_ignore_els_cmpl;12445		return retval;12446	}12447 12448	/*12449	 * If we're unloading, don't abort iocb on the ELS ring, but change12450	 * the callback so that nothing happens when it finishes.12451	 */12452	if (test_bit(FC_UNLOADING, &vport->load_flag) &&12453	    pring->ringno == LPFC_ELS_RING) {12454		if (cmdiocb->cmd_flag & LPFC_IO_FABRIC)12455			cmdiocb->fabric_cmd_cmpl = lpfc_ignore_els_cmpl;12456		else12457			cmdiocb->cmd_cmpl = lpfc_ignore_els_cmpl;12458		return retval;12459	}12460 12461	/* issue ABTS for this IOCB based on iotag */12462	abtsiocbp = __lpfc_sli_get_iocbq(phba);12463	if (abtsiocbp == NULL)12464		return IOCB_NORESOURCE;12465 12466	/* This signals the response to set the correct status12467	 * before calling the completion handler12468	 */12469	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;12470 12471	if (phba->sli_rev == LPFC_SLI_REV4) {12472		ulp_context = cmdiocb->sli4_xritag;12473		iotag = abtsiocbp->iotag;12474	} else {12475		iotag = cmdiocb->iocb.ulpIoTag;12476		if (pring->ringno == LPFC_ELS_RING) {12477			ndlp = cmdiocb->ndlp;12478			ulp_context = ndlp->nlp_rpi;12479		} else {12480			ulp_context = cmdiocb->iocb.ulpContext;12481		}12482	}12483 12484	/* Just close the exchange under certain conditions. */12485	if (test_bit(FC_UNLOADING, &vport->load_flag) ||12486	    phba->link_state < LPFC_LINK_UP ||12487	    (phba->sli_rev == LPFC_SLI_REV4 &&12488	     phba->sli4_hba.link_state.status == LPFC_FC_LA_TYPE_LINK_DOWN) ||12489	    (phba->link_flag & LS_EXTERNAL_LOOPBACK))12490		ia = true;12491	else12492		ia = false;12493 12494	lpfc_sli_prep_abort_xri(phba, abtsiocbp, ulp_context, iotag,12495				cmdiocb->iocb.ulpClass,12496				LPFC_WQE_CQ_ID_DEFAULT, ia, false);12497 12498	/* ABTS WQE must go to the same WQ as the WQE to be aborted */12499	abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;12500	if (cmdiocb->cmd_flag & LPFC_IO_FCP)12501		abtsiocbp->cmd_flag |= (LPFC_IO_FCP | LPFC_USE_FCPWQIDX);12502 12503	if (cmdiocb->cmd_flag & LPFC_IO_FOF)12504		abtsiocbp->cmd_flag |= LPFC_IO_FOF;12505 12506	if (cmpl)12507		abtsiocbp->cmd_cmpl = cmpl;12508	else12509		abtsiocbp->cmd_cmpl = lpfc_sli_abort_els_cmpl;12510	abtsiocbp->vport = vport;12511 12512	if (phba->sli_rev == LPFC_SLI_REV4) {12513		pring = lpfc_sli4_calc_ring(phba, abtsiocbp);12514		if (unlikely(pring == NULL))12515			goto abort_iotag_exit;12516		/* Note: both hbalock and ring_lock need to be set here */12517		spin_lock_irqsave(&pring->ring_lock, iflags);12518		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,12519			abtsiocbp, 0);12520		spin_unlock_irqrestore(&pring->ring_lock, iflags);12521	} else {12522		retval = __lpfc_sli_issue_iocb(phba, pring->ringno,12523			abtsiocbp, 0);12524	}12525 12526abort_iotag_exit:12527 12528	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,12529			 "0339 Abort IO XRI x%x, Original iotag x%x, "12530			 "abort tag x%x Cmdjob : x%px Abortjob : x%px "12531			 "retval x%x : IA %d cmd_cmpl %ps\n",12532			 ulp_context, (phba->sli_rev == LPFC_SLI_REV4) ?12533			 cmdiocb->iotag : iotag, iotag, cmdiocb, abtsiocbp,12534			 retval, ia, abtsiocbp->cmd_cmpl);12535	if (retval) {12536		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;12537		__lpfc_sli_release_iocbq(phba, abtsiocbp);12538	}12539 12540	/*12541	 * Caller to this routine should check for IOCB_ERROR12542	 * and handle it properly.  This routine no longer removes12543	 * iocb off txcmplq and call compl in case of IOCB_ERROR.12544	 */12545	return retval;12546}12547 12548/**12549 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.12550 * @phba: pointer to lpfc HBA data structure.12551 *12552 * This routine will abort all pending and outstanding iocbs to an HBA.12553 **/12554void12555lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)12556{12557	struct lpfc_sli *psli = &phba->sli;12558	struct lpfc_sli_ring *pring;12559	struct lpfc_queue *qp = NULL;12560	int i;12561 12562	if (phba->sli_rev != LPFC_SLI_REV4) {12563		for (i = 0; i < psli->num_rings; i++) {12564			pring = &psli->sli3_ring[i];12565			lpfc_sli_abort_iocb_ring(phba, pring);12566		}12567		return;12568	}12569	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {12570		pring = qp->pring;12571		if (!pring)12572			continue;12573		lpfc_sli_abort_iocb_ring(phba, pring);12574	}12575}12576 12577/**12578 * lpfc_sli_validate_fcp_iocb_for_abort - filter iocbs appropriate for FCP aborts12579 * @iocbq: Pointer to iocb object.12580 * @vport: Pointer to driver virtual port object.12581 *12582 * This function acts as an iocb filter for functions which abort FCP iocbs.12583 *12584 * Return values12585 * -ENODEV, if a null iocb or vport ptr is encountered12586 * -EINVAL, if the iocb is not an FCP I/O, not on the TX cmpl queue, premarked as12587 *          driver already started the abort process, or is an abort iocb itself12588 * 0, passes criteria for aborting the FCP I/O iocb12589 **/12590static int12591lpfc_sli_validate_fcp_iocb_for_abort(struct lpfc_iocbq *iocbq,12592				     struct lpfc_vport *vport)12593{12594	u8 ulp_command;12595 12596	/* No null ptr vports */12597	if (!iocbq || iocbq->vport != vport)12598		return -ENODEV;12599 12600	/* iocb must be for FCP IO, already exists on the TX cmpl queue,12601	 * can't be premarked as driver aborted, nor be an ABORT iocb itself12602	 */12603	ulp_command = get_job_cmnd(vport->phba, iocbq);12604	if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||12605	    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ) ||12606	    (iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||12607	    (ulp_command == CMD_ABORT_XRI_CN ||12608	     ulp_command == CMD_CLOSE_XRI_CN ||12609	     ulp_command == CMD_ABORT_XRI_WQE))12610		return -EINVAL;12611 12612	return 0;12613}12614 12615/**12616 * lpfc_sli_validate_fcp_iocb - validate commands associated with a SCSI target12617 * @iocbq: Pointer to driver iocb object.12618 * @vport: Pointer to driver virtual port object.12619 * @tgt_id: SCSI ID of the target.12620 * @lun_id: LUN ID of the scsi device.12621 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST12622 *12623 * This function acts as an iocb filter for validating a lun/SCSI target/SCSI12624 * host.12625 *12626 * It will return12627 * 0 if the filtering criteria is met for the given iocb and will return12628 * 1 if the filtering criteria is not met.12629 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the12630 * given iocb is for the SCSI device specified by vport, tgt_id and12631 * lun_id parameter.12632 * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the12633 * given iocb is for the SCSI target specified by vport and tgt_id12634 * parameters.12635 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the12636 * given iocb is for the SCSI host associated with the given vport.12637 * This function is called with no locks held.12638 **/12639static int12640lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,12641			   uint16_t tgt_id, uint64_t lun_id,12642			   lpfc_ctx_cmd ctx_cmd)12643{12644	struct lpfc_io_buf *lpfc_cmd;12645	int rc = 1;12646 12647	lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);12648 12649	if (lpfc_cmd->pCmd == NULL)12650		return rc;12651 12652	switch (ctx_cmd) {12653	case LPFC_CTX_LUN:12654		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&12655		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&12656		    (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))12657			rc = 0;12658		break;12659	case LPFC_CTX_TGT:12660		if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&12661		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))12662			rc = 0;12663		break;12664	case LPFC_CTX_HOST:12665		rc = 0;12666		break;12667	default:12668		printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",12669			__func__, ctx_cmd);12670		break;12671	}12672 12673	return rc;12674}12675 12676/**12677 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending12678 * @vport: Pointer to virtual port.12679 * @tgt_id: SCSI ID of the target.12680 * @lun_id: LUN ID of the scsi device.12681 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.12682 *12683 * This function returns number of FCP commands pending for the vport.12684 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP12685 * commands pending on the vport associated with SCSI device specified12686 * by tgt_id and lun_id parameters.12687 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP12688 * commands pending on the vport associated with SCSI target specified12689 * by tgt_id parameter.12690 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP12691 * commands pending on the vport.12692 * This function returns the number of iocbs which satisfy the filter.12693 * This function is called without any lock held.12694 **/12695int12696lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,12697		  lpfc_ctx_cmd ctx_cmd)12698{12699	struct lpfc_hba *phba = vport->phba;12700	struct lpfc_iocbq *iocbq;12701	int sum, i;12702	unsigned long iflags;12703	u8 ulp_command;12704 12705	spin_lock_irqsave(&phba->hbalock, iflags);12706	for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {12707		iocbq = phba->sli.iocbq_lookup[i];12708 12709		if (!iocbq || iocbq->vport != vport)12710			continue;12711		if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||12712		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ))12713			continue;12714 12715		/* Include counting outstanding aborts */12716		ulp_command = get_job_cmnd(phba, iocbq);12717		if (ulp_command == CMD_ABORT_XRI_CN ||12718		    ulp_command == CMD_CLOSE_XRI_CN ||12719		    ulp_command == CMD_ABORT_XRI_WQE) {12720			sum++;12721			continue;12722		}12723 12724		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,12725					       ctx_cmd) == 0)12726			sum++;12727	}12728	spin_unlock_irqrestore(&phba->hbalock, iflags);12729 12730	return sum;12731}12732 12733/**12734 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs12735 * @phba: Pointer to HBA context object12736 * @cmdiocb: Pointer to command iocb object.12737 * @rspiocb: Pointer to response iocb object.12738 *12739 * This function is called when an aborted FCP iocb completes. This12740 * function is called by the ring event handler with no lock held.12741 * This function frees the iocb.12742 **/12743void12744lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,12745			struct lpfc_iocbq *rspiocb)12746{12747	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,12748			"3096 ABORT_XRI_CX completing on rpi x%x "12749			"original iotag x%x, abort cmd iotag x%x "12750			"status 0x%x, reason 0x%x\n",12751			(phba->sli_rev == LPFC_SLI_REV4) ?12752			cmdiocb->sli4_xritag :12753			cmdiocb->iocb.un.acxri.abortContextTag,12754			get_job_abtsiotag(phba, cmdiocb),12755			cmdiocb->iotag, get_job_ulpstatus(phba, rspiocb),12756			get_job_word4(phba, rspiocb));12757	lpfc_sli_release_iocbq(phba, cmdiocb);12758	return;12759}12760 12761/**12762 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN12763 * @vport: Pointer to virtual port.12764 * @tgt_id: SCSI ID of the target.12765 * @lun_id: LUN ID of the scsi device.12766 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.12767 *12768 * This function sends an abort command for every SCSI command12769 * associated with the given virtual port pending on the ring12770 * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then12771 * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before12772 * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort12773 * followed by lpfc_sli_validate_fcp_iocb.12774 *12775 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the12776 * FCP iocbs associated with lun specified by tgt_id and lun_id12777 * parameters12778 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the12779 * FCP iocbs associated with SCSI target specified by tgt_id parameter.12780 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all12781 * FCP iocbs associated with virtual port.12782 * The pring used for SLI3 is sli3_ring[LPFC_FCP_RING], for SLI412783 * lpfc_sli4_calc_ring is used.12784 * This function returns number of iocbs it failed to abort.12785 * This function is called with no locks held.12786 **/12787int12788lpfc_sli_abort_iocb(struct lpfc_vport *vport, u16 tgt_id, u64 lun_id,12789		    lpfc_ctx_cmd abort_cmd)12790{12791	struct lpfc_hba *phba = vport->phba;12792	struct lpfc_sli_ring *pring = NULL;12793	struct lpfc_iocbq *iocbq;12794	int errcnt = 0, ret_val = 0;12795	unsigned long iflags;12796	int i;12797 12798	/* all I/Os are in process of being flushed */12799	if (test_bit(HBA_IOQ_FLUSH, &phba->hba_flag))12800		return errcnt;12801 12802	for (i = 1; i <= phba->sli.last_iotag; i++) {12803		iocbq = phba->sli.iocbq_lookup[i];12804 12805		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))12806			continue;12807 12808		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,12809					       abort_cmd) != 0)12810			continue;12811 12812		spin_lock_irqsave(&phba->hbalock, iflags);12813		if (phba->sli_rev == LPFC_SLI_REV3) {12814			pring = &phba->sli.sli3_ring[LPFC_FCP_RING];12815		} else if (phba->sli_rev == LPFC_SLI_REV4) {12816			pring = lpfc_sli4_calc_ring(phba, iocbq);12817		}12818		ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocbq,12819						     lpfc_sli_abort_fcp_cmpl);12820		spin_unlock_irqrestore(&phba->hbalock, iflags);12821		if (ret_val != IOCB_SUCCESS)12822			errcnt++;12823	}12824 12825	return errcnt;12826}12827 12828/**12829 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN12830 * @vport: Pointer to virtual port.12831 * @pring: Pointer to driver SLI ring object.12832 * @tgt_id: SCSI ID of the target.12833 * @lun_id: LUN ID of the scsi device.12834 * @cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.12835 *12836 * This function sends an abort command for every SCSI command12837 * associated with the given virtual port pending on the ring12838 * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then12839 * lpfc_sli_validate_fcp_iocb function.  The ordering for validation before12840 * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort12841 * followed by lpfc_sli_validate_fcp_iocb.12842 *12843 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the12844 * FCP iocbs associated with lun specified by tgt_id and lun_id12845 * parameters12846 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the12847 * FCP iocbs associated with SCSI target specified by tgt_id parameter.12848 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all12849 * FCP iocbs associated with virtual port.12850 * This function returns number of iocbs it aborted .12851 * This function is called with no locks held right after a taskmgmt12852 * command is sent.12853 **/12854int12855lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,12856			uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)12857{12858	struct lpfc_hba *phba = vport->phba;12859	struct lpfc_io_buf *lpfc_cmd;12860	struct lpfc_iocbq *abtsiocbq;12861	struct lpfc_nodelist *ndlp = NULL;12862	struct lpfc_iocbq *iocbq;12863	int sum, i, ret_val;12864	unsigned long iflags;12865	struct lpfc_sli_ring *pring_s4 = NULL;12866	u16 ulp_context, iotag, cqid = LPFC_WQE_CQ_ID_DEFAULT;12867	bool ia;12868 12869	/* all I/Os are in process of being flushed */12870	if (test_bit(HBA_IOQ_FLUSH, &phba->hba_flag))12871		return 0;12872 12873	sum = 0;12874 12875	spin_lock_irqsave(&phba->hbalock, iflags);12876	for (i = 1; i <= phba->sli.last_iotag; i++) {12877		iocbq = phba->sli.iocbq_lookup[i];12878 12879		if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))12880			continue;12881 12882		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,12883					       cmd) != 0)12884			continue;12885 12886		/* Guard against IO completion being called at same time */12887		lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);12888		spin_lock(&lpfc_cmd->buf_lock);12889 12890		if (!lpfc_cmd->pCmd) {12891			spin_unlock(&lpfc_cmd->buf_lock);12892			continue;12893		}12894 12895		if (phba->sli_rev == LPFC_SLI_REV4) {12896			pring_s4 =12897			    phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;12898			if (!pring_s4) {12899				spin_unlock(&lpfc_cmd->buf_lock);12900				continue;12901			}12902			/* Note: both hbalock and ring_lock must be set here */12903			spin_lock(&pring_s4->ring_lock);12904		}12905 12906		/*12907		 * If the iocbq is already being aborted, don't take a second12908		 * action, but do count it.12909		 */12910		if ((iocbq->cmd_flag & LPFC_DRIVER_ABORTED) ||12911		    !(iocbq->cmd_flag & LPFC_IO_ON_TXCMPLQ)) {12912			if (phba->sli_rev == LPFC_SLI_REV4)12913				spin_unlock(&pring_s4->ring_lock);12914			spin_unlock(&lpfc_cmd->buf_lock);12915			continue;12916		}12917 12918		/* issue ABTS for this IOCB based on iotag */12919		abtsiocbq = __lpfc_sli_get_iocbq(phba);12920		if (!abtsiocbq) {12921			if (phba->sli_rev == LPFC_SLI_REV4)12922				spin_unlock(&pring_s4->ring_lock);12923			spin_unlock(&lpfc_cmd->buf_lock);12924			continue;12925		}12926 12927		if (phba->sli_rev == LPFC_SLI_REV4) {12928			iotag = abtsiocbq->iotag;12929			ulp_context = iocbq->sli4_xritag;12930			cqid = lpfc_cmd->hdwq->io_cq_map;12931		} else {12932			iotag = iocbq->iocb.ulpIoTag;12933			if (pring->ringno == LPFC_ELS_RING) {12934				ndlp = iocbq->ndlp;12935				ulp_context = ndlp->nlp_rpi;12936			} else {12937				ulp_context = iocbq->iocb.ulpContext;12938			}12939		}12940 12941		ndlp = lpfc_cmd->rdata->pnode;12942 12943		if (lpfc_is_link_up(phba) &&12944		    (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE) &&12945		    !(phba->link_flag & LS_EXTERNAL_LOOPBACK))12946			ia = false;12947		else12948			ia = true;12949 12950		lpfc_sli_prep_abort_xri(phba, abtsiocbq, ulp_context, iotag,12951					iocbq->iocb.ulpClass, cqid,12952					ia, false);12953 12954		abtsiocbq->vport = vport;12955 12956		/* ABTS WQE must go to the same WQ as the WQE to be aborted */12957		abtsiocbq->hba_wqidx = iocbq->hba_wqidx;12958		if (iocbq->cmd_flag & LPFC_IO_FCP)12959			abtsiocbq->cmd_flag |= LPFC_USE_FCPWQIDX;12960		if (iocbq->cmd_flag & LPFC_IO_FOF)12961			abtsiocbq->cmd_flag |= LPFC_IO_FOF;12962 12963		/* Setup callback routine and issue the command. */12964		abtsiocbq->cmd_cmpl = lpfc_sli_abort_fcp_cmpl;12965 12966		/*12967		 * Indicate the IO is being aborted by the driver and set12968		 * the caller's flag into the aborted IO.12969		 */12970		iocbq->cmd_flag |= LPFC_DRIVER_ABORTED;12971 12972		if (phba->sli_rev == LPFC_SLI_REV4) {12973			ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,12974							abtsiocbq, 0);12975			spin_unlock(&pring_s4->ring_lock);12976		} else {12977			ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,12978							abtsiocbq, 0);12979		}12980 12981		spin_unlock(&lpfc_cmd->buf_lock);12982 12983		if (ret_val == IOCB_ERROR)12984			__lpfc_sli_release_iocbq(phba, abtsiocbq);12985		else12986			sum++;12987	}12988	spin_unlock_irqrestore(&phba->hbalock, iflags);12989	return sum;12990}12991 12992/**12993 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler12994 * @phba: Pointer to HBA context object.12995 * @cmdiocbq: Pointer to command iocb.12996 * @rspiocbq: Pointer to response iocb.12997 *12998 * This function is the completion handler for iocbs issued using12999 * lpfc_sli_issue_iocb_wait function. This function is called by the13000 * ring event handler function without any lock held. This function13001 * can be called from both worker thread context and interrupt13002 * context. This function also can be called from other thread which13003 * cleans up the SLI layer objects.13004 * This function copy the contents of the response iocb to the13005 * response iocb memory object provided by the caller of13006 * lpfc_sli_issue_iocb_wait and then wakes up the thread which13007 * sleeps for the iocb completion.13008 **/13009static void13010lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,13011			struct lpfc_iocbq *cmdiocbq,13012			struct lpfc_iocbq *rspiocbq)13013{13014	wait_queue_head_t *pdone_q;13015	unsigned long iflags;13016	struct lpfc_io_buf *lpfc_cmd;13017	size_t offset = offsetof(struct lpfc_iocbq, wqe);13018 13019	spin_lock_irqsave(&phba->hbalock, iflags);13020	if (cmdiocbq->cmd_flag & LPFC_IO_WAKE_TMO) {13021 13022		/*13023		 * A time out has occurred for the iocb.  If a time out13024		 * completion handler has been supplied, call it.  Otherwise,13025		 * just free the iocbq.13026		 */13027 13028		spin_unlock_irqrestore(&phba->hbalock, iflags);13029		cmdiocbq->cmd_cmpl = cmdiocbq->wait_cmd_cmpl;13030		cmdiocbq->wait_cmd_cmpl = NULL;13031		if (cmdiocbq->cmd_cmpl)13032			cmdiocbq->cmd_cmpl(phba, cmdiocbq, NULL);13033		else13034			lpfc_sli_release_iocbq(phba, cmdiocbq);13035		return;13036	}13037 13038	/* Copy the contents of the local rspiocb into the caller's buffer. */13039	cmdiocbq->cmd_flag |= LPFC_IO_WAKE;13040	if (cmdiocbq->rsp_iocb && rspiocbq)13041		memcpy((char *)cmdiocbq->rsp_iocb + offset,13042		       (char *)rspiocbq + offset, sizeof(*rspiocbq) - offset);13043 13044	/* Set the exchange busy flag for task management commands */13045	if ((cmdiocbq->cmd_flag & LPFC_IO_FCP) &&13046	    !(cmdiocbq->cmd_flag & LPFC_IO_LIBDFC)) {13047		lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,13048					cur_iocbq);13049		if (rspiocbq && (rspiocbq->cmd_flag & LPFC_EXCHANGE_BUSY))13050			lpfc_cmd->flags |= LPFC_SBUF_XBUSY;13051		else13052			lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;13053	}13054 13055	pdone_q = cmdiocbq->context_un.wait_queue;13056	if (pdone_q)13057		wake_up(pdone_q);13058	spin_unlock_irqrestore(&phba->hbalock, iflags);13059	return;13060}13061 13062/**13063 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.13064 * @phba: Pointer to HBA context object..13065 * @piocbq: Pointer to command iocb.13066 * @flag: Flag to test.13067 *13068 * This routine grabs the hbalock and then test the cmd_flag to13069 * see if the passed in flag is set.13070 * Returns:13071 * 1 if flag is set.13072 * 0 if flag is not set.13073 **/13074static int13075lpfc_chk_iocb_flg(struct lpfc_hba *phba,13076		 struct lpfc_iocbq *piocbq, uint32_t flag)13077{13078	unsigned long iflags;13079	int ret;13080 13081	spin_lock_irqsave(&phba->hbalock, iflags);13082	ret = piocbq->cmd_flag & flag;13083	spin_unlock_irqrestore(&phba->hbalock, iflags);13084	return ret;13085 13086}13087 13088/**13089 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands13090 * @phba: Pointer to HBA context object..13091 * @ring_number: Ring number13092 * @piocb: Pointer to command iocb.13093 * @prspiocbq: Pointer to response iocb.13094 * @timeout: Timeout in number of seconds.13095 *13096 * This function issues the iocb to firmware and waits for the13097 * iocb to complete. The cmd_cmpl field of the shall be used13098 * to handle iocbs which time out. If the field is NULL, the13099 * function shall free the iocbq structure.  If more clean up is13100 * needed, the caller is expected to provide a completion function13101 * that will provide the needed clean up.  If the iocb command is13102 * not completed within timeout seconds, the function will either13103 * free the iocbq structure (if cmd_cmpl == NULL) or execute the13104 * completion function set in the cmd_cmpl field and then return13105 * a status of IOCB_TIMEDOUT.  The caller should not free the iocb13106 * resources if this function returns IOCB_TIMEDOUT.13107 * The function waits for the iocb completion using an13108 * non-interruptible wait.13109 * This function will sleep while waiting for iocb completion.13110 * So, this function should not be called from any context which13111 * does not allow sleeping. Due to the same reason, this function13112 * cannot be called with interrupt disabled.13113 * This function assumes that the iocb completions occur while13114 * this function sleep. So, this function cannot be called from13115 * the thread which process iocb completion for this ring.13116 * This function clears the cmd_flag of the iocb object before13117 * issuing the iocb and the iocb completion handler sets this13118 * flag and wakes this thread when the iocb completes.13119 * The contents of the response iocb will be copied to prspiocbq13120 * by the completion handler when the command completes.13121 * This function returns IOCB_SUCCESS when success.13122 * This function is called with no lock held.13123 **/13124int13125lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,13126			 uint32_t ring_number,13127			 struct lpfc_iocbq *piocb,13128			 struct lpfc_iocbq *prspiocbq,13129			 uint32_t timeout)13130{13131	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);13132	long timeleft, timeout_req = 0;13133	int retval = IOCB_SUCCESS;13134	uint32_t creg_val;13135	struct lpfc_iocbq *iocb;13136	int txq_cnt = 0;13137	int txcmplq_cnt = 0;13138	struct lpfc_sli_ring *pring;13139	unsigned long iflags;13140	bool iocb_completed = true;13141 13142	if (phba->sli_rev >= LPFC_SLI_REV4) {13143		lpfc_sli_prep_wqe(phba, piocb);13144 13145		pring = lpfc_sli4_calc_ring(phba, piocb);13146	} else13147		pring = &phba->sli.sli3_ring[ring_number];13148	/*13149	 * If the caller has provided a response iocbq buffer, then rsp_iocb13150	 * is NULL or its an error.13151	 */13152	if (prspiocbq) {13153		if (piocb->rsp_iocb)13154			return IOCB_ERROR;13155		piocb->rsp_iocb = prspiocbq;13156	}13157 13158	piocb->wait_cmd_cmpl = piocb->cmd_cmpl;13159	piocb->cmd_cmpl = lpfc_sli_wake_iocb_wait;13160	piocb->context_un.wait_queue = &done_q;13161	piocb->cmd_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);13162 13163	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {13164		if (lpfc_readl(phba->HCregaddr, &creg_val))13165			return IOCB_ERROR;13166		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);13167		writel(creg_val, phba->HCregaddr);13168		readl(phba->HCregaddr); /* flush */13169	}13170 13171	retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,13172				     SLI_IOCB_RET_IOCB);13173	if (retval == IOCB_SUCCESS) {13174		timeout_req = msecs_to_jiffies(timeout * 1000);13175		timeleft = wait_event_timeout(done_q,13176				lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),13177				timeout_req);13178		spin_lock_irqsave(&phba->hbalock, iflags);13179		if (!(piocb->cmd_flag & LPFC_IO_WAKE)) {13180 13181			/*13182			 * IOCB timed out.  Inform the wake iocb wait13183			 * completion function and set local status13184			 */13185 13186			iocb_completed = false;13187			piocb->cmd_flag |= LPFC_IO_WAKE_TMO;13188		}13189		spin_unlock_irqrestore(&phba->hbalock, iflags);13190		if (iocb_completed) {13191			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,13192					"0331 IOCB wake signaled\n");13193			/* Note: we are not indicating if the IOCB has a success13194			 * status or not - that's for the caller to check.13195			 * IOCB_SUCCESS means just that the command was sent and13196			 * completed. Not that it completed successfully.13197			 * */13198		} else if (timeleft == 0) {13199			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,13200					"0338 IOCB wait timeout error - no "13201					"wake response Data x%x\n", timeout);13202			retval = IOCB_TIMEDOUT;13203		} else {13204			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,13205					"0330 IOCB wake NOT set, "13206					"Data x%x x%lx\n",13207					timeout, (timeleft / jiffies));13208			retval = IOCB_TIMEDOUT;13209		}13210	} else if (retval == IOCB_BUSY) {13211		if (phba->cfg_log_verbose & LOG_SLI) {13212			list_for_each_entry(iocb, &pring->txq, list) {13213				txq_cnt++;13214			}13215			list_for_each_entry(iocb, &pring->txcmplq, list) {13216				txcmplq_cnt++;13217			}13218			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,13219				"2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",13220				phba->iocb_cnt, txq_cnt, txcmplq_cnt);13221		}13222		return retval;13223	} else {13224		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,13225				"0332 IOCB wait issue failed, Data x%x\n",13226				retval);13227		retval = IOCB_ERROR;13228	}13229 13230	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {13231		if (lpfc_readl(phba->HCregaddr, &creg_val))13232			return IOCB_ERROR;13233		creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);13234		writel(creg_val, phba->HCregaddr);13235		readl(phba->HCregaddr); /* flush */13236	}13237 13238	if (prspiocbq)13239		piocb->rsp_iocb = NULL;13240 13241	piocb->context_un.wait_queue = NULL;13242	piocb->cmd_cmpl = NULL;13243	return retval;13244}13245 13246/**13247 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox13248 * @phba: Pointer to HBA context object.13249 * @pmboxq: Pointer to driver mailbox object.13250 * @timeout: Timeout in number of seconds.13251 *13252 * This function issues the mailbox to firmware and waits for the13253 * mailbox command to complete. If the mailbox command is not13254 * completed within timeout seconds, it returns MBX_TIMEOUT.13255 * The function waits for the mailbox completion using an13256 * interruptible wait. If the thread is woken up due to a13257 * signal, MBX_TIMEOUT error is returned to the caller. Caller13258 * should not free the mailbox resources, if this function returns13259 * MBX_TIMEOUT.13260 * This function will sleep while waiting for mailbox completion.13261 * So, this function should not be called from any context which13262 * does not allow sleeping. Due to the same reason, this function13263 * cannot be called with interrupt disabled.13264 * This function assumes that the mailbox completion occurs while13265 * this function sleep. So, this function cannot be called from13266 * the worker thread which processes mailbox completion.13267 * This function is called in the context of HBA management13268 * applications.13269 * This function returns MBX_SUCCESS when successful.13270 * This function is called with no lock held.13271 **/13272int13273lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,13274			 uint32_t timeout)13275{13276	struct completion mbox_done;13277	int retval;13278	unsigned long flag;13279 13280	pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;13281	/* setup wake call as IOCB callback */13282	pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;13283 13284	/* setup ctx_u field to pass wait_queue pointer to wake function  */13285	init_completion(&mbox_done);13286	pmboxq->ctx_u.mbox_wait = &mbox_done;13287	/* now issue the command */13288	retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);13289	if (retval == MBX_BUSY || retval == MBX_SUCCESS) {13290		wait_for_completion_timeout(&mbox_done,13291					    msecs_to_jiffies(timeout * 1000));13292 13293		spin_lock_irqsave(&phba->hbalock, flag);13294		pmboxq->ctx_u.mbox_wait = NULL;13295		/*13296		 * if LPFC_MBX_WAKE flag is set the mailbox is completed13297		 * else do not free the resources.13298		 */13299		if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {13300			retval = MBX_SUCCESS;13301		} else {13302			retval = MBX_TIMEOUT;13303			pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;13304		}13305		spin_unlock_irqrestore(&phba->hbalock, flag);13306	}13307	return retval;13308}13309 13310/**13311 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system13312 * @phba: Pointer to HBA context.13313 * @mbx_action: Mailbox shutdown options.13314 *13315 * This function is called to shutdown the driver's mailbox sub-system.13316 * It first marks the mailbox sub-system is in a block state to prevent13317 * the asynchronous mailbox command from issued off the pending mailbox13318 * command queue. If the mailbox command sub-system shutdown is due to13319 * HBA error conditions such as EEH or ERATT, this routine shall invoke13320 * the mailbox sub-system flush routine to forcefully bring down the13321 * mailbox sub-system. Otherwise, if it is due to normal condition (such13322 * as with offline or HBA function reset), this routine will wait for the13323 * outstanding mailbox command to complete before invoking the mailbox13324 * sub-system flush routine to gracefully bring down mailbox sub-system.13325 **/13326void13327lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)13328{13329	struct lpfc_sli *psli = &phba->sli;13330	unsigned long timeout;13331 13332	if (mbx_action == LPFC_MBX_NO_WAIT) {13333		/* delay 100ms for port state */13334		msleep(100);13335		lpfc_sli_mbox_sys_flush(phba);13336		return;13337	}13338	timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;13339 13340	/* Disable softirqs, including timers from obtaining phba->hbalock */13341	local_bh_disable();13342 13343	spin_lock_irq(&phba->hbalock);13344	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;13345 13346	if (psli->sli_flag & LPFC_SLI_ACTIVE) {13347		/* Determine how long we might wait for the active mailbox13348		 * command to be gracefully completed by firmware.13349		 */13350		if (phba->sli.mbox_active)13351			timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,13352						phba->sli.mbox_active) *13353						1000) + jiffies;13354		spin_unlock_irq(&phba->hbalock);13355 13356		/* Enable softirqs again, done with phba->hbalock */13357		local_bh_enable();13358 13359		while (phba->sli.mbox_active) {13360			/* Check active mailbox complete status every 2ms */13361			msleep(2);13362			if (time_after(jiffies, timeout))13363				/* Timeout, let the mailbox flush routine to13364				 * forcefully release active mailbox command13365				 */13366				break;13367		}13368	} else {13369		spin_unlock_irq(&phba->hbalock);13370 13371		/* Enable softirqs again, done with phba->hbalock */13372		local_bh_enable();13373	}13374 13375	lpfc_sli_mbox_sys_flush(phba);13376}13377 13378/**13379 * lpfc_sli_eratt_read - read sli-3 error attention events13380 * @phba: Pointer to HBA context.13381 *13382 * This function is called to read the SLI3 device error attention registers13383 * for possible error attention events. The caller must hold the hostlock13384 * with spin_lock_irq().13385 *13386 * This function returns 1 when there is Error Attention in the Host Attention13387 * Register and returns 0 otherwise.13388 **/13389static int13390lpfc_sli_eratt_read(struct lpfc_hba *phba)13391{13392	uint32_t ha_copy;13393 13394	/* Read chip Host Attention (HA) register */13395	if (lpfc_readl(phba->HAregaddr, &ha_copy))13396		goto unplug_err;13397 13398	if (ha_copy & HA_ERATT) {13399		/* Read host status register to retrieve error event */13400		if (lpfc_sli_read_hs(phba))13401			goto unplug_err;13402 13403		/* Check if there is a deferred error condition is active */13404		if ((HS_FFER1 & phba->work_hs) &&13405		    ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |13406		      HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {13407			set_bit(DEFER_ERATT, &phba->hba_flag);13408			/* Clear all interrupt enable conditions */13409			writel(0, phba->HCregaddr);13410			readl(phba->HCregaddr);13411		}13412 13413		/* Set the driver HA work bitmap */13414		phba->work_ha |= HA_ERATT;13415		/* Indicate polling handles this ERATT */13416		set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);13417		return 1;13418	}13419	return 0;13420 13421unplug_err:13422	/* Set the driver HS work bitmap */13423	phba->work_hs |= UNPLUG_ERR;13424	/* Set the driver HA work bitmap */13425	phba->work_ha |= HA_ERATT;13426	/* Indicate polling handles this ERATT */13427	set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);13428	return 1;13429}13430 13431/**13432 * lpfc_sli4_eratt_read - read sli-4 error attention events13433 * @phba: Pointer to HBA context.13434 *13435 * This function is called to read the SLI4 device error attention registers13436 * for possible error attention events. The caller must hold the hostlock13437 * with spin_lock_irq().13438 *13439 * This function returns 1 when there is Error Attention in the Host Attention13440 * Register and returns 0 otherwise.13441 **/13442static int13443lpfc_sli4_eratt_read(struct lpfc_hba *phba)13444{13445	uint32_t uerr_sta_hi, uerr_sta_lo;13446	uint32_t if_type, portsmphr;13447	struct lpfc_register portstat_reg;13448	u32 logmask;13449 13450	/*13451	 * For now, use the SLI4 device internal unrecoverable error13452	 * registers for error attention. This can be changed later.13453	 */13454	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);13455	switch (if_type) {13456	case LPFC_SLI_INTF_IF_TYPE_0:13457		if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,13458			&uerr_sta_lo) ||13459			lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,13460			&uerr_sta_hi)) {13461			phba->work_hs |= UNPLUG_ERR;13462			phba->work_ha |= HA_ERATT;13463			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);13464			return 1;13465		}13466		if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||13467		    (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {13468			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,13469					"1423 HBA Unrecoverable error: "13470					"uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "13471					"ue_mask_lo_reg=0x%x, "13472					"ue_mask_hi_reg=0x%x\n",13473					uerr_sta_lo, uerr_sta_hi,13474					phba->sli4_hba.ue_mask_lo,13475					phba->sli4_hba.ue_mask_hi);13476			phba->work_status[0] = uerr_sta_lo;13477			phba->work_status[1] = uerr_sta_hi;13478			phba->work_ha |= HA_ERATT;13479			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);13480			return 1;13481		}13482		break;13483	case LPFC_SLI_INTF_IF_TYPE_2:13484	case LPFC_SLI_INTF_IF_TYPE_6:13485		if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,13486			&portstat_reg.word0) ||13487			lpfc_readl(phba->sli4_hba.PSMPHRregaddr,13488			&portsmphr)){13489			phba->work_hs |= UNPLUG_ERR;13490			phba->work_ha |= HA_ERATT;13491			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);13492			return 1;13493		}13494		if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {13495			phba->work_status[0] =13496				readl(phba->sli4_hba.u.if_type2.ERR1regaddr);13497			phba->work_status[1] =13498				readl(phba->sli4_hba.u.if_type2.ERR2regaddr);13499			logmask = LOG_TRACE_EVENT;13500			if (phba->work_status[0] ==13501				SLIPORT_ERR1_REG_ERR_CODE_2 &&13502			    phba->work_status[1] == SLIPORT_ERR2_REG_FW_RESTART)13503				logmask = LOG_SLI;13504			lpfc_printf_log(phba, KERN_ERR, logmask,13505					"2885 Port Status Event: "13506					"port status reg 0x%x, "13507					"port smphr reg 0x%x, "13508					"error 1=0x%x, error 2=0x%x\n",13509					portstat_reg.word0,13510					portsmphr,13511					phba->work_status[0],13512					phba->work_status[1]);13513			phba->work_ha |= HA_ERATT;13514			set_bit(HBA_ERATT_HANDLED, &phba->hba_flag);13515			return 1;13516		}13517		break;13518	case LPFC_SLI_INTF_IF_TYPE_1:13519	default:13520		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,13521				"2886 HBA Error Attention on unsupported "13522				"if type %d.", if_type);13523		return 1;13524	}13525 13526	return 0;13527}13528 13529/**13530 * lpfc_sli_check_eratt - check error attention events13531 * @phba: Pointer to HBA context.13532 *13533 * This function is called from timer soft interrupt context to check HBA's13534 * error attention register bit for error attention events.13535 *13536 * This function returns 1 when there is Error Attention in the Host Attention13537 * Register and returns 0 otherwise.13538 **/13539int13540lpfc_sli_check_eratt(struct lpfc_hba *phba)13541{13542	uint32_t ha_copy;13543 13544	/* If somebody is waiting to handle an eratt, don't process it13545	 * here. The brdkill function will do this.13546	 */13547	if (phba->link_flag & LS_IGNORE_ERATT)13548		return 0;13549 13550	/* Check if interrupt handler handles this ERATT */13551	if (test_bit(HBA_ERATT_HANDLED, &phba->hba_flag))13552		/* Interrupt handler has handled ERATT */13553		return 0;13554 13555	/*13556	 * If there is deferred error attention, do not check for error13557	 * attention13558	 */13559	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))13560		return 0;13561 13562	spin_lock_irq(&phba->hbalock);13563	/* If PCI channel is offline, don't process it */13564	if (unlikely(pci_channel_offline(phba->pcidev))) {13565		spin_unlock_irq(&phba->hbalock);13566		return 0;13567	}13568 13569	switch (phba->sli_rev) {13570	case LPFC_SLI_REV2:13571	case LPFC_SLI_REV3:13572		/* Read chip Host Attention (HA) register */13573		ha_copy = lpfc_sli_eratt_read(phba);13574		break;13575	case LPFC_SLI_REV4:13576		/* Read device Uncoverable Error (UERR) registers */13577		ha_copy = lpfc_sli4_eratt_read(phba);13578		break;13579	default:13580		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,13581				"0299 Invalid SLI revision (%d)\n",13582				phba->sli_rev);13583		ha_copy = 0;13584		break;13585	}13586	spin_unlock_irq(&phba->hbalock);13587 13588	return ha_copy;13589}13590 13591/**13592 * lpfc_intr_state_check - Check device state for interrupt handling13593 * @phba: Pointer to HBA context.13594 *13595 * This inline routine checks whether a device or its PCI slot is in a state13596 * that the interrupt should be handled.13597 *13598 * This function returns 0 if the device or the PCI slot is in a state that13599 * interrupt should be handled, otherwise -EIO.13600 */13601static inline int13602lpfc_intr_state_check(struct lpfc_hba *phba)13603{13604	/* If the pci channel is offline, ignore all the interrupts */13605	if (unlikely(pci_channel_offline(phba->pcidev)))13606		return -EIO;13607 13608	/* Update device level interrupt statistics */13609	phba->sli.slistat.sli_intr++;13610 13611	/* Ignore all interrupts during initialization. */13612	if (unlikely(phba->link_state < LPFC_LINK_DOWN))13613		return -EIO;13614 13615	return 0;13616}13617 13618/**13619 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device13620 * @irq: Interrupt number.13621 * @dev_id: The device context pointer.13622 *13623 * This function is directly called from the PCI layer as an interrupt13624 * service routine when device with SLI-3 interface spec is enabled with13625 * MSI-X multi-message interrupt mode and there are slow-path events in13626 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ13627 * interrupt mode, this function is called as part of the device-level13628 * interrupt handler. When the PCI slot is in error recovery or the HBA13629 * is undergoing initialization, the interrupt handler will not process13630 * the interrupt. The link attention and ELS ring attention events are13631 * handled by the worker thread. The interrupt handler signals the worker13632 * thread and returns for these events. This function is called without13633 * any lock held. It gets the hbalock to access and update SLI data13634 * structures.13635 *13636 * This function returns IRQ_HANDLED when interrupt is handled else it13637 * returns IRQ_NONE.13638 **/13639irqreturn_t13640lpfc_sli_sp_intr_handler(int irq, void *dev_id)13641{13642	struct lpfc_hba  *phba;13643	uint32_t ha_copy, hc_copy;13644	uint32_t work_ha_copy;13645	unsigned long status;13646	unsigned long iflag;13647	uint32_t control;13648 13649	MAILBOX_t *mbox, *pmbox;13650	struct lpfc_vport *vport;13651	struct lpfc_nodelist *ndlp;13652	struct lpfc_dmabuf *mp;13653	LPFC_MBOXQ_t *pmb;13654	int rc;13655 13656	/*13657	 * Get the driver's phba structure from the dev_id and13658	 * assume the HBA is not interrupting.13659	 */13660	phba = (struct lpfc_hba *)dev_id;13661 13662	if (unlikely(!phba))13663		return IRQ_NONE;13664 13665	/*13666	 * Stuff needs to be attented to when this function is invoked as an13667	 * individual interrupt handler in MSI-X multi-message interrupt mode13668	 */13669	if (phba->intr_type == MSIX) {13670		/* Check device state for handling interrupt */13671		if (lpfc_intr_state_check(phba))13672			return IRQ_NONE;13673		/* Need to read HA REG for slow-path events */13674		spin_lock_irqsave(&phba->hbalock, iflag);13675		if (lpfc_readl(phba->HAregaddr, &ha_copy))13676			goto unplug_error;13677		/* If somebody is waiting to handle an eratt don't process it13678		 * here. The brdkill function will do this.13679		 */13680		if (phba->link_flag & LS_IGNORE_ERATT)13681			ha_copy &= ~HA_ERATT;13682		/* Check the need for handling ERATT in interrupt handler */13683		if (ha_copy & HA_ERATT) {13684			if (test_and_set_bit(HBA_ERATT_HANDLED,13685					     &phba->hba_flag))13686				/* ERATT polling has handled ERATT */13687				ha_copy &= ~HA_ERATT;13688		}13689 13690		/*13691		 * If there is deferred error attention, do not check for any13692		 * interrupt.13693		 */13694		if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {13695			spin_unlock_irqrestore(&phba->hbalock, iflag);13696			return IRQ_NONE;13697		}13698 13699		/* Clear up only attention source related to slow-path */13700		if (lpfc_readl(phba->HCregaddr, &hc_copy))13701			goto unplug_error;13702 13703		writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |13704			HC_LAINT_ENA | HC_ERINT_ENA),13705			phba->HCregaddr);13706		writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),13707			phba->HAregaddr);13708		writel(hc_copy, phba->HCregaddr);13709		readl(phba->HAregaddr); /* flush */13710		spin_unlock_irqrestore(&phba->hbalock, iflag);13711	} else13712		ha_copy = phba->ha_copy;13713 13714	work_ha_copy = ha_copy & phba->work_ha_mask;13715 13716	if (work_ha_copy) {13717		if (work_ha_copy & HA_LATT) {13718			if (phba->sli.sli_flag & LPFC_PROCESS_LA) {13719				/*13720				 * Turn off Link Attention interrupts13721				 * until CLEAR_LA done13722				 */13723				spin_lock_irqsave(&phba->hbalock, iflag);13724				phba->sli.sli_flag &= ~LPFC_PROCESS_LA;13725				if (lpfc_readl(phba->HCregaddr, &control))13726					goto unplug_error;13727				control &= ~HC_LAINT_ENA;13728				writel(control, phba->HCregaddr);13729				readl(phba->HCregaddr); /* flush */13730				spin_unlock_irqrestore(&phba->hbalock, iflag);13731			}13732			else13733				work_ha_copy &= ~HA_LATT;13734		}13735 13736		if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {13737			/*13738			 * Turn off Slow Rings interrupts, LPFC_ELS_RING is13739			 * the only slow ring.13740			 */13741			status = (work_ha_copy &13742				(HA_RXMASK  << (4*LPFC_ELS_RING)));13743			status >>= (4*LPFC_ELS_RING);13744			if (status & HA_RXMASK) {13745				spin_lock_irqsave(&phba->hbalock, iflag);13746				if (lpfc_readl(phba->HCregaddr, &control))13747					goto unplug_error;13748 13749				lpfc_debugfs_slow_ring_trc(phba,13750				"ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",13751				control, status,13752				(uint32_t)phba->sli.slistat.sli_intr);13753 13754				if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {13755					lpfc_debugfs_slow_ring_trc(phba,13756						"ISR Disable ring:"13757						"pwork:x%x hawork:x%x wait:x%x",13758						phba->work_ha, work_ha_copy,13759						(uint32_t)((unsigned long)13760						&phba->work_waitq));13761 13762					control &=13763					    ~(HC_R0INT_ENA << LPFC_ELS_RING);13764					writel(control, phba->HCregaddr);13765					readl(phba->HCregaddr); /* flush */13766				}13767				else {13768					lpfc_debugfs_slow_ring_trc(phba,13769						"ISR slow ring:   pwork:"13770						"x%x hawork:x%x wait:x%x",13771						phba->work_ha, work_ha_copy,13772						(uint32_t)((unsigned long)13773						&phba->work_waitq));13774				}13775				spin_unlock_irqrestore(&phba->hbalock, iflag);13776			}13777		}13778		spin_lock_irqsave(&phba->hbalock, iflag);13779		if (work_ha_copy & HA_ERATT) {13780			if (lpfc_sli_read_hs(phba))13781				goto unplug_error;13782			/*13783			 * Check if there is a deferred error condition13784			 * is active13785			 */13786			if ((HS_FFER1 & phba->work_hs) &&13787				((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |13788				  HS_FFER6 | HS_FFER7 | HS_FFER8) &13789				  phba->work_hs)) {13790				set_bit(DEFER_ERATT, &phba->hba_flag);13791				/* Clear all interrupt enable conditions */13792				writel(0, phba->HCregaddr);13793				readl(phba->HCregaddr);13794			}13795		}13796 13797		if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {13798			pmb = phba->sli.mbox_active;13799			pmbox = &pmb->u.mb;13800			mbox = phba->mbox;13801			vport = pmb->vport;13802 13803			/* First check out the status word */13804			lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));13805			if (pmbox->mbxOwner != OWN_HOST) {13806				spin_unlock_irqrestore(&phba->hbalock, iflag);13807				/*13808				 * Stray Mailbox Interrupt, mbxCommand <cmd>13809				 * mbxStatus <status>13810				 */13811				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,13812						"(%d):0304 Stray Mailbox "13813						"Interrupt mbxCommand x%x "13814						"mbxStatus x%x\n",13815						(vport ? vport->vpi : 0),13816						pmbox->mbxCommand,13817						pmbox->mbxStatus);13818				/* clear mailbox attention bit */13819				work_ha_copy &= ~HA_MBATT;13820			} else {13821				phba->sli.mbox_active = NULL;13822				spin_unlock_irqrestore(&phba->hbalock, iflag);13823				phba->last_completion_time = jiffies;13824				del_timer(&phba->sli.mbox_tmo);13825				if (pmb->mbox_cmpl) {13826					lpfc_sli_pcimem_bcopy(mbox, pmbox,13827							MAILBOX_CMD_SIZE);13828					if (pmb->out_ext_byte_len &&13829						pmb->ext_buf)13830						lpfc_sli_pcimem_bcopy(13831						phba->mbox_ext,13832						pmb->ext_buf,13833						pmb->out_ext_byte_len);13834				}13835				if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {13836					pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;13837 13838					lpfc_debugfs_disc_trc(vport,13839						LPFC_DISC_TRC_MBOX_VPORT,13840						"MBOX dflt rpi: : "13841						"status:x%x rpi:x%x",13842						(uint32_t)pmbox->mbxStatus,13843						pmbox->un.varWords[0], 0);13844 13845					if (!pmbox->mbxStatus) {13846						mp = pmb->ctx_buf;13847						ndlp = pmb->ctx_ndlp;13848 13849						/* Reg_LOGIN of dflt RPI was13850						 * successful. new lets get13851						 * rid of the RPI using the13852						 * same mbox buffer.13853						 */13854						lpfc_unreg_login(phba,13855							vport->vpi,13856							pmbox->un.varWords[0],13857							pmb);13858						pmb->mbox_cmpl =13859							lpfc_mbx_cmpl_dflt_rpi;13860						pmb->ctx_buf = mp;13861						pmb->ctx_ndlp = ndlp;13862						pmb->vport = vport;13863						rc = lpfc_sli_issue_mbox(phba,13864								pmb,13865								MBX_NOWAIT);13866						if (rc != MBX_BUSY)13867							lpfc_printf_log(phba,13868							KERN_ERR,13869							LOG_TRACE_EVENT,13870							"0350 rc should have"13871							"been MBX_BUSY\n");13872						if (rc != MBX_NOT_FINISHED)13873							goto send_current_mbox;13874					}13875				}13876				spin_lock_irqsave(13877						&phba->pport->work_port_lock,13878						iflag);13879				phba->pport->work_port_events &=13880					~WORKER_MBOX_TMO;13881				spin_unlock_irqrestore(13882						&phba->pport->work_port_lock,13883						iflag);13884 13885				/* Do NOT queue MBX_HEARTBEAT to the worker13886				 * thread for processing.13887				 */13888				if (pmbox->mbxCommand == MBX_HEARTBEAT) {13889					/* Process mbox now */13890					phba->sli.mbox_active = NULL;13891					phba->sli.sli_flag &=13892						~LPFC_SLI_MBOX_ACTIVE;13893					if (pmb->mbox_cmpl)13894						pmb->mbox_cmpl(phba, pmb);13895				} else {13896					/* Queue to worker thread to process */13897					lpfc_mbox_cmpl_put(phba, pmb);13898				}13899			}13900		} else13901			spin_unlock_irqrestore(&phba->hbalock, iflag);13902 13903		if ((work_ha_copy & HA_MBATT) &&13904		    (phba->sli.mbox_active == NULL)) {13905send_current_mbox:13906			/* Process next mailbox command if there is one */13907			do {13908				rc = lpfc_sli_issue_mbox(phba, NULL,13909							 MBX_NOWAIT);13910			} while (rc == MBX_NOT_FINISHED);13911			if (rc != MBX_SUCCESS)13912				lpfc_printf_log(phba, KERN_ERR,13913						LOG_TRACE_EVENT,13914						"0349 rc should be "13915						"MBX_SUCCESS\n");13916		}13917 13918		spin_lock_irqsave(&phba->hbalock, iflag);13919		phba->work_ha |= work_ha_copy;13920		spin_unlock_irqrestore(&phba->hbalock, iflag);13921		lpfc_worker_wake_up(phba);13922	}13923	return IRQ_HANDLED;13924unplug_error:13925	spin_unlock_irqrestore(&phba->hbalock, iflag);13926	return IRQ_HANDLED;13927 13928} /* lpfc_sli_sp_intr_handler */13929 13930/**13931 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.13932 * @irq: Interrupt number.13933 * @dev_id: The device context pointer.13934 *13935 * This function is directly called from the PCI layer as an interrupt13936 * service routine when device with SLI-3 interface spec is enabled with13937 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB13938 * ring event in the HBA. However, when the device is enabled with either13939 * MSI or Pin-IRQ interrupt mode, this function is called as part of the13940 * device-level interrupt handler. When the PCI slot is in error recovery13941 * or the HBA is undergoing initialization, the interrupt handler will not13942 * process the interrupt. The SCSI FCP fast-path ring event are handled in13943 * the intrrupt context. This function is called without any lock held.13944 * It gets the hbalock to access and update SLI data structures.13945 *13946 * This function returns IRQ_HANDLED when interrupt is handled else it13947 * returns IRQ_NONE.13948 **/13949irqreturn_t13950lpfc_sli_fp_intr_handler(int irq, void *dev_id)13951{13952	struct lpfc_hba  *phba;13953	uint32_t ha_copy;13954	unsigned long status;13955	unsigned long iflag;13956	struct lpfc_sli_ring *pring;13957 13958	/* Get the driver's phba structure from the dev_id and13959	 * assume the HBA is not interrupting.13960	 */13961	phba = (struct lpfc_hba *) dev_id;13962 13963	if (unlikely(!phba))13964		return IRQ_NONE;13965 13966	/*13967	 * Stuff needs to be attented to when this function is invoked as an13968	 * individual interrupt handler in MSI-X multi-message interrupt mode13969	 */13970	if (phba->intr_type == MSIX) {13971		/* Check device state for handling interrupt */13972		if (lpfc_intr_state_check(phba))13973			return IRQ_NONE;13974		/* Need to read HA REG for FCP ring and other ring events */13975		if (lpfc_readl(phba->HAregaddr, &ha_copy))13976			return IRQ_HANDLED;13977 13978		/*13979		 * If there is deferred error attention, do not check for13980		 * any interrupt.13981		 */13982		if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag)))13983			return IRQ_NONE;13984 13985		/* Clear up only attention source related to fast-path */13986		spin_lock_irqsave(&phba->hbalock, iflag);13987		writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),13988			phba->HAregaddr);13989		readl(phba->HAregaddr); /* flush */13990		spin_unlock_irqrestore(&phba->hbalock, iflag);13991	} else13992		ha_copy = phba->ha_copy;13993 13994	/*13995	 * Process all events on FCP ring. Take the optimized path for FCP IO.13996	 */13997	ha_copy &= ~(phba->work_ha_mask);13998 13999	status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));14000	status >>= (4*LPFC_FCP_RING);14001	pring = &phba->sli.sli3_ring[LPFC_FCP_RING];14002	if (status & HA_RXMASK)14003		lpfc_sli_handle_fast_ring_event(phba, pring, status);14004 14005	if (phba->cfg_multi_ring_support == 2) {14006		/*14007		 * Process all events on extra ring. Take the optimized path14008		 * for extra ring IO.14009		 */14010		status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));14011		status >>= (4*LPFC_EXTRA_RING);14012		if (status & HA_RXMASK) {14013			lpfc_sli_handle_fast_ring_event(phba,14014					&phba->sli.sli3_ring[LPFC_EXTRA_RING],14015					status);14016		}14017	}14018	return IRQ_HANDLED;14019}  /* lpfc_sli_fp_intr_handler */14020 14021/**14022 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device14023 * @irq: Interrupt number.14024 * @dev_id: The device context pointer.14025 *14026 * This function is the HBA device-level interrupt handler to device with14027 * SLI-3 interface spec, called from the PCI layer when either MSI or14028 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which14029 * requires driver attention. This function invokes the slow-path interrupt14030 * attention handling function and fast-path interrupt attention handling14031 * function in turn to process the relevant HBA attention events. This14032 * function is called without any lock held. It gets the hbalock to access14033 * and update SLI data structures.14034 *14035 * This function returns IRQ_HANDLED when interrupt is handled, else it14036 * returns IRQ_NONE.14037 **/14038irqreturn_t14039lpfc_sli_intr_handler(int irq, void *dev_id)14040{14041	struct lpfc_hba  *phba;14042	irqreturn_t sp_irq_rc, fp_irq_rc;14043	unsigned long status1, status2;14044	uint32_t hc_copy;14045 14046	/*14047	 * Get the driver's phba structure from the dev_id and14048	 * assume the HBA is not interrupting.14049	 */14050	phba = (struct lpfc_hba *) dev_id;14051 14052	if (unlikely(!phba))14053		return IRQ_NONE;14054 14055	/* Check device state for handling interrupt */14056	if (lpfc_intr_state_check(phba))14057		return IRQ_NONE;14058 14059	spin_lock(&phba->hbalock);14060	if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {14061		spin_unlock(&phba->hbalock);14062		return IRQ_HANDLED;14063	}14064 14065	if (unlikely(!phba->ha_copy)) {14066		spin_unlock(&phba->hbalock);14067		return IRQ_NONE;14068	} else if (phba->ha_copy & HA_ERATT) {14069		if (test_and_set_bit(HBA_ERATT_HANDLED, &phba->hba_flag))14070			/* ERATT polling has handled ERATT */14071			phba->ha_copy &= ~HA_ERATT;14072	}14073 14074	/*14075	 * If there is deferred error attention, do not check for any interrupt.14076	 */14077	if (unlikely(test_bit(DEFER_ERATT, &phba->hba_flag))) {14078		spin_unlock(&phba->hbalock);14079		return IRQ_NONE;14080	}14081 14082	/* Clear attention sources except link and error attentions */14083	if (lpfc_readl(phba->HCregaddr, &hc_copy)) {14084		spin_unlock(&phba->hbalock);14085		return IRQ_HANDLED;14086	}14087	writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA14088		| HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),14089		phba->HCregaddr);14090	writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);14091	writel(hc_copy, phba->HCregaddr);14092	readl(phba->HAregaddr); /* flush */14093	spin_unlock(&phba->hbalock);14094 14095	/*14096	 * Invokes slow-path host attention interrupt handling as appropriate.14097	 */14098 14099	/* status of events with mailbox and link attention */14100	status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);14101 14102	/* status of events with ELS ring */14103	status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));14104	status2 >>= (4*LPFC_ELS_RING);14105 14106	if (status1 || (status2 & HA_RXMASK))14107		sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);14108	else14109		sp_irq_rc = IRQ_NONE;14110 14111	/*14112	 * Invoke fast-path host attention interrupt handling as appropriate.14113	 */14114 14115	/* status of events with FCP ring */14116	status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));14117	status1 >>= (4*LPFC_FCP_RING);14118 14119	/* status of events with extra ring */14120	if (phba->cfg_multi_ring_support == 2) {14121		status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));14122		status2 >>= (4*LPFC_EXTRA_RING);14123	} else14124		status2 = 0;14125 14126	if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))14127		fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);14128	else14129		fp_irq_rc = IRQ_NONE;14130 14131	/* Return device-level interrupt handling status */14132	return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;14133}  /* lpfc_sli_intr_handler */14134 14135/**14136 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event14137 * @phba: pointer to lpfc hba data structure.14138 *14139 * This routine is invoked by the worker thread to process all the pending14140 * SLI4 els abort xri events.14141 **/14142void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)14143{14144	struct lpfc_cq_event *cq_event;14145	unsigned long iflags;14146 14147	/* First, declare the els xri abort event has been handled */14148	clear_bit(ELS_XRI_ABORT_EVENT, &phba->hba_flag);14149 14150	/* Now, handle all the els xri abort events */14151	spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);14152	while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {14153		/* Get the first event from the head of the event queue */14154		list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,14155				 cq_event, struct lpfc_cq_event, list);14156		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,14157				       iflags);14158		/* Notify aborted XRI for ELS work queue */14159		lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);14160 14161		/* Free the event processed back to the free pool */14162		lpfc_sli4_cq_event_release(phba, cq_event);14163		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,14164				  iflags);14165	}14166	spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);14167}14168 14169/**14170 * lpfc_sli4_els_preprocess_rspiocbq - Get response iocbq from els wcqe14171 * @phba: Pointer to HBA context object.14172 * @irspiocbq: Pointer to work-queue completion queue entry.14173 *14174 * This routine handles an ELS work-queue completion event and construct14175 * a pseudo response ELS IOCBQ from the SLI4 ELS WCQE for the common14176 * discovery engine to handle.14177 *14178 * Return: Pointer to the receive IOCBQ, NULL otherwise.14179 **/14180static struct lpfc_iocbq *14181lpfc_sli4_els_preprocess_rspiocbq(struct lpfc_hba *phba,14182				  struct lpfc_iocbq *irspiocbq)14183{14184	struct lpfc_sli_ring *pring;14185	struct lpfc_iocbq *cmdiocbq;14186	struct lpfc_wcqe_complete *wcqe;14187	unsigned long iflags;14188 14189	pring = lpfc_phba_elsring(phba);14190	if (unlikely(!pring))14191		return NULL;14192 14193	wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;14194	spin_lock_irqsave(&pring->ring_lock, iflags);14195	pring->stats.iocb_event++;14196	/* Look up the ELS command IOCB and create pseudo response IOCB */14197	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,14198				bf_get(lpfc_wcqe_c_request_tag, wcqe));14199	if (unlikely(!cmdiocbq)) {14200		spin_unlock_irqrestore(&pring->ring_lock, iflags);14201		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,14202				"0386 ELS complete with no corresponding "14203				"cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",14204				wcqe->word0, wcqe->total_data_placed,14205				wcqe->parameter, wcqe->word3);14206		lpfc_sli_release_iocbq(phba, irspiocbq);14207		return NULL;14208	}14209 14210	memcpy(&irspiocbq->wqe, &cmdiocbq->wqe, sizeof(union lpfc_wqe128));14211	memcpy(&irspiocbq->wcqe_cmpl, wcqe, sizeof(*wcqe));14212 14213	/* Put the iocb back on the txcmplq */14214	lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);14215	spin_unlock_irqrestore(&pring->ring_lock, iflags);14216 14217	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {14218		spin_lock_irqsave(&phba->hbalock, iflags);14219		irspiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;14220		spin_unlock_irqrestore(&phba->hbalock, iflags);14221	}14222 14223	return irspiocbq;14224}14225 14226inline struct lpfc_cq_event *14227lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)14228{14229	struct lpfc_cq_event *cq_event;14230 14231	/* Allocate a new internal CQ_EVENT entry */14232	cq_event = lpfc_sli4_cq_event_alloc(phba);14233	if (!cq_event) {14234		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,14235				"0602 Failed to alloc CQ_EVENT entry\n");14236		return NULL;14237	}14238 14239	/* Move the CQE into the event */14240	memcpy(&cq_event->cqe, entry, size);14241	return cq_event;14242}14243 14244/**14245 * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event14246 * @phba: Pointer to HBA context object.14247 * @mcqe: Pointer to mailbox completion queue entry.14248 *14249 * This routine process a mailbox completion queue entry with asynchronous14250 * event.14251 *14252 * Return: true if work posted to worker thread, otherwise false.14253 **/14254static bool14255lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)14256{14257	struct lpfc_cq_event *cq_event;14258	unsigned long iflags;14259 14260	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,14261			"0392 Async Event: word0:x%x, word1:x%x, "14262			"word2:x%x, word3:x%x\n", mcqe->word0,14263			mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);14264 14265	cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));14266	if (!cq_event)14267		return false;14268 14269	spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);14270	list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);14271	spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);14272 14273	/* Set the async event flag */14274	set_bit(ASYNC_EVENT, &phba->hba_flag);14275 14276	return true;14277}14278 14279/**14280 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event14281 * @phba: Pointer to HBA context object.14282 * @mcqe: Pointer to mailbox completion queue entry.14283 *14284 * This routine process a mailbox completion queue entry with mailbox14285 * completion event.14286 *14287 * Return: true if work posted to worker thread, otherwise false.14288 **/14289static bool14290lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)14291{14292	uint32_t mcqe_status;14293	MAILBOX_t *mbox, *pmbox;14294	struct lpfc_mqe *mqe;14295	struct lpfc_vport *vport;14296	struct lpfc_nodelist *ndlp;14297	struct lpfc_dmabuf *mp;14298	unsigned long iflags;14299	LPFC_MBOXQ_t *pmb;14300	bool workposted = false;14301	int rc;14302 14303	/* If not a mailbox complete MCQE, out by checking mailbox consume */14304	if (!bf_get(lpfc_trailer_completed, mcqe))14305		goto out_no_mqe_complete;14306 14307	/* Get the reference to the active mbox command */14308	spin_lock_irqsave(&phba->hbalock, iflags);14309	pmb = phba->sli.mbox_active;14310	if (unlikely(!pmb)) {14311		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,14312				"1832 No pending MBOX command to handle\n");14313		spin_unlock_irqrestore(&phba->hbalock, iflags);14314		goto out_no_mqe_complete;14315	}14316	spin_unlock_irqrestore(&phba->hbalock, iflags);14317	mqe = &pmb->u.mqe;14318	pmbox = (MAILBOX_t *)&pmb->u.mqe;14319	mbox = phba->mbox;14320	vport = pmb->vport;14321 14322	/* Reset heartbeat timer */14323	phba->last_completion_time = jiffies;14324	del_timer(&phba->sli.mbox_tmo);14325 14326	/* Move mbox data to caller's mailbox region, do endian swapping */14327	if (pmb->mbox_cmpl && mbox)14328		lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));14329 14330	/*14331	 * For mcqe errors, conditionally move a modified error code to14332	 * the mbox so that the error will not be missed.14333	 */14334	mcqe_status = bf_get(lpfc_mcqe_status, mcqe);14335	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {14336		if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)14337			bf_set(lpfc_mqe_status, mqe,14338			       (LPFC_MBX_ERROR_RANGE | mcqe_status));14339	}14340	if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {14341		pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;14342		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,14343				      "MBOX dflt rpi: status:x%x rpi:x%x",14344				      mcqe_status,14345				      pmbox->un.varWords[0], 0);14346		if (mcqe_status == MB_CQE_STATUS_SUCCESS) {14347			mp = pmb->ctx_buf;14348			ndlp = pmb->ctx_ndlp;14349 14350			/* Reg_LOGIN of dflt RPI was successful. Mark the14351			 * node as having an UNREG_LOGIN in progress to stop14352			 * an unsolicited PLOGI from the same NPortId from14353			 * starting another mailbox transaction.14354			 */14355			spin_lock_irqsave(&ndlp->lock, iflags);14356			ndlp->nlp_flag |= NLP_UNREG_INP;14357			spin_unlock_irqrestore(&ndlp->lock, iflags);14358			lpfc_unreg_login(phba, vport->vpi,14359					 pmbox->un.varWords[0], pmb);14360			pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;14361			pmb->ctx_buf = mp;14362 14363			/* No reference taken here.  This is a default14364			 * RPI reg/immediate unreg cycle. The reference was14365			 * taken in the reg rpi path and is released when14366			 * this mailbox completes.14367			 */14368			pmb->ctx_ndlp = ndlp;14369			pmb->vport = vport;14370			rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);14371			if (rc != MBX_BUSY)14372				lpfc_printf_log(phba, KERN_ERR,14373						LOG_TRACE_EVENT,14374						"0385 rc should "14375						"have been MBX_BUSY\n");14376			if (rc != MBX_NOT_FINISHED)14377				goto send_current_mbox;14378		}14379	}14380	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);14381	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;14382	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);14383 14384	/* Do NOT queue MBX_HEARTBEAT to the worker thread for processing. */14385	if (pmbox->mbxCommand == MBX_HEARTBEAT) {14386		spin_lock_irqsave(&phba->hbalock, iflags);14387		/* Release the mailbox command posting token */14388		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;14389		phba->sli.mbox_active = NULL;14390		if (bf_get(lpfc_trailer_consumed, mcqe))14391			lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);14392		spin_unlock_irqrestore(&phba->hbalock, iflags);14393 14394		/* Post the next mbox command, if there is one */14395		lpfc_sli4_post_async_mbox(phba);14396 14397		/* Process cmpl now */14398		if (pmb->mbox_cmpl)14399			pmb->mbox_cmpl(phba, pmb);14400		return false;14401	}14402 14403	/* There is mailbox completion work to queue to the worker thread */14404	spin_lock_irqsave(&phba->hbalock, iflags);14405	__lpfc_mbox_cmpl_put(phba, pmb);14406	phba->work_ha |= HA_MBATT;14407	spin_unlock_irqrestore(&phba->hbalock, iflags);14408	workposted = true;14409 14410send_current_mbox:14411	spin_lock_irqsave(&phba->hbalock, iflags);14412	/* Release the mailbox command posting token */14413	phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;14414	/* Setting active mailbox pointer need to be in sync to flag clear */14415	phba->sli.mbox_active = NULL;14416	if (bf_get(lpfc_trailer_consumed, mcqe))14417		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);14418	spin_unlock_irqrestore(&phba->hbalock, iflags);14419	/* Wake up worker thread to post the next pending mailbox command */14420	lpfc_worker_wake_up(phba);14421	return workposted;14422 14423out_no_mqe_complete:14424	spin_lock_irqsave(&phba->hbalock, iflags);14425	if (bf_get(lpfc_trailer_consumed, mcqe))14426		lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);14427	spin_unlock_irqrestore(&phba->hbalock, iflags);14428	return false;14429}14430 14431/**14432 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry14433 * @phba: Pointer to HBA context object.14434 * @cq: Pointer to associated CQ14435 * @cqe: Pointer to mailbox completion queue entry.14436 *14437 * This routine process a mailbox completion queue entry, it invokes the14438 * proper mailbox complete handling or asynchronous event handling routine14439 * according to the MCQE's async bit.14440 *14441 * Return: true if work posted to worker thread, otherwise false.14442 **/14443static bool14444lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,14445			 struct lpfc_cqe *cqe)14446{14447	struct lpfc_mcqe mcqe;14448	bool workposted;14449 14450	cq->CQ_mbox++;14451 14452	/* Copy the mailbox MCQE and convert endian order as needed */14453	lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));14454 14455	/* Invoke the proper event handling routine */14456	if (!bf_get(lpfc_trailer_async, &mcqe))14457		workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);14458	else14459		workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);14460	return workposted;14461}14462 14463/**14464 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event14465 * @phba: Pointer to HBA context object.14466 * @cq: Pointer to associated CQ14467 * @wcqe: Pointer to work-queue completion queue entry.14468 *14469 * This routine handles an ELS work-queue completion event.14470 *14471 * Return: true if work posted to worker thread, otherwise false.14472 **/14473static bool14474lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,14475			     struct lpfc_wcqe_complete *wcqe)14476{14477	struct lpfc_iocbq *irspiocbq;14478	unsigned long iflags;14479	struct lpfc_sli_ring *pring = cq->pring;14480	int txq_cnt = 0;14481	int txcmplq_cnt = 0;14482 14483	/* Check for response status */14484	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {14485		/* Log the error status */14486		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,14487				"0357 ELS CQE error: status=x%x: "14488				"CQE: %08x %08x %08x %08x\n",14489				bf_get(lpfc_wcqe_c_status, wcqe),14490				wcqe->word0, wcqe->total_data_placed,14491				wcqe->parameter, wcqe->word3);14492	}14493 14494	/* Get an irspiocbq for later ELS response processing use */14495	irspiocbq = lpfc_sli_get_iocbq(phba);14496	if (!irspiocbq) {14497		if (!list_empty(&pring->txq))14498			txq_cnt++;14499		if (!list_empty(&pring->txcmplq))14500			txcmplq_cnt++;14501		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,14502			"0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "14503			"els_txcmplq_cnt=%d\n",14504			txq_cnt, phba->iocb_cnt,14505			txcmplq_cnt);14506		return false;14507	}14508 14509	/* Save off the slow-path queue event for work thread to process */14510	memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));14511	spin_lock_irqsave(&phba->hbalock, iflags);14512	list_add_tail(&irspiocbq->cq_event.list,14513		      &phba->sli4_hba.sp_queue_event);14514	spin_unlock_irqrestore(&phba->hbalock, iflags);14515	set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);14516 14517	return true;14518}14519 14520/**14521 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event14522 * @phba: Pointer to HBA context object.14523 * @wcqe: Pointer to work-queue completion queue entry.14524 *14525 * This routine handles slow-path WQ entry consumed event by invoking the14526 * proper WQ release routine to the slow-path WQ.14527 **/14528static void14529lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,14530			     struct lpfc_wcqe_release *wcqe)14531{14532	/* sanity check on queue memory */14533	if (unlikely(!phba->sli4_hba.els_wq))14534		return;14535	/* Check for the slow-path ELS work queue */14536	if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)14537		lpfc_sli4_wq_release(phba->sli4_hba.els_wq,14538				     bf_get(lpfc_wcqe_r_wqe_index, wcqe));14539	else14540		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,14541				"2579 Slow-path wqe consume event carries "14542				"miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",14543				bf_get(lpfc_wcqe_r_wqe_index, wcqe),14544				phba->sli4_hba.els_wq->queue_id);14545}14546 14547/**14548 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event14549 * @phba: Pointer to HBA context object.14550 * @cq: Pointer to a WQ completion queue.14551 * @wcqe: Pointer to work-queue completion queue entry.14552 *14553 * This routine handles an XRI abort event.14554 *14555 * Return: true if work posted to worker thread, otherwise false.14556 **/14557static bool14558lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,14559				   struct lpfc_queue *cq,14560				   struct sli4_wcqe_xri_aborted *wcqe)14561{14562	bool workposted = false;14563	struct lpfc_cq_event *cq_event;14564	unsigned long iflags;14565 14566	switch (cq->subtype) {14567	case LPFC_IO:14568		lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);14569		if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {14570			/* Notify aborted XRI for NVME work queue */14571			if (phba->nvmet_support)14572				lpfc_sli4_nvmet_xri_aborted(phba, wcqe);14573		}14574		workposted = false;14575		break;14576	case LPFC_NVME_LS: /* NVME LS uses ELS resources */14577	case LPFC_ELS:14578		cq_event = lpfc_cq_event_setup(phba, wcqe, sizeof(*wcqe));14579		if (!cq_event) {14580			workposted = false;14581			break;14582		}14583		cq_event->hdwq = cq->hdwq;14584		spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,14585				  iflags);14586		list_add_tail(&cq_event->list,14587			      &phba->sli4_hba.sp_els_xri_aborted_work_queue);14588		/* Set the els xri abort event flag */14589		set_bit(ELS_XRI_ABORT_EVENT, &phba->hba_flag);14590		spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,14591				       iflags);14592		workposted = true;14593		break;14594	default:14595		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,14596				"0603 Invalid CQ subtype %d: "14597				"%08x %08x %08x %08x\n",14598				cq->subtype, wcqe->word0, wcqe->parameter,14599				wcqe->word2, wcqe->word3);14600		workposted = false;14601		break;14602	}14603	return workposted;14604}14605 14606#define FC_RCTL_MDS_DIAGS	0xF414607 14608/**14609 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry14610 * @phba: Pointer to HBA context object.14611 * @rcqe: Pointer to receive-queue completion queue entry.14612 *14613 * This routine process a receive-queue completion queue entry.14614 *14615 * Return: true if work posted to worker thread, otherwise false.14616 **/14617static bool14618lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)14619{14620	bool workposted = false;14621	struct fc_frame_header *fc_hdr;14622	struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;14623	struct lpfc_queue *drq = phba->sli4_hba.dat_rq;14624	struct lpfc_nvmet_tgtport *tgtp;14625	struct hbq_dmabuf *dma_buf;14626	uint32_t status, rq_id;14627	unsigned long iflags;14628 14629	/* sanity check on queue memory */14630	if (unlikely(!hrq) || unlikely(!drq))14631		return workposted;14632 14633	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)14634		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);14635	else14636		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);14637	if (rq_id != hrq->queue_id)14638		goto out;14639 14640	status = bf_get(lpfc_rcqe_status, rcqe);14641	switch (status) {14642	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:14643		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,14644				"2537 Receive Frame Truncated!!\n");14645		fallthrough;14646	case FC_STATUS_RQ_SUCCESS:14647		spin_lock_irqsave(&phba->hbalock, iflags);14648		lpfc_sli4_rq_release(hrq, drq);14649		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);14650		if (!dma_buf) {14651			hrq->RQ_no_buf_found++;14652			spin_unlock_irqrestore(&phba->hbalock, iflags);14653			goto out;14654		}14655		hrq->RQ_rcv_buf++;14656		hrq->RQ_buf_posted--;14657		memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));14658 14659		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;14660 14661		if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||14662		    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {14663			spin_unlock_irqrestore(&phba->hbalock, iflags);14664			/* Handle MDS Loopback frames */14665			if  (!test_bit(FC_UNLOADING, &phba->pport->load_flag))14666				lpfc_sli4_handle_mds_loopback(phba->pport,14667							      dma_buf);14668			else14669				lpfc_in_buf_free(phba, &dma_buf->dbuf);14670			break;14671		}14672 14673		/* save off the frame for the work thread to process */14674		list_add_tail(&dma_buf->cq_event.list,14675			      &phba->sli4_hba.sp_queue_event);14676		spin_unlock_irqrestore(&phba->hbalock, iflags);14677		/* Frame received */14678		set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);14679		workposted = true;14680		break;14681	case FC_STATUS_INSUFF_BUF_FRM_DISC:14682		if (phba->nvmet_support) {14683			tgtp = phba->targetport->private;14684			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,14685					"6402 RQE Error x%x, posted %d err_cnt "14686					"%d: %x %x %x\n",14687					status, hrq->RQ_buf_posted,14688					hrq->RQ_no_posted_buf,14689					atomic_read(&tgtp->rcv_fcp_cmd_in),14690					atomic_read(&tgtp->rcv_fcp_cmd_out),14691					atomic_read(&tgtp->xmt_fcp_release));14692		}14693		fallthrough;14694 14695	case FC_STATUS_INSUFF_BUF_NEED_BUF:14696		hrq->RQ_no_posted_buf++;14697		/* Post more buffers if possible */14698		set_bit(HBA_POST_RECEIVE_BUFFER, &phba->hba_flag);14699		workposted = true;14700		break;14701	case FC_STATUS_RQ_DMA_FAILURE:14702		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,14703				"2564 RQE DMA Error x%x, x%08x x%08x x%08x "14704				"x%08x\n",14705				status, rcqe->word0, rcqe->word1,14706				rcqe->word2, rcqe->word3);14707 14708		/* If IV set, no further recovery */14709		if (bf_get(lpfc_rcqe_iv, rcqe))14710			break;14711 14712		/* recycle consumed resource */14713		spin_lock_irqsave(&phba->hbalock, iflags);14714		lpfc_sli4_rq_release(hrq, drq);14715		dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);14716		if (!dma_buf) {14717			hrq->RQ_no_buf_found++;14718			spin_unlock_irqrestore(&phba->hbalock, iflags);14719			break;14720		}14721		hrq->RQ_rcv_buf++;14722		hrq->RQ_buf_posted--;14723		spin_unlock_irqrestore(&phba->hbalock, iflags);14724		lpfc_in_buf_free(phba, &dma_buf->dbuf);14725		break;14726	default:14727		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,14728				"2565 Unexpected RQE Status x%x, w0-3 x%08x "14729				"x%08x x%08x x%08x\n",14730				status, rcqe->word0, rcqe->word1,14731				rcqe->word2, rcqe->word3);14732		break;14733	}14734out:14735	return workposted;14736}14737 14738/**14739 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry14740 * @phba: Pointer to HBA context object.14741 * @cq: Pointer to the completion queue.14742 * @cqe: Pointer to a completion queue entry.14743 *14744 * This routine process a slow-path work-queue or receive queue completion queue14745 * entry.14746 *14747 * Return: true if work posted to worker thread, otherwise false.14748 **/14749static bool14750lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,14751			 struct lpfc_cqe *cqe)14752{14753	struct lpfc_cqe cqevt;14754	bool workposted = false;14755 14756	/* Copy the work queue CQE and convert endian order if needed */14757	lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));14758 14759	/* Check and process for different type of WCQE and dispatch */14760	switch (bf_get(lpfc_cqe_code, &cqevt)) {14761	case CQE_CODE_COMPL_WQE:14762		/* Process the WQ/RQ complete event */14763		phba->last_completion_time = jiffies;14764		workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,14765				(struct lpfc_wcqe_complete *)&cqevt);14766		break;14767	case CQE_CODE_RELEASE_WQE:14768		/* Process the WQ release event */14769		lpfc_sli4_sp_handle_rel_wcqe(phba,14770				(struct lpfc_wcqe_release *)&cqevt);14771		break;14772	case CQE_CODE_XRI_ABORTED:14773		/* Process the WQ XRI abort event */14774		phba->last_completion_time = jiffies;14775		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,14776				(struct sli4_wcqe_xri_aborted *)&cqevt);14777		break;14778	case CQE_CODE_RECEIVE:14779	case CQE_CODE_RECEIVE_V1:14780		/* Process the RQ event */14781		phba->last_completion_time = jiffies;14782		workposted = lpfc_sli4_sp_handle_rcqe(phba,14783				(struct lpfc_rcqe *)&cqevt);14784		break;14785	default:14786		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,14787				"0388 Not a valid WCQE code: x%x\n",14788				bf_get(lpfc_cqe_code, &cqevt));14789		break;14790	}14791	return workposted;14792}14793 14794/**14795 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry14796 * @phba: Pointer to HBA context object.14797 * @eqe: Pointer to fast-path event queue entry.14798 * @speq: Pointer to slow-path event queue.14799 *14800 * This routine process a event queue entry from the slow-path event queue.14801 * It will check the MajorCode and MinorCode to determine this is for a14802 * completion event on a completion queue, if not, an error shall be logged14803 * and just return. Otherwise, it will get to the corresponding completion14804 * queue and process all the entries on that completion queue, rearm the14805 * completion queue, and then return.14806 *14807 **/14808static void14809lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,14810	struct lpfc_queue *speq)14811{14812	struct lpfc_queue *cq = NULL, *childq;14813	uint16_t cqid;14814	int ret = 0;14815 14816	/* Get the reference to the corresponding CQ */14817	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);14818 14819	list_for_each_entry(childq, &speq->child_list, list) {14820		if (childq->queue_id == cqid) {14821			cq = childq;14822			break;14823		}14824	}14825	if (unlikely(!cq)) {14826		if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)14827			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,14828					"0365 Slow-path CQ identifier "14829					"(%d) does not exist\n", cqid);14830		return;14831	}14832 14833	/* Save EQ associated with this CQ */14834	cq->assoc_qp = speq;14835 14836	if (is_kdump_kernel())14837		ret = queue_work(phba->wq, &cq->spwork);14838	else14839		ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);14840 14841	if (!ret)14842		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,14843				"0390 Cannot schedule queue work "14844				"for CQ eqcqid=%d, cqid=%d on CPU %d\n",14845				cqid, cq->queue_id, raw_smp_processor_id());14846}14847 14848/**14849 * __lpfc_sli4_process_cq - Process elements of a CQ14850 * @phba: Pointer to HBA context object.14851 * @cq: Pointer to CQ to be processed14852 * @handler: Routine to process each cqe14853 * @delay: Pointer to usdelay to set in case of rescheduling of the handler14854 *14855 * This routine processes completion queue entries in a CQ. While a valid14856 * queue element is found, the handler is called. During processing checks14857 * are made for periodic doorbell writes to let the hardware know of14858 * element consumption.14859 *14860 * If the max limit on cqes to process is hit, or there are no more valid14861 * entries, the loop stops. If we processed a sufficient number of elements,14862 * meaning there is sufficient load, rather than rearming and generating14863 * another interrupt, a cq rescheduling delay will be set. A delay of 014864 * indicates no rescheduling.14865 *14866 * Returns True if work scheduled, False otherwise.14867 **/14868static bool14869__lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,14870	bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,14871			struct lpfc_cqe *), unsigned long *delay)14872{14873	struct lpfc_cqe *cqe;14874	bool workposted = false;14875	int count = 0, consumed = 0;14876	bool arm = true;14877 14878	/* default - no reschedule */14879	*delay = 0;14880 14881	if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)14882		goto rearm_and_exit;14883 14884	/* Process all the entries to the CQ */14885	cq->q_flag = 0;14886	cqe = lpfc_sli4_cq_get(cq);14887	while (cqe) {14888		workposted |= handler(phba, cq, cqe);14889		__lpfc_sli4_consume_cqe(phba, cq, cqe);14890 14891		consumed++;14892		if (!(++count % cq->max_proc_limit))14893			break;14894 14895		if (!(count % cq->notify_interval)) {14896			phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,14897						LPFC_QUEUE_NOARM);14898			consumed = 0;14899			cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;14900		}14901 14902		if (count == LPFC_NVMET_CQ_NOTIFY)14903			cq->q_flag |= HBA_NVMET_CQ_NOTIFY;14904 14905		cqe = lpfc_sli4_cq_get(cq);14906	}14907	if (count >= phba->cfg_cq_poll_threshold) {14908		*delay = 1;14909		arm = false;14910	}14911 14912	/* Track the max number of CQEs processed in 1 EQ */14913	if (count > cq->CQ_max_cqe)14914		cq->CQ_max_cqe = count;14915 14916	cq->assoc_qp->EQ_cqe_cnt += count;14917 14918	/* Catch the no cq entry condition */14919	if (unlikely(count == 0))14920		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,14921				"0369 No entry from completion queue "14922				"qid=%d\n", cq->queue_id);14923 14924	xchg(&cq->queue_claimed, 0);14925 14926rearm_and_exit:14927	phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,14928			arm ?  LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);14929 14930	return workposted;14931}14932 14933/**14934 * __lpfc_sli4_sp_process_cq - Process a slow-path event queue entry14935 * @cq: pointer to CQ to process14936 *14937 * This routine calls the cq processing routine with a handler specific14938 * to the type of queue bound to it.14939 *14940 * The CQ routine returns two values: the first is the calling status,14941 * which indicates whether work was queued to the  background discovery14942 * thread. If true, the routine should wakeup the discovery thread;14943 * the second is the delay parameter. If non-zero, rather than rearming14944 * the CQ and yet another interrupt, the CQ handler should be queued so14945 * that it is processed in a subsequent polling action. The value of14946 * the delay indicates when to reschedule it.14947 **/14948static void14949__lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)14950{14951	struct lpfc_hba *phba = cq->phba;14952	unsigned long delay;14953	bool workposted = false;14954	int ret = 0;14955 14956	/* Process and rearm the CQ */14957	switch (cq->type) {14958	case LPFC_MCQ:14959		workposted |= __lpfc_sli4_process_cq(phba, cq,14960						lpfc_sli4_sp_handle_mcqe,14961						&delay);14962		break;14963	case LPFC_WCQ:14964		if (cq->subtype == LPFC_IO)14965			workposted |= __lpfc_sli4_process_cq(phba, cq,14966						lpfc_sli4_fp_handle_cqe,14967						&delay);14968		else14969			workposted |= __lpfc_sli4_process_cq(phba, cq,14970						lpfc_sli4_sp_handle_cqe,14971						&delay);14972		break;14973	default:14974		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,14975				"0370 Invalid completion queue type (%d)\n",14976				cq->type);14977		return;14978	}14979 14980	if (delay) {14981		if (is_kdump_kernel())14982			ret = queue_delayed_work(phba->wq, &cq->sched_spwork,14983						delay);14984		else14985			ret = queue_delayed_work_on(cq->chann, phba->wq,14986						&cq->sched_spwork, delay);14987		if (!ret)14988			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,14989				"0394 Cannot schedule queue work "14990				"for cqid=%d on CPU %d\n",14991				cq->queue_id, cq->chann);14992	}14993 14994	/* wake up worker thread if there are works to be done */14995	if (workposted)14996		lpfc_worker_wake_up(phba);14997}14998 14999/**15000 * lpfc_sli4_sp_process_cq - slow-path work handler when started by15001 *   interrupt15002 * @work: pointer to work element15003 *15004 * translates from the work handler and calls the slow-path handler.15005 **/15006static void15007lpfc_sli4_sp_process_cq(struct work_struct *work)15008{15009	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);15010 15011	__lpfc_sli4_sp_process_cq(cq);15012}15013 15014/**15015 * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer15016 * @work: pointer to work element15017 *15018 * translates from the work handler and calls the slow-path handler.15019 **/15020static void15021lpfc_sli4_dly_sp_process_cq(struct work_struct *work)15022{15023	struct lpfc_queue *cq = container_of(to_delayed_work(work),15024					struct lpfc_queue, sched_spwork);15025 15026	__lpfc_sli4_sp_process_cq(cq);15027}15028 15029/**15030 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry15031 * @phba: Pointer to HBA context object.15032 * @cq: Pointer to associated CQ15033 * @wcqe: Pointer to work-queue completion queue entry.15034 *15035 * This routine process a fast-path work queue completion entry from fast-path15036 * event queue for FCP command response completion.15037 **/15038static void15039lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,15040			     struct lpfc_wcqe_complete *wcqe)15041{15042	struct lpfc_sli_ring *pring = cq->pring;15043	struct lpfc_iocbq *cmdiocbq;15044	unsigned long iflags;15045 15046	/* Check for response status */15047	if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {15048		/* If resource errors reported from HBA, reduce queue15049		 * depth of the SCSI device.15050		 */15051		if (((bf_get(lpfc_wcqe_c_status, wcqe) ==15052		     IOSTAT_LOCAL_REJECT)) &&15053		    ((wcqe->parameter & IOERR_PARAM_MASK) ==15054		     IOERR_NO_RESOURCES))15055			phba->lpfc_rampdown_queue_depth(phba);15056 15057		/* Log the cmpl status */15058		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,15059				"0373 FCP CQE cmpl: status=x%x: "15060				"CQE: %08x %08x %08x %08x\n",15061				bf_get(lpfc_wcqe_c_status, wcqe),15062				wcqe->word0, wcqe->total_data_placed,15063				wcqe->parameter, wcqe->word3);15064	}15065 15066	/* Look up the FCP command IOCB and create pseudo response IOCB */15067	spin_lock_irqsave(&pring->ring_lock, iflags);15068	pring->stats.iocb_event++;15069	cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,15070				bf_get(lpfc_wcqe_c_request_tag, wcqe));15071	spin_unlock_irqrestore(&pring->ring_lock, iflags);15072	if (unlikely(!cmdiocbq)) {15073		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,15074				"0374 FCP complete with no corresponding "15075				"cmdiocb: iotag (%d)\n",15076				bf_get(lpfc_wcqe_c_request_tag, wcqe));15077		return;15078	}15079#ifdef CONFIG_SCSI_LPFC_DEBUG_FS15080	cmdiocbq->isr_timestamp = cq->isr_timestamp;15081#endif15082	if (bf_get(lpfc_wcqe_c_xb, wcqe)) {15083		spin_lock_irqsave(&phba->hbalock, iflags);15084		cmdiocbq->cmd_flag |= LPFC_EXCHANGE_BUSY;15085		spin_unlock_irqrestore(&phba->hbalock, iflags);15086	}15087 15088	if (cmdiocbq->cmd_cmpl) {15089		/* For FCP the flag is cleared in cmd_cmpl */15090		if (!(cmdiocbq->cmd_flag & LPFC_IO_FCP) &&15091		    cmdiocbq->cmd_flag & LPFC_DRIVER_ABORTED) {15092			spin_lock_irqsave(&phba->hbalock, iflags);15093			cmdiocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;15094			spin_unlock_irqrestore(&phba->hbalock, iflags);15095		}15096 15097		/* Pass the cmd_iocb and the wcqe to the upper layer */15098		memcpy(&cmdiocbq->wcqe_cmpl, wcqe,15099		       sizeof(struct lpfc_wcqe_complete));15100		cmdiocbq->cmd_cmpl(phba, cmdiocbq, cmdiocbq);15101	} else {15102		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,15103				"0375 FCP cmdiocb not callback function "15104				"iotag: (%d)\n",15105				bf_get(lpfc_wcqe_c_request_tag, wcqe));15106	}15107}15108 15109/**15110 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event15111 * @phba: Pointer to HBA context object.15112 * @cq: Pointer to completion queue.15113 * @wcqe: Pointer to work-queue completion queue entry.15114 *15115 * This routine handles an fast-path WQ entry consumed event by invoking the15116 * proper WQ release routine to the slow-path WQ.15117 **/15118static void15119lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,15120			     struct lpfc_wcqe_release *wcqe)15121{15122	struct lpfc_queue *childwq;15123	bool wqid_matched = false;15124	uint16_t hba_wqid;15125 15126	/* Check for fast-path FCP work queue release */15127	hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);15128	list_for_each_entry(childwq, &cq->child_list, list) {15129		if (childwq->queue_id == hba_wqid) {15130			lpfc_sli4_wq_release(childwq,15131					bf_get(lpfc_wcqe_r_wqe_index, wcqe));15132			if (childwq->q_flag & HBA_NVMET_WQFULL)15133				lpfc_nvmet_wqfull_process(phba, childwq);15134			wqid_matched = true;15135			break;15136		}15137	}15138	/* Report warning log message if no match found */15139	if (wqid_matched != true)15140		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,15141				"2580 Fast-path wqe consume event carries "15142				"miss-matched qid: wcqe-qid=x%x\n", hba_wqid);15143}15144 15145/**15146 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry15147 * @phba: Pointer to HBA context object.15148 * @cq: Pointer to completion queue.15149 * @rcqe: Pointer to receive-queue completion queue entry.15150 *15151 * This routine process a receive-queue completion queue entry.15152 *15153 * Return: true if work posted to worker thread, otherwise false.15154 **/15155static bool15156lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,15157			    struct lpfc_rcqe *rcqe)15158{15159	bool workposted = false;15160	struct lpfc_queue *hrq;15161	struct lpfc_queue *drq;15162	struct rqb_dmabuf *dma_buf;15163	struct fc_frame_header *fc_hdr;15164	struct lpfc_nvmet_tgtport *tgtp;15165	uint32_t status, rq_id;15166	unsigned long iflags;15167	uint32_t fctl, idx;15168 15169	if ((phba->nvmet_support == 0) ||15170	    (phba->sli4_hba.nvmet_cqset == NULL))15171		return workposted;15172 15173	idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;15174	hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];15175	drq = phba->sli4_hba.nvmet_mrq_data[idx];15176 15177	/* sanity check on queue memory */15178	if (unlikely(!hrq) || unlikely(!drq))15179		return workposted;15180 15181	if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)15182		rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);15183	else15184		rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);15185 15186	if ((phba->nvmet_support == 0) ||15187	    (rq_id != hrq->queue_id))15188		return workposted;15189 15190	status = bf_get(lpfc_rcqe_status, rcqe);15191	switch (status) {15192	case FC_STATUS_RQ_BUF_LEN_EXCEEDED:15193		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,15194				"6126 Receive Frame Truncated!!\n");15195		fallthrough;15196	case FC_STATUS_RQ_SUCCESS:15197		spin_lock_irqsave(&phba->hbalock, iflags);15198		lpfc_sli4_rq_release(hrq, drq);15199		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);15200		if (!dma_buf) {15201			hrq->RQ_no_buf_found++;15202			spin_unlock_irqrestore(&phba->hbalock, iflags);15203			goto out;15204		}15205		spin_unlock_irqrestore(&phba->hbalock, iflags);15206		hrq->RQ_rcv_buf++;15207		hrq->RQ_buf_posted--;15208		fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;15209 15210		/* Just some basic sanity checks on FCP Command frame */15211		fctl = (fc_hdr->fh_f_ctl[0] << 16 |15212			fc_hdr->fh_f_ctl[1] << 8 |15213			fc_hdr->fh_f_ctl[2]);15214		if (((fctl &15215		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=15216		    (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||15217		    (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */15218			goto drop;15219 15220		if (fc_hdr->fh_type == FC_TYPE_FCP) {15221			dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);15222			lpfc_nvmet_unsol_fcp_event(15223				phba, idx, dma_buf, cq->isr_timestamp,15224				cq->q_flag & HBA_NVMET_CQ_NOTIFY);15225			return false;15226		}15227drop:15228		lpfc_rq_buf_free(phba, &dma_buf->hbuf);15229		break;15230	case FC_STATUS_INSUFF_BUF_FRM_DISC:15231		if (phba->nvmet_support) {15232			tgtp = phba->targetport->private;15233			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,15234					"6401 RQE Error x%x, posted %d err_cnt "15235					"%d: %x %x %x\n",15236					status, hrq->RQ_buf_posted,15237					hrq->RQ_no_posted_buf,15238					atomic_read(&tgtp->rcv_fcp_cmd_in),15239					atomic_read(&tgtp->rcv_fcp_cmd_out),15240					atomic_read(&tgtp->xmt_fcp_release));15241		}15242		fallthrough;15243 15244	case FC_STATUS_INSUFF_BUF_NEED_BUF:15245		hrq->RQ_no_posted_buf++;15246		/* Post more buffers if possible */15247		break;15248	case FC_STATUS_RQ_DMA_FAILURE:15249		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,15250				"2575 RQE DMA Error x%x, x%08x x%08x x%08x "15251				"x%08x\n",15252				status, rcqe->word0, rcqe->word1,15253				rcqe->word2, rcqe->word3);15254 15255		/* If IV set, no further recovery */15256		if (bf_get(lpfc_rcqe_iv, rcqe))15257			break;15258 15259		/* recycle consumed resource */15260		spin_lock_irqsave(&phba->hbalock, iflags);15261		lpfc_sli4_rq_release(hrq, drq);15262		dma_buf = lpfc_sli_rqbuf_get(phba, hrq);15263		if (!dma_buf) {15264			hrq->RQ_no_buf_found++;15265			spin_unlock_irqrestore(&phba->hbalock, iflags);15266			break;15267		}15268		hrq->RQ_rcv_buf++;15269		hrq->RQ_buf_posted--;15270		spin_unlock_irqrestore(&phba->hbalock, iflags);15271		lpfc_rq_buf_free(phba, &dma_buf->hbuf);15272		break;15273	default:15274		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,15275				"2576 Unexpected RQE Status x%x, w0-3 x%08x "15276				"x%08x x%08x x%08x\n",15277				status, rcqe->word0, rcqe->word1,15278				rcqe->word2, rcqe->word3);15279		break;15280	}15281out:15282	return workposted;15283}15284 15285/**15286 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry15287 * @phba: adapter with cq15288 * @cq: Pointer to the completion queue.15289 * @cqe: Pointer to fast-path completion queue entry.15290 *15291 * This routine process a fast-path work queue completion entry from fast-path15292 * event queue for FCP command response completion.15293 *15294 * Return: true if work posted to worker thread, otherwise false.15295 **/15296static bool15297lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,15298			 struct lpfc_cqe *cqe)15299{15300	struct lpfc_wcqe_release wcqe;15301	bool workposted = false;15302 15303	/* Copy the work queue CQE and convert endian order if needed */15304	lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));15305 15306	/* Check and process for different type of WCQE and dispatch */15307	switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {15308	case CQE_CODE_COMPL_WQE:15309	case CQE_CODE_NVME_ERSP:15310		cq->CQ_wq++;15311		/* Process the WQ complete event */15312		phba->last_completion_time = jiffies;15313		if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)15314			lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,15315				(struct lpfc_wcqe_complete *)&wcqe);15316		break;15317	case CQE_CODE_RELEASE_WQE:15318		cq->CQ_release_wqe++;15319		/* Process the WQ release event */15320		lpfc_sli4_fp_handle_rel_wcqe(phba, cq,15321				(struct lpfc_wcqe_release *)&wcqe);15322		break;15323	case CQE_CODE_XRI_ABORTED:15324		cq->CQ_xri_aborted++;15325		/* Process the WQ XRI abort event */15326		phba->last_completion_time = jiffies;15327		workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,15328				(struct sli4_wcqe_xri_aborted *)&wcqe);15329		break;15330	case CQE_CODE_RECEIVE_V1:15331	case CQE_CODE_RECEIVE:15332		phba->last_completion_time = jiffies;15333		if (cq->subtype == LPFC_NVMET) {15334			workposted = lpfc_sli4_nvmet_handle_rcqe(15335				phba, cq, (struct lpfc_rcqe *)&wcqe);15336		}15337		break;15338	default:15339		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,15340				"0144 Not a valid CQE code: x%x\n",15341				bf_get(lpfc_wcqe_c_code, &wcqe));15342		break;15343	}15344	return workposted;15345}15346 15347/**15348 * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry15349 * @cq: Pointer to CQ to be processed15350 *15351 * This routine calls the cq processing routine with the handler for15352 * fast path CQEs.15353 *15354 * The CQ routine returns two values: the first is the calling status,15355 * which indicates whether work was queued to the  background discovery15356 * thread. If true, the routine should wakeup the discovery thread;15357 * the second is the delay parameter. If non-zero, rather than rearming15358 * the CQ and yet another interrupt, the CQ handler should be queued so15359 * that it is processed in a subsequent polling action. The value of15360 * the delay indicates when to reschedule it.15361 **/15362static void15363__lpfc_sli4_hba_process_cq(struct lpfc_queue *cq)15364{15365	struct lpfc_hba *phba = cq->phba;15366	unsigned long delay;15367	bool workposted = false;15368	int ret;15369 15370	/* process and rearm the CQ */15371	workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,15372					     &delay);15373 15374	if (delay) {15375		if (is_kdump_kernel())15376			ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,15377						delay);15378		else15379			ret = queue_delayed_work_on(cq->chann, phba->wq,15380						&cq->sched_irqwork, delay);15381		if (!ret)15382			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,15383					"0367 Cannot schedule queue work "15384					"for cqid=%d on CPU %d\n",15385					cq->queue_id, cq->chann);15386	}15387 15388	/* wake up worker thread if there are works to be done */15389	if (workposted)15390		lpfc_worker_wake_up(phba);15391}15392 15393/**15394 * lpfc_sli4_hba_process_cq - fast-path work handler when started by15395 *   interrupt15396 * @work: pointer to work element15397 *15398 * translates from the work handler and calls the fast-path handler.15399 **/15400static void15401lpfc_sli4_hba_process_cq(struct work_struct *work)15402{15403	struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);15404 15405	__lpfc_sli4_hba_process_cq(cq);15406}15407 15408/**15409 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry15410 * @phba: Pointer to HBA context object.15411 * @eq: Pointer to the queue structure.15412 * @eqe: Pointer to fast-path event queue entry.15413 * @poll_mode: poll_mode to execute processing the cq.15414 *15415 * This routine process a event queue entry from the fast-path event queue.15416 * It will check the MajorCode and MinorCode to determine this is for a15417 * completion event on a completion queue, if not, an error shall be logged15418 * and just return. Otherwise, it will get to the corresponding completion15419 * queue and process all the entries on the completion queue, rearm the15420 * completion queue, and then return.15421 **/15422static void15423lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,15424			 struct lpfc_eqe *eqe, enum lpfc_poll_mode poll_mode)15425{15426	struct lpfc_queue *cq = NULL;15427	uint32_t qidx = eq->hdwq;15428	uint16_t cqid, id;15429	int ret;15430 15431	if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {15432		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,15433				"0366 Not a valid completion "15434				"event: majorcode=x%x, minorcode=x%x\n",15435				bf_get_le32(lpfc_eqe_major_code, eqe),15436				bf_get_le32(lpfc_eqe_minor_code, eqe));15437		return;15438	}15439 15440	/* Get the reference to the corresponding CQ */15441	cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);15442 15443	/* Use the fast lookup method first */15444	if (cqid <= phba->sli4_hba.cq_max) {15445		cq = phba->sli4_hba.cq_lookup[cqid];15446		if (cq)15447			goto  work_cq;15448	}15449 15450	/* Next check for NVMET completion */15451	if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {15452		id = phba->sli4_hba.nvmet_cqset[0]->queue_id;15453		if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {15454			/* Process NVMET unsol rcv */15455			cq = phba->sli4_hba.nvmet_cqset[cqid - id];15456			goto  process_cq;15457		}15458	}15459 15460	if (phba->sli4_hba.nvmels_cq &&15461	    (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {15462		/* Process NVME unsol rcv */15463		cq = phba->sli4_hba.nvmels_cq;15464	}15465 15466	/* Otherwise this is a Slow path event */15467	if (cq == NULL) {15468		lpfc_sli4_sp_handle_eqe(phba, eqe,15469					phba->sli4_hba.hdwq[qidx].hba_eq);15470		return;15471	}15472 15473process_cq:15474	if (unlikely(cqid != cq->queue_id)) {15475		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,15476				"0368 Miss-matched fast-path completion "15477				"queue identifier: eqcqid=%d, fcpcqid=%d\n",15478				cqid, cq->queue_id);15479		return;15480	}15481 15482work_cq:15483#if defined(CONFIG_SCSI_LPFC_DEBUG_FS)15484	if (phba->ktime_on)15485		cq->isr_timestamp = ktime_get_ns();15486	else15487		cq->isr_timestamp = 0;15488#endif15489 15490	switch (poll_mode) {15491	case LPFC_THREADED_IRQ:15492		__lpfc_sli4_hba_process_cq(cq);15493		break;15494	case LPFC_QUEUE_WORK:15495	default:15496		if (is_kdump_kernel())15497			ret = queue_work(phba->wq, &cq->irqwork);15498		else15499			ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);15500		if (!ret)15501			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,15502					"0383 Cannot schedule queue work "15503					"for CQ eqcqid=%d, cqid=%d on CPU %d\n",15504					cqid, cq->queue_id,15505					raw_smp_processor_id());15506		break;15507	}15508}15509 15510/**15511 * lpfc_sli4_dly_hba_process_cq - fast-path work handler when started by timer15512 * @work: pointer to work element15513 *15514 * translates from the work handler and calls the fast-path handler.15515 **/15516static void15517lpfc_sli4_dly_hba_process_cq(struct work_struct *work)15518{15519	struct lpfc_queue *cq = container_of(to_delayed_work(work),15520					struct lpfc_queue, sched_irqwork);15521 15522	__lpfc_sli4_hba_process_cq(cq);15523}15524 15525/**15526 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device15527 * @irq: Interrupt number.15528 * @dev_id: The device context pointer.15529 *15530 * This function is directly called from the PCI layer as an interrupt15531 * service routine when device with SLI-4 interface spec is enabled with15532 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB15533 * ring event in the HBA. However, when the device is enabled with either15534 * MSI or Pin-IRQ interrupt mode, this function is called as part of the15535 * device-level interrupt handler. When the PCI slot is in error recovery15536 * or the HBA is undergoing initialization, the interrupt handler will not15537 * process the interrupt. The SCSI FCP fast-path ring event are handled in15538 * the intrrupt context. This function is called without any lock held.15539 * It gets the hbalock to access and update SLI data structures. Note that,15540 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is15541 * equal to that of FCP CQ index.15542 *15543 * The link attention and ELS ring attention events are handled15544 * by the worker thread. The interrupt handler signals the worker thread15545 * and returns for these events. This function is called without any lock15546 * held. It gets the hbalock to access and update SLI data structures.15547 *15548 * This function returns IRQ_HANDLED when interrupt is handled, IRQ_WAKE_THREAD15549 * when interrupt is scheduled to be handled from a threaded irq context, or15550 * else returns IRQ_NONE.15551 **/15552irqreturn_t15553lpfc_sli4_hba_intr_handler(int irq, void *dev_id)15554{15555	struct lpfc_hba *phba;15556	struct lpfc_hba_eq_hdl *hba_eq_hdl;15557	struct lpfc_queue *fpeq;15558	unsigned long iflag;15559	int hba_eqidx;15560	int ecount = 0;15561	struct lpfc_eq_intr_info *eqi;15562 15563	/* Get the driver's phba structure from the dev_id */15564	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;15565	phba = hba_eq_hdl->phba;15566	hba_eqidx = hba_eq_hdl->idx;15567 15568	if (unlikely(!phba))15569		return IRQ_NONE;15570	if (unlikely(!phba->sli4_hba.hdwq))15571		return IRQ_NONE;15572 15573	/* Get to the EQ struct associated with this vector */15574	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;15575	if (unlikely(!fpeq))15576		return IRQ_NONE;15577 15578	/* Check device state for handling interrupt */15579	if (unlikely(lpfc_intr_state_check(phba))) {15580		/* Check again for link_state with lock held */15581		spin_lock_irqsave(&phba->hbalock, iflag);15582		if (phba->link_state < LPFC_LINK_DOWN)15583			/* Flush, clear interrupt, and rearm the EQ */15584			lpfc_sli4_eqcq_flush(phba, fpeq);15585		spin_unlock_irqrestore(&phba->hbalock, iflag);15586		return IRQ_NONE;15587	}15588 15589	switch (fpeq->poll_mode) {15590	case LPFC_THREADED_IRQ:15591		/* CGN mgmt is mutually exclusive from irq processing */15592		if (phba->cmf_active_mode == LPFC_CFG_OFF)15593			return IRQ_WAKE_THREAD;15594		fallthrough;15595	case LPFC_QUEUE_WORK:15596	default:15597		eqi = this_cpu_ptr(phba->sli4_hba.eq_info);15598		eqi->icnt++;15599 15600		fpeq->last_cpu = raw_smp_processor_id();15601 15602		if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&15603		    fpeq->q_flag & HBA_EQ_DELAY_CHK &&15604		    phba->cfg_auto_imax &&15605		    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&15606		    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)15607			lpfc_sli4_mod_hba_eq_delay(phba, fpeq,15608						   LPFC_MAX_AUTO_EQ_DELAY);15609 15610		/* process and rearm the EQ */15611		ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,15612					      LPFC_QUEUE_WORK);15613 15614		if (unlikely(ecount == 0)) {15615			fpeq->EQ_no_entry++;15616			if (phba->intr_type == MSIX)15617				/* MSI-X treated interrupt served as no EQ share INT */15618				lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,15619						"0358 MSI-X interrupt with no EQE\n");15620			else15621				/* Non MSI-X treated on interrupt as EQ share INT */15622				return IRQ_NONE;15623		}15624	}15625 15626	return IRQ_HANDLED;15627} /* lpfc_sli4_hba_intr_handler */15628 15629/**15630 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device15631 * @irq: Interrupt number.15632 * @dev_id: The device context pointer.15633 *15634 * This function is the device-level interrupt handler to device with SLI-415635 * interface spec, called from the PCI layer when either MSI or Pin-IRQ15636 * interrupt mode is enabled and there is an event in the HBA which requires15637 * driver attention. This function invokes the slow-path interrupt attention15638 * handling function and fast-path interrupt attention handling function in15639 * turn to process the relevant HBA attention events. This function is called15640 * without any lock held. It gets the hbalock to access and update SLI data15641 * structures.15642 *15643 * This function returns IRQ_HANDLED when interrupt is handled, else it15644 * returns IRQ_NONE.15645 **/15646irqreturn_t15647lpfc_sli4_intr_handler(int irq, void *dev_id)15648{15649	struct lpfc_hba  *phba;15650	irqreturn_t hba_irq_rc;15651	bool hba_handled = false;15652	int qidx;15653 15654	/* Get the driver's phba structure from the dev_id */15655	phba = (struct lpfc_hba *)dev_id;15656 15657	if (unlikely(!phba))15658		return IRQ_NONE;15659 15660	/*15661	 * Invoke fast-path host attention interrupt handling as appropriate.15662	 */15663	for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {15664		hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,15665					&phba->sli4_hba.hba_eq_hdl[qidx]);15666		if (hba_irq_rc == IRQ_HANDLED)15667			hba_handled |= true;15668	}15669 15670	return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;15671} /* lpfc_sli4_intr_handler */15672 15673void lpfc_sli4_poll_hbtimer(struct timer_list *t)15674{15675	struct lpfc_hba *phba = from_timer(phba, t, cpuhp_poll_timer);15676	struct lpfc_queue *eq;15677 15678	rcu_read_lock();15679 15680	list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)15681		lpfc_sli4_poll_eq(eq);15682	if (!list_empty(&phba->poll_list))15683		mod_timer(&phba->cpuhp_poll_timer,15684			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));15685 15686	rcu_read_unlock();15687}15688 15689static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)15690{15691	struct lpfc_hba *phba = eq->phba;15692 15693	/* kickstart slowpath processing if needed */15694	if (list_empty(&phba->poll_list))15695		mod_timer(&phba->cpuhp_poll_timer,15696			  jiffies + msecs_to_jiffies(LPFC_POLL_HB));15697 15698	list_add_rcu(&eq->_poll_list, &phba->poll_list);15699	synchronize_rcu();15700}15701 15702static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)15703{15704	struct lpfc_hba *phba = eq->phba;15705 15706	/* Disable slowpath processing for this eq.  Kick start the eq15707	 * by RE-ARMING the eq's ASAP15708	 */15709	list_del_rcu(&eq->_poll_list);15710	synchronize_rcu();15711 15712	if (list_empty(&phba->poll_list))15713		del_timer_sync(&phba->cpuhp_poll_timer);15714}15715 15716void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)15717{15718	struct lpfc_queue *eq, *next;15719 15720	list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)15721		list_del(&eq->_poll_list);15722 15723	INIT_LIST_HEAD(&phba->poll_list);15724	synchronize_rcu();15725}15726 15727static inline void15728__lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)15729{15730	if (mode == eq->mode)15731		return;15732	/*15733	 * currently this function is only called during a hotplug15734	 * event and the cpu on which this function is executing15735	 * is going offline.  By now the hotplug has instructed15736	 * the scheduler to remove this cpu from cpu active mask.15737	 * So we don't need to work about being put aside by the15738	 * scheduler for a high priority process.  Yes, the inte-15739	 * rrupts could come but they are known to retire ASAP.15740	 */15741 15742	/* Disable polling in the fastpath */15743	WRITE_ONCE(eq->mode, mode);15744	/* flush out the store buffer */15745	smp_wmb();15746 15747	/*15748	 * Add this eq to the polling list and start polling. For15749	 * a grace period both interrupt handler and poller will15750	 * try to process the eq _but_ that's fine.  We have a15751	 * synchronization mechanism in place (queue_claimed) to15752	 * deal with it.  This is just a draining phase for int-15753	 * errupt handler (not eq's) as we have guranteed through15754	 * barrier that all the CPUs have seen the new CQ_POLLED15755	 * state. which will effectively disable the REARMING of15756	 * the EQ.  The whole idea is eq's die off eventually as15757	 * we are not rearming EQ's anymore.15758	 */15759	mode ? lpfc_sli4_add_to_poll_list(eq) :15760	       lpfc_sli4_remove_from_poll_list(eq);15761}15762 15763void lpfc_sli4_start_polling(struct lpfc_queue *eq)15764{15765	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);15766}15767 15768void lpfc_sli4_stop_polling(struct lpfc_queue *eq)15769{15770	struct lpfc_hba *phba = eq->phba;15771 15772	__lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);15773 15774	/* Kick start for the pending io's in h/w.15775	 * Once we switch back to interrupt processing on a eq15776	 * the io path completion will only arm eq's when it15777	 * receives a completion.  But since eq's are in disa-15778	 * rmed state it doesn't receive a completion.  This15779	 * creates a deadlock scenaro.15780	 */15781	phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);15782}15783 15784/**15785 * lpfc_sli4_queue_free - free a queue structure and associated memory15786 * @queue: The queue structure to free.15787 *15788 * This function frees a queue structure and the DMAable memory used for15789 * the host resident queue. This function must be called after destroying the15790 * queue on the HBA.15791 **/15792void15793lpfc_sli4_queue_free(struct lpfc_queue *queue)15794{15795	struct lpfc_dmabuf *dmabuf;15796 15797	if (!queue)15798		return;15799 15800	if (!list_empty(&queue->wq_list))15801		list_del(&queue->wq_list);15802 15803	while (!list_empty(&queue->page_list)) {15804		list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,15805				 list);15806		dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,15807				  dmabuf->virt, dmabuf->phys);15808		kfree(dmabuf);15809	}15810	if (queue->rqbp) {15811		lpfc_free_rq_buffer(queue->phba, queue);15812		kfree(queue->rqbp);15813	}15814 15815	if (!list_empty(&queue->cpu_list))15816		list_del(&queue->cpu_list);15817 15818	kfree(queue);15819	return;15820}15821 15822/**15823 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure15824 * @phba: The HBA that this queue is being created on.15825 * @page_size: The size of a queue page15826 * @entry_size: The size of each queue entry for this queue.15827 * @entry_count: The number of entries that this queue will handle.15828 * @cpu: The cpu that will primarily utilize this queue.15829 *15830 * This function allocates a queue structure and the DMAable memory used for15831 * the host resident queue. This function must be called before creating the15832 * queue on the HBA.15833 **/15834struct lpfc_queue *15835lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,15836		      uint32_t entry_size, uint32_t entry_count, int cpu)15837{15838	struct lpfc_queue *queue;15839	struct lpfc_dmabuf *dmabuf;15840	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;15841	uint16_t x, pgcnt;15842 15843	if (!phba->sli4_hba.pc_sli4_params.supported)15844		hw_page_size = page_size;15845 15846	pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;15847 15848	/* If needed, Adjust page count to match the max the adapter supports */15849	if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)15850		pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;15851 15852	queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),15853			     GFP_KERNEL, cpu_to_node(cpu));15854	if (!queue)15855		return NULL;15856 15857	INIT_LIST_HEAD(&queue->list);15858	INIT_LIST_HEAD(&queue->_poll_list);15859	INIT_LIST_HEAD(&queue->wq_list);15860	INIT_LIST_HEAD(&queue->wqfull_list);15861	INIT_LIST_HEAD(&queue->page_list);15862	INIT_LIST_HEAD(&queue->child_list);15863	INIT_LIST_HEAD(&queue->cpu_list);15864 15865	/* Set queue parameters now.  If the system cannot provide memory15866	 * resources, the free routine needs to know what was allocated.15867	 */15868	queue->page_count = pgcnt;15869	queue->q_pgs = (void **)&queue[1];15870	queue->entry_cnt_per_pg = hw_page_size / entry_size;15871	queue->entry_size = entry_size;15872	queue->entry_count = entry_count;15873	queue->page_size = hw_page_size;15874	queue->phba = phba;15875 15876	for (x = 0; x < queue->page_count; x++) {15877		dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,15878				      dev_to_node(&phba->pcidev->dev));15879		if (!dmabuf)15880			goto out_fail;15881		dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,15882						  hw_page_size, &dmabuf->phys,15883						  GFP_KERNEL);15884		if (!dmabuf->virt) {15885			kfree(dmabuf);15886			goto out_fail;15887		}15888		dmabuf->buffer_tag = x;15889		list_add_tail(&dmabuf->list, &queue->page_list);15890		/* use lpfc_sli4_qe to index a paritcular entry in this page */15891		queue->q_pgs[x] = dmabuf->virt;15892	}15893	INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);15894	INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);15895	INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);15896	INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);15897 15898	/* notify_interval will be set during q creation */15899 15900	return queue;15901out_fail:15902	lpfc_sli4_queue_free(queue);15903	return NULL;15904}15905 15906/**15907 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory15908 * @phba: HBA structure that indicates port to create a queue on.15909 * @pci_barset: PCI BAR set flag.15910 *15911 * This function shall perform iomap of the specified PCI BAR address to host15912 * memory address if not already done so and return it. The returned host15913 * memory address can be NULL.15914 */15915static void __iomem *15916lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)15917{15918	if (!phba->pcidev)15919		return NULL;15920 15921	switch (pci_barset) {15922	case WQ_PCI_BAR_0_AND_1:15923		return phba->pci_bar0_memmap_p;15924	case WQ_PCI_BAR_2_AND_3:15925		return phba->pci_bar2_memmap_p;15926	case WQ_PCI_BAR_4_AND_5:15927		return phba->pci_bar4_memmap_p;15928	default:15929		break;15930	}15931	return NULL;15932}15933 15934/**15935 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs15936 * @phba: HBA structure that EQs are on.15937 * @startq: The starting EQ index to modify15938 * @numq: The number of EQs (consecutive indexes) to modify15939 * @usdelay: amount of delay15940 *15941 * This function revises the EQ delay on 1 or more EQs. The EQ delay15942 * is set either by writing to a register (if supported by the SLI Port)15943 * or by mailbox command. The mailbox command allows several EQs to be15944 * updated at once.15945 *15946 * The @phba struct is used to send a mailbox command to HBA. The @startq15947 * is used to get the starting EQ index to change. The @numq value is15948 * used to specify how many consecutive EQ indexes, starting at EQ index,15949 * are to be changed. This function is asynchronous and will wait for any15950 * mailbox commands to finish before returning.15951 *15952 * On success this function will return a zero. If unable to allocate15953 * enough memory this function will return -ENOMEM. If a mailbox command15954 * fails this function will return -ENXIO. Note: on ENXIO, some EQs may15955 * have had their delay multipler changed.15956 **/15957void15958lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,15959			 uint32_t numq, uint32_t usdelay)15960{15961	struct lpfc_mbx_modify_eq_delay *eq_delay;15962	LPFC_MBOXQ_t *mbox;15963	struct lpfc_queue *eq;15964	int cnt = 0, rc, length;15965	uint32_t shdr_status, shdr_add_status;15966	uint32_t dmult;15967	int qidx;15968	union lpfc_sli4_cfg_shdr *shdr;15969 15970	if (startq >= phba->cfg_irq_chann)15971		return;15972 15973	if (usdelay > 0xFFFF) {15974		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,15975				"6429 usdelay %d too large. Scaled down to "15976				"0xFFFF.\n", usdelay);15977		usdelay = 0xFFFF;15978	}15979 15980	/* set values by EQ_DELAY register if supported */15981	if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {15982		for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {15983			eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;15984			if (!eq)15985				continue;15986 15987			lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);15988 15989			if (++cnt >= numq)15990				break;15991		}15992		return;15993	}15994 15995	/* Otherwise, set values by mailbox cmd */15996 15997	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);15998	if (!mbox) {15999		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,16000				"6428 Failed allocating mailbox cmd buffer."16001				" EQ delay was not set.\n");16002		return;16003	}16004	length = (sizeof(struct lpfc_mbx_modify_eq_delay) -16005		  sizeof(struct lpfc_sli4_cfg_mhdr));16006	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,16007			 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,16008			 length, LPFC_SLI4_MBX_EMBED);16009	eq_delay = &mbox->u.mqe.un.eq_delay;16010 16011	/* Calculate delay multiper from maximum interrupt per second */16012	dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;16013	if (dmult)16014		dmult--;16015	if (dmult > LPFC_DMULT_MAX)16016		dmult = LPFC_DMULT_MAX;16017 16018	for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {16019		eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;16020		if (!eq)16021			continue;16022		eq->q_mode = usdelay;16023		eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;16024		eq_delay->u.request.eq[cnt].phase = 0;16025		eq_delay->u.request.eq[cnt].delay_multi = dmult;16026 16027		if (++cnt >= numq)16028			break;16029	}16030	eq_delay->u.request.num_eq = cnt;16031 16032	mbox->vport = phba->pport;16033	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;16034	mbox->ctx_ndlp = NULL;16035	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);16036	shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;16037	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);16038	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);16039	if (shdr_status || shdr_add_status || rc) {16040		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,16041				"2512 MODIFY_EQ_DELAY mailbox failed with "16042				"status x%x add_status x%x, mbx status x%x\n",16043				shdr_status, shdr_add_status, rc);16044	}16045	mempool_free(mbox, phba->mbox_mem_pool);16046	return;16047}16048 16049/**16050 * lpfc_eq_create - Create an Event Queue on the HBA16051 * @phba: HBA structure that indicates port to create a queue on.16052 * @eq: The queue structure to use to create the event queue.16053 * @imax: The maximum interrupt per second limit.16054 *16055 * This function creates an event queue, as detailed in @eq, on a port,16056 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.16057 *16058 * The @phba struct is used to send mailbox command to HBA. The @eq struct16059 * is used to get the entry count and entry size that are necessary to16060 * determine the number of pages to allocate and use for this queue. This16061 * function will send the EQ_CREATE mailbox command to the HBA to setup the16062 * event queue. This function is asynchronous and will wait for the mailbox16063 * command to finish before continuing.16064 *16065 * On success this function will return a zero. If unable to allocate enough16066 * memory this function will return -ENOMEM. If the queue create mailbox command16067 * fails this function will return -ENXIO.16068 **/16069int16070lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)16071{16072	struct lpfc_mbx_eq_create *eq_create;16073	LPFC_MBOXQ_t *mbox;16074	int rc, length, status = 0;16075	struct lpfc_dmabuf *dmabuf;16076	uint32_t shdr_status, shdr_add_status;16077	union lpfc_sli4_cfg_shdr *shdr;16078	uint16_t dmult;16079	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;16080 16081	/* sanity check on queue memory */16082	if (!eq)16083		return -ENODEV;16084	if (!phba->sli4_hba.pc_sli4_params.supported)16085		hw_page_size = SLI4_PAGE_SIZE;16086 16087	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);16088	if (!mbox)16089		return -ENOMEM;16090	length = (sizeof(struct lpfc_mbx_eq_create) -16091		  sizeof(struct lpfc_sli4_cfg_mhdr));16092	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,16093			 LPFC_MBOX_OPCODE_EQ_CREATE,16094			 length, LPFC_SLI4_MBX_EMBED);16095	eq_create = &mbox->u.mqe.un.eq_create;16096	shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;16097	bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,16098	       eq->page_count);16099	bf_set(lpfc_eq_context_size, &eq_create->u.request.context,16100	       LPFC_EQE_SIZE);16101	bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);16102 16103	/* Use version 2 of CREATE_EQ if eqav is set */16104	if (phba->sli4_hba.pc_sli4_params.eqav) {16105		bf_set(lpfc_mbox_hdr_version, &shdr->request,16106		       LPFC_Q_CREATE_VERSION_2);16107		bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,16108		       phba->sli4_hba.pc_sli4_params.eqav);16109	}16110 16111	/* don't setup delay multiplier using EQ_CREATE */16112	dmult = 0;16113	bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,16114	       dmult);16115	switch (eq->entry_count) {16116	default:16117		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,16118				"0360 Unsupported EQ count. (%d)\n",16119				eq->entry_count);16120		if (eq->entry_count < 256) {16121			status = -EINVAL;16122			goto out;16123		}16124		fallthrough;	/* otherwise default to smallest count */16125	case 256:16126		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,16127		       LPFC_EQ_CNT_256);16128		break;16129	case 512:16130		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,16131		       LPFC_EQ_CNT_512);16132		break;16133	case 1024:16134		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,16135		       LPFC_EQ_CNT_1024);16136		break;16137	case 2048:16138		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,16139		       LPFC_EQ_CNT_2048);16140		break;16141	case 4096:16142		bf_set(lpfc_eq_context_count, &eq_create->u.request.context,16143		       LPFC_EQ_CNT_4096);16144		break;16145	}16146	list_for_each_entry(dmabuf, &eq->page_list, list) {16147		memset(dmabuf->virt, 0, hw_page_size);16148		eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =16149					putPaddrLow(dmabuf->phys);16150		eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =16151					putPaddrHigh(dmabuf->phys);16152	}16153	mbox->vport = phba->pport;16154	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;16155	mbox->ctx_buf = NULL;16156	mbox->ctx_ndlp = NULL;16157	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);16158	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);16159	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);16160	if (shdr_status || shdr_add_status || rc) {16161		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,16162				"2500 EQ_CREATE mailbox failed with "16163				"status x%x add_status x%x, mbx status x%x\n",16164				shdr_status, shdr_add_status, rc);16165		status = -ENXIO;16166	}16167	eq->type = LPFC_EQ;16168	eq->subtype = LPFC_NONE;16169	eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);16170	if (eq->queue_id == 0xFFFF)16171		status = -ENXIO;16172	eq->host_index = 0;16173	eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;16174	eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;16175out:16176	mempool_free(mbox, phba->mbox_mem_pool);16177	return status;16178}16179 16180/**16181 * lpfc_sli4_hba_intr_handler_th - SLI4 HBA threaded interrupt handler16182 * @irq: Interrupt number.16183 * @dev_id: The device context pointer.16184 *16185 * This routine is a mirror of lpfc_sli4_hba_intr_handler, but executed within16186 * threaded irq context.16187 *16188 * Returns16189 * IRQ_HANDLED - interrupt is handled16190 * IRQ_NONE - otherwise16191 **/16192irqreturn_t lpfc_sli4_hba_intr_handler_th(int irq, void *dev_id)16193{16194	struct lpfc_hba *phba;16195	struct lpfc_hba_eq_hdl *hba_eq_hdl;16196	struct lpfc_queue *fpeq;16197	int ecount = 0;16198	int hba_eqidx;16199	struct lpfc_eq_intr_info *eqi;16200 16201	/* Get the driver's phba structure from the dev_id */16202	hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;16203	phba = hba_eq_hdl->phba;16204	hba_eqidx = hba_eq_hdl->idx;16205 16206	if (unlikely(!phba))16207		return IRQ_NONE;16208	if (unlikely(!phba->sli4_hba.hdwq))16209		return IRQ_NONE;16210 16211	/* Get to the EQ struct associated with this vector */16212	fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;16213	if (unlikely(!fpeq))16214		return IRQ_NONE;16215 16216	eqi = per_cpu_ptr(phba->sli4_hba.eq_info, raw_smp_processor_id());16217	eqi->icnt++;16218 16219	fpeq->last_cpu = raw_smp_processor_id();16220 16221	if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&16222	    fpeq->q_flag & HBA_EQ_DELAY_CHK &&16223	    phba->cfg_auto_imax &&16224	    fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&16225	    phba->sli.sli_flag & LPFC_SLI_USE_EQDR)16226		lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);16227 16228	/* process and rearm the EQ */16229	ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM,16230				      LPFC_THREADED_IRQ);16231 16232	if (unlikely(ecount == 0)) {16233		fpeq->EQ_no_entry++;16234		if (phba->intr_type == MSIX)16235			/* MSI-X treated interrupt served as no EQ share INT */16236			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,16237					"3358 MSI-X interrupt with no EQE\n");16238		else16239			/* Non MSI-X treated on interrupt as EQ share INT */16240			return IRQ_NONE;16241	}16242	return IRQ_HANDLED;16243}16244 16245/**16246 * lpfc_cq_create - Create a Completion Queue on the HBA16247 * @phba: HBA structure that indicates port to create a queue on.16248 * @cq: The queue structure to use to create the completion queue.16249 * @eq: The event queue to bind this completion queue to.16250 * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).16251 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).16252 *16253 * This function creates a completion queue, as detailed in @wq, on a port,16254 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.16255 *16256 * The @phba struct is used to send mailbox command to HBA. The @cq struct16257 * is used to get the entry count and entry size that are necessary to16258 * determine the number of pages to allocate and use for this queue. The @eq16259 * is used to indicate which event queue to bind this completion queue to. This16260 * function will send the CQ_CREATE mailbox command to the HBA to setup the16261 * completion queue. This function is asynchronous and will wait for the mailbox16262 * command to finish before continuing.16263 *16264 * On success this function will return a zero. If unable to allocate enough16265 * memory this function will return -ENOMEM. If the queue create mailbox command16266 * fails this function will return -ENXIO.16267 **/16268int16269lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,16270	       struct lpfc_queue *eq, uint32_t type, uint32_t subtype)16271{16272	struct lpfc_mbx_cq_create *cq_create;16273	struct lpfc_dmabuf *dmabuf;16274	LPFC_MBOXQ_t *mbox;16275	int rc, length, status = 0;16276	uint32_t shdr_status, shdr_add_status;16277	union lpfc_sli4_cfg_shdr *shdr;16278 16279	/* sanity check on queue memory */16280	if (!cq || !eq)16281		return -ENODEV;16282 16283	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);16284	if (!mbox)16285		return -ENOMEM;16286	length = (sizeof(struct lpfc_mbx_cq_create) -16287		  sizeof(struct lpfc_sli4_cfg_mhdr));16288	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,16289			 LPFC_MBOX_OPCODE_CQ_CREATE,16290			 length, LPFC_SLI4_MBX_EMBED);16291	cq_create = &mbox->u.mqe.un.cq_create;16292	shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;16293	bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,16294		    cq->page_count);16295	bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);16296	bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);16297	bf_set(lpfc_mbox_hdr_version, &shdr->request,16298	       phba->sli4_hba.pc_sli4_params.cqv);16299	if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {16300		bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,16301		       (cq->page_size / SLI4_PAGE_SIZE));16302		bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,16303		       eq->queue_id);16304		bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,16305		       phba->sli4_hba.pc_sli4_params.cqav);16306	} else {16307		bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,16308		       eq->queue_id);16309	}16310	switch (cq->entry_count) {16311	case 2048:16312	case 4096:16313		if (phba->sli4_hba.pc_sli4_params.cqv ==16314		    LPFC_Q_CREATE_VERSION_2) {16315			cq_create->u.request.context.lpfc_cq_context_count =16316				cq->entry_count;16317			bf_set(lpfc_cq_context_count,16318			       &cq_create->u.request.context,16319			       LPFC_CQ_CNT_WORD7);16320			break;16321		}16322		fallthrough;16323	default:16324		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,16325				"0361 Unsupported CQ count: "16326				"entry cnt %d sz %d pg cnt %d\n",16327				cq->entry_count, cq->entry_size,16328				cq->page_count);16329		if (cq->entry_count < 256) {16330			status = -EINVAL;16331			goto out;16332		}16333		fallthrough;	/* otherwise default to smallest count */16334	case 256:16335		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,16336		       LPFC_CQ_CNT_256);16337		break;16338	case 512:16339		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,16340		       LPFC_CQ_CNT_512);16341		break;16342	case 1024:16343		bf_set(lpfc_cq_context_count, &cq_create->u.request.context,16344		       LPFC_CQ_CNT_1024);16345		break;16346	}16347	list_for_each_entry(dmabuf, &cq->page_list, list) {16348		memset(dmabuf->virt, 0, cq->page_size);16349		cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =16350					putPaddrLow(dmabuf->phys);16351		cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =16352					putPaddrHigh(dmabuf->phys);16353	}16354	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);16355 16356	/* The IOCTL status is embedded in the mailbox subheader. */16357	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);16358	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);16359	if (shdr_status || shdr_add_status || rc) {16360		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,16361				"2501 CQ_CREATE mailbox failed with "16362				"status x%x add_status x%x, mbx status x%x\n",16363				shdr_status, shdr_add_status, rc);16364		status = -ENXIO;16365		goto out;16366	}16367	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);16368	if (cq->queue_id == 0xFFFF) {16369		status = -ENXIO;16370		goto out;16371	}16372	/* link the cq onto the parent eq child list */16373	list_add_tail(&cq->list, &eq->child_list);16374	/* Set up completion queue's type and subtype */16375	cq->type = type;16376	cq->subtype = subtype;16377	cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);16378	cq->assoc_qid = eq->queue_id;16379	cq->assoc_qp = eq;16380	cq->host_index = 0;16381	cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;16382	cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);16383 16384	if (cq->queue_id > phba->sli4_hba.cq_max)16385		phba->sli4_hba.cq_max = cq->queue_id;16386out:16387	mempool_free(mbox, phba->mbox_mem_pool);16388	return status;16389}16390 16391/**16392 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ16393 * @phba: HBA structure that indicates port to create a queue on.16394 * @cqp: The queue structure array to use to create the completion queues.16395 * @hdwq: The hardware queue array  with the EQ to bind completion queues to.16396 * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).16397 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).16398 *16399 * This function creates a set of  completion queue, s to support MRQ16400 * as detailed in @cqp, on a port,16401 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.16402 *16403 * The @phba struct is used to send mailbox command to HBA. The @cq struct16404 * is used to get the entry count and entry size that are necessary to16405 * determine the number of pages to allocate and use for this queue. The @eq16406 * is used to indicate which event queue to bind this completion queue to. This16407 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the16408 * completion queue. This function is asynchronous and will wait for the mailbox16409 * command to finish before continuing.16410 *16411 * On success this function will return a zero. If unable to allocate enough16412 * memory this function will return -ENOMEM. If the queue create mailbox command16413 * fails this function will return -ENXIO.16414 **/16415int16416lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,16417		   struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,16418		   uint32_t subtype)16419{16420	struct lpfc_queue *cq;16421	struct lpfc_queue *eq;16422	struct lpfc_mbx_cq_create_set *cq_set;16423	struct lpfc_dmabuf *dmabuf;16424	LPFC_MBOXQ_t *mbox;16425	int rc, length, alloclen, status = 0;16426	int cnt, idx, numcq, page_idx = 0;16427	uint32_t shdr_status, shdr_add_status;16428	union lpfc_sli4_cfg_shdr *shdr;16429	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;16430 16431	/* sanity check on queue memory */16432	numcq = phba->cfg_nvmet_mrq;16433	if (!cqp || !hdwq || !numcq)16434		return -ENODEV;16435 16436	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);16437	if (!mbox)16438		return -ENOMEM;16439 16440	length = sizeof(struct lpfc_mbx_cq_create_set);16441	length += ((numcq * cqp[0]->page_count) *16442		   sizeof(struct dma_address));16443	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,16444			LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,16445			LPFC_SLI4_MBX_NEMBED);16446	if (alloclen < length) {16447		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,16448				"3098 Allocated DMA memory size (%d) is "16449				"less than the requested DMA memory size "16450				"(%d)\n", alloclen, length);16451		status = -ENOMEM;16452		goto out;16453	}16454	cq_set = mbox->sge_array->addr[0];16455	shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;16456	bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);16457 16458	for (idx = 0; idx < numcq; idx++) {16459		cq = cqp[idx];16460		eq = hdwq[idx].hba_eq;16461		if (!cq || !eq) {16462			status = -ENOMEM;16463			goto out;16464		}16465		if (!phba->sli4_hba.pc_sli4_params.supported)16466			hw_page_size = cq->page_size;16467 16468		switch (idx) {16469		case 0:16470			bf_set(lpfc_mbx_cq_create_set_page_size,16471			       &cq_set->u.request,16472			       (hw_page_size / SLI4_PAGE_SIZE));16473			bf_set(lpfc_mbx_cq_create_set_num_pages,16474			       &cq_set->u.request, cq->page_count);16475			bf_set(lpfc_mbx_cq_create_set_evt,16476			       &cq_set->u.request, 1);16477			bf_set(lpfc_mbx_cq_create_set_valid,16478			       &cq_set->u.request, 1);16479			bf_set(lpfc_mbx_cq_create_set_cqe_size,16480			       &cq_set->u.request, 0);16481			bf_set(lpfc_mbx_cq_create_set_num_cq,16482			       &cq_set->u.request, numcq);16483			bf_set(lpfc_mbx_cq_create_set_autovalid,16484			       &cq_set->u.request,16485			       phba->sli4_hba.pc_sli4_params.cqav);16486			switch (cq->entry_count) {16487			case 2048:16488			case 4096:16489				if (phba->sli4_hba.pc_sli4_params.cqv ==16490				    LPFC_Q_CREATE_VERSION_2) {16491					bf_set(lpfc_mbx_cq_create_set_cqe_cnt,16492					       &cq_set->u.request,16493						cq->entry_count);16494					bf_set(lpfc_mbx_cq_create_set_cqe_cnt,16495					       &cq_set->u.request,16496					       LPFC_CQ_CNT_WORD7);16497					break;16498				}16499				fallthrough;16500			default:16501				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,16502						"3118 Bad CQ count. (%d)\n",16503						cq->entry_count);16504				if (cq->entry_count < 256) {16505					status = -EINVAL;16506					goto out;16507				}16508				fallthrough;	/* otherwise default to smallest */16509			case 256:16510				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,16511				       &cq_set->u.request, LPFC_CQ_CNT_256);16512				break;16513			case 512:16514				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,16515				       &cq_set->u.request, LPFC_CQ_CNT_512);16516				break;16517			case 1024:16518				bf_set(lpfc_mbx_cq_create_set_cqe_cnt,16519				       &cq_set->u.request, LPFC_CQ_CNT_1024);16520				break;16521			}16522			bf_set(lpfc_mbx_cq_create_set_eq_id0,16523			       &cq_set->u.request, eq->queue_id);16524			break;16525		case 1:16526			bf_set(lpfc_mbx_cq_create_set_eq_id1,16527			       &cq_set->u.request, eq->queue_id);16528			break;16529		case 2:16530			bf_set(lpfc_mbx_cq_create_set_eq_id2,16531			       &cq_set->u.request, eq->queue_id);16532			break;16533		case 3:16534			bf_set(lpfc_mbx_cq_create_set_eq_id3,16535			       &cq_set->u.request, eq->queue_id);16536			break;16537		case 4:16538			bf_set(lpfc_mbx_cq_create_set_eq_id4,16539			       &cq_set->u.request, eq->queue_id);16540			break;16541		case 5:16542			bf_set(lpfc_mbx_cq_create_set_eq_id5,16543			       &cq_set->u.request, eq->queue_id);16544			break;16545		case 6:16546			bf_set(lpfc_mbx_cq_create_set_eq_id6,16547			       &cq_set->u.request, eq->queue_id);16548			break;16549		case 7:16550			bf_set(lpfc_mbx_cq_create_set_eq_id7,16551			       &cq_set->u.request, eq->queue_id);16552			break;16553		case 8:16554			bf_set(lpfc_mbx_cq_create_set_eq_id8,16555			       &cq_set->u.request, eq->queue_id);16556			break;16557		case 9:16558			bf_set(lpfc_mbx_cq_create_set_eq_id9,16559			       &cq_set->u.request, eq->queue_id);16560			break;16561		case 10:16562			bf_set(lpfc_mbx_cq_create_set_eq_id10,16563			       &cq_set->u.request, eq->queue_id);16564			break;16565		case 11:16566			bf_set(lpfc_mbx_cq_create_set_eq_id11,16567			       &cq_set->u.request, eq->queue_id);16568			break;16569		case 12:16570			bf_set(lpfc_mbx_cq_create_set_eq_id12,16571			       &cq_set->u.request, eq->queue_id);16572			break;16573		case 13:16574			bf_set(lpfc_mbx_cq_create_set_eq_id13,16575			       &cq_set->u.request, eq->queue_id);16576			break;16577		case 14:16578			bf_set(lpfc_mbx_cq_create_set_eq_id14,16579			       &cq_set->u.request, eq->queue_id);16580			break;16581		case 15:16582			bf_set(lpfc_mbx_cq_create_set_eq_id15,16583			       &cq_set->u.request, eq->queue_id);16584			break;16585		}16586 16587		/* link the cq onto the parent eq child list */16588		list_add_tail(&cq->list, &eq->child_list);16589		/* Set up completion queue's type and subtype */16590		cq->type = type;16591		cq->subtype = subtype;16592		cq->assoc_qid = eq->queue_id;16593		cq->assoc_qp = eq;16594		cq->host_index = 0;16595		cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;16596		cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,16597					 cq->entry_count);16598		cq->chann = idx;16599 16600		rc = 0;16601		list_for_each_entry(dmabuf, &cq->page_list, list) {16602			memset(dmabuf->virt, 0, hw_page_size);16603			cnt = page_idx + dmabuf->buffer_tag;16604			cq_set->u.request.page[cnt].addr_lo =16605					putPaddrLow(dmabuf->phys);16606			cq_set->u.request.page[cnt].addr_hi =16607					putPaddrHigh(dmabuf->phys);16608			rc++;16609		}16610		page_idx += rc;16611	}16612 16613	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);16614 16615	/* The IOCTL status is embedded in the mailbox subheader. */16616	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);16617	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);16618	if (shdr_status || shdr_add_status || rc) {16619		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,16620				"3119 CQ_CREATE_SET mailbox failed with "16621				"status x%x add_status x%x, mbx status x%x\n",16622				shdr_status, shdr_add_status, rc);16623		status = -ENXIO;16624		goto out;16625	}16626	rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);16627	if (rc == 0xFFFF) {16628		status = -ENXIO;16629		goto out;16630	}16631 16632	for (idx = 0; idx < numcq; idx++) {16633		cq = cqp[idx];16634		cq->queue_id = rc + idx;16635		if (cq->queue_id > phba->sli4_hba.cq_max)16636			phba->sli4_hba.cq_max = cq->queue_id;16637	}16638 16639out:16640	lpfc_sli4_mbox_cmd_free(phba, mbox);16641	return status;16642}16643 16644/**16645 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration16646 * @phba: HBA structure that indicates port to create a queue on.16647 * @mq: The queue structure to use to create the mailbox queue.16648 * @mbox: An allocated pointer to type LPFC_MBOXQ_t16649 * @cq: The completion queue to associate with this cq.16650 *16651 * This function provides failback (fb) functionality when the16652 * mq_create_ext fails on older FW generations.  It's purpose is identical16653 * to mq_create_ext otherwise.16654 *16655 * This routine cannot fail as all attributes were previously accessed and16656 * initialized in mq_create_ext.16657 **/16658static void16659lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,16660		       LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)16661{16662	struct lpfc_mbx_mq_create *mq_create;16663	struct lpfc_dmabuf *dmabuf;16664	int length;16665 16666	length = (sizeof(struct lpfc_mbx_mq_create) -16667		  sizeof(struct lpfc_sli4_cfg_mhdr));16668	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,16669			 LPFC_MBOX_OPCODE_MQ_CREATE,16670			 length, LPFC_SLI4_MBX_EMBED);16671	mq_create = &mbox->u.mqe.un.mq_create;16672	bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,16673	       mq->page_count);16674	bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,16675	       cq->queue_id);16676	bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);16677	switch (mq->entry_count) {16678	case 16:16679		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,16680		       LPFC_MQ_RING_SIZE_16);16681		break;16682	case 32:16683		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,16684		       LPFC_MQ_RING_SIZE_32);16685		break;16686	case 64:16687		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,16688		       LPFC_MQ_RING_SIZE_64);16689		break;16690	case 128:16691		bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,16692		       LPFC_MQ_RING_SIZE_128);16693		break;16694	}16695	list_for_each_entry(dmabuf, &mq->page_list, list) {16696		mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =16697			putPaddrLow(dmabuf->phys);16698		mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =16699			putPaddrHigh(dmabuf->phys);16700	}16701}16702 16703/**16704 * lpfc_mq_create - Create a mailbox Queue on the HBA16705 * @phba: HBA structure that indicates port to create a queue on.16706 * @mq: The queue structure to use to create the mailbox queue.16707 * @cq: The completion queue to associate with this cq.16708 * @subtype: The queue's subtype.16709 *16710 * This function creates a mailbox queue, as detailed in @mq, on a port,16711 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.16712 *16713 * The @phba struct is used to send mailbox command to HBA. The @cq struct16714 * is used to get the entry count and entry size that are necessary to16715 * determine the number of pages to allocate and use for this queue. This16716 * function will send the MQ_CREATE mailbox command to the HBA to setup the16717 * mailbox queue. This function is asynchronous and will wait for the mailbox16718 * command to finish before continuing.16719 *16720 * On success this function will return a zero. If unable to allocate enough16721 * memory this function will return -ENOMEM. If the queue create mailbox command16722 * fails this function will return -ENXIO.16723 **/16724int32_t16725lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,16726	       struct lpfc_queue *cq, uint32_t subtype)16727{16728	struct lpfc_mbx_mq_create *mq_create;16729	struct lpfc_mbx_mq_create_ext *mq_create_ext;16730	struct lpfc_dmabuf *dmabuf;16731	LPFC_MBOXQ_t *mbox;16732	int rc, length, status = 0;16733	uint32_t shdr_status, shdr_add_status;16734	union lpfc_sli4_cfg_shdr *shdr;16735	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;16736 16737	/* sanity check on queue memory */16738	if (!mq || !cq)16739		return -ENODEV;16740	if (!phba->sli4_hba.pc_sli4_params.supported)16741		hw_page_size = SLI4_PAGE_SIZE;16742 16743	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);16744	if (!mbox)16745		return -ENOMEM;16746	length = (sizeof(struct lpfc_mbx_mq_create_ext) -16747		  sizeof(struct lpfc_sli4_cfg_mhdr));16748	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,16749			 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,16750			 length, LPFC_SLI4_MBX_EMBED);16751 16752	mq_create_ext = &mbox->u.mqe.un.mq_create_ext;16753	shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;16754	bf_set(lpfc_mbx_mq_create_ext_num_pages,16755	       &mq_create_ext->u.request, mq->page_count);16756	bf_set(lpfc_mbx_mq_create_ext_async_evt_link,16757	       &mq_create_ext->u.request, 1);16758	bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,16759	       &mq_create_ext->u.request, 1);16760	bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,16761	       &mq_create_ext->u.request, 1);16762	bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,16763	       &mq_create_ext->u.request, 1);16764	bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,16765	       &mq_create_ext->u.request, 1);16766	bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);16767	bf_set(lpfc_mbox_hdr_version, &shdr->request,16768	       phba->sli4_hba.pc_sli4_params.mqv);16769	if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)16770		bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,16771		       cq->queue_id);16772	else16773		bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,16774		       cq->queue_id);16775	switch (mq->entry_count) {16776	default:16777		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,16778				"0362 Unsupported MQ count. (%d)\n",16779				mq->entry_count);16780		if (mq->entry_count < 16) {16781			status = -EINVAL;16782			goto out;16783		}16784		fallthrough;	/* otherwise default to smallest count */16785	case 16:16786		bf_set(lpfc_mq_context_ring_size,16787		       &mq_create_ext->u.request.context,16788		       LPFC_MQ_RING_SIZE_16);16789		break;16790	case 32:16791		bf_set(lpfc_mq_context_ring_size,16792		       &mq_create_ext->u.request.context,16793		       LPFC_MQ_RING_SIZE_32);16794		break;16795	case 64:16796		bf_set(lpfc_mq_context_ring_size,16797		       &mq_create_ext->u.request.context,16798		       LPFC_MQ_RING_SIZE_64);16799		break;16800	case 128:16801		bf_set(lpfc_mq_context_ring_size,16802		       &mq_create_ext->u.request.context,16803		       LPFC_MQ_RING_SIZE_128);16804		break;16805	}16806	list_for_each_entry(dmabuf, &mq->page_list, list) {16807		memset(dmabuf->virt, 0, hw_page_size);16808		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =16809					putPaddrLow(dmabuf->phys);16810		mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =16811					putPaddrHigh(dmabuf->phys);16812	}16813	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);16814	mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,16815			      &mq_create_ext->u.response);16816	if (rc != MBX_SUCCESS) {16817		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,16818				"2795 MQ_CREATE_EXT failed with "16819				"status x%x. Failback to MQ_CREATE.\n",16820				rc);16821		lpfc_mq_create_fb_init(phba, mq, mbox, cq);16822		mq_create = &mbox->u.mqe.un.mq_create;16823		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);16824		shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;16825		mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,16826				      &mq_create->u.response);16827	}16828 16829	/* The IOCTL status is embedded in the mailbox subheader. */16830	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);16831	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);16832	if (shdr_status || shdr_add_status || rc) {16833		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,16834				"2502 MQ_CREATE mailbox failed with "16835				"status x%x add_status x%x, mbx status x%x\n",16836				shdr_status, shdr_add_status, rc);16837		status = -ENXIO;16838		goto out;16839	}16840	if (mq->queue_id == 0xFFFF) {16841		status = -ENXIO;16842		goto out;16843	}16844	mq->type = LPFC_MQ;16845	mq->assoc_qid = cq->queue_id;16846	mq->subtype = subtype;16847	mq->host_index = 0;16848	mq->hba_index = 0;16849 16850	/* link the mq onto the parent cq child list */16851	list_add_tail(&mq->list, &cq->child_list);16852out:16853	mempool_free(mbox, phba->mbox_mem_pool);16854	return status;16855}16856 16857/**16858 * lpfc_wq_create - Create a Work Queue on the HBA16859 * @phba: HBA structure that indicates port to create a queue on.16860 * @wq: The queue structure to use to create the work queue.16861 * @cq: The completion queue to bind this work queue to.16862 * @subtype: The subtype of the work queue indicating its functionality.16863 *16864 * This function creates a work queue, as detailed in @wq, on a port, described16865 * by @phba by sending a WQ_CREATE mailbox command to the HBA.16866 *16867 * The @phba struct is used to send mailbox command to HBA. The @wq struct16868 * is used to get the entry count and entry size that are necessary to16869 * determine the number of pages to allocate and use for this queue. The @cq16870 * is used to indicate which completion queue to bind this work queue to. This16871 * function will send the WQ_CREATE mailbox command to the HBA to setup the16872 * work queue. This function is asynchronous and will wait for the mailbox16873 * command to finish before continuing.16874 *16875 * On success this function will return a zero. If unable to allocate enough16876 * memory this function will return -ENOMEM. If the queue create mailbox command16877 * fails this function will return -ENXIO.16878 **/16879int16880lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,16881	       struct lpfc_queue *cq, uint32_t subtype)16882{16883	struct lpfc_mbx_wq_create *wq_create;16884	struct lpfc_dmabuf *dmabuf;16885	LPFC_MBOXQ_t *mbox;16886	int rc, length, status = 0;16887	uint32_t shdr_status, shdr_add_status;16888	union lpfc_sli4_cfg_shdr *shdr;16889	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;16890	struct dma_address *page;16891	void __iomem *bar_memmap_p;16892	uint32_t db_offset;16893	uint16_t pci_barset;16894	uint8_t dpp_barset;16895	uint32_t dpp_offset;16896	uint8_t wq_create_version;16897#ifdef CONFIG_X8616898	unsigned long pg_addr;16899#endif16900 16901	/* sanity check on queue memory */16902	if (!wq || !cq)16903		return -ENODEV;16904	if (!phba->sli4_hba.pc_sli4_params.supported)16905		hw_page_size = wq->page_size;16906 16907	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);16908	if (!mbox)16909		return -ENOMEM;16910	length = (sizeof(struct lpfc_mbx_wq_create) -16911		  sizeof(struct lpfc_sli4_cfg_mhdr));16912	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,16913			 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,16914			 length, LPFC_SLI4_MBX_EMBED);16915	wq_create = &mbox->u.mqe.un.wq_create;16916	shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;16917	bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,16918		    wq->page_count);16919	bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,16920		    cq->queue_id);16921 16922	/* wqv is the earliest version supported, NOT the latest */16923	bf_set(lpfc_mbox_hdr_version, &shdr->request,16924	       phba->sli4_hba.pc_sli4_params.wqv);16925 16926	if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||16927	    (wq->page_size > SLI4_PAGE_SIZE))16928		wq_create_version = LPFC_Q_CREATE_VERSION_1;16929	else16930		wq_create_version = LPFC_Q_CREATE_VERSION_0;16931 16932	switch (wq_create_version) {16933	case LPFC_Q_CREATE_VERSION_1:16934		bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,16935		       wq->entry_count);16936		bf_set(lpfc_mbox_hdr_version, &shdr->request,16937		       LPFC_Q_CREATE_VERSION_1);16938 16939		switch (wq->entry_size) {16940		default:16941		case 64:16942			bf_set(lpfc_mbx_wq_create_wqe_size,16943			       &wq_create->u.request_1,16944			       LPFC_WQ_WQE_SIZE_64);16945			break;16946		case 128:16947			bf_set(lpfc_mbx_wq_create_wqe_size,16948			       &wq_create->u.request_1,16949			       LPFC_WQ_WQE_SIZE_128);16950			break;16951		}16952		/* Request DPP by default */16953		bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);16954		bf_set(lpfc_mbx_wq_create_page_size,16955		       &wq_create->u.request_1,16956		       (wq->page_size / SLI4_PAGE_SIZE));16957		page = wq_create->u.request_1.page;16958		break;16959	default:16960		page = wq_create->u.request.page;16961		break;16962	}16963 16964	list_for_each_entry(dmabuf, &wq->page_list, list) {16965		memset(dmabuf->virt, 0, hw_page_size);16966		page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);16967		page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);16968	}16969 16970	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)16971		bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);16972 16973	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);16974	/* The IOCTL status is embedded in the mailbox subheader. */16975	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);16976	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);16977	if (shdr_status || shdr_add_status || rc) {16978		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,16979				"2503 WQ_CREATE mailbox failed with "16980				"status x%x add_status x%x, mbx status x%x\n",16981				shdr_status, shdr_add_status, rc);16982		status = -ENXIO;16983		goto out;16984	}16985 16986	if (wq_create_version == LPFC_Q_CREATE_VERSION_0)16987		wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,16988					&wq_create->u.response);16989	else16990		wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,16991					&wq_create->u.response_1);16992 16993	if (wq->queue_id == 0xFFFF) {16994		status = -ENXIO;16995		goto out;16996	}16997 16998	wq->db_format = LPFC_DB_LIST_FORMAT;16999	if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {17000		if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {17001			wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,17002					       &wq_create->u.response);17003			if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&17004			    (wq->db_format != LPFC_DB_RING_FORMAT)) {17005				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17006						"3265 WQ[%d] doorbell format "17007						"not supported: x%x\n",17008						wq->queue_id, wq->db_format);17009				status = -EINVAL;17010				goto out;17011			}17012			pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,17013					    &wq_create->u.response);17014			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,17015								   pci_barset);17016			if (!bar_memmap_p) {17017				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17018						"3263 WQ[%d] failed to memmap "17019						"pci barset:x%x\n",17020						wq->queue_id, pci_barset);17021				status = -ENOMEM;17022				goto out;17023			}17024			db_offset = wq_create->u.response.doorbell_offset;17025			if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&17026			    (db_offset != LPFC_ULP1_WQ_DOORBELL)) {17027				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17028						"3252 WQ[%d] doorbell offset "17029						"not supported: x%x\n",17030						wq->queue_id, db_offset);17031				status = -EINVAL;17032				goto out;17033			}17034			wq->db_regaddr = bar_memmap_p + db_offset;17035			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,17036					"3264 WQ[%d]: barset:x%x, offset:x%x, "17037					"format:x%x\n", wq->queue_id,17038					pci_barset, db_offset, wq->db_format);17039		} else17040			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;17041	} else {17042		/* Check if DPP was honored by the firmware */17043		wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,17044				    &wq_create->u.response_1);17045		if (wq->dpp_enable) {17046			pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,17047					    &wq_create->u.response_1);17048			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,17049								   pci_barset);17050			if (!bar_memmap_p) {17051				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17052						"3267 WQ[%d] failed to memmap "17053						"pci barset:x%x\n",17054						wq->queue_id, pci_barset);17055				status = -ENOMEM;17056				goto out;17057			}17058			db_offset = wq_create->u.response_1.doorbell_offset;17059			wq->db_regaddr = bar_memmap_p + db_offset;17060			wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,17061					    &wq_create->u.response_1);17062			dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,17063					    &wq_create->u.response_1);17064			bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,17065								   dpp_barset);17066			if (!bar_memmap_p) {17067				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17068						"3268 WQ[%d] failed to memmap "17069						"pci barset:x%x\n",17070						wq->queue_id, dpp_barset);17071				status = -ENOMEM;17072				goto out;17073			}17074			dpp_offset = wq_create->u.response_1.dpp_offset;17075			wq->dpp_regaddr = bar_memmap_p + dpp_offset;17076			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,17077					"3271 WQ[%d]: barset:x%x, offset:x%x, "17078					"dpp_id:x%x dpp_barset:x%x "17079					"dpp_offset:x%x\n",17080					wq->queue_id, pci_barset, db_offset,17081					wq->dpp_id, dpp_barset, dpp_offset);17082 17083#ifdef CONFIG_X8617084			/* Enable combined writes for DPP aperture */17085			pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;17086			rc = set_memory_wc(pg_addr, 1);17087			if (rc) {17088				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,17089					"3272 Cannot setup Combined "17090					"Write on WQ[%d] - disable DPP\n",17091					wq->queue_id);17092				phba->cfg_enable_dpp = 0;17093			}17094#else17095			phba->cfg_enable_dpp = 0;17096#endif17097		} else17098			wq->db_regaddr = phba->sli4_hba.WQDBregaddr;17099	}17100	wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);17101	if (wq->pring == NULL) {17102		status = -ENOMEM;17103		goto out;17104	}17105	wq->type = LPFC_WQ;17106	wq->assoc_qid = cq->queue_id;17107	wq->subtype = subtype;17108	wq->host_index = 0;17109	wq->hba_index = 0;17110	wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;17111 17112	/* link the wq onto the parent cq child list */17113	list_add_tail(&wq->list, &cq->child_list);17114out:17115	mempool_free(mbox, phba->mbox_mem_pool);17116	return status;17117}17118 17119/**17120 * lpfc_rq_create - Create a Receive Queue on the HBA17121 * @phba: HBA structure that indicates port to create a queue on.17122 * @hrq: The queue structure to use to create the header receive queue.17123 * @drq: The queue structure to use to create the data receive queue.17124 * @cq: The completion queue to bind this work queue to.17125 * @subtype: The subtype of the work queue indicating its functionality.17126 *17127 * This function creates a receive buffer queue pair , as detailed in @hrq and17128 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command17129 * to the HBA.17130 *17131 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq17132 * struct is used to get the entry count that is necessary to determine the17133 * number of pages to use for this queue. The @cq is used to indicate which17134 * completion queue to bind received buffers that are posted to these queues to.17135 * This function will send the RQ_CREATE mailbox command to the HBA to setup the17136 * receive queue pair. This function is asynchronous and will wait for the17137 * mailbox command to finish before continuing.17138 *17139 * On success this function will return a zero. If unable to allocate enough17140 * memory this function will return -ENOMEM. If the queue create mailbox command17141 * fails this function will return -ENXIO.17142 **/17143int17144lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,17145	       struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)17146{17147	struct lpfc_mbx_rq_create *rq_create;17148	struct lpfc_dmabuf *dmabuf;17149	LPFC_MBOXQ_t *mbox;17150	int rc, length, status = 0;17151	uint32_t shdr_status, shdr_add_status;17152	union lpfc_sli4_cfg_shdr *shdr;17153	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;17154	void __iomem *bar_memmap_p;17155	uint32_t db_offset;17156	uint16_t pci_barset;17157 17158	/* sanity check on queue memory */17159	if (!hrq || !drq || !cq)17160		return -ENODEV;17161	if (!phba->sli4_hba.pc_sli4_params.supported)17162		hw_page_size = SLI4_PAGE_SIZE;17163 17164	if (hrq->entry_count != drq->entry_count)17165		return -EINVAL;17166	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);17167	if (!mbox)17168		return -ENOMEM;17169	length = (sizeof(struct lpfc_mbx_rq_create) -17170		  sizeof(struct lpfc_sli4_cfg_mhdr));17171	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,17172			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,17173			 length, LPFC_SLI4_MBX_EMBED);17174	rq_create = &mbox->u.mqe.un.rq_create;17175	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;17176	bf_set(lpfc_mbox_hdr_version, &shdr->request,17177	       phba->sli4_hba.pc_sli4_params.rqv);17178	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {17179		bf_set(lpfc_rq_context_rqe_count_1,17180		       &rq_create->u.request.context,17181		       hrq->entry_count);17182		rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;17183		bf_set(lpfc_rq_context_rqe_size,17184		       &rq_create->u.request.context,17185		       LPFC_RQE_SIZE_8);17186		bf_set(lpfc_rq_context_page_size,17187		       &rq_create->u.request.context,17188		       LPFC_RQ_PAGE_SIZE_4096);17189	} else {17190		switch (hrq->entry_count) {17191		default:17192			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17193					"2535 Unsupported RQ count. (%d)\n",17194					hrq->entry_count);17195			if (hrq->entry_count < 512) {17196				status = -EINVAL;17197				goto out;17198			}17199			fallthrough;	/* otherwise default to smallest count */17200		case 512:17201			bf_set(lpfc_rq_context_rqe_count,17202			       &rq_create->u.request.context,17203			       LPFC_RQ_RING_SIZE_512);17204			break;17205		case 1024:17206			bf_set(lpfc_rq_context_rqe_count,17207			       &rq_create->u.request.context,17208			       LPFC_RQ_RING_SIZE_1024);17209			break;17210		case 2048:17211			bf_set(lpfc_rq_context_rqe_count,17212			       &rq_create->u.request.context,17213			       LPFC_RQ_RING_SIZE_2048);17214			break;17215		case 4096:17216			bf_set(lpfc_rq_context_rqe_count,17217			       &rq_create->u.request.context,17218			       LPFC_RQ_RING_SIZE_4096);17219			break;17220		}17221		bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,17222		       LPFC_HDR_BUF_SIZE);17223	}17224	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,17225	       cq->queue_id);17226	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,17227	       hrq->page_count);17228	list_for_each_entry(dmabuf, &hrq->page_list, list) {17229		memset(dmabuf->virt, 0, hw_page_size);17230		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =17231					putPaddrLow(dmabuf->phys);17232		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =17233					putPaddrHigh(dmabuf->phys);17234	}17235	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)17236		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);17237 17238	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);17239	/* The IOCTL status is embedded in the mailbox subheader. */17240	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);17241	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);17242	if (shdr_status || shdr_add_status || rc) {17243		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17244				"2504 RQ_CREATE mailbox failed with "17245				"status x%x add_status x%x, mbx status x%x\n",17246				shdr_status, shdr_add_status, rc);17247		status = -ENXIO;17248		goto out;17249	}17250	hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);17251	if (hrq->queue_id == 0xFFFF) {17252		status = -ENXIO;17253		goto out;17254	}17255 17256	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {17257		hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,17258					&rq_create->u.response);17259		if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&17260		    (hrq->db_format != LPFC_DB_RING_FORMAT)) {17261			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17262					"3262 RQ [%d] doorbell format not "17263					"supported: x%x\n", hrq->queue_id,17264					hrq->db_format);17265			status = -EINVAL;17266			goto out;17267		}17268 17269		pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,17270				    &rq_create->u.response);17271		bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);17272		if (!bar_memmap_p) {17273			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17274					"3269 RQ[%d] failed to memmap pci "17275					"barset:x%x\n", hrq->queue_id,17276					pci_barset);17277			status = -ENOMEM;17278			goto out;17279		}17280 17281		db_offset = rq_create->u.response.doorbell_offset;17282		if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&17283		    (db_offset != LPFC_ULP1_RQ_DOORBELL)) {17284			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17285					"3270 RQ[%d] doorbell offset not "17286					"supported: x%x\n", hrq->queue_id,17287					db_offset);17288			status = -EINVAL;17289			goto out;17290		}17291		hrq->db_regaddr = bar_memmap_p + db_offset;17292		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,17293				"3266 RQ[qid:%d]: barset:x%x, offset:x%x, "17294				"format:x%x\n", hrq->queue_id, pci_barset,17295				db_offset, hrq->db_format);17296	} else {17297		hrq->db_format = LPFC_DB_RING_FORMAT;17298		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;17299	}17300	hrq->type = LPFC_HRQ;17301	hrq->assoc_qid = cq->queue_id;17302	hrq->subtype = subtype;17303	hrq->host_index = 0;17304	hrq->hba_index = 0;17305	hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;17306 17307	/* now create the data queue */17308	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,17309			 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,17310			 length, LPFC_SLI4_MBX_EMBED);17311	bf_set(lpfc_mbox_hdr_version, &shdr->request,17312	       phba->sli4_hba.pc_sli4_params.rqv);17313	if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {17314		bf_set(lpfc_rq_context_rqe_count_1,17315		       &rq_create->u.request.context, hrq->entry_count);17316		if (subtype == LPFC_NVMET)17317			rq_create->u.request.context.buffer_size =17318				LPFC_NVMET_DATA_BUF_SIZE;17319		else17320			rq_create->u.request.context.buffer_size =17321				LPFC_DATA_BUF_SIZE;17322		bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,17323		       LPFC_RQE_SIZE_8);17324		bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,17325		       (PAGE_SIZE/SLI4_PAGE_SIZE));17326	} else {17327		switch (drq->entry_count) {17328		default:17329			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17330					"2536 Unsupported RQ count. (%d)\n",17331					drq->entry_count);17332			if (drq->entry_count < 512) {17333				status = -EINVAL;17334				goto out;17335			}17336			fallthrough;	/* otherwise default to smallest count */17337		case 512:17338			bf_set(lpfc_rq_context_rqe_count,17339			       &rq_create->u.request.context,17340			       LPFC_RQ_RING_SIZE_512);17341			break;17342		case 1024:17343			bf_set(lpfc_rq_context_rqe_count,17344			       &rq_create->u.request.context,17345			       LPFC_RQ_RING_SIZE_1024);17346			break;17347		case 2048:17348			bf_set(lpfc_rq_context_rqe_count,17349			       &rq_create->u.request.context,17350			       LPFC_RQ_RING_SIZE_2048);17351			break;17352		case 4096:17353			bf_set(lpfc_rq_context_rqe_count,17354			       &rq_create->u.request.context,17355			       LPFC_RQ_RING_SIZE_4096);17356			break;17357		}17358		if (subtype == LPFC_NVMET)17359			bf_set(lpfc_rq_context_buf_size,17360			       &rq_create->u.request.context,17361			       LPFC_NVMET_DATA_BUF_SIZE);17362		else17363			bf_set(lpfc_rq_context_buf_size,17364			       &rq_create->u.request.context,17365			       LPFC_DATA_BUF_SIZE);17366	}17367	bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,17368	       cq->queue_id);17369	bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,17370	       drq->page_count);17371	list_for_each_entry(dmabuf, &drq->page_list, list) {17372		rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =17373					putPaddrLow(dmabuf->phys);17374		rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =17375					putPaddrHigh(dmabuf->phys);17376	}17377	if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)17378		bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);17379	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);17380	/* The IOCTL status is embedded in the mailbox subheader. */17381	shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;17382	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);17383	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);17384	if (shdr_status || shdr_add_status || rc) {17385		status = -ENXIO;17386		goto out;17387	}17388	drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);17389	if (drq->queue_id == 0xFFFF) {17390		status = -ENXIO;17391		goto out;17392	}17393	drq->type = LPFC_DRQ;17394	drq->assoc_qid = cq->queue_id;17395	drq->subtype = subtype;17396	drq->host_index = 0;17397	drq->hba_index = 0;17398	drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;17399 17400	/* link the header and data RQs onto the parent cq child list */17401	list_add_tail(&hrq->list, &cq->child_list);17402	list_add_tail(&drq->list, &cq->child_list);17403 17404out:17405	mempool_free(mbox, phba->mbox_mem_pool);17406	return status;17407}17408 17409/**17410 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA17411 * @phba: HBA structure that indicates port to create a queue on.17412 * @hrqp: The queue structure array to use to create the header receive queues.17413 * @drqp: The queue structure array to use to create the data receive queues.17414 * @cqp: The completion queue array to bind these receive queues to.17415 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).17416 *17417 * This function creates a receive buffer queue pair , as detailed in @hrq and17418 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command17419 * to the HBA.17420 *17421 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq17422 * struct is used to get the entry count that is necessary to determine the17423 * number of pages to use for this queue. The @cq is used to indicate which17424 * completion queue to bind received buffers that are posted to these queues to.17425 * This function will send the RQ_CREATE mailbox command to the HBA to setup the17426 * receive queue pair. This function is asynchronous and will wait for the17427 * mailbox command to finish before continuing.17428 *17429 * On success this function will return a zero. If unable to allocate enough17430 * memory this function will return -ENOMEM. If the queue create mailbox command17431 * fails this function will return -ENXIO.17432 **/17433int17434lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,17435		struct lpfc_queue **drqp, struct lpfc_queue **cqp,17436		uint32_t subtype)17437{17438	struct lpfc_queue *hrq, *drq, *cq;17439	struct lpfc_mbx_rq_create_v2 *rq_create;17440	struct lpfc_dmabuf *dmabuf;17441	LPFC_MBOXQ_t *mbox;17442	int rc, length, alloclen, status = 0;17443	int cnt, idx, numrq, page_idx = 0;17444	uint32_t shdr_status, shdr_add_status;17445	union lpfc_sli4_cfg_shdr *shdr;17446	uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;17447 17448	numrq = phba->cfg_nvmet_mrq;17449	/* sanity check on array memory */17450	if (!hrqp || !drqp || !cqp || !numrq)17451		return -ENODEV;17452	if (!phba->sli4_hba.pc_sli4_params.supported)17453		hw_page_size = SLI4_PAGE_SIZE;17454 17455	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);17456	if (!mbox)17457		return -ENOMEM;17458 17459	length = sizeof(struct lpfc_mbx_rq_create_v2);17460	length += ((2 * numrq * hrqp[0]->page_count) *17461		   sizeof(struct dma_address));17462 17463	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,17464				    LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,17465				    LPFC_SLI4_MBX_NEMBED);17466	if (alloclen < length) {17467		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17468				"3099 Allocated DMA memory size (%d) is "17469				"less than the requested DMA memory size "17470				"(%d)\n", alloclen, length);17471		status = -ENOMEM;17472		goto out;17473	}17474 17475 17476 17477	rq_create = mbox->sge_array->addr[0];17478	shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;17479 17480	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);17481	cnt = 0;17482 17483	for (idx = 0; idx < numrq; idx++) {17484		hrq = hrqp[idx];17485		drq = drqp[idx];17486		cq  = cqp[idx];17487 17488		/* sanity check on queue memory */17489		if (!hrq || !drq || !cq) {17490			status = -ENODEV;17491			goto out;17492		}17493 17494		if (hrq->entry_count != drq->entry_count) {17495			status = -EINVAL;17496			goto out;17497		}17498 17499		if (idx == 0) {17500			bf_set(lpfc_mbx_rq_create_num_pages,17501			       &rq_create->u.request,17502			       hrq->page_count);17503			bf_set(lpfc_mbx_rq_create_rq_cnt,17504			       &rq_create->u.request, (numrq * 2));17505			bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,17506			       1);17507			bf_set(lpfc_rq_context_base_cq,17508			       &rq_create->u.request.context,17509			       cq->queue_id);17510			bf_set(lpfc_rq_context_data_size,17511			       &rq_create->u.request.context,17512			       LPFC_NVMET_DATA_BUF_SIZE);17513			bf_set(lpfc_rq_context_hdr_size,17514			       &rq_create->u.request.context,17515			       LPFC_HDR_BUF_SIZE);17516			bf_set(lpfc_rq_context_rqe_count_1,17517			       &rq_create->u.request.context,17518			       hrq->entry_count);17519			bf_set(lpfc_rq_context_rqe_size,17520			       &rq_create->u.request.context,17521			       LPFC_RQE_SIZE_8);17522			bf_set(lpfc_rq_context_page_size,17523			       &rq_create->u.request.context,17524			       (PAGE_SIZE/SLI4_PAGE_SIZE));17525		}17526		rc = 0;17527		list_for_each_entry(dmabuf, &hrq->page_list, list) {17528			memset(dmabuf->virt, 0, hw_page_size);17529			cnt = page_idx + dmabuf->buffer_tag;17530			rq_create->u.request.page[cnt].addr_lo =17531					putPaddrLow(dmabuf->phys);17532			rq_create->u.request.page[cnt].addr_hi =17533					putPaddrHigh(dmabuf->phys);17534			rc++;17535		}17536		page_idx += rc;17537 17538		rc = 0;17539		list_for_each_entry(dmabuf, &drq->page_list, list) {17540			memset(dmabuf->virt, 0, hw_page_size);17541			cnt = page_idx + dmabuf->buffer_tag;17542			rq_create->u.request.page[cnt].addr_lo =17543					putPaddrLow(dmabuf->phys);17544			rq_create->u.request.page[cnt].addr_hi =17545					putPaddrHigh(dmabuf->phys);17546			rc++;17547		}17548		page_idx += rc;17549 17550		hrq->db_format = LPFC_DB_RING_FORMAT;17551		hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;17552		hrq->type = LPFC_HRQ;17553		hrq->assoc_qid = cq->queue_id;17554		hrq->subtype = subtype;17555		hrq->host_index = 0;17556		hrq->hba_index = 0;17557		hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;17558 17559		drq->db_format = LPFC_DB_RING_FORMAT;17560		drq->db_regaddr = phba->sli4_hba.RQDBregaddr;17561		drq->type = LPFC_DRQ;17562		drq->assoc_qid = cq->queue_id;17563		drq->subtype = subtype;17564		drq->host_index = 0;17565		drq->hba_index = 0;17566		drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;17567 17568		list_add_tail(&hrq->list, &cq->child_list);17569		list_add_tail(&drq->list, &cq->child_list);17570	}17571 17572	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);17573	/* The IOCTL status is embedded in the mailbox subheader. */17574	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);17575	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);17576	if (shdr_status || shdr_add_status || rc) {17577		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17578				"3120 RQ_CREATE mailbox failed with "17579				"status x%x add_status x%x, mbx status x%x\n",17580				shdr_status, shdr_add_status, rc);17581		status = -ENXIO;17582		goto out;17583	}17584	rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);17585	if (rc == 0xFFFF) {17586		status = -ENXIO;17587		goto out;17588	}17589 17590	/* Initialize all RQs with associated queue id */17591	for (idx = 0; idx < numrq; idx++) {17592		hrq = hrqp[idx];17593		hrq->queue_id = rc + (2 * idx);17594		drq = drqp[idx];17595		drq->queue_id = rc + (2 * idx) + 1;17596	}17597 17598out:17599	lpfc_sli4_mbox_cmd_free(phba, mbox);17600	return status;17601}17602 17603/**17604 * lpfc_eq_destroy - Destroy an event Queue on the HBA17605 * @phba: HBA structure that indicates port to destroy a queue on.17606 * @eq: The queue structure associated with the queue to destroy.17607 *17608 * This function destroys a queue, as detailed in @eq by sending an mailbox17609 * command, specific to the type of queue, to the HBA.17610 *17611 * The @eq struct is used to get the queue ID of the queue to destroy.17612 *17613 * On success this function will return a zero. If the queue destroy mailbox17614 * command fails this function will return -ENXIO.17615 **/17616int17617lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)17618{17619	LPFC_MBOXQ_t *mbox;17620	int rc, length, status = 0;17621	uint32_t shdr_status, shdr_add_status;17622	union lpfc_sli4_cfg_shdr *shdr;17623 17624	/* sanity check on queue memory */17625	if (!eq)17626		return -ENODEV;17627 17628	mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);17629	if (!mbox)17630		return -ENOMEM;17631	length = (sizeof(struct lpfc_mbx_eq_destroy) -17632		  sizeof(struct lpfc_sli4_cfg_mhdr));17633	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,17634			 LPFC_MBOX_OPCODE_EQ_DESTROY,17635			 length, LPFC_SLI4_MBX_EMBED);17636	bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,17637	       eq->queue_id);17638	mbox->vport = eq->phba->pport;17639	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;17640 17641	rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);17642	/* The IOCTL status is embedded in the mailbox subheader. */17643	shdr = (union lpfc_sli4_cfg_shdr *)17644		&mbox->u.mqe.un.eq_destroy.header.cfg_shdr;17645	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);17646	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);17647	if (shdr_status || shdr_add_status || rc) {17648		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17649				"2505 EQ_DESTROY mailbox failed with "17650				"status x%x add_status x%x, mbx status x%x\n",17651				shdr_status, shdr_add_status, rc);17652		status = -ENXIO;17653	}17654 17655	/* Remove eq from any list */17656	list_del_init(&eq->list);17657	mempool_free(mbox, eq->phba->mbox_mem_pool);17658	return status;17659}17660 17661/**17662 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA17663 * @phba: HBA structure that indicates port to destroy a queue on.17664 * @cq: The queue structure associated with the queue to destroy.17665 *17666 * This function destroys a queue, as detailed in @cq by sending an mailbox17667 * command, specific to the type of queue, to the HBA.17668 *17669 * The @cq struct is used to get the queue ID of the queue to destroy.17670 *17671 * On success this function will return a zero. If the queue destroy mailbox17672 * command fails this function will return -ENXIO.17673 **/17674int17675lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)17676{17677	LPFC_MBOXQ_t *mbox;17678	int rc, length, status = 0;17679	uint32_t shdr_status, shdr_add_status;17680	union lpfc_sli4_cfg_shdr *shdr;17681 17682	/* sanity check on queue memory */17683	if (!cq)17684		return -ENODEV;17685	mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);17686	if (!mbox)17687		return -ENOMEM;17688	length = (sizeof(struct lpfc_mbx_cq_destroy) -17689		  sizeof(struct lpfc_sli4_cfg_mhdr));17690	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,17691			 LPFC_MBOX_OPCODE_CQ_DESTROY,17692			 length, LPFC_SLI4_MBX_EMBED);17693	bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,17694	       cq->queue_id);17695	mbox->vport = cq->phba->pport;17696	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;17697	rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);17698	/* The IOCTL status is embedded in the mailbox subheader. */17699	shdr = (union lpfc_sli4_cfg_shdr *)17700		&mbox->u.mqe.un.wq_create.header.cfg_shdr;17701	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);17702	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);17703	if (shdr_status || shdr_add_status || rc) {17704		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17705				"2506 CQ_DESTROY mailbox failed with "17706				"status x%x add_status x%x, mbx status x%x\n",17707				shdr_status, shdr_add_status, rc);17708		status = -ENXIO;17709	}17710	/* Remove cq from any list */17711	list_del_init(&cq->list);17712	mempool_free(mbox, cq->phba->mbox_mem_pool);17713	return status;17714}17715 17716/**17717 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA17718 * @phba: HBA structure that indicates port to destroy a queue on.17719 * @mq: The queue structure associated with the queue to destroy.17720 *17721 * This function destroys a queue, as detailed in @mq by sending an mailbox17722 * command, specific to the type of queue, to the HBA.17723 *17724 * The @mq struct is used to get the queue ID of the queue to destroy.17725 *17726 * On success this function will return a zero. If the queue destroy mailbox17727 * command fails this function will return -ENXIO.17728 **/17729int17730lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)17731{17732	LPFC_MBOXQ_t *mbox;17733	int rc, length, status = 0;17734	uint32_t shdr_status, shdr_add_status;17735	union lpfc_sli4_cfg_shdr *shdr;17736 17737	/* sanity check on queue memory */17738	if (!mq)17739		return -ENODEV;17740	mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);17741	if (!mbox)17742		return -ENOMEM;17743	length = (sizeof(struct lpfc_mbx_mq_destroy) -17744		  sizeof(struct lpfc_sli4_cfg_mhdr));17745	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,17746			 LPFC_MBOX_OPCODE_MQ_DESTROY,17747			 length, LPFC_SLI4_MBX_EMBED);17748	bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,17749	       mq->queue_id);17750	mbox->vport = mq->phba->pport;17751	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;17752	rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);17753	/* The IOCTL status is embedded in the mailbox subheader. */17754	shdr = (union lpfc_sli4_cfg_shdr *)17755		&mbox->u.mqe.un.mq_destroy.header.cfg_shdr;17756	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);17757	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);17758	if (shdr_status || shdr_add_status || rc) {17759		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17760				"2507 MQ_DESTROY mailbox failed with "17761				"status x%x add_status x%x, mbx status x%x\n",17762				shdr_status, shdr_add_status, rc);17763		status = -ENXIO;17764	}17765	/* Remove mq from any list */17766	list_del_init(&mq->list);17767	mempool_free(mbox, mq->phba->mbox_mem_pool);17768	return status;17769}17770 17771/**17772 * lpfc_wq_destroy - Destroy a Work Queue on the HBA17773 * @phba: HBA structure that indicates port to destroy a queue on.17774 * @wq: The queue structure associated with the queue to destroy.17775 *17776 * This function destroys a queue, as detailed in @wq by sending an mailbox17777 * command, specific to the type of queue, to the HBA.17778 *17779 * The @wq struct is used to get the queue ID of the queue to destroy.17780 *17781 * On success this function will return a zero. If the queue destroy mailbox17782 * command fails this function will return -ENXIO.17783 **/17784int17785lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)17786{17787	LPFC_MBOXQ_t *mbox;17788	int rc, length, status = 0;17789	uint32_t shdr_status, shdr_add_status;17790	union lpfc_sli4_cfg_shdr *shdr;17791 17792	/* sanity check on queue memory */17793	if (!wq)17794		return -ENODEV;17795	mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);17796	if (!mbox)17797		return -ENOMEM;17798	length = (sizeof(struct lpfc_mbx_wq_destroy) -17799		  sizeof(struct lpfc_sli4_cfg_mhdr));17800	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,17801			 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,17802			 length, LPFC_SLI4_MBX_EMBED);17803	bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,17804	       wq->queue_id);17805	mbox->vport = wq->phba->pport;17806	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;17807	rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);17808	shdr = (union lpfc_sli4_cfg_shdr *)17809		&mbox->u.mqe.un.wq_destroy.header.cfg_shdr;17810	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);17811	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);17812	if (shdr_status || shdr_add_status || rc) {17813		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17814				"2508 WQ_DESTROY mailbox failed with "17815				"status x%x add_status x%x, mbx status x%x\n",17816				shdr_status, shdr_add_status, rc);17817		status = -ENXIO;17818	}17819	/* Remove wq from any list */17820	list_del_init(&wq->list);17821	kfree(wq->pring);17822	wq->pring = NULL;17823	mempool_free(mbox, wq->phba->mbox_mem_pool);17824	return status;17825}17826 17827/**17828 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA17829 * @phba: HBA structure that indicates port to destroy a queue on.17830 * @hrq: The queue structure associated with the queue to destroy.17831 * @drq: The queue structure associated with the queue to destroy.17832 *17833 * This function destroys a queue, as detailed in @rq by sending an mailbox17834 * command, specific to the type of queue, to the HBA.17835 *17836 * The @rq struct is used to get the queue ID of the queue to destroy.17837 *17838 * On success this function will return a zero. If the queue destroy mailbox17839 * command fails this function will return -ENXIO.17840 **/17841int17842lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,17843		struct lpfc_queue *drq)17844{17845	LPFC_MBOXQ_t *mbox;17846	int rc, length, status = 0;17847	uint32_t shdr_status, shdr_add_status;17848	union lpfc_sli4_cfg_shdr *shdr;17849 17850	/* sanity check on queue memory */17851	if (!hrq || !drq)17852		return -ENODEV;17853	mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);17854	if (!mbox)17855		return -ENOMEM;17856	length = (sizeof(struct lpfc_mbx_rq_destroy) -17857		  sizeof(struct lpfc_sli4_cfg_mhdr));17858	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,17859			 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,17860			 length, LPFC_SLI4_MBX_EMBED);17861	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,17862	       hrq->queue_id);17863	mbox->vport = hrq->phba->pport;17864	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;17865	rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);17866	/* The IOCTL status is embedded in the mailbox subheader. */17867	shdr = (union lpfc_sli4_cfg_shdr *)17868		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;17869	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);17870	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);17871	if (shdr_status || shdr_add_status || rc) {17872		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17873				"2509 RQ_DESTROY mailbox failed with "17874				"status x%x add_status x%x, mbx status x%x\n",17875				shdr_status, shdr_add_status, rc);17876		mempool_free(mbox, hrq->phba->mbox_mem_pool);17877		return -ENXIO;17878	}17879	bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,17880	       drq->queue_id);17881	rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);17882	shdr = (union lpfc_sli4_cfg_shdr *)17883		&mbox->u.mqe.un.rq_destroy.header.cfg_shdr;17884	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);17885	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);17886	if (shdr_status || shdr_add_status || rc) {17887		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17888				"2510 RQ_DESTROY mailbox failed with "17889				"status x%x add_status x%x, mbx status x%x\n",17890				shdr_status, shdr_add_status, rc);17891		status = -ENXIO;17892	}17893	list_del_init(&hrq->list);17894	list_del_init(&drq->list);17895	mempool_free(mbox, hrq->phba->mbox_mem_pool);17896	return status;17897}17898 17899/**17900 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA17901 * @phba: The virtual port for which this call being executed.17902 * @pdma_phys_addr0: Physical address of the 1st SGL page.17903 * @pdma_phys_addr1: Physical address of the 2nd SGL page.17904 * @xritag: the xritag that ties this io to the SGL pages.17905 *17906 * This routine will post the sgl pages for the IO that has the xritag17907 * that is in the iocbq structure. The xritag is assigned during iocbq17908 * creation and persists for as long as the driver is loaded.17909 * if the caller has fewer than 256 scatter gather segments to map then17910 * pdma_phys_addr1 should be 0.17911 * If the caller needs to map more than 256 scatter gather segment then17912 * pdma_phys_addr1 should be a valid physical address.17913 * physical address for SGLs must be 64 byte aligned.17914 * If you are going to map 2 SGL's then the first one must have 256 entries17915 * the second sgl can have between 1 and 256 entries.17916 *17917 * Return codes:17918 * 	0 - Success17919 * 	-ENXIO, -ENOMEM - Failure17920 **/17921int17922lpfc_sli4_post_sgl(struct lpfc_hba *phba,17923		dma_addr_t pdma_phys_addr0,17924		dma_addr_t pdma_phys_addr1,17925		uint16_t xritag)17926{17927	struct lpfc_mbx_post_sgl_pages *post_sgl_pages;17928	LPFC_MBOXQ_t *mbox;17929	int rc;17930	uint32_t shdr_status, shdr_add_status;17931	uint32_t mbox_tmo;17932	union lpfc_sli4_cfg_shdr *shdr;17933 17934	if (xritag == NO_XRI) {17935		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17936				"0364 Invalid param:\n");17937		return -EINVAL;17938	}17939 17940	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);17941	if (!mbox)17942		return -ENOMEM;17943 17944	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,17945			LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,17946			sizeof(struct lpfc_mbx_post_sgl_pages) -17947			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);17948 17949	post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)17950				&mbox->u.mqe.un.post_sgl_pages;17951	bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);17952	bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);17953 17954	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo	=17955				cpu_to_le32(putPaddrLow(pdma_phys_addr0));17956	post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =17957				cpu_to_le32(putPaddrHigh(pdma_phys_addr0));17958 17959	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo	=17960				cpu_to_le32(putPaddrLow(pdma_phys_addr1));17961	post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =17962				cpu_to_le32(putPaddrHigh(pdma_phys_addr1));17963	if (!phba->sli4_hba.intr_enable)17964		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);17965	else {17966		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);17967		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);17968	}17969	/* The IOCTL status is embedded in the mailbox subheader. */17970	shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;17971	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);17972	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);17973	if (!phba->sli4_hba.intr_enable)17974		mempool_free(mbox, phba->mbox_mem_pool);17975	else if (rc != MBX_TIMEOUT)17976		mempool_free(mbox, phba->mbox_mem_pool);17977	if (shdr_status || shdr_add_status || rc) {17978		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,17979				"2511 POST_SGL mailbox failed with "17980				"status x%x add_status x%x, mbx status x%x\n",17981				shdr_status, shdr_add_status, rc);17982	}17983	return 0;17984}17985 17986/**17987 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range17988 * @phba: pointer to lpfc hba data structure.17989 *17990 * This routine is invoked to post rpi header templates to the17991 * HBA consistent with the SLI-4 interface spec.  This routine17992 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to17993 * SLI4_PAGE_SIZE modulo 64 rpi context headers.17994 *17995 * Returns17996 *	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful17997 *	LPFC_RPI_ALLOC_ERROR if no rpis are available.17998 **/17999static uint16_t18000lpfc_sli4_alloc_xri(struct lpfc_hba *phba)18001{18002	unsigned long xri;18003 18004	/*18005	 * Fetch the next logical xri.  Because this index is logical,18006	 * the driver starts at 0 each time.18007	 */18008	spin_lock_irq(&phba->hbalock);18009	xri = find_first_zero_bit(phba->sli4_hba.xri_bmask,18010				 phba->sli4_hba.max_cfg_param.max_xri);18011	if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {18012		spin_unlock_irq(&phba->hbalock);18013		return NO_XRI;18014	} else {18015		set_bit(xri, phba->sli4_hba.xri_bmask);18016		phba->sli4_hba.max_cfg_param.xri_used++;18017	}18018	spin_unlock_irq(&phba->hbalock);18019	return xri;18020}18021 18022/**18023 * __lpfc_sli4_free_xri - Release an xri for reuse.18024 * @phba: pointer to lpfc hba data structure.18025 * @xri: xri to release.18026 *18027 * This routine is invoked to release an xri to the pool of18028 * available rpis maintained by the driver.18029 **/18030static void18031__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)18032{18033	if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {18034		phba->sli4_hba.max_cfg_param.xri_used--;18035	}18036}18037 18038/**18039 * lpfc_sli4_free_xri - Release an xri for reuse.18040 * @phba: pointer to lpfc hba data structure.18041 * @xri: xri to release.18042 *18043 * This routine is invoked to release an xri to the pool of18044 * available rpis maintained by the driver.18045 **/18046void18047lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)18048{18049	spin_lock_irq(&phba->hbalock);18050	__lpfc_sli4_free_xri(phba, xri);18051	spin_unlock_irq(&phba->hbalock);18052}18053 18054/**18055 * lpfc_sli4_next_xritag - Get an xritag for the io18056 * @phba: Pointer to HBA context object.18057 *18058 * This function gets an xritag for the iocb. If there is no unused xritag18059 * it will return 0xffff.18060 * The function returns the allocated xritag if successful, else returns zero.18061 * Zero is not a valid xritag.18062 * The caller is not required to hold any lock.18063 **/18064uint16_t18065lpfc_sli4_next_xritag(struct lpfc_hba *phba)18066{18067	uint16_t xri_index;18068 18069	xri_index = lpfc_sli4_alloc_xri(phba);18070	if (xri_index == NO_XRI)18071		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,18072				"2004 Failed to allocate XRI.last XRITAG is %d"18073				" Max XRI is %d, Used XRI is %d\n",18074				xri_index,18075				phba->sli4_hba.max_cfg_param.max_xri,18076				phba->sli4_hba.max_cfg_param.xri_used);18077	return xri_index;18078}18079 18080/**18081 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.18082 * @phba: pointer to lpfc hba data structure.18083 * @post_sgl_list: pointer to els sgl entry list.18084 * @post_cnt: number of els sgl entries on the list.18085 *18086 * This routine is invoked to post a block of driver's sgl pages to the18087 * HBA using non-embedded mailbox command. No Lock is held. This routine18088 * is only called when the driver is loading and after all IO has been18089 * stopped.18090 **/18091static int18092lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,18093			    struct list_head *post_sgl_list,18094			    int post_cnt)18095{18096	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;18097	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;18098	struct sgl_page_pairs *sgl_pg_pairs;18099	void *viraddr;18100	LPFC_MBOXQ_t *mbox;18101	uint32_t reqlen, alloclen, pg_pairs;18102	uint32_t mbox_tmo;18103	uint16_t xritag_start = 0;18104	int rc = 0;18105	uint32_t shdr_status, shdr_add_status;18106	union lpfc_sli4_cfg_shdr *shdr;18107 18108	reqlen = post_cnt * sizeof(struct sgl_page_pairs) +18109		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);18110	if (reqlen > SLI4_PAGE_SIZE) {18111		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,18112				"2559 Block sgl registration required DMA "18113				"size (%d) great than a page\n", reqlen);18114		return -ENOMEM;18115	}18116 18117	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);18118	if (!mbox)18119		return -ENOMEM;18120 18121	/* Allocate DMA memory and set up the non-embedded mailbox command */18122	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,18123			 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,18124			 LPFC_SLI4_MBX_NEMBED);18125 18126	if (alloclen < reqlen) {18127		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,18128				"0285 Allocated DMA memory size (%d) is "18129				"less than the requested DMA memory "18130				"size (%d)\n", alloclen, reqlen);18131		lpfc_sli4_mbox_cmd_free(phba, mbox);18132		return -ENOMEM;18133	}18134	/* Set up the SGL pages in the non-embedded DMA pages */18135	viraddr = mbox->sge_array->addr[0];18136	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;18137	sgl_pg_pairs = &sgl->sgl_pg_pairs;18138 18139	pg_pairs = 0;18140	list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {18141		/* Set up the sge entry */18142		sgl_pg_pairs->sgl_pg0_addr_lo =18143				cpu_to_le32(putPaddrLow(sglq_entry->phys));18144		sgl_pg_pairs->sgl_pg0_addr_hi =18145				cpu_to_le32(putPaddrHigh(sglq_entry->phys));18146		sgl_pg_pairs->sgl_pg1_addr_lo =18147				cpu_to_le32(putPaddrLow(0));18148		sgl_pg_pairs->sgl_pg1_addr_hi =18149				cpu_to_le32(putPaddrHigh(0));18150 18151		/* Keep the first xritag on the list */18152		if (pg_pairs == 0)18153			xritag_start = sglq_entry->sli4_xritag;18154		sgl_pg_pairs++;18155		pg_pairs++;18156	}18157 18158	/* Complete initialization and perform endian conversion. */18159	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);18160	bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);18161	sgl->word0 = cpu_to_le32(sgl->word0);18162 18163	if (!phba->sli4_hba.intr_enable)18164		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);18165	else {18166		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);18167		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);18168	}18169	shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;18170	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);18171	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);18172	if (!phba->sli4_hba.intr_enable)18173		lpfc_sli4_mbox_cmd_free(phba, mbox);18174	else if (rc != MBX_TIMEOUT)18175		lpfc_sli4_mbox_cmd_free(phba, mbox);18176	if (shdr_status || shdr_add_status || rc) {18177		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,18178				"2513 POST_SGL_BLOCK mailbox command failed "18179				"status x%x add_status x%x mbx status x%x\n",18180				shdr_status, shdr_add_status, rc);18181		rc = -ENXIO;18182	}18183	return rc;18184}18185 18186/**18187 * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware18188 * @phba: pointer to lpfc hba data structure.18189 * @nblist: pointer to nvme buffer list.18190 * @count: number of scsi buffers on the list.18191 *18192 * This routine is invoked to post a block of @count scsi sgl pages from a18193 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.18194 * No Lock is held.18195 *18196 **/18197static int18198lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,18199			    int count)18200{18201	struct lpfc_io_buf *lpfc_ncmd;18202	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;18203	struct sgl_page_pairs *sgl_pg_pairs;18204	void *viraddr;18205	LPFC_MBOXQ_t *mbox;18206	uint32_t reqlen, alloclen, pg_pairs;18207	uint32_t mbox_tmo;18208	uint16_t xritag_start = 0;18209	int rc = 0;18210	uint32_t shdr_status, shdr_add_status;18211	dma_addr_t pdma_phys_bpl1;18212	union lpfc_sli4_cfg_shdr *shdr;18213 18214	/* Calculate the requested length of the dma memory */18215	reqlen = count * sizeof(struct sgl_page_pairs) +18216		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);18217	if (reqlen > SLI4_PAGE_SIZE) {18218		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,18219				"6118 Block sgl registration required DMA "18220				"size (%d) great than a page\n", reqlen);18221		return -ENOMEM;18222	}18223	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);18224	if (!mbox) {18225		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,18226				"6119 Failed to allocate mbox cmd memory\n");18227		return -ENOMEM;18228	}18229 18230	/* Allocate DMA memory and set up the non-embedded mailbox command */18231	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,18232				    LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,18233				    reqlen, LPFC_SLI4_MBX_NEMBED);18234 18235	if (alloclen < reqlen) {18236		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,18237				"6120 Allocated DMA memory size (%d) is "18238				"less than the requested DMA memory "18239				"size (%d)\n", alloclen, reqlen);18240		lpfc_sli4_mbox_cmd_free(phba, mbox);18241		return -ENOMEM;18242	}18243 18244	/* Get the first SGE entry from the non-embedded DMA memory */18245	viraddr = mbox->sge_array->addr[0];18246 18247	/* Set up the SGL pages in the non-embedded DMA pages */18248	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;18249	sgl_pg_pairs = &sgl->sgl_pg_pairs;18250 18251	pg_pairs = 0;18252	list_for_each_entry(lpfc_ncmd, nblist, list) {18253		/* Set up the sge entry */18254		sgl_pg_pairs->sgl_pg0_addr_lo =18255			cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));18256		sgl_pg_pairs->sgl_pg0_addr_hi =18257			cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));18258		if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)18259			pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +18260						SGL_PAGE_SIZE;18261		else18262			pdma_phys_bpl1 = 0;18263		sgl_pg_pairs->sgl_pg1_addr_lo =18264			cpu_to_le32(putPaddrLow(pdma_phys_bpl1));18265		sgl_pg_pairs->sgl_pg1_addr_hi =18266			cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));18267		/* Keep the first xritag on the list */18268		if (pg_pairs == 0)18269			xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;18270		sgl_pg_pairs++;18271		pg_pairs++;18272	}18273	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);18274	bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);18275	/* Perform endian conversion if necessary */18276	sgl->word0 = cpu_to_le32(sgl->word0);18277 18278	if (!phba->sli4_hba.intr_enable) {18279		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);18280	} else {18281		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);18282		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);18283	}18284	shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;18285	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);18286	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);18287	if (!phba->sli4_hba.intr_enable)18288		lpfc_sli4_mbox_cmd_free(phba, mbox);18289	else if (rc != MBX_TIMEOUT)18290		lpfc_sli4_mbox_cmd_free(phba, mbox);18291	if (shdr_status || shdr_add_status || rc) {18292		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,18293				"6125 POST_SGL_BLOCK mailbox command failed "18294				"status x%x add_status x%x mbx status x%x\n",18295				shdr_status, shdr_add_status, rc);18296		rc = -ENXIO;18297	}18298	return rc;18299}18300 18301/**18302 * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list18303 * @phba: pointer to lpfc hba data structure.18304 * @post_nblist: pointer to the nvme buffer list.18305 * @sb_count: number of nvme buffers.18306 *18307 * This routine walks a list of nvme buffers that was passed in. It attempts18308 * to construct blocks of nvme buffer sgls which contains contiguous xris and18309 * uses the non-embedded SGL block post mailbox commands to post to the port.18310 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use18311 * embedded SGL post mailbox command for posting. The @post_nblist passed in18312 * must be local list, thus no lock is needed when manipulate the list.18313 *18314 * Returns: 0 = failure, non-zero number of successfully posted buffers.18315 **/18316int18317lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,18318			   struct list_head *post_nblist, int sb_count)18319{18320	struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;18321	int status, sgl_size;18322	int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;18323	dma_addr_t pdma_phys_sgl1;18324	int last_xritag = NO_XRI;18325	int cur_xritag;18326	LIST_HEAD(prep_nblist);18327	LIST_HEAD(blck_nblist);18328	LIST_HEAD(nvme_nblist);18329 18330	/* sanity check */18331	if (sb_count <= 0)18332		return -EINVAL;18333 18334	sgl_size = phba->cfg_sg_dma_buf_size;18335	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {18336		list_del_init(&lpfc_ncmd->list);18337		block_cnt++;18338		if ((last_xritag != NO_XRI) &&18339		    (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {18340			/* a hole in xri block, form a sgl posting block */18341			list_splice_init(&prep_nblist, &blck_nblist);18342			post_cnt = block_cnt - 1;18343			/* prepare list for next posting block */18344			list_add_tail(&lpfc_ncmd->list, &prep_nblist);18345			block_cnt = 1;18346		} else {18347			/* prepare list for next posting block */18348			list_add_tail(&lpfc_ncmd->list, &prep_nblist);18349			/* enough sgls for non-embed sgl mbox command */18350			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {18351				list_splice_init(&prep_nblist, &blck_nblist);18352				post_cnt = block_cnt;18353				block_cnt = 0;18354			}18355		}18356		num_posting++;18357		last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;18358 18359		/* end of repost sgl list condition for NVME buffers */18360		if (num_posting == sb_count) {18361			if (post_cnt == 0) {18362				/* last sgl posting block */18363				list_splice_init(&prep_nblist, &blck_nblist);18364				post_cnt = block_cnt;18365			} else if (block_cnt == 1) {18366				/* last single sgl with non-contiguous xri */18367				if (sgl_size > SGL_PAGE_SIZE)18368					pdma_phys_sgl1 =18369						lpfc_ncmd->dma_phys_sgl +18370						SGL_PAGE_SIZE;18371				else18372					pdma_phys_sgl1 = 0;18373				cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;18374				status = lpfc_sli4_post_sgl(18375						phba, lpfc_ncmd->dma_phys_sgl,18376						pdma_phys_sgl1, cur_xritag);18377				if (status) {18378					/* Post error.  Buffer unavailable. */18379					lpfc_ncmd->flags |=18380						LPFC_SBUF_NOT_POSTED;18381				} else {18382					/* Post success. Bffer available. */18383					lpfc_ncmd->flags &=18384						~LPFC_SBUF_NOT_POSTED;18385					lpfc_ncmd->status = IOSTAT_SUCCESS;18386					num_posted++;18387				}18388				/* success, put on NVME buffer sgl list */18389				list_add_tail(&lpfc_ncmd->list, &nvme_nblist);18390			}18391		}18392 18393		/* continue until a nembed page worth of sgls */18394		if (post_cnt == 0)18395			continue;18396 18397		/* post block of NVME buffer list sgls */18398		status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,18399						     post_cnt);18400 18401		/* don't reset xirtag due to hole in xri block */18402		if (block_cnt == 0)18403			last_xritag = NO_XRI;18404 18405		/* reset NVME buffer post count for next round of posting */18406		post_cnt = 0;18407 18408		/* put posted NVME buffer-sgl posted on NVME buffer sgl list */18409		while (!list_empty(&blck_nblist)) {18410			list_remove_head(&blck_nblist, lpfc_ncmd,18411					 struct lpfc_io_buf, list);18412			if (status) {18413				/* Post error.  Mark buffer unavailable. */18414				lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;18415			} else {18416				/* Post success, Mark buffer available. */18417				lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;18418				lpfc_ncmd->status = IOSTAT_SUCCESS;18419				num_posted++;18420			}18421			list_add_tail(&lpfc_ncmd->list, &nvme_nblist);18422		}18423	}18424	/* Push NVME buffers with sgl posted to the available list */18425	lpfc_io_buf_replenish(phba, &nvme_nblist);18426 18427	return num_posted;18428}18429 18430/**18431 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle18432 * @phba: pointer to lpfc_hba struct that the frame was received on18433 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)18434 *18435 * This function checks the fields in the @fc_hdr to see if the FC frame is a18436 * valid type of frame that the LPFC driver will handle. This function will18437 * return a zero if the frame is a valid frame or a non zero value when the18438 * frame does not pass the check.18439 **/18440static int18441lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)18442{18443	/*  make rctl_names static to save stack space */18444	struct fc_vft_header *fc_vft_hdr;18445	struct fc_app_header *fc_app_hdr;18446	uint32_t *header = (uint32_t *) fc_hdr;18447 18448#define FC_RCTL_MDS_DIAGS	0xF418449 18450	switch (fc_hdr->fh_r_ctl) {18451	case FC_RCTL_DD_UNCAT:		/* uncategorized information */18452	case FC_RCTL_DD_SOL_DATA:	/* solicited data */18453	case FC_RCTL_DD_UNSOL_CTL:	/* unsolicited control */18454	case FC_RCTL_DD_SOL_CTL:	/* solicited control or reply */18455	case FC_RCTL_DD_UNSOL_DATA:	/* unsolicited data */18456	case FC_RCTL_DD_DATA_DESC:	/* data descriptor */18457	case FC_RCTL_DD_UNSOL_CMD:	/* unsolicited command */18458	case FC_RCTL_DD_CMD_STATUS:	/* command status */18459	case FC_RCTL_ELS_REQ:	/* extended link services request */18460	case FC_RCTL_ELS_REP:	/* extended link services reply */18461	case FC_RCTL_ELS4_REQ:	/* FC-4 ELS request */18462	case FC_RCTL_ELS4_REP:	/* FC-4 ELS reply */18463	case FC_RCTL_BA_ABTS: 	/* basic link service abort */18464	case FC_RCTL_BA_RMC: 	/* remove connection */18465	case FC_RCTL_BA_ACC:	/* basic accept */18466	case FC_RCTL_BA_RJT:	/* basic reject */18467	case FC_RCTL_BA_PRMT:18468	case FC_RCTL_ACK_1:	/* acknowledge_1 */18469	case FC_RCTL_ACK_0:	/* acknowledge_0 */18470	case FC_RCTL_P_RJT:	/* port reject */18471	case FC_RCTL_F_RJT:	/* fabric reject */18472	case FC_RCTL_P_BSY:	/* port busy */18473	case FC_RCTL_F_BSY:	/* fabric busy to data frame */18474	case FC_RCTL_F_BSYL:	/* fabric busy to link control frame */18475	case FC_RCTL_LCR:	/* link credit reset */18476	case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */18477	case FC_RCTL_END:	/* end */18478		break;18479	case FC_RCTL_VFTH:	/* Virtual Fabric tagging Header */18480		fc_vft_hdr = (struct fc_vft_header *)fc_hdr;18481		fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];18482		return lpfc_fc_frame_check(phba, fc_hdr);18483	case FC_RCTL_BA_NOP:	/* basic link service NOP */18484	default:18485		goto drop;18486	}18487 18488	switch (fc_hdr->fh_type) {18489	case FC_TYPE_BLS:18490	case FC_TYPE_ELS:18491	case FC_TYPE_FCP:18492	case FC_TYPE_CT:18493	case FC_TYPE_NVME:18494		break;18495	case FC_TYPE_IP:18496	case FC_TYPE_ILS:18497	default:18498		goto drop;18499	}18500 18501	if (unlikely(phba->link_flag == LS_LOOPBACK_MODE &&18502				phba->cfg_vmid_app_header)) {18503		/* Application header is 16B device header */18504		if (fc_hdr->fh_df_ctl & LPFC_FC_16B_DEVICE_HEADER) {18505			fc_app_hdr = (struct fc_app_header *) (fc_hdr + 1);18506			if (be32_to_cpu(fc_app_hdr->src_app_id) !=18507					LOOPBACK_SRC_APPID) {18508				lpfc_printf_log(phba, KERN_WARNING,18509						LOG_ELS | LOG_LIBDFC,18510						"1932 Loopback src app id "18511						"not matched, app_id:x%x\n",18512						be32_to_cpu(fc_app_hdr->src_app_id));18513 18514				goto drop;18515			}18516		} else {18517			lpfc_printf_log(phba, KERN_WARNING,18518					LOG_ELS | LOG_LIBDFC,18519					"1933 Loopback df_ctl bit not set, "18520					"df_ctl:x%x\n",18521					fc_hdr->fh_df_ctl);18522 18523			goto drop;18524		}18525	}18526 18527	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,18528			"2538 Received frame rctl:x%x, type:x%x, "18529			"frame Data:%08x %08x %08x %08x %08x %08x %08x\n",18530			fc_hdr->fh_r_ctl, fc_hdr->fh_type,18531			be32_to_cpu(header[0]), be32_to_cpu(header[1]),18532			be32_to_cpu(header[2]), be32_to_cpu(header[3]),18533			be32_to_cpu(header[4]), be32_to_cpu(header[5]),18534			be32_to_cpu(header[6]));18535	return 0;18536drop:18537	lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,18538			"2539 Dropped frame rctl:x%x type:x%x\n",18539			fc_hdr->fh_r_ctl, fc_hdr->fh_type);18540	return 1;18541}18542 18543/**18544 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame18545 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)18546 *18547 * This function processes the FC header to retrieve the VFI from the VF18548 * header, if one exists. This function will return the VFI if one exists18549 * or 0 if no VSAN Header exists.18550 **/18551static uint32_t18552lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)18553{18554	struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;18555 18556	if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)18557		return 0;18558	return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);18559}18560 18561/**18562 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to18563 * @phba: Pointer to the HBA structure to search for the vport on18564 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)18565 * @fcfi: The FC Fabric ID that the frame came from18566 * @did: Destination ID to match against18567 *18568 * This function searches the @phba for a vport that matches the content of the18569 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the18570 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function18571 * returns the matching vport pointer or NULL if unable to match frame to a18572 * vport.18573 **/18574static struct lpfc_vport *18575lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,18576		       uint16_t fcfi, uint32_t did)18577{18578	struct lpfc_vport **vports;18579	struct lpfc_vport *vport = NULL;18580	int i;18581 18582	if (did == Fabric_DID)18583		return phba->pport;18584	if (test_bit(FC_PT2PT, &phba->pport->fc_flag) &&18585	    phba->link_state != LPFC_HBA_READY)18586		return phba->pport;18587 18588	vports = lpfc_create_vport_work_array(phba);18589	if (vports != NULL) {18590		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {18591			if (phba->fcf.fcfi == fcfi &&18592			    vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&18593			    vports[i]->fc_myDID == did) {18594				vport = vports[i];18595				break;18596			}18597		}18598	}18599	lpfc_destroy_vport_work_array(phba, vports);18600	return vport;18601}18602 18603/**18604 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp18605 * @vport: The vport to work on.18606 *18607 * This function updates the receive sequence time stamp for this vport. The18608 * receive sequence time stamp indicates the time that the last frame of the18609 * the sequence that has been idle for the longest amount of time was received.18610 * the driver uses this time stamp to indicate if any received sequences have18611 * timed out.18612 **/18613static void18614lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)18615{18616	struct lpfc_dmabuf *h_buf;18617	struct hbq_dmabuf *dmabuf = NULL;18618 18619	/* get the oldest sequence on the rcv list */18620	h_buf = list_get_first(&vport->rcv_buffer_list,18621			       struct lpfc_dmabuf, list);18622	if (!h_buf)18623		return;18624	dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);18625	vport->rcv_buffer_time_stamp = dmabuf->time_stamp;18626}18627 18628/**18629 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.18630 * @vport: The vport that the received sequences were sent to.18631 *18632 * This function cleans up all outstanding received sequences. This is called18633 * by the driver when a link event or user action invalidates all the received18634 * sequences.18635 **/18636void18637lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)18638{18639	struct lpfc_dmabuf *h_buf, *hnext;18640	struct lpfc_dmabuf *d_buf, *dnext;18641	struct hbq_dmabuf *dmabuf = NULL;18642 18643	/* start with the oldest sequence on the rcv list */18644	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {18645		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);18646		list_del_init(&dmabuf->hbuf.list);18647		list_for_each_entry_safe(d_buf, dnext,18648					 &dmabuf->dbuf.list, list) {18649			list_del_init(&d_buf->list);18650			lpfc_in_buf_free(vport->phba, d_buf);18651		}18652		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);18653	}18654}18655 18656/**18657 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.18658 * @vport: The vport that the received sequences were sent to.18659 *18660 * This function determines whether any received sequences have timed out by18661 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp18662 * indicates that there is at least one timed out sequence this routine will18663 * go through the received sequences one at a time from most inactive to most18664 * active to determine which ones need to be cleaned up. Once it has determined18665 * that a sequence needs to be cleaned up it will simply free up the resources18666 * without sending an abort.18667 **/18668void18669lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)18670{18671	struct lpfc_dmabuf *h_buf, *hnext;18672	struct lpfc_dmabuf *d_buf, *dnext;18673	struct hbq_dmabuf *dmabuf = NULL;18674	unsigned long timeout;18675	int abort_count = 0;18676 18677	timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +18678		   vport->rcv_buffer_time_stamp);18679	if (list_empty(&vport->rcv_buffer_list) ||18680	    time_before(jiffies, timeout))18681		return;18682	/* start with the oldest sequence on the rcv list */18683	list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {18684		dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);18685		timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +18686			   dmabuf->time_stamp);18687		if (time_before(jiffies, timeout))18688			break;18689		abort_count++;18690		list_del_init(&dmabuf->hbuf.list);18691		list_for_each_entry_safe(d_buf, dnext,18692					 &dmabuf->dbuf.list, list) {18693			list_del_init(&d_buf->list);18694			lpfc_in_buf_free(vport->phba, d_buf);18695		}18696		lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);18697	}18698	if (abort_count)18699		lpfc_update_rcv_time_stamp(vport);18700}18701 18702/**18703 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences18704 * @vport: pointer to a vitural port18705 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame18706 *18707 * This function searches through the existing incomplete sequences that have18708 * been sent to this @vport. If the frame matches one of the incomplete18709 * sequences then the dbuf in the @dmabuf is added to the list of frames that18710 * make up that sequence. If no sequence is found that matches this frame then18711 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list18712 * This function returns a pointer to the first dmabuf in the sequence list that18713 * the frame was linked to.18714 **/18715static struct hbq_dmabuf *18716lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)18717{18718	struct fc_frame_header *new_hdr;18719	struct fc_frame_header *temp_hdr;18720	struct lpfc_dmabuf *d_buf;18721	struct lpfc_dmabuf *h_buf;18722	struct hbq_dmabuf *seq_dmabuf = NULL;18723	struct hbq_dmabuf *temp_dmabuf = NULL;18724	uint8_t	found = 0;18725 18726	INIT_LIST_HEAD(&dmabuf->dbuf.list);18727	dmabuf->time_stamp = jiffies;18728	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;18729 18730	/* Use the hdr_buf to find the sequence that this frame belongs to */18731	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {18732		temp_hdr = (struct fc_frame_header *)h_buf->virt;18733		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||18734		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||18735		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))18736			continue;18737		/* found a pending sequence that matches this frame */18738		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);18739		break;18740	}18741	if (!seq_dmabuf) {18742		/*18743		 * This indicates first frame received for this sequence.18744		 * Queue the buffer on the vport's rcv_buffer_list.18745		 */18746		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);18747		lpfc_update_rcv_time_stamp(vport);18748		return dmabuf;18749	}18750	temp_hdr = seq_dmabuf->hbuf.virt;18751	if (be16_to_cpu(new_hdr->fh_seq_cnt) <18752		be16_to_cpu(temp_hdr->fh_seq_cnt)) {18753		list_del_init(&seq_dmabuf->hbuf.list);18754		list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);18755		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);18756		lpfc_update_rcv_time_stamp(vport);18757		return dmabuf;18758	}18759	/* move this sequence to the tail to indicate a young sequence */18760	list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);18761	seq_dmabuf->time_stamp = jiffies;18762	lpfc_update_rcv_time_stamp(vport);18763	if (list_empty(&seq_dmabuf->dbuf.list)) {18764		list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);18765		return seq_dmabuf;18766	}18767	/* find the correct place in the sequence to insert this frame */18768	d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);18769	while (!found) {18770		temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);18771		temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;18772		/*18773		 * If the frame's sequence count is greater than the frame on18774		 * the list then insert the frame right after this frame18775		 */18776		if (be16_to_cpu(new_hdr->fh_seq_cnt) >18777			be16_to_cpu(temp_hdr->fh_seq_cnt)) {18778			list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);18779			found = 1;18780			break;18781		}18782 18783		if (&d_buf->list == &seq_dmabuf->dbuf.list)18784			break;18785		d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);18786	}18787 18788	if (found)18789		return seq_dmabuf;18790	return NULL;18791}18792 18793/**18794 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence18795 * @vport: pointer to a vitural port18796 * @dmabuf: pointer to a dmabuf that describes the FC sequence18797 *18798 * This function tries to abort from the partially assembed sequence, described18799 * by the information from basic abbort @dmabuf. It checks to see whether such18800 * partially assembled sequence held by the driver. If so, it shall free up all18801 * the frames from the partially assembled sequence.18802 *18803 * Return18804 * true  -- if there is matching partially assembled sequence present and all18805 *          the frames freed with the sequence;18806 * false -- if there is no matching partially assembled sequence present so18807 *          nothing got aborted in the lower layer driver18808 **/18809static bool18810lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,18811			    struct hbq_dmabuf *dmabuf)18812{18813	struct fc_frame_header *new_hdr;18814	struct fc_frame_header *temp_hdr;18815	struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;18816	struct hbq_dmabuf *seq_dmabuf = NULL;18817 18818	/* Use the hdr_buf to find the sequence that matches this frame */18819	INIT_LIST_HEAD(&dmabuf->dbuf.list);18820	INIT_LIST_HEAD(&dmabuf->hbuf.list);18821	new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;18822	list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {18823		temp_hdr = (struct fc_frame_header *)h_buf->virt;18824		if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||18825		    (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||18826		    (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))18827			continue;18828		/* found a pending sequence that matches this frame */18829		seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);18830		break;18831	}18832 18833	/* Free up all the frames from the partially assembled sequence */18834	if (seq_dmabuf) {18835		list_for_each_entry_safe(d_buf, n_buf,18836					 &seq_dmabuf->dbuf.list, list) {18837			list_del_init(&d_buf->list);18838			lpfc_in_buf_free(vport->phba, d_buf);18839		}18840		return true;18841	}18842	return false;18843}18844 18845/**18846 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp18847 * @vport: pointer to a vitural port18848 * @dmabuf: pointer to a dmabuf that describes the FC sequence18849 *18850 * This function tries to abort from the assembed sequence from upper level18851 * protocol, described by the information from basic abbort @dmabuf. It18852 * checks to see whether such pending context exists at upper level protocol.18853 * If so, it shall clean up the pending context.18854 *18855 * Return18856 * true  -- if there is matching pending context of the sequence cleaned18857 *          at ulp;18858 * false -- if there is no matching pending context of the sequence present18859 *          at ulp.18860 **/18861static bool18862lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)18863{18864	struct lpfc_hba *phba = vport->phba;18865	int handled;18866 18867	/* Accepting abort at ulp with SLI4 only */18868	if (phba->sli_rev < LPFC_SLI_REV4)18869		return false;18870 18871	/* Register all caring upper level protocols to attend abort */18872	handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);18873	if (handled)18874		return true;18875 18876	return false;18877}18878 18879/**18880 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler18881 * @phba: Pointer to HBA context object.18882 * @cmd_iocbq: pointer to the command iocbq structure.18883 * @rsp_iocbq: pointer to the response iocbq structure.18884 *18885 * This function handles the sequence abort response iocb command complete18886 * event. It properly releases the memory allocated to the sequence abort18887 * accept iocb.18888 **/18889static void18890lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,18891			     struct lpfc_iocbq *cmd_iocbq,18892			     struct lpfc_iocbq *rsp_iocbq)18893{18894	if (cmd_iocbq) {18895		lpfc_nlp_put(cmd_iocbq->ndlp);18896		lpfc_sli_release_iocbq(phba, cmd_iocbq);18897	}18898 18899	/* Failure means BLS ABORT RSP did not get delivered to remote node*/18900	if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)18901		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,18902			"3154 BLS ABORT RSP failed, data:  x%x/x%x\n",18903			get_job_ulpstatus(phba, rsp_iocbq),18904			get_job_word4(phba, rsp_iocbq));18905}18906 18907/**18908 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.18909 * @phba: Pointer to HBA context object.18910 * @xri: xri id in transaction.18911 *18912 * This function validates the xri maps to the known range of XRIs allocated an18913 * used by the driver.18914 **/18915uint16_t18916lpfc_sli4_xri_inrange(struct lpfc_hba *phba,18917		      uint16_t xri)18918{18919	uint16_t i;18920 18921	for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {18922		if (xri == phba->sli4_hba.xri_ids[i])18923			return i;18924	}18925	return NO_XRI;18926}18927 18928/**18929 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort18930 * @vport: pointer to a virtual port.18931 * @fc_hdr: pointer to a FC frame header.18932 * @aborted: was the partially assembled receive sequence successfully aborted18933 *18934 * This function sends a basic response to a previous unsol sequence abort18935 * event after aborting the sequence handling.18936 **/18937void18938lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,18939			struct fc_frame_header *fc_hdr, bool aborted)18940{18941	struct lpfc_hba *phba = vport->phba;18942	struct lpfc_iocbq *ctiocb = NULL;18943	struct lpfc_nodelist *ndlp;18944	uint16_t oxid, rxid, xri, lxri;18945	uint32_t sid, fctl;18946	union lpfc_wqe128 *icmd;18947	int rc;18948 18949	if (!lpfc_is_link_up(phba))18950		return;18951 18952	sid = sli4_sid_from_fc_hdr(fc_hdr);18953	oxid = be16_to_cpu(fc_hdr->fh_ox_id);18954	rxid = be16_to_cpu(fc_hdr->fh_rx_id);18955 18956	ndlp = lpfc_findnode_did(vport, sid);18957	if (!ndlp) {18958		ndlp = lpfc_nlp_init(vport, sid);18959		if (!ndlp) {18960			lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,18961					 "1268 Failed to allocate ndlp for "18962					 "oxid:x%x SID:x%x\n", oxid, sid);18963			return;18964		}18965		/* Put ndlp onto vport node list */18966		lpfc_enqueue_node(vport, ndlp);18967	}18968 18969	/* Allocate buffer for rsp iocb */18970	ctiocb = lpfc_sli_get_iocbq(phba);18971	if (!ctiocb)18972		return;18973 18974	icmd = &ctiocb->wqe;18975 18976	/* Extract the F_CTL field from FC_HDR */18977	fctl = sli4_fctl_from_fc_hdr(fc_hdr);18978 18979	ctiocb->ndlp = lpfc_nlp_get(ndlp);18980	if (!ctiocb->ndlp) {18981		lpfc_sli_release_iocbq(phba, ctiocb);18982		return;18983	}18984 18985	ctiocb->vport = vport;18986	ctiocb->cmd_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;18987	ctiocb->sli4_lxritag = NO_XRI;18988	ctiocb->sli4_xritag = NO_XRI;18989	ctiocb->abort_rctl = FC_RCTL_BA_ACC;18990 18991	if (fctl & FC_FC_EX_CTX)18992		/* Exchange responder sent the abort so we18993		 * own the oxid.18994		 */18995		xri = oxid;18996	else18997		xri = rxid;18998	lxri = lpfc_sli4_xri_inrange(phba, xri);18999	if (lxri != NO_XRI)19000		lpfc_set_rrq_active(phba, ndlp, lxri,19001			(xri == oxid) ? rxid : oxid, 0);19002	/* For BA_ABTS from exchange responder, if the logical xri with19003	 * the oxid maps to the FCP XRI range, the port no longer has19004	 * that exchange context, send a BLS_RJT. Override the IOCB for19005	 * a BA_RJT.19006	 */19007	if ((fctl & FC_FC_EX_CTX) &&19008	    (lxri > lpfc_sli4_get_iocb_cnt(phba))) {19009		ctiocb->abort_rctl = FC_RCTL_BA_RJT;19010		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);19011		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,19012		       FC_BA_RJT_INV_XID);19013		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,19014		       FC_BA_RJT_UNABLE);19015	}19016 19017	/* If BA_ABTS failed to abort a partially assembled receive sequence,19018	 * the driver no longer has that exchange, send a BLS_RJT. Override19019	 * the IOCB for a BA_RJT.19020	 */19021	if (aborted == false) {19022		ctiocb->abort_rctl = FC_RCTL_BA_RJT;19023		bf_set(xmit_bls_rsp64_rjt_vspec, &icmd->xmit_bls_rsp, 0);19024		bf_set(xmit_bls_rsp64_rjt_expc, &icmd->xmit_bls_rsp,19025		       FC_BA_RJT_INV_XID);19026		bf_set(xmit_bls_rsp64_rjt_rsnc, &icmd->xmit_bls_rsp,19027		       FC_BA_RJT_UNABLE);19028	}19029 19030	if (fctl & FC_FC_EX_CTX) {19031		/* ABTS sent by responder to CT exchange, construction19032		 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG19033		 * field and RX_ID from ABTS for RX_ID field.19034		 */19035		ctiocb->abort_bls = LPFC_ABTS_UNSOL_RSP;19036		bf_set(xmit_bls_rsp64_rxid, &icmd->xmit_bls_rsp, rxid);19037	} else {19038		/* ABTS sent by initiator to CT exchange, construction19039		 * of BA_ACC will need to allocate a new XRI as for the19040		 * XRI_TAG field.19041		 */19042		ctiocb->abort_bls = LPFC_ABTS_UNSOL_INT;19043	}19044 19045	/* OX_ID is invariable to who sent ABTS to CT exchange */19046	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, oxid);19047	bf_set(xmit_bls_rsp64_oxid, &icmd->xmit_bls_rsp, rxid);19048 19049	/* Use CT=VPI */19050	bf_set(wqe_els_did, &icmd->xmit_bls_rsp.wqe_dest,19051	       ndlp->nlp_DID);19052	bf_set(xmit_bls_rsp64_temprpi, &icmd->xmit_bls_rsp,19053	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);19054	bf_set(wqe_cmnd, &icmd->generic.wqe_com, CMD_XMIT_BLS_RSP64_CX);19055 19056	/* Xmit CT abts response on exchange <xid> */19057	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,19058			 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",19059			 ctiocb->abort_rctl, oxid, phba->link_state);19060 19061	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);19062	if (rc == IOCB_ERROR) {19063		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,19064				 "2925 Failed to issue CT ABTS RSP x%x on "19065				 "xri x%x, Data x%x\n",19066				 ctiocb->abort_rctl, oxid,19067				 phba->link_state);19068		lpfc_nlp_put(ndlp);19069		ctiocb->ndlp = NULL;19070		lpfc_sli_release_iocbq(phba, ctiocb);19071	}19072 19073	/* if only usage of this nodelist is BLS response, release initial ref19074	 * to free ndlp when transmit completes19075	 */19076	if (ndlp->nlp_state == NLP_STE_UNUSED_NODE &&19077	    !(ndlp->nlp_flag & NLP_DROPPED) &&19078	    !(ndlp->fc4_xpt_flags & (NVME_XPT_REGD | SCSI_XPT_REGD))) {19079		ndlp->nlp_flag |= NLP_DROPPED;19080		lpfc_nlp_put(ndlp);19081	}19082}19083 19084/**19085 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event19086 * @vport: Pointer to the vport on which this sequence was received19087 * @dmabuf: pointer to a dmabuf that describes the FC sequence19088 *19089 * This function handles an SLI-4 unsolicited abort event. If the unsolicited19090 * receive sequence is only partially assembed by the driver, it shall abort19091 * the partially assembled frames for the sequence. Otherwise, if the19092 * unsolicited receive sequence has been completely assembled and passed to19093 * the Upper Layer Protocol (ULP), it then mark the per oxid status for the19094 * unsolicited sequence has been aborted. After that, it will issue a basic19095 * accept to accept the abort.19096 **/19097static void19098lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,19099			     struct hbq_dmabuf *dmabuf)19100{19101	struct lpfc_hba *phba = vport->phba;19102	struct fc_frame_header fc_hdr;19103	uint32_t fctl;19104	bool aborted;19105 19106	/* Make a copy of fc_hdr before the dmabuf being released */19107	memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));19108	fctl = sli4_fctl_from_fc_hdr(&fc_hdr);19109 19110	if (fctl & FC_FC_EX_CTX) {19111		/* ABTS by responder to exchange, no cleanup needed */19112		aborted = true;19113	} else {19114		/* ABTS by initiator to exchange, need to do cleanup */19115		aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);19116		if (aborted == false)19117			aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);19118	}19119	lpfc_in_buf_free(phba, &dmabuf->dbuf);19120 19121	if (phba->nvmet_support) {19122		lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);19123		return;19124	}19125 19126	/* Respond with BA_ACC or BA_RJT accordingly */19127	lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);19128}19129 19130/**19131 * lpfc_seq_complete - Indicates if a sequence is complete19132 * @dmabuf: pointer to a dmabuf that describes the FC sequence19133 *19134 * This function checks the sequence, starting with the frame described by19135 * @dmabuf, to see if all the frames associated with this sequence are present.19136 * the frames associated with this sequence are linked to the @dmabuf using the19137 * dbuf list. This function looks for two major things. 1) That the first frame19138 * has a sequence count of zero. 2) There is a frame with last frame of sequence19139 * set. 3) That there are no holes in the sequence count. The function will19140 * return 1 when the sequence is complete, otherwise it will return 0.19141 **/19142static int19143lpfc_seq_complete(struct hbq_dmabuf *dmabuf)19144{19145	struct fc_frame_header *hdr;19146	struct lpfc_dmabuf *d_buf;19147	struct hbq_dmabuf *seq_dmabuf;19148	uint32_t fctl;19149	int seq_count = 0;19150 19151	hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;19152	/* make sure first fame of sequence has a sequence count of zero */19153	if (hdr->fh_seq_cnt != seq_count)19154		return 0;19155	fctl = (hdr->fh_f_ctl[0] << 16 |19156		hdr->fh_f_ctl[1] << 8 |19157		hdr->fh_f_ctl[2]);19158	/* If last frame of sequence we can return success. */19159	if (fctl & FC_FC_END_SEQ)19160		return 1;19161	list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {19162		seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);19163		hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;19164		/* If there is a hole in the sequence count then fail. */19165		if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))19166			return 0;19167		fctl = (hdr->fh_f_ctl[0] << 16 |19168			hdr->fh_f_ctl[1] << 8 |19169			hdr->fh_f_ctl[2]);19170		/* If last frame of sequence we can return success. */19171		if (fctl & FC_FC_END_SEQ)19172			return 1;19173	}19174	return 0;19175}19176 19177/**19178 * lpfc_prep_seq - Prep sequence for ULP processing19179 * @vport: Pointer to the vport on which this sequence was received19180 * @seq_dmabuf: pointer to a dmabuf that describes the FC sequence19181 *19182 * This function takes a sequence, described by a list of frames, and creates19183 * a list of iocbq structures to describe the sequence. This iocbq list will be19184 * used to issue to the generic unsolicited sequence handler. This routine19185 * returns a pointer to the first iocbq in the list. If the function is unable19186 * to allocate an iocbq then it throw out the received frames that were not19187 * able to be described and return a pointer to the first iocbq. If unable to19188 * allocate any iocbqs (including the first) this function will return NULL.19189 **/19190static struct lpfc_iocbq *19191lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)19192{19193	struct hbq_dmabuf *hbq_buf;19194	struct lpfc_dmabuf *d_buf, *n_buf;19195	struct lpfc_iocbq *first_iocbq, *iocbq;19196	struct fc_frame_header *fc_hdr;19197	uint32_t sid;19198	uint32_t len, tot_len;19199 19200	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;19201	/* remove from receive buffer list */19202	list_del_init(&seq_dmabuf->hbuf.list);19203	lpfc_update_rcv_time_stamp(vport);19204	/* get the Remote Port's SID */19205	sid = sli4_sid_from_fc_hdr(fc_hdr);19206	tot_len = 0;19207	/* Get an iocbq struct to fill in. */19208	first_iocbq = lpfc_sli_get_iocbq(vport->phba);19209	if (first_iocbq) {19210		/* Initialize the first IOCB. */19211		first_iocbq->wcqe_cmpl.total_data_placed = 0;19212		bf_set(lpfc_wcqe_c_status, &first_iocbq->wcqe_cmpl,19213		       IOSTAT_SUCCESS);19214		first_iocbq->vport = vport;19215 19216		/* Check FC Header to see what TYPE of frame we are rcv'ing */19217		if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {19218			bf_set(els_rsp64_sid, &first_iocbq->wqe.xmit_els_rsp,19219			       sli4_did_from_fc_hdr(fc_hdr));19220		}19221 19222		bf_set(wqe_ctxt_tag, &first_iocbq->wqe.xmit_els_rsp.wqe_com,19223		       NO_XRI);19224		bf_set(wqe_rcvoxid, &first_iocbq->wqe.xmit_els_rsp.wqe_com,19225		       be16_to_cpu(fc_hdr->fh_ox_id));19226 19227		/* put the first buffer into the first iocb */19228		tot_len = bf_get(lpfc_rcqe_length,19229				 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);19230 19231		first_iocbq->cmd_dmabuf = &seq_dmabuf->dbuf;19232		first_iocbq->bpl_dmabuf = NULL;19233		/* Keep track of the BDE count */19234		first_iocbq->wcqe_cmpl.word3 = 1;19235 19236		if (tot_len > LPFC_DATA_BUF_SIZE)19237			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize =19238				LPFC_DATA_BUF_SIZE;19239		else19240			first_iocbq->wqe.gen_req.bde.tus.f.bdeSize = tot_len;19241 19242		first_iocbq->wcqe_cmpl.total_data_placed = tot_len;19243		bf_set(wqe_els_did, &first_iocbq->wqe.xmit_els_rsp.wqe_dest,19244		       sid);19245	}19246	iocbq = first_iocbq;19247	/*19248	 * Each IOCBq can have two Buffers assigned, so go through the list19249	 * of buffers for this sequence and save two buffers in each IOCBq19250	 */19251	list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {19252		if (!iocbq) {19253			lpfc_in_buf_free(vport->phba, d_buf);19254			continue;19255		}19256		if (!iocbq->bpl_dmabuf) {19257			iocbq->bpl_dmabuf = d_buf;19258			iocbq->wcqe_cmpl.word3++;19259			/* We need to get the size out of the right CQE */19260			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);19261			len = bf_get(lpfc_rcqe_length,19262				       &hbq_buf->cq_event.cqe.rcqe_cmpl);19263			iocbq->unsol_rcv_len = len;19264			iocbq->wcqe_cmpl.total_data_placed += len;19265			tot_len += len;19266		} else {19267			iocbq = lpfc_sli_get_iocbq(vport->phba);19268			if (!iocbq) {19269				if (first_iocbq) {19270					bf_set(lpfc_wcqe_c_status,19271					       &first_iocbq->wcqe_cmpl,19272					       IOSTAT_SUCCESS);19273					first_iocbq->wcqe_cmpl.parameter =19274						IOERR_NO_RESOURCES;19275				}19276				lpfc_in_buf_free(vport->phba, d_buf);19277				continue;19278			}19279			/* We need to get the size out of the right CQE */19280			hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);19281			len = bf_get(lpfc_rcqe_length,19282				       &hbq_buf->cq_event.cqe.rcqe_cmpl);19283			iocbq->cmd_dmabuf = d_buf;19284			iocbq->bpl_dmabuf = NULL;19285			iocbq->wcqe_cmpl.word3 = 1;19286 19287			if (len > LPFC_DATA_BUF_SIZE)19288				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =19289					LPFC_DATA_BUF_SIZE;19290			else19291				iocbq->wqe.xmit_els_rsp.bde.tus.f.bdeSize =19292					len;19293 19294			tot_len += len;19295			iocbq->wcqe_cmpl.total_data_placed = tot_len;19296			bf_set(wqe_els_did, &iocbq->wqe.xmit_els_rsp.wqe_dest,19297			       sid);19298			list_add_tail(&iocbq->list, &first_iocbq->list);19299		}19300	}19301	/* Free the sequence's header buffer */19302	if (!first_iocbq)19303		lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);19304 19305	return first_iocbq;19306}19307 19308static void19309lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,19310			  struct hbq_dmabuf *seq_dmabuf)19311{19312	struct fc_frame_header *fc_hdr;19313	struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;19314	struct lpfc_hba *phba = vport->phba;19315 19316	fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;19317	iocbq = lpfc_prep_seq(vport, seq_dmabuf);19318	if (!iocbq) {19319		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,19320				"2707 Ring %d handler: Failed to allocate "19321				"iocb Rctl x%x Type x%x received\n",19322				LPFC_ELS_RING,19323				fc_hdr->fh_r_ctl, fc_hdr->fh_type);19324		return;19325	}19326	if (!lpfc_complete_unsol_iocb(phba,19327				      phba->sli4_hba.els_wq->pring,19328				      iocbq, fc_hdr->fh_r_ctl,19329				      fc_hdr->fh_type)) {19330		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,19331				"2540 Ring %d handler: unexpected Rctl "19332				"x%x Type x%x received\n",19333				LPFC_ELS_RING,19334				fc_hdr->fh_r_ctl, fc_hdr->fh_type);19335		lpfc_in_buf_free(phba, &seq_dmabuf->dbuf);19336	}19337 19338	/* Free iocb created in lpfc_prep_seq */19339	list_for_each_entry_safe(curr_iocb, next_iocb,19340				 &iocbq->list, list) {19341		list_del_init(&curr_iocb->list);19342		lpfc_sli_release_iocbq(phba, curr_iocb);19343	}19344	lpfc_sli_release_iocbq(phba, iocbq);19345}19346 19347static void19348lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,19349			    struct lpfc_iocbq *rspiocb)19350{19351	struct lpfc_dmabuf *pcmd = cmdiocb->cmd_dmabuf;19352 19353	if (pcmd && pcmd->virt)19354		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);19355	kfree(pcmd);19356	lpfc_sli_release_iocbq(phba, cmdiocb);19357	lpfc_drain_txq(phba);19358}19359 19360static void19361lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,19362			      struct hbq_dmabuf *dmabuf)19363{19364	struct fc_frame_header *fc_hdr;19365	struct lpfc_hba *phba = vport->phba;19366	struct lpfc_iocbq *iocbq = NULL;19367	union  lpfc_wqe128 *pwqe;19368	struct lpfc_dmabuf *pcmd = NULL;19369	uint32_t frame_len;19370	int rc;19371	unsigned long iflags;19372 19373	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;19374	frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);19375 19376	/* Send the received frame back */19377	iocbq = lpfc_sli_get_iocbq(phba);19378	if (!iocbq) {19379		/* Queue cq event and wakeup worker thread to process it */19380		spin_lock_irqsave(&phba->hbalock, iflags);19381		list_add_tail(&dmabuf->cq_event.list,19382			      &phba->sli4_hba.sp_queue_event);19383		spin_unlock_irqrestore(&phba->hbalock, iflags);19384		set_bit(HBA_SP_QUEUE_EVT, &phba->hba_flag);19385		lpfc_worker_wake_up(phba);19386		return;19387	}19388 19389	/* Allocate buffer for command payload */19390	pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);19391	if (pcmd)19392		pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,19393					    &pcmd->phys);19394	if (!pcmd || !pcmd->virt)19395		goto exit;19396 19397	INIT_LIST_HEAD(&pcmd->list);19398 19399	/* copyin the payload */19400	memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);19401 19402	iocbq->cmd_dmabuf = pcmd;19403	iocbq->vport = vport;19404	iocbq->cmd_flag &= ~LPFC_FIP_ELS_ID_MASK;19405	iocbq->cmd_flag |= LPFC_USE_FCPWQIDX;19406	iocbq->num_bdes = 0;19407 19408	pwqe = &iocbq->wqe;19409	/* fill in BDE's for command */19410	pwqe->gen_req.bde.addrHigh = putPaddrHigh(pcmd->phys);19411	pwqe->gen_req.bde.addrLow = putPaddrLow(pcmd->phys);19412	pwqe->gen_req.bde.tus.f.bdeSize = frame_len;19413	pwqe->gen_req.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;19414 19415	pwqe->send_frame.frame_len = frame_len;19416	pwqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((__be32 *)fc_hdr));19417	pwqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((__be32 *)fc_hdr + 1));19418	pwqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((__be32 *)fc_hdr + 2));19419	pwqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((__be32 *)fc_hdr + 3));19420	pwqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((__be32 *)fc_hdr + 4));19421	pwqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((__be32 *)fc_hdr + 5));19422 19423	pwqe->generic.wqe_com.word7 = 0;19424	pwqe->generic.wqe_com.word10 = 0;19425 19426	bf_set(wqe_cmnd, &pwqe->generic.wqe_com, CMD_SEND_FRAME);19427	bf_set(wqe_sof, &pwqe->generic.wqe_com, 0x2E); /* SOF byte */19428	bf_set(wqe_eof, &pwqe->generic.wqe_com, 0x41); /* EOF byte */19429	bf_set(wqe_lenloc, &pwqe->generic.wqe_com, 1);19430	bf_set(wqe_xbl, &pwqe->generic.wqe_com, 1);19431	bf_set(wqe_dbde, &pwqe->generic.wqe_com, 1);19432	bf_set(wqe_xc, &pwqe->generic.wqe_com, 1);19433	bf_set(wqe_cmd_type, &pwqe->generic.wqe_com, 0xA);19434	bf_set(wqe_cqid, &pwqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);19435	bf_set(wqe_xri_tag, &pwqe->generic.wqe_com, iocbq->sli4_xritag);19436	bf_set(wqe_reqtag, &pwqe->generic.wqe_com, iocbq->iotag);19437	bf_set(wqe_class, &pwqe->generic.wqe_com, CLASS3);19438	pwqe->generic.wqe_com.abort_tag = iocbq->iotag;19439 19440	iocbq->cmd_cmpl = lpfc_sli4_mds_loopback_cmpl;19441 19442	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);19443	if (rc == IOCB_ERROR)19444		goto exit;19445 19446	lpfc_in_buf_free(phba, &dmabuf->dbuf);19447	return;19448 19449exit:19450	lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,19451			"2023 Unable to process MDS loopback frame\n");19452	if (pcmd && pcmd->virt)19453		dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);19454	kfree(pcmd);19455	if (iocbq)19456		lpfc_sli_release_iocbq(phba, iocbq);19457	lpfc_in_buf_free(phba, &dmabuf->dbuf);19458}19459 19460/**19461 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware19462 * @phba: Pointer to HBA context object.19463 * @dmabuf: Pointer to a dmabuf that describes the FC sequence.19464 *19465 * This function is called with no lock held. This function processes all19466 * the received buffers and gives it to upper layers when a received buffer19467 * indicates that it is the final frame in the sequence. The interrupt19468 * service routine processes received buffers at interrupt contexts.19469 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the19470 * appropriate receive function when the final frame in a sequence is received.19471 **/19472void19473lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,19474				 struct hbq_dmabuf *dmabuf)19475{19476	struct hbq_dmabuf *seq_dmabuf;19477	struct fc_frame_header *fc_hdr;19478	struct lpfc_vport *vport;19479	uint32_t fcfi;19480	uint32_t did;19481 19482	/* Process each received buffer */19483	fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;19484 19485	if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||19486	    fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {19487		vport = phba->pport;19488		/* Handle MDS Loopback frames */19489		if  (!test_bit(FC_UNLOADING, &phba->pport->load_flag))19490			lpfc_sli4_handle_mds_loopback(vport, dmabuf);19491		else19492			lpfc_in_buf_free(phba, &dmabuf->dbuf);19493		return;19494	}19495 19496	/* check to see if this a valid type of frame */19497	if (lpfc_fc_frame_check(phba, fc_hdr)) {19498		lpfc_in_buf_free(phba, &dmabuf->dbuf);19499		return;19500	}19501 19502	if ((bf_get(lpfc_cqe_code,19503		    &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))19504		fcfi = bf_get(lpfc_rcqe_fcf_id_v1,19505			      &dmabuf->cq_event.cqe.rcqe_cmpl);19506	else19507		fcfi = bf_get(lpfc_rcqe_fcf_id,19508			      &dmabuf->cq_event.cqe.rcqe_cmpl);19509 19510	if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {19511		vport = phba->pport;19512		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,19513				"2023 MDS Loopback %d bytes\n",19514				bf_get(lpfc_rcqe_length,19515				       &dmabuf->cq_event.cqe.rcqe_cmpl));19516		/* Handle MDS Loopback frames */19517		lpfc_sli4_handle_mds_loopback(vport, dmabuf);19518		return;19519	}19520 19521	/* d_id this frame is directed to */19522	did = sli4_did_from_fc_hdr(fc_hdr);19523 19524	vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);19525	if (!vport) {19526		/* throw out the frame */19527		lpfc_in_buf_free(phba, &dmabuf->dbuf);19528		return;19529	}19530 19531	/* vport is registered unless we rcv a FLOGI directed to Fabric_DID */19532	if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&19533		(did != Fabric_DID)) {19534		/*19535		 * Throw out the frame if we are not pt2pt.19536		 * The pt2pt protocol allows for discovery frames19537		 * to be received without a registered VPI.19538		 */19539		if (!test_bit(FC_PT2PT, &vport->fc_flag) ||19540		    phba->link_state == LPFC_HBA_READY) {19541			lpfc_in_buf_free(phba, &dmabuf->dbuf);19542			return;19543		}19544	}19545 19546	/* Handle the basic abort sequence (BA_ABTS) event */19547	if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {19548		lpfc_sli4_handle_unsol_abort(vport, dmabuf);19549		return;19550	}19551 19552	/* Link this frame */19553	seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);19554	if (!seq_dmabuf) {19555		/* unable to add frame to vport - throw it out */19556		lpfc_in_buf_free(phba, &dmabuf->dbuf);19557		return;19558	}19559	/* If not last frame in sequence continue processing frames. */19560	if (!lpfc_seq_complete(seq_dmabuf))19561		return;19562 19563	/* Send the complete sequence to the upper layer protocol */19564	lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);19565}19566 19567/**19568 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port19569 * @phba: pointer to lpfc hba data structure.19570 *19571 * This routine is invoked to post rpi header templates to the19572 * HBA consistent with the SLI-4 interface spec.  This routine19573 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to19574 * SLI4_PAGE_SIZE modulo 64 rpi context headers.19575 *19576 * This routine does not require any locks.  It's usage is expected19577 * to be driver load or reset recovery when the driver is19578 * sequential.19579 *19580 * Return codes19581 * 	0 - successful19582 *      -EIO - The mailbox failed to complete successfully.19583 * 	When this error occurs, the driver is not guaranteed19584 *	to have any rpi regions posted to the device and19585 *	must either attempt to repost the regions or take a19586 *	fatal error.19587 **/19588int19589lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)19590{19591	struct lpfc_rpi_hdr *rpi_page;19592	uint32_t rc = 0;19593	uint16_t lrpi = 0;19594 19595	/* SLI4 ports that support extents do not require RPI headers. */19596	if (!phba->sli4_hba.rpi_hdrs_in_use)19597		goto exit;19598	if (phba->sli4_hba.extents_in_use)19599		return -EIO;19600 19601	list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {19602		/*19603		 * Assign the rpi headers a physical rpi only if the driver19604		 * has not initialized those resources.  A port reset only19605		 * needs the headers posted.19606		 */19607		if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=19608		    LPFC_RPI_RSRC_RDY)19609			rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];19610 19611		rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);19612		if (rc != MBX_SUCCESS) {19613			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,19614					"2008 Error %d posting all rpi "19615					"headers\n", rc);19616			rc = -EIO;19617			break;19618		}19619	}19620 19621 exit:19622	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,19623	       LPFC_RPI_RSRC_RDY);19624	return rc;19625}19626 19627/**19628 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port19629 * @phba: pointer to lpfc hba data structure.19630 * @rpi_page:  pointer to the rpi memory region.19631 *19632 * This routine is invoked to post a single rpi header to the19633 * HBA consistent with the SLI-4 interface spec.  This memory region19634 * maps up to 64 rpi context regions.19635 *19636 * Return codes19637 * 	0 - successful19638 * 	-ENOMEM - No available memory19639 *      -EIO - The mailbox failed to complete successfully.19640 **/19641int19642lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)19643{19644	LPFC_MBOXQ_t *mboxq;19645	struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;19646	uint32_t rc = 0;19647	uint32_t shdr_status, shdr_add_status;19648	union lpfc_sli4_cfg_shdr *shdr;19649 19650	/* SLI4 ports that support extents do not require RPI headers. */19651	if (!phba->sli4_hba.rpi_hdrs_in_use)19652		return rc;19653	if (phba->sli4_hba.extents_in_use)19654		return -EIO;19655 19656	/* The port is notified of the header region via a mailbox command. */19657	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);19658	if (!mboxq) {19659		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,19660				"2001 Unable to allocate memory for issuing "19661				"SLI_CONFIG_SPECIAL mailbox command\n");19662		return -ENOMEM;19663	}19664 19665	/* Post all rpi memory regions to the port. */19666	hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;19667	lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,19668			 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,19669			 sizeof(struct lpfc_mbx_post_hdr_tmpl) -19670			 sizeof(struct lpfc_sli4_cfg_mhdr),19671			 LPFC_SLI4_MBX_EMBED);19672 19673 19674	/* Post the physical rpi to the port for this rpi header. */19675	bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,19676	       rpi_page->start_rpi);19677	bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,19678	       hdr_tmpl, rpi_page->page_count);19679 19680	hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);19681	hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);19682	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);19683	shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;19684	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);19685	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);19686	mempool_free(mboxq, phba->mbox_mem_pool);19687	if (shdr_status || shdr_add_status || rc) {19688		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,19689				"2514 POST_RPI_HDR mailbox failed with "19690				"status x%x add_status x%x, mbx status x%x\n",19691				shdr_status, shdr_add_status, rc);19692		rc = -ENXIO;19693	} else {19694		/*19695		 * The next_rpi stores the next logical module-64 rpi value used19696		 * to post physical rpis in subsequent rpi postings.19697		 */19698		spin_lock_irq(&phba->hbalock);19699		phba->sli4_hba.next_rpi = rpi_page->next_rpi;19700		spin_unlock_irq(&phba->hbalock);19701	}19702	return rc;19703}19704 19705/**19706 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range19707 * @phba: pointer to lpfc hba data structure.19708 *19709 * This routine is invoked to post rpi header templates to the19710 * HBA consistent with the SLI-4 interface spec.  This routine19711 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to19712 * SLI4_PAGE_SIZE modulo 64 rpi context headers.19713 *19714 * Returns19715 * 	A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful19716 * 	LPFC_RPI_ALLOC_ERROR if no rpis are available.19717 **/19718int19719lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)19720{19721	unsigned long rpi;19722	uint16_t max_rpi, rpi_limit;19723	uint16_t rpi_remaining, lrpi = 0;19724	struct lpfc_rpi_hdr *rpi_hdr;19725	unsigned long iflag;19726 19727	/*19728	 * Fetch the next logical rpi.  Because this index is logical,19729	 * the  driver starts at 0 each time.19730	 */19731	spin_lock_irqsave(&phba->hbalock, iflag);19732	max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;19733	rpi_limit = phba->sli4_hba.next_rpi;19734 19735	rpi = find_first_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit);19736	if (rpi >= rpi_limit)19737		rpi = LPFC_RPI_ALLOC_ERROR;19738	else {19739		set_bit(rpi, phba->sli4_hba.rpi_bmask);19740		phba->sli4_hba.max_cfg_param.rpi_used++;19741		phba->sli4_hba.rpi_count++;19742	}19743	lpfc_printf_log(phba, KERN_INFO,19744			LOG_NODE | LOG_DISCOVERY,19745			"0001 Allocated rpi:x%x max:x%x lim:x%x\n",19746			(int) rpi, max_rpi, rpi_limit);19747 19748	/*19749	 * Don't try to allocate more rpi header regions if the device limit19750	 * has been exhausted.19751	 */19752	if ((rpi == LPFC_RPI_ALLOC_ERROR) &&19753	    (phba->sli4_hba.rpi_count >= max_rpi)) {19754		spin_unlock_irqrestore(&phba->hbalock, iflag);19755		return rpi;19756	}19757 19758	/*19759	 * RPI header postings are not required for SLI4 ports capable of19760	 * extents.19761	 */19762	if (!phba->sli4_hba.rpi_hdrs_in_use) {19763		spin_unlock_irqrestore(&phba->hbalock, iflag);19764		return rpi;19765	}19766 19767	/*19768	 * If the driver is running low on rpi resources, allocate another19769	 * page now.  Note that the next_rpi value is used because19770	 * it represents how many are actually in use whereas max_rpi notes19771	 * how many are supported max by the device.19772	 */19773	rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;19774	spin_unlock_irqrestore(&phba->hbalock, iflag);19775	if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {19776		rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);19777		if (!rpi_hdr) {19778			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,19779					"2002 Error Could not grow rpi "19780					"count\n");19781		} else {19782			lrpi = rpi_hdr->start_rpi;19783			rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];19784			lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);19785		}19786	}19787 19788	return rpi;19789}19790 19791/**19792 * __lpfc_sli4_free_rpi - Release an rpi for reuse.19793 * @phba: pointer to lpfc hba data structure.19794 * @rpi: rpi to free19795 *19796 * This routine is invoked to release an rpi to the pool of19797 * available rpis maintained by the driver.19798 **/19799static void19800__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)19801{19802	/*19803	 * if the rpi value indicates a prior unreg has already19804	 * been done, skip the unreg.19805	 */19806	if (rpi == LPFC_RPI_ALLOC_ERROR)19807		return;19808 19809	if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {19810		phba->sli4_hba.rpi_count--;19811		phba->sli4_hba.max_cfg_param.rpi_used--;19812	} else {19813		lpfc_printf_log(phba, KERN_INFO,19814				LOG_NODE | LOG_DISCOVERY,19815				"2016 rpi %x not inuse\n",19816				rpi);19817	}19818}19819 19820/**19821 * lpfc_sli4_free_rpi - Release an rpi for reuse.19822 * @phba: pointer to lpfc hba data structure.19823 * @rpi: rpi to free19824 *19825 * This routine is invoked to release an rpi to the pool of19826 * available rpis maintained by the driver.19827 **/19828void19829lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)19830{19831	spin_lock_irq(&phba->hbalock);19832	__lpfc_sli4_free_rpi(phba, rpi);19833	spin_unlock_irq(&phba->hbalock);19834}19835 19836/**19837 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region19838 * @phba: pointer to lpfc hba data structure.19839 *19840 * This routine is invoked to remove the memory region that19841 * provided rpi via a bitmask.19842 **/19843void19844lpfc_sli4_remove_rpis(struct lpfc_hba *phba)19845{19846	kfree(phba->sli4_hba.rpi_bmask);19847	kfree(phba->sli4_hba.rpi_ids);19848	bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);19849}19850 19851/**19852 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region19853 * @ndlp: pointer to lpfc nodelist data structure.19854 * @cmpl: completion call-back.19855 * @iocbq: data to load as mbox ctx_u information19856 *19857 * This routine is invoked to remove the memory region that19858 * provided rpi via a bitmask.19859 **/19860int19861lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,19862		     void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *),19863		     struct lpfc_iocbq *iocbq)19864{19865	LPFC_MBOXQ_t *mboxq;19866	struct lpfc_hba *phba = ndlp->phba;19867	int rc;19868 19869	/* The port is notified of the header region via a mailbox command. */19870	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);19871	if (!mboxq)19872		return -ENOMEM;19873 19874	/* If cmpl assigned, then this nlp_get pairs with19875	 * lpfc_mbx_cmpl_resume_rpi.19876	 *19877	 * Else cmpl is NULL, then this nlp_get pairs with19878	 * lpfc_sli_def_mbox_cmpl.19879	 */19880	if (!lpfc_nlp_get(ndlp)) {19881		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,19882				"2122 %s: Failed to get nlp ref\n",19883				__func__);19884		mempool_free(mboxq, phba->mbox_mem_pool);19885		return -EIO;19886	}19887 19888	/* Post all rpi memory regions to the port. */19889	lpfc_resume_rpi(mboxq, ndlp);19890	if (cmpl) {19891		mboxq->mbox_cmpl = cmpl;19892		mboxq->ctx_u.save_iocb = iocbq;19893	} else19894		mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;19895	mboxq->ctx_ndlp = ndlp;19896	mboxq->vport = ndlp->vport;19897	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);19898	if (rc == MBX_NOT_FINISHED) {19899		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,19900				"2010 Resume RPI Mailbox failed "19901				"status %d, mbxStatus x%x\n", rc,19902				bf_get(lpfc_mqe_status, &mboxq->u.mqe));19903		lpfc_nlp_put(ndlp);19904		mempool_free(mboxq, phba->mbox_mem_pool);19905		return -EIO;19906	}19907	return 0;19908}19909 19910/**19911 * lpfc_sli4_init_vpi - Initialize a vpi with the port19912 * @vport: Pointer to the vport for which the vpi is being initialized19913 *19914 * This routine is invoked to activate a vpi with the port.19915 *19916 * Returns:19917 *    0 success19918 *    -Evalue otherwise19919 **/19920int19921lpfc_sli4_init_vpi(struct lpfc_vport *vport)19922{19923	LPFC_MBOXQ_t *mboxq;19924	int rc = 0;19925	int retval = MBX_SUCCESS;19926	uint32_t mbox_tmo;19927	struct lpfc_hba *phba = vport->phba;19928	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);19929	if (!mboxq)19930		return -ENOMEM;19931	lpfc_init_vpi(phba, mboxq, vport->vpi);19932	mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);19933	rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);19934	if (rc != MBX_SUCCESS) {19935		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,19936				"2022 INIT VPI Mailbox failed "19937				"status %d, mbxStatus x%x\n", rc,19938				bf_get(lpfc_mqe_status, &mboxq->u.mqe));19939		retval = -EIO;19940	}19941	if (rc != MBX_TIMEOUT)19942		mempool_free(mboxq, vport->phba->mbox_mem_pool);19943 19944	return retval;19945}19946 19947/**19948 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.19949 * @phba: pointer to lpfc hba data structure.19950 * @mboxq: Pointer to mailbox object.19951 *19952 * This routine is invoked to manually add a single FCF record. The caller19953 * must pass a completely initialized FCF_Record.  This routine takes19954 * care of the nonembedded mailbox operations.19955 **/19956static void19957lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)19958{19959	void *virt_addr;19960	union lpfc_sli4_cfg_shdr *shdr;19961	uint32_t shdr_status, shdr_add_status;19962 19963	virt_addr = mboxq->sge_array->addr[0];19964	/* The IOCTL status is embedded in the mailbox subheader. */19965	shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;19966	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);19967	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);19968 19969	if ((shdr_status || shdr_add_status) &&19970		(shdr_status != STATUS_FCF_IN_USE))19971		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,19972			"2558 ADD_FCF_RECORD mailbox failed with "19973			"status x%x add_status x%x\n",19974			shdr_status, shdr_add_status);19975 19976	lpfc_sli4_mbox_cmd_free(phba, mboxq);19977}19978 19979/**19980 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.19981 * @phba: pointer to lpfc hba data structure.19982 * @fcf_record:  pointer to the initialized fcf record to add.19983 *19984 * This routine is invoked to manually add a single FCF record. The caller19985 * must pass a completely initialized FCF_Record.  This routine takes19986 * care of the nonembedded mailbox operations.19987 **/19988int19989lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)19990{19991	int rc = 0;19992	LPFC_MBOXQ_t *mboxq;19993	uint8_t *bytep;19994	void *virt_addr;19995	struct lpfc_mbx_sge sge;19996	uint32_t alloc_len, req_len;19997	uint32_t fcfindex;19998 19999	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);20000	if (!mboxq) {20001		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,20002			"2009 Failed to allocate mbox for ADD_FCF cmd\n");20003		return -ENOMEM;20004	}20005 20006	req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +20007		  sizeof(uint32_t);20008 20009	/* Allocate DMA memory and set up the non-embedded mailbox command */20010	alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,20011				     LPFC_MBOX_OPCODE_FCOE_ADD_FCF,20012				     req_len, LPFC_SLI4_MBX_NEMBED);20013	if (alloc_len < req_len) {20014		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,20015			"2523 Allocated DMA memory size (x%x) is "20016			"less than the requested DMA memory "20017			"size (x%x)\n", alloc_len, req_len);20018		lpfc_sli4_mbox_cmd_free(phba, mboxq);20019		return -ENOMEM;20020	}20021 20022	/*20023	 * Get the first SGE entry from the non-embedded DMA memory.  This20024	 * routine only uses a single SGE.20025	 */20026	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);20027	virt_addr = mboxq->sge_array->addr[0];20028	/*20029	 * Configure the FCF record for FCFI 0.  This is the driver's20030	 * hardcoded default and gets used in nonFIP mode.20031	 */20032	fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);20033	bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);20034	lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));20035 20036	/*20037	 * Copy the fcf_index and the FCF Record Data. The data starts after20038	 * the FCoE header plus word10. The data copy needs to be endian20039	 * correct.20040	 */20041	bytep += sizeof(uint32_t);20042	lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));20043	mboxq->vport = phba->pport;20044	mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;20045	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);20046	if (rc == MBX_NOT_FINISHED) {20047		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,20048			"2515 ADD_FCF_RECORD mailbox failed with "20049			"status 0x%x\n", rc);20050		lpfc_sli4_mbox_cmd_free(phba, mboxq);20051		rc = -EIO;20052	} else20053		rc = 0;20054 20055	return rc;20056}20057 20058/**20059 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.20060 * @phba: pointer to lpfc hba data structure.20061 * @fcf_record:  pointer to the fcf record to write the default data.20062 * @fcf_index: FCF table entry index.20063 *20064 * This routine is invoked to build the driver's default FCF record.  The20065 * values used are hardcoded.  This routine handles memory initialization.20066 *20067 **/20068void20069lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,20070				struct fcf_record *fcf_record,20071				uint16_t fcf_index)20072{20073	memset(fcf_record, 0, sizeof(struct fcf_record));20074	fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;20075	fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;20076	fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;20077	bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);20078	bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);20079	bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);20080	bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);20081	bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);20082	bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);20083	bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);20084	bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);20085	bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);20086	bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);20087	bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);20088	bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);20089	bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,20090		LPFC_FCF_FPMA | LPFC_FCF_SPMA);20091	/* Set the VLAN bit map */20092	if (phba->valid_vlan) {20093		fcf_record->vlan_bitmap[phba->vlan_id / 8]20094			= 1 << (phba->vlan_id % 8);20095	}20096}20097 20098/**20099 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.20100 * @phba: pointer to lpfc hba data structure.20101 * @fcf_index: FCF table entry offset.20102 *20103 * This routine is invoked to scan the entire FCF table by reading FCF20104 * record and processing it one at a time starting from the @fcf_index20105 * for initial FCF discovery or fast FCF failover rediscovery.20106 *20107 * Return 0 if the mailbox command is submitted successfully, none 020108 * otherwise.20109 **/20110int20111lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)20112{20113	int rc = 0, error;20114	LPFC_MBOXQ_t *mboxq;20115 20116	phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;20117	phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;20118	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);20119	if (!mboxq) {20120		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,20121				"2000 Failed to allocate mbox for "20122				"READ_FCF cmd\n");20123		error = -ENOMEM;20124		goto fail_fcf_scan;20125	}20126	/* Construct the read FCF record mailbox command */20127	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);20128	if (rc) {20129		error = -EINVAL;20130		goto fail_fcf_scan;20131	}20132	/* Issue the mailbox command asynchronously */20133	mboxq->vport = phba->pport;20134	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;20135 20136	set_bit(FCF_TS_INPROG, &phba->hba_flag);20137 20138	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);20139	if (rc == MBX_NOT_FINISHED)20140		error = -EIO;20141	else {20142		/* Reset eligible FCF count for new scan */20143		if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)20144			phba->fcf.eligible_fcf_cnt = 0;20145		error = 0;20146	}20147fail_fcf_scan:20148	if (error) {20149		if (mboxq)20150			lpfc_sli4_mbox_cmd_free(phba, mboxq);20151		/* FCF scan failed, clear FCF_TS_INPROG flag */20152		clear_bit(FCF_TS_INPROG, &phba->hba_flag);20153	}20154	return error;20155}20156 20157/**20158 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.20159 * @phba: pointer to lpfc hba data structure.20160 * @fcf_index: FCF table entry offset.20161 *20162 * This routine is invoked to read an FCF record indicated by @fcf_index20163 * and to use it for FLOGI roundrobin FCF failover.20164 *20165 * Return 0 if the mailbox command is submitted successfully, none 020166 * otherwise.20167 **/20168int20169lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)20170{20171	int rc = 0, error;20172	LPFC_MBOXQ_t *mboxq;20173 20174	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);20175	if (!mboxq) {20176		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,20177				"2763 Failed to allocate mbox for "20178				"READ_FCF cmd\n");20179		error = -ENOMEM;20180		goto fail_fcf_read;20181	}20182	/* Construct the read FCF record mailbox command */20183	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);20184	if (rc) {20185		error = -EINVAL;20186		goto fail_fcf_read;20187	}20188	/* Issue the mailbox command asynchronously */20189	mboxq->vport = phba->pport;20190	mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;20191	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);20192	if (rc == MBX_NOT_FINISHED)20193		error = -EIO;20194	else20195		error = 0;20196 20197fail_fcf_read:20198	if (error && mboxq)20199		lpfc_sli4_mbox_cmd_free(phba, mboxq);20200	return error;20201}20202 20203/**20204 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.20205 * @phba: pointer to lpfc hba data structure.20206 * @fcf_index: FCF table entry offset.20207 *20208 * This routine is invoked to read an FCF record indicated by @fcf_index to20209 * determine whether it's eligible for FLOGI roundrobin failover list.20210 *20211 * Return 0 if the mailbox command is submitted successfully, none 020212 * otherwise.20213 **/20214int20215lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)20216{20217	int rc = 0, error;20218	LPFC_MBOXQ_t *mboxq;20219 20220	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);20221	if (!mboxq) {20222		lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,20223				"2758 Failed to allocate mbox for "20224				"READ_FCF cmd\n");20225				error = -ENOMEM;20226				goto fail_fcf_read;20227	}20228	/* Construct the read FCF record mailbox command */20229	rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);20230	if (rc) {20231		error = -EINVAL;20232		goto fail_fcf_read;20233	}20234	/* Issue the mailbox command asynchronously */20235	mboxq->vport = phba->pport;20236	mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;20237	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);20238	if (rc == MBX_NOT_FINISHED)20239		error = -EIO;20240	else20241		error = 0;20242 20243fail_fcf_read:20244	if (error && mboxq)20245		lpfc_sli4_mbox_cmd_free(phba, mboxq);20246	return error;20247}20248 20249/**20250 * lpfc_check_next_fcf_pri_level20251 * @phba: pointer to the lpfc_hba struct for this port.20252 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get20253 * routine when the rr_bmask is empty. The FCF indecies are put into the20254 * rr_bmask based on their priority level. Starting from the highest priority20255 * to the lowest. The most likely FCF candidate will be in the highest20256 * priority group. When this routine is called it searches the fcf_pri list for20257 * next lowest priority group and repopulates the rr_bmask with only those20258 * fcf_indexes.20259 * returns:20260 * 1=success 0=failure20261 **/20262static int20263lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)20264{20265	uint16_t next_fcf_pri;20266	uint16_t last_index;20267	struct lpfc_fcf_pri *fcf_pri;20268	int rc;20269	int ret = 0;20270 20271	last_index = find_first_bit(phba->fcf.fcf_rr_bmask,20272			LPFC_SLI4_FCF_TBL_INDX_MAX);20273	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,20274			"3060 Last IDX %d\n", last_index);20275 20276	/* Verify the priority list has 2 or more entries */20277	spin_lock_irq(&phba->hbalock);20278	if (list_empty(&phba->fcf.fcf_pri_list) ||20279	    list_is_singular(&phba->fcf.fcf_pri_list)) {20280		spin_unlock_irq(&phba->hbalock);20281		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,20282			"3061 Last IDX %d\n", last_index);20283		return 0; /* Empty rr list */20284	}20285	spin_unlock_irq(&phba->hbalock);20286 20287	next_fcf_pri = 0;20288	/*20289	 * Clear the rr_bmask and set all of the bits that are at this20290	 * priority.20291	 */20292	memset(phba->fcf.fcf_rr_bmask, 0,20293			sizeof(*phba->fcf.fcf_rr_bmask));20294	spin_lock_irq(&phba->hbalock);20295	list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {20296		if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)20297			continue;20298		/*20299		 * the 1st priority that has not FLOGI failed20300		 * will be the highest.20301		 */20302		if (!next_fcf_pri)20303			next_fcf_pri = fcf_pri->fcf_rec.priority;20304		spin_unlock_irq(&phba->hbalock);20305		if (fcf_pri->fcf_rec.priority == next_fcf_pri) {20306			rc = lpfc_sli4_fcf_rr_index_set(phba,20307						fcf_pri->fcf_rec.fcf_index);20308			if (rc)20309				return 0;20310		}20311		spin_lock_irq(&phba->hbalock);20312	}20313	/*20314	 * if next_fcf_pri was not set above and the list is not empty then20315	 * we have failed flogis on all of them. So reset flogi failed20316	 * and start at the beginning.20317	 */20318	if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {20319		list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {20320			fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;20321			/*20322			 * the 1st priority that has not FLOGI failed20323			 * will be the highest.20324			 */20325			if (!next_fcf_pri)20326				next_fcf_pri = fcf_pri->fcf_rec.priority;20327			spin_unlock_irq(&phba->hbalock);20328			if (fcf_pri->fcf_rec.priority == next_fcf_pri) {20329				rc = lpfc_sli4_fcf_rr_index_set(phba,20330						fcf_pri->fcf_rec.fcf_index);20331				if (rc)20332					return 0;20333			}20334			spin_lock_irq(&phba->hbalock);20335		}20336	} else20337		ret = 1;20338	spin_unlock_irq(&phba->hbalock);20339 20340	return ret;20341}20342/**20343 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index20344 * @phba: pointer to lpfc hba data structure.20345 *20346 * This routine is to get the next eligible FCF record index in a round20347 * robin fashion. If the next eligible FCF record index equals to the20348 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)20349 * shall be returned, otherwise, the next eligible FCF record's index20350 * shall be returned.20351 **/20352uint16_t20353lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)20354{20355	uint16_t next_fcf_index;20356 20357initial_priority:20358	/* Search start from next bit of currently registered FCF index */20359	next_fcf_index = phba->fcf.current_rec.fcf_indx;20360 20361next_priority:20362	/* Determine the next fcf index to check */20363	next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;20364	next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,20365				       LPFC_SLI4_FCF_TBL_INDX_MAX,20366				       next_fcf_index);20367 20368	/* Wrap around condition on phba->fcf.fcf_rr_bmask */20369	if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {20370		/*20371		 * If we have wrapped then we need to clear the bits that20372		 * have been tested so that we can detect when we should20373		 * change the priority level.20374		 */20375		next_fcf_index = find_first_bit(phba->fcf.fcf_rr_bmask,20376					       LPFC_SLI4_FCF_TBL_INDX_MAX);20377	}20378 20379 20380	/* Check roundrobin failover list empty condition */20381	if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||20382		next_fcf_index == phba->fcf.current_rec.fcf_indx) {20383		/*20384		 * If next fcf index is not found check if there are lower20385		 * Priority level fcf's in the fcf_priority list.20386		 * Set up the rr_bmask with all of the avaiable fcf bits20387		 * at that level and continue the selection process.20388		 */20389		if (lpfc_check_next_fcf_pri_level(phba))20390			goto initial_priority;20391		lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,20392				"2844 No roundrobin failover FCF available\n");20393 20394		return LPFC_FCOE_FCF_NEXT_NONE;20395	}20396 20397	if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&20398		phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &20399		LPFC_FCF_FLOGI_FAILED) {20400		if (list_is_singular(&phba->fcf.fcf_pri_list))20401			return LPFC_FCOE_FCF_NEXT_NONE;20402 20403		goto next_priority;20404	}20405 20406	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,20407			"2845 Get next roundrobin failover FCF (x%x)\n",20408			next_fcf_index);20409 20410	return next_fcf_index;20411}20412 20413/**20414 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index20415 * @phba: pointer to lpfc hba data structure.20416 * @fcf_index: index into the FCF table to 'set'20417 *20418 * This routine sets the FCF record index in to the eligible bmask for20419 * roundrobin failover search. It checks to make sure that the index20420 * does not go beyond the range of the driver allocated bmask dimension20421 * before setting the bit.20422 *20423 * Returns 0 if the index bit successfully set, otherwise, it returns20424 * -EINVAL.20425 **/20426int20427lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)20428{20429	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {20430		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,20431				"2610 FCF (x%x) reached driver's book "20432				"keeping dimension:x%x\n",20433				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);20434		return -EINVAL;20435	}20436	/* Set the eligible FCF record index bmask */20437	set_bit(fcf_index, phba->fcf.fcf_rr_bmask);20438 20439	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,20440			"2790 Set FCF (x%x) to roundrobin FCF failover "20441			"bmask\n", fcf_index);20442 20443	return 0;20444}20445 20446/**20447 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index20448 * @phba: pointer to lpfc hba data structure.20449 * @fcf_index: index into the FCF table to 'clear'20450 *20451 * This routine clears the FCF record index from the eligible bmask for20452 * roundrobin failover search. It checks to make sure that the index20453 * does not go beyond the range of the driver allocated bmask dimension20454 * before clearing the bit.20455 **/20456void20457lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)20458{20459	struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;20460	if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {20461		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,20462				"2762 FCF (x%x) reached driver's book "20463				"keeping dimension:x%x\n",20464				fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);20465		return;20466	}20467	/* Clear the eligible FCF record index bmask */20468	spin_lock_irq(&phba->hbalock);20469	list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,20470				 list) {20471		if (fcf_pri->fcf_rec.fcf_index == fcf_index) {20472			list_del_init(&fcf_pri->list);20473			break;20474		}20475	}20476	spin_unlock_irq(&phba->hbalock);20477	clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);20478 20479	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,20480			"2791 Clear FCF (x%x) from roundrobin failover "20481			"bmask\n", fcf_index);20482}20483 20484/**20485 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table20486 * @phba: pointer to lpfc hba data structure.20487 * @mbox: An allocated pointer to type LPFC_MBOXQ_t20488 *20489 * This routine is the completion routine for the rediscover FCF table mailbox20490 * command. If the mailbox command returned failure, it will try to stop the20491 * FCF rediscover wait timer.20492 **/20493static void20494lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)20495{20496	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;20497	uint32_t shdr_status, shdr_add_status;20498 20499	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;20500 20501	shdr_status = bf_get(lpfc_mbox_hdr_status,20502			     &redisc_fcf->header.cfg_shdr.response);20503	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,20504			     &redisc_fcf->header.cfg_shdr.response);20505	if (shdr_status || shdr_add_status) {20506		lpfc_printf_log(phba, KERN_ERR, LOG_FIP,20507				"2746 Requesting for FCF rediscovery failed "20508				"status x%x add_status x%x\n",20509				shdr_status, shdr_add_status);20510		if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {20511			spin_lock_irq(&phba->hbalock);20512			phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;20513			spin_unlock_irq(&phba->hbalock);20514			/*20515			 * CVL event triggered FCF rediscover request failed,20516			 * last resort to re-try current registered FCF entry.20517			 */20518			lpfc_retry_pport_discovery(phba);20519		} else {20520			spin_lock_irq(&phba->hbalock);20521			phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;20522			spin_unlock_irq(&phba->hbalock);20523			/*20524			 * DEAD FCF event triggered FCF rediscover request20525			 * failed, last resort to fail over as a link down20526			 * to FCF registration.20527			 */20528			lpfc_sli4_fcf_dead_failthrough(phba);20529		}20530	} else {20531		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,20532				"2775 Start FCF rediscover quiescent timer\n");20533		/*20534		 * Start FCF rediscovery wait timer for pending FCF20535		 * before rescan FCF record table.20536		 */20537		lpfc_fcf_redisc_wait_start_timer(phba);20538	}20539 20540	mempool_free(mbox, phba->mbox_mem_pool);20541}20542 20543/**20544 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.20545 * @phba: pointer to lpfc hba data structure.20546 *20547 * This routine is invoked to request for rediscovery of the entire FCF table20548 * by the port.20549 **/20550int20551lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)20552{20553	LPFC_MBOXQ_t *mbox;20554	struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;20555	int rc, length;20556 20557	/* Cancel retry delay timers to all vports before FCF rediscover */20558	lpfc_cancel_all_vport_retry_delay_timer(phba);20559 20560	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);20561	if (!mbox) {20562		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,20563				"2745 Failed to allocate mbox for "20564				"requesting FCF rediscover.\n");20565		return -ENOMEM;20566	}20567 20568	length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -20569		  sizeof(struct lpfc_sli4_cfg_mhdr));20570	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,20571			 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,20572			 length, LPFC_SLI4_MBX_EMBED);20573 20574	redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;20575	/* Set count to 0 for invalidating the entire FCF database */20576	bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);20577 20578	/* Issue the mailbox command asynchronously */20579	mbox->vport = phba->pport;20580	mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;20581	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);20582 20583	if (rc == MBX_NOT_FINISHED) {20584		mempool_free(mbox, phba->mbox_mem_pool);20585		return -EIO;20586	}20587	return 0;20588}20589 20590/**20591 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event20592 * @phba: pointer to lpfc hba data structure.20593 *20594 * This function is the failover routine as a last resort to the FCF DEAD20595 * event when driver failed to perform fast FCF failover.20596 **/20597void20598lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)20599{20600	uint32_t link_state;20601 20602	/*20603	 * Last resort as FCF DEAD event failover will treat this as20604	 * a link down, but save the link state because we don't want20605	 * it to be changed to Link Down unless it is already down.20606	 */20607	link_state = phba->link_state;20608	lpfc_linkdown(phba);20609	phba->link_state = link_state;20610 20611	/* Unregister FCF if no devices connected to it */20612	lpfc_unregister_unused_fcf(phba);20613}20614 20615/**20616 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.20617 * @phba: pointer to lpfc hba data structure.20618 * @rgn23_data: pointer to configure region 23 data.20619 *20620 * This function gets SLI3 port configure region 23 data through memory dump20621 * mailbox command. When it successfully retrieves data, the size of the data20622 * will be returned, otherwise, 0 will be returned.20623 **/20624static uint32_t20625lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)20626{20627	LPFC_MBOXQ_t *pmb = NULL;20628	MAILBOX_t *mb;20629	uint32_t offset = 0;20630	int rc;20631 20632	if (!rgn23_data)20633		return 0;20634 20635	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);20636	if (!pmb) {20637		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,20638				"2600 failed to allocate mailbox memory\n");20639		return 0;20640	}20641	mb = &pmb->u.mb;20642 20643	do {20644		lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);20645		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);20646 20647		if (rc != MBX_SUCCESS) {20648			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,20649					"2601 failed to read config "20650					"region 23, rc 0x%x Status 0x%x\n",20651					rc, mb->mbxStatus);20652			mb->un.varDmp.word_cnt = 0;20653		}20654		/*20655		 * dump mem may return a zero when finished or we got a20656		 * mailbox error, either way we are done.20657		 */20658		if (mb->un.varDmp.word_cnt == 0)20659			break;20660 20661		if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)20662			mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;20663 20664		lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,20665				       rgn23_data + offset,20666				       mb->un.varDmp.word_cnt);20667		offset += mb->un.varDmp.word_cnt;20668	} while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);20669 20670	mempool_free(pmb, phba->mbox_mem_pool);20671	return offset;20672}20673 20674/**20675 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.20676 * @phba: pointer to lpfc hba data structure.20677 * @rgn23_data: pointer to configure region 23 data.20678 *20679 * This function gets SLI4 port configure region 23 data through memory dump20680 * mailbox command. When it successfully retrieves data, the size of the data20681 * will be returned, otherwise, 0 will be returned.20682 **/20683static uint32_t20684lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)20685{20686	LPFC_MBOXQ_t *mboxq = NULL;20687	struct lpfc_dmabuf *mp = NULL;20688	struct lpfc_mqe *mqe;20689	uint32_t data_length = 0;20690	int rc;20691 20692	if (!rgn23_data)20693		return 0;20694 20695	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);20696	if (!mboxq) {20697		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,20698				"3105 failed to allocate mailbox memory\n");20699		return 0;20700	}20701 20702	if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))20703		goto out;20704	mqe = &mboxq->u.mqe;20705	mp = mboxq->ctx_buf;20706	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);20707	if (rc)20708		goto out;20709	data_length = mqe->un.mb_words[5];20710	if (data_length == 0)20711		goto out;20712	if (data_length > DMP_RGN23_SIZE) {20713		data_length = 0;20714		goto out;20715	}20716	lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);20717out:20718	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);20719	return data_length;20720}20721 20722/**20723 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.20724 * @phba: pointer to lpfc hba data structure.20725 *20726 * This function read region 23 and parse TLV for port status to20727 * decide if the user disaled the port. If the TLV indicates the20728 * port is disabled, the hba_flag is set accordingly.20729 **/20730void20731lpfc_sli_read_link_ste(struct lpfc_hba *phba)20732{20733	uint8_t *rgn23_data = NULL;20734	uint32_t if_type, data_size, sub_tlv_len, tlv_offset;20735	uint32_t offset = 0;20736 20737	/* Get adapter Region 23 data */20738	rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);20739	if (!rgn23_data)20740		goto out;20741 20742	if (phba->sli_rev < LPFC_SLI_REV4)20743		data_size = lpfc_sli_get_config_region23(phba, rgn23_data);20744	else {20745		if_type = bf_get(lpfc_sli_intf_if_type,20746				 &phba->sli4_hba.sli_intf);20747		if (if_type == LPFC_SLI_INTF_IF_TYPE_0)20748			goto out;20749		data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);20750	}20751 20752	if (!data_size)20753		goto out;20754 20755	/* Check the region signature first */20756	if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {20757		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,20758			"2619 Config region 23 has bad signature\n");20759			goto out;20760	}20761	offset += 4;20762 20763	/* Check the data structure version */20764	if (rgn23_data[offset] != LPFC_REGION23_VERSION) {20765		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,20766			"2620 Config region 23 has bad version\n");20767		goto out;20768	}20769	offset += 4;20770 20771	/* Parse TLV entries in the region */20772	while (offset < data_size) {20773		if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)20774			break;20775		/*20776		 * If the TLV is not driver specific TLV or driver id is20777		 * not linux driver id, skip the record.20778		 */20779		if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||20780		    (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||20781		    (rgn23_data[offset + 3] != 0)) {20782			offset += rgn23_data[offset + 1] * 4 + 4;20783			continue;20784		}20785 20786		/* Driver found a driver specific TLV in the config region */20787		sub_tlv_len = rgn23_data[offset + 1] * 4;20788		offset += 4;20789		tlv_offset = 0;20790 20791		/*20792		 * Search for configured port state sub-TLV.20793		 */20794		while ((offset < data_size) &&20795			(tlv_offset < sub_tlv_len)) {20796			if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {20797				offset += 4;20798				tlv_offset += 4;20799				break;20800			}20801			if (rgn23_data[offset] != PORT_STE_TYPE) {20802				offset += rgn23_data[offset + 1] * 4 + 4;20803				tlv_offset += rgn23_data[offset + 1] * 4 + 4;20804				continue;20805			}20806 20807			/* This HBA contains PORT_STE configured */20808			if (!rgn23_data[offset + 2])20809				set_bit(LINK_DISABLED, &phba->hba_flag);20810 20811			goto out;20812		}20813	}20814 20815out:20816	kfree(rgn23_data);20817	return;20818}20819 20820/**20821 * lpfc_log_fw_write_cmpl - logs firmware write completion status20822 * @phba: pointer to lpfc hba data structure20823 * @shdr_status: wr_object rsp's status field20824 * @shdr_add_status: wr_object rsp's add_status field20825 * @shdr_add_status_2: wr_object rsp's add_status_2 field20826 * @shdr_change_status: wr_object rsp's change_status field20827 * @shdr_csf: wr_object rsp's csf bit20828 *20829 * This routine is intended to be called after a firmware write completes.20830 * It will log next action items to be performed by the user to instantiate20831 * the newly downloaded firmware or reason for incompatibility.20832 **/20833static void20834lpfc_log_fw_write_cmpl(struct lpfc_hba *phba, u32 shdr_status,20835		       u32 shdr_add_status, u32 shdr_add_status_2,20836		       u32 shdr_change_status, u32 shdr_csf)20837{20838	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,20839			"4198 %s: flash_id x%02x, asic_rev x%02x, "20840			"status x%02x, add_status x%02x, add_status_2 x%02x, "20841			"change_status x%02x, csf %01x\n", __func__,20842			phba->sli4_hba.flash_id, phba->sli4_hba.asic_rev,20843			shdr_status, shdr_add_status, shdr_add_status_2,20844			shdr_change_status, shdr_csf);20845 20846	if (shdr_add_status == LPFC_ADD_STATUS_INCOMPAT_OBJ) {20847		switch (shdr_add_status_2) {20848		case LPFC_ADD_STATUS_2_INCOMPAT_FLASH:20849			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,20850				     "4199 Firmware write failed: "20851				     "image incompatible with flash x%02x\n",20852				     phba->sli4_hba.flash_id);20853			break;20854		case LPFC_ADD_STATUS_2_INCORRECT_ASIC:20855			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,20856				     "4200 Firmware write failed: "20857				     "image incompatible with ASIC "20858				     "architecture x%02x\n",20859				     phba->sli4_hba.asic_rev);20860			break;20861		default:20862			lpfc_log_msg(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,20863				     "4210 Firmware write failed: "20864				     "add_status_2 x%02x\n",20865				     shdr_add_status_2);20866			break;20867		}20868	} else if (!shdr_status && !shdr_add_status) {20869		if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||20870		    shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {20871			if (shdr_csf)20872				shdr_change_status =20873						   LPFC_CHANGE_STATUS_PCI_RESET;20874		}20875 20876		switch (shdr_change_status) {20877		case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):20878			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,20879				     "3198 Firmware write complete: System "20880				     "reboot required to instantiate\n");20881			break;20882		case (LPFC_CHANGE_STATUS_FW_RESET):20883			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,20884				     "3199 Firmware write complete: "20885				     "Firmware reset required to "20886				     "instantiate\n");20887			break;20888		case (LPFC_CHANGE_STATUS_PORT_MIGRATION):20889			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,20890				     "3200 Firmware write complete: Port "20891				     "Migration or PCI Reset required to "20892				     "instantiate\n");20893			break;20894		case (LPFC_CHANGE_STATUS_PCI_RESET):20895			lpfc_log_msg(phba, KERN_NOTICE, LOG_MBOX | LOG_SLI,20896				     "3201 Firmware write complete: PCI "20897				     "Reset required to instantiate\n");20898			break;20899		default:20900			break;20901		}20902	}20903}20904 20905/**20906 * lpfc_wr_object - write an object to the firmware20907 * @phba: HBA structure that indicates port to create a queue on.20908 * @dmabuf_list: list of dmabufs to write to the port.20909 * @size: the total byte value of the objects to write to the port.20910 * @offset: the current offset to be used to start the transfer.20911 *20912 * This routine will create a wr_object mailbox command to send to the port.20913 * the mailbox command will be constructed using the dma buffers described in20914 * @dmabuf_list to create a list of BDEs. This routine will fill in as many20915 * BDEs that the imbedded mailbox can support. The @offset variable will be20916 * used to indicate the starting offset of the transfer and will also return20917 * the offset after the write object mailbox has completed. @size is used to20918 * determine the end of the object and whether the eof bit should be set.20919 *20920 * Return 0 is successful and offset will contain the new offset to use20921 * for the next write.20922 * Return negative value for error cases.20923 **/20924int20925lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,20926	       uint32_t size, uint32_t *offset)20927{20928	struct lpfc_mbx_wr_object *wr_object;20929	LPFC_MBOXQ_t *mbox;20930	int rc = 0, i = 0;20931	int mbox_status = 0;20932	uint32_t shdr_status, shdr_add_status, shdr_add_status_2;20933	uint32_t shdr_change_status = 0, shdr_csf = 0;20934	uint32_t mbox_tmo;20935	struct lpfc_dmabuf *dmabuf;20936	uint32_t written = 0;20937	bool check_change_status = false;20938 20939	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);20940	if (!mbox)20941		return -ENOMEM;20942 20943	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,20944			LPFC_MBOX_OPCODE_WRITE_OBJECT,20945			sizeof(struct lpfc_mbx_wr_object) -20946			sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);20947 20948	wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;20949	wr_object->u.request.write_offset = *offset;20950	sprintf((uint8_t *)wr_object->u.request.object_name, "/");20951	wr_object->u.request.object_name[0] =20952		cpu_to_le32(wr_object->u.request.object_name[0]);20953	bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);20954	list_for_each_entry(dmabuf, dmabuf_list, list) {20955		if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)20956			break;20957		wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);20958		wr_object->u.request.bde[i].addrHigh =20959			putPaddrHigh(dmabuf->phys);20960		if (written + SLI4_PAGE_SIZE >= size) {20961			wr_object->u.request.bde[i].tus.f.bdeSize =20962				(size - written);20963			written += (size - written);20964			bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);20965			bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);20966			check_change_status = true;20967		} else {20968			wr_object->u.request.bde[i].tus.f.bdeSize =20969				SLI4_PAGE_SIZE;20970			written += SLI4_PAGE_SIZE;20971		}20972		i++;20973	}20974	wr_object->u.request.bde_count = i;20975	bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);20976	if (!phba->sli4_hba.intr_enable)20977		mbox_status = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);20978	else {20979		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);20980		mbox_status = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);20981	}20982 20983	/* The mbox status needs to be maintained to detect MBOX_TIMEOUT. */20984	rc = mbox_status;20985 20986	/* The IOCTL status is embedded in the mailbox subheader. */20987	shdr_status = bf_get(lpfc_mbox_hdr_status,20988			     &wr_object->header.cfg_shdr.response);20989	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,20990				 &wr_object->header.cfg_shdr.response);20991	shdr_add_status_2 = bf_get(lpfc_mbox_hdr_add_status_2,20992				   &wr_object->header.cfg_shdr.response);20993	if (check_change_status) {20994		shdr_change_status = bf_get(lpfc_wr_object_change_status,20995					    &wr_object->u.response);20996		shdr_csf = bf_get(lpfc_wr_object_csf,20997				  &wr_object->u.response);20998	}20999 21000	if (shdr_status || shdr_add_status || shdr_add_status_2 || rc) {21001		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,21002				"3025 Write Object mailbox failed with "21003				"status x%x add_status x%x, add_status_2 x%x, "21004				"mbx status x%x\n",21005				shdr_status, shdr_add_status, shdr_add_status_2,21006				rc);21007		rc = -ENXIO;21008		*offset = shdr_add_status;21009	} else {21010		*offset += wr_object->u.response.actual_write_length;21011	}21012 21013	if (rc || check_change_status)21014		lpfc_log_fw_write_cmpl(phba, shdr_status, shdr_add_status,21015				       shdr_add_status_2, shdr_change_status,21016				       shdr_csf);21017 21018	if (!phba->sli4_hba.intr_enable)21019		mempool_free(mbox, phba->mbox_mem_pool);21020	else if (mbox_status != MBX_TIMEOUT)21021		mempool_free(mbox, phba->mbox_mem_pool);21022 21023	return rc;21024}21025 21026/**21027 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.21028 * @vport: pointer to vport data structure.21029 *21030 * This function iterate through the mailboxq and clean up all REG_LOGIN21031 * and REG_VPI mailbox commands associated with the vport. This function21032 * is called when driver want to restart discovery of the vport due to21033 * a Clear Virtual Link event.21034 **/21035void21036lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)21037{21038	struct lpfc_hba *phba = vport->phba;21039	LPFC_MBOXQ_t *mb, *nextmb;21040	struct lpfc_nodelist *ndlp;21041	struct lpfc_nodelist *act_mbx_ndlp = NULL;21042	LIST_HEAD(mbox_cmd_list);21043	uint8_t restart_loop;21044 21045	/* Clean up internally queued mailbox commands with the vport */21046	spin_lock_irq(&phba->hbalock);21047	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {21048		if (mb->vport != vport)21049			continue;21050 21051		if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&21052			(mb->u.mb.mbxCommand != MBX_REG_VPI))21053			continue;21054 21055		list_move_tail(&mb->list, &mbox_cmd_list);21056	}21057	/* Clean up active mailbox command with the vport */21058	mb = phba->sli.mbox_active;21059	if (mb && (mb->vport == vport)) {21060		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||21061			(mb->u.mb.mbxCommand == MBX_REG_VPI))21062			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;21063		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {21064			act_mbx_ndlp = mb->ctx_ndlp;21065 21066			/* This reference is local to this routine.  The21067			 * reference is removed at routine exit.21068			 */21069			act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);21070 21071			/* Unregister the RPI when mailbox complete */21072			mb->mbox_flag |= LPFC_MBX_IMED_UNREG;21073		}21074	}21075	/* Cleanup any mailbox completions which are not yet processed */21076	do {21077		restart_loop = 0;21078		list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {21079			/*21080			 * If this mailox is already processed or it is21081			 * for another vport ignore it.21082			 */21083			if ((mb->vport != vport) ||21084				(mb->mbox_flag & LPFC_MBX_IMED_UNREG))21085				continue;21086 21087			if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&21088				(mb->u.mb.mbxCommand != MBX_REG_VPI))21089				continue;21090 21091			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;21092			if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {21093				ndlp = mb->ctx_ndlp;21094				/* Unregister the RPI when mailbox complete */21095				mb->mbox_flag |= LPFC_MBX_IMED_UNREG;21096				restart_loop = 1;21097				spin_unlock_irq(&phba->hbalock);21098				spin_lock(&ndlp->lock);21099				ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;21100				spin_unlock(&ndlp->lock);21101				spin_lock_irq(&phba->hbalock);21102				break;21103			}21104		}21105	} while (restart_loop);21106 21107	spin_unlock_irq(&phba->hbalock);21108 21109	/* Release the cleaned-up mailbox commands */21110	while (!list_empty(&mbox_cmd_list)) {21111		list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);21112		if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {21113			ndlp = mb->ctx_ndlp;21114			mb->ctx_ndlp = NULL;21115			if (ndlp) {21116				spin_lock(&ndlp->lock);21117				ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;21118				spin_unlock(&ndlp->lock);21119				lpfc_nlp_put(ndlp);21120			}21121		}21122		lpfc_mbox_rsrc_cleanup(phba, mb, MBOX_THD_UNLOCKED);21123	}21124 21125	/* Release the ndlp with the cleaned-up active mailbox command */21126	if (act_mbx_ndlp) {21127		spin_lock(&act_mbx_ndlp->lock);21128		act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;21129		spin_unlock(&act_mbx_ndlp->lock);21130		lpfc_nlp_put(act_mbx_ndlp);21131	}21132}21133 21134/**21135 * lpfc_drain_txq - Drain the txq21136 * @phba: Pointer to HBA context object.21137 *21138 * This function attempt to submit IOCBs on the txq21139 * to the adapter.  For SLI4 adapters, the txq contains21140 * ELS IOCBs that have been deferred because the there21141 * are no SGLs.  This congestion can occur with large21142 * vport counts during node discovery.21143 **/21144 21145uint32_t21146lpfc_drain_txq(struct lpfc_hba *phba)21147{21148	LIST_HEAD(completions);21149	struct lpfc_sli_ring *pring;21150	struct lpfc_iocbq *piocbq = NULL;21151	unsigned long iflags = 0;21152	char *fail_msg = NULL;21153	uint32_t txq_cnt = 0;21154	struct lpfc_queue *wq;21155	int ret = 0;21156 21157	if (phba->link_flag & LS_MDS_LOOPBACK) {21158		/* MDS WQE are posted only to first WQ*/21159		wq = phba->sli4_hba.hdwq[0].io_wq;21160		if (unlikely(!wq))21161			return 0;21162		pring = wq->pring;21163	} else {21164		wq = phba->sli4_hba.els_wq;21165		if (unlikely(!wq))21166			return 0;21167		pring = lpfc_phba_elsring(phba);21168	}21169 21170	if (unlikely(!pring) || list_empty(&pring->txq))21171		return 0;21172 21173	spin_lock_irqsave(&pring->ring_lock, iflags);21174	list_for_each_entry(piocbq, &pring->txq, list) {21175		txq_cnt++;21176	}21177 21178	if (txq_cnt > pring->txq_max)21179		pring->txq_max = txq_cnt;21180 21181	spin_unlock_irqrestore(&pring->ring_lock, iflags);21182 21183	while (!list_empty(&pring->txq)) {21184		spin_lock_irqsave(&pring->ring_lock, iflags);21185 21186		piocbq = lpfc_sli_ringtx_get(phba, pring);21187		if (!piocbq) {21188			spin_unlock_irqrestore(&pring->ring_lock, iflags);21189			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,21190				"2823 txq empty and txq_cnt is %d\n",21191				txq_cnt);21192			break;21193		}21194		txq_cnt--;21195 21196		ret = __lpfc_sli_issue_iocb(phba, pring->ringno, piocbq, 0);21197 21198		if (ret && ret != IOCB_BUSY) {21199			fail_msg = " - Cannot send IO ";21200			piocbq->cmd_flag &= ~LPFC_DRIVER_ABORTED;21201		}21202		if (fail_msg) {21203			piocbq->cmd_flag |= LPFC_DRIVER_ABORTED;21204			/* Failed means we can't issue and need to cancel */21205			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,21206					"2822 IOCB failed %s iotag 0x%x "21207					"xri 0x%x %d flg x%x\n",21208					fail_msg, piocbq->iotag,21209					piocbq->sli4_xritag, ret,21210					piocbq->cmd_flag);21211			list_add_tail(&piocbq->list, &completions);21212			fail_msg = NULL;21213		}21214		spin_unlock_irqrestore(&pring->ring_lock, iflags);21215		if (txq_cnt == 0 || ret == IOCB_BUSY)21216			break;21217	}21218	/* Cancel all the IOCBs that cannot be issued */21219	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,21220			      IOERR_SLI_ABORTED);21221 21222	return txq_cnt;21223}21224 21225/**21226 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.21227 * @phba: Pointer to HBA context object.21228 * @pwqeq: Pointer to command WQE.21229 * @sglq: Pointer to the scatter gather queue object.21230 *21231 * This routine converts the bpl or bde that is in the WQE21232 * to a sgl list for the sli4 hardware. The physical address21233 * of the bpl/bde is converted back to a virtual address.21234 * If the WQE contains a BPL then the list of BDE's is21235 * converted to sli4_sge's. If the WQE contains a single21236 * BDE then it is converted to a single sli_sge.21237 * The WQE is still in cpu endianness so the contents of21238 * the bpl can be used without byte swapping.21239 *21240 * Returns valid XRI = Success, NO_XRI = Failure.21241 */21242static uint16_t21243lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,21244		 struct lpfc_sglq *sglq)21245{21246	uint16_t xritag = NO_XRI;21247	struct ulp_bde64 *bpl = NULL;21248	struct ulp_bde64 bde;21249	struct sli4_sge *sgl  = NULL;21250	struct lpfc_dmabuf *dmabuf;21251	union lpfc_wqe128 *wqe;21252	int numBdes = 0;21253	int i = 0;21254	uint32_t offset = 0; /* accumulated offset in the sg request list */21255	int inbound = 0; /* number of sg reply entries inbound from firmware */21256	uint32_t cmd;21257 21258	if (!pwqeq || !sglq)21259		return xritag;21260 21261	sgl  = (struct sli4_sge *)sglq->sgl;21262	wqe = &pwqeq->wqe;21263	pwqeq->iocb.ulpIoTag = pwqeq->iotag;21264 21265	cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);21266	if (cmd == CMD_XMIT_BLS_RSP64_WQE)21267		return sglq->sli4_xritag;21268	numBdes = pwqeq->num_bdes;21269	if (numBdes) {21270		/* The addrHigh and addrLow fields within the WQE21271		 * have not been byteswapped yet so there is no21272		 * need to swap them back.21273		 */21274		if (pwqeq->bpl_dmabuf)21275			dmabuf = pwqeq->bpl_dmabuf;21276		else21277			return xritag;21278 21279		bpl  = (struct ulp_bde64 *)dmabuf->virt;21280		if (!bpl)21281			return xritag;21282 21283		for (i = 0; i < numBdes; i++) {21284			/* Should already be byte swapped. */21285			sgl->addr_hi = bpl->addrHigh;21286			sgl->addr_lo = bpl->addrLow;21287 21288			sgl->word2 = le32_to_cpu(sgl->word2);21289			if ((i+1) == numBdes)21290				bf_set(lpfc_sli4_sge_last, sgl, 1);21291			else21292				bf_set(lpfc_sli4_sge_last, sgl, 0);21293			/* swap the size field back to the cpu so we21294			 * can assign it to the sgl.21295			 */21296			bde.tus.w = le32_to_cpu(bpl->tus.w);21297			sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);21298			/* The offsets in the sgl need to be accumulated21299			 * separately for the request and reply lists.21300			 * The request is always first, the reply follows.21301			 */21302			switch (cmd) {21303			case CMD_GEN_REQUEST64_WQE:21304				/* add up the reply sg entries */21305				if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)21306					inbound++;21307				/* first inbound? reset the offset */21308				if (inbound == 1)21309					offset = 0;21310				bf_set(lpfc_sli4_sge_offset, sgl, offset);21311				bf_set(lpfc_sli4_sge_type, sgl,21312					LPFC_SGE_TYPE_DATA);21313				offset += bde.tus.f.bdeSize;21314				break;21315			case CMD_FCP_TRSP64_WQE:21316				bf_set(lpfc_sli4_sge_offset, sgl, 0);21317				bf_set(lpfc_sli4_sge_type, sgl,21318					LPFC_SGE_TYPE_DATA);21319				break;21320			case CMD_FCP_TSEND64_WQE:21321			case CMD_FCP_TRECEIVE64_WQE:21322				bf_set(lpfc_sli4_sge_type, sgl,21323					bpl->tus.f.bdeFlags);21324				if (i < 3)21325					offset = 0;21326				else21327					offset += bde.tus.f.bdeSize;21328				bf_set(lpfc_sli4_sge_offset, sgl, offset);21329				break;21330			}21331			sgl->word2 = cpu_to_le32(sgl->word2);21332			bpl++;21333			sgl++;21334		}21335	} else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {21336		/* The addrHigh and addrLow fields of the BDE have not21337		 * been byteswapped yet so they need to be swapped21338		 * before putting them in the sgl.21339		 */21340		sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);21341		sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);21342		sgl->word2 = le32_to_cpu(sgl->word2);21343		bf_set(lpfc_sli4_sge_last, sgl, 1);21344		sgl->word2 = cpu_to_le32(sgl->word2);21345		sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);21346	}21347	return sglq->sli4_xritag;21348}21349 21350/**21351 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)21352 * @phba: Pointer to HBA context object.21353 * @qp: Pointer to HDW queue.21354 * @pwqe: Pointer to command WQE.21355 **/21356int21357lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,21358		    struct lpfc_iocbq *pwqe)21359{21360	union lpfc_wqe128 *wqe = &pwqe->wqe;21361	struct lpfc_async_xchg_ctx *ctxp;21362	struct lpfc_queue *wq;21363	struct lpfc_sglq *sglq;21364	struct lpfc_sli_ring *pring;21365	unsigned long iflags;21366	uint32_t ret = 0;21367 21368	/* NVME_LS and NVME_LS ABTS requests. */21369	if (pwqe->cmd_flag & LPFC_IO_NVME_LS) {21370		pring =  phba->sli4_hba.nvmels_wq->pring;21371		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,21372					  qp, wq_access);21373		sglq = __lpfc_sli_get_els_sglq(phba, pwqe);21374		if (!sglq) {21375			spin_unlock_irqrestore(&pring->ring_lock, iflags);21376			return WQE_BUSY;21377		}21378		pwqe->sli4_lxritag = sglq->sli4_lxritag;21379		pwqe->sli4_xritag = sglq->sli4_xritag;21380		if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {21381			spin_unlock_irqrestore(&pring->ring_lock, iflags);21382			return WQE_ERROR;21383		}21384		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,21385		       pwqe->sli4_xritag);21386		ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);21387		if (ret) {21388			spin_unlock_irqrestore(&pring->ring_lock, iflags);21389			return ret;21390		}21391 21392		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);21393		spin_unlock_irqrestore(&pring->ring_lock, iflags);21394 21395		lpfc_sli4_poll_eq(qp->hba_eq);21396		return 0;21397	}21398 21399	/* NVME_FCREQ and NVME_ABTS requests */21400	if (pwqe->cmd_flag & (LPFC_IO_NVME | LPFC_IO_FCP | LPFC_IO_CMF)) {21401		/* Get the IO distribution (hba_wqidx) for WQ assignment. */21402		wq = qp->io_wq;21403		pring = wq->pring;21404 21405		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);21406 21407		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,21408					  qp, wq_access);21409		ret = lpfc_sli4_wq_put(wq, wqe);21410		if (ret) {21411			spin_unlock_irqrestore(&pring->ring_lock, iflags);21412			return ret;21413		}21414		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);21415		spin_unlock_irqrestore(&pring->ring_lock, iflags);21416 21417		lpfc_sli4_poll_eq(qp->hba_eq);21418		return 0;21419	}21420 21421	/* NVMET requests */21422	if (pwqe->cmd_flag & LPFC_IO_NVMET) {21423		/* Get the IO distribution (hba_wqidx) for WQ assignment. */21424		wq = qp->io_wq;21425		pring = wq->pring;21426 21427		ctxp = pwqe->context_un.axchg;21428		sglq = ctxp->ctxbuf->sglq;21429		if (pwqe->sli4_xritag ==  NO_XRI) {21430			pwqe->sli4_lxritag = sglq->sli4_lxritag;21431			pwqe->sli4_xritag = sglq->sli4_xritag;21432		}21433		bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,21434		       pwqe->sli4_xritag);21435		bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);21436 21437		lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,21438					  qp, wq_access);21439		ret = lpfc_sli4_wq_put(wq, wqe);21440		if (ret) {21441			spin_unlock_irqrestore(&pring->ring_lock, iflags);21442			return ret;21443		}21444		lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);21445		spin_unlock_irqrestore(&pring->ring_lock, iflags);21446 21447		lpfc_sli4_poll_eq(qp->hba_eq);21448		return 0;21449	}21450	return WQE_ERROR;21451}21452 21453/**21454 * lpfc_sli4_issue_abort_iotag - SLI-4 WQE init & issue for the Abort21455 * @phba: Pointer to HBA context object.21456 * @cmdiocb: Pointer to driver command iocb object.21457 * @cmpl: completion function.21458 *21459 * Fill the appropriate fields for the abort WQE and call21460 * internal routine lpfc_sli4_issue_wqe to send the WQE21461 * This function is called with hbalock held and no ring_lock held.21462 *21463 * RETURNS 0 - SUCCESS21464 **/21465 21466int21467lpfc_sli4_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,21468			    void *cmpl)21469{21470	struct lpfc_vport *vport = cmdiocb->vport;21471	struct lpfc_iocbq *abtsiocb = NULL;21472	union lpfc_wqe128 *abtswqe;21473	struct lpfc_io_buf *lpfc_cmd;21474	int retval = IOCB_ERROR;21475	u16 xritag = cmdiocb->sli4_xritag;21476 21477	/*21478	 * The scsi command can not be in txq and it is in flight because the21479	 * pCmd is still pointing at the SCSI command we have to abort. There21480	 * is no need to search the txcmplq. Just send an abort to the FW.21481	 */21482 21483	abtsiocb = __lpfc_sli_get_iocbq(phba);21484	if (!abtsiocb)21485		return WQE_NORESOURCE;21486 21487	/* Indicate the IO is being aborted by the driver. */21488	cmdiocb->cmd_flag |= LPFC_DRIVER_ABORTED;21489 21490	abtswqe = &abtsiocb->wqe;21491	memset(abtswqe, 0, sizeof(*abtswqe));21492 21493	if (!lpfc_is_link_up(phba) || (phba->link_flag & LS_EXTERNAL_LOOPBACK))21494		bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 1);21495	bf_set(abort_cmd_criteria, &abtswqe->abort_cmd, T_XRI_TAG);21496	abtswqe->abort_cmd.rsrvd5 = 0;21497	abtswqe->abort_cmd.wqe_com.abort_tag = xritag;21498	bf_set(wqe_reqtag, &abtswqe->abort_cmd.wqe_com, abtsiocb->iotag);21499	bf_set(wqe_cmnd, &abtswqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);21500	bf_set(wqe_xri_tag, &abtswqe->generic.wqe_com, 0);21501	bf_set(wqe_qosd, &abtswqe->abort_cmd.wqe_com, 1);21502	bf_set(wqe_lenloc, &abtswqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);21503	bf_set(wqe_cmd_type, &abtswqe->abort_cmd.wqe_com, OTHER_COMMAND);21504 21505	/* ABTS WQE must go to the same WQ as the WQE to be aborted */21506	abtsiocb->hba_wqidx = cmdiocb->hba_wqidx;21507	abtsiocb->cmd_flag |= LPFC_USE_FCPWQIDX;21508	if (cmdiocb->cmd_flag & LPFC_IO_FCP)21509		abtsiocb->cmd_flag |= LPFC_IO_FCP;21510	if (cmdiocb->cmd_flag & LPFC_IO_NVME)21511		abtsiocb->cmd_flag |= LPFC_IO_NVME;21512	if (cmdiocb->cmd_flag & LPFC_IO_FOF)21513		abtsiocb->cmd_flag |= LPFC_IO_FOF;21514	abtsiocb->vport = vport;21515	abtsiocb->cmd_cmpl = cmpl;21516 21517	lpfc_cmd = container_of(cmdiocb, struct lpfc_io_buf, cur_iocbq);21518	retval = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, abtsiocb);21519 21520	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NVME_ABTS | LOG_FCP,21521			 "0359 Abort xri x%x, original iotag x%x, "21522			 "abort cmd iotag x%x retval x%x\n",21523			 xritag, cmdiocb->iotag, abtsiocb->iotag, retval);21524 21525	if (retval) {21526		cmdiocb->cmd_flag &= ~LPFC_DRIVER_ABORTED;21527		__lpfc_sli_release_iocbq(phba, abtsiocb);21528	}21529 21530	return retval;21531}21532 21533#ifdef LPFC_MXP_STAT21534/**21535 * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count21536 * @phba: pointer to lpfc hba data structure.21537 * @hwqid: belong to which HWQ.21538 *21539 * The purpose of this routine is to take a snapshot of pbl, pvt and busy count21540 * 15 seconds after a test case is running.21541 *21542 * The user should call lpfc_debugfs_multixripools_write before running a test21543 * case to clear stat_snapshot_taken. Then the user starts a test case. During21544 * test case is running, stat_snapshot_taken is incremented by 1 every time when21545 * this routine is called from heartbeat timer. When stat_snapshot_taken is21546 * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.21547 **/21548void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)21549{21550	struct lpfc_sli4_hdw_queue *qp;21551	struct lpfc_multixri_pool *multixri_pool;21552	struct lpfc_pvt_pool *pvt_pool;21553	struct lpfc_pbl_pool *pbl_pool;21554	u32 txcmplq_cnt;21555 21556	qp = &phba->sli4_hba.hdwq[hwqid];21557	multixri_pool = qp->p_multixri_pool;21558	if (!multixri_pool)21559		return;21560 21561	if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {21562		pvt_pool = &qp->p_multixri_pool->pvt_pool;21563		pbl_pool = &qp->p_multixri_pool->pbl_pool;21564		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;21565 21566		multixri_pool->stat_pbl_count = pbl_pool->count;21567		multixri_pool->stat_pvt_count = pvt_pool->count;21568		multixri_pool->stat_busy_count = txcmplq_cnt;21569	}21570 21571	multixri_pool->stat_snapshot_taken++;21572}21573#endif21574 21575/**21576 * lpfc_adjust_pvt_pool_count - Adjust private pool count21577 * @phba: pointer to lpfc hba data structure.21578 * @hwqid: belong to which HWQ.21579 *21580 * This routine moves some XRIs from private to public pool when private pool21581 * is not busy.21582 **/21583void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)21584{21585	struct lpfc_multixri_pool *multixri_pool;21586	u32 io_req_count;21587	u32 prev_io_req_count;21588 21589	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;21590	if (!multixri_pool)21591		return;21592	io_req_count = multixri_pool->io_req_count;21593	prev_io_req_count = multixri_pool->prev_io_req_count;21594 21595	if (prev_io_req_count != io_req_count) {21596		/* Private pool is busy */21597		multixri_pool->prev_io_req_count = io_req_count;21598	} else {21599		/* Private pool is not busy.21600		 * Move XRIs from private to public pool.21601		 */21602		lpfc_move_xri_pvt_to_pbl(phba, hwqid);21603	}21604}21605 21606/**21607 * lpfc_adjust_high_watermark - Adjust high watermark21608 * @phba: pointer to lpfc hba data structure.21609 * @hwqid: belong to which HWQ.21610 *21611 * This routine sets high watermark as number of outstanding XRIs,21612 * but make sure the new value is between xri_limit/2 and xri_limit.21613 **/21614void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)21615{21616	u32 new_watermark;21617	u32 watermark_max;21618	u32 watermark_min;21619	u32 xri_limit;21620	u32 txcmplq_cnt;21621	u32 abts_io_bufs;21622	struct lpfc_multixri_pool *multixri_pool;21623	struct lpfc_sli4_hdw_queue *qp;21624 21625	qp = &phba->sli4_hba.hdwq[hwqid];21626	multixri_pool = qp->p_multixri_pool;21627	if (!multixri_pool)21628		return;21629	xri_limit = multixri_pool->xri_limit;21630 21631	watermark_max = xri_limit;21632	watermark_min = xri_limit / 2;21633 21634	txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;21635	abts_io_bufs = qp->abts_scsi_io_bufs;21636	abts_io_bufs += qp->abts_nvme_io_bufs;21637 21638	new_watermark = txcmplq_cnt + abts_io_bufs;21639	new_watermark = min(watermark_max, new_watermark);21640	new_watermark = max(watermark_min, new_watermark);21641	multixri_pool->pvt_pool.high_watermark = new_watermark;21642 21643#ifdef LPFC_MXP_STAT21644	multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,21645					  new_watermark);21646#endif21647}21648 21649/**21650 * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool21651 * @phba: pointer to lpfc hba data structure.21652 * @hwqid: belong to which HWQ.21653 *21654 * This routine is called from hearbeat timer when pvt_pool is idle.21655 * All free XRIs are moved from private to public pool on hwqid with 2 steps.21656 * The first step moves (all - low_watermark) amount of XRIs.21657 * The second step moves the rest of XRIs.21658 **/21659void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)21660{21661	struct lpfc_pbl_pool *pbl_pool;21662	struct lpfc_pvt_pool *pvt_pool;21663	struct lpfc_sli4_hdw_queue *qp;21664	struct lpfc_io_buf *lpfc_ncmd;21665	struct lpfc_io_buf *lpfc_ncmd_next;21666	unsigned long iflag;21667	struct list_head tmp_list;21668	u32 tmp_count;21669 21670	qp = &phba->sli4_hba.hdwq[hwqid];21671	pbl_pool = &qp->p_multixri_pool->pbl_pool;21672	pvt_pool = &qp->p_multixri_pool->pvt_pool;21673	tmp_count = 0;21674 21675	lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);21676	lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);21677 21678	if (pvt_pool->count > pvt_pool->low_watermark) {21679		/* Step 1: move (all - low_watermark) from pvt_pool21680		 * to pbl_pool21681		 */21682 21683		/* Move low watermark of bufs from pvt_pool to tmp_list */21684		INIT_LIST_HEAD(&tmp_list);21685		list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,21686					 &pvt_pool->list, list) {21687			list_move_tail(&lpfc_ncmd->list, &tmp_list);21688			tmp_count++;21689			if (tmp_count >= pvt_pool->low_watermark)21690				break;21691		}21692 21693		/* Move all bufs from pvt_pool to pbl_pool */21694		list_splice_init(&pvt_pool->list, &pbl_pool->list);21695 21696		/* Move all bufs from tmp_list to pvt_pool */21697		list_splice(&tmp_list, &pvt_pool->list);21698 21699		pbl_pool->count += (pvt_pool->count - tmp_count);21700		pvt_pool->count = tmp_count;21701	} else {21702		/* Step 2: move the rest from pvt_pool to pbl_pool */21703		list_splice_init(&pvt_pool->list, &pbl_pool->list);21704		pbl_pool->count += pvt_pool->count;21705		pvt_pool->count = 0;21706	}21707 21708	spin_unlock(&pvt_pool->lock);21709	spin_unlock_irqrestore(&pbl_pool->lock, iflag);21710}21711 21712/**21713 * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool21714 * @phba: pointer to lpfc hba data structure21715 * @qp: pointer to HDW queue21716 * @pbl_pool: specified public free XRI pool21717 * @pvt_pool: specified private free XRI pool21718 * @count: number of XRIs to move21719 *21720 * This routine tries to move some free common bufs from the specified pbl_pool21721 * to the specified pvt_pool. It might move less than count XRIs if there's not21722 * enough in public pool.21723 *21724 * Return:21725 *   true - if XRIs are successfully moved from the specified pbl_pool to the21726 *          specified pvt_pool21727 *   false - if the specified pbl_pool is empty or locked by someone else21728 **/21729static bool21730_lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,21731			  struct lpfc_pbl_pool *pbl_pool,21732			  struct lpfc_pvt_pool *pvt_pool, u32 count)21733{21734	struct lpfc_io_buf *lpfc_ncmd;21735	struct lpfc_io_buf *lpfc_ncmd_next;21736	unsigned long iflag;21737	int ret;21738 21739	ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);21740	if (ret) {21741		if (pbl_pool->count) {21742			/* Move a batch of XRIs from public to private pool */21743			lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);21744			list_for_each_entry_safe(lpfc_ncmd,21745						 lpfc_ncmd_next,21746						 &pbl_pool->list,21747						 list) {21748				list_move_tail(&lpfc_ncmd->list,21749					       &pvt_pool->list);21750				pvt_pool->count++;21751				pbl_pool->count--;21752				count--;21753				if (count == 0)21754					break;21755			}21756 21757			spin_unlock(&pvt_pool->lock);21758			spin_unlock_irqrestore(&pbl_pool->lock, iflag);21759			return true;21760		}21761		spin_unlock_irqrestore(&pbl_pool->lock, iflag);21762	}21763 21764	return false;21765}21766 21767/**21768 * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool21769 * @phba: pointer to lpfc hba data structure.21770 * @hwqid: belong to which HWQ.21771 * @count: number of XRIs to move21772 *21773 * This routine tries to find some free common bufs in one of public pools with21774 * Round Robin method. The search always starts from local hwqid, then the next21775 * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,21776 * a batch of free common bufs are moved to private pool on hwqid.21777 * It might move less than count XRIs if there's not enough in public pool.21778 **/21779void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)21780{21781	struct lpfc_multixri_pool *multixri_pool;21782	struct lpfc_multixri_pool *next_multixri_pool;21783	struct lpfc_pvt_pool *pvt_pool;21784	struct lpfc_pbl_pool *pbl_pool;21785	struct lpfc_sli4_hdw_queue *qp;21786	u32 next_hwqid;21787	u32 hwq_count;21788	int ret;21789 21790	qp = &phba->sli4_hba.hdwq[hwqid];21791	multixri_pool = qp->p_multixri_pool;21792	pvt_pool = &multixri_pool->pvt_pool;21793	pbl_pool = &multixri_pool->pbl_pool;21794 21795	/* Check if local pbl_pool is available */21796	ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);21797	if (ret) {21798#ifdef LPFC_MXP_STAT21799		multixri_pool->local_pbl_hit_count++;21800#endif21801		return;21802	}21803 21804	hwq_count = phba->cfg_hdw_queue;21805 21806	/* Get the next hwqid which was found last time */21807	next_hwqid = multixri_pool->rrb_next_hwqid;21808 21809	do {21810		/* Go to next hwq */21811		next_hwqid = (next_hwqid + 1) % hwq_count;21812 21813		next_multixri_pool =21814			phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;21815		pbl_pool = &next_multixri_pool->pbl_pool;21816 21817		/* Check if the public free xri pool is available */21818		ret = _lpfc_move_xri_pbl_to_pvt(21819			phba, qp, pbl_pool, pvt_pool, count);21820 21821		/* Exit while-loop if success or all hwqid are checked */21822	} while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);21823 21824	/* Starting point for the next time */21825	multixri_pool->rrb_next_hwqid = next_hwqid;21826 21827	if (!ret) {21828		/* stats: all public pools are empty*/21829		multixri_pool->pbl_empty_count++;21830	}21831 21832#ifdef LPFC_MXP_STAT21833	if (ret) {21834		if (next_hwqid == hwqid)21835			multixri_pool->local_pbl_hit_count++;21836		else21837			multixri_pool->other_pbl_hit_count++;21838	}21839#endif21840}21841 21842/**21843 * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark21844 * @phba: pointer to lpfc hba data structure.21845 * @hwqid: belong to which HWQ.21846 *21847 * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than21848 * low watermark.21849 **/21850void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)21851{21852	struct lpfc_multixri_pool *multixri_pool;21853	struct lpfc_pvt_pool *pvt_pool;21854 21855	multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;21856	pvt_pool = &multixri_pool->pvt_pool;21857 21858	if (pvt_pool->count < pvt_pool->low_watermark)21859		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);21860}21861 21862/**21863 * lpfc_release_io_buf - Return one IO buf back to free pool21864 * @phba: pointer to lpfc hba data structure.21865 * @lpfc_ncmd: IO buf to be returned.21866 * @qp: belong to which HWQ.21867 *21868 * This routine returns one IO buf back to free pool. If this is an urgent IO,21869 * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,21870 * the IO buf is returned to pbl_pool or pvt_pool based on watermark and21871 * xri_limit.  If cfg_xri_rebalancing==0, the IO buf is returned to21872 * lpfc_io_buf_list_put.21873 **/21874void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,21875			 struct lpfc_sli4_hdw_queue *qp)21876{21877	unsigned long iflag;21878	struct lpfc_pbl_pool *pbl_pool;21879	struct lpfc_pvt_pool *pvt_pool;21880	struct lpfc_epd_pool *epd_pool;21881	u32 txcmplq_cnt;21882	u32 xri_owned;21883	u32 xri_limit;21884	u32 abts_io_bufs;21885 21886	/* MUST zero fields if buffer is reused by another protocol */21887	lpfc_ncmd->nvmeCmd = NULL;21888	lpfc_ncmd->cur_iocbq.cmd_cmpl = NULL;21889 21890	if (phba->cfg_xpsgl && !phba->nvmet_support &&21891	    !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))21892		lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);21893 21894	if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))21895		lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);21896 21897	if (phba->cfg_xri_rebalancing) {21898		if (lpfc_ncmd->expedite) {21899			/* Return to expedite pool */21900			epd_pool = &phba->epd_pool;21901			spin_lock_irqsave(&epd_pool->lock, iflag);21902			list_add_tail(&lpfc_ncmd->list, &epd_pool->list);21903			epd_pool->count++;21904			spin_unlock_irqrestore(&epd_pool->lock, iflag);21905			return;21906		}21907 21908		/* Avoid invalid access if an IO sneaks in and is being rejected21909		 * just _after_ xri pools are destroyed in lpfc_offline.21910		 * Nothing much can be done at this point.21911		 */21912		if (!qp->p_multixri_pool)21913			return;21914 21915		pbl_pool = &qp->p_multixri_pool->pbl_pool;21916		pvt_pool = &qp->p_multixri_pool->pvt_pool;21917 21918		txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;21919		abts_io_bufs = qp->abts_scsi_io_bufs;21920		abts_io_bufs += qp->abts_nvme_io_bufs;21921 21922		xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;21923		xri_limit = qp->p_multixri_pool->xri_limit;21924 21925#ifdef LPFC_MXP_STAT21926		if (xri_owned <= xri_limit)21927			qp->p_multixri_pool->below_limit_count++;21928		else21929			qp->p_multixri_pool->above_limit_count++;21930#endif21931 21932		/* XRI goes to either public or private free xri pool21933		 *     based on watermark and xri_limit21934		 */21935		if ((pvt_pool->count < pvt_pool->low_watermark) ||21936		    (xri_owned < xri_limit &&21937		     pvt_pool->count < pvt_pool->high_watermark)) {21938			lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,21939						  qp, free_pvt_pool);21940			list_add_tail(&lpfc_ncmd->list,21941				      &pvt_pool->list);21942			pvt_pool->count++;21943			spin_unlock_irqrestore(&pvt_pool->lock, iflag);21944		} else {21945			lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,21946						  qp, free_pub_pool);21947			list_add_tail(&lpfc_ncmd->list,21948				      &pbl_pool->list);21949			pbl_pool->count++;21950			spin_unlock_irqrestore(&pbl_pool->lock, iflag);21951		}21952	} else {21953		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,21954					  qp, free_xri);21955		list_add_tail(&lpfc_ncmd->list,21956			      &qp->lpfc_io_buf_list_put);21957		qp->put_io_bufs++;21958		spin_unlock_irqrestore(&qp->io_buf_list_put_lock,21959				       iflag);21960	}21961}21962 21963/**21964 * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool21965 * @phba: pointer to lpfc hba data structure.21966 * @qp: pointer to HDW queue21967 * @pvt_pool: pointer to private pool data structure.21968 * @ndlp: pointer to lpfc nodelist data structure.21969 *21970 * This routine tries to get one free IO buf from private pool.21971 *21972 * Return:21973 *   pointer to one free IO buf - if private pool is not empty21974 *   NULL - if private pool is empty21975 **/21976static struct lpfc_io_buf *21977lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,21978				  struct lpfc_sli4_hdw_queue *qp,21979				  struct lpfc_pvt_pool *pvt_pool,21980				  struct lpfc_nodelist *ndlp)21981{21982	struct lpfc_io_buf *lpfc_ncmd;21983	struct lpfc_io_buf *lpfc_ncmd_next;21984	unsigned long iflag;21985 21986	lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);21987	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,21988				 &pvt_pool->list, list) {21989		if (lpfc_test_rrq_active(21990			phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))21991			continue;21992		list_del(&lpfc_ncmd->list);21993		pvt_pool->count--;21994		spin_unlock_irqrestore(&pvt_pool->lock, iflag);21995		return lpfc_ncmd;21996	}21997	spin_unlock_irqrestore(&pvt_pool->lock, iflag);21998 21999	return NULL;22000}22001 22002/**22003 * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool22004 * @phba: pointer to lpfc hba data structure.22005 *22006 * This routine tries to get one free IO buf from expedite pool.22007 *22008 * Return:22009 *   pointer to one free IO buf - if expedite pool is not empty22010 *   NULL - if expedite pool is empty22011 **/22012static struct lpfc_io_buf *22013lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)22014{22015	struct lpfc_io_buf *lpfc_ncmd = NULL, *iter;22016	struct lpfc_io_buf *lpfc_ncmd_next;22017	unsigned long iflag;22018	struct lpfc_epd_pool *epd_pool;22019 22020	epd_pool = &phba->epd_pool;22021 22022	spin_lock_irqsave(&epd_pool->lock, iflag);22023	if (epd_pool->count > 0) {22024		list_for_each_entry_safe(iter, lpfc_ncmd_next,22025					 &epd_pool->list, list) {22026			list_del(&iter->list);22027			epd_pool->count--;22028			lpfc_ncmd = iter;22029			break;22030		}22031	}22032	spin_unlock_irqrestore(&epd_pool->lock, iflag);22033 22034	return lpfc_ncmd;22035}22036 22037/**22038 * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs22039 * @phba: pointer to lpfc hba data structure.22040 * @ndlp: pointer to lpfc nodelist data structure.22041 * @hwqid: belong to which HWQ22042 * @expedite: 1 means this request is urgent.22043 *22044 * This routine will do the following actions and then return a pointer to22045 * one free IO buf.22046 *22047 * 1. If private free xri count is empty, move some XRIs from public to22048 *    private pool.22049 * 2. Get one XRI from private free xri pool.22050 * 3. If we fail to get one from pvt_pool and this is an expedite request,22051 *    get one free xri from expedite pool.22052 *22053 * Note: ndlp is only used on SCSI side for RRQ testing.22054 *       The caller should pass NULL for ndlp on NVME side.22055 *22056 * Return:22057 *   pointer to one free IO buf - if private pool is not empty22058 *   NULL - if private pool is empty22059 **/22060static struct lpfc_io_buf *22061lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,22062				    struct lpfc_nodelist *ndlp,22063				    int hwqid, int expedite)22064{22065	struct lpfc_sli4_hdw_queue *qp;22066	struct lpfc_multixri_pool *multixri_pool;22067	struct lpfc_pvt_pool *pvt_pool;22068	struct lpfc_io_buf *lpfc_ncmd;22069 22070	qp = &phba->sli4_hba.hdwq[hwqid];22071	lpfc_ncmd = NULL;22072	if (!qp) {22073		lpfc_printf_log(phba, KERN_INFO,22074				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,22075				"5556 NULL qp for hwqid  x%x\n", hwqid);22076		return lpfc_ncmd;22077	}22078	multixri_pool = qp->p_multixri_pool;22079	if (!multixri_pool) {22080		lpfc_printf_log(phba, KERN_INFO,22081				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,22082				"5557 NULL multixri for hwqid  x%x\n", hwqid);22083		return lpfc_ncmd;22084	}22085	pvt_pool = &multixri_pool->pvt_pool;22086	if (!pvt_pool) {22087		lpfc_printf_log(phba, KERN_INFO,22088				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,22089				"5558 NULL pvt_pool for hwqid  x%x\n", hwqid);22090		return lpfc_ncmd;22091	}22092	multixri_pool->io_req_count++;22093 22094	/* If pvt_pool is empty, move some XRIs from public to private pool */22095	if (pvt_pool->count == 0)22096		lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);22097 22098	/* Get one XRI from private free xri pool */22099	lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);22100 22101	if (lpfc_ncmd) {22102		lpfc_ncmd->hdwq = qp;22103		lpfc_ncmd->hdwq_no = hwqid;22104	} else if (expedite) {22105		/* If we fail to get one from pvt_pool and this is an expedite22106		 * request, get one free xri from expedite pool.22107		 */22108		lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);22109	}22110 22111	return lpfc_ncmd;22112}22113 22114static inline struct lpfc_io_buf *22115lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)22116{22117	struct lpfc_sli4_hdw_queue *qp;22118	struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;22119 22120	qp = &phba->sli4_hba.hdwq[idx];22121	list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,22122				 &qp->lpfc_io_buf_list_get, list) {22123		if (lpfc_test_rrq_active(phba, ndlp,22124					 lpfc_cmd->cur_iocbq.sli4_lxritag))22125			continue;22126 22127		if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)22128			continue;22129 22130		list_del_init(&lpfc_cmd->list);22131		qp->get_io_bufs--;22132		lpfc_cmd->hdwq = qp;22133		lpfc_cmd->hdwq_no = idx;22134		return lpfc_cmd;22135	}22136	return NULL;22137}22138 22139/**22140 * lpfc_get_io_buf - Get one IO buffer from free pool22141 * @phba: The HBA for which this call is being executed.22142 * @ndlp: pointer to lpfc nodelist data structure.22143 * @hwqid: belong to which HWQ22144 * @expedite: 1 means this request is urgent.22145 *22146 * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,22147 * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes22148 * a IO buffer from head of @hdwq io_buf_list and returns to caller.22149 *22150 * Note: ndlp is only used on SCSI side for RRQ testing.22151 *       The caller should pass NULL for ndlp on NVME side.22152 *22153 * Return codes:22154 *   NULL - Error22155 *   Pointer to lpfc_io_buf - Success22156 **/22157struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,22158				    struct lpfc_nodelist *ndlp,22159				    u32 hwqid, int expedite)22160{22161	struct lpfc_sli4_hdw_queue *qp;22162	unsigned long iflag;22163	struct lpfc_io_buf *lpfc_cmd;22164 22165	qp = &phba->sli4_hba.hdwq[hwqid];22166	lpfc_cmd = NULL;22167	if (!qp) {22168		lpfc_printf_log(phba, KERN_WARNING,22169				LOG_SLI | LOG_NVME_ABTS | LOG_FCP,22170				"5555 NULL qp for hwqid  x%x\n", hwqid);22171		return lpfc_cmd;22172	}22173 22174	if (phba->cfg_xri_rebalancing)22175		lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(22176			phba, ndlp, hwqid, expedite);22177	else {22178		lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,22179					  qp, alloc_xri_get);22180		if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)22181			lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);22182		if (!lpfc_cmd) {22183			lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,22184					  qp, alloc_xri_put);22185			list_splice(&qp->lpfc_io_buf_list_put,22186				    &qp->lpfc_io_buf_list_get);22187			qp->get_io_bufs += qp->put_io_bufs;22188			INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);22189			qp->put_io_bufs = 0;22190			spin_unlock(&qp->io_buf_list_put_lock);22191			if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||22192			    expedite)22193				lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);22194		}22195		spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);22196	}22197 22198	return lpfc_cmd;22199}22200 22201/**22202 * lpfc_read_object - Retrieve object data from HBA22203 * @phba: The HBA for which this call is being executed.22204 * @rdobject: Pathname of object data we want to read.22205 * @datap: Pointer to where data will be copied to.22206 * @datasz: size of data area22207 *22208 * This routine is limited to object sizes of LPFC_BPL_SIZE (1024) or less.22209 * The data will be truncated if datasz is not large enough.22210 * Version 1 is not supported with Embedded mbox cmd, so we must use version 0.22211 * Returns the actual bytes read from the object.22212 *22213 * This routine is hard coded to use a poll completion.  Unlike other22214 * sli4_config mailboxes, it uses lpfc_mbuf memory which is not22215 * cleaned up in lpfc_sli4_cmd_mbox_free.  If this routine is modified22216 * to use interrupt-based completions, code is needed to fully cleanup22217 * the memory.22218 */22219int22220lpfc_read_object(struct lpfc_hba *phba, char *rdobject, uint32_t *datap,22221		 uint32_t datasz)22222{22223	struct lpfc_mbx_read_object *read_object;22224	LPFC_MBOXQ_t *mbox;22225	int rc, length, eof, j, byte_cnt = 0;22226	uint32_t shdr_status, shdr_add_status;22227	union lpfc_sli4_cfg_shdr *shdr;22228	struct lpfc_dmabuf *pcmd;22229	u32 rd_object_name[LPFC_MBX_OBJECT_NAME_LEN_DW] = {0};22230 22231	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);22232	if (!mbox)22233		return -ENOMEM;22234	length = (sizeof(struct lpfc_mbx_read_object) -22235		  sizeof(struct lpfc_sli4_cfg_mhdr));22236	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,22237			 LPFC_MBOX_OPCODE_READ_OBJECT,22238			 length, LPFC_SLI4_MBX_EMBED);22239	read_object = &mbox->u.mqe.un.read_object;22240	shdr = (union lpfc_sli4_cfg_shdr *)&read_object->header.cfg_shdr;22241 22242	bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_0);22243	bf_set(lpfc_mbx_rd_object_rlen, &read_object->u.request, datasz);22244	read_object->u.request.rd_object_offset = 0;22245	read_object->u.request.rd_object_cnt = 1;22246 22247	memset((void *)read_object->u.request.rd_object_name, 0,22248	       LPFC_OBJ_NAME_SZ);22249	scnprintf((char *)rd_object_name, sizeof(rd_object_name), rdobject);22250	for (j = 0; j < strlen(rdobject); j++)22251		read_object->u.request.rd_object_name[j] =22252			cpu_to_le32(rd_object_name[j]);22253 22254	pcmd = kmalloc(sizeof(*pcmd), GFP_KERNEL);22255	if (pcmd)22256		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);22257	if (!pcmd || !pcmd->virt) {22258		kfree(pcmd);22259		mempool_free(mbox, phba->mbox_mem_pool);22260		return -ENOMEM;22261	}22262	memset((void *)pcmd->virt, 0, LPFC_BPL_SIZE);22263	read_object->u.request.rd_object_hbuf[0].pa_lo =22264		putPaddrLow(pcmd->phys);22265	read_object->u.request.rd_object_hbuf[0].pa_hi =22266		putPaddrHigh(pcmd->phys);22267	read_object->u.request.rd_object_hbuf[0].length = LPFC_BPL_SIZE;22268 22269	mbox->vport = phba->pport;22270	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;22271	mbox->ctx_ndlp = NULL;22272 22273	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);22274	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);22275	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);22276 22277	if (shdr_status == STATUS_FAILED &&22278	    shdr_add_status == ADD_STATUS_INVALID_OBJECT_NAME) {22279		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,22280				"4674 No port cfg file in FW.\n");22281		byte_cnt = -ENOENT;22282	} else if (shdr_status || shdr_add_status || rc) {22283		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,22284				"2625 READ_OBJECT mailbox failed with "22285				"status x%x add_status x%x, mbx status x%x\n",22286				shdr_status, shdr_add_status, rc);22287		byte_cnt = -ENXIO;22288	} else {22289		/* Success */22290		length = read_object->u.response.rd_object_actual_rlen;22291		eof = bf_get(lpfc_mbx_rd_object_eof, &read_object->u.response);22292		lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_CGN_MGMT,22293				"2626 READ_OBJECT Success len %d:%d, EOF %d\n",22294				length, datasz, eof);22295 22296		/* Detect the port config file exists but is empty */22297		if (!length && eof) {22298			byte_cnt = 0;22299			goto exit;22300		}22301 22302		byte_cnt = length;22303		lpfc_sli_pcimem_bcopy(pcmd->virt, datap, byte_cnt);22304	}22305 22306 exit:22307	/* This is an embedded SLI4 mailbox with an external buffer allocated.22308	 * Free the pcmd and then cleanup with the correct routine.22309	 */22310	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);22311	kfree(pcmd);22312	lpfc_sli4_mbox_cmd_free(phba, mbox);22313	return byte_cnt;22314}22315 22316/**22317 * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool22318 * @phba: The HBA for which this call is being executed.22319 * @lpfc_buf: IO buf structure to append the SGL chunk22320 *22321 * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,22322 * and will allocate an SGL chunk if the pool is empty.22323 *22324 * Return codes:22325 *   NULL - Error22326 *   Pointer to sli4_hybrid_sgl - Success22327 **/22328struct sli4_hybrid_sgl *22329lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)22330{22331	struct sli4_hybrid_sgl *list_entry = NULL;22332	struct sli4_hybrid_sgl *tmp = NULL;22333	struct sli4_hybrid_sgl *allocated_sgl = NULL;22334	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;22335	struct list_head *buf_list = &hdwq->sgl_list;22336	unsigned long iflags;22337 22338	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);22339 22340	if (likely(!list_empty(buf_list))) {22341		/* break off 1 chunk from the sgl_list */22342		list_for_each_entry_safe(list_entry, tmp,22343					 buf_list, list_node) {22344			list_move_tail(&list_entry->list_node,22345				       &lpfc_buf->dma_sgl_xtra_list);22346			break;22347		}22348	} else {22349		/* allocate more */22350		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);22351		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,22352				   cpu_to_node(hdwq->io_wq->chann));22353		if (!tmp) {22354			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,22355					"8353 error kmalloc memory for HDWQ "22356					"%d %s\n",22357					lpfc_buf->hdwq_no, __func__);22358			return NULL;22359		}22360 22361		tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,22362					      GFP_ATOMIC, &tmp->dma_phys_sgl);22363		if (!tmp->dma_sgl) {22364			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,22365					"8354 error pool_alloc memory for HDWQ "22366					"%d %s\n",22367					lpfc_buf->hdwq_no, __func__);22368			kfree(tmp);22369			return NULL;22370		}22371 22372		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);22373		list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);22374	}22375 22376	allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,22377					struct sli4_hybrid_sgl,22378					list_node);22379 22380	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);22381 22382	return allocated_sgl;22383}22384 22385/**22386 * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool22387 * @phba: The HBA for which this call is being executed.22388 * @lpfc_buf: IO buf structure with the SGL chunk22389 *22390 * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.22391 *22392 * Return codes:22393 *   0 - Success22394 *   -EINVAL - Error22395 **/22396int22397lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)22398{22399	int rc = 0;22400	struct sli4_hybrid_sgl *list_entry = NULL;22401	struct sli4_hybrid_sgl *tmp = NULL;22402	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;22403	struct list_head *buf_list = &hdwq->sgl_list;22404	unsigned long iflags;22405 22406	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);22407 22408	if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {22409		list_for_each_entry_safe(list_entry, tmp,22410					 &lpfc_buf->dma_sgl_xtra_list,22411					 list_node) {22412			list_move_tail(&list_entry->list_node,22413				       buf_list);22414		}22415	} else {22416		rc = -EINVAL;22417	}22418 22419	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);22420	return rc;22421}22422 22423/**22424 * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool22425 * @phba: phba object22426 * @hdwq: hdwq to cleanup sgl buff resources on22427 *22428 * This routine frees all SGL chunks of hdwq SGL chunk pool.22429 *22430 * Return codes:22431 *   None22432 **/22433void22434lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,22435		       struct lpfc_sli4_hdw_queue *hdwq)22436{22437	struct list_head *buf_list = &hdwq->sgl_list;22438	struct sli4_hybrid_sgl *list_entry = NULL;22439	struct sli4_hybrid_sgl *tmp = NULL;22440	unsigned long iflags;22441 22442	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);22443 22444	/* Free sgl pool */22445	list_for_each_entry_safe(list_entry, tmp,22446				 buf_list, list_node) {22447		list_del(&list_entry->list_node);22448		dma_pool_free(phba->lpfc_sg_dma_buf_pool,22449			      list_entry->dma_sgl,22450			      list_entry->dma_phys_sgl);22451		kfree(list_entry);22452	}22453 22454	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);22455}22456 22457/**22458 * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq22459 * @phba: The HBA for which this call is being executed.22460 * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer22461 *22462 * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,22463 * and will allocate an CMD/RSP buffer if the pool is empty.22464 *22465 * Return codes:22466 *   NULL - Error22467 *   Pointer to fcp_cmd_rsp_buf - Success22468 **/22469struct fcp_cmd_rsp_buf *22470lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,22471			      struct lpfc_io_buf *lpfc_buf)22472{22473	struct fcp_cmd_rsp_buf *list_entry = NULL;22474	struct fcp_cmd_rsp_buf *tmp = NULL;22475	struct fcp_cmd_rsp_buf *allocated_buf = NULL;22476	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;22477	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;22478	unsigned long iflags;22479 22480	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);22481 22482	if (likely(!list_empty(buf_list))) {22483		/* break off 1 chunk from the list */22484		list_for_each_entry_safe(list_entry, tmp,22485					 buf_list,22486					 list_node) {22487			list_move_tail(&list_entry->list_node,22488				       &lpfc_buf->dma_cmd_rsp_list);22489			break;22490		}22491	} else {22492		/* allocate more */22493		spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);22494		tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,22495				   cpu_to_node(hdwq->io_wq->chann));22496		if (!tmp) {22497			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,22498					"8355 error kmalloc memory for HDWQ "22499					"%d %s\n",22500					lpfc_buf->hdwq_no, __func__);22501			return NULL;22502		}22503 22504		tmp->fcp_cmnd = dma_pool_zalloc(phba->lpfc_cmd_rsp_buf_pool,22505						GFP_ATOMIC,22506						&tmp->fcp_cmd_rsp_dma_handle);22507 22508		if (!tmp->fcp_cmnd) {22509			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,22510					"8356 error pool_alloc memory for HDWQ "22511					"%d %s\n",22512					lpfc_buf->hdwq_no, __func__);22513			kfree(tmp);22514			return NULL;22515		}22516 22517		tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +22518				sizeof(struct fcp_cmnd32));22519 22520		spin_lock_irqsave(&hdwq->hdwq_lock, iflags);22521		list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);22522	}22523 22524	allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,22525					struct fcp_cmd_rsp_buf,22526					list_node);22527 22528	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);22529 22530	return allocated_buf;22531}22532 22533/**22534 * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool22535 * @phba: The HBA for which this call is being executed.22536 * @lpfc_buf: IO buf structure with the CMD/RSP buf22537 *22538 * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.22539 *22540 * Return codes:22541 *   0 - Success22542 *   -EINVAL - Error22543 **/22544int22545lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,22546			      struct lpfc_io_buf *lpfc_buf)22547{22548	int rc = 0;22549	struct fcp_cmd_rsp_buf *list_entry = NULL;22550	struct fcp_cmd_rsp_buf *tmp = NULL;22551	struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;22552	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;22553	unsigned long iflags;22554 22555	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);22556 22557	if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {22558		list_for_each_entry_safe(list_entry, tmp,22559					 &lpfc_buf->dma_cmd_rsp_list,22560					 list_node) {22561			list_move_tail(&list_entry->list_node,22562				       buf_list);22563		}22564	} else {22565		rc = -EINVAL;22566	}22567 22568	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);22569	return rc;22570}22571 22572/**22573 * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool22574 * @phba: phba object22575 * @hdwq: hdwq to cleanup cmd rsp buff resources on22576 *22577 * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.22578 *22579 * Return codes:22580 *   None22581 **/22582void22583lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,22584			       struct lpfc_sli4_hdw_queue *hdwq)22585{22586	struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;22587	struct fcp_cmd_rsp_buf *list_entry = NULL;22588	struct fcp_cmd_rsp_buf *tmp = NULL;22589	unsigned long iflags;22590 22591	spin_lock_irqsave(&hdwq->hdwq_lock, iflags);22592 22593	/* Free cmd_rsp buf pool */22594	list_for_each_entry_safe(list_entry, tmp,22595				 buf_list,22596				 list_node) {22597		list_del(&list_entry->list_node);22598		dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,22599			      list_entry->fcp_cmnd,22600			      list_entry->fcp_cmd_rsp_dma_handle);22601		kfree(list_entry);22602	}22603 22604	spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);22605}22606 22607/**22608 * lpfc_sli_prep_wqe - Prepare WQE for the command to be posted22609 * @phba: phba object22610 * @job: job entry of the command to be posted.22611 *22612 * Fill the common fields of the wqe for each of the command.22613 *22614 * Return codes:22615 *	None22616 **/22617void22618lpfc_sli_prep_wqe(struct lpfc_hba *phba, struct lpfc_iocbq *job)22619{22620	u8 cmnd;22621	u32 *pcmd;22622	u32 if_type = 0;22623	u32 abort_tag;22624	bool fip;22625	struct lpfc_nodelist *ndlp = NULL;22626	union lpfc_wqe128 *wqe = &job->wqe;22627	u8 command_type = ELS_COMMAND_NON_FIP;22628 22629	fip = test_bit(HBA_FIP_SUPPORT, &phba->hba_flag);22630	/* The fcp commands will set command type */22631	if (job->cmd_flag &  LPFC_IO_FCP)22632		command_type = FCP_COMMAND;22633	else if (fip && (job->cmd_flag & LPFC_FIP_ELS_ID_MASK))22634		command_type = ELS_COMMAND_FIP;22635	else22636		command_type = ELS_COMMAND_NON_FIP;22637 22638	abort_tag = job->iotag;22639	cmnd = bf_get(wqe_cmnd, &wqe->els_req.wqe_com);22640 22641	switch (cmnd) {22642	case CMD_ELS_REQUEST64_WQE:22643		ndlp = job->ndlp;22644 22645		if_type = bf_get(lpfc_sli_intf_if_type,22646				 &phba->sli4_hba.sli_intf);22647		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {22648			pcmd = (u32 *)job->cmd_dmabuf->virt;22649			if (pcmd && (*pcmd == ELS_CMD_FLOGI ||22650				     *pcmd == ELS_CMD_SCR ||22651				     *pcmd == ELS_CMD_RDF ||22652				     *pcmd == ELS_CMD_EDC ||22653				     *pcmd == ELS_CMD_RSCN_XMT ||22654				     *pcmd == ELS_CMD_FDISC ||22655				     *pcmd == ELS_CMD_LOGO ||22656				     *pcmd == ELS_CMD_QFPA ||22657				     *pcmd == ELS_CMD_UVEM ||22658				     *pcmd == ELS_CMD_PLOGI)) {22659				bf_set(els_req64_sp, &wqe->els_req, 1);22660				bf_set(els_req64_sid, &wqe->els_req,22661				       job->vport->fc_myDID);22662 22663				if ((*pcmd == ELS_CMD_FLOGI) &&22664				    !(phba->fc_topology ==22665				      LPFC_TOPOLOGY_LOOP))22666					bf_set(els_req64_sid, &wqe->els_req, 0);22667 22668				bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);22669				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,22670				       phba->vpi_ids[job->vport->vpi]);22671			} else if (pcmd) {22672				bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);22673				bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,22674				       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);22675			}22676		}22677 22678		bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,22679		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);22680 22681		bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);22682		bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);22683		bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);22684		bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);22685		bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);22686		break;22687	case CMD_XMIT_ELS_RSP64_WQE:22688		ndlp = job->ndlp;22689 22690		/* word4 */22691		wqe->xmit_els_rsp.word4 = 0;22692 22693		if_type = bf_get(lpfc_sli_intf_if_type,22694				 &phba->sli4_hba.sli_intf);22695		if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {22696			if (test_bit(FC_PT2PT, &job->vport->fc_flag)) {22697				bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);22698				bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,22699				       job->vport->fc_myDID);22700				if (job->vport->fc_myDID == Fabric_DID) {22701					bf_set(wqe_els_did,22702					       &wqe->xmit_els_rsp.wqe_dest, 0);22703				}22704			}22705		}22706 22707		bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);22708		bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);22709		bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);22710		bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,22711		       LPFC_WQE_LENLOC_WORD3);22712		bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);22713 22714		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {22715			bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);22716			bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,22717			       job->vport->fc_myDID);22718			bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);22719		}22720 22721		if (phba->sli_rev == LPFC_SLI_REV4) {22722			bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,22723			       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);22724 22725			if (bf_get(wqe_ct, &wqe->xmit_els_rsp.wqe_com))22726				bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,22727				       phba->vpi_ids[job->vport->vpi]);22728		}22729		command_type = OTHER_COMMAND;22730		break;22731	case CMD_GEN_REQUEST64_WQE:22732		/* Word 10 */22733		bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);22734		bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);22735		bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);22736		bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);22737		bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);22738		command_type = OTHER_COMMAND;22739		break;22740	case CMD_XMIT_SEQUENCE64_WQE:22741		if (phba->link_flag & LS_LOOPBACK_MODE)22742			bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);22743 22744		wqe->xmit_sequence.rsvd3 = 0;22745		bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);22746		bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);22747		bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,22748		       LPFC_WQE_IOD_WRITE);22749		bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,22750		       LPFC_WQE_LENLOC_WORD12);22751		bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);22752		command_type = OTHER_COMMAND;22753		break;22754	case CMD_XMIT_BLS_RSP64_WQE:22755		bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);22756		bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);22757		bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);22758		bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,22759		       phba->vpi_ids[phba->pport->vpi]);22760		bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);22761		bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,22762		       LPFC_WQE_LENLOC_NONE);22763		/* Overwrite the pre-set comnd type with OTHER_COMMAND */22764		command_type = OTHER_COMMAND;22765		break;22766	case CMD_FCP_ICMND64_WQE:	/* task mgmt commands */22767	case CMD_ABORT_XRI_WQE:		/* abort iotag */22768	case CMD_SEND_FRAME:		/* mds loopback */22769		/* cases already formatted for sli4 wqe - no chgs necessary */22770		return;22771	default:22772		dump_stack();22773		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,22774				"6207 Invalid command 0x%x\n",22775				cmnd);22776		break;22777	}22778 22779	wqe->generic.wqe_com.abort_tag = abort_tag;22780	bf_set(wqe_reqtag, &wqe->generic.wqe_com, job->iotag);22781	bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);22782	bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);22783}22784