blob: 7d1c32d5d30774a05c62c41ece04ae9485a56e52 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart145e5a82020-01-27 16:23:12 -08004 * Copyright (C) 2017-2020 Broadcom. All Rights Reserved. The term *
James Smart3e21d1c2018-05-04 20:37:59 -07005 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * 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. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
23
dea31012005-04-17 16:05:31 -050024#include <linux/blkdev.h>
25#include <linux/pci.h>
26#include <linux/interrupt.h>
27#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010029#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050030
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040031#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050032#include <scsi/scsi_cmnd.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040035#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040036#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040037#include <linux/aer.h>
Dick Kennedy86ee57a2020-06-30 14:49:55 -070038#include <linux/crash_dump.h>
James Smart1351e692018-02-22 08:18:43 -080039#ifdef CONFIG_X86
40#include <asm/set_memory.h>
41#endif
dea31012005-04-17 16:05:31 -050042
James Smartda0436e2009-05-22 14:51:39 -040043#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_hw.h"
45#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040046#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040047#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050049#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080050#include "lpfc_scsi.h"
51#include "lpfc_nvme.h"
dea31012005-04-17 16:05:31 -050052#include "lpfc_crtn.h"
53#include "lpfc_logmsg.h"
54#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050055#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040056#include "lpfc_vport.h"
James Smart61bda8f2016-10-13 15:06:05 -070057#include "lpfc_version.h"
dea31012005-04-17 16:05:31 -050058
59/* There are only four IOCB completion types. */
60typedef enum _lpfc_iocb_type {
61 LPFC_UNKNOWN_IOCB,
62 LPFC_UNSOL_IOCB,
63 LPFC_SOL_IOCB,
64 LPFC_ABORT_IOCB
65} lpfc_iocb_type;
66
James Smart4f774512009-05-22 14:52:35 -040067
68/* Provide function prototypes local to this module. */
69static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
70 uint32_t);
71static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040072 uint8_t *, uint32_t *);
73static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
74 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040075static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
76 struct hbq_dmabuf *);
James Smartae9e28f2017-05-15 15:20:51 -070077static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
78 struct hbq_dmabuf *dmabuf);
James Smart32517fc2019-01-28 11:14:33 -080079static bool lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba,
80 struct lpfc_queue *cq, struct lpfc_cqe *cqe);
James Smart895427b2017-02-12 13:52:30 -080081static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
James Smart8a9d2e82012-05-09 21:16:12 -040082 int);
Dick Kennedyf485c182017-09-29 17:34:34 -070083static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
James Smart32517fc2019-01-28 11:14:33 -080084 struct lpfc_queue *eq,
85 struct lpfc_eqe *eqe);
James Smarte8d3c3b2013-10-10 12:21:30 -040086static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
87static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart24c7c0a2019-09-21 20:58:58 -070088static struct lpfc_cqe *lpfc_sli4_cq_get(struct lpfc_queue *q);
89static void __lpfc_sli4_consume_cqe(struct lpfc_hba *phba,
90 struct lpfc_queue *cq,
91 struct lpfc_cqe *cqe);
James Smart05580562011-05-24 11:40:48 -040092
James Smart4f774512009-05-22 14:52:35 -040093static IOCB_t *
94lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
95{
96 return &iocbq->iocb;
97}
98
James Smart48f8fdb2018-05-04 20:37:51 -070099#if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
100/**
101 * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
102 * @srcp: Source memory pointer.
103 * @destp: Destination memory pointer.
104 * @cnt: Number of words required to be copied.
105 * Must be a multiple of sizeof(uint64_t)
106 *
107 * This function is used for copying data between driver memory
108 * and the SLI WQ. This function also changes the endianness
109 * of each word if native endianness is different from SLI
110 * endianness. This function can be called with or without
111 * lock.
112 **/
YueHaibingd7b761b2019-05-31 23:28:41 +0800113static void
James Smart48f8fdb2018-05-04 20:37:51 -0700114lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
115{
116 uint64_t *src = srcp;
117 uint64_t *dest = destp;
118 int i;
119
120 for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
121 *dest++ = *src++;
122}
123#else
124#define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
125#endif
126
James Smart4f774512009-05-22 14:52:35 -0400127/**
128 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
129 * @q: The Work Queue to operate on.
130 * @wqe: The work Queue Entry to put on the Work queue.
131 *
132 * This routine will copy the contents of @wqe to the next available entry on
133 * the @q. This function will then ring the Work Queue Doorbell to signal the
134 * HBA to start processing the Work Queue Entry. This function returns 0 if
135 * successful. If no entries are available on @q then this function will return
136 * -ENOMEM.
137 * The caller is expected to hold the hbalock when calling this routine.
138 **/
Dick Kennedycd22d602017-08-23 16:55:35 -0700139static int
James Smart205e8242018-03-05 12:04:03 -0800140lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -0400141{
James Smart2e90f4b2011-12-13 13:22:37 -0500142 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400143 struct lpfc_register doorbell;
144 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400145 uint32_t idx;
James Smart1351e692018-02-22 08:18:43 -0800146 uint32_t i = 0;
147 uint8_t *tmp;
James Smart5cc167d2018-06-26 08:24:23 -0700148 u32 if_type;
James Smart4f774512009-05-22 14:52:35 -0400149
James Smart2e90f4b2011-12-13 13:22:37 -0500150 /* sanity check on queue memory */
151 if (unlikely(!q))
152 return -ENOMEM;
James Smart9afbee32019-03-12 16:30:28 -0700153 temp_wqe = lpfc_sli4_qe(q, q->host_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500154
James Smart4f774512009-05-22 14:52:35 -0400155 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400156 idx = ((q->host_index + 1) % q->entry_count);
157 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400158 q->WQ_overflow++;
Dick Kennedycd22d602017-08-23 16:55:35 -0700159 return -EBUSY;
James Smartb84daac2012-08-03 12:35:13 -0400160 }
161 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400162 /* set consumption flag every once in a while */
James Smart32517fc2019-01-28 11:14:33 -0800163 if (!((q->host_index + 1) % q->notify_interval))
James Smartf0d9bcc2010-10-22 11:07:09 -0400164 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smart04673e32018-01-30 15:58:45 -0800165 else
166 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
James Smartfedd3b72011-02-16 12:39:24 -0500167 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
168 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart48f8fdb2018-05-04 20:37:51 -0700169 lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart1351e692018-02-22 08:18:43 -0800170 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
171 /* write to DPP aperture taking advatage of Combined Writes */
James Smart4c066192018-03-05 10:29:03 -0800172 tmp = (uint8_t *)temp_wqe;
173#ifdef __raw_writeq
James Smart1351e692018-02-22 08:18:43 -0800174 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
James Smart4c066192018-03-05 10:29:03 -0800175 __raw_writeq(*((uint64_t *)(tmp + i)),
176 q->dpp_regaddr + i);
177#else
178 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
179 __raw_writel(*((uint32_t *)(tmp + i)),
180 q->dpp_regaddr + i);
181#endif
James Smart1351e692018-02-22 08:18:43 -0800182 }
183 /* ensure WQE bcopy and DPP flushed before doorbell write */
James Smart6b3b3bd2016-12-19 15:07:30 -0800184 wmb();
James Smart4f774512009-05-22 14:52:35 -0400185
186 /* Update the host index before invoking device */
187 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400188
189 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400190
191 /* Ring Doorbell */
192 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500193 if (q->db_format == LPFC_DB_LIST_FORMAT) {
James Smart1351e692018-02-22 08:18:43 -0800194 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
195 bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
196 bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
197 bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
198 q->dpp_id);
199 bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
200 q->queue_id);
201 } else {
202 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
James Smart1351e692018-02-22 08:18:43 -0800203 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
James Smart5cc167d2018-06-26 08:24:23 -0700204
205 /* Leave bits <23:16> clear for if_type 6 dpp */
206 if_type = bf_get(lpfc_sli_intf_if_type,
207 &q->phba->sli4_hba.sli_intf);
208 if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
209 bf_set(lpfc_wq_db_list_fm_index, &doorbell,
210 host_index);
James Smart1351e692018-02-22 08:18:43 -0800211 }
James Smart962bc512013-01-03 15:44:00 -0500212 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
213 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
214 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
215 } else {
216 return -EINVAL;
217 }
218 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400219
220 return 0;
221}
222
223/**
224 * lpfc_sli4_wq_release - Updates internal hba index for WQ
225 * @q: The Work Queue to operate on.
226 * @index: The index to advance the hba index to.
227 *
228 * This routine will update the HBA index of a queue to reflect consumption of
229 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
230 * an entry the host calls this function to update the queue's internal
James Smart1543af32020-03-22 11:12:58 -0700231 * pointers.
James Smart4f774512009-05-22 14:52:35 -0400232 **/
James Smart1543af32020-03-22 11:12:58 -0700233static void
James Smart4f774512009-05-22 14:52:35 -0400234lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
235{
James Smart2e90f4b2011-12-13 13:22:37 -0500236 /* sanity check on queue memory */
237 if (unlikely(!q))
James Smart1543af32020-03-22 11:12:58 -0700238 return;
James Smart2e90f4b2011-12-13 13:22:37 -0500239
James Smart1543af32020-03-22 11:12:58 -0700240 q->hba_index = index;
James Smart4f774512009-05-22 14:52:35 -0400241}
242
243/**
244 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
245 * @q: The Mailbox Queue to operate on.
246 * @wqe: The Mailbox Queue Entry to put on the Work queue.
247 *
248 * This routine will copy the contents of @mqe to the next available entry on
249 * the @q. This function will then ring the Work Queue Doorbell to signal the
250 * HBA to start processing the Work Queue Entry. This function returns 0 if
251 * successful. If no entries are available on @q then this function will return
252 * -ENOMEM.
253 * The caller is expected to hold the hbalock when calling this routine.
254 **/
255static uint32_t
256lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
257{
James Smart2e90f4b2011-12-13 13:22:37 -0500258 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400259 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400260
James Smart2e90f4b2011-12-13 13:22:37 -0500261 /* sanity check on queue memory */
262 if (unlikely(!q))
263 return -ENOMEM;
James Smart9afbee32019-03-12 16:30:28 -0700264 temp_mqe = lpfc_sli4_qe(q, q->host_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500265
James Smart4f774512009-05-22 14:52:35 -0400266 /* If the host has not yet processed the next entry then we are done */
267 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
268 return -ENOMEM;
James Smart48f8fdb2018-05-04 20:37:51 -0700269 lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400270 /* Save off the mailbox pointer for completion */
271 q->phba->mbox = (MAILBOX_t *)temp_mqe;
272
273 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400274 q->host_index = ((q->host_index + 1) % q->entry_count);
275
276 /* Ring Doorbell */
277 doorbell.word0 = 0;
278 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
279 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
280 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400281 return 0;
282}
283
284/**
285 * lpfc_sli4_mq_release - Updates internal hba index for MQ
286 * @q: The Mailbox Queue to operate on.
287 *
288 * This routine will update the HBA index of a queue to reflect consumption of
289 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
290 * an entry the host calls this function to update the queue's internal
291 * pointers. This routine returns the number of entries that were consumed by
292 * the HBA.
293 **/
294static uint32_t
295lpfc_sli4_mq_release(struct lpfc_queue *q)
296{
James Smart2e90f4b2011-12-13 13:22:37 -0500297 /* sanity check on queue memory */
298 if (unlikely(!q))
299 return 0;
300
James Smart4f774512009-05-22 14:52:35 -0400301 /* Clear the mailbox pointer for completion */
302 q->phba->mbox = NULL;
303 q->hba_index = ((q->hba_index + 1) % q->entry_count);
304 return 1;
305}
306
307/**
308 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
309 * @q: The Event Queue to get the first valid EQE from
310 *
311 * This routine will get the first valid Event Queue Entry from @q, update
312 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
313 * the Queue (no more work to do), or the Queue is full of EQEs that have been
314 * processed, but not popped back to the HBA then this routine will return NULL.
315 **/
316static struct lpfc_eqe *
317lpfc_sli4_eq_get(struct lpfc_queue *q)
318{
James Smart2e90f4b2011-12-13 13:22:37 -0500319 struct lpfc_eqe *eqe;
320
321 /* sanity check on queue memory */
322 if (unlikely(!q))
323 return NULL;
James Smart9afbee32019-03-12 16:30:28 -0700324 eqe = lpfc_sli4_qe(q, q->host_index);
James Smart4f774512009-05-22 14:52:35 -0400325
326 /* If the next EQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800327 if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400328 return NULL;
James Smart27f344e2014-05-07 17:16:46 -0400329
330 /*
331 * insert barrier for instruction interlock : data from the hardware
332 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800333 * upon. Speculative instructions were allowing a bcopy at the start
334 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
335 * after our return, to copy data before the valid bit check above
336 * was done. As such, some of the copied data was stale. The barrier
337 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400338 */
339 mb();
James Smart4f774512009-05-22 14:52:35 -0400340 return eqe;
341}
342
343/**
James Smartba20c852012-08-03 12:36:52 -0400344 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
345 * @q: The Event Queue to disable interrupts
346 *
347 **/
James Smart92f3b322019-03-20 10:44:22 -0700348void
James Smartba20c852012-08-03 12:36:52 -0400349lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
350{
351 struct lpfc_register doorbell;
352
353 doorbell.word0 = 0;
354 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
355 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
356 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
357 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
358 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800359 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smartba20c852012-08-03 12:36:52 -0400360}
361
362/**
James Smart27d6ac02018-02-22 08:18:42 -0800363 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
364 * @q: The Event Queue to disable interrupts
365 *
366 **/
James Smart92f3b322019-03-20 10:44:22 -0700367void
James Smart27d6ac02018-02-22 08:18:42 -0800368lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
369{
370 struct lpfc_register doorbell;
371
372 doorbell.word0 = 0;
James Smartaad59d52018-09-10 10:30:47 -0700373 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
James Smart27d6ac02018-02-22 08:18:42 -0800374 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
375}
376
377/**
James Smart32517fc2019-01-28 11:14:33 -0800378 * lpfc_sli4_write_eq_db - write EQ DB for eqe's consumed or arm state
379 * @phba: adapter with EQ
James Smart4f774512009-05-22 14:52:35 -0400380 * @q: The Event Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800381 * @count: Number of elements that have been consumed
James Smart4f774512009-05-22 14:52:35 -0400382 * @arm: Indicates whether the host wants to arms this CQ.
383 *
James Smart32517fc2019-01-28 11:14:33 -0800384 * This routine will notify the HBA, by ringing the doorbell, that count
385 * number of EQEs have been processed. The @arm parameter indicates whether
386 * the queue should be rearmed when ringing the doorbell.
James Smart4f774512009-05-22 14:52:35 -0400387 **/
James Smart32517fc2019-01-28 11:14:33 -0800388void
389lpfc_sli4_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
390 uint32_t count, bool arm)
James Smart4f774512009-05-22 14:52:35 -0400391{
James Smart4f774512009-05-22 14:52:35 -0400392 struct lpfc_register doorbell;
393
James Smart2e90f4b2011-12-13 13:22:37 -0500394 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800395 if (unlikely(!q || (count == 0 && !arm)))
396 return;
James Smart4f774512009-05-22 14:52:35 -0400397
398 /* ring doorbell for number popped */
399 doorbell.word0 = 0;
400 if (arm) {
401 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
402 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
403 }
James Smart32517fc2019-01-28 11:14:33 -0800404 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
James Smart4f774512009-05-22 14:52:35 -0400405 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500406 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
407 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
408 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800409 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500410 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
411 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
James Smart9dd35422018-02-22 08:18:41 -0800412 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400413}
414
415/**
James Smart32517fc2019-01-28 11:14:33 -0800416 * lpfc_sli4_if6_write_eq_db - write EQ DB for eqe's consumed or arm state
417 * @phba: adapter with EQ
James Smart27d6ac02018-02-22 08:18:42 -0800418 * @q: The Event Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800419 * @count: Number of elements that have been consumed
James Smart27d6ac02018-02-22 08:18:42 -0800420 * @arm: Indicates whether the host wants to arms this CQ.
421 *
James Smart32517fc2019-01-28 11:14:33 -0800422 * This routine will notify the HBA, by ringing the doorbell, that count
423 * number of EQEs have been processed. The @arm parameter indicates whether
424 * the queue should be rearmed when ringing the doorbell.
James Smart27d6ac02018-02-22 08:18:42 -0800425 **/
James Smart32517fc2019-01-28 11:14:33 -0800426void
427lpfc_sli4_if6_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
428 uint32_t count, bool arm)
James Smart27d6ac02018-02-22 08:18:42 -0800429{
James Smart27d6ac02018-02-22 08:18:42 -0800430 struct lpfc_register doorbell;
431
432 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800433 if (unlikely(!q || (count == 0 && !arm)))
434 return;
James Smart27d6ac02018-02-22 08:18:42 -0800435
436 /* ring doorbell for number popped */
437 doorbell.word0 = 0;
438 if (arm)
439 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800440 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, count);
James Smart27d6ac02018-02-22 08:18:42 -0800441 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
442 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
443 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
444 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
445 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart32517fc2019-01-28 11:14:33 -0800446}
447
448static void
449__lpfc_sli4_consume_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
450 struct lpfc_eqe *eqe)
451{
452 if (!phba->sli4_hba.pc_sli4_params.eqav)
453 bf_set_le32(lpfc_eqe_valid, eqe, 0);
454
455 eq->host_index = ((eq->host_index + 1) % eq->entry_count);
456
457 /* if the index wrapped around, toggle the valid bit */
458 if (phba->sli4_hba.pc_sli4_params.eqav && !eq->host_index)
459 eq->qe_valid = (eq->qe_valid) ? 0 : 1;
460}
461
462static void
James Smart24c7c0a2019-09-21 20:58:58 -0700463lpfc_sli4_eqcq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
James Smart32517fc2019-01-28 11:14:33 -0800464{
James Smart24c7c0a2019-09-21 20:58:58 -0700465 struct lpfc_eqe *eqe = NULL;
466 u32 eq_count = 0, cq_count = 0;
467 struct lpfc_cqe *cqe = NULL;
468 struct lpfc_queue *cq = NULL, *childq = NULL;
469 int cqid = 0;
James Smart32517fc2019-01-28 11:14:33 -0800470
471 /* walk all the EQ entries and drop on the floor */
472 eqe = lpfc_sli4_eq_get(eq);
473 while (eqe) {
James Smart24c7c0a2019-09-21 20:58:58 -0700474 /* Get the reference to the corresponding CQ */
475 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
476 cq = NULL;
477
478 list_for_each_entry(childq, &eq->child_list, list) {
479 if (childq->queue_id == cqid) {
480 cq = childq;
481 break;
482 }
483 }
484 /* If CQ is valid, iterate through it and drop all the CQEs */
485 if (cq) {
486 cqe = lpfc_sli4_cq_get(cq);
487 while (cqe) {
488 __lpfc_sli4_consume_cqe(phba, cq, cqe);
489 cq_count++;
490 cqe = lpfc_sli4_cq_get(cq);
491 }
492 /* Clear and re-arm the CQ */
493 phba->sli4_hba.sli4_write_cq_db(phba, cq, cq_count,
494 LPFC_QUEUE_REARM);
495 cq_count = 0;
496 }
James Smart32517fc2019-01-28 11:14:33 -0800497 __lpfc_sli4_consume_eqe(phba, eq, eqe);
James Smart24c7c0a2019-09-21 20:58:58 -0700498 eq_count++;
James Smart32517fc2019-01-28 11:14:33 -0800499 eqe = lpfc_sli4_eq_get(eq);
500 }
501
502 /* Clear and re-arm the EQ */
James Smart24c7c0a2019-09-21 20:58:58 -0700503 phba->sli4_hba.sli4_write_eq_db(phba, eq, eq_count, LPFC_QUEUE_REARM);
James Smart32517fc2019-01-28 11:14:33 -0800504}
505
506static int
James Smart93a4d6f2019-11-04 16:57:05 -0800507lpfc_sli4_process_eq(struct lpfc_hba *phba, struct lpfc_queue *eq,
508 uint8_t rearm)
James Smart32517fc2019-01-28 11:14:33 -0800509{
510 struct lpfc_eqe *eqe;
511 int count = 0, consumed = 0;
512
513 if (cmpxchg(&eq->queue_claimed, 0, 1) != 0)
514 goto rearm_and_exit;
515
516 eqe = lpfc_sli4_eq_get(eq);
517 while (eqe) {
518 lpfc_sli4_hba_handle_eqe(phba, eq, eqe);
519 __lpfc_sli4_consume_eqe(phba, eq, eqe);
520
521 consumed++;
522 if (!(++count % eq->max_proc_limit))
523 break;
524
525 if (!(count % eq->notify_interval)) {
526 phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed,
527 LPFC_QUEUE_NOARM);
528 consumed = 0;
529 }
530
531 eqe = lpfc_sli4_eq_get(eq);
532 }
533 eq->EQ_processed += count;
534
535 /* Track the max number of EQEs processed in 1 intr */
536 if (count > eq->EQ_max_eqe)
537 eq->EQ_max_eqe = count;
538
Dick Kennedy164ba8d2020-05-01 14:43:03 -0700539 xchg(&eq->queue_claimed, 0);
James Smart32517fc2019-01-28 11:14:33 -0800540
541rearm_and_exit:
James Smart93a4d6f2019-11-04 16:57:05 -0800542 /* Always clear the EQ. */
543 phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed, rearm);
James Smart32517fc2019-01-28 11:14:33 -0800544
545 return count;
James Smart27d6ac02018-02-22 08:18:42 -0800546}
547
548/**
James Smart4f774512009-05-22 14:52:35 -0400549 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
550 * @q: The Completion Queue to get the first valid CQE from
551 *
552 * This routine will get the first valid Completion Queue Entry from @q, update
553 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
554 * the Queue (no more work to do), or the Queue is full of CQEs that have been
555 * processed, but not popped back to the HBA then this routine will return NULL.
556 **/
557static struct lpfc_cqe *
558lpfc_sli4_cq_get(struct lpfc_queue *q)
559{
560 struct lpfc_cqe *cqe;
561
James Smart2e90f4b2011-12-13 13:22:37 -0500562 /* sanity check on queue memory */
563 if (unlikely(!q))
564 return NULL;
James Smart9afbee32019-03-12 16:30:28 -0700565 cqe = lpfc_sli4_qe(q, q->host_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500566
James Smart4f774512009-05-22 14:52:35 -0400567 /* If the next CQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800568 if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400569 return NULL;
James Smart27f344e2014-05-07 17:16:46 -0400570
571 /*
572 * insert barrier for instruction interlock : data from the hardware
573 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800574 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
575 * instructions allowing action on content before valid bit checked,
576 * add barrier here as well. May not be needed as "content" is a
577 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400578 */
579 mb();
James Smart4f774512009-05-22 14:52:35 -0400580 return cqe;
581}
582
James Smart32517fc2019-01-28 11:14:33 -0800583static void
584__lpfc_sli4_consume_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
585 struct lpfc_cqe *cqe)
586{
587 if (!phba->sli4_hba.pc_sli4_params.cqav)
588 bf_set_le32(lpfc_cqe_valid, cqe, 0);
589
590 cq->host_index = ((cq->host_index + 1) % cq->entry_count);
591
592 /* if the index wrapped around, toggle the valid bit */
593 if (phba->sli4_hba.pc_sli4_params.cqav && !cq->host_index)
594 cq->qe_valid = (cq->qe_valid) ? 0 : 1;
595}
596
James Smart4f774512009-05-22 14:52:35 -0400597/**
James Smart32517fc2019-01-28 11:14:33 -0800598 * lpfc_sli4_write_cq_db - write cq DB for entries consumed or arm state.
599 * @phba: the adapter with the CQ
James Smart4f774512009-05-22 14:52:35 -0400600 * @q: The Completion Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800601 * @count: the number of elements that were consumed
James Smart4f774512009-05-22 14:52:35 -0400602 * @arm: Indicates whether the host wants to arms this CQ.
603 *
James Smart32517fc2019-01-28 11:14:33 -0800604 * This routine will notify the HBA, by ringing the doorbell, that the
605 * CQEs have been processed. The @arm parameter specifies whether the
606 * queue should be rearmed when ringing the doorbell.
James Smart4f774512009-05-22 14:52:35 -0400607 **/
James Smart32517fc2019-01-28 11:14:33 -0800608void
609lpfc_sli4_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
610 uint32_t count, bool arm)
James Smart4f774512009-05-22 14:52:35 -0400611{
James Smart4f774512009-05-22 14:52:35 -0400612 struct lpfc_register doorbell;
613
James Smart2e90f4b2011-12-13 13:22:37 -0500614 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800615 if (unlikely(!q || (count == 0 && !arm)))
616 return;
James Smart4f774512009-05-22 14:52:35 -0400617
618 /* ring doorbell for number popped */
619 doorbell.word0 = 0;
620 if (arm)
621 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800622 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
James Smart4f774512009-05-22 14:52:35 -0400623 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500624 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
625 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
626 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800627 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400628}
629
630/**
James Smart32517fc2019-01-28 11:14:33 -0800631 * lpfc_sli4_if6_write_cq_db - write cq DB for entries consumed or arm state.
632 * @phba: the adapter with the CQ
James Smart27d6ac02018-02-22 08:18:42 -0800633 * @q: The Completion Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800634 * @count: the number of elements that were consumed
James Smart27d6ac02018-02-22 08:18:42 -0800635 * @arm: Indicates whether the host wants to arms this CQ.
636 *
James Smart32517fc2019-01-28 11:14:33 -0800637 * This routine will notify the HBA, by ringing the doorbell, that the
638 * CQEs have been processed. The @arm parameter specifies whether the
639 * queue should be rearmed when ringing the doorbell.
James Smart27d6ac02018-02-22 08:18:42 -0800640 **/
James Smart32517fc2019-01-28 11:14:33 -0800641void
642lpfc_sli4_if6_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
643 uint32_t count, bool arm)
James Smart27d6ac02018-02-22 08:18:42 -0800644{
James Smart27d6ac02018-02-22 08:18:42 -0800645 struct lpfc_register doorbell;
646
647 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800648 if (unlikely(!q || (count == 0 && !arm)))
649 return;
James Smart27d6ac02018-02-22 08:18:42 -0800650
651 /* ring doorbell for number popped */
652 doorbell.word0 = 0;
653 if (arm)
654 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800655 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, count);
James Smart27d6ac02018-02-22 08:18:42 -0800656 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
657 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart27d6ac02018-02-22 08:18:42 -0800658}
659
660/**
James Smart4f774512009-05-22 14:52:35 -0400661 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
662 * @q: The Header Receive Queue to operate on.
663 * @wqe: The Receive Queue Entry to put on the Receive queue.
664 *
665 * This routine will copy the contents of @wqe to the next available entry on
666 * the @q. This function will then ring the Receive Queue Doorbell to signal the
667 * HBA to start processing the Receive Queue Entry. This function returns the
668 * index that the rqe was copied to if successful. If no entries are available
669 * on @q then this function will return -ENOMEM.
670 * The caller is expected to hold the hbalock when calling this routine.
671 **/
James Smart895427b2017-02-12 13:52:30 -0800672int
James Smart4f774512009-05-22 14:52:35 -0400673lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
674 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
675{
James Smart2e90f4b2011-12-13 13:22:37 -0500676 struct lpfc_rqe *temp_hrqe;
677 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400678 struct lpfc_register doorbell;
James Smartcbc5de12017-12-08 17:18:04 -0800679 int hq_put_index;
680 int dq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400681
James Smart2e90f4b2011-12-13 13:22:37 -0500682 /* sanity check on queue memory */
683 if (unlikely(!hq) || unlikely(!dq))
684 return -ENOMEM;
James Smartcbc5de12017-12-08 17:18:04 -0800685 hq_put_index = hq->host_index;
686 dq_put_index = dq->host_index;
James Smart9afbee32019-03-12 16:30:28 -0700687 temp_hrqe = lpfc_sli4_qe(hq, hq_put_index);
688 temp_drqe = lpfc_sli4_qe(dq, dq_put_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500689
James Smart4f774512009-05-22 14:52:35 -0400690 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
691 return -EINVAL;
James Smartcbc5de12017-12-08 17:18:04 -0800692 if (hq_put_index != dq_put_index)
James Smart4f774512009-05-22 14:52:35 -0400693 return -EINVAL;
694 /* If the host has not yet processed the next entry then we are done */
James Smartcbc5de12017-12-08 17:18:04 -0800695 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400696 return -EBUSY;
James Smart48f8fdb2018-05-04 20:37:51 -0700697 lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
698 lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400699
700 /* Update the host index to point to the next slot */
James Smartcbc5de12017-12-08 17:18:04 -0800701 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
702 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700703 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400704
705 /* Ring The Header Receive Queue Doorbell */
James Smart32517fc2019-01-28 11:14:33 -0800706 if (!(hq->host_index % hq->notify_interval)) {
James Smart4f774512009-05-22 14:52:35 -0400707 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500708 if (hq->db_format == LPFC_DB_RING_FORMAT) {
709 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
James Smart32517fc2019-01-28 11:14:33 -0800710 hq->notify_interval);
James Smart962bc512013-01-03 15:44:00 -0500711 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
712 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
713 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
James Smart32517fc2019-01-28 11:14:33 -0800714 hq->notify_interval);
James Smart962bc512013-01-03 15:44:00 -0500715 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
716 hq->host_index);
717 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
718 } else {
719 return -EINVAL;
720 }
721 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400722 }
James Smartcbc5de12017-12-08 17:18:04 -0800723 return hq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400724}
725
726/**
727 * lpfc_sli4_rq_release - Updates internal hba index for RQ
728 * @q: The Header Receive Queue to operate on.
729 *
730 * This routine will update the HBA index of a queue to reflect consumption of
731 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
732 * consumed an entry the host calls this function to update the queue's
733 * internal pointers. This routine returns the number of entries that were
734 * consumed by the HBA.
735 **/
736static uint32_t
737lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
738{
James Smart2e90f4b2011-12-13 13:22:37 -0500739 /* sanity check on queue memory */
740 if (unlikely(!hq) || unlikely(!dq))
741 return 0;
742
James Smart4f774512009-05-22 14:52:35 -0400743 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
744 return 0;
745 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
746 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
747 return 1;
748}
749
James Smarte59058c2008-08-24 21:49:00 -0400750/**
James Smart3621a712009-04-06 18:47:14 -0400751 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400752 * @phba: Pointer to HBA context object.
753 * @pring: Pointer to driver SLI ring object.
754 *
755 * This function returns pointer to next command iocb entry
756 * in the command ring. The caller must hold hbalock to prevent
757 * other threads consume the next command iocb.
758 * SLI-2/SLI-3 provide different sized iocbs.
759 **/
James Smarted957682007-06-17 19:56:37 -0500760static inline IOCB_t *
761lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
762{
James Smart7e56aa22012-08-03 12:35:34 -0400763 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
764 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500765}
766
James Smarte59058c2008-08-24 21:49:00 -0400767/**
James Smart3621a712009-04-06 18:47:14 -0400768 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400769 * @phba: Pointer to HBA context object.
770 * @pring: Pointer to driver SLI ring object.
771 *
772 * This function returns pointer to next response iocb entry
773 * in the response ring. The caller must hold hbalock to make sure
774 * that no other thread consume the next response iocb.
775 * SLI-2/SLI-3 provide different sized iocbs.
776 **/
James Smarted957682007-06-17 19:56:37 -0500777static inline IOCB_t *
778lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
779{
James Smart7e56aa22012-08-03 12:35:34 -0400780 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
781 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500782}
783
James Smarte59058c2008-08-24 21:49:00 -0400784/**
James Smart3621a712009-04-06 18:47:14 -0400785 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400786 * @phba: Pointer to HBA context object.
787 *
788 * This function is called with hbalock held. This function
789 * allocates a new driver iocb object from the iocb pool. If the
790 * allocation is successful, it returns pointer to the newly
791 * allocated iocb object else it returns NULL.
792 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400793struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500794__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400795{
796 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
797 struct lpfc_iocbq * iocbq = NULL;
798
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100799 lockdep_assert_held(&phba->hbalock);
800
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400801 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400802 if (iocbq)
803 phba->iocb_cnt++;
804 if (phba->iocb_cnt > phba->iocb_max)
805 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400806 return iocbq;
807}
808
James Smarte59058c2008-08-24 21:49:00 -0400809/**
James Smartda0436e2009-05-22 14:51:39 -0400810 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
811 * @phba: Pointer to HBA context object.
812 * @xritag: XRI value.
813 *
814 * This function clears the sglq pointer from the array of acive
815 * sglq's. The xritag that is passed in is used to index into the
816 * array. Before the xritag can be used it needs to be adjusted
817 * by subtracting the xribase.
818 *
819 * Returns sglq ponter = success, NULL = Failure.
820 **/
James Smart895427b2017-02-12 13:52:30 -0800821struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400822__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
823{
James Smartda0436e2009-05-22 14:51:39 -0400824 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400825
826 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
827 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400828 return sglq;
829}
830
831/**
832 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
833 * @phba: Pointer to HBA context object.
834 * @xritag: XRI value.
835 *
836 * This function returns the sglq pointer from the array of acive
837 * sglq's. The xritag that is passed in is used to index into the
838 * array. Before the xritag can be used it needs to be adjusted
839 * by subtracting the xribase.
840 *
841 * Returns sglq ponter = success, NULL = Failure.
842 **/
James Smart0f65ff62010-02-26 14:14:23 -0500843struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400844__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
845{
James Smartda0436e2009-05-22 14:51:39 -0400846 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400847
848 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400849 return sglq;
850}
851
852/**
James Smart1151e3e2011-02-16 12:39:35 -0500853 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500854 * @phba: Pointer to HBA context object.
855 * @xritag: xri used in this exchange.
856 * @rrq: The RRQ to be cleared.
857 *
James Smart19ca7602010-11-20 23:11:55 -0500858 **/
James Smart1151e3e2011-02-16 12:39:35 -0500859void
860lpfc_clr_rrq_active(struct lpfc_hba *phba,
861 uint16_t xritag,
862 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500863{
James Smart1151e3e2011-02-16 12:39:35 -0500864 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500865
James Smart1151e3e2011-02-16 12:39:35 -0500866 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
867 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500868
869 /* The target DID could have been swapped (cable swap)
870 * we should use the ndlp from the findnode if it is
871 * available.
872 */
James Smart1151e3e2011-02-16 12:39:35 -0500873 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500874 ndlp = rrq->ndlp;
875
James Smart1151e3e2011-02-16 12:39:35 -0500876 if (!ndlp)
877 goto out;
878
James Smartcff261f2013-12-17 20:29:47 -0500879 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500880 rrq->send_rrq = 0;
881 rrq->xritag = 0;
882 rrq->rrq_stop_time = 0;
883 }
James Smart1151e3e2011-02-16 12:39:35 -0500884out:
James Smart19ca7602010-11-20 23:11:55 -0500885 mempool_free(rrq, phba->rrq_pool);
886}
887
888/**
889 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
890 * @phba: Pointer to HBA context object.
891 *
892 * This function is called with hbalock held. This function
893 * Checks if stop_time (ratov from setting rrq active) has
894 * been reached, if it has and the send_rrq flag is set then
895 * it will call lpfc_send_rrq. If the send_rrq flag is not set
896 * then it will just call the routine to clear the rrq and
897 * free the rrq resource.
898 * The timer is set to the next rrq that is going to expire before
899 * leaving the routine.
900 *
901 **/
902void
903lpfc_handle_rrq_active(struct lpfc_hba *phba)
904{
905 struct lpfc_node_rrq *rrq;
906 struct lpfc_node_rrq *nextrrq;
907 unsigned long next_time;
908 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500909 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500910
911 spin_lock_irqsave(&phba->hbalock, iflags);
912 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400913 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500914 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500915 &phba->active_rrq_list, list) {
916 if (time_after(jiffies, rrq->rrq_stop_time))
917 list_move(&rrq->list, &send_rrq);
918 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500919 next_time = rrq->rrq_stop_time;
920 }
921 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500922 if ((!list_empty(&phba->active_rrq_list)) &&
923 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500924 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500925 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
926 list_del(&rrq->list);
Bart Van Asscheffd43812019-03-28 11:06:17 -0700927 if (!rrq->send_rrq) {
James Smart1151e3e2011-02-16 12:39:35 -0500928 /* this call will free the rrq */
Bart Van Asscheffd43812019-03-28 11:06:17 -0700929 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
930 } else if (lpfc_send_rrq(phba, rrq)) {
James Smart1151e3e2011-02-16 12:39:35 -0500931 /* if we send the rrq then the completion handler
932 * will clear the bit in the xribitmap.
933 */
934 lpfc_clr_rrq_active(phba, rrq->xritag,
935 rrq);
936 }
937 }
James Smart19ca7602010-11-20 23:11:55 -0500938}
939
940/**
941 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
942 * @vport: Pointer to vport context object.
943 * @xri: The xri used in the exchange.
944 * @did: The targets DID for this exchange.
945 *
946 * returns NULL = rrq not found in the phba->active_rrq_list.
947 * rrq = rrq for this xri and target.
948 **/
949struct lpfc_node_rrq *
950lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
951{
952 struct lpfc_hba *phba = vport->phba;
953 struct lpfc_node_rrq *rrq;
954 struct lpfc_node_rrq *nextrrq;
955 unsigned long iflags;
956
957 if (phba->sli_rev != LPFC_SLI_REV4)
958 return NULL;
959 spin_lock_irqsave(&phba->hbalock, iflags);
960 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
961 if (rrq->vport == vport && rrq->xritag == xri &&
962 rrq->nlp_DID == did){
963 list_del(&rrq->list);
964 spin_unlock_irqrestore(&phba->hbalock, iflags);
965 return rrq;
966 }
967 }
968 spin_unlock_irqrestore(&phba->hbalock, iflags);
969 return NULL;
970}
971
972/**
973 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
974 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500975 * @ndlp: Pointer to the lpfc_node_list structure.
976 * If ndlp is NULL Remove all active RRQs for this vport from the
977 * phba->active_rrq_list and clear the rrq.
978 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500979 **/
980void
James Smart1151e3e2011-02-16 12:39:35 -0500981lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500982
983{
984 struct lpfc_hba *phba = vport->phba;
985 struct lpfc_node_rrq *rrq;
986 struct lpfc_node_rrq *nextrrq;
987 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500988 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500989
990 if (phba->sli_rev != LPFC_SLI_REV4)
991 return;
James Smart1151e3e2011-02-16 12:39:35 -0500992 if (!ndlp) {
993 lpfc_sli4_vport_delete_els_xri_aborted(vport);
994 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500995 }
James Smart1151e3e2011-02-16 12:39:35 -0500996 spin_lock_irqsave(&phba->hbalock, iflags);
997 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
998 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
999 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -05001000 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -05001001
1002 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1003 list_del(&rrq->list);
1004 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1005 }
James Smart19ca7602010-11-20 23:11:55 -05001006}
1007
1008/**
James Smart1151e3e2011-02-16 12:39:35 -05001009 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -05001010 * @phba: Pointer to HBA context object.
1011 * @ndlp: Targets nodelist pointer for this exchange.
1012 * @xritag the xri in the bitmap to test.
1013 *
James Smarte2a8be52019-05-06 17:26:47 -07001014 * This function returns:
1015 * 0 = rrq not active for this xri
1016 * 1 = rrq is valid for this xri.
James Smart19ca7602010-11-20 23:11:55 -05001017 **/
James Smart1151e3e2011-02-16 12:39:35 -05001018int
1019lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -05001020 uint16_t xritag)
1021{
James Smart19ca7602010-11-20 23:11:55 -05001022 if (!ndlp)
1023 return 0;
James Smartcff261f2013-12-17 20:29:47 -05001024 if (!ndlp->active_rrqs_xri_bitmap)
1025 return 0;
1026 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
Colin Ian King258f84f2019-02-12 15:29:45 +00001027 return 1;
James Smart19ca7602010-11-20 23:11:55 -05001028 else
1029 return 0;
1030}
1031
1032/**
1033 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1034 * @phba: Pointer to HBA context object.
1035 * @ndlp: nodelist pointer for this target.
1036 * @xritag: xri used in this exchange.
1037 * @rxid: Remote Exchange ID.
1038 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1039 *
1040 * This function takes the hbalock.
1041 * The active bit is always set in the active rrq xri_bitmap even
1042 * if there is no slot avaiable for the other rrq information.
1043 *
1044 * returns 0 rrq actived for this xri
1045 * < 0 No memory or invalid ndlp.
1046 **/
1047int
1048lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -05001049 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -05001050{
James Smart19ca7602010-11-20 23:11:55 -05001051 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -05001052 struct lpfc_node_rrq *rrq;
1053 int empty;
1054
1055 if (!ndlp)
1056 return -EINVAL;
1057
1058 if (!phba->cfg_enable_rrq)
1059 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001060
1061 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001062 if (phba->pport->load_flag & FC_UNLOADING) {
1063 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1064 goto out;
1065 }
1066
1067 /*
1068 * set the active bit even if there is no mem available.
1069 */
1070 if (NLP_CHK_FREE_REQ(ndlp))
1071 goto out;
1072
1073 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1074 goto out;
1075
James Smartcff261f2013-12-17 20:29:47 -05001076 if (!ndlp->active_rrqs_xri_bitmap)
1077 goto out;
1078
1079 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -05001080 goto out;
1081
James Smart19ca7602010-11-20 23:11:55 -05001082 spin_unlock_irqrestore(&phba->hbalock, iflags);
Dick Kennedy9dace1f2020-06-30 14:49:53 -07001083 rrq = mempool_alloc(phba->rrq_pool, GFP_ATOMIC);
James Smartb42c07c2012-01-18 16:25:55 -05001084 if (!rrq) {
1085 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1086 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1087 " DID:0x%x Send:%d\n",
1088 xritag, rxid, ndlp->nlp_DID, send_rrq);
1089 return -EINVAL;
1090 }
James Smarte5771b42013-03-01 16:37:14 -05001091 if (phba->cfg_enable_rrq == 1)
1092 rrq->send_rrq = send_rrq;
1093 else
1094 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -05001095 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -04001096 rrq->rrq_stop_time = jiffies +
1097 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -05001098 rrq->ndlp = ndlp;
1099 rrq->nlp_DID = ndlp->nlp_DID;
1100 rrq->vport = ndlp->vport;
1101 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -05001102 spin_lock_irqsave(&phba->hbalock, iflags);
1103 empty = list_empty(&phba->active_rrq_list);
1104 list_add_tail(&rrq->list, &phba->active_rrq_list);
1105 phba->hba_flag |= HBA_RRQ_ACTIVE;
1106 if (empty)
1107 lpfc_worker_wake_up(phba);
1108 spin_unlock_irqrestore(&phba->hbalock, iflags);
1109 return 0;
1110out:
1111 spin_unlock_irqrestore(&phba->hbalock, iflags);
1112 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1113 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1114 " DID:0x%x Send:%d\n",
1115 xritag, rxid, ndlp->nlp_DID, send_rrq);
1116 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001117}
1118
1119/**
James Smart895427b2017-02-12 13:52:30 -08001120 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -04001121 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -05001122 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -04001123 *
James Smarte2a8be52019-05-06 17:26:47 -07001124 * The driver calls this function with either the nvme ls ring lock
1125 * or the fc els ring lock held depending on the iocb usage. This function
1126 * gets a new driver sglq object from the sglq list. If the list is not empty
1127 * then it is successful, it returns pointer to the newly allocated sglq
1128 * object else it returns NULL.
James Smartda0436e2009-05-22 14:51:39 -04001129 **/
1130static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -08001131__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -04001132{
James Smart895427b2017-02-12 13:52:30 -08001133 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -04001134 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001135 struct lpfc_sglq *start_sglq = NULL;
James Smartc4908502019-01-28 11:14:28 -08001136 struct lpfc_io_buf *lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001137 struct lpfc_nodelist *ndlp;
James Smarte2a8be52019-05-06 17:26:47 -07001138 struct lpfc_sli_ring *pring = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001139 int found = 0;
1140
James Smarte2a8be52019-05-06 17:26:47 -07001141 if (piocbq->iocb_flag & LPFC_IO_NVME_LS)
1142 pring = phba->sli4_hba.nvmels_wq->pring;
1143 else
1144 pring = lpfc_phba_elsring(phba);
1145
1146 lockdep_assert_held(&pring->ring_lock);
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001147
James Smart19ca7602010-11-20 23:11:55 -05001148 if (piocbq->iocb_flag & LPFC_IO_FCP) {
James Smartc4908502019-01-28 11:14:28 -08001149 lpfc_cmd = (struct lpfc_io_buf *) piocbq->context1;
James Smart19ca7602010-11-20 23:11:55 -05001150 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -05001151 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -04001152 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -05001153 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -04001154 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1155 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1156 ndlp = NULL;
1157 else
1158 ndlp = piocbq->context_un.ndlp;
1159 } else {
James Smart19ca7602010-11-20 23:11:55 -05001160 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -04001161 }
James Smart19ca7602010-11-20 23:11:55 -05001162
James Smart895427b2017-02-12 13:52:30 -08001163 spin_lock(&phba->sli4_hba.sgl_list_lock);
1164 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -05001165 start_sglq = sglq;
1166 while (!found) {
1167 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -08001168 break;
James Smart895427b2017-02-12 13:52:30 -08001169 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1170 test_bit(sglq->sli4_lxritag,
1171 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -05001172 /* This xri has an rrq outstanding for this DID.
1173 * put it back in the list and get another xri.
1174 */
James Smart895427b2017-02-12 13:52:30 -08001175 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001176 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -08001177 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -05001178 struct lpfc_sglq, list);
1179 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -07001180 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001181 sglq = NULL;
1182 break;
1183 } else
1184 continue;
1185 }
1186 sglq->ndlp = ndlp;
1187 found = 1;
James Smart6d368e52011-05-24 11:44:12 -04001188 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -05001189 sglq->state = SGL_ALLOCATED;
1190 }
James Smart895427b2017-02-12 13:52:30 -08001191 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001192 return sglq;
1193}
1194
1195/**
James Smartf358dd02017-02-12 13:52:34 -08001196 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1197 * @phba: Pointer to HBA context object.
1198 * @piocb: Pointer to the iocbq.
1199 *
1200 * This function is called with the sgl_list lock held. This function
1201 * gets a new driver sglq object from the sglq list. If the
1202 * list is not empty then it is successful, it returns pointer to the newly
1203 * allocated sglq object else it returns NULL.
1204 **/
1205struct lpfc_sglq *
1206__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1207{
1208 struct list_head *lpfc_nvmet_sgl_list;
1209 struct lpfc_sglq *sglq = NULL;
1210
1211 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1212
1213 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1214
1215 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1216 if (!sglq)
1217 return NULL;
1218 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1219 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001220 return sglq;
1221}
1222
James Smarte59058c2008-08-24 21:49:00 -04001223/**
James Smart3621a712009-04-06 18:47:14 -04001224 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001225 * @phba: Pointer to HBA context object.
1226 *
1227 * This function is called with no lock held. This function
1228 * allocates a new driver iocb object from the iocb pool. If the
1229 * allocation is successful, it returns pointer to the newly
1230 * allocated iocb object else it returns NULL.
1231 **/
James Smart2e0fef82007-06-17 19:56:36 -05001232struct lpfc_iocbq *
1233lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001234{
James Smart2e0fef82007-06-17 19:56:36 -05001235 struct lpfc_iocbq * iocbq = NULL;
1236 unsigned long iflags;
1237
1238 spin_lock_irqsave(&phba->hbalock, iflags);
1239 iocbq = __lpfc_sli_get_iocbq(phba);
1240 spin_unlock_irqrestore(&phba->hbalock, iflags);
1241 return iocbq;
1242}
1243
James Smarte59058c2008-08-24 21:49:00 -04001244/**
James Smart4f774512009-05-22 14:52:35 -04001245 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1246 * @phba: Pointer to HBA context object.
1247 * @iocbq: Pointer to driver iocb object.
1248 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001249 * This function is called to release the driver iocb object
1250 * to the iocb pool. The iotag in the iocb object
James Smart4f774512009-05-22 14:52:35 -04001251 * does not change for each use of the iocb object. This function
1252 * clears all other fields of the iocb object when it is freed.
1253 * The sqlq structure that holds the xritag and phys and virtual
1254 * mappings for the scatter gather list is retrieved from the
1255 * active array of sglq. The get of the sglq pointer also clears
1256 * the entry in the array. If the status of the IO indiactes that
1257 * this IO was aborted then the sglq entry it put on the
1258 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1259 * IO has good status or fails for any other reason then the sglq
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001260 * entry is added to the free list (lpfc_els_sgl_list). The hbalock is
1261 * asserted held in the code path calling this routine.
James Smart4f774512009-05-22 14:52:35 -04001262 **/
1263static void
1264__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1265{
1266 struct lpfc_sglq *sglq;
1267 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001268 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001269 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001270
1271 if (iocbq->sli4_xritag == NO_XRI)
1272 sglq = NULL;
1273 else
James Smart6d368e52011-05-24 11:44:12 -04001274 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1275
James Smart0e9bb8d2013-03-01 16:35:12 -05001276
James Smart4f774512009-05-22 14:52:35 -04001277 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001278 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1279 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1280 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001281 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001282 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001283 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001284 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1285 spin_unlock_irqrestore(
1286 &phba->sli4_hba.sgl_list_lock, iflag);
1287 goto out;
1288 }
1289
James Smart895427b2017-02-12 13:52:30 -08001290 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001291 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1292 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001293 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1294 iflag);
James Smart341af102010-01-26 23:07:37 -05001295 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001296 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001297 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001298 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001299 } else {
James Smart895427b2017-02-12 13:52:30 -08001300 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1301 iflag);
James Smart4f774512009-05-22 14:52:35 -04001302 sglq->state = SGL_FREED;
1303 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001304 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001305 &phba->sli4_hba.lpfc_els_sgl_list);
1306 spin_unlock_irqrestore(
1307 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001308
1309 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001310 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001311 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001312 }
James Smart4f774512009-05-22 14:52:35 -04001313 }
1314
James Smartf358dd02017-02-12 13:52:34 -08001315out:
James Smart4f774512009-05-22 14:52:35 -04001316 /*
1317 * Clean all volatile data fields, preserve iotag and node struct.
1318 */
1319 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001320 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001321 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001322 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1323 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001324 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1325}
1326
James Smart2a9bf3d2010-06-07 15:24:45 -04001327
James Smart4f774512009-05-22 14:52:35 -04001328/**
James Smart3772a992009-05-22 14:50:54 -04001329 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1330 * @phba: Pointer to HBA context object.
1331 * @iocbq: Pointer to driver iocb object.
1332 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001333 * This function is called to release the driver iocb object to the
1334 * iocb pool. The iotag in the iocb object does not change for each
1335 * use of the iocb object. This function clears all other fields of
1336 * the iocb object when it is freed. The hbalock is asserted held in
1337 * the code path calling this routine.
James Smart3772a992009-05-22 14:50:54 -04001338 **/
1339static void
1340__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1341{
1342 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1343
James Smart0e9bb8d2013-03-01 16:35:12 -05001344 /*
James Smart3772a992009-05-22 14:50:54 -04001345 * Clean all volatile data fields, preserve iotag and node struct.
1346 */
1347 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1348 iocbq->sli4_xritag = NO_XRI;
1349 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1350}
1351
1352/**
James Smart3621a712009-04-06 18:47:14 -04001353 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001354 * @phba: Pointer to HBA context object.
1355 * @iocbq: Pointer to driver iocb object.
1356 *
1357 * This function is called with hbalock held to release driver
1358 * iocb object to the iocb pool. The iotag in the iocb object
1359 * does not change for each use of the iocb object. This function
1360 * clears all other fields of the iocb object when it is freed.
1361 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001362static void
James Smart2e0fef82007-06-17 19:56:36 -05001363__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1364{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001365 lockdep_assert_held(&phba->hbalock);
1366
James Smart3772a992009-05-22 14:50:54 -04001367 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001368 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001369}
1370
James Smarte59058c2008-08-24 21:49:00 -04001371/**
James Smart3621a712009-04-06 18:47:14 -04001372 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001373 * @phba: Pointer to HBA context object.
1374 * @iocbq: Pointer to driver iocb object.
1375 *
1376 * This function is called with no lock held to release the iocb to
1377 * iocb pool.
1378 **/
James Smart2e0fef82007-06-17 19:56:36 -05001379void
1380lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1381{
1382 unsigned long iflags;
1383
1384 /*
1385 * Clean all volatile data fields, preserve iotag and node struct.
1386 */
1387 spin_lock_irqsave(&phba->hbalock, iflags);
1388 __lpfc_sli_release_iocbq(phba, iocbq);
1389 spin_unlock_irqrestore(&phba->hbalock, iflags);
1390}
1391
James Smarte59058c2008-08-24 21:49:00 -04001392/**
James Smarta257bf92009-04-06 18:48:10 -04001393 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1394 * @phba: Pointer to HBA context object.
1395 * @iocblist: List of IOCBs.
1396 * @ulpstatus: ULP status in IOCB command field.
1397 * @ulpWord4: ULP word-4 in IOCB command field.
1398 *
1399 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1400 * on the list by invoking the complete callback function associated with the
1401 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1402 * fields.
1403 **/
1404void
1405lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1406 uint32_t ulpstatus, uint32_t ulpWord4)
1407{
1408 struct lpfc_iocbq *piocb;
1409
1410 while (!list_empty(iocblist)) {
1411 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smart84f2ddf2019-08-14 16:56:55 -07001412 if (!piocb->iocb_cmpl) {
1413 if (piocb->iocb_flag & LPFC_IO_NVME)
1414 lpfc_nvme_cancel_iocb(phba, piocb);
1415 else
1416 lpfc_sli_release_iocbq(phba, piocb);
1417 } else {
James Smarta257bf92009-04-06 18:48:10 -04001418 piocb->iocb.ulpStatus = ulpstatus;
1419 piocb->iocb.un.ulpWord[4] = ulpWord4;
1420 (piocb->iocb_cmpl) (phba, piocb, piocb);
1421 }
1422 }
1423 return;
1424}
1425
1426/**
James Smart3621a712009-04-06 18:47:14 -04001427 * lpfc_sli_iocb_cmd_type - Get the iocb type
1428 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001429 *
1430 * This function is called by ring event handler function to get the iocb type.
1431 * This function translates the iocb command to an iocb command type used to
1432 * decide the final disposition of each completed IOCB.
1433 * The function returns
1434 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1435 * LPFC_SOL_IOCB if it is a solicited iocb completion
1436 * LPFC_ABORT_IOCB if it is an abort iocb
1437 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1438 *
1439 * The caller is not required to hold any lock.
1440 **/
dea31012005-04-17 16:05:31 -05001441static lpfc_iocb_type
1442lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1443{
1444 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1445
1446 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1447 return 0;
1448
1449 switch (iocb_cmnd) {
1450 case CMD_XMIT_SEQUENCE_CR:
1451 case CMD_XMIT_SEQUENCE_CX:
1452 case CMD_XMIT_BCAST_CN:
1453 case CMD_XMIT_BCAST_CX:
1454 case CMD_ELS_REQUEST_CR:
1455 case CMD_ELS_REQUEST_CX:
1456 case CMD_CREATE_XRI_CR:
1457 case CMD_CREATE_XRI_CX:
1458 case CMD_GET_RPI_CN:
1459 case CMD_XMIT_ELS_RSP_CX:
1460 case CMD_GET_RPI_CR:
1461 case CMD_FCP_IWRITE_CR:
1462 case CMD_FCP_IWRITE_CX:
1463 case CMD_FCP_IREAD_CR:
1464 case CMD_FCP_IREAD_CX:
1465 case CMD_FCP_ICMND_CR:
1466 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001467 case CMD_FCP_TSEND_CX:
1468 case CMD_FCP_TRSP_CX:
1469 case CMD_FCP_TRECEIVE_CX:
1470 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001471 case CMD_ADAPTER_MSG:
1472 case CMD_ADAPTER_DUMP:
1473 case CMD_XMIT_SEQUENCE64_CR:
1474 case CMD_XMIT_SEQUENCE64_CX:
1475 case CMD_XMIT_BCAST64_CN:
1476 case CMD_XMIT_BCAST64_CX:
1477 case CMD_ELS_REQUEST64_CR:
1478 case CMD_ELS_REQUEST64_CX:
1479 case CMD_FCP_IWRITE64_CR:
1480 case CMD_FCP_IWRITE64_CX:
1481 case CMD_FCP_IREAD64_CR:
1482 case CMD_FCP_IREAD64_CX:
1483 case CMD_FCP_ICMND64_CR:
1484 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001485 case CMD_FCP_TSEND64_CX:
1486 case CMD_FCP_TRSP64_CX:
1487 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001488 case CMD_GEN_REQUEST64_CR:
1489 case CMD_GEN_REQUEST64_CX:
1490 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001491 case DSSCMD_IWRITE64_CR:
1492 case DSSCMD_IWRITE64_CX:
1493 case DSSCMD_IREAD64_CR:
1494 case DSSCMD_IREAD64_CX:
Dick Kennedyc93764a2020-06-30 14:49:49 -07001495 case CMD_SEND_FRAME:
dea31012005-04-17 16:05:31 -05001496 type = LPFC_SOL_IOCB;
1497 break;
1498 case CMD_ABORT_XRI_CN:
1499 case CMD_ABORT_XRI_CX:
1500 case CMD_CLOSE_XRI_CN:
1501 case CMD_CLOSE_XRI_CX:
1502 case CMD_XRI_ABORTED_CX:
1503 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001504 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001505 type = LPFC_ABORT_IOCB;
1506 break;
1507 case CMD_RCV_SEQUENCE_CX:
1508 case CMD_RCV_ELS_REQ_CX:
1509 case CMD_RCV_SEQUENCE64_CX:
1510 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001511 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001512 case CMD_IOCB_RCV_SEQ64_CX:
1513 case CMD_IOCB_RCV_ELS64_CX:
1514 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001515 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001516 type = LPFC_UNSOL_IOCB;
1517 break;
James Smart3163f722008-02-08 18:50:25 -05001518 case CMD_IOCB_XMIT_MSEQ64_CR:
1519 case CMD_IOCB_XMIT_MSEQ64_CX:
1520 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1521 case CMD_IOCB_RCV_ELS_LIST64_CX:
1522 case CMD_IOCB_CLOSE_EXTENDED_CN:
1523 case CMD_IOCB_ABORT_EXTENDED_CN:
1524 case CMD_IOCB_RET_HBQE64_CN:
1525 case CMD_IOCB_FCP_IBIDIR64_CR:
1526 case CMD_IOCB_FCP_IBIDIR64_CX:
1527 case CMD_IOCB_FCP_ITASKMGT64_CX:
1528 case CMD_IOCB_LOGENTRY_CN:
1529 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1530 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001531 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001532 type = LPFC_UNKNOWN_IOCB;
1533 break;
dea31012005-04-17 16:05:31 -05001534 default:
1535 type = LPFC_UNKNOWN_IOCB;
1536 break;
1537 }
1538
1539 return type;
1540}
1541
James Smarte59058c2008-08-24 21:49:00 -04001542/**
James Smart3621a712009-04-06 18:47:14 -04001543 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001544 * @phba: Pointer to HBA context object.
1545 *
1546 * This function is called from SLI initialization code
1547 * to configure every ring of the HBA's SLI interface. The
1548 * caller is not required to hold any lock. This function issues
1549 * a config_ring mailbox command for each ring.
1550 * This function returns zero if successful else returns a negative
1551 * error code.
1552 **/
dea31012005-04-17 16:05:31 -05001553static int
James Smarted957682007-06-17 19:56:37 -05001554lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001555{
1556 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001557 LPFC_MBOXQ_t *pmb;
1558 MAILBOX_t *pmbox;
1559 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001560
James Smarted957682007-06-17 19:56:37 -05001561 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1562 if (!pmb)
1563 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001564 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001565 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001566 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001567 lpfc_config_ring(phba, i, pmb);
1568 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1569 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001570 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001571 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001572 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1573 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001574 rc, pmbox->mbxCommand,
1575 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001576 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001577 ret = -ENXIO;
1578 break;
dea31012005-04-17 16:05:31 -05001579 }
1580 }
James Smarted957682007-06-17 19:56:37 -05001581 mempool_free(pmb, phba->mbox_mem_pool);
1582 return ret;
dea31012005-04-17 16:05:31 -05001583}
1584
James Smarte59058c2008-08-24 21:49:00 -04001585/**
James Smart3621a712009-04-06 18:47:14 -04001586 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001587 * @phba: Pointer to HBA context object.
1588 * @pring: Pointer to driver SLI ring object.
1589 * @piocb: Pointer to the driver iocb object.
1590 *
James Smarte2a8be52019-05-06 17:26:47 -07001591 * The driver calls this function with the hbalock held for SLI3 ports or
1592 * the ring lock held for SLI4 ports. The function adds the
James Smarte59058c2008-08-24 21:49:00 -04001593 * new iocb to txcmplq of the given ring. This function always returns
1594 * 0. If this function is called for ELS ring, this function checks if
1595 * there is a vport associated with the ELS command. This function also
1596 * starts els_tmofunc timer if this is an ELS command.
1597 **/
dea31012005-04-17 16:05:31 -05001598static int
James Smart2e0fef82007-06-17 19:56:36 -05001599lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1600 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001601{
James Smarte2a8be52019-05-06 17:26:47 -07001602 if (phba->sli_rev == LPFC_SLI_REV4)
1603 lockdep_assert_held(&pring->ring_lock);
1604 else
1605 lockdep_assert_held(&phba->hbalock);
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001606
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001607 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001608
dea31012005-04-17 16:05:31 -05001609 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001610 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08001611 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001612
James Smart92d7f7b2007-06-17 19:56:38 -05001613 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1614 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001615 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1616 BUG_ON(!piocb->vport);
1617 if (!(piocb->vport->load_flag & FC_UNLOADING))
1618 mod_timer(&piocb->vport->els_tmofunc,
1619 jiffies +
1620 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1621 }
dea31012005-04-17 16:05:31 -05001622
James Smart2e0fef82007-06-17 19:56:36 -05001623 return 0;
dea31012005-04-17 16:05:31 -05001624}
1625
James Smarte59058c2008-08-24 21:49:00 -04001626/**
James Smart3621a712009-04-06 18:47:14 -04001627 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001628 * @phba: Pointer to HBA context object.
1629 * @pring: Pointer to driver SLI ring object.
1630 *
1631 * This function is called with hbalock held to get next
1632 * iocb in txq of the given ring. If there is any iocb in
1633 * the txq, the function returns first iocb in the list after
1634 * removing the iocb from the list, else it returns NULL.
1635 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001636struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001637lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001638{
dea31012005-04-17 16:05:31 -05001639 struct lpfc_iocbq *cmd_iocb;
1640
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001641 lockdep_assert_held(&phba->hbalock);
1642
James Smart858c9f62007-06-17 19:56:39 -05001643 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001644 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001645}
1646
James Smarte59058c2008-08-24 21:49:00 -04001647/**
James Smart3621a712009-04-06 18:47:14 -04001648 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001649 * @phba: Pointer to HBA context object.
1650 * @pring: Pointer to driver SLI ring object.
1651 *
1652 * This function is called with hbalock held and the caller must post the
1653 * iocb without releasing the lock. If the caller releases the lock,
1654 * iocb slot returned by the function is not guaranteed to be available.
1655 * The function returns pointer to the next available iocb slot if there
1656 * is available slot in the ring, else it returns NULL.
1657 * If the get index of the ring is ahead of the put index, the function
1658 * will post an error attention event to the worker thread to take the
1659 * HBA to offline state.
1660 **/
dea31012005-04-17 16:05:31 -05001661static IOCB_t *
1662lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1663{
James Smart34b02dc2008-08-24 21:49:55 -04001664 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001665 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001666
1667 lockdep_assert_held(&phba->hbalock);
1668
James Smart7e56aa22012-08-03 12:35:34 -04001669 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1670 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1671 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001672
James Smart7e56aa22012-08-03 12:35:34 -04001673 if (unlikely(pring->sli.sli3.local_getidx ==
1674 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001675
James Smart7e56aa22012-08-03 12:35:34 -04001676 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001677
James Smart7e56aa22012-08-03 12:35:34 -04001678 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001679 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001680 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001681 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001682 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001683 pring->sli.sli3.local_getidx,
1684 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001685
James Smart2e0fef82007-06-17 19:56:36 -05001686 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001687 /*
1688 * All error attention handlers are posted to
1689 * worker thread
1690 */
1691 phba->work_ha |= HA_ERATT;
1692 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001693
James Smart5e9d9b82008-06-14 22:52:53 -04001694 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001695
1696 return NULL;
1697 }
1698
James Smart7e56aa22012-08-03 12:35:34 -04001699 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001700 return NULL;
1701 }
1702
James Smarted957682007-06-17 19:56:37 -05001703 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001704}
1705
James Smarte59058c2008-08-24 21:49:00 -04001706/**
James Smart3621a712009-04-06 18:47:14 -04001707 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001708 * @phba: Pointer to HBA context object.
1709 * @iocbq: Pointer to driver iocb object.
1710 *
1711 * This function gets an iotag for the iocb. If there is no unused iotag and
1712 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1713 * array and assigns a new iotag.
1714 * The function returns the allocated iotag if successful, else returns zero.
1715 * Zero is not a valid iotag.
1716 * The caller is not required to hold any lock.
1717 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001718uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001719lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001720{
James Smart2e0fef82007-06-17 19:56:36 -05001721 struct lpfc_iocbq **new_arr;
1722 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001723 size_t new_len;
1724 struct lpfc_sli *psli = &phba->sli;
1725 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001726
James Smart2e0fef82007-06-17 19:56:36 -05001727 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001728 iotag = psli->last_iotag;
1729 if(++iotag < psli->iocbq_lookup_len) {
1730 psli->last_iotag = iotag;
1731 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001732 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001733 iocbq->iotag = iotag;
1734 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001735 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001736 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1737 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001738 spin_unlock_irq(&phba->hbalock);
Kees Cook6396bb22018-06-12 14:03:40 -07001739 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001740 GFP_KERNEL);
1741 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001742 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001743 old_arr = psli->iocbq_lookup;
1744 if (new_len <= psli->iocbq_lookup_len) {
1745 /* highly unprobable case */
1746 kfree(new_arr);
1747 iotag = psli->last_iotag;
1748 if(++iotag < psli->iocbq_lookup_len) {
1749 psli->last_iotag = iotag;
1750 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001751 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001752 iocbq->iotag = iotag;
1753 return iotag;
1754 }
James Smart2e0fef82007-06-17 19:56:36 -05001755 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001756 return 0;
1757 }
1758 if (psli->iocbq_lookup)
1759 memcpy(new_arr, old_arr,
1760 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001761 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001762 psli->iocbq_lookup = new_arr;
1763 psli->iocbq_lookup_len = new_len;
1764 psli->last_iotag = iotag;
1765 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001766 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001767 iocbq->iotag = iotag;
1768 kfree(old_arr);
1769 return iotag;
1770 }
James Smart8f6d98d2006-08-01 07:34:00 -04001771 } else
James Smart2e0fef82007-06-17 19:56:36 -05001772 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001773
James Smartbc739052010-08-04 16:11:18 -04001774 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001775 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1776 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001777
James Bottomley604a3e32005-10-29 10:28:33 -05001778 return 0;
dea31012005-04-17 16:05:31 -05001779}
1780
James Smarte59058c2008-08-24 21:49:00 -04001781/**
James Smart3621a712009-04-06 18:47:14 -04001782 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001783 * @phba: Pointer to HBA context object.
1784 * @pring: Pointer to driver SLI ring object.
1785 * @iocb: Pointer to iocb slot in the ring.
1786 * @nextiocb: Pointer to driver iocb object which need to be
1787 * posted to firmware.
1788 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001789 * This function is called to post a new iocb to the firmware. This
1790 * function copies the new iocb to ring iocb slot and updates the
1791 * ring pointers. It adds the new iocb to txcmplq if there is
James Smarte59058c2008-08-24 21:49:00 -04001792 * a completion call back for this iocb else the function will free the
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001793 * iocb object. The hbalock is asserted held in the code path calling
1794 * this routine.
James Smarte59058c2008-08-24 21:49:00 -04001795 **/
dea31012005-04-17 16:05:31 -05001796static void
1797lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1798 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1799{
1800 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001801 * Set up an iotag
dea31012005-04-17 16:05:31 -05001802 */
James Bottomley604a3e32005-10-29 10:28:33 -05001803 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001804
James Smarte2a0a9d2008-12-04 22:40:02 -05001805
James Smarta58cbd52007-08-02 11:09:43 -04001806 if (pring->ringno == LPFC_ELS_RING) {
1807 lpfc_debugfs_slow_ring_trc(phba,
1808 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1809 *(((uint32_t *) &nextiocb->iocb) + 4),
1810 *(((uint32_t *) &nextiocb->iocb) + 6),
1811 *(((uint32_t *) &nextiocb->iocb) + 7));
1812 }
1813
dea31012005-04-17 16:05:31 -05001814 /*
1815 * Issue iocb command to adapter
1816 */
James Smart92d7f7b2007-06-17 19:56:38 -05001817 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001818 wmb();
1819 pring->stats.iocb_cmd++;
1820
1821 /*
1822 * If there is no completion routine to call, we can release the
1823 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1824 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1825 */
1826 if (nextiocb->iocb_cmpl)
1827 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001828 else
James Smart2e0fef82007-06-17 19:56:36 -05001829 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001830
1831 /*
1832 * Let the HBA know what IOCB slot will be the next one the
1833 * driver will put a command into.
1834 */
James Smart7e56aa22012-08-03 12:35:34 -04001835 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1836 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001837}
1838
James Smarte59058c2008-08-24 21:49:00 -04001839/**
James Smart3621a712009-04-06 18:47:14 -04001840 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001841 * @phba: Pointer to HBA context object.
1842 * @pring: Pointer to driver SLI ring object.
1843 *
1844 * The caller is not required to hold any lock for calling this function.
1845 * This function updates the chip attention bits for the ring to inform firmware
1846 * that there are pending work to be done for this ring and requests an
1847 * interrupt when there is space available in the ring. This function is
1848 * called when the driver is unable to post more iocbs to the ring due
1849 * to unavailability of space in the ring.
1850 **/
dea31012005-04-17 16:05:31 -05001851static void
James Smart2e0fef82007-06-17 19:56:36 -05001852lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001853{
1854 int ringno = pring->ringno;
1855
1856 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1857
1858 wmb();
1859
1860 /*
1861 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1862 * The HBA will tell us when an IOCB entry is available.
1863 */
1864 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1865 readl(phba->CAregaddr); /* flush */
1866
1867 pring->stats.iocb_cmd_full++;
1868}
1869
James Smarte59058c2008-08-24 21:49:00 -04001870/**
James Smart3621a712009-04-06 18:47:14 -04001871 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001872 * @phba: Pointer to HBA context object.
1873 * @pring: Pointer to driver SLI ring object.
1874 *
1875 * This function updates the chip attention register bit for the
1876 * given ring to inform HBA that there is more work to be done
1877 * in this ring. The caller is not required to hold any lock.
1878 **/
dea31012005-04-17 16:05:31 -05001879static void
James Smart2e0fef82007-06-17 19:56:36 -05001880lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001881{
1882 int ringno = pring->ringno;
1883
1884 /*
1885 * Tell the HBA that there is work to do in this ring.
1886 */
James Smart34b02dc2008-08-24 21:49:55 -04001887 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1888 wmb();
1889 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1890 readl(phba->CAregaddr); /* flush */
1891 }
dea31012005-04-17 16:05:31 -05001892}
1893
James Smarte59058c2008-08-24 21:49:00 -04001894/**
James Smart3621a712009-04-06 18:47:14 -04001895 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001896 * @phba: Pointer to HBA context object.
1897 * @pring: Pointer to driver SLI ring object.
1898 *
1899 * This function is called with hbalock held to post pending iocbs
1900 * in the txq to the firmware. This function is called when driver
1901 * detects space available in the ring.
1902 **/
dea31012005-04-17 16:05:31 -05001903static void
James Smart2e0fef82007-06-17 19:56:36 -05001904lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001905{
1906 IOCB_t *iocb;
1907 struct lpfc_iocbq *nextiocb;
1908
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001909 lockdep_assert_held(&phba->hbalock);
1910
dea31012005-04-17 16:05:31 -05001911 /*
1912 * Check to see if:
1913 * (a) there is anything on the txq to send
1914 * (b) link is up
1915 * (c) link attention events can be processed (fcp ring only)
1916 * (d) IOCB processing is not blocked by the outstanding mbox command.
1917 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001918
1919 if (lpfc_is_link_up(phba) &&
1920 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001921 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001922 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001923
1924 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1925 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1926 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1927
1928 if (iocb)
1929 lpfc_sli_update_ring(phba, pring);
1930 else
1931 lpfc_sli_update_full_ring(phba, pring);
1932 }
1933
1934 return;
1935}
1936
James Smarte59058c2008-08-24 21:49:00 -04001937/**
James Smart3621a712009-04-06 18:47:14 -04001938 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001939 * @phba: Pointer to HBA context object.
1940 * @hbqno: HBQ number.
1941 *
1942 * This function is called with hbalock held to get the next
1943 * available slot for the given HBQ. If there is free slot
1944 * available for the HBQ it will return pointer to the next available
1945 * HBQ entry else it will return NULL.
1946 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001947static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001948lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1949{
1950 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1951
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001952 lockdep_assert_held(&phba->hbalock);
1953
James Smarted957682007-06-17 19:56:37 -05001954 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1955 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1956 hbqp->next_hbqPutIdx = 0;
1957
1958 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001959 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001960 uint32_t getidx = le32_to_cpu(raw_index);
1961
1962 hbqp->local_hbqGetIdx = getidx;
1963
1964 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1965 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001966 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001967 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001968 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001969 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001970 hbqp->entry_count);
1971
1972 phba->link_state = LPFC_HBA_ERROR;
1973 return NULL;
1974 }
1975
1976 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1977 return NULL;
1978 }
1979
James Smart51ef4c22007-08-02 11:10:31 -04001980 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1981 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001982}
1983
James Smarte59058c2008-08-24 21:49:00 -04001984/**
James Smart3621a712009-04-06 18:47:14 -04001985 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001986 * @phba: Pointer to HBA context object.
1987 *
1988 * This function is called with no lock held to free all the
1989 * hbq buffers while uninitializing the SLI interface. It also
1990 * frees the HBQ buffers returned by the firmware but not yet
1991 * processed by the upper layers.
1992 **/
James Smarted957682007-06-17 19:56:37 -05001993void
1994lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1995{
James Smart92d7f7b2007-06-17 19:56:38 -05001996 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1997 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001998 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001999 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05002000
James Smart51ef4c22007-08-02 11:10:31 -04002001 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05002002 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05002003 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04002004 for (i = 0; i < hbq_count; ++i) {
2005 list_for_each_entry_safe(dmabuf, next_dmabuf,
2006 &phba->hbqs[i].hbq_buffer_list, list) {
2007 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
2008 list_del(&hbq_buf->dbuf.list);
2009 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
2010 }
James Smarta8adb832007-10-27 13:37:53 -04002011 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05002012 }
James Smart3163f722008-02-08 18:50:25 -05002013
2014 /* Mark the HBQs not in use */
2015 phba->hbq_in_use = 0;
2016 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05002017}
2018
James Smarte59058c2008-08-24 21:49:00 -04002019/**
James Smart3621a712009-04-06 18:47:14 -04002020 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002021 * @phba: Pointer to HBA context object.
2022 * @hbqno: HBQ number.
2023 * @hbq_buf: Pointer to HBQ buffer.
2024 *
2025 * This function is called with the hbalock held to post a
2026 * hbq buffer to the firmware. If the function finds an empty
2027 * slot in the HBQ, it will post the buffer. The function will return
2028 * pointer to the hbq entry if it successfully post the buffer
2029 * else it will return NULL.
2030 **/
James Smart3772a992009-05-22 14:50:54 -04002031static int
James Smarted957682007-06-17 19:56:37 -05002032lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05002033 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05002034{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002035 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002036 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2037}
2038
2039/**
2040 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2041 * @phba: Pointer to HBA context object.
2042 * @hbqno: HBQ number.
2043 * @hbq_buf: Pointer to HBQ buffer.
2044 *
2045 * This function is called with the hbalock held to post a hbq buffer to the
2046 * firmware. If the function finds an empty slot in the HBQ, it will post the
2047 * buffer and place it on the hbq_buffer_list. The function will return zero if
2048 * it successfully post the buffer else it will return an error.
2049 **/
2050static int
2051lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2052 struct hbq_dmabuf *hbq_buf)
2053{
James Smarted957682007-06-17 19:56:37 -05002054 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002055 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002056
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002057 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002058 /* Get next HBQ entry slot to use */
2059 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2060 if (hbqe) {
2061 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2062
James Smart92d7f7b2007-06-17 19:56:38 -05002063 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2064 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002065 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002066 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002067 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2068 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2069 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002070 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2071 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002072 /* flush */
James Smarted957682007-06-17 19:56:37 -05002073 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002074 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002075 return 0;
2076 } else
2077 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002078}
2079
James Smart4f774512009-05-22 14:52:35 -04002080/**
2081 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2082 * @phba: Pointer to HBA context object.
2083 * @hbqno: HBQ number.
2084 * @hbq_buf: Pointer to HBQ buffer.
2085 *
2086 * This function is called with the hbalock held to post an RQE to the SLI4
2087 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2088 * the hbq_buffer_list and return zero, otherwise it will return an error.
2089 **/
2090static int
2091lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2092 struct hbq_dmabuf *hbq_buf)
2093{
2094 int rc;
2095 struct lpfc_rqe hrqe;
2096 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002097 struct lpfc_queue *hrq;
2098 struct lpfc_queue *drq;
2099
2100 if (hbqno != LPFC_ELS_HBQ)
2101 return 1;
2102 hrq = phba->sli4_hba.hdr_rq;
2103 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002104
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002105 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002106 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2107 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2108 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2109 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002110 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002111 if (rc < 0)
2112 return rc;
James Smart895427b2017-02-12 13:52:30 -08002113 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002114 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2115 return 0;
2116}
2117
James Smarte59058c2008-08-24 21:49:00 -04002118/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002119static struct lpfc_hbq_init lpfc_els_hbq = {
2120 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002121 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002122 .mask_count = 0,
2123 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002124 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002125 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002126 .init_count = 40,
2127 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002128};
James Smarted957682007-06-17 19:56:37 -05002129
James Smarte59058c2008-08-24 21:49:00 -04002130/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002131struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002132 &lpfc_els_hbq,
2133};
2134
James Smarte59058c2008-08-24 21:49:00 -04002135/**
James Smart3621a712009-04-06 18:47:14 -04002136 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002137 * @phba: Pointer to HBA context object.
2138 * @hbqno: HBQ number.
2139 * @count: Number of HBQ buffers to be posted.
2140 *
James Smartd7c255b2008-08-24 21:50:00 -04002141 * This function is called with no lock held to post more hbq buffers to the
2142 * given HBQ. The function returns the number of HBQ buffers successfully
2143 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002144 **/
James Smart311464e2007-08-02 11:10:37 -04002145static int
James Smart92d7f7b2007-06-17 19:56:38 -05002146lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2147{
James Smartd7c255b2008-08-24 21:50:00 -04002148 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002149 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002150 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002151 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002152 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002153 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002154
James Smartd7c255b2008-08-24 21:50:00 -04002155 if ((phba->hbqs[hbqno].buffer_count + count) >
2156 lpfc_hbq_defs[hbqno]->entry_count)
2157 count = lpfc_hbq_defs[hbqno]->entry_count -
2158 phba->hbqs[hbqno].buffer_count;
2159 if (!count)
2160 return 0;
2161 /* Allocate HBQ entries */
2162 for (i = 0; i < count; i++) {
2163 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2164 if (!hbq_buffer)
2165 break;
2166 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2167 }
James Smart3163f722008-02-08 18:50:25 -05002168 /* Check whether HBQ is still in use */
2169 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002170 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002171 goto err;
2172 while (!list_empty(&hbq_buf_list)) {
2173 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2174 dbuf.list);
2175 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2176 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002177 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002178 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002179 posted++;
2180 } else
James Smart51ef4c22007-08-02 11:10:31 -04002181 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002182 }
James Smart3163f722008-02-08 18:50:25 -05002183 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002184 return posted;
2185err:
2186 spin_unlock_irqrestore(&phba->hbalock, flags);
2187 while (!list_empty(&hbq_buf_list)) {
2188 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2189 dbuf.list);
2190 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2191 }
James Smart92d7f7b2007-06-17 19:56:38 -05002192 return 0;
James Smarted957682007-06-17 19:56:37 -05002193}
2194
James Smarte59058c2008-08-24 21:49:00 -04002195/**
James Smart3621a712009-04-06 18:47:14 -04002196 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002197 * @phba: Pointer to HBA context object.
2198 * @qno: HBQ number.
2199 *
2200 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002201 * is called with no lock held. The function returns the number of HBQ entries
2202 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002203 **/
James Smarted957682007-06-17 19:56:37 -05002204int
James Smart92d7f7b2007-06-17 19:56:38 -05002205lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002206{
James Smartdef9c7a2009-12-21 17:02:28 -05002207 if (phba->sli_rev == LPFC_SLI_REV4)
2208 return 0;
2209 else
2210 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2211 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002212}
2213
James Smarte59058c2008-08-24 21:49:00 -04002214/**
James Smart3621a712009-04-06 18:47:14 -04002215 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002216 * @phba: Pointer to HBA context object.
2217 * @qno: HBQ queue number.
2218 *
2219 * This function is called from SLI initialization code path with
2220 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002221 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002222 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002223static int
James Smart92d7f7b2007-06-17 19:56:38 -05002224lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002225{
James Smartdef9c7a2009-12-21 17:02:28 -05002226 if (phba->sli_rev == LPFC_SLI_REV4)
2227 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002228 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002229 else
2230 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2231 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002232}
2233
James Smarte59058c2008-08-24 21:49:00 -04002234/**
James Smart3772a992009-05-22 14:50:54 -04002235 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2236 * @phba: Pointer to HBA context object.
2237 * @hbqno: HBQ number.
2238 *
2239 * This function removes the first hbq buffer on an hbq list and returns a
2240 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2241 **/
2242static struct hbq_dmabuf *
2243lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2244{
2245 struct lpfc_dmabuf *d_buf;
2246
2247 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2248 if (!d_buf)
2249 return NULL;
2250 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2251}
2252
2253/**
James Smart2d7dbc42017-02-12 13:52:35 -08002254 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2255 * @phba: Pointer to HBA context object.
2256 * @hbqno: HBQ number.
2257 *
2258 * This function removes the first RQ buffer on an RQ buffer list and returns a
2259 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2260 **/
2261static struct rqb_dmabuf *
2262lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2263{
2264 struct lpfc_dmabuf *h_buf;
2265 struct lpfc_rqb *rqbp;
2266
2267 rqbp = hrq->rqbp;
2268 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2269 struct lpfc_dmabuf, list);
2270 if (!h_buf)
2271 return NULL;
2272 rqbp->buffer_count--;
2273 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2274}
2275
2276/**
James Smart3621a712009-04-06 18:47:14 -04002277 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002278 * @phba: Pointer to HBA context object.
2279 * @tag: Tag of the hbq buffer.
2280 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002281 * This function searches for the hbq buffer associated with the given tag in
2282 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2283 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002284 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002285static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002286lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2287{
James Smart92d7f7b2007-06-17 19:56:38 -05002288 struct lpfc_dmabuf *d_buf;
2289 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002290 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002291
James Smart51ef4c22007-08-02 11:10:31 -04002292 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002293 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002294 return NULL;
2295
James Smart3772a992009-05-22 14:50:54 -04002296 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002297 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002298 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002299 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002300 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002301 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002302 }
2303 }
James Smart3772a992009-05-22 14:50:54 -04002304 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002305 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002306 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002307 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002308 return NULL;
James Smarted957682007-06-17 19:56:37 -05002309}
2310
James Smarte59058c2008-08-24 21:49:00 -04002311/**
James Smart3621a712009-04-06 18:47:14 -04002312 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002313 * @phba: Pointer to HBA context object.
2314 * @hbq_buffer: Pointer to HBQ buffer.
2315 *
2316 * This function is called with hbalock. This function gives back
2317 * the hbq buffer to firmware. If the HBQ does not have space to
2318 * post the buffer, it will free the buffer.
2319 **/
James Smarted957682007-06-17 19:56:37 -05002320void
James Smart51ef4c22007-08-02 11:10:31 -04002321lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002322{
2323 uint32_t hbqno;
2324
James Smart51ef4c22007-08-02 11:10:31 -04002325 if (hbq_buffer) {
2326 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002327 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002328 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002329 }
2330}
2331
James Smarte59058c2008-08-24 21:49:00 -04002332/**
James Smart3621a712009-04-06 18:47:14 -04002333 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002334 * @mbxCommand: mailbox command code.
2335 *
2336 * This function is called by the mailbox event handler function to verify
2337 * that the completed mailbox command is a legitimate mailbox command. If the
2338 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2339 * and the mailbox event handler will take the HBA offline.
2340 **/
dea31012005-04-17 16:05:31 -05002341static int
2342lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2343{
2344 uint8_t ret;
2345
2346 switch (mbxCommand) {
2347 case MBX_LOAD_SM:
2348 case MBX_READ_NV:
2349 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002350 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002351 case MBX_RUN_BIU_DIAG:
2352 case MBX_INIT_LINK:
2353 case MBX_DOWN_LINK:
2354 case MBX_CONFIG_LINK:
2355 case MBX_CONFIG_RING:
2356 case MBX_RESET_RING:
2357 case MBX_READ_CONFIG:
2358 case MBX_READ_RCONFIG:
2359 case MBX_READ_SPARM:
2360 case MBX_READ_STATUS:
2361 case MBX_READ_RPI:
2362 case MBX_READ_XRI:
2363 case MBX_READ_REV:
2364 case MBX_READ_LNK_STAT:
2365 case MBX_REG_LOGIN:
2366 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002367 case MBX_CLEAR_LA:
2368 case MBX_DUMP_MEMORY:
2369 case MBX_DUMP_CONTEXT:
2370 case MBX_RUN_DIAGS:
2371 case MBX_RESTART:
2372 case MBX_UPDATE_CFG:
2373 case MBX_DOWN_LOAD:
2374 case MBX_DEL_LD_ENTRY:
2375 case MBX_RUN_PROGRAM:
2376 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002377 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002378 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002379 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002380 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002381 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002382 case MBX_LOAD_AREA:
2383 case MBX_RUN_BIU_DIAG64:
2384 case MBX_CONFIG_PORT:
2385 case MBX_READ_SPARM64:
2386 case MBX_READ_RPI64:
2387 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002388 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002389 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002390 case MBX_SET_DEBUG:
2391 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002392 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002393 case MBX_REG_VPI:
2394 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002395 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002396 case MBX_PORT_CAPABILITIES:
2397 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002398 case MBX_SLI4_CONFIG:
2399 case MBX_SLI4_REQ_FTRS:
2400 case MBX_REG_FCFI:
2401 case MBX_UNREG_FCFI:
2402 case MBX_REG_VFI:
2403 case MBX_UNREG_VFI:
2404 case MBX_INIT_VPI:
2405 case MBX_INIT_VFI:
2406 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002407 case MBX_READ_EVENT_LOG_STATUS:
2408 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002409 case MBX_SECURITY_MGMT:
2410 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002411 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002412 ret = mbxCommand;
2413 break;
2414 default:
2415 ret = MBX_SHUTDOWN;
2416 break;
2417 }
James Smart2e0fef82007-06-17 19:56:36 -05002418 return ret;
dea31012005-04-17 16:05:31 -05002419}
James Smarte59058c2008-08-24 21:49:00 -04002420
2421/**
James Smart3621a712009-04-06 18:47:14 -04002422 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002423 * @phba: Pointer to HBA context object.
2424 * @pmboxq: Pointer to mailbox command.
2425 *
2426 * This is completion handler function for mailbox commands issued from
2427 * lpfc_sli_issue_mbox_wait function. This function is called by the
2428 * mailbox event handler function with no lock held. This function
2429 * will wake up thread waiting on the wait queue pointed by context1
2430 * of the mailbox.
2431 **/
James Smart04c68492009-05-22 14:52:52 -04002432void
James Smart2e0fef82007-06-17 19:56:36 -05002433lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002434{
James Smart858c9f62007-06-17 19:56:39 -05002435 unsigned long drvr_flag;
James Smarte29d74f2018-03-05 12:04:07 -08002436 struct completion *pmbox_done;
dea31012005-04-17 16:05:31 -05002437
2438 /*
James Smarte29d74f2018-03-05 12:04:07 -08002439 * If pmbox_done is empty, the driver thread gave up waiting and
dea31012005-04-17 16:05:31 -05002440 * continued running.
2441 */
James Smart7054a602007-04-25 09:52:34 -04002442 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002443 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarte29d74f2018-03-05 12:04:07 -08002444 pmbox_done = (struct completion *)pmboxq->context3;
2445 if (pmbox_done)
2446 complete(pmbox_done);
James Smart858c9f62007-06-17 19:56:39 -05002447 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002448 return;
2449}
2450
James Smartb95b2112019-08-14 16:56:47 -07002451static void
2452__lpfc_sli_rpi_release(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2453{
2454 unsigned long iflags;
2455
2456 if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
2457 lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
2458 spin_lock_irqsave(&vport->phba->ndlp_lock, iflags);
2459 ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
2460 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
2461 spin_unlock_irqrestore(&vport->phba->ndlp_lock, iflags);
2462 }
2463 ndlp->nlp_flag &= ~NLP_UNREG_INP;
2464}
James Smarte59058c2008-08-24 21:49:00 -04002465
2466/**
James Smart3621a712009-04-06 18:47:14 -04002467 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002468 * @phba: Pointer to HBA context object.
2469 * @pmb: Pointer to mailbox object.
2470 *
2471 * This function is the default mailbox completion handler. It
2472 * frees the memory resources associated with the completed mailbox
2473 * command. If the completed command is a REG_LOGIN mailbox command,
2474 * this function will issue a UREG_LOGIN to re-claim the RPI.
2475 **/
dea31012005-04-17 16:05:31 -05002476void
James Smart2e0fef82007-06-17 19:56:36 -05002477lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002478{
James Smartd439d282010-09-29 11:18:45 -04002479 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002480 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002481 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002482 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002483 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002484 int rc;
2485
James Smart3e1f0712018-11-29 16:09:29 -08002486 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smart7054a602007-04-25 09:52:34 -04002487
dea31012005-04-17 16:05:31 -05002488 if (mp) {
2489 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2490 kfree(mp);
2491 }
James Smart7054a602007-04-25 09:52:34 -04002492
2493 /*
2494 * If a REG_LOGIN succeeded after node is destroyed or node
2495 * is in re-discovery driver need to cleanup the RPI.
2496 */
James Smart2e0fef82007-06-17 19:56:36 -05002497 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002498 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2499 !pmb->u.mb.mbxStatus) {
2500 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002501 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart38503942020-03-22 11:12:53 -07002502 if (phba->sli_rev == LPFC_SLI_REV4)
2503 vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
James Smart04c68492009-05-22 14:52:52 -04002504 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002505 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002506 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002507 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2508 if (rc != MBX_NOT_FINISHED)
2509 return;
2510 }
2511
James Smart695a8142010-01-26 23:08:03 -05002512 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2513 !(phba->pport->load_flag & FC_UNLOADING) &&
2514 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002515 shost = lpfc_shost_from_vport(vport);
2516 spin_lock_irq(shost->host_lock);
2517 vport->vpi_state |= LPFC_VPI_REGISTERED;
2518 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2519 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002520 }
2521
James Smartd439d282010-09-29 11:18:45 -04002522 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -08002523 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -04002524 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002525 pmb->ctx_buf = NULL;
2526 pmb->ctx_ndlp = NULL;
2527 }
2528
2529 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2530 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2531
2532 /* Check to see if there are any deferred events to process */
2533 if (ndlp) {
2534 lpfc_printf_vlog(
2535 vport,
2536 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2537 "1438 UNREG cmpl deferred mbox x%x "
James Smart32350662019-08-14 16:57:06 -07002538 "on NPort x%x Data: x%x x%x %px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002539 ndlp->nlp_rpi, ndlp->nlp_DID,
2540 ndlp->nlp_flag, ndlp->nlp_defer_did, ndlp);
2541
2542 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2543 (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
James Smart00292e02018-12-13 15:17:55 -08002544 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002545 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2546 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002547 } else {
James Smartb95b2112019-08-14 16:56:47 -07002548 __lpfc_sli_rpi_release(vport, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002549 }
James Smart97acd002019-09-21 20:58:51 -07002550 if (vport->load_flag & FC_UNLOADING)
2551 lpfc_nlp_put(ndlp);
James Smart9b164062019-03-12 16:30:06 -07002552 pmb->ctx_ndlp = NULL;
James Smartdea16bd2018-11-29 16:09:30 -08002553 }
James Smartd439d282010-09-29 11:18:45 -04002554 }
2555
James Smartdcf2a4e2010-09-29 11:18:53 -04002556 /* Check security permission status on INIT_LINK mailbox command */
2557 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2558 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2559 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2560 "2860 SLI authentication is required "
2561 "for INIT_LINK but has not done yet\n");
2562
James Smart04c68492009-05-22 14:52:52 -04002563 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2564 lpfc_sli4_mbox_cmd_free(phba, pmb);
2565 else
2566 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002567}
James Smartbe6bb942015-04-07 15:07:22 -04002568 /**
2569 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2570 * @phba: Pointer to HBA context object.
2571 * @pmb: Pointer to mailbox object.
2572 *
2573 * This function is the unreg rpi mailbox completion handler. It
2574 * frees the memory resources associated with the completed mailbox
2575 * command. An additional refrenece is put on the ndlp to prevent
2576 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2577 * the unreg mailbox command completes, this routine puts the
2578 * reference back.
2579 *
2580 **/
2581void
2582lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2583{
2584 struct lpfc_vport *vport = pmb->vport;
2585 struct lpfc_nodelist *ndlp;
2586
James Smart3e1f0712018-11-29 16:09:29 -08002587 ndlp = pmb->ctx_ndlp;
James Smartbe6bb942015-04-07 15:07:22 -04002588 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2589 if (phba->sli_rev == LPFC_SLI_REV4 &&
2590 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002591 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002592 LPFC_SLI_INTF_IF_TYPE_2)) {
2593 if (ndlp) {
James Smartdea16bd2018-11-29 16:09:30 -08002594 lpfc_printf_vlog(
2595 vport, KERN_INFO, LOG_MBOX | LOG_SLI,
2596 "0010 UNREG_LOGIN vpi:%x "
2597 "rpi:%x DID:%x defer x%x flg x%x "
James Smart32350662019-08-14 16:57:06 -07002598 "map:%x %px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002599 vport->vpi, ndlp->nlp_rpi,
2600 ndlp->nlp_DID, ndlp->nlp_defer_did,
2601 ndlp->nlp_flag,
2602 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002603 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002604 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002605
2606 /* Check to see if there are any deferred
2607 * events to process
2608 */
2609 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2610 (ndlp->nlp_defer_did !=
2611 NLP_EVT_NOTHING_PENDING)) {
2612 lpfc_printf_vlog(
2613 vport, KERN_INFO, LOG_DISCOVERY,
2614 "4111 UNREG cmpl deferred "
2615 "clr x%x on "
James Smart32350662019-08-14 16:57:06 -07002616 "NPort x%x Data: x%x x%px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002617 ndlp->nlp_rpi, ndlp->nlp_DID,
2618 ndlp->nlp_defer_did, ndlp);
James Smart00292e02018-12-13 15:17:55 -08002619 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002620 ndlp->nlp_defer_did =
2621 NLP_EVT_NOTHING_PENDING;
2622 lpfc_issue_els_plogi(
2623 vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002624 } else {
James Smartb95b2112019-08-14 16:56:47 -07002625 __lpfc_sli_rpi_release(vport, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002626 }
James Smartbe6bb942015-04-07 15:07:22 -04002627 }
2628 }
2629 }
2630
2631 mempool_free(pmb, phba->mbox_mem_pool);
2632}
dea31012005-04-17 16:05:31 -05002633
James Smarte59058c2008-08-24 21:49:00 -04002634/**
James Smart3621a712009-04-06 18:47:14 -04002635 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002636 * @phba: Pointer to HBA context object.
2637 *
2638 * This function is called with no lock held. This function processes all
2639 * the completed mailbox commands and gives it to upper layers. The interrupt
2640 * service routine processes mailbox completion interrupt and adds completed
2641 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2642 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2643 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2644 * function returns the mailbox commands to the upper layer by calling the
2645 * completion handler function of each mailbox.
2646 **/
dea31012005-04-17 16:05:31 -05002647int
James Smart2e0fef82007-06-17 19:56:36 -05002648lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002649{
James Smart92d7f7b2007-06-17 19:56:38 -05002650 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002651 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002652 int rc;
2653 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002654
2655 phba->sli.slistat.mbox_event++;
2656
James Smart92d7f7b2007-06-17 19:56:38 -05002657 /* Get all completed mailboxe buffers into the cmplq */
2658 spin_lock_irq(&phba->hbalock);
2659 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2660 spin_unlock_irq(&phba->hbalock);
2661
dea31012005-04-17 16:05:31 -05002662 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002663 do {
2664 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2665 if (pmb == NULL)
2666 break;
2667
James Smart04c68492009-05-22 14:52:52 -04002668 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002669
James Smart858c9f62007-06-17 19:56:39 -05002670 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2671 if (pmb->vport) {
2672 lpfc_debugfs_disc_trc(pmb->vport,
2673 LPFC_DISC_TRC_MBOX_VPORT,
2674 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2675 (uint32_t)pmbox->mbxCommand,
2676 pmbox->un.varWords[0],
2677 pmbox->un.varWords[1]);
2678 }
2679 else {
2680 lpfc_debugfs_disc_trc(phba->pport,
2681 LPFC_DISC_TRC_MBOX,
2682 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2683 (uint32_t)pmbox->mbxCommand,
2684 pmbox->un.varWords[0],
2685 pmbox->un.varWords[1]);
2686 }
2687 }
2688
dea31012005-04-17 16:05:31 -05002689 /*
2690 * It is a fatal error if unknown mbox command completion.
2691 */
2692 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2693 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002694 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002695 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002696 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002697 "x%x (x%x/x%x) Cmpl\n",
James Smart43bfea12019-09-21 20:58:57 -07002698 pmb->vport ? pmb->vport->vpi :
2699 LPFC_VPORT_UNKNOWN,
James Smart04c68492009-05-22 14:52:52 -04002700 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002701 lpfc_sli_config_mbox_subsys_get(phba,
2702 pmb),
2703 lpfc_sli_config_mbox_opcode_get(phba,
2704 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002705 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002706 phba->work_hs = HS_FFER3;
2707 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002708 continue;
dea31012005-04-17 16:05:31 -05002709 }
2710
dea31012005-04-17 16:05:31 -05002711 if (pmbox->mbxStatus) {
2712 phba->sli.slistat.mbox_stat_err++;
2713 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2714 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002715 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002716 LOG_MBOX | LOG_SLI,
2717 "(%d):0305 Mbox cmd cmpl "
2718 "error - RETRYing Data: x%x "
2719 "(x%x/x%x) x%x x%x x%x\n",
James Smart43bfea12019-09-21 20:58:57 -07002720 pmb->vport ? pmb->vport->vpi :
2721 LPFC_VPORT_UNKNOWN,
James Smarta183a152011-10-10 21:32:43 -04002722 pmbox->mbxCommand,
2723 lpfc_sli_config_mbox_subsys_get(phba,
2724 pmb),
2725 lpfc_sli_config_mbox_opcode_get(phba,
2726 pmb),
2727 pmbox->mbxStatus,
2728 pmbox->un.varWords[0],
James Smart43bfea12019-09-21 20:58:57 -07002729 pmb->vport ? pmb->vport->port_state :
2730 LPFC_VPORT_UNKNOWN);
dea31012005-04-17 16:05:31 -05002731 pmbox->mbxStatus = 0;
2732 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002733 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002734 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002735 continue;
dea31012005-04-17 16:05:31 -05002736 }
2737 }
2738
2739 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002740 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
Sakari Ailus2d44d162019-09-04 19:04:23 +03002741 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
James Smarte74c03c2013-04-17 20:15:19 -04002742 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2743 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002744 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002745 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002746 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2747 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002748 pmb->mbox_cmpl,
2749 *((uint32_t *) pmbox),
2750 pmbox->un.varWords[0],
2751 pmbox->un.varWords[1],
2752 pmbox->un.varWords[2],
2753 pmbox->un.varWords[3],
2754 pmbox->un.varWords[4],
2755 pmbox->un.varWords[5],
2756 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002757 pmbox->un.varWords[7],
2758 pmbox->un.varWords[8],
2759 pmbox->un.varWords[9],
2760 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002761
James Smart92d7f7b2007-06-17 19:56:38 -05002762 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002763 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002764 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002765 return 0;
dea31012005-04-17 16:05:31 -05002766}
James Smart92d7f7b2007-06-17 19:56:38 -05002767
James Smarte59058c2008-08-24 21:49:00 -04002768/**
James Smart3621a712009-04-06 18:47:14 -04002769 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002770 * @phba: Pointer to HBA context object.
2771 * @pring: Pointer to driver SLI ring object.
2772 * @tag: buffer tag.
2773 *
2774 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2775 * is set in the tag the buffer is posted for a particular exchange,
2776 * the function will return the buffer without replacing the buffer.
2777 * If the buffer is for unsolicited ELS or CT traffic, this function
2778 * returns the buffer and also posts another buffer to the firmware.
2779 **/
James Smart76bb24e2007-10-27 13:38:00 -04002780static struct lpfc_dmabuf *
2781lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002782 struct lpfc_sli_ring *pring,
2783 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002784{
James Smart9f1e1b52008-12-04 22:39:40 -05002785 struct hbq_dmabuf *hbq_entry;
2786
James Smart76bb24e2007-10-27 13:38:00 -04002787 if (tag & QUE_BUFTAG_BIT)
2788 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002789 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2790 if (!hbq_entry)
2791 return NULL;
2792 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002793}
James Smart57127f12007-10-27 13:37:05 -04002794
James Smart3772a992009-05-22 14:50:54 -04002795/**
James Smart3a8070c2020-03-31 09:50:05 -07002796 * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer
2797 * containing a NVME LS request.
2798 * @phba: pointer to lpfc hba data structure.
2799 * @piocb: pointer to the iocbq struct representing the sequence starting
2800 * frame.
2801 *
2802 * This routine initially validates the NVME LS, validates there is a login
2803 * with the port that sent the LS, and then calls the appropriate nvme host
2804 * or target LS request handler.
2805 **/
2806static void
2807lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
2808{
2809 struct lpfc_nodelist *ndlp;
2810 struct lpfc_dmabuf *d_buf;
2811 struct hbq_dmabuf *nvmebuf;
2812 struct fc_frame_header *fc_hdr;
2813 struct lpfc_async_xchg_ctx *axchg = NULL;
2814 char *failwhy = NULL;
2815 uint32_t oxid, sid, did, fctl, size;
James Smart4e57e0b2020-05-20 11:59:28 -07002816 int ret = 1;
James Smart3a8070c2020-03-31 09:50:05 -07002817
2818 d_buf = piocb->context2;
2819
2820 nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2821 fc_hdr = nvmebuf->hbuf.virt;
2822 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2823 sid = sli4_sid_from_fc_hdr(fc_hdr);
2824 did = sli4_did_from_fc_hdr(fc_hdr);
2825 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
2826 fc_hdr->fh_f_ctl[1] << 8 |
2827 fc_hdr->fh_f_ctl[2]);
2828 size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
2829
2830 lpfc_nvmeio_data(phba, "NVME LS RCV: xri x%x sz %d from %06x\n",
2831 oxid, size, sid);
2832
2833 if (phba->pport->load_flag & FC_UNLOADING) {
2834 failwhy = "Driver Unloading";
2835 } else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
2836 failwhy = "NVME FC4 Disabled";
2837 } else if (!phba->nvmet_support && !phba->pport->localport) {
2838 failwhy = "No Localport";
2839 } else if (phba->nvmet_support && !phba->targetport) {
2840 failwhy = "No Targetport";
2841 } else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {
2842 failwhy = "Bad NVME LS R_CTL";
2843 } else if (unlikely((fctl & 0x00FF0000) !=
2844 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {
2845 failwhy = "Bad NVME LS F_CTL";
2846 } else {
2847 axchg = kzalloc(sizeof(*axchg), GFP_ATOMIC);
2848 if (!axchg)
2849 failwhy = "No CTX memory";
2850 }
2851
2852 if (unlikely(failwhy)) {
2853 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC | LOG_NVME_IOERR,
2854 "6154 Drop NVME LS: SID %06X OXID x%X: %s\n",
2855 sid, oxid, failwhy);
2856 goto out_fail;
2857 }
2858
2859 /* validate the source of the LS is logged in */
2860 ndlp = lpfc_findnode_did(phba->pport, sid);
2861 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2862 ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2863 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2864 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2865 "6216 NVME Unsol rcv: No ndlp: "
2866 "NPort_ID x%x oxid x%x\n",
2867 sid, oxid);
2868 goto out_fail;
2869 }
2870
2871 axchg->phba = phba;
2872 axchg->ndlp = ndlp;
2873 axchg->size = size;
2874 axchg->oxid = oxid;
2875 axchg->sid = sid;
2876 axchg->wqeq = NULL;
2877 axchg->state = LPFC_NVME_STE_LS_RCV;
2878 axchg->entry_cnt = 1;
2879 axchg->rqb_buffer = (void *)nvmebuf;
2880 axchg->hdwq = &phba->sli4_hba.hdwq[0];
2881 axchg->payload = nvmebuf->dbuf.virt;
2882 INIT_LIST_HEAD(&axchg->list);
2883
2884 if (phba->nvmet_support)
2885 ret = lpfc_nvmet_handle_lsreq(phba, axchg);
2886 else
2887 ret = lpfc_nvme_handle_lsreq(phba, axchg);
2888
2889 /* if zero, LS was successfully handled. If non-zero, LS not handled */
2890 if (!ret)
2891 return;
2892
2893 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC | LOG_NVME_IOERR,
2894 "6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "
2895 "NVMe%s handler failed %d\n",
2896 did, sid, oxid,
2897 (phba->nvmet_support) ? "T" : "I", ret);
2898
2899out_fail:
James Smart3a8070c2020-03-31 09:50:05 -07002900
2901 /* recycle receive buffer */
2902 lpfc_in_buf_free(phba, &nvmebuf->dbuf);
2903
2904 /* If start of new exchange, abort it */
James Smart4e57e0b2020-05-20 11:59:28 -07002905 if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))
2906 ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);
2907
2908 if (ret)
2909 kfree(axchg);
James Smart3a8070c2020-03-31 09:50:05 -07002910}
2911
2912/**
James Smart3772a992009-05-22 14:50:54 -04002913 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2914 * @phba: Pointer to HBA context object.
2915 * @pring: Pointer to driver SLI ring object.
2916 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2917 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2918 * @fch_type: the type for the first frame of the sequence.
2919 *
2920 * This function is called with no lock held. This function uses the r_ctl and
2921 * type of the received sequence to find the correct callback function to call
2922 * to process the sequence.
2923 **/
2924static int
2925lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2926 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2927 uint32_t fch_type)
2928{
2929 int i;
2930
James Smartf358dd02017-02-12 13:52:34 -08002931 switch (fch_type) {
2932 case FC_TYPE_NVME:
James Smart3a8070c2020-03-31 09:50:05 -07002933 lpfc_nvme_unsol_ls_handler(phba, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002934 return 1;
2935 default:
2936 break;
2937 }
2938
James Smart3772a992009-05-22 14:50:54 -04002939 /* unSolicited Responses */
2940 if (pring->prt[0].profile) {
2941 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2942 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2943 saveq);
2944 return 1;
2945 }
2946 /* We must search, based on rctl / type
2947 for the right routine */
2948 for (i = 0; i < pring->num_mask; i++) {
2949 if ((pring->prt[i].rctl == fch_r_ctl) &&
2950 (pring->prt[i].type == fch_type)) {
2951 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2952 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2953 (phba, pring, saveq);
2954 return 1;
2955 }
2956 }
2957 return 0;
2958}
James Smarte59058c2008-08-24 21:49:00 -04002959
2960/**
James Smart3621a712009-04-06 18:47:14 -04002961 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002962 * @phba: Pointer to HBA context object.
2963 * @pring: Pointer to driver SLI ring object.
2964 * @saveq: Pointer to the unsolicited iocb.
2965 *
2966 * This function is called with no lock held by the ring event handler
2967 * when there is an unsolicited iocb posted to the response ring by the
2968 * firmware. This function gets the buffer associated with the iocbs
2969 * and calls the event handler for the ring. This function handles both
2970 * qring buffers and hbq buffers.
2971 * When the function returns 1 the caller can free the iocb object otherwise
2972 * upper layer functions will free the iocb objects.
2973 **/
dea31012005-04-17 16:05:31 -05002974static int
2975lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2976 struct lpfc_iocbq *saveq)
2977{
2978 IOCB_t * irsp;
2979 WORD5 * w5p;
2980 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002981 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002982 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002983
dea31012005-04-17 16:05:31 -05002984 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002985
2986 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2987 if (pring->lpfc_sli_rcv_async_status)
2988 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2989 else
2990 lpfc_printf_log(phba,
2991 KERN_WARNING,
2992 LOG_SLI,
2993 "0316 Ring %d handler: unexpected "
2994 "ASYNC_STATUS iocb received evt_code "
2995 "0x%x\n",
2996 pring->ringno,
2997 irsp->un.asyncstat.evt_code);
2998 return 1;
2999 }
3000
James Smart3163f722008-02-08 18:50:25 -05003001 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
3002 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
3003 if (irsp->ulpBdeCount > 0) {
3004 dmzbuf = lpfc_sli_get_buff(phba, pring,
3005 irsp->un.ulpWord[3]);
3006 lpfc_in_buf_free(phba, dmzbuf);
3007 }
3008
3009 if (irsp->ulpBdeCount > 1) {
3010 dmzbuf = lpfc_sli_get_buff(phba, pring,
3011 irsp->unsli3.sli3Words[3]);
3012 lpfc_in_buf_free(phba, dmzbuf);
3013 }
3014
3015 if (irsp->ulpBdeCount > 2) {
3016 dmzbuf = lpfc_sli_get_buff(phba, pring,
3017 irsp->unsli3.sli3Words[7]);
3018 lpfc_in_buf_free(phba, dmzbuf);
3019 }
3020
3021 return 1;
3022 }
3023
James Smart92d7f7b2007-06-17 19:56:38 -05003024 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04003025 if (irsp->ulpBdeCount != 0) {
3026 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05003027 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04003028 if (!saveq->context2)
3029 lpfc_printf_log(phba,
3030 KERN_ERR,
3031 LOG_SLI,
3032 "0341 Ring %d Cannot find buffer for "
3033 "an unsolicited iocb. tag 0x%x\n",
3034 pring->ringno,
3035 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04003036 }
3037 if (irsp->ulpBdeCount == 2) {
3038 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04003039 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04003040 if (!saveq->context3)
3041 lpfc_printf_log(phba,
3042 KERN_ERR,
3043 LOG_SLI,
3044 "0342 Ring %d Cannot find buffer for an"
3045 " unsolicited iocb. tag 0x%x\n",
3046 pring->ringno,
3047 irsp->unsli3.sli3Words[7]);
3048 }
3049 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04003050 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04003051 if (irsp->ulpBdeCount != 0) {
3052 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
3053 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05003054 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04003055 lpfc_printf_log(phba,
3056 KERN_ERR,
3057 LOG_SLI,
3058 "0343 Ring %d Cannot find "
3059 "buffer for an unsolicited iocb"
3060 ". tag 0x%x\n", pring->ringno,
3061 irsp->un.ulpWord[3]);
3062 }
3063 if (irsp->ulpBdeCount == 2) {
3064 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
3065 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05003066 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04003067 lpfc_printf_log(phba,
3068 KERN_ERR,
3069 LOG_SLI,
3070 "0344 Ring %d Cannot find "
3071 "buffer for an unsolicited "
3072 "iocb. tag 0x%x\n",
3073 pring->ringno,
3074 irsp->unsli3.sli3Words[7]);
3075 }
3076 }
James Smart92d7f7b2007-06-17 19:56:38 -05003077 }
James Smart9c2face2008-01-11 01:53:18 -05003078 if (irsp->ulpBdeCount != 0 &&
3079 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
3080 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
3081 int found = 0;
3082
3083 /* search continue save q for same XRI */
3084 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04003085 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
3086 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05003087 list_add_tail(&saveq->list, &iocbq->list);
3088 found = 1;
3089 break;
3090 }
3091 }
3092 if (!found)
3093 list_add_tail(&saveq->clist,
3094 &pring->iocb_continue_saveq);
3095 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
3096 list_del_init(&iocbq->clist);
3097 saveq = iocbq;
3098 irsp = &(saveq->iocb);
3099 } else
3100 return 0;
3101 }
3102 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
3103 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
3104 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04003105 Rctl = FC_RCTL_ELS_REQ;
3106 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05003107 } else {
3108 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
3109 Rctl = w5p->hcsw.Rctl;
3110 Type = w5p->hcsw.Type;
3111
3112 /* Firmware Workaround */
3113 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
3114 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
3115 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04003116 Rctl = FC_RCTL_ELS_REQ;
3117 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05003118 w5p->hcsw.Rctl = Rctl;
3119 w5p->hcsw.Type = Type;
3120 }
3121 }
James Smart92d7f7b2007-06-17 19:56:38 -05003122
James Smart3772a992009-05-22 14:50:54 -04003123 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05003124 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003125 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003126 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04003127 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04003128
James Smart92d7f7b2007-06-17 19:56:38 -05003129 return 1;
dea31012005-04-17 16:05:31 -05003130}
3131
James Smarte59058c2008-08-24 21:49:00 -04003132/**
James Smart3621a712009-04-06 18:47:14 -04003133 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04003134 * @phba: Pointer to HBA context object.
3135 * @pring: Pointer to driver SLI ring object.
3136 * @prspiocb: Pointer to response iocb object.
3137 *
3138 * This function looks up the iocb_lookup table to get the command iocb
3139 * corresponding to the given response iocb using the iotag of the
James Smarte2a8be52019-05-06 17:26:47 -07003140 * response iocb. The driver calls this function with the hbalock held
3141 * for SLI3 ports or the ring lock held for SLI4 ports.
James Smarte59058c2008-08-24 21:49:00 -04003142 * This function returns the command iocb object if it finds the command
3143 * iocb else returns NULL.
3144 **/
dea31012005-04-17 16:05:31 -05003145static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05003146lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3147 struct lpfc_sli_ring *pring,
3148 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05003149{
dea31012005-04-17 16:05:31 -05003150 struct lpfc_iocbq *cmd_iocb = NULL;
3151 uint16_t iotag;
James Smarte2a8be52019-05-06 17:26:47 -07003152 spinlock_t *temp_lock = NULL;
3153 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05003154
James Smarte2a8be52019-05-06 17:26:47 -07003155 if (phba->sli_rev == LPFC_SLI_REV4)
3156 temp_lock = &pring->ring_lock;
3157 else
3158 temp_lock = &phba->hbalock;
3159
3160 spin_lock_irqsave(temp_lock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003161 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05003162
James Bottomley604a3e32005-10-29 10:28:33 -05003163 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3164 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003165 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07003166 /* remove from txcmpl queue list */
3167 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04003168 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003169 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003170 spin_unlock_irqrestore(temp_lock, iflag);
James Smart89533e92016-10-13 15:06:15 -07003171 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003172 }
dea31012005-04-17 16:05:31 -05003173 }
3174
James Smarte2a8be52019-05-06 17:26:47 -07003175 spin_unlock_irqrestore(temp_lock, iflag);
dea31012005-04-17 16:05:31 -05003176 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07003177 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05003178 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003179 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05003180 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05003181 return NULL;
3182}
3183
James Smarte59058c2008-08-24 21:49:00 -04003184/**
James Smart3772a992009-05-22 14:50:54 -04003185 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3186 * @phba: Pointer to HBA context object.
3187 * @pring: Pointer to driver SLI ring object.
3188 * @iotag: IOCB tag.
3189 *
3190 * This function looks up the iocb_lookup table to get the command iocb
James Smarte2a8be52019-05-06 17:26:47 -07003191 * corresponding to the given iotag. The driver calls this function with
3192 * the ring lock held because this function is an SLI4 port only helper.
James Smart3772a992009-05-22 14:50:54 -04003193 * This function returns the command iocb object if it finds the command
3194 * iocb else returns NULL.
3195 **/
3196static struct lpfc_iocbq *
3197lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3198 struct lpfc_sli_ring *pring, uint16_t iotag)
3199{
James Smart895427b2017-02-12 13:52:30 -08003200 struct lpfc_iocbq *cmd_iocb = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07003201 spinlock_t *temp_lock = NULL;
3202 unsigned long iflag = 0;
James Smart3772a992009-05-22 14:50:54 -04003203
James Smarte2a8be52019-05-06 17:26:47 -07003204 if (phba->sli_rev == LPFC_SLI_REV4)
3205 temp_lock = &pring->ring_lock;
3206 else
3207 temp_lock = &phba->hbalock;
3208
3209 spin_lock_irqsave(temp_lock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003210 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3211 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003212 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3213 /* remove from txcmpl queue list */
3214 list_del_init(&cmd_iocb->list);
3215 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003216 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003217 spin_unlock_irqrestore(temp_lock, iflag);
James Smart4f2e66c2012-05-09 21:17:07 -04003218 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003219 }
James Smart3772a992009-05-22 14:50:54 -04003220 }
James Smart89533e92016-10-13 15:06:15 -07003221
James Smarte2a8be52019-05-06 17:26:47 -07003222 spin_unlock_irqrestore(temp_lock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003223 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08003224 "0372 iotag x%x lookup error: max iotag (x%x) "
3225 "iocb_flag x%x\n",
3226 iotag, phba->sli.last_iotag,
3227 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003228 return NULL;
3229}
3230
3231/**
James Smart3621a712009-04-06 18:47:14 -04003232 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003233 * @phba: Pointer to HBA context object.
3234 * @pring: Pointer to driver SLI ring object.
3235 * @saveq: Pointer to the response iocb to be processed.
3236 *
3237 * This function is called by the ring event handler for non-fcp
3238 * rings when there is a new response iocb in the response ring.
3239 * The caller is not required to hold any locks. This function
3240 * gets the command iocb associated with the response iocb and
3241 * calls the completion handler for the command iocb. If there
3242 * is no completion handler, the function will free the resources
3243 * associated with command iocb. If the response iocb is for
3244 * an already aborted command iocb, the status of the completion
3245 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3246 * This function always returns 1.
3247 **/
dea31012005-04-17 16:05:31 -05003248static int
James Smart2e0fef82007-06-17 19:56:36 -05003249lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003250 struct lpfc_iocbq *saveq)
3251{
James Smart2e0fef82007-06-17 19:56:36 -05003252 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003253 int rc = 1;
3254 unsigned long iflag;
3255
James Bottomley604a3e32005-10-29 10:28:33 -05003256 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
dea31012005-04-17 16:05:31 -05003257 if (cmdiocbp) {
3258 if (cmdiocbp->iocb_cmpl) {
3259 /*
James Smartea2151b2008-09-07 11:52:10 -04003260 * If an ELS command failed send an event to mgmt
3261 * application.
3262 */
3263 if (saveq->iocb.ulpStatus &&
3264 (pring->ringno == LPFC_ELS_RING) &&
3265 (cmdiocbp->iocb.ulpCommand ==
3266 CMD_ELS_REQUEST64_CR))
3267 lpfc_send_els_failure_event(phba,
3268 cmdiocbp, saveq);
3269
3270 /*
dea31012005-04-17 16:05:31 -05003271 * Post all ELS completions to the worker thread.
3272 * All other are passed to the completion callback.
3273 */
3274 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003275 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3276 (cmdiocbp->iocb_flag &
3277 LPFC_DRIVER_ABORTED)) {
3278 spin_lock_irqsave(&phba->hbalock,
3279 iflag);
James Smart07951072007-04-25 09:51:38 -04003280 cmdiocbp->iocb_flag &=
3281 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003282 spin_unlock_irqrestore(&phba->hbalock,
3283 iflag);
James Smart07951072007-04-25 09:51:38 -04003284 saveq->iocb.ulpStatus =
3285 IOSTAT_LOCAL_REJECT;
3286 saveq->iocb.un.ulpWord[4] =
3287 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003288
3289 /* Firmware could still be in progress
3290 * of DMAing payload, so don't free data
3291 * buffer till after a hbeat.
3292 */
James Smart341af102010-01-26 23:07:37 -05003293 spin_lock_irqsave(&phba->hbalock,
3294 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003295 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003296 spin_unlock_irqrestore(&phba->hbalock,
3297 iflag);
3298 }
James Smart0f65ff62010-02-26 14:14:23 -05003299 if (phba->sli_rev == LPFC_SLI_REV4) {
3300 if (saveq->iocb_flag &
3301 LPFC_EXCHANGE_BUSY) {
3302 /* Set cmdiocb flag for the
3303 * exchange busy so sgl (xri)
3304 * will not be released until
3305 * the abort xri is received
3306 * from hba.
3307 */
3308 spin_lock_irqsave(
3309 &phba->hbalock, iflag);
3310 cmdiocbp->iocb_flag |=
3311 LPFC_EXCHANGE_BUSY;
3312 spin_unlock_irqrestore(
3313 &phba->hbalock, iflag);
3314 }
3315 if (cmdiocbp->iocb_flag &
3316 LPFC_DRIVER_ABORTED) {
3317 /*
3318 * Clear LPFC_DRIVER_ABORTED
3319 * bit in case it was driver
3320 * initiated abort.
3321 */
3322 spin_lock_irqsave(
3323 &phba->hbalock, iflag);
3324 cmdiocbp->iocb_flag &=
3325 ~LPFC_DRIVER_ABORTED;
3326 spin_unlock_irqrestore(
3327 &phba->hbalock, iflag);
3328 cmdiocbp->iocb.ulpStatus =
3329 IOSTAT_LOCAL_REJECT;
3330 cmdiocbp->iocb.un.ulpWord[4] =
3331 IOERR_ABORT_REQUESTED;
3332 /*
3333 * For SLI4, irsiocb contains
3334 * NO_XRI in sli_xritag, it
3335 * shall not affect releasing
3336 * sgl (xri) process.
3337 */
3338 saveq->iocb.ulpStatus =
3339 IOSTAT_LOCAL_REJECT;
3340 saveq->iocb.un.ulpWord[4] =
3341 IOERR_SLI_ABORTED;
3342 spin_lock_irqsave(
3343 &phba->hbalock, iflag);
3344 saveq->iocb_flag |=
3345 LPFC_DELAY_MEM_FREE;
3346 spin_unlock_irqrestore(
3347 &phba->hbalock, iflag);
3348 }
James Smart07951072007-04-25 09:51:38 -04003349 }
dea31012005-04-17 16:05:31 -05003350 }
James Smart2e0fef82007-06-17 19:56:36 -05003351 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003352 } else
3353 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003354 } else {
3355 /*
3356 * Unknown initiating command based on the response iotag.
3357 * This could be the case on the ELS ring because of
3358 * lpfc_els_abort().
3359 */
3360 if (pring->ringno != LPFC_ELS_RING) {
3361 /*
3362 * Ring <ringno> handler: unexpected completion IoTag
3363 * <IoTag>
3364 */
James Smarta257bf92009-04-06 18:48:10 -04003365 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003366 "0322 Ring %d handler: "
3367 "unexpected completion IoTag x%x "
3368 "Data: x%x x%x x%x x%x\n",
3369 pring->ringno,
3370 saveq->iocb.ulpIoTag,
3371 saveq->iocb.ulpStatus,
3372 saveq->iocb.un.ulpWord[4],
3373 saveq->iocb.ulpCommand,
3374 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003375 }
3376 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003377
dea31012005-04-17 16:05:31 -05003378 return rc;
3379}
3380
James Smarte59058c2008-08-24 21:49:00 -04003381/**
James Smart3621a712009-04-06 18:47:14 -04003382 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003383 * @phba: Pointer to HBA context object.
3384 * @pring: Pointer to driver SLI ring object.
3385 *
3386 * This function is called from the iocb ring event handlers when
3387 * put pointer is ahead of the get pointer for a ring. This function signal
3388 * an error attention condition to the worker thread and the worker
3389 * thread will transition the HBA to offline state.
3390 **/
James Smart2e0fef82007-06-17 19:56:36 -05003391static void
3392lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003393{
James Smart34b02dc2008-08-24 21:49:55 -04003394 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003395 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003396 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003397 * rsp ring <portRspMax>
3398 */
3399 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003400 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003401 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003402 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003403 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003404
James Smart2e0fef82007-06-17 19:56:36 -05003405 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003406
3407 /*
3408 * All error attention handlers are posted to
3409 * worker thread
3410 */
3411 phba->work_ha |= HA_ERATT;
3412 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003413
James Smart5e9d9b82008-06-14 22:52:53 -04003414 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003415
3416 return;
3417}
3418
James Smarte59058c2008-08-24 21:49:00 -04003419/**
James Smart3621a712009-04-06 18:47:14 -04003420 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003421 * @ptr: Pointer to address of HBA context object.
3422 *
3423 * This function is invoked by the Error Attention polling timer when the
3424 * timer times out. It will check the SLI Error Attention register for
3425 * possible attention events. If so, it will post an Error Attention event
3426 * and wake up worker thread to process it. Otherwise, it will set up the
3427 * Error Attention polling timer for the next poll.
3428 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003429void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003430{
3431 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003432 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003433 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003434
Kees Cookf22eb4d2017-09-06 20:24:26 -07003435 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003436
James Smartaa6fbb72012-08-03 12:36:03 -04003437 /* Here we will also keep track of interrupts per sec of the hba */
3438 sli_intr = phba->sli.slistat.sli_intr;
3439
3440 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3441 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3442 sli_intr);
3443 else
3444 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3445
James Smart65791f12016-07-06 12:35:56 -07003446 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3447 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003448 phba->sli.slistat.sli_ips = cnt;
3449
3450 phba->sli.slistat.sli_prev_intr = sli_intr;
3451
James Smart93996272008-08-24 21:50:30 -04003452 /* Check chip HA register for error event */
3453 eratt = lpfc_sli_check_eratt(phba);
3454
3455 if (eratt)
3456 /* Tell the worker thread there is work to do */
3457 lpfc_worker_wake_up(phba);
3458 else
3459 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003460 mod_timer(&phba->eratt_poll,
3461 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003462 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003463 return;
3464}
3465
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003466
James Smarte59058c2008-08-24 21:49:00 -04003467/**
James Smart3621a712009-04-06 18:47:14 -04003468 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003469 * @phba: Pointer to HBA context object.
3470 * @pring: Pointer to driver SLI ring object.
3471 * @mask: Host attention register mask for this ring.
3472 *
3473 * This function is called from the interrupt context when there is a ring
3474 * event for the fcp ring. The caller does not hold any lock.
3475 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003476 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003477 * LE bit set. The function will call the completion handler of the command iocb
3478 * if the response iocb indicates a completion for a command iocb or it is
3479 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3480 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003481 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003482 * to check it explicitly.
3483 */
3484int
James Smart2e0fef82007-06-17 19:56:36 -05003485lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3486 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003487{
James Smart34b02dc2008-08-24 21:49:55 -04003488 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003489 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003490 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003491 struct lpfc_iocbq *cmdiocbq = NULL;
3492 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003493 uint32_t status;
3494 uint32_t portRspPut, portRspMax;
3495 int rc = 1;
3496 lpfc_iocb_type type;
3497 unsigned long iflag;
3498 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003499
James Smart2e0fef82007-06-17 19:56:36 -05003500 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003501 pring->stats.iocb_event++;
3502
dea31012005-04-17 16:05:31 -05003503 /*
3504 * The next available response entry should never exceed the maximum
3505 * entries. If it does, treat it as an adapter hardware error.
3506 */
James Smart7e56aa22012-08-03 12:35:34 -04003507 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003508 portRspPut = le32_to_cpu(pgp->rspPutInx);
3509 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003510 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003511 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003512 return 1;
3513 }
James Smart45ed1192009-10-02 15:17:02 -04003514 if (phba->fcp_ring_in_use) {
3515 spin_unlock_irqrestore(&phba->hbalock, iflag);
3516 return 1;
3517 } else
3518 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003519
3520 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003521 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003522 /*
3523 * Fetch an entry off the ring and copy it into a local data
3524 * structure. The copy involves a byte-swap since the
3525 * network byte order and pci byte orders are different.
3526 */
James Smarted957682007-06-17 19:56:37 -05003527 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003528 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003529
James Smart7e56aa22012-08-03 12:35:34 -04003530 if (++pring->sli.sli3.rspidx >= portRspMax)
3531 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003532
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003533 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3534 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003535 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003536 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003537 irsp = &rspiocbq.iocb;
3538
dea31012005-04-17 16:05:31 -05003539 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3540 pring->stats.iocb_rsp++;
3541 rsp_cmpl++;
3542
3543 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003544 /*
3545 * If resource errors reported from HBA, reduce
3546 * queuedepths of the SCSI device.
3547 */
3548 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003549 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3550 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003551 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003552 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003553 spin_lock_irqsave(&phba->hbalock, iflag);
3554 }
3555
dea31012005-04-17 16:05:31 -05003556 /* Rsp ring <ringno> error: IOCB */
3557 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003558 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003559 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003560 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003561 irsp->un.ulpWord[0],
3562 irsp->un.ulpWord[1],
3563 irsp->un.ulpWord[2],
3564 irsp->un.ulpWord[3],
3565 irsp->un.ulpWord[4],
3566 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003567 *(uint32_t *)&irsp->un1,
3568 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003569 }
3570
3571 switch (type) {
3572 case LPFC_ABORT_IOCB:
3573 case LPFC_SOL_IOCB:
3574 /*
3575 * Idle exchange closed via ABTS from port. No iocb
3576 * resources need to be recovered.
3577 */
3578 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003579 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003580 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003581 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003582 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003583 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003584 break;
3585 }
3586
James Smarte2a8be52019-05-06 17:26:47 -07003587 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003588 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3589 &rspiocbq);
James Smarte2a8be52019-05-06 17:26:47 -07003590 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05003591 if (unlikely(!cmdiocbq))
3592 break;
3593 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3594 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3595 if (cmdiocbq->iocb_cmpl) {
3596 spin_unlock_irqrestore(&phba->hbalock, iflag);
3597 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3598 &rspiocbq);
3599 spin_lock_irqsave(&phba->hbalock, iflag);
3600 }
dea31012005-04-17 16:05:31 -05003601 break;
James Smarta4bc3372006-12-02 13:34:16 -05003602 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003603 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003604 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003605 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003606 break;
dea31012005-04-17 16:05:31 -05003607 default:
3608 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3609 char adaptermsg[LPFC_MAX_ADPTMSG];
3610 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3611 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3612 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003613 dev_warn(&((phba->pcidev)->dev),
3614 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003615 phba->brd_no, adaptermsg);
3616 } else {
3617 /* Unknown IOCB command */
3618 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003619 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003620 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003621 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003622 irsp->ulpStatus,
3623 irsp->ulpIoTag,
3624 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003625 }
3626 break;
3627 }
3628
3629 /*
3630 * The response IOCB has been processed. Update the ring
3631 * pointer in SLIM. If the port response put pointer has not
3632 * been updated, sync the pgp->rspPutInx and fetch the new port
3633 * response put pointer.
3634 */
James Smart7e56aa22012-08-03 12:35:34 -04003635 writel(pring->sli.sli3.rspidx,
3636 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003637
James Smart7e56aa22012-08-03 12:35:34 -04003638 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003639 portRspPut = le32_to_cpu(pgp->rspPutInx);
3640 }
3641
3642 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3643 pring->stats.iocb_rsp_full++;
3644 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3645 writel(status, phba->CAregaddr);
3646 readl(phba->CAregaddr);
3647 }
3648 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3649 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3650 pring->stats.iocb_cmd_empty++;
3651
3652 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003653 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003654 lpfc_sli_resume_iocb(phba, pring);
3655
3656 if ((pring->lpfc_sli_cmd_available))
3657 (pring->lpfc_sli_cmd_available) (phba, pring);
3658
3659 }
3660
James Smart45ed1192009-10-02 15:17:02 -04003661 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003662 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003663 return rc;
3664}
3665
James Smarte59058c2008-08-24 21:49:00 -04003666/**
James Smart3772a992009-05-22 14:50:54 -04003667 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3668 * @phba: Pointer to HBA context object.
3669 * @pring: Pointer to driver SLI ring object.
3670 * @rspiocbp: Pointer to driver response IOCB object.
3671 *
3672 * This function is called from the worker thread when there is a slow-path
3673 * response IOCB to process. This function chains all the response iocbs until
3674 * seeing the iocb with the LE bit set. The function will call
3675 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3676 * completion of a command iocb. The function will call the
3677 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3678 * The function frees the resources or calls the completion handler if this
3679 * iocb is an abort completion. The function returns NULL when the response
3680 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3681 * this function shall chain the iocb on to the iocb_continueq and return the
3682 * response iocb passed in.
3683 **/
3684static struct lpfc_iocbq *
3685lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3686 struct lpfc_iocbq *rspiocbp)
3687{
3688 struct lpfc_iocbq *saveq;
3689 struct lpfc_iocbq *cmdiocbp;
3690 struct lpfc_iocbq *next_iocb;
3691 IOCB_t *irsp = NULL;
3692 uint32_t free_saveq;
3693 uint8_t iocb_cmd_type;
3694 lpfc_iocb_type type;
3695 unsigned long iflag;
3696 int rc;
3697
3698 spin_lock_irqsave(&phba->hbalock, iflag);
3699 /* First add the response iocb to the countinueq list */
3700 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3701 pring->iocb_continueq_cnt++;
3702
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003703 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003704 irsp = &rspiocbp->iocb;
3705 if (irsp->ulpLe) {
3706 /*
3707 * By default, the driver expects to free all resources
3708 * associated with this iocb completion.
3709 */
3710 free_saveq = 1;
3711 saveq = list_get_first(&pring->iocb_continueq,
3712 struct lpfc_iocbq, list);
3713 irsp = &(saveq->iocb);
3714 list_del_init(&pring->iocb_continueq);
3715 pring->iocb_continueq_cnt = 0;
3716
3717 pring->stats.iocb_rsp++;
3718
3719 /*
3720 * If resource errors reported from HBA, reduce
3721 * queuedepths of the SCSI device.
3722 */
3723 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003724 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3725 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003726 spin_unlock_irqrestore(&phba->hbalock, iflag);
3727 phba->lpfc_rampdown_queue_depth(phba);
3728 spin_lock_irqsave(&phba->hbalock, iflag);
3729 }
3730
3731 if (irsp->ulpStatus) {
3732 /* Rsp ring <ringno> error: IOCB */
3733 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3734 "0328 Rsp Ring %d error: "
3735 "IOCB Data: "
3736 "x%x x%x x%x x%x "
3737 "x%x x%x x%x x%x "
3738 "x%x x%x x%x x%x "
3739 "x%x x%x x%x x%x\n",
3740 pring->ringno,
3741 irsp->un.ulpWord[0],
3742 irsp->un.ulpWord[1],
3743 irsp->un.ulpWord[2],
3744 irsp->un.ulpWord[3],
3745 irsp->un.ulpWord[4],
3746 irsp->un.ulpWord[5],
3747 *(((uint32_t *) irsp) + 6),
3748 *(((uint32_t *) irsp) + 7),
3749 *(((uint32_t *) irsp) + 8),
3750 *(((uint32_t *) irsp) + 9),
3751 *(((uint32_t *) irsp) + 10),
3752 *(((uint32_t *) irsp) + 11),
3753 *(((uint32_t *) irsp) + 12),
3754 *(((uint32_t *) irsp) + 13),
3755 *(((uint32_t *) irsp) + 14),
3756 *(((uint32_t *) irsp) + 15));
3757 }
3758
3759 /*
3760 * Fetch the IOCB command type and call the correct completion
3761 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3762 * get freed back to the lpfc_iocb_list by the discovery
3763 * kernel thread.
3764 */
3765 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3766 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3767 switch (type) {
3768 case LPFC_SOL_IOCB:
3769 spin_unlock_irqrestore(&phba->hbalock, iflag);
3770 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3771 spin_lock_irqsave(&phba->hbalock, iflag);
3772 break;
3773
3774 case LPFC_UNSOL_IOCB:
3775 spin_unlock_irqrestore(&phba->hbalock, iflag);
3776 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3777 spin_lock_irqsave(&phba->hbalock, iflag);
3778 if (!rc)
3779 free_saveq = 0;
3780 break;
3781
3782 case LPFC_ABORT_IOCB:
3783 cmdiocbp = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07003784 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX) {
3785 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003786 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3787 saveq);
James Smarte2a8be52019-05-06 17:26:47 -07003788 spin_lock_irqsave(&phba->hbalock, iflag);
3789 }
James Smart3772a992009-05-22 14:50:54 -04003790 if (cmdiocbp) {
3791 /* Call the specified completion routine */
3792 if (cmdiocbp->iocb_cmpl) {
3793 spin_unlock_irqrestore(&phba->hbalock,
3794 iflag);
3795 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3796 saveq);
3797 spin_lock_irqsave(&phba->hbalock,
3798 iflag);
3799 } else
3800 __lpfc_sli_release_iocbq(phba,
3801 cmdiocbp);
3802 }
3803 break;
3804
3805 case LPFC_UNKNOWN_IOCB:
3806 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3807 char adaptermsg[LPFC_MAX_ADPTMSG];
3808 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3809 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3810 MAX_MSG_DATA);
3811 dev_warn(&((phba->pcidev)->dev),
3812 "lpfc%d: %s\n",
3813 phba->brd_no, adaptermsg);
3814 } else {
3815 /* Unknown IOCB command */
3816 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3817 "0335 Unknown IOCB "
3818 "command Data: x%x "
3819 "x%x x%x x%x\n",
3820 irsp->ulpCommand,
3821 irsp->ulpStatus,
3822 irsp->ulpIoTag,
3823 irsp->ulpContext);
3824 }
3825 break;
3826 }
3827
3828 if (free_saveq) {
3829 list_for_each_entry_safe(rspiocbp, next_iocb,
3830 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003831 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003832 __lpfc_sli_release_iocbq(phba, rspiocbp);
3833 }
3834 __lpfc_sli_release_iocbq(phba, saveq);
3835 }
3836 rspiocbp = NULL;
3837 }
3838 spin_unlock_irqrestore(&phba->hbalock, iflag);
3839 return rspiocbp;
3840}
3841
3842/**
3843 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003844 * @phba: Pointer to HBA context object.
3845 * @pring: Pointer to driver SLI ring object.
3846 * @mask: Host attention register mask for this ring.
3847 *
James Smart3772a992009-05-22 14:50:54 -04003848 * This routine wraps the actual slow_ring event process routine from the
3849 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003850 **/
James Smart3772a992009-05-22 14:50:54 -04003851void
James Smart2e0fef82007-06-17 19:56:36 -05003852lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3853 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003854{
James Smart3772a992009-05-22 14:50:54 -04003855 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3856}
3857
3858/**
3859 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3860 * @phba: Pointer to HBA context object.
3861 * @pring: Pointer to driver SLI ring object.
3862 * @mask: Host attention register mask for this ring.
3863 *
3864 * This function is called from the worker thread when there is a ring event
3865 * for non-fcp rings. The caller does not hold any lock. The function will
3866 * remove each response iocb in the response ring and calls the handle
3867 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3868 **/
3869static void
3870lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3871 struct lpfc_sli_ring *pring, uint32_t mask)
3872{
James Smart34b02dc2008-08-24 21:49:55 -04003873 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003874 IOCB_t *entry;
3875 IOCB_t *irsp = NULL;
3876 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003877 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003878 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003879 uint32_t status;
dea31012005-04-17 16:05:31 -05003880
James Smart34b02dc2008-08-24 21:49:55 -04003881 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003882 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003883 pring->stats.iocb_event++;
3884
dea31012005-04-17 16:05:31 -05003885 /*
3886 * The next available response entry should never exceed the maximum
3887 * entries. If it does, treat it as an adapter hardware error.
3888 */
James Smart7e56aa22012-08-03 12:35:34 -04003889 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003890 portRspPut = le32_to_cpu(pgp->rspPutInx);
3891 if (portRspPut >= portRspMax) {
3892 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003893 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003894 * rsp ring <portRspMax>
3895 */
James Smarted957682007-06-17 19:56:37 -05003896 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003897 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003898 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003899 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003900
James Smart2e0fef82007-06-17 19:56:36 -05003901 phba->link_state = LPFC_HBA_ERROR;
3902 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003903
3904 phba->work_hs = HS_FFER3;
3905 lpfc_handle_eratt(phba);
3906
James Smart3772a992009-05-22 14:50:54 -04003907 return;
dea31012005-04-17 16:05:31 -05003908 }
3909
3910 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003911 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003912 /*
3913 * Build a completion list and call the appropriate handler.
3914 * The process is to get the next available response iocb, get
3915 * a free iocb from the list, copy the response data into the
3916 * free iocb, insert to the continuation list, and update the
3917 * next response index to slim. This process makes response
3918 * iocb's in the ring available to DMA as fast as possible but
3919 * pays a penalty for a copy operation. Since the iocb is
3920 * only 32 bytes, this penalty is considered small relative to
3921 * the PCI reads for register values and a slim write. When
3922 * the ulpLe field is set, the entire Command has been
3923 * received.
3924 */
James Smarted957682007-06-17 19:56:37 -05003925 entry = lpfc_resp_iocb(phba, pring);
3926
James Smart858c9f62007-06-17 19:56:39 -05003927 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003928 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003929 if (rspiocbp == NULL) {
3930 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003931 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003932 break;
3933 }
3934
James Smarted957682007-06-17 19:56:37 -05003935 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3936 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003937 irsp = &rspiocbp->iocb;
3938
James Smart7e56aa22012-08-03 12:35:34 -04003939 if (++pring->sli.sli3.rspidx >= portRspMax)
3940 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003941
James Smarta58cbd52007-08-02 11:09:43 -04003942 if (pring->ringno == LPFC_ELS_RING) {
3943 lpfc_debugfs_slow_ring_trc(phba,
3944 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3945 *(((uint32_t *) irsp) + 4),
3946 *(((uint32_t *) irsp) + 6),
3947 *(((uint32_t *) irsp) + 7));
3948 }
3949
James Smart7e56aa22012-08-03 12:35:34 -04003950 writel(pring->sli.sli3.rspidx,
3951 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003952
James Smart3772a992009-05-22 14:50:54 -04003953 spin_unlock_irqrestore(&phba->hbalock, iflag);
3954 /* Handle the response IOCB */
3955 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3956 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003957
3958 /*
3959 * If the port response put pointer has not been updated, sync
3960 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3961 * response put pointer.
3962 */
James Smart7e56aa22012-08-03 12:35:34 -04003963 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003964 portRspPut = le32_to_cpu(pgp->rspPutInx);
3965 }
James Smart7e56aa22012-08-03 12:35:34 -04003966 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003967
James Smart92d7f7b2007-06-17 19:56:38 -05003968 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003969 /* At least one response entry has been freed */
3970 pring->stats.iocb_rsp_full++;
3971 /* SET RxRE_RSP in Chip Att register */
3972 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3973 writel(status, phba->CAregaddr);
3974 readl(phba->CAregaddr); /* flush */
3975 }
3976 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3977 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3978 pring->stats.iocb_cmd_empty++;
3979
3980 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003981 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003982 lpfc_sli_resume_iocb(phba, pring);
3983
3984 if ((pring->lpfc_sli_cmd_available))
3985 (pring->lpfc_sli_cmd_available) (phba, pring);
3986
3987 }
3988
James Smart2e0fef82007-06-17 19:56:36 -05003989 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003990 return;
dea31012005-04-17 16:05:31 -05003991}
3992
James Smarte59058c2008-08-24 21:49:00 -04003993/**
James Smart4f774512009-05-22 14:52:35 -04003994 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3995 * @phba: Pointer to HBA context object.
3996 * @pring: Pointer to driver SLI ring object.
3997 * @mask: Host attention register mask for this ring.
3998 *
3999 * This function is called from the worker thread when there is a pending
4000 * ELS response iocb on the driver internal slow-path response iocb worker
4001 * queue. The caller does not hold any lock. The function will remove each
4002 * response iocb from the response worker queue and calls the handle
4003 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4004 **/
4005static void
4006lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
4007 struct lpfc_sli_ring *pring, uint32_t mask)
4008{
4009 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04004010 struct hbq_dmabuf *dmabuf;
4011 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04004012 unsigned long iflag;
James Smart0ef01a22018-09-10 10:30:45 -07004013 int count = 0;
James Smart4f774512009-05-22 14:52:35 -04004014
James Smart45ed1192009-10-02 15:17:02 -04004015 spin_lock_irqsave(&phba->hbalock, iflag);
4016 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
4017 spin_unlock_irqrestore(&phba->hbalock, iflag);
4018 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04004019 /* Get the response iocb from the head of work queue */
4020 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04004021 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04004022 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04004023 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04004024
4025 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
4026 case CQE_CODE_COMPL_WQE:
4027 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
4028 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04004029 /* Translate ELS WCQE to response IOCBQ */
4030 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
4031 irspiocbq);
4032 if (irspiocbq)
4033 lpfc_sli_sp_handle_rspiocb(phba, pring,
4034 irspiocbq);
James Smart0ef01a22018-09-10 10:30:45 -07004035 count++;
James Smart4d9ab992009-10-02 15:16:39 -04004036 break;
4037 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04004038 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04004039 dmabuf = container_of(cq_event, struct hbq_dmabuf,
4040 cq_event);
4041 lpfc_sli4_handle_received_buffer(phba, dmabuf);
James Smart0ef01a22018-09-10 10:30:45 -07004042 count++;
James Smart4d9ab992009-10-02 15:16:39 -04004043 break;
4044 default:
4045 break;
4046 }
James Smart0ef01a22018-09-10 10:30:45 -07004047
4048 /* Limit the number of events to 64 to avoid soft lockups */
4049 if (count == 64)
4050 break;
James Smart4f774512009-05-22 14:52:35 -04004051 }
4052}
4053
4054/**
James Smart3621a712009-04-06 18:47:14 -04004055 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04004056 * @phba: Pointer to HBA context object.
4057 * @pring: Pointer to driver SLI ring object.
4058 *
4059 * This function aborts all iocbs in the given ring and frees all the iocb
4060 * objects in txq. This function issues an abort iocb for all the iocb commands
4061 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4062 * the return of this function. The caller is not required to hold any locks.
4063 **/
James Smart2e0fef82007-06-17 19:56:36 -05004064void
dea31012005-04-17 16:05:31 -05004065lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
4066{
James Smart2534ba72007-04-25 09:52:20 -04004067 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004068 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05004069
James Smart92d7f7b2007-06-17 19:56:38 -05004070 if (pring->ringno == LPFC_ELS_RING) {
4071 lpfc_fabric_abort_hba(phba);
4072 }
4073
dea31012005-04-17 16:05:31 -05004074 /* Error everything on txq and txcmplq
4075 * First do the txq.
4076 */
James Smartdb55fba2014-04-04 13:52:02 -04004077 if (phba->sli_rev >= LPFC_SLI_REV4) {
4078 spin_lock_irq(&pring->ring_lock);
4079 list_splice_init(&pring->txq, &completions);
4080 pring->txq_cnt = 0;
4081 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05004082
James Smartdb55fba2014-04-04 13:52:02 -04004083 spin_lock_irq(&phba->hbalock);
4084 /* Next issue ABTS for everything on the txcmplq */
4085 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
4086 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
4087 spin_unlock_irq(&phba->hbalock);
4088 } else {
4089 spin_lock_irq(&phba->hbalock);
4090 list_splice_init(&pring->txq, &completions);
4091 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04004092
James Smartdb55fba2014-04-04 13:52:02 -04004093 /* Next issue ABTS for everything on the txcmplq */
4094 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
4095 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
4096 spin_unlock_irq(&phba->hbalock);
4097 }
James Smart2534ba72007-04-25 09:52:20 -04004098
James Smarta257bf92009-04-06 18:48:10 -04004099 /* Cancel all the IOCBs from the completions list */
4100 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4101 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004102}
4103
James Smarte59058c2008-08-24 21:49:00 -04004104/**
James Smartdb55fba2014-04-04 13:52:02 -04004105 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
4106 * @phba: Pointer to HBA context object.
4107 * @pring: Pointer to driver SLI ring object.
4108 *
4109 * This function aborts all iocbs in FCP rings and frees all the iocb
4110 * objects in txq. This function issues an abort iocb for all the iocb commands
4111 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4112 * the return of this function. The caller is not required to hold any locks.
4113 **/
4114void
4115lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
4116{
4117 struct lpfc_sli *psli = &phba->sli;
4118 struct lpfc_sli_ring *pring;
4119 uint32_t i;
4120
4121 /* Look on all the FCP Rings for the iotag */
4122 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004123 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07004124 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smartdb55fba2014-04-04 13:52:02 -04004125 lpfc_sli_abort_iocb_ring(phba, pring);
4126 }
4127 } else {
James Smart895427b2017-02-12 13:52:30 -08004128 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004129 lpfc_sli_abort_iocb_ring(phba, pring);
4130 }
4131}
4132
James Smart895427b2017-02-12 13:52:30 -08004133/**
James Smartc00f62e2019-08-14 16:57:11 -07004134 * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
James Smarta8e497d2008-08-24 21:50:11 -04004135 * @phba: Pointer to HBA context object.
4136 *
James Smartc00f62e2019-08-14 16:57:11 -07004137 * This function flushes all iocbs in the IO ring and frees all the iocb
James Smarta8e497d2008-08-24 21:50:11 -04004138 * objects in txq and txcmplq. This function will not issue abort iocbs
4139 * for all the iocb commands in txcmplq, they will just be returned with
4140 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4141 * slot has been permanently disabled.
4142 **/
4143void
James Smartc00f62e2019-08-14 16:57:11 -07004144lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
James Smarta8e497d2008-08-24 21:50:11 -04004145{
4146 LIST_HEAD(txq);
4147 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04004148 struct lpfc_sli *psli = &phba->sli;
4149 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04004150 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004151 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04004152
4153 spin_lock_irq(&phba->hbalock);
James Smart4cd70892020-03-22 11:12:57 -07004154 if (phba->hba_flag & HBA_IOQ_FLUSH ||
4155 !phba->sli4_hba.hdwq) {
4156 spin_unlock_irq(&phba->hbalock);
4157 return;
4158 }
James Smart4f2e66c2012-05-09 21:17:07 -04004159 /* Indicate the I/O queues are flushed */
James Smartc00f62e2019-08-14 16:57:11 -07004160 phba->hba_flag |= HBA_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04004161 spin_unlock_irq(&phba->hbalock);
4162
James Smartdb55fba2014-04-04 13:52:02 -04004163 /* Look on all the FCP Rings for the iotag */
4164 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004165 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07004166 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smarta8e497d2008-08-24 21:50:11 -04004167
James Smartdb55fba2014-04-04 13:52:02 -04004168 spin_lock_irq(&pring->ring_lock);
4169 /* Retrieve everything on txq */
4170 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004171 list_for_each_entry_safe(piocb, next_iocb,
4172 &pring->txcmplq, list)
4173 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004174 /* Retrieve everything on the txcmplq */
4175 list_splice_init(&pring->txcmplq, &txcmplq);
4176 pring->txq_cnt = 0;
4177 pring->txcmplq_cnt = 0;
4178 spin_unlock_irq(&pring->ring_lock);
4179
4180 /* Flush the txq */
4181 lpfc_sli_cancel_iocbs(phba, &txq,
4182 IOSTAT_LOCAL_REJECT,
4183 IOERR_SLI_DOWN);
4184 /* Flush the txcmpq */
4185 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4186 IOSTAT_LOCAL_REJECT,
4187 IOERR_SLI_DOWN);
4188 }
4189 } else {
James Smart895427b2017-02-12 13:52:30 -08004190 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004191
4192 spin_lock_irq(&phba->hbalock);
4193 /* Retrieve everything on txq */
4194 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004195 list_for_each_entry_safe(piocb, next_iocb,
4196 &pring->txcmplq, list)
4197 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004198 /* Retrieve everything on the txcmplq */
4199 list_splice_init(&pring->txcmplq, &txcmplq);
4200 pring->txq_cnt = 0;
4201 pring->txcmplq_cnt = 0;
4202 spin_unlock_irq(&phba->hbalock);
4203
4204 /* Flush the txq */
4205 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4206 IOERR_SLI_DOWN);
4207 /* Flush the txcmpq */
4208 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4209 IOERR_SLI_DOWN);
4210 }
James Smarta8e497d2008-08-24 21:50:11 -04004211}
4212
4213/**
James Smart3772a992009-05-22 14:50:54 -04004214 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004215 * @phba: Pointer to HBA context object.
4216 * @mask: Bit mask to be checked.
4217 *
4218 * This function reads the host status register and compares
4219 * with the provided bit mask to check if HBA completed
4220 * the restart. This function will wait in a loop for the
4221 * HBA to complete restart. If the HBA does not restart within
4222 * 15 iterations, the function will reset the HBA again. The
4223 * function returns 1 when HBA fail to restart otherwise returns
4224 * zero.
4225 **/
James Smart3772a992009-05-22 14:50:54 -04004226static int
4227lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004228{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004229 uint32_t status;
4230 int i = 0;
4231 int retval = 0;
dea31012005-04-17 16:05:31 -05004232
Jamie Wellnitz41415862006-02-28 19:25:27 -05004233 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004234 if (lpfc_readl(phba->HSregaddr, &status))
4235 return 1;
dea31012005-04-17 16:05:31 -05004236
Jamie Wellnitz41415862006-02-28 19:25:27 -05004237 /*
4238 * Check status register every 100ms for 5 retries, then every
4239 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4240 * every 2.5 sec for 4.
4241 * Break our of the loop if errors occurred during init.
4242 */
4243 while (((status & mask) != mask) &&
4244 !(status & HS_FFERM) &&
4245 i++ < 20) {
dea31012005-04-17 16:05:31 -05004246
Jamie Wellnitz41415862006-02-28 19:25:27 -05004247 if (i <= 5)
4248 msleep(10);
4249 else if (i <= 10)
4250 msleep(500);
4251 else
4252 msleep(2500);
dea31012005-04-17 16:05:31 -05004253
Jamie Wellnitz41415862006-02-28 19:25:27 -05004254 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004255 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004256 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004257 lpfc_sli_brdrestart(phba);
4258 }
4259 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004260 if (lpfc_readl(phba->HSregaddr, &status)) {
4261 retval = 1;
4262 break;
4263 }
dea31012005-04-17 16:05:31 -05004264 }
dea31012005-04-17 16:05:31 -05004265
Jamie Wellnitz41415862006-02-28 19:25:27 -05004266 /* Check to see if any errors occurred during init */
4267 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004268 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4269 "2751 Adapter failed to restart, "
4270 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4271 status,
4272 readl(phba->MBslimaddr + 0xa8),
4273 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004274 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004275 retval = 1;
4276 }
dea31012005-04-17 16:05:31 -05004277
Jamie Wellnitz41415862006-02-28 19:25:27 -05004278 return retval;
dea31012005-04-17 16:05:31 -05004279}
4280
James Smartda0436e2009-05-22 14:51:39 -04004281/**
4282 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4283 * @phba: Pointer to HBA context object.
4284 * @mask: Bit mask to be checked.
4285 *
4286 * This function checks the host status register to check if HBA is
4287 * ready. This function will wait in a loop for the HBA to be ready
4288 * If the HBA is not ready , the function will will reset the HBA PCI
4289 * function again. The function returns 1 when HBA fail to be ready
4290 * otherwise returns zero.
4291 **/
4292static int
4293lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4294{
4295 uint32_t status;
4296 int retval = 0;
4297
4298 /* Read the HBA Host Status Register */
4299 status = lpfc_sli4_post_status_check(phba);
4300
4301 if (status) {
4302 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4303 lpfc_sli_brdrestart(phba);
4304 status = lpfc_sli4_post_status_check(phba);
4305 }
4306
4307 /* Check to see if any errors occurred during init */
4308 if (status) {
4309 phba->link_state = LPFC_HBA_ERROR;
4310 retval = 1;
4311 } else
4312 phba->sli4_hba.intr_enable = 0;
4313
4314 return retval;
4315}
4316
4317/**
4318 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4319 * @phba: Pointer to HBA context object.
4320 * @mask: Bit mask to be checked.
4321 *
4322 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4323 * from the API jump table function pointer from the lpfc_hba struct.
4324 **/
4325int
4326lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4327{
4328 return phba->lpfc_sli_brdready(phba, mask);
4329}
4330
James Smart92908312006-03-07 15:04:13 -05004331#define BARRIER_TEST_PATTERN (0xdeadbeef)
4332
James Smarte59058c2008-08-24 21:49:00 -04004333/**
James Smart3621a712009-04-06 18:47:14 -04004334 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004335 * @phba: Pointer to HBA context object.
4336 *
James Smart1b511972011-12-13 13:23:09 -05004337 * This function is called before resetting an HBA. This function is called
4338 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004339 **/
James Smart2e0fef82007-06-17 19:56:36 -05004340void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004341{
James Smart65a29c12006-07-06 15:50:50 -04004342 uint32_t __iomem *resp_buf;
4343 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004344 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004345 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004346 int i;
4347 uint8_t hdrtype;
4348
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004349 lockdep_assert_held(&phba->hbalock);
4350
James Smart92908312006-03-07 15:04:13 -05004351 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4352 if (hdrtype != 0x80 ||
4353 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4354 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4355 return;
4356
4357 /*
4358 * Tell the other part of the chip to suspend temporarily all
4359 * its DMA activity.
4360 */
James Smart65a29c12006-07-06 15:50:50 -04004361 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004362
4363 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004364 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4365 return;
James Smart92908312006-03-07 15:04:13 -05004366 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4367 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004368 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004369
James Smart9940b972011-03-11 16:06:12 -05004370 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4371 return;
4372 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004373 /* Clear Chip error bit */
4374 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004375 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004376 }
4377
4378 mbox = 0;
4379 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4380 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4381
4382 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004383 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004384 writel(mbox, mbox_buf);
4385
James Smart9940b972011-03-11 16:06:12 -05004386 for (i = 0; i < 50; i++) {
4387 if (lpfc_readl((resp_buf + 1), &resp_data))
4388 return;
4389 if (resp_data != ~(BARRIER_TEST_PATTERN))
4390 mdelay(1);
4391 else
4392 break;
4393 }
4394 resp_data = 0;
4395 if (lpfc_readl((resp_buf + 1), &resp_data))
4396 return;
4397 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004398 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004399 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004400 goto restore_hc;
4401 else
4402 goto clear_errat;
4403 }
4404
4405 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004406 resp_data = 0;
4407 for (i = 0; i < 500; i++) {
4408 if (lpfc_readl(resp_buf, &resp_data))
4409 return;
4410 if (resp_data != mbox)
4411 mdelay(1);
4412 else
4413 break;
4414 }
James Smart92908312006-03-07 15:04:13 -05004415
4416clear_errat:
4417
James Smart9940b972011-03-11 16:06:12 -05004418 while (++i < 500) {
4419 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4420 return;
4421 if (!(ha_copy & HA_ERATT))
4422 mdelay(1);
4423 else
4424 break;
4425 }
James Smart92908312006-03-07 15:04:13 -05004426
4427 if (readl(phba->HAregaddr) & HA_ERATT) {
4428 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004429 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004430 }
4431
4432restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004433 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004434 writel(hc_copy, phba->HCregaddr);
4435 readl(phba->HCregaddr); /* flush */
4436}
4437
James Smarte59058c2008-08-24 21:49:00 -04004438/**
James Smart3621a712009-04-06 18:47:14 -04004439 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004440 * @phba: Pointer to HBA context object.
4441 *
4442 * This function issues a kill_board mailbox command and waits for
4443 * the error attention interrupt. This function is called for stopping
4444 * the firmware processing. The caller is not required to hold any
4445 * locks. This function calls lpfc_hba_down_post function to free
4446 * any pending commands after the kill. The function will return 1 when it
4447 * fails to kill the board else will return 0.
4448 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004449int
James Smart2e0fef82007-06-17 19:56:36 -05004450lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004451{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004452 struct lpfc_sli *psli;
4453 LPFC_MBOXQ_t *pmb;
4454 uint32_t status;
4455 uint32_t ha_copy;
4456 int retval;
4457 int i = 0;
4458
4459 psli = &phba->sli;
4460
4461 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004462 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004463 "0329 Kill HBA Data: x%x x%x\n",
4464 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004465
James Smart98c9ea52007-10-27 13:37:33 -04004466 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4467 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004468 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004469
4470 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004471 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004472 if (lpfc_readl(phba->HCregaddr, &status)) {
4473 spin_unlock_irq(&phba->hbalock);
4474 mempool_free(pmb, phba->mbox_mem_pool);
4475 return 1;
4476 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004477 status &= ~HC_ERINT_ENA;
4478 writel(status, phba->HCregaddr);
4479 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004480 phba->link_flag |= LS_IGNORE_ERATT;
4481 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004482
4483 lpfc_kill_board(phba, pmb);
4484 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4485 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4486
4487 if (retval != MBX_SUCCESS) {
4488 if (retval != MBX_BUSY)
4489 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004490 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4491 "2752 KILL_BOARD command failed retval %d\n",
4492 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004493 spin_lock_irq(&phba->hbalock);
4494 phba->link_flag &= ~LS_IGNORE_ERATT;
4495 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004496 return 1;
4497 }
4498
James Smartf4b4c682009-05-22 14:53:12 -04004499 spin_lock_irq(&phba->hbalock);
4500 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4501 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004502
Jamie Wellnitz41415862006-02-28 19:25:27 -05004503 mempool_free(pmb, phba->mbox_mem_pool);
4504
4505 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4506 * attention every 100ms for 3 seconds. If we don't get ERATT after
4507 * 3 seconds we still set HBA_ERROR state because the status of the
4508 * board is now undefined.
4509 */
James Smart9940b972011-03-11 16:06:12 -05004510 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4511 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004512 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4513 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004514 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4515 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004516 }
4517
4518 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004519 if (ha_copy & HA_ERATT) {
4520 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004521 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004522 }
James Smart2e0fef82007-06-17 19:56:36 -05004523 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004524 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004525 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004526 phba->link_flag &= ~LS_IGNORE_ERATT;
4527 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004528
Jamie Wellnitz41415862006-02-28 19:25:27 -05004529 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004530 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004531
James Smart2e0fef82007-06-17 19:56:36 -05004532 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004533}
4534
James Smarte59058c2008-08-24 21:49:00 -04004535/**
James Smart3772a992009-05-22 14:50:54 -04004536 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004537 * @phba: Pointer to HBA context object.
4538 *
4539 * This function resets the HBA by writing HC_INITFF to the control
4540 * register. After the HBA resets, this function resets all the iocb ring
4541 * indices. This function disables PCI layer parity checking during
4542 * the reset.
4543 * This function returns 0 always.
4544 * The caller is not required to hold any locks.
4545 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004546int
James Smart2e0fef82007-06-17 19:56:36 -05004547lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004548{
4549 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004550 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004551 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004552 int i;
dea31012005-04-17 16:05:31 -05004553
Jamie Wellnitz41415862006-02-28 19:25:27 -05004554 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004555
Jamie Wellnitz41415862006-02-28 19:25:27 -05004556 /* Reset HBA */
4557 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004558 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004559 (phba->pport) ? phba->pport->port_state : 0,
4560 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004561
4562 /* perform board reset */
4563 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004564 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004565 if (phba->pport) {
4566 phba->pport->fc_myDID = 0;
4567 phba->pport->fc_prevDID = 0;
4568 }
dea31012005-04-17 16:05:31 -05004569
Jamie Wellnitz41415862006-02-28 19:25:27 -05004570 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004571 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
4572 return -EIO;
4573
Jamie Wellnitz41415862006-02-28 19:25:27 -05004574 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4575 (cfg_value &
4576 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4577
James Smart3772a992009-05-22 14:50:54 -04004578 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4579
Jamie Wellnitz41415862006-02-28 19:25:27 -05004580 /* Now toggle INITFF bit in the Host Control Register */
4581 writel(HC_INITFF, phba->HCregaddr);
4582 mdelay(1);
4583 readl(phba->HCregaddr); /* flush */
4584 writel(0, phba->HCregaddr);
4585 readl(phba->HCregaddr); /* flush */
4586
4587 /* Restore PCI cmd register */
4588 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004589
4590 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004591 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004592 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004593 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004594 pring->sli.sli3.rspidx = 0;
4595 pring->sli.sli3.next_cmdidx = 0;
4596 pring->sli.sli3.local_getidx = 0;
4597 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004598 pring->missbufcnt = 0;
4599 }
dea31012005-04-17 16:05:31 -05004600
James Smart2e0fef82007-06-17 19:56:36 -05004601 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004602 return 0;
4603}
4604
James Smarte59058c2008-08-24 21:49:00 -04004605/**
James Smartda0436e2009-05-22 14:51:39 -04004606 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4607 * @phba: Pointer to HBA context object.
4608 *
4609 * This function resets a SLI4 HBA. This function disables PCI layer parity
4610 * checking during resets the device. The caller is not required to hold
4611 * any locks.
4612 *
James Smart8c24a4f2019-08-14 16:56:53 -07004613 * This function returns 0 on success else returns negative error code.
James Smartda0436e2009-05-22 14:51:39 -04004614 **/
4615int
4616lpfc_sli4_brdreset(struct lpfc_hba *phba)
4617{
4618 struct lpfc_sli *psli = &phba->sli;
4619 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004620 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004621
4622 /* Reset HBA */
4623 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004624 "0295 Reset HBA Data: x%x x%x x%x\n",
4625 phba->pport->port_state, psli->sli_flag,
4626 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004627
4628 /* perform board reset */
4629 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004630 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004631 phba->pport->fc_myDID = 0;
4632 phba->pport->fc_prevDID = 0;
4633
James Smartda0436e2009-05-22 14:51:39 -04004634 spin_lock_irq(&phba->hbalock);
4635 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4636 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004637 spin_unlock_irq(&phba->hbalock);
4638
James Smart02936352014-04-04 13:52:12 -04004639 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4640 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4641 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4642 return rc;
4643 }
4644
James Smartda0436e2009-05-22 14:51:39 -04004645 /* Now physically reset the device */
4646 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4647 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004648
4649 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004650 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
4651 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4652 "3205 PCI read Config failed\n");
4653 return -EIO;
4654 }
4655
James Smartbe858b62010-12-15 17:57:20 -05004656 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4657 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4658
James Smart88318812012-09-29 11:29:29 -04004659 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004660 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004661
James Smartbe858b62010-12-15 17:57:20 -05004662 /* Restore PCI cmd register */
4663 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4664
James Smart27b01b82012-05-09 21:19:44 -04004665 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004666}
4667
4668/**
4669 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004670 * @phba: Pointer to HBA context object.
4671 *
4672 * This function is called in the SLI initialization code path to
4673 * restart the HBA. The caller is not required to hold any lock.
4674 * This function writes MBX_RESTART mailbox command to the SLIM and
4675 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4676 * function to free any pending commands. The function enables
4677 * POST only during the first initialization. The function returns zero.
4678 * The function does not guarantee completion of MBX_RESTART mailbox
4679 * command before the return of this function.
4680 **/
James Smartda0436e2009-05-22 14:51:39 -04004681static int
4682lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004683{
4684 MAILBOX_t *mb;
4685 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004686 volatile uint32_t word0;
4687 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004688 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004689
James Smart2e0fef82007-06-17 19:56:36 -05004690 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004691
James Smart0d878412009-10-02 15:16:56 -04004692 /* Take PCIe device Advanced Error Reporting (AER) state */
4693 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4694
Jamie Wellnitz41415862006-02-28 19:25:27 -05004695 psli = &phba->sli;
4696
4697 /* Restart HBA */
4698 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004699 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004700 (phba->pport) ? phba->pport->port_state : 0,
4701 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004702
4703 word0 = 0;
4704 mb = (MAILBOX_t *) &word0;
4705 mb->mbxCommand = MBX_RESTART;
4706 mb->mbxHc = 1;
4707
James Smart92908312006-03-07 15:04:13 -05004708 lpfc_reset_barrier(phba);
4709
Jamie Wellnitz41415862006-02-28 19:25:27 -05004710 to_slim = phba->MBslimaddr;
4711 writel(*(uint32_t *) mb, to_slim);
4712 readl(to_slim); /* flush */
4713
4714 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004715 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004716 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004717 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004718 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004719 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004720 writel(*(uint32_t *) mb, to_slim);
4721 readl(to_slim); /* flush */
4722
4723 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004724 if (phba->pport)
4725 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004726 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004727 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004728 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004729
James Smart64ba8812006-08-02 15:24:34 -04004730 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004731 psli->stats_start = ktime_get_seconds();
James Smart64ba8812006-08-02 15:24:34 -04004732
James Smarteaf15d52008-12-04 22:39:29 -05004733 /* Give the INITFF and Post time to settle. */
4734 mdelay(100);
dea31012005-04-17 16:05:31 -05004735
James Smart0d878412009-10-02 15:16:56 -04004736 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4737 if (hba_aer_enabled)
4738 pci_disable_pcie_error_reporting(phba->pcidev);
4739
Jamie Wellnitz41415862006-02-28 19:25:27 -05004740 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004741
4742 return 0;
4743}
4744
James Smarte59058c2008-08-24 21:49:00 -04004745/**
James Smartda0436e2009-05-22 14:51:39 -04004746 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4747 * @phba: Pointer to HBA context object.
4748 *
4749 * This function is called in the SLI initialization code path to restart
4750 * a SLI4 HBA. The caller is not required to hold any lock.
4751 * At the end of the function, it calls lpfc_hba_down_post function to
4752 * free any pending commands.
4753 **/
4754static int
4755lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4756{
4757 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004758 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004759 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004760
4761 /* Restart HBA */
4762 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4763 "0296 Restart HBA Data: x%x x%x\n",
4764 phba->pport->port_state, psli->sli_flag);
4765
James Smart75baf692010-06-08 18:31:21 -04004766 /* Take PCIe device Advanced Error Reporting (AER) state */
4767 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4768
James Smart27b01b82012-05-09 21:19:44 -04004769 rc = lpfc_sli4_brdreset(phba);
James Smart4fb86a62019-09-03 14:54:41 -07004770 if (rc) {
4771 phba->link_state = LPFC_HBA_ERROR;
4772 goto hba_down_queue;
4773 }
James Smartda0436e2009-05-22 14:51:39 -04004774
4775 spin_lock_irq(&phba->hbalock);
4776 phba->pport->stopped = 0;
4777 phba->link_state = LPFC_INIT_START;
4778 phba->hba_flag = 0;
4779 spin_unlock_irq(&phba->hbalock);
4780
4781 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004782 psli->stats_start = ktime_get_seconds();
James Smartda0436e2009-05-22 14:51:39 -04004783
James Smart75baf692010-06-08 18:31:21 -04004784 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4785 if (hba_aer_enabled)
4786 pci_disable_pcie_error_reporting(phba->pcidev);
4787
James Smart4fb86a62019-09-03 14:54:41 -07004788hba_down_queue:
James Smartda0436e2009-05-22 14:51:39 -04004789 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004790 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004791
James Smart27b01b82012-05-09 21:19:44 -04004792 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004793}
4794
4795/**
4796 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4797 * @phba: Pointer to HBA context object.
4798 *
4799 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4800 * API jump table function pointer from the lpfc_hba struct.
4801**/
4802int
4803lpfc_sli_brdrestart(struct lpfc_hba *phba)
4804{
4805 return phba->lpfc_sli_brdrestart(phba);
4806}
4807
4808/**
James Smart3621a712009-04-06 18:47:14 -04004809 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004810 * @phba: Pointer to HBA context object.
4811 *
4812 * This function is called after a HBA restart to wait for successful
4813 * restart of the HBA. Successful restart of the HBA is indicated by
4814 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4815 * iteration, the function will restart the HBA again. The function returns
4816 * zero if HBA successfully restarted else returns negative error code.
4817 **/
James Smart4492b732017-04-27 15:08:26 -07004818int
dea31012005-04-17 16:05:31 -05004819lpfc_sli_chipset_init(struct lpfc_hba *phba)
4820{
4821 uint32_t status, i = 0;
4822
4823 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004824 if (lpfc_readl(phba->HSregaddr, &status))
4825 return -EIO;
dea31012005-04-17 16:05:31 -05004826
4827 /* Check status register to see what current state is */
4828 i = 0;
4829 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4830
James Smartdcf2a4e2010-09-29 11:18:53 -04004831 /* Check every 10ms for 10 retries, then every 100ms for 90
4832 * retries, then every 1 sec for 50 retires for a total of
4833 * ~60 seconds before reset the board again and check every
4834 * 1 sec for 50 retries. The up to 60 seconds before the
4835 * board ready is required by the Falcon FIPS zeroization
4836 * complete, and any reset the board in between shall cause
4837 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004838 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004839 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004840 /* Adapter failed to init, timeout, status reg
4841 <status> */
James Smarted957682007-06-17 19:56:37 -05004842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004843 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004844 "timeout, status reg x%x, "
4845 "FW Data: A8 x%x AC x%x\n", status,
4846 readl(phba->MBslimaddr + 0xa8),
4847 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004848 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004849 return -ETIMEDOUT;
4850 }
4851
4852 /* Check to see if any errors occurred during init */
4853 if (status & HS_FFERM) {
4854 /* ERROR: During chipset initialization */
4855 /* Adapter failed to init, chipset, status reg
4856 <status> */
James Smarted957682007-06-17 19:56:37 -05004857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004858 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004859 "chipset, status reg x%x, "
4860 "FW Data: A8 x%x AC x%x\n", status,
4861 readl(phba->MBslimaddr + 0xa8),
4862 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004863 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004864 return -EIO;
4865 }
4866
James Smartdcf2a4e2010-09-29 11:18:53 -04004867 if (i <= 10)
dea31012005-04-17 16:05:31 -05004868 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004869 else if (i <= 100)
4870 msleep(100);
4871 else
4872 msleep(1000);
dea31012005-04-17 16:05:31 -05004873
James Smartdcf2a4e2010-09-29 11:18:53 -04004874 if (i == 150) {
4875 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004876 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004877 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004878 }
4879 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004880 if (lpfc_readl(phba->HSregaddr, &status))
4881 return -EIO;
dea31012005-04-17 16:05:31 -05004882 }
4883
4884 /* Check to see if any errors occurred during init */
4885 if (status & HS_FFERM) {
4886 /* ERROR: During chipset initialization */
4887 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004888 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004889 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004890 "status reg x%x, "
4891 "FW Data: A8 x%x AC x%x\n", status,
4892 readl(phba->MBslimaddr + 0xa8),
4893 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004894 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004895 return -EIO;
4896 }
4897
4898 /* Clear all interrupt enable conditions */
4899 writel(0, phba->HCregaddr);
4900 readl(phba->HCregaddr); /* flush */
4901
4902 /* setup host attn register */
4903 writel(0xffffffff, phba->HAregaddr);
4904 readl(phba->HAregaddr); /* flush */
4905 return 0;
4906}
4907
James Smarte59058c2008-08-24 21:49:00 -04004908/**
James Smart3621a712009-04-06 18:47:14 -04004909 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004910 *
4911 * This function calculates and returns the number of HBQs required to be
4912 * configured.
4913 **/
James Smart78b2d852007-08-02 11:10:21 -04004914int
James Smarted957682007-06-17 19:56:37 -05004915lpfc_sli_hbq_count(void)
4916{
James Smart92d7f7b2007-06-17 19:56:38 -05004917 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004918}
4919
James Smarte59058c2008-08-24 21:49:00 -04004920/**
James Smart3621a712009-04-06 18:47:14 -04004921 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004922 *
4923 * This function adds the number of hbq entries in every HBQ to get
4924 * the total number of hbq entries required for the HBA and returns
4925 * the total count.
4926 **/
James Smarted957682007-06-17 19:56:37 -05004927static int
4928lpfc_sli_hbq_entry_count(void)
4929{
4930 int hbq_count = lpfc_sli_hbq_count();
4931 int count = 0;
4932 int i;
4933
4934 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004935 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004936 return count;
4937}
4938
James Smarte59058c2008-08-24 21:49:00 -04004939/**
James Smart3621a712009-04-06 18:47:14 -04004940 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004941 *
4942 * This function calculates amount of memory required for all hbq entries
4943 * to be configured and returns the total memory required.
4944 **/
dea31012005-04-17 16:05:31 -05004945int
James Smarted957682007-06-17 19:56:37 -05004946lpfc_sli_hbq_size(void)
4947{
4948 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4949}
4950
James Smarte59058c2008-08-24 21:49:00 -04004951/**
James Smart3621a712009-04-06 18:47:14 -04004952 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004953 * @phba: Pointer to HBA context object.
4954 *
4955 * This function is called during the SLI initialization to configure
4956 * all the HBQs and post buffers to the HBQ. The caller is not
4957 * required to hold any locks. This function will return zero if successful
4958 * else it will return negative error code.
4959 **/
James Smarted957682007-06-17 19:56:37 -05004960static int
4961lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4962{
4963 int hbq_count = lpfc_sli_hbq_count();
4964 LPFC_MBOXQ_t *pmb;
4965 MAILBOX_t *pmbox;
4966 uint32_t hbqno;
4967 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004968
James Smart92d7f7b2007-06-17 19:56:38 -05004969 /* Get a Mailbox buffer to setup mailbox
4970 * commands for HBA initialization
4971 */
James Smarted957682007-06-17 19:56:37 -05004972 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4973
4974 if (!pmb)
4975 return -ENOMEM;
4976
James Smart04c68492009-05-22 14:52:52 -04004977 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004978
4979 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4980 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004981 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004982
4983 hbq_entry_index = 0;
4984 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4985 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4986 phba->hbqs[hbqno].hbqPutIdx = 0;
4987 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4988 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004989 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004990 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4991 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004992 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4993
4994 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4995 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4996 mbxStatus <status>, ring <num> */
4997
4998 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004999 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005000 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05005001 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005002 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05005003 pmbox->mbxStatus, hbqno);
5004
5005 phba->link_state = LPFC_HBA_ERROR;
5006 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04005007 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05005008 }
5009 }
5010 phba->hbq_count = hbq_count;
5011
James Smarted957682007-06-17 19:56:37 -05005012 mempool_free(pmb, phba->mbox_mem_pool);
5013
James Smart92d7f7b2007-06-17 19:56:38 -05005014 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04005015 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
5016 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05005017 return 0;
5018}
5019
James Smarte59058c2008-08-24 21:49:00 -04005020/**
James Smart4f774512009-05-22 14:52:35 -04005021 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
5022 * @phba: Pointer to HBA context object.
5023 *
5024 * This function is called during the SLI initialization to configure
5025 * all the HBQs and post buffers to the HBQ. The caller is not
5026 * required to hold any locks. This function will return zero if successful
5027 * else it will return negative error code.
5028 **/
5029static int
5030lpfc_sli4_rb_setup(struct lpfc_hba *phba)
5031{
5032 phba->hbq_in_use = 1;
James Smart999fbbc2019-12-18 15:58:06 -08005033 /**
5034 * Specific case when the MDS diagnostics is enabled and supported.
5035 * The receive buffer count is truncated to manage the incoming
5036 * traffic.
5037 **/
5038 if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
5039 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5040 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
5041 else
5042 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5043 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04005044 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08005045 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04005046 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04005047 return 0;
5048}
5049
5050/**
James Smart3621a712009-04-06 18:47:14 -04005051 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04005052 * @phba: Pointer to HBA context object.
5053 * @sli_mode: sli mode - 2/3
5054 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005055 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04005056 * to issue config_port mailbox command. This function restarts the
5057 * HBA firmware and issues a config_port mailbox command to configure
5058 * the SLI interface in the sli mode specified by sli_mode
5059 * variable. The caller is not required to hold any locks.
5060 * The function returns 0 if successful, else returns negative error
5061 * code.
5062 **/
James Smart93996272008-08-24 21:50:30 -04005063int
5064lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05005065{
5066 LPFC_MBOXQ_t *pmb;
5067 uint32_t resetcount = 0, rc = 0, done = 0;
5068
5069 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5070 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05005071 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005072 return -ENOMEM;
5073 }
5074
James Smarted957682007-06-17 19:56:37 -05005075 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05005076 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05005077 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04005078 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005079 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05005080 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005081 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05005082 rc = lpfc_sli_chipset_init(phba);
5083 if (rc)
5084 break;
5085
James Smart2e0fef82007-06-17 19:56:36 -05005086 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04005087 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005088 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005089 resetcount++;
5090
James Smarted957682007-06-17 19:56:37 -05005091 /* Call pre CONFIG_PORT mailbox command initialization. A
5092 * value of 0 means the call was successful. Any other
5093 * nonzero value is a failure, but if ERESTART is returned,
5094 * the driver may reset the HBA and try again.
5095 */
dea31012005-04-17 16:05:31 -05005096 rc = lpfc_config_port_prep(phba);
5097 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05005098 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05005099 continue;
James Smart34b02dc2008-08-24 21:49:55 -04005100 } else if (rc)
dea31012005-04-17 16:05:31 -05005101 break;
James Smart6d368e52011-05-24 11:44:12 -04005102
James Smart2e0fef82007-06-17 19:56:36 -05005103 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05005104 lpfc_config_port(phba, pmb);
5105 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04005106 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5107 LPFC_SLI3_HBQ_ENABLED |
5108 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04005109 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05005110 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05005111 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005112 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05005113 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04005114 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005115 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04005116 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005117 spin_unlock_irq(&phba->hbalock);
5118 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04005119 } else {
5120 /* Allow asynchronous mailbox command to go through */
5121 spin_lock_irq(&phba->hbalock);
5122 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5123 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05005124 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05005125
5126 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5127 (pmb->u.mb.un.varCfgPort.gasabt == 0))
5128 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5129 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04005130 }
dea31012005-04-17 16:05:31 -05005131 }
James Smarted957682007-06-17 19:56:37 -05005132 if (!done) {
5133 rc = -EINVAL;
5134 goto do_prep_failed;
5135 }
James Smart04c68492009-05-22 14:52:52 -04005136 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5137 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04005138 rc = -ENXIO;
5139 goto do_prep_failed;
5140 }
James Smart04c68492009-05-22 14:52:52 -04005141 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04005142 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005143 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5144 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5145 phba->max_vpi : phba->max_vports;
5146
James Smart34b02dc2008-08-24 21:49:55 -04005147 } else
5148 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04005149 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005150 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005151 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005152 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005153
5154 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5155 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005156
James Smartf44ac122018-03-05 12:04:08 -08005157 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5158 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5159 phba->cfg_enable_bg = 0;
5160 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smarte2a0a9d2008-12-04 22:40:02 -05005161 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5162 "0443 Adapter did not grant "
5163 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005164 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005165 }
James Smart34b02dc2008-08-24 21:49:55 -04005166 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005167 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005168 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005169 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005170 }
James Smart92d7f7b2007-06-17 19:56:38 -05005171do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005172 mempool_free(pmb, phba->mbox_mem_pool);
5173 return rc;
5174}
5175
James Smarte59058c2008-08-24 21:49:00 -04005176
5177/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005178 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005179 * @phba: Pointer to HBA context object.
5180 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005181 * This function is the main SLI initialization function. This function
5182 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005183 * error attention handler code. Caller is not required to hold any
5184 * locks. This function issues config_port mailbox command to configure
5185 * the SLI, setup iocb rings and HBQ rings. In the end the function
5186 * calls the config_port_post function to issue init_link mailbox
5187 * command and to start the discovery. The function will return zero
5188 * if successful, else it will return negative error code.
5189 **/
James Smarted957682007-06-17 19:56:37 -05005190int
5191lpfc_sli_hba_setup(struct lpfc_hba *phba)
5192{
5193 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04005194 int mode = 3, i;
5195 int longs;
James Smarted957682007-06-17 19:56:37 -05005196
James Smart12247e82016-07-06 12:36:09 -07005197 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05005198 case 2:
James Smart78b2d852007-08-02 11:10:21 -04005199 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05005200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005201 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05005202 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07005203 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05005204 break;
5205 }
James Smarted957682007-06-17 19:56:37 -05005206 mode = 2;
5207 break;
5208 case 0:
5209 case 3:
5210 break;
5211 default:
James Smart92d7f7b2007-06-17 19:56:38 -05005212 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07005213 "1819 Unrecognized sli_mode parameter: %d.\n",
5214 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05005215
5216 break;
5217 }
James Smartb5c53952016-03-31 14:12:30 -07005218 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005219
James Smart93996272008-08-24 21:50:30 -04005220 rc = lpfc_sli_config_port(phba, mode);
5221
James Smart12247e82016-07-06 12:36:09 -07005222 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005223 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005224 "1820 Unable to select SLI-3. "
5225 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005226 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005227 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005228 else if (rc && mode == 2)
5229 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005230 if (rc)
dea31012005-04-17 16:05:31 -05005231 goto lpfc_sli_hba_setup_error;
5232
James Smart0d878412009-10-02 15:16:56 -04005233 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5234 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5235 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5236 if (!rc) {
5237 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5238 "2709 This device supports "
5239 "Advanced Error Reporting (AER)\n");
5240 spin_lock_irq(&phba->hbalock);
5241 phba->hba_flag |= HBA_AER_ENABLED;
5242 spin_unlock_irq(&phba->hbalock);
5243 } else {
5244 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5245 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005246 "Advanced Error Reporting (AER): %d\n",
5247 rc);
James Smart0d878412009-10-02 15:16:56 -04005248 phba->cfg_aer_support = 0;
5249 }
5250 }
5251
James Smarted957682007-06-17 19:56:37 -05005252 if (phba->sli_rev == 3) {
5253 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5254 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005255 } else {
5256 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5257 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005258 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005259 }
5260
5261 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005262 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5263 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005264 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005265
5266 if (rc)
5267 goto lpfc_sli_hba_setup_error;
5268
James Smart6d368e52011-05-24 11:44:12 -04005269 /* Initialize VPIs. */
5270 if (phba->sli_rev == LPFC_SLI_REV3) {
5271 /*
5272 * The VPI bitmask and physical ID array are allocated
5273 * and initialized once only - at driver load. A port
5274 * reset doesn't need to reinitialize this memory.
5275 */
5276 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5277 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005278 phba->vpi_bmask = kcalloc(longs,
5279 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005280 GFP_KERNEL);
5281 if (!phba->vpi_bmask) {
5282 rc = -ENOMEM;
5283 goto lpfc_sli_hba_setup_error;
5284 }
5285
Kees Cook6396bb22018-06-12 14:03:40 -07005286 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5287 sizeof(uint16_t),
5288 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005289 if (!phba->vpi_ids) {
5290 kfree(phba->vpi_bmask);
5291 rc = -ENOMEM;
5292 goto lpfc_sli_hba_setup_error;
5293 }
5294 for (i = 0; i < phba->max_vpi; i++)
5295 phba->vpi_ids[i] = i;
5296 }
5297 }
5298
James Smart93996272008-08-24 21:50:30 -04005299 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005300 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5301 rc = lpfc_sli_hbq_setup(phba);
5302 if (rc)
5303 goto lpfc_sli_hba_setup_error;
5304 }
James Smart04c68492009-05-22 14:52:52 -04005305 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005306 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005307 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005308
5309 rc = lpfc_config_port_post(phba);
5310 if (rc)
5311 goto lpfc_sli_hba_setup_error;
5312
James Smarted957682007-06-17 19:56:37 -05005313 return rc;
5314
James Smart92d7f7b2007-06-17 19:56:38 -05005315lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005316 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005317 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005318 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005319 return rc;
5320}
5321
James Smartda0436e2009-05-22 14:51:39 -04005322/**
5323 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5324 * @phba: Pointer to HBA context object.
5325 * @mboxq: mailbox pointer.
5326 * This function issue a dump mailbox command to read config region
5327 * 23 and parse the records in the region and populate driver
5328 * data structure.
5329 **/
5330static int
James Smartff78d8f2011-12-13 13:21:35 -05005331lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005332{
James Smartff78d8f2011-12-13 13:21:35 -05005333 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005334 struct lpfc_dmabuf *mp;
5335 struct lpfc_mqe *mqe;
5336 uint32_t data_length;
5337 int rc;
5338
5339 /* Program the default value of vlan_id and fc_map */
5340 phba->valid_vlan = 0;
5341 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5342 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5343 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5344
James Smartff78d8f2011-12-13 13:21:35 -05005345 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5346 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005347 return -ENOMEM;
5348
James Smartff78d8f2011-12-13 13:21:35 -05005349 mqe = &mboxq->u.mqe;
5350 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5351 rc = -ENOMEM;
5352 goto out_free_mboxq;
5353 }
5354
James Smart3e1f0712018-11-29 16:09:29 -08005355 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04005356 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5357
5358 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5359 "(%d):2571 Mailbox cmd x%x Status x%x "
5360 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5361 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5362 "CQ: x%x x%x x%x x%x\n",
5363 mboxq->vport ? mboxq->vport->vpi : 0,
5364 bf_get(lpfc_mqe_command, mqe),
5365 bf_get(lpfc_mqe_status, mqe),
5366 mqe->un.mb_words[0], mqe->un.mb_words[1],
5367 mqe->un.mb_words[2], mqe->un.mb_words[3],
5368 mqe->un.mb_words[4], mqe->un.mb_words[5],
5369 mqe->un.mb_words[6], mqe->un.mb_words[7],
5370 mqe->un.mb_words[8], mqe->un.mb_words[9],
5371 mqe->un.mb_words[10], mqe->un.mb_words[11],
5372 mqe->un.mb_words[12], mqe->un.mb_words[13],
5373 mqe->un.mb_words[14], mqe->un.mb_words[15],
5374 mqe->un.mb_words[16], mqe->un.mb_words[50],
5375 mboxq->mcqe.word0,
5376 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5377 mboxq->mcqe.trailer);
5378
5379 if (rc) {
5380 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5381 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005382 rc = -EIO;
5383 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005384 }
5385 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005386 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005387 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5388 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005389 rc = -EIO;
5390 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005391 }
James Smartda0436e2009-05-22 14:51:39 -04005392
5393 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5394 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5395 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005396 rc = 0;
5397
5398out_free_mboxq:
5399 mempool_free(mboxq, phba->mbox_mem_pool);
5400 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005401}
5402
5403/**
5404 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5405 * @phba: pointer to lpfc hba data structure.
5406 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5407 * @vpd: pointer to the memory to hold resulting port vpd data.
5408 * @vpd_size: On input, the number of bytes allocated to @vpd.
5409 * On output, the number of data bytes in @vpd.
5410 *
5411 * This routine executes a READ_REV SLI4 mailbox command. In
5412 * addition, this routine gets the port vpd data.
5413 *
5414 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005415 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005416 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005417 **/
5418static int
5419lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5420 uint8_t *vpd, uint32_t *vpd_size)
5421{
5422 int rc = 0;
5423 uint32_t dma_size;
5424 struct lpfc_dmabuf *dmabuf;
5425 struct lpfc_mqe *mqe;
5426
5427 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5428 if (!dmabuf)
5429 return -ENOMEM;
5430
5431 /*
5432 * Get a DMA buffer for the vpd data resulting from the READ_REV
5433 * mailbox command.
5434 */
5435 dma_size = *vpd_size;
Luis Chamberlain750afb02019-01-04 09:23:09 +01005436 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5437 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005438 if (!dmabuf->virt) {
5439 kfree(dmabuf);
5440 return -ENOMEM;
5441 }
James Smartda0436e2009-05-22 14:51:39 -04005442
5443 /*
5444 * The SLI4 implementation of READ_REV conflicts at word1,
5445 * bits 31:16 and SLI4 adds vpd functionality not present
5446 * in SLI3. This code corrects the conflicts.
5447 */
5448 lpfc_read_rev(phba, mboxq);
5449 mqe = &mboxq->u.mqe;
5450 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5451 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5452 mqe->un.read_rev.word1 &= 0x0000FFFF;
5453 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5454 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5455
5456 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5457 if (rc) {
5458 dma_free_coherent(&phba->pcidev->dev, dma_size,
5459 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005460 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005461 return -EIO;
5462 }
5463
James Smartda0436e2009-05-22 14:51:39 -04005464 /*
5465 * The available vpd length cannot be bigger than the
5466 * DMA buffer passed to the port. Catch the less than
5467 * case and update the caller's size.
5468 */
5469 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5470 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5471
James Smartd7c47992010-06-08 18:31:54 -04005472 memcpy(vpd, dmabuf->virt, *vpd_size);
5473
James Smartda0436e2009-05-22 14:51:39 -04005474 dma_free_coherent(&phba->pcidev->dev, dma_size,
5475 dmabuf->virt, dmabuf->phys);
5476 kfree(dmabuf);
5477 return 0;
5478}
5479
5480/**
James Smartb3b4f3e2019-03-12 16:30:23 -07005481 * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005482 * @phba: pointer to lpfc hba data structure.
5483 *
5484 * This routine retrieves SLI4 device physical port name this PCI function
5485 * is attached to.
5486 *
5487 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005488 * 0 - successful
James Smartb3b4f3e2019-03-12 16:30:23 -07005489 * otherwise - failed to retrieve controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005490 **/
5491static int
James Smartb3b4f3e2019-03-12 16:30:23 -07005492lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
James Smartcd1c8302011-10-10 21:33:25 -04005493{
5494 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005495 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5496 struct lpfc_controller_attribute *cntl_attr;
James Smartcd1c8302011-10-10 21:33:25 -04005497 void *virtaddr = NULL;
5498 uint32_t alloclen, reqlen;
5499 uint32_t shdr_status, shdr_add_status;
5500 union lpfc_sli4_cfg_shdr *shdr;
James Smartcd1c8302011-10-10 21:33:25 -04005501 int rc;
5502
James Smartcd1c8302011-10-10 21:33:25 -04005503 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5504 if (!mboxq)
5505 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005506
James Smartb3b4f3e2019-03-12 16:30:23 -07005507 /* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
James Smartcd1c8302011-10-10 21:33:25 -04005508 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5509 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5510 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5511 LPFC_SLI4_MBX_NEMBED);
James Smartb3b4f3e2019-03-12 16:30:23 -07005512
James Smartcd1c8302011-10-10 21:33:25 -04005513 if (alloclen < reqlen) {
5514 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5515 "3084 Allocated DMA memory size (%d) is "
5516 "less than the requested DMA memory size "
5517 "(%d)\n", alloclen, reqlen);
5518 rc = -ENOMEM;
5519 goto out_free_mboxq;
5520 }
5521 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5522 virtaddr = mboxq->sge_array->addr[0];
5523 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5524 shdr = &mbx_cntl_attr->cfg_shdr;
5525 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5526 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5527 if (shdr_status || shdr_add_status || rc) {
5528 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5529 "3085 Mailbox x%x (x%x/x%x) failed, "
5530 "rc:x%x, status:x%x, add_status:x%x\n",
5531 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5532 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5533 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5534 rc, shdr_status, shdr_add_status);
5535 rc = -ENXIO;
5536 goto out_free_mboxq;
5537 }
James Smartb3b4f3e2019-03-12 16:30:23 -07005538
James Smartcd1c8302011-10-10 21:33:25 -04005539 cntl_attr = &mbx_cntl_attr->cntl_attr;
5540 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5541 phba->sli4_hba.lnk_info.lnk_tp =
5542 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5543 phba->sli4_hba.lnk_info.lnk_no =
5544 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
James Smartb3b4f3e2019-03-12 16:30:23 -07005545
5546 memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
5547 strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
5548 sizeof(phba->BIOSVersion));
5549
James Smartcd1c8302011-10-10 21:33:25 -04005550 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartb3b4f3e2019-03-12 16:30:23 -07005551 "3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s\n",
James Smartcd1c8302011-10-10 21:33:25 -04005552 phba->sli4_hba.lnk_info.lnk_tp,
James Smartb3b4f3e2019-03-12 16:30:23 -07005553 phba->sli4_hba.lnk_info.lnk_no,
5554 phba->BIOSVersion);
5555out_free_mboxq:
5556 if (rc != MBX_TIMEOUT) {
5557 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5558 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5559 else
5560 mempool_free(mboxq, phba->mbox_mem_pool);
5561 }
5562 return rc;
5563}
5564
5565/**
5566 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5567 * @phba: pointer to lpfc hba data structure.
5568 *
5569 * This routine retrieves SLI4 device physical port name this PCI function
5570 * is attached to.
5571 *
5572 * Return codes
5573 * 0 - successful
5574 * otherwise - failed to retrieve physical port name
5575 **/
5576static int
5577lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5578{
5579 LPFC_MBOXQ_t *mboxq;
5580 struct lpfc_mbx_get_port_name *get_port_name;
5581 uint32_t shdr_status, shdr_add_status;
5582 union lpfc_sli4_cfg_shdr *shdr;
5583 char cport_name = 0;
5584 int rc;
5585
5586 /* We assume nothing at this point */
5587 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5588 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5589
5590 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5591 if (!mboxq)
5592 return -ENOMEM;
5593 /* obtain link type and link number via READ_CONFIG */
5594 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5595 lpfc_sli4_read_config(phba);
5596 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5597 goto retrieve_ppname;
5598
5599 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5600 rc = lpfc_sli4_get_ctl_attr(phba);
5601 if (rc)
5602 goto out_free_mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005603
5604retrieve_ppname:
5605 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5606 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5607 sizeof(struct lpfc_mbx_get_port_name) -
5608 sizeof(struct lpfc_sli4_cfg_mhdr),
5609 LPFC_SLI4_MBX_EMBED);
5610 get_port_name = &mboxq->u.mqe.un.get_port_name;
5611 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5612 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5613 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5614 phba->sli4_hba.lnk_info.lnk_tp);
5615 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5616 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5617 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5618 if (shdr_status || shdr_add_status || rc) {
5619 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5620 "3087 Mailbox x%x (x%x/x%x) failed: "
5621 "rc:x%x, status:x%x, add_status:x%x\n",
5622 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5623 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5624 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5625 rc, shdr_status, shdr_add_status);
5626 rc = -ENXIO;
5627 goto out_free_mboxq;
5628 }
5629 switch (phba->sli4_hba.lnk_info.lnk_no) {
5630 case LPFC_LINK_NUMBER_0:
5631 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5632 &get_port_name->u.response);
5633 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5634 break;
5635 case LPFC_LINK_NUMBER_1:
5636 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5637 &get_port_name->u.response);
5638 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5639 break;
5640 case LPFC_LINK_NUMBER_2:
5641 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5642 &get_port_name->u.response);
5643 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5644 break;
5645 case LPFC_LINK_NUMBER_3:
5646 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5647 &get_port_name->u.response);
5648 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5649 break;
5650 default:
5651 break;
5652 }
5653
5654 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5655 phba->Port[0] = cport_name;
5656 phba->Port[1] = '\0';
5657 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5658 "3091 SLI get port name: %s\n", phba->Port);
5659 }
5660
5661out_free_mboxq:
5662 if (rc != MBX_TIMEOUT) {
5663 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5664 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5665 else
5666 mempool_free(mboxq, phba->mbox_mem_pool);
5667 }
5668 return rc;
5669}
5670
5671/**
James Smartda0436e2009-05-22 14:51:39 -04005672 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5673 * @phba: pointer to lpfc hba data structure.
5674 *
5675 * This routine is called to explicitly arm the SLI4 device's completion and
5676 * event queues
5677 **/
5678static void
5679lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5680{
James Smart895427b2017-02-12 13:52:30 -08005681 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005682 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartcdb42be2019-01-28 11:14:21 -08005683 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07005684 struct lpfc_queue *eq;
James Smartda0436e2009-05-22 14:51:39 -04005685
James Smart32517fc2019-01-28 11:14:33 -08005686 sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
5687 sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
James Smartb71413d2018-02-22 08:18:40 -08005688 if (sli4_hba->nvmels_cq)
James Smart32517fc2019-01-28 11:14:33 -08005689 sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
5690 LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005691
James Smartcdb42be2019-01-28 11:14:21 -08005692 if (sli4_hba->hdwq) {
James Smart657add42019-05-21 17:49:06 -07005693 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08005694 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07005695 qp = &sli4_hba->hdwq[qidx];
5696 /* ARM the corresponding CQ */
James Smart01f2ef62019-08-28 16:19:11 -07005697 sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
James Smartc00f62e2019-08-14 16:57:11 -07005698 LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08005699 }
James Smart895427b2017-02-12 13:52:30 -08005700
James Smart657add42019-05-21 17:49:06 -07005701 /* Loop thru all IRQ vectors */
5702 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
5703 eq = sli4_hba->hba_eq_hdl[qidx].eq;
5704 /* ARM the corresponding EQ */
5705 sli4_hba->sli4_write_eq_db(phba, eq,
5706 0, LPFC_QUEUE_REARM);
5707 }
James Smartcdb42be2019-01-28 11:14:21 -08005708 }
James Smart1ba981f2014-02-20 09:56:45 -05005709
James Smart2d7dbc42017-02-12 13:52:35 -08005710 if (phba->nvmet_support) {
5711 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smart32517fc2019-01-28 11:14:33 -08005712 sli4_hba->sli4_write_cq_db(phba,
5713 sli4_hba->nvmet_cqset[qidx], 0,
James Smart2d7dbc42017-02-12 13:52:35 -08005714 LPFC_QUEUE_REARM);
5715 }
James Smart2e90f4b2011-12-13 13:22:37 -05005716 }
James Smartda0436e2009-05-22 14:51:39 -04005717}
5718
5719/**
James Smart6d368e52011-05-24 11:44:12 -04005720 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5721 * @phba: Pointer to HBA context object.
5722 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005723 * @extnt_count: buffer to hold port available extent count.
5724 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005725 *
James Smartb76f2dc2011-07-22 18:37:42 -04005726 * This function calls the port and retrievs the number of available
5727 * extents and their size for a particular extent type.
5728 *
5729 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005730 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005731int
James Smart6d368e52011-05-24 11:44:12 -04005732lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5733 uint16_t *extnt_count, uint16_t *extnt_size)
5734{
5735 int rc = 0;
5736 uint32_t length;
5737 uint32_t mbox_tmo;
5738 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5739 LPFC_MBOXQ_t *mbox;
5740
5741 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5742 if (!mbox)
5743 return -ENOMEM;
5744
5745 /* Find out how many extents are available for this resource type */
5746 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5747 sizeof(struct lpfc_sli4_cfg_mhdr));
5748 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5749 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5750 length, LPFC_SLI4_MBX_EMBED);
5751
5752 /* Send an extents count of 0 - the GET doesn't use it. */
5753 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5754 LPFC_SLI4_MBX_EMBED);
5755 if (unlikely(rc)) {
5756 rc = -EIO;
5757 goto err_exit;
5758 }
5759
5760 if (!phba->sli4_hba.intr_enable)
5761 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5762 else {
James Smarta183a152011-10-10 21:32:43 -04005763 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005764 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5765 }
5766 if (unlikely(rc)) {
5767 rc = -EIO;
5768 goto err_exit;
5769 }
5770
5771 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5772 if (bf_get(lpfc_mbox_hdr_status,
5773 &rsrc_info->header.cfg_shdr.response)) {
5774 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5775 "2930 Failed to get resource extents "
5776 "Status 0x%x Add'l Status 0x%x\n",
5777 bf_get(lpfc_mbox_hdr_status,
5778 &rsrc_info->header.cfg_shdr.response),
5779 bf_get(lpfc_mbox_hdr_add_status,
5780 &rsrc_info->header.cfg_shdr.response));
5781 rc = -EIO;
5782 goto err_exit;
5783 }
5784
5785 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5786 &rsrc_info->u.rsp);
5787 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5788 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005789
5790 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5791 "3162 Retrieved extents type-%d from port: count:%d, "
5792 "size:%d\n", type, *extnt_count, *extnt_size);
5793
5794err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005795 mempool_free(mbox, phba->mbox_mem_pool);
5796 return rc;
5797}
5798
5799/**
5800 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5801 * @phba: Pointer to HBA context object.
5802 * @type: The extent type to check.
5803 *
5804 * This function reads the current available extents from the port and checks
5805 * if the extent count or extent size has changed since the last access.
5806 * Callers use this routine post port reset to understand if there is a
5807 * extent reprovisioning requirement.
5808 *
5809 * Returns:
5810 * -Error: error indicates problem.
5811 * 1: Extent count or size has changed.
5812 * 0: No changes.
5813 **/
5814static int
5815lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5816{
5817 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5818 uint16_t size_diff, rsrc_ext_size;
5819 int rc = 0;
5820 struct lpfc_rsrc_blks *rsrc_entry;
5821 struct list_head *rsrc_blk_list = NULL;
5822
5823 size_diff = 0;
5824 curr_ext_cnt = 0;
5825 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5826 &rsrc_ext_cnt,
5827 &rsrc_ext_size);
5828 if (unlikely(rc))
5829 return -EIO;
5830
5831 switch (type) {
5832 case LPFC_RSC_TYPE_FCOE_RPI:
5833 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5834 break;
5835 case LPFC_RSC_TYPE_FCOE_VPI:
5836 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5837 break;
5838 case LPFC_RSC_TYPE_FCOE_XRI:
5839 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5840 break;
5841 case LPFC_RSC_TYPE_FCOE_VFI:
5842 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5843 break;
5844 default:
5845 break;
5846 }
5847
5848 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5849 curr_ext_cnt++;
5850 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5851 size_diff++;
5852 }
5853
5854 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5855 rc = 1;
5856
5857 return rc;
5858}
5859
5860/**
5861 * lpfc_sli4_cfg_post_extnts -
5862 * @phba: Pointer to HBA context object.
5863 * @extnt_cnt - number of available extents.
5864 * @type - the extent type (rpi, xri, vfi, vpi).
5865 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5866 * @mbox - pointer to the caller's allocated mailbox structure.
5867 *
5868 * This function executes the extents allocation request. It also
5869 * takes care of the amount of memory needed to allocate or get the
5870 * allocated extents. It is the caller's responsibility to evaluate
5871 * the response.
5872 *
5873 * Returns:
5874 * -Error: Error value describes the condition found.
5875 * 0: if successful
5876 **/
5877static int
James Smart8a9d2e82012-05-09 21:16:12 -04005878lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005879 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5880{
5881 int rc = 0;
5882 uint32_t req_len;
5883 uint32_t emb_len;
5884 uint32_t alloc_len, mbox_tmo;
5885
5886 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005887 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005888
5889 /*
5890 * Calculate the size of an embedded mailbox. The uint32_t
5891 * accounts for extents-specific word.
5892 */
5893 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5894 sizeof(uint32_t);
5895
5896 /*
5897 * Presume the allocation and response will fit into an embedded
5898 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5899 */
5900 *emb = LPFC_SLI4_MBX_EMBED;
5901 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005902 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005903 sizeof(union lpfc_sli4_cfg_shdr) +
5904 sizeof(uint32_t);
5905 *emb = LPFC_SLI4_MBX_NEMBED;
5906 }
5907
5908 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5909 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5910 req_len, *emb);
5911 if (alloc_len < req_len) {
5912 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005913 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005914 "less than the requested DMA memory "
5915 "size (x%x)\n", alloc_len, req_len);
5916 return -ENOMEM;
5917 }
James Smart8a9d2e82012-05-09 21:16:12 -04005918 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005919 if (unlikely(rc))
5920 return -EIO;
5921
5922 if (!phba->sli4_hba.intr_enable)
5923 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5924 else {
James Smarta183a152011-10-10 21:32:43 -04005925 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005926 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5927 }
5928
5929 if (unlikely(rc))
5930 rc = -EIO;
5931 return rc;
5932}
5933
5934/**
5935 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5936 * @phba: Pointer to HBA context object.
5937 * @type: The resource extent type to allocate.
5938 *
5939 * This function allocates the number of elements for the specified
5940 * resource type.
5941 **/
5942static int
5943lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5944{
5945 bool emb = false;
5946 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5947 uint16_t rsrc_id, rsrc_start, j, k;
5948 uint16_t *ids;
5949 int i, rc;
5950 unsigned long longs;
5951 unsigned long *bmask;
5952 struct lpfc_rsrc_blks *rsrc_blks;
5953 LPFC_MBOXQ_t *mbox;
5954 uint32_t length;
5955 struct lpfc_id_range *id_array = NULL;
5956 void *virtaddr = NULL;
5957 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5958 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5959 struct list_head *ext_blk_list;
5960
5961 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5962 &rsrc_cnt,
5963 &rsrc_size);
5964 if (unlikely(rc))
5965 return -EIO;
5966
5967 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5968 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5969 "3009 No available Resource Extents "
5970 "for resource type 0x%x: Count: 0x%x, "
5971 "Size 0x%x\n", type, rsrc_cnt,
5972 rsrc_size);
5973 return -ENOMEM;
5974 }
5975
James Smart8a9d2e82012-05-09 21:16:12 -04005976 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5977 "2903 Post resource extents type-0x%x: "
5978 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005979
5980 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5981 if (!mbox)
5982 return -ENOMEM;
5983
James Smart8a9d2e82012-05-09 21:16:12 -04005984 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005985 if (unlikely(rc)) {
5986 rc = -EIO;
5987 goto err_exit;
5988 }
5989
5990 /*
5991 * Figure out where the response is located. Then get local pointers
5992 * to the response data. The port does not guarantee to respond to
5993 * all extents counts request so update the local variable with the
5994 * allocated count from the port.
5995 */
5996 if (emb == LPFC_SLI4_MBX_EMBED) {
5997 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5998 id_array = &rsrc_ext->u.rsp.id[0];
5999 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6000 } else {
6001 virtaddr = mbox->sge_array->addr[0];
6002 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6003 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6004 id_array = &n_rsrc->id;
6005 }
6006
6007 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
6008 rsrc_id_cnt = rsrc_cnt * rsrc_size;
6009
6010 /*
6011 * Based on the resource size and count, correct the base and max
6012 * resource values.
6013 */
6014 length = sizeof(struct lpfc_rsrc_blks);
6015 switch (type) {
6016 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07006017 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006018 sizeof(unsigned long),
6019 GFP_KERNEL);
6020 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6021 rc = -ENOMEM;
6022 goto err_exit;
6023 }
Kees Cook6396bb22018-06-12 14:03:40 -07006024 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006025 sizeof(uint16_t),
6026 GFP_KERNEL);
6027 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6028 kfree(phba->sli4_hba.rpi_bmask);
6029 rc = -ENOMEM;
6030 goto err_exit;
6031 }
6032
6033 /*
6034 * The next_rpi was initialized with the maximum available
6035 * count but the port may allocate a smaller number. Catch
6036 * that case and update the next_rpi.
6037 */
6038 phba->sli4_hba.next_rpi = rsrc_id_cnt;
6039
6040 /* Initialize local ptrs for common extent processing later. */
6041 bmask = phba->sli4_hba.rpi_bmask;
6042 ids = phba->sli4_hba.rpi_ids;
6043 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6044 break;
6045 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07006046 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006047 GFP_KERNEL);
6048 if (unlikely(!phba->vpi_bmask)) {
6049 rc = -ENOMEM;
6050 goto err_exit;
6051 }
Kees Cook6396bb22018-06-12 14:03:40 -07006052 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006053 GFP_KERNEL);
6054 if (unlikely(!phba->vpi_ids)) {
6055 kfree(phba->vpi_bmask);
6056 rc = -ENOMEM;
6057 goto err_exit;
6058 }
6059
6060 /* Initialize local ptrs for common extent processing later. */
6061 bmask = phba->vpi_bmask;
6062 ids = phba->vpi_ids;
6063 ext_blk_list = &phba->lpfc_vpi_blk_list;
6064 break;
6065 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07006066 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006067 sizeof(unsigned long),
6068 GFP_KERNEL);
6069 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6070 rc = -ENOMEM;
6071 goto err_exit;
6072 }
James Smart8a9d2e82012-05-09 21:16:12 -04006073 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006074 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006075 sizeof(uint16_t),
6076 GFP_KERNEL);
6077 if (unlikely(!phba->sli4_hba.xri_ids)) {
6078 kfree(phba->sli4_hba.xri_bmask);
6079 rc = -ENOMEM;
6080 goto err_exit;
6081 }
6082
6083 /* Initialize local ptrs for common extent processing later. */
6084 bmask = phba->sli4_hba.xri_bmask;
6085 ids = phba->sli4_hba.xri_ids;
6086 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6087 break;
6088 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07006089 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006090 sizeof(unsigned long),
6091 GFP_KERNEL);
6092 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6093 rc = -ENOMEM;
6094 goto err_exit;
6095 }
Kees Cook6396bb22018-06-12 14:03:40 -07006096 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006097 sizeof(uint16_t),
6098 GFP_KERNEL);
6099 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6100 kfree(phba->sli4_hba.vfi_bmask);
6101 rc = -ENOMEM;
6102 goto err_exit;
6103 }
6104
6105 /* Initialize local ptrs for common extent processing later. */
6106 bmask = phba->sli4_hba.vfi_bmask;
6107 ids = phba->sli4_hba.vfi_ids;
6108 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6109 break;
6110 default:
6111 /* Unsupported Opcode. Fail call. */
6112 id_array = NULL;
6113 bmask = NULL;
6114 ids = NULL;
6115 ext_blk_list = NULL;
6116 goto err_exit;
6117 }
6118
6119 /*
6120 * Complete initializing the extent configuration with the
6121 * allocated ids assigned to this function. The bitmask serves
6122 * as an index into the array and manages the available ids. The
6123 * array just stores the ids communicated to the port via the wqes.
6124 */
6125 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6126 if ((i % 2) == 0)
6127 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6128 &id_array[k]);
6129 else
6130 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6131 &id_array[k]);
6132
6133 rsrc_blks = kzalloc(length, GFP_KERNEL);
6134 if (unlikely(!rsrc_blks)) {
6135 rc = -ENOMEM;
6136 kfree(bmask);
6137 kfree(ids);
6138 goto err_exit;
6139 }
6140 rsrc_blks->rsrc_start = rsrc_id;
6141 rsrc_blks->rsrc_size = rsrc_size;
6142 list_add_tail(&rsrc_blks->list, ext_blk_list);
6143 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08006144 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart5e5b5112019-01-28 11:14:22 -08006145 phba->sli4_hba.io_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08006146 lpfc_sli4_get_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08006147 }
James Smart6d368e52011-05-24 11:44:12 -04006148
6149 while (rsrc_id < (rsrc_start + rsrc_size)) {
6150 ids[j] = rsrc_id;
6151 rsrc_id++;
6152 j++;
6153 }
6154 /* Entire word processed. Get next word.*/
6155 if ((i % 2) == 1)
6156 k++;
6157 }
6158 err_exit:
6159 lpfc_sli4_mbox_cmd_free(phba, mbox);
6160 return rc;
6161}
6162
James Smart895427b2017-02-12 13:52:30 -08006163
6164
James Smart6d368e52011-05-24 11:44:12 -04006165/**
6166 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6167 * @phba: Pointer to HBA context object.
6168 * @type: the extent's type.
6169 *
6170 * This function deallocates all extents of a particular resource type.
6171 * SLI4 does not allow for deallocating a particular extent range. It
6172 * is the caller's responsibility to release all kernel memory resources.
6173 **/
6174static int
6175lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6176{
6177 int rc;
6178 uint32_t length, mbox_tmo = 0;
6179 LPFC_MBOXQ_t *mbox;
6180 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6181 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6182
6183 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6184 if (!mbox)
6185 return -ENOMEM;
6186
6187 /*
6188 * This function sends an embedded mailbox because it only sends the
6189 * the resource type. All extents of this type are released by the
6190 * port.
6191 */
6192 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6193 sizeof(struct lpfc_sli4_cfg_mhdr));
6194 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6195 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6196 length, LPFC_SLI4_MBX_EMBED);
6197
6198 /* Send an extents count of 0 - the dealloc doesn't use it. */
6199 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6200 LPFC_SLI4_MBX_EMBED);
6201 if (unlikely(rc)) {
6202 rc = -EIO;
6203 goto out_free_mbox;
6204 }
6205 if (!phba->sli4_hba.intr_enable)
6206 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6207 else {
James Smarta183a152011-10-10 21:32:43 -04006208 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006209 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6210 }
6211 if (unlikely(rc)) {
6212 rc = -EIO;
6213 goto out_free_mbox;
6214 }
6215
6216 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6217 if (bf_get(lpfc_mbox_hdr_status,
6218 &dealloc_rsrc->header.cfg_shdr.response)) {
6219 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6220 "2919 Failed to release resource extents "
6221 "for type %d - Status 0x%x Add'l Status 0x%x. "
6222 "Resource memory not released.\n",
6223 type,
6224 bf_get(lpfc_mbox_hdr_status,
6225 &dealloc_rsrc->header.cfg_shdr.response),
6226 bf_get(lpfc_mbox_hdr_add_status,
6227 &dealloc_rsrc->header.cfg_shdr.response));
6228 rc = -EIO;
6229 goto out_free_mbox;
6230 }
6231
6232 /* Release kernel memory resources for the specific type. */
6233 switch (type) {
6234 case LPFC_RSC_TYPE_FCOE_VPI:
6235 kfree(phba->vpi_bmask);
6236 kfree(phba->vpi_ids);
6237 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6238 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6239 &phba->lpfc_vpi_blk_list, list) {
6240 list_del_init(&rsrc_blk->list);
6241 kfree(rsrc_blk);
6242 }
James Smart16a3a202013-04-17 20:14:38 -04006243 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006244 break;
6245 case LPFC_RSC_TYPE_FCOE_XRI:
6246 kfree(phba->sli4_hba.xri_bmask);
6247 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006248 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6249 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6250 list_del_init(&rsrc_blk->list);
6251 kfree(rsrc_blk);
6252 }
6253 break;
6254 case LPFC_RSC_TYPE_FCOE_VFI:
6255 kfree(phba->sli4_hba.vfi_bmask);
6256 kfree(phba->sli4_hba.vfi_ids);
6257 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6258 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6259 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6260 list_del_init(&rsrc_blk->list);
6261 kfree(rsrc_blk);
6262 }
6263 break;
6264 case LPFC_RSC_TYPE_FCOE_RPI:
6265 /* RPI bitmask and physical id array are cleaned up earlier. */
6266 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6267 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6268 list_del_init(&rsrc_blk->list);
6269 kfree(rsrc_blk);
6270 }
6271 break;
6272 default:
6273 break;
6274 }
6275
6276 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6277
6278 out_free_mbox:
6279 mempool_free(mbox, phba->mbox_mem_pool);
6280 return rc;
6281}
6282
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006283static void
James Smart7bdedb32016-07-06 12:36:00 -07006284lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6285 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006286{
James Smart65791f12016-07-06 12:35:56 -07006287 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006288
James Smart65791f12016-07-06 12:35:56 -07006289 len = sizeof(struct lpfc_mbx_set_feature) -
6290 sizeof(struct lpfc_sli4_cfg_mhdr);
6291 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6292 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6293 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006294
James Smart7bdedb32016-07-06 12:36:00 -07006295 switch (feature) {
6296 case LPFC_SET_UE_RECOVERY:
6297 bf_set(lpfc_mbx_set_feature_UER,
6298 &mbox->u.mqe.un.set_feature, 1);
6299 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6300 mbox->u.mqe.un.set_feature.param_len = 8;
6301 break;
6302 case LPFC_SET_MDS_DIAGS:
6303 bf_set(lpfc_mbx_set_feature_mds,
6304 &mbox->u.mqe.un.set_feature, 1);
6305 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006306 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006307 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6308 mbox->u.mqe.un.set_feature.param_len = 8;
6309 break;
James Smart171f6c42019-11-04 16:57:07 -08006310 case LPFC_SET_DUAL_DUMP:
6311 bf_set(lpfc_mbx_set_feature_dd,
6312 &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
6313 bf_set(lpfc_mbx_set_feature_ddquery,
6314 &mbox->u.mqe.un.set_feature, 0);
6315 mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
6316 mbox->u.mqe.un.set_feature.param_len = 4;
6317 break;
James Smart65791f12016-07-06 12:35:56 -07006318 }
James Smart7bdedb32016-07-06 12:36:00 -07006319
6320 return;
James Smart65791f12016-07-06 12:35:56 -07006321}
6322
James Smart6d368e52011-05-24 11:44:12 -04006323/**
James Smart1165a5c2018-11-29 16:09:39 -08006324 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6325 * @phba: Pointer to HBA context object.
6326 *
6327 * Disable FW logging into host memory on the adapter. To
6328 * be done before reading logs from the host memory.
6329 **/
6330void
6331lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6332{
6333 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6334
James Smart95bfc6d2019-10-18 14:18:27 -07006335 spin_lock_irq(&phba->hbalock);
6336 ras_fwlog->state = INACTIVE;
6337 spin_unlock_irq(&phba->hbalock);
James Smart1165a5c2018-11-29 16:09:39 -08006338
6339 /* Disable FW logging to host memory */
6340 writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6341 phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
James Smart95bfc6d2019-10-18 14:18:27 -07006342
6343 /* Wait 10ms for firmware to stop using DMA buffer */
6344 usleep_range(10 * 1000, 20 * 1000);
James Smart1165a5c2018-11-29 16:09:39 -08006345}
6346
6347/**
James Smartd2cc9bc2018-09-10 10:30:50 -07006348 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6349 * @phba: Pointer to HBA context object.
6350 *
6351 * This function is called to free memory allocated for RAS FW logging
6352 * support in the driver.
6353 **/
6354void
6355lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6356{
6357 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6358 struct lpfc_dmabuf *dmabuf, *next;
6359
6360 if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6361 list_for_each_entry_safe(dmabuf, next,
6362 &ras_fwlog->fwlog_buff_list,
6363 list) {
6364 list_del(&dmabuf->list);
6365 dma_free_coherent(&phba->pcidev->dev,
6366 LPFC_RAS_MAX_ENTRY_SIZE,
6367 dmabuf->virt, dmabuf->phys);
6368 kfree(dmabuf);
6369 }
6370 }
6371
6372 if (ras_fwlog->lwpd.virt) {
6373 dma_free_coherent(&phba->pcidev->dev,
6374 sizeof(uint32_t) * 2,
6375 ras_fwlog->lwpd.virt,
6376 ras_fwlog->lwpd.phys);
6377 ras_fwlog->lwpd.virt = NULL;
6378 }
6379
James Smart95bfc6d2019-10-18 14:18:27 -07006380 spin_lock_irq(&phba->hbalock);
6381 ras_fwlog->state = INACTIVE;
6382 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07006383}
6384
6385/**
6386 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6387 * @phba: Pointer to HBA context object.
6388 * @fwlog_buff_count: Count of buffers to be created.
6389 *
6390 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6391 * to update FW log is posted to the adapter.
6392 * Buffer count is calculated based on module param ras_fwlog_buffsize
6393 * Size of each buffer posted to FW is 64K.
6394 **/
6395
6396static int
6397lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6398 uint32_t fwlog_buff_count)
6399{
6400 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6401 struct lpfc_dmabuf *dmabuf;
6402 int rc = 0, i = 0;
6403
6404 /* Initialize List */
6405 INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6406
6407 /* Allocate memory for the LWPD */
6408 ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6409 sizeof(uint32_t) * 2,
6410 &ras_fwlog->lwpd.phys,
6411 GFP_KERNEL);
6412 if (!ras_fwlog->lwpd.virt) {
James Smartcb349902018-11-29 16:09:27 -08006413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006414 "6185 LWPD Memory Alloc Failed\n");
6415
6416 return -ENOMEM;
6417 }
6418
6419 ras_fwlog->fw_buffcount = fwlog_buff_count;
6420 for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6421 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6422 GFP_KERNEL);
6423 if (!dmabuf) {
6424 rc = -ENOMEM;
6425 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6426 "6186 Memory Alloc failed FW logging");
6427 goto free_mem;
6428 }
6429
Luis Chamberlain750afb02019-01-04 09:23:09 +01006430 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartd2cc9bc2018-09-10 10:30:50 -07006431 LPFC_RAS_MAX_ENTRY_SIZE,
Luis Chamberlain750afb02019-01-04 09:23:09 +01006432 &dmabuf->phys, GFP_KERNEL);
James Smartd2cc9bc2018-09-10 10:30:50 -07006433 if (!dmabuf->virt) {
6434 kfree(dmabuf);
6435 rc = -ENOMEM;
6436 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6437 "6187 DMA Alloc Failed FW logging");
6438 goto free_mem;
6439 }
James Smartd2cc9bc2018-09-10 10:30:50 -07006440 dmabuf->buffer_tag = i;
6441 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6442 }
6443
6444free_mem:
6445 if (rc)
6446 lpfc_sli4_ras_dma_free(phba);
6447
6448 return rc;
6449}
6450
6451/**
6452 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6453 * @phba: pointer to lpfc hba data structure.
6454 * @pmboxq: pointer to the driver internal queue element for mailbox command.
6455 *
6456 * Completion handler for driver's RAS MBX command to the device.
6457 **/
6458static void
6459lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6460{
6461 MAILBOX_t *mb;
6462 union lpfc_sli4_cfg_shdr *shdr;
6463 uint32_t shdr_status, shdr_add_status;
6464 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6465
6466 mb = &pmb->u.mb;
6467
6468 shdr = (union lpfc_sli4_cfg_shdr *)
6469 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6470 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6471 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6472
6473 if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
James Smartcb349902018-11-29 16:09:27 -08006474 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
James Smartd2cc9bc2018-09-10 10:30:50 -07006475 "6188 FW LOG mailbox "
6476 "completed with status x%x add_status x%x,"
6477 " mbx status x%x\n",
6478 shdr_status, shdr_add_status, mb->mbxStatus);
James Smartcb349902018-11-29 16:09:27 -08006479
6480 ras_fwlog->ras_hwsupport = false;
James Smartd2cc9bc2018-09-10 10:30:50 -07006481 goto disable_ras;
6482 }
6483
James Smart95bfc6d2019-10-18 14:18:27 -07006484 spin_lock_irq(&phba->hbalock);
6485 ras_fwlog->state = ACTIVE;
6486 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07006487 mempool_free(pmb, phba->mbox_mem_pool);
6488
6489 return;
6490
6491disable_ras:
6492 /* Free RAS DMA memory */
6493 lpfc_sli4_ras_dma_free(phba);
6494 mempool_free(pmb, phba->mbox_mem_pool);
6495}
6496
6497/**
6498 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6499 * @phba: pointer to lpfc hba data structure.
6500 * @fwlog_level: Logging verbosity level.
6501 * @fwlog_enable: Enable/Disable logging.
6502 *
6503 * Initialize memory and post mailbox command to enable FW logging in host
6504 * memory.
6505 **/
6506int
6507lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6508 uint32_t fwlog_level,
6509 uint32_t fwlog_enable)
6510{
6511 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6512 struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6513 struct lpfc_dmabuf *dmabuf;
6514 LPFC_MBOXQ_t *mbox;
6515 uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6516 int rc = 0;
6517
James Smart95bfc6d2019-10-18 14:18:27 -07006518 spin_lock_irq(&phba->hbalock);
6519 ras_fwlog->state = INACTIVE;
6520 spin_unlock_irq(&phba->hbalock);
6521
James Smartd2cc9bc2018-09-10 10:30:50 -07006522 fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6523 phba->cfg_ras_fwlog_buffsize);
6524 fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6525
6526 /*
6527 * If re-enabling FW logging support use earlier allocated
6528 * DMA buffers while posting MBX command.
6529 **/
6530 if (!ras_fwlog->lwpd.virt) {
6531 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6532 if (rc) {
6533 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smartcb349902018-11-29 16:09:27 -08006534 "6189 FW Log Memory Allocation Failed");
James Smartd2cc9bc2018-09-10 10:30:50 -07006535 return rc;
6536 }
6537 }
6538
6539 /* Setup Mailbox command */
6540 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6541 if (!mbox) {
James Smartcb349902018-11-29 16:09:27 -08006542 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006543 "6190 RAS MBX Alloc Failed");
6544 rc = -ENOMEM;
6545 goto mem_free;
6546 }
6547
6548 ras_fwlog->fw_loglevel = fwlog_level;
6549 len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6550 sizeof(struct lpfc_sli4_cfg_mhdr));
6551
6552 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6553 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6554 len, LPFC_SLI4_MBX_EMBED);
6555
6556 mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6557 bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6558 fwlog_enable);
6559 bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6560 ras_fwlog->fw_loglevel);
6561 bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6562 ras_fwlog->fw_buffcount);
6563 bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6564 LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6565
6566 /* Update DMA buffer address */
6567 list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6568 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6569
6570 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6571 putPaddrLow(dmabuf->phys);
6572
6573 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6574 putPaddrHigh(dmabuf->phys);
6575 }
6576
6577 /* Update LPWD address */
6578 mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6579 mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6580
James Smart95bfc6d2019-10-18 14:18:27 -07006581 spin_lock_irq(&phba->hbalock);
6582 ras_fwlog->state = REG_INPROGRESS;
6583 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07006584 mbox->vport = phba->pport;
6585 mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6586
6587 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6588
6589 if (rc == MBX_NOT_FINISHED) {
James Smartcb349902018-11-29 16:09:27 -08006590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6591 "6191 FW-Log Mailbox failed. "
James Smartd2cc9bc2018-09-10 10:30:50 -07006592 "status %d mbxStatus : x%x", rc,
6593 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6594 mempool_free(mbox, phba->mbox_mem_pool);
6595 rc = -EIO;
6596 goto mem_free;
6597 } else
6598 rc = 0;
6599mem_free:
6600 if (rc)
6601 lpfc_sli4_ras_dma_free(phba);
6602
6603 return rc;
6604}
6605
6606/**
6607 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6608 * @phba: Pointer to HBA context object.
6609 *
6610 * Check if RAS is supported on the adapter and initialize it.
6611 **/
6612void
6613lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6614{
6615 /* Check RAS FW Log needs to be enabled or not */
6616 if (lpfc_check_fwlog_support(phba))
6617 return;
6618
6619 lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6620 LPFC_RAS_ENABLE_LOGGING);
6621}
6622
6623/**
James Smart6d368e52011-05-24 11:44:12 -04006624 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6625 * @phba: Pointer to HBA context object.
6626 *
6627 * This function allocates all SLI4 resource identifiers.
6628 **/
6629int
6630lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6631{
6632 int i, rc, error = 0;
6633 uint16_t count, base;
6634 unsigned long longs;
6635
James Smartff78d8f2011-12-13 13:21:35 -05006636 if (!phba->sli4_hba.rpi_hdrs_in_use)
6637 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006638 if (phba->sli4_hba.extents_in_use) {
6639 /*
6640 * The port supports resource extents. The XRI, VPI, VFI, RPI
6641 * resource extent count must be read and allocated before
6642 * provisioning the resource id arrays.
6643 */
6644 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6645 LPFC_IDX_RSRC_RDY) {
6646 /*
6647 * Extent-based resources are set - the driver could
6648 * be in a port reset. Figure out if any corrective
6649 * actions need to be taken.
6650 */
6651 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6652 LPFC_RSC_TYPE_FCOE_VFI);
6653 if (rc != 0)
6654 error++;
6655 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6656 LPFC_RSC_TYPE_FCOE_VPI);
6657 if (rc != 0)
6658 error++;
6659 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6660 LPFC_RSC_TYPE_FCOE_XRI);
6661 if (rc != 0)
6662 error++;
6663 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6664 LPFC_RSC_TYPE_FCOE_RPI);
6665 if (rc != 0)
6666 error++;
6667
6668 /*
6669 * It's possible that the number of resources
6670 * provided to this port instance changed between
6671 * resets. Detect this condition and reallocate
6672 * resources. Otherwise, there is no action.
6673 */
6674 if (error) {
6675 lpfc_printf_log(phba, KERN_INFO,
6676 LOG_MBOX | LOG_INIT,
6677 "2931 Detected extent resource "
6678 "change. Reallocating all "
6679 "extents.\n");
6680 rc = lpfc_sli4_dealloc_extent(phba,
6681 LPFC_RSC_TYPE_FCOE_VFI);
6682 rc = lpfc_sli4_dealloc_extent(phba,
6683 LPFC_RSC_TYPE_FCOE_VPI);
6684 rc = lpfc_sli4_dealloc_extent(phba,
6685 LPFC_RSC_TYPE_FCOE_XRI);
6686 rc = lpfc_sli4_dealloc_extent(phba,
6687 LPFC_RSC_TYPE_FCOE_RPI);
6688 } else
6689 return 0;
6690 }
6691
6692 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6693 if (unlikely(rc))
6694 goto err_exit;
6695
6696 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6697 if (unlikely(rc))
6698 goto err_exit;
6699
6700 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6701 if (unlikely(rc))
6702 goto err_exit;
6703
6704 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6705 if (unlikely(rc))
6706 goto err_exit;
6707 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6708 LPFC_IDX_RSRC_RDY);
6709 return rc;
6710 } else {
6711 /*
6712 * The port does not support resource extents. The XRI, VPI,
6713 * VFI, RPI resource ids were determined from READ_CONFIG.
6714 * Just allocate the bitmasks and provision the resource id
6715 * arrays. If a port reset is active, the resources don't
6716 * need any action - just exit.
6717 */
6718 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006719 LPFC_IDX_RSRC_RDY) {
6720 lpfc_sli4_dealloc_resource_identifiers(phba);
6721 lpfc_sli4_remove_rpis(phba);
6722 }
James Smart6d368e52011-05-24 11:44:12 -04006723 /* RPIs. */
6724 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006725 if (count <= 0) {
6726 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6727 "3279 Invalid provisioning of "
6728 "rpi:%d\n", count);
6729 rc = -EINVAL;
6730 goto err_exit;
6731 }
James Smart6d368e52011-05-24 11:44:12 -04006732 base = phba->sli4_hba.max_cfg_param.rpi_base;
6733 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006734 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006735 sizeof(unsigned long),
6736 GFP_KERNEL);
6737 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6738 rc = -ENOMEM;
6739 goto err_exit;
6740 }
Kees Cook6396bb22018-06-12 14:03:40 -07006741 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006742 GFP_KERNEL);
6743 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6744 rc = -ENOMEM;
6745 goto free_rpi_bmask;
6746 }
6747
6748 for (i = 0; i < count; i++)
6749 phba->sli4_hba.rpi_ids[i] = base + i;
6750
6751 /* VPIs. */
6752 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006753 if (count <= 0) {
6754 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6755 "3280 Invalid provisioning of "
6756 "vpi:%d\n", count);
6757 rc = -EINVAL;
6758 goto free_rpi_ids;
6759 }
James Smart6d368e52011-05-24 11:44:12 -04006760 base = phba->sli4_hba.max_cfg_param.vpi_base;
6761 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006762 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006763 GFP_KERNEL);
6764 if (unlikely(!phba->vpi_bmask)) {
6765 rc = -ENOMEM;
6766 goto free_rpi_ids;
6767 }
Kees Cook6396bb22018-06-12 14:03:40 -07006768 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006769 GFP_KERNEL);
6770 if (unlikely(!phba->vpi_ids)) {
6771 rc = -ENOMEM;
6772 goto free_vpi_bmask;
6773 }
6774
6775 for (i = 0; i < count; i++)
6776 phba->vpi_ids[i] = base + i;
6777
6778 /* XRIs. */
6779 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006780 if (count <= 0) {
6781 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6782 "3281 Invalid provisioning of "
6783 "xri:%d\n", count);
6784 rc = -EINVAL;
6785 goto free_vpi_ids;
6786 }
James Smart6d368e52011-05-24 11:44:12 -04006787 base = phba->sli4_hba.max_cfg_param.xri_base;
6788 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006789 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006790 sizeof(unsigned long),
6791 GFP_KERNEL);
6792 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6793 rc = -ENOMEM;
6794 goto free_vpi_ids;
6795 }
James Smart41899be2012-03-01 22:34:19 -05006796 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006797 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006798 GFP_KERNEL);
6799 if (unlikely(!phba->sli4_hba.xri_ids)) {
6800 rc = -ENOMEM;
6801 goto free_xri_bmask;
6802 }
6803
6804 for (i = 0; i < count; i++)
6805 phba->sli4_hba.xri_ids[i] = base + i;
6806
6807 /* VFIs. */
6808 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006809 if (count <= 0) {
6810 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6811 "3282 Invalid provisioning of "
6812 "vfi:%d\n", count);
6813 rc = -EINVAL;
6814 goto free_xri_ids;
6815 }
James Smart6d368e52011-05-24 11:44:12 -04006816 base = phba->sli4_hba.max_cfg_param.vfi_base;
6817 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006818 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006819 sizeof(unsigned long),
6820 GFP_KERNEL);
6821 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6822 rc = -ENOMEM;
6823 goto free_xri_ids;
6824 }
Kees Cook6396bb22018-06-12 14:03:40 -07006825 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006826 GFP_KERNEL);
6827 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6828 rc = -ENOMEM;
6829 goto free_vfi_bmask;
6830 }
6831
6832 for (i = 0; i < count; i++)
6833 phba->sli4_hba.vfi_ids[i] = base + i;
6834
6835 /*
6836 * Mark all resources ready. An HBA reset doesn't need
6837 * to reset the initialization.
6838 */
6839 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6840 LPFC_IDX_RSRC_RDY);
6841 return 0;
6842 }
6843
6844 free_vfi_bmask:
6845 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006846 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006847 free_xri_ids:
6848 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006849 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006850 free_xri_bmask:
6851 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006852 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006853 free_vpi_ids:
6854 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006855 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006856 free_vpi_bmask:
6857 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006858 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006859 free_rpi_ids:
6860 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006861 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006862 free_rpi_bmask:
6863 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006864 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006865 err_exit:
6866 return rc;
6867}
6868
6869/**
6870 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6871 * @phba: Pointer to HBA context object.
6872 *
6873 * This function allocates the number of elements for the specified
6874 * resource type.
6875 **/
6876int
6877lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6878{
6879 if (phba->sli4_hba.extents_in_use) {
6880 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6881 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6882 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6883 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6884 } else {
6885 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006886 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006887 kfree(phba->vpi_ids);
6888 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6889 kfree(phba->sli4_hba.xri_bmask);
6890 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006891 kfree(phba->sli4_hba.vfi_bmask);
6892 kfree(phba->sli4_hba.vfi_ids);
6893 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6894 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6895 }
6896
6897 return 0;
6898}
6899
6900/**
James Smartb76f2dc2011-07-22 18:37:42 -04006901 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6902 * @phba: Pointer to HBA context object.
6903 * @type: The resource extent type.
6904 * @extnt_count: buffer to hold port extent count response
6905 * @extnt_size: buffer to hold port extent size response.
6906 *
6907 * This function calls the port to read the host allocated extents
6908 * for a particular type.
6909 **/
6910int
6911lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6912 uint16_t *extnt_cnt, uint16_t *extnt_size)
6913{
6914 bool emb;
6915 int rc = 0;
6916 uint16_t curr_blks = 0;
6917 uint32_t req_len, emb_len;
6918 uint32_t alloc_len, mbox_tmo;
6919 struct list_head *blk_list_head;
6920 struct lpfc_rsrc_blks *rsrc_blk;
6921 LPFC_MBOXQ_t *mbox;
6922 void *virtaddr = NULL;
6923 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6924 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6925 union lpfc_sli4_cfg_shdr *shdr;
6926
6927 switch (type) {
6928 case LPFC_RSC_TYPE_FCOE_VPI:
6929 blk_list_head = &phba->lpfc_vpi_blk_list;
6930 break;
6931 case LPFC_RSC_TYPE_FCOE_XRI:
6932 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6933 break;
6934 case LPFC_RSC_TYPE_FCOE_VFI:
6935 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6936 break;
6937 case LPFC_RSC_TYPE_FCOE_RPI:
6938 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6939 break;
6940 default:
6941 return -EIO;
6942 }
6943
6944 /* Count the number of extents currently allocatd for this type. */
6945 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6946 if (curr_blks == 0) {
6947 /*
6948 * The GET_ALLOCATED mailbox does not return the size,
6949 * just the count. The size should be just the size
6950 * stored in the current allocated block and all sizes
6951 * for an extent type are the same so set the return
6952 * value now.
6953 */
6954 *extnt_size = rsrc_blk->rsrc_size;
6955 }
6956 curr_blks++;
6957 }
6958
James Smartb76f2dc2011-07-22 18:37:42 -04006959 /*
6960 * Calculate the size of an embedded mailbox. The uint32_t
6961 * accounts for extents-specific word.
6962 */
6963 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6964 sizeof(uint32_t);
6965
6966 /*
6967 * Presume the allocation and response will fit into an embedded
6968 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6969 */
6970 emb = LPFC_SLI4_MBX_EMBED;
6971 req_len = emb_len;
6972 if (req_len > emb_len) {
6973 req_len = curr_blks * sizeof(uint16_t) +
6974 sizeof(union lpfc_sli4_cfg_shdr) +
6975 sizeof(uint32_t);
6976 emb = LPFC_SLI4_MBX_NEMBED;
6977 }
6978
6979 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6980 if (!mbox)
6981 return -ENOMEM;
6982 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6983
6984 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6985 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6986 req_len, emb);
6987 if (alloc_len < req_len) {
6988 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6989 "2983 Allocated DMA memory size (x%x) is "
6990 "less than the requested DMA memory "
6991 "size (x%x)\n", alloc_len, req_len);
6992 rc = -ENOMEM;
6993 goto err_exit;
6994 }
6995 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6996 if (unlikely(rc)) {
6997 rc = -EIO;
6998 goto err_exit;
6999 }
7000
7001 if (!phba->sli4_hba.intr_enable)
7002 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
7003 else {
James Smarta183a152011-10-10 21:32:43 -04007004 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04007005 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
7006 }
7007
7008 if (unlikely(rc)) {
7009 rc = -EIO;
7010 goto err_exit;
7011 }
7012
7013 /*
7014 * Figure out where the response is located. Then get local pointers
7015 * to the response data. The port does not guarantee to respond to
7016 * all extents counts request so update the local variable with the
7017 * allocated count from the port.
7018 */
7019 if (emb == LPFC_SLI4_MBX_EMBED) {
7020 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
7021 shdr = &rsrc_ext->header.cfg_shdr;
7022 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
7023 } else {
7024 virtaddr = mbox->sge_array->addr[0];
7025 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
7026 shdr = &n_rsrc->cfg_shdr;
7027 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
7028 }
7029
7030 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
7031 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
7032 "2984 Failed to read allocated resources "
7033 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
7034 type,
7035 bf_get(lpfc_mbox_hdr_status, &shdr->response),
7036 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
7037 rc = -EIO;
7038 goto err_exit;
7039 }
7040 err_exit:
7041 lpfc_sli4_mbox_cmd_free(phba, mbox);
7042 return rc;
7043}
7044
7045/**
James Smart0ef69962017-04-21 16:04:50 -07007046 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04007047 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08007048 * @pring: Pointer to driver SLI ring object.
7049 * @sgl_list: linked link of sgl buffers to post
7050 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04007051 *
James Smart895427b2017-02-12 13:52:30 -08007052 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04007053 * repost them to the port by using SGL block post. This is needed after a
7054 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08007055 * of buffer sgls which contains contiguous xris and uses the non-embedded
7056 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04007057 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
7058 * mailbox command for posting.
7059 *
7060 * Returns: 0 = success, non-zero failure.
7061 **/
7062static int
James Smart895427b2017-02-12 13:52:30 -08007063lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
7064 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04007065{
7066 struct lpfc_sglq *sglq_entry = NULL;
7067 struct lpfc_sglq *sglq_entry_next = NULL;
7068 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08007069 int status, total_cnt;
7070 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04007071 int last_xritag = NO_XRI;
7072 LIST_HEAD(prep_sgl_list);
7073 LIST_HEAD(blck_sgl_list);
7074 LIST_HEAD(allc_sgl_list);
7075 LIST_HEAD(post_sgl_list);
7076 LIST_HEAD(free_sgl_list);
7077
James Smart38c20672013-03-01 16:37:44 -05007078 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007079 spin_lock(&phba->sli4_hba.sgl_list_lock);
7080 list_splice_init(sgl_list, &allc_sgl_list);
7081 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007082 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007083
James Smart895427b2017-02-12 13:52:30 -08007084 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007085 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
7086 &allc_sgl_list, list) {
7087 list_del_init(&sglq_entry->list);
7088 block_cnt++;
7089 if ((last_xritag != NO_XRI) &&
7090 (sglq_entry->sli4_xritag != last_xritag + 1)) {
7091 /* a hole in xri block, form a sgl posting block */
7092 list_splice_init(&prep_sgl_list, &blck_sgl_list);
7093 post_cnt = block_cnt - 1;
7094 /* prepare list for next posting block */
7095 list_add_tail(&sglq_entry->list, &prep_sgl_list);
7096 block_cnt = 1;
7097 } else {
7098 /* prepare list for next posting block */
7099 list_add_tail(&sglq_entry->list, &prep_sgl_list);
7100 /* enough sgls for non-embed sgl mbox command */
7101 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
7102 list_splice_init(&prep_sgl_list,
7103 &blck_sgl_list);
7104 post_cnt = block_cnt;
7105 block_cnt = 0;
7106 }
7107 }
7108 num_posted++;
7109
7110 /* keep track of last sgl's xritag */
7111 last_xritag = sglq_entry->sli4_xritag;
7112
James Smart895427b2017-02-12 13:52:30 -08007113 /* end of repost sgl list condition for buffers */
7114 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04007115 if (post_cnt == 0) {
7116 list_splice_init(&prep_sgl_list,
7117 &blck_sgl_list);
7118 post_cnt = block_cnt;
7119 } else if (block_cnt == 1) {
7120 status = lpfc_sli4_post_sgl(phba,
7121 sglq_entry->phys, 0,
7122 sglq_entry->sli4_xritag);
7123 if (!status) {
7124 /* successful, put sgl to posted list */
7125 list_add_tail(&sglq_entry->list,
7126 &post_sgl_list);
7127 } else {
7128 /* Failure, put sgl to free list */
7129 lpfc_printf_log(phba, KERN_WARNING,
7130 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007131 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04007132 "sgl, xritag:x%x\n",
7133 sglq_entry->sli4_xritag);
7134 list_add_tail(&sglq_entry->list,
7135 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007136 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04007137 }
7138 }
7139 }
7140
7141 /* continue until a nembed page worth of sgls */
7142 if (post_cnt == 0)
7143 continue;
7144
James Smart895427b2017-02-12 13:52:30 -08007145 /* post the buffer list sgls as a block */
7146 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7147 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04007148
7149 if (!status) {
7150 /* success, put sgl list to posted sgl list */
7151 list_splice_init(&blck_sgl_list, &post_sgl_list);
7152 } else {
7153 /* Failure, put sgl list to free sgl list */
7154 sglq_entry_first = list_first_entry(&blck_sgl_list,
7155 struct lpfc_sglq,
7156 list);
7157 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007158 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04007159 "xritag:x%x-x%x\n",
7160 sglq_entry_first->sli4_xritag,
7161 (sglq_entry_first->sli4_xritag +
7162 post_cnt - 1));
7163 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007164 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007165 }
7166
7167 /* don't reset xirtag due to hole in xri block */
7168 if (block_cnt == 0)
7169 last_xritag = NO_XRI;
7170
James Smart895427b2017-02-12 13:52:30 -08007171 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04007172 post_cnt = 0;
7173 }
7174
James Smart895427b2017-02-12 13:52:30 -08007175 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04007176 lpfc_free_sgl_list(phba, &free_sgl_list);
7177
James Smart895427b2017-02-12 13:52:30 -08007178 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04007179 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05007180 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007181 spin_lock(&phba->sli4_hba.sgl_list_lock);
7182 list_splice_init(&post_sgl_list, sgl_list);
7183 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007184 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007185 } else {
7186 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007187 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04007188 return -EIO;
7189 }
James Smart895427b2017-02-12 13:52:30 -08007190
7191 /* return the number of XRIs actually posted */
7192 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007193}
7194
James Smart0794d602019-01-28 11:14:19 -08007195/**
James Smart5e5b5112019-01-28 11:14:22 -08007196 * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
James Smart0794d602019-01-28 11:14:19 -08007197 * @phba: pointer to lpfc hba data structure.
7198 *
7199 * This routine walks the list of nvme buffers that have been allocated and
7200 * repost them to the port by using SGL block post. This is needed after a
7201 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7202 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
James Smart5e5b5112019-01-28 11:14:22 -08007203 * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
James Smart0794d602019-01-28 11:14:19 -08007204 *
7205 * Returns: 0 = success, non-zero failure.
7206 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07007207static int
James Smart5e5b5112019-01-28 11:14:22 -08007208lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
James Smart0794d602019-01-28 11:14:19 -08007209{
7210 LIST_HEAD(post_nblist);
7211 int num_posted, rc = 0;
7212
7213 /* get all NVME buffers need to repost to a local list */
James Smart5e5b5112019-01-28 11:14:22 -08007214 lpfc_io_buf_flush(phba, &post_nblist);
James Smart0794d602019-01-28 11:14:19 -08007215
7216 /* post the list of nvme buffer sgls to port if available */
7217 if (!list_empty(&post_nblist)) {
James Smart5e5b5112019-01-28 11:14:22 -08007218 num_posted = lpfc_sli4_post_io_sgl_list(
7219 phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
James Smart0794d602019-01-28 11:14:19 -08007220 /* failed to post any nvme buffer, return error */
7221 if (num_posted == 0)
7222 rc = -EIO;
7223 }
7224 return rc;
7225}
7226
Bart Van Assche3999df72019-03-28 11:06:16 -07007227static void
James Smart61bda8f2016-10-13 15:06:05 -07007228lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7229{
7230 uint32_t len;
7231
7232 len = sizeof(struct lpfc_mbx_set_host_data) -
7233 sizeof(struct lpfc_sli4_cfg_mhdr);
7234 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7235 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7236 LPFC_SLI4_MBX_EMBED);
7237
7238 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08007239 mbox->u.mqe.un.set_host_data.param_len =
7240 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07007241 snprintf(mbox->u.mqe.un.set_host_data.data,
7242 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7243 "Linux %s v"LPFC_DRIVER_VERSION,
7244 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7245}
7246
James Smarta8cf5df2017-05-15 15:20:46 -07007247int
James Smart6c621a22017-05-15 15:20:45 -07007248lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07007249 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07007250{
7251 int rc, i;
7252 struct lpfc_rqe hrqe;
7253 struct lpfc_rqe drqe;
7254 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08007255 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07007256 struct rqb_dmabuf *rqb_buffer;
7257 LIST_HEAD(rqb_buf_list);
7258
James Smart411de512018-01-30 15:58:52 -08007259 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007260 rqbp = hrq->rqbp;
7261 for (i = 0; i < count; i++) {
7262 /* IF RQ is already full, don't bother */
7263 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
7264 break;
7265 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7266 if (!rqb_buffer)
7267 break;
7268 rqb_buffer->hrq = hrq;
7269 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07007270 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07007271 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7272 }
7273 while (!list_empty(&rqb_buf_list)) {
7274 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7275 hbuf.list);
7276
7277 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7278 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7279 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7280 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7281 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7282 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08007283 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7284 "6421 Cannot post to HRQ %d: %x %x %x "
7285 "DRQ %x %x\n",
7286 hrq->queue_id,
7287 hrq->host_index,
7288 hrq->hba_index,
7289 hrq->entry_count,
7290 drq->host_index,
7291 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07007292 rqbp->rqb_free_buffer(phba, rqb_buffer);
7293 } else {
7294 list_add_tail(&rqb_buffer->hbuf.list,
7295 &rqbp->rqb_buffer_list);
7296 rqbp->buffer_count++;
7297 }
7298 }
James Smart411de512018-01-30 15:58:52 -08007299 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007300 return 1;
7301}
7302
Dick Kennedy317aeb82020-06-30 14:49:59 -07007303/**
7304 * lpfc_init_idle_stat_hb - Initialize idle_stat tracking
7305 *
7306 * This routine initializes the per-cq idle_stat to dynamically dictate
7307 * polling decisions.
7308 *
7309 * Return codes:
7310 * None
7311 **/
7312static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)
7313{
7314 int i;
7315 struct lpfc_sli4_hdw_queue *hdwq;
7316 struct lpfc_queue *cq;
7317 struct lpfc_idle_stat *idle_stat;
7318 u64 wall;
7319
7320 for_each_present_cpu(i) {
7321 hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
7322 cq = hdwq->io_cq;
7323
7324 /* Skip if we've already handled this cq's primary CPU */
7325 if (cq->chann != i)
7326 continue;
7327
7328 idle_stat = &phba->sli4_hba.idle_stat[i];
7329
7330 idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);
7331 idle_stat->prev_wall = wall;
7332
7333 if (phba->nvmet_support)
7334 cq->poll_mode = LPFC_QUEUE_WORK;
7335 else
7336 cq->poll_mode = LPFC_IRQ_POLL;
7337 }
7338
7339 if (!phba->nvmet_support)
7340 schedule_delayed_work(&phba->idle_stat_delay_work,
7341 msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
7342}
7343
Dick Kennedyf0020e42020-06-30 14:49:58 -07007344static void lpfc_sli4_dip(struct lpfc_hba *phba)
7345{
7346 uint32_t if_type;
7347
7348 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7349 if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
7350 if_type == LPFC_SLI_INTF_IF_TYPE_6) {
7351 struct lpfc_register reg_data;
7352
7353 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7354 &reg_data.word0))
7355 return;
7356
7357 if (bf_get(lpfc_sliport_status_dip, &reg_data))
7358 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7359 "2904 Firmware Dump Image Present"
7360 " on Adapter");
7361 }
7362}
7363
James Smart8a9d2e82012-05-09 21:16:12 -04007364/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08007365 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04007366 * @phba: Pointer to HBA context object.
7367 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08007368 * This function is the main SLI4 device initialization PCI function. This
7369 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04007370 * HBA error attention handler code. Caller is not required to hold any
7371 * locks.
7372 **/
7373int
7374lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7375{
James Smart171f6c42019-11-04 16:57:07 -08007376 int rc, i, cnt, len, dd;
James Smartda0436e2009-05-22 14:51:39 -04007377 LPFC_MBOXQ_t *mboxq;
7378 struct lpfc_mqe *mqe;
7379 uint8_t *vpd;
7380 uint32_t vpd_size;
7381 uint32_t ftr_rsp = 0;
7382 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7383 struct lpfc_vport *vport = phba->pport;
7384 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08007385 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04007386
7387 /* Perform a PCI function reset to start from clean */
7388 rc = lpfc_pci_function_reset(phba);
7389 if (unlikely(rc))
7390 return -ENODEV;
7391
7392 /* Check the HBA Host Status Register for readyness */
7393 rc = lpfc_sli4_post_status_check(phba);
7394 if (unlikely(rc))
7395 return -ENODEV;
7396 else {
7397 spin_lock_irq(&phba->hbalock);
7398 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7399 spin_unlock_irq(&phba->hbalock);
7400 }
7401
Dick Kennedyf0020e42020-06-30 14:49:58 -07007402 lpfc_sli4_dip(phba);
7403
James Smartda0436e2009-05-22 14:51:39 -04007404 /*
7405 * Allocate a single mailbox container for initializing the
7406 * port.
7407 */
7408 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7409 if (!mboxq)
7410 return -ENOMEM;
7411
James Smartda0436e2009-05-22 14:51:39 -04007412 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04007413 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04007414 vpd = kzalloc(vpd_size, GFP_KERNEL);
7415 if (!vpd) {
7416 rc = -ENOMEM;
7417 goto out_free_mbox;
7418 }
7419
7420 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05007421 if (unlikely(rc)) {
7422 kfree(vpd);
7423 goto out_free_mbox;
7424 }
James Smart572709e2013-07-15 18:32:43 -04007425
James Smartda0436e2009-05-22 14:51:39 -04007426 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04007427 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07007428 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05007429 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007430 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7431 } else {
James Smart76a95d72010-11-20 23:11:48 -05007432 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007433 }
James Smart45ed1192009-10-02 15:17:02 -04007434
7435 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7436 LPFC_DCBX_CEE_MODE)
7437 phba->hba_flag |= HBA_FIP_SUPPORT;
7438 else
7439 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7440
James Smartc00f62e2019-08-14 16:57:11 -07007441 phba->hba_flag &= ~HBA_IOQ_FLUSH;
James Smart4f2e66c2012-05-09 21:17:07 -04007442
James Smartc31098c2011-04-16 11:03:33 -04007443 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04007444 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7445 "0376 READ_REV Error. SLI Level %d "
7446 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05007447 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04007448 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007449 kfree(vpd);
7450 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007451 }
James Smartcd1c8302011-10-10 21:33:25 -04007452
7453 /*
James Smartff78d8f2011-12-13 13:21:35 -05007454 * Continue initialization with default values even if driver failed
7455 * to read FCoE param config regions, only read parameters if the
7456 * board is FCoE
7457 */
7458 if (phba->hba_flag & HBA_FCOE_MODE &&
7459 lpfc_sli4_read_fcoe_params(phba))
7460 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7461 "2570 Failed to read FCoE parameters\n");
7462
7463 /*
James Smartcd1c8302011-10-10 21:33:25 -04007464 * Retrieve sli4 device physical port name, failure of doing it
7465 * is considered as non-fatal.
7466 */
7467 rc = lpfc_sli4_retrieve_pport_name(phba);
7468 if (!rc)
7469 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7470 "3080 Successful retrieving SLI4 device "
7471 "physical port name: %s.\n", phba->Port);
7472
James Smartb3b4f3e2019-03-12 16:30:23 -07007473 rc = lpfc_sli4_get_ctl_attr(phba);
7474 if (!rc)
7475 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7476 "8351 Successful retrieving SLI4 device "
7477 "CTL ATTR\n");
7478
James Smartda0436e2009-05-22 14:51:39 -04007479 /*
7480 * Evaluate the read rev and vpd data. Populate the driver
7481 * state with the results. If this routine fails, the failure
7482 * is not fatal as the driver will use generic values.
7483 */
7484 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7485 if (unlikely(!rc)) {
7486 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7487 "0377 Error %d parsing vpd. "
7488 "Using defaults.\n", rc);
7489 rc = 0;
7490 }
James Smart76a95d72010-11-20 23:11:48 -05007491 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04007492
James Smartf1126682009-06-10 17:22:44 -04007493 /* Save information as VPD data */
7494 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7495 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08007496
7497 /*
7498 * This is because first G7 ASIC doesn't support the standard
7499 * 0x5a NVME cmd descriptor type/subtype
7500 */
7501 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7502 LPFC_SLI_INTF_IF_TYPE_6) &&
7503 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7504 (phba->vpd.rev.smRev == 0) &&
7505 (phba->cfg_nvme_embed_cmd == 1))
7506 phba->cfg_nvme_embed_cmd = 0;
7507
James Smartf1126682009-06-10 17:22:44 -04007508 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7509 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7510 &mqe->un.read_rev);
7511 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7512 &mqe->un.read_rev);
7513 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7514 &mqe->un.read_rev);
7515 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7516 &mqe->un.read_rev);
7517 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7518 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7519 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7520 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7521 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7522 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7523 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7524 "(%d):0380 READ_REV Status x%x "
7525 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7526 mboxq->vport ? mboxq->vport->vpi : 0,
7527 bf_get(lpfc_mqe_status, mqe),
7528 phba->vpd.rev.opFwName,
7529 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7530 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04007531
James Smart65791f12016-07-06 12:35:56 -07007532 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07007533 LPFC_SLI_INTF_IF_TYPE_0) {
7534 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7535 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7536 if (rc == MBX_SUCCESS) {
7537 phba->hba_flag |= HBA_RECOVERABLE_UE;
7538 /* Set 1Sec interval to detect UE */
7539 phba->eratt_poll_interval = 1;
7540 phba->sli4_hba.ue_to_sr = bf_get(
7541 lpfc_mbx_set_feature_UESR,
7542 &mboxq->u.mqe.un.set_feature);
7543 phba->sli4_hba.ue_to_rp = bf_get(
7544 lpfc_mbx_set_feature_UERP,
7545 &mboxq->u.mqe.un.set_feature);
7546 }
7547 }
7548
7549 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7550 /* Enable MDS Diagnostics only if the SLI Port supports it */
7551 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7552 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7553 if (rc != MBX_SUCCESS)
7554 phba->mds_diags_support = 0;
7555 }
James Smart572709e2013-07-15 18:32:43 -04007556
James Smartda0436e2009-05-22 14:51:39 -04007557 /*
7558 * Discover the port's supported feature set and match it against the
7559 * hosts requests.
7560 */
7561 lpfc_request_features(phba, mboxq);
7562 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7563 if (unlikely(rc)) {
7564 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007565 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007566 }
7567
7568 /*
7569 * The port must support FCP initiator mode as this is the
7570 * only mode running in the host.
7571 */
7572 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7573 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7574 "0378 No support for fcpi mode.\n");
7575 ftr_rsp++;
7576 }
James Smart0bc2b7c2018-02-22 08:18:48 -08007577
7578 /* Performance Hints are ONLY for FCoE */
7579 if (phba->hba_flag & HBA_FCOE_MODE) {
7580 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7581 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7582 else
7583 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7584 }
7585
James Smartda0436e2009-05-22 14:51:39 -04007586 /*
7587 * If the port cannot support the host's requested features
7588 * then turn off the global config parameters to disable the
7589 * feature in the driver. This is not a fatal error.
7590 */
James Smartf44ac122018-03-05 12:04:08 -08007591 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7592 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7593 phba->cfg_enable_bg = 0;
7594 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04007595 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08007596 }
James Smartbf086112011-08-21 21:48:13 -04007597 }
James Smartda0436e2009-05-22 14:51:39 -04007598
7599 if (phba->max_vpi && phba->cfg_enable_npiv &&
7600 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7601 ftr_rsp++;
7602
7603 if (ftr_rsp) {
7604 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7605 "0379 Feature Mismatch Data: x%08x %08x "
7606 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7607 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7608 phba->cfg_enable_npiv, phba->max_vpi);
7609 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7610 phba->cfg_enable_bg = 0;
7611 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7612 phba->cfg_enable_npiv = 0;
7613 }
7614
7615 /* These SLI3 features are assumed in SLI4 */
7616 spin_lock_irq(&phba->hbalock);
7617 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7618 spin_unlock_irq(&phba->hbalock);
7619
James Smart171f6c42019-11-04 16:57:07 -08007620 /* Always try to enable dual dump feature if we can */
7621 lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
7622 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7623 dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
7624 if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
7625 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_INIT,
7626 "6448 Dual Dump is enabled\n");
7627 else
7628 lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
7629 "6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
7630 "rc:x%x dd:x%x\n",
7631 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7632 lpfc_sli_config_mbox_subsys_get(
7633 phba, mboxq),
7634 lpfc_sli_config_mbox_opcode_get(
7635 phba, mboxq),
7636 rc, dd);
James Smart6d368e52011-05-24 11:44:12 -04007637 /*
7638 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7639 * calls depends on these resources to complete port setup.
7640 */
7641 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7642 if (rc) {
7643 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7644 "2920 Failed to alloc Resource IDs "
7645 "rc = x%x\n", rc);
7646 goto out_free_mbox;
7647 }
7648
James Smart61bda8f2016-10-13 15:06:05 -07007649 lpfc_set_host_data(phba, mboxq);
7650
7651 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7652 if (rc) {
7653 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7654 "2134 Failed to set host os driver version %x",
7655 rc);
7656 }
7657
James Smartda0436e2009-05-22 14:51:39 -04007658 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05007659 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7660 if (rc) {
7661 phba->link_state = LPFC_HBA_ERROR;
7662 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05007663 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05007664 }
7665
James Smartda0436e2009-05-22 14:51:39 -04007666 mboxq->vport = vport;
7667 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart3e1f0712018-11-29 16:09:29 -08007668 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04007669 if (rc == MBX_SUCCESS) {
7670 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7671 rc = 0;
7672 }
7673
7674 /*
7675 * This memory was allocated by the lpfc_read_sparam routine. Release
7676 * it to the mbuf pool.
7677 */
7678 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7679 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -08007680 mboxq->ctx_buf = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007681 if (unlikely(rc)) {
7682 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7683 "0382 READ_SPARAM command failed "
7684 "status %d, mbxStatus x%x\n",
7685 rc, bf_get(lpfc_mqe_status, mqe));
7686 phba->link_state = LPFC_HBA_ERROR;
7687 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007688 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007689 }
7690
James Smart05580562011-05-24 11:40:48 -04007691 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007692
7693 /* Update the fc_host data structures with new wwn. */
7694 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7695 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7696
James Smart895427b2017-02-12 13:52:30 -08007697 /* Create all the SLI4 queues */
7698 rc = lpfc_sli4_queue_create(phba);
7699 if (rc) {
7700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7701 "3089 Failed to allocate queues\n");
7702 rc = -ENODEV;
7703 goto out_free_mbox;
7704 }
7705 /* Set up all the queues to the device */
7706 rc = lpfc_sli4_queue_setup(phba);
7707 if (unlikely(rc)) {
7708 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7709 "0381 Error %d during queue setup.\n ", rc);
7710 goto out_stop_timers;
7711 }
7712 /* Initialize the driver internal SLI layer lists. */
7713 lpfc_sli4_setup(phba);
7714 lpfc_sli4_queue_init(phba);
7715
7716 /* update host els xri-sgl sizes and mappings */
7717 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007718 if (unlikely(rc)) {
7719 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7720 "1400 Failed to update xri-sgl size and "
7721 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007722 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007723 }
7724
James Smart8a9d2e82012-05-09 21:16:12 -04007725 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007726 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7727 phba->sli4_hba.els_xri_cnt);
7728 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04007729 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7730 "0582 Error %d during els sgl post "
7731 "operation\n", rc);
7732 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007733 goto out_destroy_queue;
7734 }
7735 phba->sli4_hba.els_xri_cnt = rc;
7736
James Smartf358dd02017-02-12 13:52:34 -08007737 if (phba->nvmet_support) {
7738 /* update host nvmet xri-sgl sizes and mappings */
7739 rc = lpfc_sli4_nvmet_sgl_update(phba);
7740 if (unlikely(rc)) {
7741 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7742 "6308 Failed to update nvmet-sgl size "
7743 "and mapping: %d\n", rc);
7744 goto out_destroy_queue;
7745 }
7746
7747 /* register the nvmet sgl pool to the port */
7748 rc = lpfc_sli4_repost_sgl_list(
7749 phba,
7750 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7751 phba->sli4_hba.nvmet_xri_cnt);
7752 if (unlikely(rc < 0)) {
7753 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7754 "3117 Error %d during nvmet "
7755 "sgl post\n", rc);
7756 rc = -ENODEV;
7757 goto out_destroy_queue;
7758 }
7759 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007760
James Smarta5f73372019-09-21 20:58:50 -07007761 /* We allocate an iocbq for every receive context SGL.
7762 * The additional allocation is for abort and ls handling.
7763 */
7764 cnt = phba->sli4_hba.nvmet_xri_cnt +
7765 phba->sli4_hba.max_cfg_param.max_xri;
James Smartf358dd02017-02-12 13:52:34 -08007766 } else {
James Smart0794d602019-01-28 11:14:19 -08007767 /* update host common xri-sgl sizes and mappings */
James Smart5e5b5112019-01-28 11:14:22 -08007768 rc = lpfc_sli4_io_sgl_update(phba);
James Smart895427b2017-02-12 13:52:30 -08007769 if (unlikely(rc)) {
7770 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7771 "6082 Failed to update nvme-sgl size "
7772 "and mapping: %d\n", rc);
7773 goto out_destroy_queue;
7774 }
James Smart6c621a22017-05-15 15:20:45 -07007775
James Smart0794d602019-01-28 11:14:19 -08007776 /* register the allocated common sgl pool to the port */
James Smart5e5b5112019-01-28 11:14:22 -08007777 rc = lpfc_sli4_repost_io_sgl_list(phba);
James Smart0794d602019-01-28 11:14:19 -08007778 if (unlikely(rc)) {
7779 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7780 "6116 Error %d during nvme sgl post "
7781 "operation\n", rc);
7782 /* Some NVME buffers were moved to abort nvme list */
7783 /* A pci function reset will repost them */
7784 rc = -ENODEV;
7785 goto out_destroy_queue;
7786 }
James Smarta5f73372019-09-21 20:58:50 -07007787 /* Each lpfc_io_buf job structure has an iocbq element.
7788 * This cnt provides for abort, els, ct and ls requests.
7789 */
7790 cnt = phba->sli4_hba.max_cfg_param.max_xri;
James Smart11e644e2017-06-15 22:56:48 -07007791 }
7792
7793 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007794 /* Initialize and populate the iocb list per host */
7795 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta5f73372019-09-21 20:58:50 -07007796 "2821 initialize iocb list with %d entries\n",
7797 cnt);
James Smart6c621a22017-05-15 15:20:45 -07007798 rc = lpfc_init_iocb_list(phba, cnt);
7799 if (rc) {
7800 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007801 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007802 goto out_destroy_queue;
7803 }
James Smart8a9d2e82012-05-09 21:16:12 -04007804 }
7805
James Smart11e644e2017-06-15 22:56:48 -07007806 if (phba->nvmet_support)
7807 lpfc_nvmet_create_targetport(phba);
7808
James Smart2d7dbc42017-02-12 13:52:35 -08007809 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007810 /* Post initial buffers to all RQs created */
7811 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7812 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7813 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7814 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7815 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007816 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007817 rqbp->buffer_count = 0;
7818
James Smart2d7dbc42017-02-12 13:52:35 -08007819 lpfc_post_rq_buffer(
7820 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7821 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07007822 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007823 }
7824 }
7825
James Smartda0436e2009-05-22 14:51:39 -04007826 /* Post the rpi header region to the device. */
7827 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7828 if (unlikely(rc)) {
7829 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7830 "0393 Error %d during rpi post operation\n",
7831 rc);
7832 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007833 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007834 }
James Smart97f2ecf2012-03-01 22:35:23 -05007835 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007836
James Smart895427b2017-02-12 13:52:30 -08007837 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007838 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007839 /*
7840 * The FC Port needs to register FCFI (index 0)
7841 */
7842 lpfc_reg_fcfi(phba, mboxq);
7843 mboxq->vport = phba->pport;
7844 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7845 if (rc != MBX_SUCCESS)
7846 goto out_unset_queue;
7847 rc = 0;
7848 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7849 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007850 } else {
7851 /* We are a NVME Target mode with MRQ > 1 */
7852
7853 /* First register the FCFI */
7854 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7855 mboxq->vport = phba->pport;
7856 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7857 if (rc != MBX_SUCCESS)
7858 goto out_unset_queue;
7859 rc = 0;
7860 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7861 &mboxq->u.mqe.un.reg_fcfi_mrq);
7862
7863 /* Next register the MRQs */
7864 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7865 mboxq->vport = phba->pport;
7866 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7867 if (rc != MBX_SUCCESS)
7868 goto out_unset_queue;
7869 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007870 }
7871 /* Check if the port is configured to be disabled */
7872 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007873 }
7874
James Smartc4908502019-01-28 11:14:28 -08007875 /* Don't post more new bufs if repost already recovered
7876 * the nvme sgls.
7877 */
7878 if (phba->nvmet_support == 0) {
7879 if (phba->sli4_hba.io_xri_cnt == 0) {
7880 len = lpfc_new_io_buf(
7881 phba, phba->sli4_hba.io_xri_max);
7882 if (len == 0) {
7883 rc = -ENOMEM;
7884 goto out_unset_queue;
7885 }
7886
7887 if (phba->cfg_xri_rebalancing)
7888 lpfc_create_multixri_pools(phba);
7889 }
7890 } else {
7891 phba->cfg_xri_rebalancing = 0;
7892 }
7893
James Smartda0436e2009-05-22 14:51:39 -04007894 /* Allow asynchronous mailbox command to go through */
7895 spin_lock_irq(&phba->hbalock);
7896 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7897 spin_unlock_irq(&phba->hbalock);
7898
7899 /* Post receive buffers to the device */
7900 lpfc_sli4_rb_setup(phba);
7901
James Smartfc2b9892010-02-26 14:15:29 -05007902 /* Reset HBA FCF states after HBA reset */
7903 phba->fcf.fcf_flag = 0;
7904 phba->fcf.current_rec.flag = 0;
7905
James Smartda0436e2009-05-22 14:51:39 -04007906 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007907 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007908 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007909
7910 /* Start heart beat timer */
7911 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007912 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007913 phba->hb_outstanding = 0;
7914 phba->last_completion_time = jiffies;
7915
James Smart32517fc2019-01-28 11:14:33 -08007916 /* start eq_delay heartbeat */
7917 if (phba->cfg_auto_imax)
7918 queue_delayed_work(phba->wq, &phba->eq_delay_work,
7919 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
7920
Dick Kennedy317aeb82020-06-30 14:49:59 -07007921 /* start per phba idle_stat_delay heartbeat */
7922 lpfc_init_idle_stat_hb(phba);
7923
James Smartda0436e2009-05-22 14:51:39 -04007924 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007925 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007926 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007927
James Smart75baf692010-06-08 18:31:21 -04007928 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7929 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7930 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7931 if (!rc) {
7932 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7933 "2829 This device supports "
7934 "Advanced Error Reporting (AER)\n");
7935 spin_lock_irq(&phba->hbalock);
7936 phba->hba_flag |= HBA_AER_ENABLED;
7937 spin_unlock_irq(&phba->hbalock);
7938 } else {
7939 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7940 "2830 This device does not support "
7941 "Advanced Error Reporting (AER)\n");
7942 phba->cfg_aer_support = 0;
7943 }
James Smart0a96e972011-07-22 18:37:28 -04007944 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007945 }
7946
James Smartda0436e2009-05-22 14:51:39 -04007947 /*
7948 * The port is ready, set the host's link state to LINK_DOWN
7949 * in preparation for link interrupts.
7950 */
James Smartda0436e2009-05-22 14:51:39 -04007951 spin_lock_irq(&phba->hbalock);
7952 phba->link_state = LPFC_LINK_DOWN;
James Smart1dc5ec22018-10-23 13:41:11 -07007953
7954 /* Check if physical ports are trunked */
7955 if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
7956 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
7957 if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
7958 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
7959 if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
7960 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
7961 if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
7962 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04007963 spin_unlock_irq(&phba->hbalock);
James Smart1dc5ec22018-10-23 13:41:11 -07007964
James Smarte8869f52019-03-12 16:30:18 -07007965 /* Arm the CQs and then EQs on device */
7966 lpfc_sli4_arm_cqeq_intr(phba);
7967
7968 /* Indicate device interrupt mode */
7969 phba->sli4_hba.intr_enable = 1;
7970
James Smart026abb82011-12-13 13:20:45 -05007971 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7972 (phba->hba_flag & LINK_DISABLED)) {
7973 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7974 "3103 Adapter Link is disabled.\n");
7975 lpfc_down_link(phba, mboxq);
7976 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7977 if (rc != MBX_SUCCESS) {
7978 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7979 "3104 Adapter failed to issue "
7980 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
James Smartc4908502019-01-28 11:14:28 -08007981 goto out_io_buff_free;
James Smart026abb82011-12-13 13:20:45 -05007982 }
7983 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007984 /* don't perform init_link on SLI4 FC port loopback test */
7985 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7986 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7987 if (rc)
James Smartc4908502019-01-28 11:14:28 -08007988 goto out_io_buff_free;
James Smart1b511972011-12-13 13:23:09 -05007989 }
James Smart5350d872011-10-10 21:33:49 -04007990 }
7991 mempool_free(mboxq, phba->mbox_mem_pool);
7992 return rc;
James Smartc4908502019-01-28 11:14:28 -08007993out_io_buff_free:
7994 /* Free allocated IO Buffers */
7995 lpfc_io_free(phba);
James Smart76a95d72010-11-20 23:11:48 -05007996out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007997 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007998 lpfc_sli4_queue_unset(phba);
7999out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07008000 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04008001 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04008002out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04008003 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04008004out_free_mbox:
8005 mempool_free(mboxq, phba->mbox_mem_pool);
8006 return rc;
8007}
James Smarte59058c2008-08-24 21:49:00 -04008008
8009/**
James Smart3621a712009-04-06 18:47:14 -04008010 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04008011 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05008012 *
James Smarte59058c2008-08-24 21:49:00 -04008013 * This is the callback function for mailbox timer. The mailbox
8014 * timer is armed when a new mailbox command is issued and the timer
8015 * is deleted when the mailbox complete. The function is called by
8016 * the kernel timer code when a mailbox does not complete within
8017 * expected time. This function wakes up the worker thread to
8018 * process the mailbox timeout and returns. All the processing is
8019 * done by the worker thread function lpfc_mbox_timeout_handler.
8020 **/
dea31012005-04-17 16:05:31 -05008021void
Kees Cookf22eb4d2017-09-06 20:24:26 -07008022lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05008023{
Kees Cookf22eb4d2017-09-06 20:24:26 -07008024 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05008025 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05008026 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05008027
James Smart2e0fef82007-06-17 19:56:36 -05008028 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008029 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05008030 if (!tmo_posted)
8031 phba->pport->work_port_events |= WORKER_MBOX_TMO;
8032 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
8033
James Smart5e9d9b82008-06-14 22:52:53 -04008034 if (!tmo_posted)
8035 lpfc_worker_wake_up(phba);
8036 return;
dea31012005-04-17 16:05:31 -05008037}
8038
James Smarte8d3c3b2013-10-10 12:21:30 -04008039/**
8040 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
8041 * are pending
8042 * @phba: Pointer to HBA context object.
8043 *
8044 * This function checks if any mailbox completions are present on the mailbox
8045 * completion queue.
8046 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04008047static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04008048lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
8049{
8050
8051 uint32_t idx;
8052 struct lpfc_queue *mcq;
8053 struct lpfc_mcqe *mcqe;
8054 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08008055 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04008056
8057 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8058 return false;
8059
8060 /* Check for completions on mailbox completion queue */
8061
8062 mcq = phba->sli4_hba.mbx_cq;
8063 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08008064 qe_valid = mcq->qe_valid;
James Smart9afbee32019-03-12 16:30:28 -07008065 while (bf_get_le32(lpfc_cqe_valid,
8066 (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
8067 mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
James Smarte8d3c3b2013-10-10 12:21:30 -04008068 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
8069 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
8070 pending_completions = true;
8071 break;
8072 }
8073 idx = (idx + 1) % mcq->entry_count;
8074 if (mcq->hba_index == idx)
8075 break;
James Smart7365f6f2018-02-22 08:18:46 -08008076
8077 /* if the index wrapped around, toggle the valid bit */
8078 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
8079 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04008080 }
8081 return pending_completions;
8082
8083}
8084
8085/**
8086 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
8087 * that were missed.
8088 * @phba: Pointer to HBA context object.
8089 *
8090 * For sli4, it is possible to miss an interrupt. As such mbox completions
8091 * maybe missed causing erroneous mailbox timeouts to occur. This function
8092 * checks to see if mbox completions are on the mailbox completion queue
8093 * and will process all the completions associated with the eq for the
8094 * mailbox completion queue.
8095 **/
YueHaibingd7b761b2019-05-31 23:28:41 +08008096static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04008097lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
8098{
James Smartb71413d2018-02-22 08:18:40 -08008099 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04008100 uint32_t eqidx;
8101 struct lpfc_queue *fpeq = NULL;
James Smart657add42019-05-21 17:49:06 -07008102 struct lpfc_queue *eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04008103 bool mbox_pending;
8104
8105 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8106 return false;
8107
James Smart657add42019-05-21 17:49:06 -07008108 /* Find the EQ associated with the mbox CQ */
8109 if (sli4_hba->hdwq) {
8110 for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
8111 eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
Daniel Wagner535fb492019-10-18 18:21:11 +02008112 if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
James Smart657add42019-05-21 17:49:06 -07008113 fpeq = eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04008114 break;
8115 }
James Smart657add42019-05-21 17:49:06 -07008116 }
8117 }
James Smarte8d3c3b2013-10-10 12:21:30 -04008118 if (!fpeq)
8119 return false;
8120
8121 /* Turn off interrupts from this EQ */
8122
James Smartb71413d2018-02-22 08:18:40 -08008123 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04008124
8125 /* Check to see if a mbox completion is pending */
8126
8127 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
8128
8129 /*
8130 * If a mbox completion is pending, process all the events on EQ
8131 * associated with the mbox completion queue (this could include
8132 * mailbox commands, async events, els commands, receive queue data
8133 * and fcp commands)
8134 */
8135
8136 if (mbox_pending)
James Smart32517fc2019-01-28 11:14:33 -08008137 /* process and rearm the EQ */
James Smart93a4d6f2019-11-04 16:57:05 -08008138 lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
James Smart32517fc2019-01-28 11:14:33 -08008139 else
8140 /* Always clear and re-arm the EQ */
8141 sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04008142
8143 return mbox_pending;
8144
8145}
James Smarte59058c2008-08-24 21:49:00 -04008146
8147/**
James Smart3621a712009-04-06 18:47:14 -04008148 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04008149 * @phba: Pointer to HBA context object.
8150 *
8151 * This function is called from worker thread when a mailbox command times out.
8152 * The caller is not required to hold any locks. This function will reset the
8153 * HBA and recover all the pending commands.
8154 **/
dea31012005-04-17 16:05:31 -05008155void
8156lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
8157{
James Smart2e0fef82007-06-17 19:56:36 -05008158 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04008159 MAILBOX_t *mb = NULL;
8160
James Smart1dcb58e2007-04-25 09:51:30 -04008161 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008162
James Smarte8d3c3b2013-10-10 12:21:30 -04008163 /* If the mailbox completed, process the completion and return */
8164 if (lpfc_sli4_process_missed_mbox_completions(phba))
8165 return;
8166
James Smarteb016562014-09-03 12:58:06 -04008167 if (pmbox != NULL)
8168 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04008169 /* Check the pmbox pointer first. There is a race condition
8170 * between the mbox timeout handler getting executed in the
8171 * worklist and the mailbox actually completing. When this
8172 * race condition occurs, the mbox_active will be NULL.
8173 */
8174 spin_lock_irq(&phba->hbalock);
8175 if (pmbox == NULL) {
8176 lpfc_printf_log(phba, KERN_WARNING,
8177 LOG_MBOX | LOG_SLI,
8178 "0353 Active Mailbox cleared - mailbox timeout "
8179 "exiting\n");
8180 spin_unlock_irq(&phba->hbalock);
8181 return;
8182 }
8183
dea31012005-04-17 16:05:31 -05008184 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05008185 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart32350662019-08-14 16:57:06 -07008186 "0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008187 mb->mbxCommand,
8188 phba->pport->port_state,
8189 phba->sli.sli_flag,
8190 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04008191 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008192
James Smart1dcb58e2007-04-25 09:51:30 -04008193 /* Setting state unknown so lpfc_sli_abort_iocb_ring
8194 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008195 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04008196 */
James Smart2e0fef82007-06-17 19:56:36 -05008197 spin_lock_irq(&phba->pport->work_port_lock);
8198 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8199 spin_unlock_irq(&phba->pport->work_port_lock);
8200 spin_lock_irq(&phba->hbalock);
8201 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04008202 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008203 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04008204
James Smartdb55fba2014-04-04 13:52:02 -04008205 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04008206
8207 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04008208 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04008209
8210 /* Reset the HBA device */
8211 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05008212}
8213
James Smarte59058c2008-08-24 21:49:00 -04008214/**
James Smart3772a992009-05-22 14:50:54 -04008215 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04008216 * @phba: Pointer to HBA context object.
8217 * @pmbox: Pointer to mailbox object.
8218 * @flag: Flag indicating how the mailbox need to be processed.
8219 *
8220 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04008221 * to submit a mailbox command to firmware with SLI-3 interface spec. This
8222 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04008223 * The mailbox command can be submitted in polling mode, in which case
8224 * this function will wait in a polling loop for the completion of the
8225 * mailbox.
8226 * If the mailbox is submitted in no_wait mode (not polling) the
8227 * function will submit the command and returns immediately without waiting
8228 * for the mailbox completion. The no_wait is supported only when HBA
8229 * is in SLI2/SLI3 mode - interrupts are enabled.
8230 * The SLI interface allows only one mailbox pending at a time. If the
8231 * mailbox is issued in polling mode and there is already a mailbox
8232 * pending, then the function will return an error. If the mailbox is issued
8233 * in NO_WAIT mode and there is a mailbox pending already, the function
8234 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
8235 * The sli layer owns the mailbox object until the completion of mailbox
8236 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
8237 * return codes the caller owns the mailbox command after the return of
8238 * the function.
8239 **/
James Smart3772a992009-05-22 14:50:54 -04008240static int
8241lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
8242 uint32_t flag)
dea31012005-04-17 16:05:31 -05008243{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008244 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05008245 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008246 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05008247 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008248 int i;
James Smart09372822008-01-11 01:52:54 -05008249 unsigned long timeout;
dea31012005-04-17 16:05:31 -05008250 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04008251 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05008252 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04008253 int processing_queue = 0;
8254
8255 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8256 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04008257 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04008258 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04008259 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8260 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8261 return MBX_SUCCESS;
8262 }
James Smart58da1ff2008-04-07 10:15:56 -04008263 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04008264 pmbox = lpfc_mbox_get(phba);
8265 if (!pmbox) {
8266 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8267 return MBX_SUCCESS;
8268 }
8269 }
dea31012005-04-17 16:05:31 -05008270
James Smarted957682007-06-17 19:56:37 -05008271 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05008272 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05008273 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04008274 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05008275 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05008276 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008277 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04008278 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05008279 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04008280 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05008281 }
8282 }
8283
Linas Vepstas8d63f372007-02-14 14:28:36 -06008284 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04008285 if (unlikely(pci_channel_offline(phba->pcidev))) {
8286 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8287 goto out_not_finished;
8288 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008289
James Smarta257bf92009-04-06 18:48:10 -04008290 /* If HBA has a deferred error attention, fail the iocb. */
8291 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8292 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8293 goto out_not_finished;
8294 }
8295
dea31012005-04-17 16:05:31 -05008296 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05008297
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008298 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05008299 status = MBX_SUCCESS;
8300
James Smart2e0fef82007-06-17 19:56:36 -05008301 if (phba->link_state == LPFC_HBA_ERROR) {
8302 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05008303
8304 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008305 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8306 "(%d):0311 Mailbox command x%x cannot "
8307 "issue Data: x%x x%x\n",
8308 pmbox->vport ? pmbox->vport->vpi : 0,
8309 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008310 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05008311 }
8312
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008313 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05008314 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
8315 !(hc_copy & HC_MBINT_ENA)) {
8316 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8317 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04008318 "(%d):2528 Mailbox command x%x cannot "
8319 "issue Data: x%x x%x\n",
8320 pmbox->vport ? pmbox->vport->vpi : 0,
8321 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05008322 goto out_not_finished;
8323 }
James Smart92908312006-03-07 15:04:13 -05008324 }
8325
dea31012005-04-17 16:05:31 -05008326 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8327 /* Polling for a mbox command when another one is already active
8328 * is not allowed in SLI. Also, the driver must have established
8329 * SLI2 mode to queue and process multiple mbox commands.
8330 */
8331
8332 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05008333 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008334
8335 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008336 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8337 "(%d):2529 Mailbox command x%x "
8338 "cannot issue Data: x%x x%x\n",
8339 pmbox->vport ? pmbox->vport->vpi : 0,
8340 pmbox->u.mb.mbxCommand,
8341 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008342 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008343 }
8344
James Smart3772a992009-05-22 14:50:54 -04008345 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05008346 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008347 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008348 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8349 "(%d):2530 Mailbox command x%x "
8350 "cannot issue Data: x%x x%x\n",
8351 pmbox->vport ? pmbox->vport->vpi : 0,
8352 pmbox->u.mb.mbxCommand,
8353 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008354 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008355 }
8356
dea31012005-04-17 16:05:31 -05008357 /* Another mailbox command is still being processed, queue this
8358 * command to be processed later.
8359 */
8360 lpfc_mbox_put(phba, pmbox);
8361
8362 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05008363 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008364 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008365 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008366 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07008367 mbx->mbxCommand,
8368 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008369 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008370
8371 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05008372 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008373
James Smart858c9f62007-06-17 19:56:39 -05008374 if (pmbox->vport) {
8375 lpfc_debugfs_disc_trc(pmbox->vport,
8376 LPFC_DISC_TRC_MBOX_VPORT,
8377 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008378 (uint32_t)mbx->mbxCommand,
8379 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008380 }
8381 else {
8382 lpfc_debugfs_disc_trc(phba->pport,
8383 LPFC_DISC_TRC_MBOX,
8384 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008385 (uint32_t)mbx->mbxCommand,
8386 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008387 }
8388
James Smart2e0fef82007-06-17 19:56:36 -05008389 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05008390 }
8391
dea31012005-04-17 16:05:31 -05008392 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8393
8394 /* If we are not polling, we MUST be in SLI2 mode */
8395 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04008396 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008397 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05008398 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008399 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008400 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04008401 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8402 "(%d):2531 Mailbox command x%x "
8403 "cannot issue Data: x%x x%x\n",
8404 pmbox->vport ? pmbox->vport->vpi : 0,
8405 pmbox->u.mb.mbxCommand,
8406 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008407 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008408 }
8409 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04008410 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8411 1000);
8412 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05008413 }
8414
8415 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05008416 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008417 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05008418 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008419 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07008420 mbx->mbxCommand,
8421 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008422 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008423
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008424 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05008425 if (pmbox->vport) {
8426 lpfc_debugfs_disc_trc(pmbox->vport,
8427 LPFC_DISC_TRC_MBOX_VPORT,
8428 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008429 (uint32_t)mbx->mbxCommand,
8430 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008431 }
8432 else {
8433 lpfc_debugfs_disc_trc(phba->pport,
8434 LPFC_DISC_TRC_MBOX,
8435 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008436 (uint32_t)mbx->mbxCommand,
8437 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008438 }
8439 }
8440
dea31012005-04-17 16:05:31 -05008441 psli->slistat.mbox_cmd++;
8442 evtctr = psli->slistat.mbox_event;
8443
8444 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008445 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05008446
James Smart3772a992009-05-22 14:50:54 -04008447 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04008448 /* Populate mbox extension offset word. */
8449 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008450 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008451 = (uint8_t *)phba->mbox_ext
8452 - (uint8_t *)phba->mbox;
8453 }
8454
8455 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008456 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
8457 lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
8458 (uint8_t *)phba->mbox_ext,
8459 pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008460 }
8461 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008462 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008463 } else {
James Smart7a470272010-03-15 11:25:20 -04008464 /* Populate mbox extension offset word. */
8465 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008466 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008467 = MAILBOX_HBA_EXT_OFFSET;
8468
8469 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008470 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -04008471 lpfc_memcpy_to_slim(phba->MBslimaddr +
8472 MAILBOX_HBA_EXT_OFFSET,
James Smart3e1f0712018-11-29 16:09:29 -08008473 pmbox->ctx_buf, pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008474
James Smart895427b2017-02-12 13:52:30 -08008475 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008476 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08008477 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8478 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008479
8480 /* First copy mbox command data to HBA SLIM, skip past first
8481 word */
8482 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008483 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05008484 MAILBOX_CMD_SIZE - sizeof (uint32_t));
8485
8486 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008487 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05008488 to_slim = phba->MBslimaddr;
8489 writel(ldata, to_slim);
8490 readl(to_slim); /* flush */
8491
James Smart895427b2017-02-12 13:52:30 -08008492 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008493 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04008494 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008495 }
8496
8497 wmb();
dea31012005-04-17 16:05:31 -05008498
8499 switch (flag) {
8500 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05008501 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05008502 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05008503 /* Interrupt board to do it */
8504 writel(CA_MBATT, phba->CAregaddr);
8505 readl(phba->CAregaddr); /* flush */
8506 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05008507 break;
8508
8509 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05008510 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05008511 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05008512 /* Interrupt board to do it */
8513 writel(CA_MBATT, phba->CAregaddr);
8514 readl(phba->CAregaddr); /* flush */
8515
James Smart3772a992009-05-22 14:50:54 -04008516 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008517 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04008518 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008519 word0 = le32_to_cpu(word0);
8520 } else {
8521 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05008522 if (lpfc_readl(phba->MBslimaddr, &word0)) {
8523 spin_unlock_irqrestore(&phba->hbalock,
8524 drvr_flag);
8525 goto out_not_finished;
8526 }
dea31012005-04-17 16:05:31 -05008527 }
8528
8529 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008530 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8531 spin_unlock_irqrestore(&phba->hbalock,
8532 drvr_flag);
8533 goto out_not_finished;
8534 }
James Smarta183a152011-10-10 21:32:43 -04008535 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8536 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05008537 i = 0;
dea31012005-04-17 16:05:31 -05008538 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05008539 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8540 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05008541 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05008542 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05008543 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008544 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05008545 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04008546 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008547 }
8548
8549 /* Check if we took a mbox interrupt while we were
8550 polling */
8551 if (((word0 & OWN_CHIP) != OWN_CHIP)
8552 && (evtctr != psli->slistat.mbox_event))
8553 break;
8554
James Smart09372822008-01-11 01:52:54 -05008555 if (i++ > 10) {
8556 spin_unlock_irqrestore(&phba->hbalock,
8557 drvr_flag);
8558 msleep(1);
8559 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8560 }
dea31012005-04-17 16:05:31 -05008561
James Smart3772a992009-05-22 14:50:54 -04008562 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008563 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04008564 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008565 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008566 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05008567 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04008568 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05008569 /* Check real SLIM for any errors */
8570 slimword0 = readl(phba->MBslimaddr);
8571 slimmb = (MAILBOX_t *) & slimword0;
8572 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8573 && slimmb->mbxStatus) {
8574 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04008575 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008576 word0 = slimword0;
8577 }
8578 }
8579 } else {
8580 /* First copy command data */
8581 word0 = readl(phba->MBslimaddr);
8582 }
8583 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008584 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8585 spin_unlock_irqrestore(&phba->hbalock,
8586 drvr_flag);
8587 goto out_not_finished;
8588 }
dea31012005-04-17 16:05:31 -05008589 }
8590
James Smart3772a992009-05-22 14:50:54 -04008591 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008592 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08008593 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8594 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008595 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008596 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
James Smart7a470272010-03-15 11:25:20 -04008597 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -08008598 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008599 pmbox->out_ext_byte_len);
8600 }
dea31012005-04-17 16:05:31 -05008601 } else {
8602 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008603 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08008604 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008605 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008606 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8607 lpfc_memcpy_from_slim(
8608 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008609 phba->MBslimaddr +
8610 MAILBOX_HBA_EXT_OFFSET,
8611 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05008612 }
8613 }
8614
8615 writel(HA_MBATT, phba->HAregaddr);
8616 readl(phba->HAregaddr); /* flush */
8617
8618 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008619 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05008620 }
8621
James Smart2e0fef82007-06-17 19:56:36 -05008622 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8623 return status;
James Smart58da1ff2008-04-07 10:15:56 -04008624
8625out_not_finished:
8626 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04008627 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04008628 lpfc_mbox_cmpl_put(phba, pmbox);
8629 }
8630 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008631}
8632
James Smarte59058c2008-08-24 21:49:00 -04008633/**
James Smartf1126682009-06-10 17:22:44 -04008634 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8635 * @phba: Pointer to HBA context object.
8636 *
8637 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8638 * the driver internal pending mailbox queue. It will then try to wait out the
8639 * possible outstanding mailbox command before return.
8640 *
8641 * Returns:
8642 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8643 * the outstanding mailbox command timed out.
8644 **/
8645static int
8646lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8647{
8648 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008649 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008650 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008651
8652 /* Mark the asynchronous mailbox command posting as blocked */
8653 spin_lock_irq(&phba->hbalock);
8654 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008655 /* Determine how long we might wait for the active mailbox
8656 * command to be gracefully completed by firmware.
8657 */
James Smarta183a152011-10-10 21:32:43 -04008658 if (phba->sli.mbox_active)
8659 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8660 phba->sli.mbox_active) *
8661 1000) + jiffies;
8662 spin_unlock_irq(&phba->hbalock);
8663
James Smarte8d3c3b2013-10-10 12:21:30 -04008664 /* Make sure the mailbox is really active */
8665 if (timeout)
8666 lpfc_sli4_process_missed_mbox_completions(phba);
8667
James Smartf1126682009-06-10 17:22:44 -04008668 /* Wait for the outstnading mailbox command to complete */
8669 while (phba->sli.mbox_active) {
8670 /* Check active mailbox complete status every 2ms */
8671 msleep(2);
8672 if (time_after(jiffies, timeout)) {
8673 /* Timeout, marked the outstanding cmd not complete */
8674 rc = 1;
8675 break;
8676 }
8677 }
8678
8679 /* Can not cleanly block async mailbox command, fails it */
8680 if (rc) {
8681 spin_lock_irq(&phba->hbalock);
8682 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8683 spin_unlock_irq(&phba->hbalock);
8684 }
8685 return rc;
8686}
8687
8688/**
8689 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8690 * @phba: Pointer to HBA context object.
8691 *
8692 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8693 * commands from the driver internal pending mailbox queue. It makes sure
8694 * that there is no outstanding mailbox command before resuming posting
8695 * asynchronous mailbox commands. If, for any reason, there is outstanding
8696 * mailbox command, it will try to wait it out before resuming asynchronous
8697 * mailbox command posting.
8698 **/
8699static void
8700lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8701{
8702 struct lpfc_sli *psli = &phba->sli;
8703
8704 spin_lock_irq(&phba->hbalock);
8705 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8706 /* Asynchronous mailbox posting is not blocked, do nothing */
8707 spin_unlock_irq(&phba->hbalock);
8708 return;
8709 }
8710
8711 /* Outstanding synchronous mailbox command is guaranteed to be done,
8712 * successful or timeout, after timing-out the outstanding mailbox
8713 * command shall always be removed, so just unblock posting async
8714 * mailbox command and resume
8715 */
8716 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8717 spin_unlock_irq(&phba->hbalock);
8718
Colin Ian King291c2542019-12-18 08:43:01 +00008719 /* wake up worker thread to post asynchronous mailbox command */
James Smartf1126682009-06-10 17:22:44 -04008720 lpfc_worker_wake_up(phba);
8721}
8722
8723/**
James Smart2d843ed2012-09-29 11:29:06 -04008724 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8725 * @phba: Pointer to HBA context object.
8726 * @mboxq: Pointer to mailbox object.
8727 *
8728 * The function waits for the bootstrap mailbox register ready bit from
8729 * port for twice the regular mailbox command timeout value.
8730 *
8731 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8732 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8733 **/
8734static int
8735lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8736{
8737 uint32_t db_ready;
8738 unsigned long timeout;
8739 struct lpfc_register bmbx_reg;
8740
8741 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8742 * 1000) + jiffies;
8743
8744 do {
8745 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8746 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8747 if (!db_ready)
James Smarte2ffe4d2019-03-12 16:30:15 -07008748 mdelay(2);
James Smart2d843ed2012-09-29 11:29:06 -04008749
8750 if (time_after(jiffies, timeout))
8751 return MBXERR_ERROR;
8752 } while (!db_ready);
8753
8754 return 0;
8755}
8756
8757/**
James Smartda0436e2009-05-22 14:51:39 -04008758 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8759 * @phba: Pointer to HBA context object.
8760 * @mboxq: Pointer to mailbox object.
8761 *
8762 * The function posts a mailbox to the port. The mailbox is expected
8763 * to be comletely filled in and ready for the port to operate on it.
8764 * This routine executes a synchronous completion operation on the
8765 * mailbox by polling for its completion.
8766 *
8767 * The caller must not be holding any locks when calling this routine.
8768 *
8769 * Returns:
8770 * MBX_SUCCESS - mailbox posted successfully
8771 * Any of the MBX error values.
8772 **/
8773static int
8774lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8775{
8776 int rc = MBX_SUCCESS;
8777 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008778 uint32_t mcqe_status;
8779 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008780 struct lpfc_sli *psli = &phba->sli;
8781 struct lpfc_mqe *mb = &mboxq->u.mqe;
8782 struct lpfc_bmbx_create *mbox_rgn;
8783 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008784
8785 /*
8786 * Only one mailbox can be active to the bootstrap mailbox region
8787 * at a time and there is no queueing provided.
8788 */
8789 spin_lock_irqsave(&phba->hbalock, iflag);
8790 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8791 spin_unlock_irqrestore(&phba->hbalock, iflag);
8792 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008793 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008794 "cannot issue Data: x%x x%x\n",
8795 mboxq->vport ? mboxq->vport->vpi : 0,
8796 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008797 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8798 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008799 psli->sli_flag, MBX_POLL);
8800 return MBXERR_ERROR;
8801 }
8802 /* The server grabs the token and owns it until release */
8803 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8804 phba->sli.mbox_active = mboxq;
8805 spin_unlock_irqrestore(&phba->hbalock, iflag);
8806
James Smart2d843ed2012-09-29 11:29:06 -04008807 /* wait for bootstrap mbox register for readyness */
8808 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8809 if (rc)
8810 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008811 /*
8812 * Initialize the bootstrap memory region to avoid stale data areas
8813 * in the mailbox post. Then copy the caller's mailbox contents to
8814 * the bmbx mailbox region.
8815 */
8816 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8817 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07008818 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8819 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008820
8821 /* Post the high mailbox dma address to the port and wait for ready. */
8822 dma_address = &phba->sli4_hba.bmbx.dma_address;
8823 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8824
James Smart2d843ed2012-09-29 11:29:06 -04008825 /* wait for bootstrap mbox register for hi-address write done */
8826 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8827 if (rc)
8828 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008829
8830 /* Post the low mailbox dma address to the port. */
8831 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008832
James Smart2d843ed2012-09-29 11:29:06 -04008833 /* wait for bootstrap mbox register for low address write done */
8834 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8835 if (rc)
8836 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008837
8838 /*
8839 * Read the CQ to ensure the mailbox has completed.
8840 * If so, update the mailbox status so that the upper layers
8841 * can complete the request normally.
8842 */
James Smart48f8fdb2018-05-04 20:37:51 -07008843 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8844 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008845 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07008846 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8847 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04008848 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008849 /*
8850 * When the CQE status indicates a failure and the mailbox status
8851 * indicates success then copy the CQE status into the mailbox status
8852 * (and prefix it with x4000).
8853 */
James Smartda0436e2009-05-22 14:51:39 -04008854 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008855 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8856 bf_set(lpfc_mqe_status, mb,
8857 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008858 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008859 } else
8860 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008861
8862 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008863 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008864 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8865 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008866 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8867 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8868 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008869 bf_get(lpfc_mqe_status, mb),
8870 mb->un.mb_words[0], mb->un.mb_words[1],
8871 mb->un.mb_words[2], mb->un.mb_words[3],
8872 mb->un.mb_words[4], mb->un.mb_words[5],
8873 mb->un.mb_words[6], mb->un.mb_words[7],
8874 mb->un.mb_words[8], mb->un.mb_words[9],
8875 mb->un.mb_words[10], mb->un.mb_words[11],
8876 mb->un.mb_words[12], mboxq->mcqe.word0,
8877 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8878 mboxq->mcqe.trailer);
8879exit:
8880 /* We are holding the token, no needed for lock when release */
8881 spin_lock_irqsave(&phba->hbalock, iflag);
8882 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8883 phba->sli.mbox_active = NULL;
8884 spin_unlock_irqrestore(&phba->hbalock, iflag);
8885 return rc;
8886}
8887
8888/**
8889 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8890 * @phba: Pointer to HBA context object.
8891 * @pmbox: Pointer to mailbox object.
8892 * @flag: Flag indicating how the mailbox need to be processed.
8893 *
8894 * This function is called by discovery code and HBA management code to submit
8895 * a mailbox command to firmware with SLI-4 interface spec.
8896 *
8897 * Return codes the caller owns the mailbox command after the return of the
8898 * function.
8899 **/
8900static int
8901lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8902 uint32_t flag)
8903{
8904 struct lpfc_sli *psli = &phba->sli;
8905 unsigned long iflags;
8906 int rc;
8907
James Smartb76f2dc2011-07-22 18:37:42 -04008908 /* dump from issue mailbox command if setup */
8909 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8910
James Smart8fa38512009-07-19 10:01:03 -04008911 rc = lpfc_mbox_dev_check(phba);
8912 if (unlikely(rc)) {
8913 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008914 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008915 "cannot issue Data: x%x x%x\n",
8916 mboxq->vport ? mboxq->vport->vpi : 0,
8917 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008918 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8919 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008920 psli->sli_flag, flag);
8921 goto out_not_finished;
8922 }
8923
James Smartda0436e2009-05-22 14:51:39 -04008924 /* Detect polling mode and jump to a handler */
8925 if (!phba->sli4_hba.intr_enable) {
8926 if (flag == MBX_POLL)
8927 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8928 else
8929 rc = -EIO;
8930 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008931 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008932 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008933 "(x%x/x%x) failure: "
8934 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8935 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008936 mboxq->vport ? mboxq->vport->vpi : 0,
8937 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008938 lpfc_sli_config_mbox_subsys_get(phba,
8939 mboxq),
8940 lpfc_sli_config_mbox_opcode_get(phba,
8941 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008942 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8943 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8944 bf_get(lpfc_mcqe_ext_status,
8945 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008946 psli->sli_flag, flag);
8947 return rc;
8948 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008949 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8950 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08008951 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04008952 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008953 mboxq->vport ? mboxq->vport->vpi : 0,
8954 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008955 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8956 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008957 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008958 /* Try to block the asynchronous mailbox posting */
8959 rc = lpfc_sli4_async_mbox_block(phba);
8960 if (!rc) {
8961 /* Successfully blocked, now issue sync mbox cmd */
8962 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8963 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008964 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008965 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008966 "(%d):2597 Sync Mailbox command "
8967 "x%x (x%x/x%x) failure: "
8968 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8969 "Data: x%x x%x\n,",
8970 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008971 mboxq->u.mb.mbxCommand,
8972 lpfc_sli_config_mbox_subsys_get(phba,
8973 mboxq),
8974 lpfc_sli_config_mbox_opcode_get(phba,
8975 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008976 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8977 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8978 bf_get(lpfc_mcqe_ext_status,
8979 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008980 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008981 /* Unblock the async mailbox posting afterward */
8982 lpfc_sli4_async_mbox_unblock(phba);
8983 }
8984 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008985 }
8986
Colin Ian King291c2542019-12-18 08:43:01 +00008987 /* Now, interrupt mode asynchronous mailbox command */
James Smartda0436e2009-05-22 14:51:39 -04008988 rc = lpfc_mbox_cmd_check(phba, mboxq);
8989 if (rc) {
8990 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008991 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008992 "cannot issue Data: x%x x%x\n",
8993 mboxq->vport ? mboxq->vport->vpi : 0,
8994 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008995 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8996 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008997 psli->sli_flag, flag);
8998 goto out_not_finished;
8999 }
James Smartda0436e2009-05-22 14:51:39 -04009000
9001 /* Put the mailbox command to the driver internal FIFO */
9002 psli->slistat.mbox_busy++;
9003 spin_lock_irqsave(&phba->hbalock, iflags);
9004 lpfc_mbox_put(phba, mboxq);
9005 spin_unlock_irqrestore(&phba->hbalock, iflags);
9006 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9007 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04009008 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04009009 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
9010 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04009011 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9012 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009013 phba->pport->port_state,
9014 psli->sli_flag, MBX_NOWAIT);
9015 /* Wake up worker thread to transport mailbox command from head */
9016 lpfc_worker_wake_up(phba);
9017
9018 return MBX_BUSY;
9019
9020out_not_finished:
9021 return MBX_NOT_FINISHED;
9022}
9023
9024/**
9025 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
9026 * @phba: Pointer to HBA context object.
9027 *
9028 * This function is called by worker thread to send a mailbox command to
9029 * SLI4 HBA firmware.
9030 *
9031 **/
9032int
9033lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
9034{
9035 struct lpfc_sli *psli = &phba->sli;
9036 LPFC_MBOXQ_t *mboxq;
9037 int rc = MBX_SUCCESS;
9038 unsigned long iflags;
9039 struct lpfc_mqe *mqe;
9040 uint32_t mbx_cmnd;
9041
9042 /* Check interrupt mode before post async mailbox command */
9043 if (unlikely(!phba->sli4_hba.intr_enable))
9044 return MBX_NOT_FINISHED;
9045
9046 /* Check for mailbox command service token */
9047 spin_lock_irqsave(&phba->hbalock, iflags);
9048 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9049 spin_unlock_irqrestore(&phba->hbalock, iflags);
9050 return MBX_NOT_FINISHED;
9051 }
9052 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9053 spin_unlock_irqrestore(&phba->hbalock, iflags);
9054 return MBX_NOT_FINISHED;
9055 }
9056 if (unlikely(phba->sli.mbox_active)) {
9057 spin_unlock_irqrestore(&phba->hbalock, iflags);
9058 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
9059 "0384 There is pending active mailbox cmd\n");
9060 return MBX_NOT_FINISHED;
9061 }
9062 /* Take the mailbox command service token */
9063 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9064
9065 /* Get the next mailbox command from head of queue */
9066 mboxq = lpfc_mbox_get(phba);
9067
9068 /* If no more mailbox command waiting for post, we're done */
9069 if (!mboxq) {
9070 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9071 spin_unlock_irqrestore(&phba->hbalock, iflags);
9072 return MBX_SUCCESS;
9073 }
9074 phba->sli.mbox_active = mboxq;
9075 spin_unlock_irqrestore(&phba->hbalock, iflags);
9076
9077 /* Check device readiness for posting mailbox command */
9078 rc = lpfc_mbox_dev_check(phba);
9079 if (unlikely(rc))
9080 /* Driver clean routine will clean up pending mailbox */
9081 goto out_not_finished;
9082
9083 /* Prepare the mbox command to be posted */
9084 mqe = &mboxq->u.mqe;
9085 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
9086
9087 /* Start timer for the mbox_tmo and log some mailbox post messages */
9088 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04009089 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04009090
9091 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04009092 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04009093 "x%x x%x\n",
9094 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04009095 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9096 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009097 phba->pport->port_state, psli->sli_flag);
9098
9099 if (mbx_cmnd != MBX_HEARTBEAT) {
9100 if (mboxq->vport) {
9101 lpfc_debugfs_disc_trc(mboxq->vport,
9102 LPFC_DISC_TRC_MBOX_VPORT,
9103 "MBOX Send vport: cmd:x%x mb:x%x x%x",
9104 mbx_cmnd, mqe->un.mb_words[0],
9105 mqe->un.mb_words[1]);
9106 } else {
9107 lpfc_debugfs_disc_trc(phba->pport,
9108 LPFC_DISC_TRC_MBOX,
9109 "MBOX Send: cmd:x%x mb:x%x x%x",
9110 mbx_cmnd, mqe->un.mb_words[0],
9111 mqe->un.mb_words[1]);
9112 }
9113 }
9114 psli->slistat.mbox_cmd++;
9115
9116 /* Post the mailbox command to the port */
9117 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
9118 if (rc != MBX_SUCCESS) {
9119 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04009120 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04009121 "cannot issue Data: x%x x%x\n",
9122 mboxq->vport ? mboxq->vport->vpi : 0,
9123 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009124 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9125 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009126 psli->sli_flag, MBX_NOWAIT);
9127 goto out_not_finished;
9128 }
9129
9130 return rc;
9131
9132out_not_finished:
9133 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05009134 if (phba->sli.mbox_active) {
9135 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9136 __lpfc_mbox_cmpl_put(phba, mboxq);
9137 /* Release the token */
9138 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9139 phba->sli.mbox_active = NULL;
9140 }
James Smartda0436e2009-05-22 14:51:39 -04009141 spin_unlock_irqrestore(&phba->hbalock, iflags);
9142
9143 return MBX_NOT_FINISHED;
9144}
9145
9146/**
9147 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
9148 * @phba: Pointer to HBA context object.
9149 * @pmbox: Pointer to mailbox object.
9150 * @flag: Flag indicating how the mailbox need to be processed.
9151 *
9152 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
9153 * the API jump table function pointer from the lpfc_hba struct.
9154 *
9155 * Return codes the caller owns the mailbox command after the return of the
9156 * function.
9157 **/
9158int
9159lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
9160{
9161 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
9162}
9163
9164/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009165 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04009166 * @phba: The hba struct for which this call is being executed.
9167 * @dev_grp: The HBA PCI-Device group number.
9168 *
9169 * This routine sets up the mbox interface API function jump table in @phba
9170 * struct.
9171 * Returns: 0 - success, -ENODEV - failure.
9172 **/
9173int
9174lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9175{
9176
9177 switch (dev_grp) {
9178 case LPFC_PCI_DEV_LP:
9179 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
9180 phba->lpfc_sli_handle_slow_ring_event =
9181 lpfc_sli_handle_slow_ring_event_s3;
9182 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
9183 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
9184 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
9185 break;
9186 case LPFC_PCI_DEV_OC:
9187 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
9188 phba->lpfc_sli_handle_slow_ring_event =
9189 lpfc_sli_handle_slow_ring_event_s4;
9190 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
9191 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
9192 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
9193 break;
9194 default:
9195 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9196 "1420 Invalid HBA PCI-device group: 0x%x\n",
9197 dev_grp);
9198 return -ENODEV;
9199 break;
9200 }
9201 return 0;
9202}
9203
9204/**
James Smart3621a712009-04-06 18:47:14 -04009205 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04009206 * @phba: Pointer to HBA context object.
9207 * @pring: Pointer to driver SLI ring object.
9208 * @piocb: Pointer to address of newly added command iocb.
9209 *
James Smart27f3efd2019-10-18 14:18:19 -07009210 * This function is called with hbalock held for SLI3 ports or
9211 * the ring lock held for SLI4 ports to add a command
James Smarte59058c2008-08-24 21:49:00 -04009212 * iocb to the txq when SLI layer cannot submit the command iocb
9213 * to the ring.
9214 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009215void
James Smart92d7f7b2007-06-17 19:56:38 -05009216__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009217 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05009218{
James Smart27f3efd2019-10-18 14:18:19 -07009219 if (phba->sli_rev == LPFC_SLI_REV4)
9220 lockdep_assert_held(&pring->ring_lock);
9221 else
9222 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009223 /* Insert the caller's iocb in the txq tail for later processing. */
9224 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05009225}
9226
James Smarte59058c2008-08-24 21:49:00 -04009227/**
James Smart3621a712009-04-06 18:47:14 -04009228 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04009229 * @phba: Pointer to HBA context object.
9230 * @pring: Pointer to driver SLI ring object.
9231 * @piocb: Pointer to address of newly added command iocb.
9232 *
9233 * This function is called with hbalock held before a new
9234 * iocb is submitted to the firmware. This function checks
9235 * txq to flush the iocbs in txq to Firmware before
9236 * submitting new iocbs to the Firmware.
9237 * If there are iocbs in the txq which need to be submitted
9238 * to firmware, lpfc_sli_next_iocb returns the first element
9239 * of the txq after dequeuing it from txq.
9240 * If there is no iocb in the txq then the function will return
9241 * *piocb and *piocb is set to NULL. Caller needs to check
9242 * *piocb to find if there are more commands in the txq.
9243 **/
dea31012005-04-17 16:05:31 -05009244static struct lpfc_iocbq *
9245lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009246 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05009247{
9248 struct lpfc_iocbq * nextiocb;
9249
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009250 lockdep_assert_held(&phba->hbalock);
9251
dea31012005-04-17 16:05:31 -05009252 nextiocb = lpfc_sli_ringtx_get(phba, pring);
9253 if (!nextiocb) {
9254 nextiocb = *piocb;
9255 *piocb = NULL;
9256 }
9257
9258 return nextiocb;
9259}
9260
James Smarte59058c2008-08-24 21:49:00 -04009261/**
James Smart3772a992009-05-22 14:50:54 -04009262 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009263 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04009264 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04009265 * @piocb: Pointer to command iocb.
9266 * @flag: Flag indicating if this command can be put into txq.
9267 *
James Smart3772a992009-05-22 14:50:54 -04009268 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
9269 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
9270 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
9271 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
9272 * this function allows only iocbs for posting buffers. This function finds
9273 * next available slot in the command ring and posts the command to the
9274 * available slot and writes the port attention register to request HBA start
9275 * processing new iocb. If there is no slot available in the ring and
9276 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
9277 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04009278 *
James Smart3772a992009-05-22 14:50:54 -04009279 * This function is called with hbalock held. The function will return success
9280 * after it successfully submit the iocb to firmware or after adding to the
9281 * txq.
James Smarte59058c2008-08-24 21:49:00 -04009282 **/
James Smart98c9ea52007-10-27 13:37:33 -04009283static int
James Smart3772a992009-05-22 14:50:54 -04009284__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05009285 struct lpfc_iocbq *piocb, uint32_t flag)
9286{
9287 struct lpfc_iocbq *nextiocb;
9288 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08009289 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05009290
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009291 lockdep_assert_held(&phba->hbalock);
9292
James Smart92d7f7b2007-06-17 19:56:38 -05009293 if (piocb->iocb_cmpl && (!piocb->vport) &&
9294 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
9295 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
9296 lpfc_printf_log(phba, KERN_ERR,
9297 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04009298 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05009299 piocb->iocb.ulpCommand);
9300 dump_stack();
9301 return IOCB_ERROR;
9302 }
9303
9304
Linas Vepstas8d63f372007-02-14 14:28:36 -06009305 /* If the PCI channel is in offline state, do not post iocbs. */
9306 if (unlikely(pci_channel_offline(phba->pcidev)))
9307 return IOCB_ERROR;
9308
James Smarta257bf92009-04-06 18:48:10 -04009309 /* If HBA has a deferred error attention, fail the iocb. */
9310 if (unlikely(phba->hba_flag & DEFER_ERATT))
9311 return IOCB_ERROR;
9312
dea31012005-04-17 16:05:31 -05009313 /*
9314 * We should never get an IOCB if we are in a < LINK_DOWN state
9315 */
James Smart2e0fef82007-06-17 19:56:36 -05009316 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05009317 return IOCB_ERROR;
9318
9319 /*
9320 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04009321 * outstanding event.
dea31012005-04-17 16:05:31 -05009322 */
James Smart0b727fe2007-10-27 13:37:25 -04009323 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05009324 goto iocb_busy;
9325
James Smart2e0fef82007-06-17 19:56:36 -05009326 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05009327 /*
James Smart2680eea2007-04-25 09:52:55 -04009328 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05009329 * can be issued if the link is not up.
9330 */
9331 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04009332 case CMD_GEN_REQUEST64_CR:
9333 case CMD_GEN_REQUEST64_CX:
9334 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9335 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04009336 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04009337 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9338 MENLO_TRANSPORT_TYPE))
9339
9340 goto iocb_busy;
9341 break;
dea31012005-04-17 16:05:31 -05009342 case CMD_QUE_RING_BUF_CN:
9343 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05009344 /*
9345 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9346 * completion, iocb_cmpl MUST be 0.
9347 */
9348 if (piocb->iocb_cmpl)
9349 piocb->iocb_cmpl = NULL;
9350 /*FALLTHROUGH*/
9351 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04009352 case CMD_CLOSE_XRI_CN:
9353 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05009354 break;
9355 default:
9356 goto iocb_busy;
9357 }
9358
9359 /*
9360 * For FCP commands, we must be in a state where we can process link
9361 * attention events.
9362 */
James Smart895427b2017-02-12 13:52:30 -08009363 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05009364 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05009365 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05009366 }
dea31012005-04-17 16:05:31 -05009367
dea31012005-04-17 16:05:31 -05009368 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9369 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9370 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9371
9372 if (iocb)
9373 lpfc_sli_update_ring(phba, pring);
9374 else
9375 lpfc_sli_update_full_ring(phba, pring);
9376
9377 if (!piocb)
9378 return IOCB_SUCCESS;
9379
9380 goto out_busy;
9381
9382 iocb_busy:
9383 pring->stats.iocb_cmd_delay++;
9384
9385 out_busy:
9386
9387 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05009388 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05009389 return IOCB_SUCCESS;
9390 }
9391
9392 return IOCB_BUSY;
9393}
9394
James Smart3772a992009-05-22 14:50:54 -04009395/**
James Smart4f774512009-05-22 14:52:35 -04009396 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9397 * @phba: Pointer to HBA context object.
9398 * @piocb: Pointer to command iocb.
9399 * @sglq: Pointer to the scatter gather queue object.
9400 *
9401 * This routine converts the bpl or bde that is in the IOCB
9402 * to a sgl list for the sli4 hardware. The physical address
9403 * of the bpl/bde is converted back to a virtual address.
9404 * If the IOCB contains a BPL then the list of BDE's is
9405 * converted to sli4_sge's. If the IOCB contains a single
9406 * BDE then it is converted to a single sli_sge.
9407 * The IOCB is still in cpu endianess so the contents of
9408 * the bpl can be used without byte swapping.
9409 *
9410 * Returns valid XRI = Success, NO_XRI = Failure.
9411**/
9412static uint16_t
9413lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9414 struct lpfc_sglq *sglq)
9415{
9416 uint16_t xritag = NO_XRI;
9417 struct ulp_bde64 *bpl = NULL;
9418 struct ulp_bde64 bde;
9419 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05009420 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04009421 IOCB_t *icmd;
9422 int numBdes = 0;
9423 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05009424 uint32_t offset = 0; /* accumulated offset in the sg request list */
9425 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04009426
9427 if (!piocbq || !sglq)
9428 return xritag;
9429
9430 sgl = (struct sli4_sge *)sglq->sgl;
9431 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05009432 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9433 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009434 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9435 numBdes = icmd->un.genreq64.bdl.bdeSize /
9436 sizeof(struct ulp_bde64);
9437 /* The addrHigh and addrLow fields within the IOCB
9438 * have not been byteswapped yet so there is no
9439 * need to swap them back.
9440 */
James Smart1b511972011-12-13 13:23:09 -05009441 if (piocbq->context3)
9442 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9443 else
9444 return xritag;
James Smart4f774512009-05-22 14:52:35 -04009445
James Smart1b511972011-12-13 13:23:09 -05009446 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04009447 if (!bpl)
9448 return xritag;
9449
9450 for (i = 0; i < numBdes; i++) {
9451 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05009452 sgl->addr_hi = bpl->addrHigh;
9453 sgl->addr_lo = bpl->addrLow;
9454
James Smart05580562011-05-24 11:40:48 -04009455 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009456 if ((i+1) == numBdes)
9457 bf_set(lpfc_sli4_sge_last, sgl, 1);
9458 else
9459 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05009460 /* swap the size field back to the cpu so we
9461 * can assign it to the sgl.
9462 */
9463 bde.tus.w = le32_to_cpu(bpl->tus.w);
9464 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05009465 /* The offsets in the sgl need to be accumulated
9466 * separately for the request and reply lists.
9467 * The request is always first, the reply follows.
9468 */
9469 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9470 /* add up the reply sg entries */
9471 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9472 inbound++;
9473 /* first inbound? reset the offset */
9474 if (inbound == 1)
9475 offset = 0;
9476 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04009477 bf_set(lpfc_sli4_sge_type, sgl,
9478 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05009479 offset += bde.tus.f.bdeSize;
9480 }
James Smart546fc852011-03-11 16:06:29 -05009481 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009482 bpl++;
9483 sgl++;
9484 }
9485 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9486 /* The addrHigh and addrLow fields of the BDE have not
9487 * been byteswapped yet so they need to be swapped
9488 * before putting them in the sgl.
9489 */
9490 sgl->addr_hi =
9491 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9492 sgl->addr_lo =
9493 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04009494 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009495 bf_set(lpfc_sli4_sge_last, sgl, 1);
9496 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05009497 sgl->sge_len =
9498 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04009499 }
9500 return sglq->sli4_xritag;
9501}
9502
9503/**
James Smart4f774512009-05-22 14:52:35 -04009504 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
9505 * @phba: Pointer to HBA context object.
9506 * @piocb: Pointer to command iocb.
9507 * @wqe: Pointer to the work queue entry.
9508 *
9509 * This routine converts the iocb command to its Work Queue Entry
9510 * equivalent. The wqe pointer should not have any fields set when
9511 * this routine is called because it will memcpy over them.
9512 * This routine does not set the CQ_ID or the WQEC bits in the
9513 * wqe.
9514 *
9515 * Returns: 0 = Success, IOCB_ERROR = Failure.
9516 **/
9517static int
9518lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -08009519 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -04009520{
James Smart5ffc2662009-11-18 15:39:44 -05009521 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04009522 uint8_t ct = 0;
9523 uint32_t fip;
9524 uint32_t abort_tag;
9525 uint8_t command_type = ELS_COMMAND_NON_FIP;
9526 uint8_t cmnd;
9527 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04009528 uint16_t abrt_iotag;
9529 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04009530 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04009531 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05009532 int numBdes, i;
9533 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04009534 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05009535 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05009536 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04009537
James Smart45ed1192009-10-02 15:17:02 -04009538 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04009539 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04009540 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04009541 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05009542 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04009543 command_type = ELS_COMMAND_FIP;
9544 else
9545 command_type = ELS_COMMAND_NON_FIP;
9546
James Smartb5c53952016-03-31 14:12:30 -07009547 if (phba->fcp_embed_io)
9548 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04009549 /* Some of the fields are in the right position already */
9550 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smarte62245d2019-08-14 16:57:08 -07009551 /* The ct field has moved so reset */
9552 wqe->generic.wqe_com.word7 = 0;
9553 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07009554
9555 abort_tag = (uint32_t) iocbq->iotag;
9556 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009557 /* words0-2 bpl convert bde */
9558 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05009559 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9560 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04009561 bpl = (struct ulp_bde64 *)
9562 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9563 if (!bpl)
9564 return IOCB_ERROR;
9565
9566 /* Should already be byte swapped. */
9567 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
9568 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
9569 /* swap the size field back to the cpu so we
9570 * can assign it to the sgl.
9571 */
9572 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05009573 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9574 total_len = 0;
9575 for (i = 0; i < numBdes; i++) {
9576 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9577 total_len += bde.tus.f.bdeSize;
9578 }
James Smart4f774512009-05-22 14:52:35 -04009579 } else
James Smart5ffc2662009-11-18 15:39:44 -05009580 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04009581
9582 iocbq->iocb.ulpIoTag = iocbq->iotag;
9583 cmnd = iocbq->iocb.ulpCommand;
9584
9585 switch (iocbq->iocb.ulpCommand) {
9586 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04009587 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9588 ndlp = iocbq->context_un.ndlp;
9589 else
9590 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009591 if (!iocbq->iocb.ulpLe) {
9592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9593 "2007 Only Limited Edition cmd Format"
9594 " supported 0x%x\n",
9595 iocbq->iocb.ulpCommand);
9596 return IOCB_ERROR;
9597 }
James Smartff78d8f2011-12-13 13:21:35 -05009598
James Smart5ffc2662009-11-18 15:39:44 -05009599 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009600 /* Els_reguest64 has a TMO */
9601 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9602 iocbq->iocb.ulpTimeout);
9603 /* Need a VF for word 4 set the vf bit*/
9604 bf_set(els_req64_vf, &wqe->els_req, 0);
9605 /* And a VFID for word 12 */
9606 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04009607 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04009608 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9609 iocbq->iocb.ulpContext);
9610 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9611 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04009612 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05009613 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05009614 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9615 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05009616 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9617 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05009618 if_type = bf_get(lpfc_sli_intf_if_type,
9619 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009620 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05009621 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05009622 *pcmd == ELS_CMD_SCR ||
James Smartdf3fe762020-02-10 09:31:55 -08009623 *pcmd == ELS_CMD_RDF ||
James Smartf60cb932019-05-14 14:58:05 -07009624 *pcmd == ELS_CMD_RSCN_XMT ||
James Smart6b5151f2012-01-18 16:24:06 -05009625 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05009626 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05009627 *pcmd == ELS_CMD_PLOGI)) {
9628 bf_set(els_req64_sp, &wqe->els_req, 1);
9629 bf_set(els_req64_sid, &wqe->els_req,
9630 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009631 if ((*pcmd == ELS_CMD_FLOGI) &&
9632 !(phba->fc_topology ==
9633 LPFC_TOPOLOGY_LOOP))
9634 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05009635 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9636 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04009637 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05009638 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05009639 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9640 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9641 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9642 }
James Smartc8685952009-11-18 15:39:16 -05009643 }
James Smart6d368e52011-05-24 11:44:12 -04009644 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9645 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009646 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9647 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9648 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9649 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9650 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9651 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009652 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009653 break;
James Smart5ffc2662009-11-18 15:39:44 -05009654 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009655 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9656 iocbq->iocb.un.ulpWord[3]);
9657 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009658 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009659 /* The entire sequence is transmitted for this IOCB */
9660 xmit_len = total_len;
9661 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009662 if (phba->link_flag & LS_LOOPBACK_MODE)
9663 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -06009664 /* fall through */
James Smart4f774512009-05-22 14:52:35 -04009665 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009666 /* word3 iocb=io_tag32 wqe=reserved */
9667 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009668 /* word4 relative_offset memcpy */
9669 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009670 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9671 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9672 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9673 LPFC_WQE_IOD_WRITE);
9674 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9675 LPFC_WQE_LENLOC_WORD12);
9676 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009677 wqe->xmit_sequence.xmit_len = xmit_len;
9678 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009679 break;
James Smart4f774512009-05-22 14:52:35 -04009680 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009681 /* word3 iocb=iotag32 wqe=seq_payload_len */
9682 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009683 /* word4 iocb=rsvd wqe=rsvd */
9684 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9685 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009686 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009687 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009688 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9689 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9690 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9691 LPFC_WQE_LENLOC_WORD3);
9692 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009693 break;
James Smart4f774512009-05-22 14:52:35 -04009694 case CMD_FCP_IWRITE64_CR:
9695 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009696 /* word3 iocb=iotag wqe=payload_offset_len */
9697 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009698 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9699 xmit_len + sizeof(struct fcp_rsp));
9700 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9701 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009702 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9703 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9704 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9705 iocbq->iocb.ulpFCP2Rcvy);
9706 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9707 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009708 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9709 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9710 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009711 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009712 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009713 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9714 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009715 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9716 if (iocbq->priority) {
9717 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9718 (iocbq->priority << 1));
9719 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009720 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9721 (phba->cfg_XLanePriority << 1));
9722 }
9723 }
James Smartb5c53952016-03-31 14:12:30 -07009724 /* Note, word 10 is already initialized to 0 */
9725
James Smart414abe02018-06-26 08:24:26 -07009726 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9727 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009728 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9729 else
9730 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9731
James Smartb5c53952016-03-31 14:12:30 -07009732 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009733 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009734 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009735 struct fcp_cmnd *fcp_cmnd;
9736 uint32_t *ptr;
9737
9738 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009739
9740 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009741 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009742 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9743
9744 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009745 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009746 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009747 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9748 wqe->generic.bde.addrHigh = 0;
9749 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009750
James Smart205e8242018-03-05 12:04:03 -08009751 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9752 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009753
9754 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009755 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009756 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9757 }
James Smart7851fe22011-07-22 18:36:52 -04009758 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009759 case CMD_FCP_IREAD64_CR:
9760 /* word3 iocb=iotag wqe=payload_offset_len */
9761 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009762 bf_set(payload_offset_len, &wqe->fcp_iread,
9763 xmit_len + sizeof(struct fcp_rsp));
9764 bf_set(cmd_buff_len, &wqe->fcp_iread,
9765 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009766 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9767 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9768 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9769 iocbq->iocb.ulpFCP2Rcvy);
9770 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009771 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009772 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9773 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9774 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009775 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009776 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009777 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9778 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009779 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9780 if (iocbq->priority) {
9781 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9782 (iocbq->priority << 1));
9783 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009784 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9785 (phba->cfg_XLanePriority << 1));
9786 }
9787 }
James Smartb5c53952016-03-31 14:12:30 -07009788 /* Note, word 10 is already initialized to 0 */
9789
James Smart414abe02018-06-26 08:24:26 -07009790 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9791 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009792 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9793 else
9794 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9795
James Smartb5c53952016-03-31 14:12:30 -07009796 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009797 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009798 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009799 struct fcp_cmnd *fcp_cmnd;
9800 uint32_t *ptr;
9801
9802 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009803
9804 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009805 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009806 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9807
9808 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009809 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009810 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009811 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9812 wqe->generic.bde.addrHigh = 0;
9813 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009814
James Smart205e8242018-03-05 12:04:03 -08009815 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9816 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009817
9818 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009819 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009820 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9821 }
James Smart7851fe22011-07-22 18:36:52 -04009822 break;
James Smartf1126682009-06-10 17:22:44 -04009823 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009824 /* word3 iocb=iotag wqe=payload_offset_len */
9825 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9826 bf_set(payload_offset_len, &wqe->fcp_icmd,
9827 xmit_len + sizeof(struct fcp_rsp));
9828 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9829 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009830 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009831 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009832 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009833 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9834 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9835 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9836 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9837 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009838 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9839 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009840 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9841 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009842 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9843 if (iocbq->priority) {
9844 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9845 (iocbq->priority << 1));
9846 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009847 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9848 (phba->cfg_XLanePriority << 1));
9849 }
9850 }
James Smartb5c53952016-03-31 14:12:30 -07009851 /* Note, word 10 is already initialized to 0 */
9852
9853 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009854 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009855 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009856 struct fcp_cmnd *fcp_cmnd;
9857 uint32_t *ptr;
9858
9859 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009860
9861 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009862 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009863 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9864
9865 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009866 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009867 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009868 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9869 wqe->generic.bde.addrHigh = 0;
9870 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009871
James Smart205e8242018-03-05 12:04:03 -08009872 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
9873 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009874
9875 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009876 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009877 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9878 }
James Smart7851fe22011-07-22 18:36:52 -04009879 break;
James Smart4f774512009-05-22 14:52:35 -04009880 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009881 /* For this command calculate the xmit length of the
9882 * request bde.
9883 */
9884 xmit_len = 0;
9885 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9886 sizeof(struct ulp_bde64);
9887 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009888 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009889 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9890 break;
James Smart63e801c2010-11-20 23:14:19 -05009891 xmit_len += bde.tus.f.bdeSize;
9892 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009893 /* word3 iocb=IO_TAG wqe=request_payload_len */
9894 wqe->gen_req.request_payload_len = xmit_len;
9895 /* word4 iocb=parameter wqe=relative_offset memcpy */
9896 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009897 /* word6 context tag copied in memcpy */
9898 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9899 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9900 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9901 "2015 Invalid CT %x command 0x%x\n",
9902 ct, iocbq->iocb.ulpCommand);
9903 return IOCB_ERROR;
9904 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009905 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9906 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9907 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9908 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9909 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9910 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9911 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9912 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009913 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009914 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009915 break;
James Smart4f774512009-05-22 14:52:35 -04009916 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009917 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009918 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009919 /* word3 iocb=iotag32 wqe=response_payload_len */
9920 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009921 /* word4 */
9922 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009923 /* word5 iocb=rsvd wge=did */
9924 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009925 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9926
9927 if_type = bf_get(lpfc_sli_intf_if_type,
9928 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009929 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009930 if (iocbq->vport->fc_flag & FC_PT2PT) {
9931 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9932 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9933 iocbq->vport->fc_myDID);
9934 if (iocbq->vport->fc_myDID == Fabric_DID) {
9935 bf_set(wqe_els_did,
9936 &wqe->xmit_els_rsp.wqe_dest, 0);
9937 }
9938 }
9939 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009940 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9941 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9942 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9943 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009944 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009945 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009946 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009947 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009948 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9949 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9950 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9951 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9952 LPFC_WQE_LENLOC_WORD3);
9953 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009954 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9955 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009956 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9957 iocbq->context2)->virt);
9958 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009959 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9960 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009961 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009962 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9963 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009964 phba->vpi_ids[phba->pport->vpi]);
9965 }
James Smart4f774512009-05-22 14:52:35 -04009966 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009967 break;
James Smart4f774512009-05-22 14:52:35 -04009968 case CMD_CLOSE_XRI_CN:
9969 case CMD_ABORT_XRI_CN:
9970 case CMD_ABORT_XRI_CX:
9971 /* words 0-2 memcpy should be 0 rserved */
9972 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009973 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9974 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9975 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9976 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9977 } else
9978 fip = 0;
9979
9980 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009981 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009982 * The link is down, or the command was ELS_FIP
9983 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009984 * on the wire.
9985 */
9986 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9987 else
9988 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9989 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009990 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9991 wqe->abort_cmd.rsrvd5 = 0;
9992 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009993 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9994 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009995 /*
9996 * The abort handler will send us CMD_ABORT_XRI_CN or
9997 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9998 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009999 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10000 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
10001 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
10002 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -040010003 cmnd = CMD_ABORT_XRI_CX;
10004 command_type = OTHER_COMMAND;
10005 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -040010006 break;
James Smart6669f9b2009-10-02 15:16:45 -040010007 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -050010008 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -050010009 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -040010010 * we re-construct this WQE here based on information in
10011 * iocbq from scratch.
10012 */
James Smartd9f492a2019-08-14 16:57:04 -070010013 memset(wqe, 0, sizeof(*wqe));
James Smart5ffc2662009-11-18 15:39:44 -050010014 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -040010015 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -050010016 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
10017 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -050010018 LPFC_ABTS_UNSOL_INT) {
10019 /* ABTS sent by initiator to CT exchange, the
10020 * RX_ID field will be filled with the newly
10021 * allocated responder XRI.
10022 */
10023 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10024 iocbq->sli4_xritag);
10025 } else {
10026 /* ABTS sent by responder to CT exchange, the
10027 * RX_ID field will be filled with the responder
10028 * RX_ID from ABTS.
10029 */
10030 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -050010031 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -050010032 }
James Smart6669f9b2009-10-02 15:16:45 -040010033 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
10034 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -050010035
10036 /* Use CT=VPI */
10037 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
10038 ndlp->nlp_DID);
10039 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
10040 iocbq->iocb.ulpContext);
10041 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -040010042 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -050010043 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -040010044 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
10045 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
10046 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -040010047 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
10048 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -050010049 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
10050 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
10051 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
10052 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
10053 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
10054 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
10055 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
10056 }
10057
James Smart7851fe22011-07-22 18:36:52 -040010058 break;
James Smartae9e28f2017-05-15 15:20:51 -070010059 case CMD_SEND_FRAME:
James Smarte62245d2019-08-14 16:57:08 -070010060 bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_SEND_FRAME);
10061 bf_set(wqe_sof, &wqe->generic.wqe_com, 0x2E); /* SOF byte */
10062 bf_set(wqe_eof, &wqe->generic.wqe_com, 0x41); /* EOF byte */
10063 bf_set(wqe_lenloc, &wqe->generic.wqe_com, 1);
10064 bf_set(wqe_xbl, &wqe->generic.wqe_com, 1);
10065 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10066 bf_set(wqe_xc, &wqe->generic.wqe_com, 1);
10067 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, 0xA);
10068 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smartae9e28f2017-05-15 15:20:51 -070010069 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10070 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10071 return 0;
James Smart4f774512009-05-22 14:52:35 -040010072 case CMD_XRI_ABORTED_CX:
10073 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -040010074 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
10075 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
10076 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
10077 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
10078 default:
10079 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10080 "2014 Invalid command 0x%x\n",
10081 iocbq->iocb.ulpCommand);
10082 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -040010083 break;
James Smart4f774512009-05-22 14:52:35 -040010084 }
James Smart6d368e52011-05-24 11:44:12 -040010085
James Smart8012cc32012-10-31 14:44:49 -040010086 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
10087 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
10088 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
10089 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
10090 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
10091 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
10092 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
10093 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -040010094 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10095 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10096 wqe->generic.wqe_com.abort_tag = abort_tag;
10097 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
10098 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
10099 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
10100 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -040010101 return 0;
10102}
10103
10104/**
10105 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
10106 * @phba: Pointer to HBA context object.
10107 * @ring_number: SLI ring number to issue iocb on.
10108 * @piocb: Pointer to command iocb.
10109 * @flag: Flag indicating if this command can be put into txq.
10110 *
10111 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
10112 * an iocb command to an HBA with SLI-4 interface spec.
10113 *
James Smart27f3efd2019-10-18 14:18:19 -070010114 * This function is called with ringlock held. The function will return success
James Smart4f774512009-05-22 14:52:35 -040010115 * after it successfully submit the iocb to firmware or after adding to the
10116 * txq.
10117 **/
10118static int
10119__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
10120 struct lpfc_iocbq *piocb, uint32_t flag)
10121{
10122 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080010123 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -050010124 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -080010125 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -040010126
James Smart895427b2017-02-12 13:52:30 -080010127 /* Get the WQ */
10128 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
10129 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc00f62e2019-08-14 16:57:11 -070010130 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
James Smart895427b2017-02-12 13:52:30 -080010131 } else {
10132 wq = phba->sli4_hba.els_wq;
10133 }
10134
10135 /* Get corresponding ring */
10136 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010137
James Smartb5c53952016-03-31 14:12:30 -070010138 /*
10139 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -070010140 */
James Smartb5c53952016-03-31 14:12:30 -070010141
James Smartcda7fa182019-03-04 15:15:43 -080010142 lockdep_assert_held(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010143
James Smart4f774512009-05-22 14:52:35 -040010144 if (piocb->sli4_xritag == NO_XRI) {
10145 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -050010146 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -040010147 sglq = NULL;
10148 else {
James Smart0e9bb8d2013-03-01 16:35:12 -050010149 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -040010150 if (!(flag & SLI_IOCB_RET_IOCB)) {
10151 __lpfc_sli_ringtx_put(phba,
10152 pring, piocb);
10153 return IOCB_SUCCESS;
10154 } else {
10155 return IOCB_BUSY;
10156 }
10157 } else {
James Smart895427b2017-02-12 13:52:30 -080010158 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -040010159 if (!sglq) {
10160 if (!(flag & SLI_IOCB_RET_IOCB)) {
10161 __lpfc_sli_ringtx_put(phba,
10162 pring,
10163 piocb);
10164 return IOCB_SUCCESS;
10165 } else
10166 return IOCB_BUSY;
10167 }
10168 }
James Smart4f774512009-05-22 14:52:35 -040010169 }
James Smart2ea259e2017-02-12 13:52:27 -080010170 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -040010171 /* These IO's already have an XRI and a mapped sgl. */
10172 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -080010173 else {
James Smart6d368e52011-05-24 11:44:12 -040010174 /*
10175 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -040010176 * sglq is on the active list
10177 */
James Smartedccdc12013-01-03 15:43:45 -050010178 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -040010179 if (!sglq)
10180 return IOCB_ERROR;
10181 }
10182
10183 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -040010184 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040010185 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040010186 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -040010187 return IOCB_ERROR;
10188 }
10189
James Smart205e8242018-03-05 12:04:03 -080010190 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -040010191 return IOCB_ERROR;
10192
James Smart205e8242018-03-05 12:04:03 -080010193 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -080010194 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -040010195 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
10196
10197 return 0;
10198}
10199
10200/**
James Smart3772a992009-05-22 14:50:54 -040010201 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
10202 *
10203 * This routine wraps the actual lockless version for issusing IOCB function
10204 * pointer from the lpfc_hba struct.
10205 *
10206 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -070010207 * IOCB_ERROR - Error
10208 * IOCB_SUCCESS - Success
10209 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -040010210 **/
James Smart2a9bf3d2010-06-07 15:24:45 -040010211int
James Smart3772a992009-05-22 14:50:54 -040010212__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10213 struct lpfc_iocbq *piocb, uint32_t flag)
10214{
10215 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10216}
10217
10218/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010219 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -040010220 * @phba: The hba struct for which this call is being executed.
10221 * @dev_grp: The HBA PCI-Device group number.
10222 *
10223 * This routine sets up the SLI interface API function jump table in @phba
10224 * struct.
10225 * Returns: 0 - success, -ENODEV - failure.
10226 **/
10227int
10228lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
10229{
10230
10231 switch (dev_grp) {
10232 case LPFC_PCI_DEV_LP:
10233 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
10234 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
10235 break;
James Smart4f774512009-05-22 14:52:35 -040010236 case LPFC_PCI_DEV_OC:
10237 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
10238 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
10239 break;
James Smart3772a992009-05-22 14:50:54 -040010240 default:
10241 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10242 "1419 Invalid HBA PCI-device group: 0x%x\n",
10243 dev_grp);
10244 return -ENODEV;
10245 break;
10246 }
10247 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
10248 return 0;
10249}
James Smart92d7f7b2007-06-17 19:56:38 -050010250
James Smarta1efe162015-05-21 13:55:20 -040010251/**
James Smart895427b2017-02-12 13:52:30 -080010252 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -040010253 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -040010254 * @piocb: Pointer to command iocb.
10255 *
James Smart895427b2017-02-12 13:52:30 -080010256 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
10257 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -040010258 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -080010259 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -040010260 */
James Smart895427b2017-02-12 13:52:30 -080010261struct lpfc_sli_ring *
10262lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -040010263{
James Smartc4908502019-01-28 11:14:28 -080010264 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -080010265
James Smart895427b2017-02-12 13:52:30 -080010266 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -080010267 if (unlikely(!phba->sli4_hba.hdwq))
James Smart7370d102019-01-28 11:14:20 -080010268 return NULL;
10269 /*
10270 * for abort iocb hba_wqidx should already
10271 * be setup based on what work queue we used.
10272 */
10273 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc4908502019-01-28 11:14:28 -080010274 lpfc_cmd = (struct lpfc_io_buf *)piocb->context1;
James Smart1fbf9742019-01-28 11:14:26 -080010275 piocb->hba_wqidx = lpfc_cmd->hdwq_no;
James Smart9bd2bff52014-09-03 12:57:30 -040010276 }
James Smartc00f62e2019-08-14 16:57:11 -070010277 return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010278 } else {
10279 if (unlikely(!phba->sli4_hba.els_wq))
10280 return NULL;
10281 piocb->hba_wqidx = 0;
10282 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -040010283 }
James Smart9bd2bff52014-09-03 12:57:30 -040010284}
10285
James Smarte59058c2008-08-24 21:49:00 -040010286/**
James Smart3621a712009-04-06 18:47:14 -040010287 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -040010288 * @phba: Pointer to HBA context object.
10289 * @pring: Pointer to driver SLI ring object.
10290 * @piocb: Pointer to command iocb.
10291 * @flag: Flag indicating if this command can be put into txq.
10292 *
10293 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
10294 * function. This function gets the hbalock and calls
10295 * __lpfc_sli_issue_iocb function and will return the error returned
10296 * by __lpfc_sli_issue_iocb function. This wrapper is used by
10297 * functions which do not hold hbalock.
10298 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010299int
James Smart3772a992009-05-22 14:50:54 -040010300lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -050010301 struct lpfc_iocbq *piocb, uint32_t flag)
10302{
James Smart2a76a282012-08-03 12:35:54 -040010303 struct lpfc_sli_ring *pring;
James Smart93a4d6f2019-11-04 16:57:05 -080010304 struct lpfc_queue *eq;
James Smart92d7f7b2007-06-17 19:56:38 -050010305 unsigned long iflags;
James Smart6a828b02019-01-28 11:14:31 -080010306 int rc;
James Smart92d7f7b2007-06-17 19:56:38 -050010307
James Smart7e56aa22012-08-03 12:35:34 -040010308 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart93a4d6f2019-11-04 16:57:05 -080010309 eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
10310
James Smart895427b2017-02-12 13:52:30 -080010311 pring = lpfc_sli4_calc_ring(phba, piocb);
10312 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010313 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -040010314
James Smart9bd2bff52014-09-03 12:57:30 -040010315 spin_lock_irqsave(&pring->ring_lock, iflags);
10316 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10317 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080010318
10319 lpfc_sli4_poll_eq(eq, LPFC_POLL_FASTPATH);
James Smart7e56aa22012-08-03 12:35:34 -040010320 } else {
10321 /* For now, SLI2/3 will still use hbalock */
10322 spin_lock_irqsave(&phba->hbalock, iflags);
10323 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10324 spin_unlock_irqrestore(&phba->hbalock, iflags);
10325 }
James Smart92d7f7b2007-06-17 19:56:38 -050010326 return rc;
10327}
10328
James Smarte59058c2008-08-24 21:49:00 -040010329/**
James Smart3621a712009-04-06 18:47:14 -040010330 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010331 * @phba: Pointer to HBA context object.
10332 *
10333 * This function is called while driver attaches with the
10334 * HBA to setup the extra ring. The extra ring is used
10335 * only when driver needs to support target mode functionality
10336 * or IP over FC functionalities.
10337 *
James Smart895427b2017-02-12 13:52:30 -080010338 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -040010339 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010340static int
10341lpfc_extra_ring_setup( struct lpfc_hba *phba)
10342{
10343 struct lpfc_sli *psli;
10344 struct lpfc_sli_ring *pring;
10345
10346 psli = &phba->sli;
10347
10348 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -050010349
10350 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -080010351 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -040010352 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10353 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10354 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10355 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010356
James Smarta4bc3372006-12-02 13:34:16 -050010357 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -080010358 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -050010359
James Smart7e56aa22012-08-03 12:35:34 -040010360 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10361 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10362 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10363 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010364
10365 /* Setup default profile for this ring */
10366 pring->iotag_max = 4096;
10367 pring->num_mask = 1;
10368 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -050010369 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10370 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010371 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10372 return 0;
10373}
10374
James Smartcb69f7d2011-12-13 13:21:57 -050010375/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10376 * @phba: Pointer to HBA context object.
10377 * @iocbq: Pointer to iocb object.
10378 *
10379 * The async_event handler calls this routine when it receives
10380 * an ASYNC_STATUS_CN event from the port. The port generates
10381 * this event when an Abort Sequence request to an rport fails
10382 * twice in succession. The abort could be originated by the
10383 * driver or by the port. The ABTS could have been for an ELS
10384 * or FCP IO. The port only generates this event when an ABTS
10385 * fails to complete after one retry.
10386 */
10387static void
10388lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10389 struct lpfc_iocbq *iocbq)
10390{
10391 struct lpfc_nodelist *ndlp = NULL;
10392 uint16_t rpi = 0, vpi = 0;
10393 struct lpfc_vport *vport = NULL;
10394
10395 /* The rpi in the ulpContext is vport-sensitive. */
10396 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10397 rpi = iocbq->iocb.ulpContext;
10398
10399 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10400 "3092 Port generated ABTS async event "
10401 "on vpi %d rpi %d status 0x%x\n",
10402 vpi, rpi, iocbq->iocb.ulpStatus);
10403
10404 vport = lpfc_find_vport_by_vpid(phba, vpi);
10405 if (!vport)
10406 goto err_exit;
10407 ndlp = lpfc_findnode_rpi(vport, rpi);
10408 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
10409 goto err_exit;
10410
10411 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10412 lpfc_sli_abts_recover_port(vport, ndlp);
10413 return;
10414
10415 err_exit:
10416 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10417 "3095 Event Context not found, no "
10418 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10419 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10420 vpi, rpi);
10421}
10422
10423/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10424 * @phba: pointer to HBA context object.
10425 * @ndlp: nodelist pointer for the impacted rport.
10426 * @axri: pointer to the wcqe containing the failed exchange.
10427 *
10428 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10429 * port. The port generates this event when an abort exchange request to an
10430 * rport fails twice in succession with no reply. The abort could be originated
10431 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
10432 */
10433void
10434lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10435 struct lpfc_nodelist *ndlp,
10436 struct sli4_wcqe_xri_aborted *axri)
10437{
10438 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -050010439 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -050010440
James Smart6b5151f2012-01-18 16:24:06 -050010441 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -050010442 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10443 "3115 Node Context not found, driver "
10444 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -050010445 return;
10446 }
10447
James Smartcb69f7d2011-12-13 13:21:57 -050010448 vport = ndlp->vport;
10449 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10450 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -050010451 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -040010452 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -050010453 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -050010454 bf_get(lpfc_wcqe_xa_status, axri),
10455 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -050010456
James Smart5c1db2a2012-03-01 22:34:36 -050010457 /*
10458 * Catch the ABTS protocol failure case. Older OCe FW releases returned
10459 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10460 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10461 */
James Smarte3d2b802012-08-14 14:25:43 -040010462 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -050010463 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10464 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -050010465 lpfc_sli_abts_recover_port(vport, ndlp);
10466}
10467
James Smarte59058c2008-08-24 21:49:00 -040010468/**
James Smart3621a712009-04-06 18:47:14 -040010469 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -040010470 * @phba: Pointer to HBA context object.
10471 * @pring: Pointer to driver SLI ring object.
10472 * @iocbq: Pointer to iocb object.
10473 *
10474 * This function is called by the slow ring event handler
10475 * function when there is an ASYNC event iocb in the ring.
10476 * This function is called with no lock held.
10477 * Currently this function handles only temperature related
10478 * ASYNC events. The function decodes the temperature sensor
10479 * event message and posts events for the management applications.
10480 **/
James Smart98c9ea52007-10-27 13:37:33 -040010481static void
James Smart57127f12007-10-27 13:37:05 -040010482lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10483 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10484{
10485 IOCB_t *icmd;
10486 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -040010487 struct temp_event temp_event_data;
10488 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -040010489 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -040010490
10491 icmd = &iocbq->iocb;
10492 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -040010493
James Smartcb69f7d2011-12-13 13:21:57 -050010494 switch (evt_code) {
10495 case ASYNC_TEMP_WARN:
10496 case ASYNC_TEMP_SAFE:
10497 temp_event_data.data = (uint32_t) icmd->ulpContext;
10498 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10499 if (evt_code == ASYNC_TEMP_WARN) {
10500 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
10501 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10502 "0347 Adapter is very hot, please take "
10503 "corrective action. temperature : %d Celsius\n",
10504 (uint32_t) icmd->ulpContext);
10505 } else {
10506 temp_event_data.event_code = LPFC_NORMAL_TEMP;
10507 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
10508 "0340 Adapter temperature is OK now. "
10509 "temperature : %d Celsius\n",
10510 (uint32_t) icmd->ulpContext);
10511 }
10512
10513 /* Send temperature change event to applications */
10514 shost = lpfc_shost_from_vport(phba->pport);
10515 fc_host_post_vendor_event(shost, fc_get_event_number(),
10516 sizeof(temp_event_data), (char *) &temp_event_data,
10517 LPFC_NL_VENDOR_ID);
10518 break;
10519 case ASYNC_STATUS_CN:
10520 lpfc_sli_abts_err_handler(phba, iocbq);
10521 break;
10522 default:
James Smarta257bf92009-04-06 18:48:10 -040010523 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -050010524 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -040010525 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -040010526 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -040010527 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
10528 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
10529 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
10530 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -050010531 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -040010532 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10533 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10534 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10535 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10536
James Smartcb69f7d2011-12-13 13:21:57 -050010537 break;
James Smart57127f12007-10-27 13:37:05 -040010538 }
James Smart57127f12007-10-27 13:37:05 -040010539}
10540
10541
James Smarte59058c2008-08-24 21:49:00 -040010542/**
James Smart895427b2017-02-12 13:52:30 -080010543 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010544 * @phba: Pointer to HBA context object.
10545 *
10546 * lpfc_sli_setup sets up rings of the SLI interface with
10547 * number of iocbs per ring and iotags. This function is
10548 * called while driver attach to the HBA and before the
10549 * interrupts are enabled. So there is no need for locking.
10550 *
10551 * This function always returns 0.
10552 **/
dea31012005-04-17 16:05:31 -050010553int
James Smart895427b2017-02-12 13:52:30 -080010554lpfc_sli4_setup(struct lpfc_hba *phba)
10555{
10556 struct lpfc_sli_ring *pring;
10557
10558 pring = phba->sli4_hba.els_wq->pring;
10559 pring->num_mask = LPFC_MAX_RING_MASK;
10560 pring->prt[0].profile = 0; /* Mask 0 */
10561 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10562 pring->prt[0].type = FC_TYPE_ELS;
10563 pring->prt[0].lpfc_sli_rcv_unsol_event =
10564 lpfc_els_unsol_event;
10565 pring->prt[1].profile = 0; /* Mask 1 */
10566 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10567 pring->prt[1].type = FC_TYPE_ELS;
10568 pring->prt[1].lpfc_sli_rcv_unsol_event =
10569 lpfc_els_unsol_event;
10570 pring->prt[2].profile = 0; /* Mask 2 */
10571 /* NameServer Inquiry */
10572 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10573 /* NameServer */
10574 pring->prt[2].type = FC_TYPE_CT;
10575 pring->prt[2].lpfc_sli_rcv_unsol_event =
10576 lpfc_ct_unsol_event;
10577 pring->prt[3].profile = 0; /* Mask 3 */
10578 /* NameServer response */
10579 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10580 /* NameServer */
10581 pring->prt[3].type = FC_TYPE_CT;
10582 pring->prt[3].lpfc_sli_rcv_unsol_event =
10583 lpfc_ct_unsol_event;
10584 return 0;
10585}
10586
10587/**
10588 * lpfc_sli_setup - SLI ring setup function
10589 * @phba: Pointer to HBA context object.
10590 *
10591 * lpfc_sli_setup sets up rings of the SLI interface with
10592 * number of iocbs per ring and iotags. This function is
10593 * called while driver attach to the HBA and before the
10594 * interrupts are enabled. So there is no need for locking.
10595 *
10596 * This function always returns 0. SLI3 only.
10597 **/
10598int
dea31012005-04-17 16:05:31 -050010599lpfc_sli_setup(struct lpfc_hba *phba)
10600{
James Smarted957682007-06-17 19:56:37 -050010601 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050010602 struct lpfc_sli *psli = &phba->sli;
10603 struct lpfc_sli_ring *pring;
10604
James Smart2a76a282012-08-03 12:35:54 -040010605 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010606 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010607
James Bottomley604a3e32005-10-29 10:28:33 -050010608 psli->iocbq_lookup = NULL;
10609 psli->iocbq_lookup_len = 0;
10610 psli->last_iotag = 0;
10611
dea31012005-04-17 16:05:31 -050010612 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010613 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010614 switch (i) {
10615 case LPFC_FCP_RING: /* ring 0 - FCP */
10616 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010617 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10618 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10619 pring->sli.sli3.numCiocb +=
10620 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10621 pring->sli.sli3.numRiocb +=
10622 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10623 pring->sli.sli3.numCiocb +=
10624 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10625 pring->sli.sli3.numRiocb +=
10626 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10627 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010628 SLI3_IOCB_CMD_SIZE :
10629 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010630 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010631 SLI3_IOCB_RSP_SIZE :
10632 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010633 pring->iotag_ctr = 0;
10634 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010635 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010636 pring->fast_iotag = pring->iotag_max;
10637 pring->num_mask = 0;
10638 break;
James Smarta4bc3372006-12-02 13:34:16 -050010639 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010640 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010641 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10642 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10643 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010644 SLI3_IOCB_CMD_SIZE :
10645 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010646 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010647 SLI3_IOCB_RSP_SIZE :
10648 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010649 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010650 pring->num_mask = 0;
10651 break;
10652 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10653 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010654 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10655 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10656 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010657 SLI3_IOCB_CMD_SIZE :
10658 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010659 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010660 SLI3_IOCB_RSP_SIZE :
10661 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010662 pring->fast_iotag = 0;
10663 pring->iotag_ctr = 0;
10664 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010665 pring->lpfc_sli_rcv_async_status =
10666 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010667 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010668 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010669 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10670 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010671 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010672 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010673 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010674 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10675 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010676 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010677 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010678 pring->prt[2].profile = 0; /* Mask 2 */
10679 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010680 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010681 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010682 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010683 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010684 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010685 pring->prt[3].profile = 0; /* Mask 3 */
10686 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010687 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010688 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010689 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010690 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010691 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010692 break;
10693 }
James Smart7e56aa22012-08-03 12:35:34 -040010694 totiocbsize += (pring->sli.sli3.numCiocb *
10695 pring->sli.sli3.sizeCiocb) +
10696 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010697 }
James Smarted957682007-06-17 19:56:37 -050010698 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010699 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010700 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10701 "SLI2 SLIM Data: x%x x%lx\n",
10702 phba->brd_no, totiocbsize,
10703 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010704 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010705 if (phba->cfg_multi_ring_support == 2)
10706 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010707
10708 return 0;
10709}
10710
James Smarte59058c2008-08-24 21:49:00 -040010711/**
James Smart895427b2017-02-12 13:52:30 -080010712 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010713 * @phba: Pointer to HBA context object.
10714 *
James Smart895427b2017-02-12 13:52:30 -080010715 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010716 * ring. This function also initializes ring indices of each ring.
10717 * This function is called during the initialization of the SLI
10718 * interface of an HBA.
10719 * This function is called with no lock held and always returns
10720 * 1.
10721 **/
James Smart895427b2017-02-12 13:52:30 -080010722void
10723lpfc_sli4_queue_init(struct lpfc_hba *phba)
10724{
10725 struct lpfc_sli *psli;
10726 struct lpfc_sli_ring *pring;
10727 int i;
10728
10729 psli = &phba->sli;
10730 spin_lock_irq(&phba->hbalock);
10731 INIT_LIST_HEAD(&psli->mboxq);
10732 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10733 /* Initialize list headers for txq and txcmplq as double linked lists */
James Smartcdb42be2019-01-28 11:14:21 -080010734 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -070010735 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010736 pring->flag = 0;
10737 pring->ringno = LPFC_FCP_RING;
James Smartc4908502019-01-28 11:14:28 -080010738 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010739 INIT_LIST_HEAD(&pring->txq);
10740 INIT_LIST_HEAD(&pring->txcmplq);
10741 INIT_LIST_HEAD(&pring->iocb_continueq);
10742 spin_lock_init(&pring->ring_lock);
10743 }
10744 pring = phba->sli4_hba.els_wq->pring;
10745 pring->flag = 0;
10746 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010747 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010748 INIT_LIST_HEAD(&pring->txq);
10749 INIT_LIST_HEAD(&pring->txcmplq);
10750 INIT_LIST_HEAD(&pring->iocb_continueq);
10751 spin_lock_init(&pring->ring_lock);
10752
James Smartcdb42be2019-01-28 11:14:21 -080010753 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080010754 pring = phba->sli4_hba.nvmels_wq->pring;
10755 pring->flag = 0;
10756 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080010757 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080010758 INIT_LIST_HEAD(&pring->txq);
10759 INIT_LIST_HEAD(&pring->txcmplq);
10760 INIT_LIST_HEAD(&pring->iocb_continueq);
10761 spin_lock_init(&pring->ring_lock);
10762 }
10763
10764 spin_unlock_irq(&phba->hbalock);
10765}
10766
10767/**
10768 * lpfc_sli_queue_init - Queue initialization function
10769 * @phba: Pointer to HBA context object.
10770 *
10771 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10772 * ring. This function also initializes ring indices of each ring.
10773 * This function is called during the initialization of the SLI
10774 * interface of an HBA.
10775 * This function is called with no lock held and always returns
10776 * 1.
10777 **/
10778void
10779lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010780{
10781 struct lpfc_sli *psli;
10782 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010783 int i;
dea31012005-04-17 16:05:31 -050010784
10785 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010786 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010787 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010788 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010789 /* Initialize list headers for txq and txcmplq as double linked lists */
10790 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010791 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010792 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010793 pring->sli.sli3.next_cmdidx = 0;
10794 pring->sli.sli3.local_getidx = 0;
10795 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010796 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010797 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010798 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010799 pring->flag = 0;
10800 INIT_LIST_HEAD(&pring->txq);
10801 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010802 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010803 }
James Smart2e0fef82007-06-17 19:56:36 -050010804 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010805}
10806
James Smarte59058c2008-08-24 21:49:00 -040010807/**
James Smart04c68492009-05-22 14:52:52 -040010808 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10809 * @phba: Pointer to HBA context object.
10810 *
10811 * This routine flushes the mailbox command subsystem. It will unconditionally
10812 * flush all the mailbox commands in the three possible stages in the mailbox
10813 * command sub-system: pending mailbox command queue; the outstanding mailbox
10814 * command; and completed mailbox command queue. It is caller's responsibility
10815 * to make sure that the driver is in the proper state to flush the mailbox
10816 * command sub-system. Namely, the posting of mailbox commands into the
10817 * pending mailbox command queue from the various clients must be stopped;
10818 * either the HBA is in a state that it will never works on the outstanding
10819 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10820 * mailbox command has been completed.
10821 **/
10822static void
10823lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10824{
10825 LIST_HEAD(completions);
10826 struct lpfc_sli *psli = &phba->sli;
10827 LPFC_MBOXQ_t *pmb;
10828 unsigned long iflag;
10829
James Smart523128e2018-09-10 10:30:46 -070010830 /* Disable softirqs, including timers from obtaining phba->hbalock */
10831 local_bh_disable();
10832
James Smart04c68492009-05-22 14:52:52 -040010833 /* Flush all the mailbox commands in the mbox system */
10834 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart523128e2018-09-10 10:30:46 -070010835
James Smart04c68492009-05-22 14:52:52 -040010836 /* The pending mailbox command queue */
10837 list_splice_init(&phba->sli.mboxq, &completions);
10838 /* The outstanding active mailbox command */
10839 if (psli->mbox_active) {
10840 list_add_tail(&psli->mbox_active->list, &completions);
10841 psli->mbox_active = NULL;
10842 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10843 }
10844 /* The completed mailbox command queue */
10845 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10846 spin_unlock_irqrestore(&phba->hbalock, iflag);
10847
James Smart523128e2018-09-10 10:30:46 -070010848 /* Enable softirqs again, done with phba->hbalock */
10849 local_bh_enable();
10850
James Smart04c68492009-05-22 14:52:52 -040010851 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10852 while (!list_empty(&completions)) {
10853 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10854 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10855 if (pmb->mbox_cmpl)
10856 pmb->mbox_cmpl(phba, pmb);
10857 }
10858}
10859
10860/**
James Smart3621a712009-04-06 18:47:14 -040010861 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010862 * @vport: Pointer to virtual port object.
10863 *
10864 * lpfc_sli_host_down is called to clean up the resources
10865 * associated with a vport before destroying virtual
10866 * port data structures.
10867 * This function does following operations:
10868 * - Free discovery resources associated with this virtual
10869 * port.
10870 * - Free iocbs associated with this virtual port in
10871 * the txq.
10872 * - Send abort for all iocb commands associated with this
10873 * vport in txcmplq.
10874 *
10875 * This function is called with no lock held and always returns 1.
10876 **/
dea31012005-04-17 16:05:31 -050010877int
James Smart92d7f7b2007-06-17 19:56:38 -050010878lpfc_sli_host_down(struct lpfc_vport *vport)
10879{
James Smart858c9f62007-06-17 19:56:39 -050010880 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010881 struct lpfc_hba *phba = vport->phba;
10882 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010883 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010884 struct lpfc_sli_ring *pring;
10885 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010886 int i;
10887 unsigned long flags = 0;
10888 uint16_t prev_pring_flag;
10889
10890 lpfc_cleanup_discovery_resources(vport);
10891
10892 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010893
James Smart895427b2017-02-12 13:52:30 -080010894 /*
10895 * Error everything on the txq since these iocbs
10896 * have not been given to the FW yet.
10897 * Also issue ABTS for everything on the txcmplq
10898 */
10899 if (phba->sli_rev != LPFC_SLI_REV4) {
10900 for (i = 0; i < psli->num_rings; i++) {
10901 pring = &psli->sli3_ring[i];
10902 prev_pring_flag = pring->flag;
10903 /* Only slow rings */
10904 if (pring->ringno == LPFC_ELS_RING) {
10905 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10906 /* Set the lpfc data pending flag */
10907 set_bit(LPFC_DATA_READY, &phba->data_flags);
10908 }
10909 list_for_each_entry_safe(iocb, next_iocb,
10910 &pring->txq, list) {
10911 if (iocb->vport != vport)
10912 continue;
10913 list_move_tail(&iocb->list, &completions);
10914 }
10915 list_for_each_entry_safe(iocb, next_iocb,
10916 &pring->txcmplq, list) {
10917 if (iocb->vport != vport)
10918 continue;
10919 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10920 }
10921 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010922 }
James Smart895427b2017-02-12 13:52:30 -080010923 } else {
10924 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10925 pring = qp->pring;
10926 if (!pring)
10927 continue;
10928 if (pring == phba->sli4_hba.els_wq->pring) {
10929 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10930 /* Set the lpfc data pending flag */
10931 set_bit(LPFC_DATA_READY, &phba->data_flags);
10932 }
10933 prev_pring_flag = pring->flag;
James Smart65a3df62019-09-21 20:58:48 -070010934 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010935 list_for_each_entry_safe(iocb, next_iocb,
10936 &pring->txq, list) {
10937 if (iocb->vport != vport)
10938 continue;
10939 list_move_tail(&iocb->list, &completions);
10940 }
James Smart65a3df62019-09-21 20:58:48 -070010941 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010942 list_for_each_entry_safe(iocb, next_iocb,
10943 &pring->txcmplq, list) {
10944 if (iocb->vport != vport)
10945 continue;
10946 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10947 }
10948 pring->flag = prev_pring_flag;
10949 }
James Smart92d7f7b2007-06-17 19:56:38 -050010950 }
James Smart92d7f7b2007-06-17 19:56:38 -050010951 spin_unlock_irqrestore(&phba->hbalock, flags);
10952
James Smarta257bf92009-04-06 18:48:10 -040010953 /* Cancel all the IOCBs from the completions list */
10954 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10955 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010956 return 1;
10957}
10958
James Smarte59058c2008-08-24 21:49:00 -040010959/**
James Smart3621a712009-04-06 18:47:14 -040010960 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010961 * @phba: Pointer to HBA context object.
10962 *
10963 * This function cleans up all iocb, buffers, mailbox commands
10964 * while shutting down the HBA. This function is called with no
10965 * lock held and always returns 1.
10966 * This function does the following to cleanup driver resources:
10967 * - Free discovery resources for each virtual port
10968 * - Cleanup any pending fabric iocbs
10969 * - Iterate through the iocb txq and free each entry
10970 * in the list.
10971 * - Free up any buffer posted to the HBA
10972 * - Free mailbox commands in the mailbox queue.
10973 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010974int
James Smart2e0fef82007-06-17 19:56:36 -050010975lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010976{
James Smart2534ba72007-04-25 09:52:20 -040010977 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010978 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010979 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010980 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010981 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010982 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010983 int i;
10984
10985 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010986 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010987
dea31012005-04-17 16:05:31 -050010988 lpfc_hba_down_prep(phba);
10989
James Smart523128e2018-09-10 10:30:46 -070010990 /* Disable softirqs, including timers from obtaining phba->hbalock */
10991 local_bh_disable();
10992
James Smart92d7f7b2007-06-17 19:56:38 -050010993 lpfc_fabric_abort_hba(phba);
10994
James Smart2e0fef82007-06-17 19:56:36 -050010995 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010996
James Smart895427b2017-02-12 13:52:30 -080010997 /*
10998 * Error everything on the txq since these iocbs
10999 * have not been given to the FW yet.
11000 */
11001 if (phba->sli_rev != LPFC_SLI_REV4) {
11002 for (i = 0; i < psli->num_rings; i++) {
11003 pring = &psli->sli3_ring[i];
11004 /* Only slow rings */
11005 if (pring->ringno == LPFC_ELS_RING) {
11006 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11007 /* Set the lpfc data pending flag */
11008 set_bit(LPFC_DATA_READY, &phba->data_flags);
11009 }
11010 list_splice_init(&pring->txq, &completions);
11011 }
11012 } else {
11013 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11014 pring = qp->pring;
11015 if (!pring)
11016 continue;
James Smart4b0a42b2019-08-14 16:56:42 -070011017 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011018 list_splice_init(&pring->txq, &completions);
James Smart4b0a42b2019-08-14 16:56:42 -070011019 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011020 if (pring == phba->sli4_hba.els_wq->pring) {
11021 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11022 /* Set the lpfc data pending flag */
11023 set_bit(LPFC_DATA_READY, &phba->data_flags);
11024 }
11025 }
dea31012005-04-17 16:05:31 -050011026 }
James Smart2e0fef82007-06-17 19:56:36 -050011027 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050011028
James Smarta257bf92009-04-06 18:48:10 -040011029 /* Cancel all the IOCBs from the completions list */
11030 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11031 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040011032
James Smart0ff10d42008-01-11 01:52:36 -050011033 spin_lock_irqsave(&phba->hbalock, flags);
11034 list_splice_init(&phba->elsbuf, &completions);
11035 phba->elsbuf_cnt = 0;
11036 phba->elsbuf_prev_cnt = 0;
11037 spin_unlock_irqrestore(&phba->hbalock, flags);
11038
11039 while (!list_empty(&completions)) {
11040 list_remove_head(&completions, buf_ptr,
11041 struct lpfc_dmabuf, list);
11042 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
11043 kfree(buf_ptr);
11044 }
11045
James Smart523128e2018-09-10 10:30:46 -070011046 /* Enable softirqs again, done with phba->hbalock */
11047 local_bh_enable();
11048
dea31012005-04-17 16:05:31 -050011049 /* Return any active mbox cmds */
11050 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050011051
James Smartda0436e2009-05-22 14:51:39 -040011052 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050011053 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040011054 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050011055
James Smartda0436e2009-05-22 14:51:39 -040011056 return 1;
11057}
James Smart92d7f7b2007-06-17 19:56:38 -050011058
James Smartda0436e2009-05-22 14:51:39 -040011059/**
James Smart3621a712009-04-06 18:47:14 -040011060 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040011061 * @srcp: Source memory pointer.
11062 * @destp: Destination memory pointer.
11063 * @cnt: Number of words required to be copied.
11064 *
11065 * This function is used for copying data between driver memory
11066 * and the SLI memory. This function also changes the endianness
11067 * of each word if native endianness is different from SLI
11068 * endianness. This function can be called with or without
11069 * lock.
11070 **/
dea31012005-04-17 16:05:31 -050011071void
11072lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
11073{
11074 uint32_t *src = srcp;
11075 uint32_t *dest = destp;
11076 uint32_t ldata;
11077 int i;
11078
11079 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
11080 ldata = *src;
11081 ldata = le32_to_cpu(ldata);
11082 *dest = ldata;
11083 src++;
11084 dest++;
11085 }
11086}
11087
James Smarte59058c2008-08-24 21:49:00 -040011088
11089/**
James Smarta0c87cb2009-07-19 10:01:10 -040011090 * lpfc_sli_bemem_bcopy - SLI memory copy function
11091 * @srcp: Source memory pointer.
11092 * @destp: Destination memory pointer.
11093 * @cnt: Number of words required to be copied.
11094 *
11095 * This function is used for copying data between a data structure
11096 * with big endian representation to local endianness.
11097 * This function can be called with or without lock.
11098 **/
11099void
11100lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
11101{
11102 uint32_t *src = srcp;
11103 uint32_t *dest = destp;
11104 uint32_t ldata;
11105 int i;
11106
11107 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
11108 ldata = *src;
11109 ldata = be32_to_cpu(ldata);
11110 *dest = ldata;
11111 src++;
11112 dest++;
11113 }
11114}
11115
11116/**
James Smart3621a712009-04-06 18:47:14 -040011117 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040011118 * @phba: Pointer to HBA context object.
11119 * @pring: Pointer to driver SLI ring object.
11120 * @mp: Pointer to driver buffer object.
11121 *
11122 * This function is called with no lock held.
11123 * It always return zero after adding the buffer to the postbufq
11124 * buffer list.
11125 **/
dea31012005-04-17 16:05:31 -050011126int
James Smart2e0fef82007-06-17 19:56:36 -050011127lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11128 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050011129{
11130 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
11131 later */
James Smart2e0fef82007-06-17 19:56:36 -050011132 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011133 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050011134 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050011135 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011136 return 0;
11137}
11138
James Smarte59058c2008-08-24 21:49:00 -040011139/**
James Smart3621a712009-04-06 18:47:14 -040011140 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040011141 * @phba: Pointer to HBA context object.
11142 *
11143 * When HBQ is enabled, buffers are searched based on tags. This function
11144 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
11145 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
11146 * does not conflict with tags of buffer posted for unsolicited events.
11147 * The function returns the allocated tag. The function is called with
11148 * no locks held.
11149 **/
James Smart76bb24e2007-10-27 13:38:00 -040011150uint32_t
11151lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
11152{
11153 spin_lock_irq(&phba->hbalock);
11154 phba->buffer_tag_count++;
11155 /*
11156 * Always set the QUE_BUFTAG_BIT to distiguish between
11157 * a tag assigned by HBQ.
11158 */
11159 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
11160 spin_unlock_irq(&phba->hbalock);
11161 return phba->buffer_tag_count;
11162}
11163
James Smarte59058c2008-08-24 21:49:00 -040011164/**
James Smart3621a712009-04-06 18:47:14 -040011165 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040011166 * @phba: Pointer to HBA context object.
11167 * @pring: Pointer to driver SLI ring object.
11168 * @tag: Buffer tag.
11169 *
11170 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
11171 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
11172 * iocb is posted to the response ring with the tag of the buffer.
11173 * This function searches the pring->postbufq list using the tag
11174 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
11175 * iocb. If the buffer is found then lpfc_dmabuf object of the
11176 * buffer is returned to the caller else NULL is returned.
11177 * This function is called with no lock held.
11178 **/
James Smart76bb24e2007-10-27 13:38:00 -040011179struct lpfc_dmabuf *
11180lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11181 uint32_t tag)
11182{
11183 struct lpfc_dmabuf *mp, *next_mp;
11184 struct list_head *slp = &pring->postbufq;
11185
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011186 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040011187 spin_lock_irq(&phba->hbalock);
11188 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
11189 if (mp->buffer_tag == tag) {
11190 list_del_init(&mp->list);
11191 pring->postbufq_cnt--;
11192 spin_unlock_irq(&phba->hbalock);
11193 return mp;
11194 }
11195 }
11196
11197 spin_unlock_irq(&phba->hbalock);
11198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040011199 "0402 Cannot find virtual addr for buffer tag on "
James Smart32350662019-08-14 16:57:06 -070011200 "ring %d Data x%lx x%px x%px x%x\n",
James Smart76bb24e2007-10-27 13:38:00 -040011201 pring->ringno, (unsigned long) tag,
11202 slp->next, slp->prev, pring->postbufq_cnt);
11203
11204 return NULL;
11205}
dea31012005-04-17 16:05:31 -050011206
James Smarte59058c2008-08-24 21:49:00 -040011207/**
James Smart3621a712009-04-06 18:47:14 -040011208 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040011209 * @phba: Pointer to HBA context object.
11210 * @pring: Pointer to driver SLI ring object.
11211 * @phys: DMA address of the buffer.
11212 *
11213 * This function searches the buffer list using the dma_address
11214 * of unsolicited event to find the driver's lpfc_dmabuf object
11215 * corresponding to the dma_address. The function returns the
11216 * lpfc_dmabuf object if a buffer is found else it returns NULL.
11217 * This function is called by the ct and els unsolicited event
11218 * handlers to get the buffer associated with the unsolicited
11219 * event.
11220 *
11221 * This function is called with no lock held.
11222 **/
dea31012005-04-17 16:05:31 -050011223struct lpfc_dmabuf *
11224lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11225 dma_addr_t phys)
11226{
11227 struct lpfc_dmabuf *mp, *next_mp;
11228 struct list_head *slp = &pring->postbufq;
11229
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011230 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050011231 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011232 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
11233 if (mp->phys == phys) {
11234 list_del_init(&mp->list);
11235 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050011236 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011237 return mp;
11238 }
11239 }
11240
James Smart2e0fef82007-06-17 19:56:36 -050011241 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011242 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040011243 "0410 Cannot find virtual addr for mapped buf on "
James Smart32350662019-08-14 16:57:06 -070011244 "ring %d Data x%llx x%px x%px x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011245 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050011246 slp->next, slp->prev, pring->postbufq_cnt);
11247 return NULL;
11248}
11249
James Smarte59058c2008-08-24 21:49:00 -040011250/**
James Smart3621a712009-04-06 18:47:14 -040011251 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040011252 * @phba: Pointer to HBA context object.
11253 * @cmdiocb: Pointer to driver command iocb object.
11254 * @rspiocb: Pointer to driver response iocb object.
11255 *
11256 * This function is the completion handler for the abort iocbs for
11257 * ELS commands. This function is called from the ELS ring event
11258 * handler with no lock held. This function frees memory resources
11259 * associated with the abort iocb.
11260 **/
dea31012005-04-17 16:05:31 -050011261static void
James Smart2e0fef82007-06-17 19:56:36 -050011262lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11263 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050011264{
James Smart2e0fef82007-06-17 19:56:36 -050011265 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040011266 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050011267 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040011268
11269 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050011270
11271 /*
11272 * Assume that the port already completed and returned, or
11273 * will return the iocb. Just Log the message.
11274 */
James Smart2680eea2007-04-25 09:52:55 -040011275 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
11276 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
11277
James Smart2e0fef82007-06-17 19:56:36 -050011278 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040011279 if (phba->sli_rev < LPFC_SLI_REV4) {
James Smartfaa832e2018-07-31 17:23:18 -070011280 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11281 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11282 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11283 spin_unlock_irq(&phba->hbalock);
11284 goto release_iocb;
11285 }
James Smart45ed1192009-10-02 15:17:02 -040011286 if (abort_iotag != 0 &&
11287 abort_iotag <= phba->sli.last_iotag)
11288 abort_iocb =
11289 phba->sli.iocbq_lookup[abort_iotag];
11290 } else
11291 /* For sli4 the abort_tag is the XRI,
11292 * so the abort routine puts the iotag of the iocb
11293 * being aborted in the context field of the abort
11294 * IOCB.
11295 */
11296 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040011297
James Smart2a9bf3d2010-06-07 15:24:45 -040011298 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
James Smart32350662019-08-14 16:57:06 -070011299 "0327 Cannot abort els iocb x%px "
James Smart2a9bf3d2010-06-07 15:24:45 -040011300 "with tag %x context %x, abort status %x, "
11301 "abort code %x\n",
11302 abort_iocb, abort_iotag, abort_context,
11303 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040011304
James Smartff78d8f2011-12-13 13:21:35 -050011305 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040011306 }
James Smartfaa832e2018-07-31 17:23:18 -070011307release_iocb:
James Bottomley604a3e32005-10-29 10:28:33 -050011308 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050011309 return;
11310}
11311
James Smarte59058c2008-08-24 21:49:00 -040011312/**
James Smart3621a712009-04-06 18:47:14 -040011313 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040011314 * @phba: Pointer to HBA context object.
11315 * @cmdiocb: Pointer to driver command iocb object.
11316 * @rspiocb: Pointer to driver response iocb object.
11317 *
11318 * The function is called from SLI ring event handler with no
11319 * lock held. This function is the completion handler for ELS commands
11320 * which are aborted. The function frees memory resources used for
11321 * the aborted ELS commands.
11322 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011323static void
11324lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11325 struct lpfc_iocbq *rspiocb)
11326{
11327 IOCB_t *irsp = &rspiocb->iocb;
11328
11329 /* ELS cmd tag <ulpIoTag> completes */
11330 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040011331 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050011332 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011333 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050011334 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050011335 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11336 lpfc_ct_free_iocb(phba, cmdiocb);
11337 else
11338 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050011339 return;
11340}
11341
James Smarte59058c2008-08-24 21:49:00 -040011342/**
James Smart5af5eee2010-10-22 11:06:38 -040011343 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040011344 * @phba: Pointer to HBA context object.
11345 * @pring: Pointer to driver SLI ring object.
11346 * @cmdiocb: Pointer to driver command iocb object.
11347 *
James Smart5af5eee2010-10-22 11:06:38 -040011348 * This function issues an abort iocb for the provided command iocb down to
11349 * the port. Other than the case the outstanding command iocb is an abort
11350 * request, this function issues abort out unconditionally. This function is
11351 * called with hbalock held. The function returns 0 when it fails due to
11352 * memory allocation failure or when the command iocb is an abort request.
Dick Kennedy88acb4d2020-05-01 14:43:07 -070011353 * The hbalock is asserted held in the code path calling this routine.
James Smarte59058c2008-08-24 21:49:00 -040011354 **/
James Smart5af5eee2010-10-22 11:06:38 -040011355static int
11356lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050011357 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050011358{
James Smart2e0fef82007-06-17 19:56:36 -050011359 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011360 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050011361 IOCB_t *icmd = NULL;
11362 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011363 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040011364 unsigned long iflags;
James Smartfaa832e2018-07-31 17:23:18 -070011365 struct lpfc_nodelist *ndlp;
James Smart07951072007-04-25 09:51:38 -040011366
James Smart92d7f7b2007-06-17 19:56:38 -050011367 /*
11368 * There are certain command types we don't want to abort. And we
11369 * don't want to abort commands that are already in the process of
11370 * being aborted.
James Smart07951072007-04-25 09:51:38 -040011371 */
11372 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050011373 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050011374 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11375 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040011376 return 0;
11377
dea31012005-04-17 16:05:31 -050011378 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050011379 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011380 if (abtsiocbp == NULL)
11381 return 0;
dea31012005-04-17 16:05:31 -050011382
James Smart07951072007-04-25 09:51:38 -040011383 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050011384 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040011385 */
11386 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11387
dea31012005-04-17 16:05:31 -050011388 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040011389 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11390 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040011391 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040011392 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040011393 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
James Smartfaa832e2018-07-31 17:23:18 -070011394 } else {
James Smartda0436e2009-05-22 14:51:39 -040011395 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
James Smartfaa832e2018-07-31 17:23:18 -070011396 if (pring->ringno == LPFC_ELS_RING) {
11397 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11398 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11399 }
11400 }
dea31012005-04-17 16:05:31 -050011401 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040011402 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050011403
James Smart5ffc2662009-11-18 15:39:44 -050011404 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011405 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011406 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
11407 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011408 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11409 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011410
James Smart2e0fef82007-06-17 19:56:36 -050011411 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040011412 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11413 else
11414 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11415
11416 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080011417 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040011418
James Smarte8b62012007-08-02 11:10:09 -040011419 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11420 "0339 Abort xri x%x, original iotag x%x, "
11421 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040011422 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040011423 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040011424 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040011425
11426 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011427 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11428 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040011429 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040011430 /* Note: both hbalock and ring_lock need to be set here */
11431 spin_lock_irqsave(&pring->ring_lock, iflags);
11432 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11433 abtsiocbp, 0);
11434 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11435 } else {
11436 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11437 abtsiocbp, 0);
11438 }
James Smart07951072007-04-25 09:51:38 -040011439
James Smartd7c255b2008-08-24 21:50:00 -040011440 if (retval)
11441 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040011442
11443 /*
11444 * Caller to this routine should check for IOCB_ERROR
11445 * and handle it properly. This routine no longer removes
11446 * iocb off txcmplq and call compl in case of IOCB_ERROR.
11447 */
11448 return retval;
11449}
11450
11451/**
11452 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
11453 * @phba: Pointer to HBA context object.
11454 * @pring: Pointer to driver SLI ring object.
11455 * @cmdiocb: Pointer to driver command iocb object.
11456 *
11457 * This function issues an abort iocb for the provided command iocb. In case
11458 * of unloading, the abort iocb will not be issued to commands on the ELS
11459 * ring. Instead, the callback function shall be changed to those commands
11460 * so that nothing happens when them finishes. This function is called with
11461 * hbalock held. The function returns 0 when the command iocb is an abort
11462 * request.
11463 **/
11464int
11465lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11466 struct lpfc_iocbq *cmdiocb)
11467{
11468 struct lpfc_vport *vport = cmdiocb->vport;
11469 int retval = IOCB_ERROR;
11470 IOCB_t *icmd = NULL;
11471
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011472 lockdep_assert_held(&phba->hbalock);
11473
James Smart5af5eee2010-10-22 11:06:38 -040011474 /*
11475 * There are certain command types we don't want to abort. And we
11476 * don't want to abort commands that are already in the process of
11477 * being aborted.
11478 */
11479 icmd = &cmdiocb->iocb;
11480 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
11481 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
11482 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
11483 return 0;
11484
Dick Kennedy1234a6d2017-09-29 17:34:29 -070011485 if (!pring) {
11486 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11487 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11488 else
11489 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11490 goto abort_iotag_exit;
11491 }
11492
James Smart5af5eee2010-10-22 11:06:38 -040011493 /*
11494 * If we're unloading, don't abort iocb on the ELS ring, but change
11495 * the callback so that nothing happens when it finishes.
11496 */
11497 if ((vport->load_flag & FC_UNLOADING) &&
11498 (pring->ringno == LPFC_ELS_RING)) {
11499 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11500 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11501 else
11502 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11503 goto abort_iotag_exit;
11504 }
11505
11506 /* Now, we try to issue the abort to the cmdiocb out */
11507 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
11508
James Smart07951072007-04-25 09:51:38 -040011509abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050011510 /*
11511 * Caller to this routine should check for IOCB_ERROR
11512 * and handle it properly. This routine no longer removes
11513 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040011514 */
James Smart2e0fef82007-06-17 19:56:36 -050011515 return retval;
dea31012005-04-17 16:05:31 -050011516}
11517
James Smarte59058c2008-08-24 21:49:00 -040011518/**
James Smart5af5eee2010-10-22 11:06:38 -040011519 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11520 * @phba: pointer to lpfc HBA data structure.
11521 *
11522 * This routine will abort all pending and outstanding iocbs to an HBA.
11523 **/
11524void
11525lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11526{
11527 struct lpfc_sli *psli = &phba->sli;
11528 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011529 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011530 int i;
11531
James Smart895427b2017-02-12 13:52:30 -080011532 if (phba->sli_rev != LPFC_SLI_REV4) {
11533 for (i = 0; i < psli->num_rings; i++) {
11534 pring = &psli->sli3_ring[i];
11535 lpfc_sli_abort_iocb_ring(phba, pring);
11536 }
11537 return;
11538 }
11539 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11540 pring = qp->pring;
11541 if (!pring)
11542 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011543 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011544 }
11545}
11546
11547/**
James Smart3621a712009-04-06 18:47:14 -040011548 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011549 * @iocbq: Pointer to driver iocb object.
11550 * @vport: Pointer to driver virtual port object.
11551 * @tgt_id: SCSI ID of the target.
11552 * @lun_id: LUN ID of the scsi device.
11553 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11554 *
James Smart3621a712009-04-06 18:47:14 -040011555 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011556 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11557 * 0 if the filtering criteria is met for the given iocb and will return
11558 * 1 if the filtering criteria is not met.
11559 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11560 * given iocb is for the SCSI device specified by vport, tgt_id and
11561 * lun_id parameter.
11562 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11563 * given iocb is for the SCSI target specified by vport and tgt_id
11564 * parameters.
11565 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11566 * given iocb is for the SCSI host associated with the given vport.
11567 * This function is called with no locks held.
11568 **/
dea31012005-04-17 16:05:31 -050011569static int
James Smart51ef4c22007-08-02 11:10:31 -040011570lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11571 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011572 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011573{
James Smartc4908502019-01-28 11:14:28 -080011574 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011575 int rc = 1;
11576
James Smartb0e83012018-06-26 08:24:29 -070011577 if (iocbq->vport != vport)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011578 return rc;
11579
James Smartb0e83012018-06-26 08:24:29 -070011580 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
11581 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
James Smart51ef4c22007-08-02 11:10:31 -040011582 return rc;
11583
James Smartc4908502019-01-28 11:14:28 -080011584 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011585
James Smart495a7142008-06-14 22:52:59 -040011586 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011587 return rc;
11588
11589 switch (ctx_cmd) {
11590 case LPFC_CTX_LUN:
James Smartb0e83012018-06-26 08:24:29 -070011591 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011592 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11593 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011594 rc = 0;
11595 break;
11596 case LPFC_CTX_TGT:
James Smartb0e83012018-06-26 08:24:29 -070011597 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011598 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011599 rc = 0;
11600 break;
dea31012005-04-17 16:05:31 -050011601 case LPFC_CTX_HOST:
11602 rc = 0;
11603 break;
11604 default:
11605 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011606 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011607 break;
11608 }
11609
11610 return rc;
11611}
11612
James Smarte59058c2008-08-24 21:49:00 -040011613/**
James Smart3621a712009-04-06 18:47:14 -040011614 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011615 * @vport: Pointer to virtual port.
11616 * @tgt_id: SCSI ID of the target.
11617 * @lun_id: LUN ID of the scsi device.
11618 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11619 *
11620 * This function returns number of FCP commands pending for the vport.
11621 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11622 * commands pending on the vport associated with SCSI device specified
11623 * by tgt_id and lun_id parameters.
11624 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11625 * commands pending on the vport associated with SCSI target specified
11626 * by tgt_id parameter.
11627 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11628 * commands pending on the vport.
11629 * This function returns the number of iocbs which satisfy the filter.
11630 * This function is called without any lock held.
11631 **/
dea31012005-04-17 16:05:31 -050011632int
James Smart51ef4c22007-08-02 11:10:31 -040011633lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11634 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011635{
James Smart51ef4c22007-08-02 11:10:31 -040011636 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011637 struct lpfc_iocbq *iocbq;
11638 int sum, i;
dea31012005-04-17 16:05:31 -050011639
Johannes Thumshirn31979002016-07-18 16:06:03 +020011640 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011641 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11642 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011643
James Smart51ef4c22007-08-02 11:10:31 -040011644 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11645 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011646 sum++;
dea31012005-04-17 16:05:31 -050011647 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011648 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011649
dea31012005-04-17 16:05:31 -050011650 return sum;
11651}
11652
James Smarte59058c2008-08-24 21:49:00 -040011653/**
James Smart3621a712009-04-06 18:47:14 -040011654 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011655 * @phba: Pointer to HBA context object
11656 * @cmdiocb: Pointer to command iocb object.
11657 * @rspiocb: Pointer to response iocb object.
11658 *
11659 * This function is called when an aborted FCP iocb completes. This
11660 * function is called by the ring event handler with no lock held.
11661 * This function frees the iocb.
11662 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011663void
James Smart2e0fef82007-06-17 19:56:36 -050011664lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11665 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011666{
James Smartcb69f7d2011-12-13 13:21:57 -050011667 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011668 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011669 "original iotag x%x, abort cmd iotag x%x "
11670 "status 0x%x, reason 0x%x\n",
11671 cmdiocb->iocb.un.acxri.abortContextTag,
11672 cmdiocb->iocb.un.acxri.abortIoTag,
11673 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11674 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011675 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011676 return;
11677}
11678
James Smarte59058c2008-08-24 21:49:00 -040011679/**
James Smart3621a712009-04-06 18:47:14 -040011680 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011681 * @vport: Pointer to virtual port.
11682 * @pring: Pointer to driver SLI ring object.
11683 * @tgt_id: SCSI ID of the target.
11684 * @lun_id: LUN ID of the scsi device.
11685 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11686 *
11687 * This function sends an abort command for every SCSI command
11688 * associated with the given virtual port pending on the ring
11689 * filtered by lpfc_sli_validate_fcp_iocb function.
11690 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11691 * FCP iocbs associated with lun specified by tgt_id and lun_id
11692 * parameters
11693 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11694 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11695 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11696 * FCP iocbs associated with virtual port.
11697 * This function returns number of iocbs it failed to abort.
11698 * This function is called with no locks held.
11699 **/
dea31012005-04-17 16:05:31 -050011700int
James Smart51ef4c22007-08-02 11:10:31 -040011701lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11702 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011703{
James Smart51ef4c22007-08-02 11:10:31 -040011704 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011705 struct lpfc_iocbq *iocbq;
11706 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011707 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011708 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011709 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011710 int i;
dea31012005-04-17 16:05:31 -050011711
James Smartb0e83012018-06-26 08:24:29 -070011712 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070011713 if (phba->hba_flag & HBA_IOQ_FLUSH)
James Smartb0e83012018-06-26 08:24:29 -070011714 return errcnt;
11715
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011716 for (i = 1; i <= phba->sli.last_iotag; i++) {
11717 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011718
James Smart51ef4c22007-08-02 11:10:31 -040011719 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011720 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011721 continue;
11722
James Smartafbd8d82013-09-06 12:22:13 -040011723 /*
11724 * If the iocbq is already being aborted, don't take a second
11725 * action, but do count it.
11726 */
11727 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11728 continue;
11729
dea31012005-04-17 16:05:31 -050011730 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011731 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011732 if (abtsiocb == NULL) {
11733 errcnt++;
11734 continue;
11735 }
dea31012005-04-17 16:05:31 -050011736
James Smartafbd8d82013-09-06 12:22:13 -040011737 /* indicate the IO is being aborted by the driver. */
11738 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11739
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011740 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011741 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11742 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011743 if (phba->sli_rev == LPFC_SLI_REV4)
11744 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11745 else
11746 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011747 abtsiocb->iocb.ulpLe = 1;
11748 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011749 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011750
James Smart5ffc2662009-11-18 15:39:44 -050011751 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011752 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011753 if (iocbq->iocb_flag & LPFC_IO_FCP)
11754 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011755 if (iocbq->iocb_flag & LPFC_IO_FOF)
11756 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011757
James Smart2e0fef82007-06-17 19:56:36 -050011758 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011759 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11760 else
11761 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11762
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011763 /* Setup callback routine and issue the command. */
11764 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011765 if (phba->sli_rev == LPFC_SLI_REV4) {
11766 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11767 if (!pring_s4)
11768 continue;
11769 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11770 abtsiocb, 0);
11771 } else
11772 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11773 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011774 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011775 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011776 errcnt++;
11777 continue;
11778 }
11779 }
11780
11781 return errcnt;
11782}
11783
James Smarte59058c2008-08-24 21:49:00 -040011784/**
James Smart98912dda2014-04-04 13:52:31 -040011785 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11786 * @vport: Pointer to virtual port.
11787 * @pring: Pointer to driver SLI ring object.
11788 * @tgt_id: SCSI ID of the target.
11789 * @lun_id: LUN ID of the scsi device.
11790 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11791 *
11792 * This function sends an abort command for every SCSI command
11793 * associated with the given virtual port pending on the ring
11794 * filtered by lpfc_sli_validate_fcp_iocb function.
11795 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11796 * FCP iocbs associated with lun specified by tgt_id and lun_id
11797 * parameters
11798 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11799 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11800 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11801 * FCP iocbs associated with virtual port.
11802 * This function returns number of iocbs it aborted .
11803 * This function is called with no locks held right after a taskmgmt
11804 * command is sent.
11805 **/
11806int
11807lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11808 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11809{
11810 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -080011811 struct lpfc_io_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011812 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011813 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011814 struct lpfc_iocbq *iocbq;
11815 IOCB_t *icmd;
11816 int sum, i, ret_val;
11817 unsigned long iflags;
James Smartc2017262019-01-28 11:14:37 -080011818 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart98912dda2014-04-04 13:52:31 -040011819
James Smart59c68ea2018-04-09 14:24:25 -070011820 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011821
11822 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070011823 if (phba->hba_flag & HBA_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070011824 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011825 return 0;
11826 }
11827 sum = 0;
11828
11829 for (i = 1; i <= phba->sli.last_iotag; i++) {
11830 iocbq = phba->sli.iocbq_lookup[i];
11831
11832 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11833 cmd) != 0)
11834 continue;
11835
James Smartc2017262019-01-28 11:14:37 -080011836 /* Guard against IO completion being called at same time */
11837 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
11838 spin_lock(&lpfc_cmd->buf_lock);
11839
11840 if (!lpfc_cmd->pCmd) {
11841 spin_unlock(&lpfc_cmd->buf_lock);
11842 continue;
11843 }
11844
11845 if (phba->sli_rev == LPFC_SLI_REV4) {
11846 pring_s4 =
James Smartc00f62e2019-08-14 16:57:11 -070011847 phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
James Smartc2017262019-01-28 11:14:37 -080011848 if (!pring_s4) {
11849 spin_unlock(&lpfc_cmd->buf_lock);
11850 continue;
11851 }
11852 /* Note: both hbalock and ring_lock must be set here */
11853 spin_lock(&pring_s4->ring_lock);
11854 }
11855
James Smart98912dda2014-04-04 13:52:31 -040011856 /*
11857 * If the iocbq is already being aborted, don't take a second
11858 * action, but do count it.
11859 */
James Smartc2017262019-01-28 11:14:37 -080011860 if ((iocbq->iocb_flag & LPFC_DRIVER_ABORTED) ||
11861 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
11862 if (phba->sli_rev == LPFC_SLI_REV4)
11863 spin_unlock(&pring_s4->ring_lock);
11864 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011865 continue;
James Smartc2017262019-01-28 11:14:37 -080011866 }
James Smart98912dda2014-04-04 13:52:31 -040011867
11868 /* issue ABTS for this IOCB based on iotag */
11869 abtsiocbq = __lpfc_sli_get_iocbq(phba);
James Smartc2017262019-01-28 11:14:37 -080011870 if (!abtsiocbq) {
11871 if (phba->sli_rev == LPFC_SLI_REV4)
11872 spin_unlock(&pring_s4->ring_lock);
11873 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011874 continue;
James Smartc2017262019-01-28 11:14:37 -080011875 }
James Smart98912dda2014-04-04 13:52:31 -040011876
11877 icmd = &iocbq->iocb;
11878 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11879 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11880 if (phba->sli_rev == LPFC_SLI_REV4)
11881 abtsiocbq->iocb.un.acxri.abortIoTag =
11882 iocbq->sli4_xritag;
11883 else
11884 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11885 abtsiocbq->iocb.ulpLe = 1;
11886 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11887 abtsiocbq->vport = vport;
11888
11889 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011890 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011891 if (iocbq->iocb_flag & LPFC_IO_FCP)
11892 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011893 if (iocbq->iocb_flag & LPFC_IO_FOF)
11894 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011895
James Smart8c50d252014-09-03 12:58:16 -040011896 ndlp = lpfc_cmd->rdata->pnode;
11897
11898 if (lpfc_is_link_up(phba) &&
11899 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011900 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11901 else
11902 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11903
11904 /* Setup callback routine and issue the command. */
11905 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11906
11907 /*
11908 * Indicate the IO is being aborted by the driver and set
11909 * the caller's flag into the aborted IO.
11910 */
11911 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11912
11913 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -040011914 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11915 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070011916 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040011917 } else {
11918 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11919 abtsiocbq, 0);
11920 }
11921
James Smartc2017262019-01-28 11:14:37 -080011922 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040011923
11924 if (ret_val == IOCB_ERROR)
11925 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11926 else
11927 sum++;
11928 }
James Smart59c68ea2018-04-09 14:24:25 -070011929 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040011930 return sum;
11931}
11932
11933/**
James Smart3621a712009-04-06 18:47:14 -040011934 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011935 * @phba: Pointer to HBA context object.
11936 * @cmdiocbq: Pointer to command iocb.
11937 * @rspiocbq: Pointer to response iocb.
11938 *
11939 * This function is the completion handler for iocbs issued using
11940 * lpfc_sli_issue_iocb_wait function. This function is called by the
11941 * ring event handler function without any lock held. This function
11942 * can be called from both worker thread context and interrupt
11943 * context. This function also can be called from other thread which
11944 * cleans up the SLI layer objects.
11945 * This function copy the contents of the response iocb to the
11946 * response iocb memory object provided by the caller of
11947 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11948 * sleeps for the iocb completion.
11949 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011950static void
11951lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11952 struct lpfc_iocbq *cmdiocbq,
11953 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011954{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011955 wait_queue_head_t *pdone_q;
11956 unsigned long iflags;
James Smartc4908502019-01-28 11:14:28 -080011957 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011958
James Smart2e0fef82007-06-17 19:56:36 -050011959 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011960 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11961
11962 /*
11963 * A time out has occurred for the iocb. If a time out
11964 * completion handler has been supplied, call it. Otherwise,
11965 * just free the iocbq.
11966 */
11967
11968 spin_unlock_irqrestore(&phba->hbalock, iflags);
11969 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11970 cmdiocbq->wait_iocb_cmpl = NULL;
11971 if (cmdiocbq->iocb_cmpl)
11972 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11973 else
11974 lpfc_sli_release_iocbq(phba, cmdiocbq);
11975 return;
11976 }
11977
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011978 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11979 if (cmdiocbq->context2 && rspiocbq)
11980 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11981 &rspiocbq->iocb, sizeof(IOCB_t));
11982
James Smart0f65ff62010-02-26 14:14:23 -050011983 /* Set the exchange busy flag for task management commands */
11984 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11985 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smartc4908502019-01-28 11:14:28 -080011986 lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
James Smart0f65ff62010-02-26 14:14:23 -050011987 cur_iocbq);
James Smart324e1c42019-10-18 14:18:21 -070011988 if (rspiocbq && (rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY))
11989 lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
11990 else
11991 lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
James Smart0f65ff62010-02-26 14:14:23 -050011992 }
11993
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011994 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011995 if (pdone_q)
11996 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011997 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011998 return;
11999}
12000
James Smarte59058c2008-08-24 21:49:00 -040012001/**
James Smartd11e31d2009-06-10 17:23:06 -040012002 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
12003 * @phba: Pointer to HBA context object..
12004 * @piocbq: Pointer to command iocb.
12005 * @flag: Flag to test.
12006 *
12007 * This routine grabs the hbalock and then test the iocb_flag to
12008 * see if the passed in flag is set.
12009 * Returns:
12010 * 1 if flag is set.
12011 * 0 if flag is not set.
12012 **/
12013static int
12014lpfc_chk_iocb_flg(struct lpfc_hba *phba,
12015 struct lpfc_iocbq *piocbq, uint32_t flag)
12016{
12017 unsigned long iflags;
12018 int ret;
12019
12020 spin_lock_irqsave(&phba->hbalock, iflags);
12021 ret = piocbq->iocb_flag & flag;
12022 spin_unlock_irqrestore(&phba->hbalock, iflags);
12023 return ret;
12024
12025}
12026
12027/**
James Smart3621a712009-04-06 18:47:14 -040012028 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040012029 * @phba: Pointer to HBA context object..
12030 * @pring: Pointer to sli ring.
12031 * @piocb: Pointer to command iocb.
12032 * @prspiocbq: Pointer to response iocb.
12033 * @timeout: Timeout in number of seconds.
12034 *
12035 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040012036 * iocb to complete. The iocb_cmpl field of the shall be used
12037 * to handle iocbs which time out. If the field is NULL, the
12038 * function shall free the iocbq structure. If more clean up is
12039 * needed, the caller is expected to provide a completion function
12040 * that will provide the needed clean up. If the iocb command is
12041 * not completed within timeout seconds, the function will either
12042 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
12043 * completion function set in the iocb_cmpl field and then return
12044 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
12045 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040012046 * The function waits for the iocb completion using an
12047 * non-interruptible wait.
12048 * This function will sleep while waiting for iocb completion.
12049 * So, this function should not be called from any context which
12050 * does not allow sleeping. Due to the same reason, this function
12051 * cannot be called with interrupt disabled.
12052 * This function assumes that the iocb completions occur while
12053 * this function sleep. So, this function cannot be called from
12054 * the thread which process iocb completion for this ring.
12055 * This function clears the iocb_flag of the iocb object before
12056 * issuing the iocb and the iocb completion handler sets this
12057 * flag and wakes this thread when the iocb completes.
12058 * The contents of the response iocb will be copied to prspiocbq
12059 * by the completion handler when the command completes.
12060 * This function returns IOCB_SUCCESS when success.
12061 * This function is called with no lock held.
12062 **/
dea31012005-04-17 16:05:31 -050012063int
James Smart2e0fef82007-06-17 19:56:36 -050012064lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040012065 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050012066 struct lpfc_iocbq *piocb,
12067 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012068 uint32_t timeout)
dea31012005-04-17 16:05:31 -050012069{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080012070 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012071 long timeleft, timeout_req = 0;
12072 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012073 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050012074 struct lpfc_iocbq *iocb;
12075 int txq_cnt = 0;
12076 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080012077 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040012078 unsigned long iflags;
12079 bool iocb_completed = true;
12080
James Smart895427b2017-02-12 13:52:30 -080012081 if (phba->sli_rev >= LPFC_SLI_REV4)
12082 pring = lpfc_sli4_calc_ring(phba, piocb);
12083 else
12084 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050012085 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012086 * If the caller has provided a response iocbq buffer, then context2
12087 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050012088 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012089 if (prspiocbq) {
12090 if (piocb->context2)
12091 return IOCB_ERROR;
12092 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050012093 }
12094
James Smart5a0916b2013-07-15 18:31:42 -040012095 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012096 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
12097 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040012098 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050012099
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012100 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050012101 if (lpfc_readl(phba->HCregaddr, &creg_val))
12102 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012103 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
12104 writel(creg_val, phba->HCregaddr);
12105 readl(phba->HCregaddr); /* flush */
12106 }
12107
James Smart2a9bf3d2010-06-07 15:24:45 -040012108 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
12109 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012110 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040012111 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012112 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040012113 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012114 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040012115 spin_lock_irqsave(&phba->hbalock, iflags);
12116 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050012117
James Smart5a0916b2013-07-15 18:31:42 -040012118 /*
12119 * IOCB timed out. Inform the wake iocb wait
12120 * completion function and set local status
12121 */
12122
12123 iocb_completed = false;
12124 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
12125 }
12126 spin_unlock_irqrestore(&phba->hbalock, iflags);
12127 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040012128 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012129 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040012130 /* Note: we are not indicating if the IOCB has a success
12131 * status or not - that's for the caller to check.
12132 * IOCB_SUCCESS means just that the command was sent and
12133 * completed. Not that it completed successfully.
12134 * */
James Smart7054a602007-04-25 09:52:34 -040012135 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012136 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012137 "0338 IOCB wait timeout error - no "
12138 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012139 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040012140 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012141 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012142 "0330 IOCB wake NOT set, "
12143 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012144 timeout, (timeleft / jiffies));
12145 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050012146 }
James Smart2a9bf3d2010-06-07 15:24:45 -040012147 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012148 if (phba->cfg_log_verbose & LOG_SLI) {
12149 list_for_each_entry(iocb, &pring->txq, list) {
12150 txq_cnt++;
12151 }
12152 list_for_each_entry(iocb, &pring->txcmplq, list) {
12153 txcmplq_cnt++;
12154 }
12155 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12156 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
12157 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
12158 }
James Smart2a9bf3d2010-06-07 15:24:45 -040012159 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012160 } else {
12161 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012162 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012163 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012164 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050012165 }
12166
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012167 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050012168 if (lpfc_readl(phba->HCregaddr, &creg_val))
12169 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012170 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
12171 writel(creg_val, phba->HCregaddr);
12172 readl(phba->HCregaddr); /* flush */
12173 }
12174
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012175 if (prspiocbq)
12176 piocb->context2 = NULL;
12177
12178 piocb->context_un.wait_queue = NULL;
12179 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050012180 return retval;
12181}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012182
James Smarte59058c2008-08-24 21:49:00 -040012183/**
James Smart3621a712009-04-06 18:47:14 -040012184 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040012185 * @phba: Pointer to HBA context object.
12186 * @pmboxq: Pointer to driver mailbox object.
12187 * @timeout: Timeout in number of seconds.
12188 *
12189 * This function issues the mailbox to firmware and waits for the
12190 * mailbox command to complete. If the mailbox command is not
12191 * completed within timeout seconds, it returns MBX_TIMEOUT.
12192 * The function waits for the mailbox completion using an
12193 * interruptible wait. If the thread is woken up due to a
12194 * signal, MBX_TIMEOUT error is returned to the caller. Caller
12195 * should not free the mailbox resources, if this function returns
12196 * MBX_TIMEOUT.
12197 * This function will sleep while waiting for mailbox completion.
12198 * So, this function should not be called from any context which
12199 * does not allow sleeping. Due to the same reason, this function
12200 * cannot be called with interrupt disabled.
12201 * This function assumes that the mailbox completion occurs while
12202 * this function sleep. So, this function cannot be called from
12203 * the worker thread which processes mailbox completion.
12204 * This function is called in the context of HBA management
12205 * applications.
12206 * This function returns MBX_SUCCESS when successful.
12207 * This function is called with no lock held.
12208 **/
dea31012005-04-17 16:05:31 -050012209int
James Smart2e0fef82007-06-17 19:56:36 -050012210lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050012211 uint32_t timeout)
12212{
James Smarte29d74f2018-03-05 12:04:07 -080012213 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050012214 int retval;
James Smart858c9f62007-06-17 19:56:39 -050012215 unsigned long flag;
dea31012005-04-17 16:05:31 -050012216
James Smart495a7142008-06-14 22:52:59 -040012217 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050012218 /* setup wake call as IOCB callback */
12219 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050012220
James Smarte29d74f2018-03-05 12:04:07 -080012221 /* setup context3 field to pass wait_queue pointer to wake function */
12222 init_completion(&mbox_done);
12223 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050012224 /* now issue the command */
12225 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050012226 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080012227 wait_for_completion_timeout(&mbox_done,
12228 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040012229
James Smart858c9f62007-06-17 19:56:39 -050012230 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080012231 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040012232 /*
12233 * if LPFC_MBX_WAKE flag is set the mailbox is completed
12234 * else do not free the resources.
12235 */
James Smartd7c47992010-06-08 18:31:54 -040012236 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050012237 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040012238 } else {
James Smart7054a602007-04-25 09:52:34 -040012239 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050012240 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12241 }
12242 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050012243 }
dea31012005-04-17 16:05:31 -050012244 return retval;
12245}
12246
James Smarte59058c2008-08-24 21:49:00 -040012247/**
James Smart3772a992009-05-22 14:50:54 -040012248 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040012249 * @phba: Pointer to HBA context.
12250 *
James Smart3772a992009-05-22 14:50:54 -040012251 * This function is called to shutdown the driver's mailbox sub-system.
12252 * It first marks the mailbox sub-system is in a block state to prevent
12253 * the asynchronous mailbox command from issued off the pending mailbox
12254 * command queue. If the mailbox command sub-system shutdown is due to
12255 * HBA error conditions such as EEH or ERATT, this routine shall invoke
12256 * the mailbox sub-system flush routine to forcefully bring down the
12257 * mailbox sub-system. Otherwise, if it is due to normal condition (such
12258 * as with offline or HBA function reset), this routine will wait for the
12259 * outstanding mailbox command to complete before invoking the mailbox
12260 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040012261 **/
James Smart3772a992009-05-22 14:50:54 -040012262void
James Smart618a5232012-06-12 13:54:36 -040012263lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040012264{
James Smart3772a992009-05-22 14:50:54 -040012265 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040012266 unsigned long timeout;
12267
James Smart618a5232012-06-12 13:54:36 -040012268 if (mbx_action == LPFC_MBX_NO_WAIT) {
12269 /* delay 100ms for port state */
12270 msleep(100);
12271 lpfc_sli_mbox_sys_flush(phba);
12272 return;
12273 }
James Smarta183a152011-10-10 21:32:43 -040012274 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050012275
James Smart523128e2018-09-10 10:30:46 -070012276 /* Disable softirqs, including timers from obtaining phba->hbalock */
12277 local_bh_disable();
12278
James Smart3772a992009-05-22 14:50:54 -040012279 spin_lock_irq(&phba->hbalock);
12280 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040012281
12282 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040012283 /* Determine how long we might wait for the active mailbox
12284 * command to be gracefully completed by firmware.
12285 */
James Smarta183a152011-10-10 21:32:43 -040012286 if (phba->sli.mbox_active)
12287 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12288 phba->sli.mbox_active) *
12289 1000) + jiffies;
12290 spin_unlock_irq(&phba->hbalock);
12291
James Smart523128e2018-09-10 10:30:46 -070012292 /* Enable softirqs again, done with phba->hbalock */
12293 local_bh_enable();
12294
James Smart3772a992009-05-22 14:50:54 -040012295 while (phba->sli.mbox_active) {
12296 /* Check active mailbox complete status every 2ms */
12297 msleep(2);
12298 if (time_after(jiffies, timeout))
12299 /* Timeout, let the mailbox flush routine to
12300 * forcefully release active mailbox command
12301 */
12302 break;
12303 }
James Smart523128e2018-09-10 10:30:46 -070012304 } else {
James Smartd7069f02012-03-01 22:36:29 -050012305 spin_unlock_irq(&phba->hbalock);
12306
James Smart523128e2018-09-10 10:30:46 -070012307 /* Enable softirqs again, done with phba->hbalock */
12308 local_bh_enable();
12309 }
12310
James Smart3772a992009-05-22 14:50:54 -040012311 lpfc_sli_mbox_sys_flush(phba);
12312}
12313
12314/**
12315 * lpfc_sli_eratt_read - read sli-3 error attention events
12316 * @phba: Pointer to HBA context.
12317 *
12318 * This function is called to read the SLI3 device error attention registers
12319 * for possible error attention events. The caller must hold the hostlock
12320 * with spin_lock_irq().
12321 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012322 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040012323 * Register and returns 0 otherwise.
12324 **/
12325static int
12326lpfc_sli_eratt_read(struct lpfc_hba *phba)
12327{
James Smarted957682007-06-17 19:56:37 -050012328 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040012329
James Smart3772a992009-05-22 14:50:54 -040012330 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050012331 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12332 goto unplug_err;
12333
James Smart3772a992009-05-22 14:50:54 -040012334 if (ha_copy & HA_ERATT) {
12335 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050012336 if (lpfc_sli_read_hs(phba))
12337 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040012338
James Smart3772a992009-05-22 14:50:54 -040012339 /* Check if there is a deferred error condition is active */
12340 if ((HS_FFER1 & phba->work_hs) &&
12341 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012342 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040012343 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040012344 /* Clear all interrupt enable conditions */
12345 writel(0, phba->HCregaddr);
12346 readl(phba->HCregaddr);
12347 }
12348
12349 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040012350 phba->work_ha |= HA_ERATT;
12351 /* Indicate polling handles this ERATT */
12352 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012353 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012354 }
James Smart3772a992009-05-22 14:50:54 -040012355 return 0;
James Smart9940b972011-03-11 16:06:12 -050012356
12357unplug_err:
12358 /* Set the driver HS work bitmap */
12359 phba->work_hs |= UNPLUG_ERR;
12360 /* Set the driver HA work bitmap */
12361 phba->work_ha |= HA_ERATT;
12362 /* Indicate polling handles this ERATT */
12363 phba->hba_flag |= HBA_ERATT_HANDLED;
12364 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012365}
12366
James Smarte59058c2008-08-24 21:49:00 -040012367/**
James Smartda0436e2009-05-22 14:51:39 -040012368 * lpfc_sli4_eratt_read - read sli-4 error attention events
12369 * @phba: Pointer to HBA context.
12370 *
12371 * This function is called to read the SLI4 device error attention registers
12372 * for possible error attention events. The caller must hold the hostlock
12373 * with spin_lock_irq().
12374 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012375 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040012376 * Register and returns 0 otherwise.
12377 **/
12378static int
12379lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12380{
12381 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050012382 uint32_t if_type, portsmphr;
12383 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040012384
James Smart2fcee4b2010-12-15 17:57:46 -050012385 /*
12386 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040012387 * registers for error attention. This can be changed later.
12388 */
James Smart2fcee4b2010-12-15 17:57:46 -050012389 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12390 switch (if_type) {
12391 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050012392 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12393 &uerr_sta_lo) ||
12394 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12395 &uerr_sta_hi)) {
12396 phba->work_hs |= UNPLUG_ERR;
12397 phba->work_ha |= HA_ERATT;
12398 phba->hba_flag |= HBA_ERATT_HANDLED;
12399 return 1;
12400 }
James Smart2fcee4b2010-12-15 17:57:46 -050012401 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12402 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
12403 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12404 "1423 HBA Unrecoverable error: "
12405 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12406 "ue_mask_lo_reg=0x%x, "
12407 "ue_mask_hi_reg=0x%x\n",
12408 uerr_sta_lo, uerr_sta_hi,
12409 phba->sli4_hba.ue_mask_lo,
12410 phba->sli4_hba.ue_mask_hi);
12411 phba->work_status[0] = uerr_sta_lo;
12412 phba->work_status[1] = uerr_sta_hi;
12413 phba->work_ha |= HA_ERATT;
12414 phba->hba_flag |= HBA_ERATT_HANDLED;
12415 return 1;
12416 }
12417 break;
12418 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080012419 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050012420 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12421 &portstat_reg.word0) ||
12422 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12423 &portsmphr)){
12424 phba->work_hs |= UNPLUG_ERR;
12425 phba->work_ha |= HA_ERATT;
12426 phba->hba_flag |= HBA_ERATT_HANDLED;
12427 return 1;
12428 }
James Smart2fcee4b2010-12-15 17:57:46 -050012429 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12430 phba->work_status[0] =
12431 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12432 phba->work_status[1] =
12433 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
12434 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050012435 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050012436 "port status reg 0x%x, "
12437 "port smphr reg 0x%x, "
12438 "error 1=0x%x, error 2=0x%x\n",
12439 portstat_reg.word0,
12440 portsmphr,
12441 phba->work_status[0],
12442 phba->work_status[1]);
12443 phba->work_ha |= HA_ERATT;
12444 phba->hba_flag |= HBA_ERATT_HANDLED;
12445 return 1;
12446 }
12447 break;
12448 case LPFC_SLI_INTF_IF_TYPE_1:
12449 default:
James Smarta747c9c2009-11-18 15:41:10 -050012450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050012451 "2886 HBA Error Attention on unsupported "
12452 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050012453 return 1;
James Smartda0436e2009-05-22 14:51:39 -040012454 }
James Smart2fcee4b2010-12-15 17:57:46 -050012455
James Smartda0436e2009-05-22 14:51:39 -040012456 return 0;
12457}
12458
12459/**
James Smart3621a712009-04-06 18:47:14 -040012460 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040012461 * @phba: Pointer to HBA context.
12462 *
James Smart3772a992009-05-22 14:50:54 -040012463 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040012464 * error attention register bit for error attention events.
12465 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012466 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040012467 * Register and returns 0 otherwise.
12468 **/
12469int
12470lpfc_sli_check_eratt(struct lpfc_hba *phba)
12471{
12472 uint32_t ha_copy;
12473
12474 /* If somebody is waiting to handle an eratt, don't process it
12475 * here. The brdkill function will do this.
12476 */
12477 if (phba->link_flag & LS_IGNORE_ERATT)
12478 return 0;
12479
12480 /* Check if interrupt handler handles this ERATT */
12481 spin_lock_irq(&phba->hbalock);
12482 if (phba->hba_flag & HBA_ERATT_HANDLED) {
12483 /* Interrupt handler has handled ERATT */
12484 spin_unlock_irq(&phba->hbalock);
12485 return 0;
12486 }
12487
James Smarta257bf92009-04-06 18:48:10 -040012488 /*
12489 * If there is deferred error attention, do not check for error
12490 * attention
12491 */
12492 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12493 spin_unlock_irq(&phba->hbalock);
12494 return 0;
12495 }
12496
James Smart3772a992009-05-22 14:50:54 -040012497 /* If PCI channel is offline, don't process it */
12498 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040012499 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012500 return 0;
12501 }
12502
12503 switch (phba->sli_rev) {
12504 case LPFC_SLI_REV2:
12505 case LPFC_SLI_REV3:
12506 /* Read chip Host Attention (HA) register */
12507 ha_copy = lpfc_sli_eratt_read(phba);
12508 break;
James Smartda0436e2009-05-22 14:51:39 -040012509 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050012510 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040012511 ha_copy = lpfc_sli4_eratt_read(phba);
12512 break;
James Smart3772a992009-05-22 14:50:54 -040012513 default:
12514 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12515 "0299 Invalid SLI revision (%d)\n",
12516 phba->sli_rev);
12517 ha_copy = 0;
12518 break;
James Smart93996272008-08-24 21:50:30 -040012519 }
12520 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012521
12522 return ha_copy;
12523}
12524
12525/**
12526 * lpfc_intr_state_check - Check device state for interrupt handling
12527 * @phba: Pointer to HBA context.
12528 *
12529 * This inline routine checks whether a device or its PCI slot is in a state
12530 * that the interrupt should be handled.
12531 *
12532 * This function returns 0 if the device or the PCI slot is in a state that
12533 * interrupt should be handled, otherwise -EIO.
12534 */
12535static inline int
12536lpfc_intr_state_check(struct lpfc_hba *phba)
12537{
12538 /* If the pci channel is offline, ignore all the interrupts */
12539 if (unlikely(pci_channel_offline(phba->pcidev)))
12540 return -EIO;
12541
12542 /* Update device level interrupt statistics */
12543 phba->sli.slistat.sli_intr++;
12544
12545 /* Ignore all interrupts during initialization. */
12546 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12547 return -EIO;
12548
James Smart93996272008-08-24 21:50:30 -040012549 return 0;
12550}
12551
12552/**
James Smart3772a992009-05-22 14:50:54 -040012553 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012554 * @irq: Interrupt number.
12555 * @dev_id: The device context pointer.
12556 *
James Smart93996272008-08-24 21:50:30 -040012557 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012558 * service routine when device with SLI-3 interface spec is enabled with
12559 * MSI-X multi-message interrupt mode and there are slow-path events in
12560 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12561 * interrupt mode, this function is called as part of the device-level
12562 * interrupt handler. When the PCI slot is in error recovery or the HBA
12563 * is undergoing initialization, the interrupt handler will not process
12564 * the interrupt. The link attention and ELS ring attention events are
12565 * handled by the worker thread. The interrupt handler signals the worker
12566 * thread and returns for these events. This function is called without
12567 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012568 * structures.
12569 *
12570 * This function returns IRQ_HANDLED when interrupt is handled else it
12571 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012572 **/
dea31012005-04-17 16:05:31 -050012573irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012574lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012575{
James Smart2e0fef82007-06-17 19:56:36 -050012576 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012577 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012578 uint32_t work_ha_copy;
12579 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012580 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012581 uint32_t control;
12582
James Smart92d7f7b2007-06-17 19:56:38 -050012583 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012584 struct lpfc_vport *vport;
12585 struct lpfc_nodelist *ndlp;
12586 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012587 LPFC_MBOXQ_t *pmb;
12588 int rc;
12589
dea31012005-04-17 16:05:31 -050012590 /*
12591 * Get the driver's phba structure from the dev_id and
12592 * assume the HBA is not interrupting.
12593 */
James Smart93996272008-08-24 21:50:30 -040012594 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012595
12596 if (unlikely(!phba))
12597 return IRQ_NONE;
12598
dea31012005-04-17 16:05:31 -050012599 /*
James Smart93996272008-08-24 21:50:30 -040012600 * Stuff needs to be attented to when this function is invoked as an
12601 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012602 */
James Smart93996272008-08-24 21:50:30 -040012603 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012604 /* Check device state for handling interrupt */
12605 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012606 return IRQ_NONE;
12607 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012608 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012609 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12610 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012611 /* If somebody is waiting to handle an eratt don't process it
12612 * here. The brdkill function will do this.
12613 */
12614 if (phba->link_flag & LS_IGNORE_ERATT)
12615 ha_copy &= ~HA_ERATT;
12616 /* Check the need for handling ERATT in interrupt handler */
12617 if (ha_copy & HA_ERATT) {
12618 if (phba->hba_flag & HBA_ERATT_HANDLED)
12619 /* ERATT polling has handled ERATT */
12620 ha_copy &= ~HA_ERATT;
12621 else
12622 /* Indicate interrupt handler handles ERATT */
12623 phba->hba_flag |= HBA_ERATT_HANDLED;
12624 }
James Smarta257bf92009-04-06 18:48:10 -040012625
12626 /*
12627 * If there is deferred error attention, do not check for any
12628 * interrupt.
12629 */
12630 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012631 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012632 return IRQ_NONE;
12633 }
12634
James Smart93996272008-08-24 21:50:30 -040012635 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012636 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12637 goto unplug_error;
12638
James Smarta747c9c2009-11-18 15:41:10 -050012639 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12640 HC_LAINT_ENA | HC_ERINT_ENA),
12641 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012642 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12643 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012644 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012645 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012646 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012647 } else
12648 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012649
dea31012005-04-17 16:05:31 -050012650 work_ha_copy = ha_copy & phba->work_ha_mask;
12651
James Smart93996272008-08-24 21:50:30 -040012652 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012653 if (work_ha_copy & HA_LATT) {
12654 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12655 /*
12656 * Turn off Link Attention interrupts
12657 * until CLEAR_LA done
12658 */
James Smart5b75da22008-12-04 22:39:35 -050012659 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012660 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012661 if (lpfc_readl(phba->HCregaddr, &control))
12662 goto unplug_error;
dea31012005-04-17 16:05:31 -050012663 control &= ~HC_LAINT_ENA;
12664 writel(control, phba->HCregaddr);
12665 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012666 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012667 }
12668 else
12669 work_ha_copy &= ~HA_LATT;
12670 }
12671
James Smart93996272008-08-24 21:50:30 -040012672 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012673 /*
12674 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12675 * the only slow ring.
12676 */
12677 status = (work_ha_copy &
12678 (HA_RXMASK << (4*LPFC_ELS_RING)));
12679 status >>= (4*LPFC_ELS_RING);
12680 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012681 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012682 if (lpfc_readl(phba->HCregaddr, &control))
12683 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012684
12685 lpfc_debugfs_slow_ring_trc(phba,
12686 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12687 control, status,
12688 (uint32_t)phba->sli.slistat.sli_intr);
12689
James Smart858c9f62007-06-17 19:56:39 -050012690 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012691 lpfc_debugfs_slow_ring_trc(phba,
12692 "ISR Disable ring:"
12693 "pwork:x%x hawork:x%x wait:x%x",
12694 phba->work_ha, work_ha_copy,
12695 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012696 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012697
James Smart858c9f62007-06-17 19:56:39 -050012698 control &=
12699 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012700 writel(control, phba->HCregaddr);
12701 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012702 }
James Smarta58cbd52007-08-02 11:09:43 -040012703 else {
12704 lpfc_debugfs_slow_ring_trc(phba,
12705 "ISR slow ring: pwork:"
12706 "x%x hawork:x%x wait:x%x",
12707 phba->work_ha, work_ha_copy,
12708 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012709 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012710 }
James Smart5b75da22008-12-04 22:39:35 -050012711 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012712 }
12713 }
James Smart5b75da22008-12-04 22:39:35 -050012714 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012715 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012716 if (lpfc_sli_read_hs(phba))
12717 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012718 /*
12719 * Check if there is a deferred error condition
12720 * is active
12721 */
12722 if ((HS_FFER1 & phba->work_hs) &&
12723 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012724 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12725 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012726 phba->hba_flag |= DEFER_ERATT;
12727 /* Clear all interrupt enable conditions */
12728 writel(0, phba->HCregaddr);
12729 readl(phba->HCregaddr);
12730 }
12731 }
12732
James Smart93996272008-08-24 21:50:30 -040012733 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012734 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012735 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012736 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012737 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012738
12739 /* First check out the status word */
12740 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12741 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012742 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012743 /*
12744 * Stray Mailbox Interrupt, mbxCommand <cmd>
12745 * mbxStatus <status>
12746 */
James Smart09372822008-01-11 01:52:54 -050012747 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012748 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012749 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012750 "Interrupt mbxCommand x%x "
12751 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012752 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012753 pmbox->mbxCommand,
12754 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012755 /* clear mailbox attention bit */
12756 work_ha_copy &= ~HA_MBATT;
12757 } else {
James Smart97eab632008-04-07 10:16:05 -040012758 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012759 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012760 phba->last_completion_time = jiffies;
12761 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012762 if (pmb->mbox_cmpl) {
12763 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12764 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012765 if (pmb->out_ext_byte_len &&
James Smart3e1f0712018-11-29 16:09:29 -080012766 pmb->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -040012767 lpfc_sli_pcimem_bcopy(
12768 phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -080012769 pmb->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -040012770 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012771 }
James Smart09372822008-01-11 01:52:54 -050012772 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12773 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12774
12775 lpfc_debugfs_disc_trc(vport,
12776 LPFC_DISC_TRC_MBOX_VPORT,
12777 "MBOX dflt rpi: : "
12778 "status:x%x rpi:x%x",
12779 (uint32_t)pmbox->mbxStatus,
12780 pmbox->un.varWords[0], 0);
12781
12782 if (!pmbox->mbxStatus) {
12783 mp = (struct lpfc_dmabuf *)
James Smart3e1f0712018-11-29 16:09:29 -080012784 (pmb->ctx_buf);
James Smart09372822008-01-11 01:52:54 -050012785 ndlp = (struct lpfc_nodelist *)
James Smart3e1f0712018-11-29 16:09:29 -080012786 pmb->ctx_ndlp;
James Smart09372822008-01-11 01:52:54 -050012787
12788 /* Reg_LOGIN of dflt RPI was
12789 * successful. new lets get
12790 * rid of the RPI using the
12791 * same mbox buffer.
12792 */
12793 lpfc_unreg_login(phba,
12794 vport->vpi,
12795 pmbox->un.varWords[0],
12796 pmb);
12797 pmb->mbox_cmpl =
12798 lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080012799 pmb->ctx_buf = mp;
12800 pmb->ctx_ndlp = ndlp;
James Smart09372822008-01-11 01:52:54 -050012801 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012802 rc = lpfc_sli_issue_mbox(phba,
12803 pmb,
12804 MBX_NOWAIT);
12805 if (rc != MBX_BUSY)
12806 lpfc_printf_log(phba,
12807 KERN_ERR,
12808 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012809 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012810 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012811 if (rc != MBX_NOT_FINISHED)
12812 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012813 }
12814 }
James Smart5b75da22008-12-04 22:39:35 -050012815 spin_lock_irqsave(
12816 &phba->pport->work_port_lock,
12817 iflag);
James Smart09372822008-01-11 01:52:54 -050012818 phba->pport->work_port_events &=
12819 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012820 spin_unlock_irqrestore(
12821 &phba->pport->work_port_lock,
12822 iflag);
James Smart09372822008-01-11 01:52:54 -050012823 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012824 }
James Smart97eab632008-04-07 10:16:05 -040012825 } else
James Smart5b75da22008-12-04 22:39:35 -050012826 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012827
James Smart92d7f7b2007-06-17 19:56:38 -050012828 if ((work_ha_copy & HA_MBATT) &&
12829 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012830send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012831 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012832 do {
12833 rc = lpfc_sli_issue_mbox(phba, NULL,
12834 MBX_NOWAIT);
12835 } while (rc == MBX_NOT_FINISHED);
12836 if (rc != MBX_SUCCESS)
12837 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12838 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012839 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012840 }
12841
James Smart5b75da22008-12-04 22:39:35 -050012842 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012843 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012844 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012845 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012846 }
James Smart93996272008-08-24 21:50:30 -040012847 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012848unplug_error:
12849 spin_unlock_irqrestore(&phba->hbalock, iflag);
12850 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012851
James Smart3772a992009-05-22 14:50:54 -040012852} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012853
12854/**
James Smart3772a992009-05-22 14:50:54 -040012855 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012856 * @irq: Interrupt number.
12857 * @dev_id: The device context pointer.
12858 *
12859 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012860 * service routine when device with SLI-3 interface spec is enabled with
12861 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12862 * ring event in the HBA. However, when the device is enabled with either
12863 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12864 * device-level interrupt handler. When the PCI slot is in error recovery
12865 * or the HBA is undergoing initialization, the interrupt handler will not
12866 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12867 * the intrrupt context. This function is called without any lock held.
12868 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012869 *
12870 * This function returns IRQ_HANDLED when interrupt is handled else it
12871 * returns IRQ_NONE.
12872 **/
12873irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012874lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012875{
12876 struct lpfc_hba *phba;
12877 uint32_t ha_copy;
12878 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012879 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012880 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012881
12882 /* Get the driver's phba structure from the dev_id and
12883 * assume the HBA is not interrupting.
12884 */
12885 phba = (struct lpfc_hba *) dev_id;
12886
12887 if (unlikely(!phba))
12888 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012889
12890 /*
James Smart93996272008-08-24 21:50:30 -040012891 * Stuff needs to be attented to when this function is invoked as an
12892 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012893 */
James Smart93996272008-08-24 21:50:30 -040012894 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012895 /* Check device state for handling interrupt */
12896 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012897 return IRQ_NONE;
12898 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012899 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12900 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012901 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012902 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012903 /*
12904 * If there is deferred error attention, do not check for
12905 * any interrupt.
12906 */
12907 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012908 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012909 return IRQ_NONE;
12910 }
James Smart93996272008-08-24 21:50:30 -040012911 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12912 phba->HAregaddr);
12913 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012914 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012915 } else
12916 ha_copy = phba->ha_copy;
12917
12918 /*
12919 * Process all events on FCP ring. Take the optimized path for FCP IO.
12920 */
12921 ha_copy &= ~(phba->work_ha_mask);
12922
12923 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012924 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012925 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012926 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012927 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012928
12929 if (phba->cfg_multi_ring_support == 2) {
12930 /*
James Smart93996272008-08-24 21:50:30 -040012931 * Process all events on extra ring. Take the optimized path
12932 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012933 */
James Smart93996272008-08-24 21:50:30 -040012934 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012935 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012936 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012937 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012938 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012939 status);
12940 }
12941 }
dea31012005-04-17 16:05:31 -050012942 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012943} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012944
James Smart93996272008-08-24 21:50:30 -040012945/**
James Smart3772a992009-05-22 14:50:54 -040012946 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012947 * @irq: Interrupt number.
12948 * @dev_id: The device context pointer.
12949 *
James Smart3772a992009-05-22 14:50:54 -040012950 * This function is the HBA device-level interrupt handler to device with
12951 * SLI-3 interface spec, called from the PCI layer when either MSI or
12952 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12953 * requires driver attention. This function invokes the slow-path interrupt
12954 * attention handling function and fast-path interrupt attention handling
12955 * function in turn to process the relevant HBA attention events. This
12956 * function is called without any lock held. It gets the hbalock to access
12957 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012958 *
12959 * This function returns IRQ_HANDLED when interrupt is handled, else it
12960 * returns IRQ_NONE.
12961 **/
12962irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012963lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012964{
12965 struct lpfc_hba *phba;
12966 irqreturn_t sp_irq_rc, fp_irq_rc;
12967 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012968 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012969
12970 /*
12971 * Get the driver's phba structure from the dev_id and
12972 * assume the HBA is not interrupting.
12973 */
12974 phba = (struct lpfc_hba *) dev_id;
12975
12976 if (unlikely(!phba))
12977 return IRQ_NONE;
12978
James Smart3772a992009-05-22 14:50:54 -040012979 /* Check device state for handling interrupt */
12980 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012981 return IRQ_NONE;
12982
12983 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012984 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12985 spin_unlock(&phba->hbalock);
12986 return IRQ_HANDLED;
12987 }
12988
James Smart93996272008-08-24 21:50:30 -040012989 if (unlikely(!phba->ha_copy)) {
12990 spin_unlock(&phba->hbalock);
12991 return IRQ_NONE;
12992 } else if (phba->ha_copy & HA_ERATT) {
12993 if (phba->hba_flag & HBA_ERATT_HANDLED)
12994 /* ERATT polling has handled ERATT */
12995 phba->ha_copy &= ~HA_ERATT;
12996 else
12997 /* Indicate interrupt handler handles ERATT */
12998 phba->hba_flag |= HBA_ERATT_HANDLED;
12999 }
13000
James Smarta257bf92009-04-06 18:48:10 -040013001 /*
13002 * If there is deferred error attention, do not check for any interrupt.
13003 */
13004 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020013005 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040013006 return IRQ_NONE;
13007 }
13008
James Smart93996272008-08-24 21:50:30 -040013009 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050013010 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
13011 spin_unlock(&phba->hbalock);
13012 return IRQ_HANDLED;
13013 }
James Smarta747c9c2009-11-18 15:41:10 -050013014 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
13015 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
13016 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040013017 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050013018 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040013019 readl(phba->HAregaddr); /* flush */
13020 spin_unlock(&phba->hbalock);
13021
13022 /*
13023 * Invokes slow-path host attention interrupt handling as appropriate.
13024 */
13025
13026 /* status of events with mailbox and link attention */
13027 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
13028
13029 /* status of events with ELS ring */
13030 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
13031 status2 >>= (4*LPFC_ELS_RING);
13032
13033 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040013034 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040013035 else
13036 sp_irq_rc = IRQ_NONE;
13037
13038 /*
13039 * Invoke fast-path host attention interrupt handling as appropriate.
13040 */
13041
13042 /* status of events with FCP ring */
13043 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13044 status1 >>= (4*LPFC_FCP_RING);
13045
13046 /* status of events with extra ring */
13047 if (phba->cfg_multi_ring_support == 2) {
13048 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
13049 status2 >>= (4*LPFC_EXTRA_RING);
13050 } else
13051 status2 = 0;
13052
13053 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040013054 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040013055 else
13056 fp_irq_rc = IRQ_NONE;
13057
13058 /* Return device-level interrupt handling status */
13059 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040013060} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040013061
13062/**
James Smart4f774512009-05-22 14:52:35 -040013063 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
13064 * @phba: pointer to lpfc hba data structure.
13065 *
13066 * This routine is invoked by the worker thread to process all the pending
13067 * SLI4 els abort xri events.
13068 **/
13069void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
13070{
13071 struct lpfc_cq_event *cq_event;
13072
13073 /* First, declare the els xri abort event has been handled */
13074 spin_lock_irq(&phba->hbalock);
13075 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
13076 spin_unlock_irq(&phba->hbalock);
13077 /* Now, handle all the els xri abort events */
13078 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
13079 /* Get the first event from the head of the event queue */
13080 spin_lock_irq(&phba->hbalock);
13081 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
13082 cq_event, struct lpfc_cq_event, list);
13083 spin_unlock_irq(&phba->hbalock);
13084 /* Notify aborted XRI for ELS work queue */
13085 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
13086 /* Free the event processed back to the free pool */
13087 lpfc_sli4_cq_event_release(phba, cq_event);
13088 }
13089}
13090
James Smart341af102010-01-26 23:07:37 -050013091/**
13092 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
13093 * @phba: pointer to lpfc hba data structure
13094 * @pIocbIn: pointer to the rspiocbq
13095 * @pIocbOut: pointer to the cmdiocbq
13096 * @wcqe: pointer to the complete wcqe
13097 *
13098 * This routine transfers the fields of a command iocbq to a response iocbq
13099 * by copying all the IOCB fields from command iocbq and transferring the
13100 * completion status information from the complete wcqe.
13101 **/
James Smart4f774512009-05-22 14:52:35 -040013102static void
James Smart341af102010-01-26 23:07:37 -050013103lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
13104 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040013105 struct lpfc_iocbq *pIocbOut,
13106 struct lpfc_wcqe_complete *wcqe)
13107{
James Smartaf227412013-10-10 12:23:10 -040013108 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050013109 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040013110 uint32_t status, max_response;
13111 struct lpfc_dmabuf *dmabuf;
13112 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040013113 size_t offset = offsetof(struct lpfc_iocbq, iocb);
13114
13115 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
13116 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040013117 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050013118 status = bf_get(lpfc_wcqe_c_status, wcqe);
13119 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040013120 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
13121 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
13122 pIocbIn->iocb.un.fcpi.fcpi_parm =
13123 pIocbOut->iocb.un.fcpi.fcpi_parm -
13124 wcqe->total_data_placed;
13125 else
13126 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050013127 else {
James Smart4f774512009-05-22 14:52:35 -040013128 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040013129 switch (pIocbOut->iocb.ulpCommand) {
13130 case CMD_ELS_REQUEST64_CR:
13131 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
13132 bpl = (struct ulp_bde64 *)dmabuf->virt;
13133 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
13134 max_response = bde.tus.f.bdeSize;
13135 break;
13136 case CMD_GEN_REQUEST64_CR:
13137 max_response = 0;
13138 if (!pIocbOut->context3)
13139 break;
13140 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
13141 sizeof(struct ulp_bde64);
13142 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
13143 bpl = (struct ulp_bde64 *)dmabuf->virt;
13144 for (i = 0; i < numBdes; i++) {
13145 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
13146 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
13147 max_response += bde.tus.f.bdeSize;
13148 }
13149 break;
13150 default:
13151 max_response = wcqe->total_data_placed;
13152 break;
13153 }
13154 if (max_response < wcqe->total_data_placed)
13155 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
13156 else
13157 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
13158 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050013159 }
James Smart341af102010-01-26 23:07:37 -050013160
James Smartacd68592012-01-18 16:25:09 -050013161 /* Convert BG errors for completion status */
13162 if (status == CQE_STATUS_DI_ERROR) {
13163 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
13164
13165 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
13166 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
13167 else
13168 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
13169
13170 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
13171 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
13172 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13173 BGS_GUARD_ERR_MASK;
13174 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
13175 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13176 BGS_APPTAG_ERR_MASK;
13177 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
13178 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13179 BGS_REFTAG_ERR_MASK;
13180
13181 /* Check to see if there was any good data before the error */
13182 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
13183 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13184 BGS_HI_WATER_MARK_PRESENT_MASK;
13185 pIocbIn->iocb.unsli3.sli3_bg.bghm =
13186 wcqe->total_data_placed;
13187 }
13188
13189 /*
13190 * Set ALL the error bits to indicate we don't know what
13191 * type of error it is.
13192 */
13193 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
13194 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13195 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
13196 BGS_GUARD_ERR_MASK);
13197 }
13198
James Smart341af102010-01-26 23:07:37 -050013199 /* Pick up HBA exchange busy condition */
13200 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
13201 spin_lock_irqsave(&phba->hbalock, iflags);
13202 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
13203 spin_unlock_irqrestore(&phba->hbalock, iflags);
13204 }
James Smart4f774512009-05-22 14:52:35 -040013205}
13206
13207/**
James Smart45ed1192009-10-02 15:17:02 -040013208 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
13209 * @phba: Pointer to HBA context object.
13210 * @wcqe: Pointer to work-queue completion queue entry.
13211 *
13212 * This routine handles an ELS work-queue completion event and construct
13213 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
13214 * discovery engine to handle.
13215 *
13216 * Return: Pointer to the receive IOCBQ, NULL otherwise.
13217 **/
13218static struct lpfc_iocbq *
13219lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
13220 struct lpfc_iocbq *irspiocbq)
13221{
James Smart895427b2017-02-12 13:52:30 -080013222 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040013223 struct lpfc_iocbq *cmdiocbq;
13224 struct lpfc_wcqe_complete *wcqe;
13225 unsigned long iflags;
13226
James Smart895427b2017-02-12 13:52:30 -080013227 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070013228 if (unlikely(!pring))
13229 return NULL;
James Smart895427b2017-02-12 13:52:30 -080013230
James Smart45ed1192009-10-02 15:17:02 -040013231 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart45ed1192009-10-02 15:17:02 -040013232 pring->stats.iocb_event++;
13233 /* Look up the ELS command IOCB and create pseudo response IOCB */
13234 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13235 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040013236 if (unlikely(!cmdiocbq)) {
13237 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13238 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070013239 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
13240 wcqe->word0, wcqe->total_data_placed,
13241 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040013242 lpfc_sli_release_iocbq(phba, irspiocbq);
13243 return NULL;
13244 }
13245
James Smarte2a8be52019-05-06 17:26:47 -070013246 spin_lock_irqsave(&pring->ring_lock, iflags);
Dick Kennedy401bb412017-09-29 17:34:28 -070013247 /* Put the iocb back on the txcmplq */
13248 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13249 spin_unlock_irqrestore(&pring->ring_lock, iflags);
13250
James Smart45ed1192009-10-02 15:17:02 -040013251 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013252 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040013253
13254 return irspiocbq;
13255}
13256
James Smart8a5ca102017-11-20 16:00:30 -080013257inline struct lpfc_cq_event *
13258lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13259{
13260 struct lpfc_cq_event *cq_event;
13261
13262 /* Allocate a new internal CQ_EVENT entry */
13263 cq_event = lpfc_sli4_cq_event_alloc(phba);
13264 if (!cq_event) {
13265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13266 "0602 Failed to alloc CQ_EVENT entry\n");
13267 return NULL;
13268 }
13269
13270 /* Move the CQE into the event */
13271 memcpy(&cq_event->cqe, entry, size);
13272 return cq_event;
13273}
13274
James Smart45ed1192009-10-02 15:17:02 -040013275/**
Colin Ian King291c2542019-12-18 08:43:01 +000013276 * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
James Smart04c68492009-05-22 14:52:52 -040013277 * @phba: Pointer to HBA context object.
13278 * @cqe: Pointer to mailbox completion queue entry.
13279 *
Colin Ian King291c2542019-12-18 08:43:01 +000013280 * This routine process a mailbox completion queue entry with asynchronous
James Smart04c68492009-05-22 14:52:52 -040013281 * event.
13282 *
13283 * Return: true if work posted to worker thread, otherwise false.
13284 **/
13285static bool
13286lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13287{
13288 struct lpfc_cq_event *cq_event;
13289 unsigned long iflags;
13290
13291 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13292 "0392 Async Event: word0:x%x, word1:x%x, "
13293 "word2:x%x, word3:x%x\n", mcqe->word0,
13294 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13295
James Smart8a5ca102017-11-20 16:00:30 -080013296 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13297 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040013298 return false;
James Smart04c68492009-05-22 14:52:52 -040013299 spin_lock_irqsave(&phba->hbalock, iflags);
13300 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
13301 /* Set the async event flag */
13302 phba->hba_flag |= ASYNC_EVENT;
13303 spin_unlock_irqrestore(&phba->hbalock, iflags);
13304
13305 return true;
13306}
13307
13308/**
13309 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13310 * @phba: Pointer to HBA context object.
13311 * @cqe: Pointer to mailbox completion queue entry.
13312 *
13313 * This routine process a mailbox completion queue entry with mailbox
13314 * completion event.
13315 *
13316 * Return: true if work posted to worker thread, otherwise false.
13317 **/
13318static bool
13319lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13320{
13321 uint32_t mcqe_status;
13322 MAILBOX_t *mbox, *pmbox;
13323 struct lpfc_mqe *mqe;
13324 struct lpfc_vport *vport;
13325 struct lpfc_nodelist *ndlp;
13326 struct lpfc_dmabuf *mp;
13327 unsigned long iflags;
13328 LPFC_MBOXQ_t *pmb;
13329 bool workposted = false;
13330 int rc;
13331
13332 /* If not a mailbox complete MCQE, out by checking mailbox consume */
13333 if (!bf_get(lpfc_trailer_completed, mcqe))
13334 goto out_no_mqe_complete;
13335
13336 /* Get the reference to the active mbox command */
13337 spin_lock_irqsave(&phba->hbalock, iflags);
13338 pmb = phba->sli.mbox_active;
13339 if (unlikely(!pmb)) {
13340 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
13341 "1832 No pending MBOX command to handle\n");
13342 spin_unlock_irqrestore(&phba->hbalock, iflags);
13343 goto out_no_mqe_complete;
13344 }
13345 spin_unlock_irqrestore(&phba->hbalock, iflags);
13346 mqe = &pmb->u.mqe;
13347 pmbox = (MAILBOX_t *)&pmb->u.mqe;
13348 mbox = phba->mbox;
13349 vport = pmb->vport;
13350
13351 /* Reset heartbeat timer */
13352 phba->last_completion_time = jiffies;
13353 del_timer(&phba->sli.mbox_tmo);
13354
13355 /* Move mbox data to caller's mailbox region, do endian swapping */
13356 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070013357 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040013358
James Smart73d91e52011-10-10 21:32:10 -040013359 /*
13360 * For mcqe errors, conditionally move a modified error code to
13361 * the mbox so that the error will not be missed.
13362 */
13363 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13364 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13365 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13366 bf_set(lpfc_mqe_status, mqe,
13367 (LPFC_MBX_ERROR_RANGE | mcqe_status));
13368 }
James Smart04c68492009-05-22 14:52:52 -040013369 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13370 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13371 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13372 "MBOX dflt rpi: status:x%x rpi:x%x",
13373 mcqe_status,
13374 pmbox->un.varWords[0], 0);
13375 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
James Smart3e1f0712018-11-29 16:09:29 -080013376 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
13377 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart04c68492009-05-22 14:52:52 -040013378 /* Reg_LOGIN of dflt RPI was successful. Now lets get
13379 * RID of the PPI using the same mbox buffer.
13380 */
13381 lpfc_unreg_login(phba, vport->vpi,
13382 pmbox->un.varWords[0], pmb);
13383 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080013384 pmb->ctx_buf = mp;
13385 pmb->ctx_ndlp = ndlp;
James Smart04c68492009-05-22 14:52:52 -040013386 pmb->vport = vport;
13387 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13388 if (rc != MBX_BUSY)
13389 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
13390 LOG_SLI, "0385 rc should "
13391 "have been MBX_BUSY\n");
13392 if (rc != MBX_NOT_FINISHED)
13393 goto send_current_mbox;
13394 }
13395 }
13396 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13397 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13398 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13399
13400 /* There is mailbox completion work to do */
13401 spin_lock_irqsave(&phba->hbalock, iflags);
13402 __lpfc_mbox_cmpl_put(phba, pmb);
13403 phba->work_ha |= HA_MBATT;
13404 spin_unlock_irqrestore(&phba->hbalock, iflags);
13405 workposted = true;
13406
13407send_current_mbox:
13408 spin_lock_irqsave(&phba->hbalock, iflags);
13409 /* Release the mailbox command posting token */
13410 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13411 /* Setting active mailbox pointer need to be in sync to flag clear */
13412 phba->sli.mbox_active = NULL;
James Smart07b85822019-09-21 20:58:53 -070013413 if (bf_get(lpfc_trailer_consumed, mcqe))
13414 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
James Smart04c68492009-05-22 14:52:52 -040013415 spin_unlock_irqrestore(&phba->hbalock, iflags);
13416 /* Wake up worker thread to post the next pending mailbox command */
13417 lpfc_worker_wake_up(phba);
James Smart07b85822019-09-21 20:58:53 -070013418 return workposted;
13419
James Smart04c68492009-05-22 14:52:52 -040013420out_no_mqe_complete:
James Smart07b85822019-09-21 20:58:53 -070013421 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart04c68492009-05-22 14:52:52 -040013422 if (bf_get(lpfc_trailer_consumed, mcqe))
13423 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
James Smart07b85822019-09-21 20:58:53 -070013424 spin_unlock_irqrestore(&phba->hbalock, iflags);
13425 return false;
James Smart04c68492009-05-22 14:52:52 -040013426}
13427
13428/**
13429 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13430 * @phba: Pointer to HBA context object.
13431 * @cqe: Pointer to mailbox completion queue entry.
13432 *
13433 * This routine process a mailbox completion queue entry, it invokes the
Colin Ian King291c2542019-12-18 08:43:01 +000013434 * proper mailbox complete handling or asynchronous event handling routine
James Smart04c68492009-05-22 14:52:52 -040013435 * according to the MCQE's async bit.
13436 *
13437 * Return: true if work posted to worker thread, otherwise false.
13438 **/
13439static bool
James Smart32517fc2019-01-28 11:14:33 -080013440lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13441 struct lpfc_cqe *cqe)
James Smart04c68492009-05-22 14:52:52 -040013442{
13443 struct lpfc_mcqe mcqe;
13444 bool workposted;
13445
James Smart32517fc2019-01-28 11:14:33 -080013446 cq->CQ_mbox++;
13447
James Smart04c68492009-05-22 14:52:52 -040013448 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013449 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040013450
13451 /* Invoke the proper event handling routine */
13452 if (!bf_get(lpfc_trailer_async, &mcqe))
13453 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13454 else
13455 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13456 return workposted;
13457}
13458
13459/**
James Smart4f774512009-05-22 14:52:35 -040013460 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13461 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040013462 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040013463 * @wcqe: Pointer to work-queue completion queue entry.
13464 *
13465 * This routine handles an ELS work-queue completion event.
13466 *
13467 * Return: true if work posted to worker thread, otherwise false.
13468 **/
13469static bool
James Smart2a76a282012-08-03 12:35:54 -040013470lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013471 struct lpfc_wcqe_complete *wcqe)
13472{
James Smart4f774512009-05-22 14:52:35 -040013473 struct lpfc_iocbq *irspiocbq;
13474 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040013475 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050013476 int txq_cnt = 0;
13477 int txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040013478
James Smart11f0e342018-05-04 20:37:57 -070013479 /* Check for response status */
13480 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13481 /* Log the error status */
13482 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13483 "0357 ELS CQE error: status=x%x: "
13484 "CQE: %08x %08x %08x %08x\n",
13485 bf_get(lpfc_wcqe_c_status, wcqe),
13486 wcqe->word0, wcqe->total_data_placed,
13487 wcqe->parameter, wcqe->word3);
13488 }
13489
James Smart45ed1192009-10-02 15:17:02 -040013490 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040013491 irspiocbq = lpfc_sli_get_iocbq(phba);
13492 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050013493 if (!list_empty(&pring->txq))
13494 txq_cnt++;
13495 if (!list_empty(&pring->txcmplq))
13496 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040013497 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040013498 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
James Smartff349bc2019-09-21 20:59:05 -070013499 "els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050013500 txq_cnt, phba->iocb_cnt,
James Smart0e9bb8d2013-03-01 16:35:12 -050013501 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040013502 return false;
James Smart4f774512009-05-22 14:52:35 -040013503 }
James Smart4f774512009-05-22 14:52:35 -040013504
James Smart45ed1192009-10-02 15:17:02 -040013505 /* Save off the slow-path queue event for work thread to process */
13506 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040013507 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040013508 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013509 &phba->sli4_hba.sp_queue_event);
13510 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040013511 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013512
James Smart45ed1192009-10-02 15:17:02 -040013513 return true;
James Smart4f774512009-05-22 14:52:35 -040013514}
13515
13516/**
13517 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13518 * @phba: Pointer to HBA context object.
13519 * @wcqe: Pointer to work-queue completion queue entry.
13520 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013521 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013522 * proper WQ release routine to the slow-path WQ.
13523 **/
13524static void
13525lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13526 struct lpfc_wcqe_release *wcqe)
13527{
James Smart2e90f4b2011-12-13 13:22:37 -050013528 /* sanity check on queue memory */
13529 if (unlikely(!phba->sli4_hba.els_wq))
13530 return;
James Smart4f774512009-05-22 14:52:35 -040013531 /* Check for the slow-path ELS work queue */
13532 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13533 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13534 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13535 else
13536 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13537 "2579 Slow-path wqe consume event carries "
13538 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13539 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13540 phba->sli4_hba.els_wq->queue_id);
13541}
13542
13543/**
13544 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13545 * @phba: Pointer to HBA context object.
13546 * @cq: Pointer to a WQ completion queue.
13547 * @wcqe: Pointer to work-queue completion queue entry.
13548 *
13549 * This routine handles an XRI abort event.
13550 *
13551 * Return: true if work posted to worker thread, otherwise false.
13552 **/
13553static bool
13554lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13555 struct lpfc_queue *cq,
13556 struct sli4_wcqe_xri_aborted *wcqe)
13557{
13558 bool workposted = false;
13559 struct lpfc_cq_event *cq_event;
13560 unsigned long iflags;
13561
James Smart4f774512009-05-22 14:52:35 -040013562 switch (cq->subtype) {
James Smartc00f62e2019-08-14 16:57:11 -070013563 case LPFC_IO:
13564 lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
13565 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
13566 /* Notify aborted XRI for NVME work queue */
13567 if (phba->nvmet_support)
13568 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13569 }
James Smart5e5b5112019-01-28 11:14:22 -080013570 workposted = false;
James Smart4f774512009-05-22 14:52:35 -040013571 break;
James Smart422c4cb2017-11-20 16:00:32 -080013572 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013573 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080013574 cq_event = lpfc_cq_event_setup(
13575 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
13576 if (!cq_event)
13577 return false;
James Smart5e5b5112019-01-28 11:14:22 -080013578 cq_event->hdwq = cq->hdwq;
James Smart4f774512009-05-22 14:52:35 -040013579 spin_lock_irqsave(&phba->hbalock, iflags);
13580 list_add_tail(&cq_event->list,
13581 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13582 /* Set the els xri abort event flag */
13583 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
13584 spin_unlock_irqrestore(&phba->hbalock, iflags);
13585 workposted = true;
13586 break;
13587 default:
13588 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080013589 "0603 Invalid CQ subtype %d: "
13590 "%08x %08x %08x %08x\n",
13591 cq->subtype, wcqe->word0, wcqe->parameter,
13592 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013593 workposted = false;
13594 break;
13595 }
13596 return workposted;
13597}
13598
James Smarte817e5d2018-12-13 15:17:53 -080013599#define FC_RCTL_MDS_DIAGS 0xF4
13600
James Smart4f774512009-05-22 14:52:35 -040013601/**
James Smart4d9ab992009-10-02 15:16:39 -040013602 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013603 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013604 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013605 *
James Smart4d9ab992009-10-02 15:16:39 -040013606 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013607 *
13608 * Return: true if work posted to worker thread, otherwise false.
13609 **/
13610static bool
James Smart4d9ab992009-10-02 15:16:39 -040013611lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13612{
13613 bool workposted = false;
James Smarte817e5d2018-12-13 15:17:53 -080013614 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013615 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13616 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013617 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013618 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013619 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013620 unsigned long iflags;
13621
James Smart2e90f4b2011-12-13 13:22:37 -050013622 /* sanity check on queue memory */
13623 if (unlikely(!hrq) || unlikely(!drq))
13624 return workposted;
13625
James Smart7851fe22011-07-22 18:36:52 -040013626 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13627 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13628 else
13629 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13630 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013631 goto out;
13632
13633 status = bf_get(lpfc_rcqe_status, rcqe);
13634 switch (status) {
13635 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13636 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13637 "2537 Receive Frame Truncated!!\n");
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060013638 /* fall through */
James Smart4d9ab992009-10-02 15:16:39 -040013639 case FC_STATUS_RQ_SUCCESS:
13640 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013641 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013642 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13643 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013644 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013645 spin_unlock_irqrestore(&phba->hbalock, iflags);
13646 goto out;
13647 }
James Smartb84daac2012-08-03 12:35:13 -040013648 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013649 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013650 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013651
James Smarte817e5d2018-12-13 15:17:53 -080013652 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13653
13654 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
13655 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
13656 spin_unlock_irqrestore(&phba->hbalock, iflags);
13657 /* Handle MDS Loopback frames */
13658 lpfc_sli4_handle_mds_loopback(phba->pport, dma_buf);
13659 break;
13660 }
13661
13662 /* save off the frame for the work thread to process */
James Smart4d9ab992009-10-02 15:16:39 -040013663 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013664 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013665 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013666 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013667 spin_unlock_irqrestore(&phba->hbalock, iflags);
13668 workposted = true;
13669 break;
James Smart4d9ab992009-10-02 15:16:39 -040013670 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013671 if (phba->nvmet_support) {
13672 tgtp = phba->targetport->private;
13673 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13674 "6402 RQE Error x%x, posted %d err_cnt "
13675 "%d: %x %x %x\n",
13676 status, hrq->RQ_buf_posted,
13677 hrq->RQ_no_posted_buf,
13678 atomic_read(&tgtp->rcv_fcp_cmd_in),
13679 atomic_read(&tgtp->rcv_fcp_cmd_out),
13680 atomic_read(&tgtp->xmt_fcp_release));
13681 }
13682 /* fallthrough */
13683
13684 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013685 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013686 /* Post more buffers if possible */
13687 spin_lock_irqsave(&phba->hbalock, iflags);
13688 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13689 spin_unlock_irqrestore(&phba->hbalock, iflags);
13690 workposted = true;
13691 break;
13692 }
13693out:
13694 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013695}
13696
13697/**
13698 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13699 * @phba: Pointer to HBA context object.
13700 * @cq: Pointer to the completion queue.
James Smart32517fc2019-01-28 11:14:33 -080013701 * @cqe: Pointer to a completion queue entry.
James Smart4d9ab992009-10-02 15:16:39 -040013702 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013703 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013704 * entry.
13705 *
13706 * Return: true if work posted to worker thread, otherwise false.
13707 **/
13708static bool
13709lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013710 struct lpfc_cqe *cqe)
13711{
James Smart45ed1192009-10-02 15:17:02 -040013712 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013713 bool workposted = false;
13714
13715 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013716 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013717
13718 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013719 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013720 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013721 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013722 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013723 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013724 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013725 break;
13726 case CQE_CODE_RELEASE_WQE:
13727 /* Process the WQ release event */
13728 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013729 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013730 break;
13731 case CQE_CODE_XRI_ABORTED:
13732 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013733 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013734 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013735 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013736 break;
James Smart4d9ab992009-10-02 15:16:39 -040013737 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013738 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013739 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013740 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013741 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013742 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013743 break;
James Smart4f774512009-05-22 14:52:35 -040013744 default:
13745 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13746 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013747 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013748 break;
13749 }
13750 return workposted;
13751}
13752
13753/**
James Smart4f774512009-05-22 14:52:35 -040013754 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13755 * @phba: Pointer to HBA context object.
13756 * @eqe: Pointer to fast-path event queue entry.
13757 *
13758 * This routine process a event queue entry from the slow-path event queue.
13759 * It will check the MajorCode and MinorCode to determine this is for a
13760 * completion event on a completion queue, if not, an error shall be logged
13761 * and just return. Otherwise, it will get to the corresponding completion
13762 * queue and process all the entries on that completion queue, rearm the
13763 * completion queue, and then return.
13764 *
13765 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013766static void
James Smart67d12732012-08-03 12:36:13 -040013767lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13768 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013769{
James Smart67d12732012-08-03 12:36:13 -040013770 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013771 uint16_t cqid;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070013772 int ret = 0;
James Smart4f774512009-05-22 14:52:35 -040013773
James Smart4f774512009-05-22 14:52:35 -040013774 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013775 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013776
James Smart4f774512009-05-22 14:52:35 -040013777 list_for_each_entry(childq, &speq->child_list, list) {
13778 if (childq->queue_id == cqid) {
13779 cq = childq;
13780 break;
13781 }
13782 }
13783 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013784 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13785 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13786 "0365 Slow-path CQ identifier "
13787 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013788 return;
James Smart4f774512009-05-22 14:52:35 -040013789 }
13790
James Smart895427b2017-02-12 13:52:30 -080013791 /* Save EQ associated with this CQ */
13792 cq->assoc_qp = speq;
13793
Dick Kennedy86ee57a2020-06-30 14:49:55 -070013794 if (is_kdump_kernel())
13795 ret = queue_work(phba->wq, &cq->spwork);
13796 else
13797 ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
13798
13799 if (!ret)
Dick Kennedyf485c182017-09-29 17:34:34 -070013800 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedy317aeb82020-06-30 14:49:59 -070013801 "0390 Cannot schedule queue work "
Dick Kennedyf485c182017-09-29 17:34:34 -070013802 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
Bart Van Assched6d189c2019-03-28 11:06:22 -070013803 cqid, cq->queue_id, raw_smp_processor_id());
Dick Kennedyf485c182017-09-29 17:34:34 -070013804}
13805
13806/**
James Smart32517fc2019-01-28 11:14:33 -080013807 * __lpfc_sli4_process_cq - Process elements of a CQ
Dick Kennedyf485c182017-09-29 17:34:34 -070013808 * @phba: Pointer to HBA context object.
James Smart32517fc2019-01-28 11:14:33 -080013809 * @cq: Pointer to CQ to be processed
13810 * @handler: Routine to process each cqe
13811 * @delay: Pointer to usdelay to set in case of rescheduling of the handler
Dick Kennedy317aeb82020-06-30 14:49:59 -070013812 * @poll_mode: Polling mode we were called from
Dick Kennedyf485c182017-09-29 17:34:34 -070013813 *
James Smart32517fc2019-01-28 11:14:33 -080013814 * This routine processes completion queue entries in a CQ. While a valid
13815 * queue element is found, the handler is called. During processing checks
13816 * are made for periodic doorbell writes to let the hardware know of
13817 * element consumption.
Dick Kennedyf485c182017-09-29 17:34:34 -070013818 *
James Smart32517fc2019-01-28 11:14:33 -080013819 * If the max limit on cqes to process is hit, or there are no more valid
13820 * entries, the loop stops. If we processed a sufficient number of elements,
13821 * meaning there is sufficient load, rather than rearming and generating
13822 * another interrupt, a cq rescheduling delay will be set. A delay of 0
13823 * indicates no rescheduling.
13824 *
13825 * Returns True if work scheduled, False otherwise.
Dick Kennedyf485c182017-09-29 17:34:34 -070013826 **/
James Smart32517fc2019-01-28 11:14:33 -080013827static bool
13828__lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
13829 bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
Dick Kennedy317aeb82020-06-30 14:49:59 -070013830 struct lpfc_cqe *), unsigned long *delay,
13831 enum lpfc_poll_mode poll_mode)
Dick Kennedyf485c182017-09-29 17:34:34 -070013832{
Dick Kennedyf485c182017-09-29 17:34:34 -070013833 struct lpfc_cqe *cqe;
13834 bool workposted = false;
James Smart32517fc2019-01-28 11:14:33 -080013835 int count = 0, consumed = 0;
13836 bool arm = true;
13837
13838 /* default - no reschedule */
13839 *delay = 0;
13840
13841 if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
13842 goto rearm_and_exit;
Dick Kennedyf485c182017-09-29 17:34:34 -070013843
James Smart4f774512009-05-22 14:52:35 -040013844 /* Process all the entries to the CQ */
James Smartd74a89a2019-05-21 17:48:55 -070013845 cq->q_flag = 0;
James Smart32517fc2019-01-28 11:14:33 -080013846 cqe = lpfc_sli4_cq_get(cq);
13847 while (cqe) {
James Smart32517fc2019-01-28 11:14:33 -080013848 workposted |= handler(phba, cq, cqe);
13849 __lpfc_sli4_consume_cqe(phba, cq, cqe);
13850
13851 consumed++;
13852 if (!(++count % cq->max_proc_limit))
13853 break;
13854
13855 if (!(count % cq->notify_interval)) {
13856 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
13857 LPFC_QUEUE_NOARM);
13858 consumed = 0;
James Smart8156d372019-10-18 14:18:26 -070013859 cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
James Smart4f774512009-05-22 14:52:35 -040013860 }
James Smartb84daac2012-08-03 12:35:13 -040013861
James Smartd74a89a2019-05-21 17:48:55 -070013862 if (count == LPFC_NVMET_CQ_NOTIFY)
13863 cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
13864
James Smart32517fc2019-01-28 11:14:33 -080013865 cqe = lpfc_sli4_cq_get(cq);
13866 }
13867 if (count >= phba->cfg_cq_poll_threshold) {
13868 *delay = 1;
13869 arm = false;
13870 }
13871
Dick Kennedy317aeb82020-06-30 14:49:59 -070013872 /* Note: complete the irq_poll softirq before rearming CQ */
13873 if (poll_mode == LPFC_IRQ_POLL)
13874 irq_poll_complete(&cq->iop);
13875
James Smart32517fc2019-01-28 11:14:33 -080013876 /* Track the max number of CQEs processed in 1 EQ */
13877 if (count > cq->CQ_max_cqe)
13878 cq->CQ_max_cqe = count;
13879
13880 cq->assoc_qp->EQ_cqe_cnt += count;
13881
13882 /* Catch the no cq entry condition */
13883 if (unlikely(count == 0))
13884 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13885 "0369 No entry from completion queue "
13886 "qid=%d\n", cq->queue_id);
13887
Dick Kennedy164ba8d2020-05-01 14:43:03 -070013888 xchg(&cq->queue_claimed, 0);
James Smart32517fc2019-01-28 11:14:33 -080013889
13890rearm_and_exit:
13891 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
13892 arm ? LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
13893
13894 return workposted;
13895}
13896
13897/**
13898 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13899 * @cq: pointer to CQ to process
13900 *
13901 * This routine calls the cq processing routine with a handler specific
13902 * to the type of queue bound to it.
13903 *
13904 * The CQ routine returns two values: the first is the calling status,
13905 * which indicates whether work was queued to the background discovery
13906 * thread. If true, the routine should wakeup the discovery thread;
13907 * the second is the delay parameter. If non-zero, rather than rearming
13908 * the CQ and yet another interrupt, the CQ handler should be queued so
13909 * that it is processed in a subsequent polling action. The value of
13910 * the delay indicates when to reschedule it.
13911 **/
13912static void
13913__lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
13914{
13915 struct lpfc_hba *phba = cq->phba;
13916 unsigned long delay;
13917 bool workposted = false;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070013918 int ret = 0;
James Smart32517fc2019-01-28 11:14:33 -080013919
13920 /* Process and rearm the CQ */
13921 switch (cq->type) {
13922 case LPFC_MCQ:
13923 workposted |= __lpfc_sli4_process_cq(phba, cq,
13924 lpfc_sli4_sp_handle_mcqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070013925 &delay, LPFC_QUEUE_WORK);
James Smart32517fc2019-01-28 11:14:33 -080013926 break;
13927 case LPFC_WCQ:
James Smartc00f62e2019-08-14 16:57:11 -070013928 if (cq->subtype == LPFC_IO)
James Smart32517fc2019-01-28 11:14:33 -080013929 workposted |= __lpfc_sli4_process_cq(phba, cq,
13930 lpfc_sli4_fp_handle_cqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070013931 &delay, LPFC_QUEUE_WORK);
James Smart32517fc2019-01-28 11:14:33 -080013932 else
13933 workposted |= __lpfc_sli4_process_cq(phba, cq,
13934 lpfc_sli4_sp_handle_cqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070013935 &delay, LPFC_QUEUE_WORK);
James Smart4f774512009-05-22 14:52:35 -040013936 break;
13937 default:
13938 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13939 "0370 Invalid completion queue type (%d)\n",
13940 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013941 return;
James Smart4f774512009-05-22 14:52:35 -040013942 }
13943
James Smart32517fc2019-01-28 11:14:33 -080013944 if (delay) {
Dick Kennedy86ee57a2020-06-30 14:49:55 -070013945 if (is_kdump_kernel())
13946 ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
13947 delay);
13948 else
13949 ret = queue_delayed_work_on(cq->chann, phba->wq,
13950 &cq->sched_spwork, delay);
13951 if (!ret)
James Smart32517fc2019-01-28 11:14:33 -080013952 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedy317aeb82020-06-30 14:49:59 -070013953 "0394 Cannot schedule queue work "
James Smart32517fc2019-01-28 11:14:33 -080013954 "for cqid=%d on CPU %d\n",
13955 cq->queue_id, cq->chann);
13956 }
James Smart4f774512009-05-22 14:52:35 -040013957
13958 /* wake up worker thread if there are works to be done */
13959 if (workposted)
13960 lpfc_worker_wake_up(phba);
13961}
13962
13963/**
James Smart32517fc2019-01-28 11:14:33 -080013964 * lpfc_sli4_sp_process_cq - slow-path work handler when started by
13965 * interrupt
13966 * @work: pointer to work element
13967 *
13968 * translates from the work handler and calls the slow-path handler.
13969 **/
13970static void
13971lpfc_sli4_sp_process_cq(struct work_struct *work)
13972{
13973 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
13974
13975 __lpfc_sli4_sp_process_cq(cq);
13976}
13977
13978/**
13979 * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
13980 * @work: pointer to work element
13981 *
13982 * translates from the work handler and calls the slow-path handler.
13983 **/
13984static void
13985lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
13986{
13987 struct lpfc_queue *cq = container_of(to_delayed_work(work),
13988 struct lpfc_queue, sched_spwork);
13989
13990 __lpfc_sli4_sp_process_cq(cq);
13991}
13992
13993/**
James Smart4f774512009-05-22 14:52:35 -040013994 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013995 * @phba: Pointer to HBA context object.
13996 * @cq: Pointer to associated CQ
13997 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013998 *
13999 * This routine process a fast-path work queue completion entry from fast-path
14000 * event queue for FCP command response completion.
14001 **/
14002static void
James Smart2a76a282012-08-03 12:35:54 -040014003lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040014004 struct lpfc_wcqe_complete *wcqe)
14005{
James Smart2a76a282012-08-03 12:35:54 -040014006 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040014007 struct lpfc_iocbq *cmdiocbq;
14008 struct lpfc_iocbq irspiocbq;
14009 unsigned long iflags;
14010
James Smart4f774512009-05-22 14:52:35 -040014011 /* Check for response status */
14012 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14013 /* If resource errors reported from HBA, reduce queue
14014 * depth of the SCSI device.
14015 */
James Smarte3d2b802012-08-14 14:25:43 -040014016 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
14017 IOSTAT_LOCAL_REJECT)) &&
14018 ((wcqe->parameter & IOERR_PARAM_MASK) ==
14019 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040014020 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040014021
Dick Kennedy28ed7372020-06-30 14:49:56 -070014022 /* Log the cmpl status */
James Smart11f0e342018-05-04 20:37:57 -070014023 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
Dick Kennedy28ed7372020-06-30 14:49:56 -070014024 "0373 FCP CQE cmpl: status=x%x: "
James Smart11f0e342018-05-04 20:37:57 -070014025 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040014026 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070014027 wcqe->word0, wcqe->total_data_placed,
14028 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040014029 }
14030
14031 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040014032 spin_lock_irqsave(&pring->ring_lock, iflags);
14033 pring->stats.iocb_event++;
James Smarte2a8be52019-05-06 17:26:47 -070014034 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040014035 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
14036 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart4f774512009-05-22 14:52:35 -040014037 if (unlikely(!cmdiocbq)) {
14038 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14039 "0374 FCP complete with no corresponding "
14040 "cmdiocb: iotag (%d)\n",
14041 bf_get(lpfc_wcqe_c_request_tag, wcqe));
14042 return;
14043 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070014044#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
14045 cmdiocbq->isr_timestamp = cq->isr_timestamp;
14046#endif
James Smart895427b2017-02-12 13:52:30 -080014047 if (cmdiocbq->iocb_cmpl == NULL) {
14048 if (cmdiocbq->wqe_cmpl) {
14049 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
14050 spin_lock_irqsave(&phba->hbalock, iflags);
14051 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
14052 spin_unlock_irqrestore(&phba->hbalock, iflags);
14053 }
14054
14055 /* Pass the cmd_iocb and the wcqe to the upper layer */
14056 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
14057 return;
14058 }
James Smart4f774512009-05-22 14:52:35 -040014059 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14060 "0375 FCP cmdiocb not callback function "
14061 "iotag: (%d)\n",
14062 bf_get(lpfc_wcqe_c_request_tag, wcqe));
14063 return;
14064 }
14065
14066 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050014067 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040014068
James Smart0f65ff62010-02-26 14:14:23 -050014069 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
14070 spin_lock_irqsave(&phba->hbalock, iflags);
14071 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
14072 spin_unlock_irqrestore(&phba->hbalock, iflags);
14073 }
14074
James Smart4f774512009-05-22 14:52:35 -040014075 /* Pass the cmd_iocb and the rsp state to the upper layer */
14076 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
14077}
14078
14079/**
14080 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
14081 * @phba: Pointer to HBA context object.
14082 * @cq: Pointer to completion queue.
14083 * @wcqe: Pointer to work-queue completion queue entry.
14084 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080014085 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040014086 * proper WQ release routine to the slow-path WQ.
14087 **/
14088static void
14089lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14090 struct lpfc_wcqe_release *wcqe)
14091{
14092 struct lpfc_queue *childwq;
14093 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080014094 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040014095
14096 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080014097 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040014098 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080014099 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040014100 lpfc_sli4_wq_release(childwq,
14101 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080014102 if (childwq->q_flag & HBA_NVMET_WQFULL)
14103 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040014104 wqid_matched = true;
14105 break;
14106 }
14107 }
14108 /* Report warning log message if no match found */
14109 if (wqid_matched != true)
14110 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14111 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080014112 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040014113}
14114
14115/**
James Smart2d7dbc42017-02-12 13:52:35 -080014116 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
14117 * @phba: Pointer to HBA context object.
14118 * @rcqe: Pointer to receive-queue completion queue entry.
14119 *
14120 * This routine process a receive-queue completion queue entry.
14121 *
14122 * Return: true if work posted to worker thread, otherwise false.
14123 **/
14124static bool
14125lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14126 struct lpfc_rcqe *rcqe)
14127{
14128 bool workposted = false;
14129 struct lpfc_queue *hrq;
14130 struct lpfc_queue *drq;
14131 struct rqb_dmabuf *dma_buf;
14132 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070014133 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080014134 uint32_t status, rq_id;
14135 unsigned long iflags;
14136 uint32_t fctl, idx;
14137
14138 if ((phba->nvmet_support == 0) ||
14139 (phba->sli4_hba.nvmet_cqset == NULL))
14140 return workposted;
14141
14142 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
14143 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
14144 drq = phba->sli4_hba.nvmet_mrq_data[idx];
14145
14146 /* sanity check on queue memory */
14147 if (unlikely(!hrq) || unlikely(!drq))
14148 return workposted;
14149
14150 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14151 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14152 else
14153 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14154
14155 if ((phba->nvmet_support == 0) ||
14156 (rq_id != hrq->queue_id))
14157 return workposted;
14158
14159 status = bf_get(lpfc_rcqe_status, rcqe);
14160 switch (status) {
14161 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
14162 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14163 "6126 Receive Frame Truncated!!\n");
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060014164 /* fall through */
James Smart2d7dbc42017-02-12 13:52:35 -080014165 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080014166 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080014167 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080014168 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
14169 if (!dma_buf) {
14170 hrq->RQ_no_buf_found++;
14171 spin_unlock_irqrestore(&phba->hbalock, iflags);
14172 goto out;
14173 }
14174 spin_unlock_irqrestore(&phba->hbalock, iflags);
14175 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070014176 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080014177 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14178
14179 /* Just some basic sanity checks on FCP Command frame */
14180 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
James Smart3a8070c2020-03-31 09:50:05 -070014181 fc_hdr->fh_f_ctl[1] << 8 |
14182 fc_hdr->fh_f_ctl[2]);
James Smart2d7dbc42017-02-12 13:52:35 -080014183 if (((fctl &
14184 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
14185 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
14186 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
14187 goto drop;
14188
14189 if (fc_hdr->fh_type == FC_TYPE_FCP) {
James Smartd74a89a2019-05-21 17:48:55 -070014190 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080014191 lpfc_nvmet_unsol_fcp_event(
James Smartd74a89a2019-05-21 17:48:55 -070014192 phba, idx, dma_buf, cq->isr_timestamp,
14193 cq->q_flag & HBA_NVMET_CQ_NOTIFY);
James Smart2d7dbc42017-02-12 13:52:35 -080014194 return false;
14195 }
14196drop:
James Smart22b738a2019-03-12 16:30:11 -070014197 lpfc_rq_buf_free(phba, &dma_buf->hbuf);
James Smart2d7dbc42017-02-12 13:52:35 -080014198 break;
James Smart2d7dbc42017-02-12 13:52:35 -080014199 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070014200 if (phba->nvmet_support) {
14201 tgtp = phba->targetport->private;
14202 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
14203 "6401 RQE Error x%x, posted %d err_cnt "
14204 "%d: %x %x %x\n",
14205 status, hrq->RQ_buf_posted,
14206 hrq->RQ_no_posted_buf,
14207 atomic_read(&tgtp->rcv_fcp_cmd_in),
14208 atomic_read(&tgtp->rcv_fcp_cmd_out),
14209 atomic_read(&tgtp->xmt_fcp_release));
14210 }
14211 /* fallthrough */
14212
14213 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080014214 hrq->RQ_no_posted_buf++;
14215 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080014216 break;
14217 }
14218out:
14219 return workposted;
14220}
14221
14222/**
James Smart895427b2017-02-12 13:52:30 -080014223 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart32517fc2019-01-28 11:14:33 -080014224 * @phba: adapter with cq
James Smart4f774512009-05-22 14:52:35 -040014225 * @cq: Pointer to the completion queue.
14226 * @eqe: Pointer to fast-path completion queue entry.
14227 *
14228 * This routine process a fast-path work queue completion entry from fast-path
14229 * event queue for FCP command response completion.
James Smart32517fc2019-01-28 11:14:33 -080014230 *
14231 * Return: true if work posted to worker thread, otherwise false.
James Smart4f774512009-05-22 14:52:35 -040014232 **/
James Smart32517fc2019-01-28 11:14:33 -080014233static bool
James Smart895427b2017-02-12 13:52:30 -080014234lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040014235 struct lpfc_cqe *cqe)
14236{
14237 struct lpfc_wcqe_release wcqe;
14238 bool workposted = false;
14239
14240 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070014241 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040014242
14243 /* Check and process for different type of WCQE and dispatch */
14244 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
14245 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080014246 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040014247 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040014248 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040014249 phba->last_completion_time = jiffies;
James Smartc00f62e2019-08-14 16:57:11 -070014250 if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
James Smart895427b2017-02-12 13:52:30 -080014251 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040014252 (struct lpfc_wcqe_complete *)&wcqe);
14253 break;
14254 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040014255 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040014256 /* Process the WQ release event */
14257 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
14258 (struct lpfc_wcqe_release *)&wcqe);
14259 break;
14260 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040014261 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040014262 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040014263 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014264 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14265 (struct sli4_wcqe_xri_aborted *)&wcqe);
14266 break;
James Smart895427b2017-02-12 13:52:30 -080014267 case CQE_CODE_RECEIVE_V1:
14268 case CQE_CODE_RECEIVE:
14269 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080014270 if (cq->subtype == LPFC_NVMET) {
14271 workposted = lpfc_sli4_nvmet_handle_rcqe(
14272 phba, cq, (struct lpfc_rcqe *)&wcqe);
14273 }
James Smart895427b2017-02-12 13:52:30 -080014274 break;
James Smart4f774512009-05-22 14:52:35 -040014275 default:
14276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080014277 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040014278 bf_get(lpfc_wcqe_c_code, &wcqe));
14279 break;
14280 }
14281 return workposted;
14282}
14283
14284/**
Dick Kennedy317aeb82020-06-30 14:49:59 -070014285 * lpfc_sli4_sched_cq_work - Schedules cq work
14286 * @phba: Pointer to HBA context object.
14287 * @cq: Pointer to CQ
14288 * @cqid: CQ ID
14289 *
14290 * This routine checks the poll mode of the CQ corresponding to
14291 * cq->chann, then either schedules a softirq or queue_work to complete
14292 * cq work.
14293 *
14294 * queue_work path is taken if in NVMET mode, or if poll_mode is in
14295 * LPFC_QUEUE_WORK mode. Otherwise, softirq path is taken.
14296 *
14297 **/
14298static void lpfc_sli4_sched_cq_work(struct lpfc_hba *phba,
14299 struct lpfc_queue *cq, uint16_t cqid)
14300{
14301 int ret = 0;
14302
14303 switch (cq->poll_mode) {
14304 case LPFC_IRQ_POLL:
14305 irq_poll_sched(&cq->iop);
14306 break;
14307 case LPFC_QUEUE_WORK:
14308 default:
14309 if (is_kdump_kernel())
14310 ret = queue_work(phba->wq, &cq->irqwork);
14311 else
14312 ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
14313 if (!ret)
14314 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14315 "0383 Cannot schedule queue work "
14316 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14317 cqid, cq->queue_id,
14318 raw_smp_processor_id());
14319 }
14320}
14321
14322/**
James Smart67d12732012-08-03 12:36:13 -040014323 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040014324 * @phba: Pointer to HBA context object.
14325 * @eqe: Pointer to fast-path event queue entry.
14326 *
14327 * This routine process a event queue entry from the fast-path event queue.
14328 * It will check the MajorCode and MinorCode to determine this is for a
14329 * completion event on a completion queue, if not, an error shall be logged
14330 * and just return. Otherwise, it will get to the corresponding completion
14331 * queue and process all the entries on the completion queue, rearm the
14332 * completion queue, and then return.
14333 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070014334static void
James Smart32517fc2019-01-28 11:14:33 -080014335lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
14336 struct lpfc_eqe *eqe)
James Smart4f774512009-05-22 14:52:35 -040014337{
James Smart895427b2017-02-12 13:52:30 -080014338 struct lpfc_queue *cq = NULL;
James Smart32517fc2019-01-28 11:14:33 -080014339 uint32_t qidx = eq->hdwq;
James Smart2d7dbc42017-02-12 13:52:35 -080014340 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040014341
James Smartcb5172e2010-03-15 11:25:07 -040014342 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040014343 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040014344 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040014345 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040014346 bf_get_le32(lpfc_eqe_major_code, eqe),
14347 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070014348 return;
James Smart4f774512009-05-22 14:52:35 -040014349 }
14350
James Smart67d12732012-08-03 12:36:13 -040014351 /* Get the reference to the corresponding CQ */
14352 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14353
James Smart6a828b02019-01-28 11:14:31 -080014354 /* Use the fast lookup method first */
14355 if (cqid <= phba->sli4_hba.cq_max) {
14356 cq = phba->sli4_hba.cq_lookup[cqid];
14357 if (cq)
14358 goto work_cq;
James Smartcdb42be2019-01-28 11:14:21 -080014359 }
14360
14361 /* Next check for NVMET completion */
James Smart2d7dbc42017-02-12 13:52:35 -080014362 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14363 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14364 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14365 /* Process NVMET unsol rcv */
14366 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14367 goto process_cq;
14368 }
14369 }
14370
James Smart895427b2017-02-12 13:52:30 -080014371 if (phba->sli4_hba.nvmels_cq &&
14372 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14373 /* Process NVME unsol rcv */
14374 cq = phba->sli4_hba.nvmels_cq;
14375 }
14376
14377 /* Otherwise this is a Slow path event */
14378 if (cq == NULL) {
James Smartcdb42be2019-01-28 11:14:21 -080014379 lpfc_sli4_sp_handle_eqe(phba, eqe,
14380 phba->sli4_hba.hdwq[qidx].hba_eq);
Dick Kennedyf485c182017-09-29 17:34:34 -070014381 return;
James Smart67d12732012-08-03 12:36:13 -040014382 }
14383
James Smart895427b2017-02-12 13:52:30 -080014384process_cq:
James Smart4f774512009-05-22 14:52:35 -040014385 if (unlikely(cqid != cq->queue_id)) {
14386 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14387 "0368 Miss-matched fast-path completion "
14388 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14389 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070014390 return;
James Smart4f774512009-05-22 14:52:35 -040014391 }
14392
James Smart6a828b02019-01-28 11:14:31 -080014393work_cq:
James Smartd74a89a2019-05-21 17:48:55 -070014394#if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
14395 if (phba->ktime_on)
14396 cq->isr_timestamp = ktime_get_ns();
14397 else
14398 cq->isr_timestamp = 0;
14399#endif
Dick Kennedy317aeb82020-06-30 14:49:59 -070014400 lpfc_sli4_sched_cq_work(phba, cq, cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070014401}
14402
14403/**
James Smart32517fc2019-01-28 11:14:33 -080014404 * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14405 * @cq: Pointer to CQ to be processed
Dick Kennedy317aeb82020-06-30 14:49:59 -070014406 * @poll_mode: Enum lpfc_poll_state to determine poll mode
Dick Kennedyf485c182017-09-29 17:34:34 -070014407 *
James Smart32517fc2019-01-28 11:14:33 -080014408 * This routine calls the cq processing routine with the handler for
14409 * fast path CQEs.
14410 *
14411 * The CQ routine returns two values: the first is the calling status,
14412 * which indicates whether work was queued to the background discovery
14413 * thread. If true, the routine should wakeup the discovery thread;
14414 * the second is the delay parameter. If non-zero, rather than rearming
14415 * the CQ and yet another interrupt, the CQ handler should be queued so
14416 * that it is processed in a subsequent polling action. The value of
14417 * the delay indicates when to reschedule it.
Dick Kennedyf485c182017-09-29 17:34:34 -070014418 **/
14419static void
Dick Kennedy317aeb82020-06-30 14:49:59 -070014420__lpfc_sli4_hba_process_cq(struct lpfc_queue *cq,
14421 enum lpfc_poll_mode poll_mode)
Dick Kennedyf485c182017-09-29 17:34:34 -070014422{
Dick Kennedyf485c182017-09-29 17:34:34 -070014423 struct lpfc_hba *phba = cq->phba;
James Smart32517fc2019-01-28 11:14:33 -080014424 unsigned long delay;
Dick Kennedyf485c182017-09-29 17:34:34 -070014425 bool workposted = false;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014426 int ret = 0;
Dick Kennedyf485c182017-09-29 17:34:34 -070014427
James Smart32517fc2019-01-28 11:14:33 -080014428 /* process and rearm the CQ */
14429 workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014430 &delay, poll_mode);
James Smart32517fc2019-01-28 11:14:33 -080014431
14432 if (delay) {
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014433 if (is_kdump_kernel())
14434 ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
14435 delay);
14436 else
14437 ret = queue_delayed_work_on(cq->chann, phba->wq,
14438 &cq->sched_irqwork, delay);
14439 if (!ret)
James Smart32517fc2019-01-28 11:14:33 -080014440 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014441 "0367 Cannot schedule queue work "
14442 "for cqid=%d on CPU %d\n",
14443 cq->queue_id, cq->chann);
James Smart4f774512009-05-22 14:52:35 -040014444 }
14445
James Smart4f774512009-05-22 14:52:35 -040014446 /* wake up worker thread if there are works to be done */
14447 if (workposted)
14448 lpfc_worker_wake_up(phba);
14449}
14450
James Smart1ba981f2014-02-20 09:56:45 -050014451/**
James Smart32517fc2019-01-28 11:14:33 -080014452 * lpfc_sli4_hba_process_cq - fast-path work handler when started by
14453 * interrupt
14454 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050014455 *
James Smart32517fc2019-01-28 11:14:33 -080014456 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050014457 **/
14458static void
James Smart32517fc2019-01-28 11:14:33 -080014459lpfc_sli4_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050014460{
James Smart32517fc2019-01-28 11:14:33 -080014461 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050014462
Dick Kennedy317aeb82020-06-30 14:49:59 -070014463 __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
James Smart1ba981f2014-02-20 09:56:45 -050014464}
14465
14466/**
James Smart32517fc2019-01-28 11:14:33 -080014467 * lpfc_sli4_hba_process_cq - fast-path work handler when started by timer
14468 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050014469 *
James Smart32517fc2019-01-28 11:14:33 -080014470 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050014471 **/
James Smart32517fc2019-01-28 11:14:33 -080014472static void
14473lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050014474{
James Smart32517fc2019-01-28 11:14:33 -080014475 struct lpfc_queue *cq = container_of(to_delayed_work(work),
14476 struct lpfc_queue, sched_irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050014477
Dick Kennedy317aeb82020-06-30 14:49:59 -070014478 __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
James Smart1ba981f2014-02-20 09:56:45 -050014479}
14480
James Smart4f774512009-05-22 14:52:35 -040014481/**
James Smart67d12732012-08-03 12:36:13 -040014482 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040014483 * @irq: Interrupt number.
14484 * @dev_id: The device context pointer.
14485 *
14486 * This function is directly called from the PCI layer as an interrupt
14487 * service routine when device with SLI-4 interface spec is enabled with
14488 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14489 * ring event in the HBA. However, when the device is enabled with either
14490 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14491 * device-level interrupt handler. When the PCI slot is in error recovery
14492 * or the HBA is undergoing initialization, the interrupt handler will not
14493 * process the interrupt. The SCSI FCP fast-path ring event are handled in
14494 * the intrrupt context. This function is called without any lock held.
14495 * It gets the hbalock to access and update SLI data structures. Note that,
14496 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14497 * equal to that of FCP CQ index.
14498 *
James Smart67d12732012-08-03 12:36:13 -040014499 * The link attention and ELS ring attention events are handled
14500 * by the worker thread. The interrupt handler signals the worker thread
14501 * and returns for these events. This function is called without any lock
14502 * held. It gets the hbalock to access and update SLI data structures.
14503 *
James Smart4f774512009-05-22 14:52:35 -040014504 * This function returns IRQ_HANDLED when interrupt is handled else it
14505 * returns IRQ_NONE.
14506 **/
14507irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040014508lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040014509{
14510 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080014511 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040014512 struct lpfc_queue *fpeq;
James Smart4f774512009-05-22 14:52:35 -040014513 unsigned long iflag;
14514 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080014515 int hba_eqidx;
James Smart32517fc2019-01-28 11:14:33 -080014516 struct lpfc_eq_intr_info *eqi;
James Smart4f774512009-05-22 14:52:35 -040014517
14518 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014519 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14520 phba = hba_eq_hdl->phba;
14521 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040014522
14523 if (unlikely(!phba))
14524 return IRQ_NONE;
James Smartcdb42be2019-01-28 11:14:21 -080014525 if (unlikely(!phba->sli4_hba.hdwq))
James Smart5350d872011-10-10 21:33:49 -040014526 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014527
14528 /* Get to the EQ struct associated with this vector */
James Smart657add42019-05-21 17:49:06 -070014529 fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
James Smart2e90f4b2011-12-13 13:22:37 -050014530 if (unlikely(!fpeq))
14531 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014532
14533 /* Check device state for handling interrupt */
14534 if (unlikely(lpfc_intr_state_check(phba))) {
14535 /* Check again for link_state with lock held */
14536 spin_lock_irqsave(&phba->hbalock, iflag);
14537 if (phba->link_state < LPFC_LINK_DOWN)
14538 /* Flush, clear interrupt, and rearm the EQ */
James Smart24c7c0a2019-09-21 20:58:58 -070014539 lpfc_sli4_eqcq_flush(phba, fpeq);
James Smart4f774512009-05-22 14:52:35 -040014540 spin_unlock_irqrestore(&phba->hbalock, iflag);
14541 return IRQ_NONE;
14542 }
14543
Dick Kennedya7fc0712020-05-01 14:43:08 -070014544 eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
14545 eqi->icnt++;
14546
Bart Van Assched6d189c2019-03-28 11:06:22 -070014547 fpeq->last_cpu = raw_smp_processor_id();
James Smart4f774512009-05-22 14:52:35 -040014548
Dick Kennedya7fc0712020-05-01 14:43:08 -070014549 if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
James Smart8156d372019-10-18 14:18:26 -070014550 fpeq->q_flag & HBA_EQ_DELAY_CHK &&
James Smart32517fc2019-01-28 11:14:33 -080014551 phba->cfg_auto_imax &&
14552 fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
14553 phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
14554 lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
James Smartb84daac2012-08-03 12:35:13 -040014555
James Smart32517fc2019-01-28 11:14:33 -080014556 /* process and rearm the EQ */
James Smart93a4d6f2019-11-04 16:57:05 -080014557 ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014558
14559 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014560 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040014561 if (phba->intr_type == MSIX)
14562 /* MSI-X treated interrupt served as no EQ share INT */
14563 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14564 "0358 MSI-X interrupt with no EQE\n");
14565 else
14566 /* Non MSI-X treated on interrupt as EQ share INT */
14567 return IRQ_NONE;
14568 }
14569
14570 return IRQ_HANDLED;
14571} /* lpfc_sli4_fp_intr_handler */
14572
14573/**
14574 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14575 * @irq: Interrupt number.
14576 * @dev_id: The device context pointer.
14577 *
14578 * This function is the device-level interrupt handler to device with SLI-4
14579 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14580 * interrupt mode is enabled and there is an event in the HBA which requires
14581 * driver attention. This function invokes the slow-path interrupt attention
14582 * handling function and fast-path interrupt attention handling function in
14583 * turn to process the relevant HBA attention events. This function is called
14584 * without any lock held. It gets the hbalock to access and update SLI data
14585 * structures.
14586 *
14587 * This function returns IRQ_HANDLED when interrupt is handled, else it
14588 * returns IRQ_NONE.
14589 **/
14590irqreturn_t
14591lpfc_sli4_intr_handler(int irq, void *dev_id)
14592{
14593 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014594 irqreturn_t hba_irq_rc;
14595 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014596 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014597
14598 /* Get the driver's phba structure from the dev_id */
14599 phba = (struct lpfc_hba *)dev_id;
14600
14601 if (unlikely(!phba))
14602 return IRQ_NONE;
14603
14604 /*
James Smart4f774512009-05-22 14:52:35 -040014605 * Invoke fast-path host attention interrupt handling as appropriate.
14606 */
James Smart6a828b02019-01-28 11:14:31 -080014607 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014608 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014609 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014610 if (hba_irq_rc == IRQ_HANDLED)
14611 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014612 }
14613
James Smart67d12732012-08-03 12:36:13 -040014614 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014615} /* lpfc_sli4_intr_handler */
14616
James Smart93a4d6f2019-11-04 16:57:05 -080014617void lpfc_sli4_poll_hbtimer(struct timer_list *t)
14618{
14619 struct lpfc_hba *phba = from_timer(phba, t, cpuhp_poll_timer);
14620 struct lpfc_queue *eq;
14621 int i = 0;
14622
14623 rcu_read_lock();
14624
14625 list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
14626 i += lpfc_sli4_poll_eq(eq, LPFC_POLL_SLOWPATH);
14627 if (!list_empty(&phba->poll_list))
14628 mod_timer(&phba->cpuhp_poll_timer,
14629 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
14630
14631 rcu_read_unlock();
14632}
14633
14634inline int lpfc_sli4_poll_eq(struct lpfc_queue *eq, uint8_t path)
14635{
14636 struct lpfc_hba *phba = eq->phba;
14637 int i = 0;
14638
14639 /*
14640 * Unlocking an irq is one of the entry point to check
14641 * for re-schedule, but we are good for io submission
14642 * path as midlayer does a get_cpu to glue us in. Flush
14643 * out the invalidate queue so we can see the updated
14644 * value for flag.
14645 */
14646 smp_rmb();
14647
14648 if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
14649 /* We will not likely get the completion for the caller
14650 * during this iteration but i guess that's fine.
14651 * Future io's coming on this eq should be able to
14652 * pick it up. As for the case of single io's, they
14653 * will be handled through a sched from polling timer
14654 * function which is currently triggered every 1msec.
14655 */
14656 i = lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM);
14657
14658 return i;
14659}
14660
14661static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
14662{
14663 struct lpfc_hba *phba = eq->phba;
14664
James Smartf861f592020-03-22 11:12:54 -070014665 /* kickstart slowpath processing if needed */
14666 if (list_empty(&phba->poll_list))
James Smart93a4d6f2019-11-04 16:57:05 -080014667 mod_timer(&phba->cpuhp_poll_timer,
14668 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
James Smart93a4d6f2019-11-04 16:57:05 -080014669
14670 list_add_rcu(&eq->_poll_list, &phba->poll_list);
14671 synchronize_rcu();
14672}
14673
14674static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
14675{
14676 struct lpfc_hba *phba = eq->phba;
14677
14678 /* Disable slowpath processing for this eq. Kick start the eq
14679 * by RE-ARMING the eq's ASAP
14680 */
14681 list_del_rcu(&eq->_poll_list);
14682 synchronize_rcu();
14683
14684 if (list_empty(&phba->poll_list))
14685 del_timer_sync(&phba->cpuhp_poll_timer);
14686}
14687
James Smartd480e572019-11-11 15:03:58 -080014688void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
James Smart93a4d6f2019-11-04 16:57:05 -080014689{
14690 struct lpfc_queue *eq, *next;
14691
14692 list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
14693 list_del(&eq->_poll_list);
14694
14695 INIT_LIST_HEAD(&phba->poll_list);
14696 synchronize_rcu();
14697}
14698
14699static inline void
14700__lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
14701{
14702 if (mode == eq->mode)
14703 return;
14704 /*
14705 * currently this function is only called during a hotplug
14706 * event and the cpu on which this function is executing
14707 * is going offline. By now the hotplug has instructed
14708 * the scheduler to remove this cpu from cpu active mask.
14709 * So we don't need to work about being put aside by the
14710 * scheduler for a high priority process. Yes, the inte-
14711 * rrupts could come but they are known to retire ASAP.
14712 */
14713
14714 /* Disable polling in the fastpath */
14715 WRITE_ONCE(eq->mode, mode);
14716 /* flush out the store buffer */
14717 smp_wmb();
14718
14719 /*
14720 * Add this eq to the polling list and start polling. For
14721 * a grace period both interrupt handler and poller will
14722 * try to process the eq _but_ that's fine. We have a
14723 * synchronization mechanism in place (queue_claimed) to
14724 * deal with it. This is just a draining phase for int-
14725 * errupt handler (not eq's) as we have guranteed through
14726 * barrier that all the CPUs have seen the new CQ_POLLED
14727 * state. which will effectively disable the REARMING of
14728 * the EQ. The whole idea is eq's die off eventually as
14729 * we are not rearming EQ's anymore.
14730 */
14731 mode ? lpfc_sli4_add_to_poll_list(eq) :
14732 lpfc_sli4_remove_from_poll_list(eq);
14733}
14734
14735void lpfc_sli4_start_polling(struct lpfc_queue *eq)
14736{
14737 __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
14738}
14739
14740void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
14741{
14742 struct lpfc_hba *phba = eq->phba;
14743
14744 __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
14745
14746 /* Kick start for the pending io's in h/w.
14747 * Once we switch back to interrupt processing on a eq
14748 * the io path completion will only arm eq's when it
14749 * receives a completion. But since eq's are in disa-
14750 * rmed state it doesn't receive a completion. This
14751 * creates a deadlock scenaro.
14752 */
14753 phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
14754}
14755
James Smart4f774512009-05-22 14:52:35 -040014756/**
14757 * lpfc_sli4_queue_free - free a queue structure and associated memory
14758 * @queue: The queue structure to free.
14759 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014760 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014761 * the host resident queue. This function must be called after destroying the
14762 * queue on the HBA.
14763 **/
14764void
14765lpfc_sli4_queue_free(struct lpfc_queue *queue)
14766{
14767 struct lpfc_dmabuf *dmabuf;
14768
14769 if (!queue)
14770 return;
14771
James Smart4645f7b2019-03-12 16:30:14 -070014772 if (!list_empty(&queue->wq_list))
14773 list_del(&queue->wq_list);
14774
James Smart4f774512009-05-22 14:52:35 -040014775 while (!list_empty(&queue->page_list)) {
14776 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14777 list);
James Smart81b96ed2017-11-20 16:00:29 -080014778 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014779 dmabuf->virt, dmabuf->phys);
14780 kfree(dmabuf);
14781 }
James Smart895427b2017-02-12 13:52:30 -080014782 if (queue->rqbp) {
14783 lpfc_free_rq_buffer(queue->phba, queue);
14784 kfree(queue->rqbp);
14785 }
James Smartd1f525a2017-04-21 16:04:55 -070014786
James Smart32517fc2019-01-28 11:14:33 -080014787 if (!list_empty(&queue->cpu_list))
14788 list_del(&queue->cpu_list);
14789
James Smart4f774512009-05-22 14:52:35 -040014790 kfree(queue);
14791 return;
14792}
14793
14794/**
14795 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14796 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014797 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014798 * @entry_size: The size of each queue entry for this queue.
14799 * @entry count: The number of entries that this queue will handle.
James Smartc1a21eb2019-03-12 16:30:29 -070014800 * @cpu: The cpu that will primarily utilize this queue.
James Smart4f774512009-05-22 14:52:35 -040014801 *
14802 * This function allocates a queue structure and the DMAable memory used for
14803 * the host resident queue. This function must be called before creating the
14804 * queue on the HBA.
14805 **/
14806struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014807lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
James Smartc1a21eb2019-03-12 16:30:29 -070014808 uint32_t entry_size, uint32_t entry_count, int cpu)
James Smart4f774512009-05-22 14:52:35 -040014809{
14810 struct lpfc_queue *queue;
14811 struct lpfc_dmabuf *dmabuf;
James Smartcb5172e2010-03-15 11:25:07 -040014812 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart9afbee32019-03-12 16:30:28 -070014813 uint16_t x, pgcnt;
James Smart4f774512009-05-22 14:52:35 -040014814
James Smartcb5172e2010-03-15 11:25:07 -040014815 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014816 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014817
James Smart9afbee32019-03-12 16:30:28 -070014818 pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014819
14820 /* If needed, Adjust page count to match the max the adapter supports */
James Smart9afbee32019-03-12 16:30:28 -070014821 if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
14822 pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
14823
James Smartc1a21eb2019-03-12 16:30:29 -070014824 queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
14825 GFP_KERNEL, cpu_to_node(cpu));
James Smart9afbee32019-03-12 16:30:28 -070014826 if (!queue)
14827 return NULL;
James Smart895427b2017-02-12 13:52:30 -080014828
James Smart4f774512009-05-22 14:52:35 -040014829 INIT_LIST_HEAD(&queue->list);
James Smart93a4d6f2019-11-04 16:57:05 -080014830 INIT_LIST_HEAD(&queue->_poll_list);
James Smart895427b2017-02-12 13:52:30 -080014831 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014832 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014833 INIT_LIST_HEAD(&queue->page_list);
14834 INIT_LIST_HEAD(&queue->child_list);
James Smart32517fc2019-01-28 11:14:33 -080014835 INIT_LIST_HEAD(&queue->cpu_list);
James Smart81b96ed2017-11-20 16:00:29 -080014836
14837 /* Set queue parameters now. If the system cannot provide memory
14838 * resources, the free routine needs to know what was allocated.
14839 */
James Smart9afbee32019-03-12 16:30:28 -070014840 queue->page_count = pgcnt;
14841 queue->q_pgs = (void **)&queue[1];
14842 queue->entry_cnt_per_pg = hw_page_size / entry_size;
James Smart81b96ed2017-11-20 16:00:29 -080014843 queue->entry_size = entry_size;
14844 queue->entry_count = entry_count;
14845 queue->page_size = hw_page_size;
14846 queue->phba = phba;
14847
James Smart9afbee32019-03-12 16:30:28 -070014848 for (x = 0; x < queue->page_count; x++) {
James Smartc1a21eb2019-03-12 16:30:29 -070014849 dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
14850 dev_to_node(&phba->pcidev->dev));
James Smart4f774512009-05-22 14:52:35 -040014851 if (!dmabuf)
14852 goto out_fail;
Luis Chamberlain750afb02019-01-04 09:23:09 +010014853 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
14854 hw_page_size, &dmabuf->phys,
14855 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014856 if (!dmabuf->virt) {
14857 kfree(dmabuf);
14858 goto out_fail;
14859 }
14860 dmabuf->buffer_tag = x;
14861 list_add_tail(&dmabuf->list, &queue->page_list);
James Smart9afbee32019-03-12 16:30:28 -070014862 /* use lpfc_sli4_qe to index a paritcular entry in this page */
14863 queue->q_pgs[x] = dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -040014864 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014865 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14866 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart32517fc2019-01-28 11:14:33 -080014867 INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
14868 INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014869
James Smart32517fc2019-01-28 11:14:33 -080014870 /* notify_interval will be set during q creation */
James Smart64eb4dc2017-05-15 15:20:49 -070014871
James Smart4f774512009-05-22 14:52:35 -040014872 return queue;
14873out_fail:
14874 lpfc_sli4_queue_free(queue);
14875 return NULL;
14876}
14877
14878/**
James Smart962bc512013-01-03 15:44:00 -050014879 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14880 * @phba: HBA structure that indicates port to create a queue on.
14881 * @pci_barset: PCI BAR set flag.
14882 *
14883 * This function shall perform iomap of the specified PCI BAR address to host
14884 * memory address if not already done so and return it. The returned host
14885 * memory address can be NULL.
14886 */
14887static void __iomem *
14888lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14889{
James Smart962bc512013-01-03 15:44:00 -050014890 if (!phba->pcidev)
14891 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014892
14893 switch (pci_barset) {
14894 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014895 return phba->pci_bar0_memmap_p;
14896 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014897 return phba->pci_bar2_memmap_p;
14898 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014899 return phba->pci_bar4_memmap_p;
14900 default:
14901 break;
14902 }
14903 return NULL;
14904}
14905
14906/**
James Smartcb733e32019-01-28 11:14:32 -080014907 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
14908 * @phba: HBA structure that EQs are on.
14909 * @startq: The starting EQ index to modify
14910 * @numq: The number of EQs (consecutive indexes) to modify
14911 * @usdelay: amount of delay
James Smart173edbb2012-06-12 13:54:50 -040014912 *
James Smartcb733e32019-01-28 11:14:32 -080014913 * This function revises the EQ delay on 1 or more EQs. The EQ delay
14914 * is set either by writing to a register (if supported by the SLI Port)
14915 * or by mailbox command. The mailbox command allows several EQs to be
14916 * updated at once.
James Smart173edbb2012-06-12 13:54:50 -040014917 *
James Smartcb733e32019-01-28 11:14:32 -080014918 * The @phba struct is used to send a mailbox command to HBA. The @startq
14919 * is used to get the starting EQ index to change. The @numq value is
14920 * used to specify how many consecutive EQ indexes, starting at EQ index,
14921 * are to be changed. This function is asynchronous and will wait for any
14922 * mailbox commands to finish before returning.
James Smart173edbb2012-06-12 13:54:50 -040014923 *
James Smartcb733e32019-01-28 11:14:32 -080014924 * On success this function will return a zero. If unable to allocate
14925 * enough memory this function will return -ENOMEM. If a mailbox command
14926 * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
14927 * have had their delay multipler changed.
James Smart173edbb2012-06-12 13:54:50 -040014928 **/
James Smartcb733e32019-01-28 11:14:32 -080014929void
James Smart0cf07f842017-06-01 21:07:10 -070014930lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
James Smartcb733e32019-01-28 11:14:32 -080014931 uint32_t numq, uint32_t usdelay)
James Smart173edbb2012-06-12 13:54:50 -040014932{
14933 struct lpfc_mbx_modify_eq_delay *eq_delay;
14934 LPFC_MBOXQ_t *mbox;
14935 struct lpfc_queue *eq;
James Smartcb733e32019-01-28 11:14:32 -080014936 int cnt = 0, rc, length;
James Smart173edbb2012-06-12 13:54:50 -040014937 uint32_t shdr_status, shdr_add_status;
James Smartcb733e32019-01-28 11:14:32 -080014938 uint32_t dmult;
James Smart895427b2017-02-12 13:52:30 -080014939 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014940 union lpfc_sli4_cfg_shdr *shdr;
James Smart173edbb2012-06-12 13:54:50 -040014941
James Smart6a828b02019-01-28 11:14:31 -080014942 if (startq >= phba->cfg_irq_chann)
James Smartcb733e32019-01-28 11:14:32 -080014943 return;
14944
14945 if (usdelay > 0xFFFF) {
14946 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
14947 "6429 usdelay %d too large. Scaled down to "
14948 "0xFFFF.\n", usdelay);
14949 usdelay = 0xFFFF;
14950 }
14951
14952 /* set values by EQ_DELAY register if supported */
14953 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14954 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070014955 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smartcb733e32019-01-28 11:14:32 -080014956 if (!eq)
14957 continue;
14958
James Smart32517fc2019-01-28 11:14:33 -080014959 lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
James Smartcb733e32019-01-28 11:14:32 -080014960
14961 if (++cnt >= numq)
14962 break;
14963 }
James Smartcb733e32019-01-28 11:14:32 -080014964 return;
14965 }
14966
14967 /* Otherwise, set values by mailbox cmd */
James Smart173edbb2012-06-12 13:54:50 -040014968
14969 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smartcb733e32019-01-28 11:14:32 -080014970 if (!mbox) {
14971 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_FCP | LOG_NVME,
14972 "6428 Failed allocating mailbox cmd buffer."
14973 " EQ delay was not set.\n");
14974 return;
14975 }
James Smart173edbb2012-06-12 13:54:50 -040014976 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14977 sizeof(struct lpfc_sli4_cfg_mhdr));
14978 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14979 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14980 length, LPFC_SLI4_MBX_EMBED);
14981 eq_delay = &mbox->u.mqe.un.eq_delay;
14982
14983 /* Calculate delay multiper from maximum interrupt per second */
James Smartcb733e32019-01-28 11:14:32 -080014984 dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
14985 if (dmult)
14986 dmult--;
James Smart0cf07f842017-06-01 21:07:10 -070014987 if (dmult > LPFC_DMULT_MAX)
14988 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014989
James Smart6a828b02019-01-28 11:14:31 -080014990 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070014991 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart173edbb2012-06-12 13:54:50 -040014992 if (!eq)
14993 continue;
James Smartcb733e32019-01-28 11:14:32 -080014994 eq->q_mode = usdelay;
James Smart173edbb2012-06-12 13:54:50 -040014995 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14996 eq_delay->u.request.eq[cnt].phase = 0;
14997 eq_delay->u.request.eq[cnt].delay_multi = dmult;
James Smart0cf07f842017-06-01 21:07:10 -070014998
James Smartcb733e32019-01-28 11:14:32 -080014999 if (++cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040015000 break;
15001 }
15002 eq_delay->u.request.num_eq = cnt;
15003
15004 mbox->vport = phba->pport;
15005 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080015006 mbox->ctx_buf = NULL;
15007 mbox->ctx_ndlp = NULL;
James Smart173edbb2012-06-12 13:54:50 -040015008 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15009 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
15010 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15011 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15012 if (shdr_status || shdr_add_status || rc) {
15013 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15014 "2512 MODIFY_EQ_DELAY mailbox failed with "
15015 "status x%x add_status x%x, mbx status x%x\n",
15016 shdr_status, shdr_add_status, rc);
James Smart173edbb2012-06-12 13:54:50 -040015017 }
15018 mempool_free(mbox, phba->mbox_mem_pool);
James Smartcb733e32019-01-28 11:14:32 -080015019 return;
James Smart173edbb2012-06-12 13:54:50 -040015020}
15021
15022/**
James Smart4f774512009-05-22 14:52:35 -040015023 * lpfc_eq_create - Create an Event Queue on the HBA
15024 * @phba: HBA structure that indicates port to create a queue on.
15025 * @eq: The queue structure to use to create the event queue.
15026 * @imax: The maximum interrupt per second limit.
15027 *
15028 * This function creates an event queue, as detailed in @eq, on a port,
15029 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
15030 *
15031 * The @phba struct is used to send mailbox command to HBA. The @eq struct
15032 * is used to get the entry count and entry size that are necessary to
15033 * determine the number of pages to allocate and use for this queue. This
15034 * function will send the EQ_CREATE mailbox command to the HBA to setup the
15035 * event queue. This function is asynchronous and will wait for the mailbox
15036 * command to finish before continuing.
15037 *
15038 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015039 * memory this function will return -ENOMEM. If the queue create mailbox command
15040 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015041 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015042int
James Smartee020062012-09-29 11:28:52 -040015043lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040015044{
15045 struct lpfc_mbx_eq_create *eq_create;
15046 LPFC_MBOXQ_t *mbox;
15047 int rc, length, status = 0;
15048 struct lpfc_dmabuf *dmabuf;
15049 uint32_t shdr_status, shdr_add_status;
15050 union lpfc_sli4_cfg_shdr *shdr;
15051 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040015052 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15053
James Smart2e90f4b2011-12-13 13:22:37 -050015054 /* sanity check on queue memory */
15055 if (!eq)
15056 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015057 if (!phba->sli4_hba.pc_sli4_params.supported)
15058 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015059
15060 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15061 if (!mbox)
15062 return -ENOMEM;
15063 length = (sizeof(struct lpfc_mbx_eq_create) -
15064 sizeof(struct lpfc_sli4_cfg_mhdr));
15065 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15066 LPFC_MBOX_OPCODE_EQ_CREATE,
15067 length, LPFC_SLI4_MBX_EMBED);
15068 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080015069 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015070 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
15071 eq->page_count);
15072 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
15073 LPFC_EQE_SIZE);
15074 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080015075
15076 /* Use version 2 of CREATE_EQ if eqav is set */
15077 if (phba->sli4_hba.pc_sli4_params.eqav) {
15078 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15079 LPFC_Q_CREATE_VERSION_2);
15080 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
15081 phba->sli4_hba.pc_sli4_params.eqav);
15082 }
15083
James Smart2c9c5a02015-04-07 15:07:15 -040015084 /* don't setup delay multiplier using EQ_CREATE */
15085 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040015086 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
15087 dmult);
15088 switch (eq->entry_count) {
15089 default:
15090 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15091 "0360 Unsupported EQ count. (%d)\n",
15092 eq->entry_count);
James Smart04d210c2019-05-21 17:49:03 -070015093 if (eq->entry_count < 256) {
15094 status = -EINVAL;
15095 goto out;
15096 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015097 /* fall through - otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040015098 case 256:
15099 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15100 LPFC_EQ_CNT_256);
15101 break;
15102 case 512:
15103 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15104 LPFC_EQ_CNT_512);
15105 break;
15106 case 1024:
15107 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15108 LPFC_EQ_CNT_1024);
15109 break;
15110 case 2048:
15111 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15112 LPFC_EQ_CNT_2048);
15113 break;
15114 case 4096:
15115 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15116 LPFC_EQ_CNT_4096);
15117 break;
15118 }
15119 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015120 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015121 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15122 putPaddrLow(dmabuf->phys);
15123 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15124 putPaddrHigh(dmabuf->phys);
15125 }
15126 mbox->vport = phba->pport;
15127 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080015128 mbox->ctx_buf = NULL;
15129 mbox->ctx_ndlp = NULL;
James Smart4f774512009-05-22 14:52:35 -040015130 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040015131 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15132 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15133 if (shdr_status || shdr_add_status || rc) {
15134 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15135 "2500 EQ_CREATE mailbox failed with "
15136 "status x%x add_status x%x, mbx status x%x\n",
15137 shdr_status, shdr_add_status, rc);
15138 status = -ENXIO;
15139 }
15140 eq->type = LPFC_EQ;
15141 eq->subtype = LPFC_NONE;
15142 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
15143 if (eq->queue_id == 0xFFFF)
15144 status = -ENXIO;
15145 eq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015146 eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
15147 eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
James Smart04d210c2019-05-21 17:49:03 -070015148out:
James Smart8fa38512009-07-19 10:01:03 -040015149 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015150 return status;
15151}
15152
Dick Kennedy317aeb82020-06-30 14:49:59 -070015153static int lpfc_cq_poll_hdler(struct irq_poll *iop, int budget)
15154{
15155 struct lpfc_queue *cq = container_of(iop, struct lpfc_queue, iop);
15156
15157 __lpfc_sli4_hba_process_cq(cq, LPFC_IRQ_POLL);
15158
15159 return 1;
15160}
15161
James Smart4f774512009-05-22 14:52:35 -040015162/**
15163 * lpfc_cq_create - Create a Completion Queue on the HBA
15164 * @phba: HBA structure that indicates port to create a queue on.
15165 * @cq: The queue structure to use to create the completion queue.
15166 * @eq: The event queue to bind this completion queue to.
15167 *
15168 * This function creates a completion queue, as detailed in @wq, on a port,
15169 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
15170 *
15171 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15172 * is used to get the entry count and entry size that are necessary to
15173 * determine the number of pages to allocate and use for this queue. The @eq
15174 * is used to indicate which event queue to bind this completion queue to. This
15175 * function will send the CQ_CREATE mailbox command to the HBA to setup the
15176 * completion queue. This function is asynchronous and will wait for the mailbox
15177 * command to finish before continuing.
15178 *
15179 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015180 * memory this function will return -ENOMEM. If the queue create mailbox command
15181 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015182 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015183int
James Smart4f774512009-05-22 14:52:35 -040015184lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
15185 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
15186{
15187 struct lpfc_mbx_cq_create *cq_create;
15188 struct lpfc_dmabuf *dmabuf;
15189 LPFC_MBOXQ_t *mbox;
15190 int rc, length, status = 0;
15191 uint32_t shdr_status, shdr_add_status;
15192 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015193
James Smart2e90f4b2011-12-13 13:22:37 -050015194 /* sanity check on queue memory */
15195 if (!cq || !eq)
15196 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015197
James Smart4f774512009-05-22 14:52:35 -040015198 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15199 if (!mbox)
15200 return -ENOMEM;
15201 length = (sizeof(struct lpfc_mbx_cq_create) -
15202 sizeof(struct lpfc_sli4_cfg_mhdr));
15203 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15204 LPFC_MBOX_OPCODE_CQ_CREATE,
15205 length, LPFC_SLI4_MBX_EMBED);
15206 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015207 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015208 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
15209 cq->page_count);
15210 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
15211 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015212 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15213 phba->sli4_hba.pc_sli4_params.cqv);
15214 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080015215 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
15216 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015217 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
15218 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080015219 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
15220 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050015221 } else {
15222 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
15223 eq->queue_id);
15224 }
James Smart4f774512009-05-22 14:52:35 -040015225 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080015226 case 2048:
15227 case 4096:
15228 if (phba->sli4_hba.pc_sli4_params.cqv ==
15229 LPFC_Q_CREATE_VERSION_2) {
15230 cq_create->u.request.context.lpfc_cq_context_count =
15231 cq->entry_count;
15232 bf_set(lpfc_cq_context_count,
15233 &cq_create->u.request.context,
15234 LPFC_CQ_CNT_WORD7);
15235 break;
15236 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015237 /* fall through */
James Smart4f774512009-05-22 14:52:35 -040015238 default:
15239 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080015240 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070015241 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080015242 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070015243 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040015244 if (cq->entry_count < 256) {
15245 status = -EINVAL;
15246 goto out;
15247 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015248 /* fall through - otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040015249 case 256:
15250 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15251 LPFC_CQ_CNT_256);
15252 break;
15253 case 512:
15254 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15255 LPFC_CQ_CNT_512);
15256 break;
15257 case 1024:
15258 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15259 LPFC_CQ_CNT_1024);
15260 break;
15261 }
15262 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080015263 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040015264 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15265 putPaddrLow(dmabuf->phys);
15266 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15267 putPaddrHigh(dmabuf->phys);
15268 }
15269 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15270
15271 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015272 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15273 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15274 if (shdr_status || shdr_add_status || rc) {
15275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15276 "2501 CQ_CREATE mailbox failed with "
15277 "status x%x add_status x%x, mbx status x%x\n",
15278 shdr_status, shdr_add_status, rc);
15279 status = -ENXIO;
15280 goto out;
15281 }
15282 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
15283 if (cq->queue_id == 0xFFFF) {
15284 status = -ENXIO;
15285 goto out;
15286 }
15287 /* link the cq onto the parent eq child list */
15288 list_add_tail(&cq->list, &eq->child_list);
15289 /* Set up completion queue's type and subtype */
15290 cq->type = type;
15291 cq->subtype = subtype;
15292 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050015293 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080015294 cq->assoc_qp = eq;
James Smart4f774512009-05-22 14:52:35 -040015295 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015296 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15297 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
James Smart4f774512009-05-22 14:52:35 -040015298
James Smart6a828b02019-01-28 11:14:31 -080015299 if (cq->queue_id > phba->sli4_hba.cq_max)
15300 phba->sli4_hba.cq_max = cq->queue_id;
Dick Kennedy317aeb82020-06-30 14:49:59 -070015301
15302 irq_poll_init(&cq->iop, LPFC_IRQ_POLL_WEIGHT, lpfc_cq_poll_hdler);
James Smart8fa38512009-07-19 10:01:03 -040015303out:
15304 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015305 return status;
15306}
15307
15308/**
James Smart2d7dbc42017-02-12 13:52:35 -080015309 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
15310 * @phba: HBA structure that indicates port to create a queue on.
15311 * @cqp: The queue structure array to use to create the completion queues.
James Smartcdb42be2019-01-28 11:14:21 -080015312 * @hdwq: The hardware queue array with the EQ to bind completion queues to.
James Smart2d7dbc42017-02-12 13:52:35 -080015313 *
15314 * This function creates a set of completion queue, s to support MRQ
15315 * as detailed in @cqp, on a port,
15316 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
15317 *
15318 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15319 * is used to get the entry count and entry size that are necessary to
15320 * determine the number of pages to allocate and use for this queue. The @eq
15321 * is used to indicate which event queue to bind this completion queue to. This
15322 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
15323 * completion queue. This function is asynchronous and will wait for the mailbox
15324 * command to finish before continuing.
15325 *
15326 * On success this function will return a zero. If unable to allocate enough
15327 * memory this function will return -ENOMEM. If the queue create mailbox command
15328 * fails this function will return -ENXIO.
15329 **/
15330int
15331lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
James Smartcdb42be2019-01-28 11:14:21 -080015332 struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
15333 uint32_t subtype)
James Smart2d7dbc42017-02-12 13:52:35 -080015334{
15335 struct lpfc_queue *cq;
15336 struct lpfc_queue *eq;
15337 struct lpfc_mbx_cq_create_set *cq_set;
15338 struct lpfc_dmabuf *dmabuf;
15339 LPFC_MBOXQ_t *mbox;
15340 int rc, length, alloclen, status = 0;
15341 int cnt, idx, numcq, page_idx = 0;
15342 uint32_t shdr_status, shdr_add_status;
15343 union lpfc_sli4_cfg_shdr *shdr;
15344 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15345
15346 /* sanity check on queue memory */
15347 numcq = phba->cfg_nvmet_mrq;
James Smartcdb42be2019-01-28 11:14:21 -080015348 if (!cqp || !hdwq || !numcq)
James Smart2d7dbc42017-02-12 13:52:35 -080015349 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080015350
15351 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15352 if (!mbox)
15353 return -ENOMEM;
15354
15355 length = sizeof(struct lpfc_mbx_cq_create_set);
15356 length += ((numcq * cqp[0]->page_count) *
15357 sizeof(struct dma_address));
15358 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15359 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
15360 LPFC_SLI4_MBX_NEMBED);
15361 if (alloclen < length) {
15362 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15363 "3098 Allocated DMA memory size (%d) is "
15364 "less than the requested DMA memory size "
15365 "(%d)\n", alloclen, length);
15366 status = -ENOMEM;
15367 goto out;
15368 }
15369 cq_set = mbox->sge_array->addr[0];
15370 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
15371 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
15372
15373 for (idx = 0; idx < numcq; idx++) {
15374 cq = cqp[idx];
James Smartcdb42be2019-01-28 11:14:21 -080015375 eq = hdwq[idx].hba_eq;
James Smart2d7dbc42017-02-12 13:52:35 -080015376 if (!cq || !eq) {
15377 status = -ENOMEM;
15378 goto out;
15379 }
James Smart81b96ed2017-11-20 16:00:29 -080015380 if (!phba->sli4_hba.pc_sli4_params.supported)
15381 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080015382
15383 switch (idx) {
15384 case 0:
15385 bf_set(lpfc_mbx_cq_create_set_page_size,
15386 &cq_set->u.request,
15387 (hw_page_size / SLI4_PAGE_SIZE));
15388 bf_set(lpfc_mbx_cq_create_set_num_pages,
15389 &cq_set->u.request, cq->page_count);
15390 bf_set(lpfc_mbx_cq_create_set_evt,
15391 &cq_set->u.request, 1);
15392 bf_set(lpfc_mbx_cq_create_set_valid,
15393 &cq_set->u.request, 1);
15394 bf_set(lpfc_mbx_cq_create_set_cqe_size,
15395 &cq_set->u.request, 0);
15396 bf_set(lpfc_mbx_cq_create_set_num_cq,
15397 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080015398 bf_set(lpfc_mbx_cq_create_set_autovalid,
15399 &cq_set->u.request,
15400 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080015401 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080015402 case 2048:
15403 case 4096:
15404 if (phba->sli4_hba.pc_sli4_params.cqv ==
15405 LPFC_Q_CREATE_VERSION_2) {
15406 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15407 &cq_set->u.request,
15408 cq->entry_count);
15409 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15410 &cq_set->u.request,
15411 LPFC_CQ_CNT_WORD7);
15412 break;
15413 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015414 /* fall through */
James Smart2d7dbc42017-02-12 13:52:35 -080015415 default:
15416 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15417 "3118 Bad CQ count. (%d)\n",
15418 cq->entry_count);
15419 if (cq->entry_count < 256) {
15420 status = -EINVAL;
15421 goto out;
15422 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015423 /* fall through - otherwise default to smallest */
James Smart2d7dbc42017-02-12 13:52:35 -080015424 case 256:
15425 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15426 &cq_set->u.request, LPFC_CQ_CNT_256);
15427 break;
15428 case 512:
15429 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15430 &cq_set->u.request, LPFC_CQ_CNT_512);
15431 break;
15432 case 1024:
15433 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15434 &cq_set->u.request, LPFC_CQ_CNT_1024);
15435 break;
15436 }
15437 bf_set(lpfc_mbx_cq_create_set_eq_id0,
15438 &cq_set->u.request, eq->queue_id);
15439 break;
15440 case 1:
15441 bf_set(lpfc_mbx_cq_create_set_eq_id1,
15442 &cq_set->u.request, eq->queue_id);
15443 break;
15444 case 2:
15445 bf_set(lpfc_mbx_cq_create_set_eq_id2,
15446 &cq_set->u.request, eq->queue_id);
15447 break;
15448 case 3:
15449 bf_set(lpfc_mbx_cq_create_set_eq_id3,
15450 &cq_set->u.request, eq->queue_id);
15451 break;
15452 case 4:
15453 bf_set(lpfc_mbx_cq_create_set_eq_id4,
15454 &cq_set->u.request, eq->queue_id);
15455 break;
15456 case 5:
15457 bf_set(lpfc_mbx_cq_create_set_eq_id5,
15458 &cq_set->u.request, eq->queue_id);
15459 break;
15460 case 6:
15461 bf_set(lpfc_mbx_cq_create_set_eq_id6,
15462 &cq_set->u.request, eq->queue_id);
15463 break;
15464 case 7:
15465 bf_set(lpfc_mbx_cq_create_set_eq_id7,
15466 &cq_set->u.request, eq->queue_id);
15467 break;
15468 case 8:
15469 bf_set(lpfc_mbx_cq_create_set_eq_id8,
15470 &cq_set->u.request, eq->queue_id);
15471 break;
15472 case 9:
15473 bf_set(lpfc_mbx_cq_create_set_eq_id9,
15474 &cq_set->u.request, eq->queue_id);
15475 break;
15476 case 10:
15477 bf_set(lpfc_mbx_cq_create_set_eq_id10,
15478 &cq_set->u.request, eq->queue_id);
15479 break;
15480 case 11:
15481 bf_set(lpfc_mbx_cq_create_set_eq_id11,
15482 &cq_set->u.request, eq->queue_id);
15483 break;
15484 case 12:
15485 bf_set(lpfc_mbx_cq_create_set_eq_id12,
15486 &cq_set->u.request, eq->queue_id);
15487 break;
15488 case 13:
15489 bf_set(lpfc_mbx_cq_create_set_eq_id13,
15490 &cq_set->u.request, eq->queue_id);
15491 break;
15492 case 14:
15493 bf_set(lpfc_mbx_cq_create_set_eq_id14,
15494 &cq_set->u.request, eq->queue_id);
15495 break;
15496 case 15:
15497 bf_set(lpfc_mbx_cq_create_set_eq_id15,
15498 &cq_set->u.request, eq->queue_id);
15499 break;
15500 }
15501
15502 /* link the cq onto the parent eq child list */
15503 list_add_tail(&cq->list, &eq->child_list);
15504 /* Set up completion queue's type and subtype */
15505 cq->type = type;
15506 cq->subtype = subtype;
15507 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080015508 cq->assoc_qp = eq;
James Smart2d7dbc42017-02-12 13:52:35 -080015509 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015510 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15511 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
15512 cq->entry_count);
James Smart81b96ed2017-11-20 16:00:29 -080015513 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080015514
15515 rc = 0;
15516 list_for_each_entry(dmabuf, &cq->page_list, list) {
15517 memset(dmabuf->virt, 0, hw_page_size);
15518 cnt = page_idx + dmabuf->buffer_tag;
15519 cq_set->u.request.page[cnt].addr_lo =
15520 putPaddrLow(dmabuf->phys);
15521 cq_set->u.request.page[cnt].addr_hi =
15522 putPaddrHigh(dmabuf->phys);
15523 rc++;
15524 }
15525 page_idx += rc;
15526 }
15527
15528 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15529
15530 /* The IOCTL status is embedded in the mailbox subheader. */
15531 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15532 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15533 if (shdr_status || shdr_add_status || rc) {
15534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15535 "3119 CQ_CREATE_SET mailbox failed with "
15536 "status x%x add_status x%x, mbx status x%x\n",
15537 shdr_status, shdr_add_status, rc);
15538 status = -ENXIO;
15539 goto out;
15540 }
15541 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15542 if (rc == 0xFFFF) {
15543 status = -ENXIO;
15544 goto out;
15545 }
15546
15547 for (idx = 0; idx < numcq; idx++) {
15548 cq = cqp[idx];
15549 cq->queue_id = rc + idx;
James Smart6a828b02019-01-28 11:14:31 -080015550 if (cq->queue_id > phba->sli4_hba.cq_max)
15551 phba->sli4_hba.cq_max = cq->queue_id;
James Smart2d7dbc42017-02-12 13:52:35 -080015552 }
15553
15554out:
15555 lpfc_sli4_mbox_cmd_free(phba, mbox);
15556 return status;
15557}
15558
15559/**
James Smartb19a0612010-04-06 14:48:51 -040015560 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040015561 * @phba: HBA structure that indicates port to create a queue on.
15562 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040015563 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15564 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040015565 *
James Smartb19a0612010-04-06 14:48:51 -040015566 * This function provides failback (fb) functionality when the
15567 * mq_create_ext fails on older FW generations. It's purpose is identical
15568 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040015569 *
James Smartb19a0612010-04-06 14:48:51 -040015570 * This routine cannot fail as all attributes were previously accessed and
15571 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040015572 **/
James Smartb19a0612010-04-06 14:48:51 -040015573static void
15574lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15575 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040015576{
15577 struct lpfc_mbx_mq_create *mq_create;
15578 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040015579 int length;
James Smart04c68492009-05-22 14:52:52 -040015580
James Smart04c68492009-05-22 14:52:52 -040015581 length = (sizeof(struct lpfc_mbx_mq_create) -
15582 sizeof(struct lpfc_sli4_cfg_mhdr));
15583 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15584 LPFC_MBOX_OPCODE_MQ_CREATE,
15585 length, LPFC_SLI4_MBX_EMBED);
15586 mq_create = &mbox->u.mqe.un.mq_create;
15587 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040015588 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040015589 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040015590 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040015591 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15592 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040015593 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015594 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15595 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040015596 break;
15597 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015598 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15599 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040015600 break;
15601 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015602 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15603 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040015604 break;
15605 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015606 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15607 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040015608 break;
15609 }
15610 list_for_each_entry(dmabuf, &mq->page_list, list) {
15611 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040015612 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040015613 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040015614 putPaddrHigh(dmabuf->phys);
15615 }
15616}
15617
15618/**
15619 * lpfc_mq_create - Create a mailbox Queue on the HBA
15620 * @phba: HBA structure that indicates port to create a queue on.
15621 * @mq: The queue structure to use to create the mailbox queue.
15622 * @cq: The completion queue to associate with this cq.
15623 * @subtype: The queue's subtype.
15624 *
15625 * This function creates a mailbox queue, as detailed in @mq, on a port,
15626 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15627 *
15628 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15629 * is used to get the entry count and entry size that are necessary to
15630 * determine the number of pages to allocate and use for this queue. This
15631 * function will send the MQ_CREATE mailbox command to the HBA to setup the
15632 * mailbox queue. This function is asynchronous and will wait for the mailbox
15633 * command to finish before continuing.
15634 *
15635 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015636 * memory this function will return -ENOMEM. If the queue create mailbox command
15637 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040015638 **/
15639int32_t
15640lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15641 struct lpfc_queue *cq, uint32_t subtype)
15642{
15643 struct lpfc_mbx_mq_create *mq_create;
15644 struct lpfc_mbx_mq_create_ext *mq_create_ext;
15645 struct lpfc_dmabuf *dmabuf;
15646 LPFC_MBOXQ_t *mbox;
15647 int rc, length, status = 0;
15648 uint32_t shdr_status, shdr_add_status;
15649 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015650 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040015651
James Smart2e90f4b2011-12-13 13:22:37 -050015652 /* sanity check on queue memory */
15653 if (!mq || !cq)
15654 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015655 if (!phba->sli4_hba.pc_sli4_params.supported)
15656 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040015657
15658 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15659 if (!mbox)
15660 return -ENOMEM;
15661 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15662 sizeof(struct lpfc_sli4_cfg_mhdr));
15663 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15664 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15665 length, LPFC_SLI4_MBX_EMBED);
15666
15667 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050015668 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050015669 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15670 &mq_create_ext->u.request, mq->page_count);
15671 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15672 &mq_create_ext->u.request, 1);
15673 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040015674 &mq_create_ext->u.request, 1);
15675 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15676 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050015677 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15678 &mq_create_ext->u.request, 1);
15679 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15680 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040015681 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015682 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15683 phba->sli4_hba.pc_sli4_params.mqv);
15684 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15685 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15686 cq->queue_id);
15687 else
15688 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15689 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015690 switch (mq->entry_count) {
15691 default:
15692 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15693 "0362 Unsupported MQ count. (%d)\n",
15694 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015695 if (mq->entry_count < 16) {
15696 status = -EINVAL;
15697 goto out;
15698 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060015699 /* fall through - otherwise default to smallest count */
James Smartb19a0612010-04-06 14:48:51 -040015700 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015701 bf_set(lpfc_mq_context_ring_size,
15702 &mq_create_ext->u.request.context,
15703 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015704 break;
15705 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015706 bf_set(lpfc_mq_context_ring_size,
15707 &mq_create_ext->u.request.context,
15708 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040015709 break;
15710 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015711 bf_set(lpfc_mq_context_ring_size,
15712 &mq_create_ext->u.request.context,
15713 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040015714 break;
15715 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015716 bf_set(lpfc_mq_context_ring_size,
15717 &mq_create_ext->u.request.context,
15718 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040015719 break;
15720 }
15721 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015722 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040015723 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
15724 putPaddrLow(dmabuf->phys);
15725 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040015726 putPaddrHigh(dmabuf->phys);
15727 }
15728 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040015729 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15730 &mq_create_ext->u.response);
15731 if (rc != MBX_SUCCESS) {
15732 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15733 "2795 MQ_CREATE_EXT failed with "
15734 "status x%x. Failback to MQ_CREATE.\n",
15735 rc);
15736 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
15737 mq_create = &mbox->u.mqe.un.mq_create;
15738 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15739 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
15740 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
15741 &mq_create->u.response);
15742 }
15743
James Smart04c68492009-05-22 14:52:52 -040015744 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040015745 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15746 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15747 if (shdr_status || shdr_add_status || rc) {
15748 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15749 "2502 MQ_CREATE mailbox failed with "
15750 "status x%x add_status x%x, mbx status x%x\n",
15751 shdr_status, shdr_add_status, rc);
15752 status = -ENXIO;
15753 goto out;
15754 }
James Smart04c68492009-05-22 14:52:52 -040015755 if (mq->queue_id == 0xFFFF) {
15756 status = -ENXIO;
15757 goto out;
15758 }
15759 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050015760 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040015761 mq->subtype = subtype;
15762 mq->host_index = 0;
15763 mq->hba_index = 0;
15764
15765 /* link the mq onto the parent cq child list */
15766 list_add_tail(&mq->list, &cq->child_list);
15767out:
James Smart8fa38512009-07-19 10:01:03 -040015768 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015769 return status;
15770}
15771
15772/**
James Smart4f774512009-05-22 14:52:35 -040015773 * lpfc_wq_create - Create a Work Queue on the HBA
15774 * @phba: HBA structure that indicates port to create a queue on.
15775 * @wq: The queue structure to use to create the work queue.
15776 * @cq: The completion queue to bind this work queue to.
15777 * @subtype: The subtype of the work queue indicating its functionality.
15778 *
15779 * This function creates a work queue, as detailed in @wq, on a port, described
15780 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
15781 *
15782 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15783 * is used to get the entry count and entry size that are necessary to
15784 * determine the number of pages to allocate and use for this queue. The @cq
15785 * is used to indicate which completion queue to bind this work queue to. This
15786 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15787 * work queue. This function is asynchronous and will wait for the mailbox
15788 * command to finish before continuing.
15789 *
15790 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015791 * memory this function will return -ENOMEM. If the queue create mailbox command
15792 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015793 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015794int
James Smart4f774512009-05-22 14:52:35 -040015795lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15796 struct lpfc_queue *cq, uint32_t subtype)
15797{
15798 struct lpfc_mbx_wq_create *wq_create;
15799 struct lpfc_dmabuf *dmabuf;
15800 LPFC_MBOXQ_t *mbox;
15801 int rc, length, status = 0;
15802 uint32_t shdr_status, shdr_add_status;
15803 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015804 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015805 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015806 void __iomem *bar_memmap_p;
15807 uint32_t db_offset;
15808 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080015809 uint8_t dpp_barset;
15810 uint32_t dpp_offset;
15811 unsigned long pg_addr;
James Smart81b96ed2017-11-20 16:00:29 -080015812 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015813
James Smart2e90f4b2011-12-13 13:22:37 -050015814 /* sanity check on queue memory */
15815 if (!wq || !cq)
15816 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015817 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015818 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015819
15820 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15821 if (!mbox)
15822 return -ENOMEM;
15823 length = (sizeof(struct lpfc_mbx_wq_create) -
15824 sizeof(struct lpfc_sli4_cfg_mhdr));
15825 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15826 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15827 length, LPFC_SLI4_MBX_EMBED);
15828 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015829 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015830 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15831 wq->page_count);
15832 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15833 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015834
15835 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015836 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15837 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015838
James Smartc176ffa2018-01-30 15:58:46 -080015839 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15840 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015841 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15842 else
15843 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15844
James Smart0c651872013-07-15 18:33:23 -040015845
James Smart1351e692018-02-22 08:18:43 -080015846 if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
15847 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15848 else
15849 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15850
15851 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015852 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015853 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15854 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015855 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15856 LPFC_Q_CREATE_VERSION_1);
15857
James Smart5a6f1332011-03-11 16:05:35 -050015858 switch (wq->entry_size) {
15859 default:
15860 case 64:
15861 bf_set(lpfc_mbx_wq_create_wqe_size,
15862 &wq_create->u.request_1,
15863 LPFC_WQ_WQE_SIZE_64);
15864 break;
15865 case 128:
15866 bf_set(lpfc_mbx_wq_create_wqe_size,
15867 &wq_create->u.request_1,
15868 LPFC_WQ_WQE_SIZE_128);
15869 break;
15870 }
James Smart1351e692018-02-22 08:18:43 -080015871 /* Request DPP by default */
15872 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080015873 bf_set(lpfc_mbx_wq_create_page_size,
15874 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015875 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015876 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015877 break;
15878 default:
James Smart1351e692018-02-22 08:18:43 -080015879 page = wq_create->u.request.page;
15880 break;
James Smart5a6f1332011-03-11 16:05:35 -050015881 }
James Smart0c651872013-07-15 18:33:23 -040015882
James Smart4f774512009-05-22 14:52:35 -040015883 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015884 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015885 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15886 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015887 }
James Smart962bc512013-01-03 15:44:00 -050015888
15889 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15890 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15891
James Smart4f774512009-05-22 14:52:35 -040015892 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15893 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015894 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15895 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15896 if (shdr_status || shdr_add_status || rc) {
15897 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15898 "2503 WQ_CREATE mailbox failed with "
15899 "status x%x add_status x%x, mbx status x%x\n",
15900 shdr_status, shdr_add_status, rc);
15901 status = -ENXIO;
15902 goto out;
15903 }
James Smart1351e692018-02-22 08:18:43 -080015904
15905 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
15906 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
15907 &wq_create->u.response);
15908 else
15909 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
15910 &wq_create->u.response_1);
15911
James Smart4f774512009-05-22 14:52:35 -040015912 if (wq->queue_id == 0xFFFF) {
15913 status = -ENXIO;
15914 goto out;
15915 }
James Smart1351e692018-02-22 08:18:43 -080015916
15917 wq->db_format = LPFC_DB_LIST_FORMAT;
15918 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
15919 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15920 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15921 &wq_create->u.response);
15922 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15923 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15924 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15925 "3265 WQ[%d] doorbell format "
15926 "not supported: x%x\n",
15927 wq->queue_id, wq->db_format);
15928 status = -EINVAL;
15929 goto out;
15930 }
15931 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15932 &wq_create->u.response);
15933 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15934 pci_barset);
15935 if (!bar_memmap_p) {
15936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15937 "3263 WQ[%d] failed to memmap "
15938 "pci barset:x%x\n",
15939 wq->queue_id, pci_barset);
15940 status = -ENOMEM;
15941 goto out;
15942 }
15943 db_offset = wq_create->u.response.doorbell_offset;
15944 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15945 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15946 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15947 "3252 WQ[%d] doorbell offset "
15948 "not supported: x%x\n",
15949 wq->queue_id, db_offset);
15950 status = -EINVAL;
15951 goto out;
15952 }
15953 wq->db_regaddr = bar_memmap_p + db_offset;
15954 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15955 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15956 "format:x%x\n", wq->queue_id,
15957 pci_barset, db_offset, wq->db_format);
15958 } else
15959 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050015960 } else {
James Smart1351e692018-02-22 08:18:43 -080015961 /* Check if DPP was honored by the firmware */
15962 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
15963 &wq_create->u.response_1);
15964 if (wq->dpp_enable) {
15965 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
15966 &wq_create->u.response_1);
15967 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15968 pci_barset);
15969 if (!bar_memmap_p) {
15970 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15971 "3267 WQ[%d] failed to memmap "
15972 "pci barset:x%x\n",
15973 wq->queue_id, pci_barset);
15974 status = -ENOMEM;
15975 goto out;
15976 }
15977 db_offset = wq_create->u.response_1.doorbell_offset;
15978 wq->db_regaddr = bar_memmap_p + db_offset;
15979 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
15980 &wq_create->u.response_1);
15981 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
15982 &wq_create->u.response_1);
15983 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
15984 dpp_barset);
15985 if (!bar_memmap_p) {
15986 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15987 "3268 WQ[%d] failed to memmap "
15988 "pci barset:x%x\n",
15989 wq->queue_id, dpp_barset);
15990 status = -ENOMEM;
15991 goto out;
15992 }
15993 dpp_offset = wq_create->u.response_1.dpp_offset;
15994 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
15995 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15996 "3271 WQ[%d]: barset:x%x, offset:x%x, "
15997 "dpp_id:x%x dpp_barset:x%x "
15998 "dpp_offset:x%x\n",
15999 wq->queue_id, pci_barset, db_offset,
16000 wq->dpp_id, dpp_barset, dpp_offset);
16001
16002 /* Enable combined writes for DPP aperture */
16003 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
16004#ifdef CONFIG_X86
16005 rc = set_memory_wc(pg_addr, 1);
16006 if (rc) {
16007 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16008 "3272 Cannot setup Combined "
16009 "Write on WQ[%d] - disable DPP\n",
16010 wq->queue_id);
16011 phba->cfg_enable_dpp = 0;
16012 }
16013#else
16014 phba->cfg_enable_dpp = 0;
16015#endif
16016 } else
16017 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050016018 }
James Smart895427b2017-02-12 13:52:30 -080016019 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
16020 if (wq->pring == NULL) {
16021 status = -ENOMEM;
16022 goto out;
16023 }
James Smart4f774512009-05-22 14:52:35 -040016024 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050016025 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040016026 wq->subtype = subtype;
16027 wq->host_index = 0;
16028 wq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016029 wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040016030
16031 /* link the wq onto the parent cq child list */
16032 list_add_tail(&wq->list, &cq->child_list);
16033out:
James Smart8fa38512009-07-19 10:01:03 -040016034 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016035 return status;
16036}
16037
16038/**
16039 * lpfc_rq_create - Create a Receive Queue on the HBA
16040 * @phba: HBA structure that indicates port to create a queue on.
16041 * @hrq: The queue structure to use to create the header receive queue.
16042 * @drq: The queue structure to use to create the data receive queue.
16043 * @cq: The completion queue to bind this work queue to.
16044 *
16045 * This function creates a receive buffer queue pair , as detailed in @hrq and
16046 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
16047 * to the HBA.
16048 *
16049 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
16050 * struct is used to get the entry count that is necessary to determine the
16051 * number of pages to use for this queue. The @cq is used to indicate which
16052 * completion queue to bind received buffers that are posted to these queues to.
16053 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
16054 * receive queue pair. This function is asynchronous and will wait for the
16055 * mailbox command to finish before continuing.
16056 *
16057 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040016058 * memory this function will return -ENOMEM. If the queue create mailbox command
16059 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016060 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016061int
James Smart4f774512009-05-22 14:52:35 -040016062lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16063 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
16064{
16065 struct lpfc_mbx_rq_create *rq_create;
16066 struct lpfc_dmabuf *dmabuf;
16067 LPFC_MBOXQ_t *mbox;
16068 int rc, length, status = 0;
16069 uint32_t shdr_status, shdr_add_status;
16070 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040016071 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050016072 void __iomem *bar_memmap_p;
16073 uint32_t db_offset;
16074 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040016075
James Smart2e90f4b2011-12-13 13:22:37 -050016076 /* sanity check on queue memory */
16077 if (!hrq || !drq || !cq)
16078 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040016079 if (!phba->sli4_hba.pc_sli4_params.supported)
16080 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040016081
16082 if (hrq->entry_count != drq->entry_count)
16083 return -EINVAL;
16084 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16085 if (!mbox)
16086 return -ENOMEM;
16087 length = (sizeof(struct lpfc_mbx_rq_create) -
16088 sizeof(struct lpfc_sli4_cfg_mhdr));
16089 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16090 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
16091 length, LPFC_SLI4_MBX_EMBED);
16092 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050016093 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
16094 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16095 phba->sli4_hba.pc_sli4_params.rqv);
16096 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
16097 bf_set(lpfc_rq_context_rqe_count_1,
16098 &rq_create->u.request.context,
16099 hrq->entry_count);
16100 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040016101 bf_set(lpfc_rq_context_rqe_size,
16102 &rq_create->u.request.context,
16103 LPFC_RQE_SIZE_8);
16104 bf_set(lpfc_rq_context_page_size,
16105 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080016106 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050016107 } else {
16108 switch (hrq->entry_count) {
16109 default:
16110 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16111 "2535 Unsupported RQ count. (%d)\n",
16112 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040016113 if (hrq->entry_count < 512) {
16114 status = -EINVAL;
16115 goto out;
16116 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060016117 /* fall through - otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050016118 case 512:
16119 bf_set(lpfc_rq_context_rqe_count,
16120 &rq_create->u.request.context,
16121 LPFC_RQ_RING_SIZE_512);
16122 break;
16123 case 1024:
16124 bf_set(lpfc_rq_context_rqe_count,
16125 &rq_create->u.request.context,
16126 LPFC_RQ_RING_SIZE_1024);
16127 break;
16128 case 2048:
16129 bf_set(lpfc_rq_context_rqe_count,
16130 &rq_create->u.request.context,
16131 LPFC_RQ_RING_SIZE_2048);
16132 break;
16133 case 4096:
16134 bf_set(lpfc_rq_context_rqe_count,
16135 &rq_create->u.request.context,
16136 LPFC_RQ_RING_SIZE_4096);
16137 break;
16138 }
16139 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
16140 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040016141 }
16142 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
16143 cq->queue_id);
16144 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
16145 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040016146 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040016147 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040016148 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16149 putPaddrLow(dmabuf->phys);
16150 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16151 putPaddrHigh(dmabuf->phys);
16152 }
James Smart962bc512013-01-03 15:44:00 -050016153 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16154 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
16155
James Smart4f774512009-05-22 14:52:35 -040016156 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16157 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040016158 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_INIT,
16162 "2504 RQ_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 goto out;
16167 }
16168 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16169 if (hrq->queue_id == 0xFFFF) {
16170 status = -ENXIO;
16171 goto out;
16172 }
James Smart962bc512013-01-03 15:44:00 -050016173
16174 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16175 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
16176 &rq_create->u.response);
16177 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
16178 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
16179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16180 "3262 RQ [%d] doorbell format not "
16181 "supported: x%x\n", hrq->queue_id,
16182 hrq->db_format);
16183 status = -EINVAL;
16184 goto out;
16185 }
16186
16187 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
16188 &rq_create->u.response);
16189 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
16190 if (!bar_memmap_p) {
16191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16192 "3269 RQ[%d] failed to memmap pci "
16193 "barset:x%x\n", hrq->queue_id,
16194 pci_barset);
16195 status = -ENOMEM;
16196 goto out;
16197 }
16198
16199 db_offset = rq_create->u.response.doorbell_offset;
16200 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
16201 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
16202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16203 "3270 RQ[%d] doorbell offset not "
16204 "supported: x%x\n", hrq->queue_id,
16205 db_offset);
16206 status = -EINVAL;
16207 goto out;
16208 }
16209 hrq->db_regaddr = bar_memmap_p + db_offset;
16210 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040016211 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
16212 "format:x%x\n", hrq->queue_id, pci_barset,
16213 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050016214 } else {
16215 hrq->db_format = LPFC_DB_RING_FORMAT;
16216 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16217 }
James Smart4f774512009-05-22 14:52:35 -040016218 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050016219 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040016220 hrq->subtype = subtype;
16221 hrq->host_index = 0;
16222 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016223 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040016224
16225 /* now create the data queue */
16226 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16227 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
16228 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050016229 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16230 phba->sli4_hba.pc_sli4_params.rqv);
16231 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
16232 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040016233 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070016234 if (subtype == LPFC_NVMET)
16235 rq_create->u.request.context.buffer_size =
16236 LPFC_NVMET_DATA_BUF_SIZE;
16237 else
16238 rq_create->u.request.context.buffer_size =
16239 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040016240 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
16241 LPFC_RQE_SIZE_8);
16242 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
16243 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050016244 } else {
16245 switch (drq->entry_count) {
16246 default:
16247 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16248 "2536 Unsupported RQ count. (%d)\n",
16249 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040016250 if (drq->entry_count < 512) {
16251 status = -EINVAL;
16252 goto out;
16253 }
Gustavo A. R. Silva5bd5f662018-11-27 22:32:18 -060016254 /* fall through - otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050016255 case 512:
16256 bf_set(lpfc_rq_context_rqe_count,
16257 &rq_create->u.request.context,
16258 LPFC_RQ_RING_SIZE_512);
16259 break;
16260 case 1024:
16261 bf_set(lpfc_rq_context_rqe_count,
16262 &rq_create->u.request.context,
16263 LPFC_RQ_RING_SIZE_1024);
16264 break;
16265 case 2048:
16266 bf_set(lpfc_rq_context_rqe_count,
16267 &rq_create->u.request.context,
16268 LPFC_RQ_RING_SIZE_2048);
16269 break;
16270 case 4096:
16271 bf_set(lpfc_rq_context_rqe_count,
16272 &rq_create->u.request.context,
16273 LPFC_RQ_RING_SIZE_4096);
16274 break;
16275 }
James Smart3c603be2017-05-15 15:20:44 -070016276 if (subtype == LPFC_NVMET)
16277 bf_set(lpfc_rq_context_buf_size,
16278 &rq_create->u.request.context,
16279 LPFC_NVMET_DATA_BUF_SIZE);
16280 else
16281 bf_set(lpfc_rq_context_buf_size,
16282 &rq_create->u.request.context,
16283 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040016284 }
16285 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
16286 cq->queue_id);
16287 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
16288 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040016289 list_for_each_entry(dmabuf, &drq->page_list, list) {
16290 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16291 putPaddrLow(dmabuf->phys);
16292 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16293 putPaddrHigh(dmabuf->phys);
16294 }
James Smart962bc512013-01-03 15:44:00 -050016295 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16296 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040016297 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16298 /* The IOCTL status is embedded in the mailbox subheader. */
16299 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
16300 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16301 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16302 if (shdr_status || shdr_add_status || rc) {
16303 status = -ENXIO;
16304 goto out;
16305 }
16306 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16307 if (drq->queue_id == 0xFFFF) {
16308 status = -ENXIO;
16309 goto out;
16310 }
16311 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050016312 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040016313 drq->subtype = subtype;
16314 drq->host_index = 0;
16315 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016316 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040016317
16318 /* link the header and data RQs onto the parent cq child list */
16319 list_add_tail(&hrq->list, &cq->child_list);
16320 list_add_tail(&drq->list, &cq->child_list);
16321
16322out:
James Smart8fa38512009-07-19 10:01:03 -040016323 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016324 return status;
16325}
16326
16327/**
James Smart2d7dbc42017-02-12 13:52:35 -080016328 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
16329 * @phba: HBA structure that indicates port to create a queue on.
16330 * @hrqp: The queue structure array to use to create the header receive queues.
16331 * @drqp: The queue structure array to use to create the data receive queues.
16332 * @cqp: The completion queue array to bind these receive queues to.
16333 *
16334 * This function creates a receive buffer queue pair , as detailed in @hrq and
16335 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
16336 * to the HBA.
16337 *
16338 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
16339 * struct is used to get the entry count that is necessary to determine the
16340 * number of pages to use for this queue. The @cq is used to indicate which
16341 * completion queue to bind received buffers that are posted to these queues to.
16342 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
16343 * receive queue pair. This function is asynchronous and will wait for the
16344 * mailbox command to finish before continuing.
16345 *
16346 * On success this function will return a zero. If unable to allocate enough
16347 * memory this function will return -ENOMEM. If the queue create mailbox command
16348 * fails this function will return -ENXIO.
16349 **/
16350int
16351lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
16352 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
16353 uint32_t subtype)
16354{
16355 struct lpfc_queue *hrq, *drq, *cq;
16356 struct lpfc_mbx_rq_create_v2 *rq_create;
16357 struct lpfc_dmabuf *dmabuf;
16358 LPFC_MBOXQ_t *mbox;
16359 int rc, length, alloclen, status = 0;
16360 int cnt, idx, numrq, page_idx = 0;
16361 uint32_t shdr_status, shdr_add_status;
16362 union lpfc_sli4_cfg_shdr *shdr;
16363 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16364
16365 numrq = phba->cfg_nvmet_mrq;
16366 /* sanity check on array memory */
16367 if (!hrqp || !drqp || !cqp || !numrq)
16368 return -ENODEV;
16369 if (!phba->sli4_hba.pc_sli4_params.supported)
16370 hw_page_size = SLI4_PAGE_SIZE;
16371
16372 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16373 if (!mbox)
16374 return -ENOMEM;
16375
16376 length = sizeof(struct lpfc_mbx_rq_create_v2);
16377 length += ((2 * numrq * hrqp[0]->page_count) *
16378 sizeof(struct dma_address));
16379
16380 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16381 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
16382 LPFC_SLI4_MBX_NEMBED);
16383 if (alloclen < length) {
16384 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16385 "3099 Allocated DMA memory size (%d) is "
16386 "less than the requested DMA memory size "
16387 "(%d)\n", alloclen, length);
16388 status = -ENOMEM;
16389 goto out;
16390 }
16391
16392
16393
16394 rq_create = mbox->sge_array->addr[0];
16395 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
16396
16397 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
16398 cnt = 0;
16399
16400 for (idx = 0; idx < numrq; idx++) {
16401 hrq = hrqp[idx];
16402 drq = drqp[idx];
16403 cq = cqp[idx];
16404
James Smart2d7dbc42017-02-12 13:52:35 -080016405 /* sanity check on queue memory */
16406 if (!hrq || !drq || !cq) {
16407 status = -ENODEV;
16408 goto out;
16409 }
16410
James Smart7aabe842017-03-04 09:30:22 -080016411 if (hrq->entry_count != drq->entry_count) {
16412 status = -EINVAL;
16413 goto out;
16414 }
16415
James Smart2d7dbc42017-02-12 13:52:35 -080016416 if (idx == 0) {
16417 bf_set(lpfc_mbx_rq_create_num_pages,
16418 &rq_create->u.request,
16419 hrq->page_count);
16420 bf_set(lpfc_mbx_rq_create_rq_cnt,
16421 &rq_create->u.request, (numrq * 2));
16422 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
16423 1);
16424 bf_set(lpfc_rq_context_base_cq,
16425 &rq_create->u.request.context,
16426 cq->queue_id);
16427 bf_set(lpfc_rq_context_data_size,
16428 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070016429 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080016430 bf_set(lpfc_rq_context_hdr_size,
16431 &rq_create->u.request.context,
16432 LPFC_HDR_BUF_SIZE);
16433 bf_set(lpfc_rq_context_rqe_count_1,
16434 &rq_create->u.request.context,
16435 hrq->entry_count);
16436 bf_set(lpfc_rq_context_rqe_size,
16437 &rq_create->u.request.context,
16438 LPFC_RQE_SIZE_8);
16439 bf_set(lpfc_rq_context_page_size,
16440 &rq_create->u.request.context,
16441 (PAGE_SIZE/SLI4_PAGE_SIZE));
16442 }
16443 rc = 0;
16444 list_for_each_entry(dmabuf, &hrq->page_list, list) {
16445 memset(dmabuf->virt, 0, hw_page_size);
16446 cnt = page_idx + dmabuf->buffer_tag;
16447 rq_create->u.request.page[cnt].addr_lo =
16448 putPaddrLow(dmabuf->phys);
16449 rq_create->u.request.page[cnt].addr_hi =
16450 putPaddrHigh(dmabuf->phys);
16451 rc++;
16452 }
16453 page_idx += rc;
16454
16455 rc = 0;
16456 list_for_each_entry(dmabuf, &drq->page_list, list) {
16457 memset(dmabuf->virt, 0, hw_page_size);
16458 cnt = page_idx + dmabuf->buffer_tag;
16459 rq_create->u.request.page[cnt].addr_lo =
16460 putPaddrLow(dmabuf->phys);
16461 rq_create->u.request.page[cnt].addr_hi =
16462 putPaddrHigh(dmabuf->phys);
16463 rc++;
16464 }
16465 page_idx += rc;
16466
16467 hrq->db_format = LPFC_DB_RING_FORMAT;
16468 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16469 hrq->type = LPFC_HRQ;
16470 hrq->assoc_qid = cq->queue_id;
16471 hrq->subtype = subtype;
16472 hrq->host_index = 0;
16473 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016474 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016475
16476 drq->db_format = LPFC_DB_RING_FORMAT;
16477 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16478 drq->type = LPFC_DRQ;
16479 drq->assoc_qid = cq->queue_id;
16480 drq->subtype = subtype;
16481 drq->host_index = 0;
16482 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016483 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016484
16485 list_add_tail(&hrq->list, &cq->child_list);
16486 list_add_tail(&drq->list, &cq->child_list);
16487 }
16488
16489 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16490 /* The IOCTL status is embedded in the mailbox subheader. */
16491 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16492 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16493 if (shdr_status || shdr_add_status || rc) {
16494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16495 "3120 RQ_CREATE mailbox failed with "
16496 "status x%x add_status x%x, mbx status x%x\n",
16497 shdr_status, shdr_add_status, rc);
16498 status = -ENXIO;
16499 goto out;
16500 }
16501 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16502 if (rc == 0xFFFF) {
16503 status = -ENXIO;
16504 goto out;
16505 }
16506
16507 /* Initialize all RQs with associated queue id */
16508 for (idx = 0; idx < numrq; idx++) {
16509 hrq = hrqp[idx];
16510 hrq->queue_id = rc + (2 * idx);
16511 drq = drqp[idx];
16512 drq->queue_id = rc + (2 * idx) + 1;
16513 }
16514
16515out:
16516 lpfc_sli4_mbox_cmd_free(phba, mbox);
16517 return status;
16518}
16519
16520/**
James Smart4f774512009-05-22 14:52:35 -040016521 * lpfc_eq_destroy - Destroy an event Queue on the HBA
16522 * @eq: The queue structure associated with the queue to destroy.
16523 *
16524 * This function destroys a queue, as detailed in @eq by sending an mailbox
16525 * command, specific to the type of queue, to the HBA.
16526 *
16527 * The @eq struct is used to get the queue ID of the queue to destroy.
16528 *
16529 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016530 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016531 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016532int
James Smart4f774512009-05-22 14:52:35 -040016533lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16534{
16535 LPFC_MBOXQ_t *mbox;
16536 int rc, length, status = 0;
16537 uint32_t shdr_status, shdr_add_status;
16538 union lpfc_sli4_cfg_shdr *shdr;
16539
James Smart2e90f4b2011-12-13 13:22:37 -050016540 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016541 if (!eq)
16542 return -ENODEV;
James Smart32517fc2019-01-28 11:14:33 -080016543
James Smart4f774512009-05-22 14:52:35 -040016544 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16545 if (!mbox)
16546 return -ENOMEM;
16547 length = (sizeof(struct lpfc_mbx_eq_destroy) -
16548 sizeof(struct lpfc_sli4_cfg_mhdr));
16549 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16550 LPFC_MBOX_OPCODE_EQ_DESTROY,
16551 length, LPFC_SLI4_MBX_EMBED);
16552 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16553 eq->queue_id);
16554 mbox->vport = eq->phba->pport;
16555 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16556
16557 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16558 /* The IOCTL status is embedded in the mailbox subheader. */
16559 shdr = (union lpfc_sli4_cfg_shdr *)
16560 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16561 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16562 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16563 if (shdr_status || shdr_add_status || rc) {
16564 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16565 "2505 EQ_DESTROY mailbox failed with "
16566 "status x%x add_status x%x, mbx status x%x\n",
16567 shdr_status, shdr_add_status, rc);
16568 status = -ENXIO;
16569 }
16570
16571 /* Remove eq from any list */
16572 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040016573 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016574 return status;
16575}
16576
16577/**
16578 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
16579 * @cq: The queue structure associated with the queue to destroy.
16580 *
16581 * This function destroys a queue, as detailed in @cq by sending an mailbox
16582 * command, specific to the type of queue, to the HBA.
16583 *
16584 * The @cq struct is used to get the queue ID of the queue to destroy.
16585 *
16586 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016587 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016588 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016589int
James Smart4f774512009-05-22 14:52:35 -040016590lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16591{
16592 LPFC_MBOXQ_t *mbox;
16593 int rc, length, status = 0;
16594 uint32_t shdr_status, shdr_add_status;
16595 union lpfc_sli4_cfg_shdr *shdr;
16596
James Smart2e90f4b2011-12-13 13:22:37 -050016597 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016598 if (!cq)
16599 return -ENODEV;
16600 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16601 if (!mbox)
16602 return -ENOMEM;
16603 length = (sizeof(struct lpfc_mbx_cq_destroy) -
16604 sizeof(struct lpfc_sli4_cfg_mhdr));
16605 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16606 LPFC_MBOX_OPCODE_CQ_DESTROY,
16607 length, LPFC_SLI4_MBX_EMBED);
16608 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16609 cq->queue_id);
16610 mbox->vport = cq->phba->pport;
16611 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16612 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16613 /* The IOCTL status is embedded in the mailbox subheader. */
16614 shdr = (union lpfc_sli4_cfg_shdr *)
16615 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
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_INIT,
16620 "2506 CQ_DESTROY 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 }
16625 /* Remove cq from any list */
16626 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040016627 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016628 return status;
16629}
16630
16631/**
James Smart04c68492009-05-22 14:52:52 -040016632 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
16633 * @qm: The queue structure associated with the queue to destroy.
16634 *
16635 * This function destroys a queue, as detailed in @mq by sending an mailbox
16636 * command, specific to the type of queue, to the HBA.
16637 *
16638 * The @mq struct is used to get the queue ID of the queue to destroy.
16639 *
16640 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016641 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040016642 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016643int
James Smart04c68492009-05-22 14:52:52 -040016644lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16645{
16646 LPFC_MBOXQ_t *mbox;
16647 int rc, length, status = 0;
16648 uint32_t shdr_status, shdr_add_status;
16649 union lpfc_sli4_cfg_shdr *shdr;
16650
James Smart2e90f4b2011-12-13 13:22:37 -050016651 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040016652 if (!mq)
16653 return -ENODEV;
16654 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16655 if (!mbox)
16656 return -ENOMEM;
16657 length = (sizeof(struct lpfc_mbx_mq_destroy) -
16658 sizeof(struct lpfc_sli4_cfg_mhdr));
16659 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16660 LPFC_MBOX_OPCODE_MQ_DESTROY,
16661 length, LPFC_SLI4_MBX_EMBED);
16662 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16663 mq->queue_id);
16664 mbox->vport = mq->phba->pport;
16665 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16666 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16667 /* The IOCTL status is embedded in the mailbox subheader. */
16668 shdr = (union lpfc_sli4_cfg_shdr *)
16669 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16670 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16671 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16672 if (shdr_status || shdr_add_status || rc) {
16673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16674 "2507 MQ_DESTROY mailbox failed with "
16675 "status x%x add_status x%x, mbx status x%x\n",
16676 shdr_status, shdr_add_status, rc);
16677 status = -ENXIO;
16678 }
16679 /* Remove mq from any list */
16680 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040016681 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016682 return status;
16683}
16684
16685/**
James Smart4f774512009-05-22 14:52:35 -040016686 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
16687 * @wq: The queue structure associated with the queue to destroy.
16688 *
16689 * This function destroys a queue, as detailed in @wq by sending an mailbox
16690 * command, specific to the type of queue, to the HBA.
16691 *
16692 * The @wq struct is used to get the queue ID of the queue to destroy.
16693 *
16694 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016695 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016696 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016697int
James Smart4f774512009-05-22 14:52:35 -040016698lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16699{
16700 LPFC_MBOXQ_t *mbox;
16701 int rc, length, status = 0;
16702 uint32_t shdr_status, shdr_add_status;
16703 union lpfc_sli4_cfg_shdr *shdr;
16704
James Smart2e90f4b2011-12-13 13:22:37 -050016705 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016706 if (!wq)
16707 return -ENODEV;
16708 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
16709 if (!mbox)
16710 return -ENOMEM;
16711 length = (sizeof(struct lpfc_mbx_wq_destroy) -
16712 sizeof(struct lpfc_sli4_cfg_mhdr));
16713 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16714 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
16715 length, LPFC_SLI4_MBX_EMBED);
16716 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
16717 wq->queue_id);
16718 mbox->vport = wq->phba->pport;
16719 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16720 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
16721 shdr = (union lpfc_sli4_cfg_shdr *)
16722 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
16723 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16724 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16725 if (shdr_status || shdr_add_status || rc) {
16726 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16727 "2508 WQ_DESTROY mailbox failed with "
16728 "status x%x add_status x%x, mbx status x%x\n",
16729 shdr_status, shdr_add_status, rc);
16730 status = -ENXIO;
16731 }
16732 /* Remove wq from any list */
16733 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070016734 kfree(wq->pring);
16735 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040016736 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016737 return status;
16738}
16739
16740/**
16741 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
16742 * @rq: The queue structure associated with the queue to destroy.
16743 *
16744 * This function destroys a queue, as detailed in @rq by sending an mailbox
16745 * command, specific to the type of queue, to the HBA.
16746 *
16747 * The @rq struct is used to get the queue ID of the queue to destroy.
16748 *
16749 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016750 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016751 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016752int
James Smart4f774512009-05-22 14:52:35 -040016753lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16754 struct lpfc_queue *drq)
16755{
16756 LPFC_MBOXQ_t *mbox;
16757 int rc, length, status = 0;
16758 uint32_t shdr_status, shdr_add_status;
16759 union lpfc_sli4_cfg_shdr *shdr;
16760
James Smart2e90f4b2011-12-13 13:22:37 -050016761 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016762 if (!hrq || !drq)
16763 return -ENODEV;
16764 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
16765 if (!mbox)
16766 return -ENOMEM;
16767 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050016768 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040016769 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16770 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
16771 length, LPFC_SLI4_MBX_EMBED);
16772 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16773 hrq->queue_id);
16774 mbox->vport = hrq->phba->pport;
16775 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16776 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
16777 /* The IOCTL status is embedded in the mailbox subheader. */
16778 shdr = (union lpfc_sli4_cfg_shdr *)
16779 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16780 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16781 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16782 if (shdr_status || shdr_add_status || rc) {
16783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16784 "2509 RQ_DESTROY mailbox failed with "
16785 "status x%x add_status x%x, mbx status x%x\n",
16786 shdr_status, shdr_add_status, rc);
16787 if (rc != MBX_TIMEOUT)
16788 mempool_free(mbox, hrq->phba->mbox_mem_pool);
16789 return -ENXIO;
16790 }
16791 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
16792 drq->queue_id);
16793 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
16794 shdr = (union lpfc_sli4_cfg_shdr *)
16795 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
16796 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16797 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16798 if (shdr_status || shdr_add_status || rc) {
16799 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16800 "2510 RQ_DESTROY mailbox failed with "
16801 "status x%x add_status x%x, mbx status x%x\n",
16802 shdr_status, shdr_add_status, rc);
16803 status = -ENXIO;
16804 }
16805 list_del_init(&hrq->list);
16806 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040016807 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016808 return status;
16809}
16810
16811/**
16812 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
16813 * @phba: The virtual port for which this call being executed.
16814 * @pdma_phys_addr0: Physical address of the 1st SGL page.
16815 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
16816 * @xritag: the xritag that ties this io to the SGL pages.
16817 *
16818 * This routine will post the sgl pages for the IO that has the xritag
16819 * that is in the iocbq structure. The xritag is assigned during iocbq
16820 * creation and persists for as long as the driver is loaded.
16821 * if the caller has fewer than 256 scatter gather segments to map then
16822 * pdma_phys_addr1 should be 0.
16823 * If the caller needs to map more than 256 scatter gather segment then
16824 * pdma_phys_addr1 should be a valid physical address.
16825 * physical address for SGLs must be 64 byte aligned.
16826 * If you are going to map 2 SGL's then the first one must have 256 entries
16827 * the second sgl can have between 1 and 256 entries.
16828 *
16829 * Return codes:
16830 * 0 - Success
16831 * -ENXIO, -ENOMEM - Failure
16832 **/
16833int
16834lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16835 dma_addr_t pdma_phys_addr0,
16836 dma_addr_t pdma_phys_addr1,
16837 uint16_t xritag)
16838{
16839 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16840 LPFC_MBOXQ_t *mbox;
16841 int rc;
16842 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016843 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016844 union lpfc_sli4_cfg_shdr *shdr;
16845
16846 if (xritag == NO_XRI) {
16847 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16848 "0364 Invalid param:\n");
16849 return -EINVAL;
16850 }
16851
16852 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16853 if (!mbox)
16854 return -ENOMEM;
16855
16856 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16857 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16858 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016859 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016860
16861 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16862 &mbox->u.mqe.un.post_sgl_pages;
16863 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16864 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16865
16866 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16867 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16868 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16869 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16870
16871 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16872 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16873 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16874 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16875 if (!phba->sli4_hba.intr_enable)
16876 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016877 else {
James Smarta183a152011-10-10 21:32:43 -040016878 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016879 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16880 }
James Smart4f774512009-05-22 14:52:35 -040016881 /* The IOCTL status is embedded in the mailbox subheader. */
16882 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16883 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16884 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16885 if (rc != MBX_TIMEOUT)
16886 mempool_free(mbox, phba->mbox_mem_pool);
16887 if (shdr_status || shdr_add_status || rc) {
16888 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16889 "2511 POST_SGL mailbox failed with "
16890 "status x%x add_status x%x, mbx status x%x\n",
16891 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016892 }
16893 return 0;
16894}
James Smart4f774512009-05-22 14:52:35 -040016895
16896/**
James Smart88a2cfb2011-07-22 18:36:33 -040016897 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016898 * @phba: pointer to lpfc hba data structure.
16899 *
16900 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016901 * HBA consistent with the SLI-4 interface spec. This routine
16902 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16903 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016904 *
James Smart88a2cfb2011-07-22 18:36:33 -040016905 * Returns
16906 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16907 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16908 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016909static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016910lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16911{
16912 unsigned long xri;
16913
16914 /*
16915 * Fetch the next logical xri. Because this index is logical,
16916 * the driver starts at 0 each time.
16917 */
16918 spin_lock_irq(&phba->hbalock);
16919 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16920 phba->sli4_hba.max_cfg_param.max_xri, 0);
16921 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16922 spin_unlock_irq(&phba->hbalock);
16923 return NO_XRI;
16924 } else {
16925 set_bit(xri, phba->sli4_hba.xri_bmask);
16926 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016927 }
James Smart6d368e52011-05-24 11:44:12 -040016928 spin_unlock_irq(&phba->hbalock);
16929 return xri;
16930}
16931
16932/**
16933 * lpfc_sli4_free_xri - Release an xri for reuse.
16934 * @phba: pointer to lpfc hba data structure.
16935 *
16936 * This routine is invoked to release an xri to the pool of
16937 * available rpis maintained by the driver.
16938 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016939static void
James Smart6d368e52011-05-24 11:44:12 -040016940__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16941{
16942 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016943 phba->sli4_hba.max_cfg_param.xri_used--;
16944 }
16945}
16946
16947/**
16948 * lpfc_sli4_free_xri - Release an xri for reuse.
16949 * @phba: pointer to lpfc hba data structure.
16950 *
16951 * This routine is invoked to release an xri to the pool of
16952 * available rpis maintained by the driver.
16953 **/
16954void
16955lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16956{
16957 spin_lock_irq(&phba->hbalock);
16958 __lpfc_sli4_free_xri(phba, xri);
16959 spin_unlock_irq(&phba->hbalock);
16960}
16961
16962/**
James Smart4f774512009-05-22 14:52:35 -040016963 * lpfc_sli4_next_xritag - Get an xritag for the io
16964 * @phba: Pointer to HBA context object.
16965 *
16966 * This function gets an xritag for the iocb. If there is no unused xritag
16967 * it will return 0xffff.
16968 * The function returns the allocated xritag if successful, else returns zero.
16969 * Zero is not a valid xritag.
16970 * The caller is not required to hold any lock.
16971 **/
16972uint16_t
16973lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16974{
James Smart6d368e52011-05-24 11:44:12 -040016975 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016976
James Smart6d368e52011-05-24 11:44:12 -040016977 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016978 if (xri_index == NO_XRI)
16979 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16980 "2004 Failed to allocate XRI.last XRITAG is %d"
16981 " Max XRI is %d, Used XRI is %d\n",
16982 xri_index,
16983 phba->sli4_hba.max_cfg_param.max_xri,
16984 phba->sli4_hba.max_cfg_param.xri_used);
16985 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016986}
16987
16988/**
James Smart895427b2017-02-12 13:52:30 -080016989 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016990 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016991 * @post_sgl_list: pointer to els sgl entry list.
16992 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016993 *
16994 * This routine is invoked to post a block of driver's sgl pages to the
16995 * HBA using non-embedded mailbox command. No Lock is held. This routine
16996 * is only called when the driver is loading and after all IO has been
16997 * stopped.
16998 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016999static int
James Smart895427b2017-02-12 13:52:30 -080017000lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040017001 struct list_head *post_sgl_list,
17002 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040017003{
James Smart8a9d2e82012-05-09 21:16:12 -040017004 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040017005 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
17006 struct sgl_page_pairs *sgl_pg_pairs;
17007 void *viraddr;
17008 LPFC_MBOXQ_t *mbox;
17009 uint32_t reqlen, alloclen, pg_pairs;
17010 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040017011 uint16_t xritag_start = 0;
17012 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040017013 uint32_t shdr_status, shdr_add_status;
17014 union lpfc_sli4_cfg_shdr *shdr;
17015
James Smart895427b2017-02-12 13:52:30 -080017016 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040017017 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040017018 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080017019 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040017020 "2559 Block sgl registration required DMA "
17021 "size (%d) great than a page\n", reqlen);
17022 return -ENOMEM;
17023 }
James Smart895427b2017-02-12 13:52:30 -080017024
James Smart4f774512009-05-22 14:52:35 -040017025 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040017026 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040017027 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040017028
17029 /* Allocate DMA memory and set up the non-embedded mailbox command */
17030 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17031 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
17032 LPFC_SLI4_MBX_NEMBED);
17033
17034 if (alloclen < reqlen) {
17035 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17036 "0285 Allocated DMA memory size (%d) is "
17037 "less than the requested DMA memory "
17038 "size (%d)\n", alloclen, reqlen);
17039 lpfc_sli4_mbox_cmd_free(phba, mbox);
17040 return -ENOMEM;
17041 }
James Smart4f774512009-05-22 14:52:35 -040017042 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040017043 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040017044 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
17045 sgl_pg_pairs = &sgl->sgl_pg_pairs;
17046
James Smart8a9d2e82012-05-09 21:16:12 -040017047 pg_pairs = 0;
17048 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040017049 /* Set up the sge entry */
17050 sgl_pg_pairs->sgl_pg0_addr_lo =
17051 cpu_to_le32(putPaddrLow(sglq_entry->phys));
17052 sgl_pg_pairs->sgl_pg0_addr_hi =
17053 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
17054 sgl_pg_pairs->sgl_pg1_addr_lo =
17055 cpu_to_le32(putPaddrLow(0));
17056 sgl_pg_pairs->sgl_pg1_addr_hi =
17057 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040017058
James Smart4f774512009-05-22 14:52:35 -040017059 /* Keep the first xritag on the list */
17060 if (pg_pairs == 0)
17061 xritag_start = sglq_entry->sli4_xritag;
17062 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040017063 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040017064 }
James Smart6d368e52011-05-24 11:44:12 -040017065
17066 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040017067 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080017068 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040017069 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080017070
James Smart4f774512009-05-22 14:52:35 -040017071 if (!phba->sli4_hba.intr_enable)
17072 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17073 else {
James Smarta183a152011-10-10 21:32:43 -040017074 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040017075 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17076 }
17077 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
17078 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17079 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17080 if (rc != MBX_TIMEOUT)
17081 lpfc_sli4_mbox_cmd_free(phba, mbox);
17082 if (shdr_status || shdr_add_status || rc) {
17083 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17084 "2513 POST_SGL_BLOCK mailbox command failed "
17085 "status x%x add_status x%x mbx status x%x\n",
17086 shdr_status, shdr_add_status, rc);
17087 rc = -ENXIO;
17088 }
17089 return rc;
17090}
17091
17092/**
James Smart5e5b5112019-01-28 11:14:22 -080017093 * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
James Smart4f774512009-05-22 14:52:35 -040017094 * @phba: pointer to lpfc hba data structure.
James Smart0794d602019-01-28 11:14:19 -080017095 * @nblist: pointer to nvme buffer list.
James Smart4f774512009-05-22 14:52:35 -040017096 * @count: number of scsi buffers on the list.
17097 *
17098 * This routine is invoked to post a block of @count scsi sgl pages from a
James Smart0794d602019-01-28 11:14:19 -080017099 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
James Smart4f774512009-05-22 14:52:35 -040017100 * No Lock is held.
17101 *
17102 **/
James Smart0794d602019-01-28 11:14:19 -080017103static int
James Smart5e5b5112019-01-28 11:14:22 -080017104lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
17105 int count)
James Smart4f774512009-05-22 14:52:35 -040017106{
James Smartc4908502019-01-28 11:14:28 -080017107 struct lpfc_io_buf *lpfc_ncmd;
James Smart4f774512009-05-22 14:52:35 -040017108 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
17109 struct sgl_page_pairs *sgl_pg_pairs;
17110 void *viraddr;
17111 LPFC_MBOXQ_t *mbox;
17112 uint32_t reqlen, alloclen, pg_pairs;
17113 uint32_t mbox_tmo;
17114 uint16_t xritag_start = 0;
17115 int rc = 0;
17116 uint32_t shdr_status, shdr_add_status;
17117 dma_addr_t pdma_phys_bpl1;
17118 union lpfc_sli4_cfg_shdr *shdr;
17119
17120 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040017121 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040017122 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040017123 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040017124 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080017125 "6118 Block sgl registration required DMA "
James Smart4f774512009-05-22 14:52:35 -040017126 "size (%d) great than a page\n", reqlen);
17127 return -ENOMEM;
17128 }
17129 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17130 if (!mbox) {
17131 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080017132 "6119 Failed to allocate mbox cmd memory\n");
James Smart4f774512009-05-22 14:52:35 -040017133 return -ENOMEM;
17134 }
17135
17136 /* Allocate DMA memory and set up the non-embedded mailbox command */
17137 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
James Smart0794d602019-01-28 11:14:19 -080017138 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17139 reqlen, LPFC_SLI4_MBX_NEMBED);
James Smart4f774512009-05-22 14:52:35 -040017140
17141 if (alloclen < reqlen) {
17142 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080017143 "6120 Allocated DMA memory size (%d) is "
James Smart4f774512009-05-22 14:52:35 -040017144 "less than the requested DMA memory "
17145 "size (%d)\n", alloclen, reqlen);
17146 lpfc_sli4_mbox_cmd_free(phba, mbox);
17147 return -ENOMEM;
17148 }
James Smart6d368e52011-05-24 11:44:12 -040017149
James Smart4f774512009-05-22 14:52:35 -040017150 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040017151 viraddr = mbox->sge_array->addr[0];
17152
17153 /* Set up the SGL pages in the non-embedded DMA pages */
17154 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
17155 sgl_pg_pairs = &sgl->sgl_pg_pairs;
17156
17157 pg_pairs = 0;
James Smart0794d602019-01-28 11:14:19 -080017158 list_for_each_entry(lpfc_ncmd, nblist, list) {
James Smart4f774512009-05-22 14:52:35 -040017159 /* Set up the sge entry */
17160 sgl_pg_pairs->sgl_pg0_addr_lo =
James Smart0794d602019-01-28 11:14:19 -080017161 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040017162 sgl_pg_pairs->sgl_pg0_addr_hi =
James Smart0794d602019-01-28 11:14:19 -080017163 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040017164 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
James Smart0794d602019-01-28 11:14:19 -080017165 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
17166 SGL_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040017167 else
17168 pdma_phys_bpl1 = 0;
17169 sgl_pg_pairs->sgl_pg1_addr_lo =
17170 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
17171 sgl_pg_pairs->sgl_pg1_addr_hi =
17172 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
17173 /* Keep the first xritag on the list */
17174 if (pg_pairs == 0)
James Smart0794d602019-01-28 11:14:19 -080017175 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040017176 sgl_pg_pairs++;
17177 pg_pairs++;
17178 }
17179 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
17180 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
17181 /* Perform endian conversion if necessary */
17182 sgl->word0 = cpu_to_le32(sgl->word0);
17183
James Smart0794d602019-01-28 11:14:19 -080017184 if (!phba->sli4_hba.intr_enable) {
James Smart4f774512009-05-22 14:52:35 -040017185 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart0794d602019-01-28 11:14:19 -080017186 } else {
James Smarta183a152011-10-10 21:32:43 -040017187 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040017188 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17189 }
James Smart0794d602019-01-28 11:14:19 -080017190 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040017191 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17192 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17193 if (rc != MBX_TIMEOUT)
17194 lpfc_sli4_mbox_cmd_free(phba, mbox);
17195 if (shdr_status || shdr_add_status || rc) {
17196 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart0794d602019-01-28 11:14:19 -080017197 "6125 POST_SGL_BLOCK mailbox command failed "
James Smart4f774512009-05-22 14:52:35 -040017198 "status x%x add_status x%x mbx status x%x\n",
17199 shdr_status, shdr_add_status, rc);
17200 rc = -ENXIO;
17201 }
17202 return rc;
17203}
17204
17205/**
James Smart5e5b5112019-01-28 11:14:22 -080017206 * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
James Smart0794d602019-01-28 11:14:19 -080017207 * @phba: pointer to lpfc hba data structure.
17208 * @post_nblist: pointer to the nvme buffer list.
17209 *
17210 * This routine walks a list of nvme buffers that was passed in. It attempts
17211 * to construct blocks of nvme buffer sgls which contains contiguous xris and
17212 * uses the non-embedded SGL block post mailbox commands to post to the port.
17213 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
17214 * embedded SGL post mailbox command for posting. The @post_nblist passed in
17215 * must be local list, thus no lock is needed when manipulate the list.
17216 *
17217 * Returns: 0 = failure, non-zero number of successfully posted buffers.
17218 **/
17219int
James Smart5e5b5112019-01-28 11:14:22 -080017220lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
17221 struct list_head *post_nblist, int sb_count)
James Smart0794d602019-01-28 11:14:19 -080017222{
James Smartc4908502019-01-28 11:14:28 -080017223 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart0794d602019-01-28 11:14:19 -080017224 int status, sgl_size;
17225 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
17226 dma_addr_t pdma_phys_sgl1;
17227 int last_xritag = NO_XRI;
17228 int cur_xritag;
James Smart0794d602019-01-28 11:14:19 -080017229 LIST_HEAD(prep_nblist);
17230 LIST_HEAD(blck_nblist);
17231 LIST_HEAD(nvme_nblist);
17232
17233 /* sanity check */
17234 if (sb_count <= 0)
17235 return -EINVAL;
17236
17237 sgl_size = phba->cfg_sg_dma_buf_size;
17238 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
17239 list_del_init(&lpfc_ncmd->list);
17240 block_cnt++;
17241 if ((last_xritag != NO_XRI) &&
17242 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
17243 /* a hole in xri block, form a sgl posting block */
17244 list_splice_init(&prep_nblist, &blck_nblist);
17245 post_cnt = block_cnt - 1;
17246 /* prepare list for next posting block */
17247 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
17248 block_cnt = 1;
17249 } else {
17250 /* prepare list for next posting block */
17251 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
17252 /* enough sgls for non-embed sgl mbox command */
17253 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
17254 list_splice_init(&prep_nblist, &blck_nblist);
17255 post_cnt = block_cnt;
17256 block_cnt = 0;
17257 }
17258 }
17259 num_posting++;
17260 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
17261
17262 /* end of repost sgl list condition for NVME buffers */
17263 if (num_posting == sb_count) {
17264 if (post_cnt == 0) {
17265 /* last sgl posting block */
17266 list_splice_init(&prep_nblist, &blck_nblist);
17267 post_cnt = block_cnt;
17268 } else if (block_cnt == 1) {
17269 /* last single sgl with non-contiguous xri */
17270 if (sgl_size > SGL_PAGE_SIZE)
17271 pdma_phys_sgl1 =
17272 lpfc_ncmd->dma_phys_sgl +
17273 SGL_PAGE_SIZE;
17274 else
17275 pdma_phys_sgl1 = 0;
17276 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
17277 status = lpfc_sli4_post_sgl(
17278 phba, lpfc_ncmd->dma_phys_sgl,
17279 pdma_phys_sgl1, cur_xritag);
17280 if (status) {
James Smartc4908502019-01-28 11:14:28 -080017281 /* Post error. Buffer unavailable. */
17282 lpfc_ncmd->flags |=
17283 LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080017284 } else {
James Smartc4908502019-01-28 11:14:28 -080017285 /* Post success. Bffer available. */
17286 lpfc_ncmd->flags &=
17287 ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080017288 lpfc_ncmd->status = IOSTAT_SUCCESS;
17289 num_posted++;
17290 }
17291 /* success, put on NVME buffer sgl list */
17292 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
17293 }
17294 }
17295
17296 /* continue until a nembed page worth of sgls */
17297 if (post_cnt == 0)
17298 continue;
17299
17300 /* post block of NVME buffer list sgls */
James Smart5e5b5112019-01-28 11:14:22 -080017301 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
17302 post_cnt);
James Smart0794d602019-01-28 11:14:19 -080017303
17304 /* don't reset xirtag due to hole in xri block */
17305 if (block_cnt == 0)
17306 last_xritag = NO_XRI;
17307
17308 /* reset NVME buffer post count for next round of posting */
17309 post_cnt = 0;
17310
17311 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
17312 while (!list_empty(&blck_nblist)) {
17313 list_remove_head(&blck_nblist, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -080017314 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -080017315 if (status) {
James Smartc4908502019-01-28 11:14:28 -080017316 /* Post error. Mark buffer unavailable. */
17317 lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080017318 } else {
James Smartc4908502019-01-28 11:14:28 -080017319 /* Post success, Mark buffer available. */
17320 lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080017321 lpfc_ncmd->status = IOSTAT_SUCCESS;
17322 num_posted++;
17323 }
17324 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
17325 }
17326 }
17327 /* Push NVME buffers with sgl posted to the available list */
James Smart5e5b5112019-01-28 11:14:22 -080017328 lpfc_io_buf_replenish(phba, &nvme_nblist);
17329
James Smart0794d602019-01-28 11:14:19 -080017330 return num_posted;
17331}
17332
17333/**
James Smart4f774512009-05-22 14:52:35 -040017334 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
17335 * @phba: pointer to lpfc_hba struct that the frame was received on
17336 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17337 *
17338 * This function checks the fields in the @fc_hdr to see if the FC frame is a
17339 * valid type of frame that the LPFC driver will handle. This function will
17340 * return a zero if the frame is a valid frame or a non zero value when the
17341 * frame does not pass the check.
17342 **/
17343static int
17344lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
17345{
Tomas Henzl474ffb72010-12-22 16:52:40 +010017346 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040017347 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050017348 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040017349
James Smarte62245d2019-08-14 16:57:08 -070017350#define FC_RCTL_MDS_DIAGS 0xF4
17351
James Smart4f774512009-05-22 14:52:35 -040017352 switch (fc_hdr->fh_r_ctl) {
17353 case FC_RCTL_DD_UNCAT: /* uncategorized information */
17354 case FC_RCTL_DD_SOL_DATA: /* solicited data */
17355 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
17356 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
17357 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
17358 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
17359 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
17360 case FC_RCTL_DD_CMD_STATUS: /* command status */
17361 case FC_RCTL_ELS_REQ: /* extended link services request */
17362 case FC_RCTL_ELS_REP: /* extended link services reply */
17363 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
17364 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
17365 case FC_RCTL_BA_NOP: /* basic link service NOP */
17366 case FC_RCTL_BA_ABTS: /* basic link service abort */
17367 case FC_RCTL_BA_RMC: /* remove connection */
17368 case FC_RCTL_BA_ACC: /* basic accept */
17369 case FC_RCTL_BA_RJT: /* basic reject */
17370 case FC_RCTL_BA_PRMT:
17371 case FC_RCTL_ACK_1: /* acknowledge_1 */
17372 case FC_RCTL_ACK_0: /* acknowledge_0 */
17373 case FC_RCTL_P_RJT: /* port reject */
17374 case FC_RCTL_F_RJT: /* fabric reject */
17375 case FC_RCTL_P_BSY: /* port busy */
17376 case FC_RCTL_F_BSY: /* fabric busy to data frame */
17377 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
17378 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070017379 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040017380 case FC_RCTL_END: /* end */
17381 break;
17382 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
17383 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
17384 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
17385 return lpfc_fc_frame_check(phba, fc_hdr);
17386 default:
17387 goto drop;
17388 }
James Smartae9e28f2017-05-15 15:20:51 -070017389
James Smart4f774512009-05-22 14:52:35 -040017390 switch (fc_hdr->fh_type) {
17391 case FC_TYPE_BLS:
17392 case FC_TYPE_ELS:
17393 case FC_TYPE_FCP:
17394 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080017395 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040017396 break;
17397 case FC_TYPE_IP:
17398 case FC_TYPE_ILS:
17399 default:
17400 goto drop;
17401 }
James Smart546fc852011-03-11 16:06:29 -050017402
James Smart4f774512009-05-22 14:52:35 -040017403 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070017404 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040017405 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070017406 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
17407 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
17408 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
17409 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
17410 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040017411 return 0;
17412drop:
17413 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070017414 "2539 Dropped frame rctl:x%x type:x%x\n",
17415 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040017416 return 1;
17417}
17418
17419/**
17420 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
17421 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17422 *
17423 * This function processes the FC header to retrieve the VFI from the VF
17424 * header, if one exists. This function will return the VFI if one exists
17425 * or 0 if no VSAN Header exists.
17426 **/
17427static uint32_t
17428lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
17429{
17430 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
17431
17432 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
17433 return 0;
17434 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
17435}
17436
17437/**
17438 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
17439 * @phba: Pointer to the HBA structure to search for the vport on
17440 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17441 * @fcfi: The FC Fabric ID that the frame came from
17442 *
17443 * This function searches the @phba for a vport that matches the content of the
17444 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
17445 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
17446 * returns the matching vport pointer or NULL if unable to match frame to a
17447 * vport.
17448 **/
17449static struct lpfc_vport *
17450lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080017451 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040017452{
17453 struct lpfc_vport **vports;
17454 struct lpfc_vport *vport = NULL;
17455 int i;
James Smart939723a2012-05-09 21:19:03 -040017456
James Smartbf086112011-08-21 21:48:13 -040017457 if (did == Fabric_DID)
17458 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040017459 if ((phba->pport->fc_flag & FC_PT2PT) &&
17460 !(phba->link_state == LPFC_HBA_READY))
17461 return phba->pport;
17462
James Smart4f774512009-05-22 14:52:35 -040017463 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080017464 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040017465 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
17466 if (phba->fcf.fcfi == fcfi &&
17467 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
17468 vports[i]->fc_myDID == did) {
17469 vport = vports[i];
17470 break;
17471 }
17472 }
James Smart895427b2017-02-12 13:52:30 -080017473 }
James Smart4f774512009-05-22 14:52:35 -040017474 lpfc_destroy_vport_work_array(phba, vports);
17475 return vport;
17476}
17477
17478/**
James Smart45ed1192009-10-02 15:17:02 -040017479 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
17480 * @vport: The vport to work on.
17481 *
17482 * This function updates the receive sequence time stamp for this vport. The
17483 * receive sequence time stamp indicates the time that the last frame of the
17484 * the sequence that has been idle for the longest amount of time was received.
17485 * the driver uses this time stamp to indicate if any received sequences have
17486 * timed out.
17487 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017488static void
James Smart45ed1192009-10-02 15:17:02 -040017489lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
17490{
17491 struct lpfc_dmabuf *h_buf;
17492 struct hbq_dmabuf *dmabuf = NULL;
17493
17494 /* get the oldest sequence on the rcv list */
17495 h_buf = list_get_first(&vport->rcv_buffer_list,
17496 struct lpfc_dmabuf, list);
17497 if (!h_buf)
17498 return;
17499 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17500 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
17501}
17502
17503/**
17504 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17505 * @vport: The vport that the received sequences were sent to.
17506 *
17507 * This function cleans up all outstanding received sequences. This is called
17508 * by the driver when a link event or user action invalidates all the received
17509 * sequences.
17510 **/
17511void
17512lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17513{
17514 struct lpfc_dmabuf *h_buf, *hnext;
17515 struct lpfc_dmabuf *d_buf, *dnext;
17516 struct hbq_dmabuf *dmabuf = NULL;
17517
17518 /* start with the oldest sequence on the rcv list */
17519 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17520 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17521 list_del_init(&dmabuf->hbuf.list);
17522 list_for_each_entry_safe(d_buf, dnext,
17523 &dmabuf->dbuf.list, list) {
17524 list_del_init(&d_buf->list);
17525 lpfc_in_buf_free(vport->phba, d_buf);
17526 }
17527 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17528 }
17529}
17530
17531/**
17532 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17533 * @vport: The vport that the received sequences were sent to.
17534 *
17535 * This function determines whether any received sequences have timed out by
17536 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17537 * indicates that there is at least one timed out sequence this routine will
17538 * go through the received sequences one at a time from most inactive to most
17539 * active to determine which ones need to be cleaned up. Once it has determined
17540 * that a sequence needs to be cleaned up it will simply free up the resources
17541 * without sending an abort.
17542 **/
17543void
17544lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17545{
17546 struct lpfc_dmabuf *h_buf, *hnext;
17547 struct lpfc_dmabuf *d_buf, *dnext;
17548 struct hbq_dmabuf *dmabuf = NULL;
17549 unsigned long timeout;
17550 int abort_count = 0;
17551
17552 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17553 vport->rcv_buffer_time_stamp);
17554 if (list_empty(&vport->rcv_buffer_list) ||
17555 time_before(jiffies, timeout))
17556 return;
17557 /* start with the oldest sequence on the rcv list */
17558 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17559 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17560 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17561 dmabuf->time_stamp);
17562 if (time_before(jiffies, timeout))
17563 break;
17564 abort_count++;
17565 list_del_init(&dmabuf->hbuf.list);
17566 list_for_each_entry_safe(d_buf, dnext,
17567 &dmabuf->dbuf.list, list) {
17568 list_del_init(&d_buf->list);
17569 lpfc_in_buf_free(vport->phba, d_buf);
17570 }
17571 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17572 }
17573 if (abort_count)
17574 lpfc_update_rcv_time_stamp(vport);
17575}
17576
17577/**
James Smart4f774512009-05-22 14:52:35 -040017578 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
17579 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17580 *
17581 * This function searches through the existing incomplete sequences that have
17582 * been sent to this @vport. If the frame matches one of the incomplete
17583 * sequences then the dbuf in the @dmabuf is added to the list of frames that
17584 * make up that sequence. If no sequence is found that matches this frame then
17585 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17586 * This function returns a pointer to the first dmabuf in the sequence list that
17587 * the frame was linked to.
17588 **/
17589static struct hbq_dmabuf *
17590lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17591{
17592 struct fc_frame_header *new_hdr;
17593 struct fc_frame_header *temp_hdr;
17594 struct lpfc_dmabuf *d_buf;
17595 struct lpfc_dmabuf *h_buf;
17596 struct hbq_dmabuf *seq_dmabuf = NULL;
17597 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050017598 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040017599
James Smart4d9ab992009-10-02 15:16:39 -040017600 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017601 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040017602 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050017603
James Smart4f774512009-05-22 14:52:35 -040017604 /* Use the hdr_buf to find the sequence that this frame belongs to */
17605 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17606 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17607 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17608 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17609 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17610 continue;
17611 /* found a pending sequence that matches this frame */
17612 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17613 break;
17614 }
17615 if (!seq_dmabuf) {
17616 /*
17617 * This indicates first frame received for this sequence.
17618 * Queue the buffer on the vport's rcv_buffer_list.
17619 */
17620 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040017621 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017622 return dmabuf;
17623 }
17624 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050017625 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17626 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040017627 list_del_init(&seq_dmabuf->hbuf.list);
17628 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17629 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017630 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017631 return dmabuf;
17632 }
James Smart45ed1192009-10-02 15:17:02 -040017633 /* move this sequence to the tail to indicate a young sequence */
17634 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17635 seq_dmabuf->time_stamp = jiffies;
17636 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050017637 if (list_empty(&seq_dmabuf->dbuf.list)) {
17638 temp_hdr = dmabuf->hbuf.virt;
17639 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17640 return seq_dmabuf;
17641 }
James Smart4f774512009-05-22 14:52:35 -040017642 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050017643 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17644 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040017645 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17646 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17647 /*
17648 * If the frame's sequence count is greater than the frame on
17649 * the list then insert the frame right after this frame
17650 */
James Smarteeead812009-12-21 17:01:23 -050017651 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17652 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040017653 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050017654 found = 1;
17655 break;
James Smart4f774512009-05-22 14:52:35 -040017656 }
James Smart4360ca92015-12-16 18:12:04 -050017657
17658 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17659 break;
17660 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040017661 }
James Smart4360ca92015-12-16 18:12:04 -050017662
17663 if (found)
17664 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017665 return NULL;
17666}
17667
17668/**
James Smart6669f9b2009-10-02 15:16:45 -040017669 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17670 * @vport: pointer to a vitural port
17671 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17672 *
17673 * This function tries to abort from the partially assembed sequence, described
17674 * by the information from basic abbort @dmabuf. It checks to see whether such
17675 * partially assembled sequence held by the driver. If so, it shall free up all
17676 * the frames from the partially assembled sequence.
17677 *
17678 * Return
17679 * true -- if there is matching partially assembled sequence present and all
17680 * the frames freed with the sequence;
17681 * false -- if there is no matching partially assembled sequence present so
17682 * nothing got aborted in the lower layer driver
17683 **/
17684static bool
17685lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17686 struct hbq_dmabuf *dmabuf)
17687{
17688 struct fc_frame_header *new_hdr;
17689 struct fc_frame_header *temp_hdr;
17690 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17691 struct hbq_dmabuf *seq_dmabuf = NULL;
17692
17693 /* Use the hdr_buf to find the sequence that matches this frame */
17694 INIT_LIST_HEAD(&dmabuf->dbuf.list);
17695 INIT_LIST_HEAD(&dmabuf->hbuf.list);
17696 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17697 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17698 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17699 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17700 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17701 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17702 continue;
17703 /* found a pending sequence that matches this frame */
17704 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17705 break;
17706 }
17707
17708 /* Free up all the frames from the partially assembled sequence */
17709 if (seq_dmabuf) {
17710 list_for_each_entry_safe(d_buf, n_buf,
17711 &seq_dmabuf->dbuf.list, list) {
17712 list_del_init(&d_buf->list);
17713 lpfc_in_buf_free(vport->phba, d_buf);
17714 }
17715 return true;
17716 }
17717 return false;
17718}
17719
17720/**
James Smart6dd9e312013-01-03 15:43:37 -050017721 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
17722 * @vport: pointer to a vitural port
17723 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17724 *
17725 * This function tries to abort from the assembed sequence from upper level
17726 * protocol, described by the information from basic abbort @dmabuf. It
17727 * checks to see whether such pending context exists at upper level protocol.
17728 * If so, it shall clean up the pending context.
17729 *
17730 * Return
17731 * true -- if there is matching pending context of the sequence cleaned
17732 * at ulp;
17733 * false -- if there is no matching pending context of the sequence present
17734 * at ulp.
17735 **/
17736static bool
17737lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17738{
17739 struct lpfc_hba *phba = vport->phba;
17740 int handled;
17741
17742 /* Accepting abort at ulp with SLI4 only */
17743 if (phba->sli_rev < LPFC_SLI_REV4)
17744 return false;
17745
17746 /* Register all caring upper level protocols to attend abort */
17747 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
17748 if (handled)
17749 return true;
17750
17751 return false;
17752}
17753
17754/**
James Smart546fc852011-03-11 16:06:29 -050017755 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040017756 * @phba: Pointer to HBA context object.
17757 * @cmd_iocbq: pointer to the command iocbq structure.
17758 * @rsp_iocbq: pointer to the response iocbq structure.
17759 *
James Smart546fc852011-03-11 16:06:29 -050017760 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040017761 * event. It properly releases the memory allocated to the sequence abort
17762 * accept iocb.
17763 **/
17764static void
James Smart546fc852011-03-11 16:06:29 -050017765lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040017766 struct lpfc_iocbq *cmd_iocbq,
17767 struct lpfc_iocbq *rsp_iocbq)
17768{
James Smart6dd9e312013-01-03 15:43:37 -050017769 struct lpfc_nodelist *ndlp;
17770
17771 if (cmd_iocbq) {
17772 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
17773 lpfc_nlp_put(ndlp);
17774 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017775 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050017776 }
James Smart6b5151f2012-01-18 16:24:06 -050017777
17778 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
17779 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
17780 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17781 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
17782 rsp_iocbq->iocb.ulpStatus,
17783 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040017784}
17785
17786/**
James Smart6d368e52011-05-24 11:44:12 -040017787 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
17788 * @phba: Pointer to HBA context object.
17789 * @xri: xri id in transaction.
17790 *
17791 * This function validates the xri maps to the known range of XRIs allocated an
17792 * used by the driver.
17793 **/
James Smart7851fe22011-07-22 18:36:52 -040017794uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017795lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
17796 uint16_t xri)
17797{
James Smarta2fc4aef2014-09-03 12:57:55 -040017798 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040017799
17800 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
17801 if (xri == phba->sli4_hba.xri_ids[i])
17802 return i;
17803 }
17804 return NO_XRI;
17805}
17806
James Smart6d368e52011-05-24 11:44:12 -040017807/**
James Smart546fc852011-03-11 16:06:29 -050017808 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017809 * @phba: Pointer to HBA context object.
17810 * @fc_hdr: pointer to a FC frame header.
17811 *
James Smart546fc852011-03-11 16:06:29 -050017812 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040017813 * event after aborting the sequence handling.
17814 **/
James Smart86c67372017-04-21 16:05:04 -070017815void
James Smart6dd9e312013-01-03 15:43:37 -050017816lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
17817 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040017818{
James Smart6dd9e312013-01-03 15:43:37 -050017819 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040017820 struct lpfc_iocbq *ctiocb = NULL;
17821 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040017822 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050017823 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040017824 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050017825 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040017826
17827 if (!lpfc_is_link_up(phba))
17828 return;
17829
17830 sid = sli4_sid_from_fc_hdr(fc_hdr);
17831 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050017832 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040017833
James Smart6dd9e312013-01-03 15:43:37 -050017834 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040017835 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070017836 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050017837 if (!ndlp) {
17838 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17839 "1268 Failed to allocate ndlp for "
17840 "oxid:x%x SID:x%x\n", oxid, sid);
17841 return;
17842 }
James Smart6dd9e312013-01-03 15:43:37 -050017843 /* Put ndlp onto pport node list */
17844 lpfc_enqueue_node(vport, ndlp);
17845 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
17846 /* re-setup ndlp without removing from node list */
17847 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
17848 if (!ndlp) {
17849 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
17850 "3275 Failed to active ndlp found "
17851 "for oxid:x%x SID:x%x\n", oxid, sid);
17852 return;
17853 }
James Smart6669f9b2009-10-02 15:16:45 -040017854 }
17855
James Smart546fc852011-03-11 16:06:29 -050017856 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040017857 ctiocb = lpfc_sli_get_iocbq(phba);
17858 if (!ctiocb)
17859 return;
17860
James Smart5ffc2662009-11-18 15:39:44 -050017861 /* Extract the F_CTL field from FC_HDR */
17862 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
17863
James Smart6669f9b2009-10-02 15:16:45 -040017864 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040017865 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050017866 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040017867 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
17868 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
17869 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
17870
17871 /* Fill in the rest of iocb fields */
17872 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
17873 icmd->ulpBdeCount = 0;
17874 icmd->ulpLe = 1;
17875 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040017876 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050017877 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040017878
James Smart6669f9b2009-10-02 15:16:45 -040017879 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050017880 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040017881 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050017882 ctiocb->sli4_xritag = NO_XRI;
17883
James Smartee0f4fe2012-05-09 21:19:14 -040017884 if (fctl & FC_FC_EX_CTX)
17885 /* Exchange responder sent the abort so we
17886 * own the oxid.
17887 */
17888 xri = oxid;
17889 else
17890 xri = rxid;
17891 lxri = lpfc_sli4_xri_inrange(phba, xri);
17892 if (lxri != NO_XRI)
17893 lpfc_set_rrq_active(phba, ndlp, lxri,
17894 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050017895 /* For BA_ABTS from exchange responder, if the logical xri with
17896 * the oxid maps to the FCP XRI range, the port no longer has
17897 * that exchange context, send a BLS_RJT. Override the IOCB for
17898 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050017899 */
James Smart6dd9e312013-01-03 15:43:37 -050017900 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080017901 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050017902 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17903 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17904 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17905 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17906 }
17907
17908 /* If BA_ABTS failed to abort a partially assembled receive sequence,
17909 * the driver no longer has that exchange, send a BLS_RJT. Override
17910 * the IOCB for a BA_RJT.
17911 */
17912 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050017913 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
17914 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
17915 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
17916 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
17917 }
James Smart6669f9b2009-10-02 15:16:45 -040017918
James Smart5ffc2662009-11-18 15:39:44 -050017919 if (fctl & FC_FC_EX_CTX) {
17920 /* ABTS sent by responder to CT exchange, construction
17921 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
17922 * field and RX_ID from ABTS for RX_ID field.
17923 */
James Smart546fc852011-03-11 16:06:29 -050017924 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050017925 } else {
17926 /* ABTS sent by initiator to CT exchange, construction
17927 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050017928 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050017929 */
James Smart546fc852011-03-11 16:06:29 -050017930 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050017931 }
James Smartf09c3ac2012-03-01 22:33:29 -050017932 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050017933 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050017934
James Smart546fc852011-03-11 16:06:29 -050017935 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050017936 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
17937 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
17938 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050017939
17940 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
17941 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050017942 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
17943 "2925 Failed to issue CT ABTS RSP x%x on "
17944 "xri x%x, Data x%x\n",
17945 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
17946 phba->link_state);
17947 lpfc_nlp_put(ndlp);
17948 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050017949 lpfc_sli_release_iocbq(phba, ctiocb);
17950 }
James Smart6669f9b2009-10-02 15:16:45 -040017951}
17952
17953/**
17954 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17955 * @vport: Pointer to the vport on which this sequence was received
17956 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17957 *
17958 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17959 * receive sequence is only partially assembed by the driver, it shall abort
17960 * the partially assembled frames for the sequence. Otherwise, if the
17961 * unsolicited receive sequence has been completely assembled and passed to
17962 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17963 * unsolicited sequence has been aborted. After that, it will issue a basic
17964 * accept to accept the abort.
17965 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017966static void
James Smart6669f9b2009-10-02 15:16:45 -040017967lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17968 struct hbq_dmabuf *dmabuf)
17969{
17970 struct lpfc_hba *phba = vport->phba;
17971 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017972 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017973 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017974
James Smart6669f9b2009-10-02 15:16:45 -040017975 /* Make a copy of fc_hdr before the dmabuf being released */
17976 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017977 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017978
James Smart5ffc2662009-11-18 15:39:44 -050017979 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017980 /* ABTS by responder to exchange, no cleanup needed */
17981 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017982 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017983 /* ABTS by initiator to exchange, need to do cleanup */
17984 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17985 if (aborted == false)
17986 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017987 }
James Smart6dd9e312013-01-03 15:43:37 -050017988 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17989
James Smart86c67372017-04-21 16:05:04 -070017990 if (phba->nvmet_support) {
17991 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17992 return;
17993 }
17994
James Smart6dd9e312013-01-03 15:43:37 -050017995 /* Respond with BA_ACC or BA_RJT accordingly */
17996 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017997}
17998
17999/**
James Smart4f774512009-05-22 14:52:35 -040018000 * lpfc_seq_complete - Indicates if a sequence is complete
18001 * @dmabuf: pointer to a dmabuf that describes the FC sequence
18002 *
18003 * This function checks the sequence, starting with the frame described by
18004 * @dmabuf, to see if all the frames associated with this sequence are present.
18005 * the frames associated with this sequence are linked to the @dmabuf using the
18006 * dbuf list. This function looks for two major things. 1) That the first frame
18007 * has a sequence count of zero. 2) There is a frame with last frame of sequence
18008 * set. 3) That there are no holes in the sequence count. The function will
18009 * return 1 when the sequence is complete, otherwise it will return 0.
18010 **/
18011static int
18012lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
18013{
18014 struct fc_frame_header *hdr;
18015 struct lpfc_dmabuf *d_buf;
18016 struct hbq_dmabuf *seq_dmabuf;
18017 uint32_t fctl;
18018 int seq_count = 0;
18019
18020 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18021 /* make sure first fame of sequence has a sequence count of zero */
18022 if (hdr->fh_seq_cnt != seq_count)
18023 return 0;
18024 fctl = (hdr->fh_f_ctl[0] << 16 |
18025 hdr->fh_f_ctl[1] << 8 |
18026 hdr->fh_f_ctl[2]);
18027 /* If last frame of sequence we can return success. */
18028 if (fctl & FC_FC_END_SEQ)
18029 return 1;
18030 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
18031 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18032 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
18033 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050018034 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040018035 return 0;
18036 fctl = (hdr->fh_f_ctl[0] << 16 |
18037 hdr->fh_f_ctl[1] << 8 |
18038 hdr->fh_f_ctl[2]);
18039 /* If last frame of sequence we can return success. */
18040 if (fctl & FC_FC_END_SEQ)
18041 return 1;
18042 }
18043 return 0;
18044}
18045
18046/**
18047 * lpfc_prep_seq - Prep sequence for ULP processing
18048 * @vport: Pointer to the vport on which this sequence was received
18049 * @dmabuf: pointer to a dmabuf that describes the FC sequence
18050 *
18051 * This function takes a sequence, described by a list of frames, and creates
18052 * a list of iocbq structures to describe the sequence. This iocbq list will be
18053 * used to issue to the generic unsolicited sequence handler. This routine
18054 * returns a pointer to the first iocbq in the list. If the function is unable
18055 * to allocate an iocbq then it throw out the received frames that were not
18056 * able to be described and return a pointer to the first iocbq. If unable to
18057 * allocate any iocbqs (including the first) this function will return NULL.
18058 **/
18059static struct lpfc_iocbq *
18060lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
18061{
James Smart7851fe22011-07-22 18:36:52 -040018062 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040018063 struct lpfc_dmabuf *d_buf, *n_buf;
18064 struct lpfc_iocbq *first_iocbq, *iocbq;
18065 struct fc_frame_header *fc_hdr;
18066 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040018067 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050018068 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040018069
18070 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
18071 /* remove from receive buffer list */
18072 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040018073 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040018074 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040018075 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040018076 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040018077 /* Get an iocbq struct to fill in. */
18078 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
18079 if (first_iocbq) {
18080 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040018081 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040018082 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080018083 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040018084
18085 /* Check FC Header to see what TYPE of frame we are rcv'ing */
18086 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
18087 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
18088 first_iocbq->iocb.un.rcvels.parmRo =
18089 sli4_did_from_fc_hdr(fc_hdr);
18090 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
18091 } else
18092 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040018093 first_iocbq->iocb.ulpContext = NO_XRI;
18094 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
18095 be16_to_cpu(fc_hdr->fh_ox_id);
18096 /* iocbq is prepped for internal consumption. Physical vpi. */
18097 first_iocbq->iocb.unsli3.rcvsli3.vpi =
18098 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040018099 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040018100 tot_len = bf_get(lpfc_rcqe_length,
18101 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
18102
James Smart4f774512009-05-22 14:52:35 -040018103 first_iocbq->context2 = &seq_dmabuf->dbuf;
18104 first_iocbq->context3 = NULL;
18105 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040018106 if (tot_len > LPFC_DATA_BUF_SIZE)
18107 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040018108 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040018109 else
18110 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
18111
James Smart4f774512009-05-22 14:52:35 -040018112 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040018113
James Smart7851fe22011-07-22 18:36:52 -040018114 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040018115 }
18116 iocbq = first_iocbq;
18117 /*
18118 * Each IOCBq can have two Buffers assigned, so go through the list
18119 * of buffers for this sequence and save two buffers in each IOCBq
18120 */
18121 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
18122 if (!iocbq) {
18123 lpfc_in_buf_free(vport->phba, d_buf);
18124 continue;
18125 }
18126 if (!iocbq->context3) {
18127 iocbq->context3 = d_buf;
18128 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040018129 /* We need to get the size out of the right CQE */
18130 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18131 len = bf_get(lpfc_rcqe_length,
18132 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040018133 pbde = (struct ulp_bde64 *)
18134 &iocbq->iocb.unsli3.sli3Words[4];
18135 if (len > LPFC_DATA_BUF_SIZE)
18136 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
18137 else
18138 pbde->tus.f.bdeSize = len;
18139
James Smart7851fe22011-07-22 18:36:52 -040018140 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
18141 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040018142 } else {
18143 iocbq = lpfc_sli_get_iocbq(vport->phba);
18144 if (!iocbq) {
18145 if (first_iocbq) {
18146 first_iocbq->iocb.ulpStatus =
18147 IOSTAT_FCP_RSP_ERROR;
18148 first_iocbq->iocb.un.ulpWord[4] =
18149 IOERR_NO_RESOURCES;
18150 }
18151 lpfc_in_buf_free(vport->phba, d_buf);
18152 continue;
18153 }
James Smart7851fe22011-07-22 18:36:52 -040018154 /* We need to get the size out of the right CQE */
18155 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18156 len = bf_get(lpfc_rcqe_length,
18157 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040018158 iocbq->context2 = d_buf;
18159 iocbq->context3 = NULL;
18160 iocbq->iocb.ulpBdeCount = 1;
18161 if (len > LPFC_DATA_BUF_SIZE)
18162 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
18163 LPFC_DATA_BUF_SIZE;
18164 else
18165 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
18166
James Smart7851fe22011-07-22 18:36:52 -040018167 tot_len += len;
18168 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
18169
James Smart4f774512009-05-22 14:52:35 -040018170 iocbq->iocb.un.rcvels.remoteID = sid;
18171 list_add_tail(&iocbq->list, &first_iocbq->list);
18172 }
18173 }
James Smart39c4f1a2020-01-27 16:23:01 -080018174 /* Free the sequence's header buffer */
18175 if (!first_iocbq)
18176 lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
18177
James Smart4f774512009-05-22 14:52:35 -040018178 return first_iocbq;
18179}
18180
James Smart6669f9b2009-10-02 15:16:45 -040018181static void
18182lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
18183 struct hbq_dmabuf *seq_dmabuf)
18184{
18185 struct fc_frame_header *fc_hdr;
18186 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
18187 struct lpfc_hba *phba = vport->phba;
18188
18189 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
18190 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
18191 if (!iocbq) {
18192 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18193 "2707 Ring %d handler: Failed to allocate "
18194 "iocb Rctl x%x Type x%x received\n",
18195 LPFC_ELS_RING,
18196 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18197 return;
18198 }
18199 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080018200 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040018201 iocbq, fc_hdr->fh_r_ctl,
18202 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040018203 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040018204 "2540 Ring %d handler: unexpected Rctl "
18205 "x%x Type x%x received\n",
18206 LPFC_ELS_RING,
18207 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18208
18209 /* Free iocb created in lpfc_prep_seq */
18210 list_for_each_entry_safe(curr_iocb, next_iocb,
18211 &iocbq->list, list) {
18212 list_del_init(&curr_iocb->list);
18213 lpfc_sli_release_iocbq(phba, curr_iocb);
18214 }
18215 lpfc_sli_release_iocbq(phba, iocbq);
18216}
18217
James Smartae9e28f2017-05-15 15:20:51 -070018218static void
18219lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
18220 struct lpfc_iocbq *rspiocb)
18221{
18222 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
18223
18224 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020018225 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070018226 kfree(pcmd);
18227 lpfc_sli_release_iocbq(phba, cmdiocb);
James Smarte817e5d2018-12-13 15:17:53 -080018228 lpfc_drain_txq(phba);
James Smartae9e28f2017-05-15 15:20:51 -070018229}
18230
18231static void
18232lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
18233 struct hbq_dmabuf *dmabuf)
18234{
18235 struct fc_frame_header *fc_hdr;
18236 struct lpfc_hba *phba = vport->phba;
18237 struct lpfc_iocbq *iocbq = NULL;
18238 union lpfc_wqe *wqe;
18239 struct lpfc_dmabuf *pcmd = NULL;
18240 uint32_t frame_len;
18241 int rc;
James Smarte817e5d2018-12-13 15:17:53 -080018242 unsigned long iflags;
James Smartae9e28f2017-05-15 15:20:51 -070018243
18244 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18245 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
18246
18247 /* Send the received frame back */
18248 iocbq = lpfc_sli_get_iocbq(phba);
James Smarte817e5d2018-12-13 15:17:53 -080018249 if (!iocbq) {
18250 /* Queue cq event and wakeup worker thread to process it */
18251 spin_lock_irqsave(&phba->hbalock, iflags);
18252 list_add_tail(&dmabuf->cq_event.list,
18253 &phba->sli4_hba.sp_queue_event);
18254 phba->hba_flag |= HBA_SP_QUEUE_EVT;
18255 spin_unlock_irqrestore(&phba->hbalock, iflags);
18256 lpfc_worker_wake_up(phba);
18257 return;
18258 }
James Smartae9e28f2017-05-15 15:20:51 -070018259
18260 /* Allocate buffer for command payload */
18261 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
18262 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020018263 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070018264 &pcmd->phys);
18265 if (!pcmd || !pcmd->virt)
18266 goto exit;
18267
18268 INIT_LIST_HEAD(&pcmd->list);
18269
18270 /* copyin the payload */
18271 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
18272
18273 /* fill in BDE's for command */
18274 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
18275 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
18276 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
18277 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
18278
18279 iocbq->context2 = pcmd;
18280 iocbq->vport = vport;
18281 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
18282 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
18283
18284 /*
18285 * Setup rest of the iocb as though it were a WQE
18286 * Build the SEND_FRAME WQE
18287 */
18288 wqe = (union lpfc_wqe *)&iocbq->iocb;
18289
18290 wqe->send_frame.frame_len = frame_len;
18291 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
18292 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
18293 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
18294 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
18295 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
18296 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
18297
18298 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
18299 iocbq->iocb.ulpLe = 1;
18300 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
18301 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
18302 if (rc == IOCB_ERROR)
18303 goto exit;
18304
18305 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18306 return;
18307
18308exit:
18309 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
18310 "2023 Unable to process MDS loopback frame\n");
18311 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020018312 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070018313 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070018314 if (iocbq)
18315 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070018316 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18317}
18318
James Smart4f774512009-05-22 14:52:35 -040018319/**
18320 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
18321 * @phba: Pointer to HBA context object.
18322 *
18323 * This function is called with no lock held. This function processes all
18324 * the received buffers and gives it to upper layers when a received buffer
18325 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080018326 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040018327 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
18328 * appropriate receive function when the final frame in a sequence is received.
18329 **/
James Smart4d9ab992009-10-02 15:16:39 -040018330void
18331lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
18332 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040018333{
James Smart4d9ab992009-10-02 15:16:39 -040018334 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040018335 struct fc_frame_header *fc_hdr;
18336 struct lpfc_vport *vport;
18337 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040018338 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040018339
James Smart4f774512009-05-22 14:52:35 -040018340 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040018341 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080018342
James Smarte817e5d2018-12-13 15:17:53 -080018343 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
18344 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
18345 vport = phba->pport;
18346 /* Handle MDS Loopback frames */
18347 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
18348 return;
18349 }
18350
James Smart4d9ab992009-10-02 15:16:39 -040018351 /* check to see if this a valid type of frame */
18352 if (lpfc_fc_frame_check(phba, fc_hdr)) {
18353 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18354 return;
18355 }
James Smart2ea259e2017-02-12 13:52:27 -080018356
James Smart7851fe22011-07-22 18:36:52 -040018357 if ((bf_get(lpfc_cqe_code,
18358 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
18359 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
18360 &dmabuf->cq_event.cqe.rcqe_cmpl);
18361 else
18362 fcfi = bf_get(lpfc_rcqe_fcf_id,
18363 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040018364
James Smarte62245d2019-08-14 16:57:08 -070018365 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
18366 vport = phba->pport;
18367 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18368 "2023 MDS Loopback %d bytes\n",
18369 bf_get(lpfc_rcqe_length,
18370 &dmabuf->cq_event.cqe.rcqe_cmpl));
18371 /* Handle MDS Loopback frames */
18372 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
18373 return;
18374 }
18375
James Smart895427b2017-02-12 13:52:30 -080018376 /* d_id this frame is directed to */
18377 did = sli4_did_from_fc_hdr(fc_hdr);
18378
18379 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040018380 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040018381 /* throw out the frame */
18382 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18383 return;
18384 }
James Smart939723a2012-05-09 21:19:03 -040018385
James Smart939723a2012-05-09 21:19:03 -040018386 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
18387 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
18388 (did != Fabric_DID)) {
18389 /*
18390 * Throw out the frame if we are not pt2pt.
18391 * The pt2pt protocol allows for discovery frames
18392 * to be received without a registered VPI.
18393 */
18394 if (!(vport->fc_flag & FC_PT2PT) ||
18395 (phba->link_state == LPFC_HBA_READY)) {
18396 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18397 return;
18398 }
18399 }
18400
James Smart6669f9b2009-10-02 15:16:45 -040018401 /* Handle the basic abort sequence (BA_ABTS) event */
18402 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
18403 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
18404 return;
18405 }
18406
James Smart4d9ab992009-10-02 15:16:39 -040018407 /* Link this frame */
18408 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
18409 if (!seq_dmabuf) {
18410 /* unable to add frame to vport - throw it out */
18411 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18412 return;
18413 }
18414 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050018415 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040018416 return;
James Smartdef9c7a2009-12-21 17:02:28 -050018417
James Smart6669f9b2009-10-02 15:16:45 -040018418 /* Send the complete sequence to the upper layer protocol */
18419 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040018420}
James Smart6fb120a2009-05-22 14:52:59 -040018421
18422/**
18423 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
18424 * @phba: pointer to lpfc hba data structure.
18425 *
18426 * This routine is invoked to post rpi header templates to the
18427 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018428 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18429 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018430 *
18431 * This routine does not require any locks. It's usage is expected
18432 * to be driver load or reset recovery when the driver is
18433 * sequential.
18434 *
18435 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018436 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040018437 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040018438 * When this error occurs, the driver is not guaranteed
18439 * to have any rpi regions posted to the device and
18440 * must either attempt to repost the regions or take a
18441 * fatal error.
18442 **/
18443int
18444lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
18445{
18446 struct lpfc_rpi_hdr *rpi_page;
18447 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040018448 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018449
James Smart6d368e52011-05-24 11:44:12 -040018450 /* SLI4 ports that support extents do not require RPI headers. */
18451 if (!phba->sli4_hba.rpi_hdrs_in_use)
18452 goto exit;
18453 if (phba->sli4_hba.extents_in_use)
18454 return -EIO;
18455
James Smart6fb120a2009-05-22 14:52:59 -040018456 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040018457 /*
18458 * Assign the rpi headers a physical rpi only if the driver
18459 * has not initialized those resources. A port reset only
18460 * needs the headers posted.
18461 */
18462 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
18463 LPFC_RPI_RSRC_RDY)
18464 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
18465
James Smart6fb120a2009-05-22 14:52:59 -040018466 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
18467 if (rc != MBX_SUCCESS) {
18468 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18469 "2008 Error %d posting all rpi "
18470 "headers\n", rc);
18471 rc = -EIO;
18472 break;
18473 }
18474 }
18475
James Smart6d368e52011-05-24 11:44:12 -040018476 exit:
18477 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
18478 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040018479 return rc;
18480}
18481
18482/**
18483 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
18484 * @phba: pointer to lpfc hba data structure.
18485 * @rpi_page: pointer to the rpi memory region.
18486 *
18487 * This routine is invoked to post a single rpi header to the
18488 * HBA consistent with the SLI-4 interface spec. This memory region
18489 * maps up to 64 rpi context regions.
18490 *
18491 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018492 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040018493 * -ENOMEM - No available memory
18494 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040018495 **/
18496int
18497lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
18498{
18499 LPFC_MBOXQ_t *mboxq;
18500 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
18501 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018502 uint32_t shdr_status, shdr_add_status;
18503 union lpfc_sli4_cfg_shdr *shdr;
18504
James Smart6d368e52011-05-24 11:44:12 -040018505 /* SLI4 ports that support extents do not require RPI headers. */
18506 if (!phba->sli4_hba.rpi_hdrs_in_use)
18507 return rc;
18508 if (phba->sli4_hba.extents_in_use)
18509 return -EIO;
18510
James Smart6fb120a2009-05-22 14:52:59 -040018511 /* The port is notified of the header region via a mailbox command. */
18512 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18513 if (!mboxq) {
18514 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18515 "2001 Unable to allocate memory for issuing "
18516 "SLI_CONFIG_SPECIAL mailbox command\n");
18517 return -ENOMEM;
18518 }
18519
18520 /* Post all rpi memory regions to the port. */
18521 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040018522 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18523 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18524 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050018525 sizeof(struct lpfc_sli4_cfg_mhdr),
18526 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040018527
18528
18529 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040018530 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18531 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040018532 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18533 hdr_tmpl, rpi_page->page_count);
18534
James Smart6fb120a2009-05-22 14:52:59 -040018535 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18536 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040018537 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040018538 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18539 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18540 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18541 if (rc != MBX_TIMEOUT)
18542 mempool_free(mboxq, phba->mbox_mem_pool);
18543 if (shdr_status || shdr_add_status || rc) {
18544 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18545 "2514 POST_RPI_HDR mailbox failed with "
18546 "status x%x add_status x%x, mbx status x%x\n",
18547 shdr_status, shdr_add_status, rc);
18548 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070018549 } else {
18550 /*
18551 * The next_rpi stores the next logical module-64 rpi value used
18552 * to post physical rpis in subsequent rpi postings.
18553 */
18554 spin_lock_irq(&phba->hbalock);
18555 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18556 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040018557 }
18558 return rc;
18559}
18560
18561/**
18562 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18563 * @phba: pointer to lpfc hba data structure.
18564 *
18565 * This routine is invoked to post rpi header templates to the
18566 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018567 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18568 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018569 *
18570 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018571 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040018572 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
18573 **/
18574int
18575lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18576{
James Smart6d368e52011-05-24 11:44:12 -040018577 unsigned long rpi;
18578 uint16_t max_rpi, rpi_limit;
18579 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018580 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040018581 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040018582
James Smart6fb120a2009-05-22 14:52:59 -040018583 /*
James Smart6d368e52011-05-24 11:44:12 -040018584 * Fetch the next logical rpi. Because this index is logical,
18585 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040018586 */
James Smart4902b382013-10-10 12:20:35 -040018587 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040018588 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18589 rpi_limit = phba->sli4_hba.next_rpi;
18590
James Smart6d368e52011-05-24 11:44:12 -040018591 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18592 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040018593 rpi = LPFC_RPI_ALLOC_ERROR;
18594 else {
18595 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18596 phba->sli4_hba.max_cfg_param.rpi_used++;
18597 phba->sli4_hba.rpi_count++;
18598 }
James Smart0f154222019-09-21 20:58:52 -070018599 lpfc_printf_log(phba, KERN_INFO,
18600 LOG_NODE | LOG_DISCOVERY,
18601 "0001 Allocated rpi:x%x max:x%x lim:x%x\n",
James Smartbe6bb942015-04-07 15:07:22 -040018602 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040018603
18604 /*
18605 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040018606 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040018607 */
18608 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18609 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040018610 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018611 return rpi;
18612 }
18613
18614 /*
James Smart6d368e52011-05-24 11:44:12 -040018615 * RPI header postings are not required for SLI4 ports capable of
18616 * extents.
18617 */
18618 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040018619 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040018620 return rpi;
18621 }
18622
18623 /*
James Smart6fb120a2009-05-22 14:52:59 -040018624 * If the driver is running low on rpi resources, allocate another
18625 * page now. Note that the next_rpi value is used because
18626 * it represents how many are actually in use whereas max_rpi notes
18627 * how many are supported max by the device.
18628 */
James Smart6d368e52011-05-24 11:44:12 -040018629 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040018630 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018631 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18632 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18633 if (!rpi_hdr) {
18634 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18635 "2002 Error Could not grow rpi "
18636 "count\n");
18637 } else {
James Smart6d368e52011-05-24 11:44:12 -040018638 lrpi = rpi_hdr->start_rpi;
18639 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040018640 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18641 }
18642 }
18643
18644 return rpi;
18645}
18646
18647/**
18648 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18649 * @phba: pointer to lpfc hba data structure.
18650 *
18651 * This routine is invoked to release an rpi to the pool of
18652 * available rpis maintained by the driver.
18653 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018654static void
James Smartd7c47992010-06-08 18:31:54 -040018655__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18656{
James Smart7cfd5632019-11-04 16:56:58 -080018657 /*
18658 * if the rpi value indicates a prior unreg has already
18659 * been done, skip the unreg.
18660 */
18661 if (rpi == LPFC_RPI_ALLOC_ERROR)
18662 return;
18663
James Smartd7c47992010-06-08 18:31:54 -040018664 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18665 phba->sli4_hba.rpi_count--;
18666 phba->sli4_hba.max_cfg_param.rpi_used--;
James Smartb95b2112019-08-14 16:56:47 -070018667 } else {
James Smart0f154222019-09-21 20:58:52 -070018668 lpfc_printf_log(phba, KERN_INFO,
18669 LOG_NODE | LOG_DISCOVERY,
James Smartb95b2112019-08-14 16:56:47 -070018670 "2016 rpi %x not inuse\n",
18671 rpi);
James Smartd7c47992010-06-08 18:31:54 -040018672 }
18673}
18674
18675/**
18676 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18677 * @phba: pointer to lpfc hba data structure.
18678 *
18679 * This routine is invoked to release an rpi to the pool of
18680 * available rpis maintained by the driver.
18681 **/
18682void
James Smart6fb120a2009-05-22 14:52:59 -040018683lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18684{
18685 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040018686 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040018687 spin_unlock_irq(&phba->hbalock);
18688}
18689
18690/**
18691 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18692 * @phba: pointer to lpfc hba data structure.
18693 *
18694 * This routine is invoked to remove the memory region that
18695 * provided rpi via a bitmask.
18696 **/
18697void
18698lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
18699{
18700 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040018701 kfree(phba->sli4_hba.rpi_ids);
18702 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040018703}
18704
18705/**
18706 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
18707 * @phba: pointer to lpfc hba data structure.
18708 *
18709 * This routine is invoked to remove the memory region that
18710 * provided rpi via a bitmask.
18711 **/
18712int
James Smart6b5151f2012-01-18 16:24:06 -050018713lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
18714 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040018715{
18716 LPFC_MBOXQ_t *mboxq;
18717 struct lpfc_hba *phba = ndlp->phba;
18718 int rc;
18719
18720 /* The port is notified of the header region via a mailbox command. */
18721 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18722 if (!mboxq)
18723 return -ENOMEM;
18724
18725 /* Post all rpi memory regions to the port. */
18726 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050018727 if (cmpl) {
18728 mboxq->mbox_cmpl = cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080018729 mboxq->ctx_buf = arg;
18730 mboxq->ctx_ndlp = ndlp;
James Smart72859902012-01-18 16:25:38 -050018731 } else
18732 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050018733 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040018734 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18735 if (rc == MBX_NOT_FINISHED) {
18736 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18737 "2010 Resume RPI Mailbox failed "
18738 "status %d, mbxStatus x%x\n", rc,
18739 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
18740 mempool_free(mboxq, phba->mbox_mem_pool);
18741 return -EIO;
18742 }
18743 return 0;
18744}
18745
18746/**
18747 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050018748 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040018749 *
James Smart76a95d72010-11-20 23:11:48 -050018750 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040018751 *
18752 * Returns:
18753 * 0 success
18754 * -Evalue otherwise
18755 **/
18756int
James Smart76a95d72010-11-20 23:11:48 -050018757lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040018758{
18759 LPFC_MBOXQ_t *mboxq;
18760 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040018761 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040018762 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050018763 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040018764 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18765 if (!mboxq)
18766 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050018767 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040018768 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040018769 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040018770 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050018771 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040018772 "2022 INIT VPI Mailbox failed "
18773 "status %d, mbxStatus x%x\n", rc,
18774 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040018775 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040018776 }
James Smart6a9c52c2009-10-02 15:16:51 -040018777 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050018778 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040018779
18780 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040018781}
18782
18783/**
18784 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
18785 * @phba: pointer to lpfc hba data structure.
18786 * @mboxq: Pointer to mailbox object.
18787 *
18788 * This routine is invoked to manually add a single FCF record. The caller
18789 * must pass a completely initialized FCF_Record. This routine takes
18790 * care of the nonembedded mailbox operations.
18791 **/
18792static void
18793lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
18794{
18795 void *virt_addr;
18796 union lpfc_sli4_cfg_shdr *shdr;
18797 uint32_t shdr_status, shdr_add_status;
18798
18799 virt_addr = mboxq->sge_array->addr[0];
18800 /* The IOCTL status is embedded in the mailbox subheader. */
18801 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
18802 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18803 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18804
18805 if ((shdr_status || shdr_add_status) &&
18806 (shdr_status != STATUS_FCF_IN_USE))
18807 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18808 "2558 ADD_FCF_RECORD mailbox failed with "
18809 "status x%x add_status x%x\n",
18810 shdr_status, shdr_add_status);
18811
18812 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18813}
18814
18815/**
18816 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
18817 * @phba: pointer to lpfc hba data structure.
18818 * @fcf_record: pointer to the initialized fcf record to add.
18819 *
18820 * This routine is invoked to manually add a single FCF record. The caller
18821 * must pass a completely initialized FCF_Record. This routine takes
18822 * care of the nonembedded mailbox operations.
18823 **/
18824int
18825lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
18826{
18827 int rc = 0;
18828 LPFC_MBOXQ_t *mboxq;
18829 uint8_t *bytep;
18830 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040018831 struct lpfc_mbx_sge sge;
18832 uint32_t alloc_len, req_len;
18833 uint32_t fcfindex;
18834
18835 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18836 if (!mboxq) {
18837 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18838 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
18839 return -ENOMEM;
18840 }
18841
18842 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
18843 sizeof(uint32_t);
18844
18845 /* Allocate DMA memory and set up the non-embedded mailbox command */
18846 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18847 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
18848 req_len, LPFC_SLI4_MBX_NEMBED);
18849 if (alloc_len < req_len) {
18850 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18851 "2523 Allocated DMA memory size (x%x) is "
18852 "less than the requested DMA memory "
18853 "size (x%x)\n", alloc_len, req_len);
18854 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18855 return -ENOMEM;
18856 }
18857
18858 /*
18859 * Get the first SGE entry from the non-embedded DMA memory. This
18860 * routine only uses a single SGE.
18861 */
18862 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040018863 virt_addr = mboxq->sge_array->addr[0];
18864 /*
18865 * Configure the FCF record for FCFI 0. This is the driver's
18866 * hardcoded default and gets used in nonFIP mode.
18867 */
18868 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
18869 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
18870 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
18871
18872 /*
18873 * Copy the fcf_index and the FCF Record Data. The data starts after
18874 * the FCoE header plus word10. The data copy needs to be endian
18875 * correct.
18876 */
18877 bytep += sizeof(uint32_t);
18878 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
18879 mboxq->vport = phba->pport;
18880 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
18881 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18882 if (rc == MBX_NOT_FINISHED) {
18883 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18884 "2515 ADD_FCF_RECORD mailbox failed with "
18885 "status 0x%x\n", rc);
18886 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18887 rc = -EIO;
18888 } else
18889 rc = 0;
18890
18891 return rc;
18892}
18893
18894/**
18895 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
18896 * @phba: pointer to lpfc hba data structure.
18897 * @fcf_record: pointer to the fcf record to write the default data.
18898 * @fcf_index: FCF table entry index.
18899 *
18900 * This routine is invoked to build the driver's default FCF record. The
18901 * values used are hardcoded. This routine handles memory initialization.
18902 *
18903 **/
18904void
18905lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
18906 struct fcf_record *fcf_record,
18907 uint16_t fcf_index)
18908{
18909 memset(fcf_record, 0, sizeof(struct fcf_record));
18910 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
18911 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
18912 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
18913 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
18914 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
18915 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
18916 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
18917 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
18918 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
18919 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
18920 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
18921 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
18922 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040018923 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040018924 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
18925 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
18926 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
18927 /* Set the VLAN bit map */
18928 if (phba->valid_vlan) {
18929 fcf_record->vlan_bitmap[phba->vlan_id / 8]
18930 = 1 << (phba->vlan_id % 8);
18931 }
18932}
18933
18934/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018935 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040018936 * @phba: pointer to lpfc hba data structure.
18937 * @fcf_index: FCF table entry offset.
18938 *
James Smart0c9ab6f2010-02-26 14:15:57 -050018939 * This routine is invoked to scan the entire FCF table by reading FCF
18940 * record and processing it one at a time starting from the @fcf_index
18941 * for initial FCF discovery or fast FCF failover rediscovery.
18942 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018943 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018944 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040018945 **/
18946int
James Smart0c9ab6f2010-02-26 14:15:57 -050018947lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040018948{
18949 int rc = 0, error;
18950 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040018951
James Smart32b97932009-07-19 10:01:21 -040018952 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050018953 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040018954 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18955 if (!mboxq) {
18956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18957 "2000 Failed to allocate mbox for "
18958 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040018959 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050018960 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018961 }
James Smartecfd03c2010-02-12 14:41:27 -050018962 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050018963 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050018964 if (rc) {
18965 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050018966 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040018967 }
James Smartecfd03c2010-02-12 14:41:27 -050018968 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040018969 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050018970 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040018971
18972 spin_lock_irq(&phba->hbalock);
18973 phba->hba_flag |= FCF_TS_INPROG;
18974 spin_unlock_irq(&phba->hbalock);
18975
James Smart6fb120a2009-05-22 14:52:59 -040018976 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050018977 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040018978 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050018979 else {
James Smart38b92ef2010-08-04 16:11:39 -040018980 /* Reset eligible FCF count for new scan */
18981 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040018982 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018983 error = 0;
James Smart32b97932009-07-19 10:01:21 -040018984 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018985fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040018986 if (error) {
18987 if (mboxq)
18988 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040018989 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018990 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018991 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018992 spin_unlock_irq(&phba->hbalock);
18993 }
James Smart6fb120a2009-05-22 14:52:59 -040018994 return error;
18995}
James Smarta0c87cb2009-07-19 10:01:10 -040018996
18997/**
James Smarta93ff372010-10-22 11:06:08 -040018998 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018999 * @phba: pointer to lpfc hba data structure.
19000 * @fcf_index: FCF table entry offset.
19001 *
19002 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040019003 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050019004 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030019005 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050019006 * otherwise.
19007 **/
19008int
19009lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
19010{
19011 int rc = 0, error;
19012 LPFC_MBOXQ_t *mboxq;
19013
19014 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19015 if (!mboxq) {
19016 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
19017 "2763 Failed to allocate mbox for "
19018 "READ_FCF cmd\n");
19019 error = -ENOMEM;
19020 goto fail_fcf_read;
19021 }
19022 /* Construct the read FCF record mailbox command */
19023 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
19024 if (rc) {
19025 error = -EINVAL;
19026 goto fail_fcf_read;
19027 }
19028 /* Issue the mailbox command asynchronously */
19029 mboxq->vport = phba->pport;
19030 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
19031 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19032 if (rc == MBX_NOT_FINISHED)
19033 error = -EIO;
19034 else
19035 error = 0;
19036
19037fail_fcf_read:
19038 if (error && mboxq)
19039 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19040 return error;
19041}
19042
19043/**
19044 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
19045 * @phba: pointer to lpfc hba data structure.
19046 * @fcf_index: FCF table entry offset.
19047 *
19048 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040019049 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050019050 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030019051 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050019052 * otherwise.
19053 **/
19054int
19055lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
19056{
19057 int rc = 0, error;
19058 LPFC_MBOXQ_t *mboxq;
19059
19060 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19061 if (!mboxq) {
19062 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
19063 "2758 Failed to allocate mbox for "
19064 "READ_FCF cmd\n");
19065 error = -ENOMEM;
19066 goto fail_fcf_read;
19067 }
19068 /* Construct the read FCF record mailbox command */
19069 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
19070 if (rc) {
19071 error = -EINVAL;
19072 goto fail_fcf_read;
19073 }
19074 /* Issue the mailbox command asynchronously */
19075 mboxq->vport = phba->pport;
19076 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
19077 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19078 if (rc == MBX_NOT_FINISHED)
19079 error = -EIO;
19080 else
19081 error = 0;
19082
19083fail_fcf_read:
19084 if (error && mboxq)
19085 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19086 return error;
19087}
19088
19089/**
James Smartf5cb5302015-12-16 18:11:52 -050019090 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040019091 * phba pointer to the lpfc_hba struct for this port.
19092 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
19093 * routine when the rr_bmask is empty. The FCF indecies are put into the
19094 * rr_bmask based on their priority level. Starting from the highest priority
19095 * to the lowest. The most likely FCF candidate will be in the highest
19096 * priority group. When this routine is called it searches the fcf_pri list for
19097 * next lowest priority group and repopulates the rr_bmask with only those
19098 * fcf_indexes.
19099 * returns:
19100 * 1=success 0=failure
19101 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040019102static int
James Smart7d791df2011-07-22 18:37:52 -040019103lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
19104{
19105 uint16_t next_fcf_pri;
19106 uint16_t last_index;
19107 struct lpfc_fcf_pri *fcf_pri;
19108 int rc;
19109 int ret = 0;
19110
19111 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
19112 LPFC_SLI4_FCF_TBL_INDX_MAX);
19113 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
19114 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050019115
19116 /* Verify the priority list has 2 or more entries */
19117 spin_lock_irq(&phba->hbalock);
19118 if (list_empty(&phba->fcf.fcf_pri_list) ||
19119 list_is_singular(&phba->fcf.fcf_pri_list)) {
19120 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040019121 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
19122 "3061 Last IDX %d\n", last_index);
19123 return 0; /* Empty rr list */
19124 }
James Smart25626692013-03-01 16:36:54 -050019125 spin_unlock_irq(&phba->hbalock);
19126
James Smart7d791df2011-07-22 18:37:52 -040019127 next_fcf_pri = 0;
19128 /*
19129 * Clear the rr_bmask and set all of the bits that are at this
19130 * priority.
19131 */
19132 memset(phba->fcf.fcf_rr_bmask, 0,
19133 sizeof(*phba->fcf.fcf_rr_bmask));
19134 spin_lock_irq(&phba->hbalock);
19135 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
19136 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
19137 continue;
19138 /*
19139 * the 1st priority that has not FLOGI failed
19140 * will be the highest.
19141 */
19142 if (!next_fcf_pri)
19143 next_fcf_pri = fcf_pri->fcf_rec.priority;
19144 spin_unlock_irq(&phba->hbalock);
19145 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
19146 rc = lpfc_sli4_fcf_rr_index_set(phba,
19147 fcf_pri->fcf_rec.fcf_index);
19148 if (rc)
19149 return 0;
19150 }
19151 spin_lock_irq(&phba->hbalock);
19152 }
19153 /*
19154 * if next_fcf_pri was not set above and the list is not empty then
19155 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070019156 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040019157 */
19158 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
19159 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
19160 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
19161 /*
19162 * the 1st priority that has not FLOGI failed
19163 * will be the highest.
19164 */
19165 if (!next_fcf_pri)
19166 next_fcf_pri = fcf_pri->fcf_rec.priority;
19167 spin_unlock_irq(&phba->hbalock);
19168 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
19169 rc = lpfc_sli4_fcf_rr_index_set(phba,
19170 fcf_pri->fcf_rec.fcf_index);
19171 if (rc)
19172 return 0;
19173 }
19174 spin_lock_irq(&phba->hbalock);
19175 }
19176 } else
19177 ret = 1;
19178 spin_unlock_irq(&phba->hbalock);
19179
19180 return ret;
19181}
19182/**
James Smart0c9ab6f2010-02-26 14:15:57 -050019183 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
19184 * @phba: pointer to lpfc hba data structure.
19185 *
19186 * This routine is to get the next eligible FCF record index in a round
19187 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040019188 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050019189 * shall be returned, otherwise, the next eligible FCF record's index
19190 * shall be returned.
19191 **/
19192uint16_t
19193lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
19194{
19195 uint16_t next_fcf_index;
19196
James Smart421c6622013-01-03 15:44:16 -050019197initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040019198 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050019199 next_fcf_index = phba->fcf.current_rec.fcf_indx;
19200
James Smart7d791df2011-07-22 18:37:52 -040019201next_priority:
James Smart421c6622013-01-03 15:44:16 -050019202 /* Determine the next fcf index to check */
19203 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050019204 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
19205 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040019206 next_fcf_index);
19207
James Smart0c9ab6f2010-02-26 14:15:57 -050019208 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040019209 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19210 /*
19211 * If we have wrapped then we need to clear the bits that
19212 * have been tested so that we can detect when we should
19213 * change the priority level.
19214 */
James Smart0c9ab6f2010-02-26 14:15:57 -050019215 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
19216 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040019217 }
19218
James Smart0c9ab6f2010-02-26 14:15:57 -050019219
James Smart3804dc82010-07-14 15:31:37 -040019220 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040019221 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
19222 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
19223 /*
19224 * If next fcf index is not found check if there are lower
19225 * Priority level fcf's in the fcf_priority list.
19226 * Set up the rr_bmask with all of the avaiable fcf bits
19227 * at that level and continue the selection process.
19228 */
19229 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050019230 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040019231 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
19232 "2844 No roundrobin failover FCF available\n");
James Smart036cad12018-10-23 13:41:06 -070019233
19234 return LPFC_FCOE_FCF_NEXT_NONE;
James Smart3804dc82010-07-14 15:31:37 -040019235 }
19236
James Smart7d791df2011-07-22 18:37:52 -040019237 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
19238 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050019239 LPFC_FCF_FLOGI_FAILED) {
19240 if (list_is_singular(&phba->fcf.fcf_pri_list))
19241 return LPFC_FCOE_FCF_NEXT_NONE;
19242
James Smart7d791df2011-07-22 18:37:52 -040019243 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050019244 }
James Smart7d791df2011-07-22 18:37:52 -040019245
James Smart3804dc82010-07-14 15:31:37 -040019246 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019247 "2845 Get next roundrobin failover FCF (x%x)\n",
19248 next_fcf_index);
19249
James Smart0c9ab6f2010-02-26 14:15:57 -050019250 return next_fcf_index;
19251}
19252
19253/**
19254 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
19255 * @phba: pointer to lpfc hba data structure.
19256 *
19257 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040019258 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050019259 * does not go beyond the range of the driver allocated bmask dimension
19260 * before setting the bit.
19261 *
19262 * Returns 0 if the index bit successfully set, otherwise, it returns
19263 * -EINVAL.
19264 **/
19265int
19266lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
19267{
19268 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19269 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019270 "2610 FCF (x%x) reached driver's book "
19271 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050019272 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
19273 return -EINVAL;
19274 }
19275 /* Set the eligible FCF record index bmask */
19276 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
19277
James Smart3804dc82010-07-14 15:31:37 -040019278 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019279 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040019280 "bmask\n", fcf_index);
19281
James Smart0c9ab6f2010-02-26 14:15:57 -050019282 return 0;
19283}
19284
19285/**
James Smart3804dc82010-07-14 15:31:37 -040019286 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050019287 * @phba: pointer to lpfc hba data structure.
19288 *
19289 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040019290 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050019291 * does not go beyond the range of the driver allocated bmask dimension
19292 * before clearing the bit.
19293 **/
19294void
19295lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
19296{
James Smart9a803a72013-09-06 12:17:56 -040019297 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050019298 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19299 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019300 "2762 FCF (x%x) reached driver's book "
19301 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050019302 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
19303 return;
19304 }
19305 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040019306 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040019307 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
19308 list) {
James Smart7d791df2011-07-22 18:37:52 -040019309 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
19310 list_del_init(&fcf_pri->list);
19311 break;
19312 }
19313 }
19314 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050019315 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040019316
19317 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019318 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040019319 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050019320}
19321
19322/**
James Smartecfd03c2010-02-12 14:41:27 -050019323 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
19324 * @phba: pointer to lpfc hba data structure.
19325 *
19326 * This routine is the completion routine for the rediscover FCF table mailbox
19327 * command. If the mailbox command returned failure, it will try to stop the
19328 * FCF rediscover wait timer.
19329 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040019330static void
James Smartecfd03c2010-02-12 14:41:27 -050019331lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
19332{
19333 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
19334 uint32_t shdr_status, shdr_add_status;
19335
19336 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
19337
19338 shdr_status = bf_get(lpfc_mbox_hdr_status,
19339 &redisc_fcf->header.cfg_shdr.response);
19340 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19341 &redisc_fcf->header.cfg_shdr.response);
19342 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050019343 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050019344 "2746 Requesting for FCF rediscovery failed "
19345 "status x%x add_status x%x\n",
19346 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050019347 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050019348 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050019349 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050019350 spin_unlock_irq(&phba->hbalock);
19351 /*
19352 * CVL event triggered FCF rediscover request failed,
19353 * last resort to re-try current registered FCF entry.
19354 */
19355 lpfc_retry_pport_discovery(phba);
19356 } else {
19357 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050019358 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050019359 spin_unlock_irq(&phba->hbalock);
19360 /*
19361 * DEAD FCF event triggered FCF rediscover request
19362 * failed, last resort to fail over as a link down
19363 * to FCF registration.
19364 */
19365 lpfc_sli4_fcf_dead_failthrough(phba);
19366 }
James Smart0c9ab6f2010-02-26 14:15:57 -050019367 } else {
19368 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019369 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050019370 /*
19371 * Start FCF rediscovery wait timer for pending FCF
19372 * before rescan FCF record table.
19373 */
19374 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050019375 }
James Smartecfd03c2010-02-12 14:41:27 -050019376
19377 mempool_free(mbox, phba->mbox_mem_pool);
19378}
19379
19380/**
James Smart3804dc82010-07-14 15:31:37 -040019381 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050019382 * @phba: pointer to lpfc hba data structure.
19383 *
19384 * This routine is invoked to request for rediscovery of the entire FCF table
19385 * by the port.
19386 **/
19387int
19388lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
19389{
19390 LPFC_MBOXQ_t *mbox;
19391 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
19392 int rc, length;
19393
James Smart0c9ab6f2010-02-26 14:15:57 -050019394 /* Cancel retry delay timers to all vports before FCF rediscover */
19395 lpfc_cancel_all_vport_retry_delay_timer(phba);
19396
James Smartecfd03c2010-02-12 14:41:27 -050019397 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19398 if (!mbox) {
19399 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19400 "2745 Failed to allocate mbox for "
19401 "requesting FCF rediscover.\n");
19402 return -ENOMEM;
19403 }
19404
19405 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
19406 sizeof(struct lpfc_sli4_cfg_mhdr));
19407 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
19408 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
19409 length, LPFC_SLI4_MBX_EMBED);
19410
19411 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
19412 /* Set count to 0 for invalidating the entire FCF database */
19413 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
19414
19415 /* Issue the mailbox command asynchronously */
19416 mbox->vport = phba->pport;
19417 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
19418 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
19419
19420 if (rc == MBX_NOT_FINISHED) {
19421 mempool_free(mbox, phba->mbox_mem_pool);
19422 return -EIO;
19423 }
19424 return 0;
19425}
19426
19427/**
James Smartfc2b9892010-02-26 14:15:29 -050019428 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
19429 * @phba: pointer to lpfc hba data structure.
19430 *
19431 * This function is the failover routine as a last resort to the FCF DEAD
19432 * event when driver failed to perform fast FCF failover.
19433 **/
19434void
19435lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
19436{
19437 uint32_t link_state;
19438
19439 /*
19440 * Last resort as FCF DEAD event failover will treat this as
19441 * a link down, but save the link state because we don't want
19442 * it to be changed to Link Down unless it is already down.
19443 */
19444 link_state = phba->link_state;
19445 lpfc_linkdown(phba);
19446 phba->link_state = link_state;
19447
19448 /* Unregister FCF if no devices connected to it */
19449 lpfc_unregister_unused_fcf(phba);
19450}
19451
19452/**
James Smart026abb82011-12-13 13:20:45 -050019453 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040019454 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050019455 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040019456 *
James Smart026abb82011-12-13 13:20:45 -050019457 * This function gets SLI3 port configure region 23 data through memory dump
19458 * mailbox command. When it successfully retrieves data, the size of the data
19459 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040019460 **/
James Smart026abb82011-12-13 13:20:45 -050019461static uint32_t
19462lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040019463{
19464 LPFC_MBOXQ_t *pmb = NULL;
19465 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050019466 uint32_t offset = 0;
Dick Kennedyd91e3ab2020-06-30 14:49:52 -070019467 int i, rc;
James Smarta0c87cb2009-07-19 10:01:10 -040019468
James Smart026abb82011-12-13 13:20:45 -050019469 if (!rgn23_data)
19470 return 0;
19471
James Smarta0c87cb2009-07-19 10:01:10 -040019472 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19473 if (!pmb) {
19474 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050019475 "2600 failed to allocate mailbox memory\n");
19476 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040019477 }
19478 mb = &pmb->u.mb;
19479
James Smarta0c87cb2009-07-19 10:01:10 -040019480 do {
19481 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
19482 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
19483
19484 if (rc != MBX_SUCCESS) {
19485 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050019486 "2601 failed to read config "
19487 "region 23, rc 0x%x Status 0x%x\n",
19488 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040019489 mb->un.varDmp.word_cnt = 0;
19490 }
19491 /*
19492 * dump mem may return a zero when finished or we got a
19493 * mailbox error, either way we are done.
19494 */
19495 if (mb->un.varDmp.word_cnt == 0)
19496 break;
James Smarta0c87cb2009-07-19 10:01:10 -040019497
Dick Kennedyd91e3ab2020-06-30 14:49:52 -070019498 i = mb->un.varDmp.word_cnt * sizeof(uint32_t);
19499 if (offset + i > DMP_RGN23_SIZE)
19500 i = DMP_RGN23_SIZE - offset;
James Smarta0c87cb2009-07-19 10:01:10 -040019501 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
Dick Kennedyd91e3ab2020-06-30 14:49:52 -070019502 rgn23_data + offset, i);
19503 offset += i;
19504 } while (offset < DMP_RGN23_SIZE);
James Smarta0c87cb2009-07-19 10:01:10 -040019505
James Smart026abb82011-12-13 13:20:45 -050019506 mempool_free(pmb, phba->mbox_mem_pool);
19507 return offset;
19508}
19509
19510/**
19511 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19512 * @phba: pointer to lpfc hba data structure.
19513 * @rgn23_data: pointer to configure region 23 data.
19514 *
19515 * This function gets SLI4 port configure region 23 data through memory dump
19516 * mailbox command. When it successfully retrieves data, the size of the data
19517 * will be returned, otherwise, 0 will be returned.
19518 **/
19519static uint32_t
19520lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19521{
19522 LPFC_MBOXQ_t *mboxq = NULL;
19523 struct lpfc_dmabuf *mp = NULL;
19524 struct lpfc_mqe *mqe;
19525 uint32_t data_length = 0;
19526 int rc;
19527
19528 if (!rgn23_data)
19529 return 0;
19530
19531 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19532 if (!mboxq) {
19533 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19534 "3105 failed to allocate mailbox memory\n");
19535 return 0;
19536 }
19537
19538 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19539 goto out;
19540 mqe = &mboxq->u.mqe;
James Smart3e1f0712018-11-29 16:09:29 -080019541 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smart026abb82011-12-13 13:20:45 -050019542 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19543 if (rc)
19544 goto out;
19545 data_length = mqe->un.mb_words[5];
19546 if (data_length == 0)
19547 goto out;
19548 if (data_length > DMP_RGN23_SIZE) {
19549 data_length = 0;
19550 goto out;
19551 }
19552 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19553out:
19554 mempool_free(mboxq, phba->mbox_mem_pool);
19555 if (mp) {
19556 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19557 kfree(mp);
19558 }
19559 return data_length;
19560}
19561
19562/**
19563 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19564 * @phba: pointer to lpfc hba data structure.
19565 *
19566 * This function read region 23 and parse TLV for port status to
19567 * decide if the user disaled the port. If the TLV indicates the
19568 * port is disabled, the hba_flag is set accordingly.
19569 **/
19570void
19571lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19572{
19573 uint8_t *rgn23_data = NULL;
19574 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19575 uint32_t offset = 0;
19576
19577 /* Get adapter Region 23 data */
19578 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19579 if (!rgn23_data)
19580 goto out;
19581
19582 if (phba->sli_rev < LPFC_SLI_REV4)
19583 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19584 else {
19585 if_type = bf_get(lpfc_sli_intf_if_type,
19586 &phba->sli4_hba.sli_intf);
19587 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19588 goto out;
19589 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19590 }
James Smarta0c87cb2009-07-19 10:01:10 -040019591
19592 if (!data_size)
19593 goto out;
19594
19595 /* Check the region signature first */
19596 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
19597 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19598 "2619 Config region 23 has bad signature\n");
19599 goto out;
19600 }
19601 offset += 4;
19602
19603 /* Check the data structure version */
19604 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
19605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19606 "2620 Config region 23 has bad version\n");
19607 goto out;
19608 }
19609 offset += 4;
19610
19611 /* Parse TLV entries in the region */
19612 while (offset < data_size) {
19613 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19614 break;
19615 /*
19616 * If the TLV is not driver specific TLV or driver id is
19617 * not linux driver id, skip the record.
19618 */
19619 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19620 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19621 (rgn23_data[offset + 3] != 0)) {
19622 offset += rgn23_data[offset + 1] * 4 + 4;
19623 continue;
19624 }
19625
19626 /* Driver found a driver specific TLV in the config region */
19627 sub_tlv_len = rgn23_data[offset + 1] * 4;
19628 offset += 4;
19629 tlv_offset = 0;
19630
19631 /*
19632 * Search for configured port state sub-TLV.
19633 */
19634 while ((offset < data_size) &&
19635 (tlv_offset < sub_tlv_len)) {
19636 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19637 offset += 4;
19638 tlv_offset += 4;
19639 break;
19640 }
19641 if (rgn23_data[offset] != PORT_STE_TYPE) {
19642 offset += rgn23_data[offset + 1] * 4 + 4;
19643 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19644 continue;
19645 }
19646
19647 /* This HBA contains PORT_STE configured */
19648 if (!rgn23_data[offset + 2])
19649 phba->hba_flag |= LINK_DISABLED;
19650
19651 goto out;
19652 }
19653 }
James Smart026abb82011-12-13 13:20:45 -050019654
James Smarta0c87cb2009-07-19 10:01:10 -040019655out:
James Smarta0c87cb2009-07-19 10:01:10 -040019656 kfree(rgn23_data);
19657 return;
19658}
James Smart695a8142010-01-26 23:08:03 -050019659
19660/**
James Smart52d52442011-05-24 11:42:45 -040019661 * lpfc_wr_object - write an object to the firmware
19662 * @phba: HBA structure that indicates port to create a queue on.
19663 * @dmabuf_list: list of dmabufs to write to the port.
19664 * @size: the total byte value of the objects to write to the port.
19665 * @offset: the current offset to be used to start the transfer.
19666 *
19667 * This routine will create a wr_object mailbox command to send to the port.
19668 * the mailbox command will be constructed using the dma buffers described in
19669 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19670 * BDEs that the imbedded mailbox can support. The @offset variable will be
19671 * used to indicate the starting offset of the transfer and will also return
19672 * the offset after the write object mailbox has completed. @size is used to
19673 * determine the end of the object and whether the eof bit should be set.
19674 *
19675 * Return 0 is successful and offset will contain the the new offset to use
19676 * for the next write.
19677 * Return negative value for error cases.
19678 **/
19679int
19680lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19681 uint32_t size, uint32_t *offset)
19682{
19683 struct lpfc_mbx_wr_object *wr_object;
19684 LPFC_MBOXQ_t *mbox;
19685 int rc = 0, i = 0;
James Smartf3d0a8a2019-12-18 15:58:01 -080019686 uint32_t shdr_status, shdr_add_status, shdr_change_status, shdr_csf;
James Smart52d52442011-05-24 11:42:45 -040019687 uint32_t mbox_tmo;
James Smart52d52442011-05-24 11:42:45 -040019688 struct lpfc_dmabuf *dmabuf;
19689 uint32_t written = 0;
James Smart50212672018-12-13 15:17:57 -080019690 bool check_change_status = false;
James Smart52d52442011-05-24 11:42:45 -040019691
19692 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19693 if (!mbox)
19694 return -ENOMEM;
19695
19696 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
19697 LPFC_MBOX_OPCODE_WRITE_OBJECT,
19698 sizeof(struct lpfc_mbx_wr_object) -
19699 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
19700
19701 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
19702 wr_object->u.request.write_offset = *offset;
19703 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
19704 wr_object->u.request.object_name[0] =
19705 cpu_to_le32(wr_object->u.request.object_name[0]);
19706 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
19707 list_for_each_entry(dmabuf, dmabuf_list, list) {
19708 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
19709 break;
19710 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
19711 wr_object->u.request.bde[i].addrHigh =
19712 putPaddrHigh(dmabuf->phys);
19713 if (written + SLI4_PAGE_SIZE >= size) {
19714 wr_object->u.request.bde[i].tus.f.bdeSize =
19715 (size - written);
19716 written += (size - written);
19717 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
James Smart50212672018-12-13 15:17:57 -080019718 bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
19719 check_change_status = true;
James Smart52d52442011-05-24 11:42:45 -040019720 } else {
19721 wr_object->u.request.bde[i].tus.f.bdeSize =
19722 SLI4_PAGE_SIZE;
19723 written += SLI4_PAGE_SIZE;
19724 }
19725 i++;
19726 }
19727 wr_object->u.request.bde_count = i;
19728 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
19729 if (!phba->sli4_hba.intr_enable)
19730 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
19731 else {
James Smarta183a152011-10-10 21:32:43 -040019732 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040019733 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
19734 }
19735 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart50212672018-12-13 15:17:57 -080019736 shdr_status = bf_get(lpfc_mbox_hdr_status,
19737 &wr_object->header.cfg_shdr.response);
19738 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19739 &wr_object->header.cfg_shdr.response);
19740 if (check_change_status) {
19741 shdr_change_status = bf_get(lpfc_wr_object_change_status,
19742 &wr_object->u.response);
James Smartf3d0a8a2019-12-18 15:58:01 -080019743
19744 if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
19745 shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
19746 shdr_csf = bf_get(lpfc_wr_object_csf,
19747 &wr_object->u.response);
19748 if (shdr_csf)
19749 shdr_change_status =
19750 LPFC_CHANGE_STATUS_PCI_RESET;
19751 }
19752
James Smart50212672018-12-13 15:17:57 -080019753 switch (shdr_change_status) {
19754 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
19755 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19756 "3198 Firmware write complete: System "
19757 "reboot required to instantiate\n");
19758 break;
19759 case (LPFC_CHANGE_STATUS_FW_RESET):
19760 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19761 "3199 Firmware write complete: Firmware"
19762 " reset required to instantiate\n");
19763 break;
19764 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
19765 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19766 "3200 Firmware write complete: Port "
19767 "Migration or PCI Reset required to "
19768 "instantiate\n");
19769 break;
19770 case (LPFC_CHANGE_STATUS_PCI_RESET):
19771 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
19772 "3201 Firmware write complete: PCI "
19773 "Reset required to instantiate\n");
19774 break;
19775 default:
19776 break;
19777 }
19778 }
James Smart52d52442011-05-24 11:42:45 -040019779 if (rc != MBX_TIMEOUT)
19780 mempool_free(mbox, phba->mbox_mem_pool);
19781 if (shdr_status || shdr_add_status || rc) {
19782 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
19783 "3025 Write Object mailbox failed with "
19784 "status x%x add_status x%x, mbx status x%x\n",
19785 shdr_status, shdr_add_status, rc);
19786 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080019787 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040019788 } else
19789 *offset += wr_object->u.response.actual_write_length;
19790 return rc;
19791}
19792
19793/**
James Smart695a8142010-01-26 23:08:03 -050019794 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
19795 * @vport: pointer to vport data structure.
19796 *
19797 * This function iterate through the mailboxq and clean up all REG_LOGIN
19798 * and REG_VPI mailbox commands associated with the vport. This function
19799 * is called when driver want to restart discovery of the vport due to
19800 * a Clear Virtual Link event.
19801 **/
19802void
19803lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
19804{
19805 struct lpfc_hba *phba = vport->phba;
19806 LPFC_MBOXQ_t *mb, *nextmb;
19807 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040019808 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040019809 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040019810 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040019811 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050019812 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050019813
James Smartd439d282010-09-29 11:18:45 -040019814 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050019815 spin_lock_irq(&phba->hbalock);
19816 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
19817 if (mb->vport != vport)
19818 continue;
19819
19820 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19821 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19822 continue;
19823
James Smartd439d282010-09-29 11:18:45 -040019824 list_del(&mb->list);
19825 list_add_tail(&mb->list, &mbox_cmd_list);
19826 }
19827 /* Clean up active mailbox command with the vport */
19828 mb = phba->sli.mbox_active;
19829 if (mb && (mb->vport == vport)) {
19830 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
19831 (mb->u.mb.mbxCommand == MBX_REG_VPI))
19832 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19833 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019834 act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -040019835 /* Put reference count for delayed processing */
19836 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
19837 /* Unregister the RPI when mailbox complete */
19838 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19839 }
19840 }
James Smart63e801c2010-11-20 23:14:19 -050019841 /* Cleanup any mailbox completions which are not yet processed */
19842 do {
19843 restart_loop = 0;
19844 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
19845 /*
19846 * If this mailox is already processed or it is
19847 * for another vport ignore it.
19848 */
19849 if ((mb->vport != vport) ||
19850 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
19851 continue;
19852
19853 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
19854 (mb->u.mb.mbxCommand != MBX_REG_VPI))
19855 continue;
19856
19857 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
19858 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019859 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smart63e801c2010-11-20 23:14:19 -050019860 /* Unregister the RPI when mailbox complete */
19861 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
19862 restart_loop = 1;
19863 spin_unlock_irq(&phba->hbalock);
19864 spin_lock(shost->host_lock);
19865 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19866 spin_unlock(shost->host_lock);
19867 spin_lock_irq(&phba->hbalock);
19868 break;
19869 }
19870 }
19871 } while (restart_loop);
19872
James Smartd439d282010-09-29 11:18:45 -040019873 spin_unlock_irq(&phba->hbalock);
19874
19875 /* Release the cleaned-up mailbox commands */
19876 while (!list_empty(&mbox_cmd_list)) {
19877 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050019878 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080019879 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
James Smart695a8142010-01-26 23:08:03 -050019880 if (mp) {
19881 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
19882 kfree(mp);
19883 }
James Smart3e1f0712018-11-29 16:09:29 -080019884 mb->ctx_buf = NULL;
19885 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
19886 mb->ctx_ndlp = NULL;
James Smart78730cf2010-04-06 15:06:30 -040019887 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019888 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040019889 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020019890 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040019891 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040019892 }
James Smart695a8142010-01-26 23:08:03 -050019893 }
James Smart695a8142010-01-26 23:08:03 -050019894 mempool_free(mb, phba->mbox_mem_pool);
19895 }
James Smartd439d282010-09-29 11:18:45 -040019896
19897 /* Release the ndlp with the cleaned-up active mailbox command */
19898 if (act_mbx_ndlp) {
19899 spin_lock(shost->host_lock);
19900 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
19901 spin_unlock(shost->host_lock);
19902 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050019903 }
James Smart695a8142010-01-26 23:08:03 -050019904}
19905
James Smart2a9bf3d2010-06-07 15:24:45 -040019906/**
19907 * lpfc_drain_txq - Drain the txq
19908 * @phba: Pointer to HBA context object.
19909 *
19910 * This function attempt to submit IOCBs on the txq
19911 * to the adapter. For SLI4 adapters, the txq contains
19912 * ELS IOCBs that have been deferred because the there
19913 * are no SGLs. This congestion can occur with large
19914 * vport counts during node discovery.
19915 **/
19916
19917uint32_t
19918lpfc_drain_txq(struct lpfc_hba *phba)
19919{
19920 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080019921 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090019922 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040019923 unsigned long iflags = 0;
19924 char *fail_msg = NULL;
19925 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080019926 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040019927 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070019928 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040019929
James Smartdc19e3b2018-05-24 21:08:57 -070019930 if (phba->link_flag & LS_MDS_LOOPBACK) {
19931 /* MDS WQE are posted only to first WQ*/
James Smartc00f62e2019-08-14 16:57:11 -070019932 wq = phba->sli4_hba.hdwq[0].io_wq;
James Smartdc19e3b2018-05-24 21:08:57 -070019933 if (unlikely(!wq))
19934 return 0;
19935 pring = wq->pring;
19936 } else {
19937 wq = phba->sli4_hba.els_wq;
19938 if (unlikely(!wq))
19939 return 0;
19940 pring = lpfc_phba_elsring(phba);
19941 }
19942
19943 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070019944 return 0;
James Smart895427b2017-02-12 13:52:30 -080019945
James Smart398d81c2013-05-31 17:04:19 -040019946 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050019947 list_for_each_entry(piocbq, &pring->txq, list) {
19948 txq_cnt++;
19949 }
19950
19951 if (txq_cnt > pring->txq_max)
19952 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040019953
James Smart398d81c2013-05-31 17:04:19 -040019954 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019955
James Smart0e9bb8d2013-03-01 16:35:12 -050019956 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040019957 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019958
James Smart19ca7602010-11-20 23:11:55 -050019959 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040019960 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040019961 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040019962 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19963 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050019964 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040019965 break;
19966 }
James Smart895427b2017-02-12 13:52:30 -080019967 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040019968 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050019969 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040019970 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019971 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040019972 }
James Smart0e9bb8d2013-03-01 16:35:12 -050019973 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040019974
19975 /* The xri and iocb resources secured,
19976 * attempt to issue request
19977 */
James Smart6d368e52011-05-24 11:44:12 -040019978 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040019979 piocbq->sli4_xritag = sglq->sli4_xritag;
19980 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
19981 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080019982 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019983 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070019984 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040019985 fail_msg = " - Wq is full";
19986 else
19987 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
19988
19989 if (fail_msg) {
19990 /* Failed means we can't issue and need to cancel */
19991 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
19992 "2822 IOCB failed %s iotag 0x%x "
19993 "xri 0x%x\n",
19994 fail_msg,
19995 piocbq->iotag, piocbq->sli4_xritag);
19996 list_add_tail(&piocbq->list, &completions);
19997 }
James Smart398d81c2013-05-31 17:04:19 -040019998 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040019999 }
20000
James Smart2a9bf3d2010-06-07 15:24:45 -040020001 /* Cancel all the IOCBs that cannot be issued */
20002 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
20003 IOERR_SLI_ABORTED);
20004
James Smart0e9bb8d2013-03-01 16:35:12 -050020005 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040020006}
James Smart895427b2017-02-12 13:52:30 -080020007
20008/**
20009 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
20010 * @phba: Pointer to HBA context object.
20011 * @pwqe: Pointer to command WQE.
20012 * @sglq: Pointer to the scatter gather queue object.
20013 *
20014 * This routine converts the bpl or bde that is in the WQE
20015 * to a sgl list for the sli4 hardware. The physical address
20016 * of the bpl/bde is converted back to a virtual address.
20017 * If the WQE contains a BPL then the list of BDE's is
20018 * converted to sli4_sge's. If the WQE contains a single
20019 * BDE then it is converted to a single sli_sge.
20020 * The WQE is still in cpu endianness so the contents of
20021 * the bpl can be used without byte swapping.
20022 *
20023 * Returns valid XRI = Success, NO_XRI = Failure.
20024 */
20025static uint16_t
20026lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
20027 struct lpfc_sglq *sglq)
20028{
20029 uint16_t xritag = NO_XRI;
20030 struct ulp_bde64 *bpl = NULL;
20031 struct ulp_bde64 bde;
20032 struct sli4_sge *sgl = NULL;
20033 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080020034 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080020035 int numBdes = 0;
20036 int i = 0;
20037 uint32_t offset = 0; /* accumulated offset in the sg request list */
20038 int inbound = 0; /* number of sg reply entries inbound from firmware */
20039 uint32_t cmd;
20040
20041 if (!pwqeq || !sglq)
20042 return xritag;
20043
20044 sgl = (struct sli4_sge *)sglq->sgl;
20045 wqe = &pwqeq->wqe;
20046 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
20047
20048 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
20049 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
20050 return sglq->sli4_xritag;
20051 numBdes = pwqeq->rsvd2;
20052 if (numBdes) {
20053 /* The addrHigh and addrLow fields within the WQE
20054 * have not been byteswapped yet so there is no
20055 * need to swap them back.
20056 */
20057 if (pwqeq->context3)
20058 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
20059 else
20060 return xritag;
20061
20062 bpl = (struct ulp_bde64 *)dmabuf->virt;
20063 if (!bpl)
20064 return xritag;
20065
20066 for (i = 0; i < numBdes; i++) {
20067 /* Should already be byte swapped. */
20068 sgl->addr_hi = bpl->addrHigh;
20069 sgl->addr_lo = bpl->addrLow;
20070
20071 sgl->word2 = le32_to_cpu(sgl->word2);
20072 if ((i+1) == numBdes)
20073 bf_set(lpfc_sli4_sge_last, sgl, 1);
20074 else
20075 bf_set(lpfc_sli4_sge_last, sgl, 0);
20076 /* swap the size field back to the cpu so we
20077 * can assign it to the sgl.
20078 */
20079 bde.tus.w = le32_to_cpu(bpl->tus.w);
20080 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
20081 /* The offsets in the sgl need to be accumulated
20082 * separately for the request and reply lists.
20083 * The request is always first, the reply follows.
20084 */
20085 switch (cmd) {
20086 case CMD_GEN_REQUEST64_WQE:
20087 /* add up the reply sg entries */
20088 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
20089 inbound++;
20090 /* first inbound? reset the offset */
20091 if (inbound == 1)
20092 offset = 0;
20093 bf_set(lpfc_sli4_sge_offset, sgl, offset);
20094 bf_set(lpfc_sli4_sge_type, sgl,
20095 LPFC_SGE_TYPE_DATA);
20096 offset += bde.tus.f.bdeSize;
20097 break;
20098 case CMD_FCP_TRSP64_WQE:
20099 bf_set(lpfc_sli4_sge_offset, sgl, 0);
20100 bf_set(lpfc_sli4_sge_type, sgl,
20101 LPFC_SGE_TYPE_DATA);
20102 break;
20103 case CMD_FCP_TSEND64_WQE:
20104 case CMD_FCP_TRECEIVE64_WQE:
20105 bf_set(lpfc_sli4_sge_type, sgl,
20106 bpl->tus.f.bdeFlags);
20107 if (i < 3)
20108 offset = 0;
20109 else
20110 offset += bde.tus.f.bdeSize;
20111 bf_set(lpfc_sli4_sge_offset, sgl, offset);
20112 break;
20113 }
20114 sgl->word2 = cpu_to_le32(sgl->word2);
20115 bpl++;
20116 sgl++;
20117 }
20118 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
20119 /* The addrHigh and addrLow fields of the BDE have not
20120 * been byteswapped yet so they need to be swapped
20121 * before putting them in the sgl.
20122 */
20123 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
20124 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
20125 sgl->word2 = le32_to_cpu(sgl->word2);
20126 bf_set(lpfc_sli4_sge_last, sgl, 1);
20127 sgl->word2 = cpu_to_le32(sgl->word2);
20128 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
20129 }
20130 return sglq->sli4_xritag;
20131}
20132
20133/**
20134 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
20135 * @phba: Pointer to HBA context object.
20136 * @ring_number: Base sli ring number
20137 * @pwqe: Pointer to command WQE.
20138 **/
20139int
James Smart1fbf9742019-01-28 11:14:26 -080020140lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
James Smart895427b2017-02-12 13:52:30 -080020141 struct lpfc_iocbq *pwqe)
20142{
James Smart205e8242018-03-05 12:04:03 -080020143 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smart7cacae22020-03-31 09:50:03 -070020144 struct lpfc_async_xchg_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080020145 struct lpfc_queue *wq;
20146 struct lpfc_sglq *sglq;
20147 struct lpfc_sli_ring *pring;
20148 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070020149 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080020150
20151 /* NVME_LS and NVME_LS ABTS requests. */
20152 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
20153 pring = phba->sli4_hba.nvmels_wq->pring;
James Smart6a828b02019-01-28 11:14:31 -080020154 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20155 qp, wq_access);
James Smart895427b2017-02-12 13:52:30 -080020156 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
20157 if (!sglq) {
20158 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20159 return WQE_BUSY;
20160 }
20161 pwqe->sli4_lxritag = sglq->sli4_lxritag;
20162 pwqe->sli4_xritag = sglq->sli4_xritag;
20163 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
20164 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20165 return WQE_ERROR;
20166 }
20167 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
20168 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070020169 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
20170 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080020171 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070020172 return ret;
James Smart895427b2017-02-12 13:52:30 -080020173 }
Dick Kennedycd22d602017-08-23 16:55:35 -070020174
James Smart895427b2017-02-12 13:52:30 -080020175 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20176 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080020177
20178 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smart895427b2017-02-12 13:52:30 -080020179 return 0;
20180 }
20181
20182 /* NVME_FCREQ and NVME_ABTS requests */
20183 if (pwqe->iocb_flag & LPFC_IO_NVME) {
20184 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070020185 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080020186 pring = wq->pring;
James Smart895427b2017-02-12 13:52:30 -080020187
James Smartc00f62e2019-08-14 16:57:11 -070020188 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart895427b2017-02-12 13:52:30 -080020189
James Smart6a828b02019-01-28 11:14:31 -080020190 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20191 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070020192 ret = lpfc_sli4_wq_put(wq, wqe);
20193 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080020194 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070020195 return ret;
James Smart895427b2017-02-12 13:52:30 -080020196 }
20197 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20198 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080020199
20200 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smart895427b2017-02-12 13:52:30 -080020201 return 0;
20202 }
20203
James Smartf358dd02017-02-12 13:52:34 -080020204 /* NVMET requests */
20205 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
20206 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070020207 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080020208 pring = wq->pring;
James Smartf358dd02017-02-12 13:52:34 -080020209
James Smartf358dd02017-02-12 13:52:34 -080020210 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070020211 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080020212 if (pwqe->sli4_xritag == NO_XRI) {
20213 pwqe->sli4_lxritag = sglq->sli4_lxritag;
20214 pwqe->sli4_xritag = sglq->sli4_xritag;
20215 }
20216 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
20217 pwqe->sli4_xritag);
James Smartc00f62e2019-08-14 16:57:11 -070020218 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart1fbf9742019-01-28 11:14:26 -080020219
James Smart6a828b02019-01-28 11:14:31 -080020220 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20221 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070020222 ret = lpfc_sli4_wq_put(wq, wqe);
20223 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080020224 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070020225 return ret;
James Smartf358dd02017-02-12 13:52:34 -080020226 }
20227 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20228 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080020229
20230 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smartf358dd02017-02-12 13:52:34 -080020231 return 0;
20232 }
James Smart895427b2017-02-12 13:52:30 -080020233 return WQE_ERROR;
20234}
James Smartc4908502019-01-28 11:14:28 -080020235
20236#ifdef LPFC_MXP_STAT
20237/**
20238 * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
20239 * @phba: pointer to lpfc hba data structure.
20240 * @hwqid: belong to which HWQ.
20241 *
20242 * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
20243 * 15 seconds after a test case is running.
20244 *
20245 * The user should call lpfc_debugfs_multixripools_write before running a test
20246 * case to clear stat_snapshot_taken. Then the user starts a test case. During
20247 * test case is running, stat_snapshot_taken is incremented by 1 every time when
20248 * this routine is called from heartbeat timer. When stat_snapshot_taken is
20249 * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
20250 **/
20251void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
20252{
20253 struct lpfc_sli4_hdw_queue *qp;
20254 struct lpfc_multixri_pool *multixri_pool;
20255 struct lpfc_pvt_pool *pvt_pool;
20256 struct lpfc_pbl_pool *pbl_pool;
20257 u32 txcmplq_cnt;
20258
20259 qp = &phba->sli4_hba.hdwq[hwqid];
20260 multixri_pool = qp->p_multixri_pool;
20261 if (!multixri_pool)
20262 return;
20263
20264 if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
20265 pvt_pool = &qp->p_multixri_pool->pvt_pool;
20266 pbl_pool = &qp->p_multixri_pool->pbl_pool;
James Smartc00f62e2019-08-14 16:57:11 -070020267 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080020268
20269 multixri_pool->stat_pbl_count = pbl_pool->count;
20270 multixri_pool->stat_pvt_count = pvt_pool->count;
20271 multixri_pool->stat_busy_count = txcmplq_cnt;
20272 }
20273
20274 multixri_pool->stat_snapshot_taken++;
20275}
20276#endif
20277
20278/**
20279 * lpfc_adjust_pvt_pool_count - Adjust private pool count
20280 * @phba: pointer to lpfc hba data structure.
20281 * @hwqid: belong to which HWQ.
20282 *
20283 * This routine moves some XRIs from private to public pool when private pool
20284 * is not busy.
20285 **/
20286void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
20287{
20288 struct lpfc_multixri_pool *multixri_pool;
20289 u32 io_req_count;
20290 u32 prev_io_req_count;
20291
20292 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20293 if (!multixri_pool)
20294 return;
20295 io_req_count = multixri_pool->io_req_count;
20296 prev_io_req_count = multixri_pool->prev_io_req_count;
20297
20298 if (prev_io_req_count != io_req_count) {
20299 /* Private pool is busy */
20300 multixri_pool->prev_io_req_count = io_req_count;
20301 } else {
20302 /* Private pool is not busy.
20303 * Move XRIs from private to public pool.
20304 */
20305 lpfc_move_xri_pvt_to_pbl(phba, hwqid);
20306 }
20307}
20308
20309/**
20310 * lpfc_adjust_high_watermark - Adjust high watermark
20311 * @phba: pointer to lpfc hba data structure.
20312 * @hwqid: belong to which HWQ.
20313 *
20314 * This routine sets high watermark as number of outstanding XRIs,
20315 * but make sure the new value is between xri_limit/2 and xri_limit.
20316 **/
20317void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
20318{
20319 u32 new_watermark;
20320 u32 watermark_max;
20321 u32 watermark_min;
20322 u32 xri_limit;
20323 u32 txcmplq_cnt;
20324 u32 abts_io_bufs;
20325 struct lpfc_multixri_pool *multixri_pool;
20326 struct lpfc_sli4_hdw_queue *qp;
20327
20328 qp = &phba->sli4_hba.hdwq[hwqid];
20329 multixri_pool = qp->p_multixri_pool;
20330 if (!multixri_pool)
20331 return;
20332 xri_limit = multixri_pool->xri_limit;
20333
20334 watermark_max = xri_limit;
20335 watermark_min = xri_limit / 2;
20336
James Smartc00f62e2019-08-14 16:57:11 -070020337 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080020338 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070020339 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080020340
20341 new_watermark = txcmplq_cnt + abts_io_bufs;
20342 new_watermark = min(watermark_max, new_watermark);
20343 new_watermark = max(watermark_min, new_watermark);
20344 multixri_pool->pvt_pool.high_watermark = new_watermark;
20345
20346#ifdef LPFC_MXP_STAT
20347 multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
20348 new_watermark);
20349#endif
20350}
20351
20352/**
20353 * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
20354 * @phba: pointer to lpfc hba data structure.
20355 * @hwqid: belong to which HWQ.
20356 *
20357 * This routine is called from hearbeat timer when pvt_pool is idle.
20358 * All free XRIs are moved from private to public pool on hwqid with 2 steps.
20359 * The first step moves (all - low_watermark) amount of XRIs.
20360 * The second step moves the rest of XRIs.
20361 **/
20362void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
20363{
20364 struct lpfc_pbl_pool *pbl_pool;
20365 struct lpfc_pvt_pool *pvt_pool;
James Smart6a828b02019-01-28 11:14:31 -080020366 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080020367 struct lpfc_io_buf *lpfc_ncmd;
20368 struct lpfc_io_buf *lpfc_ncmd_next;
20369 unsigned long iflag;
20370 struct list_head tmp_list;
20371 u32 tmp_count;
20372
James Smart6a828b02019-01-28 11:14:31 -080020373 qp = &phba->sli4_hba.hdwq[hwqid];
20374 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20375 pvt_pool = &qp->p_multixri_pool->pvt_pool;
James Smartc4908502019-01-28 11:14:28 -080020376 tmp_count = 0;
20377
James Smart6a828b02019-01-28 11:14:31 -080020378 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
20379 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020380
20381 if (pvt_pool->count > pvt_pool->low_watermark) {
20382 /* Step 1: move (all - low_watermark) from pvt_pool
20383 * to pbl_pool
20384 */
20385
20386 /* Move low watermark of bufs from pvt_pool to tmp_list */
20387 INIT_LIST_HEAD(&tmp_list);
20388 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20389 &pvt_pool->list, list) {
20390 list_move_tail(&lpfc_ncmd->list, &tmp_list);
20391 tmp_count++;
20392 if (tmp_count >= pvt_pool->low_watermark)
20393 break;
20394 }
20395
20396 /* Move all bufs from pvt_pool to pbl_pool */
20397 list_splice_init(&pvt_pool->list, &pbl_pool->list);
20398
20399 /* Move all bufs from tmp_list to pvt_pool */
20400 list_splice(&tmp_list, &pvt_pool->list);
20401
20402 pbl_pool->count += (pvt_pool->count - tmp_count);
20403 pvt_pool->count = tmp_count;
20404 } else {
20405 /* Step 2: move the rest from pvt_pool to pbl_pool */
20406 list_splice_init(&pvt_pool->list, &pbl_pool->list);
20407 pbl_pool->count += pvt_pool->count;
20408 pvt_pool->count = 0;
20409 }
20410
20411 spin_unlock(&pvt_pool->lock);
20412 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20413}
20414
20415/**
20416 * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
20417 * @phba: pointer to lpfc hba data structure
20418 * @pbl_pool: specified public free XRI pool
20419 * @pvt_pool: specified private free XRI pool
20420 * @count: number of XRIs to move
20421 *
20422 * This routine tries to move some free common bufs from the specified pbl_pool
20423 * to the specified pvt_pool. It might move less than count XRIs if there's not
20424 * enough in public pool.
20425 *
20426 * Return:
20427 * true - if XRIs are successfully moved from the specified pbl_pool to the
20428 * specified pvt_pool
20429 * false - if the specified pbl_pool is empty or locked by someone else
20430 **/
20431static bool
James Smart6a828b02019-01-28 11:14:31 -080020432_lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
20433 struct lpfc_pbl_pool *pbl_pool,
James Smartc4908502019-01-28 11:14:28 -080020434 struct lpfc_pvt_pool *pvt_pool, u32 count)
20435{
20436 struct lpfc_io_buf *lpfc_ncmd;
20437 struct lpfc_io_buf *lpfc_ncmd_next;
20438 unsigned long iflag;
20439 int ret;
20440
20441 ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
20442 if (ret) {
20443 if (pbl_pool->count) {
20444 /* Move a batch of XRIs from public to private pool */
James Smart6a828b02019-01-28 11:14:31 -080020445 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020446 list_for_each_entry_safe(lpfc_ncmd,
20447 lpfc_ncmd_next,
20448 &pbl_pool->list,
20449 list) {
20450 list_move_tail(&lpfc_ncmd->list,
20451 &pvt_pool->list);
20452 pvt_pool->count++;
20453 pbl_pool->count--;
20454 count--;
20455 if (count == 0)
20456 break;
20457 }
20458
20459 spin_unlock(&pvt_pool->lock);
20460 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20461 return true;
20462 }
20463 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20464 }
20465
20466 return false;
20467}
20468
20469/**
20470 * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
20471 * @phba: pointer to lpfc hba data structure.
20472 * @hwqid: belong to which HWQ.
20473 * @count: number of XRIs to move
20474 *
20475 * This routine tries to find some free common bufs in one of public pools with
20476 * Round Robin method. The search always starts from local hwqid, then the next
20477 * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
20478 * a batch of free common bufs are moved to private pool on hwqid.
20479 * It might move less than count XRIs if there's not enough in public pool.
20480 **/
20481void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
20482{
20483 struct lpfc_multixri_pool *multixri_pool;
20484 struct lpfc_multixri_pool *next_multixri_pool;
20485 struct lpfc_pvt_pool *pvt_pool;
20486 struct lpfc_pbl_pool *pbl_pool;
James Smart6a828b02019-01-28 11:14:31 -080020487 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080020488 u32 next_hwqid;
20489 u32 hwq_count;
20490 int ret;
20491
James Smart6a828b02019-01-28 11:14:31 -080020492 qp = &phba->sli4_hba.hdwq[hwqid];
20493 multixri_pool = qp->p_multixri_pool;
James Smartc4908502019-01-28 11:14:28 -080020494 pvt_pool = &multixri_pool->pvt_pool;
20495 pbl_pool = &multixri_pool->pbl_pool;
20496
20497 /* Check if local pbl_pool is available */
James Smart6a828b02019-01-28 11:14:31 -080020498 ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020499 if (ret) {
20500#ifdef LPFC_MXP_STAT
20501 multixri_pool->local_pbl_hit_count++;
20502#endif
20503 return;
20504 }
20505
20506 hwq_count = phba->cfg_hdw_queue;
20507
20508 /* Get the next hwqid which was found last time */
20509 next_hwqid = multixri_pool->rrb_next_hwqid;
20510
20511 do {
20512 /* Go to next hwq */
20513 next_hwqid = (next_hwqid + 1) % hwq_count;
20514
20515 next_multixri_pool =
20516 phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
20517 pbl_pool = &next_multixri_pool->pbl_pool;
20518
20519 /* Check if the public free xri pool is available */
20520 ret = _lpfc_move_xri_pbl_to_pvt(
James Smart6a828b02019-01-28 11:14:31 -080020521 phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020522
20523 /* Exit while-loop if success or all hwqid are checked */
20524 } while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
20525
20526 /* Starting point for the next time */
20527 multixri_pool->rrb_next_hwqid = next_hwqid;
20528
20529 if (!ret) {
20530 /* stats: all public pools are empty*/
20531 multixri_pool->pbl_empty_count++;
20532 }
20533
20534#ifdef LPFC_MXP_STAT
20535 if (ret) {
20536 if (next_hwqid == hwqid)
20537 multixri_pool->local_pbl_hit_count++;
20538 else
20539 multixri_pool->other_pbl_hit_count++;
20540 }
20541#endif
20542}
20543
20544/**
20545 * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
20546 * @phba: pointer to lpfc hba data structure.
20547 * @qp: belong to which HWQ.
20548 *
20549 * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
20550 * low watermark.
20551 **/
20552void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
20553{
20554 struct lpfc_multixri_pool *multixri_pool;
20555 struct lpfc_pvt_pool *pvt_pool;
20556
20557 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20558 pvt_pool = &multixri_pool->pvt_pool;
20559
20560 if (pvt_pool->count < pvt_pool->low_watermark)
20561 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20562}
20563
20564/**
20565 * lpfc_release_io_buf - Return one IO buf back to free pool
20566 * @phba: pointer to lpfc hba data structure.
20567 * @lpfc_ncmd: IO buf to be returned.
20568 * @qp: belong to which HWQ.
20569 *
20570 * This routine returns one IO buf back to free pool. If this is an urgent IO,
20571 * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
20572 * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
20573 * xri_limit. If cfg_xri_rebalancing==0, the IO buf is returned to
20574 * lpfc_io_buf_list_put.
20575 **/
20576void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
20577 struct lpfc_sli4_hdw_queue *qp)
20578{
20579 unsigned long iflag;
20580 struct lpfc_pbl_pool *pbl_pool;
20581 struct lpfc_pvt_pool *pvt_pool;
20582 struct lpfc_epd_pool *epd_pool;
20583 u32 txcmplq_cnt;
20584 u32 xri_owned;
20585 u32 xri_limit;
20586 u32 abts_io_bufs;
20587
20588 /* MUST zero fields if buffer is reused by another protocol */
20589 lpfc_ncmd->nvmeCmd = NULL;
20590 lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
20591 lpfc_ncmd->cur_iocbq.iocb_cmpl = NULL;
20592
James Smart35a635a2019-09-21 20:59:02 -070020593 if (phba->cfg_xpsgl && !phba->nvmet_support &&
20594 !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
20595 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
20596
20597 if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
20598 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
20599
James Smartc4908502019-01-28 11:14:28 -080020600 if (phba->cfg_xri_rebalancing) {
20601 if (lpfc_ncmd->expedite) {
20602 /* Return to expedite pool */
20603 epd_pool = &phba->epd_pool;
20604 spin_lock_irqsave(&epd_pool->lock, iflag);
20605 list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
20606 epd_pool->count++;
20607 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20608 return;
20609 }
20610
20611 /* Avoid invalid access if an IO sneaks in and is being rejected
20612 * just _after_ xri pools are destroyed in lpfc_offline.
20613 * Nothing much can be done at this point.
20614 */
20615 if (!qp->p_multixri_pool)
20616 return;
20617
20618 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20619 pvt_pool = &qp->p_multixri_pool->pvt_pool;
20620
James Smartc00f62e2019-08-14 16:57:11 -070020621 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080020622 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070020623 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080020624
20625 xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
20626 xri_limit = qp->p_multixri_pool->xri_limit;
20627
20628#ifdef LPFC_MXP_STAT
20629 if (xri_owned <= xri_limit)
20630 qp->p_multixri_pool->below_limit_count++;
20631 else
20632 qp->p_multixri_pool->above_limit_count++;
20633#endif
20634
20635 /* XRI goes to either public or private free xri pool
20636 * based on watermark and xri_limit
20637 */
20638 if ((pvt_pool->count < pvt_pool->low_watermark) ||
20639 (xri_owned < xri_limit &&
20640 pvt_pool->count < pvt_pool->high_watermark)) {
James Smart6a828b02019-01-28 11:14:31 -080020641 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
20642 qp, free_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020643 list_add_tail(&lpfc_ncmd->list,
20644 &pvt_pool->list);
20645 pvt_pool->count++;
20646 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20647 } else {
James Smart6a828b02019-01-28 11:14:31 -080020648 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
20649 qp, free_pub_pool);
James Smartc4908502019-01-28 11:14:28 -080020650 list_add_tail(&lpfc_ncmd->list,
20651 &pbl_pool->list);
20652 pbl_pool->count++;
20653 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20654 }
20655 } else {
James Smart6a828b02019-01-28 11:14:31 -080020656 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
20657 qp, free_xri);
James Smartc4908502019-01-28 11:14:28 -080020658 list_add_tail(&lpfc_ncmd->list,
20659 &qp->lpfc_io_buf_list_put);
20660 qp->put_io_bufs++;
20661 spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
20662 iflag);
20663 }
20664}
20665
20666/**
20667 * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
20668 * @phba: pointer to lpfc hba data structure.
20669 * @pvt_pool: pointer to private pool data structure.
20670 * @ndlp: pointer to lpfc nodelist data structure.
20671 *
20672 * This routine tries to get one free IO buf from private pool.
20673 *
20674 * Return:
20675 * pointer to one free IO buf - if private pool is not empty
20676 * NULL - if private pool is empty
20677 **/
20678static struct lpfc_io_buf *
20679lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
James Smart6a828b02019-01-28 11:14:31 -080020680 struct lpfc_sli4_hdw_queue *qp,
James Smartc4908502019-01-28 11:14:28 -080020681 struct lpfc_pvt_pool *pvt_pool,
20682 struct lpfc_nodelist *ndlp)
20683{
20684 struct lpfc_io_buf *lpfc_ncmd;
20685 struct lpfc_io_buf *lpfc_ncmd_next;
20686 unsigned long iflag;
20687
James Smart6a828b02019-01-28 11:14:31 -080020688 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020689 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20690 &pvt_pool->list, list) {
20691 if (lpfc_test_rrq_active(
20692 phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
20693 continue;
20694 list_del(&lpfc_ncmd->list);
20695 pvt_pool->count--;
20696 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20697 return lpfc_ncmd;
20698 }
20699 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
20700
20701 return NULL;
20702}
20703
20704/**
20705 * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
20706 * @phba: pointer to lpfc hba data structure.
20707 *
20708 * This routine tries to get one free IO buf from expedite pool.
20709 *
20710 * Return:
20711 * pointer to one free IO buf - if expedite pool is not empty
20712 * NULL - if expedite pool is empty
20713 **/
20714static struct lpfc_io_buf *
20715lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
20716{
20717 struct lpfc_io_buf *lpfc_ncmd;
20718 struct lpfc_io_buf *lpfc_ncmd_next;
20719 unsigned long iflag;
20720 struct lpfc_epd_pool *epd_pool;
20721
20722 epd_pool = &phba->epd_pool;
20723 lpfc_ncmd = NULL;
20724
20725 spin_lock_irqsave(&epd_pool->lock, iflag);
20726 if (epd_pool->count > 0) {
20727 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20728 &epd_pool->list, list) {
20729 list_del(&lpfc_ncmd->list);
20730 epd_pool->count--;
20731 break;
20732 }
20733 }
20734 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20735
20736 return lpfc_ncmd;
20737}
20738
20739/**
20740 * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
20741 * @phba: pointer to lpfc hba data structure.
20742 * @ndlp: pointer to lpfc nodelist data structure.
20743 * @hwqid: belong to which HWQ
20744 * @expedite: 1 means this request is urgent.
20745 *
20746 * This routine will do the following actions and then return a pointer to
20747 * one free IO buf.
20748 *
20749 * 1. If private free xri count is empty, move some XRIs from public to
20750 * private pool.
20751 * 2. Get one XRI from private free xri pool.
20752 * 3. If we fail to get one from pvt_pool and this is an expedite request,
20753 * get one free xri from expedite pool.
20754 *
20755 * Note: ndlp is only used on SCSI side for RRQ testing.
20756 * The caller should pass NULL for ndlp on NVME side.
20757 *
20758 * Return:
20759 * pointer to one free IO buf - if private pool is not empty
20760 * NULL - if private pool is empty
20761 **/
20762static struct lpfc_io_buf *
20763lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
20764 struct lpfc_nodelist *ndlp,
20765 int hwqid, int expedite)
20766{
20767 struct lpfc_sli4_hdw_queue *qp;
20768 struct lpfc_multixri_pool *multixri_pool;
20769 struct lpfc_pvt_pool *pvt_pool;
20770 struct lpfc_io_buf *lpfc_ncmd;
20771
20772 qp = &phba->sli4_hba.hdwq[hwqid];
20773 lpfc_ncmd = NULL;
20774 multixri_pool = qp->p_multixri_pool;
20775 pvt_pool = &multixri_pool->pvt_pool;
20776 multixri_pool->io_req_count++;
20777
20778 /* If pvt_pool is empty, move some XRIs from public to private pool */
20779 if (pvt_pool->count == 0)
20780 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20781
20782 /* Get one XRI from private free xri pool */
James Smart6a828b02019-01-28 11:14:31 -080020783 lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
James Smartc4908502019-01-28 11:14:28 -080020784
20785 if (lpfc_ncmd) {
20786 lpfc_ncmd->hdwq = qp;
20787 lpfc_ncmd->hdwq_no = hwqid;
20788 } else if (expedite) {
20789 /* If we fail to get one from pvt_pool and this is an expedite
20790 * request, get one free xri from expedite pool.
20791 */
20792 lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
20793 }
20794
20795 return lpfc_ncmd;
20796}
20797
20798static inline struct lpfc_io_buf *
20799lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
20800{
20801 struct lpfc_sli4_hdw_queue *qp;
20802 struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
20803
20804 qp = &phba->sli4_hba.hdwq[idx];
20805 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
20806 &qp->lpfc_io_buf_list_get, list) {
20807 if (lpfc_test_rrq_active(phba, ndlp,
20808 lpfc_cmd->cur_iocbq.sli4_lxritag))
20809 continue;
20810
20811 if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
20812 continue;
20813
20814 list_del_init(&lpfc_cmd->list);
20815 qp->get_io_bufs--;
20816 lpfc_cmd->hdwq = qp;
20817 lpfc_cmd->hdwq_no = idx;
20818 return lpfc_cmd;
20819 }
20820 return NULL;
20821}
20822
20823/**
20824 * lpfc_get_io_buf - Get one IO buffer from free pool
20825 * @phba: The HBA for which this call is being executed.
20826 * @ndlp: pointer to lpfc nodelist data structure.
20827 * @hwqid: belong to which HWQ
20828 * @expedite: 1 means this request is urgent.
20829 *
20830 * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
20831 * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
20832 * a IO buffer from head of @hdwq io_buf_list and returns to caller.
20833 *
20834 * Note: ndlp is only used on SCSI side for RRQ testing.
20835 * The caller should pass NULL for ndlp on NVME side.
20836 *
20837 * Return codes:
20838 * NULL - Error
20839 * Pointer to lpfc_io_buf - Success
20840 **/
20841struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
20842 struct lpfc_nodelist *ndlp,
20843 u32 hwqid, int expedite)
20844{
20845 struct lpfc_sli4_hdw_queue *qp;
20846 unsigned long iflag;
20847 struct lpfc_io_buf *lpfc_cmd;
20848
20849 qp = &phba->sli4_hba.hdwq[hwqid];
20850 lpfc_cmd = NULL;
20851
20852 if (phba->cfg_xri_rebalancing)
20853 lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
20854 phba, ndlp, hwqid, expedite);
20855 else {
James Smart6a828b02019-01-28 11:14:31 -080020856 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
20857 qp, alloc_xri_get);
James Smartc4908502019-01-28 11:14:28 -080020858 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
20859 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20860 if (!lpfc_cmd) {
James Smart6a828b02019-01-28 11:14:31 -080020861 lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
20862 qp, alloc_xri_put);
James Smartc4908502019-01-28 11:14:28 -080020863 list_splice(&qp->lpfc_io_buf_list_put,
20864 &qp->lpfc_io_buf_list_get);
20865 qp->get_io_bufs += qp->put_io_bufs;
20866 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
20867 qp->put_io_bufs = 0;
20868 spin_unlock(&qp->io_buf_list_put_lock);
20869 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
20870 expedite)
20871 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
20872 }
20873 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
20874 }
20875
20876 return lpfc_cmd;
20877}
James Smartd79c9e92019-08-14 16:57:09 -070020878
20879/**
20880 * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
20881 * @phba: The HBA for which this call is being executed.
20882 * @lpfc_buf: IO buf structure to append the SGL chunk
20883 *
20884 * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
20885 * and will allocate an SGL chunk if the pool is empty.
20886 *
20887 * Return codes:
20888 * NULL - Error
20889 * Pointer to sli4_hybrid_sgl - Success
20890 **/
20891struct sli4_hybrid_sgl *
20892lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
20893{
20894 struct sli4_hybrid_sgl *list_entry = NULL;
20895 struct sli4_hybrid_sgl *tmp = NULL;
20896 struct sli4_hybrid_sgl *allocated_sgl = NULL;
20897 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
20898 struct list_head *buf_list = &hdwq->sgl_list;
James Smarta4c21ac2019-09-21 20:59:01 -070020899 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070020900
James Smarta4c21ac2019-09-21 20:59:01 -070020901 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020902
20903 if (likely(!list_empty(buf_list))) {
20904 /* break off 1 chunk from the sgl_list */
20905 list_for_each_entry_safe(list_entry, tmp,
20906 buf_list, list_node) {
20907 list_move_tail(&list_entry->list_node,
20908 &lpfc_buf->dma_sgl_xtra_list);
20909 break;
20910 }
20911 } else {
20912 /* allocate more */
James Smarta4c21ac2019-09-21 20:59:01 -070020913 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020914 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
James Smart4583a4f2019-11-15 16:38:47 -080020915 cpu_to_node(hdwq->io_wq->chann));
James Smartd79c9e92019-08-14 16:57:09 -070020916 if (!tmp) {
20917 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20918 "8353 error kmalloc memory for HDWQ "
20919 "%d %s\n",
20920 lpfc_buf->hdwq_no, __func__);
20921 return NULL;
20922 }
20923
20924 tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
20925 GFP_ATOMIC, &tmp->dma_phys_sgl);
20926 if (!tmp->dma_sgl) {
20927 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
20928 "8354 error pool_alloc memory for HDWQ "
20929 "%d %s\n",
20930 lpfc_buf->hdwq_no, __func__);
20931 kfree(tmp);
20932 return NULL;
20933 }
20934
James Smarta4c21ac2019-09-21 20:59:01 -070020935 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020936 list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
20937 }
20938
20939 allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
20940 struct sli4_hybrid_sgl,
20941 list_node);
20942
James Smarta4c21ac2019-09-21 20:59:01 -070020943 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020944
20945 return allocated_sgl;
20946}
20947
20948/**
20949 * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
20950 * @phba: The HBA for which this call is being executed.
20951 * @lpfc_buf: IO buf structure with the SGL chunk
20952 *
20953 * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
20954 *
20955 * Return codes:
20956 * 0 - Success
20957 * -EINVAL - Error
20958 **/
20959int
20960lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
20961{
20962 int rc = 0;
20963 struct sli4_hybrid_sgl *list_entry = NULL;
20964 struct sli4_hybrid_sgl *tmp = NULL;
20965 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
20966 struct list_head *buf_list = &hdwq->sgl_list;
James Smarta4c21ac2019-09-21 20:59:01 -070020967 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070020968
James Smarta4c21ac2019-09-21 20:59:01 -070020969 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020970
20971 if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
20972 list_for_each_entry_safe(list_entry, tmp,
20973 &lpfc_buf->dma_sgl_xtra_list,
20974 list_node) {
20975 list_move_tail(&list_entry->list_node,
20976 buf_list);
20977 }
20978 } else {
20979 rc = -EINVAL;
20980 }
20981
James Smarta4c21ac2019-09-21 20:59:01 -070020982 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070020983 return rc;
20984}
20985
20986/**
20987 * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
20988 * @phba: phba object
20989 * @hdwq: hdwq to cleanup sgl buff resources on
20990 *
20991 * This routine frees all SGL chunks of hdwq SGL chunk pool.
20992 *
20993 * Return codes:
20994 * None
20995 **/
20996void
20997lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
20998 struct lpfc_sli4_hdw_queue *hdwq)
20999{
21000 struct list_head *buf_list = &hdwq->sgl_list;
21001 struct sli4_hybrid_sgl *list_entry = NULL;
21002 struct sli4_hybrid_sgl *tmp = NULL;
James Smarta4c21ac2019-09-21 20:59:01 -070021003 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070021004
James Smarta4c21ac2019-09-21 20:59:01 -070021005 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021006
21007 /* Free sgl pool */
21008 list_for_each_entry_safe(list_entry, tmp,
21009 buf_list, list_node) {
21010 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
21011 list_entry->dma_sgl,
21012 list_entry->dma_phys_sgl);
21013 list_del(&list_entry->list_node);
21014 kfree(list_entry);
21015 }
21016
James Smarta4c21ac2019-09-21 20:59:01 -070021017 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021018}
21019
21020/**
21021 * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
21022 * @phba: The HBA for which this call is being executed.
21023 * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
21024 *
21025 * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
21026 * and will allocate an CMD/RSP buffer if the pool is empty.
21027 *
21028 * Return codes:
21029 * NULL - Error
21030 * Pointer to fcp_cmd_rsp_buf - Success
21031 **/
21032struct fcp_cmd_rsp_buf *
21033lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21034 struct lpfc_io_buf *lpfc_buf)
21035{
21036 struct fcp_cmd_rsp_buf *list_entry = NULL;
21037 struct fcp_cmd_rsp_buf *tmp = NULL;
21038 struct fcp_cmd_rsp_buf *allocated_buf = NULL;
21039 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21040 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
James Smarta4c21ac2019-09-21 20:59:01 -070021041 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070021042
James Smarta4c21ac2019-09-21 20:59:01 -070021043 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021044
21045 if (likely(!list_empty(buf_list))) {
21046 /* break off 1 chunk from the list */
21047 list_for_each_entry_safe(list_entry, tmp,
21048 buf_list,
21049 list_node) {
21050 list_move_tail(&list_entry->list_node,
21051 &lpfc_buf->dma_cmd_rsp_list);
21052 break;
21053 }
21054 } else {
21055 /* allocate more */
James Smarta4c21ac2019-09-21 20:59:01 -070021056 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021057 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
James Smart4583a4f2019-11-15 16:38:47 -080021058 cpu_to_node(hdwq->io_wq->chann));
James Smartd79c9e92019-08-14 16:57:09 -070021059 if (!tmp) {
21060 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21061 "8355 error kmalloc memory for HDWQ "
21062 "%d %s\n",
21063 lpfc_buf->hdwq_no, __func__);
21064 return NULL;
21065 }
21066
21067 tmp->fcp_cmnd = dma_pool_alloc(phba->lpfc_cmd_rsp_buf_pool,
21068 GFP_ATOMIC,
21069 &tmp->fcp_cmd_rsp_dma_handle);
21070
21071 if (!tmp->fcp_cmnd) {
21072 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21073 "8356 error pool_alloc memory for HDWQ "
21074 "%d %s\n",
21075 lpfc_buf->hdwq_no, __func__);
21076 kfree(tmp);
21077 return NULL;
21078 }
21079
21080 tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
21081 sizeof(struct fcp_cmnd));
21082
James Smarta4c21ac2019-09-21 20:59:01 -070021083 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021084 list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
21085 }
21086
21087 allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
21088 struct fcp_cmd_rsp_buf,
21089 list_node);
21090
James Smarta4c21ac2019-09-21 20:59:01 -070021091 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021092
21093 return allocated_buf;
21094}
21095
21096/**
21097 * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
21098 * @phba: The HBA for which this call is being executed.
21099 * @lpfc_buf: IO buf structure with the CMD/RSP buf
21100 *
21101 * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
21102 *
21103 * Return codes:
21104 * 0 - Success
21105 * -EINVAL - Error
21106 **/
21107int
21108lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21109 struct lpfc_io_buf *lpfc_buf)
21110{
21111 int rc = 0;
21112 struct fcp_cmd_rsp_buf *list_entry = NULL;
21113 struct fcp_cmd_rsp_buf *tmp = NULL;
21114 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21115 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
James Smarta4c21ac2019-09-21 20:59:01 -070021116 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070021117
James Smarta4c21ac2019-09-21 20:59:01 -070021118 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021119
21120 if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
21121 list_for_each_entry_safe(list_entry, tmp,
21122 &lpfc_buf->dma_cmd_rsp_list,
21123 list_node) {
21124 list_move_tail(&list_entry->list_node,
21125 buf_list);
21126 }
21127 } else {
21128 rc = -EINVAL;
21129 }
21130
James Smarta4c21ac2019-09-21 20:59:01 -070021131 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021132 return rc;
21133}
21134
21135/**
21136 * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
21137 * @phba: phba object
21138 * @hdwq: hdwq to cleanup cmd rsp buff resources on
21139 *
21140 * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
21141 *
21142 * Return codes:
21143 * None
21144 **/
21145void
21146lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21147 struct lpfc_sli4_hdw_queue *hdwq)
21148{
21149 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
21150 struct fcp_cmd_rsp_buf *list_entry = NULL;
21151 struct fcp_cmd_rsp_buf *tmp = NULL;
James Smarta4c21ac2019-09-21 20:59:01 -070021152 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070021153
James Smarta4c21ac2019-09-21 20:59:01 -070021154 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021155
21156 /* Free cmd_rsp buf pool */
21157 list_for_each_entry_safe(list_entry, tmp,
21158 buf_list,
21159 list_node) {
21160 dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
21161 list_entry->fcp_cmnd,
21162 list_entry->fcp_cmd_rsp_dma_handle);
21163 list_del(&list_entry->list_node);
21164 kfree(list_entry);
21165 }
21166
James Smarta4c21ac2019-09-21 20:59:01 -070021167 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021168}