blob: a6a8f26eda77ca94faf3fe490e467af3a2fed12d [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 Smart67073c62021-03-01 09:18:21 -08004 * Copyright (C) 2017-2021 Broadcom. All Rights Reserved. The term *
5 * “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 Smart840a4702020-11-15 11:26:40 -080093union lpfc_wqe128 lpfc_iread_cmd_template;
94union lpfc_wqe128 lpfc_iwrite_cmd_template;
95union lpfc_wqe128 lpfc_icmnd_cmd_template;
96
James Smart4f774512009-05-22 14:52:35 -040097static IOCB_t *
98lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
99{
100 return &iocbq->iocb;
101}
102
James Smart840a4702020-11-15 11:26:40 -0800103/* Setup WQE templates for IOs */
104void lpfc_wqe_cmd_template(void)
105{
106 union lpfc_wqe128 *wqe;
107
108 /* IREAD template */
109 wqe = &lpfc_iread_cmd_template;
110 memset(wqe, 0, sizeof(union lpfc_wqe128));
111
112 /* Word 0, 1, 2 - BDE is variable */
113
114 /* Word 3 - cmd_buff_len, payload_offset_len is zero */
115
116 /* Word 4 - total_xfer_len is variable */
117
118 /* Word 5 - is zero */
119
120 /* Word 6 - ctxt_tag, xri_tag is variable */
121
122 /* Word 7 */
123 bf_set(wqe_cmnd, &wqe->fcp_iread.wqe_com, CMD_FCP_IREAD64_WQE);
124 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, PARM_READ_CHECK);
125 bf_set(wqe_class, &wqe->fcp_iread.wqe_com, CLASS3);
126 bf_set(wqe_ct, &wqe->fcp_iread.wqe_com, SLI4_CT_RPI);
127
128 /* Word 8 - abort_tag is variable */
129
130 /* Word 9 - reqtag is variable */
131
132 /* Word 10 - dbde, wqes is variable */
133 bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
134 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
135 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com, LPFC_WQE_LENLOC_WORD4);
136 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
137 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
138
139 /* Word 11 - pbde is variable */
140 bf_set(wqe_cmd_type, &wqe->fcp_iread.wqe_com, COMMAND_DATA_IN);
141 bf_set(wqe_cqid, &wqe->fcp_iread.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
142 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
143
144 /* Word 12 - is zero */
145
146 /* Word 13, 14, 15 - PBDE is variable */
147
148 /* IWRITE template */
149 wqe = &lpfc_iwrite_cmd_template;
150 memset(wqe, 0, sizeof(union lpfc_wqe128));
151
152 /* Word 0, 1, 2 - BDE is variable */
153
154 /* Word 3 - cmd_buff_len, payload_offset_len is zero */
155
156 /* Word 4 - total_xfer_len is variable */
157
158 /* Word 5 - initial_xfer_len is variable */
159
160 /* Word 6 - ctxt_tag, xri_tag is variable */
161
162 /* Word 7 */
163 bf_set(wqe_cmnd, &wqe->fcp_iwrite.wqe_com, CMD_FCP_IWRITE64_WQE);
164 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, PARM_READ_CHECK);
165 bf_set(wqe_class, &wqe->fcp_iwrite.wqe_com, CLASS3);
166 bf_set(wqe_ct, &wqe->fcp_iwrite.wqe_com, SLI4_CT_RPI);
167
168 /* Word 8 - abort_tag is variable */
169
170 /* Word 9 - reqtag is variable */
171
172 /* Word 10 - dbde, wqes is variable */
173 bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
174 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
175 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_LENLOC_WORD4);
176 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
177 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
178
179 /* Word 11 - pbde is variable */
180 bf_set(wqe_cmd_type, &wqe->fcp_iwrite.wqe_com, COMMAND_DATA_OUT);
181 bf_set(wqe_cqid, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
182 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
183
184 /* Word 12 - is zero */
185
186 /* Word 13, 14, 15 - PBDE is variable */
187
188 /* ICMND template */
189 wqe = &lpfc_icmnd_cmd_template;
190 memset(wqe, 0, sizeof(union lpfc_wqe128));
191
192 /* Word 0, 1, 2 - BDE is variable */
193
194 /* Word 3 - payload_offset_len is variable */
195
196 /* Word 4, 5 - is zero */
197
198 /* Word 6 - ctxt_tag, xri_tag is variable */
199
200 /* Word 7 */
201 bf_set(wqe_cmnd, &wqe->fcp_icmd.wqe_com, CMD_FCP_ICMND64_WQE);
202 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
203 bf_set(wqe_class, &wqe->fcp_icmd.wqe_com, CLASS3);
204 bf_set(wqe_ct, &wqe->fcp_icmd.wqe_com, SLI4_CT_RPI);
205
206 /* Word 8 - abort_tag is variable */
207
208 /* Word 9 - reqtag is variable */
209
210 /* Word 10 - dbde, wqes is variable */
211 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
212 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_NONE);
213 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com, LPFC_WQE_LENLOC_NONE);
214 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
215 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
216
217 /* Word 11 */
218 bf_set(wqe_cmd_type, &wqe->fcp_icmd.wqe_com, COMMAND_DATA_IN);
219 bf_set(wqe_cqid, &wqe->fcp_icmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
220 bf_set(wqe_pbde, &wqe->fcp_icmd.wqe_com, 0);
221
222 /* Word 12, 13, 14, 15 - is zero */
223}
224
James Smart48f8fdb2018-05-04 20:37:51 -0700225#if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
226/**
227 * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
228 * @srcp: Source memory pointer.
229 * @destp: Destination memory pointer.
230 * @cnt: Number of words required to be copied.
231 * Must be a multiple of sizeof(uint64_t)
232 *
233 * This function is used for copying data between driver memory
234 * and the SLI WQ. This function also changes the endianness
235 * of each word if native endianness is different from SLI
236 * endianness. This function can be called with or without
237 * lock.
238 **/
YueHaibingd7b761b2019-05-31 23:28:41 +0800239static void
James Smart48f8fdb2018-05-04 20:37:51 -0700240lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
241{
242 uint64_t *src = srcp;
243 uint64_t *dest = destp;
244 int i;
245
246 for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
247 *dest++ = *src++;
248}
249#else
250#define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
251#endif
252
James Smart4f774512009-05-22 14:52:35 -0400253/**
254 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
255 * @q: The Work Queue to operate on.
256 * @wqe: The work Queue Entry to put on the Work queue.
257 *
258 * This routine will copy the contents of @wqe to the next available entry on
259 * the @q. This function will then ring the Work Queue Doorbell to signal the
260 * HBA to start processing the Work Queue Entry. This function returns 0 if
261 * successful. If no entries are available on @q then this function will return
262 * -ENOMEM.
263 * The caller is expected to hold the hbalock when calling this routine.
264 **/
Dick Kennedycd22d602017-08-23 16:55:35 -0700265static int
James Smart205e8242018-03-05 12:04:03 -0800266lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -0400267{
James Smart2e90f4b2011-12-13 13:22:37 -0500268 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400269 struct lpfc_register doorbell;
270 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400271 uint32_t idx;
James Smart1351e692018-02-22 08:18:43 -0800272 uint32_t i = 0;
273 uint8_t *tmp;
James Smart5cc167d2018-06-26 08:24:23 -0700274 u32 if_type;
James Smart4f774512009-05-22 14:52:35 -0400275
James Smart2e90f4b2011-12-13 13:22:37 -0500276 /* sanity check on queue memory */
277 if (unlikely(!q))
278 return -ENOMEM;
James Smart47ff4c52020-11-15 11:26:41 -0800279
James Smart9afbee32019-03-12 16:30:28 -0700280 temp_wqe = lpfc_sli4_qe(q, q->host_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500281
James Smart4f774512009-05-22 14:52:35 -0400282 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400283 idx = ((q->host_index + 1) % q->entry_count);
284 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400285 q->WQ_overflow++;
Dick Kennedycd22d602017-08-23 16:55:35 -0700286 return -EBUSY;
James Smartb84daac2012-08-03 12:35:13 -0400287 }
288 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400289 /* set consumption flag every once in a while */
James Smart32517fc2019-01-28 11:14:33 -0800290 if (!((q->host_index + 1) % q->notify_interval))
James Smartf0d9bcc2010-10-22 11:07:09 -0400291 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smart04673e32018-01-30 15:58:45 -0800292 else
293 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
James Smartfedd3b72011-02-16 12:39:24 -0500294 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
295 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart48f8fdb2018-05-04 20:37:51 -0700296 lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart1351e692018-02-22 08:18:43 -0800297 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
298 /* write to DPP aperture taking advatage of Combined Writes */
James Smart4c066192018-03-05 10:29:03 -0800299 tmp = (uint8_t *)temp_wqe;
300#ifdef __raw_writeq
James Smart1351e692018-02-22 08:18:43 -0800301 for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
James Smart4c066192018-03-05 10:29:03 -0800302 __raw_writeq(*((uint64_t *)(tmp + i)),
303 q->dpp_regaddr + i);
304#else
305 for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
306 __raw_writel(*((uint32_t *)(tmp + i)),
307 q->dpp_regaddr + i);
308#endif
James Smart1351e692018-02-22 08:18:43 -0800309 }
310 /* ensure WQE bcopy and DPP flushed before doorbell write */
James Smart6b3b3bd2016-12-19 15:07:30 -0800311 wmb();
James Smart4f774512009-05-22 14:52:35 -0400312
313 /* Update the host index before invoking device */
314 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400315
316 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400317
318 /* Ring Doorbell */
319 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500320 if (q->db_format == LPFC_DB_LIST_FORMAT) {
James Smart1351e692018-02-22 08:18:43 -0800321 if (q->dpp_enable && q->phba->cfg_enable_dpp) {
322 bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
323 bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
324 bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
325 q->dpp_id);
326 bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
327 q->queue_id);
328 } else {
329 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
James Smart1351e692018-02-22 08:18:43 -0800330 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
James Smart5cc167d2018-06-26 08:24:23 -0700331
332 /* Leave bits <23:16> clear for if_type 6 dpp */
333 if_type = bf_get(lpfc_sli_intf_if_type,
334 &q->phba->sli4_hba.sli_intf);
335 if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
336 bf_set(lpfc_wq_db_list_fm_index, &doorbell,
337 host_index);
James Smart1351e692018-02-22 08:18:43 -0800338 }
James Smart962bc512013-01-03 15:44:00 -0500339 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
340 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
341 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
342 } else {
343 return -EINVAL;
344 }
345 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400346
347 return 0;
348}
349
350/**
351 * lpfc_sli4_wq_release - Updates internal hba index for WQ
352 * @q: The Work Queue to operate on.
353 * @index: The index to advance the hba index to.
354 *
355 * This routine will update the HBA index of a queue to reflect consumption of
356 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
357 * an entry the host calls this function to update the queue's internal
James Smart1543af32020-03-22 11:12:58 -0700358 * pointers.
James Smart4f774512009-05-22 14:52:35 -0400359 **/
James Smart1543af32020-03-22 11:12:58 -0700360static void
James Smart4f774512009-05-22 14:52:35 -0400361lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
362{
James Smart2e90f4b2011-12-13 13:22:37 -0500363 /* sanity check on queue memory */
364 if (unlikely(!q))
James Smart1543af32020-03-22 11:12:58 -0700365 return;
James Smart2e90f4b2011-12-13 13:22:37 -0500366
James Smart1543af32020-03-22 11:12:58 -0700367 q->hba_index = index;
James Smart4f774512009-05-22 14:52:35 -0400368}
369
370/**
371 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
372 * @q: The Mailbox Queue to operate on.
Lee Jones7af29d42020-07-21 17:41:31 +0100373 * @mqe: The Mailbox Queue Entry to put on the Work queue.
James Smart4f774512009-05-22 14:52:35 -0400374 *
375 * This routine will copy the contents of @mqe to the next available entry on
376 * the @q. This function will then ring the Work Queue Doorbell to signal the
377 * HBA to start processing the Work Queue Entry. This function returns 0 if
378 * successful. If no entries are available on @q then this function will return
379 * -ENOMEM.
380 * The caller is expected to hold the hbalock when calling this routine.
381 **/
382static uint32_t
383lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
384{
James Smart2e90f4b2011-12-13 13:22:37 -0500385 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400386 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400387
James Smart2e90f4b2011-12-13 13:22:37 -0500388 /* sanity check on queue memory */
389 if (unlikely(!q))
390 return -ENOMEM;
James Smart9afbee32019-03-12 16:30:28 -0700391 temp_mqe = lpfc_sli4_qe(q, q->host_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500392
James Smart4f774512009-05-22 14:52:35 -0400393 /* If the host has not yet processed the next entry then we are done */
394 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
395 return -ENOMEM;
James Smart48f8fdb2018-05-04 20:37:51 -0700396 lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400397 /* Save off the mailbox pointer for completion */
398 q->phba->mbox = (MAILBOX_t *)temp_mqe;
399
400 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400401 q->host_index = ((q->host_index + 1) % q->entry_count);
402
403 /* Ring Doorbell */
404 doorbell.word0 = 0;
405 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
406 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
407 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400408 return 0;
409}
410
411/**
412 * lpfc_sli4_mq_release - Updates internal hba index for MQ
413 * @q: The Mailbox Queue to operate on.
414 *
415 * This routine will update the HBA index of a queue to reflect consumption of
416 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
417 * an entry the host calls this function to update the queue's internal
418 * pointers. This routine returns the number of entries that were consumed by
419 * the HBA.
420 **/
421static uint32_t
422lpfc_sli4_mq_release(struct lpfc_queue *q)
423{
James Smart2e90f4b2011-12-13 13:22:37 -0500424 /* sanity check on queue memory */
425 if (unlikely(!q))
426 return 0;
427
James Smart4f774512009-05-22 14:52:35 -0400428 /* Clear the mailbox pointer for completion */
429 q->phba->mbox = NULL;
430 q->hba_index = ((q->hba_index + 1) % q->entry_count);
431 return 1;
432}
433
434/**
435 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
436 * @q: The Event Queue to get the first valid EQE from
437 *
438 * This routine will get the first valid Event Queue Entry from @q, update
439 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
440 * the Queue (no more work to do), or the Queue is full of EQEs that have been
441 * processed, but not popped back to the HBA then this routine will return NULL.
442 **/
443static struct lpfc_eqe *
444lpfc_sli4_eq_get(struct lpfc_queue *q)
445{
James Smart2e90f4b2011-12-13 13:22:37 -0500446 struct lpfc_eqe *eqe;
447
448 /* sanity check on queue memory */
449 if (unlikely(!q))
450 return NULL;
James Smart9afbee32019-03-12 16:30:28 -0700451 eqe = lpfc_sli4_qe(q, q->host_index);
James Smart4f774512009-05-22 14:52:35 -0400452
453 /* If the next EQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800454 if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400455 return NULL;
James Smart27f344e2014-05-07 17:16:46 -0400456
457 /*
458 * insert barrier for instruction interlock : data from the hardware
459 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800460 * upon. Speculative instructions were allowing a bcopy at the start
461 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
462 * after our return, to copy data before the valid bit check above
463 * was done. As such, some of the copied data was stale. The barrier
464 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400465 */
466 mb();
James Smart4f774512009-05-22 14:52:35 -0400467 return eqe;
468}
469
470/**
James Smartba20c852012-08-03 12:36:52 -0400471 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
472 * @q: The Event Queue to disable interrupts
473 *
474 **/
James Smart92f3b322019-03-20 10:44:22 -0700475void
James Smartba20c852012-08-03 12:36:52 -0400476lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
477{
478 struct lpfc_register doorbell;
479
480 doorbell.word0 = 0;
481 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
482 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
483 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
484 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
485 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800486 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smartba20c852012-08-03 12:36:52 -0400487}
488
489/**
James Smart27d6ac02018-02-22 08:18:42 -0800490 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
491 * @q: The Event Queue to disable interrupts
492 *
493 **/
James Smart92f3b322019-03-20 10:44:22 -0700494void
James Smart27d6ac02018-02-22 08:18:42 -0800495lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
496{
497 struct lpfc_register doorbell;
498
499 doorbell.word0 = 0;
James Smartaad59d52018-09-10 10:30:47 -0700500 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
James Smart27d6ac02018-02-22 08:18:42 -0800501 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
502}
503
504/**
James Smart32517fc2019-01-28 11:14:33 -0800505 * lpfc_sli4_write_eq_db - write EQ DB for eqe's consumed or arm state
506 * @phba: adapter with EQ
James Smart4f774512009-05-22 14:52:35 -0400507 * @q: The Event Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800508 * @count: Number of elements that have been consumed
James Smart4f774512009-05-22 14:52:35 -0400509 * @arm: Indicates whether the host wants to arms this CQ.
510 *
James Smart32517fc2019-01-28 11:14:33 -0800511 * This routine will notify the HBA, by ringing the doorbell, that count
512 * number of EQEs have been processed. The @arm parameter indicates whether
513 * the queue should be rearmed when ringing the doorbell.
James Smart4f774512009-05-22 14:52:35 -0400514 **/
James Smart32517fc2019-01-28 11:14:33 -0800515void
516lpfc_sli4_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
517 uint32_t count, bool arm)
James Smart4f774512009-05-22 14:52:35 -0400518{
James Smart4f774512009-05-22 14:52:35 -0400519 struct lpfc_register doorbell;
520
James Smart2e90f4b2011-12-13 13:22:37 -0500521 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800522 if (unlikely(!q || (count == 0 && !arm)))
523 return;
James Smart4f774512009-05-22 14:52:35 -0400524
525 /* ring doorbell for number popped */
526 doorbell.word0 = 0;
527 if (arm) {
528 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
529 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
530 }
James Smart32517fc2019-01-28 11:14:33 -0800531 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
James Smart4f774512009-05-22 14:52:35 -0400532 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500533 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
534 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
535 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800536 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500537 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
538 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
James Smart9dd35422018-02-22 08:18:41 -0800539 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400540}
541
542/**
James Smart32517fc2019-01-28 11:14:33 -0800543 * lpfc_sli4_if6_write_eq_db - write EQ DB for eqe's consumed or arm state
544 * @phba: adapter with EQ
James Smart27d6ac02018-02-22 08:18:42 -0800545 * @q: The Event Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800546 * @count: Number of elements that have been consumed
James Smart27d6ac02018-02-22 08:18:42 -0800547 * @arm: Indicates whether the host wants to arms this CQ.
548 *
James Smart32517fc2019-01-28 11:14:33 -0800549 * This routine will notify the HBA, by ringing the doorbell, that count
550 * number of EQEs have been processed. The @arm parameter indicates whether
551 * the queue should be rearmed when ringing the doorbell.
James Smart27d6ac02018-02-22 08:18:42 -0800552 **/
James Smart32517fc2019-01-28 11:14:33 -0800553void
554lpfc_sli4_if6_write_eq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
555 uint32_t count, bool arm)
James Smart27d6ac02018-02-22 08:18:42 -0800556{
James Smart27d6ac02018-02-22 08:18:42 -0800557 struct lpfc_register doorbell;
558
559 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800560 if (unlikely(!q || (count == 0 && !arm)))
561 return;
James Smart27d6ac02018-02-22 08:18:42 -0800562
563 /* ring doorbell for number popped */
564 doorbell.word0 = 0;
565 if (arm)
566 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800567 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, count);
James Smart27d6ac02018-02-22 08:18:42 -0800568 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
569 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
570 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
571 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
572 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart32517fc2019-01-28 11:14:33 -0800573}
574
575static void
576__lpfc_sli4_consume_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
577 struct lpfc_eqe *eqe)
578{
579 if (!phba->sli4_hba.pc_sli4_params.eqav)
580 bf_set_le32(lpfc_eqe_valid, eqe, 0);
581
582 eq->host_index = ((eq->host_index + 1) % eq->entry_count);
583
584 /* if the index wrapped around, toggle the valid bit */
585 if (phba->sli4_hba.pc_sli4_params.eqav && !eq->host_index)
586 eq->qe_valid = (eq->qe_valid) ? 0 : 1;
587}
588
589static void
James Smart24c7c0a2019-09-21 20:58:58 -0700590lpfc_sli4_eqcq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
James Smart32517fc2019-01-28 11:14:33 -0800591{
James Smart24c7c0a2019-09-21 20:58:58 -0700592 struct lpfc_eqe *eqe = NULL;
593 u32 eq_count = 0, cq_count = 0;
594 struct lpfc_cqe *cqe = NULL;
595 struct lpfc_queue *cq = NULL, *childq = NULL;
596 int cqid = 0;
James Smart32517fc2019-01-28 11:14:33 -0800597
598 /* walk all the EQ entries and drop on the floor */
599 eqe = lpfc_sli4_eq_get(eq);
600 while (eqe) {
James Smart24c7c0a2019-09-21 20:58:58 -0700601 /* Get the reference to the corresponding CQ */
602 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
603 cq = NULL;
604
605 list_for_each_entry(childq, &eq->child_list, list) {
606 if (childq->queue_id == cqid) {
607 cq = childq;
608 break;
609 }
610 }
611 /* If CQ is valid, iterate through it and drop all the CQEs */
612 if (cq) {
613 cqe = lpfc_sli4_cq_get(cq);
614 while (cqe) {
615 __lpfc_sli4_consume_cqe(phba, cq, cqe);
616 cq_count++;
617 cqe = lpfc_sli4_cq_get(cq);
618 }
619 /* Clear and re-arm the CQ */
620 phba->sli4_hba.sli4_write_cq_db(phba, cq, cq_count,
621 LPFC_QUEUE_REARM);
622 cq_count = 0;
623 }
James Smart32517fc2019-01-28 11:14:33 -0800624 __lpfc_sli4_consume_eqe(phba, eq, eqe);
James Smart24c7c0a2019-09-21 20:58:58 -0700625 eq_count++;
James Smart32517fc2019-01-28 11:14:33 -0800626 eqe = lpfc_sli4_eq_get(eq);
627 }
628
629 /* Clear and re-arm the EQ */
James Smart24c7c0a2019-09-21 20:58:58 -0700630 phba->sli4_hba.sli4_write_eq_db(phba, eq, eq_count, LPFC_QUEUE_REARM);
James Smart32517fc2019-01-28 11:14:33 -0800631}
632
633static int
James Smart93a4d6f2019-11-04 16:57:05 -0800634lpfc_sli4_process_eq(struct lpfc_hba *phba, struct lpfc_queue *eq,
635 uint8_t rearm)
James Smart32517fc2019-01-28 11:14:33 -0800636{
637 struct lpfc_eqe *eqe;
638 int count = 0, consumed = 0;
639
640 if (cmpxchg(&eq->queue_claimed, 0, 1) != 0)
641 goto rearm_and_exit;
642
643 eqe = lpfc_sli4_eq_get(eq);
644 while (eqe) {
645 lpfc_sli4_hba_handle_eqe(phba, eq, eqe);
646 __lpfc_sli4_consume_eqe(phba, eq, eqe);
647
648 consumed++;
649 if (!(++count % eq->max_proc_limit))
650 break;
651
652 if (!(count % eq->notify_interval)) {
653 phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed,
654 LPFC_QUEUE_NOARM);
655 consumed = 0;
656 }
657
658 eqe = lpfc_sli4_eq_get(eq);
659 }
660 eq->EQ_processed += count;
661
662 /* Track the max number of EQEs processed in 1 intr */
663 if (count > eq->EQ_max_eqe)
664 eq->EQ_max_eqe = count;
665
Dick Kennedy164ba8d2020-05-01 14:43:03 -0700666 xchg(&eq->queue_claimed, 0);
James Smart32517fc2019-01-28 11:14:33 -0800667
668rearm_and_exit:
James Smart93a4d6f2019-11-04 16:57:05 -0800669 /* Always clear the EQ. */
670 phba->sli4_hba.sli4_write_eq_db(phba, eq, consumed, rearm);
James Smart32517fc2019-01-28 11:14:33 -0800671
672 return count;
James Smart27d6ac02018-02-22 08:18:42 -0800673}
674
675/**
James Smart4f774512009-05-22 14:52:35 -0400676 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
677 * @q: The Completion Queue to get the first valid CQE from
678 *
679 * This routine will get the first valid Completion Queue Entry from @q, update
680 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
681 * the Queue (no more work to do), or the Queue is full of CQEs that have been
682 * processed, but not popped back to the HBA then this routine will return NULL.
683 **/
684static struct lpfc_cqe *
685lpfc_sli4_cq_get(struct lpfc_queue *q)
686{
687 struct lpfc_cqe *cqe;
688
James Smart2e90f4b2011-12-13 13:22:37 -0500689 /* sanity check on queue memory */
690 if (unlikely(!q))
691 return NULL;
James Smart9afbee32019-03-12 16:30:28 -0700692 cqe = lpfc_sli4_qe(q, q->host_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500693
James Smart4f774512009-05-22 14:52:35 -0400694 /* If the next CQE is not valid then we are done */
James Smart7365f6f2018-02-22 08:18:46 -0800695 if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
James Smart4f774512009-05-22 14:52:35 -0400696 return NULL;
James Smart27f344e2014-05-07 17:16:46 -0400697
698 /*
699 * insert barrier for instruction interlock : data from the hardware
700 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800701 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
702 * instructions allowing action on content before valid bit checked,
703 * add barrier here as well. May not be needed as "content" is a
704 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400705 */
706 mb();
James Smart4f774512009-05-22 14:52:35 -0400707 return cqe;
708}
709
James Smart32517fc2019-01-28 11:14:33 -0800710static void
711__lpfc_sli4_consume_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
712 struct lpfc_cqe *cqe)
713{
714 if (!phba->sli4_hba.pc_sli4_params.cqav)
715 bf_set_le32(lpfc_cqe_valid, cqe, 0);
716
717 cq->host_index = ((cq->host_index + 1) % cq->entry_count);
718
719 /* if the index wrapped around, toggle the valid bit */
720 if (phba->sli4_hba.pc_sli4_params.cqav && !cq->host_index)
721 cq->qe_valid = (cq->qe_valid) ? 0 : 1;
722}
723
James Smart4f774512009-05-22 14:52:35 -0400724/**
James Smart32517fc2019-01-28 11:14:33 -0800725 * lpfc_sli4_write_cq_db - write cq DB for entries consumed or arm state.
726 * @phba: the adapter with the CQ
James Smart4f774512009-05-22 14:52:35 -0400727 * @q: The Completion Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800728 * @count: the number of elements that were consumed
James Smart4f774512009-05-22 14:52:35 -0400729 * @arm: Indicates whether the host wants to arms this CQ.
730 *
James Smart32517fc2019-01-28 11:14:33 -0800731 * This routine will notify the HBA, by ringing the doorbell, that the
732 * CQEs have been processed. The @arm parameter specifies whether the
733 * queue should be rearmed when ringing the doorbell.
James Smart4f774512009-05-22 14:52:35 -0400734 **/
James Smart32517fc2019-01-28 11:14:33 -0800735void
736lpfc_sli4_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
737 uint32_t count, bool arm)
James Smart4f774512009-05-22 14:52:35 -0400738{
James Smart4f774512009-05-22 14:52:35 -0400739 struct lpfc_register doorbell;
740
James Smart2e90f4b2011-12-13 13:22:37 -0500741 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800742 if (unlikely(!q || (count == 0 && !arm)))
743 return;
James Smart4f774512009-05-22 14:52:35 -0400744
745 /* ring doorbell for number popped */
746 doorbell.word0 = 0;
747 if (arm)
748 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800749 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, count);
James Smart4f774512009-05-22 14:52:35 -0400750 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500751 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
752 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
753 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800754 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400755}
756
757/**
James Smart32517fc2019-01-28 11:14:33 -0800758 * lpfc_sli4_if6_write_cq_db - write cq DB for entries consumed or arm state.
759 * @phba: the adapter with the CQ
James Smart27d6ac02018-02-22 08:18:42 -0800760 * @q: The Completion Queue that the host has completed processing for.
James Smart32517fc2019-01-28 11:14:33 -0800761 * @count: the number of elements that were consumed
James Smart27d6ac02018-02-22 08:18:42 -0800762 * @arm: Indicates whether the host wants to arms this CQ.
763 *
James Smart32517fc2019-01-28 11:14:33 -0800764 * This routine will notify the HBA, by ringing the doorbell, that the
765 * CQEs have been processed. The @arm parameter specifies whether the
766 * queue should be rearmed when ringing the doorbell.
James Smart27d6ac02018-02-22 08:18:42 -0800767 **/
James Smart32517fc2019-01-28 11:14:33 -0800768void
769lpfc_sli4_if6_write_cq_db(struct lpfc_hba *phba, struct lpfc_queue *q,
770 uint32_t count, bool arm)
James Smart27d6ac02018-02-22 08:18:42 -0800771{
James Smart27d6ac02018-02-22 08:18:42 -0800772 struct lpfc_register doorbell;
773
774 /* sanity check on queue memory */
James Smart32517fc2019-01-28 11:14:33 -0800775 if (unlikely(!q || (count == 0 && !arm)))
776 return;
James Smart27d6ac02018-02-22 08:18:42 -0800777
778 /* ring doorbell for number popped */
779 doorbell.word0 = 0;
780 if (arm)
781 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
James Smart32517fc2019-01-28 11:14:33 -0800782 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, count);
James Smart27d6ac02018-02-22 08:18:42 -0800783 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
784 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart27d6ac02018-02-22 08:18:42 -0800785}
786
Lee Jones7af29d42020-07-21 17:41:31 +0100787/*
James Smart4f774512009-05-22 14:52:35 -0400788 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
James Smart4f774512009-05-22 14:52:35 -0400789 *
790 * This routine will copy the contents of @wqe to the next available entry on
791 * the @q. This function will then ring the Receive Queue Doorbell to signal the
792 * HBA to start processing the Receive Queue Entry. This function returns the
793 * index that the rqe was copied to if successful. If no entries are available
794 * on @q then this function will return -ENOMEM.
795 * The caller is expected to hold the hbalock when calling this routine.
796 **/
James Smart895427b2017-02-12 13:52:30 -0800797int
James Smart4f774512009-05-22 14:52:35 -0400798lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
799 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
800{
James Smart2e90f4b2011-12-13 13:22:37 -0500801 struct lpfc_rqe *temp_hrqe;
802 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400803 struct lpfc_register doorbell;
James Smartcbc5de12017-12-08 17:18:04 -0800804 int hq_put_index;
805 int dq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400806
James Smart2e90f4b2011-12-13 13:22:37 -0500807 /* sanity check on queue memory */
808 if (unlikely(!hq) || unlikely(!dq))
809 return -ENOMEM;
James Smartcbc5de12017-12-08 17:18:04 -0800810 hq_put_index = hq->host_index;
811 dq_put_index = dq->host_index;
James Smart9afbee32019-03-12 16:30:28 -0700812 temp_hrqe = lpfc_sli4_qe(hq, hq_put_index);
813 temp_drqe = lpfc_sli4_qe(dq, dq_put_index);
James Smart2e90f4b2011-12-13 13:22:37 -0500814
James Smart4f774512009-05-22 14:52:35 -0400815 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
816 return -EINVAL;
James Smartcbc5de12017-12-08 17:18:04 -0800817 if (hq_put_index != dq_put_index)
James Smart4f774512009-05-22 14:52:35 -0400818 return -EINVAL;
819 /* If the host has not yet processed the next entry then we are done */
James Smartcbc5de12017-12-08 17:18:04 -0800820 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400821 return -EBUSY;
James Smart48f8fdb2018-05-04 20:37:51 -0700822 lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
823 lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
James Smart4f774512009-05-22 14:52:35 -0400824
825 /* Update the host index to point to the next slot */
James Smartcbc5de12017-12-08 17:18:04 -0800826 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
827 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700828 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400829
830 /* Ring The Header Receive Queue Doorbell */
James Smart32517fc2019-01-28 11:14:33 -0800831 if (!(hq->host_index % hq->notify_interval)) {
James Smart4f774512009-05-22 14:52:35 -0400832 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500833 if (hq->db_format == LPFC_DB_RING_FORMAT) {
834 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
James Smart32517fc2019-01-28 11:14:33 -0800835 hq->notify_interval);
James Smart962bc512013-01-03 15:44:00 -0500836 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
837 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
838 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
James Smart32517fc2019-01-28 11:14:33 -0800839 hq->notify_interval);
James Smart962bc512013-01-03 15:44:00 -0500840 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
841 hq->host_index);
842 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
843 } else {
844 return -EINVAL;
845 }
846 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400847 }
James Smartcbc5de12017-12-08 17:18:04 -0800848 return hq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400849}
850
Lee Jones7af29d42020-07-21 17:41:31 +0100851/*
James Smart4f774512009-05-22 14:52:35 -0400852 * lpfc_sli4_rq_release - Updates internal hba index for RQ
James Smart4f774512009-05-22 14:52:35 -0400853 *
854 * This routine will update the HBA index of a queue to reflect consumption of
855 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
856 * consumed an entry the host calls this function to update the queue's
857 * internal pointers. This routine returns the number of entries that were
858 * consumed by the HBA.
859 **/
860static uint32_t
861lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
862{
James Smart2e90f4b2011-12-13 13:22:37 -0500863 /* sanity check on queue memory */
864 if (unlikely(!hq) || unlikely(!dq))
865 return 0;
866
James Smart4f774512009-05-22 14:52:35 -0400867 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
868 return 0;
869 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
870 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
871 return 1;
872}
873
James Smarte59058c2008-08-24 21:49:00 -0400874/**
James Smart3621a712009-04-06 18:47:14 -0400875 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400876 * @phba: Pointer to HBA context object.
877 * @pring: Pointer to driver SLI ring object.
878 *
879 * This function returns pointer to next command iocb entry
880 * in the command ring. The caller must hold hbalock to prevent
881 * other threads consume the next command iocb.
882 * SLI-2/SLI-3 provide different sized iocbs.
883 **/
James Smarted957682007-06-17 19:56:37 -0500884static inline IOCB_t *
885lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
886{
James Smart7e56aa22012-08-03 12:35:34 -0400887 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
888 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500889}
890
James Smarte59058c2008-08-24 21:49:00 -0400891/**
James Smart3621a712009-04-06 18:47:14 -0400892 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400893 * @phba: Pointer to HBA context object.
894 * @pring: Pointer to driver SLI ring object.
895 *
896 * This function returns pointer to next response iocb entry
897 * in the response ring. The caller must hold hbalock to make sure
898 * that no other thread consume the next response iocb.
899 * SLI-2/SLI-3 provide different sized iocbs.
900 **/
James Smarted957682007-06-17 19:56:37 -0500901static inline IOCB_t *
902lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
903{
James Smart7e56aa22012-08-03 12:35:34 -0400904 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
905 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500906}
907
James Smarte59058c2008-08-24 21:49:00 -0400908/**
James Smart3621a712009-04-06 18:47:14 -0400909 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400910 * @phba: Pointer to HBA context object.
911 *
912 * This function is called with hbalock held. This function
913 * allocates a new driver iocb object from the iocb pool. If the
914 * allocation is successful, it returns pointer to the newly
915 * allocated iocb object else it returns NULL.
916 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400917struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500918__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400919{
920 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
921 struct lpfc_iocbq * iocbq = NULL;
922
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100923 lockdep_assert_held(&phba->hbalock);
924
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400925 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400926 if (iocbq)
927 phba->iocb_cnt++;
928 if (phba->iocb_cnt > phba->iocb_max)
929 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400930 return iocbq;
931}
932
James Smarte59058c2008-08-24 21:49:00 -0400933/**
James Smartda0436e2009-05-22 14:51:39 -0400934 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
935 * @phba: Pointer to HBA context object.
936 * @xritag: XRI value.
937 *
James Smart3bfab8a2021-04-11 18:31:23 -0700938 * This function clears the sglq pointer from the array of active
James Smartda0436e2009-05-22 14:51:39 -0400939 * sglq's. The xritag that is passed in is used to index into the
940 * array. Before the xritag can be used it needs to be adjusted
941 * by subtracting the xribase.
942 *
943 * Returns sglq ponter = success, NULL = Failure.
944 **/
James Smart895427b2017-02-12 13:52:30 -0800945struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400946__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
947{
James Smartda0436e2009-05-22 14:51:39 -0400948 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400949
950 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
951 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400952 return sglq;
953}
954
955/**
956 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
957 * @phba: Pointer to HBA context object.
958 * @xritag: XRI value.
959 *
James Smart3bfab8a2021-04-11 18:31:23 -0700960 * This function returns the sglq pointer from the array of active
James Smartda0436e2009-05-22 14:51:39 -0400961 * sglq's. The xritag that is passed in is used to index into the
962 * array. Before the xritag can be used it needs to be adjusted
963 * by subtracting the xribase.
964 *
965 * Returns sglq ponter = success, NULL = Failure.
966 **/
James Smart0f65ff62010-02-26 14:14:23 -0500967struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400968__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
969{
James Smartda0436e2009-05-22 14:51:39 -0400970 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400971
972 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400973 return sglq;
974}
975
976/**
James Smart1151e3e2011-02-16 12:39:35 -0500977 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500978 * @phba: Pointer to HBA context object.
979 * @xritag: xri used in this exchange.
980 * @rrq: The RRQ to be cleared.
981 *
James Smart19ca7602010-11-20 23:11:55 -0500982 **/
James Smart1151e3e2011-02-16 12:39:35 -0500983void
984lpfc_clr_rrq_active(struct lpfc_hba *phba,
985 uint16_t xritag,
986 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500987{
James Smart1151e3e2011-02-16 12:39:35 -0500988 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500989
James Smart2693f5d2021-03-01 09:18:03 -0800990 /* Lookup did to verify if did is still active on this vport */
James Smart307e3382020-11-15 11:26:30 -0800991 if (rrq->vport)
James Smart1151e3e2011-02-16 12:39:35 -0500992 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500993
James Smart1151e3e2011-02-16 12:39:35 -0500994 if (!ndlp)
995 goto out;
996
James Smartcff261f2013-12-17 20:29:47 -0500997 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500998 rrq->send_rrq = 0;
999 rrq->xritag = 0;
1000 rrq->rrq_stop_time = 0;
1001 }
James Smart1151e3e2011-02-16 12:39:35 -05001002out:
James Smart19ca7602010-11-20 23:11:55 -05001003 mempool_free(rrq, phba->rrq_pool);
1004}
1005
1006/**
1007 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
1008 * @phba: Pointer to HBA context object.
1009 *
1010 * This function is called with hbalock held. This function
1011 * Checks if stop_time (ratov from setting rrq active) has
1012 * been reached, if it has and the send_rrq flag is set then
1013 * it will call lpfc_send_rrq. If the send_rrq flag is not set
1014 * then it will just call the routine to clear the rrq and
1015 * free the rrq resource.
1016 * The timer is set to the next rrq that is going to expire before
1017 * leaving the routine.
1018 *
1019 **/
1020void
1021lpfc_handle_rrq_active(struct lpfc_hba *phba)
1022{
1023 struct lpfc_node_rrq *rrq;
1024 struct lpfc_node_rrq *nextrrq;
1025 unsigned long next_time;
1026 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -05001027 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -05001028
1029 spin_lock_irqsave(&phba->hbalock, iflags);
1030 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -04001031 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -05001032 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -05001033 &phba->active_rrq_list, list) {
1034 if (time_after(jiffies, rrq->rrq_stop_time))
1035 list_move(&rrq->list, &send_rrq);
1036 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -05001037 next_time = rrq->rrq_stop_time;
1038 }
1039 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -05001040 if ((!list_empty(&phba->active_rrq_list)) &&
1041 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -05001042 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -05001043 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
1044 list_del(&rrq->list);
Bart Van Asscheffd43812019-03-28 11:06:17 -07001045 if (!rrq->send_rrq) {
James Smart1151e3e2011-02-16 12:39:35 -05001046 /* this call will free the rrq */
Bart Van Asscheffd43812019-03-28 11:06:17 -07001047 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1048 } else if (lpfc_send_rrq(phba, rrq)) {
James Smart1151e3e2011-02-16 12:39:35 -05001049 /* if we send the rrq then the completion handler
1050 * will clear the bit in the xribitmap.
1051 */
1052 lpfc_clr_rrq_active(phba, rrq->xritag,
1053 rrq);
1054 }
1055 }
James Smart19ca7602010-11-20 23:11:55 -05001056}
1057
1058/**
1059 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
1060 * @vport: Pointer to vport context object.
1061 * @xri: The xri used in the exchange.
1062 * @did: The targets DID for this exchange.
1063 *
1064 * returns NULL = rrq not found in the phba->active_rrq_list.
1065 * rrq = rrq for this xri and target.
1066 **/
1067struct lpfc_node_rrq *
1068lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
1069{
1070 struct lpfc_hba *phba = vport->phba;
1071 struct lpfc_node_rrq *rrq;
1072 struct lpfc_node_rrq *nextrrq;
1073 unsigned long iflags;
1074
1075 if (phba->sli_rev != LPFC_SLI_REV4)
1076 return NULL;
1077 spin_lock_irqsave(&phba->hbalock, iflags);
1078 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
1079 if (rrq->vport == vport && rrq->xritag == xri &&
1080 rrq->nlp_DID == did){
1081 list_del(&rrq->list);
1082 spin_unlock_irqrestore(&phba->hbalock, iflags);
1083 return rrq;
1084 }
1085 }
1086 spin_unlock_irqrestore(&phba->hbalock, iflags);
1087 return NULL;
1088}
1089
1090/**
1091 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
1092 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -05001093 * @ndlp: Pointer to the lpfc_node_list structure.
1094 * If ndlp is NULL Remove all active RRQs for this vport from the
1095 * phba->active_rrq_list and clear the rrq.
1096 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -05001097 **/
1098void
James Smart1151e3e2011-02-16 12:39:35 -05001099lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -05001100
1101{
1102 struct lpfc_hba *phba = vport->phba;
1103 struct lpfc_node_rrq *rrq;
1104 struct lpfc_node_rrq *nextrrq;
1105 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -05001106 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -05001107
1108 if (phba->sli_rev != LPFC_SLI_REV4)
1109 return;
James Smart1151e3e2011-02-16 12:39:35 -05001110 if (!ndlp) {
1111 lpfc_sli4_vport_delete_els_xri_aborted(vport);
1112 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -05001113 }
James Smart1151e3e2011-02-16 12:39:35 -05001114 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart2693f5d2021-03-01 09:18:03 -08001115 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
1116 if (rrq->vport != vport)
1117 continue;
1118
1119 if (!ndlp || ndlp == lpfc_findnode_did(vport, rrq->nlp_DID))
James Smart1151e3e2011-02-16 12:39:35 -05001120 list_move(&rrq->list, &rrq_list);
James Smart2693f5d2021-03-01 09:18:03 -08001121
1122 }
James Smart19ca7602010-11-20 23:11:55 -05001123 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -05001124
1125 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1126 list_del(&rrq->list);
1127 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1128 }
James Smart19ca7602010-11-20 23:11:55 -05001129}
1130
1131/**
James Smart1151e3e2011-02-16 12:39:35 -05001132 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -05001133 * @phba: Pointer to HBA context object.
1134 * @ndlp: Targets nodelist pointer for this exchange.
Lee Jones7af29d42020-07-21 17:41:31 +01001135 * @xritag: the xri in the bitmap to test.
James Smart19ca7602010-11-20 23:11:55 -05001136 *
James Smarte2a8be52019-05-06 17:26:47 -07001137 * This function returns:
1138 * 0 = rrq not active for this xri
1139 * 1 = rrq is valid for this xri.
James Smart19ca7602010-11-20 23:11:55 -05001140 **/
James Smart1151e3e2011-02-16 12:39:35 -05001141int
1142lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -05001143 uint16_t xritag)
1144{
James Smart19ca7602010-11-20 23:11:55 -05001145 if (!ndlp)
1146 return 0;
James Smartcff261f2013-12-17 20:29:47 -05001147 if (!ndlp->active_rrqs_xri_bitmap)
1148 return 0;
1149 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
Colin Ian King258f84f2019-02-12 15:29:45 +00001150 return 1;
James Smart19ca7602010-11-20 23:11:55 -05001151 else
1152 return 0;
1153}
1154
1155/**
1156 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1157 * @phba: Pointer to HBA context object.
1158 * @ndlp: nodelist pointer for this target.
1159 * @xritag: xri used in this exchange.
1160 * @rxid: Remote Exchange ID.
1161 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1162 *
1163 * This function takes the hbalock.
1164 * The active bit is always set in the active rrq xri_bitmap even
1165 * if there is no slot avaiable for the other rrq information.
1166 *
1167 * returns 0 rrq actived for this xri
1168 * < 0 No memory or invalid ndlp.
1169 **/
1170int
1171lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -05001172 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -05001173{
James Smart19ca7602010-11-20 23:11:55 -05001174 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -05001175 struct lpfc_node_rrq *rrq;
1176 int empty;
1177
1178 if (!ndlp)
1179 return -EINVAL;
1180
1181 if (!phba->cfg_enable_rrq)
1182 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001183
1184 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001185 if (phba->pport->load_flag & FC_UNLOADING) {
1186 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1187 goto out;
1188 }
1189
James Smartb42c07c2012-01-18 16:25:55 -05001190 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1191 goto out;
1192
James Smartcff261f2013-12-17 20:29:47 -05001193 if (!ndlp->active_rrqs_xri_bitmap)
1194 goto out;
1195
1196 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -05001197 goto out;
1198
James Smart19ca7602010-11-20 23:11:55 -05001199 spin_unlock_irqrestore(&phba->hbalock, iflags);
Dick Kennedy9dace1f2020-06-30 14:49:53 -07001200 rrq = mempool_alloc(phba->rrq_pool, GFP_ATOMIC);
James Smartb42c07c2012-01-18 16:25:55 -05001201 if (!rrq) {
1202 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1203 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1204 " DID:0x%x Send:%d\n",
1205 xritag, rxid, ndlp->nlp_DID, send_rrq);
1206 return -EINVAL;
1207 }
James Smarte5771b42013-03-01 16:37:14 -05001208 if (phba->cfg_enable_rrq == 1)
1209 rrq->send_rrq = send_rrq;
1210 else
1211 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -05001212 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -04001213 rrq->rrq_stop_time = jiffies +
1214 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -05001215 rrq->nlp_DID = ndlp->nlp_DID;
1216 rrq->vport = ndlp->vport;
1217 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -05001218 spin_lock_irqsave(&phba->hbalock, iflags);
1219 empty = list_empty(&phba->active_rrq_list);
1220 list_add_tail(&rrq->list, &phba->active_rrq_list);
1221 phba->hba_flag |= HBA_RRQ_ACTIVE;
1222 if (empty)
1223 lpfc_worker_wake_up(phba);
1224 spin_unlock_irqrestore(&phba->hbalock, iflags);
1225 return 0;
1226out:
1227 spin_unlock_irqrestore(&phba->hbalock, iflags);
1228 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1229 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1230 " DID:0x%x Send:%d\n",
1231 xritag, rxid, ndlp->nlp_DID, send_rrq);
1232 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001233}
1234
1235/**
James Smart895427b2017-02-12 13:52:30 -08001236 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -04001237 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01001238 * @piocbq: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -04001239 *
James Smarte2a8be52019-05-06 17:26:47 -07001240 * The driver calls this function with either the nvme ls ring lock
1241 * or the fc els ring lock held depending on the iocb usage. This function
1242 * gets a new driver sglq object from the sglq list. If the list is not empty
1243 * then it is successful, it returns pointer to the newly allocated sglq
1244 * object else it returns NULL.
James Smartda0436e2009-05-22 14:51:39 -04001245 **/
1246static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -08001247__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -04001248{
James Smart895427b2017-02-12 13:52:30 -08001249 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -04001250 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001251 struct lpfc_sglq *start_sglq = NULL;
James Smartc4908502019-01-28 11:14:28 -08001252 struct lpfc_io_buf *lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001253 struct lpfc_nodelist *ndlp;
James Smarte2a8be52019-05-06 17:26:47 -07001254 struct lpfc_sli_ring *pring = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001255 int found = 0;
1256
James Smarte2a8be52019-05-06 17:26:47 -07001257 if (piocbq->iocb_flag & LPFC_IO_NVME_LS)
1258 pring = phba->sli4_hba.nvmels_wq->pring;
1259 else
1260 pring = lpfc_phba_elsring(phba);
1261
1262 lockdep_assert_held(&pring->ring_lock);
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001263
James Smart19ca7602010-11-20 23:11:55 -05001264 if (piocbq->iocb_flag & LPFC_IO_FCP) {
James Smartc4908502019-01-28 11:14:28 -08001265 lpfc_cmd = (struct lpfc_io_buf *) piocbq->context1;
James Smart19ca7602010-11-20 23:11:55 -05001266 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -05001267 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -04001268 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -05001269 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -04001270 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1271 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1272 ndlp = NULL;
1273 else
1274 ndlp = piocbq->context_un.ndlp;
1275 } else {
James Smart19ca7602010-11-20 23:11:55 -05001276 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -04001277 }
James Smart19ca7602010-11-20 23:11:55 -05001278
James Smart895427b2017-02-12 13:52:30 -08001279 spin_lock(&phba->sli4_hba.sgl_list_lock);
1280 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -05001281 start_sglq = sglq;
1282 while (!found) {
1283 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -08001284 break;
James Smart895427b2017-02-12 13:52:30 -08001285 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1286 test_bit(sglq->sli4_lxritag,
1287 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -05001288 /* This xri has an rrq outstanding for this DID.
1289 * put it back in the list and get another xri.
1290 */
James Smart895427b2017-02-12 13:52:30 -08001291 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001292 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -08001293 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -05001294 struct lpfc_sglq, list);
1295 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -07001296 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001297 sglq = NULL;
1298 break;
1299 } else
1300 continue;
1301 }
1302 sglq->ndlp = ndlp;
1303 found = 1;
James Smart6d368e52011-05-24 11:44:12 -04001304 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -05001305 sglq->state = SGL_ALLOCATED;
1306 }
James Smart895427b2017-02-12 13:52:30 -08001307 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001308 return sglq;
1309}
1310
1311/**
James Smartf358dd02017-02-12 13:52:34 -08001312 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1313 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01001314 * @piocbq: Pointer to the iocbq.
James Smartf358dd02017-02-12 13:52:34 -08001315 *
1316 * This function is called with the sgl_list lock held. This function
1317 * gets a new driver sglq object from the sglq list. If the
1318 * list is not empty then it is successful, it returns pointer to the newly
1319 * allocated sglq object else it returns NULL.
1320 **/
1321struct lpfc_sglq *
1322__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1323{
1324 struct list_head *lpfc_nvmet_sgl_list;
1325 struct lpfc_sglq *sglq = NULL;
1326
1327 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1328
1329 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1330
1331 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1332 if (!sglq)
1333 return NULL;
1334 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1335 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001336 return sglq;
1337}
1338
James Smarte59058c2008-08-24 21:49:00 -04001339/**
James Smart3621a712009-04-06 18:47:14 -04001340 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001341 * @phba: Pointer to HBA context object.
1342 *
1343 * This function is called with no lock held. This function
1344 * allocates a new driver iocb object from the iocb pool. If the
1345 * allocation is successful, it returns pointer to the newly
1346 * allocated iocb object else it returns NULL.
1347 **/
James Smart2e0fef82007-06-17 19:56:36 -05001348struct lpfc_iocbq *
1349lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001350{
James Smart2e0fef82007-06-17 19:56:36 -05001351 struct lpfc_iocbq * iocbq = NULL;
1352 unsigned long iflags;
1353
1354 spin_lock_irqsave(&phba->hbalock, iflags);
1355 iocbq = __lpfc_sli_get_iocbq(phba);
1356 spin_unlock_irqrestore(&phba->hbalock, iflags);
1357 return iocbq;
1358}
1359
James Smarte59058c2008-08-24 21:49:00 -04001360/**
James Smart4f774512009-05-22 14:52:35 -04001361 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1362 * @phba: Pointer to HBA context object.
1363 * @iocbq: Pointer to driver iocb object.
1364 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001365 * This function is called to release the driver iocb object
1366 * to the iocb pool. The iotag in the iocb object
James Smart4f774512009-05-22 14:52:35 -04001367 * does not change for each use of the iocb object. This function
1368 * clears all other fields of the iocb object when it is freed.
1369 * The sqlq structure that holds the xritag and phys and virtual
1370 * mappings for the scatter gather list is retrieved from the
1371 * active array of sglq. The get of the sglq pointer also clears
1372 * the entry in the array. If the status of the IO indiactes that
1373 * this IO was aborted then the sglq entry it put on the
1374 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1375 * IO has good status or fails for any other reason then the sglq
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001376 * entry is added to the free list (lpfc_els_sgl_list). The hbalock is
1377 * asserted held in the code path calling this routine.
James Smart4f774512009-05-22 14:52:35 -04001378 **/
1379static void
1380__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1381{
1382 struct lpfc_sglq *sglq;
1383 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001384 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001385 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001386
1387 if (iocbq->sli4_xritag == NO_XRI)
1388 sglq = NULL;
1389 else
James Smart6d368e52011-05-24 11:44:12 -04001390 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1391
James Smart0e9bb8d2013-03-01 16:35:12 -05001392
James Smart4f774512009-05-22 14:52:35 -04001393 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001394 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1395 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1396 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001397 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001398 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001399 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001400 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1401 spin_unlock_irqrestore(
1402 &phba->sli4_hba.sgl_list_lock, iflag);
1403 goto out;
1404 }
1405
James Smart4f774512009-05-22 14:52:35 -04001406 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1407 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001408 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1409 iflag);
James Smart307e3382020-11-15 11:26:30 -08001410
1411 /* Check if we can get a reference on ndlp */
1412 if (sglq->ndlp && !lpfc_nlp_get(sglq->ndlp))
1413 sglq->ndlp = NULL;
1414
James Smart341af102010-01-26 23:07:37 -05001415 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001416 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001417 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001418 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001419 } else {
James Smart895427b2017-02-12 13:52:30 -08001420 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1421 iflag);
James Smart4f774512009-05-22 14:52:35 -04001422 sglq->state = SGL_FREED;
1423 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001424 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001425 &phba->sli4_hba.lpfc_els_sgl_list);
1426 spin_unlock_irqrestore(
1427 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart9628aac2021-03-01 09:18:16 -08001428 pring = lpfc_phba_elsring(phba);
James Smart2a9bf3d2010-06-07 15:24:45 -04001429 /* Check if TXQ queue needs to be serviced */
James Smart9628aac2021-03-01 09:18:16 -08001430 if (pring && (!list_empty(&pring->txq)))
James Smart2a9bf3d2010-06-07 15:24:45 -04001431 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001432 }
James Smart4f774512009-05-22 14:52:35 -04001433 }
1434
James Smartf358dd02017-02-12 13:52:34 -08001435out:
James Smart4f774512009-05-22 14:52:35 -04001436 /*
1437 * Clean all volatile data fields, preserve iotag and node struct.
1438 */
1439 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001440 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001441 iocbq->sli4_xritag = NO_XRI;
James Smart02243832021-08-16 09:28:54 -07001442 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET | LPFC_IO_CMF |
James Smartf358dd02017-02-12 13:52:34 -08001443 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001444 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1445}
1446
James Smart2a9bf3d2010-06-07 15:24:45 -04001447
James Smart4f774512009-05-22 14:52:35 -04001448/**
James Smart3772a992009-05-22 14:50:54 -04001449 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1450 * @phba: Pointer to HBA context object.
1451 * @iocbq: Pointer to driver iocb object.
1452 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001453 * This function is called to release the driver iocb object to the
1454 * iocb pool. The iotag in the iocb object does not change for each
1455 * use of the iocb object. This function clears all other fields of
1456 * the iocb object when it is freed. The hbalock is asserted held in
1457 * the code path calling this routine.
James Smart3772a992009-05-22 14:50:54 -04001458 **/
1459static void
1460__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1461{
1462 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1463
James Smart0e9bb8d2013-03-01 16:35:12 -05001464 /*
James Smart3772a992009-05-22 14:50:54 -04001465 * Clean all volatile data fields, preserve iotag and node struct.
1466 */
1467 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1468 iocbq->sli4_xritag = NO_XRI;
1469 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1470}
1471
1472/**
James Smart3621a712009-04-06 18:47:14 -04001473 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001474 * @phba: Pointer to HBA context object.
1475 * @iocbq: Pointer to driver iocb object.
1476 *
1477 * This function is called with hbalock held to release driver
1478 * iocb object to the iocb pool. The iotag in the iocb object
1479 * does not change for each use of the iocb object. This function
1480 * clears all other fields of the iocb object when it is freed.
1481 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001482static void
James Smart2e0fef82007-06-17 19:56:36 -05001483__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1484{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001485 lockdep_assert_held(&phba->hbalock);
1486
James Smart3772a992009-05-22 14:50:54 -04001487 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001488 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001489}
1490
James Smarte59058c2008-08-24 21:49:00 -04001491/**
James Smart3621a712009-04-06 18:47:14 -04001492 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001493 * @phba: Pointer to HBA context object.
1494 * @iocbq: Pointer to driver iocb object.
1495 *
1496 * This function is called with no lock held to release the iocb to
1497 * iocb pool.
1498 **/
James Smart2e0fef82007-06-17 19:56:36 -05001499void
1500lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1501{
1502 unsigned long iflags;
1503
1504 /*
1505 * Clean all volatile data fields, preserve iotag and node struct.
1506 */
1507 spin_lock_irqsave(&phba->hbalock, iflags);
1508 __lpfc_sli_release_iocbq(phba, iocbq);
1509 spin_unlock_irqrestore(&phba->hbalock, iflags);
1510}
1511
James Smarte59058c2008-08-24 21:49:00 -04001512/**
James Smarta257bf92009-04-06 18:48:10 -04001513 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1514 * @phba: Pointer to HBA context object.
1515 * @iocblist: List of IOCBs.
1516 * @ulpstatus: ULP status in IOCB command field.
1517 * @ulpWord4: ULP word-4 in IOCB command field.
1518 *
1519 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1520 * on the list by invoking the complete callback function associated with the
1521 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1522 * fields.
1523 **/
1524void
1525lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1526 uint32_t ulpstatus, uint32_t ulpWord4)
1527{
1528 struct lpfc_iocbq *piocb;
1529
1530 while (!list_empty(iocblist)) {
1531 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smart9ec58ec2021-01-04 10:02:35 -08001532 if (piocb->wqe_cmpl) {
James Smart84f2ddf2019-08-14 16:56:55 -07001533 if (piocb->iocb_flag & LPFC_IO_NVME)
James Smart9ec58ec2021-01-04 10:02:35 -08001534 lpfc_nvme_cancel_iocb(phba, piocb,
1535 ulpstatus, ulpWord4);
James Smart84f2ddf2019-08-14 16:56:55 -07001536 else
1537 lpfc_sli_release_iocbq(phba, piocb);
James Smart9ec58ec2021-01-04 10:02:35 -08001538
1539 } else if (piocb->iocb_cmpl) {
James Smarta257bf92009-04-06 18:48:10 -04001540 piocb->iocb.ulpStatus = ulpstatus;
1541 piocb->iocb.un.ulpWord[4] = ulpWord4;
1542 (piocb->iocb_cmpl) (phba, piocb, piocb);
James Smart9ec58ec2021-01-04 10:02:35 -08001543 } else {
1544 lpfc_sli_release_iocbq(phba, piocb);
James Smarta257bf92009-04-06 18:48:10 -04001545 }
1546 }
1547 return;
1548}
1549
1550/**
James Smart3621a712009-04-06 18:47:14 -04001551 * lpfc_sli_iocb_cmd_type - Get the iocb type
1552 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001553 *
1554 * This function is called by ring event handler function to get the iocb type.
1555 * This function translates the iocb command to an iocb command type used to
1556 * decide the final disposition of each completed IOCB.
1557 * The function returns
1558 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1559 * LPFC_SOL_IOCB if it is a solicited iocb completion
1560 * LPFC_ABORT_IOCB if it is an abort iocb
1561 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1562 *
1563 * The caller is not required to hold any lock.
1564 **/
dea31012005-04-17 16:05:31 -05001565static lpfc_iocb_type
1566lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1567{
1568 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1569
1570 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1571 return 0;
1572
1573 switch (iocb_cmnd) {
1574 case CMD_XMIT_SEQUENCE_CR:
1575 case CMD_XMIT_SEQUENCE_CX:
1576 case CMD_XMIT_BCAST_CN:
1577 case CMD_XMIT_BCAST_CX:
1578 case CMD_ELS_REQUEST_CR:
1579 case CMD_ELS_REQUEST_CX:
1580 case CMD_CREATE_XRI_CR:
1581 case CMD_CREATE_XRI_CX:
1582 case CMD_GET_RPI_CN:
1583 case CMD_XMIT_ELS_RSP_CX:
1584 case CMD_GET_RPI_CR:
1585 case CMD_FCP_IWRITE_CR:
1586 case CMD_FCP_IWRITE_CX:
1587 case CMD_FCP_IREAD_CR:
1588 case CMD_FCP_IREAD_CX:
1589 case CMD_FCP_ICMND_CR:
1590 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001591 case CMD_FCP_TSEND_CX:
1592 case CMD_FCP_TRSP_CX:
1593 case CMD_FCP_TRECEIVE_CX:
1594 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001595 case CMD_ADAPTER_MSG:
1596 case CMD_ADAPTER_DUMP:
1597 case CMD_XMIT_SEQUENCE64_CR:
1598 case CMD_XMIT_SEQUENCE64_CX:
1599 case CMD_XMIT_BCAST64_CN:
1600 case CMD_XMIT_BCAST64_CX:
1601 case CMD_ELS_REQUEST64_CR:
1602 case CMD_ELS_REQUEST64_CX:
1603 case CMD_FCP_IWRITE64_CR:
1604 case CMD_FCP_IWRITE64_CX:
1605 case CMD_FCP_IREAD64_CR:
1606 case CMD_FCP_IREAD64_CX:
1607 case CMD_FCP_ICMND64_CR:
1608 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001609 case CMD_FCP_TSEND64_CX:
1610 case CMD_FCP_TRSP64_CX:
1611 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001612 case CMD_GEN_REQUEST64_CR:
1613 case CMD_GEN_REQUEST64_CX:
1614 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001615 case DSSCMD_IWRITE64_CR:
1616 case DSSCMD_IWRITE64_CX:
1617 case DSSCMD_IREAD64_CR:
1618 case DSSCMD_IREAD64_CX:
Dick Kennedyc93764a2020-06-30 14:49:49 -07001619 case CMD_SEND_FRAME:
dea31012005-04-17 16:05:31 -05001620 type = LPFC_SOL_IOCB;
1621 break;
1622 case CMD_ABORT_XRI_CN:
1623 case CMD_ABORT_XRI_CX:
1624 case CMD_CLOSE_XRI_CN:
1625 case CMD_CLOSE_XRI_CX:
1626 case CMD_XRI_ABORTED_CX:
1627 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001628 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001629 type = LPFC_ABORT_IOCB;
1630 break;
1631 case CMD_RCV_SEQUENCE_CX:
1632 case CMD_RCV_ELS_REQ_CX:
1633 case CMD_RCV_SEQUENCE64_CX:
1634 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001635 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001636 case CMD_IOCB_RCV_SEQ64_CX:
1637 case CMD_IOCB_RCV_ELS64_CX:
1638 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001639 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001640 type = LPFC_UNSOL_IOCB;
1641 break;
James Smart3163f722008-02-08 18:50:25 -05001642 case CMD_IOCB_XMIT_MSEQ64_CR:
1643 case CMD_IOCB_XMIT_MSEQ64_CX:
1644 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1645 case CMD_IOCB_RCV_ELS_LIST64_CX:
1646 case CMD_IOCB_CLOSE_EXTENDED_CN:
1647 case CMD_IOCB_ABORT_EXTENDED_CN:
1648 case CMD_IOCB_RET_HBQE64_CN:
1649 case CMD_IOCB_FCP_IBIDIR64_CR:
1650 case CMD_IOCB_FCP_IBIDIR64_CX:
1651 case CMD_IOCB_FCP_ITASKMGT64_CX:
1652 case CMD_IOCB_LOGENTRY_CN:
1653 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1654 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001655 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001656 type = LPFC_UNKNOWN_IOCB;
1657 break;
dea31012005-04-17 16:05:31 -05001658 default:
1659 type = LPFC_UNKNOWN_IOCB;
1660 break;
1661 }
1662
1663 return type;
1664}
1665
James Smarte59058c2008-08-24 21:49:00 -04001666/**
James Smart3621a712009-04-06 18:47:14 -04001667 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001668 * @phba: Pointer to HBA context object.
1669 *
1670 * This function is called from SLI initialization code
1671 * to configure every ring of the HBA's SLI interface. The
1672 * caller is not required to hold any lock. This function issues
1673 * a config_ring mailbox command for each ring.
1674 * This function returns zero if successful else returns a negative
1675 * error code.
1676 **/
dea31012005-04-17 16:05:31 -05001677static int
James Smarted957682007-06-17 19:56:37 -05001678lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001679{
1680 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001681 LPFC_MBOXQ_t *pmb;
1682 MAILBOX_t *pmbox;
1683 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001684
James Smarted957682007-06-17 19:56:37 -05001685 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1686 if (!pmb)
1687 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001688 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001689 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001690 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001691 lpfc_config_ring(phba, i, pmb);
1692 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1693 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001694 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001695 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001696 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1697 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001698 rc, pmbox->mbxCommand,
1699 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001700 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001701 ret = -ENXIO;
1702 break;
dea31012005-04-17 16:05:31 -05001703 }
1704 }
James Smarted957682007-06-17 19:56:37 -05001705 mempool_free(pmb, phba->mbox_mem_pool);
1706 return ret;
dea31012005-04-17 16:05:31 -05001707}
1708
James Smarte59058c2008-08-24 21:49:00 -04001709/**
James Smart3621a712009-04-06 18:47:14 -04001710 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001711 * @phba: Pointer to HBA context object.
1712 * @pring: Pointer to driver SLI ring object.
1713 * @piocb: Pointer to the driver iocb object.
1714 *
James Smarte2a8be52019-05-06 17:26:47 -07001715 * The driver calls this function with the hbalock held for SLI3 ports or
1716 * the ring lock held for SLI4 ports. The function adds the
James Smarte59058c2008-08-24 21:49:00 -04001717 * new iocb to txcmplq of the given ring. This function always returns
1718 * 0. If this function is called for ELS ring, this function checks if
1719 * there is a vport associated with the ELS command. This function also
1720 * starts els_tmofunc timer if this is an ELS command.
1721 **/
dea31012005-04-17 16:05:31 -05001722static int
James Smart2e0fef82007-06-17 19:56:36 -05001723lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1724 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001725{
James Smarte2a8be52019-05-06 17:26:47 -07001726 if (phba->sli_rev == LPFC_SLI_REV4)
1727 lockdep_assert_held(&pring->ring_lock);
1728 else
1729 lockdep_assert_held(&phba->hbalock);
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001730
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001731 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001732
dea31012005-04-17 16:05:31 -05001733 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001734 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08001735 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001736
James Smart92d7f7b2007-06-17 19:56:38 -05001737 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1738 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001739 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1740 BUG_ON(!piocb->vport);
1741 if (!(piocb->vport->load_flag & FC_UNLOADING))
1742 mod_timer(&piocb->vport->els_tmofunc,
1743 jiffies +
1744 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1745 }
dea31012005-04-17 16:05:31 -05001746
James Smart2e0fef82007-06-17 19:56:36 -05001747 return 0;
dea31012005-04-17 16:05:31 -05001748}
1749
James Smarte59058c2008-08-24 21:49:00 -04001750/**
James Smart3621a712009-04-06 18:47:14 -04001751 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001752 * @phba: Pointer to HBA context object.
1753 * @pring: Pointer to driver SLI ring object.
1754 *
1755 * This function is called with hbalock held to get next
1756 * iocb in txq of the given ring. If there is any iocb in
1757 * the txq, the function returns first iocb in the list after
1758 * removing the iocb from the list, else it returns NULL.
1759 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001760struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001761lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001762{
dea31012005-04-17 16:05:31 -05001763 struct lpfc_iocbq *cmd_iocb;
1764
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001765 lockdep_assert_held(&phba->hbalock);
1766
James Smart858c9f62007-06-17 19:56:39 -05001767 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001768 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001769}
1770
James Smarte59058c2008-08-24 21:49:00 -04001771/**
James Smartdaebf932021-08-16 09:28:53 -07001772 * lpfc_cmf_sync_cmpl - Process a CMF_SYNC_WQE cmpl
1773 * @phba: Pointer to HBA context object.
1774 * @cmdiocb: Pointer to driver command iocb object.
1775 * @cmf_cmpl: Pointer to completed WCQE.
1776 *
1777 * This routine will inform the driver of any BW adjustments we need
1778 * to make. These changes will be picked up during the next CMF
1779 * timer interrupt. In addition, any BW changes will be logged
1780 * with LOG_CGN_MGMT.
1781 **/
1782static void
1783lpfc_cmf_sync_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1784 struct lpfc_wcqe_complete *cmf_cmpl)
1785{
1786 union lpfc_wqe128 *wqe;
1787 uint32_t status, info;
James Smart02243832021-08-16 09:28:54 -07001788 uint64_t bw, bwdif, slop;
1789 uint64_t pcent, bwpcent;
James Smartdaebf932021-08-16 09:28:53 -07001790 int asig, afpin, sigcnt, fpincnt;
James Smart02243832021-08-16 09:28:54 -07001791 int wsigmax, wfpinmax, cg, tdp;
1792 char *s;
James Smartdaebf932021-08-16 09:28:53 -07001793
1794 /* First check for error */
1795 status = bf_get(lpfc_wcqe_c_status, cmf_cmpl);
1796 if (status) {
1797 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1798 "6211 CMF_SYNC_WQE Error "
1799 "req_tag x%x status x%x hwstatus x%x "
1800 "tdatap x%x parm x%x\n",
1801 bf_get(lpfc_wcqe_c_request_tag, cmf_cmpl),
1802 bf_get(lpfc_wcqe_c_status, cmf_cmpl),
1803 bf_get(lpfc_wcqe_c_hw_status, cmf_cmpl),
1804 cmf_cmpl->total_data_placed,
1805 cmf_cmpl->parameter);
1806 goto out;
1807 }
1808
1809 /* Gather congestion information on a successful cmpl */
1810 info = cmf_cmpl->parameter;
James Smart02243832021-08-16 09:28:54 -07001811 phba->cmf_active_info = info;
1812
1813 /* See if firmware info count is valid or has changed */
1814 if (info > LPFC_MAX_CMF_INFO || phba->cmf_info_per_interval == info)
1815 info = 0;
1816 else
1817 phba->cmf_info_per_interval = info;
1818
James Smartdaebf932021-08-16 09:28:53 -07001819 tdp = bf_get(lpfc_wcqe_c_cmf_bw, cmf_cmpl);
1820 cg = bf_get(lpfc_wcqe_c_cmf_cg, cmf_cmpl);
1821
1822 /* Get BW requirement from firmware */
1823 bw = (uint64_t)tdp * LPFC_CMF_BLK_SIZE;
1824 if (!bw) {
1825 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1826 "6212 CMF_SYNC_WQE x%x: NULL bw\n",
1827 bf_get(lpfc_wcqe_c_request_tag, cmf_cmpl));
1828 goto out;
1829 }
1830
1831 /* Gather information needed for logging if a BW change is required */
1832 wqe = &cmdiocb->wqe;
1833 asig = bf_get(cmf_sync_asig, &wqe->cmf_sync);
1834 afpin = bf_get(cmf_sync_afpin, &wqe->cmf_sync);
1835 fpincnt = bf_get(cmf_sync_wfpincnt, &wqe->cmf_sync);
1836 sigcnt = bf_get(cmf_sync_wsigcnt, &wqe->cmf_sync);
James Smart02243832021-08-16 09:28:54 -07001837 if (phba->cmf_max_bytes_per_interval != bw ||
1838 (asig || afpin || sigcnt || fpincnt)) {
1839 /* Are we increasing or decreasing BW */
1840 if (phba->cmf_max_bytes_per_interval < bw) {
1841 bwdif = bw - phba->cmf_max_bytes_per_interval;
1842 s = "Increase";
1843 } else {
1844 bwdif = phba->cmf_max_bytes_per_interval - bw;
1845 s = "Decrease";
1846 }
James Smartdaebf932021-08-16 09:28:53 -07001847
James Smart02243832021-08-16 09:28:54 -07001848 /* What is the change percentage */
1849 slop = div_u64(phba->cmf_link_byte_count, 200); /*For rounding*/
1850 pcent = div64_u64(bwdif * 100 + slop,
1851 phba->cmf_link_byte_count);
1852 bwpcent = div64_u64(bw * 100 + slop,
1853 phba->cmf_link_byte_count);
1854 if (asig) {
1855 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1856 "6237 BW Threshold %lld%% (%lld): "
1857 "%lld%% %s: Signal Alarm: cg:%d "
1858 "Info:%u\n",
1859 bwpcent, bw, pcent, s, cg,
1860 phba->cmf_active_info);
1861 } else if (afpin) {
1862 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1863 "6238 BW Threshold %lld%% (%lld): "
1864 "%lld%% %s: FPIN Alarm: cg:%d "
1865 "Info:%u\n",
1866 bwpcent, bw, pcent, s, cg,
1867 phba->cmf_active_info);
1868 } else if (sigcnt) {
1869 wsigmax = bf_get(cmf_sync_wsigmax, &wqe->cmf_sync);
1870 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1871 "6239 BW Threshold %lld%% (%lld): "
1872 "%lld%% %s: Signal Warning: "
1873 "Cnt %d Max %d: cg:%d Info:%u\n",
1874 bwpcent, bw, pcent, s, sigcnt,
1875 wsigmax, cg, phba->cmf_active_info);
1876 } else if (fpincnt) {
1877 wfpinmax = bf_get(cmf_sync_wfpinmax, &wqe->cmf_sync);
1878 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1879 "6240 BW Threshold %lld%% (%lld): "
1880 "%lld%% %s: FPIN Warning: "
1881 "Cnt %d Max %d: cg:%d Info:%u\n",
1882 bwpcent, bw, pcent, s, fpincnt,
1883 wfpinmax, cg, phba->cmf_active_info);
1884 } else {
1885 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1886 "6241 BW Threshold %lld%% (%lld): "
1887 "CMF %lld%% %s: cg:%d Info:%u\n",
1888 bwpcent, bw, pcent, s, cg,
1889 phba->cmf_active_info);
1890 }
1891 } else if (info) {
1892 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1893 "6246 Info Threshold %u\n", info);
1894 }
1895
1896 /* Save BW change to be picked up during next timer interrupt */
1897 phba->cmf_last_sync_bw = bw;
James Smartdaebf932021-08-16 09:28:53 -07001898out:
1899 lpfc_sli_release_iocbq(phba, cmdiocb);
1900}
1901
1902/**
1903 * lpfc_issue_cmf_sync_wqe - Issue a CMF_SYNC_WQE
1904 * @phba: Pointer to HBA context object.
1905 * @ms: ms to set in WQE interval, 0 means use init op
1906 * @total: Total rcv bytes for this interval
1907 *
1908 * This routine is called every CMF timer interrupt. Its purpose is
1909 * to issue a CMF_SYNC_WQE to the firmware to inform it of any events
1910 * that may indicate we have congestion (FPINs or Signals). Upon
1911 * completion, the firmware will indicate any BW restrictions the
1912 * driver may need to take.
1913 **/
1914int
1915lpfc_issue_cmf_sync_wqe(struct lpfc_hba *phba, u32 ms, u64 total)
1916{
1917 union lpfc_wqe128 *wqe;
1918 struct lpfc_iocbq *sync_buf;
1919 unsigned long iflags;
1920 u32 ret_val;
1921 u32 atot, wtot, max;
1922
1923 /* First address any alarm / warning activity */
1924 atot = atomic_xchg(&phba->cgn_sync_alarm_cnt, 0);
1925 wtot = atomic_xchg(&phba->cgn_sync_warn_cnt, 0);
1926
1927 /* ONLY Managed mode will send the CMF_SYNC_WQE to the HBA */
1928 if (phba->cmf_active_mode != LPFC_CFG_MANAGED ||
1929 phba->link_state == LPFC_LINK_DOWN)
1930 return 0;
1931
1932 spin_lock_irqsave(&phba->hbalock, iflags);
1933 sync_buf = __lpfc_sli_get_iocbq(phba);
1934 if (!sync_buf) {
1935 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT,
1936 "6213 No available WQEs for CMF_SYNC_WQE\n");
1937 ret_val = ENOMEM;
1938 goto out_unlock;
1939 }
1940
1941 wqe = &sync_buf->wqe;
1942
1943 /* WQEs are reused. Clear stale data and set key fields to zero */
1944 memset(wqe, 0, sizeof(*wqe));
1945
1946 /* If this is the very first CMF_SYNC_WQE, issue an init operation */
1947 if (!ms) {
1948 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1949 "6441 CMF Init %d - CMF_SYNC_WQE\n",
1950 phba->fc_eventTag);
1951 bf_set(cmf_sync_op, &wqe->cmf_sync, 1); /* 1=init */
1952 bf_set(cmf_sync_interval, &wqe->cmf_sync, LPFC_CMF_INTERVAL);
1953 goto initpath;
1954 }
1955
1956 bf_set(cmf_sync_op, &wqe->cmf_sync, 0); /* 0=recalc */
1957 bf_set(cmf_sync_interval, &wqe->cmf_sync, ms);
1958
1959 /* Check for alarms / warnings */
1960 if (atot) {
1961 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1962 /* We hit an Signal alarm condition */
1963 bf_set(cmf_sync_asig, &wqe->cmf_sync, 1);
1964 } else {
1965 /* We hit a FPIN alarm condition */
1966 bf_set(cmf_sync_afpin, &wqe->cmf_sync, 1);
1967 }
1968 } else if (wtot) {
1969 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY ||
1970 phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1971 /* We hit an Signal warning condition */
1972 max = LPFC_SEC_TO_MSEC / lpfc_fabric_cgn_frequency *
1973 lpfc_acqe_cgn_frequency;
1974 bf_set(cmf_sync_wsigmax, &wqe->cmf_sync, max);
1975 bf_set(cmf_sync_wsigcnt, &wqe->cmf_sync, wtot);
1976 } else {
1977 /* We hit a FPIN warning condition */
1978 bf_set(cmf_sync_wfpinmax, &wqe->cmf_sync, 1);
1979 bf_set(cmf_sync_wfpincnt, &wqe->cmf_sync, 1);
1980 }
1981 }
1982
1983 /* Update total read blocks during previous timer interval */
1984 wqe->cmf_sync.read_bytes = (u32)(total / LPFC_CMF_BLK_SIZE);
1985
1986initpath:
1987 bf_set(cmf_sync_ver, &wqe->cmf_sync, LPFC_CMF_SYNC_VER);
1988 wqe->cmf_sync.event_tag = phba->fc_eventTag;
1989 bf_set(cmf_sync_cmnd, &wqe->cmf_sync, CMD_CMF_SYNC_WQE);
1990
1991 /* Setup reqtag to match the wqe completion. */
1992 bf_set(cmf_sync_reqtag, &wqe->cmf_sync, sync_buf->iotag);
1993
1994 bf_set(cmf_sync_qosd, &wqe->cmf_sync, 1);
1995
1996 bf_set(cmf_sync_cmd_type, &wqe->cmf_sync, CMF_SYNC_COMMAND);
1997 bf_set(cmf_sync_wqec, &wqe->cmf_sync, 1);
1998 bf_set(cmf_sync_cqid, &wqe->cmf_sync, LPFC_WQE_CQ_ID_DEFAULT);
1999
2000 sync_buf->vport = phba->pport;
2001 sync_buf->wqe_cmpl = lpfc_cmf_sync_cmpl;
2002 sync_buf->iocb_cmpl = NULL;
2003 sync_buf->context1 = NULL;
2004 sync_buf->context2 = NULL;
2005 sync_buf->context3 = NULL;
2006 sync_buf->sli4_xritag = NO_XRI;
2007
2008 sync_buf->iocb_flag |= LPFC_IO_CMF;
2009 ret_val = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], sync_buf);
2010 if (ret_val)
2011 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
2012 "6214 Cannot issue CMF_SYNC_WQE: x%x\n",
2013 ret_val);
2014out_unlock:
2015 spin_unlock_irqrestore(&phba->hbalock, iflags);
2016 return ret_val;
2017}
2018
2019/**
James Smart3621a712009-04-06 18:47:14 -04002020 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04002021 * @phba: Pointer to HBA context object.
2022 * @pring: Pointer to driver SLI ring object.
2023 *
2024 * This function is called with hbalock held and the caller must post the
2025 * iocb without releasing the lock. If the caller releases the lock,
2026 * iocb slot returned by the function is not guaranteed to be available.
2027 * The function returns pointer to the next available iocb slot if there
2028 * is available slot in the ring, else it returns NULL.
2029 * If the get index of the ring is ahead of the put index, the function
2030 * will post an error attention event to the worker thread to take the
2031 * HBA to offline state.
2032 **/
dea31012005-04-17 16:05:31 -05002033static IOCB_t *
2034lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2035{
James Smart34b02dc2008-08-24 21:49:55 -04002036 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04002037 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002038
2039 lockdep_assert_held(&phba->hbalock);
2040
James Smart7e56aa22012-08-03 12:35:34 -04002041 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
2042 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
2043 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05002044
James Smart7e56aa22012-08-03 12:35:34 -04002045 if (unlikely(pring->sli.sli3.local_getidx ==
2046 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05002047
James Smart7e56aa22012-08-03 12:35:34 -04002048 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05002049
James Smart7e56aa22012-08-03 12:35:34 -04002050 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002051 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002052 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002053 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002054 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04002055 pring->sli.sli3.local_getidx,
2056 max_cmd_idx);
dea31012005-04-17 16:05:31 -05002057
James Smart2e0fef82007-06-17 19:56:36 -05002058 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002059 /*
2060 * All error attention handlers are posted to
2061 * worker thread
2062 */
2063 phba->work_ha |= HA_ERATT;
2064 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002065
James Smart5e9d9b82008-06-14 22:52:53 -04002066 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002067
2068 return NULL;
2069 }
2070
James Smart7e56aa22012-08-03 12:35:34 -04002071 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05002072 return NULL;
2073 }
2074
James Smarted957682007-06-17 19:56:37 -05002075 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05002076}
2077
James Smarte59058c2008-08-24 21:49:00 -04002078/**
James Smart3621a712009-04-06 18:47:14 -04002079 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04002080 * @phba: Pointer to HBA context object.
2081 * @iocbq: Pointer to driver iocb object.
2082 *
2083 * This function gets an iotag for the iocb. If there is no unused iotag and
2084 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
2085 * array and assigns a new iotag.
2086 * The function returns the allocated iotag if successful, else returns zero.
2087 * Zero is not a valid iotag.
2088 * The caller is not required to hold any lock.
2089 **/
James Bottomley604a3e32005-10-29 10:28:33 -05002090uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05002091lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05002092{
James Smart2e0fef82007-06-17 19:56:36 -05002093 struct lpfc_iocbq **new_arr;
2094 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05002095 size_t new_len;
2096 struct lpfc_sli *psli = &phba->sli;
2097 uint16_t iotag;
dea31012005-04-17 16:05:31 -05002098
James Smart2e0fef82007-06-17 19:56:36 -05002099 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05002100 iotag = psli->last_iotag;
2101 if(++iotag < psli->iocbq_lookup_len) {
2102 psli->last_iotag = iotag;
2103 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05002104 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05002105 iocbq->iotag = iotag;
2106 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05002107 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05002108 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
2109 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05002110 spin_unlock_irq(&phba->hbalock);
Kees Cook6396bb22018-06-12 14:03:40 -07002111 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05002112 GFP_KERNEL);
2113 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05002114 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05002115 old_arr = psli->iocbq_lookup;
2116 if (new_len <= psli->iocbq_lookup_len) {
2117 /* highly unprobable case */
2118 kfree(new_arr);
2119 iotag = psli->last_iotag;
2120 if(++iotag < psli->iocbq_lookup_len) {
2121 psli->last_iotag = iotag;
2122 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05002123 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05002124 iocbq->iotag = iotag;
2125 return iotag;
2126 }
James Smart2e0fef82007-06-17 19:56:36 -05002127 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05002128 return 0;
2129 }
2130 if (psli->iocbq_lookup)
2131 memcpy(new_arr, old_arr,
2132 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04002133 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05002134 psli->iocbq_lookup = new_arr;
2135 psli->iocbq_lookup_len = new_len;
2136 psli->last_iotag = iotag;
2137 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05002138 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05002139 iocbq->iotag = iotag;
2140 kfree(old_arr);
2141 return iotag;
2142 }
James Smart8f6d98d2006-08-01 07:34:00 -04002143 } else
James Smart2e0fef82007-06-17 19:56:36 -05002144 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002145
James Smartbc739052010-08-04 16:11:18 -04002146 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002147 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
2148 psli->last_iotag);
dea31012005-04-17 16:05:31 -05002149
James Bottomley604a3e32005-10-29 10:28:33 -05002150 return 0;
dea31012005-04-17 16:05:31 -05002151}
2152
James Smarte59058c2008-08-24 21:49:00 -04002153/**
James Smart3621a712009-04-06 18:47:14 -04002154 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04002155 * @phba: Pointer to HBA context object.
2156 * @pring: Pointer to driver SLI ring object.
2157 * @iocb: Pointer to iocb slot in the ring.
2158 * @nextiocb: Pointer to driver iocb object which need to be
2159 * posted to firmware.
2160 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07002161 * This function is called to post a new iocb to the firmware. This
2162 * function copies the new iocb to ring iocb slot and updates the
2163 * ring pointers. It adds the new iocb to txcmplq if there is
James Smarte59058c2008-08-24 21:49:00 -04002164 * a completion call back for this iocb else the function will free the
Dick Kennedy88acb4d2020-05-01 14:43:07 -07002165 * iocb object. The hbalock is asserted held in the code path calling
2166 * this routine.
James Smarte59058c2008-08-24 21:49:00 -04002167 **/
dea31012005-04-17 16:05:31 -05002168static void
2169lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2170 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
2171{
2172 /*
James Bottomley604a3e32005-10-29 10:28:33 -05002173 * Set up an iotag
dea31012005-04-17 16:05:31 -05002174 */
James Bottomley604a3e32005-10-29 10:28:33 -05002175 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05002176
James Smarte2a0a9d2008-12-04 22:40:02 -05002177
James Smarta58cbd52007-08-02 11:09:43 -04002178 if (pring->ringno == LPFC_ELS_RING) {
2179 lpfc_debugfs_slow_ring_trc(phba,
2180 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2181 *(((uint32_t *) &nextiocb->iocb) + 4),
2182 *(((uint32_t *) &nextiocb->iocb) + 6),
2183 *(((uint32_t *) &nextiocb->iocb) + 7));
2184 }
2185
dea31012005-04-17 16:05:31 -05002186 /*
2187 * Issue iocb command to adapter
2188 */
James Smart92d7f7b2007-06-17 19:56:38 -05002189 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05002190 wmb();
2191 pring->stats.iocb_cmd++;
2192
2193 /*
2194 * If there is no completion routine to call, we can release the
2195 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
2196 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
2197 */
2198 if (nextiocb->iocb_cmpl)
2199 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05002200 else
James Smart2e0fef82007-06-17 19:56:36 -05002201 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05002202
2203 /*
2204 * Let the HBA know what IOCB slot will be the next one the
2205 * driver will put a command into.
2206 */
James Smart7e56aa22012-08-03 12:35:34 -04002207 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
2208 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05002209}
2210
James Smarte59058c2008-08-24 21:49:00 -04002211/**
James Smart3621a712009-04-06 18:47:14 -04002212 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04002213 * @phba: Pointer to HBA context object.
2214 * @pring: Pointer to driver SLI ring object.
2215 *
2216 * The caller is not required to hold any lock for calling this function.
2217 * This function updates the chip attention bits for the ring to inform firmware
2218 * that there are pending work to be done for this ring and requests an
2219 * interrupt when there is space available in the ring. This function is
2220 * called when the driver is unable to post more iocbs to the ring due
2221 * to unavailability of space in the ring.
2222 **/
dea31012005-04-17 16:05:31 -05002223static void
James Smart2e0fef82007-06-17 19:56:36 -05002224lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05002225{
2226 int ringno = pring->ringno;
2227
2228 pring->flag |= LPFC_CALL_RING_AVAILABLE;
2229
2230 wmb();
2231
2232 /*
2233 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
2234 * The HBA will tell us when an IOCB entry is available.
2235 */
2236 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
2237 readl(phba->CAregaddr); /* flush */
2238
2239 pring->stats.iocb_cmd_full++;
2240}
2241
James Smarte59058c2008-08-24 21:49:00 -04002242/**
James Smart3621a712009-04-06 18:47:14 -04002243 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04002244 * @phba: Pointer to HBA context object.
2245 * @pring: Pointer to driver SLI ring object.
2246 *
2247 * This function updates the chip attention register bit for the
2248 * given ring to inform HBA that there is more work to be done
2249 * in this ring. The caller is not required to hold any lock.
2250 **/
dea31012005-04-17 16:05:31 -05002251static void
James Smart2e0fef82007-06-17 19:56:36 -05002252lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05002253{
2254 int ringno = pring->ringno;
2255
2256 /*
2257 * Tell the HBA that there is work to do in this ring.
2258 */
James Smart34b02dc2008-08-24 21:49:55 -04002259 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
2260 wmb();
2261 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
2262 readl(phba->CAregaddr); /* flush */
2263 }
dea31012005-04-17 16:05:31 -05002264}
2265
James Smarte59058c2008-08-24 21:49:00 -04002266/**
James Smart3621a712009-04-06 18:47:14 -04002267 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04002268 * @phba: Pointer to HBA context object.
2269 * @pring: Pointer to driver SLI ring object.
2270 *
2271 * This function is called with hbalock held to post pending iocbs
2272 * in the txq to the firmware. This function is called when driver
2273 * detects space available in the ring.
2274 **/
dea31012005-04-17 16:05:31 -05002275static void
James Smart2e0fef82007-06-17 19:56:36 -05002276lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05002277{
2278 IOCB_t *iocb;
2279 struct lpfc_iocbq *nextiocb;
2280
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002281 lockdep_assert_held(&phba->hbalock);
2282
dea31012005-04-17 16:05:31 -05002283 /*
2284 * Check to see if:
2285 * (a) there is anything on the txq to send
2286 * (b) link is up
2287 * (c) link attention events can be processed (fcp ring only)
2288 * (d) IOCB processing is not blocked by the outstanding mbox command.
2289 */
James Smart0e9bb8d2013-03-01 16:35:12 -05002290
2291 if (lpfc_is_link_up(phba) &&
2292 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08002293 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04002294 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05002295
2296 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2297 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
2298 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2299
2300 if (iocb)
2301 lpfc_sli_update_ring(phba, pring);
2302 else
2303 lpfc_sli_update_full_ring(phba, pring);
2304 }
2305
2306 return;
2307}
2308
James Smarte59058c2008-08-24 21:49:00 -04002309/**
James Smart3621a712009-04-06 18:47:14 -04002310 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002311 * @phba: Pointer to HBA context object.
2312 * @hbqno: HBQ number.
2313 *
2314 * This function is called with hbalock held to get the next
2315 * available slot for the given HBQ. If there is free slot
2316 * available for the HBQ it will return pointer to the next available
2317 * HBQ entry else it will return NULL.
2318 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002319static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05002320lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
2321{
2322 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2323
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002324 lockdep_assert_held(&phba->hbalock);
2325
James Smarted957682007-06-17 19:56:37 -05002326 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
2327 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
2328 hbqp->next_hbqPutIdx = 0;
2329
2330 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002331 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05002332 uint32_t getidx = le32_to_cpu(raw_index);
2333
2334 hbqp->local_hbqGetIdx = getidx;
2335
2336 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002337 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002338 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05002339 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04002340 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05002341 hbqp->entry_count);
2342
2343 phba->link_state = LPFC_HBA_ERROR;
2344 return NULL;
2345 }
2346
2347 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
2348 return NULL;
2349 }
2350
James Smart51ef4c22007-08-02 11:10:31 -04002351 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
2352 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05002353}
2354
James Smarte59058c2008-08-24 21:49:00 -04002355/**
James Smart3621a712009-04-06 18:47:14 -04002356 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04002357 * @phba: Pointer to HBA context object.
2358 *
2359 * This function is called with no lock held to free all the
2360 * hbq buffers while uninitializing the SLI interface. It also
2361 * frees the HBQ buffers returned by the firmware but not yet
2362 * processed by the upper layers.
2363 **/
James Smarted957682007-06-17 19:56:37 -05002364void
2365lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
2366{
James Smart92d7f7b2007-06-17 19:56:38 -05002367 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2368 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05002369 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04002370 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05002371
James Smart51ef4c22007-08-02 11:10:31 -04002372 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05002373 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05002374 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04002375 for (i = 0; i < hbq_count; ++i) {
2376 list_for_each_entry_safe(dmabuf, next_dmabuf,
2377 &phba->hbqs[i].hbq_buffer_list, list) {
2378 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
2379 list_del(&hbq_buf->dbuf.list);
2380 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
2381 }
James Smarta8adb832007-10-27 13:37:53 -04002382 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05002383 }
James Smart3163f722008-02-08 18:50:25 -05002384
2385 /* Mark the HBQs not in use */
2386 phba->hbq_in_use = 0;
2387 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05002388}
2389
James Smarte59058c2008-08-24 21:49:00 -04002390/**
James Smart3621a712009-04-06 18:47:14 -04002391 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002392 * @phba: Pointer to HBA context object.
2393 * @hbqno: HBQ number.
2394 * @hbq_buf: Pointer to HBQ buffer.
2395 *
2396 * This function is called with the hbalock held to post a
2397 * hbq buffer to the firmware. If the function finds an empty
2398 * slot in the HBQ, it will post the buffer. The function will return
2399 * pointer to the hbq entry if it successfully post the buffer
2400 * else it will return NULL.
2401 **/
James Smart3772a992009-05-22 14:50:54 -04002402static int
James Smarted957682007-06-17 19:56:37 -05002403lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05002404 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05002405{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002406 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002407 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2408}
2409
2410/**
2411 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2412 * @phba: Pointer to HBA context object.
2413 * @hbqno: HBQ number.
2414 * @hbq_buf: Pointer to HBQ buffer.
2415 *
2416 * This function is called with the hbalock held to post a hbq buffer to the
2417 * firmware. If the function finds an empty slot in the HBQ, it will post the
2418 * buffer and place it on the hbq_buffer_list. The function will return zero if
2419 * it successfully post the buffer else it will return an error.
2420 **/
2421static int
2422lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2423 struct hbq_dmabuf *hbq_buf)
2424{
James Smarted957682007-06-17 19:56:37 -05002425 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002426 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002427
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002428 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002429 /* Get next HBQ entry slot to use */
2430 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2431 if (hbqe) {
2432 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2433
James Smart92d7f7b2007-06-17 19:56:38 -05002434 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2435 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002436 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002437 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002438 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2439 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2440 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002441 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2442 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002443 /* flush */
James Smarted957682007-06-17 19:56:37 -05002444 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002445 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002446 return 0;
2447 } else
2448 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002449}
2450
James Smart4f774512009-05-22 14:52:35 -04002451/**
2452 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2453 * @phba: Pointer to HBA context object.
2454 * @hbqno: HBQ number.
2455 * @hbq_buf: Pointer to HBQ buffer.
2456 *
2457 * This function is called with the hbalock held to post an RQE to the SLI4
2458 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2459 * the hbq_buffer_list and return zero, otherwise it will return an error.
2460 **/
2461static int
2462lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2463 struct hbq_dmabuf *hbq_buf)
2464{
2465 int rc;
2466 struct lpfc_rqe hrqe;
2467 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002468 struct lpfc_queue *hrq;
2469 struct lpfc_queue *drq;
2470
2471 if (hbqno != LPFC_ELS_HBQ)
2472 return 1;
2473 hrq = phba->sli4_hba.hdr_rq;
2474 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002475
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002476 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002477 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2478 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2479 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2480 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002481 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002482 if (rc < 0)
2483 return rc;
James Smart895427b2017-02-12 13:52:30 -08002484 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002485 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2486 return 0;
2487}
2488
James Smarte59058c2008-08-24 21:49:00 -04002489/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002490static struct lpfc_hbq_init lpfc_els_hbq = {
2491 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002492 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002493 .mask_count = 0,
2494 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002495 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002496 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002497 .init_count = 40,
2498 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002499};
James Smarted957682007-06-17 19:56:37 -05002500
James Smarte59058c2008-08-24 21:49:00 -04002501/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002502struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002503 &lpfc_els_hbq,
2504};
2505
James Smarte59058c2008-08-24 21:49:00 -04002506/**
James Smart3621a712009-04-06 18:47:14 -04002507 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002508 * @phba: Pointer to HBA context object.
2509 * @hbqno: HBQ number.
2510 * @count: Number of HBQ buffers to be posted.
2511 *
James Smartd7c255b2008-08-24 21:50:00 -04002512 * This function is called with no lock held to post more hbq buffers to the
2513 * given HBQ. The function returns the number of HBQ buffers successfully
2514 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002515 **/
James Smart311464e2007-08-02 11:10:37 -04002516static int
James Smart92d7f7b2007-06-17 19:56:38 -05002517lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2518{
James Smartd7c255b2008-08-24 21:50:00 -04002519 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002520 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002521 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002522 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002523 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002524 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002525
James Smartd7c255b2008-08-24 21:50:00 -04002526 if ((phba->hbqs[hbqno].buffer_count + count) >
2527 lpfc_hbq_defs[hbqno]->entry_count)
2528 count = lpfc_hbq_defs[hbqno]->entry_count -
2529 phba->hbqs[hbqno].buffer_count;
2530 if (!count)
2531 return 0;
2532 /* Allocate HBQ entries */
2533 for (i = 0; i < count; i++) {
2534 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2535 if (!hbq_buffer)
2536 break;
2537 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2538 }
James Smart3163f722008-02-08 18:50:25 -05002539 /* Check whether HBQ is still in use */
2540 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002541 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002542 goto err;
2543 while (!list_empty(&hbq_buf_list)) {
2544 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2545 dbuf.list);
2546 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2547 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002548 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002549 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002550 posted++;
2551 } else
James Smart51ef4c22007-08-02 11:10:31 -04002552 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002553 }
James Smart3163f722008-02-08 18:50:25 -05002554 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002555 return posted;
2556err:
2557 spin_unlock_irqrestore(&phba->hbalock, flags);
2558 while (!list_empty(&hbq_buf_list)) {
2559 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2560 dbuf.list);
2561 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2562 }
James Smart92d7f7b2007-06-17 19:56:38 -05002563 return 0;
James Smarted957682007-06-17 19:56:37 -05002564}
2565
James Smarte59058c2008-08-24 21:49:00 -04002566/**
James Smart3621a712009-04-06 18:47:14 -04002567 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002568 * @phba: Pointer to HBA context object.
2569 * @qno: HBQ number.
2570 *
2571 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002572 * is called with no lock held. The function returns the number of HBQ entries
2573 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002574 **/
James Smarted957682007-06-17 19:56:37 -05002575int
James Smart92d7f7b2007-06-17 19:56:38 -05002576lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002577{
James Smartdef9c7a2009-12-21 17:02:28 -05002578 if (phba->sli_rev == LPFC_SLI_REV4)
2579 return 0;
2580 else
2581 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2582 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002583}
2584
James Smarte59058c2008-08-24 21:49:00 -04002585/**
James Smart3621a712009-04-06 18:47:14 -04002586 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002587 * @phba: Pointer to HBA context object.
2588 * @qno: HBQ queue number.
2589 *
2590 * This function is called from SLI initialization code path with
2591 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002592 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002593 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002594static int
James Smart92d7f7b2007-06-17 19:56:38 -05002595lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002596{
James Smartdef9c7a2009-12-21 17:02:28 -05002597 if (phba->sli_rev == LPFC_SLI_REV4)
2598 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002599 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002600 else
2601 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2602 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002603}
2604
Lee Jones7af29d42020-07-21 17:41:31 +01002605/*
James Smart3772a992009-05-22 14:50:54 -04002606 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
James Smart3772a992009-05-22 14:50:54 -04002607 *
2608 * This function removes the first hbq buffer on an hbq list and returns a
2609 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2610 **/
2611static struct hbq_dmabuf *
2612lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2613{
2614 struct lpfc_dmabuf *d_buf;
2615
2616 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2617 if (!d_buf)
2618 return NULL;
2619 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2620}
2621
2622/**
James Smart2d7dbc42017-02-12 13:52:35 -08002623 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2624 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01002625 * @hrq: HBQ number.
James Smart2d7dbc42017-02-12 13:52:35 -08002626 *
2627 * This function removes the first RQ buffer on an RQ buffer list and returns a
2628 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2629 **/
2630static struct rqb_dmabuf *
2631lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2632{
2633 struct lpfc_dmabuf *h_buf;
2634 struct lpfc_rqb *rqbp;
2635
2636 rqbp = hrq->rqbp;
2637 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2638 struct lpfc_dmabuf, list);
2639 if (!h_buf)
2640 return NULL;
2641 rqbp->buffer_count--;
2642 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2643}
2644
2645/**
James Smart3621a712009-04-06 18:47:14 -04002646 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002647 * @phba: Pointer to HBA context object.
2648 * @tag: Tag of the hbq buffer.
2649 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002650 * This function searches for the hbq buffer associated with the given tag in
2651 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2652 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002653 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002654static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002655lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2656{
James Smart92d7f7b2007-06-17 19:56:38 -05002657 struct lpfc_dmabuf *d_buf;
2658 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002659 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002660
James Smart51ef4c22007-08-02 11:10:31 -04002661 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002662 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002663 return NULL;
2664
James Smart3772a992009-05-22 14:50:54 -04002665 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002666 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002667 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002668 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002669 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002670 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002671 }
2672 }
James Smart3772a992009-05-22 14:50:54 -04002673 spin_unlock_irq(&phba->hbalock);
Dick Kennedy372c1872020-06-30 14:50:00 -07002674 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002675 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002676 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002677 return NULL;
James Smarted957682007-06-17 19:56:37 -05002678}
2679
James Smarte59058c2008-08-24 21:49:00 -04002680/**
James Smart3621a712009-04-06 18:47:14 -04002681 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002682 * @phba: Pointer to HBA context object.
2683 * @hbq_buffer: Pointer to HBQ buffer.
2684 *
2685 * This function is called with hbalock. This function gives back
2686 * the hbq buffer to firmware. If the HBQ does not have space to
2687 * post the buffer, it will free the buffer.
2688 **/
James Smarted957682007-06-17 19:56:37 -05002689void
James Smart51ef4c22007-08-02 11:10:31 -04002690lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002691{
2692 uint32_t hbqno;
2693
James Smart51ef4c22007-08-02 11:10:31 -04002694 if (hbq_buffer) {
2695 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002696 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002697 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002698 }
2699}
2700
James Smarte59058c2008-08-24 21:49:00 -04002701/**
James Smart3621a712009-04-06 18:47:14 -04002702 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002703 * @mbxCommand: mailbox command code.
2704 *
2705 * This function is called by the mailbox event handler function to verify
2706 * that the completed mailbox command is a legitimate mailbox command. If the
2707 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2708 * and the mailbox event handler will take the HBA offline.
2709 **/
dea31012005-04-17 16:05:31 -05002710static int
2711lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2712{
2713 uint8_t ret;
2714
2715 switch (mbxCommand) {
2716 case MBX_LOAD_SM:
2717 case MBX_READ_NV:
2718 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002719 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002720 case MBX_RUN_BIU_DIAG:
2721 case MBX_INIT_LINK:
2722 case MBX_DOWN_LINK:
2723 case MBX_CONFIG_LINK:
2724 case MBX_CONFIG_RING:
2725 case MBX_RESET_RING:
2726 case MBX_READ_CONFIG:
2727 case MBX_READ_RCONFIG:
2728 case MBX_READ_SPARM:
2729 case MBX_READ_STATUS:
2730 case MBX_READ_RPI:
2731 case MBX_READ_XRI:
2732 case MBX_READ_REV:
2733 case MBX_READ_LNK_STAT:
2734 case MBX_REG_LOGIN:
2735 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002736 case MBX_CLEAR_LA:
2737 case MBX_DUMP_MEMORY:
2738 case MBX_DUMP_CONTEXT:
2739 case MBX_RUN_DIAGS:
2740 case MBX_RESTART:
2741 case MBX_UPDATE_CFG:
2742 case MBX_DOWN_LOAD:
2743 case MBX_DEL_LD_ENTRY:
2744 case MBX_RUN_PROGRAM:
2745 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002746 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002747 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002748 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002749 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002750 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002751 case MBX_LOAD_AREA:
2752 case MBX_RUN_BIU_DIAG64:
2753 case MBX_CONFIG_PORT:
2754 case MBX_READ_SPARM64:
2755 case MBX_READ_RPI64:
2756 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002757 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002758 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002759 case MBX_SET_DEBUG:
2760 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002761 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002762 case MBX_REG_VPI:
2763 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002764 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002765 case MBX_PORT_CAPABILITIES:
2766 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002767 case MBX_SLI4_CONFIG:
2768 case MBX_SLI4_REQ_FTRS:
2769 case MBX_REG_FCFI:
2770 case MBX_UNREG_FCFI:
2771 case MBX_REG_VFI:
2772 case MBX_UNREG_VFI:
2773 case MBX_INIT_VPI:
2774 case MBX_INIT_VFI:
2775 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002776 case MBX_READ_EVENT_LOG_STATUS:
2777 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002778 case MBX_SECURITY_MGMT:
2779 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002780 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002781 ret = mbxCommand;
2782 break;
2783 default:
2784 ret = MBX_SHUTDOWN;
2785 break;
2786 }
James Smart2e0fef82007-06-17 19:56:36 -05002787 return ret;
dea31012005-04-17 16:05:31 -05002788}
James Smarte59058c2008-08-24 21:49:00 -04002789
2790/**
James Smart3621a712009-04-06 18:47:14 -04002791 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002792 * @phba: Pointer to HBA context object.
2793 * @pmboxq: Pointer to mailbox command.
2794 *
2795 * This is completion handler function for mailbox commands issued from
2796 * lpfc_sli_issue_mbox_wait function. This function is called by the
2797 * mailbox event handler function with no lock held. This function
2798 * will wake up thread waiting on the wait queue pointed by context1
2799 * of the mailbox.
2800 **/
James Smart04c68492009-05-22 14:52:52 -04002801void
James Smart2e0fef82007-06-17 19:56:36 -05002802lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002803{
James Smart858c9f62007-06-17 19:56:39 -05002804 unsigned long drvr_flag;
James Smarte29d74f2018-03-05 12:04:07 -08002805 struct completion *pmbox_done;
dea31012005-04-17 16:05:31 -05002806
2807 /*
James Smarte29d74f2018-03-05 12:04:07 -08002808 * If pmbox_done is empty, the driver thread gave up waiting and
dea31012005-04-17 16:05:31 -05002809 * continued running.
2810 */
James Smart7054a602007-04-25 09:52:34 -04002811 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002812 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarte29d74f2018-03-05 12:04:07 -08002813 pmbox_done = (struct completion *)pmboxq->context3;
2814 if (pmbox_done)
2815 complete(pmbox_done);
James Smart858c9f62007-06-17 19:56:39 -05002816 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002817 return;
2818}
2819
James Smartb95b2112019-08-14 16:56:47 -07002820static void
2821__lpfc_sli_rpi_release(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2822{
2823 unsigned long iflags;
2824
2825 if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
2826 lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
James Smartc6adba12020-11-15 11:26:34 -08002827 spin_lock_irqsave(&ndlp->lock, iflags);
James Smartb95b2112019-08-14 16:56:47 -07002828 ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
2829 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
James Smartc6adba12020-11-15 11:26:34 -08002830 spin_unlock_irqrestore(&ndlp->lock, iflags);
James Smartb95b2112019-08-14 16:56:47 -07002831 }
2832 ndlp->nlp_flag &= ~NLP_UNREG_INP;
2833}
James Smarte59058c2008-08-24 21:49:00 -04002834
2835/**
James Smart3621a712009-04-06 18:47:14 -04002836 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002837 * @phba: Pointer to HBA context object.
2838 * @pmb: Pointer to mailbox object.
2839 *
2840 * This function is the default mailbox completion handler. It
2841 * frees the memory resources associated with the completed mailbox
2842 * command. If the completed command is a REG_LOGIN mailbox command,
2843 * this function will issue a UREG_LOGIN to re-claim the RPI.
2844 **/
dea31012005-04-17 16:05:31 -05002845void
James Smart2e0fef82007-06-17 19:56:36 -05002846lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002847{
James Smartd439d282010-09-29 11:18:45 -04002848 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002849 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002850 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002851 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002852 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002853 int rc;
2854
James Smart3e1f0712018-11-29 16:09:29 -08002855 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smart7054a602007-04-25 09:52:34 -04002856
dea31012005-04-17 16:05:31 -05002857 if (mp) {
2858 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2859 kfree(mp);
2860 }
James Smart7054a602007-04-25 09:52:34 -04002861
2862 /*
2863 * If a REG_LOGIN succeeded after node is destroyed or node
2864 * is in re-discovery driver need to cleanup the RPI.
2865 */
James Smart2e0fef82007-06-17 19:56:36 -05002866 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002867 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2868 !pmb->u.mb.mbxStatus) {
2869 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002870 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart38503942020-03-22 11:12:53 -07002871 if (phba->sli_rev == LPFC_SLI_REV4)
2872 vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
James Smart04c68492009-05-22 14:52:52 -04002873 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002874 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002875 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002876 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2877 if (rc != MBX_NOT_FINISHED)
2878 return;
2879 }
2880
James Smart695a8142010-01-26 23:08:03 -05002881 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2882 !(phba->pport->load_flag & FC_UNLOADING) &&
2883 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002884 shost = lpfc_shost_from_vport(vport);
2885 spin_lock_irq(shost->host_lock);
2886 vport->vpi_state |= LPFC_VPI_REGISTERED;
2887 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2888 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002889 }
2890
James Smartd439d282010-09-29 11:18:45 -04002891 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -08002892 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -04002893 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002894 pmb->ctx_buf = NULL;
2895 pmb->ctx_ndlp = NULL;
2896 }
2897
2898 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2899 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2900
2901 /* Check to see if there are any deferred events to process */
2902 if (ndlp) {
2903 lpfc_printf_vlog(
2904 vport,
2905 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2906 "1438 UNREG cmpl deferred mbox x%x "
James Smartf1156122021-04-11 18:31:24 -07002907 "on NPort x%x Data: x%x x%x x%px x%x x%x\n",
James Smartdea16bd2018-11-29 16:09:30 -08002908 ndlp->nlp_rpi, ndlp->nlp_DID,
James Smarte9b11082020-11-15 11:26:33 -08002909 ndlp->nlp_flag, ndlp->nlp_defer_did,
2910 ndlp, vport->load_flag, kref_read(&ndlp->kref));
James Smartdea16bd2018-11-29 16:09:30 -08002911
2912 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2913 (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
James Smart00292e02018-12-13 15:17:55 -08002914 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002915 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2916 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002917 } else {
James Smartb95b2112019-08-14 16:56:47 -07002918 __lpfc_sli_rpi_release(vport, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002919 }
James Smarta70e63e2020-11-15 11:26:38 -08002920
2921 /* The unreg_login mailbox is complete and had a
2922 * reference that has to be released. The PLOGI
2923 * got its own ref.
2924 */
2925 lpfc_nlp_put(ndlp);
James Smart9b164062019-03-12 16:30:06 -07002926 pmb->ctx_ndlp = NULL;
James Smartdea16bd2018-11-29 16:09:30 -08002927 }
James Smartd439d282010-09-29 11:18:45 -04002928 }
2929
James Smart1037e4b2021-05-14 12:55:52 -07002930 /* This nlp_put pairs with lpfc_sli4_resume_rpi */
2931 if (pmb->u.mb.mbxCommand == MBX_RESUME_RPI) {
2932 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2933 lpfc_nlp_put(ndlp);
2934 }
2935
James Smartdcf2a4e2010-09-29 11:18:53 -04002936 /* Check security permission status on INIT_LINK mailbox command */
2937 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2938 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
Dick Kennedy372c1872020-06-30 14:50:00 -07002939 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartdcf2a4e2010-09-29 11:18:53 -04002940 "2860 SLI authentication is required "
2941 "for INIT_LINK but has not done yet\n");
2942
James Smart04c68492009-05-22 14:52:52 -04002943 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2944 lpfc_sli4_mbox_cmd_free(phba, pmb);
2945 else
2946 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002947}
James Smartbe6bb942015-04-07 15:07:22 -04002948 /**
2949 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2950 * @phba: Pointer to HBA context object.
2951 * @pmb: Pointer to mailbox object.
2952 *
2953 * This function is the unreg rpi mailbox completion handler. It
2954 * frees the memory resources associated with the completed mailbox
James Smarta70e63e2020-11-15 11:26:38 -08002955 * command. An additional reference is put on the ndlp to prevent
James Smartbe6bb942015-04-07 15:07:22 -04002956 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2957 * the unreg mailbox command completes, this routine puts the
2958 * reference back.
2959 *
2960 **/
2961void
2962lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2963{
2964 struct lpfc_vport *vport = pmb->vport;
2965 struct lpfc_nodelist *ndlp;
2966
James Smart3e1f0712018-11-29 16:09:29 -08002967 ndlp = pmb->ctx_ndlp;
James Smartbe6bb942015-04-07 15:07:22 -04002968 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2969 if (phba->sli_rev == LPFC_SLI_REV4 &&
2970 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002971 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002972 LPFC_SLI_INTF_IF_TYPE_2)) {
2973 if (ndlp) {
James Smartdea16bd2018-11-29 16:09:30 -08002974 lpfc_printf_vlog(
James Smarte9b11082020-11-15 11:26:33 -08002975 vport, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smartdea16bd2018-11-29 16:09:30 -08002976 "0010 UNREG_LOGIN vpi:%x "
2977 "rpi:%x DID:%x defer x%x flg x%x "
James Smartf1156122021-04-11 18:31:24 -07002978 "x%px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002979 vport->vpi, ndlp->nlp_rpi,
2980 ndlp->nlp_DID, ndlp->nlp_defer_did,
2981 ndlp->nlp_flag,
James Smart307e3382020-11-15 11:26:30 -08002982 ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002983 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartdea16bd2018-11-29 16:09:30 -08002984
2985 /* Check to see if there are any deferred
2986 * events to process
2987 */
2988 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2989 (ndlp->nlp_defer_did !=
2990 NLP_EVT_NOTHING_PENDING)) {
2991 lpfc_printf_vlog(
2992 vport, KERN_INFO, LOG_DISCOVERY,
2993 "4111 UNREG cmpl deferred "
2994 "clr x%x on "
James Smart32350662019-08-14 16:57:06 -07002995 "NPort x%x Data: x%x x%px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002996 ndlp->nlp_rpi, ndlp->nlp_DID,
2997 ndlp->nlp_defer_did, ndlp);
James Smart00292e02018-12-13 15:17:55 -08002998 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002999 ndlp->nlp_defer_did =
3000 NLP_EVT_NOTHING_PENDING;
3001 lpfc_issue_els_plogi(
3002 vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08003003 } else {
James Smartb95b2112019-08-14 16:56:47 -07003004 __lpfc_sli_rpi_release(vport, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08003005 }
James Smart4430f7f2020-11-15 11:26:31 -08003006 lpfc_nlp_put(ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04003007 }
3008 }
3009 }
3010
3011 mempool_free(pmb, phba->mbox_mem_pool);
3012}
dea31012005-04-17 16:05:31 -05003013
James Smarte59058c2008-08-24 21:49:00 -04003014/**
James Smart3621a712009-04-06 18:47:14 -04003015 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04003016 * @phba: Pointer to HBA context object.
3017 *
3018 * This function is called with no lock held. This function processes all
3019 * the completed mailbox commands and gives it to upper layers. The interrupt
3020 * service routine processes mailbox completion interrupt and adds completed
3021 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
3022 * Worker thread call lpfc_sli_handle_mb_event, which will return the
3023 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
3024 * function returns the mailbox commands to the upper layer by calling the
3025 * completion handler function of each mailbox.
3026 **/
dea31012005-04-17 16:05:31 -05003027int
James Smart2e0fef82007-06-17 19:56:36 -05003028lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003029{
James Smart92d7f7b2007-06-17 19:56:38 -05003030 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05003031 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05003032 int rc;
3033 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05003034
3035 phba->sli.slistat.mbox_event++;
3036
James Smart92d7f7b2007-06-17 19:56:38 -05003037 /* Get all completed mailboxe buffers into the cmplq */
3038 spin_lock_irq(&phba->hbalock);
3039 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
3040 spin_unlock_irq(&phba->hbalock);
3041
dea31012005-04-17 16:05:31 -05003042 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05003043 do {
3044 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
3045 if (pmb == NULL)
3046 break;
3047
James Smart04c68492009-05-22 14:52:52 -04003048 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003049
James Smart858c9f62007-06-17 19:56:39 -05003050 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
3051 if (pmb->vport) {
3052 lpfc_debugfs_disc_trc(pmb->vport,
3053 LPFC_DISC_TRC_MBOX_VPORT,
3054 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
3055 (uint32_t)pmbox->mbxCommand,
3056 pmbox->un.varWords[0],
3057 pmbox->un.varWords[1]);
3058 }
3059 else {
3060 lpfc_debugfs_disc_trc(phba->pport,
3061 LPFC_DISC_TRC_MBOX,
3062 "MBOX cmpl: cmd:x%x mb:x%x x%x",
3063 (uint32_t)pmbox->mbxCommand,
3064 pmbox->un.varWords[0],
3065 pmbox->un.varWords[1]);
3066 }
3067 }
3068
dea31012005-04-17 16:05:31 -05003069 /*
3070 * It is a fatal error if unknown mbox command completion.
3071 */
3072 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
3073 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003074 /* Unknown mailbox command compl */
Dick Kennedy372c1872020-06-30 14:50:00 -07003075 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003076 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04003077 "x%x (x%x/x%x) Cmpl\n",
James Smart43bfea12019-09-21 20:58:57 -07003078 pmb->vport ? pmb->vport->vpi :
3079 LPFC_VPORT_UNKNOWN,
James Smart04c68492009-05-22 14:52:52 -04003080 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04003081 lpfc_sli_config_mbox_subsys_get(phba,
3082 pmb),
3083 lpfc_sli_config_mbox_opcode_get(phba,
3084 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05003085 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003086 phba->work_hs = HS_FFER3;
3087 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003088 continue;
dea31012005-04-17 16:05:31 -05003089 }
3090
dea31012005-04-17 16:05:31 -05003091 if (pmbox->mbxStatus) {
3092 phba->sli.slistat.mbox_stat_err++;
3093 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
3094 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05003095 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04003096 LOG_MBOX | LOG_SLI,
3097 "(%d):0305 Mbox cmd cmpl "
3098 "error - RETRYing Data: x%x "
3099 "(x%x/x%x) x%x x%x x%x\n",
James Smart43bfea12019-09-21 20:58:57 -07003100 pmb->vport ? pmb->vport->vpi :
3101 LPFC_VPORT_UNKNOWN,
James Smarta183a152011-10-10 21:32:43 -04003102 pmbox->mbxCommand,
3103 lpfc_sli_config_mbox_subsys_get(phba,
3104 pmb),
3105 lpfc_sli_config_mbox_opcode_get(phba,
3106 pmb),
3107 pmbox->mbxStatus,
3108 pmbox->un.varWords[0],
James Smart43bfea12019-09-21 20:58:57 -07003109 pmb->vport ? pmb->vport->port_state :
3110 LPFC_VPORT_UNKNOWN);
dea31012005-04-17 16:05:31 -05003111 pmbox->mbxStatus = 0;
3112 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05003113 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04003114 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05003115 continue;
dea31012005-04-17 16:05:31 -05003116 }
3117 }
3118
3119 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05003120 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
Sakari Ailus2d44d162019-09-04 19:04:23 +03003121 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
James Smarte74c03c2013-04-17 20:15:19 -04003122 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
3123 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003124 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05003125 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04003126 lpfc_sli_config_mbox_subsys_get(phba, pmb),
3127 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05003128 pmb->mbox_cmpl,
3129 *((uint32_t *) pmbox),
3130 pmbox->un.varWords[0],
3131 pmbox->un.varWords[1],
3132 pmbox->un.varWords[2],
3133 pmbox->un.varWords[3],
3134 pmbox->un.varWords[4],
3135 pmbox->un.varWords[5],
3136 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04003137 pmbox->un.varWords[7],
3138 pmbox->un.varWords[8],
3139 pmbox->un.varWords[9],
3140 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05003141
James Smart92d7f7b2007-06-17 19:56:38 -05003142 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05003143 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05003144 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05003145 return 0;
dea31012005-04-17 16:05:31 -05003146}
James Smart92d7f7b2007-06-17 19:56:38 -05003147
James Smarte59058c2008-08-24 21:49:00 -04003148/**
James Smart3621a712009-04-06 18:47:14 -04003149 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04003150 * @phba: Pointer to HBA context object.
3151 * @pring: Pointer to driver SLI ring object.
3152 * @tag: buffer tag.
3153 *
3154 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
3155 * is set in the tag the buffer is posted for a particular exchange,
3156 * the function will return the buffer without replacing the buffer.
3157 * If the buffer is for unsolicited ELS or CT traffic, this function
3158 * returns the buffer and also posts another buffer to the firmware.
3159 **/
James Smart76bb24e2007-10-27 13:38:00 -04003160static struct lpfc_dmabuf *
3161lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05003162 struct lpfc_sli_ring *pring,
3163 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04003164{
James Smart9f1e1b52008-12-04 22:39:40 -05003165 struct hbq_dmabuf *hbq_entry;
3166
James Smart76bb24e2007-10-27 13:38:00 -04003167 if (tag & QUE_BUFTAG_BIT)
3168 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05003169 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
3170 if (!hbq_entry)
3171 return NULL;
3172 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04003173}
James Smart57127f12007-10-27 13:37:05 -04003174
James Smart3772a992009-05-22 14:50:54 -04003175/**
James Smart3a8070c2020-03-31 09:50:05 -07003176 * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer
3177 * containing a NVME LS request.
3178 * @phba: pointer to lpfc hba data structure.
3179 * @piocb: pointer to the iocbq struct representing the sequence starting
3180 * frame.
3181 *
3182 * This routine initially validates the NVME LS, validates there is a login
3183 * with the port that sent the LS, and then calls the appropriate nvme host
3184 * or target LS request handler.
3185 **/
3186static void
3187lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
3188{
3189 struct lpfc_nodelist *ndlp;
3190 struct lpfc_dmabuf *d_buf;
3191 struct hbq_dmabuf *nvmebuf;
3192 struct fc_frame_header *fc_hdr;
3193 struct lpfc_async_xchg_ctx *axchg = NULL;
3194 char *failwhy = NULL;
3195 uint32_t oxid, sid, did, fctl, size;
James Smart4e57e0b2020-05-20 11:59:28 -07003196 int ret = 1;
James Smart3a8070c2020-03-31 09:50:05 -07003197
3198 d_buf = piocb->context2;
3199
3200 nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
3201 fc_hdr = nvmebuf->hbuf.virt;
3202 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
3203 sid = sli4_sid_from_fc_hdr(fc_hdr);
3204 did = sli4_did_from_fc_hdr(fc_hdr);
3205 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
3206 fc_hdr->fh_f_ctl[1] << 8 |
3207 fc_hdr->fh_f_ctl[2]);
3208 size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
3209
3210 lpfc_nvmeio_data(phba, "NVME LS RCV: xri x%x sz %d from %06x\n",
3211 oxid, size, sid);
3212
3213 if (phba->pport->load_flag & FC_UNLOADING) {
3214 failwhy = "Driver Unloading";
3215 } else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
3216 failwhy = "NVME FC4 Disabled";
3217 } else if (!phba->nvmet_support && !phba->pport->localport) {
3218 failwhy = "No Localport";
3219 } else if (phba->nvmet_support && !phba->targetport) {
3220 failwhy = "No Targetport";
3221 } else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {
3222 failwhy = "Bad NVME LS R_CTL";
3223 } else if (unlikely((fctl & 0x00FF0000) !=
3224 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {
3225 failwhy = "Bad NVME LS F_CTL";
3226 } else {
3227 axchg = kzalloc(sizeof(*axchg), GFP_ATOMIC);
3228 if (!axchg)
3229 failwhy = "No CTX memory";
3230 }
3231
3232 if (unlikely(failwhy)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003233 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3a8070c2020-03-31 09:50:05 -07003234 "6154 Drop NVME LS: SID %06X OXID x%X: %s\n",
3235 sid, oxid, failwhy);
3236 goto out_fail;
3237 }
3238
3239 /* validate the source of the LS is logged in */
3240 ndlp = lpfc_findnode_did(phba->pport, sid);
James Smart307e3382020-11-15 11:26:30 -08003241 if (!ndlp ||
James Smart3a8070c2020-03-31 09:50:05 -07003242 ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3243 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3244 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
3245 "6216 NVME Unsol rcv: No ndlp: "
3246 "NPort_ID x%x oxid x%x\n",
3247 sid, oxid);
3248 goto out_fail;
3249 }
3250
3251 axchg->phba = phba;
3252 axchg->ndlp = ndlp;
3253 axchg->size = size;
3254 axchg->oxid = oxid;
3255 axchg->sid = sid;
3256 axchg->wqeq = NULL;
3257 axchg->state = LPFC_NVME_STE_LS_RCV;
3258 axchg->entry_cnt = 1;
3259 axchg->rqb_buffer = (void *)nvmebuf;
3260 axchg->hdwq = &phba->sli4_hba.hdwq[0];
3261 axchg->payload = nvmebuf->dbuf.virt;
3262 INIT_LIST_HEAD(&axchg->list);
3263
James Smart243156c2021-01-04 10:02:37 -08003264 if (phba->nvmet_support) {
James Smart3a8070c2020-03-31 09:50:05 -07003265 ret = lpfc_nvmet_handle_lsreq(phba, axchg);
James Smart243156c2021-01-04 10:02:37 -08003266 spin_lock_irq(&ndlp->lock);
3267 if (!ret && !(ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH)) {
3268 ndlp->fc4_xpt_flags |= NLP_XPT_HAS_HH;
3269 spin_unlock_irq(&ndlp->lock);
3270
3271 /* This reference is a single occurrence to hold the
3272 * node valid until the nvmet transport calls
3273 * host_release.
3274 */
3275 if (!lpfc_nlp_get(ndlp))
3276 goto out_fail;
3277
3278 lpfc_printf_log(phba, KERN_ERR, LOG_NODE,
James Smartf1156122021-04-11 18:31:24 -07003279 "6206 NVMET unsol ls_req ndlp x%px "
James Smart243156c2021-01-04 10:02:37 -08003280 "DID x%x xflags x%x refcnt %d\n",
3281 ndlp, ndlp->nlp_DID,
3282 ndlp->fc4_xpt_flags,
3283 kref_read(&ndlp->kref));
3284 } else {
3285 spin_unlock_irq(&ndlp->lock);
3286 }
3287 } else {
James Smart3a8070c2020-03-31 09:50:05 -07003288 ret = lpfc_nvme_handle_lsreq(phba, axchg);
James Smart243156c2021-01-04 10:02:37 -08003289 }
James Smart3a8070c2020-03-31 09:50:05 -07003290
3291 /* if zero, LS was successfully handled. If non-zero, LS not handled */
3292 if (!ret)
3293 return;
3294
James Smart243156c2021-01-04 10:02:37 -08003295out_fail:
Dick Kennedy372c1872020-06-30 14:50:00 -07003296 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3a8070c2020-03-31 09:50:05 -07003297 "6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "
3298 "NVMe%s handler failed %d\n",
3299 did, sid, oxid,
3300 (phba->nvmet_support) ? "T" : "I", ret);
3301
James Smart3a8070c2020-03-31 09:50:05 -07003302 /* recycle receive buffer */
3303 lpfc_in_buf_free(phba, &nvmebuf->dbuf);
3304
3305 /* If start of new exchange, abort it */
James Smart4e57e0b2020-05-20 11:59:28 -07003306 if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))
3307 ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);
3308
3309 if (ret)
3310 kfree(axchg);
James Smart3a8070c2020-03-31 09:50:05 -07003311}
3312
3313/**
James Smart3772a992009-05-22 14:50:54 -04003314 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
3315 * @phba: Pointer to HBA context object.
3316 * @pring: Pointer to driver SLI ring object.
3317 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
3318 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
3319 * @fch_type: the type for the first frame of the sequence.
3320 *
3321 * This function is called with no lock held. This function uses the r_ctl and
3322 * type of the received sequence to find the correct callback function to call
3323 * to process the sequence.
3324 **/
3325static int
3326lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3327 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
3328 uint32_t fch_type)
3329{
3330 int i;
3331
James Smartf358dd02017-02-12 13:52:34 -08003332 switch (fch_type) {
3333 case FC_TYPE_NVME:
James Smart3a8070c2020-03-31 09:50:05 -07003334 lpfc_nvme_unsol_ls_handler(phba, saveq);
James Smartf358dd02017-02-12 13:52:34 -08003335 return 1;
3336 default:
3337 break;
3338 }
3339
James Smart3772a992009-05-22 14:50:54 -04003340 /* unSolicited Responses */
3341 if (pring->prt[0].profile) {
3342 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
3343 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
3344 saveq);
3345 return 1;
3346 }
3347 /* We must search, based on rctl / type
3348 for the right routine */
3349 for (i = 0; i < pring->num_mask; i++) {
3350 if ((pring->prt[i].rctl == fch_r_ctl) &&
3351 (pring->prt[i].type == fch_type)) {
3352 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
3353 (pring->prt[i].lpfc_sli_rcv_unsol_event)
3354 (phba, pring, saveq);
3355 return 1;
3356 }
3357 }
3358 return 0;
3359}
James Smarte59058c2008-08-24 21:49:00 -04003360
3361/**
James Smart3621a712009-04-06 18:47:14 -04003362 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04003363 * @phba: Pointer to HBA context object.
3364 * @pring: Pointer to driver SLI ring object.
3365 * @saveq: Pointer to the unsolicited iocb.
3366 *
3367 * This function is called with no lock held by the ring event handler
3368 * when there is an unsolicited iocb posted to the response ring by the
3369 * firmware. This function gets the buffer associated with the iocbs
3370 * and calls the event handler for the ring. This function handles both
3371 * qring buffers and hbq buffers.
3372 * When the function returns 1 the caller can free the iocb object otherwise
3373 * upper layer functions will free the iocb objects.
3374 **/
dea31012005-04-17 16:05:31 -05003375static int
3376lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3377 struct lpfc_iocbq *saveq)
3378{
3379 IOCB_t * irsp;
3380 WORD5 * w5p;
3381 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04003382 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05003383 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05003384
dea31012005-04-17 16:05:31 -05003385 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04003386
3387 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
3388 if (pring->lpfc_sli_rcv_async_status)
3389 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
3390 else
3391 lpfc_printf_log(phba,
3392 KERN_WARNING,
3393 LOG_SLI,
3394 "0316 Ring %d handler: unexpected "
3395 "ASYNC_STATUS iocb received evt_code "
3396 "0x%x\n",
3397 pring->ringno,
3398 irsp->un.asyncstat.evt_code);
3399 return 1;
3400 }
3401
James Smart3163f722008-02-08 18:50:25 -05003402 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
3403 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
3404 if (irsp->ulpBdeCount > 0) {
3405 dmzbuf = lpfc_sli_get_buff(phba, pring,
3406 irsp->un.ulpWord[3]);
3407 lpfc_in_buf_free(phba, dmzbuf);
3408 }
3409
3410 if (irsp->ulpBdeCount > 1) {
3411 dmzbuf = lpfc_sli_get_buff(phba, pring,
3412 irsp->unsli3.sli3Words[3]);
3413 lpfc_in_buf_free(phba, dmzbuf);
3414 }
3415
3416 if (irsp->ulpBdeCount > 2) {
3417 dmzbuf = lpfc_sli_get_buff(phba, pring,
3418 irsp->unsli3.sli3Words[7]);
3419 lpfc_in_buf_free(phba, dmzbuf);
3420 }
3421
3422 return 1;
3423 }
3424
James Smart92d7f7b2007-06-17 19:56:38 -05003425 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04003426 if (irsp->ulpBdeCount != 0) {
3427 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05003428 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04003429 if (!saveq->context2)
3430 lpfc_printf_log(phba,
3431 KERN_ERR,
3432 LOG_SLI,
3433 "0341 Ring %d Cannot find buffer for "
3434 "an unsolicited iocb. tag 0x%x\n",
3435 pring->ringno,
3436 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04003437 }
3438 if (irsp->ulpBdeCount == 2) {
3439 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04003440 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04003441 if (!saveq->context3)
3442 lpfc_printf_log(phba,
3443 KERN_ERR,
3444 LOG_SLI,
3445 "0342 Ring %d Cannot find buffer for an"
3446 " unsolicited iocb. tag 0x%x\n",
3447 pring->ringno,
3448 irsp->unsli3.sli3Words[7]);
3449 }
3450 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04003451 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04003452 if (irsp->ulpBdeCount != 0) {
3453 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
3454 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05003455 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04003456 lpfc_printf_log(phba,
3457 KERN_ERR,
3458 LOG_SLI,
3459 "0343 Ring %d Cannot find "
3460 "buffer for an unsolicited iocb"
3461 ". tag 0x%x\n", pring->ringno,
3462 irsp->un.ulpWord[3]);
3463 }
3464 if (irsp->ulpBdeCount == 2) {
3465 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
3466 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05003467 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04003468 lpfc_printf_log(phba,
3469 KERN_ERR,
3470 LOG_SLI,
3471 "0344 Ring %d Cannot find "
3472 "buffer for an unsolicited "
3473 "iocb. tag 0x%x\n",
3474 pring->ringno,
3475 irsp->unsli3.sli3Words[7]);
3476 }
3477 }
James Smart92d7f7b2007-06-17 19:56:38 -05003478 }
James Smart9c2face2008-01-11 01:53:18 -05003479 if (irsp->ulpBdeCount != 0 &&
3480 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
3481 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
3482 int found = 0;
3483
3484 /* search continue save q for same XRI */
3485 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04003486 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
3487 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05003488 list_add_tail(&saveq->list, &iocbq->list);
3489 found = 1;
3490 break;
3491 }
3492 }
3493 if (!found)
3494 list_add_tail(&saveq->clist,
3495 &pring->iocb_continue_saveq);
3496 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
3497 list_del_init(&iocbq->clist);
3498 saveq = iocbq;
3499 irsp = &(saveq->iocb);
3500 } else
3501 return 0;
3502 }
3503 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
3504 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
3505 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04003506 Rctl = FC_RCTL_ELS_REQ;
3507 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05003508 } else {
3509 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
3510 Rctl = w5p->hcsw.Rctl;
3511 Type = w5p->hcsw.Type;
3512
3513 /* Firmware Workaround */
3514 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
3515 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
3516 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04003517 Rctl = FC_RCTL_ELS_REQ;
3518 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05003519 w5p->hcsw.Rctl = Rctl;
3520 w5p->hcsw.Type = Type;
3521 }
3522 }
James Smart92d7f7b2007-06-17 19:56:38 -05003523
James Smart3772a992009-05-22 14:50:54 -04003524 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05003525 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003526 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003527 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04003528 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04003529
James Smart92d7f7b2007-06-17 19:56:38 -05003530 return 1;
dea31012005-04-17 16:05:31 -05003531}
3532
James Smarte59058c2008-08-24 21:49:00 -04003533/**
James Smart3621a712009-04-06 18:47:14 -04003534 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04003535 * @phba: Pointer to HBA context object.
3536 * @pring: Pointer to driver SLI ring object.
3537 * @prspiocb: Pointer to response iocb object.
3538 *
3539 * This function looks up the iocb_lookup table to get the command iocb
3540 * corresponding to the given response iocb using the iotag of the
James Smarte2a8be52019-05-06 17:26:47 -07003541 * response iocb. The driver calls this function with the hbalock held
3542 * for SLI3 ports or the ring lock held for SLI4 ports.
James Smarte59058c2008-08-24 21:49:00 -04003543 * This function returns the command iocb object if it finds the command
3544 * iocb else returns NULL.
3545 **/
dea31012005-04-17 16:05:31 -05003546static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05003547lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3548 struct lpfc_sli_ring *pring,
3549 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05003550{
dea31012005-04-17 16:05:31 -05003551 struct lpfc_iocbq *cmd_iocb = NULL;
3552 uint16_t iotag;
James Smarte2a8be52019-05-06 17:26:47 -07003553 spinlock_t *temp_lock = NULL;
3554 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05003555
James Smarte2a8be52019-05-06 17:26:47 -07003556 if (phba->sli_rev == LPFC_SLI_REV4)
3557 temp_lock = &pring->ring_lock;
3558 else
3559 temp_lock = &phba->hbalock;
3560
3561 spin_lock_irqsave(temp_lock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003562 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05003563
James Bottomley604a3e32005-10-29 10:28:33 -05003564 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3565 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003566 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07003567 /* remove from txcmpl queue list */
3568 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04003569 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003570 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003571 spin_unlock_irqrestore(temp_lock, iflag);
James Smart89533e92016-10-13 15:06:15 -07003572 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003573 }
dea31012005-04-17 16:05:31 -05003574 }
3575
James Smarte2a8be52019-05-06 17:26:47 -07003576 spin_unlock_irqrestore(temp_lock, iflag);
Dick Kennedy372c1872020-06-30 14:50:00 -07003577 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart89533e92016-10-13 15:06:15 -07003578 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05003579 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003580 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05003581 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05003582 return NULL;
3583}
3584
James Smarte59058c2008-08-24 21:49:00 -04003585/**
James Smart3772a992009-05-22 14:50:54 -04003586 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3587 * @phba: Pointer to HBA context object.
3588 * @pring: Pointer to driver SLI ring object.
3589 * @iotag: IOCB tag.
3590 *
3591 * This function looks up the iocb_lookup table to get the command iocb
James Smarte2a8be52019-05-06 17:26:47 -07003592 * corresponding to the given iotag. The driver calls this function with
3593 * the ring lock held because this function is an SLI4 port only helper.
James Smart3772a992009-05-22 14:50:54 -04003594 * This function returns the command iocb object if it finds the command
3595 * iocb else returns NULL.
3596 **/
3597static struct lpfc_iocbq *
3598lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3599 struct lpfc_sli_ring *pring, uint16_t iotag)
3600{
James Smart895427b2017-02-12 13:52:30 -08003601 struct lpfc_iocbq *cmd_iocb = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07003602 spinlock_t *temp_lock = NULL;
3603 unsigned long iflag = 0;
James Smart3772a992009-05-22 14:50:54 -04003604
James Smarte2a8be52019-05-06 17:26:47 -07003605 if (phba->sli_rev == LPFC_SLI_REV4)
3606 temp_lock = &pring->ring_lock;
3607 else
3608 temp_lock = &phba->hbalock;
3609
3610 spin_lock_irqsave(temp_lock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003611 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3612 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003613 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3614 /* remove from txcmpl queue list */
3615 list_del_init(&cmd_iocb->list);
3616 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003617 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003618 spin_unlock_irqrestore(temp_lock, iflag);
James Smart4f2e66c2012-05-09 21:17:07 -04003619 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003620 }
James Smart3772a992009-05-22 14:50:54 -04003621 }
James Smart89533e92016-10-13 15:06:15 -07003622
James Smarte2a8be52019-05-06 17:26:47 -07003623 spin_unlock_irqrestore(temp_lock, iflag);
Dick Kennedy372c1872020-06-30 14:50:00 -07003624 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08003625 "0372 iotag x%x lookup error: max iotag (x%x) "
3626 "iocb_flag x%x\n",
3627 iotag, phba->sli.last_iotag,
3628 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003629 return NULL;
3630}
3631
3632/**
James Smart3621a712009-04-06 18:47:14 -04003633 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003634 * @phba: Pointer to HBA context object.
3635 * @pring: Pointer to driver SLI ring object.
3636 * @saveq: Pointer to the response iocb to be processed.
3637 *
3638 * This function is called by the ring event handler for non-fcp
3639 * rings when there is a new response iocb in the response ring.
3640 * The caller is not required to hold any locks. This function
3641 * gets the command iocb associated with the response iocb and
3642 * calls the completion handler for the command iocb. If there
3643 * is no completion handler, the function will free the resources
3644 * associated with command iocb. If the response iocb is for
3645 * an already aborted command iocb, the status of the completion
3646 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3647 * This function always returns 1.
3648 **/
dea31012005-04-17 16:05:31 -05003649static int
James Smart2e0fef82007-06-17 19:56:36 -05003650lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003651 struct lpfc_iocbq *saveq)
3652{
James Smart2e0fef82007-06-17 19:56:36 -05003653 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003654 int rc = 1;
3655 unsigned long iflag;
3656
James Bottomley604a3e32005-10-29 10:28:33 -05003657 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
dea31012005-04-17 16:05:31 -05003658 if (cmdiocbp) {
3659 if (cmdiocbp->iocb_cmpl) {
3660 /*
James Smartea2151b2008-09-07 11:52:10 -04003661 * If an ELS command failed send an event to mgmt
3662 * application.
3663 */
3664 if (saveq->iocb.ulpStatus &&
3665 (pring->ringno == LPFC_ELS_RING) &&
3666 (cmdiocbp->iocb.ulpCommand ==
3667 CMD_ELS_REQUEST64_CR))
3668 lpfc_send_els_failure_event(phba,
3669 cmdiocbp, saveq);
3670
3671 /*
dea31012005-04-17 16:05:31 -05003672 * Post all ELS completions to the worker thread.
3673 * All other are passed to the completion callback.
3674 */
3675 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003676 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3677 (cmdiocbp->iocb_flag &
3678 LPFC_DRIVER_ABORTED)) {
3679 spin_lock_irqsave(&phba->hbalock,
3680 iflag);
James Smart07951072007-04-25 09:51:38 -04003681 cmdiocbp->iocb_flag &=
3682 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003683 spin_unlock_irqrestore(&phba->hbalock,
3684 iflag);
James Smart07951072007-04-25 09:51:38 -04003685 saveq->iocb.ulpStatus =
3686 IOSTAT_LOCAL_REJECT;
3687 saveq->iocb.un.ulpWord[4] =
3688 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003689
3690 /* Firmware could still be in progress
3691 * of DMAing payload, so don't free data
3692 * buffer till after a hbeat.
3693 */
James Smart341af102010-01-26 23:07:37 -05003694 spin_lock_irqsave(&phba->hbalock,
3695 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003696 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003697 spin_unlock_irqrestore(&phba->hbalock,
3698 iflag);
3699 }
James Smart0f65ff62010-02-26 14:14:23 -05003700 if (phba->sli_rev == LPFC_SLI_REV4) {
3701 if (saveq->iocb_flag &
3702 LPFC_EXCHANGE_BUSY) {
3703 /* Set cmdiocb flag for the
3704 * exchange busy so sgl (xri)
3705 * will not be released until
3706 * the abort xri is received
3707 * from hba.
3708 */
3709 spin_lock_irqsave(
3710 &phba->hbalock, iflag);
3711 cmdiocbp->iocb_flag |=
3712 LPFC_EXCHANGE_BUSY;
3713 spin_unlock_irqrestore(
3714 &phba->hbalock, iflag);
3715 }
3716 if (cmdiocbp->iocb_flag &
3717 LPFC_DRIVER_ABORTED) {
3718 /*
3719 * Clear LPFC_DRIVER_ABORTED
3720 * bit in case it was driver
3721 * initiated abort.
3722 */
3723 spin_lock_irqsave(
3724 &phba->hbalock, iflag);
3725 cmdiocbp->iocb_flag &=
3726 ~LPFC_DRIVER_ABORTED;
3727 spin_unlock_irqrestore(
3728 &phba->hbalock, iflag);
3729 cmdiocbp->iocb.ulpStatus =
3730 IOSTAT_LOCAL_REJECT;
3731 cmdiocbp->iocb.un.ulpWord[4] =
3732 IOERR_ABORT_REQUESTED;
3733 /*
3734 * For SLI4, irsiocb contains
3735 * NO_XRI in sli_xritag, it
3736 * shall not affect releasing
3737 * sgl (xri) process.
3738 */
3739 saveq->iocb.ulpStatus =
3740 IOSTAT_LOCAL_REJECT;
3741 saveq->iocb.un.ulpWord[4] =
3742 IOERR_SLI_ABORTED;
3743 spin_lock_irqsave(
3744 &phba->hbalock, iflag);
3745 saveq->iocb_flag |=
3746 LPFC_DELAY_MEM_FREE;
3747 spin_unlock_irqrestore(
3748 &phba->hbalock, iflag);
3749 }
James Smart07951072007-04-25 09:51:38 -04003750 }
dea31012005-04-17 16:05:31 -05003751 }
James Smart2e0fef82007-06-17 19:56:36 -05003752 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003753 } else
3754 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003755 } else {
3756 /*
3757 * Unknown initiating command based on the response iotag.
3758 * This could be the case on the ELS ring because of
3759 * lpfc_els_abort().
3760 */
3761 if (pring->ringno != LPFC_ELS_RING) {
3762 /*
3763 * Ring <ringno> handler: unexpected completion IoTag
3764 * <IoTag>
3765 */
James Smarta257bf92009-04-06 18:48:10 -04003766 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003767 "0322 Ring %d handler: "
3768 "unexpected completion IoTag x%x "
3769 "Data: x%x x%x x%x x%x\n",
3770 pring->ringno,
3771 saveq->iocb.ulpIoTag,
3772 saveq->iocb.ulpStatus,
3773 saveq->iocb.un.ulpWord[4],
3774 saveq->iocb.ulpCommand,
3775 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003776 }
3777 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003778
dea31012005-04-17 16:05:31 -05003779 return rc;
3780}
3781
James Smarte59058c2008-08-24 21:49:00 -04003782/**
James Smart3621a712009-04-06 18:47:14 -04003783 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003784 * @phba: Pointer to HBA context object.
3785 * @pring: Pointer to driver SLI ring object.
3786 *
3787 * This function is called from the iocb ring event handlers when
3788 * put pointer is ahead of the get pointer for a ring. This function signal
3789 * an error attention condition to the worker thread and the worker
3790 * thread will transition the HBA to offline state.
3791 **/
James Smart2e0fef82007-06-17 19:56:36 -05003792static void
3793lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003794{
James Smart34b02dc2008-08-24 21:49:55 -04003795 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003796 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003797 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003798 * rsp ring <portRspMax>
3799 */
Dick Kennedy372c1872020-06-30 14:50:00 -07003800 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003801 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003802 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003803 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003804 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003805
James Smart2e0fef82007-06-17 19:56:36 -05003806 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003807
3808 /*
3809 * All error attention handlers are posted to
3810 * worker thread
3811 */
3812 phba->work_ha |= HA_ERATT;
3813 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003814
James Smart5e9d9b82008-06-14 22:52:53 -04003815 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003816
3817 return;
3818}
3819
James Smarte59058c2008-08-24 21:49:00 -04003820/**
James Smart3621a712009-04-06 18:47:14 -04003821 * lpfc_poll_eratt - Error attention polling timer timeout handler
Lee Jones7af29d42020-07-21 17:41:31 +01003822 * @t: Context to fetch pointer to address of HBA context object from.
James Smart93996272008-08-24 21:50:30 -04003823 *
3824 * This function is invoked by the Error Attention polling timer when the
3825 * timer times out. It will check the SLI Error Attention register for
3826 * possible attention events. If so, it will post an Error Attention event
3827 * and wake up worker thread to process it. Otherwise, it will set up the
3828 * Error Attention polling timer for the next poll.
3829 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003830void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003831{
3832 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003833 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003834 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003835
Kees Cookf22eb4d2017-09-06 20:24:26 -07003836 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003837
James Smartaa6fbb72012-08-03 12:36:03 -04003838 /* Here we will also keep track of interrupts per sec of the hba */
3839 sli_intr = phba->sli.slistat.sli_intr;
3840
3841 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3842 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3843 sli_intr);
3844 else
3845 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3846
James Smart65791f12016-07-06 12:35:56 -07003847 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3848 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003849 phba->sli.slistat.sli_ips = cnt;
3850
3851 phba->sli.slistat.sli_prev_intr = sli_intr;
3852
James Smart93996272008-08-24 21:50:30 -04003853 /* Check chip HA register for error event */
3854 eratt = lpfc_sli_check_eratt(phba);
3855
3856 if (eratt)
3857 /* Tell the worker thread there is work to do */
3858 lpfc_worker_wake_up(phba);
3859 else
3860 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003861 mod_timer(&phba->eratt_poll,
3862 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003863 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003864 return;
3865}
3866
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003867
James Smarte59058c2008-08-24 21:49:00 -04003868/**
James Smart3621a712009-04-06 18:47:14 -04003869 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003870 * @phba: Pointer to HBA context object.
3871 * @pring: Pointer to driver SLI ring object.
3872 * @mask: Host attention register mask for this ring.
3873 *
3874 * This function is called from the interrupt context when there is a ring
3875 * event for the fcp ring. The caller does not hold any lock.
3876 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003877 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003878 * LE bit set. The function will call the completion handler of the command iocb
3879 * if the response iocb indicates a completion for a command iocb or it is
3880 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3881 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003882 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003883 * to check it explicitly.
3884 */
3885int
James Smart2e0fef82007-06-17 19:56:36 -05003886lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3887 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003888{
James Smart34b02dc2008-08-24 21:49:55 -04003889 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003890 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003891 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003892 struct lpfc_iocbq *cmdiocbq = NULL;
3893 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003894 uint32_t status;
3895 uint32_t portRspPut, portRspMax;
3896 int rc = 1;
3897 lpfc_iocb_type type;
3898 unsigned long iflag;
3899 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003900
James Smart2e0fef82007-06-17 19:56:36 -05003901 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003902 pring->stats.iocb_event++;
3903
dea31012005-04-17 16:05:31 -05003904 /*
3905 * The next available response entry should never exceed the maximum
3906 * entries. If it does, treat it as an adapter hardware error.
3907 */
James Smart7e56aa22012-08-03 12:35:34 -04003908 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003909 portRspPut = le32_to_cpu(pgp->rspPutInx);
3910 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003911 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003912 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003913 return 1;
3914 }
James Smart45ed1192009-10-02 15:17:02 -04003915 if (phba->fcp_ring_in_use) {
3916 spin_unlock_irqrestore(&phba->hbalock, iflag);
3917 return 1;
3918 } else
3919 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003920
3921 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003922 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003923 /*
3924 * Fetch an entry off the ring and copy it into a local data
3925 * structure. The copy involves a byte-swap since the
3926 * network byte order and pci byte orders are different.
3927 */
James Smarted957682007-06-17 19:56:37 -05003928 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003929 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003930
James Smart7e56aa22012-08-03 12:35:34 -04003931 if (++pring->sli.sli3.rspidx >= portRspMax)
3932 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003933
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003934 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3935 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003936 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003937 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003938 irsp = &rspiocbq.iocb;
3939
dea31012005-04-17 16:05:31 -05003940 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3941 pring->stats.iocb_rsp++;
3942 rsp_cmpl++;
3943
3944 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003945 /*
3946 * If resource errors reported from HBA, reduce
3947 * queuedepths of the SCSI device.
3948 */
3949 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003950 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3951 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003952 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003953 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003954 spin_lock_irqsave(&phba->hbalock, iflag);
3955 }
3956
dea31012005-04-17 16:05:31 -05003957 /* Rsp ring <ringno> error: IOCB */
3958 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003959 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003960 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003961 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003962 irsp->un.ulpWord[0],
3963 irsp->un.ulpWord[1],
3964 irsp->un.ulpWord[2],
3965 irsp->un.ulpWord[3],
3966 irsp->un.ulpWord[4],
3967 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003968 *(uint32_t *)&irsp->un1,
3969 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003970 }
3971
3972 switch (type) {
3973 case LPFC_ABORT_IOCB:
3974 case LPFC_SOL_IOCB:
3975 /*
3976 * Idle exchange closed via ABTS from port. No iocb
3977 * resources need to be recovered.
3978 */
3979 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003980 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003981 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003982 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003983 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003984 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003985 break;
3986 }
3987
James Smarte2a8be52019-05-06 17:26:47 -07003988 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003989 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3990 &rspiocbq);
James Smarte2a8be52019-05-06 17:26:47 -07003991 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05003992 if (unlikely(!cmdiocbq))
3993 break;
3994 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3995 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3996 if (cmdiocbq->iocb_cmpl) {
3997 spin_unlock_irqrestore(&phba->hbalock, iflag);
3998 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3999 &rspiocbq);
4000 spin_lock_irqsave(&phba->hbalock, iflag);
4001 }
dea31012005-04-17 16:05:31 -05004002 break;
James Smarta4bc3372006-12-02 13:34:16 -05004003 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05004004 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05004005 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004006 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05004007 break;
dea31012005-04-17 16:05:31 -05004008 default:
4009 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
4010 char adaptermsg[LPFC_MAX_ADPTMSG];
4011 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4012 memcpy(&adaptermsg[0], (uint8_t *) irsp,
4013 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07004014 dev_warn(&((phba->pcidev)->dev),
4015 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05004016 phba->brd_no, adaptermsg);
4017 } else {
4018 /* Unknown IOCB command */
Dick Kennedy372c1872020-06-30 14:50:00 -07004019 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04004020 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05004021 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004022 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05004023 irsp->ulpStatus,
4024 irsp->ulpIoTag,
4025 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05004026 }
4027 break;
4028 }
4029
4030 /*
4031 * The response IOCB has been processed. Update the ring
4032 * pointer in SLIM. If the port response put pointer has not
4033 * been updated, sync the pgp->rspPutInx and fetch the new port
4034 * response put pointer.
4035 */
James Smart7e56aa22012-08-03 12:35:34 -04004036 writel(pring->sli.sli3.rspidx,
4037 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05004038
James Smart7e56aa22012-08-03 12:35:34 -04004039 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05004040 portRspPut = le32_to_cpu(pgp->rspPutInx);
4041 }
4042
4043 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
4044 pring->stats.iocb_rsp_full++;
4045 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4046 writel(status, phba->CAregaddr);
4047 readl(phba->CAregaddr);
4048 }
4049 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4050 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4051 pring->stats.iocb_cmd_empty++;
4052
4053 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04004054 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05004055 lpfc_sli_resume_iocb(phba, pring);
4056
4057 if ((pring->lpfc_sli_cmd_available))
4058 (pring->lpfc_sli_cmd_available) (phba, pring);
4059
4060 }
4061
James Smart45ed1192009-10-02 15:17:02 -04004062 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004063 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05004064 return rc;
4065}
4066
James Smarte59058c2008-08-24 21:49:00 -04004067/**
James Smart3772a992009-05-22 14:50:54 -04004068 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
4069 * @phba: Pointer to HBA context object.
4070 * @pring: Pointer to driver SLI ring object.
4071 * @rspiocbp: Pointer to driver response IOCB object.
4072 *
4073 * This function is called from the worker thread when there is a slow-path
4074 * response IOCB to process. This function chains all the response iocbs until
4075 * seeing the iocb with the LE bit set. The function will call
4076 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
4077 * completion of a command iocb. The function will call the
4078 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
4079 * The function frees the resources or calls the completion handler if this
4080 * iocb is an abort completion. The function returns NULL when the response
4081 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
4082 * this function shall chain the iocb on to the iocb_continueq and return the
4083 * response iocb passed in.
4084 **/
4085static struct lpfc_iocbq *
4086lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4087 struct lpfc_iocbq *rspiocbp)
4088{
4089 struct lpfc_iocbq *saveq;
4090 struct lpfc_iocbq *cmdiocbp;
4091 struct lpfc_iocbq *next_iocb;
4092 IOCB_t *irsp = NULL;
4093 uint32_t free_saveq;
4094 uint8_t iocb_cmd_type;
4095 lpfc_iocb_type type;
4096 unsigned long iflag;
4097 int rc;
4098
4099 spin_lock_irqsave(&phba->hbalock, iflag);
4100 /* First add the response iocb to the countinueq list */
4101 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
4102 pring->iocb_continueq_cnt++;
4103
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02004104 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04004105 irsp = &rspiocbp->iocb;
4106 if (irsp->ulpLe) {
4107 /*
4108 * By default, the driver expects to free all resources
4109 * associated with this iocb completion.
4110 */
4111 free_saveq = 1;
4112 saveq = list_get_first(&pring->iocb_continueq,
4113 struct lpfc_iocbq, list);
4114 irsp = &(saveq->iocb);
4115 list_del_init(&pring->iocb_continueq);
4116 pring->iocb_continueq_cnt = 0;
4117
4118 pring->stats.iocb_rsp++;
4119
4120 /*
4121 * If resource errors reported from HBA, reduce
4122 * queuedepths of the SCSI device.
4123 */
4124 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04004125 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
4126 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04004127 spin_unlock_irqrestore(&phba->hbalock, iflag);
4128 phba->lpfc_rampdown_queue_depth(phba);
4129 spin_lock_irqsave(&phba->hbalock, iflag);
4130 }
4131
4132 if (irsp->ulpStatus) {
4133 /* Rsp ring <ringno> error: IOCB */
4134 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4135 "0328 Rsp Ring %d error: "
4136 "IOCB Data: "
4137 "x%x x%x x%x x%x "
4138 "x%x x%x x%x x%x "
4139 "x%x x%x x%x x%x "
4140 "x%x x%x x%x x%x\n",
4141 pring->ringno,
4142 irsp->un.ulpWord[0],
4143 irsp->un.ulpWord[1],
4144 irsp->un.ulpWord[2],
4145 irsp->un.ulpWord[3],
4146 irsp->un.ulpWord[4],
4147 irsp->un.ulpWord[5],
4148 *(((uint32_t *) irsp) + 6),
4149 *(((uint32_t *) irsp) + 7),
4150 *(((uint32_t *) irsp) + 8),
4151 *(((uint32_t *) irsp) + 9),
4152 *(((uint32_t *) irsp) + 10),
4153 *(((uint32_t *) irsp) + 11),
4154 *(((uint32_t *) irsp) + 12),
4155 *(((uint32_t *) irsp) + 13),
4156 *(((uint32_t *) irsp) + 14),
4157 *(((uint32_t *) irsp) + 15));
4158 }
4159
4160 /*
4161 * Fetch the IOCB command type and call the correct completion
4162 * routine. Solicited and Unsolicited IOCBs on the ELS ring
4163 * get freed back to the lpfc_iocb_list by the discovery
4164 * kernel thread.
4165 */
4166 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
4167 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
4168 switch (type) {
4169 case LPFC_SOL_IOCB:
4170 spin_unlock_irqrestore(&phba->hbalock, iflag);
4171 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
4172 spin_lock_irqsave(&phba->hbalock, iflag);
4173 break;
4174
4175 case LPFC_UNSOL_IOCB:
4176 spin_unlock_irqrestore(&phba->hbalock, iflag);
4177 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
4178 spin_lock_irqsave(&phba->hbalock, iflag);
4179 if (!rc)
4180 free_saveq = 0;
4181 break;
4182
4183 case LPFC_ABORT_IOCB:
4184 cmdiocbp = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07004185 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX) {
4186 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04004187 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
4188 saveq);
James Smarte2a8be52019-05-06 17:26:47 -07004189 spin_lock_irqsave(&phba->hbalock, iflag);
4190 }
James Smart3772a992009-05-22 14:50:54 -04004191 if (cmdiocbp) {
4192 /* Call the specified completion routine */
4193 if (cmdiocbp->iocb_cmpl) {
4194 spin_unlock_irqrestore(&phba->hbalock,
4195 iflag);
4196 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
4197 saveq);
4198 spin_lock_irqsave(&phba->hbalock,
4199 iflag);
4200 } else
4201 __lpfc_sli_release_iocbq(phba,
4202 cmdiocbp);
4203 }
4204 break;
4205
4206 case LPFC_UNKNOWN_IOCB:
4207 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
4208 char adaptermsg[LPFC_MAX_ADPTMSG];
4209 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4210 memcpy(&adaptermsg[0], (uint8_t *)irsp,
4211 MAX_MSG_DATA);
4212 dev_warn(&((phba->pcidev)->dev),
4213 "lpfc%d: %s\n",
4214 phba->brd_no, adaptermsg);
4215 } else {
4216 /* Unknown IOCB command */
Dick Kennedy372c1872020-06-30 14:50:00 -07004217 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04004218 "0335 Unknown IOCB "
4219 "command Data: x%x "
4220 "x%x x%x x%x\n",
4221 irsp->ulpCommand,
4222 irsp->ulpStatus,
4223 irsp->ulpIoTag,
4224 irsp->ulpContext);
4225 }
4226 break;
4227 }
4228
4229 if (free_saveq) {
4230 list_for_each_entry_safe(rspiocbp, next_iocb,
4231 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04004232 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04004233 __lpfc_sli_release_iocbq(phba, rspiocbp);
4234 }
4235 __lpfc_sli_release_iocbq(phba, saveq);
4236 }
4237 rspiocbp = NULL;
4238 }
4239 spin_unlock_irqrestore(&phba->hbalock, iflag);
4240 return rspiocbp;
4241}
4242
4243/**
4244 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04004245 * @phba: Pointer to HBA context object.
4246 * @pring: Pointer to driver SLI ring object.
4247 * @mask: Host attention register mask for this ring.
4248 *
James Smart3772a992009-05-22 14:50:54 -04004249 * This routine wraps the actual slow_ring event process routine from the
4250 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04004251 **/
James Smart3772a992009-05-22 14:50:54 -04004252void
James Smart2e0fef82007-06-17 19:56:36 -05004253lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
4254 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05004255{
James Smart3772a992009-05-22 14:50:54 -04004256 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
4257}
4258
4259/**
4260 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
4261 * @phba: Pointer to HBA context object.
4262 * @pring: Pointer to driver SLI ring object.
4263 * @mask: Host attention register mask for this ring.
4264 *
4265 * This function is called from the worker thread when there is a ring event
4266 * for non-fcp rings. The caller does not hold any lock. The function will
4267 * remove each response iocb in the response ring and calls the handle
4268 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4269 **/
4270static void
4271lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
4272 struct lpfc_sli_ring *pring, uint32_t mask)
4273{
James Smart34b02dc2008-08-24 21:49:55 -04004274 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05004275 IOCB_t *entry;
4276 IOCB_t *irsp = NULL;
4277 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05004278 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05004279 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04004280 uint32_t status;
dea31012005-04-17 16:05:31 -05004281
James Smart34b02dc2008-08-24 21:49:55 -04004282 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05004283 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05004284 pring->stats.iocb_event++;
4285
dea31012005-04-17 16:05:31 -05004286 /*
4287 * The next available response entry should never exceed the maximum
4288 * entries. If it does, treat it as an adapter hardware error.
4289 */
James Smart7e56aa22012-08-03 12:35:34 -04004290 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05004291 portRspPut = le32_to_cpu(pgp->rspPutInx);
4292 if (portRspPut >= portRspMax) {
4293 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004294 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05004295 * rsp ring <portRspMax>
4296 */
Dick Kennedy372c1872020-06-30 14:50:00 -07004297 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04004298 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004299 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04004300 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05004301
James Smart2e0fef82007-06-17 19:56:36 -05004302 phba->link_state = LPFC_HBA_ERROR;
4303 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05004304
4305 phba->work_hs = HS_FFER3;
4306 lpfc_handle_eratt(phba);
4307
James Smart3772a992009-05-22 14:50:54 -04004308 return;
dea31012005-04-17 16:05:31 -05004309 }
4310
4311 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04004312 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05004313 /*
4314 * Build a completion list and call the appropriate handler.
4315 * The process is to get the next available response iocb, get
4316 * a free iocb from the list, copy the response data into the
4317 * free iocb, insert to the continuation list, and update the
4318 * next response index to slim. This process makes response
4319 * iocb's in the ring available to DMA as fast as possible but
4320 * pays a penalty for a copy operation. Since the iocb is
4321 * only 32 bytes, this penalty is considered small relative to
4322 * the PCI reads for register values and a slim write. When
4323 * the ulpLe field is set, the entire Command has been
4324 * received.
4325 */
James Smarted957682007-06-17 19:56:37 -05004326 entry = lpfc_resp_iocb(phba, pring);
4327
James Smart858c9f62007-06-17 19:56:39 -05004328 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05004329 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004330 if (rspiocbp == NULL) {
4331 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004332 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05004333 break;
4334 }
4335
James Smarted957682007-06-17 19:56:37 -05004336 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
4337 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05004338 irsp = &rspiocbp->iocb;
4339
James Smart7e56aa22012-08-03 12:35:34 -04004340 if (++pring->sli.sli3.rspidx >= portRspMax)
4341 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05004342
James Smarta58cbd52007-08-02 11:09:43 -04004343 if (pring->ringno == LPFC_ELS_RING) {
4344 lpfc_debugfs_slow_ring_trc(phba,
4345 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
4346 *(((uint32_t *) irsp) + 4),
4347 *(((uint32_t *) irsp) + 6),
4348 *(((uint32_t *) irsp) + 7));
4349 }
4350
James Smart7e56aa22012-08-03 12:35:34 -04004351 writel(pring->sli.sli3.rspidx,
4352 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05004353
James Smart3772a992009-05-22 14:50:54 -04004354 spin_unlock_irqrestore(&phba->hbalock, iflag);
4355 /* Handle the response IOCB */
4356 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
4357 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05004358
4359 /*
4360 * If the port response put pointer has not been updated, sync
4361 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
4362 * response put pointer.
4363 */
James Smart7e56aa22012-08-03 12:35:34 -04004364 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05004365 portRspPut = le32_to_cpu(pgp->rspPutInx);
4366 }
James Smart7e56aa22012-08-03 12:35:34 -04004367 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05004368
James Smart92d7f7b2007-06-17 19:56:38 -05004369 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05004370 /* At least one response entry has been freed */
4371 pring->stats.iocb_rsp_full++;
4372 /* SET RxRE_RSP in Chip Att register */
4373 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4374 writel(status, phba->CAregaddr);
4375 readl(phba->CAregaddr); /* flush */
4376 }
4377 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4378 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4379 pring->stats.iocb_cmd_empty++;
4380
4381 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04004382 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05004383 lpfc_sli_resume_iocb(phba, pring);
4384
4385 if ((pring->lpfc_sli_cmd_available))
4386 (pring->lpfc_sli_cmd_available) (phba, pring);
4387
4388 }
4389
James Smart2e0fef82007-06-17 19:56:36 -05004390 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04004391 return;
dea31012005-04-17 16:05:31 -05004392}
4393
James Smarte59058c2008-08-24 21:49:00 -04004394/**
James Smart4f774512009-05-22 14:52:35 -04004395 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
4396 * @phba: Pointer to HBA context object.
4397 * @pring: Pointer to driver SLI ring object.
4398 * @mask: Host attention register mask for this ring.
4399 *
4400 * This function is called from the worker thread when there is a pending
4401 * ELS response iocb on the driver internal slow-path response iocb worker
4402 * queue. The caller does not hold any lock. The function will remove each
4403 * response iocb from the response worker queue and calls the handle
4404 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4405 **/
4406static void
4407lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
4408 struct lpfc_sli_ring *pring, uint32_t mask)
4409{
4410 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04004411 struct hbq_dmabuf *dmabuf;
4412 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04004413 unsigned long iflag;
James Smart0ef01a22018-09-10 10:30:45 -07004414 int count = 0;
James Smart4f774512009-05-22 14:52:35 -04004415
James Smart45ed1192009-10-02 15:17:02 -04004416 spin_lock_irqsave(&phba->hbalock, iflag);
4417 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
4418 spin_unlock_irqrestore(&phba->hbalock, iflag);
4419 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04004420 /* Get the response iocb from the head of work queue */
4421 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04004422 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04004423 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04004424 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04004425
4426 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
4427 case CQE_CODE_COMPL_WQE:
4428 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
4429 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04004430 /* Translate ELS WCQE to response IOCBQ */
4431 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
4432 irspiocbq);
4433 if (irspiocbq)
4434 lpfc_sli_sp_handle_rspiocb(phba, pring,
4435 irspiocbq);
James Smart0ef01a22018-09-10 10:30:45 -07004436 count++;
James Smart4d9ab992009-10-02 15:16:39 -04004437 break;
4438 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04004439 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04004440 dmabuf = container_of(cq_event, struct hbq_dmabuf,
4441 cq_event);
4442 lpfc_sli4_handle_received_buffer(phba, dmabuf);
James Smart0ef01a22018-09-10 10:30:45 -07004443 count++;
James Smart4d9ab992009-10-02 15:16:39 -04004444 break;
4445 default:
4446 break;
4447 }
James Smart0ef01a22018-09-10 10:30:45 -07004448
4449 /* Limit the number of events to 64 to avoid soft lockups */
4450 if (count == 64)
4451 break;
James Smart4f774512009-05-22 14:52:35 -04004452 }
4453}
4454
4455/**
James Smart3621a712009-04-06 18:47:14 -04004456 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04004457 * @phba: Pointer to HBA context object.
4458 * @pring: Pointer to driver SLI ring object.
4459 *
4460 * This function aborts all iocbs in the given ring and frees all the iocb
4461 * objects in txq. This function issues an abort iocb for all the iocb commands
4462 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4463 * the return of this function. The caller is not required to hold any locks.
4464 **/
James Smart2e0fef82007-06-17 19:56:36 -05004465void
dea31012005-04-17 16:05:31 -05004466lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
4467{
James Smart2534ba72007-04-25 09:52:20 -04004468 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004469 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05004470
James Smart92d7f7b2007-06-17 19:56:38 -05004471 if (pring->ringno == LPFC_ELS_RING) {
4472 lpfc_fabric_abort_hba(phba);
4473 }
4474
dea31012005-04-17 16:05:31 -05004475 /* Error everything on txq and txcmplq
4476 * First do the txq.
4477 */
James Smartdb55fba2014-04-04 13:52:02 -04004478 if (phba->sli_rev >= LPFC_SLI_REV4) {
4479 spin_lock_irq(&pring->ring_lock);
4480 list_splice_init(&pring->txq, &completions);
4481 pring->txq_cnt = 0;
4482 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05004483
James Smartdb55fba2014-04-04 13:52:02 -04004484 spin_lock_irq(&phba->hbalock);
4485 /* Next issue ABTS for everything on the txcmplq */
4486 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
James Smartdb7531d2020-11-15 11:26:44 -08004487 lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
James Smartdb55fba2014-04-04 13:52:02 -04004488 spin_unlock_irq(&phba->hbalock);
4489 } else {
4490 spin_lock_irq(&phba->hbalock);
4491 list_splice_init(&pring->txq, &completions);
4492 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04004493
James Smartdb55fba2014-04-04 13:52:02 -04004494 /* Next issue ABTS for everything on the txcmplq */
4495 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
James Smartdb7531d2020-11-15 11:26:44 -08004496 lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
James Smartdb55fba2014-04-04 13:52:02 -04004497 spin_unlock_irq(&phba->hbalock);
4498 }
James Smarta22d73b2021-01-04 10:02:38 -08004499 /* Make sure HBA is alive */
4500 lpfc_issue_hb_tmo(phba);
James Smart2534ba72007-04-25 09:52:20 -04004501
James Smarta257bf92009-04-06 18:48:10 -04004502 /* Cancel all the IOCBs from the completions list */
4503 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4504 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004505}
4506
James Smarte59058c2008-08-24 21:49:00 -04004507/**
James Smartdb55fba2014-04-04 13:52:02 -04004508 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
4509 * @phba: Pointer to HBA context object.
James Smartdb55fba2014-04-04 13:52:02 -04004510 *
4511 * This function aborts all iocbs in FCP rings and frees all the iocb
4512 * objects in txq. This function issues an abort iocb for all the iocb commands
4513 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4514 * the return of this function. The caller is not required to hold any locks.
4515 **/
4516void
4517lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
4518{
4519 struct lpfc_sli *psli = &phba->sli;
4520 struct lpfc_sli_ring *pring;
4521 uint32_t i;
4522
4523 /* Look on all the FCP Rings for the iotag */
4524 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004525 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07004526 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smartdb55fba2014-04-04 13:52:02 -04004527 lpfc_sli_abort_iocb_ring(phba, pring);
4528 }
4529 } else {
James Smart895427b2017-02-12 13:52:30 -08004530 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004531 lpfc_sli_abort_iocb_ring(phba, pring);
4532 }
4533}
4534
James Smart895427b2017-02-12 13:52:30 -08004535/**
James Smartc00f62e2019-08-14 16:57:11 -07004536 * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
James Smarta8e497d2008-08-24 21:50:11 -04004537 * @phba: Pointer to HBA context object.
4538 *
James Smartc00f62e2019-08-14 16:57:11 -07004539 * This function flushes all iocbs in the IO ring and frees all the iocb
James Smarta8e497d2008-08-24 21:50:11 -04004540 * objects in txq and txcmplq. This function will not issue abort iocbs
4541 * for all the iocb commands in txcmplq, they will just be returned with
4542 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4543 * slot has been permanently disabled.
4544 **/
4545void
James Smartc00f62e2019-08-14 16:57:11 -07004546lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
James Smarta8e497d2008-08-24 21:50:11 -04004547{
4548 LIST_HEAD(txq);
4549 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04004550 struct lpfc_sli *psli = &phba->sli;
4551 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04004552 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004553 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04004554
4555 spin_lock_irq(&phba->hbalock);
James Smart4cd70892020-03-22 11:12:57 -07004556 if (phba->hba_flag & HBA_IOQ_FLUSH ||
4557 !phba->sli4_hba.hdwq) {
4558 spin_unlock_irq(&phba->hbalock);
4559 return;
4560 }
James Smart4f2e66c2012-05-09 21:17:07 -04004561 /* Indicate the I/O queues are flushed */
James Smartc00f62e2019-08-14 16:57:11 -07004562 phba->hba_flag |= HBA_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04004563 spin_unlock_irq(&phba->hbalock);
4564
James Smartdb55fba2014-04-04 13:52:02 -04004565 /* Look on all the FCP Rings for the iotag */
4566 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004567 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07004568 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smarta8e497d2008-08-24 21:50:11 -04004569
James Smartdb55fba2014-04-04 13:52:02 -04004570 spin_lock_irq(&pring->ring_lock);
4571 /* Retrieve everything on txq */
4572 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004573 list_for_each_entry_safe(piocb, next_iocb,
4574 &pring->txcmplq, list)
4575 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004576 /* Retrieve everything on the txcmplq */
4577 list_splice_init(&pring->txcmplq, &txcmplq);
4578 pring->txq_cnt = 0;
4579 pring->txcmplq_cnt = 0;
4580 spin_unlock_irq(&pring->ring_lock);
4581
4582 /* Flush the txq */
4583 lpfc_sli_cancel_iocbs(phba, &txq,
4584 IOSTAT_LOCAL_REJECT,
4585 IOERR_SLI_DOWN);
4586 /* Flush the txcmpq */
4587 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4588 IOSTAT_LOCAL_REJECT,
4589 IOERR_SLI_DOWN);
4590 }
4591 } else {
James Smart895427b2017-02-12 13:52:30 -08004592 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004593
4594 spin_lock_irq(&phba->hbalock);
4595 /* Retrieve everything on txq */
4596 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004597 list_for_each_entry_safe(piocb, next_iocb,
4598 &pring->txcmplq, list)
4599 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004600 /* Retrieve everything on the txcmplq */
4601 list_splice_init(&pring->txcmplq, &txcmplq);
4602 pring->txq_cnt = 0;
4603 pring->txcmplq_cnt = 0;
4604 spin_unlock_irq(&phba->hbalock);
4605
4606 /* Flush the txq */
4607 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4608 IOERR_SLI_DOWN);
4609 /* Flush the txcmpq */
4610 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4611 IOERR_SLI_DOWN);
4612 }
James Smarta8e497d2008-08-24 21:50:11 -04004613}
4614
4615/**
James Smart3772a992009-05-22 14:50:54 -04004616 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004617 * @phba: Pointer to HBA context object.
4618 * @mask: Bit mask to be checked.
4619 *
4620 * This function reads the host status register and compares
4621 * with the provided bit mask to check if HBA completed
4622 * the restart. This function will wait in a loop for the
4623 * HBA to complete restart. If the HBA does not restart within
4624 * 15 iterations, the function will reset the HBA again. The
4625 * function returns 1 when HBA fail to restart otherwise returns
4626 * zero.
4627 **/
James Smart3772a992009-05-22 14:50:54 -04004628static int
4629lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004630{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004631 uint32_t status;
4632 int i = 0;
4633 int retval = 0;
dea31012005-04-17 16:05:31 -05004634
Jamie Wellnitz41415862006-02-28 19:25:27 -05004635 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004636 if (lpfc_readl(phba->HSregaddr, &status))
4637 return 1;
dea31012005-04-17 16:05:31 -05004638
James Smartd2f25472021-01-04 10:02:27 -08004639 phba->hba_flag |= HBA_NEEDS_CFG_PORT;
4640
Jamie Wellnitz41415862006-02-28 19:25:27 -05004641 /*
4642 * Check status register every 100ms for 5 retries, then every
4643 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4644 * every 2.5 sec for 4.
4645 * Break our of the loop if errors occurred during init.
4646 */
4647 while (((status & mask) != mask) &&
4648 !(status & HS_FFERM) &&
4649 i++ < 20) {
dea31012005-04-17 16:05:31 -05004650
Jamie Wellnitz41415862006-02-28 19:25:27 -05004651 if (i <= 5)
4652 msleep(10);
4653 else if (i <= 10)
4654 msleep(500);
4655 else
4656 msleep(2500);
dea31012005-04-17 16:05:31 -05004657
Jamie Wellnitz41415862006-02-28 19:25:27 -05004658 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004659 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004660 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004661 lpfc_sli_brdrestart(phba);
4662 }
4663 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004664 if (lpfc_readl(phba->HSregaddr, &status)) {
4665 retval = 1;
4666 break;
4667 }
dea31012005-04-17 16:05:31 -05004668 }
dea31012005-04-17 16:05:31 -05004669
Jamie Wellnitz41415862006-02-28 19:25:27 -05004670 /* Check to see if any errors occurred during init */
4671 if ((status & HS_FFERM) || (i >= 20)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004672 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte40a02c2010-02-26 14:13:54 -05004673 "2751 Adapter failed to restart, "
4674 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4675 status,
4676 readl(phba->MBslimaddr + 0xa8),
4677 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004678 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004679 retval = 1;
4680 }
dea31012005-04-17 16:05:31 -05004681
Jamie Wellnitz41415862006-02-28 19:25:27 -05004682 return retval;
dea31012005-04-17 16:05:31 -05004683}
4684
James Smartda0436e2009-05-22 14:51:39 -04004685/**
4686 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4687 * @phba: Pointer to HBA context object.
4688 * @mask: Bit mask to be checked.
4689 *
4690 * This function checks the host status register to check if HBA is
4691 * ready. This function will wait in a loop for the HBA to be ready
4692 * If the HBA is not ready , the function will will reset the HBA PCI
4693 * function again. The function returns 1 when HBA fail to be ready
4694 * otherwise returns zero.
4695 **/
4696static int
4697lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4698{
4699 uint32_t status;
4700 int retval = 0;
4701
4702 /* Read the HBA Host Status Register */
4703 status = lpfc_sli4_post_status_check(phba);
4704
4705 if (status) {
4706 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4707 lpfc_sli_brdrestart(phba);
4708 status = lpfc_sli4_post_status_check(phba);
4709 }
4710
4711 /* Check to see if any errors occurred during init */
4712 if (status) {
4713 phba->link_state = LPFC_HBA_ERROR;
4714 retval = 1;
4715 } else
4716 phba->sli4_hba.intr_enable = 0;
4717
James Smart02243832021-08-16 09:28:54 -07004718 phba->hba_flag &= ~HBA_SETUP;
James Smartda0436e2009-05-22 14:51:39 -04004719 return retval;
4720}
4721
4722/**
4723 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4724 * @phba: Pointer to HBA context object.
4725 * @mask: Bit mask to be checked.
4726 *
4727 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4728 * from the API jump table function pointer from the lpfc_hba struct.
4729 **/
4730int
4731lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4732{
4733 return phba->lpfc_sli_brdready(phba, mask);
4734}
4735
James Smart92908312006-03-07 15:04:13 -05004736#define BARRIER_TEST_PATTERN (0xdeadbeef)
4737
James Smarte59058c2008-08-24 21:49:00 -04004738/**
James Smart3621a712009-04-06 18:47:14 -04004739 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004740 * @phba: Pointer to HBA context object.
4741 *
James Smart1b511972011-12-13 13:23:09 -05004742 * This function is called before resetting an HBA. This function is called
4743 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004744 **/
James Smart2e0fef82007-06-17 19:56:36 -05004745void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004746{
James Smart65a29c12006-07-06 15:50:50 -04004747 uint32_t __iomem *resp_buf;
4748 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004749 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004750 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004751 int i;
4752 uint8_t hdrtype;
4753
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004754 lockdep_assert_held(&phba->hbalock);
4755
James Smart92908312006-03-07 15:04:13 -05004756 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4757 if (hdrtype != 0x80 ||
4758 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4759 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4760 return;
4761
4762 /*
4763 * Tell the other part of the chip to suspend temporarily all
4764 * its DMA activity.
4765 */
James Smart65a29c12006-07-06 15:50:50 -04004766 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004767
4768 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004769 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4770 return;
James Smart92908312006-03-07 15:04:13 -05004771 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4772 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004773 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004774
James Smart9940b972011-03-11 16:06:12 -05004775 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4776 return;
4777 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004778 /* Clear Chip error bit */
4779 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004780 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004781 }
4782
4783 mbox = 0;
4784 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4785 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4786
4787 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004788 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004789 writel(mbox, mbox_buf);
4790
James Smart9940b972011-03-11 16:06:12 -05004791 for (i = 0; i < 50; i++) {
4792 if (lpfc_readl((resp_buf + 1), &resp_data))
4793 return;
4794 if (resp_data != ~(BARRIER_TEST_PATTERN))
4795 mdelay(1);
4796 else
4797 break;
4798 }
4799 resp_data = 0;
4800 if (lpfc_readl((resp_buf + 1), &resp_data))
4801 return;
4802 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004803 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004804 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004805 goto restore_hc;
4806 else
4807 goto clear_errat;
4808 }
4809
4810 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004811 resp_data = 0;
4812 for (i = 0; i < 500; i++) {
4813 if (lpfc_readl(resp_buf, &resp_data))
4814 return;
4815 if (resp_data != mbox)
4816 mdelay(1);
4817 else
4818 break;
4819 }
James Smart92908312006-03-07 15:04:13 -05004820
4821clear_errat:
4822
James Smart9940b972011-03-11 16:06:12 -05004823 while (++i < 500) {
4824 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4825 return;
4826 if (!(ha_copy & HA_ERATT))
4827 mdelay(1);
4828 else
4829 break;
4830 }
James Smart92908312006-03-07 15:04:13 -05004831
4832 if (readl(phba->HAregaddr) & HA_ERATT) {
4833 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004834 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004835 }
4836
4837restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004838 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004839 writel(hc_copy, phba->HCregaddr);
4840 readl(phba->HCregaddr); /* flush */
4841}
4842
James Smarte59058c2008-08-24 21:49:00 -04004843/**
James Smart3621a712009-04-06 18:47:14 -04004844 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004845 * @phba: Pointer to HBA context object.
4846 *
4847 * This function issues a kill_board mailbox command and waits for
4848 * the error attention interrupt. This function is called for stopping
4849 * the firmware processing. The caller is not required to hold any
4850 * locks. This function calls lpfc_hba_down_post function to free
4851 * any pending commands after the kill. The function will return 1 when it
4852 * fails to kill the board else will return 0.
4853 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004854int
James Smart2e0fef82007-06-17 19:56:36 -05004855lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004856{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004857 struct lpfc_sli *psli;
4858 LPFC_MBOXQ_t *pmb;
4859 uint32_t status;
4860 uint32_t ha_copy;
4861 int retval;
4862 int i = 0;
4863
4864 psli = &phba->sli;
4865
4866 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004867 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004868 "0329 Kill HBA Data: x%x x%x\n",
4869 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004870
James Smart98c9ea52007-10-27 13:37:33 -04004871 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4872 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004873 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004874
4875 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004876 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004877 if (lpfc_readl(phba->HCregaddr, &status)) {
4878 spin_unlock_irq(&phba->hbalock);
4879 mempool_free(pmb, phba->mbox_mem_pool);
4880 return 1;
4881 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004882 status &= ~HC_ERINT_ENA;
4883 writel(status, phba->HCregaddr);
4884 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004885 phba->link_flag |= LS_IGNORE_ERATT;
4886 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004887
4888 lpfc_kill_board(phba, pmb);
4889 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4890 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4891
4892 if (retval != MBX_SUCCESS) {
4893 if (retval != MBX_BUSY)
4894 mempool_free(pmb, phba->mbox_mem_pool);
Dick Kennedy372c1872020-06-30 14:50:00 -07004895 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte40a02c2010-02-26 14:13:54 -05004896 "2752 KILL_BOARD command failed retval %d\n",
4897 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004898 spin_lock_irq(&phba->hbalock);
4899 phba->link_flag &= ~LS_IGNORE_ERATT;
4900 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004901 return 1;
4902 }
4903
James Smartf4b4c682009-05-22 14:53:12 -04004904 spin_lock_irq(&phba->hbalock);
4905 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4906 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004907
Jamie Wellnitz41415862006-02-28 19:25:27 -05004908 mempool_free(pmb, phba->mbox_mem_pool);
4909
4910 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4911 * attention every 100ms for 3 seconds. If we don't get ERATT after
4912 * 3 seconds we still set HBA_ERROR state because the status of the
4913 * board is now undefined.
4914 */
James Smart9940b972011-03-11 16:06:12 -05004915 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4916 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004917 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4918 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004919 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4920 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004921 }
4922
4923 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004924 if (ha_copy & HA_ERATT) {
4925 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004926 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004927 }
James Smart2e0fef82007-06-17 19:56:36 -05004928 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004929 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004930 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004931 phba->link_flag &= ~LS_IGNORE_ERATT;
4932 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004933
Jamie Wellnitz41415862006-02-28 19:25:27 -05004934 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004935 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004936
James Smart2e0fef82007-06-17 19:56:36 -05004937 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004938}
4939
James Smarte59058c2008-08-24 21:49:00 -04004940/**
James Smart3772a992009-05-22 14:50:54 -04004941 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004942 * @phba: Pointer to HBA context object.
4943 *
4944 * This function resets the HBA by writing HC_INITFF to the control
4945 * register. After the HBA resets, this function resets all the iocb ring
4946 * indices. This function disables PCI layer parity checking during
4947 * the reset.
4948 * This function returns 0 always.
4949 * The caller is not required to hold any locks.
4950 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004951int
James Smart2e0fef82007-06-17 19:56:36 -05004952lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004953{
4954 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004955 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004956 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004957 int i;
dea31012005-04-17 16:05:31 -05004958
Jamie Wellnitz41415862006-02-28 19:25:27 -05004959 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004960
Jamie Wellnitz41415862006-02-28 19:25:27 -05004961 /* Reset HBA */
4962 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004963 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004964 (phba->pport) ? phba->pport->port_state : 0,
4965 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004966
4967 /* perform board reset */
4968 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004969 phba->link_events = 0;
James Smartd2f25472021-01-04 10:02:27 -08004970 phba->hba_flag |= HBA_NEEDS_CFG_PORT;
James Smart4492b732017-04-27 15:08:26 -07004971 if (phba->pport) {
4972 phba->pport->fc_myDID = 0;
4973 phba->pport->fc_prevDID = 0;
4974 }
dea31012005-04-17 16:05:31 -05004975
Jamie Wellnitz41415862006-02-28 19:25:27 -05004976 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004977 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
4978 return -EIO;
4979
Jamie Wellnitz41415862006-02-28 19:25:27 -05004980 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4981 (cfg_value &
4982 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4983
James Smart3772a992009-05-22 14:50:54 -04004984 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4985
Jamie Wellnitz41415862006-02-28 19:25:27 -05004986 /* Now toggle INITFF bit in the Host Control Register */
4987 writel(HC_INITFF, phba->HCregaddr);
4988 mdelay(1);
4989 readl(phba->HCregaddr); /* flush */
4990 writel(0, phba->HCregaddr);
4991 readl(phba->HCregaddr); /* flush */
4992
4993 /* Restore PCI cmd register */
4994 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004995
4996 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004997 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004998 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004999 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04005000 pring->sli.sli3.rspidx = 0;
5001 pring->sli.sli3.next_cmdidx = 0;
5002 pring->sli.sli3.local_getidx = 0;
5003 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05005004 pring->missbufcnt = 0;
5005 }
dea31012005-04-17 16:05:31 -05005006
James Smart2e0fef82007-06-17 19:56:36 -05005007 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005008 return 0;
5009}
5010
James Smarte59058c2008-08-24 21:49:00 -04005011/**
James Smartda0436e2009-05-22 14:51:39 -04005012 * lpfc_sli4_brdreset - Reset a sli-4 HBA
5013 * @phba: Pointer to HBA context object.
5014 *
5015 * This function resets a SLI4 HBA. This function disables PCI layer parity
5016 * checking during resets the device. The caller is not required to hold
5017 * any locks.
5018 *
James Smart8c24a4f2019-08-14 16:56:53 -07005019 * This function returns 0 on success else returns negative error code.
James Smartda0436e2009-05-22 14:51:39 -04005020 **/
5021int
5022lpfc_sli4_brdreset(struct lpfc_hba *phba)
5023{
5024 struct lpfc_sli *psli = &phba->sli;
5025 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04005026 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005027
5028 /* Reset HBA */
5029 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04005030 "0295 Reset HBA Data: x%x x%x x%x\n",
5031 phba->pport->port_state, psli->sli_flag,
5032 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04005033
5034 /* perform board reset */
5035 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04005036 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04005037 phba->pport->fc_myDID = 0;
5038 phba->pport->fc_prevDID = 0;
James Smart02243832021-08-16 09:28:54 -07005039 phba->hba_flag &= ~HBA_SETUP;
James Smartda0436e2009-05-22 14:51:39 -04005040
James Smartda0436e2009-05-22 14:51:39 -04005041 spin_lock_irq(&phba->hbalock);
5042 psli->sli_flag &= ~(LPFC_PROCESS_LA);
5043 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04005044 spin_unlock_irq(&phba->hbalock);
5045
James Smart02936352014-04-04 13:52:12 -04005046 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
5047 if (phba->hba_flag & HBA_FW_DUMP_OP) {
5048 phba->hba_flag &= ~HBA_FW_DUMP_OP;
5049 return rc;
5050 }
5051
James Smartda0436e2009-05-22 14:51:39 -04005052 /* Now physically reset the device */
5053 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5054 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05005055
5056 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07005057 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
5058 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5059 "3205 PCI read Config failed\n");
5060 return -EIO;
5061 }
5062
James Smartbe858b62010-12-15 17:57:20 -05005063 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
5064 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
5065
James Smart88318812012-09-29 11:29:29 -04005066 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04005067 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04005068
James Smartbe858b62010-12-15 17:57:20 -05005069 /* Restore PCI cmd register */
5070 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
5071
James Smart27b01b82012-05-09 21:19:44 -04005072 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005073}
5074
5075/**
5076 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04005077 * @phba: Pointer to HBA context object.
5078 *
5079 * This function is called in the SLI initialization code path to
5080 * restart the HBA. The caller is not required to hold any lock.
5081 * This function writes MBX_RESTART mailbox command to the SLIM and
5082 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
5083 * function to free any pending commands. The function enables
5084 * POST only during the first initialization. The function returns zero.
5085 * The function does not guarantee completion of MBX_RESTART mailbox
5086 * command before the return of this function.
5087 **/
James Smartda0436e2009-05-22 14:51:39 -04005088static int
5089lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05005090{
5091 MAILBOX_t *mb;
5092 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005093 volatile uint32_t word0;
5094 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04005095 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005096
James Smart2e0fef82007-06-17 19:56:36 -05005097 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05005098
James Smart0d878412009-10-02 15:16:56 -04005099 /* Take PCIe device Advanced Error Reporting (AER) state */
5100 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
5101
Jamie Wellnitz41415862006-02-28 19:25:27 -05005102 psli = &phba->sli;
5103
5104 /* Restart HBA */
5105 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005106 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07005107 (phba->pport) ? phba->pport->port_state : 0,
5108 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05005109
5110 word0 = 0;
5111 mb = (MAILBOX_t *) &word0;
5112 mb->mbxCommand = MBX_RESTART;
5113 mb->mbxHc = 1;
5114
James Smart92908312006-03-07 15:04:13 -05005115 lpfc_reset_barrier(phba);
5116
Jamie Wellnitz41415862006-02-28 19:25:27 -05005117 to_slim = phba->MBslimaddr;
5118 writel(*(uint32_t *) mb, to_slim);
5119 readl(to_slim); /* flush */
5120
5121 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07005122 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05005123 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05005124 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05005125 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04005126 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05005127 writel(*(uint32_t *) mb, to_slim);
5128 readl(to_slim); /* flush */
5129
5130 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07005131 if (phba->pport)
5132 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005133 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04005134 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005135 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05005136
James Smart64ba8812006-08-02 15:24:34 -04005137 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02005138 psli->stats_start = ktime_get_seconds();
James Smart64ba8812006-08-02 15:24:34 -04005139
James Smarteaf15d52008-12-04 22:39:29 -05005140 /* Give the INITFF and Post time to settle. */
5141 mdelay(100);
dea31012005-04-17 16:05:31 -05005142
James Smart0d878412009-10-02 15:16:56 -04005143 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
5144 if (hba_aer_enabled)
5145 pci_disable_pcie_error_reporting(phba->pcidev);
5146
Jamie Wellnitz41415862006-02-28 19:25:27 -05005147 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05005148
5149 return 0;
5150}
5151
James Smarte59058c2008-08-24 21:49:00 -04005152/**
James Smartda0436e2009-05-22 14:51:39 -04005153 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
5154 * @phba: Pointer to HBA context object.
5155 *
5156 * This function is called in the SLI initialization code path to restart
5157 * a SLI4 HBA. The caller is not required to hold any lock.
5158 * At the end of the function, it calls lpfc_hba_down_post function to
5159 * free any pending commands.
5160 **/
5161static int
5162lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
5163{
5164 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04005165 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04005166 int rc;
James Smartda0436e2009-05-22 14:51:39 -04005167
5168 /* Restart HBA */
5169 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5170 "0296 Restart HBA Data: x%x x%x\n",
5171 phba->pport->port_state, psli->sli_flag);
5172
James Smart75baf692010-06-08 18:31:21 -04005173 /* Take PCIe device Advanced Error Reporting (AER) state */
5174 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
5175
James Smart27b01b82012-05-09 21:19:44 -04005176 rc = lpfc_sli4_brdreset(phba);
James Smart4fb86a62019-09-03 14:54:41 -07005177 if (rc) {
5178 phba->link_state = LPFC_HBA_ERROR;
5179 goto hba_down_queue;
5180 }
James Smartda0436e2009-05-22 14:51:39 -04005181
5182 spin_lock_irq(&phba->hbalock);
5183 phba->pport->stopped = 0;
5184 phba->link_state = LPFC_INIT_START;
5185 phba->hba_flag = 0;
5186 spin_unlock_irq(&phba->hbalock);
5187
5188 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02005189 psli->stats_start = ktime_get_seconds();
James Smartda0436e2009-05-22 14:51:39 -04005190
James Smart75baf692010-06-08 18:31:21 -04005191 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
5192 if (hba_aer_enabled)
5193 pci_disable_pcie_error_reporting(phba->pcidev);
5194
James Smart4fb86a62019-09-03 14:54:41 -07005195hba_down_queue:
James Smartda0436e2009-05-22 14:51:39 -04005196 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07005197 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005198
James Smart27b01b82012-05-09 21:19:44 -04005199 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005200}
5201
5202/**
5203 * lpfc_sli_brdrestart - Wrapper func for restarting hba
5204 * @phba: Pointer to HBA context object.
5205 *
5206 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
5207 * API jump table function pointer from the lpfc_hba struct.
5208**/
5209int
5210lpfc_sli_brdrestart(struct lpfc_hba *phba)
5211{
5212 return phba->lpfc_sli_brdrestart(phba);
5213}
5214
5215/**
James Smart3621a712009-04-06 18:47:14 -04005216 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04005217 * @phba: Pointer to HBA context object.
5218 *
5219 * This function is called after a HBA restart to wait for successful
5220 * restart of the HBA. Successful restart of the HBA is indicated by
5221 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
5222 * iteration, the function will restart the HBA again. The function returns
5223 * zero if HBA successfully restarted else returns negative error code.
5224 **/
James Smart4492b732017-04-27 15:08:26 -07005225int
dea31012005-04-17 16:05:31 -05005226lpfc_sli_chipset_init(struct lpfc_hba *phba)
5227{
5228 uint32_t status, i = 0;
5229
5230 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05005231 if (lpfc_readl(phba->HSregaddr, &status))
5232 return -EIO;
dea31012005-04-17 16:05:31 -05005233
5234 /* Check status register to see what current state is */
5235 i = 0;
5236 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
5237
James Smartdcf2a4e2010-09-29 11:18:53 -04005238 /* Check every 10ms for 10 retries, then every 100ms for 90
5239 * retries, then every 1 sec for 50 retires for a total of
5240 * ~60 seconds before reset the board again and check every
5241 * 1 sec for 50 retries. The up to 60 seconds before the
5242 * board ready is required by the Falcon FIPS zeroization
5243 * complete, and any reset the board in between shall cause
5244 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05005245 */
James Smartdcf2a4e2010-09-29 11:18:53 -04005246 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05005247 /* Adapter failed to init, timeout, status reg
5248 <status> */
Dick Kennedy372c1872020-06-30 14:50:00 -07005249 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005250 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05005251 "timeout, status reg x%x, "
5252 "FW Data: A8 x%x AC x%x\n", status,
5253 readl(phba->MBslimaddr + 0xa8),
5254 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05005255 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005256 return -ETIMEDOUT;
5257 }
5258
5259 /* Check to see if any errors occurred during init */
5260 if (status & HS_FFERM) {
5261 /* ERROR: During chipset initialization */
5262 /* Adapter failed to init, chipset, status reg
5263 <status> */
Dick Kennedy372c1872020-06-30 14:50:00 -07005264 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005265 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05005266 "chipset, status reg x%x, "
5267 "FW Data: A8 x%x AC x%x\n", status,
5268 readl(phba->MBslimaddr + 0xa8),
5269 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05005270 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005271 return -EIO;
5272 }
5273
James Smartdcf2a4e2010-09-29 11:18:53 -04005274 if (i <= 10)
dea31012005-04-17 16:05:31 -05005275 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04005276 else if (i <= 100)
5277 msleep(100);
5278 else
5279 msleep(1000);
dea31012005-04-17 16:05:31 -05005280
James Smartdcf2a4e2010-09-29 11:18:53 -04005281 if (i == 150) {
5282 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05005283 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005284 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05005285 }
5286 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05005287 if (lpfc_readl(phba->HSregaddr, &status))
5288 return -EIO;
dea31012005-04-17 16:05:31 -05005289 }
5290
5291 /* Check to see if any errors occurred during init */
5292 if (status & HS_FFERM) {
5293 /* ERROR: During chipset initialization */
5294 /* Adapter failed to init, chipset, status reg <status> */
Dick Kennedy372c1872020-06-30 14:50:00 -07005295 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005296 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05005297 "status reg x%x, "
5298 "FW Data: A8 x%x AC x%x\n", status,
5299 readl(phba->MBslimaddr + 0xa8),
5300 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05005301 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005302 return -EIO;
5303 }
5304
James Smartd2f25472021-01-04 10:02:27 -08005305 phba->hba_flag |= HBA_NEEDS_CFG_PORT;
5306
dea31012005-04-17 16:05:31 -05005307 /* Clear all interrupt enable conditions */
5308 writel(0, phba->HCregaddr);
5309 readl(phba->HCregaddr); /* flush */
5310
5311 /* setup host attn register */
5312 writel(0xffffffff, phba->HAregaddr);
5313 readl(phba->HAregaddr); /* flush */
5314 return 0;
5315}
5316
James Smarte59058c2008-08-24 21:49:00 -04005317/**
James Smart3621a712009-04-06 18:47:14 -04005318 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04005319 *
5320 * This function calculates and returns the number of HBQs required to be
5321 * configured.
5322 **/
James Smart78b2d852007-08-02 11:10:21 -04005323int
James Smarted957682007-06-17 19:56:37 -05005324lpfc_sli_hbq_count(void)
5325{
James Smart92d7f7b2007-06-17 19:56:38 -05005326 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05005327}
5328
James Smarte59058c2008-08-24 21:49:00 -04005329/**
James Smart3621a712009-04-06 18:47:14 -04005330 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04005331 *
5332 * This function adds the number of hbq entries in every HBQ to get
5333 * the total number of hbq entries required for the HBA and returns
5334 * the total count.
5335 **/
James Smarted957682007-06-17 19:56:37 -05005336static int
5337lpfc_sli_hbq_entry_count(void)
5338{
5339 int hbq_count = lpfc_sli_hbq_count();
5340 int count = 0;
5341 int i;
5342
5343 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05005344 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05005345 return count;
5346}
5347
James Smarte59058c2008-08-24 21:49:00 -04005348/**
James Smart3621a712009-04-06 18:47:14 -04005349 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04005350 *
5351 * This function calculates amount of memory required for all hbq entries
5352 * to be configured and returns the total memory required.
5353 **/
dea31012005-04-17 16:05:31 -05005354int
James Smarted957682007-06-17 19:56:37 -05005355lpfc_sli_hbq_size(void)
5356{
5357 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
5358}
5359
James Smarte59058c2008-08-24 21:49:00 -04005360/**
James Smart3621a712009-04-06 18:47:14 -04005361 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04005362 * @phba: Pointer to HBA context object.
5363 *
5364 * This function is called during the SLI initialization to configure
5365 * all the HBQs and post buffers to the HBQ. The caller is not
5366 * required to hold any locks. This function will return zero if successful
5367 * else it will return negative error code.
5368 **/
James Smarted957682007-06-17 19:56:37 -05005369static int
5370lpfc_sli_hbq_setup(struct lpfc_hba *phba)
5371{
5372 int hbq_count = lpfc_sli_hbq_count();
5373 LPFC_MBOXQ_t *pmb;
5374 MAILBOX_t *pmbox;
5375 uint32_t hbqno;
5376 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05005377
James Smart92d7f7b2007-06-17 19:56:38 -05005378 /* Get a Mailbox buffer to setup mailbox
5379 * commands for HBA initialization
5380 */
James Smarted957682007-06-17 19:56:37 -05005381 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5382
5383 if (!pmb)
5384 return -ENOMEM;
5385
James Smart04c68492009-05-22 14:52:52 -04005386 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05005387
5388 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
5389 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05005390 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05005391
5392 hbq_entry_index = 0;
5393 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
5394 phba->hbqs[hbqno].next_hbqPutIdx = 0;
5395 phba->hbqs[hbqno].hbqPutIdx = 0;
5396 phba->hbqs[hbqno].local_hbqGetIdx = 0;
5397 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05005398 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04005399 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
5400 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05005401 hbq_entry_index += phba->hbqs[hbqno].entry_count;
5402
5403 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
5404 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
5405 mbxStatus <status>, ring <num> */
5406
5407 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05005408 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005409 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05005410 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005411 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05005412 pmbox->mbxStatus, hbqno);
5413
5414 phba->link_state = LPFC_HBA_ERROR;
5415 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04005416 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05005417 }
5418 }
5419 phba->hbq_count = hbq_count;
5420
James Smarted957682007-06-17 19:56:37 -05005421 mempool_free(pmb, phba->mbox_mem_pool);
5422
James Smart92d7f7b2007-06-17 19:56:38 -05005423 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04005424 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
5425 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05005426 return 0;
5427}
5428
James Smarte59058c2008-08-24 21:49:00 -04005429/**
James Smart4f774512009-05-22 14:52:35 -04005430 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
5431 * @phba: Pointer to HBA context object.
5432 *
5433 * This function is called during the SLI initialization to configure
5434 * all the HBQs and post buffers to the HBQ. The caller is not
5435 * required to hold any locks. This function will return zero if successful
5436 * else it will return negative error code.
5437 **/
5438static int
5439lpfc_sli4_rb_setup(struct lpfc_hba *phba)
5440{
5441 phba->hbq_in_use = 1;
James Smart999fbbc2019-12-18 15:58:06 -08005442 /**
5443 * Specific case when the MDS diagnostics is enabled and supported.
5444 * The receive buffer count is truncated to manage the incoming
5445 * traffic.
5446 **/
5447 if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
5448 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5449 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
5450 else
5451 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5452 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04005453 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08005454 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04005455 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04005456 return 0;
5457}
5458
5459/**
James Smart3621a712009-04-06 18:47:14 -04005460 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04005461 * @phba: Pointer to HBA context object.
5462 * @sli_mode: sli mode - 2/3
5463 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005464 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04005465 * to issue config_port mailbox command. This function restarts the
5466 * HBA firmware and issues a config_port mailbox command to configure
5467 * the SLI interface in the sli mode specified by sli_mode
5468 * variable. The caller is not required to hold any locks.
5469 * The function returns 0 if successful, else returns negative error
5470 * code.
5471 **/
James Smart93996272008-08-24 21:50:30 -04005472int
5473lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05005474{
5475 LPFC_MBOXQ_t *pmb;
5476 uint32_t resetcount = 0, rc = 0, done = 0;
5477
5478 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5479 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05005480 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005481 return -ENOMEM;
5482 }
5483
James Smarted957682007-06-17 19:56:37 -05005484 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05005485 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05005486 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04005487 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005488 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05005489 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005490 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05005491 rc = lpfc_sli_chipset_init(phba);
5492 if (rc)
5493 break;
5494
James Smart2e0fef82007-06-17 19:56:36 -05005495 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04005496 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005497 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005498 resetcount++;
5499
James Smarted957682007-06-17 19:56:37 -05005500 /* Call pre CONFIG_PORT mailbox command initialization. A
5501 * value of 0 means the call was successful. Any other
5502 * nonzero value is a failure, but if ERESTART is returned,
5503 * the driver may reset the HBA and try again.
5504 */
dea31012005-04-17 16:05:31 -05005505 rc = lpfc_config_port_prep(phba);
5506 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05005507 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05005508 continue;
James Smart34b02dc2008-08-24 21:49:55 -04005509 } else if (rc)
dea31012005-04-17 16:05:31 -05005510 break;
James Smart6d368e52011-05-24 11:44:12 -04005511
James Smart2e0fef82007-06-17 19:56:36 -05005512 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05005513 lpfc_config_port(phba, pmb);
5514 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04005515 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5516 LPFC_SLI3_HBQ_ENABLED |
5517 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04005518 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05005519 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005520 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005521 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05005522 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04005523 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005524 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04005525 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005526 spin_unlock_irq(&phba->hbalock);
5527 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04005528 } else {
5529 /* Allow asynchronous mailbox command to go through */
5530 spin_lock_irq(&phba->hbalock);
5531 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5532 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05005533 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05005534
5535 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5536 (pmb->u.mb.un.varCfgPort.gasabt == 0))
5537 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5538 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04005539 }
dea31012005-04-17 16:05:31 -05005540 }
James Smarted957682007-06-17 19:56:37 -05005541 if (!done) {
5542 rc = -EINVAL;
5543 goto do_prep_failed;
5544 }
James Smart04c68492009-05-22 14:52:52 -04005545 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5546 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04005547 rc = -ENXIO;
5548 goto do_prep_failed;
5549 }
James Smart04c68492009-05-22 14:52:52 -04005550 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04005551 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005552 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5553 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5554 phba->max_vpi : phba->max_vports;
5555
James Smart34b02dc2008-08-24 21:49:55 -04005556 } else
5557 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04005558 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005559 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005560 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005561 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005562
5563 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5564 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005565
James Smartf44ac122018-03-05 12:04:08 -08005566 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5567 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5568 phba->cfg_enable_bg = 0;
5569 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
Dick Kennedy372c1872020-06-30 14:50:00 -07005570 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte2a0a9d2008-12-04 22:40:02 -05005571 "0443 Adapter did not grant "
5572 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005573 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005574 }
James Smart34b02dc2008-08-24 21:49:55 -04005575 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005576 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005577 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005578 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005579 }
James Smart92d7f7b2007-06-17 19:56:38 -05005580do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005581 mempool_free(pmb, phba->mbox_mem_pool);
5582 return rc;
5583}
5584
James Smarte59058c2008-08-24 21:49:00 -04005585
5586/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005587 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005588 * @phba: Pointer to HBA context object.
5589 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005590 * This function is the main SLI initialization function. This function
5591 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005592 * error attention handler code. Caller is not required to hold any
5593 * locks. This function issues config_port mailbox command to configure
5594 * the SLI, setup iocb rings and HBQ rings. In the end the function
5595 * calls the config_port_post function to issue init_link mailbox
5596 * command and to start the discovery. The function will return zero
5597 * if successful, else it will return negative error code.
5598 **/
James Smarted957682007-06-17 19:56:37 -05005599int
5600lpfc_sli_hba_setup(struct lpfc_hba *phba)
5601{
5602 uint32_t rc;
James Smartd2f25472021-01-04 10:02:27 -08005603 int i;
James Smart6d368e52011-05-24 11:44:12 -04005604 int longs;
James Smarted957682007-06-17 19:56:37 -05005605
James Smartd2f25472021-01-04 10:02:27 -08005606 /* Enable ISR already does config_port because of config_msi mbx */
5607 if (phba->hba_flag & HBA_NEEDS_CFG_PORT) {
5608 rc = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
5609 if (rc)
5610 return -EIO;
5611 phba->hba_flag &= ~HBA_NEEDS_CFG_PORT;
James Smarted957682007-06-17 19:56:37 -05005612 }
James Smartb5c53952016-03-31 14:12:30 -07005613 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005614
James Smart0d878412009-10-02 15:16:56 -04005615 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5616 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5617 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5618 if (!rc) {
5619 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5620 "2709 This device supports "
5621 "Advanced Error Reporting (AER)\n");
5622 spin_lock_irq(&phba->hbalock);
5623 phba->hba_flag |= HBA_AER_ENABLED;
5624 spin_unlock_irq(&phba->hbalock);
5625 } else {
5626 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5627 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005628 "Advanced Error Reporting (AER): %d\n",
5629 rc);
James Smart0d878412009-10-02 15:16:56 -04005630 phba->cfg_aer_support = 0;
5631 }
5632 }
5633
James Smarted957682007-06-17 19:56:37 -05005634 if (phba->sli_rev == 3) {
5635 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5636 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005637 } else {
5638 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5639 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005640 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005641 }
5642
5643 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005644 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5645 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005646 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005647
5648 if (rc)
5649 goto lpfc_sli_hba_setup_error;
5650
James Smart6d368e52011-05-24 11:44:12 -04005651 /* Initialize VPIs. */
5652 if (phba->sli_rev == LPFC_SLI_REV3) {
5653 /*
5654 * The VPI bitmask and physical ID array are allocated
5655 * and initialized once only - at driver load. A port
5656 * reset doesn't need to reinitialize this memory.
5657 */
5658 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5659 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005660 phba->vpi_bmask = kcalloc(longs,
5661 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005662 GFP_KERNEL);
5663 if (!phba->vpi_bmask) {
5664 rc = -ENOMEM;
5665 goto lpfc_sli_hba_setup_error;
5666 }
5667
Kees Cook6396bb22018-06-12 14:03:40 -07005668 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5669 sizeof(uint16_t),
5670 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005671 if (!phba->vpi_ids) {
5672 kfree(phba->vpi_bmask);
5673 rc = -ENOMEM;
5674 goto lpfc_sli_hba_setup_error;
5675 }
5676 for (i = 0; i < phba->max_vpi; i++)
5677 phba->vpi_ids[i] = i;
5678 }
5679 }
5680
James Smart93996272008-08-24 21:50:30 -04005681 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005682 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5683 rc = lpfc_sli_hbq_setup(phba);
5684 if (rc)
5685 goto lpfc_sli_hba_setup_error;
5686 }
James Smart04c68492009-05-22 14:52:52 -04005687 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005688 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005689 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005690
5691 rc = lpfc_config_port_post(phba);
5692 if (rc)
5693 goto lpfc_sli_hba_setup_error;
5694
James Smarted957682007-06-17 19:56:37 -05005695 return rc;
5696
James Smart92d7f7b2007-06-17 19:56:38 -05005697lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005698 phba->link_state = LPFC_HBA_ERROR;
Dick Kennedy372c1872020-06-30 14:50:00 -07005699 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005700 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005701 return rc;
5702}
5703
James Smartda0436e2009-05-22 14:51:39 -04005704/**
5705 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5706 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01005707 *
James Smartda0436e2009-05-22 14:51:39 -04005708 * This function issue a dump mailbox command to read config region
5709 * 23 and parse the records in the region and populate driver
5710 * data structure.
5711 **/
5712static int
James Smartff78d8f2011-12-13 13:21:35 -05005713lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005714{
James Smartff78d8f2011-12-13 13:21:35 -05005715 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005716 struct lpfc_dmabuf *mp;
5717 struct lpfc_mqe *mqe;
5718 uint32_t data_length;
5719 int rc;
5720
5721 /* Program the default value of vlan_id and fc_map */
5722 phba->valid_vlan = 0;
5723 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5724 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5725 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5726
James Smartff78d8f2011-12-13 13:21:35 -05005727 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5728 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005729 return -ENOMEM;
5730
James Smartff78d8f2011-12-13 13:21:35 -05005731 mqe = &mboxq->u.mqe;
5732 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5733 rc = -ENOMEM;
5734 goto out_free_mboxq;
5735 }
5736
James Smart3e1f0712018-11-29 16:09:29 -08005737 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04005738 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5739
5740 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5741 "(%d):2571 Mailbox cmd x%x Status x%x "
5742 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5743 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5744 "CQ: x%x x%x x%x x%x\n",
5745 mboxq->vport ? mboxq->vport->vpi : 0,
5746 bf_get(lpfc_mqe_command, mqe),
5747 bf_get(lpfc_mqe_status, mqe),
5748 mqe->un.mb_words[0], mqe->un.mb_words[1],
5749 mqe->un.mb_words[2], mqe->un.mb_words[3],
5750 mqe->un.mb_words[4], mqe->un.mb_words[5],
5751 mqe->un.mb_words[6], mqe->un.mb_words[7],
5752 mqe->un.mb_words[8], mqe->un.mb_words[9],
5753 mqe->un.mb_words[10], mqe->un.mb_words[11],
5754 mqe->un.mb_words[12], mqe->un.mb_words[13],
5755 mqe->un.mb_words[14], mqe->un.mb_words[15],
5756 mqe->un.mb_words[16], mqe->un.mb_words[50],
5757 mboxq->mcqe.word0,
5758 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5759 mboxq->mcqe.trailer);
5760
5761 if (rc) {
5762 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5763 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005764 rc = -EIO;
5765 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005766 }
5767 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005768 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005769 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5770 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005771 rc = -EIO;
5772 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005773 }
James Smartda0436e2009-05-22 14:51:39 -04005774
5775 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5776 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5777 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005778 rc = 0;
5779
5780out_free_mboxq:
5781 mempool_free(mboxq, phba->mbox_mem_pool);
5782 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005783}
5784
5785/**
5786 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5787 * @phba: pointer to lpfc hba data structure.
5788 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5789 * @vpd: pointer to the memory to hold resulting port vpd data.
5790 * @vpd_size: On input, the number of bytes allocated to @vpd.
5791 * On output, the number of data bytes in @vpd.
5792 *
5793 * This routine executes a READ_REV SLI4 mailbox command. In
5794 * addition, this routine gets the port vpd data.
5795 *
5796 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005797 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005798 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005799 **/
5800static int
5801lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5802 uint8_t *vpd, uint32_t *vpd_size)
5803{
5804 int rc = 0;
5805 uint32_t dma_size;
5806 struct lpfc_dmabuf *dmabuf;
5807 struct lpfc_mqe *mqe;
5808
5809 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5810 if (!dmabuf)
5811 return -ENOMEM;
5812
5813 /*
5814 * Get a DMA buffer for the vpd data resulting from the READ_REV
5815 * mailbox command.
5816 */
5817 dma_size = *vpd_size;
Luis Chamberlain750afb02019-01-04 09:23:09 +01005818 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5819 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005820 if (!dmabuf->virt) {
5821 kfree(dmabuf);
5822 return -ENOMEM;
5823 }
James Smartda0436e2009-05-22 14:51:39 -04005824
5825 /*
5826 * The SLI4 implementation of READ_REV conflicts at word1,
5827 * bits 31:16 and SLI4 adds vpd functionality not present
5828 * in SLI3. This code corrects the conflicts.
5829 */
5830 lpfc_read_rev(phba, mboxq);
5831 mqe = &mboxq->u.mqe;
5832 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5833 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5834 mqe->un.read_rev.word1 &= 0x0000FFFF;
5835 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5836 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5837
5838 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5839 if (rc) {
5840 dma_free_coherent(&phba->pcidev->dev, dma_size,
5841 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005842 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005843 return -EIO;
5844 }
5845
James Smartda0436e2009-05-22 14:51:39 -04005846 /*
5847 * The available vpd length cannot be bigger than the
5848 * DMA buffer passed to the port. Catch the less than
5849 * case and update the caller's size.
5850 */
5851 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5852 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5853
James Smartd7c47992010-06-08 18:31:54 -04005854 memcpy(vpd, dmabuf->virt, *vpd_size);
5855
James Smartda0436e2009-05-22 14:51:39 -04005856 dma_free_coherent(&phba->pcidev->dev, dma_size,
5857 dmabuf->virt, dmabuf->phys);
5858 kfree(dmabuf);
5859 return 0;
5860}
5861
5862/**
James Smartb3b4f3e2019-03-12 16:30:23 -07005863 * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005864 * @phba: pointer to lpfc hba data structure.
5865 *
5866 * This routine retrieves SLI4 device physical port name this PCI function
5867 * is attached to.
5868 *
5869 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005870 * 0 - successful
James Smartb3b4f3e2019-03-12 16:30:23 -07005871 * otherwise - failed to retrieve controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005872 **/
5873static int
James Smartb3b4f3e2019-03-12 16:30:23 -07005874lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
James Smartcd1c8302011-10-10 21:33:25 -04005875{
5876 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005877 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5878 struct lpfc_controller_attribute *cntl_attr;
James Smartcd1c8302011-10-10 21:33:25 -04005879 void *virtaddr = NULL;
5880 uint32_t alloclen, reqlen;
5881 uint32_t shdr_status, shdr_add_status;
5882 union lpfc_sli4_cfg_shdr *shdr;
James Smartcd1c8302011-10-10 21:33:25 -04005883 int rc;
5884
James Smartcd1c8302011-10-10 21:33:25 -04005885 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5886 if (!mboxq)
5887 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005888
James Smartb3b4f3e2019-03-12 16:30:23 -07005889 /* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
James Smartcd1c8302011-10-10 21:33:25 -04005890 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5891 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5892 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5893 LPFC_SLI4_MBX_NEMBED);
James Smartb3b4f3e2019-03-12 16:30:23 -07005894
James Smartcd1c8302011-10-10 21:33:25 -04005895 if (alloclen < reqlen) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005896 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcd1c8302011-10-10 21:33:25 -04005897 "3084 Allocated DMA memory size (%d) is "
5898 "less than the requested DMA memory size "
5899 "(%d)\n", alloclen, reqlen);
5900 rc = -ENOMEM;
5901 goto out_free_mboxq;
5902 }
5903 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5904 virtaddr = mboxq->sge_array->addr[0];
5905 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5906 shdr = &mbx_cntl_attr->cfg_shdr;
5907 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5908 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5909 if (shdr_status || shdr_add_status || rc) {
5910 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5911 "3085 Mailbox x%x (x%x/x%x) failed, "
5912 "rc:x%x, status:x%x, add_status:x%x\n",
5913 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5914 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5915 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5916 rc, shdr_status, shdr_add_status);
5917 rc = -ENXIO;
5918 goto out_free_mboxq;
5919 }
James Smartb3b4f3e2019-03-12 16:30:23 -07005920
James Smartcd1c8302011-10-10 21:33:25 -04005921 cntl_attr = &mbx_cntl_attr->cntl_attr;
5922 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5923 phba->sli4_hba.lnk_info.lnk_tp =
5924 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5925 phba->sli4_hba.lnk_info.lnk_no =
5926 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
James Smart16a93e82021-07-07 11:43:34 -07005927 phba->sli4_hba.flash_id = bf_get(lpfc_cntl_attr_flash_id, cntl_attr);
5928 phba->sli4_hba.asic_rev = bf_get(lpfc_cntl_attr_asic_rev, cntl_attr);
James Smartb3b4f3e2019-03-12 16:30:23 -07005929
5930 memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
5931 strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
5932 sizeof(phba->BIOSVersion));
5933
James Smartcd1c8302011-10-10 21:33:25 -04005934 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart16a93e82021-07-07 11:43:34 -07005935 "3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s, "
5936 "flash_id: x%02x, asic_rev: x%02x\n",
James Smartcd1c8302011-10-10 21:33:25 -04005937 phba->sli4_hba.lnk_info.lnk_tp,
James Smartb3b4f3e2019-03-12 16:30:23 -07005938 phba->sli4_hba.lnk_info.lnk_no,
James Smart16a93e82021-07-07 11:43:34 -07005939 phba->BIOSVersion, phba->sli4_hba.flash_id,
5940 phba->sli4_hba.asic_rev);
James Smartb3b4f3e2019-03-12 16:30:23 -07005941out_free_mboxq:
James Smart304ee432021-04-11 18:31:17 -07005942 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5943 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5944 else
5945 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartb3b4f3e2019-03-12 16:30:23 -07005946 return rc;
5947}
5948
5949/**
5950 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5951 * @phba: pointer to lpfc hba data structure.
5952 *
5953 * This routine retrieves SLI4 device physical port name this PCI function
5954 * is attached to.
5955 *
5956 * Return codes
5957 * 0 - successful
5958 * otherwise - failed to retrieve physical port name
5959 **/
5960static int
5961lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5962{
5963 LPFC_MBOXQ_t *mboxq;
5964 struct lpfc_mbx_get_port_name *get_port_name;
5965 uint32_t shdr_status, shdr_add_status;
5966 union lpfc_sli4_cfg_shdr *shdr;
5967 char cport_name = 0;
5968 int rc;
5969
5970 /* We assume nothing at this point */
5971 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5972 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5973
5974 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5975 if (!mboxq)
5976 return -ENOMEM;
5977 /* obtain link type and link number via READ_CONFIG */
5978 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5979 lpfc_sli4_read_config(phba);
5980 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5981 goto retrieve_ppname;
5982
5983 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5984 rc = lpfc_sli4_get_ctl_attr(phba);
5985 if (rc)
5986 goto out_free_mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005987
5988retrieve_ppname:
5989 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5990 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5991 sizeof(struct lpfc_mbx_get_port_name) -
5992 sizeof(struct lpfc_sli4_cfg_mhdr),
5993 LPFC_SLI4_MBX_EMBED);
5994 get_port_name = &mboxq->u.mqe.un.get_port_name;
5995 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5996 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5997 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5998 phba->sli4_hba.lnk_info.lnk_tp);
5999 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6000 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6001 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6002 if (shdr_status || shdr_add_status || rc) {
6003 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6004 "3087 Mailbox x%x (x%x/x%x) failed: "
6005 "rc:x%x, status:x%x, add_status:x%x\n",
6006 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6007 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6008 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6009 rc, shdr_status, shdr_add_status);
6010 rc = -ENXIO;
6011 goto out_free_mboxq;
6012 }
6013 switch (phba->sli4_hba.lnk_info.lnk_no) {
6014 case LPFC_LINK_NUMBER_0:
6015 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
6016 &get_port_name->u.response);
6017 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6018 break;
6019 case LPFC_LINK_NUMBER_1:
6020 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
6021 &get_port_name->u.response);
6022 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6023 break;
6024 case LPFC_LINK_NUMBER_2:
6025 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
6026 &get_port_name->u.response);
6027 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6028 break;
6029 case LPFC_LINK_NUMBER_3:
6030 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
6031 &get_port_name->u.response);
6032 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6033 break;
6034 default:
6035 break;
6036 }
6037
6038 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
6039 phba->Port[0] = cport_name;
6040 phba->Port[1] = '\0';
6041 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6042 "3091 SLI get port name: %s\n", phba->Port);
6043 }
6044
6045out_free_mboxq:
James Smart304ee432021-04-11 18:31:17 -07006046 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
6047 lpfc_sli4_mbox_cmd_free(phba, mboxq);
6048 else
6049 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartcd1c8302011-10-10 21:33:25 -04006050 return rc;
6051}
6052
6053/**
James Smartda0436e2009-05-22 14:51:39 -04006054 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
6055 * @phba: pointer to lpfc hba data structure.
6056 *
6057 * This routine is called to explicitly arm the SLI4 device's completion and
6058 * event queues
6059 **/
6060static void
6061lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
6062{
James Smart895427b2017-02-12 13:52:30 -08006063 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08006064 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartcdb42be2019-01-28 11:14:21 -08006065 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07006066 struct lpfc_queue *eq;
James Smartda0436e2009-05-22 14:51:39 -04006067
James Smart32517fc2019-01-28 11:14:33 -08006068 sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
6069 sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
James Smartb71413d2018-02-22 08:18:40 -08006070 if (sli4_hba->nvmels_cq)
James Smart32517fc2019-01-28 11:14:33 -08006071 sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
6072 LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08006073
James Smartcdb42be2019-01-28 11:14:21 -08006074 if (sli4_hba->hdwq) {
James Smart657add42019-05-21 17:49:06 -07006075 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08006076 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07006077 qp = &sli4_hba->hdwq[qidx];
6078 /* ARM the corresponding CQ */
James Smart01f2ef62019-08-28 16:19:11 -07006079 sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
James Smartc00f62e2019-08-14 16:57:11 -07006080 LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08006081 }
James Smart895427b2017-02-12 13:52:30 -08006082
James Smart657add42019-05-21 17:49:06 -07006083 /* Loop thru all IRQ vectors */
6084 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
6085 eq = sli4_hba->hba_eq_hdl[qidx].eq;
6086 /* ARM the corresponding EQ */
6087 sli4_hba->sli4_write_eq_db(phba, eq,
6088 0, LPFC_QUEUE_REARM);
6089 }
James Smartcdb42be2019-01-28 11:14:21 -08006090 }
James Smart1ba981f2014-02-20 09:56:45 -05006091
James Smart2d7dbc42017-02-12 13:52:35 -08006092 if (phba->nvmet_support) {
6093 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smart32517fc2019-01-28 11:14:33 -08006094 sli4_hba->sli4_write_cq_db(phba,
6095 sli4_hba->nvmet_cqset[qidx], 0,
James Smart2d7dbc42017-02-12 13:52:35 -08006096 LPFC_QUEUE_REARM);
6097 }
James Smart2e90f4b2011-12-13 13:22:37 -05006098 }
James Smartda0436e2009-05-22 14:51:39 -04006099}
6100
6101/**
James Smart6d368e52011-05-24 11:44:12 -04006102 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
6103 * @phba: Pointer to HBA context object.
6104 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04006105 * @extnt_count: buffer to hold port available extent count.
6106 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04006107 *
James Smartb76f2dc2011-07-22 18:37:42 -04006108 * This function calls the port and retrievs the number of available
6109 * extents and their size for a particular extent type.
6110 *
6111 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04006112 **/
James Smartb76f2dc2011-07-22 18:37:42 -04006113int
James Smart6d368e52011-05-24 11:44:12 -04006114lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
6115 uint16_t *extnt_count, uint16_t *extnt_size)
6116{
6117 int rc = 0;
6118 uint32_t length;
6119 uint32_t mbox_tmo;
6120 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
6121 LPFC_MBOXQ_t *mbox;
6122
6123 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6124 if (!mbox)
6125 return -ENOMEM;
6126
6127 /* Find out how many extents are available for this resource type */
6128 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
6129 sizeof(struct lpfc_sli4_cfg_mhdr));
6130 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6131 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
6132 length, LPFC_SLI4_MBX_EMBED);
6133
6134 /* Send an extents count of 0 - the GET doesn't use it. */
6135 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6136 LPFC_SLI4_MBX_EMBED);
6137 if (unlikely(rc)) {
6138 rc = -EIO;
6139 goto err_exit;
6140 }
6141
6142 if (!phba->sli4_hba.intr_enable)
6143 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6144 else {
James Smarta183a152011-10-10 21:32:43 -04006145 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006146 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6147 }
6148 if (unlikely(rc)) {
6149 rc = -EIO;
6150 goto err_exit;
6151 }
6152
6153 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
6154 if (bf_get(lpfc_mbox_hdr_status,
6155 &rsrc_info->header.cfg_shdr.response)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006156 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6d368e52011-05-24 11:44:12 -04006157 "2930 Failed to get resource extents "
6158 "Status 0x%x Add'l Status 0x%x\n",
6159 bf_get(lpfc_mbox_hdr_status,
6160 &rsrc_info->header.cfg_shdr.response),
6161 bf_get(lpfc_mbox_hdr_add_status,
6162 &rsrc_info->header.cfg_shdr.response));
6163 rc = -EIO;
6164 goto err_exit;
6165 }
6166
6167 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
6168 &rsrc_info->u.rsp);
6169 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
6170 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04006171
6172 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6173 "3162 Retrieved extents type-%d from port: count:%d, "
6174 "size:%d\n", type, *extnt_count, *extnt_size);
6175
6176err_exit:
James Smart6d368e52011-05-24 11:44:12 -04006177 mempool_free(mbox, phba->mbox_mem_pool);
6178 return rc;
6179}
6180
6181/**
6182 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
6183 * @phba: Pointer to HBA context object.
6184 * @type: The extent type to check.
6185 *
6186 * This function reads the current available extents from the port and checks
6187 * if the extent count or extent size has changed since the last access.
6188 * Callers use this routine post port reset to understand if there is a
6189 * extent reprovisioning requirement.
6190 *
6191 * Returns:
6192 * -Error: error indicates problem.
6193 * 1: Extent count or size has changed.
6194 * 0: No changes.
6195 **/
6196static int
6197lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
6198{
6199 uint16_t curr_ext_cnt, rsrc_ext_cnt;
6200 uint16_t size_diff, rsrc_ext_size;
6201 int rc = 0;
6202 struct lpfc_rsrc_blks *rsrc_entry;
6203 struct list_head *rsrc_blk_list = NULL;
6204
6205 size_diff = 0;
6206 curr_ext_cnt = 0;
6207 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6208 &rsrc_ext_cnt,
6209 &rsrc_ext_size);
6210 if (unlikely(rc))
6211 return -EIO;
6212
6213 switch (type) {
6214 case LPFC_RSC_TYPE_FCOE_RPI:
6215 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6216 break;
6217 case LPFC_RSC_TYPE_FCOE_VPI:
6218 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
6219 break;
6220 case LPFC_RSC_TYPE_FCOE_XRI:
6221 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6222 break;
6223 case LPFC_RSC_TYPE_FCOE_VFI:
6224 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6225 break;
6226 default:
6227 break;
6228 }
6229
6230 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
6231 curr_ext_cnt++;
6232 if (rsrc_entry->rsrc_size != rsrc_ext_size)
6233 size_diff++;
6234 }
6235
6236 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
6237 rc = 1;
6238
6239 return rc;
6240}
6241
6242/**
6243 * lpfc_sli4_cfg_post_extnts -
6244 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01006245 * @extnt_cnt: number of available extents.
6246 * @type: the extent type (rpi, xri, vfi, vpi).
6247 * @emb: buffer to hold either MBX_EMBED or MBX_NEMBED operation.
6248 * @mbox: pointer to the caller's allocated mailbox structure.
James Smart6d368e52011-05-24 11:44:12 -04006249 *
6250 * This function executes the extents allocation request. It also
6251 * takes care of the amount of memory needed to allocate or get the
6252 * allocated extents. It is the caller's responsibility to evaluate
6253 * the response.
6254 *
6255 * Returns:
6256 * -Error: Error value describes the condition found.
6257 * 0: if successful
6258 **/
6259static int
James Smart8a9d2e82012-05-09 21:16:12 -04006260lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006261 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
6262{
6263 int rc = 0;
6264 uint32_t req_len;
6265 uint32_t emb_len;
6266 uint32_t alloc_len, mbox_tmo;
6267
6268 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04006269 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04006270
6271 /*
6272 * Calculate the size of an embedded mailbox. The uint32_t
6273 * accounts for extents-specific word.
6274 */
6275 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6276 sizeof(uint32_t);
6277
6278 /*
6279 * Presume the allocation and response will fit into an embedded
6280 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6281 */
6282 *emb = LPFC_SLI4_MBX_EMBED;
6283 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04006284 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04006285 sizeof(union lpfc_sli4_cfg_shdr) +
6286 sizeof(uint32_t);
6287 *emb = LPFC_SLI4_MBX_NEMBED;
6288 }
6289
6290 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6291 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
6292 req_len, *emb);
6293 if (alloc_len < req_len) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006294 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartb76f2dc2011-07-22 18:37:42 -04006295 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04006296 "less than the requested DMA memory "
6297 "size (x%x)\n", alloc_len, req_len);
6298 return -ENOMEM;
6299 }
James Smart8a9d2e82012-05-09 21:16:12 -04006300 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04006301 if (unlikely(rc))
6302 return -EIO;
6303
6304 if (!phba->sli4_hba.intr_enable)
6305 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6306 else {
James Smarta183a152011-10-10 21:32:43 -04006307 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006308 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6309 }
6310
6311 if (unlikely(rc))
6312 rc = -EIO;
6313 return rc;
6314}
6315
6316/**
6317 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
6318 * @phba: Pointer to HBA context object.
6319 * @type: The resource extent type to allocate.
6320 *
6321 * This function allocates the number of elements for the specified
6322 * resource type.
6323 **/
6324static int
6325lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
6326{
6327 bool emb = false;
6328 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
6329 uint16_t rsrc_id, rsrc_start, j, k;
6330 uint16_t *ids;
6331 int i, rc;
6332 unsigned long longs;
6333 unsigned long *bmask;
6334 struct lpfc_rsrc_blks *rsrc_blks;
6335 LPFC_MBOXQ_t *mbox;
6336 uint32_t length;
6337 struct lpfc_id_range *id_array = NULL;
6338 void *virtaddr = NULL;
6339 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6340 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6341 struct list_head *ext_blk_list;
6342
6343 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6344 &rsrc_cnt,
6345 &rsrc_size);
6346 if (unlikely(rc))
6347 return -EIO;
6348
6349 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006350 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6d368e52011-05-24 11:44:12 -04006351 "3009 No available Resource Extents "
6352 "for resource type 0x%x: Count: 0x%x, "
6353 "Size 0x%x\n", type, rsrc_cnt,
6354 rsrc_size);
6355 return -ENOMEM;
6356 }
6357
James Smart8a9d2e82012-05-09 21:16:12 -04006358 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
6359 "2903 Post resource extents type-0x%x: "
6360 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04006361
6362 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6363 if (!mbox)
6364 return -ENOMEM;
6365
James Smart8a9d2e82012-05-09 21:16:12 -04006366 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006367 if (unlikely(rc)) {
6368 rc = -EIO;
6369 goto err_exit;
6370 }
6371
6372 /*
6373 * Figure out where the response is located. Then get local pointers
6374 * to the response data. The port does not guarantee to respond to
6375 * all extents counts request so update the local variable with the
6376 * allocated count from the port.
6377 */
6378 if (emb == LPFC_SLI4_MBX_EMBED) {
6379 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6380 id_array = &rsrc_ext->u.rsp.id[0];
6381 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6382 } else {
6383 virtaddr = mbox->sge_array->addr[0];
6384 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6385 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6386 id_array = &n_rsrc->id;
6387 }
6388
6389 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
6390 rsrc_id_cnt = rsrc_cnt * rsrc_size;
6391
6392 /*
6393 * Based on the resource size and count, correct the base and max
6394 * resource values.
6395 */
6396 length = sizeof(struct lpfc_rsrc_blks);
6397 switch (type) {
6398 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07006399 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006400 sizeof(unsigned long),
6401 GFP_KERNEL);
6402 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6403 rc = -ENOMEM;
6404 goto err_exit;
6405 }
Kees Cook6396bb22018-06-12 14:03:40 -07006406 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006407 sizeof(uint16_t),
6408 GFP_KERNEL);
6409 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6410 kfree(phba->sli4_hba.rpi_bmask);
6411 rc = -ENOMEM;
6412 goto err_exit;
6413 }
6414
6415 /*
6416 * The next_rpi was initialized with the maximum available
6417 * count but the port may allocate a smaller number. Catch
6418 * that case and update the next_rpi.
6419 */
6420 phba->sli4_hba.next_rpi = rsrc_id_cnt;
6421
6422 /* Initialize local ptrs for common extent processing later. */
6423 bmask = phba->sli4_hba.rpi_bmask;
6424 ids = phba->sli4_hba.rpi_ids;
6425 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6426 break;
6427 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07006428 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006429 GFP_KERNEL);
6430 if (unlikely(!phba->vpi_bmask)) {
6431 rc = -ENOMEM;
6432 goto err_exit;
6433 }
Kees Cook6396bb22018-06-12 14:03:40 -07006434 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006435 GFP_KERNEL);
6436 if (unlikely(!phba->vpi_ids)) {
6437 kfree(phba->vpi_bmask);
6438 rc = -ENOMEM;
6439 goto err_exit;
6440 }
6441
6442 /* Initialize local ptrs for common extent processing later. */
6443 bmask = phba->vpi_bmask;
6444 ids = phba->vpi_ids;
6445 ext_blk_list = &phba->lpfc_vpi_blk_list;
6446 break;
6447 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07006448 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006449 sizeof(unsigned long),
6450 GFP_KERNEL);
6451 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6452 rc = -ENOMEM;
6453 goto err_exit;
6454 }
James Smart8a9d2e82012-05-09 21:16:12 -04006455 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006456 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006457 sizeof(uint16_t),
6458 GFP_KERNEL);
6459 if (unlikely(!phba->sli4_hba.xri_ids)) {
6460 kfree(phba->sli4_hba.xri_bmask);
6461 rc = -ENOMEM;
6462 goto err_exit;
6463 }
6464
6465 /* Initialize local ptrs for common extent processing later. */
6466 bmask = phba->sli4_hba.xri_bmask;
6467 ids = phba->sli4_hba.xri_ids;
6468 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6469 break;
6470 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07006471 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006472 sizeof(unsigned long),
6473 GFP_KERNEL);
6474 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6475 rc = -ENOMEM;
6476 goto err_exit;
6477 }
Kees Cook6396bb22018-06-12 14:03:40 -07006478 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006479 sizeof(uint16_t),
6480 GFP_KERNEL);
6481 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6482 kfree(phba->sli4_hba.vfi_bmask);
6483 rc = -ENOMEM;
6484 goto err_exit;
6485 }
6486
6487 /* Initialize local ptrs for common extent processing later. */
6488 bmask = phba->sli4_hba.vfi_bmask;
6489 ids = phba->sli4_hba.vfi_ids;
6490 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6491 break;
6492 default:
6493 /* Unsupported Opcode. Fail call. */
6494 id_array = NULL;
6495 bmask = NULL;
6496 ids = NULL;
6497 ext_blk_list = NULL;
6498 goto err_exit;
6499 }
6500
6501 /*
6502 * Complete initializing the extent configuration with the
6503 * allocated ids assigned to this function. The bitmask serves
6504 * as an index into the array and manages the available ids. The
6505 * array just stores the ids communicated to the port via the wqes.
6506 */
6507 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6508 if ((i % 2) == 0)
6509 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6510 &id_array[k]);
6511 else
6512 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6513 &id_array[k]);
6514
6515 rsrc_blks = kzalloc(length, GFP_KERNEL);
6516 if (unlikely(!rsrc_blks)) {
6517 rc = -ENOMEM;
6518 kfree(bmask);
6519 kfree(ids);
6520 goto err_exit;
6521 }
6522 rsrc_blks->rsrc_start = rsrc_id;
6523 rsrc_blks->rsrc_size = rsrc_size;
6524 list_add_tail(&rsrc_blks->list, ext_blk_list);
6525 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08006526 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart5e5b5112019-01-28 11:14:22 -08006527 phba->sli4_hba.io_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08006528 lpfc_sli4_get_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08006529 }
James Smart6d368e52011-05-24 11:44:12 -04006530
6531 while (rsrc_id < (rsrc_start + rsrc_size)) {
6532 ids[j] = rsrc_id;
6533 rsrc_id++;
6534 j++;
6535 }
6536 /* Entire word processed. Get next word.*/
6537 if ((i % 2) == 1)
6538 k++;
6539 }
6540 err_exit:
6541 lpfc_sli4_mbox_cmd_free(phba, mbox);
6542 return rc;
6543}
6544
James Smart895427b2017-02-12 13:52:30 -08006545
6546
James Smart6d368e52011-05-24 11:44:12 -04006547/**
6548 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6549 * @phba: Pointer to HBA context object.
6550 * @type: the extent's type.
6551 *
6552 * This function deallocates all extents of a particular resource type.
6553 * SLI4 does not allow for deallocating a particular extent range. It
6554 * is the caller's responsibility to release all kernel memory resources.
6555 **/
6556static int
6557lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6558{
6559 int rc;
6560 uint32_t length, mbox_tmo = 0;
6561 LPFC_MBOXQ_t *mbox;
6562 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6563 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6564
6565 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6566 if (!mbox)
6567 return -ENOMEM;
6568
6569 /*
6570 * This function sends an embedded mailbox because it only sends the
6571 * the resource type. All extents of this type are released by the
6572 * port.
6573 */
6574 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6575 sizeof(struct lpfc_sli4_cfg_mhdr));
6576 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6577 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6578 length, LPFC_SLI4_MBX_EMBED);
6579
6580 /* Send an extents count of 0 - the dealloc doesn't use it. */
6581 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6582 LPFC_SLI4_MBX_EMBED);
6583 if (unlikely(rc)) {
6584 rc = -EIO;
6585 goto out_free_mbox;
6586 }
6587 if (!phba->sli4_hba.intr_enable)
6588 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6589 else {
James Smarta183a152011-10-10 21:32:43 -04006590 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006591 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6592 }
6593 if (unlikely(rc)) {
6594 rc = -EIO;
6595 goto out_free_mbox;
6596 }
6597
6598 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6599 if (bf_get(lpfc_mbox_hdr_status,
6600 &dealloc_rsrc->header.cfg_shdr.response)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006601 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6d368e52011-05-24 11:44:12 -04006602 "2919 Failed to release resource extents "
6603 "for type %d - Status 0x%x Add'l Status 0x%x. "
6604 "Resource memory not released.\n",
6605 type,
6606 bf_get(lpfc_mbox_hdr_status,
6607 &dealloc_rsrc->header.cfg_shdr.response),
6608 bf_get(lpfc_mbox_hdr_add_status,
6609 &dealloc_rsrc->header.cfg_shdr.response));
6610 rc = -EIO;
6611 goto out_free_mbox;
6612 }
6613
6614 /* Release kernel memory resources for the specific type. */
6615 switch (type) {
6616 case LPFC_RSC_TYPE_FCOE_VPI:
6617 kfree(phba->vpi_bmask);
6618 kfree(phba->vpi_ids);
6619 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6620 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6621 &phba->lpfc_vpi_blk_list, list) {
6622 list_del_init(&rsrc_blk->list);
6623 kfree(rsrc_blk);
6624 }
James Smart16a3a202013-04-17 20:14:38 -04006625 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006626 break;
6627 case LPFC_RSC_TYPE_FCOE_XRI:
6628 kfree(phba->sli4_hba.xri_bmask);
6629 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006630 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6631 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6632 list_del_init(&rsrc_blk->list);
6633 kfree(rsrc_blk);
6634 }
6635 break;
6636 case LPFC_RSC_TYPE_FCOE_VFI:
6637 kfree(phba->sli4_hba.vfi_bmask);
6638 kfree(phba->sli4_hba.vfi_ids);
6639 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6640 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6641 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6642 list_del_init(&rsrc_blk->list);
6643 kfree(rsrc_blk);
6644 }
6645 break;
6646 case LPFC_RSC_TYPE_FCOE_RPI:
6647 /* RPI bitmask and physical id array are cleaned up earlier. */
6648 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6649 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6650 list_del_init(&rsrc_blk->list);
6651 kfree(rsrc_blk);
6652 }
6653 break;
6654 default:
6655 break;
6656 }
6657
6658 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6659
6660 out_free_mbox:
6661 mempool_free(mbox, phba->mbox_mem_pool);
6662 return rc;
6663}
6664
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006665static void
James Smart7bdedb32016-07-06 12:36:00 -07006666lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6667 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006668{
James Smart65791f12016-07-06 12:35:56 -07006669 uint32_t len;
James Smart9064aeb2021-08-16 09:28:50 -07006670 u32 sig_freq = 0;
James Smart65791f12016-07-06 12:35:56 -07006671
James Smart65791f12016-07-06 12:35:56 -07006672 len = sizeof(struct lpfc_mbx_set_feature) -
6673 sizeof(struct lpfc_sli4_cfg_mhdr);
6674 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6675 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6676 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006677
James Smart7bdedb32016-07-06 12:36:00 -07006678 switch (feature) {
6679 case LPFC_SET_UE_RECOVERY:
6680 bf_set(lpfc_mbx_set_feature_UER,
6681 &mbox->u.mqe.un.set_feature, 1);
6682 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6683 mbox->u.mqe.un.set_feature.param_len = 8;
6684 break;
6685 case LPFC_SET_MDS_DIAGS:
6686 bf_set(lpfc_mbx_set_feature_mds,
6687 &mbox->u.mqe.un.set_feature, 1);
6688 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006689 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006690 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6691 mbox->u.mqe.un.set_feature.param_len = 8;
6692 break;
James Smart9064aeb2021-08-16 09:28:50 -07006693 case LPFC_SET_CGN_SIGNAL:
6694 if (phba->cmf_active_mode == LPFC_CFG_OFF)
6695 sig_freq = 0;
6696 else
6697 sig_freq = phba->cgn_sig_freq;
6698
6699 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
6700 bf_set(lpfc_mbx_set_feature_CGN_alarm_freq,
6701 &mbox->u.mqe.un.set_feature, sig_freq);
6702 bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6703 &mbox->u.mqe.un.set_feature, sig_freq);
6704 }
6705
6706 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY)
6707 bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6708 &mbox->u.mqe.un.set_feature, sig_freq);
6709
6710 if (phba->cmf_active_mode == LPFC_CFG_OFF ||
6711 phba->cgn_reg_signal == EDC_CG_SIG_NOTSUPPORTED)
6712 sig_freq = 0;
6713 else
6714 sig_freq = lpfc_acqe_cgn_frequency;
6715
6716 bf_set(lpfc_mbx_set_feature_CGN_acqe_freq,
6717 &mbox->u.mqe.un.set_feature, sig_freq);
6718
6719 mbox->u.mqe.un.set_feature.feature = LPFC_SET_CGN_SIGNAL;
6720 mbox->u.mqe.un.set_feature.param_len = 12;
6721 break;
James Smart171f6c42019-11-04 16:57:07 -08006722 case LPFC_SET_DUAL_DUMP:
6723 bf_set(lpfc_mbx_set_feature_dd,
6724 &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
6725 bf_set(lpfc_mbx_set_feature_ddquery,
6726 &mbox->u.mqe.un.set_feature, 0);
6727 mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
6728 mbox->u.mqe.un.set_feature.param_len = 4;
6729 break;
James Smartc6a5c7472021-08-16 09:28:48 -07006730 case LPFC_SET_ENABLE_MI:
6731 mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_MI;
6732 mbox->u.mqe.un.set_feature.param_len = 4;
6733 bf_set(lpfc_mbx_set_feature_milunq, &mbox->u.mqe.un.set_feature,
6734 phba->pport->cfg_lun_queue_depth);
6735 bf_set(lpfc_mbx_set_feature_mi, &mbox->u.mqe.un.set_feature,
6736 phba->sli4_hba.pc_sli4_params.mi_ver);
6737 break;
James Smart02243832021-08-16 09:28:54 -07006738 case LPFC_SET_ENABLE_CMF:
6739 bf_set(lpfc_mbx_set_feature_dd, &mbox->u.mqe.un.set_feature, 1);
6740 mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_CMF;
6741 mbox->u.mqe.un.set_feature.param_len = 4;
6742 bf_set(lpfc_mbx_set_feature_cmf,
6743 &mbox->u.mqe.un.set_feature, 1);
6744 break;
James Smart65791f12016-07-06 12:35:56 -07006745 }
James Smart7bdedb32016-07-06 12:36:00 -07006746 return;
James Smart65791f12016-07-06 12:35:56 -07006747}
6748
James Smart6d368e52011-05-24 11:44:12 -04006749/**
James Smart1165a5c2018-11-29 16:09:39 -08006750 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6751 * @phba: Pointer to HBA context object.
6752 *
6753 * Disable FW logging into host memory on the adapter. To
6754 * be done before reading logs from the host memory.
6755 **/
6756void
6757lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6758{
6759 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6760
James Smart95bfc6d2019-10-18 14:18:27 -07006761 spin_lock_irq(&phba->hbalock);
6762 ras_fwlog->state = INACTIVE;
6763 spin_unlock_irq(&phba->hbalock);
James Smart1165a5c2018-11-29 16:09:39 -08006764
6765 /* Disable FW logging to host memory */
6766 writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6767 phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
James Smart95bfc6d2019-10-18 14:18:27 -07006768
6769 /* Wait 10ms for firmware to stop using DMA buffer */
6770 usleep_range(10 * 1000, 20 * 1000);
James Smart1165a5c2018-11-29 16:09:39 -08006771}
6772
6773/**
James Smartd2cc9bc2018-09-10 10:30:50 -07006774 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6775 * @phba: Pointer to HBA context object.
6776 *
6777 * This function is called to free memory allocated for RAS FW logging
6778 * support in the driver.
6779 **/
6780void
6781lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6782{
6783 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6784 struct lpfc_dmabuf *dmabuf, *next;
6785
6786 if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6787 list_for_each_entry_safe(dmabuf, next,
6788 &ras_fwlog->fwlog_buff_list,
6789 list) {
6790 list_del(&dmabuf->list);
6791 dma_free_coherent(&phba->pcidev->dev,
6792 LPFC_RAS_MAX_ENTRY_SIZE,
6793 dmabuf->virt, dmabuf->phys);
6794 kfree(dmabuf);
6795 }
6796 }
6797
6798 if (ras_fwlog->lwpd.virt) {
6799 dma_free_coherent(&phba->pcidev->dev,
6800 sizeof(uint32_t) * 2,
6801 ras_fwlog->lwpd.virt,
6802 ras_fwlog->lwpd.phys);
6803 ras_fwlog->lwpd.virt = NULL;
6804 }
6805
James Smart95bfc6d2019-10-18 14:18:27 -07006806 spin_lock_irq(&phba->hbalock);
6807 ras_fwlog->state = INACTIVE;
6808 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07006809}
6810
6811/**
6812 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6813 * @phba: Pointer to HBA context object.
6814 * @fwlog_buff_count: Count of buffers to be created.
6815 *
6816 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6817 * to update FW log is posted to the adapter.
6818 * Buffer count is calculated based on module param ras_fwlog_buffsize
6819 * Size of each buffer posted to FW is 64K.
6820 **/
6821
6822static int
6823lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6824 uint32_t fwlog_buff_count)
6825{
6826 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6827 struct lpfc_dmabuf *dmabuf;
6828 int rc = 0, i = 0;
6829
6830 /* Initialize List */
6831 INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6832
6833 /* Allocate memory for the LWPD */
6834 ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6835 sizeof(uint32_t) * 2,
6836 &ras_fwlog->lwpd.phys,
6837 GFP_KERNEL);
6838 if (!ras_fwlog->lwpd.virt) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006839 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006840 "6185 LWPD Memory Alloc Failed\n");
6841
6842 return -ENOMEM;
6843 }
6844
6845 ras_fwlog->fw_buffcount = fwlog_buff_count;
6846 for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6847 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6848 GFP_KERNEL);
6849 if (!dmabuf) {
6850 rc = -ENOMEM;
6851 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6852 "6186 Memory Alloc failed FW logging");
6853 goto free_mem;
6854 }
6855
Luis Chamberlain750afb02019-01-04 09:23:09 +01006856 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartd2cc9bc2018-09-10 10:30:50 -07006857 LPFC_RAS_MAX_ENTRY_SIZE,
Luis Chamberlain750afb02019-01-04 09:23:09 +01006858 &dmabuf->phys, GFP_KERNEL);
James Smartd2cc9bc2018-09-10 10:30:50 -07006859 if (!dmabuf->virt) {
6860 kfree(dmabuf);
6861 rc = -ENOMEM;
6862 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6863 "6187 DMA Alloc Failed FW logging");
6864 goto free_mem;
6865 }
James Smartd2cc9bc2018-09-10 10:30:50 -07006866 dmabuf->buffer_tag = i;
6867 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6868 }
6869
6870free_mem:
6871 if (rc)
6872 lpfc_sli4_ras_dma_free(phba);
6873
6874 return rc;
6875}
6876
6877/**
6878 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6879 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +01006880 * @pmb: pointer to the driver internal queue element for mailbox command.
James Smartd2cc9bc2018-09-10 10:30:50 -07006881 *
6882 * Completion handler for driver's RAS MBX command to the device.
6883 **/
6884static void
6885lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6886{
6887 MAILBOX_t *mb;
6888 union lpfc_sli4_cfg_shdr *shdr;
6889 uint32_t shdr_status, shdr_add_status;
6890 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6891
6892 mb = &pmb->u.mb;
6893
6894 shdr = (union lpfc_sli4_cfg_shdr *)
6895 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6896 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6897 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6898
6899 if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006900 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006901 "6188 FW LOG mailbox "
6902 "completed with status x%x add_status x%x,"
6903 " mbx status x%x\n",
6904 shdr_status, shdr_add_status, mb->mbxStatus);
James Smartcb349902018-11-29 16:09:27 -08006905
6906 ras_fwlog->ras_hwsupport = false;
James Smartd2cc9bc2018-09-10 10:30:50 -07006907 goto disable_ras;
6908 }
6909
James Smart95bfc6d2019-10-18 14:18:27 -07006910 spin_lock_irq(&phba->hbalock);
6911 ras_fwlog->state = ACTIVE;
6912 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07006913 mempool_free(pmb, phba->mbox_mem_pool);
6914
6915 return;
6916
6917disable_ras:
6918 /* Free RAS DMA memory */
6919 lpfc_sli4_ras_dma_free(phba);
6920 mempool_free(pmb, phba->mbox_mem_pool);
6921}
6922
6923/**
6924 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6925 * @phba: pointer to lpfc hba data structure.
6926 * @fwlog_level: Logging verbosity level.
6927 * @fwlog_enable: Enable/Disable logging.
6928 *
6929 * Initialize memory and post mailbox command to enable FW logging in host
6930 * memory.
6931 **/
6932int
6933lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6934 uint32_t fwlog_level,
6935 uint32_t fwlog_enable)
6936{
6937 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6938 struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6939 struct lpfc_dmabuf *dmabuf;
6940 LPFC_MBOXQ_t *mbox;
6941 uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6942 int rc = 0;
6943
James Smart95bfc6d2019-10-18 14:18:27 -07006944 spin_lock_irq(&phba->hbalock);
6945 ras_fwlog->state = INACTIVE;
6946 spin_unlock_irq(&phba->hbalock);
6947
James Smartd2cc9bc2018-09-10 10:30:50 -07006948 fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6949 phba->cfg_ras_fwlog_buffsize);
6950 fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6951
6952 /*
6953 * If re-enabling FW logging support use earlier allocated
6954 * DMA buffers while posting MBX command.
6955 **/
6956 if (!ras_fwlog->lwpd.virt) {
6957 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6958 if (rc) {
6959 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smartcb349902018-11-29 16:09:27 -08006960 "6189 FW Log Memory Allocation Failed");
James Smartd2cc9bc2018-09-10 10:30:50 -07006961 return rc;
6962 }
6963 }
6964
6965 /* Setup Mailbox command */
6966 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6967 if (!mbox) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006968 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006969 "6190 RAS MBX Alloc Failed");
6970 rc = -ENOMEM;
6971 goto mem_free;
6972 }
6973
6974 ras_fwlog->fw_loglevel = fwlog_level;
6975 len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6976 sizeof(struct lpfc_sli4_cfg_mhdr));
6977
6978 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6979 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6980 len, LPFC_SLI4_MBX_EMBED);
6981
6982 mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6983 bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6984 fwlog_enable);
6985 bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6986 ras_fwlog->fw_loglevel);
6987 bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6988 ras_fwlog->fw_buffcount);
6989 bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6990 LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6991
6992 /* Update DMA buffer address */
6993 list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6994 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6995
6996 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6997 putPaddrLow(dmabuf->phys);
6998
6999 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
7000 putPaddrHigh(dmabuf->phys);
7001 }
7002
7003 /* Update LPWD address */
7004 mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
7005 mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
7006
James Smart95bfc6d2019-10-18 14:18:27 -07007007 spin_lock_irq(&phba->hbalock);
7008 ras_fwlog->state = REG_INPROGRESS;
7009 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07007010 mbox->vport = phba->pport;
7011 mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
7012
7013 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7014
7015 if (rc == MBX_NOT_FINISHED) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007016 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcb349902018-11-29 16:09:27 -08007017 "6191 FW-Log Mailbox failed. "
James Smartd2cc9bc2018-09-10 10:30:50 -07007018 "status %d mbxStatus : x%x", rc,
7019 bf_get(lpfc_mqe_status, &mbox->u.mqe));
7020 mempool_free(mbox, phba->mbox_mem_pool);
7021 rc = -EIO;
7022 goto mem_free;
7023 } else
7024 rc = 0;
7025mem_free:
7026 if (rc)
7027 lpfc_sli4_ras_dma_free(phba);
7028
7029 return rc;
7030}
7031
7032/**
7033 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
7034 * @phba: Pointer to HBA context object.
7035 *
7036 * Check if RAS is supported on the adapter and initialize it.
7037 **/
7038void
7039lpfc_sli4_ras_setup(struct lpfc_hba *phba)
7040{
7041 /* Check RAS FW Log needs to be enabled or not */
7042 if (lpfc_check_fwlog_support(phba))
7043 return;
7044
7045 lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
7046 LPFC_RAS_ENABLE_LOGGING);
7047}
7048
7049/**
James Smart6d368e52011-05-24 11:44:12 -04007050 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
7051 * @phba: Pointer to HBA context object.
7052 *
7053 * This function allocates all SLI4 resource identifiers.
7054 **/
7055int
7056lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
7057{
7058 int i, rc, error = 0;
7059 uint16_t count, base;
7060 unsigned long longs;
7061
James Smartff78d8f2011-12-13 13:21:35 -05007062 if (!phba->sli4_hba.rpi_hdrs_in_use)
7063 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04007064 if (phba->sli4_hba.extents_in_use) {
7065 /*
7066 * The port supports resource extents. The XRI, VPI, VFI, RPI
7067 * resource extent count must be read and allocated before
7068 * provisioning the resource id arrays.
7069 */
7070 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7071 LPFC_IDX_RSRC_RDY) {
7072 /*
7073 * Extent-based resources are set - the driver could
7074 * be in a port reset. Figure out if any corrective
7075 * actions need to be taken.
7076 */
7077 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7078 LPFC_RSC_TYPE_FCOE_VFI);
7079 if (rc != 0)
7080 error++;
7081 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7082 LPFC_RSC_TYPE_FCOE_VPI);
7083 if (rc != 0)
7084 error++;
7085 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7086 LPFC_RSC_TYPE_FCOE_XRI);
7087 if (rc != 0)
7088 error++;
7089 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7090 LPFC_RSC_TYPE_FCOE_RPI);
7091 if (rc != 0)
7092 error++;
7093
7094 /*
7095 * It's possible that the number of resources
7096 * provided to this port instance changed between
7097 * resets. Detect this condition and reallocate
7098 * resources. Otherwise, there is no action.
7099 */
7100 if (error) {
7101 lpfc_printf_log(phba, KERN_INFO,
7102 LOG_MBOX | LOG_INIT,
7103 "2931 Detected extent resource "
7104 "change. Reallocating all "
7105 "extents.\n");
7106 rc = lpfc_sli4_dealloc_extent(phba,
7107 LPFC_RSC_TYPE_FCOE_VFI);
7108 rc = lpfc_sli4_dealloc_extent(phba,
7109 LPFC_RSC_TYPE_FCOE_VPI);
7110 rc = lpfc_sli4_dealloc_extent(phba,
7111 LPFC_RSC_TYPE_FCOE_XRI);
7112 rc = lpfc_sli4_dealloc_extent(phba,
7113 LPFC_RSC_TYPE_FCOE_RPI);
7114 } else
7115 return 0;
7116 }
7117
7118 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7119 if (unlikely(rc))
7120 goto err_exit;
7121
7122 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7123 if (unlikely(rc))
7124 goto err_exit;
7125
7126 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7127 if (unlikely(rc))
7128 goto err_exit;
7129
7130 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7131 if (unlikely(rc))
7132 goto err_exit;
7133 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7134 LPFC_IDX_RSRC_RDY);
7135 return rc;
7136 } else {
7137 /*
7138 * The port does not support resource extents. The XRI, VPI,
7139 * VFI, RPI resource ids were determined from READ_CONFIG.
7140 * Just allocate the bitmasks and provision the resource id
7141 * arrays. If a port reset is active, the resources don't
7142 * need any action - just exit.
7143 */
7144 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05007145 LPFC_IDX_RSRC_RDY) {
7146 lpfc_sli4_dealloc_resource_identifiers(phba);
7147 lpfc_sli4_remove_rpis(phba);
7148 }
James Smart6d368e52011-05-24 11:44:12 -04007149 /* RPIs. */
7150 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05007151 if (count <= 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007152 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a630c22013-01-03 15:44:09 -05007153 "3279 Invalid provisioning of "
7154 "rpi:%d\n", count);
7155 rc = -EINVAL;
7156 goto err_exit;
7157 }
James Smart6d368e52011-05-24 11:44:12 -04007158 base = phba->sli4_hba.max_cfg_param.rpi_base;
7159 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07007160 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04007161 sizeof(unsigned long),
7162 GFP_KERNEL);
7163 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
7164 rc = -ENOMEM;
7165 goto err_exit;
7166 }
Kees Cook6396bb22018-06-12 14:03:40 -07007167 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04007168 GFP_KERNEL);
7169 if (unlikely(!phba->sli4_hba.rpi_ids)) {
7170 rc = -ENOMEM;
7171 goto free_rpi_bmask;
7172 }
7173
7174 for (i = 0; i < count; i++)
7175 phba->sli4_hba.rpi_ids[i] = base + i;
7176
7177 /* VPIs. */
7178 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05007179 if (count <= 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007180 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a630c22013-01-03 15:44:09 -05007181 "3280 Invalid provisioning of "
7182 "vpi:%d\n", count);
7183 rc = -EINVAL;
7184 goto free_rpi_ids;
7185 }
James Smart6d368e52011-05-24 11:44:12 -04007186 base = phba->sli4_hba.max_cfg_param.vpi_base;
7187 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07007188 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04007189 GFP_KERNEL);
7190 if (unlikely(!phba->vpi_bmask)) {
7191 rc = -ENOMEM;
7192 goto free_rpi_ids;
7193 }
Kees Cook6396bb22018-06-12 14:03:40 -07007194 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04007195 GFP_KERNEL);
7196 if (unlikely(!phba->vpi_ids)) {
7197 rc = -ENOMEM;
7198 goto free_vpi_bmask;
7199 }
7200
7201 for (i = 0; i < count; i++)
7202 phba->vpi_ids[i] = base + i;
7203
7204 /* XRIs. */
7205 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05007206 if (count <= 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007207 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a630c22013-01-03 15:44:09 -05007208 "3281 Invalid provisioning of "
7209 "xri:%d\n", count);
7210 rc = -EINVAL;
7211 goto free_vpi_ids;
7212 }
James Smart6d368e52011-05-24 11:44:12 -04007213 base = phba->sli4_hba.max_cfg_param.xri_base;
7214 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07007215 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04007216 sizeof(unsigned long),
7217 GFP_KERNEL);
7218 if (unlikely(!phba->sli4_hba.xri_bmask)) {
7219 rc = -ENOMEM;
7220 goto free_vpi_ids;
7221 }
James Smart41899be2012-03-01 22:34:19 -05007222 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07007223 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04007224 GFP_KERNEL);
7225 if (unlikely(!phba->sli4_hba.xri_ids)) {
7226 rc = -ENOMEM;
7227 goto free_xri_bmask;
7228 }
7229
7230 for (i = 0; i < count; i++)
7231 phba->sli4_hba.xri_ids[i] = base + i;
7232
7233 /* VFIs. */
7234 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05007235 if (count <= 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007236 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a630c22013-01-03 15:44:09 -05007237 "3282 Invalid provisioning of "
7238 "vfi:%d\n", count);
7239 rc = -EINVAL;
7240 goto free_xri_ids;
7241 }
James Smart6d368e52011-05-24 11:44:12 -04007242 base = phba->sli4_hba.max_cfg_param.vfi_base;
7243 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07007244 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04007245 sizeof(unsigned long),
7246 GFP_KERNEL);
7247 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
7248 rc = -ENOMEM;
7249 goto free_xri_ids;
7250 }
Kees Cook6396bb22018-06-12 14:03:40 -07007251 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04007252 GFP_KERNEL);
7253 if (unlikely(!phba->sli4_hba.vfi_ids)) {
7254 rc = -ENOMEM;
7255 goto free_vfi_bmask;
7256 }
7257
7258 for (i = 0; i < count; i++)
7259 phba->sli4_hba.vfi_ids[i] = base + i;
7260
7261 /*
7262 * Mark all resources ready. An HBA reset doesn't need
7263 * to reset the initialization.
7264 */
7265 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7266 LPFC_IDX_RSRC_RDY);
7267 return 0;
7268 }
7269
7270 free_vfi_bmask:
7271 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01007272 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007273 free_xri_ids:
7274 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01007275 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007276 free_xri_bmask:
7277 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01007278 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007279 free_vpi_ids:
7280 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01007281 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007282 free_vpi_bmask:
7283 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01007284 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007285 free_rpi_ids:
7286 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01007287 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007288 free_rpi_bmask:
7289 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01007290 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007291 err_exit:
7292 return rc;
7293}
7294
7295/**
7296 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
7297 * @phba: Pointer to HBA context object.
7298 *
7299 * This function allocates the number of elements for the specified
7300 * resource type.
7301 **/
7302int
7303lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
7304{
7305 if (phba->sli4_hba.extents_in_use) {
7306 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7307 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7308 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7309 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7310 } else {
7311 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04007312 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04007313 kfree(phba->vpi_ids);
7314 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7315 kfree(phba->sli4_hba.xri_bmask);
7316 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04007317 kfree(phba->sli4_hba.vfi_bmask);
7318 kfree(phba->sli4_hba.vfi_ids);
7319 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7320 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7321 }
7322
7323 return 0;
7324}
7325
7326/**
James Smartb76f2dc2011-07-22 18:37:42 -04007327 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
7328 * @phba: Pointer to HBA context object.
7329 * @type: The resource extent type.
Lee Jones7af29d42020-07-21 17:41:31 +01007330 * @extnt_cnt: buffer to hold port extent count response
James Smartb76f2dc2011-07-22 18:37:42 -04007331 * @extnt_size: buffer to hold port extent size response.
7332 *
7333 * This function calls the port to read the host allocated extents
7334 * for a particular type.
7335 **/
7336int
7337lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
7338 uint16_t *extnt_cnt, uint16_t *extnt_size)
7339{
7340 bool emb;
7341 int rc = 0;
7342 uint16_t curr_blks = 0;
7343 uint32_t req_len, emb_len;
7344 uint32_t alloc_len, mbox_tmo;
7345 struct list_head *blk_list_head;
7346 struct lpfc_rsrc_blks *rsrc_blk;
7347 LPFC_MBOXQ_t *mbox;
7348 void *virtaddr = NULL;
7349 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
7350 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
7351 union lpfc_sli4_cfg_shdr *shdr;
7352
7353 switch (type) {
7354 case LPFC_RSC_TYPE_FCOE_VPI:
7355 blk_list_head = &phba->lpfc_vpi_blk_list;
7356 break;
7357 case LPFC_RSC_TYPE_FCOE_XRI:
7358 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
7359 break;
7360 case LPFC_RSC_TYPE_FCOE_VFI:
7361 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
7362 break;
7363 case LPFC_RSC_TYPE_FCOE_RPI:
7364 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
7365 break;
7366 default:
7367 return -EIO;
7368 }
7369
7370 /* Count the number of extents currently allocatd for this type. */
7371 list_for_each_entry(rsrc_blk, blk_list_head, list) {
7372 if (curr_blks == 0) {
7373 /*
7374 * The GET_ALLOCATED mailbox does not return the size,
7375 * just the count. The size should be just the size
7376 * stored in the current allocated block and all sizes
7377 * for an extent type are the same so set the return
7378 * value now.
7379 */
7380 *extnt_size = rsrc_blk->rsrc_size;
7381 }
7382 curr_blks++;
7383 }
7384
James Smartb76f2dc2011-07-22 18:37:42 -04007385 /*
7386 * Calculate the size of an embedded mailbox. The uint32_t
7387 * accounts for extents-specific word.
7388 */
7389 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
7390 sizeof(uint32_t);
7391
7392 /*
7393 * Presume the allocation and response will fit into an embedded
7394 * mailbox. If not true, reconfigure to a non-embedded mailbox.
7395 */
7396 emb = LPFC_SLI4_MBX_EMBED;
7397 req_len = emb_len;
7398 if (req_len > emb_len) {
7399 req_len = curr_blks * sizeof(uint16_t) +
7400 sizeof(union lpfc_sli4_cfg_shdr) +
7401 sizeof(uint32_t);
7402 emb = LPFC_SLI4_MBX_NEMBED;
7403 }
7404
7405 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7406 if (!mbox)
7407 return -ENOMEM;
7408 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
7409
7410 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7411 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
7412 req_len, emb);
7413 if (alloc_len < req_len) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007414 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartb76f2dc2011-07-22 18:37:42 -04007415 "2983 Allocated DMA memory size (x%x) is "
7416 "less than the requested DMA memory "
7417 "size (x%x)\n", alloc_len, req_len);
7418 rc = -ENOMEM;
7419 goto err_exit;
7420 }
7421 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
7422 if (unlikely(rc)) {
7423 rc = -EIO;
7424 goto err_exit;
7425 }
7426
7427 if (!phba->sli4_hba.intr_enable)
7428 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
7429 else {
James Smarta183a152011-10-10 21:32:43 -04007430 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04007431 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
7432 }
7433
7434 if (unlikely(rc)) {
7435 rc = -EIO;
7436 goto err_exit;
7437 }
7438
7439 /*
7440 * Figure out where the response is located. Then get local pointers
7441 * to the response data. The port does not guarantee to respond to
7442 * all extents counts request so update the local variable with the
7443 * allocated count from the port.
7444 */
7445 if (emb == LPFC_SLI4_MBX_EMBED) {
7446 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
7447 shdr = &rsrc_ext->header.cfg_shdr;
7448 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
7449 } else {
7450 virtaddr = mbox->sge_array->addr[0];
7451 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
7452 shdr = &n_rsrc->cfg_shdr;
7453 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
7454 }
7455
7456 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007457 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartb76f2dc2011-07-22 18:37:42 -04007458 "2984 Failed to read allocated resources "
7459 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
7460 type,
7461 bf_get(lpfc_mbox_hdr_status, &shdr->response),
7462 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
7463 rc = -EIO;
7464 goto err_exit;
7465 }
7466 err_exit:
7467 lpfc_sli4_mbox_cmd_free(phba, mbox);
7468 return rc;
7469}
7470
7471/**
James Smart0ef69962017-04-21 16:04:50 -07007472 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04007473 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08007474 * @sgl_list: linked link of sgl buffers to post
7475 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04007476 *
James Smart895427b2017-02-12 13:52:30 -08007477 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04007478 * repost them to the port by using SGL block post. This is needed after a
7479 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08007480 * of buffer sgls which contains contiguous xris and uses the non-embedded
7481 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04007482 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
7483 * mailbox command for posting.
7484 *
7485 * Returns: 0 = success, non-zero failure.
7486 **/
7487static int
James Smart895427b2017-02-12 13:52:30 -08007488lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
7489 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04007490{
7491 struct lpfc_sglq *sglq_entry = NULL;
7492 struct lpfc_sglq *sglq_entry_next = NULL;
7493 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08007494 int status, total_cnt;
7495 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04007496 int last_xritag = NO_XRI;
7497 LIST_HEAD(prep_sgl_list);
7498 LIST_HEAD(blck_sgl_list);
7499 LIST_HEAD(allc_sgl_list);
7500 LIST_HEAD(post_sgl_list);
7501 LIST_HEAD(free_sgl_list);
7502
James Smart38c20672013-03-01 16:37:44 -05007503 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007504 spin_lock(&phba->sli4_hba.sgl_list_lock);
7505 list_splice_init(sgl_list, &allc_sgl_list);
7506 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007507 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007508
James Smart895427b2017-02-12 13:52:30 -08007509 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007510 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
7511 &allc_sgl_list, list) {
7512 list_del_init(&sglq_entry->list);
7513 block_cnt++;
7514 if ((last_xritag != NO_XRI) &&
7515 (sglq_entry->sli4_xritag != last_xritag + 1)) {
7516 /* a hole in xri block, form a sgl posting block */
7517 list_splice_init(&prep_sgl_list, &blck_sgl_list);
7518 post_cnt = block_cnt - 1;
7519 /* prepare list for next posting block */
7520 list_add_tail(&sglq_entry->list, &prep_sgl_list);
7521 block_cnt = 1;
7522 } else {
7523 /* prepare list for next posting block */
7524 list_add_tail(&sglq_entry->list, &prep_sgl_list);
7525 /* enough sgls for non-embed sgl mbox command */
7526 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
7527 list_splice_init(&prep_sgl_list,
7528 &blck_sgl_list);
7529 post_cnt = block_cnt;
7530 block_cnt = 0;
7531 }
7532 }
7533 num_posted++;
7534
7535 /* keep track of last sgl's xritag */
7536 last_xritag = sglq_entry->sli4_xritag;
7537
James Smart895427b2017-02-12 13:52:30 -08007538 /* end of repost sgl list condition for buffers */
7539 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04007540 if (post_cnt == 0) {
7541 list_splice_init(&prep_sgl_list,
7542 &blck_sgl_list);
7543 post_cnt = block_cnt;
7544 } else if (block_cnt == 1) {
7545 status = lpfc_sli4_post_sgl(phba,
7546 sglq_entry->phys, 0,
7547 sglq_entry->sli4_xritag);
7548 if (!status) {
7549 /* successful, put sgl to posted list */
7550 list_add_tail(&sglq_entry->list,
7551 &post_sgl_list);
7552 } else {
7553 /* Failure, put sgl to free list */
7554 lpfc_printf_log(phba, KERN_WARNING,
7555 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007556 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04007557 "sgl, xritag:x%x\n",
7558 sglq_entry->sli4_xritag);
7559 list_add_tail(&sglq_entry->list,
7560 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007561 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04007562 }
7563 }
7564 }
7565
7566 /* continue until a nembed page worth of sgls */
7567 if (post_cnt == 0)
7568 continue;
7569
James Smart895427b2017-02-12 13:52:30 -08007570 /* post the buffer list sgls as a block */
7571 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7572 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04007573
7574 if (!status) {
7575 /* success, put sgl list to posted sgl list */
7576 list_splice_init(&blck_sgl_list, &post_sgl_list);
7577 } else {
7578 /* Failure, put sgl list to free sgl list */
7579 sglq_entry_first = list_first_entry(&blck_sgl_list,
7580 struct lpfc_sglq,
7581 list);
7582 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007583 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04007584 "xritag:x%x-x%x\n",
7585 sglq_entry_first->sli4_xritag,
7586 (sglq_entry_first->sli4_xritag +
7587 post_cnt - 1));
7588 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007589 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007590 }
7591
7592 /* don't reset xirtag due to hole in xri block */
7593 if (block_cnt == 0)
7594 last_xritag = NO_XRI;
7595
James Smart895427b2017-02-12 13:52:30 -08007596 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04007597 post_cnt = 0;
7598 }
7599
James Smart895427b2017-02-12 13:52:30 -08007600 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04007601 lpfc_free_sgl_list(phba, &free_sgl_list);
7602
James Smart895427b2017-02-12 13:52:30 -08007603 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04007604 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05007605 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007606 spin_lock(&phba->sli4_hba.sgl_list_lock);
7607 list_splice_init(&post_sgl_list, sgl_list);
7608 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007609 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007610 } else {
Dick Kennedy372c1872020-06-30 14:50:00 -07007611 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08007612 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04007613 return -EIO;
7614 }
James Smart895427b2017-02-12 13:52:30 -08007615
7616 /* return the number of XRIs actually posted */
7617 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007618}
7619
James Smart0794d602019-01-28 11:14:19 -08007620/**
James Smart5e5b5112019-01-28 11:14:22 -08007621 * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
James Smart0794d602019-01-28 11:14:19 -08007622 * @phba: pointer to lpfc hba data structure.
7623 *
7624 * This routine walks the list of nvme buffers that have been allocated and
7625 * repost them to the port by using SGL block post. This is needed after a
7626 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7627 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
James Smart5e5b5112019-01-28 11:14:22 -08007628 * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
James Smart0794d602019-01-28 11:14:19 -08007629 *
7630 * Returns: 0 = success, non-zero failure.
7631 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07007632static int
James Smart5e5b5112019-01-28 11:14:22 -08007633lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
James Smart0794d602019-01-28 11:14:19 -08007634{
7635 LIST_HEAD(post_nblist);
7636 int num_posted, rc = 0;
7637
7638 /* get all NVME buffers need to repost to a local list */
James Smart5e5b5112019-01-28 11:14:22 -08007639 lpfc_io_buf_flush(phba, &post_nblist);
James Smart0794d602019-01-28 11:14:19 -08007640
7641 /* post the list of nvme buffer sgls to port if available */
7642 if (!list_empty(&post_nblist)) {
James Smart5e5b5112019-01-28 11:14:22 -08007643 num_posted = lpfc_sli4_post_io_sgl_list(
7644 phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
James Smart0794d602019-01-28 11:14:19 -08007645 /* failed to post any nvme buffer, return error */
7646 if (num_posted == 0)
7647 rc = -EIO;
7648 }
7649 return rc;
7650}
7651
Bart Van Assche3999df72019-03-28 11:06:16 -07007652static void
James Smart61bda8f2016-10-13 15:06:05 -07007653lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7654{
7655 uint32_t len;
7656
7657 len = sizeof(struct lpfc_mbx_set_host_data) -
7658 sizeof(struct lpfc_sli4_cfg_mhdr);
7659 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7660 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7661 LPFC_SLI4_MBX_EMBED);
7662
7663 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08007664 mbox->u.mqe.un.set_host_data.param_len =
7665 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart3b0009c2021-08-16 09:28:47 -07007666 snprintf(mbox->u.mqe.un.set_host_data.un.data,
James Smart61bda8f2016-10-13 15:06:05 -07007667 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7668 "Linux %s v"LPFC_DRIVER_VERSION,
7669 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7670}
7671
James Smarta8cf5df2017-05-15 15:20:46 -07007672int
James Smart6c621a22017-05-15 15:20:45 -07007673lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07007674 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07007675{
7676 int rc, i;
7677 struct lpfc_rqe hrqe;
7678 struct lpfc_rqe drqe;
7679 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08007680 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07007681 struct rqb_dmabuf *rqb_buffer;
7682 LIST_HEAD(rqb_buf_list);
7683
7684 rqbp = hrq->rqbp;
7685 for (i = 0; i < count; i++) {
James Smart62e3a931d2020-10-20 13:27:11 -07007686 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007687 /* IF RQ is already full, don't bother */
James Smart62e3a931d2020-10-20 13:27:11 -07007688 if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {
7689 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007690 break;
James Smart62e3a931d2020-10-20 13:27:11 -07007691 }
7692 spin_unlock_irqrestore(&phba->hbalock, flags);
7693
James Smart6c621a22017-05-15 15:20:45 -07007694 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7695 if (!rqb_buffer)
7696 break;
7697 rqb_buffer->hrq = hrq;
7698 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07007699 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07007700 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7701 }
James Smart62e3a931d2020-10-20 13:27:11 -07007702
7703 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007704 while (!list_empty(&rqb_buf_list)) {
7705 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7706 hbuf.list);
7707
7708 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7709 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7710 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7711 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7712 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7713 if (rc < 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007714 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart411de512018-01-30 15:58:52 -08007715 "6421 Cannot post to HRQ %d: %x %x %x "
7716 "DRQ %x %x\n",
7717 hrq->queue_id,
7718 hrq->host_index,
7719 hrq->hba_index,
7720 hrq->entry_count,
7721 drq->host_index,
7722 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07007723 rqbp->rqb_free_buffer(phba, rqb_buffer);
7724 } else {
7725 list_add_tail(&rqb_buffer->hbuf.list,
7726 &rqbp->rqb_buffer_list);
7727 rqbp->buffer_count++;
7728 }
7729 }
James Smart411de512018-01-30 15:58:52 -08007730 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007731 return 1;
7732}
7733
James Smart9064aeb2021-08-16 09:28:50 -07007734static void
7735lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7736{
7737 struct lpfc_vport *vport = pmb->vport;
7738 union lpfc_sli4_cfg_shdr *shdr;
7739 u32 shdr_status, shdr_add_status;
7740 u32 sig, acqe;
7741
7742 /* Two outcomes. (1) Set featurs was successul and EDC negotiation
7743 * is done. (2) Mailbox failed and send FPIN support only.
7744 */
7745 shdr = (union lpfc_sli4_cfg_shdr *)
7746 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7747 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7748 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7749 if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
7750 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
7751 "2516 CGN SET_FEATURE mbox failed with "
7752 "status x%x add_status x%x, mbx status x%x "
7753 "Reset Congestion to FPINs only\n",
7754 shdr_status, shdr_add_status,
7755 pmb->u.mb.mbxStatus);
7756 /* If there is a mbox error, move on to RDF */
7757 phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7758 phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7759 goto out;
7760 }
7761
7762 /* Zero out Congestion Signal ACQE counter */
7763 phba->cgn_acqe_cnt = 0;
7764 atomic64_set(&phba->cgn_acqe_stat.warn, 0);
7765 atomic64_set(&phba->cgn_acqe_stat.alarm, 0);
7766
7767 acqe = bf_get(lpfc_mbx_set_feature_CGN_acqe_freq,
7768 &pmb->u.mqe.un.set_feature);
7769 sig = bf_get(lpfc_mbx_set_feature_CGN_warn_freq,
7770 &pmb->u.mqe.un.set_feature);
7771 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7772 "4620 SET_FEATURES Success: Freq: %ds %dms "
7773 " Reg: x%x x%x\n", acqe, sig,
7774 phba->cgn_reg_signal, phba->cgn_reg_fpin);
7775out:
7776 mempool_free(pmb, phba->mbox_mem_pool);
7777
7778 /* Register for FPIN events from the fabric now that the
7779 * EDC common_set_features has completed.
7780 */
7781 lpfc_issue_els_rdf(vport, 0);
7782}
7783
7784int
7785lpfc_config_cgn_signal(struct lpfc_hba *phba)
7786{
7787 LPFC_MBOXQ_t *mboxq;
7788 u32 rc;
7789
7790 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7791 if (!mboxq)
7792 goto out_rdf;
7793
7794 lpfc_set_features(phba, mboxq, LPFC_SET_CGN_SIGNAL);
7795 mboxq->vport = phba->pport;
7796 mboxq->mbox_cmpl = lpfc_mbx_cmpl_cgn_set_ftrs;
7797
7798 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7799 "4621 SET_FEATURES: FREQ sig x%x acqe x%x: "
7800 "Reg: x%x x%x\n",
7801 phba->cgn_sig_freq, lpfc_acqe_cgn_frequency,
7802 phba->cgn_reg_signal, phba->cgn_reg_fpin);
7803
7804 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
7805 if (rc == MBX_NOT_FINISHED)
7806 goto out;
7807 return 0;
7808
7809out:
7810 mempool_free(mboxq, phba->mbox_mem_pool);
7811out_rdf:
7812 /* If there is a mbox error, move on to RDF */
7813 phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7814 phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7815 lpfc_issue_els_rdf(phba->pport, 0);
7816 return -EIO;
7817}
7818
Dick Kennedy317aeb82020-06-30 14:49:59 -07007819/**
7820 * lpfc_init_idle_stat_hb - Initialize idle_stat tracking
Lee Jones7af29d42020-07-21 17:41:31 +01007821 * @phba: pointer to lpfc hba data structure.
Dick Kennedy317aeb82020-06-30 14:49:59 -07007822 *
7823 * This routine initializes the per-cq idle_stat to dynamically dictate
7824 * polling decisions.
7825 *
7826 * Return codes:
7827 * None
7828 **/
7829static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)
7830{
7831 int i;
7832 struct lpfc_sli4_hdw_queue *hdwq;
7833 struct lpfc_queue *cq;
7834 struct lpfc_idle_stat *idle_stat;
7835 u64 wall;
7836
7837 for_each_present_cpu(i) {
7838 hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
7839 cq = hdwq->io_cq;
7840
7841 /* Skip if we've already handled this cq's primary CPU */
7842 if (cq->chann != i)
7843 continue;
7844
7845 idle_stat = &phba->sli4_hba.idle_stat[i];
7846
7847 idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);
7848 idle_stat->prev_wall = wall;
7849
James Smart9064aeb2021-08-16 09:28:50 -07007850 if (phba->nvmet_support ||
7851 phba->cmf_active_mode != LPFC_CFG_OFF)
Dick Kennedy317aeb82020-06-30 14:49:59 -07007852 cq->poll_mode = LPFC_QUEUE_WORK;
7853 else
7854 cq->poll_mode = LPFC_IRQ_POLL;
7855 }
7856
7857 if (!phba->nvmet_support)
7858 schedule_delayed_work(&phba->idle_stat_delay_work,
7859 msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
7860}
7861
Dick Kennedyf0020e42020-06-30 14:49:58 -07007862static void lpfc_sli4_dip(struct lpfc_hba *phba)
7863{
7864 uint32_t if_type;
7865
7866 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7867 if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
7868 if_type == LPFC_SLI_INTF_IF_TYPE_6) {
7869 struct lpfc_register reg_data;
7870
7871 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7872 &reg_data.word0))
7873 return;
7874
7875 if (bf_get(lpfc_sliport_status_dip, &reg_data))
James Smart0b3ad322021-01-04 10:02:39 -08007876 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedyf0020e42020-06-30 14:49:58 -07007877 "2904 Firmware Dump Image Present"
7878 " on Adapter");
7879 }
7880}
7881
James Smartc6a5c7472021-08-16 09:28:48 -07007882/**
7883 * lpfc_cmf_setup - Initialize idle_stat tracking
7884 * @phba: Pointer to HBA context object.
7885 *
7886 * This is called from HBA setup during driver load or when the HBA
7887 * comes online. this does all the initialization to support CMF and MI.
7888 **/
7889static int
7890lpfc_cmf_setup(struct lpfc_hba *phba)
7891{
7892 LPFC_MBOXQ_t *mboxq;
7893 struct lpfc_mqe *mqe;
James Smart02243832021-08-16 09:28:54 -07007894 struct lpfc_dmabuf *mp;
James Smartc6a5c7472021-08-16 09:28:48 -07007895 struct lpfc_pc_sli4_params *sli4_params;
7896 struct lpfc_sli4_parameters *mbx_sli4_parameters;
7897 int length;
James Smart02243832021-08-16 09:28:54 -07007898 int rc, cmf, mi_ver;
James Smartc6a5c7472021-08-16 09:28:48 -07007899
7900 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7901 if (!mboxq)
7902 return -ENOMEM;
7903 mqe = &mboxq->u.mqe;
7904
7905 /* Read the port's SLI4 Config Parameters */
7906 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
7907 sizeof(struct lpfc_sli4_cfg_mhdr));
7908 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7909 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
7910 length, LPFC_SLI4_MBX_EMBED);
7911
7912 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7913 if (unlikely(rc)) {
7914 mempool_free(mboxq, phba->mbox_mem_pool);
7915 return rc;
7916 }
7917
James Smart02243832021-08-16 09:28:54 -07007918 /* Gather info on CMF and MI support */
James Smartc6a5c7472021-08-16 09:28:48 -07007919 sli4_params = &phba->sli4_hba.pc_sli4_params;
7920 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
7921 sli4_params->mi_ver = bf_get(cfg_mi_ver, mbx_sli4_parameters);
James Smart02243832021-08-16 09:28:54 -07007922 sli4_params->cmf = bf_get(cfg_cmf, mbx_sli4_parameters);
James Smartc6a5c7472021-08-16 09:28:48 -07007923
7924 /* Are we forcing MI off via module parameter? */
7925 if (!phba->cfg_enable_mi)
7926 sli4_params->mi_ver = 0;
7927
7928 /* Always try to enable MI feature if we can */
7929 if (sli4_params->mi_ver) {
7930 lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_MI);
7931 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7932 mi_ver = bf_get(lpfc_mbx_set_feature_mi,
7933 &mboxq->u.mqe.un.set_feature);
7934
7935 if (rc == MBX_SUCCESS) {
7936 if (mi_ver) {
7937 lpfc_printf_log(phba,
7938 KERN_WARNING, LOG_CGN_MGMT,
7939 "6215 MI is enabled\n");
7940 sli4_params->mi_ver = mi_ver;
7941 } else {
7942 lpfc_printf_log(phba,
7943 KERN_WARNING, LOG_CGN_MGMT,
7944 "6338 MI is disabled\n");
7945 sli4_params->mi_ver = 0;
7946 }
7947 } else {
7948 /* mi_ver is already set from GET_SLI4_PARAMETERS */
7949 lpfc_printf_log(phba, KERN_INFO,
7950 LOG_CGN_MGMT | LOG_INIT,
7951 "6245 Enable MI Mailbox x%x (x%x/x%x) "
7952 "failed, rc:x%x mi:x%x\n",
7953 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7954 lpfc_sli_config_mbox_subsys_get
7955 (phba, mboxq),
7956 lpfc_sli_config_mbox_opcode_get
7957 (phba, mboxq),
7958 rc, sli4_params->mi_ver);
7959 }
7960 } else {
7961 lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
7962 "6217 MI is disabled\n");
7963 }
7964
7965 /* Ensure FDMI is enabled for MI if enable_mi is set */
7966 if (sli4_params->mi_ver)
7967 phba->cfg_fdmi_on = LPFC_FDMI_SUPPORT;
7968
James Smart02243832021-08-16 09:28:54 -07007969 /* Always try to enable CMF feature if we can */
7970 if (sli4_params->cmf) {
7971 lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_CMF);
7972 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7973 cmf = bf_get(lpfc_mbx_set_feature_cmf,
7974 &mboxq->u.mqe.un.set_feature);
7975 if (rc == MBX_SUCCESS && cmf) {
7976 lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
7977 "6218 CMF is enabled: mode %d\n",
7978 phba->cmf_active_mode);
7979 } else {
7980 lpfc_printf_log(phba, KERN_WARNING,
7981 LOG_CGN_MGMT | LOG_INIT,
7982 "6219 Enable CMF Mailbox x%x (x%x/x%x) "
7983 "failed, rc:x%x dd:x%x\n",
7984 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7985 lpfc_sli_config_mbox_subsys_get
7986 (phba, mboxq),
7987 lpfc_sli_config_mbox_opcode_get
7988 (phba, mboxq),
7989 rc, cmf);
7990 sli4_params->cmf = 0;
7991 phba->cmf_active_mode = LPFC_CFG_OFF;
7992 goto no_cmf;
7993 }
7994
7995 /* Allocate Congestion Information Buffer */
7996 if (!phba->cgn_i) {
7997 mp = kmalloc(sizeof(*mp), GFP_KERNEL);
7998 if (mp)
7999 mp->virt = dma_alloc_coherent
8000 (&phba->pcidev->dev,
8001 sizeof(struct lpfc_cgn_info),
8002 &mp->phys, GFP_KERNEL);
8003 if (!mp || !mp->virt) {
8004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8005 "2640 Failed to alloc memory "
8006 "for Congestion Info\n");
8007 kfree(mp);
8008 sli4_params->cmf = 0;
8009 phba->cmf_active_mode = LPFC_CFG_OFF;
8010 goto no_cmf;
8011 }
8012 phba->cgn_i = mp;
8013
8014 /* initialize congestion buffer info */
8015 lpfc_init_congestion_buf(phba);
8016 lpfc_init_congestion_stat(phba);
8017 }
8018
8019 rc = lpfc_sli4_cgn_params_read(phba);
8020 if (rc < 0) {
8021 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8022 "6242 Error reading Cgn Params (%d)\n",
8023 rc);
8024 /* Ensure CGN Mode is off */
8025 sli4_params->cmf = 0;
8026 } else if (!rc) {
8027 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8028 "6243 CGN Event empty object.\n");
8029 /* Ensure CGN Mode is off */
8030 sli4_params->cmf = 0;
8031 }
8032 } else {
8033no_cmf:
8034 lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8035 "6220 CMF is disabled\n");
8036 }
8037
8038 /* Only register congestion buffer with firmware if BOTH
8039 * CMF and E2E are enabled.
8040 */
8041 if (sli4_params->cmf && sli4_params->mi_ver) {
8042 rc = lpfc_reg_congestion_buf(phba);
8043 if (rc) {
8044 dma_free_coherent(&phba->pcidev->dev,
8045 sizeof(struct lpfc_cgn_info),
8046 phba->cgn_i->virt, phba->cgn_i->phys);
8047 kfree(phba->cgn_i);
8048 phba->cgn_i = NULL;
8049 /* Ensure CGN Mode is off */
8050 phba->cmf_active_mode = LPFC_CFG_OFF;
8051 return 0;
8052 }
8053 }
James Smartc6a5c7472021-08-16 09:28:48 -07008054 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart02243832021-08-16 09:28:54 -07008055 "6470 Setup MI version %d CMF %d mode %d\n",
8056 sli4_params->mi_ver, sli4_params->cmf,
8057 phba->cmf_active_mode);
James Smartc6a5c7472021-08-16 09:28:48 -07008058
8059 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart8c42a652021-08-16 09:28:51 -07008060
8061 /* Initialize atomic counters */
8062 atomic_set(&phba->cgn_fabric_warn_cnt, 0);
8063 atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
8064 atomic_set(&phba->cgn_sync_alarm_cnt, 0);
8065 atomic_set(&phba->cgn_sync_warn_cnt, 0);
8066 atomic_set(&phba->cgn_driver_evt_cnt, 0);
8067 atomic_set(&phba->cgn_latency_evt_cnt, 0);
8068 atomic64_set(&phba->cgn_latency_evt, 0);
8069
James Smart02243832021-08-16 09:28:54 -07008070 phba->cmf_interval_rate = LPFC_CMF_INTERVAL;
James Smartc6a5c7472021-08-16 09:28:48 -07008071 return 0;
8072}
8073
James Smart3b0009c2021-08-16 09:28:47 -07008074static int
8075lpfc_set_host_tm(struct lpfc_hba *phba)
8076{
8077 LPFC_MBOXQ_t *mboxq;
8078 uint32_t len, rc;
8079 struct timespec64 cur_time;
8080 struct tm broken;
8081 uint32_t month, day, year;
8082 uint32_t hour, minute, second;
8083 struct lpfc_mbx_set_host_date_time *tm;
8084
8085 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8086 if (!mboxq)
8087 return -ENOMEM;
8088
8089 len = sizeof(struct lpfc_mbx_set_host_data) -
8090 sizeof(struct lpfc_sli4_cfg_mhdr);
8091 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8092 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
8093 LPFC_SLI4_MBX_EMBED);
8094
8095 mboxq->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_DATE_TIME;
8096 mboxq->u.mqe.un.set_host_data.param_len =
8097 sizeof(struct lpfc_mbx_set_host_date_time);
8098 tm = &mboxq->u.mqe.un.set_host_data.un.tm;
8099 ktime_get_real_ts64(&cur_time);
8100 time64_to_tm(cur_time.tv_sec, 0, &broken);
8101 month = broken.tm_mon + 1;
8102 day = broken.tm_mday;
8103 year = broken.tm_year - 100;
8104 hour = broken.tm_hour;
8105 minute = broken.tm_min;
8106 second = broken.tm_sec;
8107 bf_set(lpfc_mbx_set_host_month, tm, month);
8108 bf_set(lpfc_mbx_set_host_day, tm, day);
8109 bf_set(lpfc_mbx_set_host_year, tm, year);
8110 bf_set(lpfc_mbx_set_host_hour, tm, hour);
8111 bf_set(lpfc_mbx_set_host_min, tm, minute);
8112 bf_set(lpfc_mbx_set_host_sec, tm, second);
8113
8114 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8115 mempool_free(mboxq, phba->mbox_mem_pool);
8116 return rc;
8117}
8118
James Smart8a9d2e82012-05-09 21:16:12 -04008119/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08008120 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04008121 * @phba: Pointer to HBA context object.
8122 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08008123 * This function is the main SLI4 device initialization PCI function. This
8124 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04008125 * HBA error attention handler code. Caller is not required to hold any
8126 * locks.
8127 **/
8128int
8129lpfc_sli4_hba_setup(struct lpfc_hba *phba)
8130{
James Smart171f6c42019-11-04 16:57:07 -08008131 int rc, i, cnt, len, dd;
James Smartda0436e2009-05-22 14:51:39 -04008132 LPFC_MBOXQ_t *mboxq;
8133 struct lpfc_mqe *mqe;
8134 uint8_t *vpd;
8135 uint32_t vpd_size;
8136 uint32_t ftr_rsp = 0;
8137 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
8138 struct lpfc_vport *vport = phba->pport;
8139 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08008140 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04008141
8142 /* Perform a PCI function reset to start from clean */
8143 rc = lpfc_pci_function_reset(phba);
8144 if (unlikely(rc))
8145 return -ENODEV;
8146
8147 /* Check the HBA Host Status Register for readyness */
8148 rc = lpfc_sli4_post_status_check(phba);
8149 if (unlikely(rc))
8150 return -ENODEV;
8151 else {
8152 spin_lock_irq(&phba->hbalock);
8153 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
8154 spin_unlock_irq(&phba->hbalock);
8155 }
8156
Dick Kennedyf0020e42020-06-30 14:49:58 -07008157 lpfc_sli4_dip(phba);
8158
James Smartda0436e2009-05-22 14:51:39 -04008159 /*
8160 * Allocate a single mailbox container for initializing the
8161 * port.
8162 */
8163 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8164 if (!mboxq)
8165 return -ENOMEM;
8166
James Smartda0436e2009-05-22 14:51:39 -04008167 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04008168 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04008169 vpd = kzalloc(vpd_size, GFP_KERNEL);
8170 if (!vpd) {
8171 rc = -ENOMEM;
8172 goto out_free_mbox;
8173 }
8174
8175 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05008176 if (unlikely(rc)) {
8177 kfree(vpd);
8178 goto out_free_mbox;
8179 }
James Smart572709e2013-07-15 18:32:43 -04008180
James Smartda0436e2009-05-22 14:51:39 -04008181 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04008182 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07008183 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05008184 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07008185 phba->fcp_embed_io = 0; /* SLI4 FC support only */
8186 } else {
James Smart76a95d72010-11-20 23:11:48 -05008187 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07008188 }
James Smart45ed1192009-10-02 15:17:02 -04008189
8190 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
8191 LPFC_DCBX_CEE_MODE)
8192 phba->hba_flag |= HBA_FIP_SUPPORT;
8193 else
8194 phba->hba_flag &= ~HBA_FIP_SUPPORT;
8195
James Smartc00f62e2019-08-14 16:57:11 -07008196 phba->hba_flag &= ~HBA_IOQ_FLUSH;
James Smart4f2e66c2012-05-09 21:17:07 -04008197
James Smartc31098c2011-04-16 11:03:33 -04008198 if (phba->sli_rev != LPFC_SLI_REV4) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008199 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04008200 "0376 READ_REV Error. SLI Level %d "
8201 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05008202 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04008203 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05008204 kfree(vpd);
8205 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04008206 }
James Smartcd1c8302011-10-10 21:33:25 -04008207
James Smart3b0009c2021-08-16 09:28:47 -07008208 rc = lpfc_set_host_tm(phba);
8209 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
8210 "6468 Set host date / time: Status x%x:\n", rc);
8211
James Smartcd1c8302011-10-10 21:33:25 -04008212 /*
James Smartff78d8f2011-12-13 13:21:35 -05008213 * Continue initialization with default values even if driver failed
8214 * to read FCoE param config regions, only read parameters if the
8215 * board is FCoE
8216 */
8217 if (phba->hba_flag & HBA_FCOE_MODE &&
8218 lpfc_sli4_read_fcoe_params(phba))
8219 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
8220 "2570 Failed to read FCoE parameters\n");
8221
8222 /*
James Smartcd1c8302011-10-10 21:33:25 -04008223 * Retrieve sli4 device physical port name, failure of doing it
8224 * is considered as non-fatal.
8225 */
8226 rc = lpfc_sli4_retrieve_pport_name(phba);
8227 if (!rc)
8228 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8229 "3080 Successful retrieving SLI4 device "
8230 "physical port name: %s.\n", phba->Port);
8231
James Smartb3b4f3e2019-03-12 16:30:23 -07008232 rc = lpfc_sli4_get_ctl_attr(phba);
8233 if (!rc)
8234 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8235 "8351 Successful retrieving SLI4 device "
8236 "CTL ATTR\n");
8237
James Smartda0436e2009-05-22 14:51:39 -04008238 /*
8239 * Evaluate the read rev and vpd data. Populate the driver
8240 * state with the results. If this routine fails, the failure
8241 * is not fatal as the driver will use generic values.
8242 */
8243 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
8244 if (unlikely(!rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008245 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04008246 "0377 Error %d parsing vpd. "
8247 "Using defaults.\n", rc);
8248 rc = 0;
8249 }
James Smart76a95d72010-11-20 23:11:48 -05008250 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04008251
James Smartf1126682009-06-10 17:22:44 -04008252 /* Save information as VPD data */
8253 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
8254 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08008255
8256 /*
8257 * This is because first G7 ASIC doesn't support the standard
8258 * 0x5a NVME cmd descriptor type/subtype
8259 */
8260 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8261 LPFC_SLI_INTF_IF_TYPE_6) &&
8262 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
8263 (phba->vpd.rev.smRev == 0) &&
8264 (phba->cfg_nvme_embed_cmd == 1))
8265 phba->cfg_nvme_embed_cmd = 0;
8266
James Smartf1126682009-06-10 17:22:44 -04008267 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
8268 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
8269 &mqe->un.read_rev);
8270 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
8271 &mqe->un.read_rev);
8272 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
8273 &mqe->un.read_rev);
8274 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
8275 &mqe->un.read_rev);
8276 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
8277 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
8278 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
8279 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
8280 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
8281 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
8282 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8283 "(%d):0380 READ_REV Status x%x "
8284 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
8285 mboxq->vport ? mboxq->vport->vpi : 0,
8286 bf_get(lpfc_mqe_status, mqe),
8287 phba->vpd.rev.opFwName,
8288 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
8289 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04008290
James Smart65791f12016-07-06 12:35:56 -07008291 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07008292 LPFC_SLI_INTF_IF_TYPE_0) {
8293 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
8294 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8295 if (rc == MBX_SUCCESS) {
8296 phba->hba_flag |= HBA_RECOVERABLE_UE;
8297 /* Set 1Sec interval to detect UE */
8298 phba->eratt_poll_interval = 1;
8299 phba->sli4_hba.ue_to_sr = bf_get(
8300 lpfc_mbx_set_feature_UESR,
8301 &mboxq->u.mqe.un.set_feature);
8302 phba->sli4_hba.ue_to_rp = bf_get(
8303 lpfc_mbx_set_feature_UERP,
8304 &mboxq->u.mqe.un.set_feature);
8305 }
8306 }
8307
8308 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
8309 /* Enable MDS Diagnostics only if the SLI Port supports it */
8310 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
8311 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8312 if (rc != MBX_SUCCESS)
8313 phba->mds_diags_support = 0;
8314 }
James Smart572709e2013-07-15 18:32:43 -04008315
James Smartda0436e2009-05-22 14:51:39 -04008316 /*
8317 * Discover the port's supported feature set and match it against the
8318 * hosts requests.
8319 */
8320 lpfc_request_features(phba, mboxq);
8321 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8322 if (unlikely(rc)) {
8323 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05008324 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04008325 }
8326
Gaurav Srivastava5e633302021-06-08 10:05:49 +05308327 /* Disable VMID if app header is not supported */
8328 if (phba->cfg_vmid_app_header && !(bf_get(lpfc_mbx_rq_ftr_rsp_ashdr,
8329 &mqe->un.req_ftrs))) {
8330 bf_set(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags, 0);
8331 phba->cfg_vmid_app_header = 0;
8332 lpfc_printf_log(phba, KERN_DEBUG, LOG_SLI,
8333 "1242 vmid feature not supported\n");
8334 }
8335
James Smartda0436e2009-05-22 14:51:39 -04008336 /*
8337 * The port must support FCP initiator mode as this is the
8338 * only mode running in the host.
8339 */
8340 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
8341 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8342 "0378 No support for fcpi mode.\n");
8343 ftr_rsp++;
8344 }
James Smart0bc2b7c2018-02-22 08:18:48 -08008345
8346 /* Performance Hints are ONLY for FCoE */
8347 if (phba->hba_flag & HBA_FCOE_MODE) {
8348 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
8349 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
8350 else
8351 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
8352 }
8353
James Smartda0436e2009-05-22 14:51:39 -04008354 /*
8355 * If the port cannot support the host's requested features
8356 * then turn off the global config parameters to disable the
8357 * feature in the driver. This is not a fatal error.
8358 */
James Smartf44ac122018-03-05 12:04:08 -08008359 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
8360 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
8361 phba->cfg_enable_bg = 0;
8362 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04008363 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08008364 }
James Smartbf086112011-08-21 21:48:13 -04008365 }
James Smartda0436e2009-05-22 14:51:39 -04008366
8367 if (phba->max_vpi && phba->cfg_enable_npiv &&
8368 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8369 ftr_rsp++;
8370
8371 if (ftr_rsp) {
8372 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8373 "0379 Feature Mismatch Data: x%08x %08x "
8374 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
8375 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
8376 phba->cfg_enable_npiv, phba->max_vpi);
8377 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
8378 phba->cfg_enable_bg = 0;
8379 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8380 phba->cfg_enable_npiv = 0;
8381 }
8382
8383 /* These SLI3 features are assumed in SLI4 */
8384 spin_lock_irq(&phba->hbalock);
8385 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
8386 spin_unlock_irq(&phba->hbalock);
8387
James Smart171f6c42019-11-04 16:57:07 -08008388 /* Always try to enable dual dump feature if we can */
8389 lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
8390 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8391 dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
8392 if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
Dick Kennedy372c1872020-06-30 14:50:00 -07008393 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart171f6c42019-11-04 16:57:07 -08008394 "6448 Dual Dump is enabled\n");
8395 else
8396 lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
8397 "6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
8398 "rc:x%x dd:x%x\n",
8399 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8400 lpfc_sli_config_mbox_subsys_get(
8401 phba, mboxq),
8402 lpfc_sli_config_mbox_opcode_get(
8403 phba, mboxq),
8404 rc, dd);
James Smart6d368e52011-05-24 11:44:12 -04008405 /*
8406 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
8407 * calls depends on these resources to complete port setup.
8408 */
8409 rc = lpfc_sli4_alloc_resource_identifiers(phba);
8410 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008411 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6d368e52011-05-24 11:44:12 -04008412 "2920 Failed to alloc Resource IDs "
8413 "rc = x%x\n", rc);
8414 goto out_free_mbox;
8415 }
8416
James Smart61bda8f2016-10-13 15:06:05 -07008417 lpfc_set_host_data(phba, mboxq);
8418
8419 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8420 if (rc) {
8421 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8422 "2134 Failed to set host os driver version %x",
8423 rc);
8424 }
8425
James Smartda0436e2009-05-22 14:51:39 -04008426 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05008427 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
8428 if (rc) {
8429 phba->link_state = LPFC_HBA_ERROR;
8430 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05008431 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05008432 }
8433
James Smartda0436e2009-05-22 14:51:39 -04008434 mboxq->vport = vport;
8435 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart3e1f0712018-11-29 16:09:29 -08008436 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04008437 if (rc == MBX_SUCCESS) {
8438 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
8439 rc = 0;
8440 }
8441
8442 /*
8443 * This memory was allocated by the lpfc_read_sparam routine. Release
8444 * it to the mbuf pool.
8445 */
8446 lpfc_mbuf_free(phba, mp->virt, mp->phys);
8447 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -08008448 mboxq->ctx_buf = NULL;
James Smartda0436e2009-05-22 14:51:39 -04008449 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008450 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04008451 "0382 READ_SPARAM command failed "
8452 "status %d, mbxStatus x%x\n",
8453 rc, bf_get(lpfc_mqe_status, mqe));
8454 phba->link_state = LPFC_HBA_ERROR;
8455 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05008456 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04008457 }
8458
James Smart05580562011-05-24 11:40:48 -04008459 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04008460
8461 /* Update the fc_host data structures with new wwn. */
8462 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
8463 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
8464
James Smart895427b2017-02-12 13:52:30 -08008465 /* Create all the SLI4 queues */
8466 rc = lpfc_sli4_queue_create(phba);
8467 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008468 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08008469 "3089 Failed to allocate queues\n");
8470 rc = -ENODEV;
8471 goto out_free_mbox;
8472 }
8473 /* Set up all the queues to the device */
8474 rc = lpfc_sli4_queue_setup(phba);
8475 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008476 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08008477 "0381 Error %d during queue setup.\n ", rc);
8478 goto out_stop_timers;
8479 }
8480 /* Initialize the driver internal SLI layer lists. */
8481 lpfc_sli4_setup(phba);
8482 lpfc_sli4_queue_init(phba);
8483
8484 /* update host els xri-sgl sizes and mappings */
8485 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04008486 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008487 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04008488 "1400 Failed to update xri-sgl size and "
8489 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08008490 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04008491 }
8492
James Smart8a9d2e82012-05-09 21:16:12 -04008493 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08008494 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
8495 phba->sli4_hba.els_xri_cnt);
8496 if (unlikely(rc < 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008497 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04008498 "0582 Error %d during els sgl post "
8499 "operation\n", rc);
8500 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08008501 goto out_destroy_queue;
8502 }
8503 phba->sli4_hba.els_xri_cnt = rc;
8504
James Smartf358dd02017-02-12 13:52:34 -08008505 if (phba->nvmet_support) {
8506 /* update host nvmet xri-sgl sizes and mappings */
8507 rc = lpfc_sli4_nvmet_sgl_update(phba);
8508 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008509 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08008510 "6308 Failed to update nvmet-sgl size "
8511 "and mapping: %d\n", rc);
8512 goto out_destroy_queue;
8513 }
8514
8515 /* register the nvmet sgl pool to the port */
8516 rc = lpfc_sli4_repost_sgl_list(
8517 phba,
8518 &phba->sli4_hba.lpfc_nvmet_sgl_list,
8519 phba->sli4_hba.nvmet_xri_cnt);
8520 if (unlikely(rc < 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008521 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08008522 "3117 Error %d during nvmet "
8523 "sgl post\n", rc);
8524 rc = -ENODEV;
8525 goto out_destroy_queue;
8526 }
8527 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07008528
James Smarta5f73372019-09-21 20:58:50 -07008529 /* We allocate an iocbq for every receive context SGL.
8530 * The additional allocation is for abort and ls handling.
8531 */
8532 cnt = phba->sli4_hba.nvmet_xri_cnt +
8533 phba->sli4_hba.max_cfg_param.max_xri;
James Smartf358dd02017-02-12 13:52:34 -08008534 } else {
James Smart0794d602019-01-28 11:14:19 -08008535 /* update host common xri-sgl sizes and mappings */
James Smart5e5b5112019-01-28 11:14:22 -08008536 rc = lpfc_sli4_io_sgl_update(phba);
James Smart895427b2017-02-12 13:52:30 -08008537 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008538 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08008539 "6082 Failed to update nvme-sgl size "
8540 "and mapping: %d\n", rc);
8541 goto out_destroy_queue;
8542 }
James Smart6c621a22017-05-15 15:20:45 -07008543
James Smart0794d602019-01-28 11:14:19 -08008544 /* register the allocated common sgl pool to the port */
James Smart5e5b5112019-01-28 11:14:22 -08008545 rc = lpfc_sli4_repost_io_sgl_list(phba);
James Smart0794d602019-01-28 11:14:19 -08008546 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008547 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -08008548 "6116 Error %d during nvme sgl post "
8549 "operation\n", rc);
8550 /* Some NVME buffers were moved to abort nvme list */
8551 /* A pci function reset will repost them */
8552 rc = -ENODEV;
8553 goto out_destroy_queue;
8554 }
James Smarta5f73372019-09-21 20:58:50 -07008555 /* Each lpfc_io_buf job structure has an iocbq element.
8556 * This cnt provides for abort, els, ct and ls requests.
8557 */
8558 cnt = phba->sli4_hba.max_cfg_param.max_xri;
James Smart11e644e2017-06-15 22:56:48 -07008559 }
8560
8561 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07008562 /* Initialize and populate the iocb list per host */
8563 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta5f73372019-09-21 20:58:50 -07008564 "2821 initialize iocb list with %d entries\n",
8565 cnt);
James Smart6c621a22017-05-15 15:20:45 -07008566 rc = lpfc_init_iocb_list(phba, cnt);
8567 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008568 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart11e644e2017-06-15 22:56:48 -07008569 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07008570 goto out_destroy_queue;
8571 }
James Smart8a9d2e82012-05-09 21:16:12 -04008572 }
8573
James Smart11e644e2017-06-15 22:56:48 -07008574 if (phba->nvmet_support)
8575 lpfc_nvmet_create_targetport(phba);
8576
James Smart2d7dbc42017-02-12 13:52:35 -08008577 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08008578 /* Post initial buffers to all RQs created */
8579 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
8580 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
8581 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
8582 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
8583 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07008584 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08008585 rqbp->buffer_count = 0;
8586
James Smart2d7dbc42017-02-12 13:52:35 -08008587 lpfc_post_rq_buffer(
8588 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
8589 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07008590 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08008591 }
8592 }
8593
James Smartda0436e2009-05-22 14:51:39 -04008594 /* Post the rpi header region to the device. */
8595 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
8596 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008597 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04008598 "0393 Error %d during rpi post operation\n",
8599 rc);
8600 rc = -ENODEV;
James Smart5aa615d2021-05-14 12:55:56 -07008601 goto out_free_iocblist;
James Smartda0436e2009-05-22 14:51:39 -04008602 }
James Smart97f2ecf2012-03-01 22:35:23 -05008603 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04008604
James Smart895427b2017-02-12 13:52:30 -08008605 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08008606 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08008607 /*
8608 * The FC Port needs to register FCFI (index 0)
8609 */
8610 lpfc_reg_fcfi(phba, mboxq);
8611 mboxq->vport = phba->pport;
8612 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8613 if (rc != MBX_SUCCESS)
8614 goto out_unset_queue;
8615 rc = 0;
8616 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
8617 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08008618 } else {
8619 /* We are a NVME Target mode with MRQ > 1 */
8620
8621 /* First register the FCFI */
8622 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
8623 mboxq->vport = phba->pport;
8624 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8625 if (rc != MBX_SUCCESS)
8626 goto out_unset_queue;
8627 rc = 0;
8628 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
8629 &mboxq->u.mqe.un.reg_fcfi_mrq);
8630
8631 /* Next register the MRQs */
8632 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
8633 mboxq->vport = phba->pport;
8634 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8635 if (rc != MBX_SUCCESS)
8636 goto out_unset_queue;
8637 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08008638 }
8639 /* Check if the port is configured to be disabled */
8640 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04008641 }
8642
James Smartc4908502019-01-28 11:14:28 -08008643 /* Don't post more new bufs if repost already recovered
8644 * the nvme sgls.
8645 */
8646 if (phba->nvmet_support == 0) {
8647 if (phba->sli4_hba.io_xri_cnt == 0) {
8648 len = lpfc_new_io_buf(
8649 phba, phba->sli4_hba.io_xri_max);
8650 if (len == 0) {
8651 rc = -ENOMEM;
8652 goto out_unset_queue;
8653 }
8654
8655 if (phba->cfg_xri_rebalancing)
8656 lpfc_create_multixri_pools(phba);
8657 }
8658 } else {
8659 phba->cfg_xri_rebalancing = 0;
8660 }
8661
James Smartda0436e2009-05-22 14:51:39 -04008662 /* Allow asynchronous mailbox command to go through */
8663 spin_lock_irq(&phba->hbalock);
8664 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8665 spin_unlock_irq(&phba->hbalock);
8666
8667 /* Post receive buffers to the device */
8668 lpfc_sli4_rb_setup(phba);
8669
James Smartfc2b9892010-02-26 14:15:29 -05008670 /* Reset HBA FCF states after HBA reset */
8671 phba->fcf.fcf_flag = 0;
8672 phba->fcf.current_rec.flag = 0;
8673
James Smartda0436e2009-05-22 14:51:39 -04008674 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04008675 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04008676 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04008677
8678 /* Start heart beat timer */
8679 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04008680 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smarta22d73b2021-01-04 10:02:38 -08008681 phba->hba_flag &= ~(HBA_HBEAT_INP | HBA_HBEAT_TMO);
James Smartda0436e2009-05-22 14:51:39 -04008682 phba->last_completion_time = jiffies;
8683
James Smart32517fc2019-01-28 11:14:33 -08008684 /* start eq_delay heartbeat */
8685 if (phba->cfg_auto_imax)
8686 queue_delayed_work(phba->wq, &phba->eq_delay_work,
8687 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
8688
Dick Kennedy317aeb82020-06-30 14:49:59 -07008689 /* start per phba idle_stat_delay heartbeat */
8690 lpfc_init_idle_stat_hb(phba);
8691
James Smartda0436e2009-05-22 14:51:39 -04008692 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04008693 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07008694 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04008695
James Smart75baf692010-06-08 18:31:21 -04008696 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
8697 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
8698 rc = pci_enable_pcie_error_reporting(phba->pcidev);
8699 if (!rc) {
8700 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8701 "2829 This device supports "
8702 "Advanced Error Reporting (AER)\n");
8703 spin_lock_irq(&phba->hbalock);
8704 phba->hba_flag |= HBA_AER_ENABLED;
8705 spin_unlock_irq(&phba->hbalock);
8706 } else {
8707 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8708 "2830 This device does not support "
8709 "Advanced Error Reporting (AER)\n");
8710 phba->cfg_aer_support = 0;
8711 }
James Smart0a96e972011-07-22 18:37:28 -04008712 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04008713 }
8714
James Smartda0436e2009-05-22 14:51:39 -04008715 /*
8716 * The port is ready, set the host's link state to LINK_DOWN
8717 * in preparation for link interrupts.
8718 */
James Smartda0436e2009-05-22 14:51:39 -04008719 spin_lock_irq(&phba->hbalock);
8720 phba->link_state = LPFC_LINK_DOWN;
James Smart1dc5ec22018-10-23 13:41:11 -07008721
8722 /* Check if physical ports are trunked */
8723 if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
8724 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
8725 if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
8726 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
8727 if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
8728 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
8729 if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
8730 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04008731 spin_unlock_irq(&phba->hbalock);
James Smart1dc5ec22018-10-23 13:41:11 -07008732
James Smarte8869f52019-03-12 16:30:18 -07008733 /* Arm the CQs and then EQs on device */
8734 lpfc_sli4_arm_cqeq_intr(phba);
8735
8736 /* Indicate device interrupt mode */
8737 phba->sli4_hba.intr_enable = 1;
8738
James Smart02243832021-08-16 09:28:54 -07008739 /* Setup CMF after HBA is initialized */
8740 lpfc_cmf_setup(phba);
8741
James Smart026abb82011-12-13 13:20:45 -05008742 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
8743 (phba->hba_flag & LINK_DISABLED)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008744 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart026abb82011-12-13 13:20:45 -05008745 "3103 Adapter Link is disabled.\n");
8746 lpfc_down_link(phba, mboxq);
8747 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8748 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008749 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart026abb82011-12-13 13:20:45 -05008750 "3104 Adapter failed to issue "
8751 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
James Smartc4908502019-01-28 11:14:28 -08008752 goto out_io_buff_free;
James Smart026abb82011-12-13 13:20:45 -05008753 }
8754 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05008755 /* don't perform init_link on SLI4 FC port loopback test */
8756 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
8757 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
8758 if (rc)
James Smartc4908502019-01-28 11:14:28 -08008759 goto out_io_buff_free;
James Smart1b511972011-12-13 13:23:09 -05008760 }
James Smart5350d872011-10-10 21:33:49 -04008761 }
8762 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart02243832021-08-16 09:28:54 -07008763
8764 phba->hba_flag |= HBA_SETUP;
James Smart5350d872011-10-10 21:33:49 -04008765 return rc;
James Smart02243832021-08-16 09:28:54 -07008766
James Smartc4908502019-01-28 11:14:28 -08008767out_io_buff_free:
8768 /* Free allocated IO Buffers */
8769 lpfc_io_free(phba);
James Smart76a95d72010-11-20 23:11:48 -05008770out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04008771 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04008772 lpfc_sli4_queue_unset(phba);
James Smart5aa615d2021-05-14 12:55:56 -07008773out_free_iocblist:
James Smart6c621a22017-05-15 15:20:45 -07008774 lpfc_free_iocb_list(phba);
James Smart5aa615d2021-05-14 12:55:56 -07008775out_destroy_queue:
James Smart5350d872011-10-10 21:33:49 -04008776 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04008777out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04008778 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04008779out_free_mbox:
8780 mempool_free(mboxq, phba->mbox_mem_pool);
8781 return rc;
8782}
James Smarte59058c2008-08-24 21:49:00 -04008783
8784/**
James Smart3621a712009-04-06 18:47:14 -04008785 * lpfc_mbox_timeout - Timeout call back function for mbox timer
Lee Jones7af29d42020-07-21 17:41:31 +01008786 * @t: Context to fetch pointer to hba structure from.
dea31012005-04-17 16:05:31 -05008787 *
James Smarte59058c2008-08-24 21:49:00 -04008788 * This is the callback function for mailbox timer. The mailbox
8789 * timer is armed when a new mailbox command is issued and the timer
8790 * is deleted when the mailbox complete. The function is called by
8791 * the kernel timer code when a mailbox does not complete within
8792 * expected time. This function wakes up the worker thread to
8793 * process the mailbox timeout and returns. All the processing is
8794 * done by the worker thread function lpfc_mbox_timeout_handler.
8795 **/
dea31012005-04-17 16:05:31 -05008796void
Kees Cookf22eb4d2017-09-06 20:24:26 -07008797lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05008798{
Kees Cookf22eb4d2017-09-06 20:24:26 -07008799 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05008800 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05008801 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05008802
James Smart2e0fef82007-06-17 19:56:36 -05008803 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008804 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05008805 if (!tmo_posted)
8806 phba->pport->work_port_events |= WORKER_MBOX_TMO;
8807 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
8808
James Smart5e9d9b82008-06-14 22:52:53 -04008809 if (!tmo_posted)
8810 lpfc_worker_wake_up(phba);
8811 return;
dea31012005-04-17 16:05:31 -05008812}
8813
James Smarte8d3c3b2013-10-10 12:21:30 -04008814/**
8815 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
8816 * are pending
8817 * @phba: Pointer to HBA context object.
8818 *
8819 * This function checks if any mailbox completions are present on the mailbox
8820 * completion queue.
8821 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04008822static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04008823lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
8824{
8825
8826 uint32_t idx;
8827 struct lpfc_queue *mcq;
8828 struct lpfc_mcqe *mcqe;
8829 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08008830 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04008831
8832 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8833 return false;
8834
8835 /* Check for completions on mailbox completion queue */
8836
8837 mcq = phba->sli4_hba.mbx_cq;
8838 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08008839 qe_valid = mcq->qe_valid;
James Smart9afbee32019-03-12 16:30:28 -07008840 while (bf_get_le32(lpfc_cqe_valid,
8841 (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
8842 mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
James Smarte8d3c3b2013-10-10 12:21:30 -04008843 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
8844 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
8845 pending_completions = true;
8846 break;
8847 }
8848 idx = (idx + 1) % mcq->entry_count;
8849 if (mcq->hba_index == idx)
8850 break;
James Smart7365f6f2018-02-22 08:18:46 -08008851
8852 /* if the index wrapped around, toggle the valid bit */
8853 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
8854 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04008855 }
8856 return pending_completions;
8857
8858}
8859
8860/**
8861 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
8862 * that were missed.
8863 * @phba: Pointer to HBA context object.
8864 *
8865 * For sli4, it is possible to miss an interrupt. As such mbox completions
8866 * maybe missed causing erroneous mailbox timeouts to occur. This function
8867 * checks to see if mbox completions are on the mailbox completion queue
8868 * and will process all the completions associated with the eq for the
8869 * mailbox completion queue.
8870 **/
YueHaibingd7b761b2019-05-31 23:28:41 +08008871static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04008872lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
8873{
James Smartb71413d2018-02-22 08:18:40 -08008874 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04008875 uint32_t eqidx;
8876 struct lpfc_queue *fpeq = NULL;
James Smart657add42019-05-21 17:49:06 -07008877 struct lpfc_queue *eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04008878 bool mbox_pending;
8879
8880 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8881 return false;
8882
James Smart657add42019-05-21 17:49:06 -07008883 /* Find the EQ associated with the mbox CQ */
8884 if (sli4_hba->hdwq) {
8885 for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
8886 eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
Daniel Wagner535fb492019-10-18 18:21:11 +02008887 if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
James Smart657add42019-05-21 17:49:06 -07008888 fpeq = eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04008889 break;
8890 }
James Smart657add42019-05-21 17:49:06 -07008891 }
8892 }
James Smarte8d3c3b2013-10-10 12:21:30 -04008893 if (!fpeq)
8894 return false;
8895
8896 /* Turn off interrupts from this EQ */
8897
James Smartb71413d2018-02-22 08:18:40 -08008898 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04008899
8900 /* Check to see if a mbox completion is pending */
8901
8902 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
8903
8904 /*
8905 * If a mbox completion is pending, process all the events on EQ
8906 * associated with the mbox completion queue (this could include
8907 * mailbox commands, async events, els commands, receive queue data
8908 * and fcp commands)
8909 */
8910
8911 if (mbox_pending)
James Smart32517fc2019-01-28 11:14:33 -08008912 /* process and rearm the EQ */
James Smart93a4d6f2019-11-04 16:57:05 -08008913 lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
James Smart32517fc2019-01-28 11:14:33 -08008914 else
8915 /* Always clear and re-arm the EQ */
8916 sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04008917
8918 return mbox_pending;
8919
8920}
James Smarte59058c2008-08-24 21:49:00 -04008921
8922/**
James Smart3621a712009-04-06 18:47:14 -04008923 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04008924 * @phba: Pointer to HBA context object.
8925 *
8926 * This function is called from worker thread when a mailbox command times out.
8927 * The caller is not required to hold any locks. This function will reset the
8928 * HBA and recover all the pending commands.
8929 **/
dea31012005-04-17 16:05:31 -05008930void
8931lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
8932{
James Smart2e0fef82007-06-17 19:56:36 -05008933 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04008934 MAILBOX_t *mb = NULL;
8935
James Smart1dcb58e2007-04-25 09:51:30 -04008936 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008937
James Smart9ec58ec2021-01-04 10:02:35 -08008938 /* If the mailbox completed, process the completion */
8939 lpfc_sli4_process_missed_mbox_completions(phba);
8940
8941 if (!(psli->sli_flag & LPFC_SLI_ACTIVE))
James Smarte8d3c3b2013-10-10 12:21:30 -04008942 return;
8943
James Smarteb016562014-09-03 12:58:06 -04008944 if (pmbox != NULL)
8945 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04008946 /* Check the pmbox pointer first. There is a race condition
8947 * between the mbox timeout handler getting executed in the
8948 * worklist and the mailbox actually completing. When this
8949 * race condition occurs, the mbox_active will be NULL.
8950 */
8951 spin_lock_irq(&phba->hbalock);
8952 if (pmbox == NULL) {
8953 lpfc_printf_log(phba, KERN_WARNING,
8954 LOG_MBOX | LOG_SLI,
8955 "0353 Active Mailbox cleared - mailbox timeout "
8956 "exiting\n");
8957 spin_unlock_irq(&phba->hbalock);
8958 return;
8959 }
8960
dea31012005-04-17 16:05:31 -05008961 /* Mbox cmd <mbxCommand> timeout */
Dick Kennedy372c1872020-06-30 14:50:00 -07008962 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07008963 "0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008964 mb->mbxCommand,
8965 phba->pport->port_state,
8966 phba->sli.sli_flag,
8967 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04008968 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008969
James Smart1dcb58e2007-04-25 09:51:30 -04008970 /* Setting state unknown so lpfc_sli_abort_iocb_ring
8971 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008972 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04008973 */
James Smart2e0fef82007-06-17 19:56:36 -05008974 spin_lock_irq(&phba->pport->work_port_lock);
8975 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8976 spin_unlock_irq(&phba->pport->work_port_lock);
8977 spin_lock_irq(&phba->hbalock);
8978 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04008979 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008980 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04008981
Dick Kennedy372c1872020-06-30 14:50:00 -07008982 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart76bb24e2007-10-27 13:38:00 -04008983 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04008984
8985 /* Reset the HBA device */
8986 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05008987}
8988
James Smarte59058c2008-08-24 21:49:00 -04008989/**
James Smart3772a992009-05-22 14:50:54 -04008990 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04008991 * @phba: Pointer to HBA context object.
8992 * @pmbox: Pointer to mailbox object.
8993 * @flag: Flag indicating how the mailbox need to be processed.
8994 *
8995 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04008996 * to submit a mailbox command to firmware with SLI-3 interface spec. This
8997 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04008998 * The mailbox command can be submitted in polling mode, in which case
8999 * this function will wait in a polling loop for the completion of the
9000 * mailbox.
9001 * If the mailbox is submitted in no_wait mode (not polling) the
9002 * function will submit the command and returns immediately without waiting
9003 * for the mailbox completion. The no_wait is supported only when HBA
9004 * is in SLI2/SLI3 mode - interrupts are enabled.
9005 * The SLI interface allows only one mailbox pending at a time. If the
9006 * mailbox is issued in polling mode and there is already a mailbox
9007 * pending, then the function will return an error. If the mailbox is issued
9008 * in NO_WAIT mode and there is a mailbox pending already, the function
9009 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
9010 * The sli layer owns the mailbox object until the completion of mailbox
9011 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
9012 * return codes the caller owns the mailbox command after the return of
9013 * the function.
9014 **/
James Smart3772a992009-05-22 14:50:54 -04009015static int
9016lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
9017 uint32_t flag)
dea31012005-04-17 16:05:31 -05009018{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009019 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05009020 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009021 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05009022 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05009023 int i;
James Smart09372822008-01-11 01:52:54 -05009024 unsigned long timeout;
dea31012005-04-17 16:05:31 -05009025 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04009026 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05009027 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04009028 int processing_queue = 0;
9029
9030 spin_lock_irqsave(&phba->hbalock, drvr_flag);
9031 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04009032 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04009033 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04009034 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9035 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9036 return MBX_SUCCESS;
9037 }
James Smart58da1ff2008-04-07 10:15:56 -04009038 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04009039 pmbox = lpfc_mbox_get(phba);
9040 if (!pmbox) {
9041 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9042 return MBX_SUCCESS;
9043 }
9044 }
dea31012005-04-17 16:05:31 -05009045
James Smarted957682007-06-17 19:56:37 -05009046 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05009047 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05009048 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04009049 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05009050 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05009051 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04009052 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04009053 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05009054 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04009055 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05009056 }
9057 }
9058
Linas Vepstas8d63f372007-02-14 14:28:36 -06009059 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04009060 if (unlikely(pci_channel_offline(phba->pcidev))) {
9061 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9062 goto out_not_finished;
9063 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009064
James Smarta257bf92009-04-06 18:48:10 -04009065 /* If HBA has a deferred error attention, fail the iocb. */
9066 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9067 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9068 goto out_not_finished;
9069 }
9070
dea31012005-04-17 16:05:31 -05009071 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05009072
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009073 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05009074 status = MBX_SUCCESS;
9075
James Smart2e0fef82007-06-17 19:56:36 -05009076 if (phba->link_state == LPFC_HBA_ERROR) {
9077 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05009078
9079 /* Mbox command <mbxCommand> cannot issue */
Dick Kennedy372c1872020-06-30 14:50:00 -07009080 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04009081 "(%d):0311 Mailbox command x%x cannot "
9082 "issue Data: x%x x%x\n",
9083 pmbox->vport ? pmbox->vport->vpi : 0,
9084 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04009085 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05009086 }
9087
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009088 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05009089 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
9090 !(hc_copy & HC_MBINT_ENA)) {
9091 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Dick Kennedy372c1872020-06-30 14:50:00 -07009092 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04009093 "(%d):2528 Mailbox command x%x cannot "
9094 "issue Data: x%x x%x\n",
9095 pmbox->vport ? pmbox->vport->vpi : 0,
9096 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05009097 goto out_not_finished;
9098 }
James Smart92908312006-03-07 15:04:13 -05009099 }
9100
dea31012005-04-17 16:05:31 -05009101 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9102 /* Polling for a mbox command when another one is already active
9103 * is not allowed in SLI. Also, the driver must have established
9104 * SLI2 mode to queue and process multiple mbox commands.
9105 */
9106
9107 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05009108 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05009109
9110 /* Mbox command <mbxCommand> cannot issue */
Dick Kennedy372c1872020-06-30 14:50:00 -07009111 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04009112 "(%d):2529 Mailbox command x%x "
9113 "cannot issue Data: x%x x%x\n",
9114 pmbox->vport ? pmbox->vport->vpi : 0,
9115 pmbox->u.mb.mbxCommand,
9116 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04009117 goto out_not_finished;
dea31012005-04-17 16:05:31 -05009118 }
9119
James Smart3772a992009-05-22 14:50:54 -04009120 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05009121 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05009122 /* Mbox command <mbxCommand> cannot issue */
Dick Kennedy372c1872020-06-30 14:50:00 -07009123 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04009124 "(%d):2530 Mailbox command x%x "
9125 "cannot issue Data: x%x x%x\n",
9126 pmbox->vport ? pmbox->vport->vpi : 0,
9127 pmbox->u.mb.mbxCommand,
9128 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04009129 goto out_not_finished;
dea31012005-04-17 16:05:31 -05009130 }
9131
dea31012005-04-17 16:05:31 -05009132 /* Another mailbox command is still being processed, queue this
9133 * command to be processed later.
9134 */
9135 lpfc_mbox_put(phba, pmbox);
9136
9137 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05009138 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009139 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009140 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05009141 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07009142 mbx->mbxCommand,
9143 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05009144 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05009145
9146 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05009147 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05009148
James Smart858c9f62007-06-17 19:56:39 -05009149 if (pmbox->vport) {
9150 lpfc_debugfs_disc_trc(pmbox->vport,
9151 LPFC_DISC_TRC_MBOX_VPORT,
9152 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009153 (uint32_t)mbx->mbxCommand,
9154 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05009155 }
9156 else {
9157 lpfc_debugfs_disc_trc(phba->pport,
9158 LPFC_DISC_TRC_MBOX,
9159 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009160 (uint32_t)mbx->mbxCommand,
9161 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05009162 }
9163
James Smart2e0fef82007-06-17 19:56:36 -05009164 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05009165 }
9166
dea31012005-04-17 16:05:31 -05009167 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9168
9169 /* If we are not polling, we MUST be in SLI2 mode */
9170 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04009171 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009172 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05009173 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05009174 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05009175 /* Mbox command <mbxCommand> cannot issue */
Dick Kennedy372c1872020-06-30 14:50:00 -07009176 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04009177 "(%d):2531 Mailbox command x%x "
9178 "cannot issue Data: x%x x%x\n",
9179 pmbox->vport ? pmbox->vport->vpi : 0,
9180 pmbox->u.mb.mbxCommand,
9181 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04009182 goto out_not_finished;
dea31012005-04-17 16:05:31 -05009183 }
9184 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04009185 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
9186 1000);
9187 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05009188 }
9189
9190 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05009191 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009192 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05009193 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009194 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07009195 mbx->mbxCommand,
9196 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05009197 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05009198
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009199 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05009200 if (pmbox->vport) {
9201 lpfc_debugfs_disc_trc(pmbox->vport,
9202 LPFC_DISC_TRC_MBOX_VPORT,
9203 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009204 (uint32_t)mbx->mbxCommand,
9205 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05009206 }
9207 else {
9208 lpfc_debugfs_disc_trc(phba->pport,
9209 LPFC_DISC_TRC_MBOX,
9210 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009211 (uint32_t)mbx->mbxCommand,
9212 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05009213 }
9214 }
9215
dea31012005-04-17 16:05:31 -05009216 psli->slistat.mbox_cmd++;
9217 evtctr = psli->slistat.mbox_event;
9218
9219 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009220 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05009221
James Smart3772a992009-05-22 14:50:54 -04009222 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04009223 /* Populate mbox extension offset word. */
9224 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009225 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04009226 = (uint8_t *)phba->mbox_ext
9227 - (uint8_t *)phba->mbox;
9228 }
9229
9230 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08009231 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
9232 lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
9233 (uint8_t *)phba->mbox_ext,
9234 pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04009235 }
9236 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009237 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05009238 } else {
James Smart7a470272010-03-15 11:25:20 -04009239 /* Populate mbox extension offset word. */
9240 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009241 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04009242 = MAILBOX_HBA_EXT_OFFSET;
9243
9244 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08009245 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -04009246 lpfc_memcpy_to_slim(phba->MBslimaddr +
9247 MAILBOX_HBA_EXT_OFFSET,
James Smart3e1f0712018-11-29 16:09:29 -08009248 pmbox->ctx_buf, pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04009249
James Smart895427b2017-02-12 13:52:30 -08009250 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05009251 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08009252 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
9253 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05009254
9255 /* First copy mbox command data to HBA SLIM, skip past first
9256 word */
9257 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009258 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05009259 MAILBOX_CMD_SIZE - sizeof (uint32_t));
9260
9261 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009262 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05009263 to_slim = phba->MBslimaddr;
9264 writel(ldata, to_slim);
9265 readl(to_slim); /* flush */
9266
James Smart895427b2017-02-12 13:52:30 -08009267 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05009268 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04009269 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05009270 }
9271
9272 wmb();
dea31012005-04-17 16:05:31 -05009273
9274 switch (flag) {
9275 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05009276 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05009277 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05009278 /* Interrupt board to do it */
9279 writel(CA_MBATT, phba->CAregaddr);
9280 readl(phba->CAregaddr); /* flush */
9281 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05009282 break;
9283
9284 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05009285 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05009286 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05009287 /* Interrupt board to do it */
9288 writel(CA_MBATT, phba->CAregaddr);
9289 readl(phba->CAregaddr); /* flush */
9290
James Smart3772a992009-05-22 14:50:54 -04009291 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05009292 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04009293 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05009294 word0 = le32_to_cpu(word0);
9295 } else {
9296 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05009297 if (lpfc_readl(phba->MBslimaddr, &word0)) {
9298 spin_unlock_irqrestore(&phba->hbalock,
9299 drvr_flag);
9300 goto out_not_finished;
9301 }
dea31012005-04-17 16:05:31 -05009302 }
9303
9304 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05009305 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9306 spin_unlock_irqrestore(&phba->hbalock,
9307 drvr_flag);
9308 goto out_not_finished;
9309 }
James Smarta183a152011-10-10 21:32:43 -04009310 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
9311 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05009312 i = 0;
dea31012005-04-17 16:05:31 -05009313 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05009314 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
9315 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05009316 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05009317 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05009318 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05009319 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05009320 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04009321 goto out_not_finished;
dea31012005-04-17 16:05:31 -05009322 }
9323
9324 /* Check if we took a mbox interrupt while we were
9325 polling */
9326 if (((word0 & OWN_CHIP) != OWN_CHIP)
9327 && (evtctr != psli->slistat.mbox_event))
9328 break;
9329
James Smart09372822008-01-11 01:52:54 -05009330 if (i++ > 10) {
9331 spin_unlock_irqrestore(&phba->hbalock,
9332 drvr_flag);
9333 msleep(1);
9334 spin_lock_irqsave(&phba->hbalock, drvr_flag);
9335 }
dea31012005-04-17 16:05:31 -05009336
James Smart3772a992009-05-22 14:50:54 -04009337 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05009338 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04009339 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05009340 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009341 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05009342 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04009343 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05009344 /* Check real SLIM for any errors */
9345 slimword0 = readl(phba->MBslimaddr);
9346 slimmb = (MAILBOX_t *) & slimword0;
9347 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
9348 && slimmb->mbxStatus) {
9349 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04009350 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05009351 word0 = slimword0;
9352 }
9353 }
9354 } else {
9355 /* First copy command data */
9356 word0 = readl(phba->MBslimaddr);
9357 }
9358 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05009359 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9360 spin_unlock_irqrestore(&phba->hbalock,
9361 drvr_flag);
9362 goto out_not_finished;
9363 }
dea31012005-04-17 16:05:31 -05009364 }
9365
James Smart3772a992009-05-22 14:50:54 -04009366 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05009367 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08009368 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
9369 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04009370 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08009371 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
James Smart7a470272010-03-15 11:25:20 -04009372 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -08009373 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04009374 pmbox->out_ext_byte_len);
9375 }
dea31012005-04-17 16:05:31 -05009376 } else {
9377 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009378 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08009379 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04009380 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08009381 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
9382 lpfc_memcpy_from_slim(
9383 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04009384 phba->MBslimaddr +
9385 MAILBOX_HBA_EXT_OFFSET,
9386 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05009387 }
9388 }
9389
9390 writel(HA_MBATT, phba->HAregaddr);
9391 readl(phba->HAregaddr); /* flush */
9392
9393 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009394 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05009395 }
9396
James Smart2e0fef82007-06-17 19:56:36 -05009397 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9398 return status;
James Smart58da1ff2008-04-07 10:15:56 -04009399
9400out_not_finished:
9401 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04009402 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04009403 lpfc_mbox_cmpl_put(phba, pmbox);
9404 }
9405 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009406}
9407
James Smarte59058c2008-08-24 21:49:00 -04009408/**
James Smartf1126682009-06-10 17:22:44 -04009409 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
9410 * @phba: Pointer to HBA context object.
9411 *
9412 * The function blocks the posting of SLI4 asynchronous mailbox commands from
9413 * the driver internal pending mailbox queue. It will then try to wait out the
9414 * possible outstanding mailbox command before return.
9415 *
9416 * Returns:
9417 * 0 - the outstanding mailbox command completed; otherwise, the wait for
9418 * the outstanding mailbox command timed out.
9419 **/
9420static int
9421lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
9422{
9423 struct lpfc_sli *psli = &phba->sli;
James Smarta9978e32021-07-07 11:43:43 -07009424 LPFC_MBOXQ_t *mboxq;
James Smartf1126682009-06-10 17:22:44 -04009425 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04009426 unsigned long timeout = 0;
James Smarta9978e32021-07-07 11:43:43 -07009427 u32 sli_flag;
9428 u8 cmd, subsys, opcode;
James Smartf1126682009-06-10 17:22:44 -04009429
9430 /* Mark the asynchronous mailbox command posting as blocked */
9431 spin_lock_irq(&phba->hbalock);
9432 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04009433 /* Determine how long we might wait for the active mailbox
9434 * command to be gracefully completed by firmware.
9435 */
James Smarta183a152011-10-10 21:32:43 -04009436 if (phba->sli.mbox_active)
9437 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9438 phba->sli.mbox_active) *
9439 1000) + jiffies;
9440 spin_unlock_irq(&phba->hbalock);
9441
James Smarte8d3c3b2013-10-10 12:21:30 -04009442 /* Make sure the mailbox is really active */
9443 if (timeout)
9444 lpfc_sli4_process_missed_mbox_completions(phba);
9445
James Smarta9978e32021-07-07 11:43:43 -07009446 /* Wait for the outstanding mailbox command to complete */
James Smartf1126682009-06-10 17:22:44 -04009447 while (phba->sli.mbox_active) {
9448 /* Check active mailbox complete status every 2ms */
9449 msleep(2);
9450 if (time_after(jiffies, timeout)) {
James Smarta9978e32021-07-07 11:43:43 -07009451 /* Timeout, mark the outstanding cmd not complete */
9452
9453 /* Sanity check sli.mbox_active has not completed or
9454 * cancelled from another context during last 2ms sleep,
9455 * so take hbalock to be sure before logging.
9456 */
9457 spin_lock_irq(&phba->hbalock);
9458 if (phba->sli.mbox_active) {
9459 mboxq = phba->sli.mbox_active;
9460 cmd = mboxq->u.mb.mbxCommand;
9461 subsys = lpfc_sli_config_mbox_subsys_get(phba,
9462 mboxq);
9463 opcode = lpfc_sli_config_mbox_opcode_get(phba,
9464 mboxq);
9465 sli_flag = psli->sli_flag;
9466 spin_unlock_irq(&phba->hbalock);
9467 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9468 "2352 Mailbox command x%x "
9469 "(x%x/x%x) sli_flag x%x could "
9470 "not complete\n",
9471 cmd, subsys, opcode,
9472 sli_flag);
9473 } else {
9474 spin_unlock_irq(&phba->hbalock);
9475 }
9476
James Smartf1126682009-06-10 17:22:44 -04009477 rc = 1;
9478 break;
9479 }
9480 }
9481
9482 /* Can not cleanly block async mailbox command, fails it */
9483 if (rc) {
9484 spin_lock_irq(&phba->hbalock);
9485 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9486 spin_unlock_irq(&phba->hbalock);
9487 }
9488 return rc;
9489}
9490
9491/**
9492 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
9493 * @phba: Pointer to HBA context object.
9494 *
9495 * The function unblocks and resume posting of SLI4 asynchronous mailbox
9496 * commands from the driver internal pending mailbox queue. It makes sure
9497 * that there is no outstanding mailbox command before resuming posting
9498 * asynchronous mailbox commands. If, for any reason, there is outstanding
9499 * mailbox command, it will try to wait it out before resuming asynchronous
9500 * mailbox command posting.
9501 **/
9502static void
9503lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
9504{
9505 struct lpfc_sli *psli = &phba->sli;
9506
9507 spin_lock_irq(&phba->hbalock);
9508 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9509 /* Asynchronous mailbox posting is not blocked, do nothing */
9510 spin_unlock_irq(&phba->hbalock);
9511 return;
9512 }
9513
9514 /* Outstanding synchronous mailbox command is guaranteed to be done,
9515 * successful or timeout, after timing-out the outstanding mailbox
9516 * command shall always be removed, so just unblock posting async
9517 * mailbox command and resume
9518 */
9519 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9520 spin_unlock_irq(&phba->hbalock);
9521
Colin Ian King291c2542019-12-18 08:43:01 +00009522 /* wake up worker thread to post asynchronous mailbox command */
James Smartf1126682009-06-10 17:22:44 -04009523 lpfc_worker_wake_up(phba);
9524}
9525
9526/**
James Smart2d843ed2012-09-29 11:29:06 -04009527 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
9528 * @phba: Pointer to HBA context object.
9529 * @mboxq: Pointer to mailbox object.
9530 *
9531 * The function waits for the bootstrap mailbox register ready bit from
9532 * port for twice the regular mailbox command timeout value.
9533 *
9534 * 0 - no timeout on waiting for bootstrap mailbox register ready.
9535 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
9536 **/
9537static int
9538lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9539{
9540 uint32_t db_ready;
9541 unsigned long timeout;
9542 struct lpfc_register bmbx_reg;
9543
9544 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
9545 * 1000) + jiffies;
9546
9547 do {
9548 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
9549 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
9550 if (!db_ready)
James Smarte2ffe4d2019-03-12 16:30:15 -07009551 mdelay(2);
James Smart2d843ed2012-09-29 11:29:06 -04009552
9553 if (time_after(jiffies, timeout))
9554 return MBXERR_ERROR;
9555 } while (!db_ready);
9556
9557 return 0;
9558}
9559
9560/**
James Smartda0436e2009-05-22 14:51:39 -04009561 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
9562 * @phba: Pointer to HBA context object.
9563 * @mboxq: Pointer to mailbox object.
9564 *
9565 * The function posts a mailbox to the port. The mailbox is expected
9566 * to be comletely filled in and ready for the port to operate on it.
9567 * This routine executes a synchronous completion operation on the
9568 * mailbox by polling for its completion.
9569 *
9570 * The caller must not be holding any locks when calling this routine.
9571 *
9572 * Returns:
9573 * MBX_SUCCESS - mailbox posted successfully
9574 * Any of the MBX error values.
9575 **/
9576static int
9577lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9578{
9579 int rc = MBX_SUCCESS;
9580 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04009581 uint32_t mcqe_status;
9582 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04009583 struct lpfc_sli *psli = &phba->sli;
9584 struct lpfc_mqe *mb = &mboxq->u.mqe;
9585 struct lpfc_bmbx_create *mbox_rgn;
9586 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04009587
9588 /*
9589 * Only one mailbox can be active to the bootstrap mailbox region
9590 * at a time and there is no queueing provided.
9591 */
9592 spin_lock_irqsave(&phba->hbalock, iflag);
9593 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9594 spin_unlock_irqrestore(&phba->hbalock, iflag);
Dick Kennedy372c1872020-06-30 14:50:00 -07009595 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta183a152011-10-10 21:32:43 -04009596 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04009597 "cannot issue Data: x%x x%x\n",
9598 mboxq->vport ? mboxq->vport->vpi : 0,
9599 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009600 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9601 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009602 psli->sli_flag, MBX_POLL);
9603 return MBXERR_ERROR;
9604 }
9605 /* The server grabs the token and owns it until release */
9606 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9607 phba->sli.mbox_active = mboxq;
9608 spin_unlock_irqrestore(&phba->hbalock, iflag);
9609
James Smart2d843ed2012-09-29 11:29:06 -04009610 /* wait for bootstrap mbox register for readyness */
9611 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9612 if (rc)
9613 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04009614 /*
9615 * Initialize the bootstrap memory region to avoid stale data areas
9616 * in the mailbox post. Then copy the caller's mailbox contents to
9617 * the bmbx mailbox region.
9618 */
9619 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
9620 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07009621 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
9622 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04009623
9624 /* Post the high mailbox dma address to the port and wait for ready. */
9625 dma_address = &phba->sli4_hba.bmbx.dma_address;
9626 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
9627
James Smart2d843ed2012-09-29 11:29:06 -04009628 /* wait for bootstrap mbox register for hi-address write done */
9629 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9630 if (rc)
9631 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04009632
9633 /* Post the low mailbox dma address to the port. */
9634 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04009635
James Smart2d843ed2012-09-29 11:29:06 -04009636 /* wait for bootstrap mbox register for low address write done */
9637 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9638 if (rc)
9639 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04009640
9641 /*
9642 * Read the CQ to ensure the mailbox has completed.
9643 * If so, update the mailbox status so that the upper layers
9644 * can complete the request normally.
9645 */
James Smart48f8fdb2018-05-04 20:37:51 -07009646 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
9647 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04009648 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07009649 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
9650 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04009651 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04009652 /*
9653 * When the CQE status indicates a failure and the mailbox status
9654 * indicates success then copy the CQE status into the mailbox status
9655 * (and prefix it with x4000).
9656 */
James Smartda0436e2009-05-22 14:51:39 -04009657 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04009658 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
9659 bf_set(lpfc_mqe_status, mb,
9660 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04009661 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04009662 } else
9663 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04009664
9665 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04009666 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04009667 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
9668 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04009669 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
9670 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9671 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009672 bf_get(lpfc_mqe_status, mb),
9673 mb->un.mb_words[0], mb->un.mb_words[1],
9674 mb->un.mb_words[2], mb->un.mb_words[3],
9675 mb->un.mb_words[4], mb->un.mb_words[5],
9676 mb->un.mb_words[6], mb->un.mb_words[7],
9677 mb->un.mb_words[8], mb->un.mb_words[9],
9678 mb->un.mb_words[10], mb->un.mb_words[11],
9679 mb->un.mb_words[12], mboxq->mcqe.word0,
9680 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
9681 mboxq->mcqe.trailer);
9682exit:
9683 /* We are holding the token, no needed for lock when release */
9684 spin_lock_irqsave(&phba->hbalock, iflag);
9685 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9686 phba->sli.mbox_active = NULL;
9687 spin_unlock_irqrestore(&phba->hbalock, iflag);
9688 return rc;
9689}
9690
9691/**
9692 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
9693 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01009694 * @mboxq: Pointer to mailbox object.
James Smartda0436e2009-05-22 14:51:39 -04009695 * @flag: Flag indicating how the mailbox need to be processed.
9696 *
9697 * This function is called by discovery code and HBA management code to submit
9698 * a mailbox command to firmware with SLI-4 interface spec.
9699 *
9700 * Return codes the caller owns the mailbox command after the return of the
9701 * function.
9702 **/
9703static int
9704lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
9705 uint32_t flag)
9706{
9707 struct lpfc_sli *psli = &phba->sli;
9708 unsigned long iflags;
9709 int rc;
9710
James Smartb76f2dc2011-07-22 18:37:42 -04009711 /* dump from issue mailbox command if setup */
9712 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
9713
James Smart8fa38512009-07-19 10:01:03 -04009714 rc = lpfc_mbox_dev_check(phba);
9715 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009716 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta183a152011-10-10 21:32:43 -04009717 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04009718 "cannot issue Data: x%x x%x\n",
9719 mboxq->vport ? mboxq->vport->vpi : 0,
9720 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009721 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9722 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04009723 psli->sli_flag, flag);
9724 goto out_not_finished;
9725 }
9726
James Smartda0436e2009-05-22 14:51:39 -04009727 /* Detect polling mode and jump to a handler */
9728 if (!phba->sli4_hba.intr_enable) {
9729 if (flag == MBX_POLL)
9730 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
9731 else
9732 rc = -EIO;
9733 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04009734 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04009735 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04009736 "(x%x/x%x) failure: "
9737 "mqe_sta: x%x mcqe_sta: x%x/x%x "
9738 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04009739 mboxq->vport ? mboxq->vport->vpi : 0,
9740 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009741 lpfc_sli_config_mbox_subsys_get(phba,
9742 mboxq),
9743 lpfc_sli_config_mbox_opcode_get(phba,
9744 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04009745 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
9746 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
9747 bf_get(lpfc_mcqe_ext_status,
9748 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04009749 psli->sli_flag, flag);
9750 return rc;
9751 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04009752 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
9753 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08009754 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04009755 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04009756 mboxq->vport ? mboxq->vport->vpi : 0,
9757 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009758 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9759 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009760 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04009761 /* Try to block the asynchronous mailbox posting */
9762 rc = lpfc_sli4_async_mbox_block(phba);
9763 if (!rc) {
9764 /* Successfully blocked, now issue sync mbox cmd */
9765 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
9766 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04009767 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04009768 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04009769 "(%d):2597 Sync Mailbox command "
9770 "x%x (x%x/x%x) failure: "
9771 "mqe_sta: x%x mcqe_sta: x%x/x%x "
9772 "Data: x%x x%x\n,",
9773 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04009774 mboxq->u.mb.mbxCommand,
9775 lpfc_sli_config_mbox_subsys_get(phba,
9776 mboxq),
9777 lpfc_sli_config_mbox_opcode_get(phba,
9778 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04009779 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
9780 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
9781 bf_get(lpfc_mcqe_ext_status,
9782 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04009783 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04009784 /* Unblock the async mailbox posting afterward */
9785 lpfc_sli4_async_mbox_unblock(phba);
9786 }
9787 return rc;
James Smartda0436e2009-05-22 14:51:39 -04009788 }
9789
Colin Ian King291c2542019-12-18 08:43:01 +00009790 /* Now, interrupt mode asynchronous mailbox command */
James Smartda0436e2009-05-22 14:51:39 -04009791 rc = lpfc_mbox_cmd_check(phba, mboxq);
9792 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009793 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta183a152011-10-10 21:32:43 -04009794 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04009795 "cannot issue Data: x%x x%x\n",
9796 mboxq->vport ? mboxq->vport->vpi : 0,
9797 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009798 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9799 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009800 psli->sli_flag, flag);
9801 goto out_not_finished;
9802 }
James Smartda0436e2009-05-22 14:51:39 -04009803
9804 /* Put the mailbox command to the driver internal FIFO */
9805 psli->slistat.mbox_busy++;
9806 spin_lock_irqsave(&phba->hbalock, iflags);
9807 lpfc_mbox_put(phba, mboxq);
9808 spin_unlock_irqrestore(&phba->hbalock, iflags);
9809 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9810 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04009811 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04009812 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
9813 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04009814 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9815 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009816 phba->pport->port_state,
9817 psli->sli_flag, MBX_NOWAIT);
9818 /* Wake up worker thread to transport mailbox command from head */
9819 lpfc_worker_wake_up(phba);
9820
9821 return MBX_BUSY;
9822
9823out_not_finished:
9824 return MBX_NOT_FINISHED;
9825}
9826
9827/**
9828 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
9829 * @phba: Pointer to HBA context object.
9830 *
9831 * This function is called by worker thread to send a mailbox command to
9832 * SLI4 HBA firmware.
9833 *
9834 **/
9835int
9836lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
9837{
9838 struct lpfc_sli *psli = &phba->sli;
9839 LPFC_MBOXQ_t *mboxq;
9840 int rc = MBX_SUCCESS;
9841 unsigned long iflags;
9842 struct lpfc_mqe *mqe;
9843 uint32_t mbx_cmnd;
9844
9845 /* Check interrupt mode before post async mailbox command */
9846 if (unlikely(!phba->sli4_hba.intr_enable))
9847 return MBX_NOT_FINISHED;
9848
9849 /* Check for mailbox command service token */
9850 spin_lock_irqsave(&phba->hbalock, iflags);
9851 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9852 spin_unlock_irqrestore(&phba->hbalock, iflags);
9853 return MBX_NOT_FINISHED;
9854 }
9855 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9856 spin_unlock_irqrestore(&phba->hbalock, iflags);
9857 return MBX_NOT_FINISHED;
9858 }
9859 if (unlikely(phba->sli.mbox_active)) {
9860 spin_unlock_irqrestore(&phba->hbalock, iflags);
Dick Kennedy372c1872020-06-30 14:50:00 -07009861 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009862 "0384 There is pending active mailbox cmd\n");
9863 return MBX_NOT_FINISHED;
9864 }
9865 /* Take the mailbox command service token */
9866 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9867
9868 /* Get the next mailbox command from head of queue */
9869 mboxq = lpfc_mbox_get(phba);
9870
9871 /* If no more mailbox command waiting for post, we're done */
9872 if (!mboxq) {
9873 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9874 spin_unlock_irqrestore(&phba->hbalock, iflags);
9875 return MBX_SUCCESS;
9876 }
9877 phba->sli.mbox_active = mboxq;
9878 spin_unlock_irqrestore(&phba->hbalock, iflags);
9879
9880 /* Check device readiness for posting mailbox command */
9881 rc = lpfc_mbox_dev_check(phba);
9882 if (unlikely(rc))
9883 /* Driver clean routine will clean up pending mailbox */
9884 goto out_not_finished;
9885
9886 /* Prepare the mbox command to be posted */
9887 mqe = &mboxq->u.mqe;
9888 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
9889
9890 /* Start timer for the mbox_tmo and log some mailbox post messages */
9891 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04009892 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04009893
9894 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04009895 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04009896 "x%x x%x\n",
9897 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04009898 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9899 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009900 phba->pport->port_state, psli->sli_flag);
9901
9902 if (mbx_cmnd != MBX_HEARTBEAT) {
9903 if (mboxq->vport) {
9904 lpfc_debugfs_disc_trc(mboxq->vport,
9905 LPFC_DISC_TRC_MBOX_VPORT,
9906 "MBOX Send vport: cmd:x%x mb:x%x x%x",
9907 mbx_cmnd, mqe->un.mb_words[0],
9908 mqe->un.mb_words[1]);
9909 } else {
9910 lpfc_debugfs_disc_trc(phba->pport,
9911 LPFC_DISC_TRC_MBOX,
9912 "MBOX Send: cmd:x%x mb:x%x x%x",
9913 mbx_cmnd, mqe->un.mb_words[0],
9914 mqe->un.mb_words[1]);
9915 }
9916 }
9917 psli->slistat.mbox_cmd++;
9918
9919 /* Post the mailbox command to the port */
9920 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
9921 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009922 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta183a152011-10-10 21:32:43 -04009923 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04009924 "cannot issue Data: x%x x%x\n",
9925 mboxq->vport ? mboxq->vport->vpi : 0,
9926 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009927 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9928 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009929 psli->sli_flag, MBX_NOWAIT);
9930 goto out_not_finished;
9931 }
9932
9933 return rc;
9934
9935out_not_finished:
9936 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05009937 if (phba->sli.mbox_active) {
9938 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9939 __lpfc_mbox_cmpl_put(phba, mboxq);
9940 /* Release the token */
9941 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9942 phba->sli.mbox_active = NULL;
9943 }
James Smartda0436e2009-05-22 14:51:39 -04009944 spin_unlock_irqrestore(&phba->hbalock, iflags);
9945
9946 return MBX_NOT_FINISHED;
9947}
9948
9949/**
9950 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
9951 * @phba: Pointer to HBA context object.
9952 * @pmbox: Pointer to mailbox object.
9953 * @flag: Flag indicating how the mailbox need to be processed.
9954 *
9955 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
9956 * the API jump table function pointer from the lpfc_hba struct.
9957 *
9958 * Return codes the caller owns the mailbox command after the return of the
9959 * function.
9960 **/
9961int
9962lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
9963{
9964 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
9965}
9966
9967/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009968 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04009969 * @phba: The hba struct for which this call is being executed.
9970 * @dev_grp: The HBA PCI-Device group number.
9971 *
9972 * This routine sets up the mbox interface API function jump table in @phba
9973 * struct.
9974 * Returns: 0 - success, -ENODEV - failure.
9975 **/
9976int
9977lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9978{
9979
9980 switch (dev_grp) {
9981 case LPFC_PCI_DEV_LP:
9982 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
9983 phba->lpfc_sli_handle_slow_ring_event =
9984 lpfc_sli_handle_slow_ring_event_s3;
9985 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
9986 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
9987 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
9988 break;
9989 case LPFC_PCI_DEV_OC:
9990 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
9991 phba->lpfc_sli_handle_slow_ring_event =
9992 lpfc_sli_handle_slow_ring_event_s4;
9993 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
9994 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
9995 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
9996 break;
9997 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07009998 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009999 "1420 Invalid HBA PCI-device group: 0x%x\n",
10000 dev_grp);
10001 return -ENODEV;
James Smartda0436e2009-05-22 14:51:39 -040010002 }
10003 return 0;
10004}
10005
10006/**
James Smart3621a712009-04-06 18:47:14 -040010007 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -040010008 * @phba: Pointer to HBA context object.
10009 * @pring: Pointer to driver SLI ring object.
10010 * @piocb: Pointer to address of newly added command iocb.
10011 *
James Smart27f3efd2019-10-18 14:18:19 -070010012 * This function is called with hbalock held for SLI3 ports or
10013 * the ring lock held for SLI4 ports to add a command
James Smarte59058c2008-08-24 21:49:00 -040010014 * iocb to the txq when SLI layer cannot submit the command iocb
10015 * to the ring.
10016 **/
James Smart2a9bf3d2010-06-07 15:24:45 -040010017void
James Smart92d7f7b2007-06-17 19:56:38 -050010018__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010019 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -050010020{
James Smart27f3efd2019-10-18 14:18:19 -070010021 if (phba->sli_rev == LPFC_SLI_REV4)
10022 lockdep_assert_held(&pring->ring_lock);
10023 else
10024 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010025 /* Insert the caller's iocb in the txq tail for later processing. */
10026 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -050010027}
10028
James Smarte59058c2008-08-24 21:49:00 -040010029/**
James Smart3621a712009-04-06 18:47:14 -040010030 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -040010031 * @phba: Pointer to HBA context object.
10032 * @pring: Pointer to driver SLI ring object.
10033 * @piocb: Pointer to address of newly added command iocb.
10034 *
10035 * This function is called with hbalock held before a new
10036 * iocb is submitted to the firmware. This function checks
10037 * txq to flush the iocbs in txq to Firmware before
10038 * submitting new iocbs to the Firmware.
10039 * If there are iocbs in the txq which need to be submitted
10040 * to firmware, lpfc_sli_next_iocb returns the first element
10041 * of the txq after dequeuing it from txq.
10042 * If there is no iocb in the txq then the function will return
10043 * *piocb and *piocb is set to NULL. Caller needs to check
10044 * *piocb to find if there are more commands in the txq.
10045 **/
dea31012005-04-17 16:05:31 -050010046static struct lpfc_iocbq *
10047lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010048 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -050010049{
10050 struct lpfc_iocbq * nextiocb;
10051
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010052 lockdep_assert_held(&phba->hbalock);
10053
dea31012005-04-17 16:05:31 -050010054 nextiocb = lpfc_sli_ringtx_get(phba, pring);
10055 if (!nextiocb) {
10056 nextiocb = *piocb;
10057 *piocb = NULL;
10058 }
10059
10060 return nextiocb;
10061}
10062
James Smarte59058c2008-08-24 21:49:00 -040010063/**
James Smart3772a992009-05-22 14:50:54 -040010064 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -040010065 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -040010066 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -040010067 * @piocb: Pointer to command iocb.
10068 * @flag: Flag indicating if this command can be put into txq.
10069 *
James Smart3772a992009-05-22 14:50:54 -040010070 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
10071 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
10072 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
10073 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
10074 * this function allows only iocbs for posting buffers. This function finds
10075 * next available slot in the command ring and posts the command to the
10076 * available slot and writes the port attention register to request HBA start
10077 * processing new iocb. If there is no slot available in the ring and
10078 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
10079 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -040010080 *
James Smart3772a992009-05-22 14:50:54 -040010081 * This function is called with hbalock held. The function will return success
10082 * after it successfully submit the iocb to firmware or after adding to the
10083 * txq.
James Smarte59058c2008-08-24 21:49:00 -040010084 **/
James Smart98c9ea52007-10-27 13:37:33 -040010085static int
James Smart3772a992009-05-22 14:50:54 -040010086__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -050010087 struct lpfc_iocbq *piocb, uint32_t flag)
10088{
10089 struct lpfc_iocbq *nextiocb;
10090 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -080010091 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050010092
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010093 lockdep_assert_held(&phba->hbalock);
10094
James Smart92d7f7b2007-06-17 19:56:38 -050010095 if (piocb->iocb_cmpl && (!piocb->vport) &&
10096 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
10097 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010098 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040010099 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -050010100 piocb->iocb.ulpCommand);
10101 dump_stack();
10102 return IOCB_ERROR;
10103 }
10104
10105
Linas Vepstas8d63f372007-02-14 14:28:36 -060010106 /* If the PCI channel is in offline state, do not post iocbs. */
10107 if (unlikely(pci_channel_offline(phba->pcidev)))
10108 return IOCB_ERROR;
10109
James Smarta257bf92009-04-06 18:48:10 -040010110 /* If HBA has a deferred error attention, fail the iocb. */
10111 if (unlikely(phba->hba_flag & DEFER_ERATT))
10112 return IOCB_ERROR;
10113
dea31012005-04-17 16:05:31 -050010114 /*
10115 * We should never get an IOCB if we are in a < LINK_DOWN state
10116 */
James Smart2e0fef82007-06-17 19:56:36 -050010117 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -050010118 return IOCB_ERROR;
10119
10120 /*
10121 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -040010122 * outstanding event.
dea31012005-04-17 16:05:31 -050010123 */
James Smart0b727fe2007-10-27 13:37:25 -040010124 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -050010125 goto iocb_busy;
10126
James Smart2e0fef82007-06-17 19:56:36 -050010127 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -050010128 /*
James Smart2680eea2007-04-25 09:52:55 -040010129 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -050010130 * can be issued if the link is not up.
10131 */
10132 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -040010133 case CMD_GEN_REQUEST64_CR:
10134 case CMD_GEN_REQUEST64_CX:
10135 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
10136 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -040010137 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -040010138 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
10139 MENLO_TRANSPORT_TYPE))
10140
10141 goto iocb_busy;
10142 break;
dea31012005-04-17 16:05:31 -050010143 case CMD_QUE_RING_BUF_CN:
10144 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -050010145 /*
10146 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
10147 * completion, iocb_cmpl MUST be 0.
10148 */
10149 if (piocb->iocb_cmpl)
10150 piocb->iocb_cmpl = NULL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050010151 fallthrough;
dea31012005-04-17 16:05:31 -050010152 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -040010153 case CMD_CLOSE_XRI_CN:
10154 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -050010155 break;
10156 default:
10157 goto iocb_busy;
10158 }
10159
10160 /*
10161 * For FCP commands, we must be in a state where we can process link
10162 * attention events.
10163 */
James Smart895427b2017-02-12 13:52:30 -080010164 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -050010165 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -050010166 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -050010167 }
dea31012005-04-17 16:05:31 -050010168
dea31012005-04-17 16:05:31 -050010169 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
10170 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
10171 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
10172
10173 if (iocb)
10174 lpfc_sli_update_ring(phba, pring);
10175 else
10176 lpfc_sli_update_full_ring(phba, pring);
10177
10178 if (!piocb)
10179 return IOCB_SUCCESS;
10180
10181 goto out_busy;
10182
10183 iocb_busy:
10184 pring->stats.iocb_cmd_delay++;
10185
10186 out_busy:
10187
10188 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010189 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -050010190 return IOCB_SUCCESS;
10191 }
10192
10193 return IOCB_BUSY;
10194}
10195
James Smart3772a992009-05-22 14:50:54 -040010196/**
James Smart4f774512009-05-22 14:52:35 -040010197 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
10198 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010010199 * @piocbq: Pointer to command iocb.
James Smart4f774512009-05-22 14:52:35 -040010200 * @sglq: Pointer to the scatter gather queue object.
10201 *
10202 * This routine converts the bpl or bde that is in the IOCB
10203 * to a sgl list for the sli4 hardware. The physical address
10204 * of the bpl/bde is converted back to a virtual address.
10205 * If the IOCB contains a BPL then the list of BDE's is
10206 * converted to sli4_sge's. If the IOCB contains a single
10207 * BDE then it is converted to a single sli_sge.
10208 * The IOCB is still in cpu endianess so the contents of
10209 * the bpl can be used without byte swapping.
10210 *
10211 * Returns valid XRI = Success, NO_XRI = Failure.
10212**/
10213static uint16_t
10214lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
10215 struct lpfc_sglq *sglq)
10216{
10217 uint16_t xritag = NO_XRI;
10218 struct ulp_bde64 *bpl = NULL;
10219 struct ulp_bde64 bde;
10220 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -050010221 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -040010222 IOCB_t *icmd;
10223 int numBdes = 0;
10224 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -050010225 uint32_t offset = 0; /* accumulated offset in the sg request list */
10226 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -040010227
10228 if (!piocbq || !sglq)
10229 return xritag;
10230
10231 sgl = (struct sli4_sge *)sglq->sgl;
10232 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -050010233 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
10234 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040010235 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
10236 numBdes = icmd->un.genreq64.bdl.bdeSize /
10237 sizeof(struct ulp_bde64);
10238 /* The addrHigh and addrLow fields within the IOCB
10239 * have not been byteswapped yet so there is no
10240 * need to swap them back.
10241 */
James Smart1b511972011-12-13 13:23:09 -050010242 if (piocbq->context3)
10243 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
10244 else
10245 return xritag;
James Smart4f774512009-05-22 14:52:35 -040010246
James Smart1b511972011-12-13 13:23:09 -050010247 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -040010248 if (!bpl)
10249 return xritag;
10250
10251 for (i = 0; i < numBdes; i++) {
10252 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -050010253 sgl->addr_hi = bpl->addrHigh;
10254 sgl->addr_lo = bpl->addrLow;
10255
James Smart05580562011-05-24 11:40:48 -040010256 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -040010257 if ((i+1) == numBdes)
10258 bf_set(lpfc_sli4_sge_last, sgl, 1);
10259 else
10260 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -050010261 /* swap the size field back to the cpu so we
10262 * can assign it to the sgl.
10263 */
10264 bde.tus.w = le32_to_cpu(bpl->tus.w);
10265 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -050010266 /* The offsets in the sgl need to be accumulated
10267 * separately for the request and reply lists.
10268 * The request is always first, the reply follows.
10269 */
10270 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
10271 /* add up the reply sg entries */
10272 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
10273 inbound++;
10274 /* first inbound? reset the offset */
10275 if (inbound == 1)
10276 offset = 0;
10277 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -040010278 bf_set(lpfc_sli4_sge_type, sgl,
10279 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -050010280 offset += bde.tus.f.bdeSize;
10281 }
James Smart546fc852011-03-11 16:06:29 -050010282 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -040010283 bpl++;
10284 sgl++;
10285 }
10286 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
10287 /* The addrHigh and addrLow fields of the BDE have not
10288 * been byteswapped yet so they need to be swapped
10289 * before putting them in the sgl.
10290 */
10291 sgl->addr_hi =
10292 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
10293 sgl->addr_lo =
10294 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -040010295 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -040010296 bf_set(lpfc_sli4_sge_last, sgl, 1);
10297 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -050010298 sgl->sge_len =
10299 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -040010300 }
10301 return sglq->sli4_xritag;
10302}
10303
10304/**
Lee Jones8514e2f2021-03-03 14:46:18 +000010305 * lpfc_sli4_iocb2wqe - Convert the IOCB to a work queue entry.
James Smart4f774512009-05-22 14:52:35 -040010306 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010010307 * @iocbq: Pointer to command iocb.
James Smart4f774512009-05-22 14:52:35 -040010308 * @wqe: Pointer to the work queue entry.
10309 *
10310 * This routine converts the iocb command to its Work Queue Entry
10311 * equivalent. The wqe pointer should not have any fields set when
10312 * this routine is called because it will memcpy over them.
10313 * This routine does not set the CQ_ID or the WQEC bits in the
10314 * wqe.
10315 *
10316 * Returns: 0 = Success, IOCB_ERROR = Failure.
10317 **/
10318static int
10319lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -080010320 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -040010321{
James Smart5ffc2662009-11-18 15:39:44 -050010322 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -040010323 uint8_t ct = 0;
10324 uint32_t fip;
10325 uint32_t abort_tag;
10326 uint8_t command_type = ELS_COMMAND_NON_FIP;
10327 uint8_t cmnd;
10328 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -040010329 uint16_t abrt_iotag;
10330 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -040010331 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -040010332 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -050010333 int numBdes, i;
10334 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -040010335 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -050010336 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -050010337 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -040010338
James Smart45ed1192009-10-02 15:17:02 -040010339 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -040010340 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -040010341 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -040010342 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -050010343 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -040010344 command_type = ELS_COMMAND_FIP;
10345 else
10346 command_type = ELS_COMMAND_NON_FIP;
10347
James Smartb5c53952016-03-31 14:12:30 -070010348 if (phba->fcp_embed_io)
10349 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -040010350 /* Some of the fields are in the right position already */
10351 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smarte62245d2019-08-14 16:57:08 -070010352 /* The ct field has moved so reset */
10353 wqe->generic.wqe_com.word7 = 0;
10354 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -070010355
10356 abort_tag = (uint32_t) iocbq->iotag;
10357 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040010358 /* words0-2 bpl convert bde */
10359 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -050010360 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
10361 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -040010362 bpl = (struct ulp_bde64 *)
10363 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
10364 if (!bpl)
10365 return IOCB_ERROR;
10366
10367 /* Should already be byte swapped. */
10368 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
10369 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
10370 /* swap the size field back to the cpu so we
10371 * can assign it to the sgl.
10372 */
10373 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -050010374 xmit_len = wqe->generic.bde.tus.f.bdeSize;
10375 total_len = 0;
10376 for (i = 0; i < numBdes; i++) {
10377 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
10378 total_len += bde.tus.f.bdeSize;
10379 }
James Smart4f774512009-05-22 14:52:35 -040010380 } else
James Smart5ffc2662009-11-18 15:39:44 -050010381 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -040010382
10383 iocbq->iocb.ulpIoTag = iocbq->iotag;
10384 cmnd = iocbq->iocb.ulpCommand;
10385
10386 switch (iocbq->iocb.ulpCommand) {
10387 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -040010388 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
10389 ndlp = iocbq->context_un.ndlp;
10390 else
10391 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -040010392 if (!iocbq->iocb.ulpLe) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010393 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040010394 "2007 Only Limited Edition cmd Format"
10395 " supported 0x%x\n",
10396 iocbq->iocb.ulpCommand);
10397 return IOCB_ERROR;
10398 }
James Smartff78d8f2011-12-13 13:21:35 -050010399
James Smart5ffc2662009-11-18 15:39:44 -050010400 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -040010401 /* Els_reguest64 has a TMO */
10402 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
10403 iocbq->iocb.ulpTimeout);
10404 /* Need a VF for word 4 set the vf bit*/
10405 bf_set(els_req64_vf, &wqe->els_req, 0);
10406 /* And a VFID for word 12 */
10407 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -040010408 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -040010409 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
10410 iocbq->iocb.ulpContext);
10411 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
10412 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -040010413 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -050010414 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -050010415 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
10416 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -050010417 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
10418 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -050010419 if_type = bf_get(lpfc_sli_intf_if_type,
10420 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -080010421 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -050010422 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -050010423 *pcmd == ELS_CMD_SCR ||
James Smartdf3fe762020-02-10 09:31:55 -080010424 *pcmd == ELS_CMD_RDF ||
James Smart9064aeb2021-08-16 09:28:50 -070010425 *pcmd == ELS_CMD_EDC ||
James Smartf60cb932019-05-14 14:58:05 -070010426 *pcmd == ELS_CMD_RSCN_XMT ||
James Smart6b5151f2012-01-18 16:24:06 -050010427 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -050010428 *pcmd == ELS_CMD_LOGO ||
Gaurav Srivastavaf56e86a2021-06-08 10:05:53 +053010429 *pcmd == ELS_CMD_QFPA ||
10430 *pcmd == ELS_CMD_UVEM ||
James Smartff78d8f2011-12-13 13:21:35 -050010431 *pcmd == ELS_CMD_PLOGI)) {
10432 bf_set(els_req64_sp, &wqe->els_req, 1);
10433 bf_set(els_req64_sid, &wqe->els_req,
10434 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -040010435 if ((*pcmd == ELS_CMD_FLOGI) &&
10436 !(phba->fc_topology ==
10437 LPFC_TOPOLOGY_LOOP))
10438 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -050010439 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
10440 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -040010441 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -050010442 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -050010443 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
10444 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
10445 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
10446 }
James Smartc8685952009-11-18 15:39:16 -050010447 }
James Smart6d368e52011-05-24 11:44:12 -040010448 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
10449 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -040010450 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
10451 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
10452 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
10453 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
10454 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
10455 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -040010456 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -040010457 break;
James Smart5ffc2662009-11-18 15:39:44 -050010458 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -040010459 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
10460 iocbq->iocb.un.ulpWord[3]);
10461 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -040010462 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050010463 /* The entire sequence is transmitted for this IOCB */
10464 xmit_len = total_len;
10465 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -050010466 if (phba->link_flag & LS_LOOPBACK_MODE)
10467 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050010468 fallthrough;
James Smart4f774512009-05-22 14:52:35 -040010469 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -040010470 /* word3 iocb=io_tag32 wqe=reserved */
10471 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -040010472 /* word4 relative_offset memcpy */
10473 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -040010474 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
10475 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
10476 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
10477 LPFC_WQE_IOD_WRITE);
10478 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
10479 LPFC_WQE_LENLOC_WORD12);
10480 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -050010481 wqe->xmit_sequence.xmit_len = xmit_len;
10482 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -040010483 break;
James Smart4f774512009-05-22 14:52:35 -040010484 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -040010485 /* word3 iocb=iotag32 wqe=seq_payload_len */
10486 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -040010487 /* word4 iocb=rsvd wqe=rsvd */
10488 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
10489 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -040010490 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -040010491 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -040010492 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
10493 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
10494 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
10495 LPFC_WQE_LENLOC_WORD3);
10496 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -040010497 break;
James Smart4f774512009-05-22 14:52:35 -040010498 case CMD_FCP_IWRITE64_CR:
10499 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -040010500 /* word3 iocb=iotag wqe=payload_offset_len */
10501 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -040010502 bf_set(payload_offset_len, &wqe->fcp_iwrite,
10503 xmit_len + sizeof(struct fcp_rsp));
10504 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
10505 0);
James Smartf0d9bcc2010-10-22 11:07:09 -040010506 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
10507 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
10508 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
10509 iocbq->iocb.ulpFCP2Rcvy);
10510 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
10511 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -040010512 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
10513 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
10514 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -040010515 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -050010516 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -050010517 if (iocbq->iocb_flag & LPFC_IO_OAS) {
10518 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -070010519 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
10520 if (iocbq->priority) {
10521 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
10522 (iocbq->priority << 1));
10523 } else {
James Smart1ba981f2014-02-20 09:56:45 -050010524 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
10525 (phba->cfg_XLanePriority << 1));
10526 }
10527 }
James Smartb5c53952016-03-31 14:12:30 -070010528 /* Note, word 10 is already initialized to 0 */
10529
James Smart414abe02018-06-26 08:24:26 -070010530 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
10531 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -080010532 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
10533 else
10534 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
10535
James Smartb5c53952016-03-31 14:12:30 -070010536 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -080010537 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -070010538 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -070010539 struct fcp_cmnd *fcp_cmnd;
10540 uint32_t *ptr;
10541
10542 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -070010543
10544 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -080010545 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -070010546 fcp_cmnd = lpfc_cmd->fcp_cmnd;
10547
10548 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -080010549 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -070010550 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -080010551 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10552 wqe->generic.bde.addrHigh = 0;
10553 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -070010554
James Smart205e8242018-03-05 12:04:03 -080010555 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10556 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -070010557
10558 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -080010559 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -070010560 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10561 }
James Smart7851fe22011-07-22 18:36:52 -040010562 break;
James Smartf0d9bcc2010-10-22 11:07:09 -040010563 case CMD_FCP_IREAD64_CR:
10564 /* word3 iocb=iotag wqe=payload_offset_len */
10565 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -040010566 bf_set(payload_offset_len, &wqe->fcp_iread,
10567 xmit_len + sizeof(struct fcp_rsp));
10568 bf_set(cmd_buff_len, &wqe->fcp_iread,
10569 0);
James Smartf0d9bcc2010-10-22 11:07:09 -040010570 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
10571 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
10572 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
10573 iocbq->iocb.ulpFCP2Rcvy);
10574 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -040010575 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -040010576 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
10577 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
10578 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -040010579 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -050010580 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -050010581 if (iocbq->iocb_flag & LPFC_IO_OAS) {
10582 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -070010583 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
10584 if (iocbq->priority) {
10585 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
10586 (iocbq->priority << 1));
10587 } else {
James Smart1ba981f2014-02-20 09:56:45 -050010588 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
10589 (phba->cfg_XLanePriority << 1));
10590 }
10591 }
James Smartb5c53952016-03-31 14:12:30 -070010592 /* Note, word 10 is already initialized to 0 */
10593
James Smart414abe02018-06-26 08:24:26 -070010594 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
10595 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -080010596 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
10597 else
10598 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
10599
James Smartb5c53952016-03-31 14:12:30 -070010600 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -080010601 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -070010602 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -070010603 struct fcp_cmnd *fcp_cmnd;
10604 uint32_t *ptr;
10605
10606 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -070010607
10608 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -080010609 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -070010610 fcp_cmnd = lpfc_cmd->fcp_cmnd;
10611
10612 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -080010613 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -070010614 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -080010615 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10616 wqe->generic.bde.addrHigh = 0;
10617 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -070010618
James Smart205e8242018-03-05 12:04:03 -080010619 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
10620 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -070010621
10622 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -080010623 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -070010624 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10625 }
James Smart7851fe22011-07-22 18:36:52 -040010626 break;
James Smartf1126682009-06-10 17:22:44 -040010627 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -040010628 /* word3 iocb=iotag wqe=payload_offset_len */
10629 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
10630 bf_set(payload_offset_len, &wqe->fcp_icmd,
10631 xmit_len + sizeof(struct fcp_rsp));
10632 bf_set(cmd_buff_len, &wqe->fcp_icmd,
10633 0);
James Smartf0d9bcc2010-10-22 11:07:09 -040010634 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -040010635 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -040010636 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -040010637 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
10638 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
10639 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
10640 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
10641 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -040010642 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
10643 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -050010644 if (iocbq->iocb_flag & LPFC_IO_OAS) {
10645 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -070010646 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
10647 if (iocbq->priority) {
10648 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
10649 (iocbq->priority << 1));
10650 } else {
James Smart1ba981f2014-02-20 09:56:45 -050010651 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
10652 (phba->cfg_XLanePriority << 1));
10653 }
10654 }
James Smartb5c53952016-03-31 14:12:30 -070010655 /* Note, word 10 is already initialized to 0 */
10656
10657 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -080010658 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -070010659 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -070010660 struct fcp_cmnd *fcp_cmnd;
10661 uint32_t *ptr;
10662
10663 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -070010664
10665 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -080010666 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -070010667 fcp_cmnd = lpfc_cmd->fcp_cmnd;
10668
10669 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -080010670 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -070010671 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -080010672 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10673 wqe->generic.bde.addrHigh = 0;
10674 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -070010675
James Smart205e8242018-03-05 12:04:03 -080010676 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
10677 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -070010678
10679 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -080010680 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -070010681 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10682 }
James Smart7851fe22011-07-22 18:36:52 -040010683 break;
James Smart4f774512009-05-22 14:52:35 -040010684 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -050010685 /* For this command calculate the xmit length of the
10686 * request bde.
10687 */
10688 xmit_len = 0;
10689 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
10690 sizeof(struct ulp_bde64);
10691 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -050010692 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -050010693 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
10694 break;
James Smart63e801c2010-11-20 23:14:19 -050010695 xmit_len += bde.tus.f.bdeSize;
10696 }
James Smartf0d9bcc2010-10-22 11:07:09 -040010697 /* word3 iocb=IO_TAG wqe=request_payload_len */
10698 wqe->gen_req.request_payload_len = xmit_len;
10699 /* word4 iocb=parameter wqe=relative_offset memcpy */
10700 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -040010701 /* word6 context tag copied in memcpy */
10702 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
10703 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
Dick Kennedy372c1872020-06-30 14:50:00 -070010704 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040010705 "2015 Invalid CT %x command 0x%x\n",
10706 ct, iocbq->iocb.ulpCommand);
10707 return IOCB_ERROR;
10708 }
James Smartf0d9bcc2010-10-22 11:07:09 -040010709 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
10710 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
10711 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
10712 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
10713 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
10714 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
10715 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
10716 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -040010717 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -040010718 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -040010719 break;
James Smart4f774512009-05-22 14:52:35 -040010720 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -040010721 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -040010722 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -040010723 /* word3 iocb=iotag32 wqe=response_payload_len */
10724 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -040010725 /* word4 */
10726 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -040010727 /* word5 iocb=rsvd wge=did */
10728 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -040010729 iocbq->iocb.un.xseq64.xmit_els_remoteID);
10730
10731 if_type = bf_get(lpfc_sli_intf_if_type,
10732 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -080010733 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -040010734 if (iocbq->vport->fc_flag & FC_PT2PT) {
10735 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
10736 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
10737 iocbq->vport->fc_myDID);
10738 if (iocbq->vport->fc_myDID == Fabric_DID) {
10739 bf_set(wqe_els_did,
10740 &wqe->xmit_els_rsp.wqe_dest, 0);
10741 }
10742 }
10743 }
James Smartf0d9bcc2010-10-22 11:07:09 -040010744 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
10745 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
10746 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
10747 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -040010748 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -040010749 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -040010750 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -040010751 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -040010752 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
10753 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
10754 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
10755 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
10756 LPFC_WQE_LENLOC_WORD3);
10757 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -040010758 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
10759 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -050010760 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -040010761 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
10762 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -050010763 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -040010764 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
10765 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -050010766 phba->vpi_ids[phba->pport->vpi]);
10767 }
James Smart4f774512009-05-22 14:52:35 -040010768 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -040010769 break;
James Smart4f774512009-05-22 14:52:35 -040010770 case CMD_CLOSE_XRI_CN:
10771 case CMD_ABORT_XRI_CN:
10772 case CMD_ABORT_XRI_CX:
10773 /* words 0-2 memcpy should be 0 rserved */
10774 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -040010775 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
10776 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
10777 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
10778 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
10779 } else
10780 fip = 0;
10781
10782 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -040010783 /*
James Smartdcf2a4e2010-09-29 11:18:53 -040010784 * The link is down, or the command was ELS_FIP
10785 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -040010786 * on the wire.
10787 */
10788 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
10789 else
10790 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
10791 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -040010792 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
10793 wqe->abort_cmd.rsrvd5 = 0;
10794 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -040010795 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
10796 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -040010797 /*
10798 * The abort handler will send us CMD_ABORT_XRI_CN or
10799 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
10800 */
James Smartf0d9bcc2010-10-22 11:07:09 -040010801 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10802 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
10803 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
10804 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -040010805 cmnd = CMD_ABORT_XRI_CX;
10806 command_type = OTHER_COMMAND;
10807 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -040010808 break;
James Smart6669f9b2009-10-02 15:16:45 -040010809 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -050010810 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -050010811 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -040010812 * we re-construct this WQE here based on information in
10813 * iocbq from scratch.
10814 */
James Smartd9f492a2019-08-14 16:57:04 -070010815 memset(wqe, 0, sizeof(*wqe));
James Smart5ffc2662009-11-18 15:39:44 -050010816 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -040010817 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -050010818 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
10819 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -050010820 LPFC_ABTS_UNSOL_INT) {
10821 /* ABTS sent by initiator to CT exchange, the
10822 * RX_ID field will be filled with the newly
10823 * allocated responder XRI.
10824 */
10825 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10826 iocbq->sli4_xritag);
10827 } else {
10828 /* ABTS sent by responder to CT exchange, the
10829 * RX_ID field will be filled with the responder
10830 * RX_ID from ABTS.
10831 */
10832 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -050010833 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -050010834 }
James Smart6669f9b2009-10-02 15:16:45 -040010835 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
10836 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -050010837
10838 /* Use CT=VPI */
10839 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
10840 ndlp->nlp_DID);
10841 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
10842 iocbq->iocb.ulpContext);
10843 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -040010844 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -050010845 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -040010846 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
10847 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
10848 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -040010849 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
10850 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -050010851 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
10852 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
10853 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
10854 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
10855 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
10856 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
10857 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
10858 }
10859
James Smart7851fe22011-07-22 18:36:52 -040010860 break;
James Smartae9e28f2017-05-15 15:20:51 -070010861 case CMD_SEND_FRAME:
James Smarte62245d2019-08-14 16:57:08 -070010862 bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_SEND_FRAME);
10863 bf_set(wqe_sof, &wqe->generic.wqe_com, 0x2E); /* SOF byte */
10864 bf_set(wqe_eof, &wqe->generic.wqe_com, 0x41); /* EOF byte */
10865 bf_set(wqe_lenloc, &wqe->generic.wqe_com, 1);
10866 bf_set(wqe_xbl, &wqe->generic.wqe_com, 1);
10867 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10868 bf_set(wqe_xc, &wqe->generic.wqe_com, 1);
10869 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, 0xA);
10870 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smartae9e28f2017-05-15 15:20:51 -070010871 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10872 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10873 return 0;
James Smart4f774512009-05-22 14:52:35 -040010874 case CMD_XRI_ABORTED_CX:
10875 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -040010876 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
10877 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
10878 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
10879 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
10880 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070010881 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040010882 "2014 Invalid command 0x%x\n",
10883 iocbq->iocb.ulpCommand);
10884 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -040010885 }
James Smart6d368e52011-05-24 11:44:12 -040010886
James Smart8012cc32012-10-31 14:44:49 -040010887 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
10888 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
10889 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
10890 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
10891 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
10892 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
10893 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
10894 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -040010895 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10896 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10897 wqe->generic.wqe_com.abort_tag = abort_tag;
10898 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
10899 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
10900 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
10901 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -040010902 return 0;
10903}
10904
10905/**
James Smart47ff4c52020-11-15 11:26:41 -080010906 * __lpfc_sli_issue_fcp_io_s3 - SLI3 device for sending fcp io iocb
10907 * @phba: Pointer to HBA context object.
10908 * @ring_number: SLI ring number to issue wqe on.
10909 * @piocb: Pointer to command iocb.
10910 * @flag: Flag indicating if this command can be put into txq.
10911 *
10912 * __lpfc_sli_issue_fcp_io_s3 is wrapper function to invoke lockless func to
10913 * send an iocb command to an HBA with SLI-4 interface spec.
10914 *
10915 * This function takes the hbalock before invoking the lockless version.
10916 * The function will return success after it successfully submit the wqe to
10917 * firmware or after adding to the txq.
10918 **/
10919static int
10920__lpfc_sli_issue_fcp_io_s3(struct lpfc_hba *phba, uint32_t ring_number,
10921 struct lpfc_iocbq *piocb, uint32_t flag)
10922{
10923 unsigned long iflags;
10924 int rc;
10925
10926 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartda255e22020-11-15 11:26:42 -080010927 rc = __lpfc_sli_issue_iocb_s3(phba, ring_number, piocb, flag);
James Smart47ff4c52020-11-15 11:26:41 -080010928 spin_unlock_irqrestore(&phba->hbalock, iflags);
10929
10930 return rc;
10931}
10932
10933/**
10934 * __lpfc_sli_issue_fcp_io_s4 - SLI4 device for sending fcp io wqe
10935 * @phba: Pointer to HBA context object.
10936 * @ring_number: SLI ring number to issue wqe on.
10937 * @piocb: Pointer to command iocb.
10938 * @flag: Flag indicating if this command can be put into txq.
10939 *
10940 * __lpfc_sli_issue_fcp_io_s4 is used by other functions in the driver to issue
10941 * an wqe command to an HBA with SLI-4 interface spec.
10942 *
10943 * This function is a lockless version. The function will return success
10944 * after it successfully submit the wqe to firmware or after adding to the
10945 * txq.
10946 **/
10947static int
10948__lpfc_sli_issue_fcp_io_s4(struct lpfc_hba *phba, uint32_t ring_number,
10949 struct lpfc_iocbq *piocb, uint32_t flag)
10950{
James Smart47ff4c52020-11-15 11:26:41 -080010951 int rc;
James Smartda255e22020-11-15 11:26:42 -080010952 struct lpfc_io_buf *lpfc_cmd =
10953 (struct lpfc_io_buf *)piocb->context1;
10954 union lpfc_wqe128 *wqe = &piocb->wqe;
10955 struct sli4_sge *sgl;
James Smart47ff4c52020-11-15 11:26:41 -080010956
James Smartda255e22020-11-15 11:26:42 -080010957 /* 128 byte wqe support here */
10958 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smart47ff4c52020-11-15 11:26:41 -080010959
James Smartda255e22020-11-15 11:26:42 -080010960 if (phba->fcp_embed_io) {
10961 struct fcp_cmnd *fcp_cmnd;
10962 u32 *ptr;
James Smart47ff4c52020-11-15 11:26:41 -080010963
James Smartda255e22020-11-15 11:26:42 -080010964 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart47ff4c52020-11-15 11:26:41 -080010965
James Smartda255e22020-11-15 11:26:42 -080010966 /* Word 0-2 - FCP_CMND */
10967 wqe->generic.bde.tus.f.bdeFlags =
10968 BUFF_TYPE_BDE_IMMED;
10969 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10970 wqe->generic.bde.addrHigh = 0;
10971 wqe->generic.bde.addrLow = 88; /* Word 22 */
10972
10973 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10974 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
10975
10976 /* Word 22-29 FCP CMND Payload */
10977 ptr = &wqe->words[22];
10978 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10979 } else {
10980 /* Word 0-2 - Inline BDE */
10981 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
10982 wqe->generic.bde.tus.f.bdeSize = sizeof(struct fcp_cmnd);
10983 wqe->generic.bde.addrHigh = sgl->addr_hi;
10984 wqe->generic.bde.addrLow = sgl->addr_lo;
10985
10986 /* Word 10 */
10987 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10988 bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
10989 }
10990
Gaurav Srivastavaf56e86a2021-06-08 10:05:53 +053010991 /* add the VMID tags as per switch response */
10992 if (unlikely(piocb->iocb_flag & LPFC_IO_VMID)) {
10993 if (phba->pport->vmid_priority_tagging) {
10994 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
10995 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
10996 (piocb->vmid_tag.cs_ctl_vmid));
10997 } else {
10998 bf_set(wqe_appid, &wqe->fcp_iwrite.wqe_com, 1);
10999 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
11000 wqe->words[31] = piocb->vmid_tag.app_id;
11001 }
11002 }
James Smartda255e22020-11-15 11:26:42 -080011003 rc = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, piocb);
James Smart47ff4c52020-11-15 11:26:41 -080011004 return rc;
11005}
11006
11007/**
James Smart4f774512009-05-22 14:52:35 -040011008 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
11009 * @phba: Pointer to HBA context object.
11010 * @ring_number: SLI ring number to issue iocb on.
11011 * @piocb: Pointer to command iocb.
11012 * @flag: Flag indicating if this command can be put into txq.
11013 *
11014 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
11015 * an iocb command to an HBA with SLI-4 interface spec.
11016 *
James Smart27f3efd2019-10-18 14:18:19 -070011017 * This function is called with ringlock held. The function will return success
James Smart4f774512009-05-22 14:52:35 -040011018 * after it successfully submit the iocb to firmware or after adding to the
11019 * txq.
11020 **/
11021static int
11022__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
11023 struct lpfc_iocbq *piocb, uint32_t flag)
11024{
11025 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080011026 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -050011027 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -080011028 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -040011029
James Smart895427b2017-02-12 13:52:30 -080011030 /* Get the WQ */
11031 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
11032 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc00f62e2019-08-14 16:57:11 -070011033 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
James Smart895427b2017-02-12 13:52:30 -080011034 } else {
11035 wq = phba->sli4_hba.els_wq;
11036 }
11037
11038 /* Get corresponding ring */
11039 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011040
James Smartb5c53952016-03-31 14:12:30 -070011041 /*
11042 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -070011043 */
James Smartb5c53952016-03-31 14:12:30 -070011044
James Smartcda7fa182019-03-04 15:15:43 -080011045 lockdep_assert_held(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011046
James Smart4f774512009-05-22 14:52:35 -040011047 if (piocb->sli4_xritag == NO_XRI) {
11048 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -050011049 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -040011050 sglq = NULL;
11051 else {
James Smart0e9bb8d2013-03-01 16:35:12 -050011052 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -040011053 if (!(flag & SLI_IOCB_RET_IOCB)) {
11054 __lpfc_sli_ringtx_put(phba,
11055 pring, piocb);
11056 return IOCB_SUCCESS;
11057 } else {
11058 return IOCB_BUSY;
11059 }
11060 } else {
James Smart895427b2017-02-12 13:52:30 -080011061 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -040011062 if (!sglq) {
11063 if (!(flag & SLI_IOCB_RET_IOCB)) {
11064 __lpfc_sli_ringtx_put(phba,
11065 pring,
11066 piocb);
11067 return IOCB_SUCCESS;
11068 } else
11069 return IOCB_BUSY;
11070 }
11071 }
James Smart4f774512009-05-22 14:52:35 -040011072 }
James Smartda255e22020-11-15 11:26:42 -080011073 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -040011074 /* These IO's already have an XRI and a mapped sgl. */
11075 sglq = NULL;
James Smartda255e22020-11-15 11:26:42 -080011076 }
James Smart2ea259e2017-02-12 13:52:27 -080011077 else {
James Smart6d368e52011-05-24 11:44:12 -040011078 /*
11079 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -040011080 * sglq is on the active list
11081 */
James Smartedccdc12013-01-03 15:43:45 -050011082 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -040011083 if (!sglq)
11084 return IOCB_ERROR;
11085 }
11086
11087 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -040011088 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040011089 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040011090 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -040011091 return IOCB_ERROR;
11092 }
11093
James Smart205e8242018-03-05 12:04:03 -080011094 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -040011095 return IOCB_ERROR;
11096
James Smart205e8242018-03-05 12:04:03 -080011097 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -080011098 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -040011099 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
11100
11101 return 0;
11102}
11103
Lee Jones8514e2f2021-03-03 14:46:18 +000011104/*
James Smart47ff4c52020-11-15 11:26:41 -080011105 * lpfc_sli_issue_fcp_io - Wrapper func for issuing fcp i/o
11106 *
11107 * This routine wraps the actual fcp i/o function for issusing WQE for sli-4
11108 * or IOCB for sli-3 function.
11109 * pointer from the lpfc_hba struct.
11110 *
11111 * Return codes:
11112 * IOCB_ERROR - Error
11113 * IOCB_SUCCESS - Success
11114 * IOCB_BUSY - Busy
11115 **/
11116int
11117lpfc_sli_issue_fcp_io(struct lpfc_hba *phba, uint32_t ring_number,
11118 struct lpfc_iocbq *piocb, uint32_t flag)
11119{
11120 return phba->__lpfc_sli_issue_fcp_io(phba, ring_number, piocb, flag);
11121}
11122
Lee Jones7af29d42020-07-21 17:41:31 +010011123/*
James Smart3772a992009-05-22 14:50:54 -040011124 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
11125 *
11126 * This routine wraps the actual lockless version for issusing IOCB function
11127 * pointer from the lpfc_hba struct.
11128 *
11129 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -070011130 * IOCB_ERROR - Error
11131 * IOCB_SUCCESS - Success
11132 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -040011133 **/
James Smart2a9bf3d2010-06-07 15:24:45 -040011134int
James Smart3772a992009-05-22 14:50:54 -040011135__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
11136 struct lpfc_iocbq *piocb, uint32_t flag)
11137{
11138 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11139}
11140
11141/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011142 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -040011143 * @phba: The hba struct for which this call is being executed.
11144 * @dev_grp: The HBA PCI-Device group number.
11145 *
11146 * This routine sets up the SLI interface API function jump table in @phba
11147 * struct.
11148 * Returns: 0 - success, -ENODEV - failure.
11149 **/
11150int
11151lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
11152{
11153
11154 switch (dev_grp) {
11155 case LPFC_PCI_DEV_LP:
11156 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
11157 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
James Smart47ff4c52020-11-15 11:26:41 -080011158 phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s3;
James Smart3772a992009-05-22 14:50:54 -040011159 break;
James Smart4f774512009-05-22 14:52:35 -040011160 case LPFC_PCI_DEV_OC:
11161 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
11162 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
James Smart47ff4c52020-11-15 11:26:41 -080011163 phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s4;
James Smart4f774512009-05-22 14:52:35 -040011164 break;
James Smart3772a992009-05-22 14:50:54 -040011165 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070011166 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040011167 "1419 Invalid HBA PCI-device group: 0x%x\n",
11168 dev_grp);
11169 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -040011170 }
11171 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
11172 return 0;
11173}
James Smart92d7f7b2007-06-17 19:56:38 -050011174
James Smarta1efe162015-05-21 13:55:20 -040011175/**
James Smart895427b2017-02-12 13:52:30 -080011176 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -040011177 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -040011178 * @piocb: Pointer to command iocb.
11179 *
James Smart895427b2017-02-12 13:52:30 -080011180 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
11181 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -040011182 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -080011183 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -040011184 */
James Smart895427b2017-02-12 13:52:30 -080011185struct lpfc_sli_ring *
11186lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -040011187{
James Smartc4908502019-01-28 11:14:28 -080011188 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -080011189
James Smart895427b2017-02-12 13:52:30 -080011190 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -080011191 if (unlikely(!phba->sli4_hba.hdwq))
James Smart7370d102019-01-28 11:14:20 -080011192 return NULL;
11193 /*
11194 * for abort iocb hba_wqidx should already
11195 * be setup based on what work queue we used.
11196 */
11197 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc4908502019-01-28 11:14:28 -080011198 lpfc_cmd = (struct lpfc_io_buf *)piocb->context1;
James Smart1fbf9742019-01-28 11:14:26 -080011199 piocb->hba_wqidx = lpfc_cmd->hdwq_no;
James Smart9bd2bff52014-09-03 12:57:30 -040011200 }
James Smartc00f62e2019-08-14 16:57:11 -070011201 return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080011202 } else {
11203 if (unlikely(!phba->sli4_hba.els_wq))
11204 return NULL;
11205 piocb->hba_wqidx = 0;
11206 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -040011207 }
James Smart9bd2bff52014-09-03 12:57:30 -040011208}
11209
James Smarte59058c2008-08-24 21:49:00 -040011210/**
James Smart3621a712009-04-06 18:47:14 -040011211 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -040011212 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010011213 * @ring_number: Ring number
James Smarte59058c2008-08-24 21:49:00 -040011214 * @piocb: Pointer to command iocb.
11215 * @flag: Flag indicating if this command can be put into txq.
11216 *
11217 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
11218 * function. This function gets the hbalock and calls
11219 * __lpfc_sli_issue_iocb function and will return the error returned
11220 * by __lpfc_sli_issue_iocb function. This wrapper is used by
11221 * functions which do not hold hbalock.
11222 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011223int
James Smart3772a992009-05-22 14:50:54 -040011224lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -050011225 struct lpfc_iocbq *piocb, uint32_t flag)
11226{
James Smart2a76a282012-08-03 12:35:54 -040011227 struct lpfc_sli_ring *pring;
James Smart93a4d6f2019-11-04 16:57:05 -080011228 struct lpfc_queue *eq;
James Smart92d7f7b2007-06-17 19:56:38 -050011229 unsigned long iflags;
James Smart6a828b02019-01-28 11:14:31 -080011230 int rc;
James Smart92d7f7b2007-06-17 19:56:38 -050011231
James Smart7e56aa22012-08-03 12:35:34 -040011232 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart93a4d6f2019-11-04 16:57:05 -080011233 eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
11234
James Smart895427b2017-02-12 13:52:30 -080011235 pring = lpfc_sli4_calc_ring(phba, piocb);
11236 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040011237 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -040011238
James Smart9bd2bff52014-09-03 12:57:30 -040011239 spin_lock_irqsave(&pring->ring_lock, iflags);
11240 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11241 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080011242
11243 lpfc_sli4_poll_eq(eq, LPFC_POLL_FASTPATH);
James Smart7e56aa22012-08-03 12:35:34 -040011244 } else {
11245 /* For now, SLI2/3 will still use hbalock */
11246 spin_lock_irqsave(&phba->hbalock, iflags);
11247 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11248 spin_unlock_irqrestore(&phba->hbalock, iflags);
11249 }
James Smart92d7f7b2007-06-17 19:56:38 -050011250 return rc;
11251}
11252
James Smarte59058c2008-08-24 21:49:00 -040011253/**
James Smart3621a712009-04-06 18:47:14 -040011254 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -040011255 * @phba: Pointer to HBA context object.
11256 *
11257 * This function is called while driver attaches with the
11258 * HBA to setup the extra ring. The extra ring is used
11259 * only when driver needs to support target mode functionality
11260 * or IP over FC functionalities.
11261 *
James Smart895427b2017-02-12 13:52:30 -080011262 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -040011263 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050011264static int
11265lpfc_extra_ring_setup( struct lpfc_hba *phba)
11266{
11267 struct lpfc_sli *psli;
11268 struct lpfc_sli_ring *pring;
11269
11270 psli = &phba->sli;
11271
11272 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -050011273
11274 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -080011275 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -040011276 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11277 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11278 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11279 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050011280
James Smarta4bc3372006-12-02 13:34:16 -050011281 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -080011282 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -050011283
James Smart7e56aa22012-08-03 12:35:34 -040011284 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11285 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11286 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11287 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050011288
11289 /* Setup default profile for this ring */
11290 pring->iotag_max = 4096;
11291 pring->num_mask = 1;
11292 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -050011293 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
11294 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050011295 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
11296 return 0;
11297}
11298
James Smarte7dab162020-10-20 13:27:12 -070011299static void
11300lpfc_sli_post_recovery_event(struct lpfc_hba *phba,
11301 struct lpfc_nodelist *ndlp)
11302{
11303 unsigned long iflags;
11304 struct lpfc_work_evt *evtp = &ndlp->recovery_evt;
11305
11306 spin_lock_irqsave(&phba->hbalock, iflags);
11307 if (!list_empty(&evtp->evt_listp)) {
11308 spin_unlock_irqrestore(&phba->hbalock, iflags);
11309 return;
11310 }
11311
11312 /* Incrementing the reference count until the queued work is done. */
11313 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
11314 if (!evtp->evt_arg1) {
11315 spin_unlock_irqrestore(&phba->hbalock, iflags);
11316 return;
11317 }
11318 evtp->evt = LPFC_EVT_RECOVER_PORT;
11319 list_add_tail(&evtp->evt_listp, &phba->work_list);
11320 spin_unlock_irqrestore(&phba->hbalock, iflags);
11321
11322 lpfc_worker_wake_up(phba);
11323}
11324
James Smartcb69f7d2011-12-13 13:21:57 -050011325/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
11326 * @phba: Pointer to HBA context object.
11327 * @iocbq: Pointer to iocb object.
11328 *
11329 * The async_event handler calls this routine when it receives
11330 * an ASYNC_STATUS_CN event from the port. The port generates
11331 * this event when an Abort Sequence request to an rport fails
11332 * twice in succession. The abort could be originated by the
11333 * driver or by the port. The ABTS could have been for an ELS
11334 * or FCP IO. The port only generates this event when an ABTS
11335 * fails to complete after one retry.
11336 */
11337static void
11338lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
11339 struct lpfc_iocbq *iocbq)
11340{
11341 struct lpfc_nodelist *ndlp = NULL;
11342 uint16_t rpi = 0, vpi = 0;
11343 struct lpfc_vport *vport = NULL;
11344
11345 /* The rpi in the ulpContext is vport-sensitive. */
11346 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
11347 rpi = iocbq->iocb.ulpContext;
11348
11349 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11350 "3092 Port generated ABTS async event "
11351 "on vpi %d rpi %d status 0x%x\n",
11352 vpi, rpi, iocbq->iocb.ulpStatus);
11353
11354 vport = lpfc_find_vport_by_vpid(phba, vpi);
11355 if (!vport)
11356 goto err_exit;
11357 ndlp = lpfc_findnode_rpi(vport, rpi);
James Smart307e3382020-11-15 11:26:30 -080011358 if (!ndlp)
James Smartcb69f7d2011-12-13 13:21:57 -050011359 goto err_exit;
11360
11361 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
11362 lpfc_sli_abts_recover_port(vport, ndlp);
11363 return;
11364
11365 err_exit:
11366 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11367 "3095 Event Context not found, no "
11368 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
11369 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
11370 vpi, rpi);
11371}
11372
11373/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
11374 * @phba: pointer to HBA context object.
11375 * @ndlp: nodelist pointer for the impacted rport.
11376 * @axri: pointer to the wcqe containing the failed exchange.
11377 *
11378 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
11379 * port. The port generates this event when an abort exchange request to an
11380 * rport fails twice in succession with no reply. The abort could be originated
11381 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
11382 */
11383void
11384lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
11385 struct lpfc_nodelist *ndlp,
11386 struct sli4_wcqe_xri_aborted *axri)
11387{
James Smart5c1db2a2012-03-01 22:34:36 -050011388 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -050011389
James Smart307e3382020-11-15 11:26:30 -080011390 if (!ndlp) {
James Smartcb69f7d2011-12-13 13:21:57 -050011391 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11392 "3115 Node Context not found, driver "
11393 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -050011394 return;
11395 }
11396
James Smartcb69f7d2011-12-13 13:21:57 -050011397 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11398 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -050011399 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -040011400 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -050011401 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -050011402 bf_get(lpfc_wcqe_xa_status, axri),
11403 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -050011404
James Smart5c1db2a2012-03-01 22:34:36 -050011405 /*
11406 * Catch the ABTS protocol failure case. Older OCe FW releases returned
11407 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
11408 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
11409 */
James Smarte3d2b802012-08-14 14:25:43 -040011410 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -050011411 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
11412 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smarte7dab162020-10-20 13:27:12 -070011413 lpfc_sli_post_recovery_event(phba, ndlp);
James Smartcb69f7d2011-12-13 13:21:57 -050011414}
11415
James Smarte59058c2008-08-24 21:49:00 -040011416/**
James Smart3621a712009-04-06 18:47:14 -040011417 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -040011418 * @phba: Pointer to HBA context object.
11419 * @pring: Pointer to driver SLI ring object.
11420 * @iocbq: Pointer to iocb object.
11421 *
11422 * This function is called by the slow ring event handler
11423 * function when there is an ASYNC event iocb in the ring.
11424 * This function is called with no lock held.
11425 * Currently this function handles only temperature related
11426 * ASYNC events. The function decodes the temperature sensor
11427 * event message and posts events for the management applications.
11428 **/
James Smart98c9ea52007-10-27 13:37:33 -040011429static void
James Smart57127f12007-10-27 13:37:05 -040011430lpfc_sli_async_event_handler(struct lpfc_hba * phba,
11431 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
11432{
11433 IOCB_t *icmd;
11434 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -040011435 struct temp_event temp_event_data;
11436 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -040011437 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -040011438
11439 icmd = &iocbq->iocb;
11440 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -040011441
James Smartcb69f7d2011-12-13 13:21:57 -050011442 switch (evt_code) {
11443 case ASYNC_TEMP_WARN:
11444 case ASYNC_TEMP_SAFE:
11445 temp_event_data.data = (uint32_t) icmd->ulpContext;
11446 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
11447 if (evt_code == ASYNC_TEMP_WARN) {
11448 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
Dick Kennedy372c1872020-06-30 14:50:00 -070011449 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcb69f7d2011-12-13 13:21:57 -050011450 "0347 Adapter is very hot, please take "
11451 "corrective action. temperature : %d Celsius\n",
11452 (uint32_t) icmd->ulpContext);
11453 } else {
11454 temp_event_data.event_code = LPFC_NORMAL_TEMP;
Dick Kennedy372c1872020-06-30 14:50:00 -070011455 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcb69f7d2011-12-13 13:21:57 -050011456 "0340 Adapter temperature is OK now. "
11457 "temperature : %d Celsius\n",
11458 (uint32_t) icmd->ulpContext);
11459 }
11460
11461 /* Send temperature change event to applications */
11462 shost = lpfc_shost_from_vport(phba->pport);
11463 fc_host_post_vendor_event(shost, fc_get_event_number(),
11464 sizeof(temp_event_data), (char *) &temp_event_data,
11465 LPFC_NL_VENDOR_ID);
11466 break;
11467 case ASYNC_STATUS_CN:
11468 lpfc_sli_abts_err_handler(phba, iocbq);
11469 break;
11470 default:
James Smarta257bf92009-04-06 18:48:10 -040011471 iocb_w = (uint32_t *) icmd;
Dick Kennedy372c1872020-06-30 14:50:00 -070011472 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart76bb24e2007-10-27 13:38:00 -040011473 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -040011474 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -040011475 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
11476 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
11477 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
11478 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -050011479 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -040011480 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
11481 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
11482 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
11483 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
11484
James Smartcb69f7d2011-12-13 13:21:57 -050011485 break;
James Smart57127f12007-10-27 13:37:05 -040011486 }
James Smart57127f12007-10-27 13:37:05 -040011487}
11488
11489
James Smarte59058c2008-08-24 21:49:00 -040011490/**
James Smart895427b2017-02-12 13:52:30 -080011491 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -040011492 * @phba: Pointer to HBA context object.
11493 *
11494 * lpfc_sli_setup sets up rings of the SLI interface with
11495 * number of iocbs per ring and iotags. This function is
11496 * called while driver attach to the HBA and before the
11497 * interrupts are enabled. So there is no need for locking.
11498 *
11499 * This function always returns 0.
11500 **/
dea31012005-04-17 16:05:31 -050011501int
James Smart895427b2017-02-12 13:52:30 -080011502lpfc_sli4_setup(struct lpfc_hba *phba)
11503{
11504 struct lpfc_sli_ring *pring;
11505
11506 pring = phba->sli4_hba.els_wq->pring;
11507 pring->num_mask = LPFC_MAX_RING_MASK;
11508 pring->prt[0].profile = 0; /* Mask 0 */
11509 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11510 pring->prt[0].type = FC_TYPE_ELS;
11511 pring->prt[0].lpfc_sli_rcv_unsol_event =
11512 lpfc_els_unsol_event;
11513 pring->prt[1].profile = 0; /* Mask 1 */
11514 pring->prt[1].rctl = FC_RCTL_ELS_REP;
11515 pring->prt[1].type = FC_TYPE_ELS;
11516 pring->prt[1].lpfc_sli_rcv_unsol_event =
11517 lpfc_els_unsol_event;
11518 pring->prt[2].profile = 0; /* Mask 2 */
11519 /* NameServer Inquiry */
11520 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11521 /* NameServer */
11522 pring->prt[2].type = FC_TYPE_CT;
11523 pring->prt[2].lpfc_sli_rcv_unsol_event =
11524 lpfc_ct_unsol_event;
11525 pring->prt[3].profile = 0; /* Mask 3 */
11526 /* NameServer response */
11527 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11528 /* NameServer */
11529 pring->prt[3].type = FC_TYPE_CT;
11530 pring->prt[3].lpfc_sli_rcv_unsol_event =
11531 lpfc_ct_unsol_event;
11532 return 0;
11533}
11534
11535/**
11536 * lpfc_sli_setup - SLI ring setup function
11537 * @phba: Pointer to HBA context object.
11538 *
11539 * lpfc_sli_setup sets up rings of the SLI interface with
11540 * number of iocbs per ring and iotags. This function is
11541 * called while driver attach to the HBA and before the
11542 * interrupts are enabled. So there is no need for locking.
11543 *
11544 * This function always returns 0. SLI3 only.
11545 **/
11546int
dea31012005-04-17 16:05:31 -050011547lpfc_sli_setup(struct lpfc_hba *phba)
11548{
James Smarted957682007-06-17 19:56:37 -050011549 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050011550 struct lpfc_sli *psli = &phba->sli;
11551 struct lpfc_sli_ring *pring;
11552
James Smart2a76a282012-08-03 12:35:54 -040011553 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050011554 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050011555
James Bottomley604a3e32005-10-29 10:28:33 -050011556 psli->iocbq_lookup = NULL;
11557 psli->iocbq_lookup_len = 0;
11558 psli->last_iotag = 0;
11559
dea31012005-04-17 16:05:31 -050011560 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080011561 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050011562 switch (i) {
11563 case LPFC_FCP_RING: /* ring 0 - FCP */
11564 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040011565 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
11566 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
11567 pring->sli.sli3.numCiocb +=
11568 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11569 pring->sli.sli3.numRiocb +=
11570 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11571 pring->sli.sli3.numCiocb +=
11572 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11573 pring->sli.sli3.numRiocb +=
11574 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11575 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050011576 SLI3_IOCB_CMD_SIZE :
11577 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040011578 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050011579 SLI3_IOCB_RSP_SIZE :
11580 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050011581 pring->iotag_ctr = 0;
11582 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050011583 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050011584 pring->fast_iotag = pring->iotag_max;
11585 pring->num_mask = 0;
11586 break;
James Smarta4bc3372006-12-02 13:34:16 -050011587 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050011588 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040011589 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
11590 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
11591 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050011592 SLI3_IOCB_CMD_SIZE :
11593 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040011594 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050011595 SLI3_IOCB_RSP_SIZE :
11596 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050011597 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050011598 pring->num_mask = 0;
11599 break;
11600 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
11601 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040011602 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
11603 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
11604 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050011605 SLI3_IOCB_CMD_SIZE :
11606 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040011607 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050011608 SLI3_IOCB_RSP_SIZE :
11609 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050011610 pring->fast_iotag = 0;
11611 pring->iotag_ctr = 0;
11612 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040011613 pring->lpfc_sli_rcv_async_status =
11614 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040011615 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050011616 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040011617 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11618 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050011619 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050011620 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050011621 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040011622 pring->prt[1].rctl = FC_RCTL_ELS_REP;
11623 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050011624 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050011625 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050011626 pring->prt[2].profile = 0; /* Mask 2 */
11627 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040011628 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050011629 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040011630 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050011631 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050011632 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050011633 pring->prt[3].profile = 0; /* Mask 3 */
11634 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040011635 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050011636 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040011637 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050011638 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050011639 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050011640 break;
11641 }
James Smart7e56aa22012-08-03 12:35:34 -040011642 totiocbsize += (pring->sli.sli3.numCiocb *
11643 pring->sli.sli3.sizeCiocb) +
11644 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050011645 }
James Smarted957682007-06-17 19:56:37 -050011646 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050011647 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040011648 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
11649 "SLI2 SLIM Data: x%x x%lx\n",
11650 phba->brd_no, totiocbsize,
11651 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050011652 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050011653 if (phba->cfg_multi_ring_support == 2)
11654 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050011655
11656 return 0;
11657}
11658
James Smarte59058c2008-08-24 21:49:00 -040011659/**
James Smart895427b2017-02-12 13:52:30 -080011660 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040011661 * @phba: Pointer to HBA context object.
11662 *
James Smart895427b2017-02-12 13:52:30 -080011663 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040011664 * ring. This function also initializes ring indices of each ring.
11665 * This function is called during the initialization of the SLI
11666 * interface of an HBA.
11667 * This function is called with no lock held and always returns
11668 * 1.
11669 **/
James Smart895427b2017-02-12 13:52:30 -080011670void
11671lpfc_sli4_queue_init(struct lpfc_hba *phba)
11672{
11673 struct lpfc_sli *psli;
11674 struct lpfc_sli_ring *pring;
11675 int i;
11676
11677 psli = &phba->sli;
11678 spin_lock_irq(&phba->hbalock);
11679 INIT_LIST_HEAD(&psli->mboxq);
11680 INIT_LIST_HEAD(&psli->mboxq_cmpl);
11681 /* Initialize list headers for txq and txcmplq as double linked lists */
James Smartcdb42be2019-01-28 11:14:21 -080011682 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -070011683 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080011684 pring->flag = 0;
11685 pring->ringno = LPFC_FCP_RING;
James Smartc4908502019-01-28 11:14:28 -080011686 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011687 INIT_LIST_HEAD(&pring->txq);
11688 INIT_LIST_HEAD(&pring->txcmplq);
11689 INIT_LIST_HEAD(&pring->iocb_continueq);
11690 spin_lock_init(&pring->ring_lock);
11691 }
11692 pring = phba->sli4_hba.els_wq->pring;
11693 pring->flag = 0;
11694 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080011695 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011696 INIT_LIST_HEAD(&pring->txq);
11697 INIT_LIST_HEAD(&pring->txcmplq);
11698 INIT_LIST_HEAD(&pring->iocb_continueq);
11699 spin_lock_init(&pring->ring_lock);
11700
James Smartcdb42be2019-01-28 11:14:21 -080011701 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080011702 pring = phba->sli4_hba.nvmels_wq->pring;
11703 pring->flag = 0;
11704 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080011705 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011706 INIT_LIST_HEAD(&pring->txq);
11707 INIT_LIST_HEAD(&pring->txcmplq);
11708 INIT_LIST_HEAD(&pring->iocb_continueq);
11709 spin_lock_init(&pring->ring_lock);
11710 }
11711
11712 spin_unlock_irq(&phba->hbalock);
11713}
11714
11715/**
11716 * lpfc_sli_queue_init - Queue initialization function
11717 * @phba: Pointer to HBA context object.
11718 *
11719 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
11720 * ring. This function also initializes ring indices of each ring.
11721 * This function is called during the initialization of the SLI
11722 * interface of an HBA.
11723 * This function is called with no lock held and always returns
11724 * 1.
11725 **/
11726void
11727lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050011728{
11729 struct lpfc_sli *psli;
11730 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050011731 int i;
dea31012005-04-17 16:05:31 -050011732
11733 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050011734 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011735 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050011736 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050011737 /* Initialize list headers for txq and txcmplq as double linked lists */
11738 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080011739 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050011740 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040011741 pring->sli.sli3.next_cmdidx = 0;
11742 pring->sli.sli3.local_getidx = 0;
11743 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050011744 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050011745 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050011746 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080011747 pring->flag = 0;
11748 INIT_LIST_HEAD(&pring->txq);
11749 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040011750 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050011751 }
James Smart2e0fef82007-06-17 19:56:36 -050011752 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011753}
11754
James Smarte59058c2008-08-24 21:49:00 -040011755/**
James Smart04c68492009-05-22 14:52:52 -040011756 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
11757 * @phba: Pointer to HBA context object.
11758 *
11759 * This routine flushes the mailbox command subsystem. It will unconditionally
11760 * flush all the mailbox commands in the three possible stages in the mailbox
11761 * command sub-system: pending mailbox command queue; the outstanding mailbox
11762 * command; and completed mailbox command queue. It is caller's responsibility
11763 * to make sure that the driver is in the proper state to flush the mailbox
11764 * command sub-system. Namely, the posting of mailbox commands into the
11765 * pending mailbox command queue from the various clients must be stopped;
11766 * either the HBA is in a state that it will never works on the outstanding
11767 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
11768 * mailbox command has been completed.
11769 **/
11770static void
11771lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
11772{
11773 LIST_HEAD(completions);
11774 struct lpfc_sli *psli = &phba->sli;
11775 LPFC_MBOXQ_t *pmb;
11776 unsigned long iflag;
11777
James Smart523128e2018-09-10 10:30:46 -070011778 /* Disable softirqs, including timers from obtaining phba->hbalock */
11779 local_bh_disable();
11780
James Smart04c68492009-05-22 14:52:52 -040011781 /* Flush all the mailbox commands in the mbox system */
11782 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart523128e2018-09-10 10:30:46 -070011783
James Smart04c68492009-05-22 14:52:52 -040011784 /* The pending mailbox command queue */
11785 list_splice_init(&phba->sli.mboxq, &completions);
11786 /* The outstanding active mailbox command */
11787 if (psli->mbox_active) {
11788 list_add_tail(&psli->mbox_active->list, &completions);
11789 psli->mbox_active = NULL;
11790 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11791 }
11792 /* The completed mailbox command queue */
11793 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
11794 spin_unlock_irqrestore(&phba->hbalock, iflag);
11795
James Smart523128e2018-09-10 10:30:46 -070011796 /* Enable softirqs again, done with phba->hbalock */
11797 local_bh_enable();
11798
James Smart04c68492009-05-22 14:52:52 -040011799 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
11800 while (!list_empty(&completions)) {
11801 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
11802 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
11803 if (pmb->mbox_cmpl)
11804 pmb->mbox_cmpl(phba, pmb);
11805 }
11806}
11807
11808/**
James Smart3621a712009-04-06 18:47:14 -040011809 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040011810 * @vport: Pointer to virtual port object.
11811 *
11812 * lpfc_sli_host_down is called to clean up the resources
11813 * associated with a vport before destroying virtual
11814 * port data structures.
11815 * This function does following operations:
11816 * - Free discovery resources associated with this virtual
11817 * port.
11818 * - Free iocbs associated with this virtual port in
11819 * the txq.
11820 * - Send abort for all iocb commands associated with this
11821 * vport in txcmplq.
11822 *
11823 * This function is called with no lock held and always returns 1.
11824 **/
dea31012005-04-17 16:05:31 -050011825int
James Smart92d7f7b2007-06-17 19:56:38 -050011826lpfc_sli_host_down(struct lpfc_vport *vport)
11827{
James Smart858c9f62007-06-17 19:56:39 -050011828 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050011829 struct lpfc_hba *phba = vport->phba;
11830 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080011831 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050011832 struct lpfc_sli_ring *pring;
11833 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050011834 int i;
11835 unsigned long flags = 0;
11836 uint16_t prev_pring_flag;
11837
11838 lpfc_cleanup_discovery_resources(vport);
11839
11840 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050011841
James Smart895427b2017-02-12 13:52:30 -080011842 /*
11843 * Error everything on the txq since these iocbs
11844 * have not been given to the FW yet.
11845 * Also issue ABTS for everything on the txcmplq
11846 */
11847 if (phba->sli_rev != LPFC_SLI_REV4) {
11848 for (i = 0; i < psli->num_rings; i++) {
11849 pring = &psli->sli3_ring[i];
11850 prev_pring_flag = pring->flag;
11851 /* Only slow rings */
11852 if (pring->ringno == LPFC_ELS_RING) {
11853 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11854 /* Set the lpfc data pending flag */
11855 set_bit(LPFC_DATA_READY, &phba->data_flags);
11856 }
11857 list_for_each_entry_safe(iocb, next_iocb,
11858 &pring->txq, list) {
11859 if (iocb->vport != vport)
11860 continue;
11861 list_move_tail(&iocb->list, &completions);
11862 }
11863 list_for_each_entry_safe(iocb, next_iocb,
11864 &pring->txcmplq, list) {
11865 if (iocb->vport != vport)
11866 continue;
James Smartdb7531d2020-11-15 11:26:44 -080011867 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11868 NULL);
James Smart895427b2017-02-12 13:52:30 -080011869 }
11870 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050011871 }
James Smart895427b2017-02-12 13:52:30 -080011872 } else {
11873 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11874 pring = qp->pring;
11875 if (!pring)
11876 continue;
11877 if (pring == phba->sli4_hba.els_wq->pring) {
11878 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11879 /* Set the lpfc data pending flag */
11880 set_bit(LPFC_DATA_READY, &phba->data_flags);
11881 }
11882 prev_pring_flag = pring->flag;
James Smart65a3df62019-09-21 20:58:48 -070011883 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011884 list_for_each_entry_safe(iocb, next_iocb,
11885 &pring->txq, list) {
11886 if (iocb->vport != vport)
11887 continue;
11888 list_move_tail(&iocb->list, &completions);
11889 }
James Smart65a3df62019-09-21 20:58:48 -070011890 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011891 list_for_each_entry_safe(iocb, next_iocb,
11892 &pring->txcmplq, list) {
11893 if (iocb->vport != vport)
11894 continue;
James Smartdb7531d2020-11-15 11:26:44 -080011895 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11896 NULL);
James Smart895427b2017-02-12 13:52:30 -080011897 }
11898 pring->flag = prev_pring_flag;
11899 }
James Smart92d7f7b2007-06-17 19:56:38 -050011900 }
James Smart92d7f7b2007-06-17 19:56:38 -050011901 spin_unlock_irqrestore(&phba->hbalock, flags);
11902
James Smarta22d73b2021-01-04 10:02:38 -080011903 /* Make sure HBA is alive */
11904 lpfc_issue_hb_tmo(phba);
11905
James Smarta257bf92009-04-06 18:48:10 -040011906 /* Cancel all the IOCBs from the completions list */
11907 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11908 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050011909 return 1;
11910}
11911
James Smarte59058c2008-08-24 21:49:00 -040011912/**
James Smart3621a712009-04-06 18:47:14 -040011913 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040011914 * @phba: Pointer to HBA context object.
11915 *
11916 * This function cleans up all iocb, buffers, mailbox commands
11917 * while shutting down the HBA. This function is called with no
11918 * lock held and always returns 1.
11919 * This function does the following to cleanup driver resources:
11920 * - Free discovery resources for each virtual port
11921 * - Cleanup any pending fabric iocbs
11922 * - Iterate through the iocb txq and free each entry
11923 * in the list.
11924 * - Free up any buffer posted to the HBA
11925 * - Free mailbox commands in the mailbox queue.
11926 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011927int
James Smart2e0fef82007-06-17 19:56:36 -050011928lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050011929{
James Smart2534ba72007-04-25 09:52:20 -040011930 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050011931 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080011932 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050011933 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050011934 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050011935 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040011936 int i;
11937
11938 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040011939 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050011940
dea31012005-04-17 16:05:31 -050011941 lpfc_hba_down_prep(phba);
11942
James Smart523128e2018-09-10 10:30:46 -070011943 /* Disable softirqs, including timers from obtaining phba->hbalock */
11944 local_bh_disable();
11945
James Smart92d7f7b2007-06-17 19:56:38 -050011946 lpfc_fabric_abort_hba(phba);
11947
James Smart2e0fef82007-06-17 19:56:36 -050011948 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050011949
James Smart895427b2017-02-12 13:52:30 -080011950 /*
11951 * Error everything on the txq since these iocbs
11952 * have not been given to the FW yet.
11953 */
11954 if (phba->sli_rev != LPFC_SLI_REV4) {
11955 for (i = 0; i < psli->num_rings; i++) {
11956 pring = &psli->sli3_ring[i];
11957 /* Only slow rings */
11958 if (pring->ringno == LPFC_ELS_RING) {
11959 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11960 /* Set the lpfc data pending flag */
11961 set_bit(LPFC_DATA_READY, &phba->data_flags);
11962 }
11963 list_splice_init(&pring->txq, &completions);
11964 }
11965 } else {
11966 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11967 pring = qp->pring;
11968 if (!pring)
11969 continue;
James Smart4b0a42b2019-08-14 16:56:42 -070011970 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011971 list_splice_init(&pring->txq, &completions);
James Smart4b0a42b2019-08-14 16:56:42 -070011972 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011973 if (pring == phba->sli4_hba.els_wq->pring) {
11974 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11975 /* Set the lpfc data pending flag */
11976 set_bit(LPFC_DATA_READY, &phba->data_flags);
11977 }
11978 }
dea31012005-04-17 16:05:31 -050011979 }
James Smart2e0fef82007-06-17 19:56:36 -050011980 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050011981
James Smarta257bf92009-04-06 18:48:10 -040011982 /* Cancel all the IOCBs from the completions list */
11983 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11984 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040011985
James Smart0ff10d42008-01-11 01:52:36 -050011986 spin_lock_irqsave(&phba->hbalock, flags);
11987 list_splice_init(&phba->elsbuf, &completions);
11988 phba->elsbuf_cnt = 0;
11989 phba->elsbuf_prev_cnt = 0;
11990 spin_unlock_irqrestore(&phba->hbalock, flags);
11991
11992 while (!list_empty(&completions)) {
11993 list_remove_head(&completions, buf_ptr,
11994 struct lpfc_dmabuf, list);
11995 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
11996 kfree(buf_ptr);
11997 }
11998
James Smart523128e2018-09-10 10:30:46 -070011999 /* Enable softirqs again, done with phba->hbalock */
12000 local_bh_enable();
12001
dea31012005-04-17 16:05:31 -050012002 /* Return any active mbox cmds */
12003 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050012004
James Smartda0436e2009-05-22 14:51:39 -040012005 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050012006 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040012007 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050012008
James Smartda0436e2009-05-22 14:51:39 -040012009 return 1;
12010}
James Smart92d7f7b2007-06-17 19:56:38 -050012011
James Smartda0436e2009-05-22 14:51:39 -040012012/**
James Smart3621a712009-04-06 18:47:14 -040012013 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040012014 * @srcp: Source memory pointer.
12015 * @destp: Destination memory pointer.
12016 * @cnt: Number of words required to be copied.
12017 *
12018 * This function is used for copying data between driver memory
12019 * and the SLI memory. This function also changes the endianness
12020 * of each word if native endianness is different from SLI
12021 * endianness. This function can be called with or without
12022 * lock.
12023 **/
dea31012005-04-17 16:05:31 -050012024void
12025lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
12026{
12027 uint32_t *src = srcp;
12028 uint32_t *dest = destp;
12029 uint32_t ldata;
12030 int i;
12031
12032 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
12033 ldata = *src;
12034 ldata = le32_to_cpu(ldata);
12035 *dest = ldata;
12036 src++;
12037 dest++;
12038 }
12039}
12040
James Smarte59058c2008-08-24 21:49:00 -040012041
12042/**
James Smarta0c87cb2009-07-19 10:01:10 -040012043 * lpfc_sli_bemem_bcopy - SLI memory copy function
12044 * @srcp: Source memory pointer.
12045 * @destp: Destination memory pointer.
12046 * @cnt: Number of words required to be copied.
12047 *
12048 * This function is used for copying data between a data structure
12049 * with big endian representation to local endianness.
12050 * This function can be called with or without lock.
12051 **/
12052void
12053lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
12054{
12055 uint32_t *src = srcp;
12056 uint32_t *dest = destp;
12057 uint32_t ldata;
12058 int i;
12059
12060 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
12061 ldata = *src;
12062 ldata = be32_to_cpu(ldata);
12063 *dest = ldata;
12064 src++;
12065 dest++;
12066 }
12067}
12068
12069/**
James Smart3621a712009-04-06 18:47:14 -040012070 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040012071 * @phba: Pointer to HBA context object.
12072 * @pring: Pointer to driver SLI ring object.
12073 * @mp: Pointer to driver buffer object.
12074 *
12075 * This function is called with no lock held.
12076 * It always return zero after adding the buffer to the postbufq
12077 * buffer list.
12078 **/
dea31012005-04-17 16:05:31 -050012079int
James Smart2e0fef82007-06-17 19:56:36 -050012080lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12081 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050012082{
12083 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
12084 later */
James Smart2e0fef82007-06-17 19:56:36 -050012085 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050012086 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050012087 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050012088 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050012089 return 0;
12090}
12091
James Smarte59058c2008-08-24 21:49:00 -040012092/**
James Smart3621a712009-04-06 18:47:14 -040012093 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040012094 * @phba: Pointer to HBA context object.
12095 *
12096 * When HBQ is enabled, buffers are searched based on tags. This function
12097 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
12098 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
12099 * does not conflict with tags of buffer posted for unsolicited events.
12100 * The function returns the allocated tag. The function is called with
12101 * no locks held.
12102 **/
James Smart76bb24e2007-10-27 13:38:00 -040012103uint32_t
12104lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
12105{
12106 spin_lock_irq(&phba->hbalock);
12107 phba->buffer_tag_count++;
12108 /*
12109 * Always set the QUE_BUFTAG_BIT to distiguish between
12110 * a tag assigned by HBQ.
12111 */
12112 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
12113 spin_unlock_irq(&phba->hbalock);
12114 return phba->buffer_tag_count;
12115}
12116
James Smarte59058c2008-08-24 21:49:00 -040012117/**
James Smart3621a712009-04-06 18:47:14 -040012118 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040012119 * @phba: Pointer to HBA context object.
12120 * @pring: Pointer to driver SLI ring object.
12121 * @tag: Buffer tag.
12122 *
12123 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
12124 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
12125 * iocb is posted to the response ring with the tag of the buffer.
12126 * This function searches the pring->postbufq list using the tag
12127 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
12128 * iocb. If the buffer is found then lpfc_dmabuf object of the
12129 * buffer is returned to the caller else NULL is returned.
12130 * This function is called with no lock held.
12131 **/
James Smart76bb24e2007-10-27 13:38:00 -040012132struct lpfc_dmabuf *
12133lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12134 uint32_t tag)
12135{
12136 struct lpfc_dmabuf *mp, *next_mp;
12137 struct list_head *slp = &pring->postbufq;
12138
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012139 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040012140 spin_lock_irq(&phba->hbalock);
12141 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12142 if (mp->buffer_tag == tag) {
12143 list_del_init(&mp->list);
12144 pring->postbufq_cnt--;
12145 spin_unlock_irq(&phba->hbalock);
12146 return mp;
12147 }
12148 }
12149
12150 spin_unlock_irq(&phba->hbalock);
Dick Kennedy372c1872020-06-30 14:50:00 -070012151 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd7c255b2008-08-24 21:50:00 -040012152 "0402 Cannot find virtual addr for buffer tag on "
James Smart32350662019-08-14 16:57:06 -070012153 "ring %d Data x%lx x%px x%px x%x\n",
James Smart76bb24e2007-10-27 13:38:00 -040012154 pring->ringno, (unsigned long) tag,
12155 slp->next, slp->prev, pring->postbufq_cnt);
12156
12157 return NULL;
12158}
dea31012005-04-17 16:05:31 -050012159
James Smarte59058c2008-08-24 21:49:00 -040012160/**
James Smart3621a712009-04-06 18:47:14 -040012161 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040012162 * @phba: Pointer to HBA context object.
12163 * @pring: Pointer to driver SLI ring object.
12164 * @phys: DMA address of the buffer.
12165 *
12166 * This function searches the buffer list using the dma_address
12167 * of unsolicited event to find the driver's lpfc_dmabuf object
12168 * corresponding to the dma_address. The function returns the
12169 * lpfc_dmabuf object if a buffer is found else it returns NULL.
12170 * This function is called by the ct and els unsolicited event
12171 * handlers to get the buffer associated with the unsolicited
12172 * event.
12173 *
12174 * This function is called with no lock held.
12175 **/
dea31012005-04-17 16:05:31 -050012176struct lpfc_dmabuf *
12177lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12178 dma_addr_t phys)
12179{
12180 struct lpfc_dmabuf *mp, *next_mp;
12181 struct list_head *slp = &pring->postbufq;
12182
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012183 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050012184 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050012185 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12186 if (mp->phys == phys) {
12187 list_del_init(&mp->list);
12188 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050012189 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050012190 return mp;
12191 }
12192 }
12193
James Smart2e0fef82007-06-17 19:56:36 -050012194 spin_unlock_irq(&phba->hbalock);
Dick Kennedy372c1872020-06-30 14:50:00 -070012195 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040012196 "0410 Cannot find virtual addr for mapped buf on "
James Smart32350662019-08-14 16:57:06 -070012197 "ring %d Data x%llx x%px x%px x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012198 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050012199 slp->next, slp->prev, pring->postbufq_cnt);
12200 return NULL;
12201}
12202
James Smarte59058c2008-08-24 21:49:00 -040012203/**
James Smart3621a712009-04-06 18:47:14 -040012204 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040012205 * @phba: Pointer to HBA context object.
12206 * @cmdiocb: Pointer to driver command iocb object.
12207 * @rspiocb: Pointer to driver response iocb object.
12208 *
12209 * This function is the completion handler for the abort iocbs for
12210 * ELS commands. This function is called from the ELS ring event
12211 * handler with no lock held. This function frees memory resources
12212 * associated with the abort iocb.
12213 **/
dea31012005-04-17 16:05:31 -050012214static void
James Smart2e0fef82007-06-17 19:56:36 -050012215lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12216 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050012217{
James Smart2e0fef82007-06-17 19:56:36 -050012218 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040012219 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050012220 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040012221
12222 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050012223
12224 /*
12225 * Assume that the port already completed and returned, or
12226 * will return the iocb. Just Log the message.
12227 */
James Smart2680eea2007-04-25 09:52:55 -040012228 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
12229 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
12230
James Smart2e0fef82007-06-17 19:56:36 -050012231 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040012232 if (phba->sli_rev < LPFC_SLI_REV4) {
James Smartfaa832e2018-07-31 17:23:18 -070012233 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
12234 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
12235 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
12236 spin_unlock_irq(&phba->hbalock);
12237 goto release_iocb;
12238 }
James Smart45ed1192009-10-02 15:17:02 -040012239 if (abort_iotag != 0 &&
12240 abort_iotag <= phba->sli.last_iotag)
12241 abort_iocb =
12242 phba->sli.iocbq_lookup[abort_iotag];
12243 } else
12244 /* For sli4 the abort_tag is the XRI,
12245 * so the abort routine puts the iotag of the iocb
12246 * being aborted in the context field of the abort
12247 * IOCB.
12248 */
12249 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040012250
James Smart2a9bf3d2010-06-07 15:24:45 -040012251 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
James Smart32350662019-08-14 16:57:06 -070012252 "0327 Cannot abort els iocb x%px "
James Smart2a9bf3d2010-06-07 15:24:45 -040012253 "with tag %x context %x, abort status %x, "
12254 "abort code %x\n",
12255 abort_iocb, abort_iotag, abort_context,
12256 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040012257
James Smartff78d8f2011-12-13 13:21:35 -050012258 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040012259 }
James Smartfaa832e2018-07-31 17:23:18 -070012260release_iocb:
James Bottomley604a3e32005-10-29 10:28:33 -050012261 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050012262 return;
12263}
12264
James Smarte59058c2008-08-24 21:49:00 -040012265/**
James Smart3621a712009-04-06 18:47:14 -040012266 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040012267 * @phba: Pointer to HBA context object.
12268 * @cmdiocb: Pointer to driver command iocb object.
12269 * @rspiocb: Pointer to driver response iocb object.
12270 *
12271 * The function is called from SLI ring event handler with no
12272 * lock held. This function is the completion handler for ELS commands
12273 * which are aborted. The function frees memory resources used for
12274 * the aborted ELS commands.
12275 **/
James Smart9dd83f72021-03-01 09:18:11 -080012276void
James Smart92d7f7b2007-06-17 19:56:38 -050012277lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12278 struct lpfc_iocbq *rspiocb)
12279{
James Smart4e670c8a2021-07-07 11:43:38 -070012280 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
James Smart92d7f7b2007-06-17 19:56:38 -050012281 IOCB_t *irsp = &rspiocb->iocb;
12282
12283 /* ELS cmd tag <ulpIoTag> completes */
12284 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040012285 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050012286 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012287 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050012288 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart4e670c8a2021-07-07 11:43:38 -070012289 /*
12290 * Deref the ndlp after free_iocb. sli_release_iocb will access the ndlp
12291 * if exchange is busy.
12292 */
James Smart858c9f62007-06-17 19:56:39 -050012293 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
12294 lpfc_ct_free_iocb(phba, cmdiocb);
12295 else
12296 lpfc_els_free_iocb(phba, cmdiocb);
James Smart4e670c8a2021-07-07 11:43:38 -070012297
12298 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -050012299}
12300
James Smarte59058c2008-08-24 21:49:00 -040012301/**
James Smartdb7531d2020-11-15 11:26:44 -080012302 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040012303 * @phba: Pointer to HBA context object.
12304 * @pring: Pointer to driver SLI ring object.
12305 * @cmdiocb: Pointer to driver command iocb object.
James Smartdb7531d2020-11-15 11:26:44 -080012306 * @cmpl: completion function.
James Smarte59058c2008-08-24 21:49:00 -040012307 *
James Smartdb7531d2020-11-15 11:26:44 -080012308 * This function issues an abort iocb for the provided command iocb. In case
12309 * of unloading, the abort iocb will not be issued to commands on the ELS
12310 * ring. Instead, the callback function shall be changed to those commands
12311 * so that nothing happens when them finishes. This function is called with
12312 * hbalock held andno ring_lock held (SLI4). The function returns IOCB_SUCCESS
12313 * when the command iocb is an abort request.
12314 *
James Smarte59058c2008-08-24 21:49:00 -040012315 **/
James Smartdb7531d2020-11-15 11:26:44 -080012316int
12317lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12318 struct lpfc_iocbq *cmdiocb, void *cmpl)
dea31012005-04-17 16:05:31 -050012319{
James Smart2e0fef82007-06-17 19:56:36 -050012320 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012321 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050012322 IOCB_t *icmd = NULL;
12323 IOCB_t *iabt = NULL;
James Smartdb7531d2020-11-15 11:26:44 -080012324 int retval = IOCB_ERROR;
James Smart7e56aa22012-08-03 12:35:34 -040012325 unsigned long iflags;
James Smartfaa832e2018-07-31 17:23:18 -070012326 struct lpfc_nodelist *ndlp;
James Smart07951072007-04-25 09:51:38 -040012327
James Smart92d7f7b2007-06-17 19:56:38 -050012328 /*
12329 * There are certain command types we don't want to abort. And we
12330 * don't want to abort commands that are already in the process of
12331 * being aborted.
James Smart07951072007-04-25 09:51:38 -040012332 */
12333 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050012334 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050012335 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
James Smart078c68b2021-04-11 18:31:12 -070012336 cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED)
James Smartdb7531d2020-11-15 11:26:44 -080012337 return IOCB_ABORTING;
12338
12339 if (!pring) {
12340 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
12341 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
12342 else
12343 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
12344 return retval;
12345 }
12346
12347 /*
12348 * If we're unloading, don't abort iocb on the ELS ring, but change
12349 * the callback so that nothing happens when it finishes.
12350 */
12351 if ((vport->load_flag & FC_UNLOADING) &&
12352 pring->ringno == LPFC_ELS_RING) {
12353 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
12354 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
12355 else
12356 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
12357 return retval;
12358 }
James Smart07951072007-04-25 09:51:38 -040012359
dea31012005-04-17 16:05:31 -050012360 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050012361 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050012362 if (abtsiocbp == NULL)
James Smartdb7531d2020-11-15 11:26:44 -080012363 return IOCB_NORESOURCE;
dea31012005-04-17 16:05:31 -050012364
James Smart07951072007-04-25 09:51:38 -040012365 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050012366 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040012367 */
12368 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
12369
dea31012005-04-17 16:05:31 -050012370 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040012371 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
12372 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040012373 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040012374 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smartdb7531d2020-11-15 11:26:44 -080012375 if (pring->ringno == LPFC_ELS_RING)
12376 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
James Smartfaa832e2018-07-31 17:23:18 -070012377 } else {
James Smartda0436e2009-05-22 14:51:39 -040012378 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
James Smartfaa832e2018-07-31 17:23:18 -070012379 if (pring->ringno == LPFC_ELS_RING) {
12380 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
12381 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
12382 }
12383 }
dea31012005-04-17 16:05:31 -050012384 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040012385 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050012386
James Smart5ffc2662009-11-18 15:39:44 -050012387 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080012388 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smartdb7531d2020-11-15 11:26:44 -080012389 if (cmdiocb->iocb_flag & LPFC_IO_FCP) {
12390 abtsiocbp->iocb_flag |= LPFC_IO_FCP;
James Smart341af102010-01-26 23:07:37 -050012391 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smartdb7531d2020-11-15 11:26:44 -080012392 }
James Smart9bd2bff52014-09-03 12:57:30 -040012393 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
12394 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050012395
James Smart2e0fef82007-06-17 19:56:36 -050012396 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040012397 iabt->ulpCommand = CMD_ABORT_XRI_CN;
12398 else
12399 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
12400
James Smartdb7531d2020-11-15 11:26:44 -080012401 if (cmpl)
12402 abtsiocbp->iocb_cmpl = cmpl;
12403 else
12404 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080012405 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040012406
James Smart7e56aa22012-08-03 12:35:34 -040012407 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080012408 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
12409 if (unlikely(pring == NULL))
James Smartdb7531d2020-11-15 11:26:44 -080012410 goto abort_iotag_exit;
James Smart7e56aa22012-08-03 12:35:34 -040012411 /* Note: both hbalock and ring_lock need to be set here */
12412 spin_lock_irqsave(&pring->ring_lock, iflags);
12413 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12414 abtsiocbp, 0);
12415 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12416 } else {
12417 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12418 abtsiocbp, 0);
12419 }
James Smart07951072007-04-25 09:51:38 -040012420
12421abort_iotag_exit:
James Smartdb7531d2020-11-15 11:26:44 -080012422
12423 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
12424 "0339 Abort xri x%x, original iotag x%x, "
12425 "abort cmd iotag x%x retval x%x\n",
12426 iabt->un.acxri.abortIoTag,
12427 iabt->un.acxri.abortContextTag,
12428 abtsiocbp->iotag, retval);
12429
12430 if (retval) {
12431 cmdiocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12432 __lpfc_sli_release_iocbq(phba, abtsiocbp);
12433 }
12434
James Smart2e0fef82007-06-17 19:56:36 -050012435 /*
12436 * Caller to this routine should check for IOCB_ERROR
12437 * and handle it properly. This routine no longer removes
12438 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040012439 */
James Smart2e0fef82007-06-17 19:56:36 -050012440 return retval;
dea31012005-04-17 16:05:31 -050012441}
12442
James Smarte59058c2008-08-24 21:49:00 -040012443/**
James Smart5af5eee2010-10-22 11:06:38 -040012444 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
12445 * @phba: pointer to lpfc HBA data structure.
12446 *
12447 * This routine will abort all pending and outstanding iocbs to an HBA.
12448 **/
12449void
12450lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
12451{
12452 struct lpfc_sli *psli = &phba->sli;
12453 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080012454 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040012455 int i;
12456
James Smart895427b2017-02-12 13:52:30 -080012457 if (phba->sli_rev != LPFC_SLI_REV4) {
12458 for (i = 0; i < psli->num_rings; i++) {
12459 pring = &psli->sli3_ring[i];
12460 lpfc_sli_abort_iocb_ring(phba, pring);
12461 }
12462 return;
12463 }
12464 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12465 pring = qp->pring;
12466 if (!pring)
12467 continue;
James Smartdb55fba2014-04-04 13:52:02 -040012468 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040012469 }
12470}
12471
12472/**
James Smart3621a712009-04-06 18:47:14 -040012473 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040012474 * @iocbq: Pointer to driver iocb object.
12475 * @vport: Pointer to driver virtual port object.
12476 * @tgt_id: SCSI ID of the target.
12477 * @lun_id: LUN ID of the scsi device.
12478 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
12479 *
James Smart3621a712009-04-06 18:47:14 -040012480 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040012481 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
12482 * 0 if the filtering criteria is met for the given iocb and will return
12483 * 1 if the filtering criteria is not met.
12484 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
12485 * given iocb is for the SCSI device specified by vport, tgt_id and
12486 * lun_id parameter.
12487 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
12488 * given iocb is for the SCSI target specified by vport and tgt_id
12489 * parameters.
12490 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
12491 * given iocb is for the SCSI host associated with the given vport.
12492 * This function is called with no locks held.
12493 **/
dea31012005-04-17 16:05:31 -050012494static int
James Smart51ef4c22007-08-02 11:10:31 -040012495lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
12496 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012497 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050012498{
James Smartc4908502019-01-28 11:14:28 -080012499 struct lpfc_io_buf *lpfc_cmd;
James Smarte1364712021-04-21 16:44:33 -070012500 IOCB_t *icmd = NULL;
dea31012005-04-17 16:05:31 -050012501 int rc = 1;
12502
James Smart9ec58ec2021-01-04 10:02:35 -080012503 if (!iocbq || iocbq->vport != vport)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012504 return rc;
12505
James Smarte1364712021-04-21 16:44:33 -070012506 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
12507 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ) ||
12508 iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
12509 return rc;
12510
12511 icmd = &iocbq->iocb;
12512 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
12513 icmd->ulpCommand == CMD_CLOSE_XRI_CN)
James Smart51ef4c22007-08-02 11:10:31 -040012514 return rc;
12515
James Smartc4908502019-01-28 11:14:28 -080012516 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012517
James Smart495a7142008-06-14 22:52:59 -040012518 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050012519 return rc;
12520
12521 switch (ctx_cmd) {
12522 case LPFC_CTX_LUN:
James Smartb0e83012018-06-26 08:24:29 -070012523 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040012524 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
12525 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050012526 rc = 0;
12527 break;
12528 case LPFC_CTX_TGT:
James Smartb0e83012018-06-26 08:24:29 -070012529 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040012530 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050012531 rc = 0;
12532 break;
dea31012005-04-17 16:05:31 -050012533 case LPFC_CTX_HOST:
12534 rc = 0;
12535 break;
12536 default:
12537 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070012538 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050012539 break;
12540 }
12541
12542 return rc;
12543}
12544
James Smarte59058c2008-08-24 21:49:00 -040012545/**
James Smart3621a712009-04-06 18:47:14 -040012546 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040012547 * @vport: Pointer to virtual port.
12548 * @tgt_id: SCSI ID of the target.
12549 * @lun_id: LUN ID of the scsi device.
12550 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12551 *
12552 * This function returns number of FCP commands pending for the vport.
12553 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
12554 * commands pending on the vport associated with SCSI device specified
12555 * by tgt_id and lun_id parameters.
12556 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
12557 * commands pending on the vport associated with SCSI target specified
12558 * by tgt_id parameter.
12559 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
12560 * commands pending on the vport.
12561 * This function returns the number of iocbs which satisfy the filter.
12562 * This function is called without any lock held.
12563 **/
dea31012005-04-17 16:05:31 -050012564int
James Smart51ef4c22007-08-02 11:10:31 -040012565lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
12566 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050012567{
James Smart51ef4c22007-08-02 11:10:31 -040012568 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012569 struct lpfc_iocbq *iocbq;
12570 int sum, i;
dea31012005-04-17 16:05:31 -050012571
Johannes Thumshirn31979002016-07-18 16:06:03 +020012572 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012573 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
12574 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050012575
James Smart51ef4c22007-08-02 11:10:31 -040012576 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
12577 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012578 sum++;
dea31012005-04-17 16:05:31 -050012579 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020012580 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012581
dea31012005-04-17 16:05:31 -050012582 return sum;
12583}
12584
James Smarte59058c2008-08-24 21:49:00 -040012585/**
James Smartdb7531d2020-11-15 11:26:44 -080012586 * lpfc_sli4_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
12587 * @phba: Pointer to HBA context object
12588 * @cmdiocb: Pointer to command iocb object.
12589 * @wcqe: pointer to the complete wcqe
12590 *
12591 * This function is called when an aborted FCP iocb completes. This
12592 * function is called by the ring event handler with no lock held.
12593 * This function frees the iocb. It is called for sli-4 adapters.
12594 **/
12595void
12596lpfc_sli4_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12597 struct lpfc_wcqe_complete *wcqe)
12598{
12599 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12600 "3017 ABORT_XRI_CN completing on rpi x%x "
12601 "original iotag x%x, abort cmd iotag x%x "
12602 "status 0x%x, reason 0x%x\n",
12603 cmdiocb->iocb.un.acxri.abortContextTag,
12604 cmdiocb->iocb.un.acxri.abortIoTag,
12605 cmdiocb->iotag,
12606 (bf_get(lpfc_wcqe_c_status, wcqe)
12607 & LPFC_IOCB_STATUS_MASK),
12608 wcqe->parameter);
12609 lpfc_sli_release_iocbq(phba, cmdiocb);
12610}
12611
12612/**
James Smart3621a712009-04-06 18:47:14 -040012613 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040012614 * @phba: Pointer to HBA context object
12615 * @cmdiocb: Pointer to command iocb object.
12616 * @rspiocb: Pointer to response iocb object.
12617 *
12618 * This function is called when an aborted FCP iocb completes. This
12619 * function is called by the ring event handler with no lock held.
12620 * This function frees the iocb.
12621 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040012622void
James Smart2e0fef82007-06-17 19:56:36 -050012623lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12624 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040012625{
James Smartcb69f7d2011-12-13 13:21:57 -050012626 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040012627 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050012628 "original iotag x%x, abort cmd iotag x%x "
12629 "status 0x%x, reason 0x%x\n",
12630 cmdiocb->iocb.un.acxri.abortContextTag,
12631 cmdiocb->iocb.un.acxri.abortIoTag,
12632 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
12633 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050012634 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040012635 return;
12636}
12637
James Smarte59058c2008-08-24 21:49:00 -040012638/**
James Smart3621a712009-04-06 18:47:14 -040012639 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040012640 * @vport: Pointer to virtual port.
James Smarte59058c2008-08-24 21:49:00 -040012641 * @tgt_id: SCSI ID of the target.
12642 * @lun_id: LUN ID of the scsi device.
12643 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12644 *
12645 * This function sends an abort command for every SCSI command
12646 * associated with the given virtual port pending on the ring
12647 * filtered by lpfc_sli_validate_fcp_iocb function.
12648 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
12649 * FCP iocbs associated with lun specified by tgt_id and lun_id
12650 * parameters
12651 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
12652 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12653 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
12654 * FCP iocbs associated with virtual port.
James Smart078c68b2021-04-11 18:31:12 -070012655 * The pring used for SLI3 is sli3_ring[LPFC_FCP_RING], for SLI4
12656 * lpfc_sli4_calc_ring is used.
James Smarte59058c2008-08-24 21:49:00 -040012657 * This function returns number of iocbs it failed to abort.
12658 * This function is called with no locks held.
12659 **/
dea31012005-04-17 16:05:31 -050012660int
James Smart078c68b2021-04-11 18:31:12 -070012661lpfc_sli_abort_iocb(struct lpfc_vport *vport, u16 tgt_id, u64 lun_id,
12662 lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050012663{
James Smart51ef4c22007-08-02 11:10:31 -040012664 struct lpfc_hba *phba = vport->phba;
James Smart078c68b2021-04-11 18:31:12 -070012665 struct lpfc_sli_ring *pring = NULL;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012666 struct lpfc_iocbq *iocbq;
dea31012005-04-17 16:05:31 -050012667 int errcnt = 0, ret_val = 0;
James Smartdb7531d2020-11-15 11:26:44 -080012668 unsigned long iflags;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012669 int i;
James Smart078c68b2021-04-11 18:31:12 -070012670 void *fcp_cmpl = NULL;
dea31012005-04-17 16:05:31 -050012671
James Smartb0e83012018-06-26 08:24:29 -070012672 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070012673 if (phba->hba_flag & HBA_IOQ_FLUSH)
James Smartb0e83012018-06-26 08:24:29 -070012674 return errcnt;
12675
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012676 for (i = 1; i <= phba->sli.last_iotag; i++) {
12677 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050012678
James Smart51ef4c22007-08-02 11:10:31 -040012679 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050012680 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050012681 continue;
12682
James Smartdb7531d2020-11-15 11:26:44 -080012683 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart078c68b2021-04-11 18:31:12 -070012684 if (phba->sli_rev == LPFC_SLI_REV3) {
12685 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12686 fcp_cmpl = lpfc_sli_abort_fcp_cmpl;
12687 } else if (phba->sli_rev == LPFC_SLI_REV4) {
12688 pring = lpfc_sli4_calc_ring(phba, iocbq);
12689 fcp_cmpl = lpfc_sli4_abort_fcp_cmpl;
12690 }
James Smartdb7531d2020-11-15 11:26:44 -080012691 ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocbq,
James Smart078c68b2021-04-11 18:31:12 -070012692 fcp_cmpl);
James Smartdb7531d2020-11-15 11:26:44 -080012693 spin_unlock_irqrestore(&phba->hbalock, iflags);
12694 if (ret_val != IOCB_SUCCESS)
dea31012005-04-17 16:05:31 -050012695 errcnt++;
dea31012005-04-17 16:05:31 -050012696 }
12697
12698 return errcnt;
12699}
12700
James Smarte59058c2008-08-24 21:49:00 -040012701/**
James Smart98912dda2014-04-04 13:52:31 -040012702 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
12703 * @vport: Pointer to virtual port.
12704 * @pring: Pointer to driver SLI ring object.
12705 * @tgt_id: SCSI ID of the target.
12706 * @lun_id: LUN ID of the scsi device.
Lee Jones7af29d42020-07-21 17:41:31 +010012707 * @cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
James Smart98912dda2014-04-04 13:52:31 -040012708 *
12709 * This function sends an abort command for every SCSI command
12710 * associated with the given virtual port pending on the ring
12711 * filtered by lpfc_sli_validate_fcp_iocb function.
12712 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
12713 * FCP iocbs associated with lun specified by tgt_id and lun_id
12714 * parameters
12715 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
12716 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12717 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
12718 * FCP iocbs associated with virtual port.
12719 * This function returns number of iocbs it aborted .
12720 * This function is called with no locks held right after a taskmgmt
12721 * command is sent.
12722 **/
12723int
12724lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
12725 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
12726{
12727 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -080012728 struct lpfc_io_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040012729 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040012730 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040012731 struct lpfc_iocbq *iocbq;
12732 IOCB_t *icmd;
12733 int sum, i, ret_val;
12734 unsigned long iflags;
James Smartc2017262019-01-28 11:14:37 -080012735 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart98912dda2014-04-04 13:52:31 -040012736
James Smart59c68ea2018-04-09 14:24:25 -070012737 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040012738
12739 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070012740 if (phba->hba_flag & HBA_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070012741 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040012742 return 0;
12743 }
12744 sum = 0;
12745
12746 for (i = 1; i <= phba->sli.last_iotag; i++) {
12747 iocbq = phba->sli.iocbq_lookup[i];
12748
12749 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12750 cmd) != 0)
12751 continue;
12752
James Smartc2017262019-01-28 11:14:37 -080012753 /* Guard against IO completion being called at same time */
12754 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12755 spin_lock(&lpfc_cmd->buf_lock);
12756
12757 if (!lpfc_cmd->pCmd) {
12758 spin_unlock(&lpfc_cmd->buf_lock);
12759 continue;
12760 }
12761
12762 if (phba->sli_rev == LPFC_SLI_REV4) {
12763 pring_s4 =
James Smartc00f62e2019-08-14 16:57:11 -070012764 phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
James Smartc2017262019-01-28 11:14:37 -080012765 if (!pring_s4) {
12766 spin_unlock(&lpfc_cmd->buf_lock);
12767 continue;
12768 }
12769 /* Note: both hbalock and ring_lock must be set here */
12770 spin_lock(&pring_s4->ring_lock);
12771 }
12772
James Smart98912dda2014-04-04 13:52:31 -040012773 /*
12774 * If the iocbq is already being aborted, don't take a second
12775 * action, but do count it.
12776 */
James Smartc2017262019-01-28 11:14:37 -080012777 if ((iocbq->iocb_flag & LPFC_DRIVER_ABORTED) ||
12778 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
12779 if (phba->sli_rev == LPFC_SLI_REV4)
12780 spin_unlock(&pring_s4->ring_lock);
12781 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040012782 continue;
James Smartc2017262019-01-28 11:14:37 -080012783 }
James Smart98912dda2014-04-04 13:52:31 -040012784
12785 /* issue ABTS for this IOCB based on iotag */
12786 abtsiocbq = __lpfc_sli_get_iocbq(phba);
James Smartc2017262019-01-28 11:14:37 -080012787 if (!abtsiocbq) {
12788 if (phba->sli_rev == LPFC_SLI_REV4)
12789 spin_unlock(&pring_s4->ring_lock);
12790 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040012791 continue;
James Smartc2017262019-01-28 11:14:37 -080012792 }
James Smart98912dda2014-04-04 13:52:31 -040012793
12794 icmd = &iocbq->iocb;
12795 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
12796 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
12797 if (phba->sli_rev == LPFC_SLI_REV4)
12798 abtsiocbq->iocb.un.acxri.abortIoTag =
12799 iocbq->sli4_xritag;
12800 else
12801 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
12802 abtsiocbq->iocb.ulpLe = 1;
12803 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
12804 abtsiocbq->vport = vport;
12805
12806 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080012807 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040012808 if (iocbq->iocb_flag & LPFC_IO_FCP)
12809 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040012810 if (iocbq->iocb_flag & LPFC_IO_FOF)
12811 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040012812
James Smart8c50d252014-09-03 12:58:16 -040012813 ndlp = lpfc_cmd->rdata->pnode;
12814
12815 if (lpfc_is_link_up(phba) &&
12816 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040012817 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
12818 else
12819 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
12820
12821 /* Setup callback routine and issue the command. */
12822 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
12823
12824 /*
12825 * Indicate the IO is being aborted by the driver and set
12826 * the caller's flag into the aborted IO.
12827 */
12828 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
12829
12830 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -040012831 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
12832 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070012833 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040012834 } else {
12835 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
12836 abtsiocbq, 0);
12837 }
12838
James Smartc2017262019-01-28 11:14:37 -080012839 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040012840
12841 if (ret_val == IOCB_ERROR)
12842 __lpfc_sli_release_iocbq(phba, abtsiocbq);
12843 else
12844 sum++;
12845 }
James Smart59c68ea2018-04-09 14:24:25 -070012846 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040012847 return sum;
12848}
12849
12850/**
James Smart3621a712009-04-06 18:47:14 -040012851 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040012852 * @phba: Pointer to HBA context object.
12853 * @cmdiocbq: Pointer to command iocb.
12854 * @rspiocbq: Pointer to response iocb.
12855 *
12856 * This function is the completion handler for iocbs issued using
12857 * lpfc_sli_issue_iocb_wait function. This function is called by the
12858 * ring event handler function without any lock held. This function
12859 * can be called from both worker thread context and interrupt
12860 * context. This function also can be called from other thread which
12861 * cleans up the SLI layer objects.
12862 * This function copy the contents of the response iocb to the
12863 * response iocb memory object provided by the caller of
12864 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
12865 * sleeps for the iocb completion.
12866 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012867static void
12868lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
12869 struct lpfc_iocbq *cmdiocbq,
12870 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050012871{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012872 wait_queue_head_t *pdone_q;
12873 unsigned long iflags;
James Smartc4908502019-01-28 11:14:28 -080012874 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050012875
James Smart2e0fef82007-06-17 19:56:36 -050012876 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040012877 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
12878
12879 /*
12880 * A time out has occurred for the iocb. If a time out
12881 * completion handler has been supplied, call it. Otherwise,
12882 * just free the iocbq.
12883 */
12884
12885 spin_unlock_irqrestore(&phba->hbalock, iflags);
12886 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
12887 cmdiocbq->wait_iocb_cmpl = NULL;
12888 if (cmdiocbq->iocb_cmpl)
12889 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
12890 else
12891 lpfc_sli_release_iocbq(phba, cmdiocbq);
12892 return;
12893 }
12894
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012895 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
12896 if (cmdiocbq->context2 && rspiocbq)
12897 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
12898 &rspiocbq->iocb, sizeof(IOCB_t));
12899
James Smart0f65ff62010-02-26 14:14:23 -050012900 /* Set the exchange busy flag for task management commands */
12901 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
12902 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smartc4908502019-01-28 11:14:28 -080012903 lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
James Smart0f65ff62010-02-26 14:14:23 -050012904 cur_iocbq);
James Smart324e1c42019-10-18 14:18:21 -070012905 if (rspiocbq && (rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY))
12906 lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
12907 else
12908 lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
James Smart0f65ff62010-02-26 14:14:23 -050012909 }
12910
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012911 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012912 if (pdone_q)
12913 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050012914 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050012915 return;
12916}
12917
James Smarte59058c2008-08-24 21:49:00 -040012918/**
James Smartd11e31d2009-06-10 17:23:06 -040012919 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
12920 * @phba: Pointer to HBA context object..
12921 * @piocbq: Pointer to command iocb.
12922 * @flag: Flag to test.
12923 *
12924 * This routine grabs the hbalock and then test the iocb_flag to
12925 * see if the passed in flag is set.
12926 * Returns:
12927 * 1 if flag is set.
12928 * 0 if flag is not set.
12929 **/
12930static int
12931lpfc_chk_iocb_flg(struct lpfc_hba *phba,
12932 struct lpfc_iocbq *piocbq, uint32_t flag)
12933{
12934 unsigned long iflags;
12935 int ret;
12936
12937 spin_lock_irqsave(&phba->hbalock, iflags);
12938 ret = piocbq->iocb_flag & flag;
12939 spin_unlock_irqrestore(&phba->hbalock, iflags);
12940 return ret;
12941
12942}
12943
12944/**
James Smart3621a712009-04-06 18:47:14 -040012945 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040012946 * @phba: Pointer to HBA context object..
Lee Jones7af29d42020-07-21 17:41:31 +010012947 * @ring_number: Ring number
James Smarte59058c2008-08-24 21:49:00 -040012948 * @piocb: Pointer to command iocb.
12949 * @prspiocbq: Pointer to response iocb.
12950 * @timeout: Timeout in number of seconds.
12951 *
12952 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040012953 * iocb to complete. The iocb_cmpl field of the shall be used
12954 * to handle iocbs which time out. If the field is NULL, the
12955 * function shall free the iocbq structure. If more clean up is
12956 * needed, the caller is expected to provide a completion function
12957 * that will provide the needed clean up. If the iocb command is
12958 * not completed within timeout seconds, the function will either
12959 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
12960 * completion function set in the iocb_cmpl field and then return
12961 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
12962 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040012963 * The function waits for the iocb completion using an
12964 * non-interruptible wait.
12965 * This function will sleep while waiting for iocb completion.
12966 * So, this function should not be called from any context which
12967 * does not allow sleeping. Due to the same reason, this function
12968 * cannot be called with interrupt disabled.
12969 * This function assumes that the iocb completions occur while
12970 * this function sleep. So, this function cannot be called from
12971 * the thread which process iocb completion for this ring.
12972 * This function clears the iocb_flag of the iocb object before
12973 * issuing the iocb and the iocb completion handler sets this
12974 * flag and wakes this thread when the iocb completes.
12975 * The contents of the response iocb will be copied to prspiocbq
12976 * by the completion handler when the command completes.
12977 * This function returns IOCB_SUCCESS when success.
12978 * This function is called with no lock held.
12979 **/
dea31012005-04-17 16:05:31 -050012980int
James Smart2e0fef82007-06-17 19:56:36 -050012981lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040012982 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050012983 struct lpfc_iocbq *piocb,
12984 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012985 uint32_t timeout)
dea31012005-04-17 16:05:31 -050012986{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080012987 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012988 long timeleft, timeout_req = 0;
12989 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012990 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050012991 struct lpfc_iocbq *iocb;
12992 int txq_cnt = 0;
12993 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080012994 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040012995 unsigned long iflags;
12996 bool iocb_completed = true;
12997
James Smart895427b2017-02-12 13:52:30 -080012998 if (phba->sli_rev >= LPFC_SLI_REV4)
12999 pring = lpfc_sli4_calc_ring(phba, piocb);
13000 else
13001 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050013002 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013003 * If the caller has provided a response iocbq buffer, then context2
13004 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050013005 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013006 if (prspiocbq) {
13007 if (piocb->context2)
13008 return IOCB_ERROR;
13009 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050013010 }
13011
James Smart5a0916b2013-07-15 18:31:42 -040013012 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013013 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
13014 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040013015 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050013016
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050013017 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050013018 if (lpfc_readl(phba->HCregaddr, &creg_val))
13019 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050013020 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
13021 writel(creg_val, phba->HCregaddr);
13022 readl(phba->HCregaddr); /* flush */
13023 }
13024
James Smart2a9bf3d2010-06-07 15:24:45 -040013025 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
13026 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013027 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040013028 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013029 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040013030 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013031 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040013032 spin_lock_irqsave(&phba->hbalock, iflags);
13033 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050013034
James Smart5a0916b2013-07-15 18:31:42 -040013035 /*
13036 * IOCB timed out. Inform the wake iocb wait
13037 * completion function and set local status
13038 */
13039
13040 iocb_completed = false;
13041 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
13042 }
13043 spin_unlock_irqrestore(&phba->hbalock, iflags);
13044 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040013045 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040013046 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040013047 /* Note: we are not indicating if the IOCB has a success
13048 * status or not - that's for the caller to check.
13049 * IOCB_SUCCESS means just that the command was sent and
13050 * completed. Not that it completed successfully.
13051 * */
James Smart7054a602007-04-25 09:52:34 -040013052 } else if (timeleft == 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013053 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040013054 "0338 IOCB wait timeout error - no "
13055 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013056 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040013057 } else {
Dick Kennedy372c1872020-06-30 14:50:00 -070013058 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040013059 "0330 IOCB wake NOT set, "
13060 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013061 timeout, (timeleft / jiffies));
13062 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050013063 }
James Smart2a9bf3d2010-06-07 15:24:45 -040013064 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050013065 if (phba->cfg_log_verbose & LOG_SLI) {
13066 list_for_each_entry(iocb, &pring->txq, list) {
13067 txq_cnt++;
13068 }
13069 list_for_each_entry(iocb, &pring->txcmplq, list) {
13070 txcmplq_cnt++;
13071 }
13072 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13073 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
13074 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
13075 }
James Smart2a9bf3d2010-06-07 15:24:45 -040013076 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013077 } else {
13078 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040013079 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040013080 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013081 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050013082 }
13083
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050013084 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050013085 if (lpfc_readl(phba->HCregaddr, &creg_val))
13086 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050013087 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
13088 writel(creg_val, phba->HCregaddr);
13089 readl(phba->HCregaddr); /* flush */
13090 }
13091
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013092 if (prspiocbq)
13093 piocb->context2 = NULL;
13094
13095 piocb->context_un.wait_queue = NULL;
13096 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050013097 return retval;
13098}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013099
James Smarte59058c2008-08-24 21:49:00 -040013100/**
James Smart3621a712009-04-06 18:47:14 -040013101 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040013102 * @phba: Pointer to HBA context object.
13103 * @pmboxq: Pointer to driver mailbox object.
13104 * @timeout: Timeout in number of seconds.
13105 *
13106 * This function issues the mailbox to firmware and waits for the
13107 * mailbox command to complete. If the mailbox command is not
13108 * completed within timeout seconds, it returns MBX_TIMEOUT.
13109 * The function waits for the mailbox completion using an
13110 * interruptible wait. If the thread is woken up due to a
13111 * signal, MBX_TIMEOUT error is returned to the caller. Caller
13112 * should not free the mailbox resources, if this function returns
13113 * MBX_TIMEOUT.
13114 * This function will sleep while waiting for mailbox completion.
13115 * So, this function should not be called from any context which
13116 * does not allow sleeping. Due to the same reason, this function
13117 * cannot be called with interrupt disabled.
13118 * This function assumes that the mailbox completion occurs while
13119 * this function sleep. So, this function cannot be called from
13120 * the worker thread which processes mailbox completion.
13121 * This function is called in the context of HBA management
13122 * applications.
13123 * This function returns MBX_SUCCESS when successful.
13124 * This function is called with no lock held.
13125 **/
dea31012005-04-17 16:05:31 -050013126int
James Smart2e0fef82007-06-17 19:56:36 -050013127lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050013128 uint32_t timeout)
13129{
James Smarte29d74f2018-03-05 12:04:07 -080013130 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050013131 int retval;
James Smart858c9f62007-06-17 19:56:39 -050013132 unsigned long flag;
dea31012005-04-17 16:05:31 -050013133
James Smart495a7142008-06-14 22:52:59 -040013134 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050013135 /* setup wake call as IOCB callback */
13136 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050013137
James Smarte29d74f2018-03-05 12:04:07 -080013138 /* setup context3 field to pass wait_queue pointer to wake function */
13139 init_completion(&mbox_done);
13140 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050013141 /* now issue the command */
13142 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050013143 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080013144 wait_for_completion_timeout(&mbox_done,
13145 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040013146
James Smart858c9f62007-06-17 19:56:39 -050013147 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080013148 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040013149 /*
13150 * if LPFC_MBX_WAKE flag is set the mailbox is completed
13151 * else do not free the resources.
13152 */
James Smartd7c47992010-06-08 18:31:54 -040013153 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050013154 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040013155 } else {
James Smart7054a602007-04-25 09:52:34 -040013156 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050013157 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13158 }
13159 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050013160 }
dea31012005-04-17 16:05:31 -050013161 return retval;
13162}
13163
James Smarte59058c2008-08-24 21:49:00 -040013164/**
James Smart3772a992009-05-22 14:50:54 -040013165 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040013166 * @phba: Pointer to HBA context.
Lee Jones7af29d42020-07-21 17:41:31 +010013167 * @mbx_action: Mailbox shutdown options.
James Smarte59058c2008-08-24 21:49:00 -040013168 *
James Smart3772a992009-05-22 14:50:54 -040013169 * This function is called to shutdown the driver's mailbox sub-system.
13170 * It first marks the mailbox sub-system is in a block state to prevent
13171 * the asynchronous mailbox command from issued off the pending mailbox
13172 * command queue. If the mailbox command sub-system shutdown is due to
13173 * HBA error conditions such as EEH or ERATT, this routine shall invoke
13174 * the mailbox sub-system flush routine to forcefully bring down the
13175 * mailbox sub-system. Otherwise, if it is due to normal condition (such
13176 * as with offline or HBA function reset), this routine will wait for the
13177 * outstanding mailbox command to complete before invoking the mailbox
13178 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040013179 **/
James Smart3772a992009-05-22 14:50:54 -040013180void
James Smart618a5232012-06-12 13:54:36 -040013181lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040013182{
James Smart3772a992009-05-22 14:50:54 -040013183 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040013184 unsigned long timeout;
13185
James Smart618a5232012-06-12 13:54:36 -040013186 if (mbx_action == LPFC_MBX_NO_WAIT) {
13187 /* delay 100ms for port state */
13188 msleep(100);
13189 lpfc_sli_mbox_sys_flush(phba);
13190 return;
13191 }
James Smarta183a152011-10-10 21:32:43 -040013192 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050013193
James Smart523128e2018-09-10 10:30:46 -070013194 /* Disable softirqs, including timers from obtaining phba->hbalock */
13195 local_bh_disable();
13196
James Smart3772a992009-05-22 14:50:54 -040013197 spin_lock_irq(&phba->hbalock);
13198 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040013199
13200 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040013201 /* Determine how long we might wait for the active mailbox
13202 * command to be gracefully completed by firmware.
13203 */
James Smarta183a152011-10-10 21:32:43 -040013204 if (phba->sli.mbox_active)
13205 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
13206 phba->sli.mbox_active) *
13207 1000) + jiffies;
13208 spin_unlock_irq(&phba->hbalock);
13209
James Smart523128e2018-09-10 10:30:46 -070013210 /* Enable softirqs again, done with phba->hbalock */
13211 local_bh_enable();
13212
James Smart3772a992009-05-22 14:50:54 -040013213 while (phba->sli.mbox_active) {
13214 /* Check active mailbox complete status every 2ms */
13215 msleep(2);
13216 if (time_after(jiffies, timeout))
13217 /* Timeout, let the mailbox flush routine to
13218 * forcefully release active mailbox command
13219 */
13220 break;
13221 }
James Smart523128e2018-09-10 10:30:46 -070013222 } else {
James Smartd7069f02012-03-01 22:36:29 -050013223 spin_unlock_irq(&phba->hbalock);
13224
James Smart523128e2018-09-10 10:30:46 -070013225 /* Enable softirqs again, done with phba->hbalock */
13226 local_bh_enable();
13227 }
13228
James Smart3772a992009-05-22 14:50:54 -040013229 lpfc_sli_mbox_sys_flush(phba);
13230}
13231
13232/**
13233 * lpfc_sli_eratt_read - read sli-3 error attention events
13234 * @phba: Pointer to HBA context.
13235 *
13236 * This function is called to read the SLI3 device error attention registers
13237 * for possible error attention events. The caller must hold the hostlock
13238 * with spin_lock_irq().
13239 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013240 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040013241 * Register and returns 0 otherwise.
13242 **/
13243static int
13244lpfc_sli_eratt_read(struct lpfc_hba *phba)
13245{
James Smarted957682007-06-17 19:56:37 -050013246 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040013247
James Smart3772a992009-05-22 14:50:54 -040013248 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050013249 if (lpfc_readl(phba->HAregaddr, &ha_copy))
13250 goto unplug_err;
13251
James Smart3772a992009-05-22 14:50:54 -040013252 if (ha_copy & HA_ERATT) {
13253 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050013254 if (lpfc_sli_read_hs(phba))
13255 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040013256
James Smart3772a992009-05-22 14:50:54 -040013257 /* Check if there is a deferred error condition is active */
13258 if ((HS_FFER1 & phba->work_hs) &&
13259 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040013260 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040013261 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040013262 /* Clear all interrupt enable conditions */
13263 writel(0, phba->HCregaddr);
13264 readl(phba->HCregaddr);
13265 }
13266
13267 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040013268 phba->work_ha |= HA_ERATT;
13269 /* Indicate polling handles this ERATT */
13270 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040013271 return 1;
James Smartb4c02652006-07-06 15:50:43 -040013272 }
James Smart3772a992009-05-22 14:50:54 -040013273 return 0;
James Smart9940b972011-03-11 16:06:12 -050013274
13275unplug_err:
13276 /* Set the driver HS work bitmap */
13277 phba->work_hs |= UNPLUG_ERR;
13278 /* Set the driver HA work bitmap */
13279 phba->work_ha |= HA_ERATT;
13280 /* Indicate polling handles this ERATT */
13281 phba->hba_flag |= HBA_ERATT_HANDLED;
13282 return 1;
James Smartb4c02652006-07-06 15:50:43 -040013283}
13284
James Smarte59058c2008-08-24 21:49:00 -040013285/**
James Smartda0436e2009-05-22 14:51:39 -040013286 * lpfc_sli4_eratt_read - read sli-4 error attention events
13287 * @phba: Pointer to HBA context.
13288 *
13289 * This function is called to read the SLI4 device error attention registers
13290 * for possible error attention events. The caller must hold the hostlock
13291 * with spin_lock_irq().
13292 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013293 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040013294 * Register and returns 0 otherwise.
13295 **/
13296static int
13297lpfc_sli4_eratt_read(struct lpfc_hba *phba)
13298{
13299 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050013300 uint32_t if_type, portsmphr;
13301 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040013302
James Smart2fcee4b2010-12-15 17:57:46 -050013303 /*
13304 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040013305 * registers for error attention. This can be changed later.
13306 */
James Smart2fcee4b2010-12-15 17:57:46 -050013307 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
13308 switch (if_type) {
13309 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050013310 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
13311 &uerr_sta_lo) ||
13312 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
13313 &uerr_sta_hi)) {
13314 phba->work_hs |= UNPLUG_ERR;
13315 phba->work_ha |= HA_ERATT;
13316 phba->hba_flag |= HBA_ERATT_HANDLED;
13317 return 1;
13318 }
James Smart2fcee4b2010-12-15 17:57:46 -050013319 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
13320 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013321 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050013322 "1423 HBA Unrecoverable error: "
13323 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
13324 "ue_mask_lo_reg=0x%x, "
13325 "ue_mask_hi_reg=0x%x\n",
13326 uerr_sta_lo, uerr_sta_hi,
13327 phba->sli4_hba.ue_mask_lo,
13328 phba->sli4_hba.ue_mask_hi);
13329 phba->work_status[0] = uerr_sta_lo;
13330 phba->work_status[1] = uerr_sta_hi;
13331 phba->work_ha |= HA_ERATT;
13332 phba->hba_flag |= HBA_ERATT_HANDLED;
13333 return 1;
13334 }
13335 break;
13336 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080013337 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050013338 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
13339 &portstat_reg.word0) ||
13340 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
13341 &portsmphr)){
13342 phba->work_hs |= UNPLUG_ERR;
13343 phba->work_ha |= HA_ERATT;
13344 phba->hba_flag |= HBA_ERATT_HANDLED;
13345 return 1;
13346 }
James Smart2fcee4b2010-12-15 17:57:46 -050013347 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
13348 phba->work_status[0] =
13349 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
13350 phba->work_status[1] =
13351 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
Dick Kennedy372c1872020-06-30 14:50:00 -070013352 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2e90f4b2011-12-13 13:22:37 -050013353 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050013354 "port status reg 0x%x, "
13355 "port smphr reg 0x%x, "
13356 "error 1=0x%x, error 2=0x%x\n",
13357 portstat_reg.word0,
13358 portsmphr,
13359 phba->work_status[0],
13360 phba->work_status[1]);
13361 phba->work_ha |= HA_ERATT;
13362 phba->hba_flag |= HBA_ERATT_HANDLED;
13363 return 1;
13364 }
13365 break;
13366 case LPFC_SLI_INTF_IF_TYPE_1:
13367 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013368 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050013369 "2886 HBA Error Attention on unsupported "
13370 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050013371 return 1;
James Smartda0436e2009-05-22 14:51:39 -040013372 }
James Smart2fcee4b2010-12-15 17:57:46 -050013373
James Smartda0436e2009-05-22 14:51:39 -040013374 return 0;
13375}
13376
13377/**
James Smart3621a712009-04-06 18:47:14 -040013378 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040013379 * @phba: Pointer to HBA context.
13380 *
James Smart3772a992009-05-22 14:50:54 -040013381 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040013382 * error attention register bit for error attention events.
13383 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013384 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040013385 * Register and returns 0 otherwise.
13386 **/
13387int
13388lpfc_sli_check_eratt(struct lpfc_hba *phba)
13389{
13390 uint32_t ha_copy;
13391
13392 /* If somebody is waiting to handle an eratt, don't process it
13393 * here. The brdkill function will do this.
13394 */
13395 if (phba->link_flag & LS_IGNORE_ERATT)
13396 return 0;
13397
13398 /* Check if interrupt handler handles this ERATT */
13399 spin_lock_irq(&phba->hbalock);
13400 if (phba->hba_flag & HBA_ERATT_HANDLED) {
13401 /* Interrupt handler has handled ERATT */
13402 spin_unlock_irq(&phba->hbalock);
13403 return 0;
13404 }
13405
James Smarta257bf92009-04-06 18:48:10 -040013406 /*
13407 * If there is deferred error attention, do not check for error
13408 * attention
13409 */
13410 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
13411 spin_unlock_irq(&phba->hbalock);
13412 return 0;
13413 }
13414
James Smart3772a992009-05-22 14:50:54 -040013415 /* If PCI channel is offline, don't process it */
13416 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040013417 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040013418 return 0;
13419 }
13420
13421 switch (phba->sli_rev) {
13422 case LPFC_SLI_REV2:
13423 case LPFC_SLI_REV3:
13424 /* Read chip Host Attention (HA) register */
13425 ha_copy = lpfc_sli_eratt_read(phba);
13426 break;
James Smartda0436e2009-05-22 14:51:39 -040013427 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050013428 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040013429 ha_copy = lpfc_sli4_eratt_read(phba);
13430 break;
James Smart3772a992009-05-22 14:50:54 -040013431 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013432 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013433 "0299 Invalid SLI revision (%d)\n",
13434 phba->sli_rev);
13435 ha_copy = 0;
13436 break;
James Smart93996272008-08-24 21:50:30 -040013437 }
13438 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040013439
13440 return ha_copy;
13441}
13442
13443/**
13444 * lpfc_intr_state_check - Check device state for interrupt handling
13445 * @phba: Pointer to HBA context.
13446 *
13447 * This inline routine checks whether a device or its PCI slot is in a state
13448 * that the interrupt should be handled.
13449 *
13450 * This function returns 0 if the device or the PCI slot is in a state that
13451 * interrupt should be handled, otherwise -EIO.
13452 */
13453static inline int
13454lpfc_intr_state_check(struct lpfc_hba *phba)
13455{
13456 /* If the pci channel is offline, ignore all the interrupts */
13457 if (unlikely(pci_channel_offline(phba->pcidev)))
13458 return -EIO;
13459
13460 /* Update device level interrupt statistics */
13461 phba->sli.slistat.sli_intr++;
13462
13463 /* Ignore all interrupts during initialization. */
13464 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
13465 return -EIO;
13466
James Smart93996272008-08-24 21:50:30 -040013467 return 0;
13468}
13469
13470/**
James Smart3772a992009-05-22 14:50:54 -040013471 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040013472 * @irq: Interrupt number.
13473 * @dev_id: The device context pointer.
13474 *
James Smart93996272008-08-24 21:50:30 -040013475 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040013476 * service routine when device with SLI-3 interface spec is enabled with
13477 * MSI-X multi-message interrupt mode and there are slow-path events in
13478 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
13479 * interrupt mode, this function is called as part of the device-level
13480 * interrupt handler. When the PCI slot is in error recovery or the HBA
13481 * is undergoing initialization, the interrupt handler will not process
13482 * the interrupt. The link attention and ELS ring attention events are
13483 * handled by the worker thread. The interrupt handler signals the worker
13484 * thread and returns for these events. This function is called without
13485 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040013486 * structures.
13487 *
13488 * This function returns IRQ_HANDLED when interrupt is handled else it
13489 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040013490 **/
dea31012005-04-17 16:05:31 -050013491irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040013492lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050013493{
James Smart2e0fef82007-06-17 19:56:36 -050013494 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050013495 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050013496 uint32_t work_ha_copy;
13497 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050013498 unsigned long iflag;
dea31012005-04-17 16:05:31 -050013499 uint32_t control;
13500
James Smart92d7f7b2007-06-17 19:56:38 -050013501 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050013502 struct lpfc_vport *vport;
13503 struct lpfc_nodelist *ndlp;
13504 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050013505 LPFC_MBOXQ_t *pmb;
13506 int rc;
13507
dea31012005-04-17 16:05:31 -050013508 /*
13509 * Get the driver's phba structure from the dev_id and
13510 * assume the HBA is not interrupting.
13511 */
James Smart93996272008-08-24 21:50:30 -040013512 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050013513
13514 if (unlikely(!phba))
13515 return IRQ_NONE;
13516
dea31012005-04-17 16:05:31 -050013517 /*
James Smart93996272008-08-24 21:50:30 -040013518 * Stuff needs to be attented to when this function is invoked as an
13519 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050013520 */
James Smart93996272008-08-24 21:50:30 -040013521 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040013522 /* Check device state for handling interrupt */
13523 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040013524 return IRQ_NONE;
13525 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050013526 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050013527 if (lpfc_readl(phba->HAregaddr, &ha_copy))
13528 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040013529 /* If somebody is waiting to handle an eratt don't process it
13530 * here. The brdkill function will do this.
13531 */
13532 if (phba->link_flag & LS_IGNORE_ERATT)
13533 ha_copy &= ~HA_ERATT;
13534 /* Check the need for handling ERATT in interrupt handler */
13535 if (ha_copy & HA_ERATT) {
13536 if (phba->hba_flag & HBA_ERATT_HANDLED)
13537 /* ERATT polling has handled ERATT */
13538 ha_copy &= ~HA_ERATT;
13539 else
13540 /* Indicate interrupt handler handles ERATT */
13541 phba->hba_flag |= HBA_ERATT_HANDLED;
13542 }
James Smarta257bf92009-04-06 18:48:10 -040013543
13544 /*
13545 * If there is deferred error attention, do not check for any
13546 * interrupt.
13547 */
13548 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040013549 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040013550 return IRQ_NONE;
13551 }
13552
James Smart93996272008-08-24 21:50:30 -040013553 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050013554 if (lpfc_readl(phba->HCregaddr, &hc_copy))
13555 goto unplug_error;
13556
James Smarta747c9c2009-11-18 15:41:10 -050013557 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
13558 HC_LAINT_ENA | HC_ERINT_ENA),
13559 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040013560 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
13561 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050013562 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040013563 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050013564 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040013565 } else
13566 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050013567
dea31012005-04-17 16:05:31 -050013568 work_ha_copy = ha_copy & phba->work_ha_mask;
13569
James Smart93996272008-08-24 21:50:30 -040013570 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050013571 if (work_ha_copy & HA_LATT) {
13572 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
13573 /*
13574 * Turn off Link Attention interrupts
13575 * until CLEAR_LA done
13576 */
James Smart5b75da22008-12-04 22:39:35 -050013577 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050013578 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050013579 if (lpfc_readl(phba->HCregaddr, &control))
13580 goto unplug_error;
dea31012005-04-17 16:05:31 -050013581 control &= ~HC_LAINT_ENA;
13582 writel(control, phba->HCregaddr);
13583 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050013584 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050013585 }
13586 else
13587 work_ha_copy &= ~HA_LATT;
13588 }
13589
James Smart93996272008-08-24 21:50:30 -040013590 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050013591 /*
13592 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
13593 * the only slow ring.
13594 */
13595 status = (work_ha_copy &
13596 (HA_RXMASK << (4*LPFC_ELS_RING)));
13597 status >>= (4*LPFC_ELS_RING);
13598 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050013599 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050013600 if (lpfc_readl(phba->HCregaddr, &control))
13601 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040013602
13603 lpfc_debugfs_slow_ring_trc(phba,
13604 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
13605 control, status,
13606 (uint32_t)phba->sli.slistat.sli_intr);
13607
James Smart858c9f62007-06-17 19:56:39 -050013608 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040013609 lpfc_debugfs_slow_ring_trc(phba,
13610 "ISR Disable ring:"
13611 "pwork:x%x hawork:x%x wait:x%x",
13612 phba->work_ha, work_ha_copy,
13613 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040013614 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040013615
James Smart858c9f62007-06-17 19:56:39 -050013616 control &=
13617 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050013618 writel(control, phba->HCregaddr);
13619 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050013620 }
James Smarta58cbd52007-08-02 11:09:43 -040013621 else {
13622 lpfc_debugfs_slow_ring_trc(phba,
13623 "ISR slow ring: pwork:"
13624 "x%x hawork:x%x wait:x%x",
13625 phba->work_ha, work_ha_copy,
13626 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040013627 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040013628 }
James Smart5b75da22008-12-04 22:39:35 -050013629 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050013630 }
13631 }
James Smart5b75da22008-12-04 22:39:35 -050013632 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040013633 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050013634 if (lpfc_sli_read_hs(phba))
13635 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040013636 /*
13637 * Check if there is a deferred error condition
13638 * is active
13639 */
13640 if ((HS_FFER1 & phba->work_hs) &&
13641 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040013642 HS_FFER6 | HS_FFER7 | HS_FFER8) &
13643 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040013644 phba->hba_flag |= DEFER_ERATT;
13645 /* Clear all interrupt enable conditions */
13646 writel(0, phba->HCregaddr);
13647 readl(phba->HCregaddr);
13648 }
13649 }
13650
James Smart93996272008-08-24 21:50:30 -040013651 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050013652 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040013653 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040013654 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050013655 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050013656
13657 /* First check out the status word */
13658 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
13659 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050013660 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050013661 /*
13662 * Stray Mailbox Interrupt, mbxCommand <cmd>
13663 * mbxStatus <status>
13664 */
Dick Kennedy372c1872020-06-30 14:50:00 -070013665 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040013666 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050013667 "Interrupt mbxCommand x%x "
13668 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040013669 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050013670 pmbox->mbxCommand,
13671 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050013672 /* clear mailbox attention bit */
13673 work_ha_copy &= ~HA_MBATT;
13674 } else {
James Smart97eab632008-04-07 10:16:05 -040013675 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050013676 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050013677 phba->last_completion_time = jiffies;
13678 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050013679 if (pmb->mbox_cmpl) {
13680 lpfc_sli_pcimem_bcopy(mbox, pmbox,
13681 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040013682 if (pmb->out_ext_byte_len &&
James Smart3e1f0712018-11-29 16:09:29 -080013683 pmb->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -040013684 lpfc_sli_pcimem_bcopy(
13685 phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -080013686 pmb->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -040013687 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050013688 }
James Smart09372822008-01-11 01:52:54 -050013689 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13690 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13691
13692 lpfc_debugfs_disc_trc(vport,
13693 LPFC_DISC_TRC_MBOX_VPORT,
13694 "MBOX dflt rpi: : "
13695 "status:x%x rpi:x%x",
13696 (uint32_t)pmbox->mbxStatus,
13697 pmbox->un.varWords[0], 0);
13698
13699 if (!pmbox->mbxStatus) {
13700 mp = (struct lpfc_dmabuf *)
James Smart3e1f0712018-11-29 16:09:29 -080013701 (pmb->ctx_buf);
James Smart09372822008-01-11 01:52:54 -050013702 ndlp = (struct lpfc_nodelist *)
James Smart3e1f0712018-11-29 16:09:29 -080013703 pmb->ctx_ndlp;
James Smart09372822008-01-11 01:52:54 -050013704
13705 /* Reg_LOGIN of dflt RPI was
13706 * successful. new lets get
13707 * rid of the RPI using the
13708 * same mbox buffer.
13709 */
13710 lpfc_unreg_login(phba,
13711 vport->vpi,
13712 pmbox->un.varWords[0],
13713 pmb);
13714 pmb->mbox_cmpl =
13715 lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080013716 pmb->ctx_buf = mp;
13717 pmb->ctx_ndlp = ndlp;
James Smart09372822008-01-11 01:52:54 -050013718 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040013719 rc = lpfc_sli_issue_mbox(phba,
13720 pmb,
13721 MBX_NOWAIT);
13722 if (rc != MBX_BUSY)
13723 lpfc_printf_log(phba,
13724 KERN_ERR,
Dick Kennedy372c1872020-06-30 14:50:00 -070013725 LOG_TRACE_EVENT,
James Smartd7c255b2008-08-24 21:50:00 -040013726 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040013727 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040013728 if (rc != MBX_NOT_FINISHED)
13729 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050013730 }
13731 }
James Smart5b75da22008-12-04 22:39:35 -050013732 spin_lock_irqsave(
13733 &phba->pport->work_port_lock,
13734 iflag);
James Smart09372822008-01-11 01:52:54 -050013735 phba->pport->work_port_events &=
13736 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050013737 spin_unlock_irqrestore(
13738 &phba->pport->work_port_lock,
13739 iflag);
James Smarta22d73b2021-01-04 10:02:38 -080013740
13741 /* Do NOT queue MBX_HEARTBEAT to the worker
13742 * thread for processing.
13743 */
13744 if (pmbox->mbxCommand == MBX_HEARTBEAT) {
13745 /* Process mbox now */
13746 phba->sli.mbox_active = NULL;
13747 phba->sli.sli_flag &=
13748 ~LPFC_SLI_MBOX_ACTIVE;
13749 if (pmb->mbox_cmpl)
13750 pmb->mbox_cmpl(phba, pmb);
13751 } else {
13752 /* Queue to worker thread to process */
13753 lpfc_mbox_cmpl_put(phba, pmb);
13754 }
James Smart858c9f62007-06-17 19:56:39 -050013755 }
James Smart97eab632008-04-07 10:16:05 -040013756 } else
James Smart5b75da22008-12-04 22:39:35 -050013757 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040013758
James Smart92d7f7b2007-06-17 19:56:38 -050013759 if ((work_ha_copy & HA_MBATT) &&
13760 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050013761send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050013762 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040013763 do {
13764 rc = lpfc_sli_issue_mbox(phba, NULL,
13765 MBX_NOWAIT);
13766 } while (rc == MBX_NOT_FINISHED);
13767 if (rc != MBX_SUCCESS)
Dick Kennedy372c1872020-06-30 14:50:00 -070013768 lpfc_printf_log(phba, KERN_ERR,
13769 LOG_TRACE_EVENT,
13770 "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040013771 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050013772 }
13773
James Smart5b75da22008-12-04 22:39:35 -050013774 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050013775 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050013776 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040013777 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050013778 }
James Smart93996272008-08-24 21:50:30 -040013779 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050013780unplug_error:
13781 spin_unlock_irqrestore(&phba->hbalock, iflag);
13782 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050013783
James Smart3772a992009-05-22 14:50:54 -040013784} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040013785
13786/**
James Smart3772a992009-05-22 14:50:54 -040013787 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040013788 * @irq: Interrupt number.
13789 * @dev_id: The device context pointer.
13790 *
13791 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040013792 * service routine when device with SLI-3 interface spec is enabled with
13793 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13794 * ring event in the HBA. However, when the device is enabled with either
13795 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13796 * device-level interrupt handler. When the PCI slot is in error recovery
13797 * or the HBA is undergoing initialization, the interrupt handler will not
13798 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13799 * the intrrupt context. This function is called without any lock held.
13800 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040013801 *
13802 * This function returns IRQ_HANDLED when interrupt is handled else it
13803 * returns IRQ_NONE.
13804 **/
13805irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040013806lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040013807{
13808 struct lpfc_hba *phba;
13809 uint32_t ha_copy;
13810 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050013811 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080013812 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040013813
13814 /* Get the driver's phba structure from the dev_id and
13815 * assume the HBA is not interrupting.
13816 */
13817 phba = (struct lpfc_hba *) dev_id;
13818
13819 if (unlikely(!phba))
13820 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050013821
13822 /*
James Smart93996272008-08-24 21:50:30 -040013823 * Stuff needs to be attented to when this function is invoked as an
13824 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050013825 */
James Smart93996272008-08-24 21:50:30 -040013826 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040013827 /* Check device state for handling interrupt */
13828 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040013829 return IRQ_NONE;
13830 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050013831 if (lpfc_readl(phba->HAregaddr, &ha_copy))
13832 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040013833 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050013834 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040013835 /*
13836 * If there is deferred error attention, do not check for
13837 * any interrupt.
13838 */
13839 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040013840 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040013841 return IRQ_NONE;
13842 }
James Smart93996272008-08-24 21:50:30 -040013843 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
13844 phba->HAregaddr);
13845 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050013846 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040013847 } else
13848 ha_copy = phba->ha_copy;
13849
13850 /*
13851 * Process all events on FCP ring. Take the optimized path for FCP IO.
13852 */
13853 ha_copy &= ~(phba->work_ha_mask);
13854
13855 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050013856 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080013857 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050013858 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080013859 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050013860
13861 if (phba->cfg_multi_ring_support == 2) {
13862 /*
James Smart93996272008-08-24 21:50:30 -040013863 * Process all events on extra ring. Take the optimized path
13864 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050013865 */
James Smart93996272008-08-24 21:50:30 -040013866 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050013867 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050013868 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050013869 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080013870 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050013871 status);
13872 }
13873 }
dea31012005-04-17 16:05:31 -050013874 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040013875} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050013876
James Smart93996272008-08-24 21:50:30 -040013877/**
James Smart3772a992009-05-22 14:50:54 -040013878 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040013879 * @irq: Interrupt number.
13880 * @dev_id: The device context pointer.
13881 *
James Smart3772a992009-05-22 14:50:54 -040013882 * This function is the HBA device-level interrupt handler to device with
13883 * SLI-3 interface spec, called from the PCI layer when either MSI or
13884 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
13885 * requires driver attention. This function invokes the slow-path interrupt
13886 * attention handling function and fast-path interrupt attention handling
13887 * function in turn to process the relevant HBA attention events. This
13888 * function is called without any lock held. It gets the hbalock to access
13889 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040013890 *
13891 * This function returns IRQ_HANDLED when interrupt is handled, else it
13892 * returns IRQ_NONE.
13893 **/
13894irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040013895lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040013896{
13897 struct lpfc_hba *phba;
13898 irqreturn_t sp_irq_rc, fp_irq_rc;
13899 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050013900 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040013901
13902 /*
13903 * Get the driver's phba structure from the dev_id and
13904 * assume the HBA is not interrupting.
13905 */
13906 phba = (struct lpfc_hba *) dev_id;
13907
13908 if (unlikely(!phba))
13909 return IRQ_NONE;
13910
James Smart3772a992009-05-22 14:50:54 -040013911 /* Check device state for handling interrupt */
13912 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040013913 return IRQ_NONE;
13914
13915 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050013916 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
13917 spin_unlock(&phba->hbalock);
13918 return IRQ_HANDLED;
13919 }
13920
James Smart93996272008-08-24 21:50:30 -040013921 if (unlikely(!phba->ha_copy)) {
13922 spin_unlock(&phba->hbalock);
13923 return IRQ_NONE;
13924 } else if (phba->ha_copy & HA_ERATT) {
13925 if (phba->hba_flag & HBA_ERATT_HANDLED)
13926 /* ERATT polling has handled ERATT */
13927 phba->ha_copy &= ~HA_ERATT;
13928 else
13929 /* Indicate interrupt handler handles ERATT */
13930 phba->hba_flag |= HBA_ERATT_HANDLED;
13931 }
13932
James Smarta257bf92009-04-06 18:48:10 -040013933 /*
13934 * If there is deferred error attention, do not check for any interrupt.
13935 */
13936 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020013937 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040013938 return IRQ_NONE;
13939 }
13940
James Smart93996272008-08-24 21:50:30 -040013941 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050013942 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
13943 spin_unlock(&phba->hbalock);
13944 return IRQ_HANDLED;
13945 }
James Smarta747c9c2009-11-18 15:41:10 -050013946 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
13947 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
13948 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040013949 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050013950 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040013951 readl(phba->HAregaddr); /* flush */
13952 spin_unlock(&phba->hbalock);
13953
13954 /*
13955 * Invokes slow-path host attention interrupt handling as appropriate.
13956 */
13957
13958 /* status of events with mailbox and link attention */
13959 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
13960
13961 /* status of events with ELS ring */
13962 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
13963 status2 >>= (4*LPFC_ELS_RING);
13964
13965 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040013966 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040013967 else
13968 sp_irq_rc = IRQ_NONE;
13969
13970 /*
13971 * Invoke fast-path host attention interrupt handling as appropriate.
13972 */
13973
13974 /* status of events with FCP ring */
13975 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13976 status1 >>= (4*LPFC_FCP_RING);
13977
13978 /* status of events with extra ring */
13979 if (phba->cfg_multi_ring_support == 2) {
13980 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
13981 status2 >>= (4*LPFC_EXTRA_RING);
13982 } else
13983 status2 = 0;
13984
13985 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040013986 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040013987 else
13988 fp_irq_rc = IRQ_NONE;
13989
13990 /* Return device-level interrupt handling status */
13991 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040013992} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040013993
13994/**
James Smart4f774512009-05-22 14:52:35 -040013995 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
13996 * @phba: pointer to lpfc hba data structure.
13997 *
13998 * This routine is invoked by the worker thread to process all the pending
13999 * SLI4 els abort xri events.
14000 **/
14001void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
14002{
14003 struct lpfc_cq_event *cq_event;
James Smarte7dab162020-10-20 13:27:12 -070014004 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -040014005
14006 /* First, declare the els xri abort event has been handled */
James Smarte7dab162020-10-20 13:27:12 -070014007 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040014008 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
James Smarte7dab162020-10-20 13:27:12 -070014009 spin_unlock_irqrestore(&phba->hbalock, iflags);
14010
James Smart4f774512009-05-22 14:52:35 -040014011 /* Now, handle all the els xri abort events */
James Smarte7dab162020-10-20 13:27:12 -070014012 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040014013 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
14014 /* Get the first event from the head of the event queue */
James Smart4f774512009-05-22 14:52:35 -040014015 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
14016 cq_event, struct lpfc_cq_event, list);
James Smarte7dab162020-10-20 13:27:12 -070014017 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14018 iflags);
James Smart4f774512009-05-22 14:52:35 -040014019 /* Notify aborted XRI for ELS work queue */
14020 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
James Smarte7dab162020-10-20 13:27:12 -070014021
James Smart4f774512009-05-22 14:52:35 -040014022 /* Free the event processed back to the free pool */
14023 lpfc_sli4_cq_event_release(phba, cq_event);
James Smarte7dab162020-10-20 13:27:12 -070014024 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14025 iflags);
James Smart4f774512009-05-22 14:52:35 -040014026 }
James Smarte7dab162020-10-20 13:27:12 -070014027 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040014028}
14029
James Smart341af102010-01-26 23:07:37 -050014030/**
14031 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
14032 * @phba: pointer to lpfc hba data structure
14033 * @pIocbIn: pointer to the rspiocbq
14034 * @pIocbOut: pointer to the cmdiocbq
14035 * @wcqe: pointer to the complete wcqe
14036 *
14037 * This routine transfers the fields of a command iocbq to a response iocbq
14038 * by copying all the IOCB fields from command iocbq and transferring the
14039 * completion status information from the complete wcqe.
14040 **/
James Smart4f774512009-05-22 14:52:35 -040014041static void
James Smart341af102010-01-26 23:07:37 -050014042lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
14043 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040014044 struct lpfc_iocbq *pIocbOut,
14045 struct lpfc_wcqe_complete *wcqe)
14046{
James Smartaf227412013-10-10 12:23:10 -040014047 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050014048 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040014049 uint32_t status, max_response;
14050 struct lpfc_dmabuf *dmabuf;
14051 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040014052 size_t offset = offsetof(struct lpfc_iocbq, iocb);
14053
14054 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
14055 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040014056 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050014057 status = bf_get(lpfc_wcqe_c_status, wcqe);
14058 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040014059 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
14060 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
14061 pIocbIn->iocb.un.fcpi.fcpi_parm =
14062 pIocbOut->iocb.un.fcpi.fcpi_parm -
14063 wcqe->total_data_placed;
14064 else
14065 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050014066 else {
James Smart4f774512009-05-22 14:52:35 -040014067 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040014068 switch (pIocbOut->iocb.ulpCommand) {
14069 case CMD_ELS_REQUEST64_CR:
14070 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
14071 bpl = (struct ulp_bde64 *)dmabuf->virt;
14072 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
14073 max_response = bde.tus.f.bdeSize;
14074 break;
14075 case CMD_GEN_REQUEST64_CR:
14076 max_response = 0;
14077 if (!pIocbOut->context3)
14078 break;
14079 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
14080 sizeof(struct ulp_bde64);
14081 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
14082 bpl = (struct ulp_bde64 *)dmabuf->virt;
14083 for (i = 0; i < numBdes; i++) {
14084 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
14085 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
14086 max_response += bde.tus.f.bdeSize;
14087 }
14088 break;
14089 default:
14090 max_response = wcqe->total_data_placed;
14091 break;
14092 }
14093 if (max_response < wcqe->total_data_placed)
14094 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
14095 else
14096 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
14097 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050014098 }
James Smart341af102010-01-26 23:07:37 -050014099
James Smartacd68592012-01-18 16:25:09 -050014100 /* Convert BG errors for completion status */
14101 if (status == CQE_STATUS_DI_ERROR) {
14102 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
14103
14104 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
14105 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
14106 else
14107 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
14108
14109 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
14110 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
14111 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14112 BGS_GUARD_ERR_MASK;
14113 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
14114 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14115 BGS_APPTAG_ERR_MASK;
14116 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
14117 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14118 BGS_REFTAG_ERR_MASK;
14119
14120 /* Check to see if there was any good data before the error */
14121 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
14122 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14123 BGS_HI_WATER_MARK_PRESENT_MASK;
14124 pIocbIn->iocb.unsli3.sli3_bg.bghm =
14125 wcqe->total_data_placed;
14126 }
14127
14128 /*
14129 * Set ALL the error bits to indicate we don't know what
14130 * type of error it is.
14131 */
14132 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
14133 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14134 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
14135 BGS_GUARD_ERR_MASK);
14136 }
14137
James Smart341af102010-01-26 23:07:37 -050014138 /* Pick up HBA exchange busy condition */
14139 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
14140 spin_lock_irqsave(&phba->hbalock, iflags);
14141 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
14142 spin_unlock_irqrestore(&phba->hbalock, iflags);
14143 }
James Smart4f774512009-05-22 14:52:35 -040014144}
14145
14146/**
James Smart45ed1192009-10-02 15:17:02 -040014147 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
14148 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010014149 * @irspiocbq: Pointer to work-queue completion queue entry.
James Smart45ed1192009-10-02 15:17:02 -040014150 *
14151 * This routine handles an ELS work-queue completion event and construct
14152 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
14153 * discovery engine to handle.
14154 *
14155 * Return: Pointer to the receive IOCBQ, NULL otherwise.
14156 **/
14157static struct lpfc_iocbq *
14158lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
14159 struct lpfc_iocbq *irspiocbq)
14160{
James Smart895427b2017-02-12 13:52:30 -080014161 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040014162 struct lpfc_iocbq *cmdiocbq;
14163 struct lpfc_wcqe_complete *wcqe;
14164 unsigned long iflags;
14165
James Smart895427b2017-02-12 13:52:30 -080014166 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070014167 if (unlikely(!pring))
14168 return NULL;
James Smart895427b2017-02-12 13:52:30 -080014169
James Smart45ed1192009-10-02 15:17:02 -040014170 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart45ed1192009-10-02 15:17:02 -040014171 pring->stats.iocb_event++;
14172 /* Look up the ELS command IOCB and create pseudo response IOCB */
14173 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
14174 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040014175 if (unlikely(!cmdiocbq)) {
14176 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14177 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070014178 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
14179 wcqe->word0, wcqe->total_data_placed,
14180 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040014181 lpfc_sli_release_iocbq(phba, irspiocbq);
14182 return NULL;
14183 }
14184
James Smarte2a8be52019-05-06 17:26:47 -070014185 spin_lock_irqsave(&pring->ring_lock, iflags);
Dick Kennedy401bb412017-09-29 17:34:28 -070014186 /* Put the iocb back on the txcmplq */
14187 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
14188 spin_unlock_irqrestore(&pring->ring_lock, iflags);
14189
James Smart45ed1192009-10-02 15:17:02 -040014190 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050014191 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040014192
14193 return irspiocbq;
14194}
14195
James Smart8a5ca102017-11-20 16:00:30 -080014196inline struct lpfc_cq_event *
14197lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
14198{
14199 struct lpfc_cq_event *cq_event;
14200
14201 /* Allocate a new internal CQ_EVENT entry */
14202 cq_event = lpfc_sli4_cq_event_alloc(phba);
14203 if (!cq_event) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014204 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8a5ca102017-11-20 16:00:30 -080014205 "0602 Failed to alloc CQ_EVENT entry\n");
14206 return NULL;
14207 }
14208
14209 /* Move the CQE into the event */
14210 memcpy(&cq_event->cqe, entry, size);
14211 return cq_event;
14212}
14213
James Smart45ed1192009-10-02 15:17:02 -040014214/**
Colin Ian King291c2542019-12-18 08:43:01 +000014215 * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
James Smart04c68492009-05-22 14:52:52 -040014216 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010014217 * @mcqe: Pointer to mailbox completion queue entry.
James Smart04c68492009-05-22 14:52:52 -040014218 *
Colin Ian King291c2542019-12-18 08:43:01 +000014219 * This routine process a mailbox completion queue entry with asynchronous
James Smart04c68492009-05-22 14:52:52 -040014220 * event.
14221 *
14222 * Return: true if work posted to worker thread, otherwise false.
14223 **/
14224static bool
14225lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14226{
14227 struct lpfc_cq_event *cq_event;
14228 unsigned long iflags;
14229
14230 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14231 "0392 Async Event: word0:x%x, word1:x%x, "
14232 "word2:x%x, word3:x%x\n", mcqe->word0,
14233 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
14234
James Smart8a5ca102017-11-20 16:00:30 -080014235 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
14236 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040014237 return false;
James Smarte7dab162020-10-20 13:27:12 -070014238
14239 spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
James Smart04c68492009-05-22 14:52:52 -040014240 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
James Smarte7dab162020-10-20 13:27:12 -070014241 spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
14242
James Smart04c68492009-05-22 14:52:52 -040014243 /* Set the async event flag */
James Smarte7dab162020-10-20 13:27:12 -070014244 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart04c68492009-05-22 14:52:52 -040014245 phba->hba_flag |= ASYNC_EVENT;
14246 spin_unlock_irqrestore(&phba->hbalock, iflags);
14247
14248 return true;
14249}
14250
14251/**
14252 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
14253 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010014254 * @mcqe: Pointer to mailbox completion queue entry.
James Smart04c68492009-05-22 14:52:52 -040014255 *
14256 * This routine process a mailbox completion queue entry with mailbox
14257 * completion event.
14258 *
14259 * Return: true if work posted to worker thread, otherwise false.
14260 **/
14261static bool
14262lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14263{
14264 uint32_t mcqe_status;
14265 MAILBOX_t *mbox, *pmbox;
14266 struct lpfc_mqe *mqe;
14267 struct lpfc_vport *vport;
14268 struct lpfc_nodelist *ndlp;
14269 struct lpfc_dmabuf *mp;
14270 unsigned long iflags;
14271 LPFC_MBOXQ_t *pmb;
14272 bool workposted = false;
14273 int rc;
14274
14275 /* If not a mailbox complete MCQE, out by checking mailbox consume */
14276 if (!bf_get(lpfc_trailer_completed, mcqe))
14277 goto out_no_mqe_complete;
14278
14279 /* Get the reference to the active mbox command */
14280 spin_lock_irqsave(&phba->hbalock, iflags);
14281 pmb = phba->sli.mbox_active;
14282 if (unlikely(!pmb)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014283 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart04c68492009-05-22 14:52:52 -040014284 "1832 No pending MBOX command to handle\n");
14285 spin_unlock_irqrestore(&phba->hbalock, iflags);
14286 goto out_no_mqe_complete;
14287 }
14288 spin_unlock_irqrestore(&phba->hbalock, iflags);
14289 mqe = &pmb->u.mqe;
14290 pmbox = (MAILBOX_t *)&pmb->u.mqe;
14291 mbox = phba->mbox;
14292 vport = pmb->vport;
14293
14294 /* Reset heartbeat timer */
14295 phba->last_completion_time = jiffies;
14296 del_timer(&phba->sli.mbox_tmo);
14297
14298 /* Move mbox data to caller's mailbox region, do endian swapping */
14299 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070014300 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040014301
James Smart73d91e52011-10-10 21:32:10 -040014302 /*
14303 * For mcqe errors, conditionally move a modified error code to
14304 * the mbox so that the error will not be missed.
14305 */
14306 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
14307 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
14308 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
14309 bf_set(lpfc_mqe_status, mqe,
14310 (LPFC_MBX_ERROR_RANGE | mcqe_status));
14311 }
James Smart04c68492009-05-22 14:52:52 -040014312 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
14313 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
14314 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
14315 "MBOX dflt rpi: status:x%x rpi:x%x",
14316 mcqe_status,
14317 pmbox->un.varWords[0], 0);
14318 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
James Smart3e1f0712018-11-29 16:09:29 -080014319 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
14320 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart01131e72021-05-14 12:55:49 -070014321
14322 /* Reg_LOGIN of dflt RPI was successful. Mark the
14323 * node as having an UNREG_LOGIN in progress to stop
14324 * an unsolicited PLOGI from the same NPortId from
14325 * starting another mailbox transaction.
James Smart04c68492009-05-22 14:52:52 -040014326 */
James Smart01131e72021-05-14 12:55:49 -070014327 spin_lock_irqsave(&ndlp->lock, iflags);
14328 ndlp->nlp_flag |= NLP_UNREG_INP;
14329 spin_unlock_irqrestore(&ndlp->lock, iflags);
James Smart04c68492009-05-22 14:52:52 -040014330 lpfc_unreg_login(phba, vport->vpi,
14331 pmbox->un.varWords[0], pmb);
14332 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080014333 pmb->ctx_buf = mp;
James Smarte9b11082020-11-15 11:26:33 -080014334
14335 /* No reference taken here. This is a default
14336 * RPI reg/immediate unreg cycle. The reference was
14337 * taken in the reg rpi path and is released when
14338 * this mailbox completes.
14339 */
James Smart3e1f0712018-11-29 16:09:29 -080014340 pmb->ctx_ndlp = ndlp;
James Smart04c68492009-05-22 14:52:52 -040014341 pmb->vport = vport;
14342 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
14343 if (rc != MBX_BUSY)
Dick Kennedy372c1872020-06-30 14:50:00 -070014344 lpfc_printf_log(phba, KERN_ERR,
14345 LOG_TRACE_EVENT,
14346 "0385 rc should "
James Smart04c68492009-05-22 14:52:52 -040014347 "have been MBX_BUSY\n");
14348 if (rc != MBX_NOT_FINISHED)
14349 goto send_current_mbox;
14350 }
14351 }
14352 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
14353 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
14354 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
14355
James Smarta22d73b2021-01-04 10:02:38 -080014356 /* Do NOT queue MBX_HEARTBEAT to the worker thread for processing. */
14357 if (pmbox->mbxCommand == MBX_HEARTBEAT) {
14358 spin_lock_irqsave(&phba->hbalock, iflags);
14359 /* Release the mailbox command posting token */
14360 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14361 phba->sli.mbox_active = NULL;
14362 if (bf_get(lpfc_trailer_consumed, mcqe))
14363 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14364 spin_unlock_irqrestore(&phba->hbalock, iflags);
14365
14366 /* Post the next mbox command, if there is one */
14367 lpfc_sli4_post_async_mbox(phba);
14368
14369 /* Process cmpl now */
14370 if (pmb->mbox_cmpl)
14371 pmb->mbox_cmpl(phba, pmb);
14372 return false;
14373 }
14374
14375 /* There is mailbox completion work to queue to the worker thread */
James Smart04c68492009-05-22 14:52:52 -040014376 spin_lock_irqsave(&phba->hbalock, iflags);
14377 __lpfc_mbox_cmpl_put(phba, pmb);
14378 phba->work_ha |= HA_MBATT;
14379 spin_unlock_irqrestore(&phba->hbalock, iflags);
14380 workposted = true;
14381
14382send_current_mbox:
14383 spin_lock_irqsave(&phba->hbalock, iflags);
14384 /* Release the mailbox command posting token */
14385 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14386 /* Setting active mailbox pointer need to be in sync to flag clear */
14387 phba->sli.mbox_active = NULL;
James Smart07b85822019-09-21 20:58:53 -070014388 if (bf_get(lpfc_trailer_consumed, mcqe))
14389 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
James Smart04c68492009-05-22 14:52:52 -040014390 spin_unlock_irqrestore(&phba->hbalock, iflags);
14391 /* Wake up worker thread to post the next pending mailbox command */
14392 lpfc_worker_wake_up(phba);
James Smart07b85822019-09-21 20:58:53 -070014393 return workposted;
14394
James Smart04c68492009-05-22 14:52:52 -040014395out_no_mqe_complete:
James Smart07b85822019-09-21 20:58:53 -070014396 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart04c68492009-05-22 14:52:52 -040014397 if (bf_get(lpfc_trailer_consumed, mcqe))
14398 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
James Smart07b85822019-09-21 20:58:53 -070014399 spin_unlock_irqrestore(&phba->hbalock, iflags);
14400 return false;
James Smart04c68492009-05-22 14:52:52 -040014401}
14402
14403/**
14404 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
14405 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010014406 * @cq: Pointer to associated CQ
James Smart04c68492009-05-22 14:52:52 -040014407 * @cqe: Pointer to mailbox completion queue entry.
14408 *
14409 * This routine process a mailbox completion queue entry, it invokes the
Colin Ian King291c2542019-12-18 08:43:01 +000014410 * proper mailbox complete handling or asynchronous event handling routine
James Smart04c68492009-05-22 14:52:52 -040014411 * according to the MCQE's async bit.
14412 *
14413 * Return: true if work posted to worker thread, otherwise false.
14414 **/
14415static bool
James Smart32517fc2019-01-28 11:14:33 -080014416lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14417 struct lpfc_cqe *cqe)
James Smart04c68492009-05-22 14:52:52 -040014418{
14419 struct lpfc_mcqe mcqe;
14420 bool workposted;
14421
James Smart32517fc2019-01-28 11:14:33 -080014422 cq->CQ_mbox++;
14423
James Smart04c68492009-05-22 14:52:52 -040014424 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070014425 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040014426
14427 /* Invoke the proper event handling routine */
14428 if (!bf_get(lpfc_trailer_async, &mcqe))
14429 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
14430 else
14431 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
14432 return workposted;
14433}
14434
14435/**
James Smart4f774512009-05-22 14:52:35 -040014436 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
14437 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040014438 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040014439 * @wcqe: Pointer to work-queue completion queue entry.
14440 *
14441 * This routine handles an ELS work-queue completion event.
14442 *
14443 * Return: true if work posted to worker thread, otherwise false.
14444 **/
14445static bool
James Smart2a76a282012-08-03 12:35:54 -040014446lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040014447 struct lpfc_wcqe_complete *wcqe)
14448{
James Smart4f774512009-05-22 14:52:35 -040014449 struct lpfc_iocbq *irspiocbq;
14450 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040014451 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050014452 int txq_cnt = 0;
14453 int txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040014454
James Smart11f0e342018-05-04 20:37:57 -070014455 /* Check for response status */
14456 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14457 /* Log the error status */
14458 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14459 "0357 ELS CQE error: status=x%x: "
14460 "CQE: %08x %08x %08x %08x\n",
14461 bf_get(lpfc_wcqe_c_status, wcqe),
14462 wcqe->word0, wcqe->total_data_placed,
14463 wcqe->parameter, wcqe->word3);
14464 }
14465
James Smart45ed1192009-10-02 15:17:02 -040014466 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040014467 irspiocbq = lpfc_sli_get_iocbq(phba);
14468 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050014469 if (!list_empty(&pring->txq))
14470 txq_cnt++;
14471 if (!list_empty(&pring->txcmplq))
14472 txcmplq_cnt++;
Dick Kennedy372c1872020-06-30 14:50:00 -070014473 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2a9bf3d2010-06-07 15:24:45 -040014474 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
James Smartff349bc2019-09-21 20:59:05 -070014475 "els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050014476 txq_cnt, phba->iocb_cnt,
James Smart0e9bb8d2013-03-01 16:35:12 -050014477 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040014478 return false;
James Smart4f774512009-05-22 14:52:35 -040014479 }
James Smart4f774512009-05-22 14:52:35 -040014480
James Smart45ed1192009-10-02 15:17:02 -040014481 /* Save off the slow-path queue event for work thread to process */
14482 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040014483 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040014484 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040014485 &phba->sli4_hba.sp_queue_event);
14486 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040014487 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040014488
James Smart45ed1192009-10-02 15:17:02 -040014489 return true;
James Smart4f774512009-05-22 14:52:35 -040014490}
14491
14492/**
14493 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
14494 * @phba: Pointer to HBA context object.
14495 * @wcqe: Pointer to work-queue completion queue entry.
14496 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080014497 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040014498 * proper WQ release routine to the slow-path WQ.
14499 **/
14500static void
14501lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
14502 struct lpfc_wcqe_release *wcqe)
14503{
James Smart2e90f4b2011-12-13 13:22:37 -050014504 /* sanity check on queue memory */
14505 if (unlikely(!phba->sli4_hba.els_wq))
14506 return;
James Smart4f774512009-05-22 14:52:35 -040014507 /* Check for the slow-path ELS work queue */
14508 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
14509 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
14510 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
14511 else
14512 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14513 "2579 Slow-path wqe consume event carries "
14514 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
14515 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
14516 phba->sli4_hba.els_wq->queue_id);
14517}
14518
14519/**
14520 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
14521 * @phba: Pointer to HBA context object.
14522 * @cq: Pointer to a WQ completion queue.
14523 * @wcqe: Pointer to work-queue completion queue entry.
14524 *
14525 * This routine handles an XRI abort event.
14526 *
14527 * Return: true if work posted to worker thread, otherwise false.
14528 **/
14529static bool
14530lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
14531 struct lpfc_queue *cq,
14532 struct sli4_wcqe_xri_aborted *wcqe)
14533{
14534 bool workposted = false;
14535 struct lpfc_cq_event *cq_event;
14536 unsigned long iflags;
14537
James Smart4f774512009-05-22 14:52:35 -040014538 switch (cq->subtype) {
James Smartc00f62e2019-08-14 16:57:11 -070014539 case LPFC_IO:
14540 lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
14541 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
14542 /* Notify aborted XRI for NVME work queue */
14543 if (phba->nvmet_support)
14544 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
14545 }
James Smart5e5b5112019-01-28 11:14:22 -080014546 workposted = false;
James Smart4f774512009-05-22 14:52:35 -040014547 break;
James Smart422c4cb2017-11-20 16:00:32 -080014548 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040014549 case LPFC_ELS:
James Smarte7dab162020-10-20 13:27:12 -070014550 cq_event = lpfc_cq_event_setup(phba, wcqe, sizeof(*wcqe));
14551 if (!cq_event) {
14552 workposted = false;
14553 break;
14554 }
James Smart5e5b5112019-01-28 11:14:22 -080014555 cq_event->hdwq = cq->hdwq;
James Smarte7dab162020-10-20 13:27:12 -070014556 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14557 iflags);
James Smart4f774512009-05-22 14:52:35 -040014558 list_add_tail(&cq_event->list,
14559 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
14560 /* Set the els xri abort event flag */
14561 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
James Smarte7dab162020-10-20 13:27:12 -070014562 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14563 iflags);
James Smart4f774512009-05-22 14:52:35 -040014564 workposted = true;
14565 break;
14566 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070014567 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart318083a2017-03-04 09:30:30 -080014568 "0603 Invalid CQ subtype %d: "
14569 "%08x %08x %08x %08x\n",
14570 cq->subtype, wcqe->word0, wcqe->parameter,
14571 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040014572 workposted = false;
14573 break;
14574 }
14575 return workposted;
14576}
14577
James Smarte817e5d2018-12-13 15:17:53 -080014578#define FC_RCTL_MDS_DIAGS 0xF4
14579
James Smart4f774512009-05-22 14:52:35 -040014580/**
James Smart4d9ab992009-10-02 15:16:39 -040014581 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040014582 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040014583 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040014584 *
James Smart4d9ab992009-10-02 15:16:39 -040014585 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040014586 *
14587 * Return: true if work posted to worker thread, otherwise false.
14588 **/
14589static bool
James Smart4d9ab992009-10-02 15:16:39 -040014590lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
14591{
14592 bool workposted = false;
James Smarte817e5d2018-12-13 15:17:53 -080014593 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040014594 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
14595 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070014596 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040014597 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040014598 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040014599 unsigned long iflags;
14600
James Smart2e90f4b2011-12-13 13:22:37 -050014601 /* sanity check on queue memory */
14602 if (unlikely(!hrq) || unlikely(!drq))
14603 return workposted;
14604
James Smart7851fe22011-07-22 18:36:52 -040014605 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14606 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14607 else
14608 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14609 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040014610 goto out;
14611
14612 status = bf_get(lpfc_rcqe_status, rcqe);
14613 switch (status) {
14614 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
Dick Kennedy372c1872020-06-30 14:50:00 -070014615 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4d9ab992009-10-02 15:16:39 -040014616 "2537 Receive Frame Truncated!!\n");
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050014617 fallthrough;
James Smart4d9ab992009-10-02 15:16:39 -040014618 case FC_STATUS_RQ_SUCCESS:
14619 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080014620 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040014621 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14622 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040014623 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040014624 spin_unlock_irqrestore(&phba->hbalock, iflags);
14625 goto out;
14626 }
James Smartb84daac2012-08-03 12:35:13 -040014627 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070014628 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040014629 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080014630
James Smarte817e5d2018-12-13 15:17:53 -080014631 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14632
14633 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
14634 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
14635 spin_unlock_irqrestore(&phba->hbalock, iflags);
14636 /* Handle MDS Loopback frames */
Dick Kennedy24411fc2020-08-03 14:02:25 -070014637 if (!(phba->pport->load_flag & FC_UNLOADING))
14638 lpfc_sli4_handle_mds_loopback(phba->pport,
14639 dma_buf);
14640 else
14641 lpfc_in_buf_free(phba, &dma_buf->dbuf);
James Smarte817e5d2018-12-13 15:17:53 -080014642 break;
14643 }
14644
14645 /* save off the frame for the work thread to process */
James Smart4d9ab992009-10-02 15:16:39 -040014646 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040014647 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040014648 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040014649 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040014650 spin_unlock_irqrestore(&phba->hbalock, iflags);
14651 workposted = true;
14652 break;
James Smart4d9ab992009-10-02 15:16:39 -040014653 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070014654 if (phba->nvmet_support) {
14655 tgtp = phba->targetport->private;
Dick Kennedy372c1872020-06-30 14:50:00 -070014656 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart547077a2017-05-15 15:20:40 -070014657 "6402 RQE Error x%x, posted %d err_cnt "
14658 "%d: %x %x %x\n",
14659 status, hrq->RQ_buf_posted,
14660 hrq->RQ_no_posted_buf,
14661 atomic_read(&tgtp->rcv_fcp_cmd_in),
14662 atomic_read(&tgtp->rcv_fcp_cmd_out),
14663 atomic_read(&tgtp->xmt_fcp_release));
14664 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050014665 fallthrough;
James Smart547077a2017-05-15 15:20:40 -070014666
14667 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040014668 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040014669 /* Post more buffers if possible */
14670 spin_lock_irqsave(&phba->hbalock, iflags);
14671 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
14672 spin_unlock_irqrestore(&phba->hbalock, iflags);
14673 workposted = true;
14674 break;
14675 }
14676out:
14677 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040014678}
14679
14680/**
14681 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
14682 * @phba: Pointer to HBA context object.
14683 * @cq: Pointer to the completion queue.
James Smart32517fc2019-01-28 11:14:33 -080014684 * @cqe: Pointer to a completion queue entry.
James Smart4d9ab992009-10-02 15:16:39 -040014685 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014686 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040014687 * entry.
14688 *
14689 * Return: true if work posted to worker thread, otherwise false.
14690 **/
14691static bool
14692lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040014693 struct lpfc_cqe *cqe)
14694{
James Smart45ed1192009-10-02 15:17:02 -040014695 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040014696 bool workposted = false;
14697
14698 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070014699 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040014700
14701 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040014702 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040014703 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040014704 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040014705 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040014706 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040014707 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040014708 break;
14709 case CQE_CODE_RELEASE_WQE:
14710 /* Process the WQ release event */
14711 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040014712 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040014713 break;
14714 case CQE_CODE_XRI_ABORTED:
14715 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040014716 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014717 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040014718 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040014719 break;
James Smart4d9ab992009-10-02 15:16:39 -040014720 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040014721 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040014722 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040014723 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040014724 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040014725 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040014726 break;
James Smart4f774512009-05-22 14:52:35 -040014727 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070014728 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040014729 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040014730 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040014731 break;
14732 }
14733 return workposted;
14734}
14735
14736/**
James Smart4f774512009-05-22 14:52:35 -040014737 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
14738 * @phba: Pointer to HBA context object.
14739 * @eqe: Pointer to fast-path event queue entry.
Lee Jones7af29d42020-07-21 17:41:31 +010014740 * @speq: Pointer to slow-path event queue.
James Smart4f774512009-05-22 14:52:35 -040014741 *
14742 * This routine process a event queue entry from the slow-path event queue.
14743 * It will check the MajorCode and MinorCode to determine this is for a
14744 * completion event on a completion queue, if not, an error shall be logged
14745 * and just return. Otherwise, it will get to the corresponding completion
14746 * queue and process all the entries on that completion queue, rearm the
14747 * completion queue, and then return.
14748 *
14749 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070014750static void
James Smart67d12732012-08-03 12:36:13 -040014751lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14752 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040014753{
James Smart67d12732012-08-03 12:36:13 -040014754 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040014755 uint16_t cqid;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014756 int ret = 0;
James Smart4f774512009-05-22 14:52:35 -040014757
James Smart4f774512009-05-22 14:52:35 -040014758 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040014759 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040014760
James Smart4f774512009-05-22 14:52:35 -040014761 list_for_each_entry(childq, &speq->child_list, list) {
14762 if (childq->queue_id == cqid) {
14763 cq = childq;
14764 break;
14765 }
14766 }
14767 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040014768 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
Dick Kennedy372c1872020-06-30 14:50:00 -070014769 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040014770 "0365 Slow-path CQ identifier "
14771 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070014772 return;
James Smart4f774512009-05-22 14:52:35 -040014773 }
14774
James Smart895427b2017-02-12 13:52:30 -080014775 /* Save EQ associated with this CQ */
14776 cq->assoc_qp = speq;
14777
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014778 if (is_kdump_kernel())
14779 ret = queue_work(phba->wq, &cq->spwork);
14780 else
14781 ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
14782
14783 if (!ret)
Dick Kennedy372c1872020-06-30 14:50:00 -070014784 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014785 "0390 Cannot schedule queue work "
Dick Kennedyf485c182017-09-29 17:34:34 -070014786 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
Bart Van Assched6d189c2019-03-28 11:06:22 -070014787 cqid, cq->queue_id, raw_smp_processor_id());
Dick Kennedyf485c182017-09-29 17:34:34 -070014788}
14789
14790/**
James Smart32517fc2019-01-28 11:14:33 -080014791 * __lpfc_sli4_process_cq - Process elements of a CQ
Dick Kennedyf485c182017-09-29 17:34:34 -070014792 * @phba: Pointer to HBA context object.
James Smart32517fc2019-01-28 11:14:33 -080014793 * @cq: Pointer to CQ to be processed
14794 * @handler: Routine to process each cqe
14795 * @delay: Pointer to usdelay to set in case of rescheduling of the handler
Dick Kennedy317aeb82020-06-30 14:49:59 -070014796 * @poll_mode: Polling mode we were called from
Dick Kennedyf485c182017-09-29 17:34:34 -070014797 *
James Smart32517fc2019-01-28 11:14:33 -080014798 * This routine processes completion queue entries in a CQ. While a valid
14799 * queue element is found, the handler is called. During processing checks
14800 * are made for periodic doorbell writes to let the hardware know of
14801 * element consumption.
Dick Kennedyf485c182017-09-29 17:34:34 -070014802 *
James Smart32517fc2019-01-28 11:14:33 -080014803 * If the max limit on cqes to process is hit, or there are no more valid
14804 * entries, the loop stops. If we processed a sufficient number of elements,
14805 * meaning there is sufficient load, rather than rearming and generating
14806 * another interrupt, a cq rescheduling delay will be set. A delay of 0
14807 * indicates no rescheduling.
14808 *
14809 * Returns True if work scheduled, False otherwise.
Dick Kennedyf485c182017-09-29 17:34:34 -070014810 **/
James Smart32517fc2019-01-28 11:14:33 -080014811static bool
14812__lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
14813 bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014814 struct lpfc_cqe *), unsigned long *delay,
14815 enum lpfc_poll_mode poll_mode)
Dick Kennedyf485c182017-09-29 17:34:34 -070014816{
Dick Kennedyf485c182017-09-29 17:34:34 -070014817 struct lpfc_cqe *cqe;
14818 bool workposted = false;
James Smart32517fc2019-01-28 11:14:33 -080014819 int count = 0, consumed = 0;
14820 bool arm = true;
14821
14822 /* default - no reschedule */
14823 *delay = 0;
14824
14825 if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
14826 goto rearm_and_exit;
Dick Kennedyf485c182017-09-29 17:34:34 -070014827
James Smart4f774512009-05-22 14:52:35 -040014828 /* Process all the entries to the CQ */
James Smartd74a89a2019-05-21 17:48:55 -070014829 cq->q_flag = 0;
James Smart32517fc2019-01-28 11:14:33 -080014830 cqe = lpfc_sli4_cq_get(cq);
14831 while (cqe) {
James Smart32517fc2019-01-28 11:14:33 -080014832 workposted |= handler(phba, cq, cqe);
14833 __lpfc_sli4_consume_cqe(phba, cq, cqe);
14834
14835 consumed++;
14836 if (!(++count % cq->max_proc_limit))
14837 break;
14838
14839 if (!(count % cq->notify_interval)) {
14840 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14841 LPFC_QUEUE_NOARM);
14842 consumed = 0;
James Smart8156d372019-10-18 14:18:26 -070014843 cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
James Smart4f774512009-05-22 14:52:35 -040014844 }
James Smartb84daac2012-08-03 12:35:13 -040014845
James Smartd74a89a2019-05-21 17:48:55 -070014846 if (count == LPFC_NVMET_CQ_NOTIFY)
14847 cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
14848
James Smart32517fc2019-01-28 11:14:33 -080014849 cqe = lpfc_sli4_cq_get(cq);
14850 }
14851 if (count >= phba->cfg_cq_poll_threshold) {
14852 *delay = 1;
14853 arm = false;
14854 }
14855
Dick Kennedy317aeb82020-06-30 14:49:59 -070014856 /* Note: complete the irq_poll softirq before rearming CQ */
14857 if (poll_mode == LPFC_IRQ_POLL)
14858 irq_poll_complete(&cq->iop);
14859
James Smart32517fc2019-01-28 11:14:33 -080014860 /* Track the max number of CQEs processed in 1 EQ */
14861 if (count > cq->CQ_max_cqe)
14862 cq->CQ_max_cqe = count;
14863
14864 cq->assoc_qp->EQ_cqe_cnt += count;
14865
14866 /* Catch the no cq entry condition */
14867 if (unlikely(count == 0))
14868 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14869 "0369 No entry from completion queue "
14870 "qid=%d\n", cq->queue_id);
14871
Dick Kennedy164ba8d2020-05-01 14:43:03 -070014872 xchg(&cq->queue_claimed, 0);
James Smart32517fc2019-01-28 11:14:33 -080014873
14874rearm_and_exit:
14875 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14876 arm ? LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
14877
14878 return workposted;
14879}
14880
14881/**
Lee Jones8514e2f2021-03-03 14:46:18 +000014882 * __lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
James Smart32517fc2019-01-28 11:14:33 -080014883 * @cq: pointer to CQ to process
14884 *
14885 * This routine calls the cq processing routine with a handler specific
14886 * to the type of queue bound to it.
14887 *
14888 * The CQ routine returns two values: the first is the calling status,
14889 * which indicates whether work was queued to the background discovery
14890 * thread. If true, the routine should wakeup the discovery thread;
14891 * the second is the delay parameter. If non-zero, rather than rearming
14892 * the CQ and yet another interrupt, the CQ handler should be queued so
14893 * that it is processed in a subsequent polling action. The value of
14894 * the delay indicates when to reschedule it.
14895 **/
14896static void
14897__lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
14898{
14899 struct lpfc_hba *phba = cq->phba;
14900 unsigned long delay;
14901 bool workposted = false;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014902 int ret = 0;
James Smart32517fc2019-01-28 11:14:33 -080014903
14904 /* Process and rearm the CQ */
14905 switch (cq->type) {
14906 case LPFC_MCQ:
14907 workposted |= __lpfc_sli4_process_cq(phba, cq,
14908 lpfc_sli4_sp_handle_mcqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014909 &delay, LPFC_QUEUE_WORK);
James Smart32517fc2019-01-28 11:14:33 -080014910 break;
14911 case LPFC_WCQ:
James Smartc00f62e2019-08-14 16:57:11 -070014912 if (cq->subtype == LPFC_IO)
James Smart32517fc2019-01-28 11:14:33 -080014913 workposted |= __lpfc_sli4_process_cq(phba, cq,
14914 lpfc_sli4_fp_handle_cqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014915 &delay, LPFC_QUEUE_WORK);
James Smart32517fc2019-01-28 11:14:33 -080014916 else
14917 workposted |= __lpfc_sli4_process_cq(phba, cq,
14918 lpfc_sli4_sp_handle_cqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014919 &delay, LPFC_QUEUE_WORK);
James Smart4f774512009-05-22 14:52:35 -040014920 break;
14921 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070014922 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040014923 "0370 Invalid completion queue type (%d)\n",
14924 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070014925 return;
James Smart4f774512009-05-22 14:52:35 -040014926 }
14927
James Smart32517fc2019-01-28 11:14:33 -080014928 if (delay) {
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014929 if (is_kdump_kernel())
14930 ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
14931 delay);
14932 else
14933 ret = queue_delayed_work_on(cq->chann, phba->wq,
14934 &cq->sched_spwork, delay);
14935 if (!ret)
Dick Kennedy372c1872020-06-30 14:50:00 -070014936 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014937 "0394 Cannot schedule queue work "
James Smart32517fc2019-01-28 11:14:33 -080014938 "for cqid=%d on CPU %d\n",
14939 cq->queue_id, cq->chann);
14940 }
James Smart4f774512009-05-22 14:52:35 -040014941
14942 /* wake up worker thread if there are works to be done */
14943 if (workposted)
14944 lpfc_worker_wake_up(phba);
14945}
14946
14947/**
James Smart32517fc2019-01-28 11:14:33 -080014948 * lpfc_sli4_sp_process_cq - slow-path work handler when started by
14949 * interrupt
14950 * @work: pointer to work element
14951 *
14952 * translates from the work handler and calls the slow-path handler.
14953 **/
14954static void
14955lpfc_sli4_sp_process_cq(struct work_struct *work)
14956{
14957 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
14958
14959 __lpfc_sli4_sp_process_cq(cq);
14960}
14961
14962/**
14963 * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
14964 * @work: pointer to work element
14965 *
14966 * translates from the work handler and calls the slow-path handler.
14967 **/
14968static void
14969lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
14970{
14971 struct lpfc_queue *cq = container_of(to_delayed_work(work),
14972 struct lpfc_queue, sched_spwork);
14973
14974 __lpfc_sli4_sp_process_cq(cq);
14975}
14976
14977/**
James Smart4f774512009-05-22 14:52:35 -040014978 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040014979 * @phba: Pointer to HBA context object.
14980 * @cq: Pointer to associated CQ
14981 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040014982 *
14983 * This routine process a fast-path work queue completion entry from fast-path
14984 * event queue for FCP command response completion.
14985 **/
14986static void
James Smart2a76a282012-08-03 12:35:54 -040014987lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040014988 struct lpfc_wcqe_complete *wcqe)
14989{
James Smart2a76a282012-08-03 12:35:54 -040014990 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040014991 struct lpfc_iocbq *cmdiocbq;
14992 struct lpfc_iocbq irspiocbq;
14993 unsigned long iflags;
14994
James Smart4f774512009-05-22 14:52:35 -040014995 /* Check for response status */
14996 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14997 /* If resource errors reported from HBA, reduce queue
14998 * depth of the SCSI device.
14999 */
James Smarte3d2b802012-08-14 14:25:43 -040015000 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
15001 IOSTAT_LOCAL_REJECT)) &&
15002 ((wcqe->parameter & IOERR_PARAM_MASK) ==
15003 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040015004 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040015005
Dick Kennedy28ed7372020-06-30 14:49:56 -070015006 /* Log the cmpl status */
James Smart11f0e342018-05-04 20:37:57 -070015007 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
Dick Kennedy28ed7372020-06-30 14:49:56 -070015008 "0373 FCP CQE cmpl: status=x%x: "
James Smart11f0e342018-05-04 20:37:57 -070015009 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040015010 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070015011 wcqe->word0, wcqe->total_data_placed,
15012 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040015013 }
15014
15015 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040015016 spin_lock_irqsave(&pring->ring_lock, iflags);
15017 pring->stats.iocb_event++;
James Smarte2a8be52019-05-06 17:26:47 -070015018 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040015019 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
15020 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart4f774512009-05-22 14:52:35 -040015021 if (unlikely(!cmdiocbq)) {
15022 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15023 "0374 FCP complete with no corresponding "
15024 "cmdiocb: iotag (%d)\n",
15025 bf_get(lpfc_wcqe_c_request_tag, wcqe));
15026 return;
15027 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070015028#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
15029 cmdiocbq->isr_timestamp = cq->isr_timestamp;
15030#endif
James Smart895427b2017-02-12 13:52:30 -080015031 if (cmdiocbq->iocb_cmpl == NULL) {
15032 if (cmdiocbq->wqe_cmpl) {
James Smart96e209b2020-11-15 11:26:43 -080015033 /* For FCP the flag is cleared in wqe_cmpl */
15034 if (!(cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
15035 cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
James Smart895427b2017-02-12 13:52:30 -080015036 spin_lock_irqsave(&phba->hbalock, iflags);
15037 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
15038 spin_unlock_irqrestore(&phba->hbalock, iflags);
15039 }
15040
15041 /* Pass the cmd_iocb and the wcqe to the upper layer */
15042 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
15043 return;
15044 }
James Smart4f774512009-05-22 14:52:35 -040015045 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15046 "0375 FCP cmdiocb not callback function "
15047 "iotag: (%d)\n",
15048 bf_get(lpfc_wcqe_c_request_tag, wcqe));
15049 return;
15050 }
15051
James Smart96e209b2020-11-15 11:26:43 -080015052 /* Only SLI4 non-IO commands stil use IOCB */
James Smart4f774512009-05-22 14:52:35 -040015053 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050015054 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040015055
James Smart0f65ff62010-02-26 14:14:23 -050015056 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
15057 spin_lock_irqsave(&phba->hbalock, iflags);
15058 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
15059 spin_unlock_irqrestore(&phba->hbalock, iflags);
15060 }
15061
James Smart4f774512009-05-22 14:52:35 -040015062 /* Pass the cmd_iocb and the rsp state to the upper layer */
15063 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
15064}
15065
15066/**
15067 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
15068 * @phba: Pointer to HBA context object.
15069 * @cq: Pointer to completion queue.
15070 * @wcqe: Pointer to work-queue completion queue entry.
15071 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080015072 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040015073 * proper WQ release routine to the slow-path WQ.
15074 **/
15075static void
15076lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15077 struct lpfc_wcqe_release *wcqe)
15078{
15079 struct lpfc_queue *childwq;
15080 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080015081 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040015082
15083 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080015084 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040015085 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080015086 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040015087 lpfc_sli4_wq_release(childwq,
15088 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080015089 if (childwq->q_flag & HBA_NVMET_WQFULL)
15090 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040015091 wqid_matched = true;
15092 break;
15093 }
15094 }
15095 /* Report warning log message if no match found */
15096 if (wqid_matched != true)
15097 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15098 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080015099 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040015100}
15101
15102/**
James Smart2d7dbc42017-02-12 13:52:35 -080015103 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
15104 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010015105 * @cq: Pointer to completion queue.
James Smart2d7dbc42017-02-12 13:52:35 -080015106 * @rcqe: Pointer to receive-queue completion queue entry.
15107 *
15108 * This routine process a receive-queue completion queue entry.
15109 *
15110 * Return: true if work posted to worker thread, otherwise false.
15111 **/
15112static bool
15113lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15114 struct lpfc_rcqe *rcqe)
15115{
15116 bool workposted = false;
15117 struct lpfc_queue *hrq;
15118 struct lpfc_queue *drq;
15119 struct rqb_dmabuf *dma_buf;
15120 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070015121 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080015122 uint32_t status, rq_id;
15123 unsigned long iflags;
15124 uint32_t fctl, idx;
15125
15126 if ((phba->nvmet_support == 0) ||
15127 (phba->sli4_hba.nvmet_cqset == NULL))
15128 return workposted;
15129
15130 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
15131 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
15132 drq = phba->sli4_hba.nvmet_mrq_data[idx];
15133
15134 /* sanity check on queue memory */
15135 if (unlikely(!hrq) || unlikely(!drq))
15136 return workposted;
15137
15138 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
15139 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
15140 else
15141 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
15142
15143 if ((phba->nvmet_support == 0) ||
15144 (rq_id != hrq->queue_id))
15145 return workposted;
15146
15147 status = bf_get(lpfc_rcqe_status, rcqe);
15148 switch (status) {
15149 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
Dick Kennedy372c1872020-06-30 14:50:00 -070015150 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080015151 "6126 Receive Frame Truncated!!\n");
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050015152 fallthrough;
James Smart2d7dbc42017-02-12 13:52:35 -080015153 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080015154 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080015155 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080015156 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15157 if (!dma_buf) {
15158 hrq->RQ_no_buf_found++;
15159 spin_unlock_irqrestore(&phba->hbalock, iflags);
15160 goto out;
15161 }
15162 spin_unlock_irqrestore(&phba->hbalock, iflags);
15163 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070015164 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080015165 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
15166
15167 /* Just some basic sanity checks on FCP Command frame */
15168 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
James Smart3a8070c2020-03-31 09:50:05 -070015169 fc_hdr->fh_f_ctl[1] << 8 |
15170 fc_hdr->fh_f_ctl[2]);
James Smart2d7dbc42017-02-12 13:52:35 -080015171 if (((fctl &
15172 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
15173 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
15174 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
15175 goto drop;
15176
15177 if (fc_hdr->fh_type == FC_TYPE_FCP) {
James Smartd74a89a2019-05-21 17:48:55 -070015178 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080015179 lpfc_nvmet_unsol_fcp_event(
James Smartd74a89a2019-05-21 17:48:55 -070015180 phba, idx, dma_buf, cq->isr_timestamp,
15181 cq->q_flag & HBA_NVMET_CQ_NOTIFY);
James Smart2d7dbc42017-02-12 13:52:35 -080015182 return false;
15183 }
15184drop:
James Smart22b738a2019-03-12 16:30:11 -070015185 lpfc_rq_buf_free(phba, &dma_buf->hbuf);
James Smart2d7dbc42017-02-12 13:52:35 -080015186 break;
James Smart2d7dbc42017-02-12 13:52:35 -080015187 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070015188 if (phba->nvmet_support) {
15189 tgtp = phba->targetport->private;
Dick Kennedy372c1872020-06-30 14:50:00 -070015190 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart547077a2017-05-15 15:20:40 -070015191 "6401 RQE Error x%x, posted %d err_cnt "
15192 "%d: %x %x %x\n",
15193 status, hrq->RQ_buf_posted,
15194 hrq->RQ_no_posted_buf,
15195 atomic_read(&tgtp->rcv_fcp_cmd_in),
15196 atomic_read(&tgtp->rcv_fcp_cmd_out),
15197 atomic_read(&tgtp->xmt_fcp_release));
15198 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050015199 fallthrough;
James Smart547077a2017-05-15 15:20:40 -070015200
15201 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080015202 hrq->RQ_no_posted_buf++;
15203 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080015204 break;
15205 }
15206out:
15207 return workposted;
15208}
15209
15210/**
James Smart895427b2017-02-12 13:52:30 -080015211 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart32517fc2019-01-28 11:14:33 -080015212 * @phba: adapter with cq
James Smart4f774512009-05-22 14:52:35 -040015213 * @cq: Pointer to the completion queue.
Lee Jones7af29d42020-07-21 17:41:31 +010015214 * @cqe: Pointer to fast-path completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040015215 *
15216 * This routine process a fast-path work queue completion entry from fast-path
15217 * event queue for FCP command response completion.
James Smart32517fc2019-01-28 11:14:33 -080015218 *
15219 * Return: true if work posted to worker thread, otherwise false.
James Smart4f774512009-05-22 14:52:35 -040015220 **/
James Smart32517fc2019-01-28 11:14:33 -080015221static bool
James Smart895427b2017-02-12 13:52:30 -080015222lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040015223 struct lpfc_cqe *cqe)
15224{
15225 struct lpfc_wcqe_release wcqe;
15226 bool workposted = false;
15227
15228 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070015229 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040015230
15231 /* Check and process for different type of WCQE and dispatch */
15232 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
15233 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080015234 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040015235 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040015236 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040015237 phba->last_completion_time = jiffies;
James Smartc00f62e2019-08-14 16:57:11 -070015238 if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
James Smart895427b2017-02-12 13:52:30 -080015239 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040015240 (struct lpfc_wcqe_complete *)&wcqe);
15241 break;
15242 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040015243 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040015244 /* Process the WQ release event */
15245 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
15246 (struct lpfc_wcqe_release *)&wcqe);
15247 break;
15248 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040015249 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040015250 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040015251 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040015252 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
15253 (struct sli4_wcqe_xri_aborted *)&wcqe);
15254 break;
James Smart895427b2017-02-12 13:52:30 -080015255 case CQE_CODE_RECEIVE_V1:
15256 case CQE_CODE_RECEIVE:
15257 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080015258 if (cq->subtype == LPFC_NVMET) {
15259 workposted = lpfc_sli4_nvmet_handle_rcqe(
15260 phba, cq, (struct lpfc_rcqe *)&wcqe);
15261 }
James Smart895427b2017-02-12 13:52:30 -080015262 break;
James Smart4f774512009-05-22 14:52:35 -040015263 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070015264 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -080015265 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040015266 bf_get(lpfc_wcqe_c_code, &wcqe));
15267 break;
15268 }
15269 return workposted;
15270}
15271
15272/**
Dick Kennedy317aeb82020-06-30 14:49:59 -070015273 * lpfc_sli4_sched_cq_work - Schedules cq work
15274 * @phba: Pointer to HBA context object.
15275 * @cq: Pointer to CQ
15276 * @cqid: CQ ID
15277 *
15278 * This routine checks the poll mode of the CQ corresponding to
15279 * cq->chann, then either schedules a softirq or queue_work to complete
15280 * cq work.
15281 *
15282 * queue_work path is taken if in NVMET mode, or if poll_mode is in
15283 * LPFC_QUEUE_WORK mode. Otherwise, softirq path is taken.
15284 *
15285 **/
15286static void lpfc_sli4_sched_cq_work(struct lpfc_hba *phba,
15287 struct lpfc_queue *cq, uint16_t cqid)
15288{
15289 int ret = 0;
15290
15291 switch (cq->poll_mode) {
15292 case LPFC_IRQ_POLL:
James Smart9064aeb2021-08-16 09:28:50 -070015293 /* CGN mgmt is mutually exclusive from softirq processing */
15294 if (phba->cmf_active_mode == LPFC_CFG_OFF) {
15295 irq_poll_sched(&cq->iop);
15296 break;
15297 }
15298 fallthrough;
Dick Kennedy317aeb82020-06-30 14:49:59 -070015299 case LPFC_QUEUE_WORK:
15300 default:
15301 if (is_kdump_kernel())
15302 ret = queue_work(phba->wq, &cq->irqwork);
15303 else
15304 ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
15305 if (!ret)
Dick Kennedy372c1872020-06-30 14:50:00 -070015306 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -070015307 "0383 Cannot schedule queue work "
15308 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
15309 cqid, cq->queue_id,
15310 raw_smp_processor_id());
15311 }
15312}
15313
15314/**
James Smart67d12732012-08-03 12:36:13 -040015315 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040015316 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010015317 * @eq: Pointer to the queue structure.
James Smart4f774512009-05-22 14:52:35 -040015318 * @eqe: Pointer to fast-path event queue entry.
15319 *
15320 * This routine process a event queue entry from the fast-path event queue.
15321 * It will check the MajorCode and MinorCode to determine this is for a
15322 * completion event on a completion queue, if not, an error shall be logged
15323 * and just return. Otherwise, it will get to the corresponding completion
15324 * queue and process all the entries on the completion queue, rearm the
15325 * completion queue, and then return.
15326 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070015327static void
James Smart32517fc2019-01-28 11:14:33 -080015328lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
15329 struct lpfc_eqe *eqe)
James Smart4f774512009-05-22 14:52:35 -040015330{
James Smart895427b2017-02-12 13:52:30 -080015331 struct lpfc_queue *cq = NULL;
James Smart32517fc2019-01-28 11:14:33 -080015332 uint32_t qidx = eq->hdwq;
James Smart2d7dbc42017-02-12 13:52:35 -080015333 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040015334
James Smartcb5172e2010-03-15 11:25:07 -040015335 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070015336 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart67d12732012-08-03 12:36:13 -040015337 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040015338 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040015339 bf_get_le32(lpfc_eqe_major_code, eqe),
15340 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070015341 return;
James Smart4f774512009-05-22 14:52:35 -040015342 }
15343
James Smart67d12732012-08-03 12:36:13 -040015344 /* Get the reference to the corresponding CQ */
15345 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
15346
James Smart6a828b02019-01-28 11:14:31 -080015347 /* Use the fast lookup method first */
15348 if (cqid <= phba->sli4_hba.cq_max) {
15349 cq = phba->sli4_hba.cq_lookup[cqid];
15350 if (cq)
15351 goto work_cq;
James Smartcdb42be2019-01-28 11:14:21 -080015352 }
15353
15354 /* Next check for NVMET completion */
James Smart2d7dbc42017-02-12 13:52:35 -080015355 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
15356 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
15357 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
15358 /* Process NVMET unsol rcv */
15359 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
15360 goto process_cq;
15361 }
15362 }
15363
James Smart895427b2017-02-12 13:52:30 -080015364 if (phba->sli4_hba.nvmels_cq &&
15365 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
15366 /* Process NVME unsol rcv */
15367 cq = phba->sli4_hba.nvmels_cq;
15368 }
15369
15370 /* Otherwise this is a Slow path event */
15371 if (cq == NULL) {
James Smartcdb42be2019-01-28 11:14:21 -080015372 lpfc_sli4_sp_handle_eqe(phba, eqe,
15373 phba->sli4_hba.hdwq[qidx].hba_eq);
Dick Kennedyf485c182017-09-29 17:34:34 -070015374 return;
James Smart67d12732012-08-03 12:36:13 -040015375 }
15376
James Smart895427b2017-02-12 13:52:30 -080015377process_cq:
James Smart4f774512009-05-22 14:52:35 -040015378 if (unlikely(cqid != cq->queue_id)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070015379 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040015380 "0368 Miss-matched fast-path completion "
15381 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
15382 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070015383 return;
James Smart4f774512009-05-22 14:52:35 -040015384 }
15385
James Smart6a828b02019-01-28 11:14:31 -080015386work_cq:
James Smartd74a89a2019-05-21 17:48:55 -070015387#if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
15388 if (phba->ktime_on)
15389 cq->isr_timestamp = ktime_get_ns();
15390 else
15391 cq->isr_timestamp = 0;
15392#endif
Dick Kennedy317aeb82020-06-30 14:49:59 -070015393 lpfc_sli4_sched_cq_work(phba, cq, cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070015394}
15395
15396/**
James Smart32517fc2019-01-28 11:14:33 -080015397 * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
15398 * @cq: Pointer to CQ to be processed
Dick Kennedy317aeb82020-06-30 14:49:59 -070015399 * @poll_mode: Enum lpfc_poll_state to determine poll mode
Dick Kennedyf485c182017-09-29 17:34:34 -070015400 *
James Smart32517fc2019-01-28 11:14:33 -080015401 * This routine calls the cq processing routine with the handler for
15402 * fast path CQEs.
15403 *
15404 * The CQ routine returns two values: the first is the calling status,
15405 * which indicates whether work was queued to the background discovery
15406 * thread. If true, the routine should wakeup the discovery thread;
15407 * the second is the delay parameter. If non-zero, rather than rearming
15408 * the CQ and yet another interrupt, the CQ handler should be queued so
15409 * that it is processed in a subsequent polling action. The value of
15410 * the delay indicates when to reschedule it.
Dick Kennedyf485c182017-09-29 17:34:34 -070015411 **/
15412static void
Dick Kennedy317aeb82020-06-30 14:49:59 -070015413__lpfc_sli4_hba_process_cq(struct lpfc_queue *cq,
15414 enum lpfc_poll_mode poll_mode)
Dick Kennedyf485c182017-09-29 17:34:34 -070015415{
Dick Kennedyf485c182017-09-29 17:34:34 -070015416 struct lpfc_hba *phba = cq->phba;
James Smart32517fc2019-01-28 11:14:33 -080015417 unsigned long delay;
Dick Kennedyf485c182017-09-29 17:34:34 -070015418 bool workposted = false;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070015419 int ret = 0;
Dick Kennedyf485c182017-09-29 17:34:34 -070015420
James Smart32517fc2019-01-28 11:14:33 -080015421 /* process and rearm the CQ */
15422 workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070015423 &delay, poll_mode);
James Smart32517fc2019-01-28 11:14:33 -080015424
15425 if (delay) {
Dick Kennedy86ee57a2020-06-30 14:49:55 -070015426 if (is_kdump_kernel())
15427 ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
15428 delay);
15429 else
15430 ret = queue_delayed_work_on(cq->chann, phba->wq,
15431 &cq->sched_irqwork, delay);
15432 if (!ret)
Dick Kennedy372c1872020-06-30 14:50:00 -070015433 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -070015434 "0367 Cannot schedule queue work "
15435 "for cqid=%d on CPU %d\n",
15436 cq->queue_id, cq->chann);
James Smart4f774512009-05-22 14:52:35 -040015437 }
15438
James Smart4f774512009-05-22 14:52:35 -040015439 /* wake up worker thread if there are works to be done */
15440 if (workposted)
15441 lpfc_worker_wake_up(phba);
15442}
15443
James Smart1ba981f2014-02-20 09:56:45 -050015444/**
James Smart32517fc2019-01-28 11:14:33 -080015445 * lpfc_sli4_hba_process_cq - fast-path work handler when started by
15446 * interrupt
15447 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050015448 *
James Smart32517fc2019-01-28 11:14:33 -080015449 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050015450 **/
15451static void
James Smart32517fc2019-01-28 11:14:33 -080015452lpfc_sli4_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050015453{
James Smart32517fc2019-01-28 11:14:33 -080015454 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050015455
Dick Kennedy317aeb82020-06-30 14:49:59 -070015456 __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
James Smart1ba981f2014-02-20 09:56:45 -050015457}
15458
15459/**
Lee Jones8514e2f2021-03-03 14:46:18 +000015460 * lpfc_sli4_dly_hba_process_cq - fast-path work handler when started by timer
James Smart32517fc2019-01-28 11:14:33 -080015461 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050015462 *
James Smart32517fc2019-01-28 11:14:33 -080015463 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050015464 **/
James Smart32517fc2019-01-28 11:14:33 -080015465static void
15466lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050015467{
James Smart32517fc2019-01-28 11:14:33 -080015468 struct lpfc_queue *cq = container_of(to_delayed_work(work),
15469 struct lpfc_queue, sched_irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050015470
Dick Kennedy317aeb82020-06-30 14:49:59 -070015471 __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
James Smart1ba981f2014-02-20 09:56:45 -050015472}
15473
James Smart4f774512009-05-22 14:52:35 -040015474/**
James Smart67d12732012-08-03 12:36:13 -040015475 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040015476 * @irq: Interrupt number.
15477 * @dev_id: The device context pointer.
15478 *
15479 * This function is directly called from the PCI layer as an interrupt
15480 * service routine when device with SLI-4 interface spec is enabled with
15481 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
15482 * ring event in the HBA. However, when the device is enabled with either
15483 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
15484 * device-level interrupt handler. When the PCI slot is in error recovery
15485 * or the HBA is undergoing initialization, the interrupt handler will not
15486 * process the interrupt. The SCSI FCP fast-path ring event are handled in
15487 * the intrrupt context. This function is called without any lock held.
15488 * It gets the hbalock to access and update SLI data structures. Note that,
15489 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
15490 * equal to that of FCP CQ index.
15491 *
James Smart67d12732012-08-03 12:36:13 -040015492 * The link attention and ELS ring attention events are handled
15493 * by the worker thread. The interrupt handler signals the worker thread
15494 * and returns for these events. This function is called without any lock
15495 * held. It gets the hbalock to access and update SLI data structures.
15496 *
James Smart4f774512009-05-22 14:52:35 -040015497 * This function returns IRQ_HANDLED when interrupt is handled else it
15498 * returns IRQ_NONE.
15499 **/
15500irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040015501lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040015502{
15503 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080015504 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040015505 struct lpfc_queue *fpeq;
James Smart4f774512009-05-22 14:52:35 -040015506 unsigned long iflag;
15507 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080015508 int hba_eqidx;
James Smart32517fc2019-01-28 11:14:33 -080015509 struct lpfc_eq_intr_info *eqi;
James Smart4f774512009-05-22 14:52:35 -040015510
15511 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080015512 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
15513 phba = hba_eq_hdl->phba;
15514 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040015515
15516 if (unlikely(!phba))
15517 return IRQ_NONE;
James Smartcdb42be2019-01-28 11:14:21 -080015518 if (unlikely(!phba->sli4_hba.hdwq))
James Smart5350d872011-10-10 21:33:49 -040015519 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040015520
15521 /* Get to the EQ struct associated with this vector */
James Smart657add42019-05-21 17:49:06 -070015522 fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
James Smart2e90f4b2011-12-13 13:22:37 -050015523 if (unlikely(!fpeq))
15524 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040015525
15526 /* Check device state for handling interrupt */
15527 if (unlikely(lpfc_intr_state_check(phba))) {
15528 /* Check again for link_state with lock held */
15529 spin_lock_irqsave(&phba->hbalock, iflag);
15530 if (phba->link_state < LPFC_LINK_DOWN)
15531 /* Flush, clear interrupt, and rearm the EQ */
James Smart24c7c0a2019-09-21 20:58:58 -070015532 lpfc_sli4_eqcq_flush(phba, fpeq);
James Smart4f774512009-05-22 14:52:35 -040015533 spin_unlock_irqrestore(&phba->hbalock, iflag);
15534 return IRQ_NONE;
15535 }
15536
Dick Kennedya7fc0712020-05-01 14:43:08 -070015537 eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
15538 eqi->icnt++;
15539
Bart Van Assched6d189c2019-03-28 11:06:22 -070015540 fpeq->last_cpu = raw_smp_processor_id();
James Smart4f774512009-05-22 14:52:35 -040015541
Dick Kennedya7fc0712020-05-01 14:43:08 -070015542 if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
James Smart8156d372019-10-18 14:18:26 -070015543 fpeq->q_flag & HBA_EQ_DELAY_CHK &&
James Smart32517fc2019-01-28 11:14:33 -080015544 phba->cfg_auto_imax &&
15545 fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
15546 phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
15547 lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
James Smartb84daac2012-08-03 12:35:13 -040015548
James Smart32517fc2019-01-28 11:14:33 -080015549 /* process and rearm the EQ */
James Smart93a4d6f2019-11-04 16:57:05 -080015550 ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040015551
15552 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040015553 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040015554 if (phba->intr_type == MSIX)
15555 /* MSI-X treated interrupt served as no EQ share INT */
15556 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15557 "0358 MSI-X interrupt with no EQE\n");
15558 else
15559 /* Non MSI-X treated on interrupt as EQ share INT */
15560 return IRQ_NONE;
15561 }
15562
15563 return IRQ_HANDLED;
James Smart3bfab8a2021-04-11 18:31:23 -070015564} /* lpfc_sli4_hba_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040015565
15566/**
15567 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
15568 * @irq: Interrupt number.
15569 * @dev_id: The device context pointer.
15570 *
15571 * This function is the device-level interrupt handler to device with SLI-4
15572 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
15573 * interrupt mode is enabled and there is an event in the HBA which requires
15574 * driver attention. This function invokes the slow-path interrupt attention
15575 * handling function and fast-path interrupt attention handling function in
15576 * turn to process the relevant HBA attention events. This function is called
15577 * without any lock held. It gets the hbalock to access and update SLI data
15578 * structures.
15579 *
15580 * This function returns IRQ_HANDLED when interrupt is handled, else it
15581 * returns IRQ_NONE.
15582 **/
15583irqreturn_t
15584lpfc_sli4_intr_handler(int irq, void *dev_id)
15585{
15586 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040015587 irqreturn_t hba_irq_rc;
15588 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080015589 int qidx;
James Smart4f774512009-05-22 14:52:35 -040015590
15591 /* Get the driver's phba structure from the dev_id */
15592 phba = (struct lpfc_hba *)dev_id;
15593
15594 if (unlikely(!phba))
15595 return IRQ_NONE;
15596
15597 /*
James Smart4f774512009-05-22 14:52:35 -040015598 * Invoke fast-path host attention interrupt handling as appropriate.
15599 */
James Smart6a828b02019-01-28 11:14:31 -080015600 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040015601 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080015602 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040015603 if (hba_irq_rc == IRQ_HANDLED)
15604 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040015605 }
15606
James Smart67d12732012-08-03 12:36:13 -040015607 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040015608} /* lpfc_sli4_intr_handler */
15609
James Smart93a4d6f2019-11-04 16:57:05 -080015610void lpfc_sli4_poll_hbtimer(struct timer_list *t)
15611{
15612 struct lpfc_hba *phba = from_timer(phba, t, cpuhp_poll_timer);
15613 struct lpfc_queue *eq;
15614 int i = 0;
15615
15616 rcu_read_lock();
15617
15618 list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
15619 i += lpfc_sli4_poll_eq(eq, LPFC_POLL_SLOWPATH);
15620 if (!list_empty(&phba->poll_list))
15621 mod_timer(&phba->cpuhp_poll_timer,
15622 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15623
15624 rcu_read_unlock();
15625}
15626
15627inline int lpfc_sli4_poll_eq(struct lpfc_queue *eq, uint8_t path)
15628{
15629 struct lpfc_hba *phba = eq->phba;
15630 int i = 0;
15631
15632 /*
15633 * Unlocking an irq is one of the entry point to check
15634 * for re-schedule, but we are good for io submission
15635 * path as midlayer does a get_cpu to glue us in. Flush
15636 * out the invalidate queue so we can see the updated
15637 * value for flag.
15638 */
15639 smp_rmb();
15640
15641 if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
15642 /* We will not likely get the completion for the caller
15643 * during this iteration but i guess that's fine.
15644 * Future io's coming on this eq should be able to
15645 * pick it up. As for the case of single io's, they
15646 * will be handled through a sched from polling timer
15647 * function which is currently triggered every 1msec.
15648 */
15649 i = lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM);
15650
15651 return i;
15652}
15653
15654static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
15655{
15656 struct lpfc_hba *phba = eq->phba;
15657
James Smartf861f592020-03-22 11:12:54 -070015658 /* kickstart slowpath processing if needed */
15659 if (list_empty(&phba->poll_list))
James Smart93a4d6f2019-11-04 16:57:05 -080015660 mod_timer(&phba->cpuhp_poll_timer,
15661 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
James Smart93a4d6f2019-11-04 16:57:05 -080015662
15663 list_add_rcu(&eq->_poll_list, &phba->poll_list);
15664 synchronize_rcu();
15665}
15666
15667static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
15668{
15669 struct lpfc_hba *phba = eq->phba;
15670
15671 /* Disable slowpath processing for this eq. Kick start the eq
15672 * by RE-ARMING the eq's ASAP
15673 */
15674 list_del_rcu(&eq->_poll_list);
15675 synchronize_rcu();
15676
15677 if (list_empty(&phba->poll_list))
15678 del_timer_sync(&phba->cpuhp_poll_timer);
15679}
15680
James Smartd480e572019-11-11 15:03:58 -080015681void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
James Smart93a4d6f2019-11-04 16:57:05 -080015682{
15683 struct lpfc_queue *eq, *next;
15684
15685 list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
15686 list_del(&eq->_poll_list);
15687
15688 INIT_LIST_HEAD(&phba->poll_list);
15689 synchronize_rcu();
15690}
15691
15692static inline void
15693__lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
15694{
15695 if (mode == eq->mode)
15696 return;
15697 /*
15698 * currently this function is only called during a hotplug
15699 * event and the cpu on which this function is executing
15700 * is going offline. By now the hotplug has instructed
15701 * the scheduler to remove this cpu from cpu active mask.
15702 * So we don't need to work about being put aside by the
15703 * scheduler for a high priority process. Yes, the inte-
15704 * rrupts could come but they are known to retire ASAP.
15705 */
15706
15707 /* Disable polling in the fastpath */
15708 WRITE_ONCE(eq->mode, mode);
15709 /* flush out the store buffer */
15710 smp_wmb();
15711
15712 /*
15713 * Add this eq to the polling list and start polling. For
15714 * a grace period both interrupt handler and poller will
15715 * try to process the eq _but_ that's fine. We have a
15716 * synchronization mechanism in place (queue_claimed) to
15717 * deal with it. This is just a draining phase for int-
15718 * errupt handler (not eq's) as we have guranteed through
15719 * barrier that all the CPUs have seen the new CQ_POLLED
15720 * state. which will effectively disable the REARMING of
15721 * the EQ. The whole idea is eq's die off eventually as
15722 * we are not rearming EQ's anymore.
15723 */
15724 mode ? lpfc_sli4_add_to_poll_list(eq) :
15725 lpfc_sli4_remove_from_poll_list(eq);
15726}
15727
15728void lpfc_sli4_start_polling(struct lpfc_queue *eq)
15729{
15730 __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
15731}
15732
15733void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
15734{
15735 struct lpfc_hba *phba = eq->phba;
15736
15737 __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
15738
15739 /* Kick start for the pending io's in h/w.
15740 * Once we switch back to interrupt processing on a eq
15741 * the io path completion will only arm eq's when it
15742 * receives a completion. But since eq's are in disa-
15743 * rmed state it doesn't receive a completion. This
15744 * creates a deadlock scenaro.
15745 */
15746 phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
15747}
15748
James Smart4f774512009-05-22 14:52:35 -040015749/**
15750 * lpfc_sli4_queue_free - free a queue structure and associated memory
15751 * @queue: The queue structure to free.
15752 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040015753 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040015754 * the host resident queue. This function must be called after destroying the
15755 * queue on the HBA.
15756 **/
15757void
15758lpfc_sli4_queue_free(struct lpfc_queue *queue)
15759{
15760 struct lpfc_dmabuf *dmabuf;
15761
15762 if (!queue)
15763 return;
15764
James Smart4645f7b2019-03-12 16:30:14 -070015765 if (!list_empty(&queue->wq_list))
15766 list_del(&queue->wq_list);
15767
James Smart4f774512009-05-22 14:52:35 -040015768 while (!list_empty(&queue->page_list)) {
15769 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
15770 list);
James Smart81b96ed2017-11-20 16:00:29 -080015771 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040015772 dmabuf->virt, dmabuf->phys);
15773 kfree(dmabuf);
15774 }
James Smart895427b2017-02-12 13:52:30 -080015775 if (queue->rqbp) {
15776 lpfc_free_rq_buffer(queue->phba, queue);
15777 kfree(queue->rqbp);
15778 }
James Smartd1f525a2017-04-21 16:04:55 -070015779
James Smart32517fc2019-01-28 11:14:33 -080015780 if (!list_empty(&queue->cpu_list))
15781 list_del(&queue->cpu_list);
15782
James Smart4f774512009-05-22 14:52:35 -040015783 kfree(queue);
15784 return;
15785}
15786
15787/**
15788 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
15789 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080015790 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040015791 * @entry_size: The size of each queue entry for this queue.
Lee Jones7af29d42020-07-21 17:41:31 +010015792 * @entry_count: The number of entries that this queue will handle.
James Smartc1a21eb2019-03-12 16:30:29 -070015793 * @cpu: The cpu that will primarily utilize this queue.
James Smart4f774512009-05-22 14:52:35 -040015794 *
15795 * This function allocates a queue structure and the DMAable memory used for
15796 * the host resident queue. This function must be called before creating the
15797 * queue on the HBA.
15798 **/
15799struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080015800lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
James Smartc1a21eb2019-03-12 16:30:29 -070015801 uint32_t entry_size, uint32_t entry_count, int cpu)
James Smart4f774512009-05-22 14:52:35 -040015802{
15803 struct lpfc_queue *queue;
15804 struct lpfc_dmabuf *dmabuf;
James Smartcb5172e2010-03-15 11:25:07 -040015805 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart9afbee32019-03-12 16:30:28 -070015806 uint16_t x, pgcnt;
James Smart4f774512009-05-22 14:52:35 -040015807
James Smartcb5172e2010-03-15 11:25:07 -040015808 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015809 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040015810
James Smart9afbee32019-03-12 16:30:28 -070015811 pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080015812
15813 /* If needed, Adjust page count to match the max the adapter supports */
James Smart9afbee32019-03-12 16:30:28 -070015814 if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
15815 pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
15816
James Smartc1a21eb2019-03-12 16:30:29 -070015817 queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
15818 GFP_KERNEL, cpu_to_node(cpu));
James Smart9afbee32019-03-12 16:30:28 -070015819 if (!queue)
15820 return NULL;
James Smart895427b2017-02-12 13:52:30 -080015821
James Smart4f774512009-05-22 14:52:35 -040015822 INIT_LIST_HEAD(&queue->list);
James Smart93a4d6f2019-11-04 16:57:05 -080015823 INIT_LIST_HEAD(&queue->_poll_list);
James Smart895427b2017-02-12 13:52:30 -080015824 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080015825 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040015826 INIT_LIST_HEAD(&queue->page_list);
15827 INIT_LIST_HEAD(&queue->child_list);
James Smart32517fc2019-01-28 11:14:33 -080015828 INIT_LIST_HEAD(&queue->cpu_list);
James Smart81b96ed2017-11-20 16:00:29 -080015829
15830 /* Set queue parameters now. If the system cannot provide memory
15831 * resources, the free routine needs to know what was allocated.
15832 */
James Smart9afbee32019-03-12 16:30:28 -070015833 queue->page_count = pgcnt;
15834 queue->q_pgs = (void **)&queue[1];
15835 queue->entry_cnt_per_pg = hw_page_size / entry_size;
James Smart81b96ed2017-11-20 16:00:29 -080015836 queue->entry_size = entry_size;
15837 queue->entry_count = entry_count;
15838 queue->page_size = hw_page_size;
15839 queue->phba = phba;
15840
James Smart9afbee32019-03-12 16:30:28 -070015841 for (x = 0; x < queue->page_count; x++) {
James Smartc1a21eb2019-03-12 16:30:29 -070015842 dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
15843 dev_to_node(&phba->pcidev->dev));
James Smart4f774512009-05-22 14:52:35 -040015844 if (!dmabuf)
15845 goto out_fail;
Luis Chamberlain750afb02019-01-04 09:23:09 +010015846 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
15847 hw_page_size, &dmabuf->phys,
15848 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040015849 if (!dmabuf->virt) {
15850 kfree(dmabuf);
15851 goto out_fail;
15852 }
15853 dmabuf->buffer_tag = x;
15854 list_add_tail(&dmabuf->list, &queue->page_list);
James Smart9afbee32019-03-12 16:30:28 -070015855 /* use lpfc_sli4_qe to index a paritcular entry in this page */
15856 queue->q_pgs[x] = dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -040015857 }
Dick Kennedyf485c182017-09-29 17:34:34 -070015858 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
15859 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart32517fc2019-01-28 11:14:33 -080015860 INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
15861 INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040015862
James Smart32517fc2019-01-28 11:14:33 -080015863 /* notify_interval will be set during q creation */
James Smart64eb4dc2017-05-15 15:20:49 -070015864
James Smart4f774512009-05-22 14:52:35 -040015865 return queue;
15866out_fail:
15867 lpfc_sli4_queue_free(queue);
15868 return NULL;
15869}
15870
15871/**
James Smart962bc512013-01-03 15:44:00 -050015872 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
15873 * @phba: HBA structure that indicates port to create a queue on.
15874 * @pci_barset: PCI BAR set flag.
15875 *
15876 * This function shall perform iomap of the specified PCI BAR address to host
15877 * memory address if not already done so and return it. The returned host
15878 * memory address can be NULL.
15879 */
15880static void __iomem *
15881lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
15882{
James Smart962bc512013-01-03 15:44:00 -050015883 if (!phba->pcidev)
15884 return NULL;
James Smart962bc512013-01-03 15:44:00 -050015885
15886 switch (pci_barset) {
15887 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050015888 return phba->pci_bar0_memmap_p;
15889 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050015890 return phba->pci_bar2_memmap_p;
15891 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050015892 return phba->pci_bar4_memmap_p;
15893 default:
15894 break;
15895 }
15896 return NULL;
15897}
15898
15899/**
James Smartcb733e32019-01-28 11:14:32 -080015900 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
15901 * @phba: HBA structure that EQs are on.
15902 * @startq: The starting EQ index to modify
15903 * @numq: The number of EQs (consecutive indexes) to modify
15904 * @usdelay: amount of delay
James Smart173edbb2012-06-12 13:54:50 -040015905 *
James Smartcb733e32019-01-28 11:14:32 -080015906 * This function revises the EQ delay on 1 or more EQs. The EQ delay
15907 * is set either by writing to a register (if supported by the SLI Port)
15908 * or by mailbox command. The mailbox command allows several EQs to be
15909 * updated at once.
James Smart173edbb2012-06-12 13:54:50 -040015910 *
James Smartcb733e32019-01-28 11:14:32 -080015911 * The @phba struct is used to send a mailbox command to HBA. The @startq
15912 * is used to get the starting EQ index to change. The @numq value is
15913 * used to specify how many consecutive EQ indexes, starting at EQ index,
15914 * are to be changed. This function is asynchronous and will wait for any
15915 * mailbox commands to finish before returning.
James Smart173edbb2012-06-12 13:54:50 -040015916 *
James Smartcb733e32019-01-28 11:14:32 -080015917 * On success this function will return a zero. If unable to allocate
15918 * enough memory this function will return -ENOMEM. If a mailbox command
15919 * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
15920 * have had their delay multipler changed.
James Smart173edbb2012-06-12 13:54:50 -040015921 **/
James Smartcb733e32019-01-28 11:14:32 -080015922void
James Smart0cf07f842017-06-01 21:07:10 -070015923lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
James Smartcb733e32019-01-28 11:14:32 -080015924 uint32_t numq, uint32_t usdelay)
James Smart173edbb2012-06-12 13:54:50 -040015925{
15926 struct lpfc_mbx_modify_eq_delay *eq_delay;
15927 LPFC_MBOXQ_t *mbox;
15928 struct lpfc_queue *eq;
James Smartcb733e32019-01-28 11:14:32 -080015929 int cnt = 0, rc, length;
James Smart173edbb2012-06-12 13:54:50 -040015930 uint32_t shdr_status, shdr_add_status;
James Smartcb733e32019-01-28 11:14:32 -080015931 uint32_t dmult;
James Smart895427b2017-02-12 13:52:30 -080015932 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040015933 union lpfc_sli4_cfg_shdr *shdr;
James Smart173edbb2012-06-12 13:54:50 -040015934
James Smart6a828b02019-01-28 11:14:31 -080015935 if (startq >= phba->cfg_irq_chann)
James Smartcb733e32019-01-28 11:14:32 -080015936 return;
15937
15938 if (usdelay > 0xFFFF) {
15939 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
15940 "6429 usdelay %d too large. Scaled down to "
15941 "0xFFFF.\n", usdelay);
15942 usdelay = 0xFFFF;
15943 }
15944
15945 /* set values by EQ_DELAY register if supported */
15946 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
15947 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070015948 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smartcb733e32019-01-28 11:14:32 -080015949 if (!eq)
15950 continue;
15951
James Smart32517fc2019-01-28 11:14:33 -080015952 lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
James Smartcb733e32019-01-28 11:14:32 -080015953
15954 if (++cnt >= numq)
15955 break;
15956 }
James Smartcb733e32019-01-28 11:14:32 -080015957 return;
15958 }
15959
15960 /* Otherwise, set values by mailbox cmd */
James Smart173edbb2012-06-12 13:54:50 -040015961
15962 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smartcb733e32019-01-28 11:14:32 -080015963 if (!mbox) {
Dick Kennedy372c1872020-06-30 14:50:00 -070015964 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcb733e32019-01-28 11:14:32 -080015965 "6428 Failed allocating mailbox cmd buffer."
15966 " EQ delay was not set.\n");
15967 return;
15968 }
James Smart173edbb2012-06-12 13:54:50 -040015969 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
15970 sizeof(struct lpfc_sli4_cfg_mhdr));
15971 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15972 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
15973 length, LPFC_SLI4_MBX_EMBED);
15974 eq_delay = &mbox->u.mqe.un.eq_delay;
15975
15976 /* Calculate delay multiper from maximum interrupt per second */
James Smartcb733e32019-01-28 11:14:32 -080015977 dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
15978 if (dmult)
15979 dmult--;
James Smart0cf07f842017-06-01 21:07:10 -070015980 if (dmult > LPFC_DMULT_MAX)
15981 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040015982
James Smart6a828b02019-01-28 11:14:31 -080015983 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070015984 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart173edbb2012-06-12 13:54:50 -040015985 if (!eq)
15986 continue;
James Smartcb733e32019-01-28 11:14:32 -080015987 eq->q_mode = usdelay;
James Smart173edbb2012-06-12 13:54:50 -040015988 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
15989 eq_delay->u.request.eq[cnt].phase = 0;
15990 eq_delay->u.request.eq[cnt].delay_multi = dmult;
James Smart0cf07f842017-06-01 21:07:10 -070015991
James Smartcb733e32019-01-28 11:14:32 -080015992 if (++cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040015993 break;
15994 }
15995 eq_delay->u.request.num_eq = cnt;
15996
15997 mbox->vport = phba->pport;
15998 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080015999 mbox->ctx_buf = NULL;
16000 mbox->ctx_ndlp = NULL;
James Smart173edbb2012-06-12 13:54:50 -040016001 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16002 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
16003 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16004 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16005 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016006 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart173edbb2012-06-12 13:54:50 -040016007 "2512 MODIFY_EQ_DELAY mailbox failed with "
16008 "status x%x add_status x%x, mbx status x%x\n",
16009 shdr_status, shdr_add_status, rc);
James Smart173edbb2012-06-12 13:54:50 -040016010 }
16011 mempool_free(mbox, phba->mbox_mem_pool);
James Smartcb733e32019-01-28 11:14:32 -080016012 return;
James Smart173edbb2012-06-12 13:54:50 -040016013}
16014
16015/**
James Smart4f774512009-05-22 14:52:35 -040016016 * lpfc_eq_create - Create an Event Queue on the HBA
16017 * @phba: HBA structure that indicates port to create a queue on.
16018 * @eq: The queue structure to use to create the event queue.
16019 * @imax: The maximum interrupt per second limit.
16020 *
16021 * This function creates an event queue, as detailed in @eq, on a port,
16022 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
16023 *
16024 * The @phba struct is used to send mailbox command to HBA. The @eq struct
16025 * is used to get the entry count and entry size that are necessary to
16026 * determine the number of pages to allocate and use for this queue. This
16027 * function will send the EQ_CREATE mailbox command to the HBA to setup the
16028 * event queue. This function is asynchronous and will wait for the mailbox
16029 * command to finish before continuing.
16030 *
16031 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040016032 * memory this function will return -ENOMEM. If the queue create mailbox command
16033 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016034 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016035int
James Smartee020062012-09-29 11:28:52 -040016036lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040016037{
16038 struct lpfc_mbx_eq_create *eq_create;
16039 LPFC_MBOXQ_t *mbox;
16040 int rc, length, status = 0;
16041 struct lpfc_dmabuf *dmabuf;
16042 uint32_t shdr_status, shdr_add_status;
16043 union lpfc_sli4_cfg_shdr *shdr;
16044 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040016045 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16046
James Smart2e90f4b2011-12-13 13:22:37 -050016047 /* sanity check on queue memory */
16048 if (!eq)
16049 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040016050 if (!phba->sli4_hba.pc_sli4_params.supported)
16051 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040016052
16053 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16054 if (!mbox)
16055 return -ENOMEM;
16056 length = (sizeof(struct lpfc_mbx_eq_create) -
16057 sizeof(struct lpfc_sli4_cfg_mhdr));
16058 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16059 LPFC_MBOX_OPCODE_EQ_CREATE,
16060 length, LPFC_SLI4_MBX_EMBED);
16061 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080016062 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040016063 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
16064 eq->page_count);
16065 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
16066 LPFC_EQE_SIZE);
16067 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080016068
16069 /* Use version 2 of CREATE_EQ if eqav is set */
16070 if (phba->sli4_hba.pc_sli4_params.eqav) {
16071 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16072 LPFC_Q_CREATE_VERSION_2);
16073 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
16074 phba->sli4_hba.pc_sli4_params.eqav);
16075 }
16076
James Smart2c9c5a02015-04-07 15:07:15 -040016077 /* don't setup delay multiplier using EQ_CREATE */
16078 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040016079 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
16080 dmult);
16081 switch (eq->entry_count) {
16082 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070016083 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040016084 "0360 Unsupported EQ count. (%d)\n",
16085 eq->entry_count);
James Smart04d210c2019-05-21 17:49:03 -070016086 if (eq->entry_count < 256) {
16087 status = -EINVAL;
16088 goto out;
16089 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016090 fallthrough; /* otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040016091 case 256:
16092 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16093 LPFC_EQ_CNT_256);
16094 break;
16095 case 512:
16096 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16097 LPFC_EQ_CNT_512);
16098 break;
16099 case 1024:
16100 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16101 LPFC_EQ_CNT_1024);
16102 break;
16103 case 2048:
16104 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16105 LPFC_EQ_CNT_2048);
16106 break;
16107 case 4096:
16108 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16109 LPFC_EQ_CNT_4096);
16110 break;
16111 }
16112 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040016113 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040016114 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16115 putPaddrLow(dmabuf->phys);
16116 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16117 putPaddrHigh(dmabuf->phys);
16118 }
16119 mbox->vport = phba->pport;
16120 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080016121 mbox->ctx_buf = NULL;
16122 mbox->ctx_ndlp = NULL;
James Smart4f774512009-05-22 14:52:35 -040016123 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040016124 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16125 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16126 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016127 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040016128 "2500 EQ_CREATE mailbox failed with "
16129 "status x%x add_status x%x, mbx status x%x\n",
16130 shdr_status, shdr_add_status, rc);
16131 status = -ENXIO;
16132 }
16133 eq->type = LPFC_EQ;
16134 eq->subtype = LPFC_NONE;
16135 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
16136 if (eq->queue_id == 0xFFFF)
16137 status = -ENXIO;
16138 eq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016139 eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
16140 eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
James Smart04d210c2019-05-21 17:49:03 -070016141out:
James Smart8fa38512009-07-19 10:01:03 -040016142 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016143 return status;
16144}
16145
Dick Kennedy317aeb82020-06-30 14:49:59 -070016146static int lpfc_cq_poll_hdler(struct irq_poll *iop, int budget)
16147{
16148 struct lpfc_queue *cq = container_of(iop, struct lpfc_queue, iop);
16149
Colin Ian King26e0b9a2020-07-07 16:00:18 +010016150 __lpfc_sli4_hba_process_cq(cq, LPFC_IRQ_POLL);
Dick Kennedy317aeb82020-06-30 14:49:59 -070016151
16152 return 1;
16153}
16154
James Smart4f774512009-05-22 14:52:35 -040016155/**
16156 * lpfc_cq_create - Create a Completion Queue on the HBA
16157 * @phba: HBA structure that indicates port to create a queue on.
16158 * @cq: The queue structure to use to create the completion queue.
16159 * @eq: The event queue to bind this completion queue to.
Lee Jones7af29d42020-07-21 17:41:31 +010016160 * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16161 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
James Smart4f774512009-05-22 14:52:35 -040016162 *
16163 * This function creates a completion queue, as detailed in @wq, on a port,
16164 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
16165 *
16166 * The @phba struct is used to send mailbox command to HBA. The @cq struct
16167 * is used to get the entry count and entry size that are necessary to
16168 * determine the number of pages to allocate and use for this queue. The @eq
16169 * is used to indicate which event queue to bind this completion queue to. This
16170 * function will send the CQ_CREATE mailbox command to the HBA to setup the
16171 * completion queue. This function is asynchronous and will wait for the mailbox
16172 * command to finish before continuing.
16173 *
16174 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040016175 * memory this function will return -ENOMEM. If the queue create mailbox command
16176 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016177 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016178int
James Smart4f774512009-05-22 14:52:35 -040016179lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
16180 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
16181{
16182 struct lpfc_mbx_cq_create *cq_create;
16183 struct lpfc_dmabuf *dmabuf;
16184 LPFC_MBOXQ_t *mbox;
16185 int rc, length, status = 0;
16186 uint32_t shdr_status, shdr_add_status;
16187 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040016188
James Smart2e90f4b2011-12-13 13:22:37 -050016189 /* sanity check on queue memory */
16190 if (!cq || !eq)
16191 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040016192
James Smart4f774512009-05-22 14:52:35 -040016193 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16194 if (!mbox)
16195 return -ENOMEM;
16196 length = (sizeof(struct lpfc_mbx_cq_create) -
16197 sizeof(struct lpfc_sli4_cfg_mhdr));
16198 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16199 LPFC_MBOX_OPCODE_CQ_CREATE,
16200 length, LPFC_SLI4_MBX_EMBED);
16201 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050016202 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040016203 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
16204 cq->page_count);
16205 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
16206 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050016207 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16208 phba->sli4_hba.pc_sli4_params.cqv);
16209 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080016210 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
16211 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050016212 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
16213 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080016214 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
16215 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050016216 } else {
16217 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
16218 eq->queue_id);
16219 }
James Smart4f774512009-05-22 14:52:35 -040016220 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080016221 case 2048:
16222 case 4096:
16223 if (phba->sli4_hba.pc_sli4_params.cqv ==
16224 LPFC_Q_CREATE_VERSION_2) {
16225 cq_create->u.request.context.lpfc_cq_context_count =
16226 cq->entry_count;
16227 bf_set(lpfc_cq_context_count,
16228 &cq_create->u.request.context,
16229 LPFC_CQ_CNT_WORD7);
16230 break;
16231 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016232 fallthrough;
James Smart4f774512009-05-22 14:52:35 -040016233 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070016234 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2ea259e2017-02-12 13:52:27 -080016235 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070016236 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080016237 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070016238 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040016239 if (cq->entry_count < 256) {
16240 status = -EINVAL;
16241 goto out;
16242 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016243 fallthrough; /* otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040016244 case 256:
16245 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16246 LPFC_CQ_CNT_256);
16247 break;
16248 case 512:
16249 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16250 LPFC_CQ_CNT_512);
16251 break;
16252 case 1024:
16253 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16254 LPFC_CQ_CNT_1024);
16255 break;
16256 }
16257 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080016258 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040016259 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16260 putPaddrLow(dmabuf->phys);
16261 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16262 putPaddrHigh(dmabuf->phys);
16263 }
16264 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16265
16266 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040016267 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16268 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16269 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016270 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040016271 "2501 CQ_CREATE mailbox failed with "
16272 "status x%x add_status x%x, mbx status x%x\n",
16273 shdr_status, shdr_add_status, rc);
16274 status = -ENXIO;
16275 goto out;
16276 }
16277 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16278 if (cq->queue_id == 0xFFFF) {
16279 status = -ENXIO;
16280 goto out;
16281 }
16282 /* link the cq onto the parent eq child list */
16283 list_add_tail(&cq->list, &eq->child_list);
16284 /* Set up completion queue's type and subtype */
16285 cq->type = type;
16286 cq->subtype = subtype;
16287 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050016288 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080016289 cq->assoc_qp = eq;
James Smart4f774512009-05-22 14:52:35 -040016290 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016291 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16292 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
James Smart4f774512009-05-22 14:52:35 -040016293
James Smart6a828b02019-01-28 11:14:31 -080016294 if (cq->queue_id > phba->sli4_hba.cq_max)
16295 phba->sli4_hba.cq_max = cq->queue_id;
Dick Kennedy317aeb82020-06-30 14:49:59 -070016296
16297 irq_poll_init(&cq->iop, LPFC_IRQ_POLL_WEIGHT, lpfc_cq_poll_hdler);
James Smart8fa38512009-07-19 10:01:03 -040016298out:
16299 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016300 return status;
16301}
16302
16303/**
James Smart2d7dbc42017-02-12 13:52:35 -080016304 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
16305 * @phba: HBA structure that indicates port to create a queue on.
16306 * @cqp: The queue structure array to use to create the completion queues.
James Smartcdb42be2019-01-28 11:14:21 -080016307 * @hdwq: The hardware queue array with the EQ to bind completion queues to.
Lee Jones7af29d42020-07-21 17:41:31 +010016308 * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16309 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
James Smart2d7dbc42017-02-12 13:52:35 -080016310 *
16311 * This function creates a set of completion queue, s to support MRQ
16312 * as detailed in @cqp, on a port,
16313 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
16314 *
16315 * The @phba struct is used to send mailbox command to HBA. The @cq struct
16316 * is used to get the entry count and entry size that are necessary to
16317 * determine the number of pages to allocate and use for this queue. The @eq
16318 * is used to indicate which event queue to bind this completion queue to. This
16319 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
16320 * completion queue. This function is asynchronous and will wait for the mailbox
16321 * command to finish before continuing.
16322 *
16323 * On success this function will return a zero. If unable to allocate enough
16324 * memory this function will return -ENOMEM. If the queue create mailbox command
16325 * fails this function will return -ENXIO.
16326 **/
16327int
16328lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
James Smartcdb42be2019-01-28 11:14:21 -080016329 struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
16330 uint32_t subtype)
James Smart2d7dbc42017-02-12 13:52:35 -080016331{
16332 struct lpfc_queue *cq;
16333 struct lpfc_queue *eq;
16334 struct lpfc_mbx_cq_create_set *cq_set;
16335 struct lpfc_dmabuf *dmabuf;
16336 LPFC_MBOXQ_t *mbox;
16337 int rc, length, alloclen, status = 0;
16338 int cnt, idx, numcq, page_idx = 0;
16339 uint32_t shdr_status, shdr_add_status;
16340 union lpfc_sli4_cfg_shdr *shdr;
16341 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16342
16343 /* sanity check on queue memory */
16344 numcq = phba->cfg_nvmet_mrq;
James Smartcdb42be2019-01-28 11:14:21 -080016345 if (!cqp || !hdwq || !numcq)
James Smart2d7dbc42017-02-12 13:52:35 -080016346 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080016347
16348 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16349 if (!mbox)
16350 return -ENOMEM;
16351
16352 length = sizeof(struct lpfc_mbx_cq_create_set);
16353 length += ((numcq * cqp[0]->page_count) *
16354 sizeof(struct dma_address));
16355 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16356 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
16357 LPFC_SLI4_MBX_NEMBED);
16358 if (alloclen < length) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016359 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080016360 "3098 Allocated DMA memory size (%d) is "
16361 "less than the requested DMA memory size "
16362 "(%d)\n", alloclen, length);
16363 status = -ENOMEM;
16364 goto out;
16365 }
16366 cq_set = mbox->sge_array->addr[0];
16367 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
16368 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
16369
16370 for (idx = 0; idx < numcq; idx++) {
16371 cq = cqp[idx];
James Smartcdb42be2019-01-28 11:14:21 -080016372 eq = hdwq[idx].hba_eq;
James Smart2d7dbc42017-02-12 13:52:35 -080016373 if (!cq || !eq) {
16374 status = -ENOMEM;
16375 goto out;
16376 }
James Smart81b96ed2017-11-20 16:00:29 -080016377 if (!phba->sli4_hba.pc_sli4_params.supported)
16378 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080016379
16380 switch (idx) {
16381 case 0:
16382 bf_set(lpfc_mbx_cq_create_set_page_size,
16383 &cq_set->u.request,
16384 (hw_page_size / SLI4_PAGE_SIZE));
16385 bf_set(lpfc_mbx_cq_create_set_num_pages,
16386 &cq_set->u.request, cq->page_count);
16387 bf_set(lpfc_mbx_cq_create_set_evt,
16388 &cq_set->u.request, 1);
16389 bf_set(lpfc_mbx_cq_create_set_valid,
16390 &cq_set->u.request, 1);
16391 bf_set(lpfc_mbx_cq_create_set_cqe_size,
16392 &cq_set->u.request, 0);
16393 bf_set(lpfc_mbx_cq_create_set_num_cq,
16394 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080016395 bf_set(lpfc_mbx_cq_create_set_autovalid,
16396 &cq_set->u.request,
16397 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080016398 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080016399 case 2048:
16400 case 4096:
16401 if (phba->sli4_hba.pc_sli4_params.cqv ==
16402 LPFC_Q_CREATE_VERSION_2) {
16403 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16404 &cq_set->u.request,
16405 cq->entry_count);
16406 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16407 &cq_set->u.request,
16408 LPFC_CQ_CNT_WORD7);
16409 break;
16410 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016411 fallthrough;
James Smart2d7dbc42017-02-12 13:52:35 -080016412 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070016413 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080016414 "3118 Bad CQ count. (%d)\n",
16415 cq->entry_count);
16416 if (cq->entry_count < 256) {
16417 status = -EINVAL;
16418 goto out;
16419 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016420 fallthrough; /* otherwise default to smallest */
James Smart2d7dbc42017-02-12 13:52:35 -080016421 case 256:
16422 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16423 &cq_set->u.request, LPFC_CQ_CNT_256);
16424 break;
16425 case 512:
16426 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16427 &cq_set->u.request, LPFC_CQ_CNT_512);
16428 break;
16429 case 1024:
16430 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16431 &cq_set->u.request, LPFC_CQ_CNT_1024);
16432 break;
16433 }
16434 bf_set(lpfc_mbx_cq_create_set_eq_id0,
16435 &cq_set->u.request, eq->queue_id);
16436 break;
16437 case 1:
16438 bf_set(lpfc_mbx_cq_create_set_eq_id1,
16439 &cq_set->u.request, eq->queue_id);
16440 break;
16441 case 2:
16442 bf_set(lpfc_mbx_cq_create_set_eq_id2,
16443 &cq_set->u.request, eq->queue_id);
16444 break;
16445 case 3:
16446 bf_set(lpfc_mbx_cq_create_set_eq_id3,
16447 &cq_set->u.request, eq->queue_id);
16448 break;
16449 case 4:
16450 bf_set(lpfc_mbx_cq_create_set_eq_id4,
16451 &cq_set->u.request, eq->queue_id);
16452 break;
16453 case 5:
16454 bf_set(lpfc_mbx_cq_create_set_eq_id5,
16455 &cq_set->u.request, eq->queue_id);
16456 break;
16457 case 6:
16458 bf_set(lpfc_mbx_cq_create_set_eq_id6,
16459 &cq_set->u.request, eq->queue_id);
16460 break;
16461 case 7:
16462 bf_set(lpfc_mbx_cq_create_set_eq_id7,
16463 &cq_set->u.request, eq->queue_id);
16464 break;
16465 case 8:
16466 bf_set(lpfc_mbx_cq_create_set_eq_id8,
16467 &cq_set->u.request, eq->queue_id);
16468 break;
16469 case 9:
16470 bf_set(lpfc_mbx_cq_create_set_eq_id9,
16471 &cq_set->u.request, eq->queue_id);
16472 break;
16473 case 10:
16474 bf_set(lpfc_mbx_cq_create_set_eq_id10,
16475 &cq_set->u.request, eq->queue_id);
16476 break;
16477 case 11:
16478 bf_set(lpfc_mbx_cq_create_set_eq_id11,
16479 &cq_set->u.request, eq->queue_id);
16480 break;
16481 case 12:
16482 bf_set(lpfc_mbx_cq_create_set_eq_id12,
16483 &cq_set->u.request, eq->queue_id);
16484 break;
16485 case 13:
16486 bf_set(lpfc_mbx_cq_create_set_eq_id13,
16487 &cq_set->u.request, eq->queue_id);
16488 break;
16489 case 14:
16490 bf_set(lpfc_mbx_cq_create_set_eq_id14,
16491 &cq_set->u.request, eq->queue_id);
16492 break;
16493 case 15:
16494 bf_set(lpfc_mbx_cq_create_set_eq_id15,
16495 &cq_set->u.request, eq->queue_id);
16496 break;
16497 }
16498
16499 /* link the cq onto the parent eq child list */
16500 list_add_tail(&cq->list, &eq->child_list);
16501 /* Set up completion queue's type and subtype */
16502 cq->type = type;
16503 cq->subtype = subtype;
16504 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080016505 cq->assoc_qp = eq;
James Smart2d7dbc42017-02-12 13:52:35 -080016506 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016507 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16508 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
16509 cq->entry_count);
James Smart81b96ed2017-11-20 16:00:29 -080016510 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080016511
16512 rc = 0;
16513 list_for_each_entry(dmabuf, &cq->page_list, list) {
16514 memset(dmabuf->virt, 0, hw_page_size);
16515 cnt = page_idx + dmabuf->buffer_tag;
16516 cq_set->u.request.page[cnt].addr_lo =
16517 putPaddrLow(dmabuf->phys);
16518 cq_set->u.request.page[cnt].addr_hi =
16519 putPaddrHigh(dmabuf->phys);
16520 rc++;
16521 }
16522 page_idx += rc;
16523 }
16524
16525 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16526
16527 /* The IOCTL status is embedded in the mailbox subheader. */
16528 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16529 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16530 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016531 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080016532 "3119 CQ_CREATE_SET mailbox failed with "
16533 "status x%x add_status x%x, mbx status x%x\n",
16534 shdr_status, shdr_add_status, rc);
16535 status = -ENXIO;
16536 goto out;
16537 }
16538 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
16539 if (rc == 0xFFFF) {
16540 status = -ENXIO;
16541 goto out;
16542 }
16543
16544 for (idx = 0; idx < numcq; idx++) {
16545 cq = cqp[idx];
16546 cq->queue_id = rc + idx;
James Smart6a828b02019-01-28 11:14:31 -080016547 if (cq->queue_id > phba->sli4_hba.cq_max)
16548 phba->sli4_hba.cq_max = cq->queue_id;
James Smart2d7dbc42017-02-12 13:52:35 -080016549 }
16550
16551out:
16552 lpfc_sli4_mbox_cmd_free(phba, mbox);
16553 return status;
16554}
16555
16556/**
James Smartb19a0612010-04-06 14:48:51 -040016557 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040016558 * @phba: HBA structure that indicates port to create a queue on.
16559 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040016560 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
16561 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040016562 *
James Smartb19a0612010-04-06 14:48:51 -040016563 * This function provides failback (fb) functionality when the
16564 * mq_create_ext fails on older FW generations. It's purpose is identical
16565 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040016566 *
James Smartb19a0612010-04-06 14:48:51 -040016567 * This routine cannot fail as all attributes were previously accessed and
16568 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040016569 **/
James Smartb19a0612010-04-06 14:48:51 -040016570static void
16571lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
16572 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040016573{
16574 struct lpfc_mbx_mq_create *mq_create;
16575 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040016576 int length;
James Smart04c68492009-05-22 14:52:52 -040016577
James Smart04c68492009-05-22 14:52:52 -040016578 length = (sizeof(struct lpfc_mbx_mq_create) -
16579 sizeof(struct lpfc_sli4_cfg_mhdr));
16580 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16581 LPFC_MBOX_OPCODE_MQ_CREATE,
16582 length, LPFC_SLI4_MBX_EMBED);
16583 mq_create = &mbox->u.mqe.un.mq_create;
16584 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040016585 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040016586 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040016587 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040016588 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
16589 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040016590 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050016591 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16592 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040016593 break;
16594 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050016595 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16596 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040016597 break;
16598 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050016599 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16600 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040016601 break;
16602 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050016603 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16604 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040016605 break;
16606 }
16607 list_for_each_entry(dmabuf, &mq->page_list, list) {
16608 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040016609 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040016610 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040016611 putPaddrHigh(dmabuf->phys);
16612 }
16613}
16614
16615/**
16616 * lpfc_mq_create - Create a mailbox Queue on the HBA
16617 * @phba: HBA structure that indicates port to create a queue on.
16618 * @mq: The queue structure to use to create the mailbox queue.
16619 * @cq: The completion queue to associate with this cq.
16620 * @subtype: The queue's subtype.
16621 *
16622 * This function creates a mailbox queue, as detailed in @mq, on a port,
16623 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
16624 *
16625 * The @phba struct is used to send mailbox command to HBA. The @cq struct
16626 * is used to get the entry count and entry size that are necessary to
16627 * determine the number of pages to allocate and use for this queue. This
16628 * function will send the MQ_CREATE mailbox command to the HBA to setup the
16629 * mailbox queue. This function is asynchronous and will wait for the mailbox
16630 * command to finish before continuing.
16631 *
16632 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040016633 * memory this function will return -ENOMEM. If the queue create mailbox command
16634 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040016635 **/
16636int32_t
16637lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
16638 struct lpfc_queue *cq, uint32_t subtype)
16639{
16640 struct lpfc_mbx_mq_create *mq_create;
16641 struct lpfc_mbx_mq_create_ext *mq_create_ext;
16642 struct lpfc_dmabuf *dmabuf;
16643 LPFC_MBOXQ_t *mbox;
16644 int rc, length, status = 0;
16645 uint32_t shdr_status, shdr_add_status;
16646 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040016647 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040016648
James Smart2e90f4b2011-12-13 13:22:37 -050016649 /* sanity check on queue memory */
16650 if (!mq || !cq)
16651 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040016652 if (!phba->sli4_hba.pc_sli4_params.supported)
16653 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040016654
16655 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16656 if (!mbox)
16657 return -ENOMEM;
16658 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
16659 sizeof(struct lpfc_sli4_cfg_mhdr));
16660 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16661 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
16662 length, LPFC_SLI4_MBX_EMBED);
16663
16664 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050016665 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050016666 bf_set(lpfc_mbx_mq_create_ext_num_pages,
16667 &mq_create_ext->u.request, mq->page_count);
16668 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
16669 &mq_create_ext->u.request, 1);
16670 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040016671 &mq_create_ext->u.request, 1);
16672 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
16673 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050016674 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
16675 &mq_create_ext->u.request, 1);
16676 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
16677 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040016678 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050016679 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16680 phba->sli4_hba.pc_sli4_params.mqv);
16681 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
16682 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
16683 cq->queue_id);
16684 else
16685 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
16686 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040016687 switch (mq->entry_count) {
16688 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070016689 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartb19a0612010-04-06 14:48:51 -040016690 "0362 Unsupported MQ count. (%d)\n",
16691 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040016692 if (mq->entry_count < 16) {
16693 status = -EINVAL;
16694 goto out;
16695 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016696 fallthrough; /* otherwise default to smallest count */
James Smartb19a0612010-04-06 14:48:51 -040016697 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050016698 bf_set(lpfc_mq_context_ring_size,
16699 &mq_create_ext->u.request.context,
16700 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040016701 break;
16702 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050016703 bf_set(lpfc_mq_context_ring_size,
16704 &mq_create_ext->u.request.context,
16705 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040016706 break;
16707 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050016708 bf_set(lpfc_mq_context_ring_size,
16709 &mq_create_ext->u.request.context,
16710 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040016711 break;
16712 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050016713 bf_set(lpfc_mq_context_ring_size,
16714 &mq_create_ext->u.request.context,
16715 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040016716 break;
16717 }
16718 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040016719 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040016720 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
16721 putPaddrLow(dmabuf->phys);
16722 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040016723 putPaddrHigh(dmabuf->phys);
16724 }
16725 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040016726 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16727 &mq_create_ext->u.response);
16728 if (rc != MBX_SUCCESS) {
16729 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16730 "2795 MQ_CREATE_EXT failed with "
16731 "status x%x. Failback to MQ_CREATE.\n",
16732 rc);
16733 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
16734 mq_create = &mbox->u.mqe.un.mq_create;
16735 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16736 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
16737 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16738 &mq_create->u.response);
16739 }
16740
James Smart04c68492009-05-22 14:52:52 -040016741 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040016742 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16743 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16744 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016745 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart04c68492009-05-22 14:52:52 -040016746 "2502 MQ_CREATE mailbox failed with "
16747 "status x%x add_status x%x, mbx status x%x\n",
16748 shdr_status, shdr_add_status, rc);
16749 status = -ENXIO;
16750 goto out;
16751 }
James Smart04c68492009-05-22 14:52:52 -040016752 if (mq->queue_id == 0xFFFF) {
16753 status = -ENXIO;
16754 goto out;
16755 }
16756 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050016757 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040016758 mq->subtype = subtype;
16759 mq->host_index = 0;
16760 mq->hba_index = 0;
16761
16762 /* link the mq onto the parent cq child list */
16763 list_add_tail(&mq->list, &cq->child_list);
16764out:
James Smart8fa38512009-07-19 10:01:03 -040016765 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016766 return status;
16767}
16768
16769/**
James Smart4f774512009-05-22 14:52:35 -040016770 * lpfc_wq_create - Create a Work Queue on the HBA
16771 * @phba: HBA structure that indicates port to create a queue on.
16772 * @wq: The queue structure to use to create the work queue.
16773 * @cq: The completion queue to bind this work queue to.
16774 * @subtype: The subtype of the work queue indicating its functionality.
16775 *
16776 * This function creates a work queue, as detailed in @wq, on a port, described
16777 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
16778 *
16779 * The @phba struct is used to send mailbox command to HBA. The @wq struct
16780 * is used to get the entry count and entry size that are necessary to
16781 * determine the number of pages to allocate and use for this queue. The @cq
16782 * is used to indicate which completion queue to bind this work queue to. This
16783 * function will send the WQ_CREATE mailbox command to the HBA to setup the
16784 * work queue. This function is asynchronous and will wait for the mailbox
16785 * command to finish before continuing.
16786 *
16787 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040016788 * memory this function will return -ENOMEM. If the queue create mailbox command
16789 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016790 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016791int
James Smart4f774512009-05-22 14:52:35 -040016792lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
16793 struct lpfc_queue *cq, uint32_t subtype)
16794{
16795 struct lpfc_mbx_wq_create *wq_create;
16796 struct lpfc_dmabuf *dmabuf;
16797 LPFC_MBOXQ_t *mbox;
16798 int rc, length, status = 0;
16799 uint32_t shdr_status, shdr_add_status;
16800 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040016801 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050016802 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050016803 void __iomem *bar_memmap_p;
16804 uint32_t db_offset;
16805 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080016806 uint8_t dpp_barset;
16807 uint32_t dpp_offset;
James Smart81b96ed2017-11-20 16:00:29 -080016808 uint8_t wq_create_version;
Lee Jones11d8e562020-07-21 17:41:44 +010016809#ifdef CONFIG_X86
16810 unsigned long pg_addr;
16811#endif
James Smart49198b32010-04-06 15:04:33 -040016812
James Smart2e90f4b2011-12-13 13:22:37 -050016813 /* sanity check on queue memory */
16814 if (!wq || !cq)
16815 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040016816 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080016817 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040016818
16819 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16820 if (!mbox)
16821 return -ENOMEM;
16822 length = (sizeof(struct lpfc_mbx_wq_create) -
16823 sizeof(struct lpfc_sli4_cfg_mhdr));
16824 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16825 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
16826 length, LPFC_SLI4_MBX_EMBED);
16827 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050016828 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040016829 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
16830 wq->page_count);
16831 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
16832 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040016833
16834 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050016835 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16836 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050016837
James Smartc176ffa2018-01-30 15:58:46 -080016838 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
16839 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080016840 wq_create_version = LPFC_Q_CREATE_VERSION_1;
16841 else
16842 wq_create_version = LPFC_Q_CREATE_VERSION_0;
16843
James Smart1351e692018-02-22 08:18:43 -080016844 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040016845 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050016846 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
16847 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070016848 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16849 LPFC_Q_CREATE_VERSION_1);
16850
James Smart5a6f1332011-03-11 16:05:35 -050016851 switch (wq->entry_size) {
16852 default:
16853 case 64:
16854 bf_set(lpfc_mbx_wq_create_wqe_size,
16855 &wq_create->u.request_1,
16856 LPFC_WQ_WQE_SIZE_64);
16857 break;
16858 case 128:
16859 bf_set(lpfc_mbx_wq_create_wqe_size,
16860 &wq_create->u.request_1,
16861 LPFC_WQ_WQE_SIZE_128);
16862 break;
16863 }
James Smart1351e692018-02-22 08:18:43 -080016864 /* Request DPP by default */
16865 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080016866 bf_set(lpfc_mbx_wq_create_page_size,
16867 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080016868 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050016869 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040016870 break;
16871 default:
James Smart1351e692018-02-22 08:18:43 -080016872 page = wq_create->u.request.page;
16873 break;
James Smart5a6f1332011-03-11 16:05:35 -050016874 }
James Smart0c651872013-07-15 18:33:23 -040016875
James Smart4f774512009-05-22 14:52:35 -040016876 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040016877 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050016878 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
16879 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040016880 }
James Smart962bc512013-01-03 15:44:00 -050016881
16882 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16883 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
16884
James Smart4f774512009-05-22 14:52:35 -040016885 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16886 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040016887 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16888 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16889 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016890 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040016891 "2503 WQ_CREATE mailbox failed with "
16892 "status x%x add_status x%x, mbx status x%x\n",
16893 shdr_status, shdr_add_status, rc);
16894 status = -ENXIO;
16895 goto out;
16896 }
James Smart1351e692018-02-22 08:18:43 -080016897
16898 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
16899 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
16900 &wq_create->u.response);
16901 else
16902 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
16903 &wq_create->u.response_1);
16904
James Smart4f774512009-05-22 14:52:35 -040016905 if (wq->queue_id == 0xFFFF) {
16906 status = -ENXIO;
16907 goto out;
16908 }
James Smart1351e692018-02-22 08:18:43 -080016909
16910 wq->db_format = LPFC_DB_LIST_FORMAT;
16911 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
16912 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16913 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
16914 &wq_create->u.response);
16915 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
16916 (wq->db_format != LPFC_DB_RING_FORMAT)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016917 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080016918 "3265 WQ[%d] doorbell format "
16919 "not supported: x%x\n",
16920 wq->queue_id, wq->db_format);
16921 status = -EINVAL;
16922 goto out;
16923 }
16924 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
16925 &wq_create->u.response);
16926 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16927 pci_barset);
16928 if (!bar_memmap_p) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016929 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080016930 "3263 WQ[%d] failed to memmap "
16931 "pci barset:x%x\n",
16932 wq->queue_id, pci_barset);
16933 status = -ENOMEM;
16934 goto out;
16935 }
16936 db_offset = wq_create->u.response.doorbell_offset;
16937 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
16938 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016939 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080016940 "3252 WQ[%d] doorbell offset "
16941 "not supported: x%x\n",
16942 wq->queue_id, db_offset);
16943 status = -EINVAL;
16944 goto out;
16945 }
16946 wq->db_regaddr = bar_memmap_p + db_offset;
16947 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16948 "3264 WQ[%d]: barset:x%x, offset:x%x, "
16949 "format:x%x\n", wq->queue_id,
16950 pci_barset, db_offset, wq->db_format);
16951 } else
16952 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050016953 } else {
James Smart1351e692018-02-22 08:18:43 -080016954 /* Check if DPP was honored by the firmware */
16955 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
16956 &wq_create->u.response_1);
16957 if (wq->dpp_enable) {
16958 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
16959 &wq_create->u.response_1);
16960 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16961 pci_barset);
16962 if (!bar_memmap_p) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016963 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080016964 "3267 WQ[%d] failed to memmap "
16965 "pci barset:x%x\n",
16966 wq->queue_id, pci_barset);
16967 status = -ENOMEM;
16968 goto out;
16969 }
16970 db_offset = wq_create->u.response_1.doorbell_offset;
16971 wq->db_regaddr = bar_memmap_p + db_offset;
16972 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
16973 &wq_create->u.response_1);
16974 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
16975 &wq_create->u.response_1);
16976 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16977 dpp_barset);
16978 if (!bar_memmap_p) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016979 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080016980 "3268 WQ[%d] failed to memmap "
16981 "pci barset:x%x\n",
16982 wq->queue_id, dpp_barset);
16983 status = -ENOMEM;
16984 goto out;
16985 }
16986 dpp_offset = wq_create->u.response_1.dpp_offset;
16987 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
16988 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16989 "3271 WQ[%d]: barset:x%x, offset:x%x, "
16990 "dpp_id:x%x dpp_barset:x%x "
16991 "dpp_offset:x%x\n",
16992 wq->queue_id, pci_barset, db_offset,
16993 wq->dpp_id, dpp_barset, dpp_offset);
16994
Lee Jones3c1311a2020-07-21 17:41:28 +010016995#ifdef CONFIG_X86
James Smart1351e692018-02-22 08:18:43 -080016996 /* Enable combined writes for DPP aperture */
16997 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
James Smart1351e692018-02-22 08:18:43 -080016998 rc = set_memory_wc(pg_addr, 1);
16999 if (rc) {
17000 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17001 "3272 Cannot setup Combined "
17002 "Write on WQ[%d] - disable DPP\n",
17003 wq->queue_id);
17004 phba->cfg_enable_dpp = 0;
17005 }
17006#else
17007 phba->cfg_enable_dpp = 0;
17008#endif
17009 } else
17010 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050017011 }
James Smart895427b2017-02-12 13:52:30 -080017012 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
17013 if (wq->pring == NULL) {
17014 status = -ENOMEM;
17015 goto out;
17016 }
James Smart4f774512009-05-22 14:52:35 -040017017 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050017018 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040017019 wq->subtype = subtype;
17020 wq->host_index = 0;
17021 wq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080017022 wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040017023
17024 /* link the wq onto the parent cq child list */
17025 list_add_tail(&wq->list, &cq->child_list);
17026out:
James Smart8fa38512009-07-19 10:01:03 -040017027 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017028 return status;
17029}
17030
17031/**
17032 * lpfc_rq_create - Create a Receive Queue on the HBA
17033 * @phba: HBA structure that indicates port to create a queue on.
17034 * @hrq: The queue structure to use to create the header receive queue.
17035 * @drq: The queue structure to use to create the data receive queue.
17036 * @cq: The completion queue to bind this work queue to.
Lee Jones7af29d42020-07-21 17:41:31 +010017037 * @subtype: The subtype of the work queue indicating its functionality.
James Smart4f774512009-05-22 14:52:35 -040017038 *
17039 * This function creates a receive buffer queue pair , as detailed in @hrq and
17040 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17041 * to the HBA.
17042 *
17043 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17044 * struct is used to get the entry count that is necessary to determine the
17045 * number of pages to use for this queue. The @cq is used to indicate which
17046 * completion queue to bind received buffers that are posted to these queues to.
17047 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17048 * receive queue pair. This function is asynchronous and will wait for the
17049 * mailbox command to finish before continuing.
17050 *
17051 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040017052 * memory this function will return -ENOMEM. If the queue create mailbox command
17053 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040017054 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040017055int
James Smart4f774512009-05-22 14:52:35 -040017056lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17057 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
17058{
17059 struct lpfc_mbx_rq_create *rq_create;
17060 struct lpfc_dmabuf *dmabuf;
17061 LPFC_MBOXQ_t *mbox;
17062 int rc, length, status = 0;
17063 uint32_t shdr_status, shdr_add_status;
17064 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040017065 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050017066 void __iomem *bar_memmap_p;
17067 uint32_t db_offset;
17068 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040017069
James Smart2e90f4b2011-12-13 13:22:37 -050017070 /* sanity check on queue memory */
17071 if (!hrq || !drq || !cq)
17072 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040017073 if (!phba->sli4_hba.pc_sli4_params.supported)
17074 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040017075
17076 if (hrq->entry_count != drq->entry_count)
17077 return -EINVAL;
17078 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17079 if (!mbox)
17080 return -ENOMEM;
17081 length = (sizeof(struct lpfc_mbx_rq_create) -
17082 sizeof(struct lpfc_sli4_cfg_mhdr));
17083 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17084 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17085 length, LPFC_SLI4_MBX_EMBED);
17086 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050017087 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17088 bf_set(lpfc_mbox_hdr_version, &shdr->request,
17089 phba->sli4_hba.pc_sli4_params.rqv);
17090 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17091 bf_set(lpfc_rq_context_rqe_count_1,
17092 &rq_create->u.request.context,
17093 hrq->entry_count);
17094 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040017095 bf_set(lpfc_rq_context_rqe_size,
17096 &rq_create->u.request.context,
17097 LPFC_RQE_SIZE_8);
17098 bf_set(lpfc_rq_context_page_size,
17099 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080017100 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050017101 } else {
17102 switch (hrq->entry_count) {
17103 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070017104 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5a6f1332011-03-11 16:05:35 -050017105 "2535 Unsupported RQ count. (%d)\n",
17106 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040017107 if (hrq->entry_count < 512) {
17108 status = -EINVAL;
17109 goto out;
17110 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050017111 fallthrough; /* otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050017112 case 512:
17113 bf_set(lpfc_rq_context_rqe_count,
17114 &rq_create->u.request.context,
17115 LPFC_RQ_RING_SIZE_512);
17116 break;
17117 case 1024:
17118 bf_set(lpfc_rq_context_rqe_count,
17119 &rq_create->u.request.context,
17120 LPFC_RQ_RING_SIZE_1024);
17121 break;
17122 case 2048:
17123 bf_set(lpfc_rq_context_rqe_count,
17124 &rq_create->u.request.context,
17125 LPFC_RQ_RING_SIZE_2048);
17126 break;
17127 case 4096:
17128 bf_set(lpfc_rq_context_rqe_count,
17129 &rq_create->u.request.context,
17130 LPFC_RQ_RING_SIZE_4096);
17131 break;
17132 }
17133 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
17134 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040017135 }
17136 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17137 cq->queue_id);
17138 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17139 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040017140 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040017141 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040017142 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17143 putPaddrLow(dmabuf->phys);
17144 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17145 putPaddrHigh(dmabuf->phys);
17146 }
James Smart962bc512013-01-03 15:44:00 -050017147 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17148 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17149
James Smart4f774512009-05-22 14:52:35 -040017150 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17151 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040017152 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17153 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17154 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017155 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017156 "2504 RQ_CREATE mailbox failed with "
17157 "status x%x add_status x%x, mbx status x%x\n",
17158 shdr_status, shdr_add_status, rc);
17159 status = -ENXIO;
17160 goto out;
17161 }
17162 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17163 if (hrq->queue_id == 0xFFFF) {
17164 status = -ENXIO;
17165 goto out;
17166 }
James Smart962bc512013-01-03 15:44:00 -050017167
17168 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
17169 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
17170 &rq_create->u.response);
17171 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
17172 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017173 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -050017174 "3262 RQ [%d] doorbell format not "
17175 "supported: x%x\n", hrq->queue_id,
17176 hrq->db_format);
17177 status = -EINVAL;
17178 goto out;
17179 }
17180
17181 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
17182 &rq_create->u.response);
17183 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
17184 if (!bar_memmap_p) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017185 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -050017186 "3269 RQ[%d] failed to memmap pci "
17187 "barset:x%x\n", hrq->queue_id,
17188 pci_barset);
17189 status = -ENOMEM;
17190 goto out;
17191 }
17192
17193 db_offset = rq_create->u.response.doorbell_offset;
17194 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
17195 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017196 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -050017197 "3270 RQ[%d] doorbell offset not "
17198 "supported: x%x\n", hrq->queue_id,
17199 db_offset);
17200 status = -EINVAL;
17201 goto out;
17202 }
17203 hrq->db_regaddr = bar_memmap_p + db_offset;
17204 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040017205 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
17206 "format:x%x\n", hrq->queue_id, pci_barset,
17207 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050017208 } else {
17209 hrq->db_format = LPFC_DB_RING_FORMAT;
17210 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17211 }
James Smart4f774512009-05-22 14:52:35 -040017212 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050017213 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040017214 hrq->subtype = subtype;
17215 hrq->host_index = 0;
17216 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080017217 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040017218
17219 /* now create the data queue */
17220 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17221 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17222 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050017223 bf_set(lpfc_mbox_hdr_version, &shdr->request,
17224 phba->sli4_hba.pc_sli4_params.rqv);
17225 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17226 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040017227 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070017228 if (subtype == LPFC_NVMET)
17229 rq_create->u.request.context.buffer_size =
17230 LPFC_NVMET_DATA_BUF_SIZE;
17231 else
17232 rq_create->u.request.context.buffer_size =
17233 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040017234 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
17235 LPFC_RQE_SIZE_8);
17236 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
17237 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050017238 } else {
17239 switch (drq->entry_count) {
17240 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070017241 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5a6f1332011-03-11 16:05:35 -050017242 "2536 Unsupported RQ count. (%d)\n",
17243 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040017244 if (drq->entry_count < 512) {
17245 status = -EINVAL;
17246 goto out;
17247 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050017248 fallthrough; /* otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050017249 case 512:
17250 bf_set(lpfc_rq_context_rqe_count,
17251 &rq_create->u.request.context,
17252 LPFC_RQ_RING_SIZE_512);
17253 break;
17254 case 1024:
17255 bf_set(lpfc_rq_context_rqe_count,
17256 &rq_create->u.request.context,
17257 LPFC_RQ_RING_SIZE_1024);
17258 break;
17259 case 2048:
17260 bf_set(lpfc_rq_context_rqe_count,
17261 &rq_create->u.request.context,
17262 LPFC_RQ_RING_SIZE_2048);
17263 break;
17264 case 4096:
17265 bf_set(lpfc_rq_context_rqe_count,
17266 &rq_create->u.request.context,
17267 LPFC_RQ_RING_SIZE_4096);
17268 break;
17269 }
James Smart3c603be2017-05-15 15:20:44 -070017270 if (subtype == LPFC_NVMET)
17271 bf_set(lpfc_rq_context_buf_size,
17272 &rq_create->u.request.context,
17273 LPFC_NVMET_DATA_BUF_SIZE);
17274 else
17275 bf_set(lpfc_rq_context_buf_size,
17276 &rq_create->u.request.context,
17277 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040017278 }
17279 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17280 cq->queue_id);
17281 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17282 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040017283 list_for_each_entry(dmabuf, &drq->page_list, list) {
17284 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17285 putPaddrLow(dmabuf->phys);
17286 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17287 putPaddrHigh(dmabuf->phys);
17288 }
James Smart962bc512013-01-03 15:44:00 -050017289 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17290 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040017291 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17292 /* The IOCTL status is embedded in the mailbox subheader. */
17293 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17294 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17295 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17296 if (shdr_status || shdr_add_status || rc) {
17297 status = -ENXIO;
17298 goto out;
17299 }
17300 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17301 if (drq->queue_id == 0xFFFF) {
17302 status = -ENXIO;
17303 goto out;
17304 }
17305 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050017306 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040017307 drq->subtype = subtype;
17308 drq->host_index = 0;
17309 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080017310 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040017311
17312 /* link the header and data RQs onto the parent cq child list */
17313 list_add_tail(&hrq->list, &cq->child_list);
17314 list_add_tail(&drq->list, &cq->child_list);
17315
17316out:
James Smart8fa38512009-07-19 10:01:03 -040017317 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017318 return status;
17319}
17320
17321/**
James Smart2d7dbc42017-02-12 13:52:35 -080017322 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
17323 * @phba: HBA structure that indicates port to create a queue on.
17324 * @hrqp: The queue structure array to use to create the header receive queues.
17325 * @drqp: The queue structure array to use to create the data receive queues.
17326 * @cqp: The completion queue array to bind these receive queues to.
Lee Jones7af29d42020-07-21 17:41:31 +010017327 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
James Smart2d7dbc42017-02-12 13:52:35 -080017328 *
17329 * This function creates a receive buffer queue pair , as detailed in @hrq and
17330 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17331 * to the HBA.
17332 *
17333 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17334 * struct is used to get the entry count that is necessary to determine the
17335 * number of pages to use for this queue. The @cq is used to indicate which
17336 * completion queue to bind received buffers that are posted to these queues to.
17337 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17338 * receive queue pair. This function is asynchronous and will wait for the
17339 * mailbox command to finish before continuing.
17340 *
17341 * On success this function will return a zero. If unable to allocate enough
17342 * memory this function will return -ENOMEM. If the queue create mailbox command
17343 * fails this function will return -ENXIO.
17344 **/
17345int
17346lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
17347 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
17348 uint32_t subtype)
17349{
17350 struct lpfc_queue *hrq, *drq, *cq;
17351 struct lpfc_mbx_rq_create_v2 *rq_create;
17352 struct lpfc_dmabuf *dmabuf;
17353 LPFC_MBOXQ_t *mbox;
17354 int rc, length, alloclen, status = 0;
17355 int cnt, idx, numrq, page_idx = 0;
17356 uint32_t shdr_status, shdr_add_status;
17357 union lpfc_sli4_cfg_shdr *shdr;
17358 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17359
17360 numrq = phba->cfg_nvmet_mrq;
17361 /* sanity check on array memory */
17362 if (!hrqp || !drqp || !cqp || !numrq)
17363 return -ENODEV;
17364 if (!phba->sli4_hba.pc_sli4_params.supported)
17365 hw_page_size = SLI4_PAGE_SIZE;
17366
17367 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17368 if (!mbox)
17369 return -ENOMEM;
17370
17371 length = sizeof(struct lpfc_mbx_rq_create_v2);
17372 length += ((2 * numrq * hrqp[0]->page_count) *
17373 sizeof(struct dma_address));
17374
17375 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17376 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
17377 LPFC_SLI4_MBX_NEMBED);
17378 if (alloclen < length) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017379 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080017380 "3099 Allocated DMA memory size (%d) is "
17381 "less than the requested DMA memory size "
17382 "(%d)\n", alloclen, length);
17383 status = -ENOMEM;
17384 goto out;
17385 }
17386
17387
17388
17389 rq_create = mbox->sge_array->addr[0];
17390 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
17391
17392 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
17393 cnt = 0;
17394
17395 for (idx = 0; idx < numrq; idx++) {
17396 hrq = hrqp[idx];
17397 drq = drqp[idx];
17398 cq = cqp[idx];
17399
James Smart2d7dbc42017-02-12 13:52:35 -080017400 /* sanity check on queue memory */
17401 if (!hrq || !drq || !cq) {
17402 status = -ENODEV;
17403 goto out;
17404 }
17405
James Smart7aabe842017-03-04 09:30:22 -080017406 if (hrq->entry_count != drq->entry_count) {
17407 status = -EINVAL;
17408 goto out;
17409 }
17410
James Smart2d7dbc42017-02-12 13:52:35 -080017411 if (idx == 0) {
17412 bf_set(lpfc_mbx_rq_create_num_pages,
17413 &rq_create->u.request,
17414 hrq->page_count);
17415 bf_set(lpfc_mbx_rq_create_rq_cnt,
17416 &rq_create->u.request, (numrq * 2));
17417 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
17418 1);
17419 bf_set(lpfc_rq_context_base_cq,
17420 &rq_create->u.request.context,
17421 cq->queue_id);
17422 bf_set(lpfc_rq_context_data_size,
17423 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070017424 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080017425 bf_set(lpfc_rq_context_hdr_size,
17426 &rq_create->u.request.context,
17427 LPFC_HDR_BUF_SIZE);
17428 bf_set(lpfc_rq_context_rqe_count_1,
17429 &rq_create->u.request.context,
17430 hrq->entry_count);
17431 bf_set(lpfc_rq_context_rqe_size,
17432 &rq_create->u.request.context,
17433 LPFC_RQE_SIZE_8);
17434 bf_set(lpfc_rq_context_page_size,
17435 &rq_create->u.request.context,
17436 (PAGE_SIZE/SLI4_PAGE_SIZE));
17437 }
17438 rc = 0;
17439 list_for_each_entry(dmabuf, &hrq->page_list, list) {
17440 memset(dmabuf->virt, 0, hw_page_size);
17441 cnt = page_idx + dmabuf->buffer_tag;
17442 rq_create->u.request.page[cnt].addr_lo =
17443 putPaddrLow(dmabuf->phys);
17444 rq_create->u.request.page[cnt].addr_hi =
17445 putPaddrHigh(dmabuf->phys);
17446 rc++;
17447 }
17448 page_idx += rc;
17449
17450 rc = 0;
17451 list_for_each_entry(dmabuf, &drq->page_list, list) {
17452 memset(dmabuf->virt, 0, hw_page_size);
17453 cnt = page_idx + dmabuf->buffer_tag;
17454 rq_create->u.request.page[cnt].addr_lo =
17455 putPaddrLow(dmabuf->phys);
17456 rq_create->u.request.page[cnt].addr_hi =
17457 putPaddrHigh(dmabuf->phys);
17458 rc++;
17459 }
17460 page_idx += rc;
17461
17462 hrq->db_format = LPFC_DB_RING_FORMAT;
17463 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17464 hrq->type = LPFC_HRQ;
17465 hrq->assoc_qid = cq->queue_id;
17466 hrq->subtype = subtype;
17467 hrq->host_index = 0;
17468 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080017469 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080017470
17471 drq->db_format = LPFC_DB_RING_FORMAT;
17472 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17473 drq->type = LPFC_DRQ;
17474 drq->assoc_qid = cq->queue_id;
17475 drq->subtype = subtype;
17476 drq->host_index = 0;
17477 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080017478 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080017479
17480 list_add_tail(&hrq->list, &cq->child_list);
17481 list_add_tail(&drq->list, &cq->child_list);
17482 }
17483
17484 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17485 /* The IOCTL status is embedded in the mailbox subheader. */
17486 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17487 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17488 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017489 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080017490 "3120 RQ_CREATE mailbox failed with "
17491 "status x%x add_status x%x, mbx status x%x\n",
17492 shdr_status, shdr_add_status, rc);
17493 status = -ENXIO;
17494 goto out;
17495 }
17496 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17497 if (rc == 0xFFFF) {
17498 status = -ENXIO;
17499 goto out;
17500 }
17501
17502 /* Initialize all RQs with associated queue id */
17503 for (idx = 0; idx < numrq; idx++) {
17504 hrq = hrqp[idx];
17505 hrq->queue_id = rc + (2 * idx);
17506 drq = drqp[idx];
17507 drq->queue_id = rc + (2 * idx) + 1;
17508 }
17509
17510out:
17511 lpfc_sli4_mbox_cmd_free(phba, mbox);
17512 return status;
17513}
17514
17515/**
James Smart4f774512009-05-22 14:52:35 -040017516 * lpfc_eq_destroy - Destroy an event Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010017517 * @phba: HBA structure that indicates port to destroy a queue on.
James Smart4f774512009-05-22 14:52:35 -040017518 * @eq: The queue structure associated with the queue to destroy.
17519 *
17520 * This function destroys a queue, as detailed in @eq by sending an mailbox
17521 * command, specific to the type of queue, to the HBA.
17522 *
17523 * The @eq struct is used to get the queue ID of the queue to destroy.
17524 *
17525 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040017526 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040017527 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040017528int
James Smart4f774512009-05-22 14:52:35 -040017529lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
17530{
17531 LPFC_MBOXQ_t *mbox;
17532 int rc, length, status = 0;
17533 uint32_t shdr_status, shdr_add_status;
17534 union lpfc_sli4_cfg_shdr *shdr;
17535
James Smart2e90f4b2011-12-13 13:22:37 -050017536 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040017537 if (!eq)
17538 return -ENODEV;
James Smart32517fc2019-01-28 11:14:33 -080017539
James Smart4f774512009-05-22 14:52:35 -040017540 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
17541 if (!mbox)
17542 return -ENOMEM;
17543 length = (sizeof(struct lpfc_mbx_eq_destroy) -
17544 sizeof(struct lpfc_sli4_cfg_mhdr));
17545 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17546 LPFC_MBOX_OPCODE_EQ_DESTROY,
17547 length, LPFC_SLI4_MBX_EMBED);
17548 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
17549 eq->queue_id);
17550 mbox->vport = eq->phba->pport;
17551 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17552
17553 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
17554 /* The IOCTL status is embedded in the mailbox subheader. */
17555 shdr = (union lpfc_sli4_cfg_shdr *)
17556 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
17557 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17558 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17559 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017560 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017561 "2505 EQ_DESTROY mailbox failed with "
17562 "status x%x add_status x%x, mbx status x%x\n",
17563 shdr_status, shdr_add_status, rc);
17564 status = -ENXIO;
17565 }
17566
17567 /* Remove eq from any list */
17568 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040017569 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017570 return status;
17571}
17572
17573/**
17574 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010017575 * @phba: HBA structure that indicates port to destroy a queue on.
James Smart4f774512009-05-22 14:52:35 -040017576 * @cq: The queue structure associated with the queue to destroy.
17577 *
17578 * This function destroys a queue, as detailed in @cq by sending an mailbox
17579 * command, specific to the type of queue, to the HBA.
17580 *
17581 * The @cq struct is used to get the queue ID of the queue to destroy.
17582 *
17583 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040017584 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040017585 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040017586int
James Smart4f774512009-05-22 14:52:35 -040017587lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
17588{
17589 LPFC_MBOXQ_t *mbox;
17590 int rc, length, status = 0;
17591 uint32_t shdr_status, shdr_add_status;
17592 union lpfc_sli4_cfg_shdr *shdr;
17593
James Smart2e90f4b2011-12-13 13:22:37 -050017594 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040017595 if (!cq)
17596 return -ENODEV;
17597 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
17598 if (!mbox)
17599 return -ENOMEM;
17600 length = (sizeof(struct lpfc_mbx_cq_destroy) -
17601 sizeof(struct lpfc_sli4_cfg_mhdr));
17602 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17603 LPFC_MBOX_OPCODE_CQ_DESTROY,
17604 length, LPFC_SLI4_MBX_EMBED);
17605 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
17606 cq->queue_id);
17607 mbox->vport = cq->phba->pport;
17608 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17609 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
17610 /* The IOCTL status is embedded in the mailbox subheader. */
17611 shdr = (union lpfc_sli4_cfg_shdr *)
17612 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
17613 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17614 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17615 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017616 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017617 "2506 CQ_DESTROY mailbox failed with "
17618 "status x%x add_status x%x, mbx status x%x\n",
17619 shdr_status, shdr_add_status, rc);
17620 status = -ENXIO;
17621 }
17622 /* Remove cq from any list */
17623 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040017624 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017625 return status;
17626}
17627
17628/**
James Smart04c68492009-05-22 14:52:52 -040017629 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010017630 * @phba: HBA structure that indicates port to destroy a queue on.
17631 * @mq: The queue structure associated with the queue to destroy.
James Smart04c68492009-05-22 14:52:52 -040017632 *
17633 * This function destroys a queue, as detailed in @mq by sending an mailbox
17634 * command, specific to the type of queue, to the HBA.
17635 *
17636 * The @mq struct is used to get the queue ID of the queue to destroy.
17637 *
17638 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040017639 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040017640 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040017641int
James Smart04c68492009-05-22 14:52:52 -040017642lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
17643{
17644 LPFC_MBOXQ_t *mbox;
17645 int rc, length, status = 0;
17646 uint32_t shdr_status, shdr_add_status;
17647 union lpfc_sli4_cfg_shdr *shdr;
17648
James Smart2e90f4b2011-12-13 13:22:37 -050017649 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040017650 if (!mq)
17651 return -ENODEV;
17652 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
17653 if (!mbox)
17654 return -ENOMEM;
17655 length = (sizeof(struct lpfc_mbx_mq_destroy) -
17656 sizeof(struct lpfc_sli4_cfg_mhdr));
17657 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17658 LPFC_MBOX_OPCODE_MQ_DESTROY,
17659 length, LPFC_SLI4_MBX_EMBED);
17660 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
17661 mq->queue_id);
17662 mbox->vport = mq->phba->pport;
17663 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17664 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
17665 /* The IOCTL status is embedded in the mailbox subheader. */
17666 shdr = (union lpfc_sli4_cfg_shdr *)
17667 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
17668 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17669 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17670 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017671 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart04c68492009-05-22 14:52:52 -040017672 "2507 MQ_DESTROY mailbox failed with "
17673 "status x%x add_status x%x, mbx status x%x\n",
17674 shdr_status, shdr_add_status, rc);
17675 status = -ENXIO;
17676 }
17677 /* Remove mq from any list */
17678 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040017679 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040017680 return status;
17681}
17682
17683/**
James Smart4f774512009-05-22 14:52:35 -040017684 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010017685 * @phba: HBA structure that indicates port to destroy a queue on.
James Smart4f774512009-05-22 14:52:35 -040017686 * @wq: The queue structure associated with the queue to destroy.
17687 *
17688 * This function destroys a queue, as detailed in @wq by sending an mailbox
17689 * command, specific to the type of queue, to the HBA.
17690 *
17691 * The @wq struct is used to get the queue ID of the queue to destroy.
17692 *
17693 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040017694 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040017695 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040017696int
James Smart4f774512009-05-22 14:52:35 -040017697lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
17698{
17699 LPFC_MBOXQ_t *mbox;
17700 int rc, length, status = 0;
17701 uint32_t shdr_status, shdr_add_status;
17702 union lpfc_sli4_cfg_shdr *shdr;
17703
James Smart2e90f4b2011-12-13 13:22:37 -050017704 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040017705 if (!wq)
17706 return -ENODEV;
17707 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
17708 if (!mbox)
17709 return -ENOMEM;
17710 length = (sizeof(struct lpfc_mbx_wq_destroy) -
17711 sizeof(struct lpfc_sli4_cfg_mhdr));
17712 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17713 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
17714 length, LPFC_SLI4_MBX_EMBED);
17715 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
17716 wq->queue_id);
17717 mbox->vport = wq->phba->pport;
17718 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17719 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
17720 shdr = (union lpfc_sli4_cfg_shdr *)
17721 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
17722 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17723 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17724 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017725 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017726 "2508 WQ_DESTROY mailbox failed with "
17727 "status x%x add_status x%x, mbx status x%x\n",
17728 shdr_status, shdr_add_status, rc);
17729 status = -ENXIO;
17730 }
17731 /* Remove wq from any list */
17732 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070017733 kfree(wq->pring);
17734 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040017735 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017736 return status;
17737}
17738
17739/**
17740 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010017741 * @phba: HBA structure that indicates port to destroy a queue on.
17742 * @hrq: The queue structure associated with the queue to destroy.
17743 * @drq: The queue structure associated with the queue to destroy.
James Smart4f774512009-05-22 14:52:35 -040017744 *
17745 * This function destroys a queue, as detailed in @rq by sending an mailbox
17746 * command, specific to the type of queue, to the HBA.
17747 *
17748 * The @rq struct is used to get the queue ID of the queue to destroy.
17749 *
17750 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040017751 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040017752 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040017753int
James Smart4f774512009-05-22 14:52:35 -040017754lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17755 struct lpfc_queue *drq)
17756{
17757 LPFC_MBOXQ_t *mbox;
17758 int rc, length, status = 0;
17759 uint32_t shdr_status, shdr_add_status;
17760 union lpfc_sli4_cfg_shdr *shdr;
17761
James Smart2e90f4b2011-12-13 13:22:37 -050017762 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040017763 if (!hrq || !drq)
17764 return -ENODEV;
17765 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
17766 if (!mbox)
17767 return -ENOMEM;
17768 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050017769 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040017770 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17771 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
17772 length, LPFC_SLI4_MBX_EMBED);
17773 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17774 hrq->queue_id);
17775 mbox->vport = hrq->phba->pport;
17776 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17777 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
17778 /* The IOCTL status is embedded in the mailbox subheader. */
17779 shdr = (union lpfc_sli4_cfg_shdr *)
17780 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17781 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17782 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17783 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017784 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017785 "2509 RQ_DESTROY mailbox failed with "
17786 "status x%x add_status x%x, mbx status x%x\n",
17787 shdr_status, shdr_add_status, rc);
James Smart304ee432021-04-11 18:31:17 -070017788 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017789 return -ENXIO;
17790 }
17791 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17792 drq->queue_id);
17793 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
17794 shdr = (union lpfc_sli4_cfg_shdr *)
17795 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17796 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17797 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17798 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017799 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017800 "2510 RQ_DESTROY mailbox failed with "
17801 "status x%x add_status x%x, mbx status x%x\n",
17802 shdr_status, shdr_add_status, rc);
17803 status = -ENXIO;
17804 }
17805 list_del_init(&hrq->list);
17806 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040017807 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017808 return status;
17809}
17810
17811/**
17812 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
17813 * @phba: The virtual port for which this call being executed.
17814 * @pdma_phys_addr0: Physical address of the 1st SGL page.
17815 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
17816 * @xritag: the xritag that ties this io to the SGL pages.
17817 *
17818 * This routine will post the sgl pages for the IO that has the xritag
17819 * that is in the iocbq structure. The xritag is assigned during iocbq
17820 * creation and persists for as long as the driver is loaded.
17821 * if the caller has fewer than 256 scatter gather segments to map then
17822 * pdma_phys_addr1 should be 0.
17823 * If the caller needs to map more than 256 scatter gather segment then
17824 * pdma_phys_addr1 should be a valid physical address.
17825 * physical address for SGLs must be 64 byte aligned.
17826 * If you are going to map 2 SGL's then the first one must have 256 entries
17827 * the second sgl can have between 1 and 256 entries.
17828 *
17829 * Return codes:
17830 * 0 - Success
17831 * -ENXIO, -ENOMEM - Failure
17832 **/
17833int
17834lpfc_sli4_post_sgl(struct lpfc_hba *phba,
17835 dma_addr_t pdma_phys_addr0,
17836 dma_addr_t pdma_phys_addr1,
17837 uint16_t xritag)
17838{
17839 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
17840 LPFC_MBOXQ_t *mbox;
17841 int rc;
17842 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040017843 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040017844 union lpfc_sli4_cfg_shdr *shdr;
17845
17846 if (xritag == NO_XRI) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017847 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017848 "0364 Invalid param:\n");
17849 return -EINVAL;
17850 }
17851
17852 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17853 if (!mbox)
17854 return -ENOMEM;
17855
17856 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17857 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17858 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050017859 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040017860
17861 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
17862 &mbox->u.mqe.un.post_sgl_pages;
17863 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
17864 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
17865
17866 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
17867 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
17868 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
17869 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
17870
17871 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
17872 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
17873 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
17874 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
17875 if (!phba->sli4_hba.intr_enable)
17876 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040017877 else {
James Smarta183a152011-10-10 21:32:43 -040017878 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040017879 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17880 }
James Smart4f774512009-05-22 14:52:35 -040017881 /* The IOCTL status is embedded in the mailbox subheader. */
17882 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
17883 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17884 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart304ee432021-04-11 18:31:17 -070017885 if (!phba->sli4_hba.intr_enable)
17886 mempool_free(mbox, phba->mbox_mem_pool);
17887 else if (rc != MBX_TIMEOUT)
James Smart4f774512009-05-22 14:52:35 -040017888 mempool_free(mbox, phba->mbox_mem_pool);
17889 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017890 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017891 "2511 POST_SGL mailbox failed with "
17892 "status x%x add_status x%x, mbx status x%x\n",
17893 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040017894 }
17895 return 0;
17896}
James Smart4f774512009-05-22 14:52:35 -040017897
17898/**
James Smart88a2cfb2011-07-22 18:36:33 -040017899 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040017900 * @phba: pointer to lpfc hba data structure.
17901 *
17902 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040017903 * HBA consistent with the SLI-4 interface spec. This routine
17904 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17905 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040017906 *
James Smart88a2cfb2011-07-22 18:36:33 -040017907 * Returns
17908 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
17909 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17910 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017911static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017912lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
17913{
17914 unsigned long xri;
17915
17916 /*
17917 * Fetch the next logical xri. Because this index is logical,
17918 * the driver starts at 0 each time.
17919 */
17920 spin_lock_irq(&phba->hbalock);
17921 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
17922 phba->sli4_hba.max_cfg_param.max_xri, 0);
17923 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
17924 spin_unlock_irq(&phba->hbalock);
17925 return NO_XRI;
17926 } else {
17927 set_bit(xri, phba->sli4_hba.xri_bmask);
17928 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040017929 }
James Smart6d368e52011-05-24 11:44:12 -040017930 spin_unlock_irq(&phba->hbalock);
17931 return xri;
17932}
17933
17934/**
Lee Jones8514e2f2021-03-03 14:46:18 +000017935 * __lpfc_sli4_free_xri - Release an xri for reuse.
James Smart6d368e52011-05-24 11:44:12 -040017936 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010017937 * @xri: xri to release.
James Smart6d368e52011-05-24 11:44:12 -040017938 *
17939 * This routine is invoked to release an xri to the pool of
17940 * available rpis maintained by the driver.
17941 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017942static void
James Smart6d368e52011-05-24 11:44:12 -040017943__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
17944{
17945 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040017946 phba->sli4_hba.max_cfg_param.xri_used--;
17947 }
17948}
17949
17950/**
17951 * lpfc_sli4_free_xri - Release an xri for reuse.
17952 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010017953 * @xri: xri to release.
James Smart6d368e52011-05-24 11:44:12 -040017954 *
17955 * This routine is invoked to release an xri to the pool of
17956 * available rpis maintained by the driver.
17957 **/
17958void
17959lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
17960{
17961 spin_lock_irq(&phba->hbalock);
17962 __lpfc_sli4_free_xri(phba, xri);
17963 spin_unlock_irq(&phba->hbalock);
17964}
17965
17966/**
James Smart4f774512009-05-22 14:52:35 -040017967 * lpfc_sli4_next_xritag - Get an xritag for the io
17968 * @phba: Pointer to HBA context object.
17969 *
17970 * This function gets an xritag for the iocb. If there is no unused xritag
17971 * it will return 0xffff.
17972 * The function returns the allocated xritag if successful, else returns zero.
17973 * Zero is not a valid xritag.
17974 * The caller is not required to hold any lock.
17975 **/
17976uint16_t
17977lpfc_sli4_next_xritag(struct lpfc_hba *phba)
17978{
James Smart6d368e52011-05-24 11:44:12 -040017979 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040017980
James Smart6d368e52011-05-24 11:44:12 -040017981 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040017982 if (xri_index == NO_XRI)
17983 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17984 "2004 Failed to allocate XRI.last XRITAG is %d"
17985 " Max XRI is %d, Used XRI is %d\n",
17986 xri_index,
17987 phba->sli4_hba.max_cfg_param.max_xri,
17988 phba->sli4_hba.max_cfg_param.xri_used);
17989 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040017990}
17991
17992/**
James Smart895427b2017-02-12 13:52:30 -080017993 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040017994 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040017995 * @post_sgl_list: pointer to els sgl entry list.
Lee Jones7af29d42020-07-21 17:41:31 +010017996 * @post_cnt: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040017997 *
17998 * This routine is invoked to post a block of driver's sgl pages to the
17999 * HBA using non-embedded mailbox command. No Lock is held. This routine
18000 * is only called when the driver is loading and after all IO has been
18001 * stopped.
18002 **/
James Smart8a9d2e82012-05-09 21:16:12 -040018003static int
James Smart895427b2017-02-12 13:52:30 -080018004lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040018005 struct list_head *post_sgl_list,
18006 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040018007{
James Smart8a9d2e82012-05-09 21:16:12 -040018008 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040018009 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18010 struct sgl_page_pairs *sgl_pg_pairs;
18011 void *viraddr;
18012 LPFC_MBOXQ_t *mbox;
18013 uint32_t reqlen, alloclen, pg_pairs;
18014 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040018015 uint16_t xritag_start = 0;
18016 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040018017 uint32_t shdr_status, shdr_add_status;
18018 union lpfc_sli4_cfg_shdr *shdr;
18019
James Smart895427b2017-02-12 13:52:30 -080018020 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040018021 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040018022 if (reqlen > SLI4_PAGE_SIZE) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018023 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040018024 "2559 Block sgl registration required DMA "
18025 "size (%d) great than a page\n", reqlen);
18026 return -ENOMEM;
18027 }
James Smart895427b2017-02-12 13:52:30 -080018028
James Smart4f774512009-05-22 14:52:35 -040018029 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040018030 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040018031 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040018032
18033 /* Allocate DMA memory and set up the non-embedded mailbox command */
18034 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18035 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
18036 LPFC_SLI4_MBX_NEMBED);
18037
18038 if (alloclen < reqlen) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018039 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040018040 "0285 Allocated DMA memory size (%d) is "
18041 "less than the requested DMA memory "
18042 "size (%d)\n", alloclen, reqlen);
18043 lpfc_sli4_mbox_cmd_free(phba, mbox);
18044 return -ENOMEM;
18045 }
James Smart4f774512009-05-22 14:52:35 -040018046 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040018047 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040018048 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18049 sgl_pg_pairs = &sgl->sgl_pg_pairs;
18050
James Smart8a9d2e82012-05-09 21:16:12 -040018051 pg_pairs = 0;
18052 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040018053 /* Set up the sge entry */
18054 sgl_pg_pairs->sgl_pg0_addr_lo =
18055 cpu_to_le32(putPaddrLow(sglq_entry->phys));
18056 sgl_pg_pairs->sgl_pg0_addr_hi =
18057 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
18058 sgl_pg_pairs->sgl_pg1_addr_lo =
18059 cpu_to_le32(putPaddrLow(0));
18060 sgl_pg_pairs->sgl_pg1_addr_hi =
18061 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040018062
James Smart4f774512009-05-22 14:52:35 -040018063 /* Keep the first xritag on the list */
18064 if (pg_pairs == 0)
18065 xritag_start = sglq_entry->sli4_xritag;
18066 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040018067 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040018068 }
James Smart6d368e52011-05-24 11:44:12 -040018069
18070 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040018071 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080018072 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040018073 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080018074
James Smart4f774512009-05-22 14:52:35 -040018075 if (!phba->sli4_hba.intr_enable)
18076 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18077 else {
James Smarta183a152011-10-10 21:32:43 -040018078 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040018079 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18080 }
18081 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
18082 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18083 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart304ee432021-04-11 18:31:17 -070018084 if (!phba->sli4_hba.intr_enable)
18085 lpfc_sli4_mbox_cmd_free(phba, mbox);
18086 else if (rc != MBX_TIMEOUT)
James Smart4f774512009-05-22 14:52:35 -040018087 lpfc_sli4_mbox_cmd_free(phba, mbox);
18088 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018089 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040018090 "2513 POST_SGL_BLOCK mailbox command failed "
18091 "status x%x add_status x%x mbx status x%x\n",
18092 shdr_status, shdr_add_status, rc);
18093 rc = -ENXIO;
18094 }
18095 return rc;
18096}
18097
18098/**
James Smart5e5b5112019-01-28 11:14:22 -080018099 * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
James Smart4f774512009-05-22 14:52:35 -040018100 * @phba: pointer to lpfc hba data structure.
James Smart0794d602019-01-28 11:14:19 -080018101 * @nblist: pointer to nvme buffer list.
James Smart4f774512009-05-22 14:52:35 -040018102 * @count: number of scsi buffers on the list.
18103 *
18104 * This routine is invoked to post a block of @count scsi sgl pages from a
James Smart0794d602019-01-28 11:14:19 -080018105 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
James Smart4f774512009-05-22 14:52:35 -040018106 * No Lock is held.
18107 *
18108 **/
James Smart0794d602019-01-28 11:14:19 -080018109static int
James Smart5e5b5112019-01-28 11:14:22 -080018110lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
18111 int count)
James Smart4f774512009-05-22 14:52:35 -040018112{
James Smartc4908502019-01-28 11:14:28 -080018113 struct lpfc_io_buf *lpfc_ncmd;
James Smart4f774512009-05-22 14:52:35 -040018114 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18115 struct sgl_page_pairs *sgl_pg_pairs;
18116 void *viraddr;
18117 LPFC_MBOXQ_t *mbox;
18118 uint32_t reqlen, alloclen, pg_pairs;
18119 uint32_t mbox_tmo;
18120 uint16_t xritag_start = 0;
18121 int rc = 0;
18122 uint32_t shdr_status, shdr_add_status;
18123 dma_addr_t pdma_phys_bpl1;
18124 union lpfc_sli4_cfg_shdr *shdr;
18125
18126 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040018127 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040018128 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040018129 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040018130 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080018131 "6118 Block sgl registration required DMA "
James Smart4f774512009-05-22 14:52:35 -040018132 "size (%d) great than a page\n", reqlen);
18133 return -ENOMEM;
18134 }
18135 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18136 if (!mbox) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018137 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -080018138 "6119 Failed to allocate mbox cmd memory\n");
James Smart4f774512009-05-22 14:52:35 -040018139 return -ENOMEM;
18140 }
18141
18142 /* Allocate DMA memory and set up the non-embedded mailbox command */
18143 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
James Smart0794d602019-01-28 11:14:19 -080018144 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
18145 reqlen, LPFC_SLI4_MBX_NEMBED);
James Smart4f774512009-05-22 14:52:35 -040018146
18147 if (alloclen < reqlen) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018148 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -080018149 "6120 Allocated DMA memory size (%d) is "
James Smart4f774512009-05-22 14:52:35 -040018150 "less than the requested DMA memory "
18151 "size (%d)\n", alloclen, reqlen);
18152 lpfc_sli4_mbox_cmd_free(phba, mbox);
18153 return -ENOMEM;
18154 }
James Smart6d368e52011-05-24 11:44:12 -040018155
James Smart4f774512009-05-22 14:52:35 -040018156 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040018157 viraddr = mbox->sge_array->addr[0];
18158
18159 /* Set up the SGL pages in the non-embedded DMA pages */
18160 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18161 sgl_pg_pairs = &sgl->sgl_pg_pairs;
18162
18163 pg_pairs = 0;
James Smart0794d602019-01-28 11:14:19 -080018164 list_for_each_entry(lpfc_ncmd, nblist, list) {
James Smart4f774512009-05-22 14:52:35 -040018165 /* Set up the sge entry */
18166 sgl_pg_pairs->sgl_pg0_addr_lo =
James Smart0794d602019-01-28 11:14:19 -080018167 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040018168 sgl_pg_pairs->sgl_pg0_addr_hi =
James Smart0794d602019-01-28 11:14:19 -080018169 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040018170 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
James Smart0794d602019-01-28 11:14:19 -080018171 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
18172 SGL_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040018173 else
18174 pdma_phys_bpl1 = 0;
18175 sgl_pg_pairs->sgl_pg1_addr_lo =
18176 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
18177 sgl_pg_pairs->sgl_pg1_addr_hi =
18178 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
18179 /* Keep the first xritag on the list */
18180 if (pg_pairs == 0)
James Smart0794d602019-01-28 11:14:19 -080018181 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040018182 sgl_pg_pairs++;
18183 pg_pairs++;
18184 }
18185 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18186 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
18187 /* Perform endian conversion if necessary */
18188 sgl->word0 = cpu_to_le32(sgl->word0);
18189
James Smart0794d602019-01-28 11:14:19 -080018190 if (!phba->sli4_hba.intr_enable) {
James Smart4f774512009-05-22 14:52:35 -040018191 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart0794d602019-01-28 11:14:19 -080018192 } else {
James Smarta183a152011-10-10 21:32:43 -040018193 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040018194 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18195 }
James Smart0794d602019-01-28 11:14:19 -080018196 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040018197 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18198 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart304ee432021-04-11 18:31:17 -070018199 if (!phba->sli4_hba.intr_enable)
18200 lpfc_sli4_mbox_cmd_free(phba, mbox);
18201 else if (rc != MBX_TIMEOUT)
James Smart4f774512009-05-22 14:52:35 -040018202 lpfc_sli4_mbox_cmd_free(phba, mbox);
18203 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018204 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -080018205 "6125 POST_SGL_BLOCK mailbox command failed "
James Smart4f774512009-05-22 14:52:35 -040018206 "status x%x add_status x%x mbx status x%x\n",
18207 shdr_status, shdr_add_status, rc);
18208 rc = -ENXIO;
18209 }
18210 return rc;
18211}
18212
18213/**
James Smart5e5b5112019-01-28 11:14:22 -080018214 * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
James Smart0794d602019-01-28 11:14:19 -080018215 * @phba: pointer to lpfc hba data structure.
18216 * @post_nblist: pointer to the nvme buffer list.
Lee Jones7af29d42020-07-21 17:41:31 +010018217 * @sb_count: number of nvme buffers.
James Smart0794d602019-01-28 11:14:19 -080018218 *
18219 * This routine walks a list of nvme buffers that was passed in. It attempts
18220 * to construct blocks of nvme buffer sgls which contains contiguous xris and
18221 * uses the non-embedded SGL block post mailbox commands to post to the port.
18222 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
18223 * embedded SGL post mailbox command for posting. The @post_nblist passed in
18224 * must be local list, thus no lock is needed when manipulate the list.
18225 *
18226 * Returns: 0 = failure, non-zero number of successfully posted buffers.
18227 **/
18228int
James Smart5e5b5112019-01-28 11:14:22 -080018229lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
18230 struct list_head *post_nblist, int sb_count)
James Smart0794d602019-01-28 11:14:19 -080018231{
James Smartc4908502019-01-28 11:14:28 -080018232 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart0794d602019-01-28 11:14:19 -080018233 int status, sgl_size;
18234 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
18235 dma_addr_t pdma_phys_sgl1;
18236 int last_xritag = NO_XRI;
18237 int cur_xritag;
James Smart0794d602019-01-28 11:14:19 -080018238 LIST_HEAD(prep_nblist);
18239 LIST_HEAD(blck_nblist);
18240 LIST_HEAD(nvme_nblist);
18241
18242 /* sanity check */
18243 if (sb_count <= 0)
18244 return -EINVAL;
18245
18246 sgl_size = phba->cfg_sg_dma_buf_size;
18247 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
18248 list_del_init(&lpfc_ncmd->list);
18249 block_cnt++;
18250 if ((last_xritag != NO_XRI) &&
18251 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
18252 /* a hole in xri block, form a sgl posting block */
18253 list_splice_init(&prep_nblist, &blck_nblist);
18254 post_cnt = block_cnt - 1;
18255 /* prepare list for next posting block */
18256 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18257 block_cnt = 1;
18258 } else {
18259 /* prepare list for next posting block */
18260 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18261 /* enough sgls for non-embed sgl mbox command */
18262 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
18263 list_splice_init(&prep_nblist, &blck_nblist);
18264 post_cnt = block_cnt;
18265 block_cnt = 0;
18266 }
18267 }
18268 num_posting++;
18269 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18270
18271 /* end of repost sgl list condition for NVME buffers */
18272 if (num_posting == sb_count) {
18273 if (post_cnt == 0) {
18274 /* last sgl posting block */
18275 list_splice_init(&prep_nblist, &blck_nblist);
18276 post_cnt = block_cnt;
18277 } else if (block_cnt == 1) {
18278 /* last single sgl with non-contiguous xri */
18279 if (sgl_size > SGL_PAGE_SIZE)
18280 pdma_phys_sgl1 =
18281 lpfc_ncmd->dma_phys_sgl +
18282 SGL_PAGE_SIZE;
18283 else
18284 pdma_phys_sgl1 = 0;
18285 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18286 status = lpfc_sli4_post_sgl(
18287 phba, lpfc_ncmd->dma_phys_sgl,
18288 pdma_phys_sgl1, cur_xritag);
18289 if (status) {
James Smartc4908502019-01-28 11:14:28 -080018290 /* Post error. Buffer unavailable. */
18291 lpfc_ncmd->flags |=
18292 LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080018293 } else {
James Smartc4908502019-01-28 11:14:28 -080018294 /* Post success. Bffer available. */
18295 lpfc_ncmd->flags &=
18296 ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080018297 lpfc_ncmd->status = IOSTAT_SUCCESS;
18298 num_posted++;
18299 }
18300 /* success, put on NVME buffer sgl list */
18301 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18302 }
18303 }
18304
18305 /* continue until a nembed page worth of sgls */
18306 if (post_cnt == 0)
18307 continue;
18308
18309 /* post block of NVME buffer list sgls */
James Smart5e5b5112019-01-28 11:14:22 -080018310 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
18311 post_cnt);
James Smart0794d602019-01-28 11:14:19 -080018312
18313 /* don't reset xirtag due to hole in xri block */
18314 if (block_cnt == 0)
18315 last_xritag = NO_XRI;
18316
18317 /* reset NVME buffer post count for next round of posting */
18318 post_cnt = 0;
18319
18320 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
18321 while (!list_empty(&blck_nblist)) {
18322 list_remove_head(&blck_nblist, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -080018323 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -080018324 if (status) {
James Smartc4908502019-01-28 11:14:28 -080018325 /* Post error. Mark buffer unavailable. */
18326 lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080018327 } else {
James Smartc4908502019-01-28 11:14:28 -080018328 /* Post success, Mark buffer available. */
18329 lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080018330 lpfc_ncmd->status = IOSTAT_SUCCESS;
18331 num_posted++;
18332 }
18333 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18334 }
18335 }
18336 /* Push NVME buffers with sgl posted to the available list */
James Smart5e5b5112019-01-28 11:14:22 -080018337 lpfc_io_buf_replenish(phba, &nvme_nblist);
18338
James Smart0794d602019-01-28 11:14:19 -080018339 return num_posted;
18340}
18341
18342/**
James Smart4f774512009-05-22 14:52:35 -040018343 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
18344 * @phba: pointer to lpfc_hba struct that the frame was received on
18345 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18346 *
18347 * This function checks the fields in the @fc_hdr to see if the FC frame is a
18348 * valid type of frame that the LPFC driver will handle. This function will
18349 * return a zero if the frame is a valid frame or a non zero value when the
18350 * frame does not pass the check.
18351 **/
18352static int
18353lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
18354{
Tomas Henzl474ffb72010-12-22 16:52:40 +010018355 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040018356 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050018357 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040018358
James Smarte62245d2019-08-14 16:57:08 -070018359#define FC_RCTL_MDS_DIAGS 0xF4
18360
James Smart4f774512009-05-22 14:52:35 -040018361 switch (fc_hdr->fh_r_ctl) {
18362 case FC_RCTL_DD_UNCAT: /* uncategorized information */
18363 case FC_RCTL_DD_SOL_DATA: /* solicited data */
18364 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
18365 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
18366 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
18367 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
18368 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
18369 case FC_RCTL_DD_CMD_STATUS: /* command status */
18370 case FC_RCTL_ELS_REQ: /* extended link services request */
18371 case FC_RCTL_ELS_REP: /* extended link services reply */
18372 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
18373 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
18374 case FC_RCTL_BA_NOP: /* basic link service NOP */
18375 case FC_RCTL_BA_ABTS: /* basic link service abort */
18376 case FC_RCTL_BA_RMC: /* remove connection */
18377 case FC_RCTL_BA_ACC: /* basic accept */
18378 case FC_RCTL_BA_RJT: /* basic reject */
18379 case FC_RCTL_BA_PRMT:
18380 case FC_RCTL_ACK_1: /* acknowledge_1 */
18381 case FC_RCTL_ACK_0: /* acknowledge_0 */
18382 case FC_RCTL_P_RJT: /* port reject */
18383 case FC_RCTL_F_RJT: /* fabric reject */
18384 case FC_RCTL_P_BSY: /* port busy */
18385 case FC_RCTL_F_BSY: /* fabric busy to data frame */
18386 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
18387 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070018388 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040018389 case FC_RCTL_END: /* end */
18390 break;
18391 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
18392 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18393 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
18394 return lpfc_fc_frame_check(phba, fc_hdr);
18395 default:
18396 goto drop;
18397 }
James Smartae9e28f2017-05-15 15:20:51 -070018398
James Smart4f774512009-05-22 14:52:35 -040018399 switch (fc_hdr->fh_type) {
18400 case FC_TYPE_BLS:
18401 case FC_TYPE_ELS:
18402 case FC_TYPE_FCP:
18403 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080018404 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040018405 break;
18406 case FC_TYPE_IP:
18407 case FC_TYPE_ILS:
18408 default:
18409 goto drop;
18410 }
James Smart546fc852011-03-11 16:06:29 -050018411
James Smart4f774512009-05-22 14:52:35 -040018412 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070018413 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040018414 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070018415 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
18416 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
18417 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
18418 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
18419 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040018420 return 0;
18421drop:
18422 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070018423 "2539 Dropped frame rctl:x%x type:x%x\n",
18424 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040018425 return 1;
18426}
18427
18428/**
18429 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
18430 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18431 *
18432 * This function processes the FC header to retrieve the VFI from the VF
18433 * header, if one exists. This function will return the VFI if one exists
18434 * or 0 if no VSAN Header exists.
18435 **/
18436static uint32_t
18437lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
18438{
18439 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18440
18441 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
18442 return 0;
18443 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
18444}
18445
18446/**
18447 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
18448 * @phba: Pointer to the HBA structure to search for the vport on
18449 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18450 * @fcfi: The FC Fabric ID that the frame came from
Lee Jones7af29d42020-07-21 17:41:31 +010018451 * @did: Destination ID to match against
James Smart4f774512009-05-22 14:52:35 -040018452 *
18453 * This function searches the @phba for a vport that matches the content of the
18454 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
18455 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
18456 * returns the matching vport pointer or NULL if unable to match frame to a
18457 * vport.
18458 **/
18459static struct lpfc_vport *
18460lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080018461 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040018462{
18463 struct lpfc_vport **vports;
18464 struct lpfc_vport *vport = NULL;
18465 int i;
James Smart939723a2012-05-09 21:19:03 -040018466
James Smartbf086112011-08-21 21:48:13 -040018467 if (did == Fabric_DID)
18468 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040018469 if ((phba->pport->fc_flag & FC_PT2PT) &&
18470 !(phba->link_state == LPFC_HBA_READY))
18471 return phba->pport;
18472
James Smart4f774512009-05-22 14:52:35 -040018473 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080018474 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040018475 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
18476 if (phba->fcf.fcfi == fcfi &&
18477 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
18478 vports[i]->fc_myDID == did) {
18479 vport = vports[i];
18480 break;
18481 }
18482 }
James Smart895427b2017-02-12 13:52:30 -080018483 }
James Smart4f774512009-05-22 14:52:35 -040018484 lpfc_destroy_vport_work_array(phba, vports);
18485 return vport;
18486}
18487
18488/**
James Smart45ed1192009-10-02 15:17:02 -040018489 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
18490 * @vport: The vport to work on.
18491 *
18492 * This function updates the receive sequence time stamp for this vport. The
18493 * receive sequence time stamp indicates the time that the last frame of the
18494 * the sequence that has been idle for the longest amount of time was received.
18495 * the driver uses this time stamp to indicate if any received sequences have
18496 * timed out.
18497 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018498static void
James Smart45ed1192009-10-02 15:17:02 -040018499lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
18500{
18501 struct lpfc_dmabuf *h_buf;
18502 struct hbq_dmabuf *dmabuf = NULL;
18503
18504 /* get the oldest sequence on the rcv list */
18505 h_buf = list_get_first(&vport->rcv_buffer_list,
18506 struct lpfc_dmabuf, list);
18507 if (!h_buf)
18508 return;
18509 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18510 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
18511}
18512
18513/**
18514 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
18515 * @vport: The vport that the received sequences were sent to.
18516 *
18517 * This function cleans up all outstanding received sequences. This is called
18518 * by the driver when a link event or user action invalidates all the received
18519 * sequences.
18520 **/
18521void
18522lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
18523{
18524 struct lpfc_dmabuf *h_buf, *hnext;
18525 struct lpfc_dmabuf *d_buf, *dnext;
18526 struct hbq_dmabuf *dmabuf = NULL;
18527
18528 /* start with the oldest sequence on the rcv list */
18529 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18530 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18531 list_del_init(&dmabuf->hbuf.list);
18532 list_for_each_entry_safe(d_buf, dnext,
18533 &dmabuf->dbuf.list, list) {
18534 list_del_init(&d_buf->list);
18535 lpfc_in_buf_free(vport->phba, d_buf);
18536 }
18537 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18538 }
18539}
18540
18541/**
18542 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
18543 * @vport: The vport that the received sequences were sent to.
18544 *
18545 * This function determines whether any received sequences have timed out by
18546 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
18547 * indicates that there is at least one timed out sequence this routine will
18548 * go through the received sequences one at a time from most inactive to most
18549 * active to determine which ones need to be cleaned up. Once it has determined
18550 * that a sequence needs to be cleaned up it will simply free up the resources
18551 * without sending an abort.
18552 **/
18553void
18554lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
18555{
18556 struct lpfc_dmabuf *h_buf, *hnext;
18557 struct lpfc_dmabuf *d_buf, *dnext;
18558 struct hbq_dmabuf *dmabuf = NULL;
18559 unsigned long timeout;
18560 int abort_count = 0;
18561
18562 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18563 vport->rcv_buffer_time_stamp);
18564 if (list_empty(&vport->rcv_buffer_list) ||
18565 time_before(jiffies, timeout))
18566 return;
18567 /* start with the oldest sequence on the rcv list */
18568 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18569 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18570 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18571 dmabuf->time_stamp);
18572 if (time_before(jiffies, timeout))
18573 break;
18574 abort_count++;
18575 list_del_init(&dmabuf->hbuf.list);
18576 list_for_each_entry_safe(d_buf, dnext,
18577 &dmabuf->dbuf.list, list) {
18578 list_del_init(&d_buf->list);
18579 lpfc_in_buf_free(vport->phba, d_buf);
18580 }
18581 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18582 }
18583 if (abort_count)
18584 lpfc_update_rcv_time_stamp(vport);
18585}
18586
18587/**
James Smart4f774512009-05-22 14:52:35 -040018588 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
Lee Jones7af29d42020-07-21 17:41:31 +010018589 * @vport: pointer to a vitural port
James Smart4f774512009-05-22 14:52:35 -040018590 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
18591 *
18592 * This function searches through the existing incomplete sequences that have
18593 * been sent to this @vport. If the frame matches one of the incomplete
18594 * sequences then the dbuf in the @dmabuf is added to the list of frames that
18595 * make up that sequence. If no sequence is found that matches this frame then
18596 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
18597 * This function returns a pointer to the first dmabuf in the sequence list that
18598 * the frame was linked to.
18599 **/
18600static struct hbq_dmabuf *
18601lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18602{
18603 struct fc_frame_header *new_hdr;
18604 struct fc_frame_header *temp_hdr;
18605 struct lpfc_dmabuf *d_buf;
18606 struct lpfc_dmabuf *h_buf;
18607 struct hbq_dmabuf *seq_dmabuf = NULL;
18608 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050018609 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040018610
James Smart4d9ab992009-10-02 15:16:39 -040018611 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040018612 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040018613 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050018614
James Smart4f774512009-05-22 14:52:35 -040018615 /* Use the hdr_buf to find the sequence that this frame belongs to */
18616 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18617 temp_hdr = (struct fc_frame_header *)h_buf->virt;
18618 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18619 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18620 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18621 continue;
18622 /* found a pending sequence that matches this frame */
18623 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18624 break;
18625 }
18626 if (!seq_dmabuf) {
18627 /*
18628 * This indicates first frame received for this sequence.
18629 * Queue the buffer on the vport's rcv_buffer_list.
18630 */
18631 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040018632 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040018633 return dmabuf;
18634 }
18635 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050018636 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
18637 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040018638 list_del_init(&seq_dmabuf->hbuf.list);
18639 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18640 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040018641 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040018642 return dmabuf;
18643 }
James Smart45ed1192009-10-02 15:17:02 -040018644 /* move this sequence to the tail to indicate a young sequence */
18645 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
18646 seq_dmabuf->time_stamp = jiffies;
18647 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050018648 if (list_empty(&seq_dmabuf->dbuf.list)) {
James Smarteeead812009-12-21 17:01:23 -050018649 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18650 return seq_dmabuf;
18651 }
James Smart4f774512009-05-22 14:52:35 -040018652 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050018653 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
18654 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040018655 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18656 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
18657 /*
18658 * If the frame's sequence count is greater than the frame on
18659 * the list then insert the frame right after this frame
18660 */
James Smarteeead812009-12-21 17:01:23 -050018661 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
18662 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040018663 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050018664 found = 1;
18665 break;
James Smart4f774512009-05-22 14:52:35 -040018666 }
James Smart4360ca92015-12-16 18:12:04 -050018667
18668 if (&d_buf->list == &seq_dmabuf->dbuf.list)
18669 break;
18670 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040018671 }
James Smart4360ca92015-12-16 18:12:04 -050018672
18673 if (found)
18674 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040018675 return NULL;
18676}
18677
18678/**
James Smart6669f9b2009-10-02 15:16:45 -040018679 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
18680 * @vport: pointer to a vitural port
18681 * @dmabuf: pointer to a dmabuf that describes the FC sequence
18682 *
18683 * This function tries to abort from the partially assembed sequence, described
18684 * by the information from basic abbort @dmabuf. It checks to see whether such
18685 * partially assembled sequence held by the driver. If so, it shall free up all
18686 * the frames from the partially assembled sequence.
18687 *
18688 * Return
18689 * true -- if there is matching partially assembled sequence present and all
18690 * the frames freed with the sequence;
18691 * false -- if there is no matching partially assembled sequence present so
18692 * nothing got aborted in the lower layer driver
18693 **/
18694static bool
18695lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
18696 struct hbq_dmabuf *dmabuf)
18697{
18698 struct fc_frame_header *new_hdr;
18699 struct fc_frame_header *temp_hdr;
18700 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
18701 struct hbq_dmabuf *seq_dmabuf = NULL;
18702
18703 /* Use the hdr_buf to find the sequence that matches this frame */
18704 INIT_LIST_HEAD(&dmabuf->dbuf.list);
18705 INIT_LIST_HEAD(&dmabuf->hbuf.list);
18706 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18707 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18708 temp_hdr = (struct fc_frame_header *)h_buf->virt;
18709 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18710 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18711 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18712 continue;
18713 /* found a pending sequence that matches this frame */
18714 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18715 break;
18716 }
18717
18718 /* Free up all the frames from the partially assembled sequence */
18719 if (seq_dmabuf) {
18720 list_for_each_entry_safe(d_buf, n_buf,
18721 &seq_dmabuf->dbuf.list, list) {
18722 list_del_init(&d_buf->list);
18723 lpfc_in_buf_free(vport->phba, d_buf);
18724 }
18725 return true;
18726 }
18727 return false;
18728}
18729
18730/**
James Smart6dd9e312013-01-03 15:43:37 -050018731 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
18732 * @vport: pointer to a vitural port
18733 * @dmabuf: pointer to a dmabuf that describes the FC sequence
18734 *
18735 * This function tries to abort from the assembed sequence from upper level
18736 * protocol, described by the information from basic abbort @dmabuf. It
18737 * checks to see whether such pending context exists at upper level protocol.
18738 * If so, it shall clean up the pending context.
18739 *
18740 * Return
18741 * true -- if there is matching pending context of the sequence cleaned
18742 * at ulp;
18743 * false -- if there is no matching pending context of the sequence present
18744 * at ulp.
18745 **/
18746static bool
18747lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18748{
18749 struct lpfc_hba *phba = vport->phba;
18750 int handled;
18751
18752 /* Accepting abort at ulp with SLI4 only */
18753 if (phba->sli_rev < LPFC_SLI_REV4)
18754 return false;
18755
18756 /* Register all caring upper level protocols to attend abort */
18757 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
18758 if (handled)
18759 return true;
18760
18761 return false;
18762}
18763
18764/**
James Smart546fc852011-03-11 16:06:29 -050018765 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040018766 * @phba: Pointer to HBA context object.
18767 * @cmd_iocbq: pointer to the command iocbq structure.
18768 * @rsp_iocbq: pointer to the response iocbq structure.
18769 *
James Smart546fc852011-03-11 16:06:29 -050018770 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040018771 * event. It properly releases the memory allocated to the sequence abort
18772 * accept iocb.
18773 **/
18774static void
James Smart546fc852011-03-11 16:06:29 -050018775lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040018776 struct lpfc_iocbq *cmd_iocbq,
18777 struct lpfc_iocbq *rsp_iocbq)
18778{
James Smart6dd9e312013-01-03 15:43:37 -050018779 struct lpfc_nodelist *ndlp;
18780
18781 if (cmd_iocbq) {
18782 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
18783 lpfc_nlp_put(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040018784 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050018785 }
James Smart6b5151f2012-01-18 16:24:06 -050018786
18787 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
18788 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
Dick Kennedy372c1872020-06-30 14:50:00 -070018789 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6b5151f2012-01-18 16:24:06 -050018790 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
18791 rsp_iocbq->iocb.ulpStatus,
18792 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040018793}
18794
18795/**
James Smart6d368e52011-05-24 11:44:12 -040018796 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
18797 * @phba: Pointer to HBA context object.
18798 * @xri: xri id in transaction.
18799 *
18800 * This function validates the xri maps to the known range of XRIs allocated an
18801 * used by the driver.
18802 **/
James Smart7851fe22011-07-22 18:36:52 -040018803uint16_t
James Smart6d368e52011-05-24 11:44:12 -040018804lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
18805 uint16_t xri)
18806{
James Smarta2fc4aef2014-09-03 12:57:55 -040018807 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040018808
18809 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
18810 if (xri == phba->sli4_hba.xri_ids[i])
18811 return i;
18812 }
18813 return NO_XRI;
18814}
18815
James Smart6d368e52011-05-24 11:44:12 -040018816/**
James Smart546fc852011-03-11 16:06:29 -050018817 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart3bfab8a2021-04-11 18:31:23 -070018818 * @vport: pointer to a virtual port.
James Smart6669f9b2009-10-02 15:16:45 -040018819 * @fc_hdr: pointer to a FC frame header.
Lee Jones7af29d42020-07-21 17:41:31 +010018820 * @aborted: was the partially assembled receive sequence successfully aborted
James Smart6669f9b2009-10-02 15:16:45 -040018821 *
James Smart546fc852011-03-11 16:06:29 -050018822 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040018823 * event after aborting the sequence handling.
18824 **/
James Smart86c67372017-04-21 16:05:04 -070018825void
James Smart6dd9e312013-01-03 15:43:37 -050018826lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
18827 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040018828{
James Smart6dd9e312013-01-03 15:43:37 -050018829 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040018830 struct lpfc_iocbq *ctiocb = NULL;
18831 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040018832 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050018833 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040018834 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050018835 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040018836
18837 if (!lpfc_is_link_up(phba))
18838 return;
18839
18840 sid = sli4_sid_from_fc_hdr(fc_hdr);
18841 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050018842 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040018843
James Smart6dd9e312013-01-03 15:43:37 -050018844 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040018845 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070018846 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050018847 if (!ndlp) {
18848 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
18849 "1268 Failed to allocate ndlp for "
18850 "oxid:x%x SID:x%x\n", oxid, sid);
18851 return;
18852 }
James Smart6dd9e312013-01-03 15:43:37 -050018853 /* Put ndlp onto pport node list */
18854 lpfc_enqueue_node(vport, ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040018855 }
18856
James Smart546fc852011-03-11 16:06:29 -050018857 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040018858 ctiocb = lpfc_sli_get_iocbq(phba);
18859 if (!ctiocb)
18860 return;
18861
James Smart5ffc2662009-11-18 15:39:44 -050018862 /* Extract the F_CTL field from FC_HDR */
18863 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
18864
James Smart6669f9b2009-10-02 15:16:45 -040018865 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040018866 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050018867 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040018868 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
18869 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
18870 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
18871
18872 /* Fill in the rest of iocb fields */
18873 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
18874 icmd->ulpBdeCount = 0;
18875 icmd->ulpLe = 1;
18876 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040018877 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050018878 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart4430f7f2020-11-15 11:26:31 -080018879 if (!ctiocb->context1) {
18880 lpfc_sli_release_iocbq(phba, ctiocb);
18881 return;
18882 }
James Smart6669f9b2009-10-02 15:16:45 -040018883
James Smart6669f9b2009-10-02 15:16:45 -040018884 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050018885 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040018886 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050018887 ctiocb->sli4_xritag = NO_XRI;
18888
James Smartee0f4fe2012-05-09 21:19:14 -040018889 if (fctl & FC_FC_EX_CTX)
18890 /* Exchange responder sent the abort so we
18891 * own the oxid.
18892 */
18893 xri = oxid;
18894 else
18895 xri = rxid;
18896 lxri = lpfc_sli4_xri_inrange(phba, xri);
18897 if (lxri != NO_XRI)
18898 lpfc_set_rrq_active(phba, ndlp, lxri,
18899 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050018900 /* For BA_ABTS from exchange responder, if the logical xri with
18901 * the oxid maps to the FCP XRI range, the port no longer has
18902 * that exchange context, send a BLS_RJT. Override the IOCB for
18903 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050018904 */
James Smart6dd9e312013-01-03 15:43:37 -050018905 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080018906 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050018907 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
18908 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
18909 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
18910 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
18911 }
18912
18913 /* If BA_ABTS failed to abort a partially assembled receive sequence,
18914 * the driver no longer has that exchange, send a BLS_RJT. Override
18915 * the IOCB for a BA_RJT.
18916 */
18917 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050018918 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
18919 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
18920 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
18921 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
18922 }
James Smart6669f9b2009-10-02 15:16:45 -040018923
James Smart5ffc2662009-11-18 15:39:44 -050018924 if (fctl & FC_FC_EX_CTX) {
18925 /* ABTS sent by responder to CT exchange, construction
18926 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
18927 * field and RX_ID from ABTS for RX_ID field.
18928 */
James Smart546fc852011-03-11 16:06:29 -050018929 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050018930 } else {
18931 /* ABTS sent by initiator to CT exchange, construction
18932 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050018933 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050018934 */
James Smart546fc852011-03-11 16:06:29 -050018935 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050018936 }
James Smartf09c3ac2012-03-01 22:33:29 -050018937 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050018938 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050018939
James Smart546fc852011-03-11 16:06:29 -050018940 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050018941 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
18942 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
18943 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050018944
18945 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
18946 if (rc == IOCB_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018947 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart6dd9e312013-01-03 15:43:37 -050018948 "2925 Failed to issue CT ABTS RSP x%x on "
18949 "xri x%x, Data x%x\n",
18950 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
18951 phba->link_state);
18952 lpfc_nlp_put(ndlp);
18953 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050018954 lpfc_sli_release_iocbq(phba, ctiocb);
18955 }
James Smart6669f9b2009-10-02 15:16:45 -040018956}
18957
18958/**
18959 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
18960 * @vport: Pointer to the vport on which this sequence was received
18961 * @dmabuf: pointer to a dmabuf that describes the FC sequence
18962 *
18963 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
18964 * receive sequence is only partially assembed by the driver, it shall abort
18965 * the partially assembled frames for the sequence. Otherwise, if the
18966 * unsolicited receive sequence has been completely assembled and passed to
Ferruh Yigit02e3e582020-07-28 15:56:06 +010018967 * the Upper Layer Protocol (ULP), it then mark the per oxid status for the
James Smart6669f9b2009-10-02 15:16:45 -040018968 * unsolicited sequence has been aborted. After that, it will issue a basic
18969 * accept to accept the abort.
18970 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018971static void
James Smart6669f9b2009-10-02 15:16:45 -040018972lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
18973 struct hbq_dmabuf *dmabuf)
18974{
18975 struct lpfc_hba *phba = vport->phba;
18976 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050018977 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050018978 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040018979
James Smart6669f9b2009-10-02 15:16:45 -040018980 /* Make a copy of fc_hdr before the dmabuf being released */
18981 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050018982 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040018983
James Smart5ffc2662009-11-18 15:39:44 -050018984 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050018985 /* ABTS by responder to exchange, no cleanup needed */
18986 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050018987 } else {
James Smart6dd9e312013-01-03 15:43:37 -050018988 /* ABTS by initiator to exchange, need to do cleanup */
18989 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
18990 if (aborted == false)
18991 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050018992 }
James Smart6dd9e312013-01-03 15:43:37 -050018993 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18994
James Smart86c67372017-04-21 16:05:04 -070018995 if (phba->nvmet_support) {
18996 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
18997 return;
18998 }
18999
James Smart6dd9e312013-01-03 15:43:37 -050019000 /* Respond with BA_ACC or BA_RJT accordingly */
19001 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040019002}
19003
19004/**
James Smart4f774512009-05-22 14:52:35 -040019005 * lpfc_seq_complete - Indicates if a sequence is complete
19006 * @dmabuf: pointer to a dmabuf that describes the FC sequence
19007 *
19008 * This function checks the sequence, starting with the frame described by
19009 * @dmabuf, to see if all the frames associated with this sequence are present.
19010 * the frames associated with this sequence are linked to the @dmabuf using the
19011 * dbuf list. This function looks for two major things. 1) That the first frame
19012 * has a sequence count of zero. 2) There is a frame with last frame of sequence
19013 * set. 3) That there are no holes in the sequence count. The function will
19014 * return 1 when the sequence is complete, otherwise it will return 0.
19015 **/
19016static int
19017lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
19018{
19019 struct fc_frame_header *hdr;
19020 struct lpfc_dmabuf *d_buf;
19021 struct hbq_dmabuf *seq_dmabuf;
19022 uint32_t fctl;
19023 int seq_count = 0;
19024
19025 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19026 /* make sure first fame of sequence has a sequence count of zero */
19027 if (hdr->fh_seq_cnt != seq_count)
19028 return 0;
19029 fctl = (hdr->fh_f_ctl[0] << 16 |
19030 hdr->fh_f_ctl[1] << 8 |
19031 hdr->fh_f_ctl[2]);
19032 /* If last frame of sequence we can return success. */
19033 if (fctl & FC_FC_END_SEQ)
19034 return 1;
19035 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
19036 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19037 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19038 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050019039 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040019040 return 0;
19041 fctl = (hdr->fh_f_ctl[0] << 16 |
19042 hdr->fh_f_ctl[1] << 8 |
19043 hdr->fh_f_ctl[2]);
19044 /* If last frame of sequence we can return success. */
19045 if (fctl & FC_FC_END_SEQ)
19046 return 1;
19047 }
19048 return 0;
19049}
19050
19051/**
19052 * lpfc_prep_seq - Prep sequence for ULP processing
19053 * @vport: Pointer to the vport on which this sequence was received
Lee Jones7af29d42020-07-21 17:41:31 +010019054 * @seq_dmabuf: pointer to a dmabuf that describes the FC sequence
James Smart4f774512009-05-22 14:52:35 -040019055 *
19056 * This function takes a sequence, described by a list of frames, and creates
19057 * a list of iocbq structures to describe the sequence. This iocbq list will be
19058 * used to issue to the generic unsolicited sequence handler. This routine
19059 * returns a pointer to the first iocbq in the list. If the function is unable
19060 * to allocate an iocbq then it throw out the received frames that were not
19061 * able to be described and return a pointer to the first iocbq. If unable to
19062 * allocate any iocbqs (including the first) this function will return NULL.
19063 **/
19064static struct lpfc_iocbq *
19065lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
19066{
James Smart7851fe22011-07-22 18:36:52 -040019067 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040019068 struct lpfc_dmabuf *d_buf, *n_buf;
19069 struct lpfc_iocbq *first_iocbq, *iocbq;
19070 struct fc_frame_header *fc_hdr;
19071 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040019072 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050019073 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040019074
19075 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19076 /* remove from receive buffer list */
19077 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040019078 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040019079 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040019080 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040019081 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040019082 /* Get an iocbq struct to fill in. */
19083 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
19084 if (first_iocbq) {
19085 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040019086 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040019087 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080019088 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040019089
19090 /* Check FC Header to see what TYPE of frame we are rcv'ing */
19091 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
19092 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
19093 first_iocbq->iocb.un.rcvels.parmRo =
19094 sli4_did_from_fc_hdr(fc_hdr);
19095 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
19096 } else
19097 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040019098 first_iocbq->iocb.ulpContext = NO_XRI;
19099 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
19100 be16_to_cpu(fc_hdr->fh_ox_id);
19101 /* iocbq is prepped for internal consumption. Physical vpi. */
19102 first_iocbq->iocb.unsli3.rcvsli3.vpi =
19103 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040019104 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040019105 tot_len = bf_get(lpfc_rcqe_length,
19106 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
19107
James Smart4f774512009-05-22 14:52:35 -040019108 first_iocbq->context2 = &seq_dmabuf->dbuf;
19109 first_iocbq->context3 = NULL;
19110 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040019111 if (tot_len > LPFC_DATA_BUF_SIZE)
19112 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040019113 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040019114 else
19115 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
19116
James Smart4f774512009-05-22 14:52:35 -040019117 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040019118
James Smart7851fe22011-07-22 18:36:52 -040019119 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040019120 }
19121 iocbq = first_iocbq;
19122 /*
19123 * Each IOCBq can have two Buffers assigned, so go through the list
19124 * of buffers for this sequence and save two buffers in each IOCBq
19125 */
19126 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
19127 if (!iocbq) {
19128 lpfc_in_buf_free(vport->phba, d_buf);
19129 continue;
19130 }
19131 if (!iocbq->context3) {
19132 iocbq->context3 = d_buf;
19133 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040019134 /* We need to get the size out of the right CQE */
19135 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19136 len = bf_get(lpfc_rcqe_length,
19137 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040019138 pbde = (struct ulp_bde64 *)
19139 &iocbq->iocb.unsli3.sli3Words[4];
19140 if (len > LPFC_DATA_BUF_SIZE)
19141 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
19142 else
19143 pbde->tus.f.bdeSize = len;
19144
James Smart7851fe22011-07-22 18:36:52 -040019145 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
19146 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040019147 } else {
19148 iocbq = lpfc_sli_get_iocbq(vport->phba);
19149 if (!iocbq) {
19150 if (first_iocbq) {
19151 first_iocbq->iocb.ulpStatus =
19152 IOSTAT_FCP_RSP_ERROR;
19153 first_iocbq->iocb.un.ulpWord[4] =
19154 IOERR_NO_RESOURCES;
19155 }
19156 lpfc_in_buf_free(vport->phba, d_buf);
19157 continue;
19158 }
James Smart7851fe22011-07-22 18:36:52 -040019159 /* We need to get the size out of the right CQE */
19160 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19161 len = bf_get(lpfc_rcqe_length,
19162 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040019163 iocbq->context2 = d_buf;
19164 iocbq->context3 = NULL;
19165 iocbq->iocb.ulpBdeCount = 1;
19166 if (len > LPFC_DATA_BUF_SIZE)
19167 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
19168 LPFC_DATA_BUF_SIZE;
19169 else
19170 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
19171
James Smart7851fe22011-07-22 18:36:52 -040019172 tot_len += len;
19173 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
19174
James Smart4f774512009-05-22 14:52:35 -040019175 iocbq->iocb.un.rcvels.remoteID = sid;
19176 list_add_tail(&iocbq->list, &first_iocbq->list);
19177 }
19178 }
James Smart39c4f1a2020-01-27 16:23:01 -080019179 /* Free the sequence's header buffer */
19180 if (!first_iocbq)
19181 lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
19182
James Smart4f774512009-05-22 14:52:35 -040019183 return first_iocbq;
19184}
19185
James Smart6669f9b2009-10-02 15:16:45 -040019186static void
19187lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
19188 struct hbq_dmabuf *seq_dmabuf)
19189{
19190 struct fc_frame_header *fc_hdr;
19191 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
19192 struct lpfc_hba *phba = vport->phba;
19193
19194 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19195 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
19196 if (!iocbq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019197 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6669f9b2009-10-02 15:16:45 -040019198 "2707 Ring %d handler: Failed to allocate "
19199 "iocb Rctl x%x Type x%x received\n",
19200 LPFC_ELS_RING,
19201 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19202 return;
19203 }
19204 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080019205 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040019206 iocbq, fc_hdr->fh_r_ctl,
19207 fc_hdr->fh_type))
Dick Kennedy372c1872020-06-30 14:50:00 -070019208 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6669f9b2009-10-02 15:16:45 -040019209 "2540 Ring %d handler: unexpected Rctl "
19210 "x%x Type x%x received\n",
19211 LPFC_ELS_RING,
19212 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19213
19214 /* Free iocb created in lpfc_prep_seq */
19215 list_for_each_entry_safe(curr_iocb, next_iocb,
19216 &iocbq->list, list) {
19217 list_del_init(&curr_iocb->list);
19218 lpfc_sli_release_iocbq(phba, curr_iocb);
19219 }
19220 lpfc_sli_release_iocbq(phba, iocbq);
19221}
19222
James Smartae9e28f2017-05-15 15:20:51 -070019223static void
19224lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
19225 struct lpfc_iocbq *rspiocb)
19226{
19227 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
19228
19229 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020019230 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070019231 kfree(pcmd);
19232 lpfc_sli_release_iocbq(phba, cmdiocb);
James Smarte817e5d2018-12-13 15:17:53 -080019233 lpfc_drain_txq(phba);
James Smartae9e28f2017-05-15 15:20:51 -070019234}
19235
19236static void
19237lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
19238 struct hbq_dmabuf *dmabuf)
19239{
19240 struct fc_frame_header *fc_hdr;
19241 struct lpfc_hba *phba = vport->phba;
19242 struct lpfc_iocbq *iocbq = NULL;
19243 union lpfc_wqe *wqe;
19244 struct lpfc_dmabuf *pcmd = NULL;
19245 uint32_t frame_len;
19246 int rc;
James Smarte817e5d2018-12-13 15:17:53 -080019247 unsigned long iflags;
James Smartae9e28f2017-05-15 15:20:51 -070019248
19249 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19250 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
19251
19252 /* Send the received frame back */
19253 iocbq = lpfc_sli_get_iocbq(phba);
James Smarte817e5d2018-12-13 15:17:53 -080019254 if (!iocbq) {
19255 /* Queue cq event and wakeup worker thread to process it */
19256 spin_lock_irqsave(&phba->hbalock, iflags);
19257 list_add_tail(&dmabuf->cq_event.list,
19258 &phba->sli4_hba.sp_queue_event);
19259 phba->hba_flag |= HBA_SP_QUEUE_EVT;
19260 spin_unlock_irqrestore(&phba->hbalock, iflags);
19261 lpfc_worker_wake_up(phba);
19262 return;
19263 }
James Smartae9e28f2017-05-15 15:20:51 -070019264
19265 /* Allocate buffer for command payload */
19266 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
19267 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020019268 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070019269 &pcmd->phys);
19270 if (!pcmd || !pcmd->virt)
19271 goto exit;
19272
19273 INIT_LIST_HEAD(&pcmd->list);
19274
19275 /* copyin the payload */
19276 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
19277
19278 /* fill in BDE's for command */
19279 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
19280 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
19281 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
19282 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
19283
19284 iocbq->context2 = pcmd;
19285 iocbq->vport = vport;
19286 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
19287 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
19288
19289 /*
19290 * Setup rest of the iocb as though it were a WQE
19291 * Build the SEND_FRAME WQE
19292 */
19293 wqe = (union lpfc_wqe *)&iocbq->iocb;
19294
19295 wqe->send_frame.frame_len = frame_len;
19296 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
19297 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
19298 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
19299 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
19300 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
19301 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
19302
19303 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
19304 iocbq->iocb.ulpLe = 1;
19305 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
19306 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
19307 if (rc == IOCB_ERROR)
19308 goto exit;
19309
19310 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19311 return;
19312
19313exit:
19314 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
19315 "2023 Unable to process MDS loopback frame\n");
19316 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020019317 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070019318 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070019319 if (iocbq)
19320 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070019321 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19322}
19323
James Smart4f774512009-05-22 14:52:35 -040019324/**
19325 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
19326 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010019327 * @dmabuf: Pointer to a dmabuf that describes the FC sequence.
James Smart4f774512009-05-22 14:52:35 -040019328 *
19329 * This function is called with no lock held. This function processes all
19330 * the received buffers and gives it to upper layers when a received buffer
19331 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080019332 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040019333 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
19334 * appropriate receive function when the final frame in a sequence is received.
19335 **/
James Smart4d9ab992009-10-02 15:16:39 -040019336void
19337lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
19338 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040019339{
James Smart4d9ab992009-10-02 15:16:39 -040019340 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040019341 struct fc_frame_header *fc_hdr;
19342 struct lpfc_vport *vport;
19343 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040019344 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040019345
James Smart4f774512009-05-22 14:52:35 -040019346 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040019347 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080019348
James Smarte817e5d2018-12-13 15:17:53 -080019349 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
19350 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
19351 vport = phba->pport;
19352 /* Handle MDS Loopback frames */
Dick Kennedy24411fc2020-08-03 14:02:25 -070019353 if (!(phba->pport->load_flag & FC_UNLOADING))
19354 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19355 else
19356 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smarte817e5d2018-12-13 15:17:53 -080019357 return;
19358 }
19359
James Smart4d9ab992009-10-02 15:16:39 -040019360 /* check to see if this a valid type of frame */
19361 if (lpfc_fc_frame_check(phba, fc_hdr)) {
19362 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19363 return;
19364 }
James Smart2ea259e2017-02-12 13:52:27 -080019365
James Smart7851fe22011-07-22 18:36:52 -040019366 if ((bf_get(lpfc_cqe_code,
19367 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
19368 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
19369 &dmabuf->cq_event.cqe.rcqe_cmpl);
19370 else
19371 fcfi = bf_get(lpfc_rcqe_fcf_id,
19372 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040019373
James Smarte62245d2019-08-14 16:57:08 -070019374 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
19375 vport = phba->pport;
19376 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
19377 "2023 MDS Loopback %d bytes\n",
19378 bf_get(lpfc_rcqe_length,
19379 &dmabuf->cq_event.cqe.rcqe_cmpl));
19380 /* Handle MDS Loopback frames */
19381 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19382 return;
19383 }
19384
James Smart895427b2017-02-12 13:52:30 -080019385 /* d_id this frame is directed to */
19386 did = sli4_did_from_fc_hdr(fc_hdr);
19387
19388 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040019389 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040019390 /* throw out the frame */
19391 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19392 return;
19393 }
James Smart939723a2012-05-09 21:19:03 -040019394
James Smart939723a2012-05-09 21:19:03 -040019395 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
19396 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
19397 (did != Fabric_DID)) {
19398 /*
19399 * Throw out the frame if we are not pt2pt.
19400 * The pt2pt protocol allows for discovery frames
19401 * to be received without a registered VPI.
19402 */
19403 if (!(vport->fc_flag & FC_PT2PT) ||
19404 (phba->link_state == LPFC_HBA_READY)) {
19405 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19406 return;
19407 }
19408 }
19409
James Smart6669f9b2009-10-02 15:16:45 -040019410 /* Handle the basic abort sequence (BA_ABTS) event */
19411 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
19412 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
19413 return;
19414 }
19415
James Smart4d9ab992009-10-02 15:16:39 -040019416 /* Link this frame */
19417 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
19418 if (!seq_dmabuf) {
19419 /* unable to add frame to vport - throw it out */
19420 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19421 return;
19422 }
19423 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050019424 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040019425 return;
James Smartdef9c7a2009-12-21 17:02:28 -050019426
James Smart6669f9b2009-10-02 15:16:45 -040019427 /* Send the complete sequence to the upper layer protocol */
19428 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040019429}
James Smart6fb120a2009-05-22 14:52:59 -040019430
19431/**
19432 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
19433 * @phba: pointer to lpfc hba data structure.
19434 *
19435 * This routine is invoked to post rpi header templates to the
19436 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040019437 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19438 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040019439 *
19440 * This routine does not require any locks. It's usage is expected
19441 * to be driver load or reset recovery when the driver is
19442 * sequential.
19443 *
19444 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020019445 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040019446 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040019447 * When this error occurs, the driver is not guaranteed
19448 * to have any rpi regions posted to the device and
19449 * must either attempt to repost the regions or take a
19450 * fatal error.
19451 **/
19452int
19453lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
19454{
19455 struct lpfc_rpi_hdr *rpi_page;
19456 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040019457 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040019458
James Smart6d368e52011-05-24 11:44:12 -040019459 /* SLI4 ports that support extents do not require RPI headers. */
19460 if (!phba->sli4_hba.rpi_hdrs_in_use)
19461 goto exit;
19462 if (phba->sli4_hba.extents_in_use)
19463 return -EIO;
19464
James Smart6fb120a2009-05-22 14:52:59 -040019465 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040019466 /*
19467 * Assign the rpi headers a physical rpi only if the driver
19468 * has not initialized those resources. A port reset only
19469 * needs the headers posted.
19470 */
19471 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
19472 LPFC_RPI_RSRC_RDY)
19473 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19474
James Smart6fb120a2009-05-22 14:52:59 -040019475 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
19476 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019477 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019478 "2008 Error %d posting all rpi "
19479 "headers\n", rc);
19480 rc = -EIO;
19481 break;
19482 }
19483 }
19484
James Smart6d368e52011-05-24 11:44:12 -040019485 exit:
19486 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
19487 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040019488 return rc;
19489}
19490
19491/**
19492 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
19493 * @phba: pointer to lpfc hba data structure.
19494 * @rpi_page: pointer to the rpi memory region.
19495 *
19496 * This routine is invoked to post a single rpi header to the
19497 * HBA consistent with the SLI-4 interface spec. This memory region
19498 * maps up to 64 rpi context regions.
19499 *
19500 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020019501 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040019502 * -ENOMEM - No available memory
19503 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040019504 **/
19505int
19506lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
19507{
19508 LPFC_MBOXQ_t *mboxq;
19509 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
19510 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040019511 uint32_t shdr_status, shdr_add_status;
19512 union lpfc_sli4_cfg_shdr *shdr;
19513
James Smart6d368e52011-05-24 11:44:12 -040019514 /* SLI4 ports that support extents do not require RPI headers. */
19515 if (!phba->sli4_hba.rpi_hdrs_in_use)
19516 return rc;
19517 if (phba->sli4_hba.extents_in_use)
19518 return -EIO;
19519
James Smart6fb120a2009-05-22 14:52:59 -040019520 /* The port is notified of the header region via a mailbox command. */
19521 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19522 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019523 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019524 "2001 Unable to allocate memory for issuing "
19525 "SLI_CONFIG_SPECIAL mailbox command\n");
19526 return -ENOMEM;
19527 }
19528
19529 /* Post all rpi memory regions to the port. */
19530 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040019531 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19532 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
19533 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050019534 sizeof(struct lpfc_sli4_cfg_mhdr),
19535 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040019536
19537
19538 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040019539 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
19540 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040019541 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
19542 hdr_tmpl, rpi_page->page_count);
19543
James Smart6fb120a2009-05-22 14:52:59 -040019544 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
19545 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040019546 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040019547 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
19548 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19549 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart304ee432021-04-11 18:31:17 -070019550 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -040019551 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019552 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019553 "2514 POST_RPI_HDR mailbox failed with "
19554 "status x%x add_status x%x, mbx status x%x\n",
19555 shdr_status, shdr_add_status, rc);
19556 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070019557 } else {
19558 /*
19559 * The next_rpi stores the next logical module-64 rpi value used
19560 * to post physical rpis in subsequent rpi postings.
19561 */
19562 spin_lock_irq(&phba->hbalock);
19563 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
19564 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040019565 }
19566 return rc;
19567}
19568
19569/**
19570 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
19571 * @phba: pointer to lpfc hba data structure.
19572 *
19573 * This routine is invoked to post rpi header templates to the
19574 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040019575 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19576 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040019577 *
19578 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020019579 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040019580 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
19581 **/
19582int
19583lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
19584{
James Smart6d368e52011-05-24 11:44:12 -040019585 unsigned long rpi;
19586 uint16_t max_rpi, rpi_limit;
19587 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040019588 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040019589 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040019590
James Smart6fb120a2009-05-22 14:52:59 -040019591 /*
James Smart6d368e52011-05-24 11:44:12 -040019592 * Fetch the next logical rpi. Because this index is logical,
19593 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040019594 */
James Smart4902b382013-10-10 12:20:35 -040019595 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040019596 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
19597 rpi_limit = phba->sli4_hba.next_rpi;
19598
James Smart6d368e52011-05-24 11:44:12 -040019599 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
19600 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040019601 rpi = LPFC_RPI_ALLOC_ERROR;
19602 else {
19603 set_bit(rpi, phba->sli4_hba.rpi_bmask);
19604 phba->sli4_hba.max_cfg_param.rpi_used++;
19605 phba->sli4_hba.rpi_count++;
19606 }
James Smart0f154222019-09-21 20:58:52 -070019607 lpfc_printf_log(phba, KERN_INFO,
19608 LOG_NODE | LOG_DISCOVERY,
19609 "0001 Allocated rpi:x%x max:x%x lim:x%x\n",
James Smartbe6bb942015-04-07 15:07:22 -040019610 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040019611
19612 /*
19613 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040019614 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040019615 */
19616 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
19617 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040019618 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040019619 return rpi;
19620 }
19621
19622 /*
James Smart6d368e52011-05-24 11:44:12 -040019623 * RPI header postings are not required for SLI4 ports capable of
19624 * extents.
19625 */
19626 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040019627 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040019628 return rpi;
19629 }
19630
19631 /*
James Smart6fb120a2009-05-22 14:52:59 -040019632 * If the driver is running low on rpi resources, allocate another
19633 * page now. Note that the next_rpi value is used because
19634 * it represents how many are actually in use whereas max_rpi notes
19635 * how many are supported max by the device.
19636 */
James Smart6d368e52011-05-24 11:44:12 -040019637 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040019638 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040019639 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
19640 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
19641 if (!rpi_hdr) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019642 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019643 "2002 Error Could not grow rpi "
19644 "count\n");
19645 } else {
James Smart6d368e52011-05-24 11:44:12 -040019646 lrpi = rpi_hdr->start_rpi;
19647 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040019648 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
19649 }
19650 }
19651
19652 return rpi;
19653}
19654
19655/**
Lee Jones8514e2f2021-03-03 14:46:18 +000019656 * __lpfc_sli4_free_rpi - Release an rpi for reuse.
James Smart6fb120a2009-05-22 14:52:59 -040019657 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010019658 * @rpi: rpi to free
James Smart6fb120a2009-05-22 14:52:59 -040019659 *
19660 * This routine is invoked to release an rpi to the pool of
19661 * available rpis maintained by the driver.
19662 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040019663static void
James Smartd7c47992010-06-08 18:31:54 -040019664__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19665{
James Smart7cfd5632019-11-04 16:56:58 -080019666 /*
19667 * if the rpi value indicates a prior unreg has already
19668 * been done, skip the unreg.
19669 */
19670 if (rpi == LPFC_RPI_ALLOC_ERROR)
19671 return;
19672
James Smartd7c47992010-06-08 18:31:54 -040019673 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
19674 phba->sli4_hba.rpi_count--;
19675 phba->sli4_hba.max_cfg_param.rpi_used--;
James Smartb95b2112019-08-14 16:56:47 -070019676 } else {
James Smart0f154222019-09-21 20:58:52 -070019677 lpfc_printf_log(phba, KERN_INFO,
19678 LOG_NODE | LOG_DISCOVERY,
James Smartb95b2112019-08-14 16:56:47 -070019679 "2016 rpi %x not inuse\n",
19680 rpi);
James Smartd7c47992010-06-08 18:31:54 -040019681 }
19682}
19683
19684/**
19685 * lpfc_sli4_free_rpi - Release an rpi for reuse.
19686 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010019687 * @rpi: rpi to free
James Smartd7c47992010-06-08 18:31:54 -040019688 *
19689 * This routine is invoked to release an rpi to the pool of
19690 * available rpis maintained by the driver.
19691 **/
19692void
James Smart6fb120a2009-05-22 14:52:59 -040019693lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19694{
19695 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040019696 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040019697 spin_unlock_irq(&phba->hbalock);
19698}
19699
19700/**
19701 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
19702 * @phba: pointer to lpfc hba data structure.
19703 *
19704 * This routine is invoked to remove the memory region that
19705 * provided rpi via a bitmask.
19706 **/
19707void
19708lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
19709{
19710 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040019711 kfree(phba->sli4_hba.rpi_ids);
19712 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040019713}
19714
19715/**
19716 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
Lee Jones7af29d42020-07-21 17:41:31 +010019717 * @ndlp: pointer to lpfc nodelist data structure.
19718 * @cmpl: completion call-back.
19719 * @arg: data to load as MBox 'caller buffer information'
James Smart6fb120a2009-05-22 14:52:59 -040019720 *
19721 * This routine is invoked to remove the memory region that
19722 * provided rpi via a bitmask.
19723 **/
19724int
James Smart6b5151f2012-01-18 16:24:06 -050019725lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
19726 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040019727{
19728 LPFC_MBOXQ_t *mboxq;
19729 struct lpfc_hba *phba = ndlp->phba;
19730 int rc;
19731
19732 /* The port is notified of the header region via a mailbox command. */
19733 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19734 if (!mboxq)
19735 return -ENOMEM;
19736
James Smart1037e4b2021-05-14 12:55:52 -070019737 /* If cmpl assigned, then this nlp_get pairs with
19738 * lpfc_mbx_cmpl_resume_rpi.
19739 *
19740 * Else cmpl is NULL, then this nlp_get pairs with
19741 * lpfc_sli_def_mbox_cmpl.
19742 */
19743 if (!lpfc_nlp_get(ndlp)) {
19744 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19745 "2122 %s: Failed to get nlp ref\n",
19746 __func__);
19747 mempool_free(mboxq, phba->mbox_mem_pool);
19748 return -EIO;
19749 }
19750
James Smart6fb120a2009-05-22 14:52:59 -040019751 /* Post all rpi memory regions to the port. */
19752 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050019753 if (cmpl) {
19754 mboxq->mbox_cmpl = cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080019755 mboxq->ctx_buf = arg;
James Smart72859902012-01-18 16:25:38 -050019756 } else
19757 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1037e4b2021-05-14 12:55:52 -070019758 mboxq->ctx_ndlp = ndlp;
James Smart6b5151f2012-01-18 16:24:06 -050019759 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040019760 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19761 if (rc == MBX_NOT_FINISHED) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019762 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019763 "2010 Resume RPI Mailbox failed "
19764 "status %d, mbxStatus x%x\n", rc,
19765 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart1037e4b2021-05-14 12:55:52 -070019766 lpfc_nlp_put(ndlp);
James Smart6fb120a2009-05-22 14:52:59 -040019767 mempool_free(mboxq, phba->mbox_mem_pool);
19768 return -EIO;
19769 }
19770 return 0;
19771}
19772
19773/**
19774 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050019775 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040019776 *
James Smart76a95d72010-11-20 23:11:48 -050019777 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040019778 *
19779 * Returns:
19780 * 0 success
19781 * -Evalue otherwise
19782 **/
19783int
James Smart76a95d72010-11-20 23:11:48 -050019784lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040019785{
19786 LPFC_MBOXQ_t *mboxq;
19787 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040019788 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040019789 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050019790 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040019791 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19792 if (!mboxq)
19793 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050019794 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040019795 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040019796 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040019797 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019798 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019799 "2022 INIT VPI Mailbox failed "
19800 "status %d, mbxStatus x%x\n", rc,
19801 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040019802 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040019803 }
James Smart6a9c52c2009-10-02 15:16:51 -040019804 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050019805 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040019806
19807 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040019808}
19809
19810/**
19811 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
19812 * @phba: pointer to lpfc hba data structure.
19813 * @mboxq: Pointer to mailbox object.
19814 *
19815 * This routine is invoked to manually add a single FCF record. The caller
19816 * must pass a completely initialized FCF_Record. This routine takes
19817 * care of the nonembedded mailbox operations.
19818 **/
19819static void
19820lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
19821{
19822 void *virt_addr;
19823 union lpfc_sli4_cfg_shdr *shdr;
19824 uint32_t shdr_status, shdr_add_status;
19825
19826 virt_addr = mboxq->sge_array->addr[0];
19827 /* The IOCTL status is embedded in the mailbox subheader. */
19828 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
19829 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19830 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19831
19832 if ((shdr_status || shdr_add_status) &&
19833 (shdr_status != STATUS_FCF_IN_USE))
Dick Kennedy372c1872020-06-30 14:50:00 -070019834 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019835 "2558 ADD_FCF_RECORD mailbox failed with "
19836 "status x%x add_status x%x\n",
19837 shdr_status, shdr_add_status);
19838
19839 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19840}
19841
19842/**
19843 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
19844 * @phba: pointer to lpfc hba data structure.
19845 * @fcf_record: pointer to the initialized fcf record to add.
19846 *
19847 * This routine is invoked to manually add a single FCF record. The caller
19848 * must pass a completely initialized FCF_Record. This routine takes
19849 * care of the nonembedded mailbox operations.
19850 **/
19851int
19852lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
19853{
19854 int rc = 0;
19855 LPFC_MBOXQ_t *mboxq;
19856 uint8_t *bytep;
19857 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040019858 struct lpfc_mbx_sge sge;
19859 uint32_t alloc_len, req_len;
19860 uint32_t fcfindex;
19861
19862 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19863 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019864 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019865 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
19866 return -ENOMEM;
19867 }
19868
19869 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
19870 sizeof(uint32_t);
19871
19872 /* Allocate DMA memory and set up the non-embedded mailbox command */
19873 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19874 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
19875 req_len, LPFC_SLI4_MBX_NEMBED);
19876 if (alloc_len < req_len) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019877 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019878 "2523 Allocated DMA memory size (x%x) is "
19879 "less than the requested DMA memory "
19880 "size (x%x)\n", alloc_len, req_len);
19881 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19882 return -ENOMEM;
19883 }
19884
19885 /*
19886 * Get the first SGE entry from the non-embedded DMA memory. This
19887 * routine only uses a single SGE.
19888 */
19889 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040019890 virt_addr = mboxq->sge_array->addr[0];
19891 /*
19892 * Configure the FCF record for FCFI 0. This is the driver's
19893 * hardcoded default and gets used in nonFIP mode.
19894 */
19895 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
19896 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
19897 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
19898
19899 /*
19900 * Copy the fcf_index and the FCF Record Data. The data starts after
19901 * the FCoE header plus word10. The data copy needs to be endian
19902 * correct.
19903 */
19904 bytep += sizeof(uint32_t);
19905 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
19906 mboxq->vport = phba->pport;
19907 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
19908 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19909 if (rc == MBX_NOT_FINISHED) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019910 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019911 "2515 ADD_FCF_RECORD mailbox failed with "
19912 "status 0x%x\n", rc);
19913 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19914 rc = -EIO;
19915 } else
19916 rc = 0;
19917
19918 return rc;
19919}
19920
19921/**
19922 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
19923 * @phba: pointer to lpfc hba data structure.
19924 * @fcf_record: pointer to the fcf record to write the default data.
19925 * @fcf_index: FCF table entry index.
19926 *
19927 * This routine is invoked to build the driver's default FCF record. The
19928 * values used are hardcoded. This routine handles memory initialization.
19929 *
19930 **/
19931void
19932lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
19933 struct fcf_record *fcf_record,
19934 uint16_t fcf_index)
19935{
19936 memset(fcf_record, 0, sizeof(struct fcf_record));
19937 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
19938 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
19939 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
19940 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
19941 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
19942 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
19943 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
19944 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
19945 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
19946 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
19947 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
19948 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
19949 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040019950 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040019951 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
19952 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
19953 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
19954 /* Set the VLAN bit map */
19955 if (phba->valid_vlan) {
19956 fcf_record->vlan_bitmap[phba->vlan_id / 8]
19957 = 1 << (phba->vlan_id % 8);
19958 }
19959}
19960
19961/**
James Smart0c9ab6f2010-02-26 14:15:57 -050019962 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040019963 * @phba: pointer to lpfc hba data structure.
19964 * @fcf_index: FCF table entry offset.
19965 *
James Smart0c9ab6f2010-02-26 14:15:57 -050019966 * This routine is invoked to scan the entire FCF table by reading FCF
19967 * record and processing it one at a time starting from the @fcf_index
19968 * for initial FCF discovery or fast FCF failover rediscovery.
19969 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030019970 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050019971 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040019972 **/
19973int
James Smart0c9ab6f2010-02-26 14:15:57 -050019974lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040019975{
19976 int rc = 0, error;
19977 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040019978
James Smart32b97932009-07-19 10:01:21 -040019979 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050019980 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040019981 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19982 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019983 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019984 "2000 Failed to allocate mbox for "
19985 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040019986 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050019987 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040019988 }
James Smartecfd03c2010-02-12 14:41:27 -050019989 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050019990 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050019991 if (rc) {
19992 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050019993 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040019994 }
James Smartecfd03c2010-02-12 14:41:27 -050019995 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040019996 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050019997 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040019998
19999 spin_lock_irq(&phba->hbalock);
20000 phba->hba_flag |= FCF_TS_INPROG;
20001 spin_unlock_irq(&phba->hbalock);
20002
James Smart6fb120a2009-05-22 14:52:59 -040020003 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050020004 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040020005 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050020006 else {
James Smart38b92ef2010-08-04 16:11:39 -040020007 /* Reset eligible FCF count for new scan */
20008 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040020009 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040020010 error = 0;
James Smart32b97932009-07-19 10:01:21 -040020011 }
James Smart0c9ab6f2010-02-26 14:15:57 -050020012fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040020013 if (error) {
20014 if (mboxq)
20015 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040020016 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040020017 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040020018 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040020019 spin_unlock_irq(&phba->hbalock);
20020 }
James Smart6fb120a2009-05-22 14:52:59 -040020021 return error;
20022}
James Smarta0c87cb2009-07-19 10:01:10 -040020023
20024/**
James Smarta93ff372010-10-22 11:06:08 -040020025 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050020026 * @phba: pointer to lpfc hba data structure.
20027 * @fcf_index: FCF table entry offset.
20028 *
20029 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040020030 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050020031 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030020032 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050020033 * otherwise.
20034 **/
20035int
20036lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20037{
20038 int rc = 0, error;
20039 LPFC_MBOXQ_t *mboxq;
20040
20041 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20042 if (!mboxq) {
20043 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20044 "2763 Failed to allocate mbox for "
20045 "READ_FCF cmd\n");
20046 error = -ENOMEM;
20047 goto fail_fcf_read;
20048 }
20049 /* Construct the read FCF record mailbox command */
20050 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20051 if (rc) {
20052 error = -EINVAL;
20053 goto fail_fcf_read;
20054 }
20055 /* Issue the mailbox command asynchronously */
20056 mboxq->vport = phba->pport;
20057 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
20058 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20059 if (rc == MBX_NOT_FINISHED)
20060 error = -EIO;
20061 else
20062 error = 0;
20063
20064fail_fcf_read:
20065 if (error && mboxq)
20066 lpfc_sli4_mbox_cmd_free(phba, mboxq);
20067 return error;
20068}
20069
20070/**
20071 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
20072 * @phba: pointer to lpfc hba data structure.
20073 * @fcf_index: FCF table entry offset.
20074 *
20075 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040020076 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050020077 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030020078 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050020079 * otherwise.
20080 **/
20081int
20082lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20083{
20084 int rc = 0, error;
20085 LPFC_MBOXQ_t *mboxq;
20086
20087 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20088 if (!mboxq) {
20089 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20090 "2758 Failed to allocate mbox for "
20091 "READ_FCF cmd\n");
20092 error = -ENOMEM;
20093 goto fail_fcf_read;
20094 }
20095 /* Construct the read FCF record mailbox command */
20096 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20097 if (rc) {
20098 error = -EINVAL;
20099 goto fail_fcf_read;
20100 }
20101 /* Issue the mailbox command asynchronously */
20102 mboxq->vport = phba->pport;
20103 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
20104 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20105 if (rc == MBX_NOT_FINISHED)
20106 error = -EIO;
20107 else
20108 error = 0;
20109
20110fail_fcf_read:
20111 if (error && mboxq)
20112 lpfc_sli4_mbox_cmd_free(phba, mboxq);
20113 return error;
20114}
20115
20116/**
James Smartf5cb5302015-12-16 18:11:52 -050020117 * lpfc_check_next_fcf_pri_level
Lee Jones7af29d42020-07-21 17:41:31 +010020118 * @phba: pointer to the lpfc_hba struct for this port.
James Smart7d791df2011-07-22 18:37:52 -040020119 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
20120 * routine when the rr_bmask is empty. The FCF indecies are put into the
20121 * rr_bmask based on their priority level. Starting from the highest priority
20122 * to the lowest. The most likely FCF candidate will be in the highest
20123 * priority group. When this routine is called it searches the fcf_pri list for
20124 * next lowest priority group and repopulates the rr_bmask with only those
20125 * fcf_indexes.
20126 * returns:
20127 * 1=success 0=failure
20128 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040020129static int
James Smart7d791df2011-07-22 18:37:52 -040020130lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
20131{
20132 uint16_t next_fcf_pri;
20133 uint16_t last_index;
20134 struct lpfc_fcf_pri *fcf_pri;
20135 int rc;
20136 int ret = 0;
20137
20138 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20139 LPFC_SLI4_FCF_TBL_INDX_MAX);
20140 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20141 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050020142
20143 /* Verify the priority list has 2 or more entries */
20144 spin_lock_irq(&phba->hbalock);
20145 if (list_empty(&phba->fcf.fcf_pri_list) ||
20146 list_is_singular(&phba->fcf.fcf_pri_list)) {
20147 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040020148 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20149 "3061 Last IDX %d\n", last_index);
20150 return 0; /* Empty rr list */
20151 }
James Smart25626692013-03-01 16:36:54 -050020152 spin_unlock_irq(&phba->hbalock);
20153
James Smart7d791df2011-07-22 18:37:52 -040020154 next_fcf_pri = 0;
20155 /*
20156 * Clear the rr_bmask and set all of the bits that are at this
20157 * priority.
20158 */
20159 memset(phba->fcf.fcf_rr_bmask, 0,
20160 sizeof(*phba->fcf.fcf_rr_bmask));
20161 spin_lock_irq(&phba->hbalock);
20162 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20163 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
20164 continue;
20165 /*
20166 * the 1st priority that has not FLOGI failed
20167 * will be the highest.
20168 */
20169 if (!next_fcf_pri)
20170 next_fcf_pri = fcf_pri->fcf_rec.priority;
20171 spin_unlock_irq(&phba->hbalock);
20172 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20173 rc = lpfc_sli4_fcf_rr_index_set(phba,
20174 fcf_pri->fcf_rec.fcf_index);
20175 if (rc)
20176 return 0;
20177 }
20178 spin_lock_irq(&phba->hbalock);
20179 }
20180 /*
20181 * if next_fcf_pri was not set above and the list is not empty then
20182 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070020183 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040020184 */
20185 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
20186 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20187 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
20188 /*
20189 * the 1st priority that has not FLOGI failed
20190 * will be the highest.
20191 */
20192 if (!next_fcf_pri)
20193 next_fcf_pri = fcf_pri->fcf_rec.priority;
20194 spin_unlock_irq(&phba->hbalock);
20195 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20196 rc = lpfc_sli4_fcf_rr_index_set(phba,
20197 fcf_pri->fcf_rec.fcf_index);
20198 if (rc)
20199 return 0;
20200 }
20201 spin_lock_irq(&phba->hbalock);
20202 }
20203 } else
20204 ret = 1;
20205 spin_unlock_irq(&phba->hbalock);
20206
20207 return ret;
20208}
20209/**
James Smart0c9ab6f2010-02-26 14:15:57 -050020210 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
20211 * @phba: pointer to lpfc hba data structure.
20212 *
20213 * This routine is to get the next eligible FCF record index in a round
20214 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040020215 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050020216 * shall be returned, otherwise, the next eligible FCF record's index
20217 * shall be returned.
20218 **/
20219uint16_t
20220lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
20221{
20222 uint16_t next_fcf_index;
20223
James Smart421c6622013-01-03 15:44:16 -050020224initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040020225 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050020226 next_fcf_index = phba->fcf.current_rec.fcf_indx;
20227
James Smart7d791df2011-07-22 18:37:52 -040020228next_priority:
James Smart421c6622013-01-03 15:44:16 -050020229 /* Determine the next fcf index to check */
20230 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050020231 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
20232 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040020233 next_fcf_index);
20234
James Smart0c9ab6f2010-02-26 14:15:57 -050020235 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040020236 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20237 /*
20238 * If we have wrapped then we need to clear the bits that
20239 * have been tested so that we can detect when we should
20240 * change the priority level.
20241 */
James Smart0c9ab6f2010-02-26 14:15:57 -050020242 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
20243 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040020244 }
20245
James Smart0c9ab6f2010-02-26 14:15:57 -050020246
James Smart3804dc82010-07-14 15:31:37 -040020247 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040020248 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
20249 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
20250 /*
20251 * If next fcf index is not found check if there are lower
20252 * Priority level fcf's in the fcf_priority list.
20253 * Set up the rr_bmask with all of the avaiable fcf bits
20254 * at that level and continue the selection process.
20255 */
20256 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050020257 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040020258 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
20259 "2844 No roundrobin failover FCF available\n");
James Smart036cad12018-10-23 13:41:06 -070020260
20261 return LPFC_FCOE_FCF_NEXT_NONE;
James Smart3804dc82010-07-14 15:31:37 -040020262 }
20263
James Smart7d791df2011-07-22 18:37:52 -040020264 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
20265 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050020266 LPFC_FCF_FLOGI_FAILED) {
20267 if (list_is_singular(&phba->fcf.fcf_pri_list))
20268 return LPFC_FCOE_FCF_NEXT_NONE;
20269
James Smart7d791df2011-07-22 18:37:52 -040020270 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050020271 }
James Smart7d791df2011-07-22 18:37:52 -040020272
James Smart3804dc82010-07-14 15:31:37 -040020273 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040020274 "2845 Get next roundrobin failover FCF (x%x)\n",
20275 next_fcf_index);
20276
James Smart0c9ab6f2010-02-26 14:15:57 -050020277 return next_fcf_index;
20278}
20279
20280/**
20281 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
20282 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010020283 * @fcf_index: index into the FCF table to 'set'
James Smart0c9ab6f2010-02-26 14:15:57 -050020284 *
20285 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040020286 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050020287 * does not go beyond the range of the driver allocated bmask dimension
20288 * before setting the bit.
20289 *
20290 * Returns 0 if the index bit successfully set, otherwise, it returns
20291 * -EINVAL.
20292 **/
20293int
20294lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
20295{
20296 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20297 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040020298 "2610 FCF (x%x) reached driver's book "
20299 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050020300 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20301 return -EINVAL;
20302 }
20303 /* Set the eligible FCF record index bmask */
20304 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20305
James Smart3804dc82010-07-14 15:31:37 -040020306 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040020307 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040020308 "bmask\n", fcf_index);
20309
James Smart0c9ab6f2010-02-26 14:15:57 -050020310 return 0;
20311}
20312
20313/**
James Smart3804dc82010-07-14 15:31:37 -040020314 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050020315 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010020316 * @fcf_index: index into the FCF table to 'clear'
James Smart0c9ab6f2010-02-26 14:15:57 -050020317 *
20318 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040020319 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050020320 * does not go beyond the range of the driver allocated bmask dimension
20321 * before clearing the bit.
20322 **/
20323void
20324lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
20325{
James Smart9a803a72013-09-06 12:17:56 -040020326 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050020327 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20328 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040020329 "2762 FCF (x%x) reached driver's book "
20330 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050020331 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20332 return;
20333 }
20334 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040020335 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040020336 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
20337 list) {
James Smart7d791df2011-07-22 18:37:52 -040020338 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
20339 list_del_init(&fcf_pri->list);
20340 break;
20341 }
20342 }
20343 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050020344 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040020345
20346 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040020347 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040020348 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050020349}
20350
20351/**
James Smartecfd03c2010-02-12 14:41:27 -050020352 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
20353 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010020354 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
James Smartecfd03c2010-02-12 14:41:27 -050020355 *
20356 * This routine is the completion routine for the rediscover FCF table mailbox
20357 * command. If the mailbox command returned failure, it will try to stop the
20358 * FCF rediscover wait timer.
20359 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040020360static void
James Smartecfd03c2010-02-12 14:41:27 -050020361lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
20362{
20363 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20364 uint32_t shdr_status, shdr_add_status;
20365
20366 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20367
20368 shdr_status = bf_get(lpfc_mbox_hdr_status,
20369 &redisc_fcf->header.cfg_shdr.response);
20370 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20371 &redisc_fcf->header.cfg_shdr.response);
20372 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050020373 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050020374 "2746 Requesting for FCF rediscovery failed "
20375 "status x%x add_status x%x\n",
20376 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050020377 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050020378 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050020379 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050020380 spin_unlock_irq(&phba->hbalock);
20381 /*
20382 * CVL event triggered FCF rediscover request failed,
20383 * last resort to re-try current registered FCF entry.
20384 */
20385 lpfc_retry_pport_discovery(phba);
20386 } else {
20387 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050020388 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050020389 spin_unlock_irq(&phba->hbalock);
20390 /*
20391 * DEAD FCF event triggered FCF rediscover request
20392 * failed, last resort to fail over as a link down
20393 * to FCF registration.
20394 */
20395 lpfc_sli4_fcf_dead_failthrough(phba);
20396 }
James Smart0c9ab6f2010-02-26 14:15:57 -050020397 } else {
20398 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040020399 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050020400 /*
20401 * Start FCF rediscovery wait timer for pending FCF
20402 * before rescan FCF record table.
20403 */
20404 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050020405 }
James Smartecfd03c2010-02-12 14:41:27 -050020406
20407 mempool_free(mbox, phba->mbox_mem_pool);
20408}
20409
20410/**
James Smart3804dc82010-07-14 15:31:37 -040020411 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050020412 * @phba: pointer to lpfc hba data structure.
20413 *
20414 * This routine is invoked to request for rediscovery of the entire FCF table
20415 * by the port.
20416 **/
20417int
20418lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
20419{
20420 LPFC_MBOXQ_t *mbox;
20421 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20422 int rc, length;
20423
James Smart0c9ab6f2010-02-26 14:15:57 -050020424 /* Cancel retry delay timers to all vports before FCF rediscover */
20425 lpfc_cancel_all_vport_retry_delay_timer(phba);
20426
James Smartecfd03c2010-02-12 14:41:27 -050020427 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20428 if (!mbox) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020429 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartecfd03c2010-02-12 14:41:27 -050020430 "2745 Failed to allocate mbox for "
20431 "requesting FCF rediscover.\n");
20432 return -ENOMEM;
20433 }
20434
20435 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
20436 sizeof(struct lpfc_sli4_cfg_mhdr));
20437 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
20438 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
20439 length, LPFC_SLI4_MBX_EMBED);
20440
20441 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20442 /* Set count to 0 for invalidating the entire FCF database */
20443 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
20444
20445 /* Issue the mailbox command asynchronously */
20446 mbox->vport = phba->pport;
20447 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
20448 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
20449
20450 if (rc == MBX_NOT_FINISHED) {
20451 mempool_free(mbox, phba->mbox_mem_pool);
20452 return -EIO;
20453 }
20454 return 0;
20455}
20456
20457/**
James Smartfc2b9892010-02-26 14:15:29 -050020458 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
20459 * @phba: pointer to lpfc hba data structure.
20460 *
20461 * This function is the failover routine as a last resort to the FCF DEAD
20462 * event when driver failed to perform fast FCF failover.
20463 **/
20464void
20465lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
20466{
20467 uint32_t link_state;
20468
20469 /*
20470 * Last resort as FCF DEAD event failover will treat this as
20471 * a link down, but save the link state because we don't want
20472 * it to be changed to Link Down unless it is already down.
20473 */
20474 link_state = phba->link_state;
20475 lpfc_linkdown(phba);
20476 phba->link_state = link_state;
20477
20478 /* Unregister FCF if no devices connected to it */
20479 lpfc_unregister_unused_fcf(phba);
20480}
20481
20482/**
James Smart026abb82011-12-13 13:20:45 -050020483 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040020484 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050020485 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040020486 *
James Smart026abb82011-12-13 13:20:45 -050020487 * This function gets SLI3 port configure region 23 data through memory dump
20488 * mailbox command. When it successfully retrieves data, the size of the data
20489 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040020490 **/
James Smart026abb82011-12-13 13:20:45 -050020491static uint32_t
20492lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040020493{
20494 LPFC_MBOXQ_t *pmb = NULL;
20495 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050020496 uint32_t offset = 0;
James Smarte4ec1022021-04-21 16:45:11 -070020497 int rc;
James Smarta0c87cb2009-07-19 10:01:10 -040020498
James Smart026abb82011-12-13 13:20:45 -050020499 if (!rgn23_data)
20500 return 0;
20501
James Smarta0c87cb2009-07-19 10:01:10 -040020502 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20503 if (!pmb) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020504 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart026abb82011-12-13 13:20:45 -050020505 "2600 failed to allocate mailbox memory\n");
20506 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040020507 }
20508 mb = &pmb->u.mb;
20509
James Smarta0c87cb2009-07-19 10:01:10 -040020510 do {
20511 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
20512 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
20513
20514 if (rc != MBX_SUCCESS) {
20515 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050020516 "2601 failed to read config "
20517 "region 23, rc 0x%x Status 0x%x\n",
20518 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040020519 mb->un.varDmp.word_cnt = 0;
20520 }
20521 /*
20522 * dump mem may return a zero when finished or we got a
20523 * mailbox error, either way we are done.
20524 */
20525 if (mb->un.varDmp.word_cnt == 0)
20526 break;
James Smarta0c87cb2009-07-19 10:01:10 -040020527
James Smarte4ec1022021-04-21 16:45:11 -070020528 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
20529 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
20530
James Smarta0c87cb2009-07-19 10:01:10 -040020531 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smarte4ec1022021-04-21 16:45:11 -070020532 rgn23_data + offset,
20533 mb->un.varDmp.word_cnt);
20534 offset += mb->un.varDmp.word_cnt;
20535 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
James Smarta0c87cb2009-07-19 10:01:10 -040020536
James Smart026abb82011-12-13 13:20:45 -050020537 mempool_free(pmb, phba->mbox_mem_pool);
20538 return offset;
20539}
20540
20541/**
20542 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
20543 * @phba: pointer to lpfc hba data structure.
20544 * @rgn23_data: pointer to configure region 23 data.
20545 *
20546 * This function gets SLI4 port configure region 23 data through memory dump
20547 * mailbox command. When it successfully retrieves data, the size of the data
20548 * will be returned, otherwise, 0 will be returned.
20549 **/
20550static uint32_t
20551lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20552{
20553 LPFC_MBOXQ_t *mboxq = NULL;
20554 struct lpfc_dmabuf *mp = NULL;
20555 struct lpfc_mqe *mqe;
20556 uint32_t data_length = 0;
20557 int rc;
20558
20559 if (!rgn23_data)
20560 return 0;
20561
20562 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20563 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020564 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart026abb82011-12-13 13:20:45 -050020565 "3105 failed to allocate mailbox memory\n");
20566 return 0;
20567 }
20568
20569 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
20570 goto out;
20571 mqe = &mboxq->u.mqe;
James Smart3e1f0712018-11-29 16:09:29 -080020572 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smart026abb82011-12-13 13:20:45 -050020573 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
20574 if (rc)
20575 goto out;
20576 data_length = mqe->un.mb_words[5];
20577 if (data_length == 0)
20578 goto out;
20579 if (data_length > DMP_RGN23_SIZE) {
20580 data_length = 0;
20581 goto out;
20582 }
20583 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
20584out:
20585 mempool_free(mboxq, phba->mbox_mem_pool);
20586 if (mp) {
20587 lpfc_mbuf_free(phba, mp->virt, mp->phys);
20588 kfree(mp);
20589 }
20590 return data_length;
20591}
20592
20593/**
20594 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
20595 * @phba: pointer to lpfc hba data structure.
20596 *
20597 * This function read region 23 and parse TLV for port status to
20598 * decide if the user disaled the port. If the TLV indicates the
20599 * port is disabled, the hba_flag is set accordingly.
20600 **/
20601void
20602lpfc_sli_read_link_ste(struct lpfc_hba *phba)
20603{
20604 uint8_t *rgn23_data = NULL;
20605 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
20606 uint32_t offset = 0;
20607
20608 /* Get adapter Region 23 data */
20609 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
20610 if (!rgn23_data)
20611 goto out;
20612
20613 if (phba->sli_rev < LPFC_SLI_REV4)
20614 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
20615 else {
20616 if_type = bf_get(lpfc_sli_intf_if_type,
20617 &phba->sli4_hba.sli_intf);
20618 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
20619 goto out;
20620 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
20621 }
James Smarta0c87cb2009-07-19 10:01:10 -040020622
20623 if (!data_size)
20624 goto out;
20625
20626 /* Check the region signature first */
20627 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020628 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta0c87cb2009-07-19 10:01:10 -040020629 "2619 Config region 23 has bad signature\n");
20630 goto out;
20631 }
20632 offset += 4;
20633
20634 /* Check the data structure version */
20635 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020636 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta0c87cb2009-07-19 10:01:10 -040020637 "2620 Config region 23 has bad version\n");
20638 goto out;
20639 }
20640 offset += 4;
20641
20642 /* Parse TLV entries in the region */
20643 while (offset < data_size) {
20644 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
20645 break;
20646 /*
20647 * If the TLV is not driver specific TLV or driver id is
20648 * not linux driver id, skip the record.
20649 */
20650 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
20651 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
20652 (rgn23_data[offset + 3] != 0)) {
20653 offset += rgn23_data[offset + 1] * 4 + 4;
20654 continue;
20655 }
20656
20657 /* Driver found a driver specific TLV in the config region */
20658 sub_tlv_len = rgn23_data[offset + 1] * 4;
20659 offset += 4;
20660 tlv_offset = 0;
20661
20662 /*
20663 * Search for configured port state sub-TLV.
20664 */
20665 while ((offset < data_size) &&
20666 (tlv_offset < sub_tlv_len)) {
20667 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
20668 offset += 4;
20669 tlv_offset += 4;
20670 break;
20671 }
20672 if (rgn23_data[offset] != PORT_STE_TYPE) {
20673 offset += rgn23_data[offset + 1] * 4 + 4;
20674 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
20675 continue;
20676 }
20677
20678 /* This HBA contains PORT_STE configured */
20679 if (!rgn23_data[offset + 2])
20680 phba->hba_flag |= LINK_DISABLED;
20681
20682 goto out;
20683 }
20684 }
James Smart026abb82011-12-13 13:20:45 -050020685
James Smarta0c87cb2009-07-19 10:01:10 -040020686out:
James Smarta0c87cb2009-07-19 10:01:10 -040020687 kfree(rgn23_data);
20688 return;
20689}
James Smart695a8142010-01-26 23:08:03 -050020690
20691/**
James Smart16a93e82021-07-07 11:43:34 -070020692 * lpfc_log_fw_write_cmpl - logs firmware write completion status
20693 * @phba: pointer to lpfc hba data structure
20694 * @shdr_status: wr_object rsp's status field
20695 * @shdr_add_status: wr_object rsp's add_status field
20696 * @shdr_add_status_2: wr_object rsp's add_status_2 field
20697 * @shdr_change_status: wr_object rsp's change_status field
20698 * @shdr_csf: wr_object rsp's csf bit
20699 *
20700 * This routine is intended to be called after a firmware write completes.
20701 * It will log next action items to be performed by the user to instantiate
20702 * the newly downloaded firmware or reason for incompatibility.
20703 **/
20704static void
20705lpfc_log_fw_write_cmpl(struct lpfc_hba *phba, u32 shdr_status,
20706 u32 shdr_add_status, u32 shdr_add_status_2,
20707 u32 shdr_change_status, u32 shdr_csf)
20708{
20709 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20710 "4198 %s: flash_id x%02x, asic_rev x%02x, "
20711 "status x%02x, add_status x%02x, add_status_2 x%02x, "
20712 "change_status x%02x, csf %01x\n", __func__,
20713 phba->sli4_hba.flash_id, phba->sli4_hba.asic_rev,
20714 shdr_status, shdr_add_status, shdr_add_status_2,
20715 shdr_change_status, shdr_csf);
20716
20717 if (shdr_add_status == LPFC_ADD_STATUS_INCOMPAT_OBJ) {
20718 switch (shdr_add_status_2) {
20719 case LPFC_ADD_STATUS_2_INCOMPAT_FLASH:
20720 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20721 "4199 Firmware write failed: "
20722 "image incompatible with flash x%02x\n",
20723 phba->sli4_hba.flash_id);
20724 break;
20725 case LPFC_ADD_STATUS_2_INCORRECT_ASIC:
20726 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20727 "4200 Firmware write failed: "
20728 "image incompatible with ASIC "
20729 "architecture x%02x\n",
20730 phba->sli4_hba.asic_rev);
20731 break;
20732 default:
20733 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20734 "4210 Firmware write failed: "
20735 "add_status_2 x%02x\n",
20736 shdr_add_status_2);
20737 break;
20738 }
20739 } else if (!shdr_status && !shdr_add_status) {
20740 if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
20741 shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
20742 if (shdr_csf)
20743 shdr_change_status =
20744 LPFC_CHANGE_STATUS_PCI_RESET;
20745 }
20746
20747 switch (shdr_change_status) {
20748 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
20749 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20750 "3198 Firmware write complete: System "
20751 "reboot required to instantiate\n");
20752 break;
20753 case (LPFC_CHANGE_STATUS_FW_RESET):
20754 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20755 "3199 Firmware write complete: "
20756 "Firmware reset required to "
20757 "instantiate\n");
20758 break;
20759 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
20760 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20761 "3200 Firmware write complete: Port "
20762 "Migration or PCI Reset required to "
20763 "instantiate\n");
20764 break;
20765 case (LPFC_CHANGE_STATUS_PCI_RESET):
20766 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20767 "3201 Firmware write complete: PCI "
20768 "Reset required to instantiate\n");
20769 break;
20770 default:
20771 break;
20772 }
20773 }
20774}
20775
20776/**
James Smart52d52442011-05-24 11:42:45 -040020777 * lpfc_wr_object - write an object to the firmware
20778 * @phba: HBA structure that indicates port to create a queue on.
20779 * @dmabuf_list: list of dmabufs to write to the port.
20780 * @size: the total byte value of the objects to write to the port.
20781 * @offset: the current offset to be used to start the transfer.
20782 *
20783 * This routine will create a wr_object mailbox command to send to the port.
20784 * the mailbox command will be constructed using the dma buffers described in
20785 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
20786 * BDEs that the imbedded mailbox can support. The @offset variable will be
20787 * used to indicate the starting offset of the transfer and will also return
20788 * the offset after the write object mailbox has completed. @size is used to
20789 * determine the end of the object and whether the eof bit should be set.
20790 *
20791 * Return 0 is successful and offset will contain the the new offset to use
20792 * for the next write.
20793 * Return negative value for error cases.
20794 **/
20795int
20796lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
20797 uint32_t size, uint32_t *offset)
20798{
20799 struct lpfc_mbx_wr_object *wr_object;
20800 LPFC_MBOXQ_t *mbox;
20801 int rc = 0, i = 0;
James Smart16a93e82021-07-07 11:43:34 -070020802 uint32_t shdr_status, shdr_add_status, shdr_add_status_2;
20803 uint32_t shdr_change_status = 0, shdr_csf = 0;
James Smart52d52442011-05-24 11:42:45 -040020804 uint32_t mbox_tmo;
James Smart52d52442011-05-24 11:42:45 -040020805 struct lpfc_dmabuf *dmabuf;
20806 uint32_t written = 0;
James Smart50212672018-12-13 15:17:57 -080020807 bool check_change_status = false;
James Smart52d52442011-05-24 11:42:45 -040020808
20809 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20810 if (!mbox)
20811 return -ENOMEM;
20812
20813 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
20814 LPFC_MBOX_OPCODE_WRITE_OBJECT,
20815 sizeof(struct lpfc_mbx_wr_object) -
20816 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
20817
20818 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
20819 wr_object->u.request.write_offset = *offset;
20820 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
20821 wr_object->u.request.object_name[0] =
20822 cpu_to_le32(wr_object->u.request.object_name[0]);
20823 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
20824 list_for_each_entry(dmabuf, dmabuf_list, list) {
20825 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
20826 break;
20827 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
20828 wr_object->u.request.bde[i].addrHigh =
20829 putPaddrHigh(dmabuf->phys);
20830 if (written + SLI4_PAGE_SIZE >= size) {
20831 wr_object->u.request.bde[i].tus.f.bdeSize =
20832 (size - written);
20833 written += (size - written);
20834 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
James Smart50212672018-12-13 15:17:57 -080020835 bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
20836 check_change_status = true;
James Smart52d52442011-05-24 11:42:45 -040020837 } else {
20838 wr_object->u.request.bde[i].tus.f.bdeSize =
20839 SLI4_PAGE_SIZE;
20840 written += SLI4_PAGE_SIZE;
20841 }
20842 i++;
20843 }
20844 wr_object->u.request.bde_count = i;
20845 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
20846 if (!phba->sli4_hba.intr_enable)
20847 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
20848 else {
James Smarta183a152011-10-10 21:32:43 -040020849 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040020850 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
20851 }
20852 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart50212672018-12-13 15:17:57 -080020853 shdr_status = bf_get(lpfc_mbox_hdr_status,
20854 &wr_object->header.cfg_shdr.response);
20855 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20856 &wr_object->header.cfg_shdr.response);
James Smart16a93e82021-07-07 11:43:34 -070020857 shdr_add_status_2 = bf_get(lpfc_mbox_hdr_add_status_2,
20858 &wr_object->header.cfg_shdr.response);
James Smart50212672018-12-13 15:17:57 -080020859 if (check_change_status) {
20860 shdr_change_status = bf_get(lpfc_wr_object_change_status,
20861 &wr_object->u.response);
James Smart16a93e82021-07-07 11:43:34 -070020862 shdr_csf = bf_get(lpfc_wr_object_csf,
20863 &wr_object->u.response);
James Smart50212672018-12-13 15:17:57 -080020864 }
James Smart16a93e82021-07-07 11:43:34 -070020865
James Smart304ee432021-04-11 18:31:17 -070020866 if (!phba->sli4_hba.intr_enable)
20867 mempool_free(mbox, phba->mbox_mem_pool);
20868 else if (rc != MBX_TIMEOUT)
James Smart52d52442011-05-24 11:42:45 -040020869 mempool_free(mbox, phba->mbox_mem_pool);
James Smart16a93e82021-07-07 11:43:34 -070020870 if (shdr_status || shdr_add_status || shdr_add_status_2 || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020871 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart52d52442011-05-24 11:42:45 -040020872 "3025 Write Object mailbox failed with "
James Smart16a93e82021-07-07 11:43:34 -070020873 "status x%x add_status x%x, add_status_2 x%x, "
20874 "mbx status x%x\n",
20875 shdr_status, shdr_add_status, shdr_add_status_2,
20876 rc);
James Smart52d52442011-05-24 11:42:45 -040020877 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080020878 *offset = shdr_add_status;
James Smart16a93e82021-07-07 11:43:34 -070020879 } else {
James Smart52d52442011-05-24 11:42:45 -040020880 *offset += wr_object->u.response.actual_write_length;
James Smart16a93e82021-07-07 11:43:34 -070020881 }
20882
20883 if (rc || check_change_status)
20884 lpfc_log_fw_write_cmpl(phba, shdr_status, shdr_add_status,
20885 shdr_add_status_2, shdr_change_status,
20886 shdr_csf);
James Smart52d52442011-05-24 11:42:45 -040020887 return rc;
20888}
20889
20890/**
James Smart695a8142010-01-26 23:08:03 -050020891 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
20892 * @vport: pointer to vport data structure.
20893 *
20894 * This function iterate through the mailboxq and clean up all REG_LOGIN
20895 * and REG_VPI mailbox commands associated with the vport. This function
20896 * is called when driver want to restart discovery of the vport due to
20897 * a Clear Virtual Link event.
20898 **/
20899void
20900lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
20901{
20902 struct lpfc_hba *phba = vport->phba;
20903 LPFC_MBOXQ_t *mb, *nextmb;
20904 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040020905 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040020906 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smartd439d282010-09-29 11:18:45 -040020907 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050020908 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050020909
James Smartd439d282010-09-29 11:18:45 -040020910 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050020911 spin_lock_irq(&phba->hbalock);
20912 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
20913 if (mb->vport != vport)
20914 continue;
20915
20916 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
20917 (mb->u.mb.mbxCommand != MBX_REG_VPI))
20918 continue;
20919
Zou Wei47018082021-06-08 08:51:33 +080020920 list_move_tail(&mb->list, &mbox_cmd_list);
James Smartd439d282010-09-29 11:18:45 -040020921 }
20922 /* Clean up active mailbox command with the vport */
20923 mb = phba->sli.mbox_active;
20924 if (mb && (mb->vport == vport)) {
20925 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
20926 (mb->u.mb.mbxCommand == MBX_REG_VPI))
20927 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
20928 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080020929 act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -040020930 /* Put reference count for delayed processing */
20931 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
20932 /* Unregister the RPI when mailbox complete */
20933 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
20934 }
20935 }
James Smart63e801c2010-11-20 23:14:19 -050020936 /* Cleanup any mailbox completions which are not yet processed */
20937 do {
20938 restart_loop = 0;
20939 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
20940 /*
20941 * If this mailox is already processed or it is
20942 * for another vport ignore it.
20943 */
20944 if ((mb->vport != vport) ||
20945 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
20946 continue;
20947
20948 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
20949 (mb->u.mb.mbxCommand != MBX_REG_VPI))
20950 continue;
20951
20952 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
20953 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080020954 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smart63e801c2010-11-20 23:14:19 -050020955 /* Unregister the RPI when mailbox complete */
20956 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
20957 restart_loop = 1;
20958 spin_unlock_irq(&phba->hbalock);
James Smartc6adba12020-11-15 11:26:34 -080020959 spin_lock(&ndlp->lock);
James Smart63e801c2010-11-20 23:14:19 -050020960 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
James Smartc6adba12020-11-15 11:26:34 -080020961 spin_unlock(&ndlp->lock);
James Smart63e801c2010-11-20 23:14:19 -050020962 spin_lock_irq(&phba->hbalock);
20963 break;
20964 }
20965 }
20966 } while (restart_loop);
20967
James Smartd439d282010-09-29 11:18:45 -040020968 spin_unlock_irq(&phba->hbalock);
20969
20970 /* Release the cleaned-up mailbox commands */
20971 while (!list_empty(&mbox_cmd_list)) {
20972 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050020973 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080020974 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
James Smart695a8142010-01-26 23:08:03 -050020975 if (mp) {
20976 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
20977 kfree(mp);
20978 }
James Smart3e1f0712018-11-29 16:09:29 -080020979 mb->ctx_buf = NULL;
20980 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
20981 mb->ctx_ndlp = NULL;
James Smart78730cf2010-04-06 15:06:30 -040020982 if (ndlp) {
James Smartc6adba12020-11-15 11:26:34 -080020983 spin_lock(&ndlp->lock);
James Smart589a52d2010-07-14 15:30:54 -040020984 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
James Smartc6adba12020-11-15 11:26:34 -080020985 spin_unlock(&ndlp->lock);
James Smart78730cf2010-04-06 15:06:30 -040020986 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040020987 }
James Smart695a8142010-01-26 23:08:03 -050020988 }
James Smart695a8142010-01-26 23:08:03 -050020989 mempool_free(mb, phba->mbox_mem_pool);
20990 }
James Smartd439d282010-09-29 11:18:45 -040020991
20992 /* Release the ndlp with the cleaned-up active mailbox command */
20993 if (act_mbx_ndlp) {
James Smartc6adba12020-11-15 11:26:34 -080020994 spin_lock(&act_mbx_ndlp->lock);
James Smartd439d282010-09-29 11:18:45 -040020995 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
James Smartc6adba12020-11-15 11:26:34 -080020996 spin_unlock(&act_mbx_ndlp->lock);
James Smartd439d282010-09-29 11:18:45 -040020997 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050020998 }
James Smart695a8142010-01-26 23:08:03 -050020999}
21000
James Smart2a9bf3d2010-06-07 15:24:45 -040021001/**
21002 * lpfc_drain_txq - Drain the txq
21003 * @phba: Pointer to HBA context object.
21004 *
21005 * This function attempt to submit IOCBs on the txq
21006 * to the adapter. For SLI4 adapters, the txq contains
21007 * ELS IOCBs that have been deferred because the there
21008 * are no SGLs. This congestion can occur with large
21009 * vport counts during node discovery.
21010 **/
21011
21012uint32_t
21013lpfc_drain_txq(struct lpfc_hba *phba)
21014{
21015 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080021016 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090021017 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040021018 unsigned long iflags = 0;
21019 char *fail_msg = NULL;
21020 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080021021 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040021022 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070021023 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040021024
James Smartdc19e3b2018-05-24 21:08:57 -070021025 if (phba->link_flag & LS_MDS_LOOPBACK) {
21026 /* MDS WQE are posted only to first WQ*/
James Smartc00f62e2019-08-14 16:57:11 -070021027 wq = phba->sli4_hba.hdwq[0].io_wq;
James Smartdc19e3b2018-05-24 21:08:57 -070021028 if (unlikely(!wq))
21029 return 0;
21030 pring = wq->pring;
21031 } else {
21032 wq = phba->sli4_hba.els_wq;
21033 if (unlikely(!wq))
21034 return 0;
21035 pring = lpfc_phba_elsring(phba);
21036 }
21037
21038 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070021039 return 0;
James Smart895427b2017-02-12 13:52:30 -080021040
James Smart398d81c2013-05-31 17:04:19 -040021041 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050021042 list_for_each_entry(piocbq, &pring->txq, list) {
21043 txq_cnt++;
21044 }
21045
21046 if (txq_cnt > pring->txq_max)
21047 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040021048
James Smart398d81c2013-05-31 17:04:19 -040021049 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040021050
James Smart0e9bb8d2013-03-01 16:35:12 -050021051 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040021052 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040021053
James Smart19ca7602010-11-20 23:11:55 -050021054 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040021055 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040021056 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedy372c1872020-06-30 14:50:00 -070021057 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta6298522012-06-12 13:54:11 -040021058 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050021059 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040021060 break;
21061 }
James Smart895427b2017-02-12 13:52:30 -080021062 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040021063 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050021064 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040021065 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040021066 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040021067 }
James Smart0e9bb8d2013-03-01 16:35:12 -050021068 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040021069
21070 /* The xri and iocb resources secured,
21071 * attempt to issue request
21072 */
James Smart6d368e52011-05-24 11:44:12 -040021073 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040021074 piocbq->sli4_xritag = sglq->sli4_xritag;
21075 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
21076 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080021077 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040021078 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070021079 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040021080 fail_msg = " - Wq is full";
21081 else
21082 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
21083
21084 if (fail_msg) {
21085 /* Failed means we can't issue and need to cancel */
Dick Kennedy372c1872020-06-30 14:50:00 -070021086 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2a9bf3d2010-06-07 15:24:45 -040021087 "2822 IOCB failed %s iotag 0x%x "
21088 "xri 0x%x\n",
21089 fail_msg,
21090 piocbq->iotag, piocbq->sli4_xritag);
21091 list_add_tail(&piocbq->list, &completions);
21092 }
James Smart398d81c2013-05-31 17:04:19 -040021093 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040021094 }
21095
James Smart2a9bf3d2010-06-07 15:24:45 -040021096 /* Cancel all the IOCBs that cannot be issued */
21097 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
21098 IOERR_SLI_ABORTED);
21099
James Smart0e9bb8d2013-03-01 16:35:12 -050021100 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040021101}
James Smart895427b2017-02-12 13:52:30 -080021102
21103/**
21104 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
21105 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010021106 * @pwqeq: Pointer to command WQE.
James Smart895427b2017-02-12 13:52:30 -080021107 * @sglq: Pointer to the scatter gather queue object.
21108 *
21109 * This routine converts the bpl or bde that is in the WQE
21110 * to a sgl list for the sli4 hardware. The physical address
21111 * of the bpl/bde is converted back to a virtual address.
21112 * If the WQE contains a BPL then the list of BDE's is
21113 * converted to sli4_sge's. If the WQE contains a single
21114 * BDE then it is converted to a single sli_sge.
21115 * The WQE is still in cpu endianness so the contents of
21116 * the bpl can be used without byte swapping.
21117 *
21118 * Returns valid XRI = Success, NO_XRI = Failure.
21119 */
21120static uint16_t
21121lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
21122 struct lpfc_sglq *sglq)
21123{
21124 uint16_t xritag = NO_XRI;
21125 struct ulp_bde64 *bpl = NULL;
21126 struct ulp_bde64 bde;
21127 struct sli4_sge *sgl = NULL;
21128 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080021129 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080021130 int numBdes = 0;
21131 int i = 0;
21132 uint32_t offset = 0; /* accumulated offset in the sg request list */
21133 int inbound = 0; /* number of sg reply entries inbound from firmware */
21134 uint32_t cmd;
21135
21136 if (!pwqeq || !sglq)
21137 return xritag;
21138
21139 sgl = (struct sli4_sge *)sglq->sgl;
21140 wqe = &pwqeq->wqe;
21141 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
21142
21143 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
21144 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
21145 return sglq->sli4_xritag;
21146 numBdes = pwqeq->rsvd2;
21147 if (numBdes) {
21148 /* The addrHigh and addrLow fields within the WQE
21149 * have not been byteswapped yet so there is no
21150 * need to swap them back.
21151 */
21152 if (pwqeq->context3)
21153 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
21154 else
21155 return xritag;
21156
21157 bpl = (struct ulp_bde64 *)dmabuf->virt;
21158 if (!bpl)
21159 return xritag;
21160
21161 for (i = 0; i < numBdes; i++) {
21162 /* Should already be byte swapped. */
21163 sgl->addr_hi = bpl->addrHigh;
21164 sgl->addr_lo = bpl->addrLow;
21165
21166 sgl->word2 = le32_to_cpu(sgl->word2);
21167 if ((i+1) == numBdes)
21168 bf_set(lpfc_sli4_sge_last, sgl, 1);
21169 else
21170 bf_set(lpfc_sli4_sge_last, sgl, 0);
21171 /* swap the size field back to the cpu so we
21172 * can assign it to the sgl.
21173 */
21174 bde.tus.w = le32_to_cpu(bpl->tus.w);
21175 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
21176 /* The offsets in the sgl need to be accumulated
21177 * separately for the request and reply lists.
21178 * The request is always first, the reply follows.
21179 */
21180 switch (cmd) {
21181 case CMD_GEN_REQUEST64_WQE:
21182 /* add up the reply sg entries */
21183 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
21184 inbound++;
21185 /* first inbound? reset the offset */
21186 if (inbound == 1)
21187 offset = 0;
21188 bf_set(lpfc_sli4_sge_offset, sgl, offset);
21189 bf_set(lpfc_sli4_sge_type, sgl,
21190 LPFC_SGE_TYPE_DATA);
21191 offset += bde.tus.f.bdeSize;
21192 break;
21193 case CMD_FCP_TRSP64_WQE:
21194 bf_set(lpfc_sli4_sge_offset, sgl, 0);
21195 bf_set(lpfc_sli4_sge_type, sgl,
21196 LPFC_SGE_TYPE_DATA);
21197 break;
21198 case CMD_FCP_TSEND64_WQE:
21199 case CMD_FCP_TRECEIVE64_WQE:
21200 bf_set(lpfc_sli4_sge_type, sgl,
21201 bpl->tus.f.bdeFlags);
21202 if (i < 3)
21203 offset = 0;
21204 else
21205 offset += bde.tus.f.bdeSize;
21206 bf_set(lpfc_sli4_sge_offset, sgl, offset);
21207 break;
21208 }
21209 sgl->word2 = cpu_to_le32(sgl->word2);
21210 bpl++;
21211 sgl++;
21212 }
21213 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
21214 /* The addrHigh and addrLow fields of the BDE have not
21215 * been byteswapped yet so they need to be swapped
21216 * before putting them in the sgl.
21217 */
21218 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
21219 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
21220 sgl->word2 = le32_to_cpu(sgl->word2);
21221 bf_set(lpfc_sli4_sge_last, sgl, 1);
21222 sgl->word2 = cpu_to_le32(sgl->word2);
21223 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
21224 }
21225 return sglq->sli4_xritag;
21226}
21227
21228/**
21229 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
21230 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010021231 * @qp: Pointer to HDW queue.
James Smart895427b2017-02-12 13:52:30 -080021232 * @pwqe: Pointer to command WQE.
21233 **/
21234int
James Smart1fbf9742019-01-28 11:14:26 -080021235lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
James Smart895427b2017-02-12 13:52:30 -080021236 struct lpfc_iocbq *pwqe)
21237{
James Smart205e8242018-03-05 12:04:03 -080021238 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smart7cacae22020-03-31 09:50:03 -070021239 struct lpfc_async_xchg_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080021240 struct lpfc_queue *wq;
21241 struct lpfc_sglq *sglq;
21242 struct lpfc_sli_ring *pring;
21243 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070021244 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080021245
21246 /* NVME_LS and NVME_LS ABTS requests. */
21247 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
21248 pring = phba->sli4_hba.nvmels_wq->pring;
James Smart6a828b02019-01-28 11:14:31 -080021249 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21250 qp, wq_access);
James Smart895427b2017-02-12 13:52:30 -080021251 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
21252 if (!sglq) {
21253 spin_unlock_irqrestore(&pring->ring_lock, iflags);
21254 return WQE_BUSY;
21255 }
21256 pwqe->sli4_lxritag = sglq->sli4_lxritag;
21257 pwqe->sli4_xritag = sglq->sli4_xritag;
21258 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
21259 spin_unlock_irqrestore(&pring->ring_lock, iflags);
21260 return WQE_ERROR;
21261 }
21262 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21263 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070021264 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
21265 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080021266 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070021267 return ret;
James Smart895427b2017-02-12 13:52:30 -080021268 }
Dick Kennedycd22d602017-08-23 16:55:35 -070021269
James Smart895427b2017-02-12 13:52:30 -080021270 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21271 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080021272
21273 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smart895427b2017-02-12 13:52:30 -080021274 return 0;
21275 }
21276
21277 /* NVME_FCREQ and NVME_ABTS requests */
James Smart02243832021-08-16 09:28:54 -070021278 if (pwqe->iocb_flag & (LPFC_IO_NVME | LPFC_IO_FCP | LPFC_IO_CMF)) {
James Smart895427b2017-02-12 13:52:30 -080021279 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070021280 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080021281 pring = wq->pring;
James Smart895427b2017-02-12 13:52:30 -080021282
James Smartc00f62e2019-08-14 16:57:11 -070021283 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart895427b2017-02-12 13:52:30 -080021284
James Smart6a828b02019-01-28 11:14:31 -080021285 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21286 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070021287 ret = lpfc_sli4_wq_put(wq, wqe);
21288 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080021289 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070021290 return ret;
James Smart895427b2017-02-12 13:52:30 -080021291 }
21292 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21293 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080021294
21295 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smart895427b2017-02-12 13:52:30 -080021296 return 0;
21297 }
21298
James Smartf358dd02017-02-12 13:52:34 -080021299 /* NVMET requests */
21300 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
21301 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070021302 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080021303 pring = wq->pring;
James Smartf358dd02017-02-12 13:52:34 -080021304
James Smartf358dd02017-02-12 13:52:34 -080021305 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070021306 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080021307 if (pwqe->sli4_xritag == NO_XRI) {
21308 pwqe->sli4_lxritag = sglq->sli4_lxritag;
21309 pwqe->sli4_xritag = sglq->sli4_xritag;
21310 }
21311 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21312 pwqe->sli4_xritag);
James Smartc00f62e2019-08-14 16:57:11 -070021313 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart1fbf9742019-01-28 11:14:26 -080021314
James Smart6a828b02019-01-28 11:14:31 -080021315 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21316 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070021317 ret = lpfc_sli4_wq_put(wq, wqe);
21318 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080021319 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070021320 return ret;
James Smartf358dd02017-02-12 13:52:34 -080021321 }
21322 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21323 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080021324
21325 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smartf358dd02017-02-12 13:52:34 -080021326 return 0;
21327 }
James Smart895427b2017-02-12 13:52:30 -080021328 return WQE_ERROR;
21329}
James Smartc4908502019-01-28 11:14:28 -080021330
James Smartdb7531d2020-11-15 11:26:44 -080021331/**
21332 * lpfc_sli4_issue_abort_iotag - SLI-4 WQE init & issue for the Abort
21333 * @phba: Pointer to HBA context object.
21334 * @cmdiocb: Pointer to driver command iocb object.
21335 * @cmpl: completion function.
21336 *
21337 * Fill the appropriate fields for the abort WQE and call
21338 * internal routine lpfc_sli4_issue_wqe to send the WQE
21339 * This function is called with hbalock held and no ring_lock held.
21340 *
21341 * RETURNS 0 - SUCCESS
21342 **/
21343
21344int
21345lpfc_sli4_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
21346 void *cmpl)
21347{
21348 struct lpfc_vport *vport = cmdiocb->vport;
21349 struct lpfc_iocbq *abtsiocb = NULL;
21350 union lpfc_wqe128 *abtswqe;
21351 struct lpfc_io_buf *lpfc_cmd;
21352 int retval = IOCB_ERROR;
21353 u16 xritag = cmdiocb->sli4_xritag;
21354
21355 /*
21356 * The scsi command can not be in txq and it is in flight because the
21357 * pCmd is still pointing at the SCSI command we have to abort. There
21358 * is no need to search the txcmplq. Just send an abort to the FW.
21359 */
21360
21361 abtsiocb = __lpfc_sli_get_iocbq(phba);
21362 if (!abtsiocb)
21363 return WQE_NORESOURCE;
21364
21365 /* Indicate the IO is being aborted by the driver. */
21366 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
21367
21368 abtswqe = &abtsiocb->wqe;
21369 memset(abtswqe, 0, sizeof(*abtswqe));
21370
James Smart696770e2021-05-28 14:22:40 -070021371 if (!lpfc_is_link_up(phba))
James Smartdb7531d2020-11-15 11:26:44 -080021372 bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 1);
James Smartdb7531d2020-11-15 11:26:44 -080021373 bf_set(abort_cmd_criteria, &abtswqe->abort_cmd, T_XRI_TAG);
21374 abtswqe->abort_cmd.rsrvd5 = 0;
21375 abtswqe->abort_cmd.wqe_com.abort_tag = xritag;
21376 bf_set(wqe_reqtag, &abtswqe->abort_cmd.wqe_com, abtsiocb->iotag);
21377 bf_set(wqe_cmnd, &abtswqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
21378 bf_set(wqe_xri_tag, &abtswqe->generic.wqe_com, 0);
21379 bf_set(wqe_qosd, &abtswqe->abort_cmd.wqe_com, 1);
21380 bf_set(wqe_lenloc, &abtswqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
21381 bf_set(wqe_cmd_type, &abtswqe->abort_cmd.wqe_com, OTHER_COMMAND);
21382
21383 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
21384 abtsiocb->hba_wqidx = cmdiocb->hba_wqidx;
21385 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
21386 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
21387 abtsiocb->iocb_flag |= LPFC_IO_FCP;
21388 if (cmdiocb->iocb_flag & LPFC_IO_NVME)
21389 abtsiocb->iocb_flag |= LPFC_IO_NVME;
21390 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
21391 abtsiocb->iocb_flag |= LPFC_IO_FOF;
21392 abtsiocb->vport = vport;
21393 abtsiocb->wqe_cmpl = cmpl;
21394
21395 lpfc_cmd = container_of(cmdiocb, struct lpfc_io_buf, cur_iocbq);
21396 retval = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, abtsiocb);
21397
21398 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
21399 "0359 Abort xri x%x, original iotag x%x, "
21400 "abort cmd iotag x%x retval x%x\n",
21401 xritag, cmdiocb->iotag, abtsiocb->iotag, retval);
21402
21403 if (retval) {
21404 cmdiocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
21405 __lpfc_sli_release_iocbq(phba, abtsiocb);
21406 }
21407
21408 return retval;
21409}
21410
James Smartc4908502019-01-28 11:14:28 -080021411#ifdef LPFC_MXP_STAT
21412/**
21413 * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
21414 * @phba: pointer to lpfc hba data structure.
21415 * @hwqid: belong to which HWQ.
21416 *
21417 * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
21418 * 15 seconds after a test case is running.
21419 *
21420 * The user should call lpfc_debugfs_multixripools_write before running a test
21421 * case to clear stat_snapshot_taken. Then the user starts a test case. During
21422 * test case is running, stat_snapshot_taken is incremented by 1 every time when
21423 * this routine is called from heartbeat timer. When stat_snapshot_taken is
21424 * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
21425 **/
21426void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
21427{
21428 struct lpfc_sli4_hdw_queue *qp;
21429 struct lpfc_multixri_pool *multixri_pool;
21430 struct lpfc_pvt_pool *pvt_pool;
21431 struct lpfc_pbl_pool *pbl_pool;
21432 u32 txcmplq_cnt;
21433
21434 qp = &phba->sli4_hba.hdwq[hwqid];
21435 multixri_pool = qp->p_multixri_pool;
21436 if (!multixri_pool)
21437 return;
21438
21439 if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
21440 pvt_pool = &qp->p_multixri_pool->pvt_pool;
21441 pbl_pool = &qp->p_multixri_pool->pbl_pool;
James Smartc00f62e2019-08-14 16:57:11 -070021442 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080021443
21444 multixri_pool->stat_pbl_count = pbl_pool->count;
21445 multixri_pool->stat_pvt_count = pvt_pool->count;
21446 multixri_pool->stat_busy_count = txcmplq_cnt;
21447 }
21448
21449 multixri_pool->stat_snapshot_taken++;
21450}
21451#endif
21452
21453/**
21454 * lpfc_adjust_pvt_pool_count - Adjust private pool count
21455 * @phba: pointer to lpfc hba data structure.
21456 * @hwqid: belong to which HWQ.
21457 *
21458 * This routine moves some XRIs from private to public pool when private pool
21459 * is not busy.
21460 **/
21461void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
21462{
21463 struct lpfc_multixri_pool *multixri_pool;
21464 u32 io_req_count;
21465 u32 prev_io_req_count;
21466
21467 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21468 if (!multixri_pool)
21469 return;
21470 io_req_count = multixri_pool->io_req_count;
21471 prev_io_req_count = multixri_pool->prev_io_req_count;
21472
21473 if (prev_io_req_count != io_req_count) {
21474 /* Private pool is busy */
21475 multixri_pool->prev_io_req_count = io_req_count;
21476 } else {
21477 /* Private pool is not busy.
21478 * Move XRIs from private to public pool.
21479 */
21480 lpfc_move_xri_pvt_to_pbl(phba, hwqid);
21481 }
21482}
21483
21484/**
21485 * lpfc_adjust_high_watermark - Adjust high watermark
21486 * @phba: pointer to lpfc hba data structure.
21487 * @hwqid: belong to which HWQ.
21488 *
21489 * This routine sets high watermark as number of outstanding XRIs,
21490 * but make sure the new value is between xri_limit/2 and xri_limit.
21491 **/
21492void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
21493{
21494 u32 new_watermark;
21495 u32 watermark_max;
21496 u32 watermark_min;
21497 u32 xri_limit;
21498 u32 txcmplq_cnt;
21499 u32 abts_io_bufs;
21500 struct lpfc_multixri_pool *multixri_pool;
21501 struct lpfc_sli4_hdw_queue *qp;
21502
21503 qp = &phba->sli4_hba.hdwq[hwqid];
21504 multixri_pool = qp->p_multixri_pool;
21505 if (!multixri_pool)
21506 return;
21507 xri_limit = multixri_pool->xri_limit;
21508
21509 watermark_max = xri_limit;
21510 watermark_min = xri_limit / 2;
21511
James Smartc00f62e2019-08-14 16:57:11 -070021512 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080021513 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070021514 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080021515
21516 new_watermark = txcmplq_cnt + abts_io_bufs;
21517 new_watermark = min(watermark_max, new_watermark);
21518 new_watermark = max(watermark_min, new_watermark);
21519 multixri_pool->pvt_pool.high_watermark = new_watermark;
21520
21521#ifdef LPFC_MXP_STAT
21522 multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
21523 new_watermark);
21524#endif
21525}
21526
21527/**
21528 * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
21529 * @phba: pointer to lpfc hba data structure.
21530 * @hwqid: belong to which HWQ.
21531 *
21532 * This routine is called from hearbeat timer when pvt_pool is idle.
21533 * All free XRIs are moved from private to public pool on hwqid with 2 steps.
21534 * The first step moves (all - low_watermark) amount of XRIs.
21535 * The second step moves the rest of XRIs.
21536 **/
21537void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
21538{
21539 struct lpfc_pbl_pool *pbl_pool;
21540 struct lpfc_pvt_pool *pvt_pool;
James Smart6a828b02019-01-28 11:14:31 -080021541 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080021542 struct lpfc_io_buf *lpfc_ncmd;
21543 struct lpfc_io_buf *lpfc_ncmd_next;
21544 unsigned long iflag;
21545 struct list_head tmp_list;
21546 u32 tmp_count;
21547
James Smart6a828b02019-01-28 11:14:31 -080021548 qp = &phba->sli4_hba.hdwq[hwqid];
21549 pbl_pool = &qp->p_multixri_pool->pbl_pool;
21550 pvt_pool = &qp->p_multixri_pool->pvt_pool;
James Smartc4908502019-01-28 11:14:28 -080021551 tmp_count = 0;
21552
James Smart6a828b02019-01-28 11:14:31 -080021553 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
21554 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080021555
21556 if (pvt_pool->count > pvt_pool->low_watermark) {
21557 /* Step 1: move (all - low_watermark) from pvt_pool
21558 * to pbl_pool
21559 */
21560
21561 /* Move low watermark of bufs from pvt_pool to tmp_list */
21562 INIT_LIST_HEAD(&tmp_list);
21563 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21564 &pvt_pool->list, list) {
21565 list_move_tail(&lpfc_ncmd->list, &tmp_list);
21566 tmp_count++;
21567 if (tmp_count >= pvt_pool->low_watermark)
21568 break;
21569 }
21570
21571 /* Move all bufs from pvt_pool to pbl_pool */
21572 list_splice_init(&pvt_pool->list, &pbl_pool->list);
21573
21574 /* Move all bufs from tmp_list to pvt_pool */
21575 list_splice(&tmp_list, &pvt_pool->list);
21576
21577 pbl_pool->count += (pvt_pool->count - tmp_count);
21578 pvt_pool->count = tmp_count;
21579 } else {
21580 /* Step 2: move the rest from pvt_pool to pbl_pool */
21581 list_splice_init(&pvt_pool->list, &pbl_pool->list);
21582 pbl_pool->count += pvt_pool->count;
21583 pvt_pool->count = 0;
21584 }
21585
21586 spin_unlock(&pvt_pool->lock);
21587 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21588}
21589
21590/**
21591 * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21592 * @phba: pointer to lpfc hba data structure
Lee Jones7af29d42020-07-21 17:41:31 +010021593 * @qp: pointer to HDW queue
James Smartc4908502019-01-28 11:14:28 -080021594 * @pbl_pool: specified public free XRI pool
21595 * @pvt_pool: specified private free XRI pool
21596 * @count: number of XRIs to move
21597 *
21598 * This routine tries to move some free common bufs from the specified pbl_pool
21599 * to the specified pvt_pool. It might move less than count XRIs if there's not
21600 * enough in public pool.
21601 *
21602 * Return:
21603 * true - if XRIs are successfully moved from the specified pbl_pool to the
21604 * specified pvt_pool
21605 * false - if the specified pbl_pool is empty or locked by someone else
21606 **/
21607static bool
James Smart6a828b02019-01-28 11:14:31 -080021608_lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21609 struct lpfc_pbl_pool *pbl_pool,
James Smartc4908502019-01-28 11:14:28 -080021610 struct lpfc_pvt_pool *pvt_pool, u32 count)
21611{
21612 struct lpfc_io_buf *lpfc_ncmd;
21613 struct lpfc_io_buf *lpfc_ncmd_next;
21614 unsigned long iflag;
21615 int ret;
21616
21617 ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
21618 if (ret) {
21619 if (pbl_pool->count) {
21620 /* Move a batch of XRIs from public to private pool */
James Smart6a828b02019-01-28 11:14:31 -080021621 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080021622 list_for_each_entry_safe(lpfc_ncmd,
21623 lpfc_ncmd_next,
21624 &pbl_pool->list,
21625 list) {
21626 list_move_tail(&lpfc_ncmd->list,
21627 &pvt_pool->list);
21628 pvt_pool->count++;
21629 pbl_pool->count--;
21630 count--;
21631 if (count == 0)
21632 break;
21633 }
21634
21635 spin_unlock(&pvt_pool->lock);
21636 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21637 return true;
21638 }
21639 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21640 }
21641
21642 return false;
21643}
21644
21645/**
21646 * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21647 * @phba: pointer to lpfc hba data structure.
21648 * @hwqid: belong to which HWQ.
21649 * @count: number of XRIs to move
21650 *
21651 * This routine tries to find some free common bufs in one of public pools with
21652 * Round Robin method. The search always starts from local hwqid, then the next
21653 * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
21654 * a batch of free common bufs are moved to private pool on hwqid.
21655 * It might move less than count XRIs if there's not enough in public pool.
21656 **/
21657void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
21658{
21659 struct lpfc_multixri_pool *multixri_pool;
21660 struct lpfc_multixri_pool *next_multixri_pool;
21661 struct lpfc_pvt_pool *pvt_pool;
21662 struct lpfc_pbl_pool *pbl_pool;
James Smart6a828b02019-01-28 11:14:31 -080021663 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080021664 u32 next_hwqid;
21665 u32 hwq_count;
21666 int ret;
21667
James Smart6a828b02019-01-28 11:14:31 -080021668 qp = &phba->sli4_hba.hdwq[hwqid];
21669 multixri_pool = qp->p_multixri_pool;
James Smartc4908502019-01-28 11:14:28 -080021670 pvt_pool = &multixri_pool->pvt_pool;
21671 pbl_pool = &multixri_pool->pbl_pool;
21672
21673 /* Check if local pbl_pool is available */
James Smart6a828b02019-01-28 11:14:31 -080021674 ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080021675 if (ret) {
21676#ifdef LPFC_MXP_STAT
21677 multixri_pool->local_pbl_hit_count++;
21678#endif
21679 return;
21680 }
21681
21682 hwq_count = phba->cfg_hdw_queue;
21683
21684 /* Get the next hwqid which was found last time */
21685 next_hwqid = multixri_pool->rrb_next_hwqid;
21686
21687 do {
21688 /* Go to next hwq */
21689 next_hwqid = (next_hwqid + 1) % hwq_count;
21690
21691 next_multixri_pool =
21692 phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
21693 pbl_pool = &next_multixri_pool->pbl_pool;
21694
21695 /* Check if the public free xri pool is available */
21696 ret = _lpfc_move_xri_pbl_to_pvt(
James Smart6a828b02019-01-28 11:14:31 -080021697 phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080021698
21699 /* Exit while-loop if success or all hwqid are checked */
21700 } while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
21701
21702 /* Starting point for the next time */
21703 multixri_pool->rrb_next_hwqid = next_hwqid;
21704
21705 if (!ret) {
21706 /* stats: all public pools are empty*/
21707 multixri_pool->pbl_empty_count++;
21708 }
21709
21710#ifdef LPFC_MXP_STAT
21711 if (ret) {
21712 if (next_hwqid == hwqid)
21713 multixri_pool->local_pbl_hit_count++;
21714 else
21715 multixri_pool->other_pbl_hit_count++;
21716 }
21717#endif
21718}
21719
21720/**
21721 * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
21722 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010021723 * @hwqid: belong to which HWQ.
James Smartc4908502019-01-28 11:14:28 -080021724 *
21725 * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
21726 * low watermark.
21727 **/
21728void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
21729{
21730 struct lpfc_multixri_pool *multixri_pool;
21731 struct lpfc_pvt_pool *pvt_pool;
21732
21733 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21734 pvt_pool = &multixri_pool->pvt_pool;
21735
21736 if (pvt_pool->count < pvt_pool->low_watermark)
21737 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21738}
21739
21740/**
21741 * lpfc_release_io_buf - Return one IO buf back to free pool
21742 * @phba: pointer to lpfc hba data structure.
21743 * @lpfc_ncmd: IO buf to be returned.
21744 * @qp: belong to which HWQ.
21745 *
21746 * This routine returns one IO buf back to free pool. If this is an urgent IO,
21747 * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
21748 * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
21749 * xri_limit. If cfg_xri_rebalancing==0, the IO buf is returned to
21750 * lpfc_io_buf_list_put.
21751 **/
21752void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
21753 struct lpfc_sli4_hdw_queue *qp)
21754{
21755 unsigned long iflag;
21756 struct lpfc_pbl_pool *pbl_pool;
21757 struct lpfc_pvt_pool *pvt_pool;
21758 struct lpfc_epd_pool *epd_pool;
21759 u32 txcmplq_cnt;
21760 u32 xri_owned;
21761 u32 xri_limit;
21762 u32 abts_io_bufs;
21763
21764 /* MUST zero fields if buffer is reused by another protocol */
21765 lpfc_ncmd->nvmeCmd = NULL;
21766 lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
21767 lpfc_ncmd->cur_iocbq.iocb_cmpl = NULL;
21768
James Smart35a635a2019-09-21 20:59:02 -070021769 if (phba->cfg_xpsgl && !phba->nvmet_support &&
21770 !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
21771 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
21772
21773 if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
21774 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
21775
James Smartc4908502019-01-28 11:14:28 -080021776 if (phba->cfg_xri_rebalancing) {
21777 if (lpfc_ncmd->expedite) {
21778 /* Return to expedite pool */
21779 epd_pool = &phba->epd_pool;
21780 spin_lock_irqsave(&epd_pool->lock, iflag);
21781 list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
21782 epd_pool->count++;
21783 spin_unlock_irqrestore(&epd_pool->lock, iflag);
21784 return;
21785 }
21786
21787 /* Avoid invalid access if an IO sneaks in and is being rejected
21788 * just _after_ xri pools are destroyed in lpfc_offline.
21789 * Nothing much can be done at this point.
21790 */
21791 if (!qp->p_multixri_pool)
21792 return;
21793
21794 pbl_pool = &qp->p_multixri_pool->pbl_pool;
21795 pvt_pool = &qp->p_multixri_pool->pvt_pool;
21796
James Smartc00f62e2019-08-14 16:57:11 -070021797 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080021798 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070021799 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080021800
21801 xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
21802 xri_limit = qp->p_multixri_pool->xri_limit;
21803
21804#ifdef LPFC_MXP_STAT
21805 if (xri_owned <= xri_limit)
21806 qp->p_multixri_pool->below_limit_count++;
21807 else
21808 qp->p_multixri_pool->above_limit_count++;
21809#endif
21810
21811 /* XRI goes to either public or private free xri pool
21812 * based on watermark and xri_limit
21813 */
21814 if ((pvt_pool->count < pvt_pool->low_watermark) ||
21815 (xri_owned < xri_limit &&
21816 pvt_pool->count < pvt_pool->high_watermark)) {
James Smart6a828b02019-01-28 11:14:31 -080021817 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
21818 qp, free_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080021819 list_add_tail(&lpfc_ncmd->list,
21820 &pvt_pool->list);
21821 pvt_pool->count++;
21822 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21823 } else {
James Smart6a828b02019-01-28 11:14:31 -080021824 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
21825 qp, free_pub_pool);
James Smartc4908502019-01-28 11:14:28 -080021826 list_add_tail(&lpfc_ncmd->list,
21827 &pbl_pool->list);
21828 pbl_pool->count++;
21829 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21830 }
21831 } else {
James Smart6a828b02019-01-28 11:14:31 -080021832 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
21833 qp, free_xri);
James Smartc4908502019-01-28 11:14:28 -080021834 list_add_tail(&lpfc_ncmd->list,
21835 &qp->lpfc_io_buf_list_put);
21836 qp->put_io_bufs++;
21837 spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
21838 iflag);
21839 }
21840}
21841
21842/**
21843 * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
21844 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010021845 * @qp: pointer to HDW queue
James Smartc4908502019-01-28 11:14:28 -080021846 * @pvt_pool: pointer to private pool data structure.
21847 * @ndlp: pointer to lpfc nodelist data structure.
21848 *
21849 * This routine tries to get one free IO buf from private pool.
21850 *
21851 * Return:
21852 * pointer to one free IO buf - if private pool is not empty
21853 * NULL - if private pool is empty
21854 **/
21855static struct lpfc_io_buf *
21856lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
James Smart6a828b02019-01-28 11:14:31 -080021857 struct lpfc_sli4_hdw_queue *qp,
James Smartc4908502019-01-28 11:14:28 -080021858 struct lpfc_pvt_pool *pvt_pool,
21859 struct lpfc_nodelist *ndlp)
21860{
21861 struct lpfc_io_buf *lpfc_ncmd;
21862 struct lpfc_io_buf *lpfc_ncmd_next;
21863 unsigned long iflag;
21864
James Smart6a828b02019-01-28 11:14:31 -080021865 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080021866 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21867 &pvt_pool->list, list) {
21868 if (lpfc_test_rrq_active(
21869 phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
21870 continue;
21871 list_del(&lpfc_ncmd->list);
21872 pvt_pool->count--;
21873 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21874 return lpfc_ncmd;
21875 }
21876 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21877
21878 return NULL;
21879}
21880
21881/**
21882 * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
21883 * @phba: pointer to lpfc hba data structure.
21884 *
21885 * This routine tries to get one free IO buf from expedite pool.
21886 *
21887 * Return:
21888 * pointer to one free IO buf - if expedite pool is not empty
21889 * NULL - if expedite pool is empty
21890 **/
21891static struct lpfc_io_buf *
21892lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
21893{
21894 struct lpfc_io_buf *lpfc_ncmd;
21895 struct lpfc_io_buf *lpfc_ncmd_next;
21896 unsigned long iflag;
21897 struct lpfc_epd_pool *epd_pool;
21898
21899 epd_pool = &phba->epd_pool;
21900 lpfc_ncmd = NULL;
21901
21902 spin_lock_irqsave(&epd_pool->lock, iflag);
21903 if (epd_pool->count > 0) {
21904 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21905 &epd_pool->list, list) {
21906 list_del(&lpfc_ncmd->list);
21907 epd_pool->count--;
21908 break;
21909 }
21910 }
21911 spin_unlock_irqrestore(&epd_pool->lock, iflag);
21912
21913 return lpfc_ncmd;
21914}
21915
21916/**
21917 * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
21918 * @phba: pointer to lpfc hba data structure.
21919 * @ndlp: pointer to lpfc nodelist data structure.
21920 * @hwqid: belong to which HWQ
21921 * @expedite: 1 means this request is urgent.
21922 *
21923 * This routine will do the following actions and then return a pointer to
21924 * one free IO buf.
21925 *
21926 * 1. If private free xri count is empty, move some XRIs from public to
21927 * private pool.
21928 * 2. Get one XRI from private free xri pool.
21929 * 3. If we fail to get one from pvt_pool and this is an expedite request,
21930 * get one free xri from expedite pool.
21931 *
21932 * Note: ndlp is only used on SCSI side for RRQ testing.
21933 * The caller should pass NULL for ndlp on NVME side.
21934 *
21935 * Return:
21936 * pointer to one free IO buf - if private pool is not empty
21937 * NULL - if private pool is empty
21938 **/
21939static struct lpfc_io_buf *
21940lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
21941 struct lpfc_nodelist *ndlp,
21942 int hwqid, int expedite)
21943{
21944 struct lpfc_sli4_hdw_queue *qp;
21945 struct lpfc_multixri_pool *multixri_pool;
21946 struct lpfc_pvt_pool *pvt_pool;
21947 struct lpfc_io_buf *lpfc_ncmd;
21948
21949 qp = &phba->sli4_hba.hdwq[hwqid];
21950 lpfc_ncmd = NULL;
21951 multixri_pool = qp->p_multixri_pool;
21952 pvt_pool = &multixri_pool->pvt_pool;
21953 multixri_pool->io_req_count++;
21954
21955 /* If pvt_pool is empty, move some XRIs from public to private pool */
21956 if (pvt_pool->count == 0)
21957 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21958
21959 /* Get one XRI from private free xri pool */
James Smart6a828b02019-01-28 11:14:31 -080021960 lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
James Smartc4908502019-01-28 11:14:28 -080021961
21962 if (lpfc_ncmd) {
21963 lpfc_ncmd->hdwq = qp;
21964 lpfc_ncmd->hdwq_no = hwqid;
21965 } else if (expedite) {
21966 /* If we fail to get one from pvt_pool and this is an expedite
21967 * request, get one free xri from expedite pool.
21968 */
21969 lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
21970 }
21971
21972 return lpfc_ncmd;
21973}
21974
21975static inline struct lpfc_io_buf *
21976lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
21977{
21978 struct lpfc_sli4_hdw_queue *qp;
21979 struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
21980
21981 qp = &phba->sli4_hba.hdwq[idx];
21982 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
21983 &qp->lpfc_io_buf_list_get, list) {
21984 if (lpfc_test_rrq_active(phba, ndlp,
21985 lpfc_cmd->cur_iocbq.sli4_lxritag))
21986 continue;
21987
21988 if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
21989 continue;
21990
21991 list_del_init(&lpfc_cmd->list);
21992 qp->get_io_bufs--;
21993 lpfc_cmd->hdwq = qp;
21994 lpfc_cmd->hdwq_no = idx;
21995 return lpfc_cmd;
21996 }
21997 return NULL;
21998}
21999
22000/**
22001 * lpfc_get_io_buf - Get one IO buffer from free pool
22002 * @phba: The HBA for which this call is being executed.
22003 * @ndlp: pointer to lpfc nodelist data structure.
22004 * @hwqid: belong to which HWQ
22005 * @expedite: 1 means this request is urgent.
22006 *
22007 * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
22008 * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
22009 * a IO buffer from head of @hdwq io_buf_list and returns to caller.
22010 *
22011 * Note: ndlp is only used on SCSI side for RRQ testing.
22012 * The caller should pass NULL for ndlp on NVME side.
22013 *
22014 * Return codes:
22015 * NULL - Error
22016 * Pointer to lpfc_io_buf - Success
22017 **/
22018struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
22019 struct lpfc_nodelist *ndlp,
22020 u32 hwqid, int expedite)
22021{
22022 struct lpfc_sli4_hdw_queue *qp;
22023 unsigned long iflag;
22024 struct lpfc_io_buf *lpfc_cmd;
22025
22026 qp = &phba->sli4_hba.hdwq[hwqid];
22027 lpfc_cmd = NULL;
22028
22029 if (phba->cfg_xri_rebalancing)
22030 lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
22031 phba, ndlp, hwqid, expedite);
22032 else {
James Smart6a828b02019-01-28 11:14:31 -080022033 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
22034 qp, alloc_xri_get);
James Smartc4908502019-01-28 11:14:28 -080022035 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
22036 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22037 if (!lpfc_cmd) {
James Smart6a828b02019-01-28 11:14:31 -080022038 lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
22039 qp, alloc_xri_put);
James Smartc4908502019-01-28 11:14:28 -080022040 list_splice(&qp->lpfc_io_buf_list_put,
22041 &qp->lpfc_io_buf_list_get);
22042 qp->get_io_bufs += qp->put_io_bufs;
22043 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
22044 qp->put_io_bufs = 0;
22045 spin_unlock(&qp->io_buf_list_put_lock);
22046 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
22047 expedite)
22048 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22049 }
22050 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
22051 }
22052
22053 return lpfc_cmd;
22054}
James Smartd79c9e92019-08-14 16:57:09 -070022055
22056/**
James Smart72df8a452021-08-16 09:28:52 -070022057 * lpfc_read_object - Retrieve object data from HBA
22058 * @phba: The HBA for which this call is being executed.
22059 * @rdobject: Pathname of object data we want to read.
22060 * @datap: Pointer to where data will be copied to.
22061 * @datasz: size of data area
22062 *
22063 * This routine is limited to object sizes of LPFC_BPL_SIZE (1024) or less.
22064 * The data will be truncated if datasz is not large enough.
22065 * Version 1 is not supported with Embedded mbox cmd, so we must use version 0.
22066 * Returns the actual bytes read from the object.
22067 */
22068int
22069lpfc_read_object(struct lpfc_hba *phba, char *rdobject, uint32_t *datap,
22070 uint32_t datasz)
22071{
22072 struct lpfc_mbx_read_object *read_object;
22073 LPFC_MBOXQ_t *mbox;
22074 int rc, length, eof, j, byte_cnt = 0;
22075 uint32_t shdr_status, shdr_add_status;
22076 union lpfc_sli4_cfg_shdr *shdr;
22077 struct lpfc_dmabuf *pcmd;
22078
22079 /* sanity check on queue memory */
22080 if (!datap)
22081 return -ENODEV;
22082
22083 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
22084 if (!mbox)
22085 return -ENOMEM;
22086 length = (sizeof(struct lpfc_mbx_read_object) -
22087 sizeof(struct lpfc_sli4_cfg_mhdr));
22088 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
22089 LPFC_MBOX_OPCODE_READ_OBJECT,
22090 length, LPFC_SLI4_MBX_EMBED);
22091 read_object = &mbox->u.mqe.un.read_object;
22092 shdr = (union lpfc_sli4_cfg_shdr *)&read_object->header.cfg_shdr;
22093
22094 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_0);
22095 bf_set(lpfc_mbx_rd_object_rlen, &read_object->u.request, datasz);
22096 read_object->u.request.rd_object_offset = 0;
22097 read_object->u.request.rd_object_cnt = 1;
22098
22099 memset((void *)read_object->u.request.rd_object_name, 0,
22100 LPFC_OBJ_NAME_SZ);
22101 sprintf((uint8_t *)read_object->u.request.rd_object_name, rdobject);
22102 for (j = 0; j < strlen(rdobject); j++)
22103 read_object->u.request.rd_object_name[j] =
22104 cpu_to_le32(read_object->u.request.rd_object_name[j]);
22105
22106 pcmd = kmalloc(sizeof(*pcmd), GFP_KERNEL);
22107 if (pcmd)
22108 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
22109 if (!pcmd || !pcmd->virt) {
22110 kfree(pcmd);
22111 mempool_free(mbox, phba->mbox_mem_pool);
22112 return -ENOMEM;
22113 }
22114 memset((void *)pcmd->virt, 0, LPFC_BPL_SIZE);
22115 read_object->u.request.rd_object_hbuf[0].pa_lo =
22116 putPaddrLow(pcmd->phys);
22117 read_object->u.request.rd_object_hbuf[0].pa_hi =
22118 putPaddrHigh(pcmd->phys);
22119 read_object->u.request.rd_object_hbuf[0].length = LPFC_BPL_SIZE;
22120
22121 mbox->vport = phba->pport;
22122 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
22123 mbox->ctx_buf = NULL;
22124 mbox->ctx_ndlp = NULL;
22125
22126 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
22127 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
22128 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
22129
22130 if (shdr_status == STATUS_FAILED &&
22131 shdr_add_status == ADD_STATUS_INVALID_OBJECT_NAME) {
22132 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22133 "4674 No port cfg file in FW.\n");
22134 byte_cnt = -ENOENT;
22135 } else if (shdr_status || shdr_add_status || rc) {
22136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22137 "2625 READ_OBJECT mailbox failed with "
22138 "status x%x add_status x%x, mbx status x%x\n",
22139 shdr_status, shdr_add_status, rc);
22140 byte_cnt = -ENXIO;
22141 } else {
22142 /* Success */
22143 length = read_object->u.response.rd_object_actual_rlen;
22144 eof = bf_get(lpfc_mbx_rd_object_eof, &read_object->u.response);
22145 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_CGN_MGMT,
22146 "2626 READ_OBJECT Success len %d:%d, EOF %d\n",
22147 length, datasz, eof);
22148
22149 /* Detect the port config file exists but is empty */
22150 if (!length && eof) {
22151 byte_cnt = 0;
22152 goto exit;
22153 }
22154
22155 byte_cnt = length;
22156 lpfc_sli_pcimem_bcopy(pcmd->virt, datap, byte_cnt);
22157 }
22158
22159 exit:
22160 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
22161 kfree(pcmd);
22162 mempool_free(mbox, phba->mbox_mem_pool);
22163 return byte_cnt;
22164}
22165
22166/**
James Smartd79c9e92019-08-14 16:57:09 -070022167 * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
22168 * @phba: The HBA for which this call is being executed.
22169 * @lpfc_buf: IO buf structure to append the SGL chunk
22170 *
22171 * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
22172 * and will allocate an SGL chunk if the pool is empty.
22173 *
22174 * Return codes:
22175 * NULL - Error
22176 * Pointer to sli4_hybrid_sgl - Success
22177 **/
22178struct sli4_hybrid_sgl *
22179lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22180{
22181 struct sli4_hybrid_sgl *list_entry = NULL;
22182 struct sli4_hybrid_sgl *tmp = NULL;
22183 struct sli4_hybrid_sgl *allocated_sgl = NULL;
22184 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22185 struct list_head *buf_list = &hdwq->sgl_list;
James Smarta4c21ac2019-09-21 20:59:01 -070022186 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070022187
James Smarta4c21ac2019-09-21 20:59:01 -070022188 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022189
22190 if (likely(!list_empty(buf_list))) {
22191 /* break off 1 chunk from the sgl_list */
22192 list_for_each_entry_safe(list_entry, tmp,
22193 buf_list, list_node) {
22194 list_move_tail(&list_entry->list_node,
22195 &lpfc_buf->dma_sgl_xtra_list);
22196 break;
22197 }
22198 } else {
22199 /* allocate more */
James Smarta4c21ac2019-09-21 20:59:01 -070022200 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022201 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
James Smart4583a4f2019-11-15 16:38:47 -080022202 cpu_to_node(hdwq->io_wq->chann));
James Smartd79c9e92019-08-14 16:57:09 -070022203 if (!tmp) {
22204 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22205 "8353 error kmalloc memory for HDWQ "
22206 "%d %s\n",
22207 lpfc_buf->hdwq_no, __func__);
22208 return NULL;
22209 }
22210
22211 tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
22212 GFP_ATOMIC, &tmp->dma_phys_sgl);
22213 if (!tmp->dma_sgl) {
22214 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22215 "8354 error pool_alloc memory for HDWQ "
22216 "%d %s\n",
22217 lpfc_buf->hdwq_no, __func__);
22218 kfree(tmp);
22219 return NULL;
22220 }
22221
James Smarta4c21ac2019-09-21 20:59:01 -070022222 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022223 list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
22224 }
22225
22226 allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
22227 struct sli4_hybrid_sgl,
22228 list_node);
22229
James Smarta4c21ac2019-09-21 20:59:01 -070022230 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022231
22232 return allocated_sgl;
22233}
22234
22235/**
22236 * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
22237 * @phba: The HBA for which this call is being executed.
22238 * @lpfc_buf: IO buf structure with the SGL chunk
22239 *
22240 * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
22241 *
22242 * Return codes:
22243 * 0 - Success
22244 * -EINVAL - Error
22245 **/
22246int
22247lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22248{
22249 int rc = 0;
22250 struct sli4_hybrid_sgl *list_entry = NULL;
22251 struct sli4_hybrid_sgl *tmp = NULL;
22252 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22253 struct list_head *buf_list = &hdwq->sgl_list;
James Smarta4c21ac2019-09-21 20:59:01 -070022254 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070022255
James Smarta4c21ac2019-09-21 20:59:01 -070022256 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022257
22258 if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
22259 list_for_each_entry_safe(list_entry, tmp,
22260 &lpfc_buf->dma_sgl_xtra_list,
22261 list_node) {
22262 list_move_tail(&list_entry->list_node,
22263 buf_list);
22264 }
22265 } else {
22266 rc = -EINVAL;
22267 }
22268
James Smarta4c21ac2019-09-21 20:59:01 -070022269 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022270 return rc;
22271}
22272
22273/**
22274 * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
22275 * @phba: phba object
22276 * @hdwq: hdwq to cleanup sgl buff resources on
22277 *
22278 * This routine frees all SGL chunks of hdwq SGL chunk pool.
22279 *
22280 * Return codes:
22281 * None
22282 **/
22283void
22284lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
22285 struct lpfc_sli4_hdw_queue *hdwq)
22286{
22287 struct list_head *buf_list = &hdwq->sgl_list;
22288 struct sli4_hybrid_sgl *list_entry = NULL;
22289 struct sli4_hybrid_sgl *tmp = NULL;
James Smarta4c21ac2019-09-21 20:59:01 -070022290 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070022291
James Smarta4c21ac2019-09-21 20:59:01 -070022292 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022293
22294 /* Free sgl pool */
22295 list_for_each_entry_safe(list_entry, tmp,
22296 buf_list, list_node) {
22297 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
22298 list_entry->dma_sgl,
22299 list_entry->dma_phys_sgl);
22300 list_del(&list_entry->list_node);
22301 kfree(list_entry);
22302 }
22303
James Smarta4c21ac2019-09-21 20:59:01 -070022304 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022305}
22306
22307/**
22308 * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
22309 * @phba: The HBA for which this call is being executed.
22310 * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
22311 *
22312 * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
22313 * and will allocate an CMD/RSP buffer if the pool is empty.
22314 *
22315 * Return codes:
22316 * NULL - Error
22317 * Pointer to fcp_cmd_rsp_buf - Success
22318 **/
22319struct fcp_cmd_rsp_buf *
22320lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22321 struct lpfc_io_buf *lpfc_buf)
22322{
22323 struct fcp_cmd_rsp_buf *list_entry = NULL;
22324 struct fcp_cmd_rsp_buf *tmp = NULL;
22325 struct fcp_cmd_rsp_buf *allocated_buf = NULL;
22326 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22327 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
James Smarta4c21ac2019-09-21 20:59:01 -070022328 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070022329
James Smarta4c21ac2019-09-21 20:59:01 -070022330 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022331
22332 if (likely(!list_empty(buf_list))) {
22333 /* break off 1 chunk from the list */
22334 list_for_each_entry_safe(list_entry, tmp,
22335 buf_list,
22336 list_node) {
22337 list_move_tail(&list_entry->list_node,
22338 &lpfc_buf->dma_cmd_rsp_list);
22339 break;
22340 }
22341 } else {
22342 /* allocate more */
James Smarta4c21ac2019-09-21 20:59:01 -070022343 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022344 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
James Smart4583a4f2019-11-15 16:38:47 -080022345 cpu_to_node(hdwq->io_wq->chann));
James Smartd79c9e92019-08-14 16:57:09 -070022346 if (!tmp) {
22347 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22348 "8355 error kmalloc memory for HDWQ "
22349 "%d %s\n",
22350 lpfc_buf->hdwq_no, __func__);
22351 return NULL;
22352 }
22353
22354 tmp->fcp_cmnd = dma_pool_alloc(phba->lpfc_cmd_rsp_buf_pool,
22355 GFP_ATOMIC,
22356 &tmp->fcp_cmd_rsp_dma_handle);
22357
22358 if (!tmp->fcp_cmnd) {
22359 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22360 "8356 error pool_alloc memory for HDWQ "
22361 "%d %s\n",
22362 lpfc_buf->hdwq_no, __func__);
22363 kfree(tmp);
22364 return NULL;
22365 }
22366
22367 tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
22368 sizeof(struct fcp_cmnd));
22369
James Smarta4c21ac2019-09-21 20:59:01 -070022370 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022371 list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
22372 }
22373
22374 allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
22375 struct fcp_cmd_rsp_buf,
22376 list_node);
22377
James Smarta4c21ac2019-09-21 20:59:01 -070022378 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022379
22380 return allocated_buf;
22381}
22382
22383/**
22384 * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
22385 * @phba: The HBA for which this call is being executed.
22386 * @lpfc_buf: IO buf structure with the CMD/RSP buf
22387 *
22388 * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
22389 *
22390 * Return codes:
22391 * 0 - Success
22392 * -EINVAL - Error
22393 **/
22394int
22395lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22396 struct lpfc_io_buf *lpfc_buf)
22397{
22398 int rc = 0;
22399 struct fcp_cmd_rsp_buf *list_entry = NULL;
22400 struct fcp_cmd_rsp_buf *tmp = NULL;
22401 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22402 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
James Smarta4c21ac2019-09-21 20:59:01 -070022403 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070022404
James Smarta4c21ac2019-09-21 20:59:01 -070022405 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022406
22407 if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
22408 list_for_each_entry_safe(list_entry, tmp,
22409 &lpfc_buf->dma_cmd_rsp_list,
22410 list_node) {
22411 list_move_tail(&list_entry->list_node,
22412 buf_list);
22413 }
22414 } else {
22415 rc = -EINVAL;
22416 }
22417
James Smarta4c21ac2019-09-21 20:59:01 -070022418 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022419 return rc;
22420}
22421
22422/**
22423 * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
22424 * @phba: phba object
22425 * @hdwq: hdwq to cleanup cmd rsp buff resources on
22426 *
22427 * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
22428 *
22429 * Return codes:
22430 * None
22431 **/
22432void
22433lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22434 struct lpfc_sli4_hdw_queue *hdwq)
22435{
22436 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22437 struct fcp_cmd_rsp_buf *list_entry = NULL;
22438 struct fcp_cmd_rsp_buf *tmp = NULL;
James Smarta4c21ac2019-09-21 20:59:01 -070022439 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070022440
James Smarta4c21ac2019-09-21 20:59:01 -070022441 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022442
22443 /* Free cmd_rsp buf pool */
22444 list_for_each_entry_safe(list_entry, tmp,
22445 buf_list,
22446 list_node) {
22447 dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
22448 list_entry->fcp_cmnd,
22449 list_entry->fcp_cmd_rsp_dma_handle);
22450 list_del(&list_entry->list_node);
22451 kfree(list_entry);
22452 }
22453
James Smarta4c21ac2019-09-21 20:59:01 -070022454 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022455}