blob: 430abebf99f1513b3ae49ebb70db5d92842a62f3 [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) &&
James Smart25ac2c972021-09-10 16:31:54 -07001407 (!(unlikely(pci_channel_offline(phba->pcidev)))) &&
1408 sglq->state != SGL_XRI_ABORTED) {
James Smart895427b2017-02-12 13:52:30 -08001409 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1410 iflag);
James Smart307e3382020-11-15 11:26:30 -08001411
1412 /* Check if we can get a reference on ndlp */
1413 if (sglq->ndlp && !lpfc_nlp_get(sglq->ndlp))
1414 sglq->ndlp = NULL;
1415
James Smart341af102010-01-26 23:07:37 -05001416 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001417 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001418 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001419 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001420 } else {
James Smart895427b2017-02-12 13:52:30 -08001421 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1422 iflag);
James Smart4f774512009-05-22 14:52:35 -04001423 sglq->state = SGL_FREED;
1424 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001425 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001426 &phba->sli4_hba.lpfc_els_sgl_list);
1427 spin_unlock_irqrestore(
1428 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart9628aac2021-03-01 09:18:16 -08001429 pring = lpfc_phba_elsring(phba);
James Smart2a9bf3d2010-06-07 15:24:45 -04001430 /* Check if TXQ queue needs to be serviced */
James Smart9628aac2021-03-01 09:18:16 -08001431 if (pring && (!list_empty(&pring->txq)))
James Smart2a9bf3d2010-06-07 15:24:45 -04001432 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001433 }
James Smart4f774512009-05-22 14:52:35 -04001434 }
1435
James Smartf358dd02017-02-12 13:52:34 -08001436out:
James Smart4f774512009-05-22 14:52:35 -04001437 /*
1438 * Clean all volatile data fields, preserve iotag and node struct.
1439 */
1440 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001441 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001442 iocbq->sli4_xritag = NO_XRI;
James Smart02243832021-08-16 09:28:54 -07001443 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET | LPFC_IO_CMF |
James Smartf358dd02017-02-12 13:52:34 -08001444 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001445 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1446}
1447
James Smart2a9bf3d2010-06-07 15:24:45 -04001448
James Smart4f774512009-05-22 14:52:35 -04001449/**
James Smart3772a992009-05-22 14:50:54 -04001450 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1451 * @phba: Pointer to HBA context object.
1452 * @iocbq: Pointer to driver iocb object.
1453 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001454 * This function is called to release the driver iocb object to the
1455 * iocb pool. The iotag in the iocb object does not change for each
1456 * use of the iocb object. This function clears all other fields of
1457 * the iocb object when it is freed. The hbalock is asserted held in
1458 * the code path calling this routine.
James Smart3772a992009-05-22 14:50:54 -04001459 **/
1460static void
1461__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1462{
1463 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1464
James Smart0e9bb8d2013-03-01 16:35:12 -05001465 /*
James Smart3772a992009-05-22 14:50:54 -04001466 * Clean all volatile data fields, preserve iotag and node struct.
1467 */
1468 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1469 iocbq->sli4_xritag = NO_XRI;
1470 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1471}
1472
1473/**
James Smart3621a712009-04-06 18:47:14 -04001474 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001475 * @phba: Pointer to HBA context object.
1476 * @iocbq: Pointer to driver iocb object.
1477 *
1478 * This function is called with hbalock held to release driver
1479 * iocb object to the iocb pool. The iotag in the iocb object
1480 * does not change for each use of the iocb object. This function
1481 * clears all other fields of the iocb object when it is freed.
1482 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001483static void
James Smart2e0fef82007-06-17 19:56:36 -05001484__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1485{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001486 lockdep_assert_held(&phba->hbalock);
1487
James Smart3772a992009-05-22 14:50:54 -04001488 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001489 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001490}
1491
James Smarte59058c2008-08-24 21:49:00 -04001492/**
James Smart3621a712009-04-06 18:47:14 -04001493 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001494 * @phba: Pointer to HBA context object.
1495 * @iocbq: Pointer to driver iocb object.
1496 *
1497 * This function is called with no lock held to release the iocb to
1498 * iocb pool.
1499 **/
James Smart2e0fef82007-06-17 19:56:36 -05001500void
1501lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1502{
1503 unsigned long iflags;
1504
1505 /*
1506 * Clean all volatile data fields, preserve iotag and node struct.
1507 */
1508 spin_lock_irqsave(&phba->hbalock, iflags);
1509 __lpfc_sli_release_iocbq(phba, iocbq);
1510 spin_unlock_irqrestore(&phba->hbalock, iflags);
1511}
1512
James Smarte59058c2008-08-24 21:49:00 -04001513/**
James Smarta257bf92009-04-06 18:48:10 -04001514 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1515 * @phba: Pointer to HBA context object.
1516 * @iocblist: List of IOCBs.
1517 * @ulpstatus: ULP status in IOCB command field.
1518 * @ulpWord4: ULP word-4 in IOCB command field.
1519 *
1520 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1521 * on the list by invoking the complete callback function associated with the
1522 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1523 * fields.
1524 **/
1525void
1526lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1527 uint32_t ulpstatus, uint32_t ulpWord4)
1528{
1529 struct lpfc_iocbq *piocb;
1530
1531 while (!list_empty(iocblist)) {
1532 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smart9ec58ec2021-01-04 10:02:35 -08001533 if (piocb->wqe_cmpl) {
James Smart84f2ddf2019-08-14 16:56:55 -07001534 if (piocb->iocb_flag & LPFC_IO_NVME)
James Smart9ec58ec2021-01-04 10:02:35 -08001535 lpfc_nvme_cancel_iocb(phba, piocb,
1536 ulpstatus, ulpWord4);
James Smart84f2ddf2019-08-14 16:56:55 -07001537 else
1538 lpfc_sli_release_iocbq(phba, piocb);
James Smart9ec58ec2021-01-04 10:02:35 -08001539
1540 } else if (piocb->iocb_cmpl) {
James Smarta257bf92009-04-06 18:48:10 -04001541 piocb->iocb.ulpStatus = ulpstatus;
1542 piocb->iocb.un.ulpWord[4] = ulpWord4;
1543 (piocb->iocb_cmpl) (phba, piocb, piocb);
James Smart9ec58ec2021-01-04 10:02:35 -08001544 } else {
1545 lpfc_sli_release_iocbq(phba, piocb);
James Smarta257bf92009-04-06 18:48:10 -04001546 }
1547 }
1548 return;
1549}
1550
1551/**
James Smart3621a712009-04-06 18:47:14 -04001552 * lpfc_sli_iocb_cmd_type - Get the iocb type
1553 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001554 *
1555 * This function is called by ring event handler function to get the iocb type.
1556 * This function translates the iocb command to an iocb command type used to
1557 * decide the final disposition of each completed IOCB.
1558 * The function returns
1559 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1560 * LPFC_SOL_IOCB if it is a solicited iocb completion
1561 * LPFC_ABORT_IOCB if it is an abort iocb
1562 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1563 *
1564 * The caller is not required to hold any lock.
1565 **/
dea31012005-04-17 16:05:31 -05001566static lpfc_iocb_type
1567lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1568{
1569 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1570
1571 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1572 return 0;
1573
1574 switch (iocb_cmnd) {
1575 case CMD_XMIT_SEQUENCE_CR:
1576 case CMD_XMIT_SEQUENCE_CX:
1577 case CMD_XMIT_BCAST_CN:
1578 case CMD_XMIT_BCAST_CX:
1579 case CMD_ELS_REQUEST_CR:
1580 case CMD_ELS_REQUEST_CX:
1581 case CMD_CREATE_XRI_CR:
1582 case CMD_CREATE_XRI_CX:
1583 case CMD_GET_RPI_CN:
1584 case CMD_XMIT_ELS_RSP_CX:
1585 case CMD_GET_RPI_CR:
1586 case CMD_FCP_IWRITE_CR:
1587 case CMD_FCP_IWRITE_CX:
1588 case CMD_FCP_IREAD_CR:
1589 case CMD_FCP_IREAD_CX:
1590 case CMD_FCP_ICMND_CR:
1591 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001592 case CMD_FCP_TSEND_CX:
1593 case CMD_FCP_TRSP_CX:
1594 case CMD_FCP_TRECEIVE_CX:
1595 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001596 case CMD_ADAPTER_MSG:
1597 case CMD_ADAPTER_DUMP:
1598 case CMD_XMIT_SEQUENCE64_CR:
1599 case CMD_XMIT_SEQUENCE64_CX:
1600 case CMD_XMIT_BCAST64_CN:
1601 case CMD_XMIT_BCAST64_CX:
1602 case CMD_ELS_REQUEST64_CR:
1603 case CMD_ELS_REQUEST64_CX:
1604 case CMD_FCP_IWRITE64_CR:
1605 case CMD_FCP_IWRITE64_CX:
1606 case CMD_FCP_IREAD64_CR:
1607 case CMD_FCP_IREAD64_CX:
1608 case CMD_FCP_ICMND64_CR:
1609 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001610 case CMD_FCP_TSEND64_CX:
1611 case CMD_FCP_TRSP64_CX:
1612 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001613 case CMD_GEN_REQUEST64_CR:
1614 case CMD_GEN_REQUEST64_CX:
1615 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001616 case DSSCMD_IWRITE64_CR:
1617 case DSSCMD_IWRITE64_CX:
1618 case DSSCMD_IREAD64_CR:
1619 case DSSCMD_IREAD64_CX:
Dick Kennedyc93764a2020-06-30 14:49:49 -07001620 case CMD_SEND_FRAME:
dea31012005-04-17 16:05:31 -05001621 type = LPFC_SOL_IOCB;
1622 break;
1623 case CMD_ABORT_XRI_CN:
1624 case CMD_ABORT_XRI_CX:
1625 case CMD_CLOSE_XRI_CN:
1626 case CMD_CLOSE_XRI_CX:
1627 case CMD_XRI_ABORTED_CX:
1628 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001629 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001630 type = LPFC_ABORT_IOCB;
1631 break;
1632 case CMD_RCV_SEQUENCE_CX:
1633 case CMD_RCV_ELS_REQ_CX:
1634 case CMD_RCV_SEQUENCE64_CX:
1635 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001636 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001637 case CMD_IOCB_RCV_SEQ64_CX:
1638 case CMD_IOCB_RCV_ELS64_CX:
1639 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001640 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001641 type = LPFC_UNSOL_IOCB;
1642 break;
James Smart3163f722008-02-08 18:50:25 -05001643 case CMD_IOCB_XMIT_MSEQ64_CR:
1644 case CMD_IOCB_XMIT_MSEQ64_CX:
1645 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1646 case CMD_IOCB_RCV_ELS_LIST64_CX:
1647 case CMD_IOCB_CLOSE_EXTENDED_CN:
1648 case CMD_IOCB_ABORT_EXTENDED_CN:
1649 case CMD_IOCB_RET_HBQE64_CN:
1650 case CMD_IOCB_FCP_IBIDIR64_CR:
1651 case CMD_IOCB_FCP_IBIDIR64_CX:
1652 case CMD_IOCB_FCP_ITASKMGT64_CX:
1653 case CMD_IOCB_LOGENTRY_CN:
1654 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1655 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001656 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001657 type = LPFC_UNKNOWN_IOCB;
1658 break;
dea31012005-04-17 16:05:31 -05001659 default:
1660 type = LPFC_UNKNOWN_IOCB;
1661 break;
1662 }
1663
1664 return type;
1665}
1666
James Smarte59058c2008-08-24 21:49:00 -04001667/**
James Smart3621a712009-04-06 18:47:14 -04001668 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001669 * @phba: Pointer to HBA context object.
1670 *
1671 * This function is called from SLI initialization code
1672 * to configure every ring of the HBA's SLI interface. The
1673 * caller is not required to hold any lock. This function issues
1674 * a config_ring mailbox command for each ring.
1675 * This function returns zero if successful else returns a negative
1676 * error code.
1677 **/
dea31012005-04-17 16:05:31 -05001678static int
James Smarted957682007-06-17 19:56:37 -05001679lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001680{
1681 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001682 LPFC_MBOXQ_t *pmb;
1683 MAILBOX_t *pmbox;
1684 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001685
James Smarted957682007-06-17 19:56:37 -05001686 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1687 if (!pmb)
1688 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001689 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001690 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001691 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001692 lpfc_config_ring(phba, i, pmb);
1693 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1694 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001695 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001696 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001697 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1698 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001699 rc, pmbox->mbxCommand,
1700 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001701 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001702 ret = -ENXIO;
1703 break;
dea31012005-04-17 16:05:31 -05001704 }
1705 }
James Smarted957682007-06-17 19:56:37 -05001706 mempool_free(pmb, phba->mbox_mem_pool);
1707 return ret;
dea31012005-04-17 16:05:31 -05001708}
1709
James Smarte59058c2008-08-24 21:49:00 -04001710/**
James Smart3621a712009-04-06 18:47:14 -04001711 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001712 * @phba: Pointer to HBA context object.
1713 * @pring: Pointer to driver SLI ring object.
1714 * @piocb: Pointer to the driver iocb object.
1715 *
James Smarte2a8be52019-05-06 17:26:47 -07001716 * The driver calls this function with the hbalock held for SLI3 ports or
1717 * the ring lock held for SLI4 ports. The function adds the
James Smarte59058c2008-08-24 21:49:00 -04001718 * new iocb to txcmplq of the given ring. This function always returns
1719 * 0. If this function is called for ELS ring, this function checks if
1720 * there is a vport associated with the ELS command. This function also
1721 * starts els_tmofunc timer if this is an ELS command.
1722 **/
dea31012005-04-17 16:05:31 -05001723static int
James Smart2e0fef82007-06-17 19:56:36 -05001724lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1725 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001726{
James Smarte2a8be52019-05-06 17:26:47 -07001727 if (phba->sli_rev == LPFC_SLI_REV4)
1728 lockdep_assert_held(&pring->ring_lock);
1729 else
1730 lockdep_assert_held(&phba->hbalock);
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001731
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001732 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001733
dea31012005-04-17 16:05:31 -05001734 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001735 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08001736 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001737
James Smart92d7f7b2007-06-17 19:56:38 -05001738 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1739 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001740 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1741 BUG_ON(!piocb->vport);
1742 if (!(piocb->vport->load_flag & FC_UNLOADING))
1743 mod_timer(&piocb->vport->els_tmofunc,
1744 jiffies +
1745 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1746 }
dea31012005-04-17 16:05:31 -05001747
James Smart2e0fef82007-06-17 19:56:36 -05001748 return 0;
dea31012005-04-17 16:05:31 -05001749}
1750
James Smarte59058c2008-08-24 21:49:00 -04001751/**
James Smart3621a712009-04-06 18:47:14 -04001752 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001753 * @phba: Pointer to HBA context object.
1754 * @pring: Pointer to driver SLI ring object.
1755 *
1756 * This function is called with hbalock held to get next
1757 * iocb in txq of the given ring. If there is any iocb in
1758 * the txq, the function returns first iocb in the list after
1759 * removing the iocb from the list, else it returns NULL.
1760 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001761struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001762lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001763{
dea31012005-04-17 16:05:31 -05001764 struct lpfc_iocbq *cmd_iocb;
1765
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001766 lockdep_assert_held(&phba->hbalock);
1767
James Smart858c9f62007-06-17 19:56:39 -05001768 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001769 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001770}
1771
James Smarte59058c2008-08-24 21:49:00 -04001772/**
James Smartdaebf932021-08-16 09:28:53 -07001773 * lpfc_cmf_sync_cmpl - Process a CMF_SYNC_WQE cmpl
1774 * @phba: Pointer to HBA context object.
1775 * @cmdiocb: Pointer to driver command iocb object.
1776 * @cmf_cmpl: Pointer to completed WCQE.
1777 *
1778 * This routine will inform the driver of any BW adjustments we need
1779 * to make. These changes will be picked up during the next CMF
1780 * timer interrupt. In addition, any BW changes will be logged
1781 * with LOG_CGN_MGMT.
1782 **/
1783static void
1784lpfc_cmf_sync_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1785 struct lpfc_wcqe_complete *cmf_cmpl)
1786{
1787 union lpfc_wqe128 *wqe;
1788 uint32_t status, info;
James Smart02243832021-08-16 09:28:54 -07001789 uint64_t bw, bwdif, slop;
1790 uint64_t pcent, bwpcent;
James Smartdaebf932021-08-16 09:28:53 -07001791 int asig, afpin, sigcnt, fpincnt;
James Smart02243832021-08-16 09:28:54 -07001792 int wsigmax, wfpinmax, cg, tdp;
1793 char *s;
James Smartdaebf932021-08-16 09:28:53 -07001794
1795 /* First check for error */
1796 status = bf_get(lpfc_wcqe_c_status, cmf_cmpl);
1797 if (status) {
1798 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1799 "6211 CMF_SYNC_WQE Error "
1800 "req_tag x%x status x%x hwstatus x%x "
1801 "tdatap x%x parm x%x\n",
1802 bf_get(lpfc_wcqe_c_request_tag, cmf_cmpl),
1803 bf_get(lpfc_wcqe_c_status, cmf_cmpl),
1804 bf_get(lpfc_wcqe_c_hw_status, cmf_cmpl),
1805 cmf_cmpl->total_data_placed,
1806 cmf_cmpl->parameter);
1807 goto out;
1808 }
1809
1810 /* Gather congestion information on a successful cmpl */
1811 info = cmf_cmpl->parameter;
James Smart02243832021-08-16 09:28:54 -07001812 phba->cmf_active_info = info;
1813
1814 /* See if firmware info count is valid or has changed */
1815 if (info > LPFC_MAX_CMF_INFO || phba->cmf_info_per_interval == info)
1816 info = 0;
1817 else
1818 phba->cmf_info_per_interval = info;
1819
James Smartdaebf932021-08-16 09:28:53 -07001820 tdp = bf_get(lpfc_wcqe_c_cmf_bw, cmf_cmpl);
1821 cg = bf_get(lpfc_wcqe_c_cmf_cg, cmf_cmpl);
1822
1823 /* Get BW requirement from firmware */
1824 bw = (uint64_t)tdp * LPFC_CMF_BLK_SIZE;
1825 if (!bw) {
1826 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1827 "6212 CMF_SYNC_WQE x%x: NULL bw\n",
1828 bf_get(lpfc_wcqe_c_request_tag, cmf_cmpl));
1829 goto out;
1830 }
1831
1832 /* Gather information needed for logging if a BW change is required */
1833 wqe = &cmdiocb->wqe;
1834 asig = bf_get(cmf_sync_asig, &wqe->cmf_sync);
1835 afpin = bf_get(cmf_sync_afpin, &wqe->cmf_sync);
1836 fpincnt = bf_get(cmf_sync_wfpincnt, &wqe->cmf_sync);
1837 sigcnt = bf_get(cmf_sync_wsigcnt, &wqe->cmf_sync);
James Smart02243832021-08-16 09:28:54 -07001838 if (phba->cmf_max_bytes_per_interval != bw ||
1839 (asig || afpin || sigcnt || fpincnt)) {
1840 /* Are we increasing or decreasing BW */
1841 if (phba->cmf_max_bytes_per_interval < bw) {
1842 bwdif = bw - phba->cmf_max_bytes_per_interval;
1843 s = "Increase";
1844 } else {
1845 bwdif = phba->cmf_max_bytes_per_interval - bw;
1846 s = "Decrease";
1847 }
James Smartdaebf932021-08-16 09:28:53 -07001848
James Smart02243832021-08-16 09:28:54 -07001849 /* What is the change percentage */
1850 slop = div_u64(phba->cmf_link_byte_count, 200); /*For rounding*/
1851 pcent = div64_u64(bwdif * 100 + slop,
1852 phba->cmf_link_byte_count);
1853 bwpcent = div64_u64(bw * 100 + slop,
1854 phba->cmf_link_byte_count);
1855 if (asig) {
1856 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1857 "6237 BW Threshold %lld%% (%lld): "
1858 "%lld%% %s: Signal Alarm: cg:%d "
1859 "Info:%u\n",
1860 bwpcent, bw, pcent, s, cg,
1861 phba->cmf_active_info);
1862 } else if (afpin) {
1863 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1864 "6238 BW Threshold %lld%% (%lld): "
1865 "%lld%% %s: FPIN Alarm: cg:%d "
1866 "Info:%u\n",
1867 bwpcent, bw, pcent, s, cg,
1868 phba->cmf_active_info);
1869 } else if (sigcnt) {
1870 wsigmax = bf_get(cmf_sync_wsigmax, &wqe->cmf_sync);
1871 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1872 "6239 BW Threshold %lld%% (%lld): "
1873 "%lld%% %s: Signal Warning: "
1874 "Cnt %d Max %d: cg:%d Info:%u\n",
1875 bwpcent, bw, pcent, s, sigcnt,
1876 wsigmax, cg, phba->cmf_active_info);
1877 } else if (fpincnt) {
1878 wfpinmax = bf_get(cmf_sync_wfpinmax, &wqe->cmf_sync);
1879 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1880 "6240 BW Threshold %lld%% (%lld): "
1881 "%lld%% %s: FPIN Warning: "
1882 "Cnt %d Max %d: cg:%d Info:%u\n",
1883 bwpcent, bw, pcent, s, fpincnt,
1884 wfpinmax, cg, phba->cmf_active_info);
1885 } else {
1886 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1887 "6241 BW Threshold %lld%% (%lld): "
1888 "CMF %lld%% %s: cg:%d Info:%u\n",
1889 bwpcent, bw, pcent, s, cg,
1890 phba->cmf_active_info);
1891 }
1892 } else if (info) {
1893 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1894 "6246 Info Threshold %u\n", info);
1895 }
1896
1897 /* Save BW change to be picked up during next timer interrupt */
1898 phba->cmf_last_sync_bw = bw;
James Smartdaebf932021-08-16 09:28:53 -07001899out:
1900 lpfc_sli_release_iocbq(phba, cmdiocb);
1901}
1902
1903/**
1904 * lpfc_issue_cmf_sync_wqe - Issue a CMF_SYNC_WQE
1905 * @phba: Pointer to HBA context object.
1906 * @ms: ms to set in WQE interval, 0 means use init op
1907 * @total: Total rcv bytes for this interval
1908 *
1909 * This routine is called every CMF timer interrupt. Its purpose is
1910 * to issue a CMF_SYNC_WQE to the firmware to inform it of any events
1911 * that may indicate we have congestion (FPINs or Signals). Upon
1912 * completion, the firmware will indicate any BW restrictions the
1913 * driver may need to take.
1914 **/
1915int
1916lpfc_issue_cmf_sync_wqe(struct lpfc_hba *phba, u32 ms, u64 total)
1917{
1918 union lpfc_wqe128 *wqe;
1919 struct lpfc_iocbq *sync_buf;
1920 unsigned long iflags;
1921 u32 ret_val;
1922 u32 atot, wtot, max;
1923
1924 /* First address any alarm / warning activity */
1925 atot = atomic_xchg(&phba->cgn_sync_alarm_cnt, 0);
1926 wtot = atomic_xchg(&phba->cgn_sync_warn_cnt, 0);
1927
1928 /* ONLY Managed mode will send the CMF_SYNC_WQE to the HBA */
1929 if (phba->cmf_active_mode != LPFC_CFG_MANAGED ||
1930 phba->link_state == LPFC_LINK_DOWN)
1931 return 0;
1932
1933 spin_lock_irqsave(&phba->hbalock, iflags);
1934 sync_buf = __lpfc_sli_get_iocbq(phba);
1935 if (!sync_buf) {
1936 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT,
1937 "6213 No available WQEs for CMF_SYNC_WQE\n");
1938 ret_val = ENOMEM;
1939 goto out_unlock;
1940 }
1941
1942 wqe = &sync_buf->wqe;
1943
1944 /* WQEs are reused. Clear stale data and set key fields to zero */
1945 memset(wqe, 0, sizeof(*wqe));
1946
1947 /* If this is the very first CMF_SYNC_WQE, issue an init operation */
1948 if (!ms) {
1949 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
1950 "6441 CMF Init %d - CMF_SYNC_WQE\n",
1951 phba->fc_eventTag);
1952 bf_set(cmf_sync_op, &wqe->cmf_sync, 1); /* 1=init */
1953 bf_set(cmf_sync_interval, &wqe->cmf_sync, LPFC_CMF_INTERVAL);
1954 goto initpath;
1955 }
1956
1957 bf_set(cmf_sync_op, &wqe->cmf_sync, 0); /* 0=recalc */
1958 bf_set(cmf_sync_interval, &wqe->cmf_sync, ms);
1959
1960 /* Check for alarms / warnings */
1961 if (atot) {
1962 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1963 /* We hit an Signal alarm condition */
1964 bf_set(cmf_sync_asig, &wqe->cmf_sync, 1);
1965 } else {
1966 /* We hit a FPIN alarm condition */
1967 bf_set(cmf_sync_afpin, &wqe->cmf_sync, 1);
1968 }
1969 } else if (wtot) {
1970 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY ||
1971 phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
1972 /* We hit an Signal warning condition */
1973 max = LPFC_SEC_TO_MSEC / lpfc_fabric_cgn_frequency *
1974 lpfc_acqe_cgn_frequency;
1975 bf_set(cmf_sync_wsigmax, &wqe->cmf_sync, max);
1976 bf_set(cmf_sync_wsigcnt, &wqe->cmf_sync, wtot);
1977 } else {
1978 /* We hit a FPIN warning condition */
1979 bf_set(cmf_sync_wfpinmax, &wqe->cmf_sync, 1);
1980 bf_set(cmf_sync_wfpincnt, &wqe->cmf_sync, 1);
1981 }
1982 }
1983
1984 /* Update total read blocks during previous timer interval */
1985 wqe->cmf_sync.read_bytes = (u32)(total / LPFC_CMF_BLK_SIZE);
1986
1987initpath:
1988 bf_set(cmf_sync_ver, &wqe->cmf_sync, LPFC_CMF_SYNC_VER);
1989 wqe->cmf_sync.event_tag = phba->fc_eventTag;
1990 bf_set(cmf_sync_cmnd, &wqe->cmf_sync, CMD_CMF_SYNC_WQE);
1991
1992 /* Setup reqtag to match the wqe completion. */
1993 bf_set(cmf_sync_reqtag, &wqe->cmf_sync, sync_buf->iotag);
1994
1995 bf_set(cmf_sync_qosd, &wqe->cmf_sync, 1);
1996
1997 bf_set(cmf_sync_cmd_type, &wqe->cmf_sync, CMF_SYNC_COMMAND);
1998 bf_set(cmf_sync_wqec, &wqe->cmf_sync, 1);
1999 bf_set(cmf_sync_cqid, &wqe->cmf_sync, LPFC_WQE_CQ_ID_DEFAULT);
2000
2001 sync_buf->vport = phba->pport;
2002 sync_buf->wqe_cmpl = lpfc_cmf_sync_cmpl;
2003 sync_buf->iocb_cmpl = NULL;
2004 sync_buf->context1 = NULL;
2005 sync_buf->context2 = NULL;
2006 sync_buf->context3 = NULL;
2007 sync_buf->sli4_xritag = NO_XRI;
2008
2009 sync_buf->iocb_flag |= LPFC_IO_CMF;
2010 ret_val = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], sync_buf);
2011 if (ret_val)
2012 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
2013 "6214 Cannot issue CMF_SYNC_WQE: x%x\n",
2014 ret_val);
2015out_unlock:
2016 spin_unlock_irqrestore(&phba->hbalock, iflags);
2017 return ret_val;
2018}
2019
2020/**
James Smart3621a712009-04-06 18:47:14 -04002021 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04002022 * @phba: Pointer to HBA context object.
2023 * @pring: Pointer to driver SLI ring object.
2024 *
2025 * This function is called with hbalock held and the caller must post the
2026 * iocb without releasing the lock. If the caller releases the lock,
2027 * iocb slot returned by the function is not guaranteed to be available.
2028 * The function returns pointer to the next available iocb slot if there
2029 * is available slot in the ring, else it returns NULL.
2030 * If the get index of the ring is ahead of the put index, the function
2031 * will post an error attention event to the worker thread to take the
2032 * HBA to offline state.
2033 **/
dea31012005-04-17 16:05:31 -05002034static IOCB_t *
2035lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2036{
James Smart34b02dc2008-08-24 21:49:55 -04002037 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04002038 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002039
2040 lockdep_assert_held(&phba->hbalock);
2041
James Smart7e56aa22012-08-03 12:35:34 -04002042 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
2043 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
2044 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05002045
James Smart7e56aa22012-08-03 12:35:34 -04002046 if (unlikely(pring->sli.sli3.local_getidx ==
2047 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05002048
James Smart7e56aa22012-08-03 12:35:34 -04002049 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05002050
James Smart7e56aa22012-08-03 12:35:34 -04002051 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002052 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002053 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002054 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002055 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04002056 pring->sli.sli3.local_getidx,
2057 max_cmd_idx);
dea31012005-04-17 16:05:31 -05002058
James Smart2e0fef82007-06-17 19:56:36 -05002059 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002060 /*
2061 * All error attention handlers are posted to
2062 * worker thread
2063 */
2064 phba->work_ha |= HA_ERATT;
2065 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002066
James Smart5e9d9b82008-06-14 22:52:53 -04002067 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002068
2069 return NULL;
2070 }
2071
James Smart7e56aa22012-08-03 12:35:34 -04002072 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05002073 return NULL;
2074 }
2075
James Smarted957682007-06-17 19:56:37 -05002076 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05002077}
2078
James Smarte59058c2008-08-24 21:49:00 -04002079/**
James Smart3621a712009-04-06 18:47:14 -04002080 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04002081 * @phba: Pointer to HBA context object.
2082 * @iocbq: Pointer to driver iocb object.
2083 *
2084 * This function gets an iotag for the iocb. If there is no unused iotag and
2085 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
2086 * array and assigns a new iotag.
2087 * The function returns the allocated iotag if successful, else returns zero.
2088 * Zero is not a valid iotag.
2089 * The caller is not required to hold any lock.
2090 **/
James Bottomley604a3e32005-10-29 10:28:33 -05002091uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05002092lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05002093{
James Smart2e0fef82007-06-17 19:56:36 -05002094 struct lpfc_iocbq **new_arr;
2095 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05002096 size_t new_len;
2097 struct lpfc_sli *psli = &phba->sli;
2098 uint16_t iotag;
dea31012005-04-17 16:05:31 -05002099
James Smart2e0fef82007-06-17 19:56:36 -05002100 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05002101 iotag = psli->last_iotag;
2102 if(++iotag < psli->iocbq_lookup_len) {
2103 psli->last_iotag = iotag;
2104 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05002105 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05002106 iocbq->iotag = iotag;
2107 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05002108 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05002109 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
2110 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05002111 spin_unlock_irq(&phba->hbalock);
Kees Cook6396bb22018-06-12 14:03:40 -07002112 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05002113 GFP_KERNEL);
2114 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05002115 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05002116 old_arr = psli->iocbq_lookup;
2117 if (new_len <= psli->iocbq_lookup_len) {
2118 /* highly unprobable case */
2119 kfree(new_arr);
2120 iotag = psli->last_iotag;
2121 if(++iotag < psli->iocbq_lookup_len) {
2122 psli->last_iotag = iotag;
2123 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05002124 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05002125 iocbq->iotag = iotag;
2126 return iotag;
2127 }
James Smart2e0fef82007-06-17 19:56:36 -05002128 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05002129 return 0;
2130 }
2131 if (psli->iocbq_lookup)
2132 memcpy(new_arr, old_arr,
2133 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04002134 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05002135 psli->iocbq_lookup = new_arr;
2136 psli->iocbq_lookup_len = new_len;
2137 psli->last_iotag = iotag;
2138 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05002139 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05002140 iocbq->iotag = iotag;
2141 kfree(old_arr);
2142 return iotag;
2143 }
James Smart8f6d98d2006-08-01 07:34:00 -04002144 } else
James Smart2e0fef82007-06-17 19:56:36 -05002145 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002146
James Smartbc739052010-08-04 16:11:18 -04002147 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002148 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
2149 psli->last_iotag);
dea31012005-04-17 16:05:31 -05002150
James Bottomley604a3e32005-10-29 10:28:33 -05002151 return 0;
dea31012005-04-17 16:05:31 -05002152}
2153
James Smarte59058c2008-08-24 21:49:00 -04002154/**
James Smart3621a712009-04-06 18:47:14 -04002155 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04002156 * @phba: Pointer to HBA context object.
2157 * @pring: Pointer to driver SLI ring object.
2158 * @iocb: Pointer to iocb slot in the ring.
2159 * @nextiocb: Pointer to driver iocb object which need to be
2160 * posted to firmware.
2161 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07002162 * This function is called to post a new iocb to the firmware. This
2163 * function copies the new iocb to ring iocb slot and updates the
2164 * ring pointers. It adds the new iocb to txcmplq if there is
James Smarte59058c2008-08-24 21:49:00 -04002165 * a completion call back for this iocb else the function will free the
Dick Kennedy88acb4d2020-05-01 14:43:07 -07002166 * iocb object. The hbalock is asserted held in the code path calling
2167 * this routine.
James Smarte59058c2008-08-24 21:49:00 -04002168 **/
dea31012005-04-17 16:05:31 -05002169static void
2170lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2171 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
2172{
2173 /*
James Bottomley604a3e32005-10-29 10:28:33 -05002174 * Set up an iotag
dea31012005-04-17 16:05:31 -05002175 */
James Bottomley604a3e32005-10-29 10:28:33 -05002176 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05002177
James Smarte2a0a9d2008-12-04 22:40:02 -05002178
James Smarta58cbd52007-08-02 11:09:43 -04002179 if (pring->ringno == LPFC_ELS_RING) {
2180 lpfc_debugfs_slow_ring_trc(phba,
2181 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2182 *(((uint32_t *) &nextiocb->iocb) + 4),
2183 *(((uint32_t *) &nextiocb->iocb) + 6),
2184 *(((uint32_t *) &nextiocb->iocb) + 7));
2185 }
2186
dea31012005-04-17 16:05:31 -05002187 /*
2188 * Issue iocb command to adapter
2189 */
James Smart92d7f7b2007-06-17 19:56:38 -05002190 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05002191 wmb();
2192 pring->stats.iocb_cmd++;
2193
2194 /*
2195 * If there is no completion routine to call, we can release the
2196 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
2197 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
2198 */
2199 if (nextiocb->iocb_cmpl)
2200 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05002201 else
James Smart2e0fef82007-06-17 19:56:36 -05002202 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05002203
2204 /*
2205 * Let the HBA know what IOCB slot will be the next one the
2206 * driver will put a command into.
2207 */
James Smart7e56aa22012-08-03 12:35:34 -04002208 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
2209 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05002210}
2211
James Smarte59058c2008-08-24 21:49:00 -04002212/**
James Smart3621a712009-04-06 18:47:14 -04002213 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04002214 * @phba: Pointer to HBA context object.
2215 * @pring: Pointer to driver SLI ring object.
2216 *
2217 * The caller is not required to hold any lock for calling this function.
2218 * This function updates the chip attention bits for the ring to inform firmware
2219 * that there are pending work to be done for this ring and requests an
2220 * interrupt when there is space available in the ring. This function is
2221 * called when the driver is unable to post more iocbs to the ring due
2222 * to unavailability of space in the ring.
2223 **/
dea31012005-04-17 16:05:31 -05002224static void
James Smart2e0fef82007-06-17 19:56:36 -05002225lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05002226{
2227 int ringno = pring->ringno;
2228
2229 pring->flag |= LPFC_CALL_RING_AVAILABLE;
2230
2231 wmb();
2232
2233 /*
2234 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
2235 * The HBA will tell us when an IOCB entry is available.
2236 */
2237 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
2238 readl(phba->CAregaddr); /* flush */
2239
2240 pring->stats.iocb_cmd_full++;
2241}
2242
James Smarte59058c2008-08-24 21:49:00 -04002243/**
James Smart3621a712009-04-06 18:47:14 -04002244 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04002245 * @phba: Pointer to HBA context object.
2246 * @pring: Pointer to driver SLI ring object.
2247 *
2248 * This function updates the chip attention register bit for the
2249 * given ring to inform HBA that there is more work to be done
2250 * in this ring. The caller is not required to hold any lock.
2251 **/
dea31012005-04-17 16:05:31 -05002252static void
James Smart2e0fef82007-06-17 19:56:36 -05002253lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05002254{
2255 int ringno = pring->ringno;
2256
2257 /*
2258 * Tell the HBA that there is work to do in this ring.
2259 */
James Smart34b02dc2008-08-24 21:49:55 -04002260 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
2261 wmb();
2262 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
2263 readl(phba->CAregaddr); /* flush */
2264 }
dea31012005-04-17 16:05:31 -05002265}
2266
James Smarte59058c2008-08-24 21:49:00 -04002267/**
James Smart3621a712009-04-06 18:47:14 -04002268 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04002269 * @phba: Pointer to HBA context object.
2270 * @pring: Pointer to driver SLI ring object.
2271 *
2272 * This function is called with hbalock held to post pending iocbs
2273 * in the txq to the firmware. This function is called when driver
2274 * detects space available in the ring.
2275 **/
dea31012005-04-17 16:05:31 -05002276static void
James Smart2e0fef82007-06-17 19:56:36 -05002277lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05002278{
2279 IOCB_t *iocb;
2280 struct lpfc_iocbq *nextiocb;
2281
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002282 lockdep_assert_held(&phba->hbalock);
2283
dea31012005-04-17 16:05:31 -05002284 /*
2285 * Check to see if:
2286 * (a) there is anything on the txq to send
2287 * (b) link is up
2288 * (c) link attention events can be processed (fcp ring only)
2289 * (d) IOCB processing is not blocked by the outstanding mbox command.
2290 */
James Smart0e9bb8d2013-03-01 16:35:12 -05002291
2292 if (lpfc_is_link_up(phba) &&
2293 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08002294 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04002295 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05002296
2297 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2298 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
2299 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2300
2301 if (iocb)
2302 lpfc_sli_update_ring(phba, pring);
2303 else
2304 lpfc_sli_update_full_ring(phba, pring);
2305 }
2306
2307 return;
2308}
2309
James Smarte59058c2008-08-24 21:49:00 -04002310/**
James Smart3621a712009-04-06 18:47:14 -04002311 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002312 * @phba: Pointer to HBA context object.
2313 * @hbqno: HBQ number.
2314 *
2315 * This function is called with hbalock held to get the next
2316 * available slot for the given HBQ. If there is free slot
2317 * available for the HBQ it will return pointer to the next available
2318 * HBQ entry else it will return NULL.
2319 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002320static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05002321lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
2322{
2323 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2324
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002325 lockdep_assert_held(&phba->hbalock);
2326
James Smarted957682007-06-17 19:56:37 -05002327 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
2328 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
2329 hbqp->next_hbqPutIdx = 0;
2330
2331 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002332 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05002333 uint32_t getidx = le32_to_cpu(raw_index);
2334
2335 hbqp->local_hbqGetIdx = getidx;
2336
2337 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002338 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002339 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05002340 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04002341 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05002342 hbqp->entry_count);
2343
2344 phba->link_state = LPFC_HBA_ERROR;
2345 return NULL;
2346 }
2347
2348 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
2349 return NULL;
2350 }
2351
James Smart51ef4c22007-08-02 11:10:31 -04002352 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
2353 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05002354}
2355
James Smarte59058c2008-08-24 21:49:00 -04002356/**
James Smart3621a712009-04-06 18:47:14 -04002357 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04002358 * @phba: Pointer to HBA context object.
2359 *
2360 * This function is called with no lock held to free all the
2361 * hbq buffers while uninitializing the SLI interface. It also
2362 * frees the HBQ buffers returned by the firmware but not yet
2363 * processed by the upper layers.
2364 **/
James Smarted957682007-06-17 19:56:37 -05002365void
2366lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
2367{
James Smart92d7f7b2007-06-17 19:56:38 -05002368 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2369 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05002370 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04002371 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05002372
James Smart51ef4c22007-08-02 11:10:31 -04002373 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05002374 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05002375 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04002376 for (i = 0; i < hbq_count; ++i) {
2377 list_for_each_entry_safe(dmabuf, next_dmabuf,
2378 &phba->hbqs[i].hbq_buffer_list, list) {
2379 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
2380 list_del(&hbq_buf->dbuf.list);
2381 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
2382 }
James Smarta8adb832007-10-27 13:37:53 -04002383 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05002384 }
James Smart3163f722008-02-08 18:50:25 -05002385
2386 /* Mark the HBQs not in use */
2387 phba->hbq_in_use = 0;
2388 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05002389}
2390
James Smarte59058c2008-08-24 21:49:00 -04002391/**
James Smart3621a712009-04-06 18:47:14 -04002392 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002393 * @phba: Pointer to HBA context object.
2394 * @hbqno: HBQ number.
2395 * @hbq_buf: Pointer to HBQ buffer.
2396 *
2397 * This function is called with the hbalock held to post a
2398 * hbq buffer to the firmware. If the function finds an empty
2399 * slot in the HBQ, it will post the buffer. The function will return
2400 * pointer to the hbq entry if it successfully post the buffer
2401 * else it will return NULL.
2402 **/
James Smart3772a992009-05-22 14:50:54 -04002403static int
James Smarted957682007-06-17 19:56:37 -05002404lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05002405 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05002406{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002407 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002408 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2409}
2410
2411/**
2412 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2413 * @phba: Pointer to HBA context object.
2414 * @hbqno: HBQ number.
2415 * @hbq_buf: Pointer to HBQ buffer.
2416 *
2417 * This function is called with the hbalock held to post a hbq buffer to the
2418 * firmware. If the function finds an empty slot in the HBQ, it will post the
2419 * buffer and place it on the hbq_buffer_list. The function will return zero if
2420 * it successfully post the buffer else it will return an error.
2421 **/
2422static int
2423lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2424 struct hbq_dmabuf *hbq_buf)
2425{
James Smarted957682007-06-17 19:56:37 -05002426 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002427 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002428
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002429 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002430 /* Get next HBQ entry slot to use */
2431 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2432 if (hbqe) {
2433 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2434
James Smart92d7f7b2007-06-17 19:56:38 -05002435 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2436 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002437 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002438 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002439 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2440 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2441 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002442 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2443 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002444 /* flush */
James Smarted957682007-06-17 19:56:37 -05002445 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002446 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002447 return 0;
2448 } else
2449 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002450}
2451
James Smart4f774512009-05-22 14:52:35 -04002452/**
2453 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2454 * @phba: Pointer to HBA context object.
2455 * @hbqno: HBQ number.
2456 * @hbq_buf: Pointer to HBQ buffer.
2457 *
2458 * This function is called with the hbalock held to post an RQE to the SLI4
2459 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2460 * the hbq_buffer_list and return zero, otherwise it will return an error.
2461 **/
2462static int
2463lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2464 struct hbq_dmabuf *hbq_buf)
2465{
2466 int rc;
2467 struct lpfc_rqe hrqe;
2468 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002469 struct lpfc_queue *hrq;
2470 struct lpfc_queue *drq;
2471
2472 if (hbqno != LPFC_ELS_HBQ)
2473 return 1;
2474 hrq = phba->sli4_hba.hdr_rq;
2475 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002476
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002477 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002478 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2479 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2480 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2481 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002482 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002483 if (rc < 0)
2484 return rc;
James Smart895427b2017-02-12 13:52:30 -08002485 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002486 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2487 return 0;
2488}
2489
James Smarte59058c2008-08-24 21:49:00 -04002490/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002491static struct lpfc_hbq_init lpfc_els_hbq = {
2492 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002493 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002494 .mask_count = 0,
2495 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002496 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002497 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002498 .init_count = 40,
2499 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002500};
James Smarted957682007-06-17 19:56:37 -05002501
James Smarte59058c2008-08-24 21:49:00 -04002502/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002503struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002504 &lpfc_els_hbq,
2505};
2506
James Smarte59058c2008-08-24 21:49:00 -04002507/**
James Smart3621a712009-04-06 18:47:14 -04002508 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002509 * @phba: Pointer to HBA context object.
2510 * @hbqno: HBQ number.
2511 * @count: Number of HBQ buffers to be posted.
2512 *
James Smartd7c255b2008-08-24 21:50:00 -04002513 * This function is called with no lock held to post more hbq buffers to the
2514 * given HBQ. The function returns the number of HBQ buffers successfully
2515 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002516 **/
James Smart311464e2007-08-02 11:10:37 -04002517static int
James Smart92d7f7b2007-06-17 19:56:38 -05002518lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2519{
James Smartd7c255b2008-08-24 21:50:00 -04002520 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002521 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002522 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002523 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002524 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002525 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002526
James Smartd7c255b2008-08-24 21:50:00 -04002527 if ((phba->hbqs[hbqno].buffer_count + count) >
2528 lpfc_hbq_defs[hbqno]->entry_count)
2529 count = lpfc_hbq_defs[hbqno]->entry_count -
2530 phba->hbqs[hbqno].buffer_count;
2531 if (!count)
2532 return 0;
2533 /* Allocate HBQ entries */
2534 for (i = 0; i < count; i++) {
2535 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2536 if (!hbq_buffer)
2537 break;
2538 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2539 }
James Smart3163f722008-02-08 18:50:25 -05002540 /* Check whether HBQ is still in use */
2541 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002542 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002543 goto err;
2544 while (!list_empty(&hbq_buf_list)) {
2545 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2546 dbuf.list);
2547 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2548 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002549 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002550 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002551 posted++;
2552 } else
James Smart51ef4c22007-08-02 11:10:31 -04002553 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002554 }
James Smart3163f722008-02-08 18:50:25 -05002555 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002556 return posted;
2557err:
2558 spin_unlock_irqrestore(&phba->hbalock, flags);
2559 while (!list_empty(&hbq_buf_list)) {
2560 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2561 dbuf.list);
2562 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2563 }
James Smart92d7f7b2007-06-17 19:56:38 -05002564 return 0;
James Smarted957682007-06-17 19:56:37 -05002565}
2566
James Smarte59058c2008-08-24 21:49:00 -04002567/**
James Smart3621a712009-04-06 18:47:14 -04002568 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002569 * @phba: Pointer to HBA context object.
2570 * @qno: HBQ number.
2571 *
2572 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002573 * is called with no lock held. The function returns the number of HBQ entries
2574 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002575 **/
James Smarted957682007-06-17 19:56:37 -05002576int
James Smart92d7f7b2007-06-17 19:56:38 -05002577lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002578{
James Smartdef9c7a2009-12-21 17:02:28 -05002579 if (phba->sli_rev == LPFC_SLI_REV4)
2580 return 0;
2581 else
2582 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2583 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002584}
2585
James Smarte59058c2008-08-24 21:49:00 -04002586/**
James Smart3621a712009-04-06 18:47:14 -04002587 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002588 * @phba: Pointer to HBA context object.
2589 * @qno: HBQ queue number.
2590 *
2591 * This function is called from SLI initialization code path with
2592 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002593 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002594 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002595static int
James Smart92d7f7b2007-06-17 19:56:38 -05002596lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002597{
James Smartdef9c7a2009-12-21 17:02:28 -05002598 if (phba->sli_rev == LPFC_SLI_REV4)
2599 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002600 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002601 else
2602 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2603 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002604}
2605
Lee Jones7af29d42020-07-21 17:41:31 +01002606/*
James Smart3772a992009-05-22 14:50:54 -04002607 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
James Smart3772a992009-05-22 14:50:54 -04002608 *
2609 * This function removes the first hbq buffer on an hbq list and returns a
2610 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2611 **/
2612static struct hbq_dmabuf *
2613lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2614{
2615 struct lpfc_dmabuf *d_buf;
2616
2617 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2618 if (!d_buf)
2619 return NULL;
2620 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2621}
2622
2623/**
James Smart2d7dbc42017-02-12 13:52:35 -08002624 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2625 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01002626 * @hrq: HBQ number.
James Smart2d7dbc42017-02-12 13:52:35 -08002627 *
2628 * This function removes the first RQ buffer on an RQ buffer list and returns a
2629 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2630 **/
2631static struct rqb_dmabuf *
2632lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2633{
2634 struct lpfc_dmabuf *h_buf;
2635 struct lpfc_rqb *rqbp;
2636
2637 rqbp = hrq->rqbp;
2638 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2639 struct lpfc_dmabuf, list);
2640 if (!h_buf)
2641 return NULL;
2642 rqbp->buffer_count--;
2643 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2644}
2645
2646/**
James Smart3621a712009-04-06 18:47:14 -04002647 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002648 * @phba: Pointer to HBA context object.
2649 * @tag: Tag of the hbq buffer.
2650 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002651 * This function searches for the hbq buffer associated with the given tag in
2652 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2653 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002654 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002655static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002656lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2657{
James Smart92d7f7b2007-06-17 19:56:38 -05002658 struct lpfc_dmabuf *d_buf;
2659 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002660 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002661
James Smart51ef4c22007-08-02 11:10:31 -04002662 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002663 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002664 return NULL;
2665
James Smart3772a992009-05-22 14:50:54 -04002666 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002667 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002668 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002669 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002670 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002671 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002672 }
2673 }
James Smart3772a992009-05-22 14:50:54 -04002674 spin_unlock_irq(&phba->hbalock);
Dick Kennedy372c1872020-06-30 14:50:00 -07002675 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002676 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002677 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002678 return NULL;
James Smarted957682007-06-17 19:56:37 -05002679}
2680
James Smarte59058c2008-08-24 21:49:00 -04002681/**
James Smart3621a712009-04-06 18:47:14 -04002682 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002683 * @phba: Pointer to HBA context object.
2684 * @hbq_buffer: Pointer to HBQ buffer.
2685 *
2686 * This function is called with hbalock. This function gives back
2687 * the hbq buffer to firmware. If the HBQ does not have space to
2688 * post the buffer, it will free the buffer.
2689 **/
James Smarted957682007-06-17 19:56:37 -05002690void
James Smart51ef4c22007-08-02 11:10:31 -04002691lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002692{
2693 uint32_t hbqno;
2694
James Smart51ef4c22007-08-02 11:10:31 -04002695 if (hbq_buffer) {
2696 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002697 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002698 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002699 }
2700}
2701
James Smarte59058c2008-08-24 21:49:00 -04002702/**
James Smart3621a712009-04-06 18:47:14 -04002703 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002704 * @mbxCommand: mailbox command code.
2705 *
2706 * This function is called by the mailbox event handler function to verify
2707 * that the completed mailbox command is a legitimate mailbox command. If the
2708 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2709 * and the mailbox event handler will take the HBA offline.
2710 **/
dea31012005-04-17 16:05:31 -05002711static int
2712lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2713{
2714 uint8_t ret;
2715
2716 switch (mbxCommand) {
2717 case MBX_LOAD_SM:
2718 case MBX_READ_NV:
2719 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002720 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002721 case MBX_RUN_BIU_DIAG:
2722 case MBX_INIT_LINK:
2723 case MBX_DOWN_LINK:
2724 case MBX_CONFIG_LINK:
2725 case MBX_CONFIG_RING:
2726 case MBX_RESET_RING:
2727 case MBX_READ_CONFIG:
2728 case MBX_READ_RCONFIG:
2729 case MBX_READ_SPARM:
2730 case MBX_READ_STATUS:
2731 case MBX_READ_RPI:
2732 case MBX_READ_XRI:
2733 case MBX_READ_REV:
2734 case MBX_READ_LNK_STAT:
2735 case MBX_REG_LOGIN:
2736 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002737 case MBX_CLEAR_LA:
2738 case MBX_DUMP_MEMORY:
2739 case MBX_DUMP_CONTEXT:
2740 case MBX_RUN_DIAGS:
2741 case MBX_RESTART:
2742 case MBX_UPDATE_CFG:
2743 case MBX_DOWN_LOAD:
2744 case MBX_DEL_LD_ENTRY:
2745 case MBX_RUN_PROGRAM:
2746 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002747 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002748 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002749 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002750 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002751 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002752 case MBX_LOAD_AREA:
2753 case MBX_RUN_BIU_DIAG64:
2754 case MBX_CONFIG_PORT:
2755 case MBX_READ_SPARM64:
2756 case MBX_READ_RPI64:
2757 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002758 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002759 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002760 case MBX_SET_DEBUG:
2761 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002762 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002763 case MBX_REG_VPI:
2764 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002765 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002766 case MBX_PORT_CAPABILITIES:
2767 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002768 case MBX_SLI4_CONFIG:
2769 case MBX_SLI4_REQ_FTRS:
2770 case MBX_REG_FCFI:
2771 case MBX_UNREG_FCFI:
2772 case MBX_REG_VFI:
2773 case MBX_UNREG_VFI:
2774 case MBX_INIT_VPI:
2775 case MBX_INIT_VFI:
2776 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002777 case MBX_READ_EVENT_LOG_STATUS:
2778 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002779 case MBX_SECURITY_MGMT:
2780 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002781 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002782 ret = mbxCommand;
2783 break;
2784 default:
2785 ret = MBX_SHUTDOWN;
2786 break;
2787 }
James Smart2e0fef82007-06-17 19:56:36 -05002788 return ret;
dea31012005-04-17 16:05:31 -05002789}
James Smarte59058c2008-08-24 21:49:00 -04002790
2791/**
James Smart3621a712009-04-06 18:47:14 -04002792 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002793 * @phba: Pointer to HBA context object.
2794 * @pmboxq: Pointer to mailbox command.
2795 *
2796 * This is completion handler function for mailbox commands issued from
2797 * lpfc_sli_issue_mbox_wait function. This function is called by the
2798 * mailbox event handler function with no lock held. This function
2799 * will wake up thread waiting on the wait queue pointed by context1
2800 * of the mailbox.
2801 **/
James Smart04c68492009-05-22 14:52:52 -04002802void
James Smart2e0fef82007-06-17 19:56:36 -05002803lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002804{
James Smart858c9f62007-06-17 19:56:39 -05002805 unsigned long drvr_flag;
James Smarte29d74f2018-03-05 12:04:07 -08002806 struct completion *pmbox_done;
dea31012005-04-17 16:05:31 -05002807
2808 /*
James Smarte29d74f2018-03-05 12:04:07 -08002809 * If pmbox_done is empty, the driver thread gave up waiting and
dea31012005-04-17 16:05:31 -05002810 * continued running.
2811 */
James Smart7054a602007-04-25 09:52:34 -04002812 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002813 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarte29d74f2018-03-05 12:04:07 -08002814 pmbox_done = (struct completion *)pmboxq->context3;
2815 if (pmbox_done)
2816 complete(pmbox_done);
James Smart858c9f62007-06-17 19:56:39 -05002817 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002818 return;
2819}
2820
James Smartb95b2112019-08-14 16:56:47 -07002821static void
2822__lpfc_sli_rpi_release(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2823{
2824 unsigned long iflags;
2825
2826 if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
2827 lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
James Smartc6adba12020-11-15 11:26:34 -08002828 spin_lock_irqsave(&ndlp->lock, iflags);
James Smartb95b2112019-08-14 16:56:47 -07002829 ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
2830 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
James Smartc6adba12020-11-15 11:26:34 -08002831 spin_unlock_irqrestore(&ndlp->lock, iflags);
James Smartb95b2112019-08-14 16:56:47 -07002832 }
2833 ndlp->nlp_flag &= ~NLP_UNREG_INP;
2834}
James Smarte59058c2008-08-24 21:49:00 -04002835
2836/**
James Smart3621a712009-04-06 18:47:14 -04002837 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002838 * @phba: Pointer to HBA context object.
2839 * @pmb: Pointer to mailbox object.
2840 *
2841 * This function is the default mailbox completion handler. It
2842 * frees the memory resources associated with the completed mailbox
2843 * command. If the completed command is a REG_LOGIN mailbox command,
2844 * this function will issue a UREG_LOGIN to re-claim the RPI.
2845 **/
dea31012005-04-17 16:05:31 -05002846void
James Smart2e0fef82007-06-17 19:56:36 -05002847lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002848{
James Smartd439d282010-09-29 11:18:45 -04002849 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002850 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002851 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002852 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002853 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002854 int rc;
2855
James Smart3e1f0712018-11-29 16:09:29 -08002856 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smart7054a602007-04-25 09:52:34 -04002857
dea31012005-04-17 16:05:31 -05002858 if (mp) {
2859 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2860 kfree(mp);
2861 }
James Smart7054a602007-04-25 09:52:34 -04002862
2863 /*
2864 * If a REG_LOGIN succeeded after node is destroyed or node
2865 * is in re-discovery driver need to cleanup the RPI.
2866 */
James Smart2e0fef82007-06-17 19:56:36 -05002867 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002868 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2869 !pmb->u.mb.mbxStatus) {
2870 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002871 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart38503942020-03-22 11:12:53 -07002872 if (phba->sli_rev == LPFC_SLI_REV4)
2873 vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
James Smart04c68492009-05-22 14:52:52 -04002874 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002875 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002876 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002877 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2878 if (rc != MBX_NOT_FINISHED)
2879 return;
2880 }
2881
James Smart695a8142010-01-26 23:08:03 -05002882 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2883 !(phba->pport->load_flag & FC_UNLOADING) &&
2884 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002885 shost = lpfc_shost_from_vport(vport);
2886 spin_lock_irq(shost->host_lock);
2887 vport->vpi_state |= LPFC_VPI_REGISTERED;
2888 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2889 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002890 }
2891
James Smartd439d282010-09-29 11:18:45 -04002892 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -08002893 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -04002894 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002895 pmb->ctx_buf = NULL;
2896 pmb->ctx_ndlp = NULL;
2897 }
2898
2899 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2900 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2901
2902 /* Check to see if there are any deferred events to process */
2903 if (ndlp) {
2904 lpfc_printf_vlog(
2905 vport,
2906 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2907 "1438 UNREG cmpl deferred mbox x%x "
James Smartf1156122021-04-11 18:31:24 -07002908 "on NPort x%x Data: x%x x%x x%px x%x x%x\n",
James Smartdea16bd2018-11-29 16:09:30 -08002909 ndlp->nlp_rpi, ndlp->nlp_DID,
James Smarte9b11082020-11-15 11:26:33 -08002910 ndlp->nlp_flag, ndlp->nlp_defer_did,
2911 ndlp, vport->load_flag, kref_read(&ndlp->kref));
James Smartdea16bd2018-11-29 16:09:30 -08002912
2913 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2914 (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
James Smart00292e02018-12-13 15:17:55 -08002915 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002916 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2917 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002918 } else {
James Smartb95b2112019-08-14 16:56:47 -07002919 __lpfc_sli_rpi_release(vport, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002920 }
James Smarta70e63e2020-11-15 11:26:38 -08002921
2922 /* The unreg_login mailbox is complete and had a
2923 * reference that has to be released. The PLOGI
2924 * got its own ref.
2925 */
2926 lpfc_nlp_put(ndlp);
James Smart9b164062019-03-12 16:30:06 -07002927 pmb->ctx_ndlp = NULL;
James Smartdea16bd2018-11-29 16:09:30 -08002928 }
James Smartd439d282010-09-29 11:18:45 -04002929 }
2930
James Smart1037e4b2021-05-14 12:55:52 -07002931 /* This nlp_put pairs with lpfc_sli4_resume_rpi */
2932 if (pmb->u.mb.mbxCommand == MBX_RESUME_RPI) {
2933 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2934 lpfc_nlp_put(ndlp);
2935 }
2936
James Smartdcf2a4e2010-09-29 11:18:53 -04002937 /* Check security permission status on INIT_LINK mailbox command */
2938 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2939 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
Dick Kennedy372c1872020-06-30 14:50:00 -07002940 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartdcf2a4e2010-09-29 11:18:53 -04002941 "2860 SLI authentication is required "
2942 "for INIT_LINK but has not done yet\n");
2943
James Smart04c68492009-05-22 14:52:52 -04002944 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2945 lpfc_sli4_mbox_cmd_free(phba, pmb);
2946 else
2947 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002948}
James Smartbe6bb942015-04-07 15:07:22 -04002949 /**
2950 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2951 * @phba: Pointer to HBA context object.
2952 * @pmb: Pointer to mailbox object.
2953 *
2954 * This function is the unreg rpi mailbox completion handler. It
2955 * frees the memory resources associated with the completed mailbox
James Smarta70e63e2020-11-15 11:26:38 -08002956 * command. An additional reference is put on the ndlp to prevent
James Smartbe6bb942015-04-07 15:07:22 -04002957 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2958 * the unreg mailbox command completes, this routine puts the
2959 * reference back.
2960 *
2961 **/
2962void
2963lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2964{
2965 struct lpfc_vport *vport = pmb->vport;
2966 struct lpfc_nodelist *ndlp;
2967
James Smart3e1f0712018-11-29 16:09:29 -08002968 ndlp = pmb->ctx_ndlp;
James Smartbe6bb942015-04-07 15:07:22 -04002969 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2970 if (phba->sli_rev == LPFC_SLI_REV4 &&
2971 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002972 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002973 LPFC_SLI_INTF_IF_TYPE_2)) {
2974 if (ndlp) {
James Smartdea16bd2018-11-29 16:09:30 -08002975 lpfc_printf_vlog(
James Smarte9b11082020-11-15 11:26:33 -08002976 vport, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smartdea16bd2018-11-29 16:09:30 -08002977 "0010 UNREG_LOGIN vpi:%x "
2978 "rpi:%x DID:%x defer x%x flg x%x "
James Smartf1156122021-04-11 18:31:24 -07002979 "x%px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002980 vport->vpi, ndlp->nlp_rpi,
2981 ndlp->nlp_DID, ndlp->nlp_defer_did,
2982 ndlp->nlp_flag,
James Smart307e3382020-11-15 11:26:30 -08002983 ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002984 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartdea16bd2018-11-29 16:09:30 -08002985
2986 /* Check to see if there are any deferred
2987 * events to process
2988 */
2989 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2990 (ndlp->nlp_defer_did !=
2991 NLP_EVT_NOTHING_PENDING)) {
2992 lpfc_printf_vlog(
2993 vport, KERN_INFO, LOG_DISCOVERY,
2994 "4111 UNREG cmpl deferred "
2995 "clr x%x on "
James Smart32350662019-08-14 16:57:06 -07002996 "NPort x%x Data: x%x x%px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002997 ndlp->nlp_rpi, ndlp->nlp_DID,
2998 ndlp->nlp_defer_did, ndlp);
James Smart00292e02018-12-13 15:17:55 -08002999 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08003000 ndlp->nlp_defer_did =
3001 NLP_EVT_NOTHING_PENDING;
3002 lpfc_issue_els_plogi(
3003 vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08003004 } else {
James Smartb95b2112019-08-14 16:56:47 -07003005 __lpfc_sli_rpi_release(vport, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08003006 }
James Smart4430f7f2020-11-15 11:26:31 -08003007 lpfc_nlp_put(ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04003008 }
3009 }
3010 }
3011
3012 mempool_free(pmb, phba->mbox_mem_pool);
3013}
dea31012005-04-17 16:05:31 -05003014
James Smarte59058c2008-08-24 21:49:00 -04003015/**
James Smart3621a712009-04-06 18:47:14 -04003016 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04003017 * @phba: Pointer to HBA context object.
3018 *
3019 * This function is called with no lock held. This function processes all
3020 * the completed mailbox commands and gives it to upper layers. The interrupt
3021 * service routine processes mailbox completion interrupt and adds completed
3022 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
3023 * Worker thread call lpfc_sli_handle_mb_event, which will return the
3024 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
3025 * function returns the mailbox commands to the upper layer by calling the
3026 * completion handler function of each mailbox.
3027 **/
dea31012005-04-17 16:05:31 -05003028int
James Smart2e0fef82007-06-17 19:56:36 -05003029lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003030{
James Smart92d7f7b2007-06-17 19:56:38 -05003031 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05003032 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05003033 int rc;
3034 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05003035
3036 phba->sli.slistat.mbox_event++;
3037
James Smart92d7f7b2007-06-17 19:56:38 -05003038 /* Get all completed mailboxe buffers into the cmplq */
3039 spin_lock_irq(&phba->hbalock);
3040 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
3041 spin_unlock_irq(&phba->hbalock);
3042
dea31012005-04-17 16:05:31 -05003043 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05003044 do {
3045 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
3046 if (pmb == NULL)
3047 break;
3048
James Smart04c68492009-05-22 14:52:52 -04003049 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003050
James Smart858c9f62007-06-17 19:56:39 -05003051 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
3052 if (pmb->vport) {
3053 lpfc_debugfs_disc_trc(pmb->vport,
3054 LPFC_DISC_TRC_MBOX_VPORT,
3055 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
3056 (uint32_t)pmbox->mbxCommand,
3057 pmbox->un.varWords[0],
3058 pmbox->un.varWords[1]);
3059 }
3060 else {
3061 lpfc_debugfs_disc_trc(phba->pport,
3062 LPFC_DISC_TRC_MBOX,
3063 "MBOX cmpl: cmd:x%x mb:x%x x%x",
3064 (uint32_t)pmbox->mbxCommand,
3065 pmbox->un.varWords[0],
3066 pmbox->un.varWords[1]);
3067 }
3068 }
3069
dea31012005-04-17 16:05:31 -05003070 /*
3071 * It is a fatal error if unknown mbox command completion.
3072 */
3073 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
3074 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003075 /* Unknown mailbox command compl */
Dick Kennedy372c1872020-06-30 14:50:00 -07003076 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003077 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04003078 "x%x (x%x/x%x) Cmpl\n",
James Smart43bfea12019-09-21 20:58:57 -07003079 pmb->vport ? pmb->vport->vpi :
3080 LPFC_VPORT_UNKNOWN,
James Smart04c68492009-05-22 14:52:52 -04003081 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04003082 lpfc_sli_config_mbox_subsys_get(phba,
3083 pmb),
3084 lpfc_sli_config_mbox_opcode_get(phba,
3085 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05003086 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003087 phba->work_hs = HS_FFER3;
3088 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003089 continue;
dea31012005-04-17 16:05:31 -05003090 }
3091
dea31012005-04-17 16:05:31 -05003092 if (pmbox->mbxStatus) {
3093 phba->sli.slistat.mbox_stat_err++;
3094 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
3095 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05003096 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04003097 LOG_MBOX | LOG_SLI,
3098 "(%d):0305 Mbox cmd cmpl "
3099 "error - RETRYing Data: x%x "
3100 "(x%x/x%x) x%x x%x x%x\n",
James Smart43bfea12019-09-21 20:58:57 -07003101 pmb->vport ? pmb->vport->vpi :
3102 LPFC_VPORT_UNKNOWN,
James Smarta183a152011-10-10 21:32:43 -04003103 pmbox->mbxCommand,
3104 lpfc_sli_config_mbox_subsys_get(phba,
3105 pmb),
3106 lpfc_sli_config_mbox_opcode_get(phba,
3107 pmb),
3108 pmbox->mbxStatus,
3109 pmbox->un.varWords[0],
James Smart43bfea12019-09-21 20:58:57 -07003110 pmb->vport ? pmb->vport->port_state :
3111 LPFC_VPORT_UNKNOWN);
dea31012005-04-17 16:05:31 -05003112 pmbox->mbxStatus = 0;
3113 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05003114 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04003115 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05003116 continue;
dea31012005-04-17 16:05:31 -05003117 }
3118 }
3119
3120 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05003121 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
Sakari Ailus2d44d162019-09-04 19:04:23 +03003122 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
James Smarte74c03c2013-04-17 20:15:19 -04003123 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
3124 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003125 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05003126 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04003127 lpfc_sli_config_mbox_subsys_get(phba, pmb),
3128 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05003129 pmb->mbox_cmpl,
3130 *((uint32_t *) pmbox),
3131 pmbox->un.varWords[0],
3132 pmbox->un.varWords[1],
3133 pmbox->un.varWords[2],
3134 pmbox->un.varWords[3],
3135 pmbox->un.varWords[4],
3136 pmbox->un.varWords[5],
3137 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04003138 pmbox->un.varWords[7],
3139 pmbox->un.varWords[8],
3140 pmbox->un.varWords[9],
3141 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05003142
James Smart92d7f7b2007-06-17 19:56:38 -05003143 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05003144 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05003145 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05003146 return 0;
dea31012005-04-17 16:05:31 -05003147}
James Smart92d7f7b2007-06-17 19:56:38 -05003148
James Smarte59058c2008-08-24 21:49:00 -04003149/**
James Smart3621a712009-04-06 18:47:14 -04003150 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04003151 * @phba: Pointer to HBA context object.
3152 * @pring: Pointer to driver SLI ring object.
3153 * @tag: buffer tag.
3154 *
3155 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
3156 * is set in the tag the buffer is posted for a particular exchange,
3157 * the function will return the buffer without replacing the buffer.
3158 * If the buffer is for unsolicited ELS or CT traffic, this function
3159 * returns the buffer and also posts another buffer to the firmware.
3160 **/
James Smart76bb24e2007-10-27 13:38:00 -04003161static struct lpfc_dmabuf *
3162lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05003163 struct lpfc_sli_ring *pring,
3164 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04003165{
James Smart9f1e1b52008-12-04 22:39:40 -05003166 struct hbq_dmabuf *hbq_entry;
3167
James Smart76bb24e2007-10-27 13:38:00 -04003168 if (tag & QUE_BUFTAG_BIT)
3169 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05003170 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
3171 if (!hbq_entry)
3172 return NULL;
3173 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04003174}
James Smart57127f12007-10-27 13:37:05 -04003175
James Smart3772a992009-05-22 14:50:54 -04003176/**
James Smart3a8070c2020-03-31 09:50:05 -07003177 * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer
3178 * containing a NVME LS request.
3179 * @phba: pointer to lpfc hba data structure.
3180 * @piocb: pointer to the iocbq struct representing the sequence starting
3181 * frame.
3182 *
3183 * This routine initially validates the NVME LS, validates there is a login
3184 * with the port that sent the LS, and then calls the appropriate nvme host
3185 * or target LS request handler.
3186 **/
3187static void
3188lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
3189{
3190 struct lpfc_nodelist *ndlp;
3191 struct lpfc_dmabuf *d_buf;
3192 struct hbq_dmabuf *nvmebuf;
3193 struct fc_frame_header *fc_hdr;
3194 struct lpfc_async_xchg_ctx *axchg = NULL;
3195 char *failwhy = NULL;
3196 uint32_t oxid, sid, did, fctl, size;
James Smart4e57e0b2020-05-20 11:59:28 -07003197 int ret = 1;
James Smart3a8070c2020-03-31 09:50:05 -07003198
3199 d_buf = piocb->context2;
3200
3201 nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
3202 fc_hdr = nvmebuf->hbuf.virt;
3203 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
3204 sid = sli4_sid_from_fc_hdr(fc_hdr);
3205 did = sli4_did_from_fc_hdr(fc_hdr);
3206 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
3207 fc_hdr->fh_f_ctl[1] << 8 |
3208 fc_hdr->fh_f_ctl[2]);
3209 size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
3210
3211 lpfc_nvmeio_data(phba, "NVME LS RCV: xri x%x sz %d from %06x\n",
3212 oxid, size, sid);
3213
3214 if (phba->pport->load_flag & FC_UNLOADING) {
3215 failwhy = "Driver Unloading";
3216 } else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
3217 failwhy = "NVME FC4 Disabled";
3218 } else if (!phba->nvmet_support && !phba->pport->localport) {
3219 failwhy = "No Localport";
3220 } else if (phba->nvmet_support && !phba->targetport) {
3221 failwhy = "No Targetport";
3222 } else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {
3223 failwhy = "Bad NVME LS R_CTL";
3224 } else if (unlikely((fctl & 0x00FF0000) !=
3225 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {
3226 failwhy = "Bad NVME LS F_CTL";
3227 } else {
3228 axchg = kzalloc(sizeof(*axchg), GFP_ATOMIC);
3229 if (!axchg)
3230 failwhy = "No CTX memory";
3231 }
3232
3233 if (unlikely(failwhy)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07003234 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3a8070c2020-03-31 09:50:05 -07003235 "6154 Drop NVME LS: SID %06X OXID x%X: %s\n",
3236 sid, oxid, failwhy);
3237 goto out_fail;
3238 }
3239
3240 /* validate the source of the LS is logged in */
3241 ndlp = lpfc_findnode_did(phba->pport, sid);
James Smart307e3382020-11-15 11:26:30 -08003242 if (!ndlp ||
James Smart3a8070c2020-03-31 09:50:05 -07003243 ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3244 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3245 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
3246 "6216 NVME Unsol rcv: No ndlp: "
3247 "NPort_ID x%x oxid x%x\n",
3248 sid, oxid);
3249 goto out_fail;
3250 }
3251
3252 axchg->phba = phba;
3253 axchg->ndlp = ndlp;
3254 axchg->size = size;
3255 axchg->oxid = oxid;
3256 axchg->sid = sid;
3257 axchg->wqeq = NULL;
3258 axchg->state = LPFC_NVME_STE_LS_RCV;
3259 axchg->entry_cnt = 1;
3260 axchg->rqb_buffer = (void *)nvmebuf;
3261 axchg->hdwq = &phba->sli4_hba.hdwq[0];
3262 axchg->payload = nvmebuf->dbuf.virt;
3263 INIT_LIST_HEAD(&axchg->list);
3264
James Smart243156c2021-01-04 10:02:37 -08003265 if (phba->nvmet_support) {
James Smart3a8070c2020-03-31 09:50:05 -07003266 ret = lpfc_nvmet_handle_lsreq(phba, axchg);
James Smart243156c2021-01-04 10:02:37 -08003267 spin_lock_irq(&ndlp->lock);
3268 if (!ret && !(ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH)) {
3269 ndlp->fc4_xpt_flags |= NLP_XPT_HAS_HH;
3270 spin_unlock_irq(&ndlp->lock);
3271
3272 /* This reference is a single occurrence to hold the
3273 * node valid until the nvmet transport calls
3274 * host_release.
3275 */
3276 if (!lpfc_nlp_get(ndlp))
3277 goto out_fail;
3278
3279 lpfc_printf_log(phba, KERN_ERR, LOG_NODE,
James Smartf1156122021-04-11 18:31:24 -07003280 "6206 NVMET unsol ls_req ndlp x%px "
James Smart243156c2021-01-04 10:02:37 -08003281 "DID x%x xflags x%x refcnt %d\n",
3282 ndlp, ndlp->nlp_DID,
3283 ndlp->fc4_xpt_flags,
3284 kref_read(&ndlp->kref));
3285 } else {
3286 spin_unlock_irq(&ndlp->lock);
3287 }
3288 } else {
James Smart3a8070c2020-03-31 09:50:05 -07003289 ret = lpfc_nvme_handle_lsreq(phba, axchg);
James Smart243156c2021-01-04 10:02:37 -08003290 }
James Smart3a8070c2020-03-31 09:50:05 -07003291
3292 /* if zero, LS was successfully handled. If non-zero, LS not handled */
3293 if (!ret)
3294 return;
3295
James Smart243156c2021-01-04 10:02:37 -08003296out_fail:
Dick Kennedy372c1872020-06-30 14:50:00 -07003297 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3a8070c2020-03-31 09:50:05 -07003298 "6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "
3299 "NVMe%s handler failed %d\n",
3300 did, sid, oxid,
3301 (phba->nvmet_support) ? "T" : "I", ret);
3302
James Smart3a8070c2020-03-31 09:50:05 -07003303 /* recycle receive buffer */
3304 lpfc_in_buf_free(phba, &nvmebuf->dbuf);
3305
3306 /* If start of new exchange, abort it */
James Smart4e57e0b2020-05-20 11:59:28 -07003307 if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))
3308 ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);
3309
3310 if (ret)
3311 kfree(axchg);
James Smart3a8070c2020-03-31 09:50:05 -07003312}
3313
3314/**
James Smart3772a992009-05-22 14:50:54 -04003315 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
3316 * @phba: Pointer to HBA context object.
3317 * @pring: Pointer to driver SLI ring object.
3318 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
3319 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
3320 * @fch_type: the type for the first frame of the sequence.
3321 *
3322 * This function is called with no lock held. This function uses the r_ctl and
3323 * type of the received sequence to find the correct callback function to call
3324 * to process the sequence.
3325 **/
3326static int
3327lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3328 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
3329 uint32_t fch_type)
3330{
3331 int i;
3332
James Smartf358dd02017-02-12 13:52:34 -08003333 switch (fch_type) {
3334 case FC_TYPE_NVME:
James Smart3a8070c2020-03-31 09:50:05 -07003335 lpfc_nvme_unsol_ls_handler(phba, saveq);
James Smartf358dd02017-02-12 13:52:34 -08003336 return 1;
3337 default:
3338 break;
3339 }
3340
James Smart3772a992009-05-22 14:50:54 -04003341 /* unSolicited Responses */
3342 if (pring->prt[0].profile) {
3343 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
3344 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
3345 saveq);
3346 return 1;
3347 }
3348 /* We must search, based on rctl / type
3349 for the right routine */
3350 for (i = 0; i < pring->num_mask; i++) {
3351 if ((pring->prt[i].rctl == fch_r_ctl) &&
3352 (pring->prt[i].type == fch_type)) {
3353 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
3354 (pring->prt[i].lpfc_sli_rcv_unsol_event)
3355 (phba, pring, saveq);
3356 return 1;
3357 }
3358 }
3359 return 0;
3360}
James Smarte59058c2008-08-24 21:49:00 -04003361
3362/**
James Smart3621a712009-04-06 18:47:14 -04003363 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04003364 * @phba: Pointer to HBA context object.
3365 * @pring: Pointer to driver SLI ring object.
3366 * @saveq: Pointer to the unsolicited iocb.
3367 *
3368 * This function is called with no lock held by the ring event handler
3369 * when there is an unsolicited iocb posted to the response ring by the
3370 * firmware. This function gets the buffer associated with the iocbs
3371 * and calls the event handler for the ring. This function handles both
3372 * qring buffers and hbq buffers.
3373 * When the function returns 1 the caller can free the iocb object otherwise
3374 * upper layer functions will free the iocb objects.
3375 **/
dea31012005-04-17 16:05:31 -05003376static int
3377lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3378 struct lpfc_iocbq *saveq)
3379{
3380 IOCB_t * irsp;
3381 WORD5 * w5p;
3382 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04003383 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05003384 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05003385
dea31012005-04-17 16:05:31 -05003386 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04003387
3388 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
3389 if (pring->lpfc_sli_rcv_async_status)
3390 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
3391 else
3392 lpfc_printf_log(phba,
3393 KERN_WARNING,
3394 LOG_SLI,
3395 "0316 Ring %d handler: unexpected "
3396 "ASYNC_STATUS iocb received evt_code "
3397 "0x%x\n",
3398 pring->ringno,
3399 irsp->un.asyncstat.evt_code);
3400 return 1;
3401 }
3402
James Smart3163f722008-02-08 18:50:25 -05003403 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
3404 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
3405 if (irsp->ulpBdeCount > 0) {
3406 dmzbuf = lpfc_sli_get_buff(phba, pring,
3407 irsp->un.ulpWord[3]);
3408 lpfc_in_buf_free(phba, dmzbuf);
3409 }
3410
3411 if (irsp->ulpBdeCount > 1) {
3412 dmzbuf = lpfc_sli_get_buff(phba, pring,
3413 irsp->unsli3.sli3Words[3]);
3414 lpfc_in_buf_free(phba, dmzbuf);
3415 }
3416
3417 if (irsp->ulpBdeCount > 2) {
3418 dmzbuf = lpfc_sli_get_buff(phba, pring,
3419 irsp->unsli3.sli3Words[7]);
3420 lpfc_in_buf_free(phba, dmzbuf);
3421 }
3422
3423 return 1;
3424 }
3425
James Smart92d7f7b2007-06-17 19:56:38 -05003426 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04003427 if (irsp->ulpBdeCount != 0) {
3428 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05003429 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04003430 if (!saveq->context2)
3431 lpfc_printf_log(phba,
3432 KERN_ERR,
3433 LOG_SLI,
3434 "0341 Ring %d Cannot find buffer for "
3435 "an unsolicited iocb. tag 0x%x\n",
3436 pring->ringno,
3437 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04003438 }
3439 if (irsp->ulpBdeCount == 2) {
3440 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04003441 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04003442 if (!saveq->context3)
3443 lpfc_printf_log(phba,
3444 KERN_ERR,
3445 LOG_SLI,
3446 "0342 Ring %d Cannot find buffer for an"
3447 " unsolicited iocb. tag 0x%x\n",
3448 pring->ringno,
3449 irsp->unsli3.sli3Words[7]);
3450 }
3451 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04003452 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04003453 if (irsp->ulpBdeCount != 0) {
3454 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
3455 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05003456 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04003457 lpfc_printf_log(phba,
3458 KERN_ERR,
3459 LOG_SLI,
3460 "0343 Ring %d Cannot find "
3461 "buffer for an unsolicited iocb"
3462 ". tag 0x%x\n", pring->ringno,
3463 irsp->un.ulpWord[3]);
3464 }
3465 if (irsp->ulpBdeCount == 2) {
3466 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
3467 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05003468 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04003469 lpfc_printf_log(phba,
3470 KERN_ERR,
3471 LOG_SLI,
3472 "0344 Ring %d Cannot find "
3473 "buffer for an unsolicited "
3474 "iocb. tag 0x%x\n",
3475 pring->ringno,
3476 irsp->unsli3.sli3Words[7]);
3477 }
3478 }
James Smart92d7f7b2007-06-17 19:56:38 -05003479 }
James Smart9c2face2008-01-11 01:53:18 -05003480 if (irsp->ulpBdeCount != 0 &&
3481 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
3482 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
3483 int found = 0;
3484
3485 /* search continue save q for same XRI */
3486 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04003487 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
3488 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05003489 list_add_tail(&saveq->list, &iocbq->list);
3490 found = 1;
3491 break;
3492 }
3493 }
3494 if (!found)
3495 list_add_tail(&saveq->clist,
3496 &pring->iocb_continue_saveq);
3497 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
3498 list_del_init(&iocbq->clist);
3499 saveq = iocbq;
3500 irsp = &(saveq->iocb);
3501 } else
3502 return 0;
3503 }
3504 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
3505 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
3506 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04003507 Rctl = FC_RCTL_ELS_REQ;
3508 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05003509 } else {
3510 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
3511 Rctl = w5p->hcsw.Rctl;
3512 Type = w5p->hcsw.Type;
3513
3514 /* Firmware Workaround */
3515 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
3516 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
3517 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04003518 Rctl = FC_RCTL_ELS_REQ;
3519 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05003520 w5p->hcsw.Rctl = Rctl;
3521 w5p->hcsw.Type = Type;
3522 }
3523 }
James Smart92d7f7b2007-06-17 19:56:38 -05003524
James Smart3772a992009-05-22 14:50:54 -04003525 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05003526 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003527 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003528 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04003529 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04003530
James Smart92d7f7b2007-06-17 19:56:38 -05003531 return 1;
dea31012005-04-17 16:05:31 -05003532}
3533
James Smarte59058c2008-08-24 21:49:00 -04003534/**
James Smart3621a712009-04-06 18:47:14 -04003535 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04003536 * @phba: Pointer to HBA context object.
3537 * @pring: Pointer to driver SLI ring object.
3538 * @prspiocb: Pointer to response iocb object.
3539 *
3540 * This function looks up the iocb_lookup table to get the command iocb
3541 * corresponding to the given response iocb using the iotag of the
James Smarte2a8be52019-05-06 17:26:47 -07003542 * response iocb. The driver calls this function with the hbalock held
3543 * for SLI3 ports or the ring lock held for SLI4 ports.
James Smarte59058c2008-08-24 21:49:00 -04003544 * This function returns the command iocb object if it finds the command
3545 * iocb else returns NULL.
3546 **/
dea31012005-04-17 16:05:31 -05003547static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05003548lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3549 struct lpfc_sli_ring *pring,
3550 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05003551{
dea31012005-04-17 16:05:31 -05003552 struct lpfc_iocbq *cmd_iocb = NULL;
3553 uint16_t iotag;
James Smarte2a8be52019-05-06 17:26:47 -07003554 spinlock_t *temp_lock = NULL;
3555 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05003556
James Smarte2a8be52019-05-06 17:26:47 -07003557 if (phba->sli_rev == LPFC_SLI_REV4)
3558 temp_lock = &pring->ring_lock;
3559 else
3560 temp_lock = &phba->hbalock;
3561
3562 spin_lock_irqsave(temp_lock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003563 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05003564
James Bottomley604a3e32005-10-29 10:28:33 -05003565 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3566 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003567 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07003568 /* remove from txcmpl queue list */
3569 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04003570 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003571 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003572 spin_unlock_irqrestore(temp_lock, iflag);
James Smart89533e92016-10-13 15:06:15 -07003573 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003574 }
dea31012005-04-17 16:05:31 -05003575 }
3576
James Smarte2a8be52019-05-06 17:26:47 -07003577 spin_unlock_irqrestore(temp_lock, iflag);
Dick Kennedy372c1872020-06-30 14:50:00 -07003578 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart89533e92016-10-13 15:06:15 -07003579 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05003580 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003581 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05003582 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05003583 return NULL;
3584}
3585
James Smarte59058c2008-08-24 21:49:00 -04003586/**
James Smart3772a992009-05-22 14:50:54 -04003587 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3588 * @phba: Pointer to HBA context object.
3589 * @pring: Pointer to driver SLI ring object.
3590 * @iotag: IOCB tag.
3591 *
3592 * This function looks up the iocb_lookup table to get the command iocb
James Smarte2a8be52019-05-06 17:26:47 -07003593 * corresponding to the given iotag. The driver calls this function with
3594 * the ring lock held because this function is an SLI4 port only helper.
James Smart3772a992009-05-22 14:50:54 -04003595 * This function returns the command iocb object if it finds the command
3596 * iocb else returns NULL.
3597 **/
3598static struct lpfc_iocbq *
3599lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3600 struct lpfc_sli_ring *pring, uint16_t iotag)
3601{
James Smart895427b2017-02-12 13:52:30 -08003602 struct lpfc_iocbq *cmd_iocb = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07003603 spinlock_t *temp_lock = NULL;
3604 unsigned long iflag = 0;
James Smart3772a992009-05-22 14:50:54 -04003605
James Smarte2a8be52019-05-06 17:26:47 -07003606 if (phba->sli_rev == LPFC_SLI_REV4)
3607 temp_lock = &pring->ring_lock;
3608 else
3609 temp_lock = &phba->hbalock;
3610
3611 spin_lock_irqsave(temp_lock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003612 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3613 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003614 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3615 /* remove from txcmpl queue list */
3616 list_del_init(&cmd_iocb->list);
3617 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003618 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003619 spin_unlock_irqrestore(temp_lock, iflag);
James Smart4f2e66c2012-05-09 21:17:07 -04003620 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003621 }
James Smart3772a992009-05-22 14:50:54 -04003622 }
James Smart89533e92016-10-13 15:06:15 -07003623
James Smarte2a8be52019-05-06 17:26:47 -07003624 spin_unlock_irqrestore(temp_lock, iflag);
Dick Kennedy372c1872020-06-30 14:50:00 -07003625 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08003626 "0372 iotag x%x lookup error: max iotag (x%x) "
3627 "iocb_flag x%x\n",
3628 iotag, phba->sli.last_iotag,
3629 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003630 return NULL;
3631}
3632
3633/**
James Smart3621a712009-04-06 18:47:14 -04003634 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003635 * @phba: Pointer to HBA context object.
3636 * @pring: Pointer to driver SLI ring object.
3637 * @saveq: Pointer to the response iocb to be processed.
3638 *
3639 * This function is called by the ring event handler for non-fcp
3640 * rings when there is a new response iocb in the response ring.
3641 * The caller is not required to hold any locks. This function
3642 * gets the command iocb associated with the response iocb and
3643 * calls the completion handler for the command iocb. If there
3644 * is no completion handler, the function will free the resources
3645 * associated with command iocb. If the response iocb is for
3646 * an already aborted command iocb, the status of the completion
3647 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3648 * This function always returns 1.
3649 **/
dea31012005-04-17 16:05:31 -05003650static int
James Smart2e0fef82007-06-17 19:56:36 -05003651lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003652 struct lpfc_iocbq *saveq)
3653{
James Smart2e0fef82007-06-17 19:56:36 -05003654 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003655 int rc = 1;
3656 unsigned long iflag;
3657
James Bottomley604a3e32005-10-29 10:28:33 -05003658 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
dea31012005-04-17 16:05:31 -05003659 if (cmdiocbp) {
3660 if (cmdiocbp->iocb_cmpl) {
3661 /*
James Smartea2151b2008-09-07 11:52:10 -04003662 * If an ELS command failed send an event to mgmt
3663 * application.
3664 */
3665 if (saveq->iocb.ulpStatus &&
3666 (pring->ringno == LPFC_ELS_RING) &&
3667 (cmdiocbp->iocb.ulpCommand ==
3668 CMD_ELS_REQUEST64_CR))
3669 lpfc_send_els_failure_event(phba,
3670 cmdiocbp, saveq);
3671
3672 /*
dea31012005-04-17 16:05:31 -05003673 * Post all ELS completions to the worker thread.
3674 * All other are passed to the completion callback.
3675 */
3676 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003677 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3678 (cmdiocbp->iocb_flag &
3679 LPFC_DRIVER_ABORTED)) {
3680 spin_lock_irqsave(&phba->hbalock,
3681 iflag);
James Smart07951072007-04-25 09:51:38 -04003682 cmdiocbp->iocb_flag &=
3683 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003684 spin_unlock_irqrestore(&phba->hbalock,
3685 iflag);
James Smart07951072007-04-25 09:51:38 -04003686 saveq->iocb.ulpStatus =
3687 IOSTAT_LOCAL_REJECT;
3688 saveq->iocb.un.ulpWord[4] =
3689 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003690
3691 /* Firmware could still be in progress
3692 * of DMAing payload, so don't free data
3693 * buffer till after a hbeat.
3694 */
James Smart341af102010-01-26 23:07:37 -05003695 spin_lock_irqsave(&phba->hbalock,
3696 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003697 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003698 spin_unlock_irqrestore(&phba->hbalock,
3699 iflag);
3700 }
James Smart0f65ff62010-02-26 14:14:23 -05003701 if (phba->sli_rev == LPFC_SLI_REV4) {
3702 if (saveq->iocb_flag &
3703 LPFC_EXCHANGE_BUSY) {
3704 /* Set cmdiocb flag for the
3705 * exchange busy so sgl (xri)
3706 * will not be released until
3707 * the abort xri is received
3708 * from hba.
3709 */
3710 spin_lock_irqsave(
3711 &phba->hbalock, iflag);
3712 cmdiocbp->iocb_flag |=
3713 LPFC_EXCHANGE_BUSY;
3714 spin_unlock_irqrestore(
3715 &phba->hbalock, iflag);
3716 }
3717 if (cmdiocbp->iocb_flag &
3718 LPFC_DRIVER_ABORTED) {
3719 /*
3720 * Clear LPFC_DRIVER_ABORTED
3721 * bit in case it was driver
3722 * initiated abort.
3723 */
3724 spin_lock_irqsave(
3725 &phba->hbalock, iflag);
3726 cmdiocbp->iocb_flag &=
3727 ~LPFC_DRIVER_ABORTED;
3728 spin_unlock_irqrestore(
3729 &phba->hbalock, iflag);
3730 cmdiocbp->iocb.ulpStatus =
3731 IOSTAT_LOCAL_REJECT;
3732 cmdiocbp->iocb.un.ulpWord[4] =
3733 IOERR_ABORT_REQUESTED;
3734 /*
3735 * For SLI4, irsiocb contains
3736 * NO_XRI in sli_xritag, it
3737 * shall not affect releasing
3738 * sgl (xri) process.
3739 */
3740 saveq->iocb.ulpStatus =
3741 IOSTAT_LOCAL_REJECT;
3742 saveq->iocb.un.ulpWord[4] =
3743 IOERR_SLI_ABORTED;
3744 spin_lock_irqsave(
3745 &phba->hbalock, iflag);
3746 saveq->iocb_flag |=
3747 LPFC_DELAY_MEM_FREE;
3748 spin_unlock_irqrestore(
3749 &phba->hbalock, iflag);
3750 }
James Smart07951072007-04-25 09:51:38 -04003751 }
dea31012005-04-17 16:05:31 -05003752 }
James Smart2e0fef82007-06-17 19:56:36 -05003753 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003754 } else
3755 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003756 } else {
3757 /*
3758 * Unknown initiating command based on the response iotag.
3759 * This could be the case on the ELS ring because of
3760 * lpfc_els_abort().
3761 */
3762 if (pring->ringno != LPFC_ELS_RING) {
3763 /*
3764 * Ring <ringno> handler: unexpected completion IoTag
3765 * <IoTag>
3766 */
James Smarta257bf92009-04-06 18:48:10 -04003767 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003768 "0322 Ring %d handler: "
3769 "unexpected completion IoTag x%x "
3770 "Data: x%x x%x x%x x%x\n",
3771 pring->ringno,
3772 saveq->iocb.ulpIoTag,
3773 saveq->iocb.ulpStatus,
3774 saveq->iocb.un.ulpWord[4],
3775 saveq->iocb.ulpCommand,
3776 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003777 }
3778 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003779
dea31012005-04-17 16:05:31 -05003780 return rc;
3781}
3782
James Smarte59058c2008-08-24 21:49:00 -04003783/**
James Smart3621a712009-04-06 18:47:14 -04003784 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003785 * @phba: Pointer to HBA context object.
3786 * @pring: Pointer to driver SLI ring object.
3787 *
3788 * This function is called from the iocb ring event handlers when
3789 * put pointer is ahead of the get pointer for a ring. This function signal
3790 * an error attention condition to the worker thread and the worker
3791 * thread will transition the HBA to offline state.
3792 **/
James Smart2e0fef82007-06-17 19:56:36 -05003793static void
3794lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003795{
James Smart34b02dc2008-08-24 21:49:55 -04003796 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003797 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003798 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003799 * rsp ring <portRspMax>
3800 */
Dick Kennedy372c1872020-06-30 14:50:00 -07003801 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003802 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003803 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003804 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003805 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003806
James Smart2e0fef82007-06-17 19:56:36 -05003807 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003808
3809 /*
3810 * All error attention handlers are posted to
3811 * worker thread
3812 */
3813 phba->work_ha |= HA_ERATT;
3814 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003815
James Smart5e9d9b82008-06-14 22:52:53 -04003816 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003817
3818 return;
3819}
3820
James Smarte59058c2008-08-24 21:49:00 -04003821/**
James Smart3621a712009-04-06 18:47:14 -04003822 * lpfc_poll_eratt - Error attention polling timer timeout handler
Lee Jones7af29d42020-07-21 17:41:31 +01003823 * @t: Context to fetch pointer to address of HBA context object from.
James Smart93996272008-08-24 21:50:30 -04003824 *
3825 * This function is invoked by the Error Attention polling timer when the
3826 * timer times out. It will check the SLI Error Attention register for
3827 * possible attention events. If so, it will post an Error Attention event
3828 * and wake up worker thread to process it. Otherwise, it will set up the
3829 * Error Attention polling timer for the next poll.
3830 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003831void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003832{
3833 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003834 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003835 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003836
Kees Cookf22eb4d2017-09-06 20:24:26 -07003837 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003838
James Smartaa6fbb72012-08-03 12:36:03 -04003839 /* Here we will also keep track of interrupts per sec of the hba */
3840 sli_intr = phba->sli.slistat.sli_intr;
3841
3842 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3843 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3844 sli_intr);
3845 else
3846 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3847
James Smart65791f12016-07-06 12:35:56 -07003848 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3849 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003850 phba->sli.slistat.sli_ips = cnt;
3851
3852 phba->sli.slistat.sli_prev_intr = sli_intr;
3853
James Smart93996272008-08-24 21:50:30 -04003854 /* Check chip HA register for error event */
3855 eratt = lpfc_sli_check_eratt(phba);
3856
3857 if (eratt)
3858 /* Tell the worker thread there is work to do */
3859 lpfc_worker_wake_up(phba);
3860 else
3861 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003862 mod_timer(&phba->eratt_poll,
3863 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003864 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003865 return;
3866}
3867
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003868
James Smarte59058c2008-08-24 21:49:00 -04003869/**
James Smart3621a712009-04-06 18:47:14 -04003870 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003871 * @phba: Pointer to HBA context object.
3872 * @pring: Pointer to driver SLI ring object.
3873 * @mask: Host attention register mask for this ring.
3874 *
3875 * This function is called from the interrupt context when there is a ring
3876 * event for the fcp ring. The caller does not hold any lock.
3877 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003878 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003879 * LE bit set. The function will call the completion handler of the command iocb
3880 * if the response iocb indicates a completion for a command iocb or it is
3881 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3882 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003883 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003884 * to check it explicitly.
3885 */
3886int
James Smart2e0fef82007-06-17 19:56:36 -05003887lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3888 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003889{
James Smart34b02dc2008-08-24 21:49:55 -04003890 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003891 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003892 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003893 struct lpfc_iocbq *cmdiocbq = NULL;
3894 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003895 uint32_t status;
3896 uint32_t portRspPut, portRspMax;
3897 int rc = 1;
3898 lpfc_iocb_type type;
3899 unsigned long iflag;
3900 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003901
James Smart2e0fef82007-06-17 19:56:36 -05003902 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003903 pring->stats.iocb_event++;
3904
dea31012005-04-17 16:05:31 -05003905 /*
3906 * The next available response entry should never exceed the maximum
3907 * entries. If it does, treat it as an adapter hardware error.
3908 */
James Smart7e56aa22012-08-03 12:35:34 -04003909 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003910 portRspPut = le32_to_cpu(pgp->rspPutInx);
3911 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003912 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003913 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003914 return 1;
3915 }
James Smart45ed1192009-10-02 15:17:02 -04003916 if (phba->fcp_ring_in_use) {
3917 spin_unlock_irqrestore(&phba->hbalock, iflag);
3918 return 1;
3919 } else
3920 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003921
3922 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003923 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003924 /*
3925 * Fetch an entry off the ring and copy it into a local data
3926 * structure. The copy involves a byte-swap since the
3927 * network byte order and pci byte orders are different.
3928 */
James Smarted957682007-06-17 19:56:37 -05003929 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003930 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003931
James Smart7e56aa22012-08-03 12:35:34 -04003932 if (++pring->sli.sli3.rspidx >= portRspMax)
3933 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003934
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003935 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3936 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003937 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003938 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003939 irsp = &rspiocbq.iocb;
3940
dea31012005-04-17 16:05:31 -05003941 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3942 pring->stats.iocb_rsp++;
3943 rsp_cmpl++;
3944
3945 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003946 /*
3947 * If resource errors reported from HBA, reduce
3948 * queuedepths of the SCSI device.
3949 */
3950 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003951 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3952 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003953 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003954 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003955 spin_lock_irqsave(&phba->hbalock, iflag);
3956 }
3957
dea31012005-04-17 16:05:31 -05003958 /* Rsp ring <ringno> error: IOCB */
3959 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003960 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003961 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003962 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003963 irsp->un.ulpWord[0],
3964 irsp->un.ulpWord[1],
3965 irsp->un.ulpWord[2],
3966 irsp->un.ulpWord[3],
3967 irsp->un.ulpWord[4],
3968 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003969 *(uint32_t *)&irsp->un1,
3970 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003971 }
3972
3973 switch (type) {
3974 case LPFC_ABORT_IOCB:
3975 case LPFC_SOL_IOCB:
3976 /*
3977 * Idle exchange closed via ABTS from port. No iocb
3978 * resources need to be recovered.
3979 */
3980 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003981 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003982 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003983 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003984 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003985 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003986 break;
3987 }
3988
James Smarte2a8be52019-05-06 17:26:47 -07003989 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003990 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3991 &rspiocbq);
James Smarte2a8be52019-05-06 17:26:47 -07003992 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05003993 if (unlikely(!cmdiocbq))
3994 break;
3995 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3996 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3997 if (cmdiocbq->iocb_cmpl) {
3998 spin_unlock_irqrestore(&phba->hbalock, iflag);
3999 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
4000 &rspiocbq);
4001 spin_lock_irqsave(&phba->hbalock, iflag);
4002 }
dea31012005-04-17 16:05:31 -05004003 break;
James Smarta4bc3372006-12-02 13:34:16 -05004004 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05004005 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05004006 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004007 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05004008 break;
dea31012005-04-17 16:05:31 -05004009 default:
4010 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
4011 char adaptermsg[LPFC_MAX_ADPTMSG];
4012 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4013 memcpy(&adaptermsg[0], (uint8_t *) irsp,
4014 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07004015 dev_warn(&((phba->pcidev)->dev),
4016 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05004017 phba->brd_no, adaptermsg);
4018 } else {
4019 /* Unknown IOCB command */
Dick Kennedy372c1872020-06-30 14:50:00 -07004020 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04004021 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05004022 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004023 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05004024 irsp->ulpStatus,
4025 irsp->ulpIoTag,
4026 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05004027 }
4028 break;
4029 }
4030
4031 /*
4032 * The response IOCB has been processed. Update the ring
4033 * pointer in SLIM. If the port response put pointer has not
4034 * been updated, sync the pgp->rspPutInx and fetch the new port
4035 * response put pointer.
4036 */
James Smart7e56aa22012-08-03 12:35:34 -04004037 writel(pring->sli.sli3.rspidx,
4038 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05004039
James Smart7e56aa22012-08-03 12:35:34 -04004040 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05004041 portRspPut = le32_to_cpu(pgp->rspPutInx);
4042 }
4043
4044 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
4045 pring->stats.iocb_rsp_full++;
4046 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4047 writel(status, phba->CAregaddr);
4048 readl(phba->CAregaddr);
4049 }
4050 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4051 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4052 pring->stats.iocb_cmd_empty++;
4053
4054 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04004055 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05004056 lpfc_sli_resume_iocb(phba, pring);
4057
4058 if ((pring->lpfc_sli_cmd_available))
4059 (pring->lpfc_sli_cmd_available) (phba, pring);
4060
4061 }
4062
James Smart45ed1192009-10-02 15:17:02 -04004063 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004064 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05004065 return rc;
4066}
4067
James Smarte59058c2008-08-24 21:49:00 -04004068/**
James Smart3772a992009-05-22 14:50:54 -04004069 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
4070 * @phba: Pointer to HBA context object.
4071 * @pring: Pointer to driver SLI ring object.
4072 * @rspiocbp: Pointer to driver response IOCB object.
4073 *
4074 * This function is called from the worker thread when there is a slow-path
4075 * response IOCB to process. This function chains all the response iocbs until
4076 * seeing the iocb with the LE bit set. The function will call
4077 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
4078 * completion of a command iocb. The function will call the
4079 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
4080 * The function frees the resources or calls the completion handler if this
4081 * iocb is an abort completion. The function returns NULL when the response
4082 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
4083 * this function shall chain the iocb on to the iocb_continueq and return the
4084 * response iocb passed in.
4085 **/
4086static struct lpfc_iocbq *
4087lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4088 struct lpfc_iocbq *rspiocbp)
4089{
4090 struct lpfc_iocbq *saveq;
4091 struct lpfc_iocbq *cmdiocbp;
4092 struct lpfc_iocbq *next_iocb;
4093 IOCB_t *irsp = NULL;
4094 uint32_t free_saveq;
4095 uint8_t iocb_cmd_type;
4096 lpfc_iocb_type type;
4097 unsigned long iflag;
4098 int rc;
4099
4100 spin_lock_irqsave(&phba->hbalock, iflag);
4101 /* First add the response iocb to the countinueq list */
4102 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
4103 pring->iocb_continueq_cnt++;
4104
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02004105 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04004106 irsp = &rspiocbp->iocb;
4107 if (irsp->ulpLe) {
4108 /*
4109 * By default, the driver expects to free all resources
4110 * associated with this iocb completion.
4111 */
4112 free_saveq = 1;
4113 saveq = list_get_first(&pring->iocb_continueq,
4114 struct lpfc_iocbq, list);
4115 irsp = &(saveq->iocb);
4116 list_del_init(&pring->iocb_continueq);
4117 pring->iocb_continueq_cnt = 0;
4118
4119 pring->stats.iocb_rsp++;
4120
4121 /*
4122 * If resource errors reported from HBA, reduce
4123 * queuedepths of the SCSI device.
4124 */
4125 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04004126 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
4127 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04004128 spin_unlock_irqrestore(&phba->hbalock, iflag);
4129 phba->lpfc_rampdown_queue_depth(phba);
4130 spin_lock_irqsave(&phba->hbalock, iflag);
4131 }
4132
4133 if (irsp->ulpStatus) {
4134 /* Rsp ring <ringno> error: IOCB */
4135 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4136 "0328 Rsp Ring %d error: "
4137 "IOCB Data: "
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 "
4141 "x%x x%x x%x x%x\n",
4142 pring->ringno,
4143 irsp->un.ulpWord[0],
4144 irsp->un.ulpWord[1],
4145 irsp->un.ulpWord[2],
4146 irsp->un.ulpWord[3],
4147 irsp->un.ulpWord[4],
4148 irsp->un.ulpWord[5],
4149 *(((uint32_t *) irsp) + 6),
4150 *(((uint32_t *) irsp) + 7),
4151 *(((uint32_t *) irsp) + 8),
4152 *(((uint32_t *) irsp) + 9),
4153 *(((uint32_t *) irsp) + 10),
4154 *(((uint32_t *) irsp) + 11),
4155 *(((uint32_t *) irsp) + 12),
4156 *(((uint32_t *) irsp) + 13),
4157 *(((uint32_t *) irsp) + 14),
4158 *(((uint32_t *) irsp) + 15));
4159 }
4160
4161 /*
4162 * Fetch the IOCB command type and call the correct completion
4163 * routine. Solicited and Unsolicited IOCBs on the ELS ring
4164 * get freed back to the lpfc_iocb_list by the discovery
4165 * kernel thread.
4166 */
4167 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
4168 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
4169 switch (type) {
4170 case LPFC_SOL_IOCB:
4171 spin_unlock_irqrestore(&phba->hbalock, iflag);
4172 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
4173 spin_lock_irqsave(&phba->hbalock, iflag);
4174 break;
4175
4176 case LPFC_UNSOL_IOCB:
4177 spin_unlock_irqrestore(&phba->hbalock, iflag);
4178 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
4179 spin_lock_irqsave(&phba->hbalock, iflag);
4180 if (!rc)
4181 free_saveq = 0;
4182 break;
4183
4184 case LPFC_ABORT_IOCB:
4185 cmdiocbp = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07004186 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX) {
4187 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04004188 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
4189 saveq);
James Smarte2a8be52019-05-06 17:26:47 -07004190 spin_lock_irqsave(&phba->hbalock, iflag);
4191 }
James Smart3772a992009-05-22 14:50:54 -04004192 if (cmdiocbp) {
4193 /* Call the specified completion routine */
4194 if (cmdiocbp->iocb_cmpl) {
4195 spin_unlock_irqrestore(&phba->hbalock,
4196 iflag);
4197 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
4198 saveq);
4199 spin_lock_irqsave(&phba->hbalock,
4200 iflag);
4201 } else
4202 __lpfc_sli_release_iocbq(phba,
4203 cmdiocbp);
4204 }
4205 break;
4206
4207 case LPFC_UNKNOWN_IOCB:
4208 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
4209 char adaptermsg[LPFC_MAX_ADPTMSG];
4210 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
4211 memcpy(&adaptermsg[0], (uint8_t *)irsp,
4212 MAX_MSG_DATA);
4213 dev_warn(&((phba->pcidev)->dev),
4214 "lpfc%d: %s\n",
4215 phba->brd_no, adaptermsg);
4216 } else {
4217 /* Unknown IOCB command */
Dick Kennedy372c1872020-06-30 14:50:00 -07004218 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04004219 "0335 Unknown IOCB "
4220 "command Data: x%x "
4221 "x%x x%x x%x\n",
4222 irsp->ulpCommand,
4223 irsp->ulpStatus,
4224 irsp->ulpIoTag,
4225 irsp->ulpContext);
4226 }
4227 break;
4228 }
4229
4230 if (free_saveq) {
4231 list_for_each_entry_safe(rspiocbp, next_iocb,
4232 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04004233 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04004234 __lpfc_sli_release_iocbq(phba, rspiocbp);
4235 }
4236 __lpfc_sli_release_iocbq(phba, saveq);
4237 }
4238 rspiocbp = NULL;
4239 }
4240 spin_unlock_irqrestore(&phba->hbalock, iflag);
4241 return rspiocbp;
4242}
4243
4244/**
4245 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04004246 * @phba: Pointer to HBA context object.
4247 * @pring: Pointer to driver SLI ring object.
4248 * @mask: Host attention register mask for this ring.
4249 *
James Smart3772a992009-05-22 14:50:54 -04004250 * This routine wraps the actual slow_ring event process routine from the
4251 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04004252 **/
James Smart3772a992009-05-22 14:50:54 -04004253void
James Smart2e0fef82007-06-17 19:56:36 -05004254lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
4255 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05004256{
James Smart3772a992009-05-22 14:50:54 -04004257 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
4258}
4259
4260/**
4261 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
4262 * @phba: Pointer to HBA context object.
4263 * @pring: Pointer to driver SLI ring object.
4264 * @mask: Host attention register mask for this ring.
4265 *
4266 * This function is called from the worker thread when there is a ring event
4267 * for non-fcp rings. The caller does not hold any lock. The function will
4268 * remove each response iocb in the response ring and calls the handle
4269 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4270 **/
4271static void
4272lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
4273 struct lpfc_sli_ring *pring, uint32_t mask)
4274{
James Smart34b02dc2008-08-24 21:49:55 -04004275 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05004276 IOCB_t *entry;
4277 IOCB_t *irsp = NULL;
4278 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05004279 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05004280 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04004281 uint32_t status;
dea31012005-04-17 16:05:31 -05004282
James Smart34b02dc2008-08-24 21:49:55 -04004283 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05004284 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05004285 pring->stats.iocb_event++;
4286
dea31012005-04-17 16:05:31 -05004287 /*
4288 * The next available response entry should never exceed the maximum
4289 * entries. If it does, treat it as an adapter hardware error.
4290 */
James Smart7e56aa22012-08-03 12:35:34 -04004291 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05004292 portRspPut = le32_to_cpu(pgp->rspPutInx);
4293 if (portRspPut >= portRspMax) {
4294 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004295 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05004296 * rsp ring <portRspMax>
4297 */
Dick Kennedy372c1872020-06-30 14:50:00 -07004298 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04004299 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004300 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04004301 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05004302
James Smart2e0fef82007-06-17 19:56:36 -05004303 phba->link_state = LPFC_HBA_ERROR;
4304 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05004305
4306 phba->work_hs = HS_FFER3;
4307 lpfc_handle_eratt(phba);
4308
James Smart3772a992009-05-22 14:50:54 -04004309 return;
dea31012005-04-17 16:05:31 -05004310 }
4311
4312 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04004313 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05004314 /*
4315 * Build a completion list and call the appropriate handler.
4316 * The process is to get the next available response iocb, get
4317 * a free iocb from the list, copy the response data into the
4318 * free iocb, insert to the continuation list, and update the
4319 * next response index to slim. This process makes response
4320 * iocb's in the ring available to DMA as fast as possible but
4321 * pays a penalty for a copy operation. Since the iocb is
4322 * only 32 bytes, this penalty is considered small relative to
4323 * the PCI reads for register values and a slim write. When
4324 * the ulpLe field is set, the entire Command has been
4325 * received.
4326 */
James Smarted957682007-06-17 19:56:37 -05004327 entry = lpfc_resp_iocb(phba, pring);
4328
James Smart858c9f62007-06-17 19:56:39 -05004329 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05004330 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004331 if (rspiocbp == NULL) {
4332 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004333 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05004334 break;
4335 }
4336
James Smarted957682007-06-17 19:56:37 -05004337 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
4338 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05004339 irsp = &rspiocbp->iocb;
4340
James Smart7e56aa22012-08-03 12:35:34 -04004341 if (++pring->sli.sli3.rspidx >= portRspMax)
4342 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05004343
James Smarta58cbd52007-08-02 11:09:43 -04004344 if (pring->ringno == LPFC_ELS_RING) {
4345 lpfc_debugfs_slow_ring_trc(phba,
4346 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
4347 *(((uint32_t *) irsp) + 4),
4348 *(((uint32_t *) irsp) + 6),
4349 *(((uint32_t *) irsp) + 7));
4350 }
4351
James Smart7e56aa22012-08-03 12:35:34 -04004352 writel(pring->sli.sli3.rspidx,
4353 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05004354
James Smart3772a992009-05-22 14:50:54 -04004355 spin_unlock_irqrestore(&phba->hbalock, iflag);
4356 /* Handle the response IOCB */
4357 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
4358 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05004359
4360 /*
4361 * If the port response put pointer has not been updated, sync
4362 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
4363 * response put pointer.
4364 */
James Smart7e56aa22012-08-03 12:35:34 -04004365 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05004366 portRspPut = le32_to_cpu(pgp->rspPutInx);
4367 }
James Smart7e56aa22012-08-03 12:35:34 -04004368 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05004369
James Smart92d7f7b2007-06-17 19:56:38 -05004370 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05004371 /* At least one response entry has been freed */
4372 pring->stats.iocb_rsp_full++;
4373 /* SET RxRE_RSP in Chip Att register */
4374 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4375 writel(status, phba->CAregaddr);
4376 readl(phba->CAregaddr); /* flush */
4377 }
4378 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4379 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4380 pring->stats.iocb_cmd_empty++;
4381
4382 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04004383 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05004384 lpfc_sli_resume_iocb(phba, pring);
4385
4386 if ((pring->lpfc_sli_cmd_available))
4387 (pring->lpfc_sli_cmd_available) (phba, pring);
4388
4389 }
4390
James Smart2e0fef82007-06-17 19:56:36 -05004391 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04004392 return;
dea31012005-04-17 16:05:31 -05004393}
4394
James Smarte59058c2008-08-24 21:49:00 -04004395/**
James Smart4f774512009-05-22 14:52:35 -04004396 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
4397 * @phba: Pointer to HBA context object.
4398 * @pring: Pointer to driver SLI ring object.
4399 * @mask: Host attention register mask for this ring.
4400 *
4401 * This function is called from the worker thread when there is a pending
4402 * ELS response iocb on the driver internal slow-path response iocb worker
4403 * queue. The caller does not hold any lock. The function will remove each
4404 * response iocb from the response worker queue and calls the handle
4405 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4406 **/
4407static void
4408lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
4409 struct lpfc_sli_ring *pring, uint32_t mask)
4410{
4411 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04004412 struct hbq_dmabuf *dmabuf;
4413 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04004414 unsigned long iflag;
James Smart0ef01a22018-09-10 10:30:45 -07004415 int count = 0;
James Smart4f774512009-05-22 14:52:35 -04004416
James Smart45ed1192009-10-02 15:17:02 -04004417 spin_lock_irqsave(&phba->hbalock, iflag);
4418 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
4419 spin_unlock_irqrestore(&phba->hbalock, iflag);
4420 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04004421 /* Get the response iocb from the head of work queue */
4422 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04004423 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04004424 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04004425 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04004426
4427 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
4428 case CQE_CODE_COMPL_WQE:
4429 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
4430 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04004431 /* Translate ELS WCQE to response IOCBQ */
4432 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
4433 irspiocbq);
4434 if (irspiocbq)
4435 lpfc_sli_sp_handle_rspiocb(phba, pring,
4436 irspiocbq);
James Smart0ef01a22018-09-10 10:30:45 -07004437 count++;
James Smart4d9ab992009-10-02 15:16:39 -04004438 break;
4439 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04004440 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04004441 dmabuf = container_of(cq_event, struct hbq_dmabuf,
4442 cq_event);
4443 lpfc_sli4_handle_received_buffer(phba, dmabuf);
James Smart0ef01a22018-09-10 10:30:45 -07004444 count++;
James Smart4d9ab992009-10-02 15:16:39 -04004445 break;
4446 default:
4447 break;
4448 }
James Smart0ef01a22018-09-10 10:30:45 -07004449
4450 /* Limit the number of events to 64 to avoid soft lockups */
4451 if (count == 64)
4452 break;
James Smart4f774512009-05-22 14:52:35 -04004453 }
4454}
4455
4456/**
James Smart3621a712009-04-06 18:47:14 -04004457 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04004458 * @phba: Pointer to HBA context object.
4459 * @pring: Pointer to driver SLI ring object.
4460 *
4461 * This function aborts all iocbs in the given ring and frees all the iocb
4462 * objects in txq. This function issues an abort iocb for all the iocb commands
4463 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4464 * the return of this function. The caller is not required to hold any locks.
4465 **/
James Smart2e0fef82007-06-17 19:56:36 -05004466void
dea31012005-04-17 16:05:31 -05004467lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
4468{
James Smart2534ba72007-04-25 09:52:20 -04004469 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004470 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05004471
James Smart92d7f7b2007-06-17 19:56:38 -05004472 if (pring->ringno == LPFC_ELS_RING) {
4473 lpfc_fabric_abort_hba(phba);
4474 }
4475
dea31012005-04-17 16:05:31 -05004476 /* Error everything on txq and txcmplq
4477 * First do the txq.
4478 */
James Smartdb55fba2014-04-04 13:52:02 -04004479 if (phba->sli_rev >= LPFC_SLI_REV4) {
4480 spin_lock_irq(&pring->ring_lock);
4481 list_splice_init(&pring->txq, &completions);
4482 pring->txq_cnt = 0;
4483 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05004484
James Smartdb55fba2014-04-04 13:52:02 -04004485 spin_lock_irq(&phba->hbalock);
4486 /* Next issue ABTS for everything on the txcmplq */
4487 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
James Smartdb7531d2020-11-15 11:26:44 -08004488 lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
James Smartdb55fba2014-04-04 13:52:02 -04004489 spin_unlock_irq(&phba->hbalock);
4490 } else {
4491 spin_lock_irq(&phba->hbalock);
4492 list_splice_init(&pring->txq, &completions);
4493 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04004494
James Smartdb55fba2014-04-04 13:52:02 -04004495 /* Next issue ABTS for everything on the txcmplq */
4496 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
James Smartdb7531d2020-11-15 11:26:44 -08004497 lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
James Smartdb55fba2014-04-04 13:52:02 -04004498 spin_unlock_irq(&phba->hbalock);
4499 }
James Smarta22d73b2021-01-04 10:02:38 -08004500 /* Make sure HBA is alive */
4501 lpfc_issue_hb_tmo(phba);
James Smart2534ba72007-04-25 09:52:20 -04004502
James Smarta257bf92009-04-06 18:48:10 -04004503 /* Cancel all the IOCBs from the completions list */
4504 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4505 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004506}
4507
James Smarte59058c2008-08-24 21:49:00 -04004508/**
James Smartdb55fba2014-04-04 13:52:02 -04004509 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
4510 * @phba: Pointer to HBA context object.
James Smartdb55fba2014-04-04 13:52:02 -04004511 *
4512 * This function aborts all iocbs in FCP rings and frees all the iocb
4513 * objects in txq. This function issues an abort iocb for all the iocb commands
4514 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4515 * the return of this function. The caller is not required to hold any locks.
4516 **/
4517void
4518lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
4519{
4520 struct lpfc_sli *psli = &phba->sli;
4521 struct lpfc_sli_ring *pring;
4522 uint32_t i;
4523
4524 /* Look on all the FCP Rings for the iotag */
4525 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004526 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07004527 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smartdb55fba2014-04-04 13:52:02 -04004528 lpfc_sli_abort_iocb_ring(phba, pring);
4529 }
4530 } else {
James Smart895427b2017-02-12 13:52:30 -08004531 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004532 lpfc_sli_abort_iocb_ring(phba, pring);
4533 }
4534}
4535
James Smart895427b2017-02-12 13:52:30 -08004536/**
James Smartc00f62e2019-08-14 16:57:11 -07004537 * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
James Smarta8e497d2008-08-24 21:50:11 -04004538 * @phba: Pointer to HBA context object.
4539 *
James Smartc00f62e2019-08-14 16:57:11 -07004540 * This function flushes all iocbs in the IO ring and frees all the iocb
James Smarta8e497d2008-08-24 21:50:11 -04004541 * objects in txq and txcmplq. This function will not issue abort iocbs
4542 * for all the iocb commands in txcmplq, they will just be returned with
4543 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4544 * slot has been permanently disabled.
4545 **/
4546void
James Smartc00f62e2019-08-14 16:57:11 -07004547lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
James Smarta8e497d2008-08-24 21:50:11 -04004548{
4549 LIST_HEAD(txq);
4550 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04004551 struct lpfc_sli *psli = &phba->sli;
4552 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04004553 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004554 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04004555
4556 spin_lock_irq(&phba->hbalock);
James Smart4cd70892020-03-22 11:12:57 -07004557 if (phba->hba_flag & HBA_IOQ_FLUSH ||
4558 !phba->sli4_hba.hdwq) {
4559 spin_unlock_irq(&phba->hbalock);
4560 return;
4561 }
James Smart4f2e66c2012-05-09 21:17:07 -04004562 /* Indicate the I/O queues are flushed */
James Smartc00f62e2019-08-14 16:57:11 -07004563 phba->hba_flag |= HBA_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04004564 spin_unlock_irq(&phba->hbalock);
4565
James Smartdb55fba2014-04-04 13:52:02 -04004566 /* Look on all the FCP Rings for the iotag */
4567 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004568 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07004569 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smarta8e497d2008-08-24 21:50:11 -04004570
James Smartdb55fba2014-04-04 13:52:02 -04004571 spin_lock_irq(&pring->ring_lock);
4572 /* Retrieve everything on txq */
4573 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004574 list_for_each_entry_safe(piocb, next_iocb,
4575 &pring->txcmplq, list)
4576 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004577 /* Retrieve everything on the txcmplq */
4578 list_splice_init(&pring->txcmplq, &txcmplq);
4579 pring->txq_cnt = 0;
4580 pring->txcmplq_cnt = 0;
4581 spin_unlock_irq(&pring->ring_lock);
4582
4583 /* Flush the txq */
4584 lpfc_sli_cancel_iocbs(phba, &txq,
4585 IOSTAT_LOCAL_REJECT,
4586 IOERR_SLI_DOWN);
James Smart25ac2c972021-09-10 16:31:54 -07004587 /* Flush the txcmplq */
James Smartdb55fba2014-04-04 13:52:02 -04004588 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4589 IOSTAT_LOCAL_REJECT,
4590 IOERR_SLI_DOWN);
James Smart25ac2c972021-09-10 16:31:54 -07004591 if (unlikely(pci_channel_offline(phba->pcidev)))
4592 lpfc_sli4_io_xri_aborted(phba, NULL, 0);
James Smartdb55fba2014-04-04 13:52:02 -04004593 }
4594 } else {
James Smart895427b2017-02-12 13:52:30 -08004595 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004596
4597 spin_lock_irq(&phba->hbalock);
4598 /* Retrieve everything on txq */
4599 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004600 list_for_each_entry_safe(piocb, next_iocb,
4601 &pring->txcmplq, list)
4602 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004603 /* Retrieve everything on the txcmplq */
4604 list_splice_init(&pring->txcmplq, &txcmplq);
4605 pring->txq_cnt = 0;
4606 pring->txcmplq_cnt = 0;
4607 spin_unlock_irq(&phba->hbalock);
4608
4609 /* Flush the txq */
4610 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4611 IOERR_SLI_DOWN);
4612 /* Flush the txcmpq */
4613 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4614 IOERR_SLI_DOWN);
4615 }
James Smarta8e497d2008-08-24 21:50:11 -04004616}
4617
4618/**
James Smart3772a992009-05-22 14:50:54 -04004619 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004620 * @phba: Pointer to HBA context object.
4621 * @mask: Bit mask to be checked.
4622 *
4623 * This function reads the host status register and compares
4624 * with the provided bit mask to check if HBA completed
4625 * the restart. This function will wait in a loop for the
4626 * HBA to complete restart. If the HBA does not restart within
4627 * 15 iterations, the function will reset the HBA again. The
4628 * function returns 1 when HBA fail to restart otherwise returns
4629 * zero.
4630 **/
James Smart3772a992009-05-22 14:50:54 -04004631static int
4632lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004633{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004634 uint32_t status;
4635 int i = 0;
4636 int retval = 0;
dea31012005-04-17 16:05:31 -05004637
Jamie Wellnitz41415862006-02-28 19:25:27 -05004638 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004639 if (lpfc_readl(phba->HSregaddr, &status))
4640 return 1;
dea31012005-04-17 16:05:31 -05004641
James Smartd2f25472021-01-04 10:02:27 -08004642 phba->hba_flag |= HBA_NEEDS_CFG_PORT;
4643
Jamie Wellnitz41415862006-02-28 19:25:27 -05004644 /*
4645 * Check status register every 100ms for 5 retries, then every
4646 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4647 * every 2.5 sec for 4.
4648 * Break our of the loop if errors occurred during init.
4649 */
4650 while (((status & mask) != mask) &&
4651 !(status & HS_FFERM) &&
4652 i++ < 20) {
dea31012005-04-17 16:05:31 -05004653
Jamie Wellnitz41415862006-02-28 19:25:27 -05004654 if (i <= 5)
4655 msleep(10);
4656 else if (i <= 10)
4657 msleep(500);
4658 else
4659 msleep(2500);
dea31012005-04-17 16:05:31 -05004660
Jamie Wellnitz41415862006-02-28 19:25:27 -05004661 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004662 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004663 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004664 lpfc_sli_brdrestart(phba);
4665 }
4666 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004667 if (lpfc_readl(phba->HSregaddr, &status)) {
4668 retval = 1;
4669 break;
4670 }
dea31012005-04-17 16:05:31 -05004671 }
dea31012005-04-17 16:05:31 -05004672
Jamie Wellnitz41415862006-02-28 19:25:27 -05004673 /* Check to see if any errors occurred during init */
4674 if ((status & HS_FFERM) || (i >= 20)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004675 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte40a02c2010-02-26 14:13:54 -05004676 "2751 Adapter failed to restart, "
4677 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4678 status,
4679 readl(phba->MBslimaddr + 0xa8),
4680 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004681 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004682 retval = 1;
4683 }
dea31012005-04-17 16:05:31 -05004684
Jamie Wellnitz41415862006-02-28 19:25:27 -05004685 return retval;
dea31012005-04-17 16:05:31 -05004686}
4687
James Smartda0436e2009-05-22 14:51:39 -04004688/**
4689 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4690 * @phba: Pointer to HBA context object.
4691 * @mask: Bit mask to be checked.
4692 *
4693 * This function checks the host status register to check if HBA is
4694 * ready. This function will wait in a loop for the HBA to be ready
4695 * If the HBA is not ready , the function will will reset the HBA PCI
4696 * function again. The function returns 1 when HBA fail to be ready
4697 * otherwise returns zero.
4698 **/
4699static int
4700lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4701{
4702 uint32_t status;
4703 int retval = 0;
4704
4705 /* Read the HBA Host Status Register */
4706 status = lpfc_sli4_post_status_check(phba);
4707
4708 if (status) {
4709 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4710 lpfc_sli_brdrestart(phba);
4711 status = lpfc_sli4_post_status_check(phba);
4712 }
4713
4714 /* Check to see if any errors occurred during init */
4715 if (status) {
4716 phba->link_state = LPFC_HBA_ERROR;
4717 retval = 1;
4718 } else
4719 phba->sli4_hba.intr_enable = 0;
4720
James Smart02243832021-08-16 09:28:54 -07004721 phba->hba_flag &= ~HBA_SETUP;
James Smartda0436e2009-05-22 14:51:39 -04004722 return retval;
4723}
4724
4725/**
4726 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4727 * @phba: Pointer to HBA context object.
4728 * @mask: Bit mask to be checked.
4729 *
4730 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4731 * from the API jump table function pointer from the lpfc_hba struct.
4732 **/
4733int
4734lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4735{
4736 return phba->lpfc_sli_brdready(phba, mask);
4737}
4738
James Smart92908312006-03-07 15:04:13 -05004739#define BARRIER_TEST_PATTERN (0xdeadbeef)
4740
James Smarte59058c2008-08-24 21:49:00 -04004741/**
James Smart3621a712009-04-06 18:47:14 -04004742 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004743 * @phba: Pointer to HBA context object.
4744 *
James Smart1b511972011-12-13 13:23:09 -05004745 * This function is called before resetting an HBA. This function is called
4746 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004747 **/
James Smart2e0fef82007-06-17 19:56:36 -05004748void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004749{
James Smart65a29c12006-07-06 15:50:50 -04004750 uint32_t __iomem *resp_buf;
4751 uint32_t __iomem *mbox_buf;
Kees Cookc167dd02021-12-03 14:33:51 -08004752 volatile struct MAILBOX_word0 mbox;
James Smart9940b972011-03-11 16:06:12 -05004753 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004754 int i;
4755 uint8_t hdrtype;
4756
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004757 lockdep_assert_held(&phba->hbalock);
4758
James Smart92908312006-03-07 15:04:13 -05004759 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4760 if (hdrtype != 0x80 ||
4761 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4762 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4763 return;
4764
4765 /*
4766 * Tell the other part of the chip to suspend temporarily all
4767 * its DMA activity.
4768 */
James Smart65a29c12006-07-06 15:50:50 -04004769 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004770
4771 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004772 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4773 return;
James Smart92908312006-03-07 15:04:13 -05004774 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4775 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004776 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004777
James Smart9940b972011-03-11 16:06:12 -05004778 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4779 return;
4780 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004781 /* Clear Chip error bit */
4782 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004783 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004784 }
4785
Kees Cookc167dd02021-12-03 14:33:51 -08004786 mbox.word0 = 0;
4787 mbox.mbxCommand = MBX_KILL_BOARD;
4788 mbox.mbxOwner = OWN_CHIP;
James Smart92908312006-03-07 15:04:13 -05004789
4790 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004791 mbox_buf = phba->MBslimaddr;
Kees Cookc167dd02021-12-03 14:33:51 -08004792 writel(mbox.word0, mbox_buf);
James Smart92908312006-03-07 15:04:13 -05004793
James Smart9940b972011-03-11 16:06:12 -05004794 for (i = 0; i < 50; i++) {
4795 if (lpfc_readl((resp_buf + 1), &resp_data))
4796 return;
4797 if (resp_data != ~(BARRIER_TEST_PATTERN))
4798 mdelay(1);
4799 else
4800 break;
4801 }
4802 resp_data = 0;
4803 if (lpfc_readl((resp_buf + 1), &resp_data))
4804 return;
4805 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004806 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004807 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004808 goto restore_hc;
4809 else
4810 goto clear_errat;
4811 }
4812
Kees Cookc167dd02021-12-03 14:33:51 -08004813 mbox.mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004814 resp_data = 0;
4815 for (i = 0; i < 500; i++) {
4816 if (lpfc_readl(resp_buf, &resp_data))
4817 return;
Kees Cookc167dd02021-12-03 14:33:51 -08004818 if (resp_data != mbox.word0)
James Smart9940b972011-03-11 16:06:12 -05004819 mdelay(1);
4820 else
4821 break;
4822 }
James Smart92908312006-03-07 15:04:13 -05004823
4824clear_errat:
4825
James Smart9940b972011-03-11 16:06:12 -05004826 while (++i < 500) {
4827 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4828 return;
4829 if (!(ha_copy & HA_ERATT))
4830 mdelay(1);
4831 else
4832 break;
4833 }
James Smart92908312006-03-07 15:04:13 -05004834
4835 if (readl(phba->HAregaddr) & HA_ERATT) {
4836 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004837 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004838 }
4839
4840restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004841 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004842 writel(hc_copy, phba->HCregaddr);
4843 readl(phba->HCregaddr); /* flush */
4844}
4845
James Smarte59058c2008-08-24 21:49:00 -04004846/**
James Smart3621a712009-04-06 18:47:14 -04004847 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004848 * @phba: Pointer to HBA context object.
4849 *
4850 * This function issues a kill_board mailbox command and waits for
4851 * the error attention interrupt. This function is called for stopping
4852 * the firmware processing. The caller is not required to hold any
4853 * locks. This function calls lpfc_hba_down_post function to free
4854 * any pending commands after the kill. The function will return 1 when it
4855 * fails to kill the board else will return 0.
4856 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004857int
James Smart2e0fef82007-06-17 19:56:36 -05004858lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004859{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004860 struct lpfc_sli *psli;
4861 LPFC_MBOXQ_t *pmb;
4862 uint32_t status;
4863 uint32_t ha_copy;
4864 int retval;
4865 int i = 0;
4866
4867 psli = &phba->sli;
4868
4869 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004870 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004871 "0329 Kill HBA Data: x%x x%x\n",
4872 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004873
James Smart98c9ea52007-10-27 13:37:33 -04004874 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4875 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004876 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004877
4878 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004879 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004880 if (lpfc_readl(phba->HCregaddr, &status)) {
4881 spin_unlock_irq(&phba->hbalock);
4882 mempool_free(pmb, phba->mbox_mem_pool);
4883 return 1;
4884 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004885 status &= ~HC_ERINT_ENA;
4886 writel(status, phba->HCregaddr);
4887 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004888 phba->link_flag |= LS_IGNORE_ERATT;
4889 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004890
4891 lpfc_kill_board(phba, pmb);
4892 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4893 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4894
4895 if (retval != MBX_SUCCESS) {
4896 if (retval != MBX_BUSY)
4897 mempool_free(pmb, phba->mbox_mem_pool);
Dick Kennedy372c1872020-06-30 14:50:00 -07004898 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte40a02c2010-02-26 14:13:54 -05004899 "2752 KILL_BOARD command failed retval %d\n",
4900 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004901 spin_lock_irq(&phba->hbalock);
4902 phba->link_flag &= ~LS_IGNORE_ERATT;
4903 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004904 return 1;
4905 }
4906
James Smartf4b4c682009-05-22 14:53:12 -04004907 spin_lock_irq(&phba->hbalock);
4908 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4909 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004910
Jamie Wellnitz41415862006-02-28 19:25:27 -05004911 mempool_free(pmb, phba->mbox_mem_pool);
4912
4913 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4914 * attention every 100ms for 3 seconds. If we don't get ERATT after
4915 * 3 seconds we still set HBA_ERROR state because the status of the
4916 * board is now undefined.
4917 */
James Smart9940b972011-03-11 16:06:12 -05004918 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4919 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004920 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4921 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004922 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4923 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004924 }
4925
4926 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004927 if (ha_copy & HA_ERATT) {
4928 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004929 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004930 }
James Smart2e0fef82007-06-17 19:56:36 -05004931 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004932 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004933 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004934 phba->link_flag &= ~LS_IGNORE_ERATT;
4935 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004936
Jamie Wellnitz41415862006-02-28 19:25:27 -05004937 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004938 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004939
James Smart2e0fef82007-06-17 19:56:36 -05004940 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004941}
4942
James Smarte59058c2008-08-24 21:49:00 -04004943/**
James Smart3772a992009-05-22 14:50:54 -04004944 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004945 * @phba: Pointer to HBA context object.
4946 *
4947 * This function resets the HBA by writing HC_INITFF to the control
4948 * register. After the HBA resets, this function resets all the iocb ring
4949 * indices. This function disables PCI layer parity checking during
4950 * the reset.
4951 * This function returns 0 always.
4952 * The caller is not required to hold any locks.
4953 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004954int
James Smart2e0fef82007-06-17 19:56:36 -05004955lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004956{
4957 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004958 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004959 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004960 int i;
dea31012005-04-17 16:05:31 -05004961
Jamie Wellnitz41415862006-02-28 19:25:27 -05004962 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004963
Jamie Wellnitz41415862006-02-28 19:25:27 -05004964 /* Reset HBA */
4965 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004966 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004967 (phba->pport) ? phba->pport->port_state : 0,
4968 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004969
4970 /* perform board reset */
4971 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004972 phba->link_events = 0;
James Smartd2f25472021-01-04 10:02:27 -08004973 phba->hba_flag |= HBA_NEEDS_CFG_PORT;
James Smart4492b732017-04-27 15:08:26 -07004974 if (phba->pport) {
4975 phba->pport->fc_myDID = 0;
4976 phba->pport->fc_prevDID = 0;
4977 }
dea31012005-04-17 16:05:31 -05004978
Jamie Wellnitz41415862006-02-28 19:25:27 -05004979 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004980 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
4981 return -EIO;
4982
Jamie Wellnitz41415862006-02-28 19:25:27 -05004983 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4984 (cfg_value &
4985 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4986
James Smart3772a992009-05-22 14:50:54 -04004987 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4988
Jamie Wellnitz41415862006-02-28 19:25:27 -05004989 /* Now toggle INITFF bit in the Host Control Register */
4990 writel(HC_INITFF, phba->HCregaddr);
4991 mdelay(1);
4992 readl(phba->HCregaddr); /* flush */
4993 writel(0, phba->HCregaddr);
4994 readl(phba->HCregaddr); /* flush */
4995
4996 /* Restore PCI cmd register */
4997 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004998
4999 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05005000 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08005001 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05005002 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04005003 pring->sli.sli3.rspidx = 0;
5004 pring->sli.sli3.next_cmdidx = 0;
5005 pring->sli.sli3.local_getidx = 0;
5006 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05005007 pring->missbufcnt = 0;
5008 }
dea31012005-04-17 16:05:31 -05005009
James Smart2e0fef82007-06-17 19:56:36 -05005010 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005011 return 0;
5012}
5013
James Smarte59058c2008-08-24 21:49:00 -04005014/**
James Smartda0436e2009-05-22 14:51:39 -04005015 * lpfc_sli4_brdreset - Reset a sli-4 HBA
5016 * @phba: Pointer to HBA context object.
5017 *
5018 * This function resets a SLI4 HBA. This function disables PCI layer parity
5019 * checking during resets the device. The caller is not required to hold
5020 * any locks.
5021 *
James Smart8c24a4f2019-08-14 16:56:53 -07005022 * This function returns 0 on success else returns negative error code.
James Smartda0436e2009-05-22 14:51:39 -04005023 **/
5024int
5025lpfc_sli4_brdreset(struct lpfc_hba *phba)
5026{
5027 struct lpfc_sli *psli = &phba->sli;
5028 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04005029 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04005030
5031 /* Reset HBA */
5032 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04005033 "0295 Reset HBA Data: x%x x%x x%x\n",
5034 phba->pport->port_state, psli->sli_flag,
5035 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04005036
5037 /* perform board reset */
5038 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04005039 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04005040 phba->pport->fc_myDID = 0;
5041 phba->pport->fc_prevDID = 0;
James Smart02243832021-08-16 09:28:54 -07005042 phba->hba_flag &= ~HBA_SETUP;
James Smartda0436e2009-05-22 14:51:39 -04005043
James Smartda0436e2009-05-22 14:51:39 -04005044 spin_lock_irq(&phba->hbalock);
5045 psli->sli_flag &= ~(LPFC_PROCESS_LA);
5046 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04005047 spin_unlock_irq(&phba->hbalock);
5048
5049 /* Now physically reset the device */
5050 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5051 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05005052
5053 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07005054 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
5055 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5056 "3205 PCI read Config failed\n");
5057 return -EIO;
5058 }
5059
James Smartbe858b62010-12-15 17:57:20 -05005060 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
5061 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
5062
James Smart88318812012-09-29 11:29:29 -04005063 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04005064 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04005065
James Smartbe858b62010-12-15 17:57:20 -05005066 /* Restore PCI cmd register */
5067 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
5068
James Smart27b01b82012-05-09 21:19:44 -04005069 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005070}
5071
5072/**
5073 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04005074 * @phba: Pointer to HBA context object.
5075 *
5076 * This function is called in the SLI initialization code path to
5077 * restart the HBA. The caller is not required to hold any lock.
5078 * This function writes MBX_RESTART mailbox command to the SLIM and
5079 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
5080 * function to free any pending commands. The function enables
5081 * POST only during the first initialization. The function returns zero.
5082 * The function does not guarantee completion of MBX_RESTART mailbox
5083 * command before the return of this function.
5084 **/
James Smartda0436e2009-05-22 14:51:39 -04005085static int
5086lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05005087{
Kees Cookc167dd02021-12-03 14:33:51 -08005088 volatile struct MAILBOX_word0 mb;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005089 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005090 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04005091 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005092
James Smart2e0fef82007-06-17 19:56:36 -05005093 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05005094
James Smart0d878412009-10-02 15:16:56 -04005095 /* Take PCIe device Advanced Error Reporting (AER) state */
5096 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
5097
Jamie Wellnitz41415862006-02-28 19:25:27 -05005098 psli = &phba->sli;
5099
5100 /* Restart HBA */
5101 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04005102 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07005103 (phba->pport) ? phba->pport->port_state : 0,
5104 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05005105
Kees Cookc167dd02021-12-03 14:33:51 -08005106 mb.word0 = 0;
5107 mb.mbxCommand = MBX_RESTART;
5108 mb.mbxHc = 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005109
James Smart92908312006-03-07 15:04:13 -05005110 lpfc_reset_barrier(phba);
5111
Jamie Wellnitz41415862006-02-28 19:25:27 -05005112 to_slim = phba->MBslimaddr;
Kees Cookc167dd02021-12-03 14:33:51 -08005113 writel(mb.word0, to_slim);
Jamie Wellnitz41415862006-02-28 19:25:27 -05005114 readl(to_slim); /* flush */
5115
5116 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07005117 if (phba->pport && phba->pport->port_state)
Kees Cookc167dd02021-12-03 14:33:51 -08005118 mb.word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05005119 else
Kees Cookc167dd02021-12-03 14:33:51 -08005120 mb.word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04005121 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Kees Cookc167dd02021-12-03 14:33:51 -08005122 writel(mb.word0, to_slim);
Jamie Wellnitz41415862006-02-28 19:25:27 -05005123 readl(to_slim); /* flush */
5124
5125 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07005126 if (phba->pport)
5127 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005128 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04005129 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005130 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05005131
James Smart64ba8812006-08-02 15:24:34 -04005132 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02005133 psli->stats_start = ktime_get_seconds();
James Smart64ba8812006-08-02 15:24:34 -04005134
James Smarteaf15d52008-12-04 22:39:29 -05005135 /* Give the INITFF and Post time to settle. */
5136 mdelay(100);
dea31012005-04-17 16:05:31 -05005137
James Smart0d878412009-10-02 15:16:56 -04005138 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
5139 if (hba_aer_enabled)
5140 pci_disable_pcie_error_reporting(phba->pcidev);
5141
Jamie Wellnitz41415862006-02-28 19:25:27 -05005142 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05005143
5144 return 0;
5145}
5146
James Smarte59058c2008-08-24 21:49:00 -04005147/**
James Smartda0436e2009-05-22 14:51:39 -04005148 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
5149 * @phba: Pointer to HBA context object.
5150 *
5151 * This function is called in the SLI initialization code path to restart
5152 * a SLI4 HBA. The caller is not required to hold any lock.
5153 * At the end of the function, it calls lpfc_hba_down_post function to
5154 * free any pending commands.
5155 **/
5156static int
5157lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
5158{
5159 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04005160 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04005161 int rc;
James Smartda0436e2009-05-22 14:51:39 -04005162
5163 /* Restart HBA */
5164 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5165 "0296 Restart HBA Data: x%x x%x\n",
5166 phba->pport->port_state, psli->sli_flag);
5167
James Smart75baf692010-06-08 18:31:21 -04005168 /* Take PCIe device Advanced Error Reporting (AER) state */
5169 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
5170
James Smart27b01b82012-05-09 21:19:44 -04005171 rc = lpfc_sli4_brdreset(phba);
James Smart4fb86a62019-09-03 14:54:41 -07005172 if (rc) {
5173 phba->link_state = LPFC_HBA_ERROR;
5174 goto hba_down_queue;
5175 }
James Smartda0436e2009-05-22 14:51:39 -04005176
5177 spin_lock_irq(&phba->hbalock);
5178 phba->pport->stopped = 0;
5179 phba->link_state = LPFC_INIT_START;
5180 phba->hba_flag = 0;
5181 spin_unlock_irq(&phba->hbalock);
5182
5183 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02005184 psli->stats_start = ktime_get_seconds();
James Smartda0436e2009-05-22 14:51:39 -04005185
James Smart75baf692010-06-08 18:31:21 -04005186 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
5187 if (hba_aer_enabled)
5188 pci_disable_pcie_error_reporting(phba->pcidev);
5189
James Smart4fb86a62019-09-03 14:54:41 -07005190hba_down_queue:
James Smartda0436e2009-05-22 14:51:39 -04005191 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07005192 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04005193
James Smart27b01b82012-05-09 21:19:44 -04005194 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005195}
5196
5197/**
5198 * lpfc_sli_brdrestart - Wrapper func for restarting hba
5199 * @phba: Pointer to HBA context object.
5200 *
5201 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
5202 * API jump table function pointer from the lpfc_hba struct.
5203**/
5204int
5205lpfc_sli_brdrestart(struct lpfc_hba *phba)
5206{
5207 return phba->lpfc_sli_brdrestart(phba);
5208}
5209
5210/**
James Smart3621a712009-04-06 18:47:14 -04005211 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04005212 * @phba: Pointer to HBA context object.
5213 *
5214 * This function is called after a HBA restart to wait for successful
5215 * restart of the HBA. Successful restart of the HBA is indicated by
5216 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
5217 * iteration, the function will restart the HBA again. The function returns
5218 * zero if HBA successfully restarted else returns negative error code.
5219 **/
James Smart4492b732017-04-27 15:08:26 -07005220int
dea31012005-04-17 16:05:31 -05005221lpfc_sli_chipset_init(struct lpfc_hba *phba)
5222{
5223 uint32_t status, i = 0;
5224
5225 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05005226 if (lpfc_readl(phba->HSregaddr, &status))
5227 return -EIO;
dea31012005-04-17 16:05:31 -05005228
5229 /* Check status register to see what current state is */
5230 i = 0;
5231 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
5232
James Smartdcf2a4e2010-09-29 11:18:53 -04005233 /* Check every 10ms for 10 retries, then every 100ms for 90
5234 * retries, then every 1 sec for 50 retires for a total of
5235 * ~60 seconds before reset the board again and check every
5236 * 1 sec for 50 retries. The up to 60 seconds before the
5237 * board ready is required by the Falcon FIPS zeroization
5238 * complete, and any reset the board in between shall cause
5239 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05005240 */
James Smartdcf2a4e2010-09-29 11:18:53 -04005241 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05005242 /* Adapter failed to init, timeout, status reg
5243 <status> */
Dick Kennedy372c1872020-06-30 14:50:00 -07005244 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005245 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05005246 "timeout, status reg x%x, "
5247 "FW Data: A8 x%x AC x%x\n", status,
5248 readl(phba->MBslimaddr + 0xa8),
5249 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05005250 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005251 return -ETIMEDOUT;
5252 }
5253
5254 /* Check to see if any errors occurred during init */
5255 if (status & HS_FFERM) {
5256 /* ERROR: During chipset initialization */
5257 /* Adapter failed to init, chipset, status reg
5258 <status> */
Dick Kennedy372c1872020-06-30 14:50:00 -07005259 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005260 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05005261 "chipset, status reg x%x, "
5262 "FW Data: A8 x%x AC x%x\n", status,
5263 readl(phba->MBslimaddr + 0xa8),
5264 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05005265 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005266 return -EIO;
5267 }
5268
James Smartdcf2a4e2010-09-29 11:18:53 -04005269 if (i <= 10)
dea31012005-04-17 16:05:31 -05005270 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04005271 else if (i <= 100)
5272 msleep(100);
5273 else
5274 msleep(1000);
dea31012005-04-17 16:05:31 -05005275
James Smartdcf2a4e2010-09-29 11:18:53 -04005276 if (i == 150) {
5277 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05005278 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005279 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05005280 }
5281 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05005282 if (lpfc_readl(phba->HSregaddr, &status))
5283 return -EIO;
dea31012005-04-17 16:05:31 -05005284 }
5285
5286 /* Check to see if any errors occurred during init */
5287 if (status & HS_FFERM) {
5288 /* ERROR: During chipset initialization */
5289 /* Adapter failed to init, chipset, status reg <status> */
Dick Kennedy372c1872020-06-30 14:50:00 -07005290 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005291 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05005292 "status reg x%x, "
5293 "FW Data: A8 x%x AC x%x\n", status,
5294 readl(phba->MBslimaddr + 0xa8),
5295 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05005296 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005297 return -EIO;
5298 }
5299
James Smartd2f25472021-01-04 10:02:27 -08005300 phba->hba_flag |= HBA_NEEDS_CFG_PORT;
5301
dea31012005-04-17 16:05:31 -05005302 /* Clear all interrupt enable conditions */
5303 writel(0, phba->HCregaddr);
5304 readl(phba->HCregaddr); /* flush */
5305
5306 /* setup host attn register */
5307 writel(0xffffffff, phba->HAregaddr);
5308 readl(phba->HAregaddr); /* flush */
5309 return 0;
5310}
5311
James Smarte59058c2008-08-24 21:49:00 -04005312/**
James Smart3621a712009-04-06 18:47:14 -04005313 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04005314 *
5315 * This function calculates and returns the number of HBQs required to be
5316 * configured.
5317 **/
James Smart78b2d852007-08-02 11:10:21 -04005318int
James Smarted957682007-06-17 19:56:37 -05005319lpfc_sli_hbq_count(void)
5320{
James Smart92d7f7b2007-06-17 19:56:38 -05005321 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05005322}
5323
James Smarte59058c2008-08-24 21:49:00 -04005324/**
James Smart3621a712009-04-06 18:47:14 -04005325 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04005326 *
5327 * This function adds the number of hbq entries in every HBQ to get
5328 * the total number of hbq entries required for the HBA and returns
5329 * the total count.
5330 **/
James Smarted957682007-06-17 19:56:37 -05005331static int
5332lpfc_sli_hbq_entry_count(void)
5333{
5334 int hbq_count = lpfc_sli_hbq_count();
5335 int count = 0;
5336 int i;
5337
5338 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05005339 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05005340 return count;
5341}
5342
James Smarte59058c2008-08-24 21:49:00 -04005343/**
James Smart3621a712009-04-06 18:47:14 -04005344 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04005345 *
5346 * This function calculates amount of memory required for all hbq entries
5347 * to be configured and returns the total memory required.
5348 **/
dea31012005-04-17 16:05:31 -05005349int
James Smarted957682007-06-17 19:56:37 -05005350lpfc_sli_hbq_size(void)
5351{
5352 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
5353}
5354
James Smarte59058c2008-08-24 21:49:00 -04005355/**
James Smart3621a712009-04-06 18:47:14 -04005356 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04005357 * @phba: Pointer to HBA context object.
5358 *
5359 * This function is called during the SLI initialization to configure
5360 * all the HBQs and post buffers to the HBQ. The caller is not
5361 * required to hold any locks. This function will return zero if successful
5362 * else it will return negative error code.
5363 **/
James Smarted957682007-06-17 19:56:37 -05005364static int
5365lpfc_sli_hbq_setup(struct lpfc_hba *phba)
5366{
5367 int hbq_count = lpfc_sli_hbq_count();
5368 LPFC_MBOXQ_t *pmb;
5369 MAILBOX_t *pmbox;
5370 uint32_t hbqno;
5371 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05005372
James Smart92d7f7b2007-06-17 19:56:38 -05005373 /* Get a Mailbox buffer to setup mailbox
5374 * commands for HBA initialization
5375 */
James Smarted957682007-06-17 19:56:37 -05005376 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5377
5378 if (!pmb)
5379 return -ENOMEM;
5380
James Smart04c68492009-05-22 14:52:52 -04005381 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05005382
5383 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
5384 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05005385 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05005386
5387 hbq_entry_index = 0;
5388 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
5389 phba->hbqs[hbqno].next_hbqPutIdx = 0;
5390 phba->hbqs[hbqno].hbqPutIdx = 0;
5391 phba->hbqs[hbqno].local_hbqGetIdx = 0;
5392 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05005393 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04005394 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
5395 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05005396 hbq_entry_index += phba->hbqs[hbqno].entry_count;
5397
5398 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
5399 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
5400 mbxStatus <status>, ring <num> */
5401
5402 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05005403 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005404 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05005405 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005406 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05005407 pmbox->mbxStatus, hbqno);
5408
5409 phba->link_state = LPFC_HBA_ERROR;
5410 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04005411 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05005412 }
5413 }
5414 phba->hbq_count = hbq_count;
5415
James Smarted957682007-06-17 19:56:37 -05005416 mempool_free(pmb, phba->mbox_mem_pool);
5417
James Smart92d7f7b2007-06-17 19:56:38 -05005418 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04005419 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
5420 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05005421 return 0;
5422}
5423
James Smarte59058c2008-08-24 21:49:00 -04005424/**
James Smart4f774512009-05-22 14:52:35 -04005425 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
5426 * @phba: Pointer to HBA context object.
5427 *
5428 * This function is called during the SLI initialization to configure
5429 * all the HBQs and post buffers to the HBQ. The caller is not
5430 * required to hold any locks. This function will return zero if successful
5431 * else it will return negative error code.
5432 **/
5433static int
5434lpfc_sli4_rb_setup(struct lpfc_hba *phba)
5435{
5436 phba->hbq_in_use = 1;
James Smart999fbbc2019-12-18 15:58:06 -08005437 /**
5438 * Specific case when the MDS diagnostics is enabled and supported.
5439 * The receive buffer count is truncated to manage the incoming
5440 * traffic.
5441 **/
5442 if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
5443 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5444 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
5445 else
5446 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5447 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04005448 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08005449 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04005450 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04005451 return 0;
5452}
5453
5454/**
James Smart3621a712009-04-06 18:47:14 -04005455 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04005456 * @phba: Pointer to HBA context object.
5457 * @sli_mode: sli mode - 2/3
5458 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005459 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04005460 * to issue config_port mailbox command. This function restarts the
5461 * HBA firmware and issues a config_port mailbox command to configure
5462 * the SLI interface in the sli mode specified by sli_mode
5463 * variable. The caller is not required to hold any locks.
5464 * The function returns 0 if successful, else returns negative error
5465 * code.
5466 **/
James Smart93996272008-08-24 21:50:30 -04005467int
5468lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05005469{
5470 LPFC_MBOXQ_t *pmb;
5471 uint32_t resetcount = 0, rc = 0, done = 0;
5472
5473 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5474 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05005475 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005476 return -ENOMEM;
5477 }
5478
James Smarted957682007-06-17 19:56:37 -05005479 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05005480 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05005481 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04005482 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005483 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05005484 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005485 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05005486 rc = lpfc_sli_chipset_init(phba);
5487 if (rc)
5488 break;
5489
James Smart2e0fef82007-06-17 19:56:36 -05005490 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04005491 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005492 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005493 resetcount++;
5494
James Smarted957682007-06-17 19:56:37 -05005495 /* Call pre CONFIG_PORT mailbox command initialization. A
5496 * value of 0 means the call was successful. Any other
5497 * nonzero value is a failure, but if ERESTART is returned,
5498 * the driver may reset the HBA and try again.
5499 */
dea31012005-04-17 16:05:31 -05005500 rc = lpfc_config_port_prep(phba);
5501 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05005502 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05005503 continue;
James Smart34b02dc2008-08-24 21:49:55 -04005504 } else if (rc)
dea31012005-04-17 16:05:31 -05005505 break;
James Smart6d368e52011-05-24 11:44:12 -04005506
James Smart2e0fef82007-06-17 19:56:36 -05005507 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05005508 lpfc_config_port(phba, pmb);
5509 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04005510 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5511 LPFC_SLI3_HBQ_ENABLED |
5512 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04005513 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05005514 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005515 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005516 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05005517 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04005518 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005519 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04005520 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005521 spin_unlock_irq(&phba->hbalock);
5522 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04005523 } else {
5524 /* Allow asynchronous mailbox command to go through */
5525 spin_lock_irq(&phba->hbalock);
5526 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5527 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05005528 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05005529
5530 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5531 (pmb->u.mb.un.varCfgPort.gasabt == 0))
5532 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5533 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04005534 }
dea31012005-04-17 16:05:31 -05005535 }
James Smarted957682007-06-17 19:56:37 -05005536 if (!done) {
5537 rc = -EINVAL;
5538 goto do_prep_failed;
5539 }
James Smart04c68492009-05-22 14:52:52 -04005540 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5541 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04005542 rc = -ENXIO;
5543 goto do_prep_failed;
5544 }
James Smart04c68492009-05-22 14:52:52 -04005545 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04005546 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005547 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5548 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5549 phba->max_vpi : phba->max_vports;
5550
James Smart34b02dc2008-08-24 21:49:55 -04005551 } else
5552 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04005553 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005554 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005555 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005556 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005557
5558 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5559 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005560
James Smartf44ac122018-03-05 12:04:08 -08005561 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5562 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5563 phba->cfg_enable_bg = 0;
5564 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
Dick Kennedy372c1872020-06-30 14:50:00 -07005565 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte2a0a9d2008-12-04 22:40:02 -05005566 "0443 Adapter did not grant "
5567 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005568 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005569 }
James Smart34b02dc2008-08-24 21:49:55 -04005570 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005571 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005572 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005573 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005574 }
James Smart92d7f7b2007-06-17 19:56:38 -05005575do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005576 mempool_free(pmb, phba->mbox_mem_pool);
5577 return rc;
5578}
5579
James Smarte59058c2008-08-24 21:49:00 -04005580
5581/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005582 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005583 * @phba: Pointer to HBA context object.
5584 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005585 * This function is the main SLI initialization function. This function
5586 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005587 * error attention handler code. Caller is not required to hold any
5588 * locks. This function issues config_port mailbox command to configure
5589 * the SLI, setup iocb rings and HBQ rings. In the end the function
5590 * calls the config_port_post function to issue init_link mailbox
5591 * command and to start the discovery. The function will return zero
5592 * if successful, else it will return negative error code.
5593 **/
James Smarted957682007-06-17 19:56:37 -05005594int
5595lpfc_sli_hba_setup(struct lpfc_hba *phba)
5596{
5597 uint32_t rc;
James Smartd2f25472021-01-04 10:02:27 -08005598 int i;
James Smart6d368e52011-05-24 11:44:12 -04005599 int longs;
James Smarted957682007-06-17 19:56:37 -05005600
James Smartd2f25472021-01-04 10:02:27 -08005601 /* Enable ISR already does config_port because of config_msi mbx */
5602 if (phba->hba_flag & HBA_NEEDS_CFG_PORT) {
5603 rc = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
5604 if (rc)
5605 return -EIO;
5606 phba->hba_flag &= ~HBA_NEEDS_CFG_PORT;
James Smarted957682007-06-17 19:56:37 -05005607 }
James Smartb5c53952016-03-31 14:12:30 -07005608 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005609
James Smart0d878412009-10-02 15:16:56 -04005610 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5611 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5612 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5613 if (!rc) {
5614 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5615 "2709 This device supports "
5616 "Advanced Error Reporting (AER)\n");
5617 spin_lock_irq(&phba->hbalock);
5618 phba->hba_flag |= HBA_AER_ENABLED;
5619 spin_unlock_irq(&phba->hbalock);
5620 } else {
5621 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5622 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005623 "Advanced Error Reporting (AER): %d\n",
5624 rc);
James Smart0d878412009-10-02 15:16:56 -04005625 phba->cfg_aer_support = 0;
5626 }
5627 }
5628
James Smarted957682007-06-17 19:56:37 -05005629 if (phba->sli_rev == 3) {
5630 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5631 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005632 } else {
5633 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5634 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005635 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005636 }
5637
5638 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005639 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5640 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005641 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005642
5643 if (rc)
5644 goto lpfc_sli_hba_setup_error;
5645
James Smart6d368e52011-05-24 11:44:12 -04005646 /* Initialize VPIs. */
5647 if (phba->sli_rev == LPFC_SLI_REV3) {
5648 /*
5649 * The VPI bitmask and physical ID array are allocated
5650 * and initialized once only - at driver load. A port
5651 * reset doesn't need to reinitialize this memory.
5652 */
5653 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5654 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005655 phba->vpi_bmask = kcalloc(longs,
5656 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005657 GFP_KERNEL);
5658 if (!phba->vpi_bmask) {
5659 rc = -ENOMEM;
5660 goto lpfc_sli_hba_setup_error;
5661 }
5662
Kees Cook6396bb22018-06-12 14:03:40 -07005663 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5664 sizeof(uint16_t),
5665 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005666 if (!phba->vpi_ids) {
5667 kfree(phba->vpi_bmask);
5668 rc = -ENOMEM;
5669 goto lpfc_sli_hba_setup_error;
5670 }
5671 for (i = 0; i < phba->max_vpi; i++)
5672 phba->vpi_ids[i] = i;
5673 }
5674 }
5675
James Smart93996272008-08-24 21:50:30 -04005676 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005677 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5678 rc = lpfc_sli_hbq_setup(phba);
5679 if (rc)
5680 goto lpfc_sli_hba_setup_error;
5681 }
James Smart04c68492009-05-22 14:52:52 -04005682 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005683 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005684 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005685
5686 rc = lpfc_config_port_post(phba);
5687 if (rc)
5688 goto lpfc_sli_hba_setup_error;
5689
James Smarted957682007-06-17 19:56:37 -05005690 return rc;
5691
James Smart92d7f7b2007-06-17 19:56:38 -05005692lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005693 phba->link_state = LPFC_HBA_ERROR;
Dick Kennedy372c1872020-06-30 14:50:00 -07005694 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005695 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005696 return rc;
5697}
5698
James Smartda0436e2009-05-22 14:51:39 -04005699/**
5700 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5701 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01005702 *
James Smartda0436e2009-05-22 14:51:39 -04005703 * This function issue a dump mailbox command to read config region
5704 * 23 and parse the records in the region and populate driver
5705 * data structure.
5706 **/
5707static int
James Smartff78d8f2011-12-13 13:21:35 -05005708lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005709{
James Smartff78d8f2011-12-13 13:21:35 -05005710 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005711 struct lpfc_dmabuf *mp;
5712 struct lpfc_mqe *mqe;
5713 uint32_t data_length;
5714 int rc;
5715
5716 /* Program the default value of vlan_id and fc_map */
5717 phba->valid_vlan = 0;
5718 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5719 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5720 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5721
James Smartff78d8f2011-12-13 13:21:35 -05005722 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5723 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005724 return -ENOMEM;
5725
James Smartff78d8f2011-12-13 13:21:35 -05005726 mqe = &mboxq->u.mqe;
5727 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5728 rc = -ENOMEM;
5729 goto out_free_mboxq;
5730 }
5731
James Smart3e1f0712018-11-29 16:09:29 -08005732 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04005733 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5734
5735 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5736 "(%d):2571 Mailbox cmd x%x Status x%x "
5737 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5738 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5739 "CQ: x%x x%x x%x x%x\n",
5740 mboxq->vport ? mboxq->vport->vpi : 0,
5741 bf_get(lpfc_mqe_command, mqe),
5742 bf_get(lpfc_mqe_status, mqe),
5743 mqe->un.mb_words[0], mqe->un.mb_words[1],
5744 mqe->un.mb_words[2], mqe->un.mb_words[3],
5745 mqe->un.mb_words[4], mqe->un.mb_words[5],
5746 mqe->un.mb_words[6], mqe->un.mb_words[7],
5747 mqe->un.mb_words[8], mqe->un.mb_words[9],
5748 mqe->un.mb_words[10], mqe->un.mb_words[11],
5749 mqe->un.mb_words[12], mqe->un.mb_words[13],
5750 mqe->un.mb_words[14], mqe->un.mb_words[15],
5751 mqe->un.mb_words[16], mqe->un.mb_words[50],
5752 mboxq->mcqe.word0,
5753 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5754 mboxq->mcqe.trailer);
5755
5756 if (rc) {
5757 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5758 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005759 rc = -EIO;
5760 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005761 }
5762 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005763 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005764 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5765 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005766 rc = -EIO;
5767 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005768 }
James Smartda0436e2009-05-22 14:51:39 -04005769
5770 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5771 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5772 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005773 rc = 0;
5774
5775out_free_mboxq:
5776 mempool_free(mboxq, phba->mbox_mem_pool);
5777 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005778}
5779
5780/**
5781 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5782 * @phba: pointer to lpfc hba data structure.
5783 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5784 * @vpd: pointer to the memory to hold resulting port vpd data.
5785 * @vpd_size: On input, the number of bytes allocated to @vpd.
5786 * On output, the number of data bytes in @vpd.
5787 *
5788 * This routine executes a READ_REV SLI4 mailbox command. In
5789 * addition, this routine gets the port vpd data.
5790 *
5791 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005792 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005793 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005794 **/
5795static int
5796lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5797 uint8_t *vpd, uint32_t *vpd_size)
5798{
5799 int rc = 0;
5800 uint32_t dma_size;
5801 struct lpfc_dmabuf *dmabuf;
5802 struct lpfc_mqe *mqe;
5803
5804 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5805 if (!dmabuf)
5806 return -ENOMEM;
5807
5808 /*
5809 * Get a DMA buffer for the vpd data resulting from the READ_REV
5810 * mailbox command.
5811 */
5812 dma_size = *vpd_size;
Luis Chamberlain750afb02019-01-04 09:23:09 +01005813 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5814 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005815 if (!dmabuf->virt) {
5816 kfree(dmabuf);
5817 return -ENOMEM;
5818 }
James Smartda0436e2009-05-22 14:51:39 -04005819
5820 /*
5821 * The SLI4 implementation of READ_REV conflicts at word1,
5822 * bits 31:16 and SLI4 adds vpd functionality not present
5823 * in SLI3. This code corrects the conflicts.
5824 */
5825 lpfc_read_rev(phba, mboxq);
5826 mqe = &mboxq->u.mqe;
5827 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5828 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5829 mqe->un.read_rev.word1 &= 0x0000FFFF;
5830 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5831 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5832
5833 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5834 if (rc) {
5835 dma_free_coherent(&phba->pcidev->dev, dma_size,
5836 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005837 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005838 return -EIO;
5839 }
5840
James Smartda0436e2009-05-22 14:51:39 -04005841 /*
5842 * The available vpd length cannot be bigger than the
5843 * DMA buffer passed to the port. Catch the less than
5844 * case and update the caller's size.
5845 */
5846 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5847 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5848
James Smartd7c47992010-06-08 18:31:54 -04005849 memcpy(vpd, dmabuf->virt, *vpd_size);
5850
James Smartda0436e2009-05-22 14:51:39 -04005851 dma_free_coherent(&phba->pcidev->dev, dma_size,
5852 dmabuf->virt, dmabuf->phys);
5853 kfree(dmabuf);
5854 return 0;
5855}
5856
5857/**
James Smartb3b4f3e2019-03-12 16:30:23 -07005858 * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005859 * @phba: pointer to lpfc hba data structure.
5860 *
5861 * This routine retrieves SLI4 device physical port name this PCI function
5862 * is attached to.
5863 *
5864 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005865 * 0 - successful
James Smartb3b4f3e2019-03-12 16:30:23 -07005866 * otherwise - failed to retrieve controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005867 **/
5868static int
James Smartb3b4f3e2019-03-12 16:30:23 -07005869lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
James Smartcd1c8302011-10-10 21:33:25 -04005870{
5871 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005872 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5873 struct lpfc_controller_attribute *cntl_attr;
James Smartcd1c8302011-10-10 21:33:25 -04005874 void *virtaddr = NULL;
5875 uint32_t alloclen, reqlen;
5876 uint32_t shdr_status, shdr_add_status;
5877 union lpfc_sli4_cfg_shdr *shdr;
James Smartcd1c8302011-10-10 21:33:25 -04005878 int rc;
5879
James Smartcd1c8302011-10-10 21:33:25 -04005880 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5881 if (!mboxq)
5882 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005883
James Smartb3b4f3e2019-03-12 16:30:23 -07005884 /* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
James Smartcd1c8302011-10-10 21:33:25 -04005885 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5886 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5887 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5888 LPFC_SLI4_MBX_NEMBED);
James Smartb3b4f3e2019-03-12 16:30:23 -07005889
James Smartcd1c8302011-10-10 21:33:25 -04005890 if (alloclen < reqlen) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005891 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcd1c8302011-10-10 21:33:25 -04005892 "3084 Allocated DMA memory size (%d) is "
5893 "less than the requested DMA memory size "
5894 "(%d)\n", alloclen, reqlen);
5895 rc = -ENOMEM;
5896 goto out_free_mboxq;
5897 }
5898 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5899 virtaddr = mboxq->sge_array->addr[0];
5900 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5901 shdr = &mbx_cntl_attr->cfg_shdr;
5902 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5903 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5904 if (shdr_status || shdr_add_status || rc) {
5905 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5906 "3085 Mailbox x%x (x%x/x%x) failed, "
5907 "rc:x%x, status:x%x, add_status:x%x\n",
5908 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5909 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5910 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5911 rc, shdr_status, shdr_add_status);
5912 rc = -ENXIO;
5913 goto out_free_mboxq;
5914 }
James Smartb3b4f3e2019-03-12 16:30:23 -07005915
James Smartcd1c8302011-10-10 21:33:25 -04005916 cntl_attr = &mbx_cntl_attr->cntl_attr;
5917 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5918 phba->sli4_hba.lnk_info.lnk_tp =
5919 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5920 phba->sli4_hba.lnk_info.lnk_no =
5921 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
James Smart16a93e82021-07-07 11:43:34 -07005922 phba->sli4_hba.flash_id = bf_get(lpfc_cntl_attr_flash_id, cntl_attr);
5923 phba->sli4_hba.asic_rev = bf_get(lpfc_cntl_attr_asic_rev, cntl_attr);
James Smartb3b4f3e2019-03-12 16:30:23 -07005924
5925 memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
5926 strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
5927 sizeof(phba->BIOSVersion));
5928
James Smartcd1c8302011-10-10 21:33:25 -04005929 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart16a93e82021-07-07 11:43:34 -07005930 "3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s, "
5931 "flash_id: x%02x, asic_rev: x%02x\n",
James Smartcd1c8302011-10-10 21:33:25 -04005932 phba->sli4_hba.lnk_info.lnk_tp,
James Smartb3b4f3e2019-03-12 16:30:23 -07005933 phba->sli4_hba.lnk_info.lnk_no,
James Smart16a93e82021-07-07 11:43:34 -07005934 phba->BIOSVersion, phba->sli4_hba.flash_id,
5935 phba->sli4_hba.asic_rev);
James Smartb3b4f3e2019-03-12 16:30:23 -07005936out_free_mboxq:
James Smart304ee432021-04-11 18:31:17 -07005937 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5938 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5939 else
5940 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartb3b4f3e2019-03-12 16:30:23 -07005941 return rc;
5942}
5943
5944/**
5945 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5946 * @phba: pointer to lpfc hba data structure.
5947 *
5948 * This routine retrieves SLI4 device physical port name this PCI function
5949 * is attached to.
5950 *
5951 * Return codes
5952 * 0 - successful
5953 * otherwise - failed to retrieve physical port name
5954 **/
5955static int
5956lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5957{
5958 LPFC_MBOXQ_t *mboxq;
5959 struct lpfc_mbx_get_port_name *get_port_name;
5960 uint32_t shdr_status, shdr_add_status;
5961 union lpfc_sli4_cfg_shdr *shdr;
5962 char cport_name = 0;
5963 int rc;
5964
5965 /* We assume nothing at this point */
5966 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5967 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5968
5969 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5970 if (!mboxq)
5971 return -ENOMEM;
5972 /* obtain link type and link number via READ_CONFIG */
5973 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5974 lpfc_sli4_read_config(phba);
5975 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5976 goto retrieve_ppname;
5977
5978 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5979 rc = lpfc_sli4_get_ctl_attr(phba);
5980 if (rc)
5981 goto out_free_mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005982
5983retrieve_ppname:
5984 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5985 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5986 sizeof(struct lpfc_mbx_get_port_name) -
5987 sizeof(struct lpfc_sli4_cfg_mhdr),
5988 LPFC_SLI4_MBX_EMBED);
5989 get_port_name = &mboxq->u.mqe.un.get_port_name;
5990 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5991 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5992 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5993 phba->sli4_hba.lnk_info.lnk_tp);
5994 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5995 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5996 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5997 if (shdr_status || shdr_add_status || rc) {
5998 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5999 "3087 Mailbox x%x (x%x/x%x) failed: "
6000 "rc:x%x, status:x%x, add_status:x%x\n",
6001 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
6002 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6003 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
6004 rc, shdr_status, shdr_add_status);
6005 rc = -ENXIO;
6006 goto out_free_mboxq;
6007 }
6008 switch (phba->sli4_hba.lnk_info.lnk_no) {
6009 case LPFC_LINK_NUMBER_0:
6010 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
6011 &get_port_name->u.response);
6012 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6013 break;
6014 case LPFC_LINK_NUMBER_1:
6015 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
6016 &get_port_name->u.response);
6017 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6018 break;
6019 case LPFC_LINK_NUMBER_2:
6020 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
6021 &get_port_name->u.response);
6022 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6023 break;
6024 case LPFC_LINK_NUMBER_3:
6025 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
6026 &get_port_name->u.response);
6027 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
6028 break;
6029 default:
6030 break;
6031 }
6032
6033 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
6034 phba->Port[0] = cport_name;
6035 phba->Port[1] = '\0';
6036 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6037 "3091 SLI get port name: %s\n", phba->Port);
6038 }
6039
6040out_free_mboxq:
James Smart304ee432021-04-11 18:31:17 -07006041 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
6042 lpfc_sli4_mbox_cmd_free(phba, mboxq);
6043 else
6044 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartcd1c8302011-10-10 21:33:25 -04006045 return rc;
6046}
6047
6048/**
James Smartda0436e2009-05-22 14:51:39 -04006049 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
6050 * @phba: pointer to lpfc hba data structure.
6051 *
6052 * This routine is called to explicitly arm the SLI4 device's completion and
6053 * event queues
6054 **/
6055static void
6056lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
6057{
James Smart895427b2017-02-12 13:52:30 -08006058 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08006059 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartcdb42be2019-01-28 11:14:21 -08006060 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07006061 struct lpfc_queue *eq;
James Smartda0436e2009-05-22 14:51:39 -04006062
James Smart32517fc2019-01-28 11:14:33 -08006063 sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
6064 sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
James Smartb71413d2018-02-22 08:18:40 -08006065 if (sli4_hba->nvmels_cq)
James Smart32517fc2019-01-28 11:14:33 -08006066 sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
6067 LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08006068
James Smartcdb42be2019-01-28 11:14:21 -08006069 if (sli4_hba->hdwq) {
James Smart657add42019-05-21 17:49:06 -07006070 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08006071 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07006072 qp = &sli4_hba->hdwq[qidx];
6073 /* ARM the corresponding CQ */
James Smart01f2ef62019-08-28 16:19:11 -07006074 sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
James Smartc00f62e2019-08-14 16:57:11 -07006075 LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08006076 }
James Smart895427b2017-02-12 13:52:30 -08006077
James Smart657add42019-05-21 17:49:06 -07006078 /* Loop thru all IRQ vectors */
6079 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
6080 eq = sli4_hba->hba_eq_hdl[qidx].eq;
6081 /* ARM the corresponding EQ */
6082 sli4_hba->sli4_write_eq_db(phba, eq,
6083 0, LPFC_QUEUE_REARM);
6084 }
James Smartcdb42be2019-01-28 11:14:21 -08006085 }
James Smart1ba981f2014-02-20 09:56:45 -05006086
James Smart2d7dbc42017-02-12 13:52:35 -08006087 if (phba->nvmet_support) {
6088 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smart32517fc2019-01-28 11:14:33 -08006089 sli4_hba->sli4_write_cq_db(phba,
6090 sli4_hba->nvmet_cqset[qidx], 0,
James Smart2d7dbc42017-02-12 13:52:35 -08006091 LPFC_QUEUE_REARM);
6092 }
James Smart2e90f4b2011-12-13 13:22:37 -05006093 }
James Smartda0436e2009-05-22 14:51:39 -04006094}
6095
6096/**
James Smart6d368e52011-05-24 11:44:12 -04006097 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
6098 * @phba: Pointer to HBA context object.
6099 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04006100 * @extnt_count: buffer to hold port available extent count.
6101 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04006102 *
James Smartb76f2dc2011-07-22 18:37:42 -04006103 * This function calls the port and retrievs the number of available
6104 * extents and their size for a particular extent type.
6105 *
6106 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04006107 **/
James Smartb76f2dc2011-07-22 18:37:42 -04006108int
James Smart6d368e52011-05-24 11:44:12 -04006109lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
6110 uint16_t *extnt_count, uint16_t *extnt_size)
6111{
6112 int rc = 0;
6113 uint32_t length;
6114 uint32_t mbox_tmo;
6115 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
6116 LPFC_MBOXQ_t *mbox;
6117
6118 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6119 if (!mbox)
6120 return -ENOMEM;
6121
6122 /* Find out how many extents are available for this resource type */
6123 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
6124 sizeof(struct lpfc_sli4_cfg_mhdr));
6125 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6126 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
6127 length, LPFC_SLI4_MBX_EMBED);
6128
6129 /* Send an extents count of 0 - the GET doesn't use it. */
6130 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6131 LPFC_SLI4_MBX_EMBED);
6132 if (unlikely(rc)) {
6133 rc = -EIO;
6134 goto err_exit;
6135 }
6136
6137 if (!phba->sli4_hba.intr_enable)
6138 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6139 else {
James Smarta183a152011-10-10 21:32:43 -04006140 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006141 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6142 }
6143 if (unlikely(rc)) {
6144 rc = -EIO;
6145 goto err_exit;
6146 }
6147
6148 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
6149 if (bf_get(lpfc_mbox_hdr_status,
6150 &rsrc_info->header.cfg_shdr.response)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006151 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6d368e52011-05-24 11:44:12 -04006152 "2930 Failed to get resource extents "
6153 "Status 0x%x Add'l Status 0x%x\n",
6154 bf_get(lpfc_mbox_hdr_status,
6155 &rsrc_info->header.cfg_shdr.response),
6156 bf_get(lpfc_mbox_hdr_add_status,
6157 &rsrc_info->header.cfg_shdr.response));
6158 rc = -EIO;
6159 goto err_exit;
6160 }
6161
6162 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
6163 &rsrc_info->u.rsp);
6164 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
6165 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04006166
6167 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6168 "3162 Retrieved extents type-%d from port: count:%d, "
6169 "size:%d\n", type, *extnt_count, *extnt_size);
6170
6171err_exit:
James Smart6d368e52011-05-24 11:44:12 -04006172 mempool_free(mbox, phba->mbox_mem_pool);
6173 return rc;
6174}
6175
6176/**
6177 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
6178 * @phba: Pointer to HBA context object.
6179 * @type: The extent type to check.
6180 *
6181 * This function reads the current available extents from the port and checks
6182 * if the extent count or extent size has changed since the last access.
6183 * Callers use this routine post port reset to understand if there is a
6184 * extent reprovisioning requirement.
6185 *
6186 * Returns:
6187 * -Error: error indicates problem.
6188 * 1: Extent count or size has changed.
6189 * 0: No changes.
6190 **/
6191static int
6192lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
6193{
6194 uint16_t curr_ext_cnt, rsrc_ext_cnt;
6195 uint16_t size_diff, rsrc_ext_size;
6196 int rc = 0;
6197 struct lpfc_rsrc_blks *rsrc_entry;
6198 struct list_head *rsrc_blk_list = NULL;
6199
6200 size_diff = 0;
6201 curr_ext_cnt = 0;
6202 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6203 &rsrc_ext_cnt,
6204 &rsrc_ext_size);
6205 if (unlikely(rc))
6206 return -EIO;
6207
6208 switch (type) {
6209 case LPFC_RSC_TYPE_FCOE_RPI:
6210 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6211 break;
6212 case LPFC_RSC_TYPE_FCOE_VPI:
6213 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
6214 break;
6215 case LPFC_RSC_TYPE_FCOE_XRI:
6216 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6217 break;
6218 case LPFC_RSC_TYPE_FCOE_VFI:
6219 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6220 break;
6221 default:
6222 break;
6223 }
6224
6225 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
6226 curr_ext_cnt++;
6227 if (rsrc_entry->rsrc_size != rsrc_ext_size)
6228 size_diff++;
6229 }
6230
6231 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
6232 rc = 1;
6233
6234 return rc;
6235}
6236
6237/**
6238 * lpfc_sli4_cfg_post_extnts -
6239 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01006240 * @extnt_cnt: number of available extents.
6241 * @type: the extent type (rpi, xri, vfi, vpi).
6242 * @emb: buffer to hold either MBX_EMBED or MBX_NEMBED operation.
6243 * @mbox: pointer to the caller's allocated mailbox structure.
James Smart6d368e52011-05-24 11:44:12 -04006244 *
6245 * This function executes the extents allocation request. It also
6246 * takes care of the amount of memory needed to allocate or get the
6247 * allocated extents. It is the caller's responsibility to evaluate
6248 * the response.
6249 *
6250 * Returns:
6251 * -Error: Error value describes the condition found.
6252 * 0: if successful
6253 **/
6254static int
James Smart8a9d2e82012-05-09 21:16:12 -04006255lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006256 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
6257{
6258 int rc = 0;
6259 uint32_t req_len;
6260 uint32_t emb_len;
6261 uint32_t alloc_len, mbox_tmo;
6262
6263 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04006264 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04006265
6266 /*
6267 * Calculate the size of an embedded mailbox. The uint32_t
6268 * accounts for extents-specific word.
6269 */
6270 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6271 sizeof(uint32_t);
6272
6273 /*
6274 * Presume the allocation and response will fit into an embedded
6275 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6276 */
6277 *emb = LPFC_SLI4_MBX_EMBED;
6278 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04006279 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04006280 sizeof(union lpfc_sli4_cfg_shdr) +
6281 sizeof(uint32_t);
6282 *emb = LPFC_SLI4_MBX_NEMBED;
6283 }
6284
6285 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6286 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
6287 req_len, *emb);
6288 if (alloc_len < req_len) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006289 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartb76f2dc2011-07-22 18:37:42 -04006290 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04006291 "less than the requested DMA memory "
6292 "size (x%x)\n", alloc_len, req_len);
6293 return -ENOMEM;
6294 }
James Smart8a9d2e82012-05-09 21:16:12 -04006295 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04006296 if (unlikely(rc))
6297 return -EIO;
6298
6299 if (!phba->sli4_hba.intr_enable)
6300 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6301 else {
James Smarta183a152011-10-10 21:32:43 -04006302 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006303 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6304 }
6305
6306 if (unlikely(rc))
6307 rc = -EIO;
6308 return rc;
6309}
6310
6311/**
6312 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
6313 * @phba: Pointer to HBA context object.
6314 * @type: The resource extent type to allocate.
6315 *
6316 * This function allocates the number of elements for the specified
6317 * resource type.
6318 **/
6319static int
6320lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
6321{
6322 bool emb = false;
6323 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
6324 uint16_t rsrc_id, rsrc_start, j, k;
6325 uint16_t *ids;
6326 int i, rc;
6327 unsigned long longs;
6328 unsigned long *bmask;
6329 struct lpfc_rsrc_blks *rsrc_blks;
6330 LPFC_MBOXQ_t *mbox;
6331 uint32_t length;
6332 struct lpfc_id_range *id_array = NULL;
6333 void *virtaddr = NULL;
6334 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6335 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6336 struct list_head *ext_blk_list;
6337
6338 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6339 &rsrc_cnt,
6340 &rsrc_size);
6341 if (unlikely(rc))
6342 return -EIO;
6343
6344 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006345 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6d368e52011-05-24 11:44:12 -04006346 "3009 No available Resource Extents "
6347 "for resource type 0x%x: Count: 0x%x, "
6348 "Size 0x%x\n", type, rsrc_cnt,
6349 rsrc_size);
6350 return -ENOMEM;
6351 }
6352
James Smart8a9d2e82012-05-09 21:16:12 -04006353 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
6354 "2903 Post resource extents type-0x%x: "
6355 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04006356
6357 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6358 if (!mbox)
6359 return -ENOMEM;
6360
James Smart8a9d2e82012-05-09 21:16:12 -04006361 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006362 if (unlikely(rc)) {
6363 rc = -EIO;
6364 goto err_exit;
6365 }
6366
6367 /*
6368 * Figure out where the response is located. Then get local pointers
6369 * to the response data. The port does not guarantee to respond to
6370 * all extents counts request so update the local variable with the
6371 * allocated count from the port.
6372 */
6373 if (emb == LPFC_SLI4_MBX_EMBED) {
6374 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6375 id_array = &rsrc_ext->u.rsp.id[0];
6376 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6377 } else {
6378 virtaddr = mbox->sge_array->addr[0];
6379 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6380 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6381 id_array = &n_rsrc->id;
6382 }
6383
6384 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
6385 rsrc_id_cnt = rsrc_cnt * rsrc_size;
6386
6387 /*
6388 * Based on the resource size and count, correct the base and max
6389 * resource values.
6390 */
6391 length = sizeof(struct lpfc_rsrc_blks);
6392 switch (type) {
6393 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07006394 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006395 sizeof(unsigned long),
6396 GFP_KERNEL);
6397 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6398 rc = -ENOMEM;
6399 goto err_exit;
6400 }
Kees Cook6396bb22018-06-12 14:03:40 -07006401 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006402 sizeof(uint16_t),
6403 GFP_KERNEL);
6404 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6405 kfree(phba->sli4_hba.rpi_bmask);
6406 rc = -ENOMEM;
6407 goto err_exit;
6408 }
6409
6410 /*
6411 * The next_rpi was initialized with the maximum available
6412 * count but the port may allocate a smaller number. Catch
6413 * that case and update the next_rpi.
6414 */
6415 phba->sli4_hba.next_rpi = rsrc_id_cnt;
6416
6417 /* Initialize local ptrs for common extent processing later. */
6418 bmask = phba->sli4_hba.rpi_bmask;
6419 ids = phba->sli4_hba.rpi_ids;
6420 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6421 break;
6422 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07006423 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006424 GFP_KERNEL);
6425 if (unlikely(!phba->vpi_bmask)) {
6426 rc = -ENOMEM;
6427 goto err_exit;
6428 }
Kees Cook6396bb22018-06-12 14:03:40 -07006429 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006430 GFP_KERNEL);
6431 if (unlikely(!phba->vpi_ids)) {
6432 kfree(phba->vpi_bmask);
6433 rc = -ENOMEM;
6434 goto err_exit;
6435 }
6436
6437 /* Initialize local ptrs for common extent processing later. */
6438 bmask = phba->vpi_bmask;
6439 ids = phba->vpi_ids;
6440 ext_blk_list = &phba->lpfc_vpi_blk_list;
6441 break;
6442 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07006443 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006444 sizeof(unsigned long),
6445 GFP_KERNEL);
6446 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6447 rc = -ENOMEM;
6448 goto err_exit;
6449 }
James Smart8a9d2e82012-05-09 21:16:12 -04006450 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006451 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006452 sizeof(uint16_t),
6453 GFP_KERNEL);
6454 if (unlikely(!phba->sli4_hba.xri_ids)) {
6455 kfree(phba->sli4_hba.xri_bmask);
6456 rc = -ENOMEM;
6457 goto err_exit;
6458 }
6459
6460 /* Initialize local ptrs for common extent processing later. */
6461 bmask = phba->sli4_hba.xri_bmask;
6462 ids = phba->sli4_hba.xri_ids;
6463 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6464 break;
6465 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07006466 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006467 sizeof(unsigned long),
6468 GFP_KERNEL);
6469 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6470 rc = -ENOMEM;
6471 goto err_exit;
6472 }
Kees Cook6396bb22018-06-12 14:03:40 -07006473 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006474 sizeof(uint16_t),
6475 GFP_KERNEL);
6476 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6477 kfree(phba->sli4_hba.vfi_bmask);
6478 rc = -ENOMEM;
6479 goto err_exit;
6480 }
6481
6482 /* Initialize local ptrs for common extent processing later. */
6483 bmask = phba->sli4_hba.vfi_bmask;
6484 ids = phba->sli4_hba.vfi_ids;
6485 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6486 break;
6487 default:
6488 /* Unsupported Opcode. Fail call. */
6489 id_array = NULL;
6490 bmask = NULL;
6491 ids = NULL;
6492 ext_blk_list = NULL;
6493 goto err_exit;
6494 }
6495
6496 /*
6497 * Complete initializing the extent configuration with the
6498 * allocated ids assigned to this function. The bitmask serves
6499 * as an index into the array and manages the available ids. The
6500 * array just stores the ids communicated to the port via the wqes.
6501 */
6502 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6503 if ((i % 2) == 0)
6504 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6505 &id_array[k]);
6506 else
6507 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6508 &id_array[k]);
6509
6510 rsrc_blks = kzalloc(length, GFP_KERNEL);
6511 if (unlikely(!rsrc_blks)) {
6512 rc = -ENOMEM;
6513 kfree(bmask);
6514 kfree(ids);
6515 goto err_exit;
6516 }
6517 rsrc_blks->rsrc_start = rsrc_id;
6518 rsrc_blks->rsrc_size = rsrc_size;
6519 list_add_tail(&rsrc_blks->list, ext_blk_list);
6520 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08006521 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart5e5b5112019-01-28 11:14:22 -08006522 phba->sli4_hba.io_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08006523 lpfc_sli4_get_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08006524 }
James Smart6d368e52011-05-24 11:44:12 -04006525
6526 while (rsrc_id < (rsrc_start + rsrc_size)) {
6527 ids[j] = rsrc_id;
6528 rsrc_id++;
6529 j++;
6530 }
6531 /* Entire word processed. Get next word.*/
6532 if ((i % 2) == 1)
6533 k++;
6534 }
6535 err_exit:
6536 lpfc_sli4_mbox_cmd_free(phba, mbox);
6537 return rc;
6538}
6539
James Smart895427b2017-02-12 13:52:30 -08006540
6541
James Smart6d368e52011-05-24 11:44:12 -04006542/**
6543 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6544 * @phba: Pointer to HBA context object.
6545 * @type: the extent's type.
6546 *
6547 * This function deallocates all extents of a particular resource type.
6548 * SLI4 does not allow for deallocating a particular extent range. It
6549 * is the caller's responsibility to release all kernel memory resources.
6550 **/
6551static int
6552lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6553{
6554 int rc;
6555 uint32_t length, mbox_tmo = 0;
6556 LPFC_MBOXQ_t *mbox;
6557 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6558 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6559
6560 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6561 if (!mbox)
6562 return -ENOMEM;
6563
6564 /*
6565 * This function sends an embedded mailbox because it only sends the
6566 * the resource type. All extents of this type are released by the
6567 * port.
6568 */
6569 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6570 sizeof(struct lpfc_sli4_cfg_mhdr));
6571 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6572 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6573 length, LPFC_SLI4_MBX_EMBED);
6574
6575 /* Send an extents count of 0 - the dealloc doesn't use it. */
6576 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6577 LPFC_SLI4_MBX_EMBED);
6578 if (unlikely(rc)) {
6579 rc = -EIO;
6580 goto out_free_mbox;
6581 }
6582 if (!phba->sli4_hba.intr_enable)
6583 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6584 else {
James Smarta183a152011-10-10 21:32:43 -04006585 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006586 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6587 }
6588 if (unlikely(rc)) {
6589 rc = -EIO;
6590 goto out_free_mbox;
6591 }
6592
6593 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6594 if (bf_get(lpfc_mbox_hdr_status,
6595 &dealloc_rsrc->header.cfg_shdr.response)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006596 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6d368e52011-05-24 11:44:12 -04006597 "2919 Failed to release resource extents "
6598 "for type %d - Status 0x%x Add'l Status 0x%x. "
6599 "Resource memory not released.\n",
6600 type,
6601 bf_get(lpfc_mbox_hdr_status,
6602 &dealloc_rsrc->header.cfg_shdr.response),
6603 bf_get(lpfc_mbox_hdr_add_status,
6604 &dealloc_rsrc->header.cfg_shdr.response));
6605 rc = -EIO;
6606 goto out_free_mbox;
6607 }
6608
6609 /* Release kernel memory resources for the specific type. */
6610 switch (type) {
6611 case LPFC_RSC_TYPE_FCOE_VPI:
6612 kfree(phba->vpi_bmask);
6613 kfree(phba->vpi_ids);
6614 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6615 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6616 &phba->lpfc_vpi_blk_list, list) {
6617 list_del_init(&rsrc_blk->list);
6618 kfree(rsrc_blk);
6619 }
James Smart16a3a202013-04-17 20:14:38 -04006620 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006621 break;
6622 case LPFC_RSC_TYPE_FCOE_XRI:
6623 kfree(phba->sli4_hba.xri_bmask);
6624 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006625 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6626 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6627 list_del_init(&rsrc_blk->list);
6628 kfree(rsrc_blk);
6629 }
6630 break;
6631 case LPFC_RSC_TYPE_FCOE_VFI:
6632 kfree(phba->sli4_hba.vfi_bmask);
6633 kfree(phba->sli4_hba.vfi_ids);
6634 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6635 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6636 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6637 list_del_init(&rsrc_blk->list);
6638 kfree(rsrc_blk);
6639 }
6640 break;
6641 case LPFC_RSC_TYPE_FCOE_RPI:
6642 /* RPI bitmask and physical id array are cleaned up earlier. */
6643 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6644 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6645 list_del_init(&rsrc_blk->list);
6646 kfree(rsrc_blk);
6647 }
6648 break;
6649 default:
6650 break;
6651 }
6652
6653 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6654
6655 out_free_mbox:
6656 mempool_free(mbox, phba->mbox_mem_pool);
6657 return rc;
6658}
6659
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006660static void
James Smart7bdedb32016-07-06 12:36:00 -07006661lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6662 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006663{
James Smart65791f12016-07-06 12:35:56 -07006664 uint32_t len;
James Smart9064aeb2021-08-16 09:28:50 -07006665 u32 sig_freq = 0;
James Smart65791f12016-07-06 12:35:56 -07006666
James Smart65791f12016-07-06 12:35:56 -07006667 len = sizeof(struct lpfc_mbx_set_feature) -
6668 sizeof(struct lpfc_sli4_cfg_mhdr);
6669 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6670 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6671 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006672
James Smart7bdedb32016-07-06 12:36:00 -07006673 switch (feature) {
6674 case LPFC_SET_UE_RECOVERY:
6675 bf_set(lpfc_mbx_set_feature_UER,
6676 &mbox->u.mqe.un.set_feature, 1);
6677 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6678 mbox->u.mqe.un.set_feature.param_len = 8;
6679 break;
6680 case LPFC_SET_MDS_DIAGS:
6681 bf_set(lpfc_mbx_set_feature_mds,
6682 &mbox->u.mqe.un.set_feature, 1);
6683 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006684 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006685 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6686 mbox->u.mqe.un.set_feature.param_len = 8;
6687 break;
James Smart9064aeb2021-08-16 09:28:50 -07006688 case LPFC_SET_CGN_SIGNAL:
6689 if (phba->cmf_active_mode == LPFC_CFG_OFF)
6690 sig_freq = 0;
6691 else
6692 sig_freq = phba->cgn_sig_freq;
6693
6694 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ALARM) {
6695 bf_set(lpfc_mbx_set_feature_CGN_alarm_freq,
6696 &mbox->u.mqe.un.set_feature, sig_freq);
6697 bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6698 &mbox->u.mqe.un.set_feature, sig_freq);
6699 }
6700
6701 if (phba->cgn_reg_signal == EDC_CG_SIG_WARN_ONLY)
6702 bf_set(lpfc_mbx_set_feature_CGN_warn_freq,
6703 &mbox->u.mqe.un.set_feature, sig_freq);
6704
6705 if (phba->cmf_active_mode == LPFC_CFG_OFF ||
6706 phba->cgn_reg_signal == EDC_CG_SIG_NOTSUPPORTED)
6707 sig_freq = 0;
6708 else
6709 sig_freq = lpfc_acqe_cgn_frequency;
6710
6711 bf_set(lpfc_mbx_set_feature_CGN_acqe_freq,
6712 &mbox->u.mqe.un.set_feature, sig_freq);
6713
6714 mbox->u.mqe.un.set_feature.feature = LPFC_SET_CGN_SIGNAL;
6715 mbox->u.mqe.un.set_feature.param_len = 12;
6716 break;
James Smart171f6c42019-11-04 16:57:07 -08006717 case LPFC_SET_DUAL_DUMP:
6718 bf_set(lpfc_mbx_set_feature_dd,
6719 &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
6720 bf_set(lpfc_mbx_set_feature_ddquery,
6721 &mbox->u.mqe.un.set_feature, 0);
6722 mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
6723 mbox->u.mqe.un.set_feature.param_len = 4;
6724 break;
James Smartc6a5c7472021-08-16 09:28:48 -07006725 case LPFC_SET_ENABLE_MI:
6726 mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_MI;
6727 mbox->u.mqe.un.set_feature.param_len = 4;
6728 bf_set(lpfc_mbx_set_feature_milunq, &mbox->u.mqe.un.set_feature,
6729 phba->pport->cfg_lun_queue_depth);
6730 bf_set(lpfc_mbx_set_feature_mi, &mbox->u.mqe.un.set_feature,
6731 phba->sli4_hba.pc_sli4_params.mi_ver);
6732 break;
James Smart02243832021-08-16 09:28:54 -07006733 case LPFC_SET_ENABLE_CMF:
6734 bf_set(lpfc_mbx_set_feature_dd, &mbox->u.mqe.un.set_feature, 1);
6735 mbox->u.mqe.un.set_feature.feature = LPFC_SET_ENABLE_CMF;
6736 mbox->u.mqe.un.set_feature.param_len = 4;
6737 bf_set(lpfc_mbx_set_feature_cmf,
6738 &mbox->u.mqe.un.set_feature, 1);
6739 break;
James Smart65791f12016-07-06 12:35:56 -07006740 }
James Smart7bdedb32016-07-06 12:36:00 -07006741 return;
James Smart65791f12016-07-06 12:35:56 -07006742}
6743
James Smart6d368e52011-05-24 11:44:12 -04006744/**
James Smart1165a5c2018-11-29 16:09:39 -08006745 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6746 * @phba: Pointer to HBA context object.
6747 *
6748 * Disable FW logging into host memory on the adapter. To
6749 * be done before reading logs from the host memory.
6750 **/
6751void
6752lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6753{
6754 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6755
James Smart95bfc6d2019-10-18 14:18:27 -07006756 spin_lock_irq(&phba->hbalock);
6757 ras_fwlog->state = INACTIVE;
6758 spin_unlock_irq(&phba->hbalock);
James Smart1165a5c2018-11-29 16:09:39 -08006759
6760 /* Disable FW logging to host memory */
6761 writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6762 phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
James Smart95bfc6d2019-10-18 14:18:27 -07006763
6764 /* Wait 10ms for firmware to stop using DMA buffer */
6765 usleep_range(10 * 1000, 20 * 1000);
James Smart1165a5c2018-11-29 16:09:39 -08006766}
6767
6768/**
James Smartd2cc9bc2018-09-10 10:30:50 -07006769 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6770 * @phba: Pointer to HBA context object.
6771 *
6772 * This function is called to free memory allocated for RAS FW logging
6773 * support in the driver.
6774 **/
6775void
6776lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6777{
6778 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6779 struct lpfc_dmabuf *dmabuf, *next;
6780
6781 if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6782 list_for_each_entry_safe(dmabuf, next,
6783 &ras_fwlog->fwlog_buff_list,
6784 list) {
6785 list_del(&dmabuf->list);
6786 dma_free_coherent(&phba->pcidev->dev,
6787 LPFC_RAS_MAX_ENTRY_SIZE,
6788 dmabuf->virt, dmabuf->phys);
6789 kfree(dmabuf);
6790 }
6791 }
6792
6793 if (ras_fwlog->lwpd.virt) {
6794 dma_free_coherent(&phba->pcidev->dev,
6795 sizeof(uint32_t) * 2,
6796 ras_fwlog->lwpd.virt,
6797 ras_fwlog->lwpd.phys);
6798 ras_fwlog->lwpd.virt = NULL;
6799 }
6800
James Smart95bfc6d2019-10-18 14:18:27 -07006801 spin_lock_irq(&phba->hbalock);
6802 ras_fwlog->state = INACTIVE;
6803 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07006804}
6805
6806/**
6807 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6808 * @phba: Pointer to HBA context object.
6809 * @fwlog_buff_count: Count of buffers to be created.
6810 *
6811 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6812 * to update FW log is posted to the adapter.
6813 * Buffer count is calculated based on module param ras_fwlog_buffsize
6814 * Size of each buffer posted to FW is 64K.
6815 **/
6816
6817static int
6818lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6819 uint32_t fwlog_buff_count)
6820{
6821 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6822 struct lpfc_dmabuf *dmabuf;
6823 int rc = 0, i = 0;
6824
6825 /* Initialize List */
6826 INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6827
6828 /* Allocate memory for the LWPD */
6829 ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6830 sizeof(uint32_t) * 2,
6831 &ras_fwlog->lwpd.phys,
6832 GFP_KERNEL);
6833 if (!ras_fwlog->lwpd.virt) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006834 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006835 "6185 LWPD Memory Alloc Failed\n");
6836
6837 return -ENOMEM;
6838 }
6839
6840 ras_fwlog->fw_buffcount = fwlog_buff_count;
6841 for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6842 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6843 GFP_KERNEL);
6844 if (!dmabuf) {
6845 rc = -ENOMEM;
6846 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6847 "6186 Memory Alloc failed FW logging");
6848 goto free_mem;
6849 }
6850
Luis Chamberlain750afb02019-01-04 09:23:09 +01006851 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartd2cc9bc2018-09-10 10:30:50 -07006852 LPFC_RAS_MAX_ENTRY_SIZE,
Luis Chamberlain750afb02019-01-04 09:23:09 +01006853 &dmabuf->phys, GFP_KERNEL);
James Smartd2cc9bc2018-09-10 10:30:50 -07006854 if (!dmabuf->virt) {
6855 kfree(dmabuf);
6856 rc = -ENOMEM;
6857 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6858 "6187 DMA Alloc Failed FW logging");
6859 goto free_mem;
6860 }
James Smartd2cc9bc2018-09-10 10:30:50 -07006861 dmabuf->buffer_tag = i;
6862 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6863 }
6864
6865free_mem:
6866 if (rc)
6867 lpfc_sli4_ras_dma_free(phba);
6868
6869 return rc;
6870}
6871
6872/**
6873 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6874 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +01006875 * @pmb: pointer to the driver internal queue element for mailbox command.
James Smartd2cc9bc2018-09-10 10:30:50 -07006876 *
6877 * Completion handler for driver's RAS MBX command to the device.
6878 **/
6879static void
6880lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6881{
6882 MAILBOX_t *mb;
6883 union lpfc_sli4_cfg_shdr *shdr;
6884 uint32_t shdr_status, shdr_add_status;
6885 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6886
6887 mb = &pmb->u.mb;
6888
6889 shdr = (union lpfc_sli4_cfg_shdr *)
6890 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6891 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6892 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6893
6894 if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006895 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006896 "6188 FW LOG mailbox "
6897 "completed with status x%x add_status x%x,"
6898 " mbx status x%x\n",
6899 shdr_status, shdr_add_status, mb->mbxStatus);
James Smartcb349902018-11-29 16:09:27 -08006900
6901 ras_fwlog->ras_hwsupport = false;
James Smartd2cc9bc2018-09-10 10:30:50 -07006902 goto disable_ras;
6903 }
6904
James Smart95bfc6d2019-10-18 14:18:27 -07006905 spin_lock_irq(&phba->hbalock);
6906 ras_fwlog->state = ACTIVE;
6907 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07006908 mempool_free(pmb, phba->mbox_mem_pool);
6909
6910 return;
6911
6912disable_ras:
6913 /* Free RAS DMA memory */
6914 lpfc_sli4_ras_dma_free(phba);
6915 mempool_free(pmb, phba->mbox_mem_pool);
6916}
6917
6918/**
6919 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6920 * @phba: pointer to lpfc hba data structure.
6921 * @fwlog_level: Logging verbosity level.
6922 * @fwlog_enable: Enable/Disable logging.
6923 *
6924 * Initialize memory and post mailbox command to enable FW logging in host
6925 * memory.
6926 **/
6927int
6928lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6929 uint32_t fwlog_level,
6930 uint32_t fwlog_enable)
6931{
6932 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6933 struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6934 struct lpfc_dmabuf *dmabuf;
6935 LPFC_MBOXQ_t *mbox;
6936 uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6937 int rc = 0;
6938
James Smart95bfc6d2019-10-18 14:18:27 -07006939 spin_lock_irq(&phba->hbalock);
6940 ras_fwlog->state = INACTIVE;
6941 spin_unlock_irq(&phba->hbalock);
6942
James Smartd2cc9bc2018-09-10 10:30:50 -07006943 fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6944 phba->cfg_ras_fwlog_buffsize);
6945 fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6946
6947 /*
6948 * If re-enabling FW logging support use earlier allocated
6949 * DMA buffers while posting MBX command.
6950 **/
6951 if (!ras_fwlog->lwpd.virt) {
6952 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6953 if (rc) {
6954 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smartcb349902018-11-29 16:09:27 -08006955 "6189 FW Log Memory Allocation Failed");
James Smartd2cc9bc2018-09-10 10:30:50 -07006956 return rc;
6957 }
6958 }
6959
6960 /* Setup Mailbox command */
6961 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6962 if (!mbox) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006963 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006964 "6190 RAS MBX Alloc Failed");
6965 rc = -ENOMEM;
6966 goto mem_free;
6967 }
6968
6969 ras_fwlog->fw_loglevel = fwlog_level;
6970 len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6971 sizeof(struct lpfc_sli4_cfg_mhdr));
6972
6973 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6974 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6975 len, LPFC_SLI4_MBX_EMBED);
6976
6977 mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6978 bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6979 fwlog_enable);
6980 bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6981 ras_fwlog->fw_loglevel);
6982 bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6983 ras_fwlog->fw_buffcount);
6984 bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6985 LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6986
6987 /* Update DMA buffer address */
6988 list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6989 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6990
6991 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6992 putPaddrLow(dmabuf->phys);
6993
6994 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6995 putPaddrHigh(dmabuf->phys);
6996 }
6997
6998 /* Update LPWD address */
6999 mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
7000 mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
7001
James Smart95bfc6d2019-10-18 14:18:27 -07007002 spin_lock_irq(&phba->hbalock);
7003 ras_fwlog->state = REG_INPROGRESS;
7004 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07007005 mbox->vport = phba->pport;
7006 mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
7007
7008 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
7009
7010 if (rc == MBX_NOT_FINISHED) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007011 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcb349902018-11-29 16:09:27 -08007012 "6191 FW-Log Mailbox failed. "
James Smartd2cc9bc2018-09-10 10:30:50 -07007013 "status %d mbxStatus : x%x", rc,
7014 bf_get(lpfc_mqe_status, &mbox->u.mqe));
7015 mempool_free(mbox, phba->mbox_mem_pool);
7016 rc = -EIO;
7017 goto mem_free;
7018 } else
7019 rc = 0;
7020mem_free:
7021 if (rc)
7022 lpfc_sli4_ras_dma_free(phba);
7023
7024 return rc;
7025}
7026
7027/**
7028 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
7029 * @phba: Pointer to HBA context object.
7030 *
7031 * Check if RAS is supported on the adapter and initialize it.
7032 **/
7033void
7034lpfc_sli4_ras_setup(struct lpfc_hba *phba)
7035{
7036 /* Check RAS FW Log needs to be enabled or not */
7037 if (lpfc_check_fwlog_support(phba))
7038 return;
7039
7040 lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
7041 LPFC_RAS_ENABLE_LOGGING);
7042}
7043
7044/**
James Smart6d368e52011-05-24 11:44:12 -04007045 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
7046 * @phba: Pointer to HBA context object.
7047 *
7048 * This function allocates all SLI4 resource identifiers.
7049 **/
7050int
7051lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
7052{
7053 int i, rc, error = 0;
7054 uint16_t count, base;
7055 unsigned long longs;
7056
James Smartff78d8f2011-12-13 13:21:35 -05007057 if (!phba->sli4_hba.rpi_hdrs_in_use)
7058 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04007059 if (phba->sli4_hba.extents_in_use) {
7060 /*
7061 * The port supports resource extents. The XRI, VPI, VFI, RPI
7062 * resource extent count must be read and allocated before
7063 * provisioning the resource id arrays.
7064 */
7065 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
7066 LPFC_IDX_RSRC_RDY) {
7067 /*
7068 * Extent-based resources are set - the driver could
7069 * be in a port reset. Figure out if any corrective
7070 * actions need to be taken.
7071 */
7072 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7073 LPFC_RSC_TYPE_FCOE_VFI);
7074 if (rc != 0)
7075 error++;
7076 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7077 LPFC_RSC_TYPE_FCOE_VPI);
7078 if (rc != 0)
7079 error++;
7080 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7081 LPFC_RSC_TYPE_FCOE_XRI);
7082 if (rc != 0)
7083 error++;
7084 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
7085 LPFC_RSC_TYPE_FCOE_RPI);
7086 if (rc != 0)
7087 error++;
7088
7089 /*
7090 * It's possible that the number of resources
7091 * provided to this port instance changed between
7092 * resets. Detect this condition and reallocate
7093 * resources. Otherwise, there is no action.
7094 */
7095 if (error) {
7096 lpfc_printf_log(phba, KERN_INFO,
7097 LOG_MBOX | LOG_INIT,
7098 "2931 Detected extent resource "
7099 "change. Reallocating all "
7100 "extents.\n");
7101 rc = lpfc_sli4_dealloc_extent(phba,
7102 LPFC_RSC_TYPE_FCOE_VFI);
7103 rc = lpfc_sli4_dealloc_extent(phba,
7104 LPFC_RSC_TYPE_FCOE_VPI);
7105 rc = lpfc_sli4_dealloc_extent(phba,
7106 LPFC_RSC_TYPE_FCOE_XRI);
7107 rc = lpfc_sli4_dealloc_extent(phba,
7108 LPFC_RSC_TYPE_FCOE_RPI);
7109 } else
7110 return 0;
7111 }
7112
7113 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7114 if (unlikely(rc))
7115 goto err_exit;
7116
7117 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7118 if (unlikely(rc))
7119 goto err_exit;
7120
7121 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7122 if (unlikely(rc))
7123 goto err_exit;
7124
7125 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7126 if (unlikely(rc))
7127 goto err_exit;
7128 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7129 LPFC_IDX_RSRC_RDY);
7130 return rc;
7131 } else {
7132 /*
7133 * The port does not support resource extents. The XRI, VPI,
7134 * VFI, RPI resource ids were determined from READ_CONFIG.
7135 * Just allocate the bitmasks and provision the resource id
7136 * arrays. If a port reset is active, the resources don't
7137 * need any action - just exit.
7138 */
7139 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05007140 LPFC_IDX_RSRC_RDY) {
7141 lpfc_sli4_dealloc_resource_identifiers(phba);
7142 lpfc_sli4_remove_rpis(phba);
7143 }
James Smart6d368e52011-05-24 11:44:12 -04007144 /* RPIs. */
7145 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05007146 if (count <= 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007147 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a630c22013-01-03 15:44:09 -05007148 "3279 Invalid provisioning of "
7149 "rpi:%d\n", count);
7150 rc = -EINVAL;
7151 goto err_exit;
7152 }
James Smart6d368e52011-05-24 11:44:12 -04007153 base = phba->sli4_hba.max_cfg_param.rpi_base;
7154 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07007155 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04007156 sizeof(unsigned long),
7157 GFP_KERNEL);
7158 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
7159 rc = -ENOMEM;
7160 goto err_exit;
7161 }
Kees Cook6396bb22018-06-12 14:03:40 -07007162 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04007163 GFP_KERNEL);
7164 if (unlikely(!phba->sli4_hba.rpi_ids)) {
7165 rc = -ENOMEM;
7166 goto free_rpi_bmask;
7167 }
7168
7169 for (i = 0; i < count; i++)
7170 phba->sli4_hba.rpi_ids[i] = base + i;
7171
7172 /* VPIs. */
7173 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05007174 if (count <= 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007175 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a630c22013-01-03 15:44:09 -05007176 "3280 Invalid provisioning of "
7177 "vpi:%d\n", count);
7178 rc = -EINVAL;
7179 goto free_rpi_ids;
7180 }
James Smart6d368e52011-05-24 11:44:12 -04007181 base = phba->sli4_hba.max_cfg_param.vpi_base;
7182 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07007183 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04007184 GFP_KERNEL);
7185 if (unlikely(!phba->vpi_bmask)) {
7186 rc = -ENOMEM;
7187 goto free_rpi_ids;
7188 }
Kees Cook6396bb22018-06-12 14:03:40 -07007189 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04007190 GFP_KERNEL);
7191 if (unlikely(!phba->vpi_ids)) {
7192 rc = -ENOMEM;
7193 goto free_vpi_bmask;
7194 }
7195
7196 for (i = 0; i < count; i++)
7197 phba->vpi_ids[i] = base + i;
7198
7199 /* XRIs. */
7200 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05007201 if (count <= 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007202 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a630c22013-01-03 15:44:09 -05007203 "3281 Invalid provisioning of "
7204 "xri:%d\n", count);
7205 rc = -EINVAL;
7206 goto free_vpi_ids;
7207 }
James Smart6d368e52011-05-24 11:44:12 -04007208 base = phba->sli4_hba.max_cfg_param.xri_base;
7209 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07007210 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04007211 sizeof(unsigned long),
7212 GFP_KERNEL);
7213 if (unlikely(!phba->sli4_hba.xri_bmask)) {
7214 rc = -ENOMEM;
7215 goto free_vpi_ids;
7216 }
James Smart41899be2012-03-01 22:34:19 -05007217 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07007218 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04007219 GFP_KERNEL);
7220 if (unlikely(!phba->sli4_hba.xri_ids)) {
7221 rc = -ENOMEM;
7222 goto free_xri_bmask;
7223 }
7224
7225 for (i = 0; i < count; i++)
7226 phba->sli4_hba.xri_ids[i] = base + i;
7227
7228 /* VFIs. */
7229 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05007230 if (count <= 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007231 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a630c22013-01-03 15:44:09 -05007232 "3282 Invalid provisioning of "
7233 "vfi:%d\n", count);
7234 rc = -EINVAL;
7235 goto free_xri_ids;
7236 }
James Smart6d368e52011-05-24 11:44:12 -04007237 base = phba->sli4_hba.max_cfg_param.vfi_base;
7238 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07007239 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04007240 sizeof(unsigned long),
7241 GFP_KERNEL);
7242 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
7243 rc = -ENOMEM;
7244 goto free_xri_ids;
7245 }
Kees Cook6396bb22018-06-12 14:03:40 -07007246 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04007247 GFP_KERNEL);
7248 if (unlikely(!phba->sli4_hba.vfi_ids)) {
7249 rc = -ENOMEM;
7250 goto free_vfi_bmask;
7251 }
7252
7253 for (i = 0; i < count; i++)
7254 phba->sli4_hba.vfi_ids[i] = base + i;
7255
7256 /*
7257 * Mark all resources ready. An HBA reset doesn't need
7258 * to reset the initialization.
7259 */
7260 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
7261 LPFC_IDX_RSRC_RDY);
7262 return 0;
7263 }
7264
7265 free_vfi_bmask:
7266 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01007267 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007268 free_xri_ids:
7269 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01007270 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007271 free_xri_bmask:
7272 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01007273 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007274 free_vpi_ids:
7275 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01007276 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007277 free_vpi_bmask:
7278 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01007279 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007280 free_rpi_ids:
7281 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01007282 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007283 free_rpi_bmask:
7284 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01007285 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04007286 err_exit:
7287 return rc;
7288}
7289
7290/**
7291 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
7292 * @phba: Pointer to HBA context object.
7293 *
7294 * This function allocates the number of elements for the specified
7295 * resource type.
7296 **/
7297int
7298lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
7299{
7300 if (phba->sli4_hba.extents_in_use) {
7301 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7302 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7303 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7304 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7305 } else {
7306 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04007307 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04007308 kfree(phba->vpi_ids);
7309 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7310 kfree(phba->sli4_hba.xri_bmask);
7311 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04007312 kfree(phba->sli4_hba.vfi_bmask);
7313 kfree(phba->sli4_hba.vfi_ids);
7314 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7315 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7316 }
7317
7318 return 0;
7319}
7320
7321/**
James Smartb76f2dc2011-07-22 18:37:42 -04007322 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
7323 * @phba: Pointer to HBA context object.
7324 * @type: The resource extent type.
Lee Jones7af29d42020-07-21 17:41:31 +01007325 * @extnt_cnt: buffer to hold port extent count response
James Smartb76f2dc2011-07-22 18:37:42 -04007326 * @extnt_size: buffer to hold port extent size response.
7327 *
7328 * This function calls the port to read the host allocated extents
7329 * for a particular type.
7330 **/
7331int
7332lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
7333 uint16_t *extnt_cnt, uint16_t *extnt_size)
7334{
7335 bool emb;
7336 int rc = 0;
7337 uint16_t curr_blks = 0;
7338 uint32_t req_len, emb_len;
7339 uint32_t alloc_len, mbox_tmo;
7340 struct list_head *blk_list_head;
7341 struct lpfc_rsrc_blks *rsrc_blk;
7342 LPFC_MBOXQ_t *mbox;
7343 void *virtaddr = NULL;
7344 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
7345 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
7346 union lpfc_sli4_cfg_shdr *shdr;
7347
7348 switch (type) {
7349 case LPFC_RSC_TYPE_FCOE_VPI:
7350 blk_list_head = &phba->lpfc_vpi_blk_list;
7351 break;
7352 case LPFC_RSC_TYPE_FCOE_XRI:
7353 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
7354 break;
7355 case LPFC_RSC_TYPE_FCOE_VFI:
7356 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
7357 break;
7358 case LPFC_RSC_TYPE_FCOE_RPI:
7359 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
7360 break;
7361 default:
7362 return -EIO;
7363 }
7364
7365 /* Count the number of extents currently allocatd for this type. */
7366 list_for_each_entry(rsrc_blk, blk_list_head, list) {
7367 if (curr_blks == 0) {
7368 /*
7369 * The GET_ALLOCATED mailbox does not return the size,
7370 * just the count. The size should be just the size
7371 * stored in the current allocated block and all sizes
7372 * for an extent type are the same so set the return
7373 * value now.
7374 */
7375 *extnt_size = rsrc_blk->rsrc_size;
7376 }
7377 curr_blks++;
7378 }
7379
James Smartb76f2dc2011-07-22 18:37:42 -04007380 /*
7381 * Calculate the size of an embedded mailbox. The uint32_t
7382 * accounts for extents-specific word.
7383 */
7384 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
7385 sizeof(uint32_t);
7386
7387 /*
7388 * Presume the allocation and response will fit into an embedded
7389 * mailbox. If not true, reconfigure to a non-embedded mailbox.
7390 */
7391 emb = LPFC_SLI4_MBX_EMBED;
7392 req_len = emb_len;
7393 if (req_len > emb_len) {
7394 req_len = curr_blks * sizeof(uint16_t) +
7395 sizeof(union lpfc_sli4_cfg_shdr) +
7396 sizeof(uint32_t);
7397 emb = LPFC_SLI4_MBX_NEMBED;
7398 }
7399
7400 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7401 if (!mbox)
7402 return -ENOMEM;
7403 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
7404
7405 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7406 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
7407 req_len, emb);
7408 if (alloc_len < req_len) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007409 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartb76f2dc2011-07-22 18:37:42 -04007410 "2983 Allocated DMA memory size (x%x) is "
7411 "less than the requested DMA memory "
7412 "size (x%x)\n", alloc_len, req_len);
7413 rc = -ENOMEM;
7414 goto err_exit;
7415 }
7416 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
7417 if (unlikely(rc)) {
7418 rc = -EIO;
7419 goto err_exit;
7420 }
7421
7422 if (!phba->sli4_hba.intr_enable)
7423 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
7424 else {
James Smarta183a152011-10-10 21:32:43 -04007425 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04007426 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
7427 }
7428
7429 if (unlikely(rc)) {
7430 rc = -EIO;
7431 goto err_exit;
7432 }
7433
7434 /*
7435 * Figure out where the response is located. Then get local pointers
7436 * to the response data. The port does not guarantee to respond to
7437 * all extents counts request so update the local variable with the
7438 * allocated count from the port.
7439 */
7440 if (emb == LPFC_SLI4_MBX_EMBED) {
7441 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
7442 shdr = &rsrc_ext->header.cfg_shdr;
7443 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
7444 } else {
7445 virtaddr = mbox->sge_array->addr[0];
7446 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
7447 shdr = &n_rsrc->cfg_shdr;
7448 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
7449 }
7450
7451 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007452 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartb76f2dc2011-07-22 18:37:42 -04007453 "2984 Failed to read allocated resources "
7454 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
7455 type,
7456 bf_get(lpfc_mbox_hdr_status, &shdr->response),
7457 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
7458 rc = -EIO;
7459 goto err_exit;
7460 }
7461 err_exit:
7462 lpfc_sli4_mbox_cmd_free(phba, mbox);
7463 return rc;
7464}
7465
7466/**
James Smart0ef69962017-04-21 16:04:50 -07007467 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04007468 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08007469 * @sgl_list: linked link of sgl buffers to post
7470 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04007471 *
James Smart895427b2017-02-12 13:52:30 -08007472 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04007473 * repost them to the port by using SGL block post. This is needed after a
7474 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08007475 * of buffer sgls which contains contiguous xris and uses the non-embedded
7476 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04007477 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
7478 * mailbox command for posting.
7479 *
7480 * Returns: 0 = success, non-zero failure.
7481 **/
7482static int
James Smart895427b2017-02-12 13:52:30 -08007483lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
7484 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04007485{
7486 struct lpfc_sglq *sglq_entry = NULL;
7487 struct lpfc_sglq *sglq_entry_next = NULL;
7488 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08007489 int status, total_cnt;
7490 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04007491 int last_xritag = NO_XRI;
7492 LIST_HEAD(prep_sgl_list);
7493 LIST_HEAD(blck_sgl_list);
7494 LIST_HEAD(allc_sgl_list);
7495 LIST_HEAD(post_sgl_list);
7496 LIST_HEAD(free_sgl_list);
7497
James Smart38c20672013-03-01 16:37:44 -05007498 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007499 spin_lock(&phba->sli4_hba.sgl_list_lock);
7500 list_splice_init(sgl_list, &allc_sgl_list);
7501 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007502 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007503
James Smart895427b2017-02-12 13:52:30 -08007504 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007505 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
7506 &allc_sgl_list, list) {
7507 list_del_init(&sglq_entry->list);
7508 block_cnt++;
7509 if ((last_xritag != NO_XRI) &&
7510 (sglq_entry->sli4_xritag != last_xritag + 1)) {
7511 /* a hole in xri block, form a sgl posting block */
7512 list_splice_init(&prep_sgl_list, &blck_sgl_list);
7513 post_cnt = block_cnt - 1;
7514 /* prepare list for next posting block */
7515 list_add_tail(&sglq_entry->list, &prep_sgl_list);
7516 block_cnt = 1;
7517 } else {
7518 /* prepare list for next posting block */
7519 list_add_tail(&sglq_entry->list, &prep_sgl_list);
7520 /* enough sgls for non-embed sgl mbox command */
7521 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
7522 list_splice_init(&prep_sgl_list,
7523 &blck_sgl_list);
7524 post_cnt = block_cnt;
7525 block_cnt = 0;
7526 }
7527 }
7528 num_posted++;
7529
7530 /* keep track of last sgl's xritag */
7531 last_xritag = sglq_entry->sli4_xritag;
7532
James Smart895427b2017-02-12 13:52:30 -08007533 /* end of repost sgl list condition for buffers */
7534 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04007535 if (post_cnt == 0) {
7536 list_splice_init(&prep_sgl_list,
7537 &blck_sgl_list);
7538 post_cnt = block_cnt;
7539 } else if (block_cnt == 1) {
7540 status = lpfc_sli4_post_sgl(phba,
7541 sglq_entry->phys, 0,
7542 sglq_entry->sli4_xritag);
7543 if (!status) {
7544 /* successful, put sgl to posted list */
7545 list_add_tail(&sglq_entry->list,
7546 &post_sgl_list);
7547 } else {
7548 /* Failure, put sgl to free list */
7549 lpfc_printf_log(phba, KERN_WARNING,
7550 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007551 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04007552 "sgl, xritag:x%x\n",
7553 sglq_entry->sli4_xritag);
7554 list_add_tail(&sglq_entry->list,
7555 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007556 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04007557 }
7558 }
7559 }
7560
7561 /* continue until a nembed page worth of sgls */
7562 if (post_cnt == 0)
7563 continue;
7564
James Smart895427b2017-02-12 13:52:30 -08007565 /* post the buffer list sgls as a block */
7566 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7567 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04007568
7569 if (!status) {
7570 /* success, put sgl list to posted sgl list */
7571 list_splice_init(&blck_sgl_list, &post_sgl_list);
7572 } else {
7573 /* Failure, put sgl list to free sgl list */
7574 sglq_entry_first = list_first_entry(&blck_sgl_list,
7575 struct lpfc_sglq,
7576 list);
7577 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007578 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04007579 "xritag:x%x-x%x\n",
7580 sglq_entry_first->sli4_xritag,
7581 (sglq_entry_first->sli4_xritag +
7582 post_cnt - 1));
7583 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007584 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007585 }
7586
7587 /* don't reset xirtag due to hole in xri block */
7588 if (block_cnt == 0)
7589 last_xritag = NO_XRI;
7590
James Smart895427b2017-02-12 13:52:30 -08007591 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04007592 post_cnt = 0;
7593 }
7594
James Smart895427b2017-02-12 13:52:30 -08007595 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04007596 lpfc_free_sgl_list(phba, &free_sgl_list);
7597
James Smart895427b2017-02-12 13:52:30 -08007598 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04007599 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05007600 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007601 spin_lock(&phba->sli4_hba.sgl_list_lock);
7602 list_splice_init(&post_sgl_list, sgl_list);
7603 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007604 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007605 } else {
Dick Kennedy372c1872020-06-30 14:50:00 -07007606 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08007607 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04007608 return -EIO;
7609 }
James Smart895427b2017-02-12 13:52:30 -08007610
7611 /* return the number of XRIs actually posted */
7612 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007613}
7614
James Smart0794d602019-01-28 11:14:19 -08007615/**
James Smart5e5b5112019-01-28 11:14:22 -08007616 * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
James Smart0794d602019-01-28 11:14:19 -08007617 * @phba: pointer to lpfc hba data structure.
7618 *
7619 * This routine walks the list of nvme buffers that have been allocated and
7620 * repost them to the port by using SGL block post. This is needed after a
7621 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7622 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
James Smart5e5b5112019-01-28 11:14:22 -08007623 * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
James Smart0794d602019-01-28 11:14:19 -08007624 *
7625 * Returns: 0 = success, non-zero failure.
7626 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07007627static int
James Smart5e5b5112019-01-28 11:14:22 -08007628lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
James Smart0794d602019-01-28 11:14:19 -08007629{
7630 LIST_HEAD(post_nblist);
7631 int num_posted, rc = 0;
7632
7633 /* get all NVME buffers need to repost to a local list */
James Smart5e5b5112019-01-28 11:14:22 -08007634 lpfc_io_buf_flush(phba, &post_nblist);
James Smart0794d602019-01-28 11:14:19 -08007635
7636 /* post the list of nvme buffer sgls to port if available */
7637 if (!list_empty(&post_nblist)) {
James Smart5e5b5112019-01-28 11:14:22 -08007638 num_posted = lpfc_sli4_post_io_sgl_list(
7639 phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
James Smart0794d602019-01-28 11:14:19 -08007640 /* failed to post any nvme buffer, return error */
7641 if (num_posted == 0)
7642 rc = -EIO;
7643 }
7644 return rc;
7645}
7646
Bart Van Assche3999df72019-03-28 11:06:16 -07007647static void
James Smart61bda8f2016-10-13 15:06:05 -07007648lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7649{
7650 uint32_t len;
7651
7652 len = sizeof(struct lpfc_mbx_set_host_data) -
7653 sizeof(struct lpfc_sli4_cfg_mhdr);
7654 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7655 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7656 LPFC_SLI4_MBX_EMBED);
7657
7658 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08007659 mbox->u.mqe.un.set_host_data.param_len =
7660 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart3b0009c2021-08-16 09:28:47 -07007661 snprintf(mbox->u.mqe.un.set_host_data.un.data,
James Smart61bda8f2016-10-13 15:06:05 -07007662 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7663 "Linux %s v"LPFC_DRIVER_VERSION,
7664 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7665}
7666
James Smarta8cf5df2017-05-15 15:20:46 -07007667int
James Smart6c621a22017-05-15 15:20:45 -07007668lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07007669 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07007670{
7671 int rc, i;
7672 struct lpfc_rqe hrqe;
7673 struct lpfc_rqe drqe;
7674 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08007675 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07007676 struct rqb_dmabuf *rqb_buffer;
7677 LIST_HEAD(rqb_buf_list);
7678
7679 rqbp = hrq->rqbp;
7680 for (i = 0; i < count; i++) {
James Smart62e3a931d2020-10-20 13:27:11 -07007681 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007682 /* IF RQ is already full, don't bother */
James Smart62e3a931d2020-10-20 13:27:11 -07007683 if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {
7684 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007685 break;
James Smart62e3a931d2020-10-20 13:27:11 -07007686 }
7687 spin_unlock_irqrestore(&phba->hbalock, flags);
7688
James Smart6c621a22017-05-15 15:20:45 -07007689 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7690 if (!rqb_buffer)
7691 break;
7692 rqb_buffer->hrq = hrq;
7693 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07007694 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07007695 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7696 }
James Smart62e3a931d2020-10-20 13:27:11 -07007697
7698 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007699 while (!list_empty(&rqb_buf_list)) {
7700 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7701 hbuf.list);
7702
7703 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7704 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7705 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7706 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7707 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7708 if (rc < 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007709 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart411de512018-01-30 15:58:52 -08007710 "6421 Cannot post to HRQ %d: %x %x %x "
7711 "DRQ %x %x\n",
7712 hrq->queue_id,
7713 hrq->host_index,
7714 hrq->hba_index,
7715 hrq->entry_count,
7716 drq->host_index,
7717 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07007718 rqbp->rqb_free_buffer(phba, rqb_buffer);
7719 } else {
7720 list_add_tail(&rqb_buffer->hbuf.list,
7721 &rqbp->rqb_buffer_list);
7722 rqbp->buffer_count++;
7723 }
7724 }
James Smart411de512018-01-30 15:58:52 -08007725 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007726 return 1;
7727}
7728
James Smart9064aeb2021-08-16 09:28:50 -07007729static void
7730lpfc_mbx_cmpl_cgn_set_ftrs(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7731{
7732 struct lpfc_vport *vport = pmb->vport;
7733 union lpfc_sli4_cfg_shdr *shdr;
7734 u32 shdr_status, shdr_add_status;
7735 u32 sig, acqe;
7736
7737 /* Two outcomes. (1) Set featurs was successul and EDC negotiation
7738 * is done. (2) Mailbox failed and send FPIN support only.
7739 */
7740 shdr = (union lpfc_sli4_cfg_shdr *)
7741 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
7742 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7743 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
7744 if (shdr_status || shdr_add_status || pmb->u.mb.mbxStatus) {
7745 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
7746 "2516 CGN SET_FEATURE mbox failed with "
7747 "status x%x add_status x%x, mbx status x%x "
7748 "Reset Congestion to FPINs only\n",
7749 shdr_status, shdr_add_status,
7750 pmb->u.mb.mbxStatus);
7751 /* If there is a mbox error, move on to RDF */
7752 phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7753 phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7754 goto out;
7755 }
7756
7757 /* Zero out Congestion Signal ACQE counter */
7758 phba->cgn_acqe_cnt = 0;
James Smart9064aeb2021-08-16 09:28:50 -07007759
7760 acqe = bf_get(lpfc_mbx_set_feature_CGN_acqe_freq,
7761 &pmb->u.mqe.un.set_feature);
7762 sig = bf_get(lpfc_mbx_set_feature_CGN_warn_freq,
7763 &pmb->u.mqe.un.set_feature);
7764 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7765 "4620 SET_FEATURES Success: Freq: %ds %dms "
7766 " Reg: x%x x%x\n", acqe, sig,
7767 phba->cgn_reg_signal, phba->cgn_reg_fpin);
7768out:
7769 mempool_free(pmb, phba->mbox_mem_pool);
7770
7771 /* Register for FPIN events from the fabric now that the
7772 * EDC common_set_features has completed.
7773 */
7774 lpfc_issue_els_rdf(vport, 0);
7775}
7776
7777int
7778lpfc_config_cgn_signal(struct lpfc_hba *phba)
7779{
7780 LPFC_MBOXQ_t *mboxq;
7781 u32 rc;
7782
7783 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7784 if (!mboxq)
7785 goto out_rdf;
7786
7787 lpfc_set_features(phba, mboxq, LPFC_SET_CGN_SIGNAL);
7788 mboxq->vport = phba->pport;
7789 mboxq->mbox_cmpl = lpfc_mbx_cmpl_cgn_set_ftrs;
7790
7791 lpfc_printf_log(phba, KERN_INFO, LOG_CGN_MGMT,
7792 "4621 SET_FEATURES: FREQ sig x%x acqe x%x: "
7793 "Reg: x%x x%x\n",
7794 phba->cgn_sig_freq, lpfc_acqe_cgn_frequency,
7795 phba->cgn_reg_signal, phba->cgn_reg_fpin);
7796
7797 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
7798 if (rc == MBX_NOT_FINISHED)
7799 goto out;
7800 return 0;
7801
7802out:
7803 mempool_free(mboxq, phba->mbox_mem_pool);
7804out_rdf:
7805 /* If there is a mbox error, move on to RDF */
7806 phba->cgn_reg_fpin = LPFC_CGN_FPIN_WARN | LPFC_CGN_FPIN_ALARM;
7807 phba->cgn_reg_signal = EDC_CG_SIG_NOTSUPPORTED;
7808 lpfc_issue_els_rdf(phba->pport, 0);
7809 return -EIO;
7810}
7811
Dick Kennedy317aeb82020-06-30 14:49:59 -07007812/**
7813 * lpfc_init_idle_stat_hb - Initialize idle_stat tracking
Lee Jones7af29d42020-07-21 17:41:31 +01007814 * @phba: pointer to lpfc hba data structure.
Dick Kennedy317aeb82020-06-30 14:49:59 -07007815 *
7816 * This routine initializes the per-cq idle_stat to dynamically dictate
7817 * polling decisions.
7818 *
7819 * Return codes:
7820 * None
7821 **/
7822static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)
7823{
7824 int i;
7825 struct lpfc_sli4_hdw_queue *hdwq;
7826 struct lpfc_queue *cq;
7827 struct lpfc_idle_stat *idle_stat;
7828 u64 wall;
7829
7830 for_each_present_cpu(i) {
7831 hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
7832 cq = hdwq->io_cq;
7833
7834 /* Skip if we've already handled this cq's primary CPU */
7835 if (cq->chann != i)
7836 continue;
7837
7838 idle_stat = &phba->sli4_hba.idle_stat[i];
7839
7840 idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);
7841 idle_stat->prev_wall = wall;
7842
James Smart9064aeb2021-08-16 09:28:50 -07007843 if (phba->nvmet_support ||
7844 phba->cmf_active_mode != LPFC_CFG_OFF)
Dick Kennedy317aeb82020-06-30 14:49:59 -07007845 cq->poll_mode = LPFC_QUEUE_WORK;
7846 else
7847 cq->poll_mode = LPFC_IRQ_POLL;
7848 }
7849
7850 if (!phba->nvmet_support)
7851 schedule_delayed_work(&phba->idle_stat_delay_work,
7852 msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
7853}
7854
Dick Kennedyf0020e42020-06-30 14:49:58 -07007855static void lpfc_sli4_dip(struct lpfc_hba *phba)
7856{
7857 uint32_t if_type;
7858
7859 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7860 if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
7861 if_type == LPFC_SLI_INTF_IF_TYPE_6) {
7862 struct lpfc_register reg_data;
7863
7864 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7865 &reg_data.word0))
7866 return;
7867
7868 if (bf_get(lpfc_sliport_status_dip, &reg_data))
James Smart0b3ad322021-01-04 10:02:39 -08007869 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedyf0020e42020-06-30 14:49:58 -07007870 "2904 Firmware Dump Image Present"
7871 " on Adapter");
7872 }
7873}
7874
James Smartc6a5c7472021-08-16 09:28:48 -07007875/**
7876 * lpfc_cmf_setup - Initialize idle_stat tracking
7877 * @phba: Pointer to HBA context object.
7878 *
7879 * This is called from HBA setup during driver load or when the HBA
7880 * comes online. this does all the initialization to support CMF and MI.
7881 **/
7882static int
7883lpfc_cmf_setup(struct lpfc_hba *phba)
7884{
7885 LPFC_MBOXQ_t *mboxq;
James Smart02243832021-08-16 09:28:54 -07007886 struct lpfc_dmabuf *mp;
James Smartc6a5c7472021-08-16 09:28:48 -07007887 struct lpfc_pc_sli4_params *sli4_params;
James Smart02243832021-08-16 09:28:54 -07007888 int rc, cmf, mi_ver;
James Smartc6a5c7472021-08-16 09:28:48 -07007889
James Smart7a1dda92021-10-20 14:14:12 -07007890 rc = lpfc_sli4_refresh_params(phba);
7891 if (unlikely(rc))
7892 return rc;
7893
James Smartc6a5c7472021-08-16 09:28:48 -07007894 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7895 if (!mboxq)
7896 return -ENOMEM;
James Smartc6a5c7472021-08-16 09:28:48 -07007897
James Smartc6a5c7472021-08-16 09:28:48 -07007898 sli4_params = &phba->sli4_hba.pc_sli4_params;
James Smartc6a5c7472021-08-16 09:28:48 -07007899
7900 /* Are we forcing MI off via module parameter? */
7901 if (!phba->cfg_enable_mi)
7902 sli4_params->mi_ver = 0;
7903
7904 /* Always try to enable MI feature if we can */
7905 if (sli4_params->mi_ver) {
7906 lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_MI);
7907 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7908 mi_ver = bf_get(lpfc_mbx_set_feature_mi,
7909 &mboxq->u.mqe.un.set_feature);
7910
7911 if (rc == MBX_SUCCESS) {
7912 if (mi_ver) {
7913 lpfc_printf_log(phba,
7914 KERN_WARNING, LOG_CGN_MGMT,
7915 "6215 MI is enabled\n");
7916 sli4_params->mi_ver = mi_ver;
7917 } else {
7918 lpfc_printf_log(phba,
7919 KERN_WARNING, LOG_CGN_MGMT,
7920 "6338 MI is disabled\n");
7921 sli4_params->mi_ver = 0;
7922 }
7923 } else {
7924 /* mi_ver is already set from GET_SLI4_PARAMETERS */
7925 lpfc_printf_log(phba, KERN_INFO,
7926 LOG_CGN_MGMT | LOG_INIT,
7927 "6245 Enable MI Mailbox x%x (x%x/x%x) "
7928 "failed, rc:x%x mi:x%x\n",
7929 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7930 lpfc_sli_config_mbox_subsys_get
7931 (phba, mboxq),
7932 lpfc_sli_config_mbox_opcode_get
7933 (phba, mboxq),
7934 rc, sli4_params->mi_ver);
7935 }
7936 } else {
7937 lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
7938 "6217 MI is disabled\n");
7939 }
7940
7941 /* Ensure FDMI is enabled for MI if enable_mi is set */
7942 if (sli4_params->mi_ver)
7943 phba->cfg_fdmi_on = LPFC_FDMI_SUPPORT;
7944
James Smart02243832021-08-16 09:28:54 -07007945 /* Always try to enable CMF feature if we can */
7946 if (sli4_params->cmf) {
7947 lpfc_set_features(phba, mboxq, LPFC_SET_ENABLE_CMF);
7948 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7949 cmf = bf_get(lpfc_mbx_set_feature_cmf,
7950 &mboxq->u.mqe.un.set_feature);
7951 if (rc == MBX_SUCCESS && cmf) {
7952 lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
7953 "6218 CMF is enabled: mode %d\n",
7954 phba->cmf_active_mode);
7955 } else {
7956 lpfc_printf_log(phba, KERN_WARNING,
7957 LOG_CGN_MGMT | LOG_INIT,
7958 "6219 Enable CMF Mailbox x%x (x%x/x%x) "
7959 "failed, rc:x%x dd:x%x\n",
7960 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7961 lpfc_sli_config_mbox_subsys_get
7962 (phba, mboxq),
7963 lpfc_sli_config_mbox_opcode_get
7964 (phba, mboxq),
7965 rc, cmf);
7966 sli4_params->cmf = 0;
7967 phba->cmf_active_mode = LPFC_CFG_OFF;
7968 goto no_cmf;
7969 }
7970
7971 /* Allocate Congestion Information Buffer */
7972 if (!phba->cgn_i) {
7973 mp = kmalloc(sizeof(*mp), GFP_KERNEL);
7974 if (mp)
7975 mp->virt = dma_alloc_coherent
7976 (&phba->pcidev->dev,
7977 sizeof(struct lpfc_cgn_info),
7978 &mp->phys, GFP_KERNEL);
7979 if (!mp || !mp->virt) {
7980 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7981 "2640 Failed to alloc memory "
7982 "for Congestion Info\n");
7983 kfree(mp);
7984 sli4_params->cmf = 0;
7985 phba->cmf_active_mode = LPFC_CFG_OFF;
7986 goto no_cmf;
7987 }
7988 phba->cgn_i = mp;
7989
7990 /* initialize congestion buffer info */
7991 lpfc_init_congestion_buf(phba);
7992 lpfc_init_congestion_stat(phba);
James Smartafd63fa2021-09-10 16:31:57 -07007993
7994 /* Zero out Congestion Signal counters */
7995 atomic64_set(&phba->cgn_acqe_stat.alarm, 0);
7996 atomic64_set(&phba->cgn_acqe_stat.warn, 0);
James Smart02243832021-08-16 09:28:54 -07007997 }
7998
7999 rc = lpfc_sli4_cgn_params_read(phba);
8000 if (rc < 0) {
8001 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8002 "6242 Error reading Cgn Params (%d)\n",
8003 rc);
8004 /* Ensure CGN Mode is off */
8005 sli4_params->cmf = 0;
8006 } else if (!rc) {
8007 lpfc_printf_log(phba, KERN_ERR, LOG_CGN_MGMT | LOG_INIT,
8008 "6243 CGN Event empty object.\n");
8009 /* Ensure CGN Mode is off */
8010 sli4_params->cmf = 0;
8011 }
8012 } else {
8013no_cmf:
8014 lpfc_printf_log(phba, KERN_WARNING, LOG_CGN_MGMT,
8015 "6220 CMF is disabled\n");
8016 }
8017
8018 /* Only register congestion buffer with firmware if BOTH
8019 * CMF and E2E are enabled.
8020 */
8021 if (sli4_params->cmf && sli4_params->mi_ver) {
8022 rc = lpfc_reg_congestion_buf(phba);
8023 if (rc) {
8024 dma_free_coherent(&phba->pcidev->dev,
8025 sizeof(struct lpfc_cgn_info),
8026 phba->cgn_i->virt, phba->cgn_i->phys);
8027 kfree(phba->cgn_i);
8028 phba->cgn_i = NULL;
8029 /* Ensure CGN Mode is off */
8030 phba->cmf_active_mode = LPFC_CFG_OFF;
8031 return 0;
8032 }
8033 }
James Smartc6a5c7472021-08-16 09:28:48 -07008034 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart02243832021-08-16 09:28:54 -07008035 "6470 Setup MI version %d CMF %d mode %d\n",
8036 sli4_params->mi_ver, sli4_params->cmf,
8037 phba->cmf_active_mode);
James Smartc6a5c7472021-08-16 09:28:48 -07008038
8039 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart8c42a652021-08-16 09:28:51 -07008040
8041 /* Initialize atomic counters */
8042 atomic_set(&phba->cgn_fabric_warn_cnt, 0);
8043 atomic_set(&phba->cgn_fabric_alarm_cnt, 0);
8044 atomic_set(&phba->cgn_sync_alarm_cnt, 0);
8045 atomic_set(&phba->cgn_sync_warn_cnt, 0);
8046 atomic_set(&phba->cgn_driver_evt_cnt, 0);
8047 atomic_set(&phba->cgn_latency_evt_cnt, 0);
8048 atomic64_set(&phba->cgn_latency_evt, 0);
8049
James Smart02243832021-08-16 09:28:54 -07008050 phba->cmf_interval_rate = LPFC_CMF_INTERVAL;
James Smart17b27ac2021-08-16 09:28:55 -07008051
8052 /* Allocate RX Monitor Buffer */
8053 if (!phba->rxtable) {
8054 phba->rxtable = kmalloc_array(LPFC_MAX_RXMONITOR_ENTRY,
8055 sizeof(struct rxtable_entry),
8056 GFP_KERNEL);
8057 if (!phba->rxtable) {
8058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8059 "2644 Failed to alloc memory "
8060 "for RX Monitor Buffer\n");
8061 return -ENOMEM;
8062 }
8063 }
8064 atomic_set(&phba->rxtable_idx_head, 0);
8065 atomic_set(&phba->rxtable_idx_tail, 0);
James Smartc6a5c7472021-08-16 09:28:48 -07008066 return 0;
8067}
8068
James Smart3b0009c2021-08-16 09:28:47 -07008069static int
8070lpfc_set_host_tm(struct lpfc_hba *phba)
8071{
8072 LPFC_MBOXQ_t *mboxq;
8073 uint32_t len, rc;
8074 struct timespec64 cur_time;
8075 struct tm broken;
8076 uint32_t month, day, year;
8077 uint32_t hour, minute, second;
8078 struct lpfc_mbx_set_host_date_time *tm;
8079
8080 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8081 if (!mboxq)
8082 return -ENOMEM;
8083
8084 len = sizeof(struct lpfc_mbx_set_host_data) -
8085 sizeof(struct lpfc_sli4_cfg_mhdr);
8086 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8087 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
8088 LPFC_SLI4_MBX_EMBED);
8089
8090 mboxq->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_DATE_TIME;
8091 mboxq->u.mqe.un.set_host_data.param_len =
8092 sizeof(struct lpfc_mbx_set_host_date_time);
8093 tm = &mboxq->u.mqe.un.set_host_data.un.tm;
8094 ktime_get_real_ts64(&cur_time);
8095 time64_to_tm(cur_time.tv_sec, 0, &broken);
8096 month = broken.tm_mon + 1;
8097 day = broken.tm_mday;
8098 year = broken.tm_year - 100;
8099 hour = broken.tm_hour;
8100 minute = broken.tm_min;
8101 second = broken.tm_sec;
8102 bf_set(lpfc_mbx_set_host_month, tm, month);
8103 bf_set(lpfc_mbx_set_host_day, tm, day);
8104 bf_set(lpfc_mbx_set_host_year, tm, year);
8105 bf_set(lpfc_mbx_set_host_hour, tm, hour);
8106 bf_set(lpfc_mbx_set_host_min, tm, minute);
8107 bf_set(lpfc_mbx_set_host_sec, tm, second);
8108
8109 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8110 mempool_free(mboxq, phba->mbox_mem_pool);
8111 return rc;
8112}
8113
James Smart8a9d2e82012-05-09 21:16:12 -04008114/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08008115 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04008116 * @phba: Pointer to HBA context object.
8117 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08008118 * This function is the main SLI4 device initialization PCI function. This
8119 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04008120 * HBA error attention handler code. Caller is not required to hold any
8121 * locks.
8122 **/
8123int
8124lpfc_sli4_hba_setup(struct lpfc_hba *phba)
8125{
James Smart171f6c42019-11-04 16:57:07 -08008126 int rc, i, cnt, len, dd;
James Smartda0436e2009-05-22 14:51:39 -04008127 LPFC_MBOXQ_t *mboxq;
8128 struct lpfc_mqe *mqe;
8129 uint8_t *vpd;
8130 uint32_t vpd_size;
8131 uint32_t ftr_rsp = 0;
8132 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
8133 struct lpfc_vport *vport = phba->pport;
8134 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08008135 struct lpfc_rqb *rqbp;
James Smartefe1dc52021-09-21 07:30:08 -07008136 u32 flg;
James Smartda0436e2009-05-22 14:51:39 -04008137
8138 /* Perform a PCI function reset to start from clean */
8139 rc = lpfc_pci_function_reset(phba);
8140 if (unlikely(rc))
8141 return -ENODEV;
8142
8143 /* Check the HBA Host Status Register for readyness */
8144 rc = lpfc_sli4_post_status_check(phba);
8145 if (unlikely(rc))
8146 return -ENODEV;
8147 else {
8148 spin_lock_irq(&phba->hbalock);
8149 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
James Smartefe1dc52021-09-21 07:30:08 -07008150 flg = phba->sli.sli_flag;
James Smartda0436e2009-05-22 14:51:39 -04008151 spin_unlock_irq(&phba->hbalock);
James Smartefe1dc52021-09-21 07:30:08 -07008152 /* Allow a little time after setting SLI_ACTIVE for any polled
8153 * MBX commands to complete via BSG.
8154 */
8155 for (i = 0; i < 50 && (flg & LPFC_SLI_MBOX_ACTIVE); i++) {
8156 msleep(20);
8157 spin_lock_irq(&phba->hbalock);
8158 flg = phba->sli.sli_flag;
8159 spin_unlock_irq(&phba->hbalock);
8160 }
James Smartda0436e2009-05-22 14:51:39 -04008161 }
8162
Dick Kennedyf0020e42020-06-30 14:49:58 -07008163 lpfc_sli4_dip(phba);
8164
James Smartda0436e2009-05-22 14:51:39 -04008165 /*
8166 * Allocate a single mailbox container for initializing the
8167 * port.
8168 */
8169 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8170 if (!mboxq)
8171 return -ENOMEM;
8172
James Smartda0436e2009-05-22 14:51:39 -04008173 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04008174 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04008175 vpd = kzalloc(vpd_size, GFP_KERNEL);
8176 if (!vpd) {
8177 rc = -ENOMEM;
8178 goto out_free_mbox;
8179 }
8180
8181 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05008182 if (unlikely(rc)) {
8183 kfree(vpd);
8184 goto out_free_mbox;
8185 }
James Smart572709e2013-07-15 18:32:43 -04008186
James Smartda0436e2009-05-22 14:51:39 -04008187 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04008188 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07008189 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05008190 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07008191 phba->fcp_embed_io = 0; /* SLI4 FC support only */
8192 } else {
James Smart76a95d72010-11-20 23:11:48 -05008193 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07008194 }
James Smart45ed1192009-10-02 15:17:02 -04008195
8196 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
8197 LPFC_DCBX_CEE_MODE)
8198 phba->hba_flag |= HBA_FIP_SUPPORT;
8199 else
8200 phba->hba_flag &= ~HBA_FIP_SUPPORT;
8201
James Smartc00f62e2019-08-14 16:57:11 -07008202 phba->hba_flag &= ~HBA_IOQ_FLUSH;
James Smart4f2e66c2012-05-09 21:17:07 -04008203
James Smartc31098c2011-04-16 11:03:33 -04008204 if (phba->sli_rev != LPFC_SLI_REV4) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008205 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04008206 "0376 READ_REV Error. SLI Level %d "
8207 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05008208 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04008209 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05008210 kfree(vpd);
8211 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04008212 }
James Smartcd1c8302011-10-10 21:33:25 -04008213
James Smart3b0009c2021-08-16 09:28:47 -07008214 rc = lpfc_set_host_tm(phba);
8215 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
8216 "6468 Set host date / time: Status x%x:\n", rc);
8217
James Smartcd1c8302011-10-10 21:33:25 -04008218 /*
James Smartff78d8f2011-12-13 13:21:35 -05008219 * Continue initialization with default values even if driver failed
8220 * to read FCoE param config regions, only read parameters if the
8221 * board is FCoE
8222 */
8223 if (phba->hba_flag & HBA_FCOE_MODE &&
8224 lpfc_sli4_read_fcoe_params(phba))
8225 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
8226 "2570 Failed to read FCoE parameters\n");
8227
8228 /*
James Smartcd1c8302011-10-10 21:33:25 -04008229 * Retrieve sli4 device physical port name, failure of doing it
8230 * is considered as non-fatal.
8231 */
8232 rc = lpfc_sli4_retrieve_pport_name(phba);
8233 if (!rc)
8234 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8235 "3080 Successful retrieving SLI4 device "
8236 "physical port name: %s.\n", phba->Port);
8237
James Smartb3b4f3e2019-03-12 16:30:23 -07008238 rc = lpfc_sli4_get_ctl_attr(phba);
8239 if (!rc)
8240 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8241 "8351 Successful retrieving SLI4 device "
8242 "CTL ATTR\n");
8243
James Smartda0436e2009-05-22 14:51:39 -04008244 /*
8245 * Evaluate the read rev and vpd data. Populate the driver
8246 * state with the results. If this routine fails, the failure
8247 * is not fatal as the driver will use generic values.
8248 */
8249 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
8250 if (unlikely(!rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008251 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04008252 "0377 Error %d parsing vpd. "
8253 "Using defaults.\n", rc);
8254 rc = 0;
8255 }
James Smart76a95d72010-11-20 23:11:48 -05008256 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04008257
James Smartf1126682009-06-10 17:22:44 -04008258 /* Save information as VPD data */
8259 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
8260 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08008261
8262 /*
8263 * This is because first G7 ASIC doesn't support the standard
8264 * 0x5a NVME cmd descriptor type/subtype
8265 */
8266 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
8267 LPFC_SLI_INTF_IF_TYPE_6) &&
8268 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
8269 (phba->vpd.rev.smRev == 0) &&
8270 (phba->cfg_nvme_embed_cmd == 1))
8271 phba->cfg_nvme_embed_cmd = 0;
8272
James Smartf1126682009-06-10 17:22:44 -04008273 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
8274 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
8275 &mqe->un.read_rev);
8276 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
8277 &mqe->un.read_rev);
8278 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
8279 &mqe->un.read_rev);
8280 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
8281 &mqe->un.read_rev);
8282 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
8283 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
8284 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
8285 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
8286 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
8287 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
8288 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8289 "(%d):0380 READ_REV Status x%x "
8290 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
8291 mboxq->vport ? mboxq->vport->vpi : 0,
8292 bf_get(lpfc_mqe_status, mqe),
8293 phba->vpd.rev.opFwName,
8294 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
8295 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04008296
James Smart65791f12016-07-06 12:35:56 -07008297 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07008298 LPFC_SLI_INTF_IF_TYPE_0) {
8299 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
8300 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8301 if (rc == MBX_SUCCESS) {
8302 phba->hba_flag |= HBA_RECOVERABLE_UE;
8303 /* Set 1Sec interval to detect UE */
8304 phba->eratt_poll_interval = 1;
8305 phba->sli4_hba.ue_to_sr = bf_get(
8306 lpfc_mbx_set_feature_UESR,
8307 &mboxq->u.mqe.un.set_feature);
8308 phba->sli4_hba.ue_to_rp = bf_get(
8309 lpfc_mbx_set_feature_UERP,
8310 &mboxq->u.mqe.un.set_feature);
8311 }
8312 }
8313
8314 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
8315 /* Enable MDS Diagnostics only if the SLI Port supports it */
8316 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
8317 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8318 if (rc != MBX_SUCCESS)
8319 phba->mds_diags_support = 0;
8320 }
James Smart572709e2013-07-15 18:32:43 -04008321
James Smartda0436e2009-05-22 14:51:39 -04008322 /*
8323 * Discover the port's supported feature set and match it against the
8324 * hosts requests.
8325 */
8326 lpfc_request_features(phba, mboxq);
8327 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8328 if (unlikely(rc)) {
8329 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05008330 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04008331 }
8332
Gaurav Srivastava5e633302021-06-08 10:05:49 +05308333 /* Disable VMID if app header is not supported */
8334 if (phba->cfg_vmid_app_header && !(bf_get(lpfc_mbx_rq_ftr_rsp_ashdr,
8335 &mqe->un.req_ftrs))) {
8336 bf_set(lpfc_ftr_ashdr, &phba->sli4_hba.sli4_flags, 0);
8337 phba->cfg_vmid_app_header = 0;
8338 lpfc_printf_log(phba, KERN_DEBUG, LOG_SLI,
8339 "1242 vmid feature not supported\n");
8340 }
8341
James Smartda0436e2009-05-22 14:51:39 -04008342 /*
8343 * The port must support FCP initiator mode as this is the
8344 * only mode running in the host.
8345 */
8346 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
8347 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8348 "0378 No support for fcpi mode.\n");
8349 ftr_rsp++;
8350 }
James Smart0bc2b7c2018-02-22 08:18:48 -08008351
8352 /* Performance Hints are ONLY for FCoE */
8353 if (phba->hba_flag & HBA_FCOE_MODE) {
8354 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
8355 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
8356 else
8357 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
8358 }
8359
James Smartda0436e2009-05-22 14:51:39 -04008360 /*
8361 * If the port cannot support the host's requested features
8362 * then turn off the global config parameters to disable the
8363 * feature in the driver. This is not a fatal error.
8364 */
James Smartf44ac122018-03-05 12:04:08 -08008365 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
8366 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
8367 phba->cfg_enable_bg = 0;
8368 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04008369 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08008370 }
James Smartbf086112011-08-21 21:48:13 -04008371 }
James Smartda0436e2009-05-22 14:51:39 -04008372
8373 if (phba->max_vpi && phba->cfg_enable_npiv &&
8374 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8375 ftr_rsp++;
8376
8377 if (ftr_rsp) {
8378 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8379 "0379 Feature Mismatch Data: x%08x %08x "
8380 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
8381 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
8382 phba->cfg_enable_npiv, phba->max_vpi);
8383 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
8384 phba->cfg_enable_bg = 0;
8385 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
8386 phba->cfg_enable_npiv = 0;
8387 }
8388
8389 /* These SLI3 features are assumed in SLI4 */
8390 spin_lock_irq(&phba->hbalock);
8391 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
8392 spin_unlock_irq(&phba->hbalock);
8393
James Smart171f6c42019-11-04 16:57:07 -08008394 /* Always try to enable dual dump feature if we can */
8395 lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
8396 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8397 dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
8398 if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
Dick Kennedy372c1872020-06-30 14:50:00 -07008399 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart171f6c42019-11-04 16:57:07 -08008400 "6448 Dual Dump is enabled\n");
8401 else
8402 lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
8403 "6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
8404 "rc:x%x dd:x%x\n",
8405 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
8406 lpfc_sli_config_mbox_subsys_get(
8407 phba, mboxq),
8408 lpfc_sli_config_mbox_opcode_get(
8409 phba, mboxq),
8410 rc, dd);
James Smart6d368e52011-05-24 11:44:12 -04008411 /*
8412 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
8413 * calls depends on these resources to complete port setup.
8414 */
8415 rc = lpfc_sli4_alloc_resource_identifiers(phba);
8416 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008417 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6d368e52011-05-24 11:44:12 -04008418 "2920 Failed to alloc Resource IDs "
8419 "rc = x%x\n", rc);
8420 goto out_free_mbox;
8421 }
8422
James Smart61bda8f2016-10-13 15:06:05 -07008423 lpfc_set_host_data(phba, mboxq);
8424
8425 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8426 if (rc) {
8427 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8428 "2134 Failed to set host os driver version %x",
8429 rc);
8430 }
8431
James Smartda0436e2009-05-22 14:51:39 -04008432 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05008433 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
8434 if (rc) {
8435 phba->link_state = LPFC_HBA_ERROR;
8436 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05008437 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05008438 }
8439
James Smartda0436e2009-05-22 14:51:39 -04008440 mboxq->vport = vport;
8441 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart3e1f0712018-11-29 16:09:29 -08008442 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04008443 if (rc == MBX_SUCCESS) {
8444 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
8445 rc = 0;
8446 }
8447
8448 /*
8449 * This memory was allocated by the lpfc_read_sparam routine. Release
8450 * it to the mbuf pool.
8451 */
8452 lpfc_mbuf_free(phba, mp->virt, mp->phys);
8453 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -08008454 mboxq->ctx_buf = NULL;
James Smartda0436e2009-05-22 14:51:39 -04008455 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008456 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04008457 "0382 READ_SPARAM command failed "
8458 "status %d, mbxStatus x%x\n",
8459 rc, bf_get(lpfc_mqe_status, mqe));
8460 phba->link_state = LPFC_HBA_ERROR;
8461 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05008462 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04008463 }
8464
James Smart05580562011-05-24 11:40:48 -04008465 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04008466
8467 /* Update the fc_host data structures with new wwn. */
8468 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
8469 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
8470
James Smart895427b2017-02-12 13:52:30 -08008471 /* Create all the SLI4 queues */
8472 rc = lpfc_sli4_queue_create(phba);
8473 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008474 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08008475 "3089 Failed to allocate queues\n");
8476 rc = -ENODEV;
8477 goto out_free_mbox;
8478 }
8479 /* Set up all the queues to the device */
8480 rc = lpfc_sli4_queue_setup(phba);
8481 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008482 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08008483 "0381 Error %d during queue setup.\n ", rc);
8484 goto out_stop_timers;
8485 }
8486 /* Initialize the driver internal SLI layer lists. */
8487 lpfc_sli4_setup(phba);
8488 lpfc_sli4_queue_init(phba);
8489
8490 /* update host els xri-sgl sizes and mappings */
8491 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04008492 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008493 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04008494 "1400 Failed to update xri-sgl size and "
8495 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08008496 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04008497 }
8498
James Smart8a9d2e82012-05-09 21:16:12 -04008499 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08008500 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
8501 phba->sli4_hba.els_xri_cnt);
8502 if (unlikely(rc < 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008503 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04008504 "0582 Error %d during els sgl post "
8505 "operation\n", rc);
8506 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08008507 goto out_destroy_queue;
8508 }
8509 phba->sli4_hba.els_xri_cnt = rc;
8510
James Smartf358dd02017-02-12 13:52:34 -08008511 if (phba->nvmet_support) {
8512 /* update host nvmet xri-sgl sizes and mappings */
8513 rc = lpfc_sli4_nvmet_sgl_update(phba);
8514 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008515 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08008516 "6308 Failed to update nvmet-sgl size "
8517 "and mapping: %d\n", rc);
8518 goto out_destroy_queue;
8519 }
8520
8521 /* register the nvmet sgl pool to the port */
8522 rc = lpfc_sli4_repost_sgl_list(
8523 phba,
8524 &phba->sli4_hba.lpfc_nvmet_sgl_list,
8525 phba->sli4_hba.nvmet_xri_cnt);
8526 if (unlikely(rc < 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008527 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08008528 "3117 Error %d during nvmet "
8529 "sgl post\n", rc);
8530 rc = -ENODEV;
8531 goto out_destroy_queue;
8532 }
8533 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07008534
James Smarta5f73372019-09-21 20:58:50 -07008535 /* We allocate an iocbq for every receive context SGL.
8536 * The additional allocation is for abort and ls handling.
8537 */
8538 cnt = phba->sli4_hba.nvmet_xri_cnt +
8539 phba->sli4_hba.max_cfg_param.max_xri;
James Smartf358dd02017-02-12 13:52:34 -08008540 } else {
James Smart0794d602019-01-28 11:14:19 -08008541 /* update host common xri-sgl sizes and mappings */
James Smart5e5b5112019-01-28 11:14:22 -08008542 rc = lpfc_sli4_io_sgl_update(phba);
James Smart895427b2017-02-12 13:52:30 -08008543 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008544 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08008545 "6082 Failed to update nvme-sgl size "
8546 "and mapping: %d\n", rc);
8547 goto out_destroy_queue;
8548 }
James Smart6c621a22017-05-15 15:20:45 -07008549
James Smart0794d602019-01-28 11:14:19 -08008550 /* register the allocated common sgl pool to the port */
James Smart5e5b5112019-01-28 11:14:22 -08008551 rc = lpfc_sli4_repost_io_sgl_list(phba);
James Smart0794d602019-01-28 11:14:19 -08008552 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008553 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -08008554 "6116 Error %d during nvme sgl post "
8555 "operation\n", rc);
8556 /* Some NVME buffers were moved to abort nvme list */
8557 /* A pci function reset will repost them */
8558 rc = -ENODEV;
8559 goto out_destroy_queue;
8560 }
James Smarta5f73372019-09-21 20:58:50 -07008561 /* Each lpfc_io_buf job structure has an iocbq element.
8562 * This cnt provides for abort, els, ct and ls requests.
8563 */
8564 cnt = phba->sli4_hba.max_cfg_param.max_xri;
James Smart11e644e2017-06-15 22:56:48 -07008565 }
8566
8567 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07008568 /* Initialize and populate the iocb list per host */
8569 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta5f73372019-09-21 20:58:50 -07008570 "2821 initialize iocb list with %d entries\n",
8571 cnt);
James Smart6c621a22017-05-15 15:20:45 -07008572 rc = lpfc_init_iocb_list(phba, cnt);
8573 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008574 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart11e644e2017-06-15 22:56:48 -07008575 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07008576 goto out_destroy_queue;
8577 }
James Smart8a9d2e82012-05-09 21:16:12 -04008578 }
8579
James Smart11e644e2017-06-15 22:56:48 -07008580 if (phba->nvmet_support)
8581 lpfc_nvmet_create_targetport(phba);
8582
James Smart2d7dbc42017-02-12 13:52:35 -08008583 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08008584 /* Post initial buffers to all RQs created */
8585 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
8586 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
8587 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
8588 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
8589 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07008590 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08008591 rqbp->buffer_count = 0;
8592
James Smart2d7dbc42017-02-12 13:52:35 -08008593 lpfc_post_rq_buffer(
8594 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
8595 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07008596 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08008597 }
8598 }
8599
James Smartda0436e2009-05-22 14:51:39 -04008600 /* Post the rpi header region to the device. */
8601 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
8602 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008603 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04008604 "0393 Error %d during rpi post operation\n",
8605 rc);
8606 rc = -ENODEV;
James Smart5aa615d2021-05-14 12:55:56 -07008607 goto out_free_iocblist;
James Smartda0436e2009-05-22 14:51:39 -04008608 }
James Smart97f2ecf2012-03-01 22:35:23 -05008609 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04008610
James Smart895427b2017-02-12 13:52:30 -08008611 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08008612 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08008613 /*
8614 * The FC Port needs to register FCFI (index 0)
8615 */
8616 lpfc_reg_fcfi(phba, mboxq);
8617 mboxq->vport = phba->pport;
8618 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8619 if (rc != MBX_SUCCESS)
8620 goto out_unset_queue;
8621 rc = 0;
8622 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
8623 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08008624 } else {
8625 /* We are a NVME Target mode with MRQ > 1 */
8626
8627 /* First register the FCFI */
8628 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
8629 mboxq->vport = phba->pport;
8630 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8631 if (rc != MBX_SUCCESS)
8632 goto out_unset_queue;
8633 rc = 0;
8634 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
8635 &mboxq->u.mqe.un.reg_fcfi_mrq);
8636
8637 /* Next register the MRQs */
8638 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
8639 mboxq->vport = phba->pport;
8640 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8641 if (rc != MBX_SUCCESS)
8642 goto out_unset_queue;
8643 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08008644 }
8645 /* Check if the port is configured to be disabled */
8646 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04008647 }
8648
James Smartc4908502019-01-28 11:14:28 -08008649 /* Don't post more new bufs if repost already recovered
8650 * the nvme sgls.
8651 */
8652 if (phba->nvmet_support == 0) {
8653 if (phba->sli4_hba.io_xri_cnt == 0) {
8654 len = lpfc_new_io_buf(
8655 phba, phba->sli4_hba.io_xri_max);
8656 if (len == 0) {
8657 rc = -ENOMEM;
8658 goto out_unset_queue;
8659 }
8660
8661 if (phba->cfg_xri_rebalancing)
8662 lpfc_create_multixri_pools(phba);
8663 }
8664 } else {
8665 phba->cfg_xri_rebalancing = 0;
8666 }
8667
James Smartda0436e2009-05-22 14:51:39 -04008668 /* Allow asynchronous mailbox command to go through */
8669 spin_lock_irq(&phba->hbalock);
8670 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8671 spin_unlock_irq(&phba->hbalock);
8672
8673 /* Post receive buffers to the device */
8674 lpfc_sli4_rb_setup(phba);
8675
James Smartfc2b9892010-02-26 14:15:29 -05008676 /* Reset HBA FCF states after HBA reset */
8677 phba->fcf.fcf_flag = 0;
8678 phba->fcf.current_rec.flag = 0;
8679
James Smartda0436e2009-05-22 14:51:39 -04008680 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04008681 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04008682 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04008683
8684 /* Start heart beat timer */
8685 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04008686 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smarta22d73b2021-01-04 10:02:38 -08008687 phba->hba_flag &= ~(HBA_HBEAT_INP | HBA_HBEAT_TMO);
James Smartda0436e2009-05-22 14:51:39 -04008688 phba->last_completion_time = jiffies;
8689
James Smart32517fc2019-01-28 11:14:33 -08008690 /* start eq_delay heartbeat */
8691 if (phba->cfg_auto_imax)
8692 queue_delayed_work(phba->wq, &phba->eq_delay_work,
8693 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
8694
Dick Kennedy317aeb82020-06-30 14:49:59 -07008695 /* start per phba idle_stat_delay heartbeat */
8696 lpfc_init_idle_stat_hb(phba);
8697
James Smartda0436e2009-05-22 14:51:39 -04008698 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04008699 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07008700 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04008701
James Smart75baf692010-06-08 18:31:21 -04008702 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
8703 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
8704 rc = pci_enable_pcie_error_reporting(phba->pcidev);
8705 if (!rc) {
8706 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8707 "2829 This device supports "
8708 "Advanced Error Reporting (AER)\n");
8709 spin_lock_irq(&phba->hbalock);
8710 phba->hba_flag |= HBA_AER_ENABLED;
8711 spin_unlock_irq(&phba->hbalock);
8712 } else {
8713 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8714 "2830 This device does not support "
8715 "Advanced Error Reporting (AER)\n");
8716 phba->cfg_aer_support = 0;
8717 }
James Smart0a96e972011-07-22 18:37:28 -04008718 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04008719 }
8720
James Smartda0436e2009-05-22 14:51:39 -04008721 /*
8722 * The port is ready, set the host's link state to LINK_DOWN
8723 * in preparation for link interrupts.
8724 */
James Smartda0436e2009-05-22 14:51:39 -04008725 spin_lock_irq(&phba->hbalock);
8726 phba->link_state = LPFC_LINK_DOWN;
James Smart1dc5ec22018-10-23 13:41:11 -07008727
8728 /* Check if physical ports are trunked */
8729 if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
8730 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
8731 if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
8732 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
8733 if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
8734 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
8735 if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
8736 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04008737 spin_unlock_irq(&phba->hbalock);
James Smart1dc5ec22018-10-23 13:41:11 -07008738
James Smarte8869f52019-03-12 16:30:18 -07008739 /* Arm the CQs and then EQs on device */
8740 lpfc_sli4_arm_cqeq_intr(phba);
8741
8742 /* Indicate device interrupt mode */
8743 phba->sli4_hba.intr_enable = 1;
8744
James Smart02243832021-08-16 09:28:54 -07008745 /* Setup CMF after HBA is initialized */
8746 lpfc_cmf_setup(phba);
8747
James Smart026abb82011-12-13 13:20:45 -05008748 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
8749 (phba->hba_flag & LINK_DISABLED)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008750 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart026abb82011-12-13 13:20:45 -05008751 "3103 Adapter Link is disabled.\n");
8752 lpfc_down_link(phba, mboxq);
8753 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8754 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008755 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart026abb82011-12-13 13:20:45 -05008756 "3104 Adapter failed to issue "
8757 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
James Smartc4908502019-01-28 11:14:28 -08008758 goto out_io_buff_free;
James Smart026abb82011-12-13 13:20:45 -05008759 }
8760 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05008761 /* don't perform init_link on SLI4 FC port loopback test */
8762 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
8763 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
8764 if (rc)
James Smartc4908502019-01-28 11:14:28 -08008765 goto out_io_buff_free;
James Smart1b511972011-12-13 13:23:09 -05008766 }
James Smart5350d872011-10-10 21:33:49 -04008767 }
8768 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart02243832021-08-16 09:28:54 -07008769
8770 phba->hba_flag |= HBA_SETUP;
James Smart5350d872011-10-10 21:33:49 -04008771 return rc;
James Smart02243832021-08-16 09:28:54 -07008772
James Smartc4908502019-01-28 11:14:28 -08008773out_io_buff_free:
8774 /* Free allocated IO Buffers */
8775 lpfc_io_free(phba);
James Smart76a95d72010-11-20 23:11:48 -05008776out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04008777 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04008778 lpfc_sli4_queue_unset(phba);
James Smart5aa615d2021-05-14 12:55:56 -07008779out_free_iocblist:
James Smart6c621a22017-05-15 15:20:45 -07008780 lpfc_free_iocb_list(phba);
James Smart5aa615d2021-05-14 12:55:56 -07008781out_destroy_queue:
James Smart5350d872011-10-10 21:33:49 -04008782 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04008783out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04008784 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04008785out_free_mbox:
8786 mempool_free(mboxq, phba->mbox_mem_pool);
8787 return rc;
8788}
James Smarte59058c2008-08-24 21:49:00 -04008789
8790/**
James Smart3621a712009-04-06 18:47:14 -04008791 * lpfc_mbox_timeout - Timeout call back function for mbox timer
Lee Jones7af29d42020-07-21 17:41:31 +01008792 * @t: Context to fetch pointer to hba structure from.
dea31012005-04-17 16:05:31 -05008793 *
James Smarte59058c2008-08-24 21:49:00 -04008794 * This is the callback function for mailbox timer. The mailbox
8795 * timer is armed when a new mailbox command is issued and the timer
8796 * is deleted when the mailbox complete. The function is called by
8797 * the kernel timer code when a mailbox does not complete within
8798 * expected time. This function wakes up the worker thread to
8799 * process the mailbox timeout and returns. All the processing is
8800 * done by the worker thread function lpfc_mbox_timeout_handler.
8801 **/
dea31012005-04-17 16:05:31 -05008802void
Kees Cookf22eb4d2017-09-06 20:24:26 -07008803lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05008804{
Kees Cookf22eb4d2017-09-06 20:24:26 -07008805 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05008806 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05008807 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05008808
James Smart2e0fef82007-06-17 19:56:36 -05008809 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008810 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05008811 if (!tmo_posted)
8812 phba->pport->work_port_events |= WORKER_MBOX_TMO;
8813 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
8814
James Smart5e9d9b82008-06-14 22:52:53 -04008815 if (!tmo_posted)
8816 lpfc_worker_wake_up(phba);
8817 return;
dea31012005-04-17 16:05:31 -05008818}
8819
James Smarte8d3c3b2013-10-10 12:21:30 -04008820/**
8821 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
8822 * are pending
8823 * @phba: Pointer to HBA context object.
8824 *
8825 * This function checks if any mailbox completions are present on the mailbox
8826 * completion queue.
8827 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04008828static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04008829lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
8830{
8831
8832 uint32_t idx;
8833 struct lpfc_queue *mcq;
8834 struct lpfc_mcqe *mcqe;
8835 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08008836 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04008837
8838 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8839 return false;
8840
8841 /* Check for completions on mailbox completion queue */
8842
8843 mcq = phba->sli4_hba.mbx_cq;
8844 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08008845 qe_valid = mcq->qe_valid;
James Smart9afbee32019-03-12 16:30:28 -07008846 while (bf_get_le32(lpfc_cqe_valid,
8847 (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
8848 mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
James Smarte8d3c3b2013-10-10 12:21:30 -04008849 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
8850 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
8851 pending_completions = true;
8852 break;
8853 }
8854 idx = (idx + 1) % mcq->entry_count;
8855 if (mcq->hba_index == idx)
8856 break;
James Smart7365f6f2018-02-22 08:18:46 -08008857
8858 /* if the index wrapped around, toggle the valid bit */
8859 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
8860 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04008861 }
8862 return pending_completions;
8863
8864}
8865
8866/**
8867 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
8868 * that were missed.
8869 * @phba: Pointer to HBA context object.
8870 *
8871 * For sli4, it is possible to miss an interrupt. As such mbox completions
8872 * maybe missed causing erroneous mailbox timeouts to occur. This function
8873 * checks to see if mbox completions are on the mailbox completion queue
8874 * and will process all the completions associated with the eq for the
8875 * mailbox completion queue.
8876 **/
YueHaibingd7b761b2019-05-31 23:28:41 +08008877static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04008878lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
8879{
James Smartb71413d2018-02-22 08:18:40 -08008880 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04008881 uint32_t eqidx;
8882 struct lpfc_queue *fpeq = NULL;
James Smart657add42019-05-21 17:49:06 -07008883 struct lpfc_queue *eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04008884 bool mbox_pending;
8885
8886 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8887 return false;
8888
James Smart657add42019-05-21 17:49:06 -07008889 /* Find the EQ associated with the mbox CQ */
8890 if (sli4_hba->hdwq) {
8891 for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
8892 eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
Daniel Wagner535fb492019-10-18 18:21:11 +02008893 if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
James Smart657add42019-05-21 17:49:06 -07008894 fpeq = eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04008895 break;
8896 }
James Smart657add42019-05-21 17:49:06 -07008897 }
8898 }
James Smarte8d3c3b2013-10-10 12:21:30 -04008899 if (!fpeq)
8900 return false;
8901
8902 /* Turn off interrupts from this EQ */
8903
James Smartb71413d2018-02-22 08:18:40 -08008904 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04008905
8906 /* Check to see if a mbox completion is pending */
8907
8908 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
8909
8910 /*
8911 * If a mbox completion is pending, process all the events on EQ
8912 * associated with the mbox completion queue (this could include
8913 * mailbox commands, async events, els commands, receive queue data
8914 * and fcp commands)
8915 */
8916
8917 if (mbox_pending)
James Smart32517fc2019-01-28 11:14:33 -08008918 /* process and rearm the EQ */
James Smart93a4d6f2019-11-04 16:57:05 -08008919 lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
James Smart32517fc2019-01-28 11:14:33 -08008920 else
8921 /* Always clear and re-arm the EQ */
8922 sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04008923
8924 return mbox_pending;
8925
8926}
James Smarte59058c2008-08-24 21:49:00 -04008927
8928/**
James Smart3621a712009-04-06 18:47:14 -04008929 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04008930 * @phba: Pointer to HBA context object.
8931 *
8932 * This function is called from worker thread when a mailbox command times out.
8933 * The caller is not required to hold any locks. This function will reset the
8934 * HBA and recover all the pending commands.
8935 **/
dea31012005-04-17 16:05:31 -05008936void
8937lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
8938{
James Smart2e0fef82007-06-17 19:56:36 -05008939 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04008940 MAILBOX_t *mb = NULL;
8941
James Smart1dcb58e2007-04-25 09:51:30 -04008942 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008943
James Smart9ec58ec2021-01-04 10:02:35 -08008944 /* If the mailbox completed, process the completion */
8945 lpfc_sli4_process_missed_mbox_completions(phba);
8946
8947 if (!(psli->sli_flag & LPFC_SLI_ACTIVE))
James Smarte8d3c3b2013-10-10 12:21:30 -04008948 return;
8949
James Smarteb016562014-09-03 12:58:06 -04008950 if (pmbox != NULL)
8951 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04008952 /* Check the pmbox pointer first. There is a race condition
8953 * between the mbox timeout handler getting executed in the
8954 * worklist and the mailbox actually completing. When this
8955 * race condition occurs, the mbox_active will be NULL.
8956 */
8957 spin_lock_irq(&phba->hbalock);
8958 if (pmbox == NULL) {
8959 lpfc_printf_log(phba, KERN_WARNING,
8960 LOG_MBOX | LOG_SLI,
8961 "0353 Active Mailbox cleared - mailbox timeout "
8962 "exiting\n");
8963 spin_unlock_irq(&phba->hbalock);
8964 return;
8965 }
8966
dea31012005-04-17 16:05:31 -05008967 /* Mbox cmd <mbxCommand> timeout */
Dick Kennedy372c1872020-06-30 14:50:00 -07008968 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07008969 "0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008970 mb->mbxCommand,
8971 phba->pport->port_state,
8972 phba->sli.sli_flag,
8973 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04008974 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008975
James Smart1dcb58e2007-04-25 09:51:30 -04008976 /* Setting state unknown so lpfc_sli_abort_iocb_ring
8977 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008978 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04008979 */
James Smart2e0fef82007-06-17 19:56:36 -05008980 spin_lock_irq(&phba->pport->work_port_lock);
8981 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8982 spin_unlock_irq(&phba->pport->work_port_lock);
8983 spin_lock_irq(&phba->hbalock);
8984 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04008985 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008986 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04008987
Dick Kennedy372c1872020-06-30 14:50:00 -07008988 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart76bb24e2007-10-27 13:38:00 -04008989 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04008990
8991 /* Reset the HBA device */
8992 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05008993}
8994
James Smarte59058c2008-08-24 21:49:00 -04008995/**
James Smart3772a992009-05-22 14:50:54 -04008996 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04008997 * @phba: Pointer to HBA context object.
8998 * @pmbox: Pointer to mailbox object.
8999 * @flag: Flag indicating how the mailbox need to be processed.
9000 *
9001 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04009002 * to submit a mailbox command to firmware with SLI-3 interface spec. This
9003 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04009004 * The mailbox command can be submitted in polling mode, in which case
9005 * this function will wait in a polling loop for the completion of the
9006 * mailbox.
9007 * If the mailbox is submitted in no_wait mode (not polling) the
9008 * function will submit the command and returns immediately without waiting
9009 * for the mailbox completion. The no_wait is supported only when HBA
9010 * is in SLI2/SLI3 mode - interrupts are enabled.
9011 * The SLI interface allows only one mailbox pending at a time. If the
9012 * mailbox is issued in polling mode and there is already a mailbox
9013 * pending, then the function will return an error. If the mailbox is issued
9014 * in NO_WAIT mode and there is a mailbox pending already, the function
9015 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
9016 * The sli layer owns the mailbox object until the completion of mailbox
9017 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
9018 * return codes the caller owns the mailbox command after the return of
9019 * the function.
9020 **/
James Smart3772a992009-05-22 14:50:54 -04009021static int
9022lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
9023 uint32_t flag)
dea31012005-04-17 16:05:31 -05009024{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009025 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05009026 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009027 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05009028 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05009029 int i;
James Smart09372822008-01-11 01:52:54 -05009030 unsigned long timeout;
dea31012005-04-17 16:05:31 -05009031 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04009032 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05009033 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04009034 int processing_queue = 0;
9035
9036 spin_lock_irqsave(&phba->hbalock, drvr_flag);
9037 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04009038 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04009039 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04009040 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9041 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9042 return MBX_SUCCESS;
9043 }
James Smart58da1ff2008-04-07 10:15:56 -04009044 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04009045 pmbox = lpfc_mbox_get(phba);
9046 if (!pmbox) {
9047 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9048 return MBX_SUCCESS;
9049 }
9050 }
dea31012005-04-17 16:05:31 -05009051
James Smarted957682007-06-17 19:56:37 -05009052 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05009053 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05009054 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04009055 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05009056 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05009057 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04009058 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04009059 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05009060 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04009061 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05009062 }
9063 }
9064
Linas Vepstas8d63f372007-02-14 14:28:36 -06009065 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04009066 if (unlikely(pci_channel_offline(phba->pcidev))) {
9067 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9068 goto out_not_finished;
9069 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06009070
James Smarta257bf92009-04-06 18:48:10 -04009071 /* If HBA has a deferred error attention, fail the iocb. */
9072 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9073 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9074 goto out_not_finished;
9075 }
9076
dea31012005-04-17 16:05:31 -05009077 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05009078
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009079 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05009080 status = MBX_SUCCESS;
9081
James Smart2e0fef82007-06-17 19:56:36 -05009082 if (phba->link_state == LPFC_HBA_ERROR) {
9083 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05009084
9085 /* Mbox command <mbxCommand> cannot issue */
Dick Kennedy372c1872020-06-30 14:50:00 -07009086 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04009087 "(%d):0311 Mailbox command x%x cannot "
9088 "issue Data: x%x x%x\n",
9089 pmbox->vport ? pmbox->vport->vpi : 0,
9090 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04009091 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05009092 }
9093
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009094 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05009095 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
9096 !(hc_copy & HC_MBINT_ENA)) {
9097 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Dick Kennedy372c1872020-06-30 14:50:00 -07009098 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04009099 "(%d):2528 Mailbox command x%x cannot "
9100 "issue Data: x%x x%x\n",
9101 pmbox->vport ? pmbox->vport->vpi : 0,
9102 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05009103 goto out_not_finished;
9104 }
James Smart92908312006-03-07 15:04:13 -05009105 }
9106
dea31012005-04-17 16:05:31 -05009107 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9108 /* Polling for a mbox command when another one is already active
9109 * is not allowed in SLI. Also, the driver must have established
9110 * SLI2 mode to queue and process multiple mbox commands.
9111 */
9112
9113 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05009114 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05009115
9116 /* Mbox command <mbxCommand> cannot issue */
Dick Kennedy372c1872020-06-30 14:50:00 -07009117 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04009118 "(%d):2529 Mailbox command x%x "
9119 "cannot issue Data: x%x x%x\n",
9120 pmbox->vport ? pmbox->vport->vpi : 0,
9121 pmbox->u.mb.mbxCommand,
9122 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04009123 goto out_not_finished;
dea31012005-04-17 16:05:31 -05009124 }
9125
James Smart3772a992009-05-22 14:50:54 -04009126 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05009127 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05009128 /* Mbox command <mbxCommand> cannot issue */
Dick Kennedy372c1872020-06-30 14:50:00 -07009129 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04009130 "(%d):2530 Mailbox command x%x "
9131 "cannot issue Data: x%x x%x\n",
9132 pmbox->vport ? pmbox->vport->vpi : 0,
9133 pmbox->u.mb.mbxCommand,
9134 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04009135 goto out_not_finished;
dea31012005-04-17 16:05:31 -05009136 }
9137
dea31012005-04-17 16:05:31 -05009138 /* Another mailbox command is still being processed, queue this
9139 * command to be processed later.
9140 */
9141 lpfc_mbox_put(phba, pmbox);
9142
9143 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05009144 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009145 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009146 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05009147 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07009148 mbx->mbxCommand,
9149 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05009150 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05009151
9152 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05009153 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05009154
James Smart858c9f62007-06-17 19:56:39 -05009155 if (pmbox->vport) {
9156 lpfc_debugfs_disc_trc(pmbox->vport,
9157 LPFC_DISC_TRC_MBOX_VPORT,
9158 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009159 (uint32_t)mbx->mbxCommand,
9160 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05009161 }
9162 else {
9163 lpfc_debugfs_disc_trc(phba->pport,
9164 LPFC_DISC_TRC_MBOX,
9165 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009166 (uint32_t)mbx->mbxCommand,
9167 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05009168 }
9169
James Smart2e0fef82007-06-17 19:56:36 -05009170 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05009171 }
9172
dea31012005-04-17 16:05:31 -05009173 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9174
9175 /* If we are not polling, we MUST be in SLI2 mode */
9176 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04009177 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009178 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05009179 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05009180 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05009181 /* Mbox command <mbxCommand> cannot issue */
Dick Kennedy372c1872020-06-30 14:50:00 -07009182 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04009183 "(%d):2531 Mailbox command x%x "
9184 "cannot issue Data: x%x x%x\n",
9185 pmbox->vport ? pmbox->vport->vpi : 0,
9186 pmbox->u.mb.mbxCommand,
9187 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04009188 goto out_not_finished;
dea31012005-04-17 16:05:31 -05009189 }
9190 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04009191 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
9192 1000);
9193 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05009194 }
9195
9196 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05009197 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04009198 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05009199 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009200 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07009201 mbx->mbxCommand,
9202 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05009203 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05009204
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009205 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05009206 if (pmbox->vport) {
9207 lpfc_debugfs_disc_trc(pmbox->vport,
9208 LPFC_DISC_TRC_MBOX_VPORT,
9209 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009210 (uint32_t)mbx->mbxCommand,
9211 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05009212 }
9213 else {
9214 lpfc_debugfs_disc_trc(phba->pport,
9215 LPFC_DISC_TRC_MBOX,
9216 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009217 (uint32_t)mbx->mbxCommand,
9218 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05009219 }
9220 }
9221
dea31012005-04-17 16:05:31 -05009222 psli->slistat.mbox_cmd++;
9223 evtctr = psli->slistat.mbox_event;
9224
9225 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009226 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05009227
James Smart3772a992009-05-22 14:50:54 -04009228 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04009229 /* Populate mbox extension offset word. */
9230 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009231 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04009232 = (uint8_t *)phba->mbox_ext
9233 - (uint8_t *)phba->mbox;
9234 }
9235
9236 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08009237 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
9238 lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
9239 (uint8_t *)phba->mbox_ext,
9240 pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04009241 }
9242 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009243 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05009244 } else {
James Smart7a470272010-03-15 11:25:20 -04009245 /* Populate mbox extension offset word. */
9246 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009247 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04009248 = MAILBOX_HBA_EXT_OFFSET;
9249
9250 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08009251 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -04009252 lpfc_memcpy_to_slim(phba->MBslimaddr +
9253 MAILBOX_HBA_EXT_OFFSET,
James Smart3e1f0712018-11-29 16:09:29 -08009254 pmbox->ctx_buf, pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04009255
James Smart895427b2017-02-12 13:52:30 -08009256 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05009257 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08009258 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
9259 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05009260
9261 /* First copy mbox command data to HBA SLIM, skip past first
9262 word */
9263 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009264 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05009265 MAILBOX_CMD_SIZE - sizeof (uint32_t));
9266
9267 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009268 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05009269 to_slim = phba->MBslimaddr;
9270 writel(ldata, to_slim);
9271 readl(to_slim); /* flush */
9272
James Smart895427b2017-02-12 13:52:30 -08009273 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05009274 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04009275 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05009276 }
9277
9278 wmb();
dea31012005-04-17 16:05:31 -05009279
9280 switch (flag) {
9281 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05009282 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05009283 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05009284 /* Interrupt board to do it */
9285 writel(CA_MBATT, phba->CAregaddr);
9286 readl(phba->CAregaddr); /* flush */
9287 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05009288 break;
9289
9290 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05009291 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05009292 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05009293 /* Interrupt board to do it */
9294 writel(CA_MBATT, phba->CAregaddr);
9295 readl(phba->CAregaddr); /* flush */
9296
James Smart3772a992009-05-22 14:50:54 -04009297 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05009298 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04009299 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05009300 word0 = le32_to_cpu(word0);
9301 } else {
9302 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05009303 if (lpfc_readl(phba->MBslimaddr, &word0)) {
9304 spin_unlock_irqrestore(&phba->hbalock,
9305 drvr_flag);
9306 goto out_not_finished;
9307 }
dea31012005-04-17 16:05:31 -05009308 }
9309
9310 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05009311 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9312 spin_unlock_irqrestore(&phba->hbalock,
9313 drvr_flag);
9314 goto out_not_finished;
9315 }
James Smarta183a152011-10-10 21:32:43 -04009316 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
9317 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05009318 i = 0;
dea31012005-04-17 16:05:31 -05009319 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05009320 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
9321 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05009322 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05009323 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05009324 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05009325 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05009326 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04009327 goto out_not_finished;
dea31012005-04-17 16:05:31 -05009328 }
9329
9330 /* Check if we took a mbox interrupt while we were
9331 polling */
9332 if (((word0 & OWN_CHIP) != OWN_CHIP)
9333 && (evtctr != psli->slistat.mbox_event))
9334 break;
9335
James Smart09372822008-01-11 01:52:54 -05009336 if (i++ > 10) {
9337 spin_unlock_irqrestore(&phba->hbalock,
9338 drvr_flag);
9339 msleep(1);
9340 spin_lock_irqsave(&phba->hbalock, drvr_flag);
9341 }
dea31012005-04-17 16:05:31 -05009342
James Smart3772a992009-05-22 14:50:54 -04009343 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05009344 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04009345 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05009346 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009347 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05009348 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04009349 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05009350 /* Check real SLIM for any errors */
9351 slimword0 = readl(phba->MBslimaddr);
9352 slimmb = (MAILBOX_t *) & slimword0;
9353 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
9354 && slimmb->mbxStatus) {
9355 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04009356 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05009357 word0 = slimword0;
9358 }
9359 }
9360 } else {
9361 /* First copy command data */
9362 word0 = readl(phba->MBslimaddr);
9363 }
9364 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05009365 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
9366 spin_unlock_irqrestore(&phba->hbalock,
9367 drvr_flag);
9368 goto out_not_finished;
9369 }
dea31012005-04-17 16:05:31 -05009370 }
9371
James Smart3772a992009-05-22 14:50:54 -04009372 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05009373 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08009374 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
9375 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04009376 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08009377 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
James Smart7a470272010-03-15 11:25:20 -04009378 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -08009379 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04009380 pmbox->out_ext_byte_len);
9381 }
dea31012005-04-17 16:05:31 -05009382 } else {
9383 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009384 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08009385 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04009386 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08009387 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
9388 lpfc_memcpy_from_slim(
9389 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04009390 phba->MBslimaddr +
9391 MAILBOX_HBA_EXT_OFFSET,
9392 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05009393 }
9394 }
9395
9396 writel(HA_MBATT, phba->HAregaddr);
9397 readl(phba->HAregaddr); /* flush */
9398
9399 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08009400 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05009401 }
9402
James Smart2e0fef82007-06-17 19:56:36 -05009403 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
9404 return status;
James Smart58da1ff2008-04-07 10:15:56 -04009405
9406out_not_finished:
9407 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04009408 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04009409 lpfc_mbox_cmpl_put(phba, pmbox);
9410 }
9411 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05009412}
9413
James Smarte59058c2008-08-24 21:49:00 -04009414/**
James Smartf1126682009-06-10 17:22:44 -04009415 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
9416 * @phba: Pointer to HBA context object.
9417 *
9418 * The function blocks the posting of SLI4 asynchronous mailbox commands from
9419 * the driver internal pending mailbox queue. It will then try to wait out the
9420 * possible outstanding mailbox command before return.
9421 *
9422 * Returns:
9423 * 0 - the outstanding mailbox command completed; otherwise, the wait for
9424 * the outstanding mailbox command timed out.
9425 **/
9426static int
9427lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
9428{
9429 struct lpfc_sli *psli = &phba->sli;
James Smarta9978e32021-07-07 11:43:43 -07009430 LPFC_MBOXQ_t *mboxq;
James Smartf1126682009-06-10 17:22:44 -04009431 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04009432 unsigned long timeout = 0;
James Smarta9978e32021-07-07 11:43:43 -07009433 u32 sli_flag;
9434 u8 cmd, subsys, opcode;
James Smartf1126682009-06-10 17:22:44 -04009435
9436 /* Mark the asynchronous mailbox command posting as blocked */
9437 spin_lock_irq(&phba->hbalock);
9438 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04009439 /* Determine how long we might wait for the active mailbox
9440 * command to be gracefully completed by firmware.
9441 */
James Smarta183a152011-10-10 21:32:43 -04009442 if (phba->sli.mbox_active)
9443 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9444 phba->sli.mbox_active) *
9445 1000) + jiffies;
9446 spin_unlock_irq(&phba->hbalock);
9447
James Smarte8d3c3b2013-10-10 12:21:30 -04009448 /* Make sure the mailbox is really active */
9449 if (timeout)
9450 lpfc_sli4_process_missed_mbox_completions(phba);
9451
James Smarta9978e32021-07-07 11:43:43 -07009452 /* Wait for the outstanding mailbox command to complete */
James Smartf1126682009-06-10 17:22:44 -04009453 while (phba->sli.mbox_active) {
9454 /* Check active mailbox complete status every 2ms */
9455 msleep(2);
9456 if (time_after(jiffies, timeout)) {
James Smarta9978e32021-07-07 11:43:43 -07009457 /* Timeout, mark the outstanding cmd not complete */
9458
9459 /* Sanity check sli.mbox_active has not completed or
9460 * cancelled from another context during last 2ms sleep,
9461 * so take hbalock to be sure before logging.
9462 */
9463 spin_lock_irq(&phba->hbalock);
9464 if (phba->sli.mbox_active) {
9465 mboxq = phba->sli.mbox_active;
9466 cmd = mboxq->u.mb.mbxCommand;
9467 subsys = lpfc_sli_config_mbox_subsys_get(phba,
9468 mboxq);
9469 opcode = lpfc_sli_config_mbox_opcode_get(phba,
9470 mboxq);
9471 sli_flag = psli->sli_flag;
9472 spin_unlock_irq(&phba->hbalock);
9473 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9474 "2352 Mailbox command x%x "
9475 "(x%x/x%x) sli_flag x%x could "
9476 "not complete\n",
9477 cmd, subsys, opcode,
9478 sli_flag);
9479 } else {
9480 spin_unlock_irq(&phba->hbalock);
9481 }
9482
James Smartf1126682009-06-10 17:22:44 -04009483 rc = 1;
9484 break;
9485 }
9486 }
9487
9488 /* Can not cleanly block async mailbox command, fails it */
9489 if (rc) {
9490 spin_lock_irq(&phba->hbalock);
9491 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9492 spin_unlock_irq(&phba->hbalock);
9493 }
9494 return rc;
9495}
9496
9497/**
9498 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
9499 * @phba: Pointer to HBA context object.
9500 *
9501 * The function unblocks and resume posting of SLI4 asynchronous mailbox
9502 * commands from the driver internal pending mailbox queue. It makes sure
9503 * that there is no outstanding mailbox command before resuming posting
9504 * asynchronous mailbox commands. If, for any reason, there is outstanding
9505 * mailbox command, it will try to wait it out before resuming asynchronous
9506 * mailbox command posting.
9507 **/
9508static void
9509lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
9510{
9511 struct lpfc_sli *psli = &phba->sli;
9512
9513 spin_lock_irq(&phba->hbalock);
9514 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9515 /* Asynchronous mailbox posting is not blocked, do nothing */
9516 spin_unlock_irq(&phba->hbalock);
9517 return;
9518 }
9519
9520 /* Outstanding synchronous mailbox command is guaranteed to be done,
9521 * successful or timeout, after timing-out the outstanding mailbox
9522 * command shall always be removed, so just unblock posting async
9523 * mailbox command and resume
9524 */
9525 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
9526 spin_unlock_irq(&phba->hbalock);
9527
Colin Ian King291c2542019-12-18 08:43:01 +00009528 /* wake up worker thread to post asynchronous mailbox command */
James Smartf1126682009-06-10 17:22:44 -04009529 lpfc_worker_wake_up(phba);
9530}
9531
9532/**
James Smart2d843ed2012-09-29 11:29:06 -04009533 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
9534 * @phba: Pointer to HBA context object.
9535 * @mboxq: Pointer to mailbox object.
9536 *
9537 * The function waits for the bootstrap mailbox register ready bit from
9538 * port for twice the regular mailbox command timeout value.
9539 *
9540 * 0 - no timeout on waiting for bootstrap mailbox register ready.
9541 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
9542 **/
9543static int
9544lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9545{
9546 uint32_t db_ready;
9547 unsigned long timeout;
9548 struct lpfc_register bmbx_reg;
9549
9550 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
9551 * 1000) + jiffies;
9552
9553 do {
9554 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
9555 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
9556 if (!db_ready)
James Smarte2ffe4d2019-03-12 16:30:15 -07009557 mdelay(2);
James Smart2d843ed2012-09-29 11:29:06 -04009558
9559 if (time_after(jiffies, timeout))
9560 return MBXERR_ERROR;
9561 } while (!db_ready);
9562
9563 return 0;
9564}
9565
9566/**
James Smartda0436e2009-05-22 14:51:39 -04009567 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
9568 * @phba: Pointer to HBA context object.
9569 * @mboxq: Pointer to mailbox object.
9570 *
9571 * The function posts a mailbox to the port. The mailbox is expected
9572 * to be comletely filled in and ready for the port to operate on it.
9573 * This routine executes a synchronous completion operation on the
9574 * mailbox by polling for its completion.
9575 *
9576 * The caller must not be holding any locks when calling this routine.
9577 *
9578 * Returns:
9579 * MBX_SUCCESS - mailbox posted successfully
9580 * Any of the MBX error values.
9581 **/
9582static int
9583lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
9584{
9585 int rc = MBX_SUCCESS;
9586 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04009587 uint32_t mcqe_status;
9588 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04009589 struct lpfc_sli *psli = &phba->sli;
9590 struct lpfc_mqe *mb = &mboxq->u.mqe;
9591 struct lpfc_bmbx_create *mbox_rgn;
9592 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04009593
9594 /*
9595 * Only one mailbox can be active to the bootstrap mailbox region
9596 * at a time and there is no queueing provided.
9597 */
9598 spin_lock_irqsave(&phba->hbalock, iflag);
9599 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9600 spin_unlock_irqrestore(&phba->hbalock, iflag);
Dick Kennedy372c1872020-06-30 14:50:00 -07009601 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta183a152011-10-10 21:32:43 -04009602 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04009603 "cannot issue Data: x%x x%x\n",
9604 mboxq->vport ? mboxq->vport->vpi : 0,
9605 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009606 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9607 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009608 psli->sli_flag, MBX_POLL);
9609 return MBXERR_ERROR;
9610 }
9611 /* The server grabs the token and owns it until release */
9612 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9613 phba->sli.mbox_active = mboxq;
9614 spin_unlock_irqrestore(&phba->hbalock, iflag);
9615
James Smart2d843ed2012-09-29 11:29:06 -04009616 /* wait for bootstrap mbox register for readyness */
9617 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9618 if (rc)
9619 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04009620 /*
9621 * Initialize the bootstrap memory region to avoid stale data areas
9622 * in the mailbox post. Then copy the caller's mailbox contents to
9623 * the bmbx mailbox region.
9624 */
9625 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
9626 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07009627 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
9628 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04009629
9630 /* Post the high mailbox dma address to the port and wait for ready. */
9631 dma_address = &phba->sli4_hba.bmbx.dma_address;
9632 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
9633
James Smart2d843ed2012-09-29 11:29:06 -04009634 /* wait for bootstrap mbox register for hi-address write done */
9635 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9636 if (rc)
9637 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04009638
9639 /* Post the low mailbox dma address to the port. */
9640 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04009641
James Smart2d843ed2012-09-29 11:29:06 -04009642 /* wait for bootstrap mbox register for low address write done */
9643 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
9644 if (rc)
9645 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04009646
9647 /*
9648 * Read the CQ to ensure the mailbox has completed.
9649 * If so, update the mailbox status so that the upper layers
9650 * can complete the request normally.
9651 */
James Smart48f8fdb2018-05-04 20:37:51 -07009652 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
9653 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04009654 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07009655 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
9656 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04009657 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04009658 /*
9659 * When the CQE status indicates a failure and the mailbox status
9660 * indicates success then copy the CQE status into the mailbox status
9661 * (and prefix it with x4000).
9662 */
James Smartda0436e2009-05-22 14:51:39 -04009663 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04009664 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
9665 bf_set(lpfc_mqe_status, mb,
9666 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04009667 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04009668 } else
9669 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04009670
9671 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04009672 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04009673 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
9674 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04009675 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
9676 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9677 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009678 bf_get(lpfc_mqe_status, mb),
9679 mb->un.mb_words[0], mb->un.mb_words[1],
9680 mb->un.mb_words[2], mb->un.mb_words[3],
9681 mb->un.mb_words[4], mb->un.mb_words[5],
9682 mb->un.mb_words[6], mb->un.mb_words[7],
9683 mb->un.mb_words[8], mb->un.mb_words[9],
9684 mb->un.mb_words[10], mb->un.mb_words[11],
9685 mb->un.mb_words[12], mboxq->mcqe.word0,
9686 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
9687 mboxq->mcqe.trailer);
9688exit:
9689 /* We are holding the token, no needed for lock when release */
9690 spin_lock_irqsave(&phba->hbalock, iflag);
9691 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9692 phba->sli.mbox_active = NULL;
9693 spin_unlock_irqrestore(&phba->hbalock, iflag);
9694 return rc;
9695}
9696
9697/**
9698 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
9699 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01009700 * @mboxq: Pointer to mailbox object.
James Smartda0436e2009-05-22 14:51:39 -04009701 * @flag: Flag indicating how the mailbox need to be processed.
9702 *
9703 * This function is called by discovery code and HBA management code to submit
9704 * a mailbox command to firmware with SLI-4 interface spec.
9705 *
9706 * Return codes the caller owns the mailbox command after the return of the
9707 * function.
9708 **/
9709static int
9710lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
9711 uint32_t flag)
9712{
9713 struct lpfc_sli *psli = &phba->sli;
9714 unsigned long iflags;
9715 int rc;
9716
James Smartb76f2dc2011-07-22 18:37:42 -04009717 /* dump from issue mailbox command if setup */
9718 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
9719
James Smart8fa38512009-07-19 10:01:03 -04009720 rc = lpfc_mbox_dev_check(phba);
9721 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009722 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta183a152011-10-10 21:32:43 -04009723 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04009724 "cannot issue Data: x%x x%x\n",
9725 mboxq->vport ? mboxq->vport->vpi : 0,
9726 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009727 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9728 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04009729 psli->sli_flag, flag);
9730 goto out_not_finished;
9731 }
9732
James Smartda0436e2009-05-22 14:51:39 -04009733 /* Detect polling mode and jump to a handler */
9734 if (!phba->sli4_hba.intr_enable) {
9735 if (flag == MBX_POLL)
9736 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
9737 else
9738 rc = -EIO;
9739 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04009740 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04009741 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04009742 "(x%x/x%x) failure: "
9743 "mqe_sta: x%x mcqe_sta: x%x/x%x "
James Smartefe1dc52021-09-21 07:30:08 -07009744 "Data: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04009745 mboxq->vport ? mboxq->vport->vpi : 0,
9746 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009747 lpfc_sli_config_mbox_subsys_get(phba,
9748 mboxq),
9749 lpfc_sli_config_mbox_opcode_get(phba,
9750 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04009751 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
9752 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
9753 bf_get(lpfc_mcqe_ext_status,
9754 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04009755 psli->sli_flag, flag);
9756 return rc;
9757 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04009758 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
9759 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08009760 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04009761 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04009762 mboxq->vport ? mboxq->vport->vpi : 0,
9763 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009764 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9765 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009766 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04009767 /* Try to block the asynchronous mailbox posting */
9768 rc = lpfc_sli4_async_mbox_block(phba);
9769 if (!rc) {
9770 /* Successfully blocked, now issue sync mbox cmd */
9771 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
9772 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04009773 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04009774 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04009775 "(%d):2597 Sync Mailbox command "
9776 "x%x (x%x/x%x) failure: "
9777 "mqe_sta: x%x mcqe_sta: x%x/x%x "
James Smartefe1dc52021-09-21 07:30:08 -07009778 "Data: x%x x%x\n",
James Smartcc459f12012-05-09 21:18:30 -04009779 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04009780 mboxq->u.mb.mbxCommand,
9781 lpfc_sli_config_mbox_subsys_get(phba,
9782 mboxq),
9783 lpfc_sli_config_mbox_opcode_get(phba,
9784 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04009785 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
9786 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
9787 bf_get(lpfc_mcqe_ext_status,
9788 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04009789 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04009790 /* Unblock the async mailbox posting afterward */
9791 lpfc_sli4_async_mbox_unblock(phba);
9792 }
9793 return rc;
James Smartda0436e2009-05-22 14:51:39 -04009794 }
9795
Colin Ian King291c2542019-12-18 08:43:01 +00009796 /* Now, interrupt mode asynchronous mailbox command */
James Smartda0436e2009-05-22 14:51:39 -04009797 rc = lpfc_mbox_cmd_check(phba, mboxq);
9798 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009799 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta183a152011-10-10 21:32:43 -04009800 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04009801 "cannot issue Data: x%x x%x\n",
9802 mboxq->vport ? mboxq->vport->vpi : 0,
9803 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009804 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9805 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009806 psli->sli_flag, flag);
9807 goto out_not_finished;
9808 }
James Smartda0436e2009-05-22 14:51:39 -04009809
9810 /* Put the mailbox command to the driver internal FIFO */
9811 psli->slistat.mbox_busy++;
9812 spin_lock_irqsave(&phba->hbalock, iflags);
9813 lpfc_mbox_put(phba, mboxq);
9814 spin_unlock_irqrestore(&phba->hbalock, iflags);
9815 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9816 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04009817 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04009818 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
9819 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04009820 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9821 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009822 phba->pport->port_state,
9823 psli->sli_flag, MBX_NOWAIT);
9824 /* Wake up worker thread to transport mailbox command from head */
9825 lpfc_worker_wake_up(phba);
9826
9827 return MBX_BUSY;
9828
9829out_not_finished:
9830 return MBX_NOT_FINISHED;
9831}
9832
9833/**
9834 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
9835 * @phba: Pointer to HBA context object.
9836 *
9837 * This function is called by worker thread to send a mailbox command to
9838 * SLI4 HBA firmware.
9839 *
9840 **/
9841int
9842lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
9843{
9844 struct lpfc_sli *psli = &phba->sli;
9845 LPFC_MBOXQ_t *mboxq;
9846 int rc = MBX_SUCCESS;
9847 unsigned long iflags;
9848 struct lpfc_mqe *mqe;
9849 uint32_t mbx_cmnd;
9850
9851 /* Check interrupt mode before post async mailbox command */
9852 if (unlikely(!phba->sli4_hba.intr_enable))
9853 return MBX_NOT_FINISHED;
9854
9855 /* Check for mailbox command service token */
9856 spin_lock_irqsave(&phba->hbalock, iflags);
9857 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9858 spin_unlock_irqrestore(&phba->hbalock, iflags);
9859 return MBX_NOT_FINISHED;
9860 }
9861 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9862 spin_unlock_irqrestore(&phba->hbalock, iflags);
9863 return MBX_NOT_FINISHED;
9864 }
9865 if (unlikely(phba->sli.mbox_active)) {
9866 spin_unlock_irqrestore(&phba->hbalock, iflags);
Dick Kennedy372c1872020-06-30 14:50:00 -07009867 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009868 "0384 There is pending active mailbox cmd\n");
9869 return MBX_NOT_FINISHED;
9870 }
9871 /* Take the mailbox command service token */
9872 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9873
9874 /* Get the next mailbox command from head of queue */
9875 mboxq = lpfc_mbox_get(phba);
9876
9877 /* If no more mailbox command waiting for post, we're done */
9878 if (!mboxq) {
9879 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9880 spin_unlock_irqrestore(&phba->hbalock, iflags);
9881 return MBX_SUCCESS;
9882 }
9883 phba->sli.mbox_active = mboxq;
9884 spin_unlock_irqrestore(&phba->hbalock, iflags);
9885
9886 /* Check device readiness for posting mailbox command */
9887 rc = lpfc_mbox_dev_check(phba);
9888 if (unlikely(rc))
9889 /* Driver clean routine will clean up pending mailbox */
9890 goto out_not_finished;
9891
9892 /* Prepare the mbox command to be posted */
9893 mqe = &mboxq->u.mqe;
9894 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
9895
9896 /* Start timer for the mbox_tmo and log some mailbox post messages */
9897 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04009898 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04009899
9900 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04009901 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04009902 "x%x x%x\n",
9903 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04009904 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9905 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009906 phba->pport->port_state, psli->sli_flag);
9907
9908 if (mbx_cmnd != MBX_HEARTBEAT) {
9909 if (mboxq->vport) {
9910 lpfc_debugfs_disc_trc(mboxq->vport,
9911 LPFC_DISC_TRC_MBOX_VPORT,
9912 "MBOX Send vport: cmd:x%x mb:x%x x%x",
9913 mbx_cmnd, mqe->un.mb_words[0],
9914 mqe->un.mb_words[1]);
9915 } else {
9916 lpfc_debugfs_disc_trc(phba->pport,
9917 LPFC_DISC_TRC_MBOX,
9918 "MBOX Send: cmd:x%x mb:x%x x%x",
9919 mbx_cmnd, mqe->un.mb_words[0],
9920 mqe->un.mb_words[1]);
9921 }
9922 }
9923 psli->slistat.mbox_cmd++;
9924
9925 /* Post the mailbox command to the port */
9926 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
9927 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009928 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta183a152011-10-10 21:32:43 -04009929 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04009930 "cannot issue Data: x%x x%x\n",
9931 mboxq->vport ? mboxq->vport->vpi : 0,
9932 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009933 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9934 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009935 psli->sli_flag, MBX_NOWAIT);
9936 goto out_not_finished;
9937 }
9938
9939 return rc;
9940
9941out_not_finished:
9942 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05009943 if (phba->sli.mbox_active) {
9944 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9945 __lpfc_mbox_cmpl_put(phba, mboxq);
9946 /* Release the token */
9947 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9948 phba->sli.mbox_active = NULL;
9949 }
James Smartda0436e2009-05-22 14:51:39 -04009950 spin_unlock_irqrestore(&phba->hbalock, iflags);
9951
9952 return MBX_NOT_FINISHED;
9953}
9954
9955/**
9956 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
9957 * @phba: Pointer to HBA context object.
9958 * @pmbox: Pointer to mailbox object.
9959 * @flag: Flag indicating how the mailbox need to be processed.
9960 *
9961 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
9962 * the API jump table function pointer from the lpfc_hba struct.
9963 *
9964 * Return codes the caller owns the mailbox command after the return of the
9965 * function.
9966 **/
9967int
9968lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
9969{
9970 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
9971}
9972
9973/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009974 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04009975 * @phba: The hba struct for which this call is being executed.
9976 * @dev_grp: The HBA PCI-Device group number.
9977 *
9978 * This routine sets up the mbox interface API function jump table in @phba
9979 * struct.
9980 * Returns: 0 - success, -ENODEV - failure.
9981 **/
9982int
9983lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9984{
9985
9986 switch (dev_grp) {
9987 case LPFC_PCI_DEV_LP:
9988 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
9989 phba->lpfc_sli_handle_slow_ring_event =
9990 lpfc_sli_handle_slow_ring_event_s3;
9991 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
9992 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
9993 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
9994 break;
9995 case LPFC_PCI_DEV_OC:
9996 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
9997 phba->lpfc_sli_handle_slow_ring_event =
9998 lpfc_sli_handle_slow_ring_event_s4;
9999 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
10000 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
10001 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
10002 break;
10003 default:
James Smarta5160742021-10-20 14:14:10 -070010004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartda0436e2009-05-22 14:51:39 -040010005 "1420 Invalid HBA PCI-device group: 0x%x\n",
10006 dev_grp);
10007 return -ENODEV;
James Smartda0436e2009-05-22 14:51:39 -040010008 }
10009 return 0;
10010}
10011
10012/**
James Smart3621a712009-04-06 18:47:14 -040010013 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -040010014 * @phba: Pointer to HBA context object.
10015 * @pring: Pointer to driver SLI ring object.
10016 * @piocb: Pointer to address of newly added command iocb.
10017 *
James Smart27f3efd2019-10-18 14:18:19 -070010018 * This function is called with hbalock held for SLI3 ports or
10019 * the ring lock held for SLI4 ports to add a command
James Smarte59058c2008-08-24 21:49:00 -040010020 * iocb to the txq when SLI layer cannot submit the command iocb
10021 * to the ring.
10022 **/
James Smart2a9bf3d2010-06-07 15:24:45 -040010023void
James Smart92d7f7b2007-06-17 19:56:38 -050010024__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010025 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -050010026{
James Smart27f3efd2019-10-18 14:18:19 -070010027 if (phba->sli_rev == LPFC_SLI_REV4)
10028 lockdep_assert_held(&pring->ring_lock);
10029 else
10030 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010031 /* Insert the caller's iocb in the txq tail for later processing. */
10032 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -050010033}
10034
James Smarte59058c2008-08-24 21:49:00 -040010035/**
James Smart3621a712009-04-06 18:47:14 -040010036 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -040010037 * @phba: Pointer to HBA context object.
10038 * @pring: Pointer to driver SLI ring object.
10039 * @piocb: Pointer to address of newly added command iocb.
10040 *
10041 * This function is called with hbalock held before a new
10042 * iocb is submitted to the firmware. This function checks
10043 * txq to flush the iocbs in txq to Firmware before
10044 * submitting new iocbs to the Firmware.
10045 * If there are iocbs in the txq which need to be submitted
10046 * to firmware, lpfc_sli_next_iocb returns the first element
10047 * of the txq after dequeuing it from txq.
10048 * If there is no iocb in the txq then the function will return
10049 * *piocb and *piocb is set to NULL. Caller needs to check
10050 * *piocb to find if there are more commands in the txq.
10051 **/
dea31012005-04-17 16:05:31 -050010052static struct lpfc_iocbq *
10053lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010054 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -050010055{
10056 struct lpfc_iocbq * nextiocb;
10057
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010058 lockdep_assert_held(&phba->hbalock);
10059
dea31012005-04-17 16:05:31 -050010060 nextiocb = lpfc_sli_ringtx_get(phba, pring);
10061 if (!nextiocb) {
10062 nextiocb = *piocb;
10063 *piocb = NULL;
10064 }
10065
10066 return nextiocb;
10067}
10068
James Smarte59058c2008-08-24 21:49:00 -040010069/**
James Smart3772a992009-05-22 14:50:54 -040010070 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -040010071 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -040010072 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -040010073 * @piocb: Pointer to command iocb.
10074 * @flag: Flag indicating if this command can be put into txq.
10075 *
James Smart3772a992009-05-22 14:50:54 -040010076 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
10077 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
10078 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
10079 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
10080 * this function allows only iocbs for posting buffers. This function finds
10081 * next available slot in the command ring and posts the command to the
10082 * available slot and writes the port attention register to request HBA start
10083 * processing new iocb. If there is no slot available in the ring and
10084 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
10085 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -040010086 *
James Smart3772a992009-05-22 14:50:54 -040010087 * This function is called with hbalock held. The function will return success
10088 * after it successfully submit the iocb to firmware or after adding to the
10089 * txq.
James Smarte59058c2008-08-24 21:49:00 -040010090 **/
James Smart98c9ea52007-10-27 13:37:33 -040010091static int
James Smart3772a992009-05-22 14:50:54 -040010092__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -050010093 struct lpfc_iocbq *piocb, uint32_t flag)
10094{
10095 struct lpfc_iocbq *nextiocb;
10096 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -080010097 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050010098
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010099 lockdep_assert_held(&phba->hbalock);
10100
James Smart92d7f7b2007-06-17 19:56:38 -050010101 if (piocb->iocb_cmpl && (!piocb->vport) &&
10102 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
10103 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010104 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040010105 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -050010106 piocb->iocb.ulpCommand);
10107 dump_stack();
10108 return IOCB_ERROR;
10109 }
10110
10111
Linas Vepstas8d63f372007-02-14 14:28:36 -060010112 /* If the PCI channel is in offline state, do not post iocbs. */
10113 if (unlikely(pci_channel_offline(phba->pcidev)))
10114 return IOCB_ERROR;
10115
James Smarta257bf92009-04-06 18:48:10 -040010116 /* If HBA has a deferred error attention, fail the iocb. */
10117 if (unlikely(phba->hba_flag & DEFER_ERATT))
10118 return IOCB_ERROR;
10119
dea31012005-04-17 16:05:31 -050010120 /*
10121 * We should never get an IOCB if we are in a < LINK_DOWN state
10122 */
James Smart2e0fef82007-06-17 19:56:36 -050010123 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -050010124 return IOCB_ERROR;
10125
10126 /*
10127 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -040010128 * outstanding event.
dea31012005-04-17 16:05:31 -050010129 */
James Smart0b727fe2007-10-27 13:37:25 -040010130 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -050010131 goto iocb_busy;
10132
James Smart2e0fef82007-06-17 19:56:36 -050010133 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -050010134 /*
James Smart2680eea2007-04-25 09:52:55 -040010135 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -050010136 * can be issued if the link is not up.
10137 */
10138 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -040010139 case CMD_GEN_REQUEST64_CR:
10140 case CMD_GEN_REQUEST64_CX:
10141 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
10142 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -040010143 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -040010144 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
10145 MENLO_TRANSPORT_TYPE))
10146
10147 goto iocb_busy;
10148 break;
dea31012005-04-17 16:05:31 -050010149 case CMD_QUE_RING_BUF_CN:
10150 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -050010151 /*
10152 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
10153 * completion, iocb_cmpl MUST be 0.
10154 */
10155 if (piocb->iocb_cmpl)
10156 piocb->iocb_cmpl = NULL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050010157 fallthrough;
dea31012005-04-17 16:05:31 -050010158 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -040010159 case CMD_CLOSE_XRI_CN:
10160 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -050010161 break;
10162 default:
10163 goto iocb_busy;
10164 }
10165
10166 /*
10167 * For FCP commands, we must be in a state where we can process link
10168 * attention events.
10169 */
James Smart895427b2017-02-12 13:52:30 -080010170 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -050010171 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -050010172 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -050010173 }
dea31012005-04-17 16:05:31 -050010174
dea31012005-04-17 16:05:31 -050010175 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
10176 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
10177 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
10178
10179 if (iocb)
10180 lpfc_sli_update_ring(phba, pring);
10181 else
10182 lpfc_sli_update_full_ring(phba, pring);
10183
10184 if (!piocb)
10185 return IOCB_SUCCESS;
10186
10187 goto out_busy;
10188
10189 iocb_busy:
10190 pring->stats.iocb_cmd_delay++;
10191
10192 out_busy:
10193
10194 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -050010195 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -050010196 return IOCB_SUCCESS;
10197 }
10198
10199 return IOCB_BUSY;
10200}
10201
James Smart3772a992009-05-22 14:50:54 -040010202/**
James Smart4f774512009-05-22 14:52:35 -040010203 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
10204 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010010205 * @piocbq: Pointer to command iocb.
James Smart4f774512009-05-22 14:52:35 -040010206 * @sglq: Pointer to the scatter gather queue object.
10207 *
10208 * This routine converts the bpl or bde that is in the IOCB
10209 * to a sgl list for the sli4 hardware. The physical address
10210 * of the bpl/bde is converted back to a virtual address.
10211 * If the IOCB contains a BPL then the list of BDE's is
10212 * converted to sli4_sge's. If the IOCB contains a single
10213 * BDE then it is converted to a single sli_sge.
10214 * The IOCB is still in cpu endianess so the contents of
10215 * the bpl can be used without byte swapping.
10216 *
10217 * Returns valid XRI = Success, NO_XRI = Failure.
10218**/
10219static uint16_t
10220lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
10221 struct lpfc_sglq *sglq)
10222{
10223 uint16_t xritag = NO_XRI;
10224 struct ulp_bde64 *bpl = NULL;
10225 struct ulp_bde64 bde;
10226 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -050010227 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -040010228 IOCB_t *icmd;
10229 int numBdes = 0;
10230 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -050010231 uint32_t offset = 0; /* accumulated offset in the sg request list */
10232 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -040010233
10234 if (!piocbq || !sglq)
10235 return xritag;
10236
10237 sgl = (struct sli4_sge *)sglq->sgl;
10238 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -050010239 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
10240 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040010241 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
10242 numBdes = icmd->un.genreq64.bdl.bdeSize /
10243 sizeof(struct ulp_bde64);
10244 /* The addrHigh and addrLow fields within the IOCB
10245 * have not been byteswapped yet so there is no
10246 * need to swap them back.
10247 */
James Smart1b511972011-12-13 13:23:09 -050010248 if (piocbq->context3)
10249 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
10250 else
10251 return xritag;
James Smart4f774512009-05-22 14:52:35 -040010252
James Smart1b511972011-12-13 13:23:09 -050010253 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -040010254 if (!bpl)
10255 return xritag;
10256
10257 for (i = 0; i < numBdes; i++) {
10258 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -050010259 sgl->addr_hi = bpl->addrHigh;
10260 sgl->addr_lo = bpl->addrLow;
10261
James Smart05580562011-05-24 11:40:48 -040010262 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -040010263 if ((i+1) == numBdes)
10264 bf_set(lpfc_sli4_sge_last, sgl, 1);
10265 else
10266 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -050010267 /* swap the size field back to the cpu so we
10268 * can assign it to the sgl.
10269 */
10270 bde.tus.w = le32_to_cpu(bpl->tus.w);
10271 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -050010272 /* The offsets in the sgl need to be accumulated
10273 * separately for the request and reply lists.
10274 * The request is always first, the reply follows.
10275 */
10276 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
10277 /* add up the reply sg entries */
10278 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
10279 inbound++;
10280 /* first inbound? reset the offset */
10281 if (inbound == 1)
10282 offset = 0;
10283 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -040010284 bf_set(lpfc_sli4_sge_type, sgl,
10285 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -050010286 offset += bde.tus.f.bdeSize;
10287 }
James Smart546fc852011-03-11 16:06:29 -050010288 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -040010289 bpl++;
10290 sgl++;
10291 }
10292 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
10293 /* The addrHigh and addrLow fields of the BDE have not
10294 * been byteswapped yet so they need to be swapped
10295 * before putting them in the sgl.
10296 */
10297 sgl->addr_hi =
10298 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
10299 sgl->addr_lo =
10300 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -040010301 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -040010302 bf_set(lpfc_sli4_sge_last, sgl, 1);
10303 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -050010304 sgl->sge_len =
10305 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -040010306 }
10307 return sglq->sli4_xritag;
10308}
10309
10310/**
Lee Jones8514e2f2021-03-03 14:46:18 +000010311 * lpfc_sli4_iocb2wqe - Convert the IOCB to a work queue entry.
James Smart4f774512009-05-22 14:52:35 -040010312 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010010313 * @iocbq: Pointer to command iocb.
James Smart4f774512009-05-22 14:52:35 -040010314 * @wqe: Pointer to the work queue entry.
10315 *
10316 * This routine converts the iocb command to its Work Queue Entry
10317 * equivalent. The wqe pointer should not have any fields set when
10318 * this routine is called because it will memcpy over them.
10319 * This routine does not set the CQ_ID or the WQEC bits in the
10320 * wqe.
10321 *
10322 * Returns: 0 = Success, IOCB_ERROR = Failure.
10323 **/
10324static int
10325lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -080010326 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -040010327{
James Smart5ffc2662009-11-18 15:39:44 -050010328 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -040010329 uint8_t ct = 0;
10330 uint32_t fip;
10331 uint32_t abort_tag;
10332 uint8_t command_type = ELS_COMMAND_NON_FIP;
10333 uint8_t cmnd;
10334 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -040010335 uint16_t abrt_iotag;
10336 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -040010337 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -040010338 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -050010339 int numBdes, i;
10340 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -040010341 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -050010342 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -050010343 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -040010344
James Smart45ed1192009-10-02 15:17:02 -040010345 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -040010346 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -040010347 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -040010348 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -050010349 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -040010350 command_type = ELS_COMMAND_FIP;
10351 else
10352 command_type = ELS_COMMAND_NON_FIP;
10353
James Smartb5c53952016-03-31 14:12:30 -070010354 if (phba->fcp_embed_io)
10355 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -040010356 /* Some of the fields are in the right position already */
10357 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smarte62245d2019-08-14 16:57:08 -070010358 /* The ct field has moved so reset */
10359 wqe->generic.wqe_com.word7 = 0;
10360 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -070010361
10362 abort_tag = (uint32_t) iocbq->iotag;
10363 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040010364 /* words0-2 bpl convert bde */
10365 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -050010366 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
10367 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -040010368 bpl = (struct ulp_bde64 *)
10369 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
10370 if (!bpl)
10371 return IOCB_ERROR;
10372
10373 /* Should already be byte swapped. */
10374 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
10375 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
10376 /* swap the size field back to the cpu so we
10377 * can assign it to the sgl.
10378 */
10379 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -050010380 xmit_len = wqe->generic.bde.tus.f.bdeSize;
10381 total_len = 0;
10382 for (i = 0; i < numBdes; i++) {
10383 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
10384 total_len += bde.tus.f.bdeSize;
10385 }
James Smart4f774512009-05-22 14:52:35 -040010386 } else
James Smart5ffc2662009-11-18 15:39:44 -050010387 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -040010388
10389 iocbq->iocb.ulpIoTag = iocbq->iotag;
10390 cmnd = iocbq->iocb.ulpCommand;
10391
10392 switch (iocbq->iocb.ulpCommand) {
10393 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -040010394 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
10395 ndlp = iocbq->context_un.ndlp;
10396 else
10397 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -040010398 if (!iocbq->iocb.ulpLe) {
Dick Kennedy372c1872020-06-30 14:50:00 -070010399 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040010400 "2007 Only Limited Edition cmd Format"
10401 " supported 0x%x\n",
10402 iocbq->iocb.ulpCommand);
10403 return IOCB_ERROR;
10404 }
James Smartff78d8f2011-12-13 13:21:35 -050010405
James Smart5ffc2662009-11-18 15:39:44 -050010406 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -040010407 /* Els_reguest64 has a TMO */
10408 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
10409 iocbq->iocb.ulpTimeout);
10410 /* Need a VF for word 4 set the vf bit*/
10411 bf_set(els_req64_vf, &wqe->els_req, 0);
10412 /* And a VFID for word 12 */
10413 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -040010414 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -040010415 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
10416 iocbq->iocb.ulpContext);
10417 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
10418 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -040010419 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -050010420 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -050010421 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
10422 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -050010423 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
10424 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -050010425 if_type = bf_get(lpfc_sli_intf_if_type,
10426 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -080010427 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -050010428 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -050010429 *pcmd == ELS_CMD_SCR ||
James Smartdf3fe762020-02-10 09:31:55 -080010430 *pcmd == ELS_CMD_RDF ||
James Smart9064aeb2021-08-16 09:28:50 -070010431 *pcmd == ELS_CMD_EDC ||
James Smartf60cb932019-05-14 14:58:05 -070010432 *pcmd == ELS_CMD_RSCN_XMT ||
James Smart6b5151f2012-01-18 16:24:06 -050010433 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -050010434 *pcmd == ELS_CMD_LOGO ||
Gaurav Srivastavaf56e86a2021-06-08 10:05:53 +053010435 *pcmd == ELS_CMD_QFPA ||
10436 *pcmd == ELS_CMD_UVEM ||
James Smartff78d8f2011-12-13 13:21:35 -050010437 *pcmd == ELS_CMD_PLOGI)) {
10438 bf_set(els_req64_sp, &wqe->els_req, 1);
10439 bf_set(els_req64_sid, &wqe->els_req,
10440 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -040010441 if ((*pcmd == ELS_CMD_FLOGI) &&
10442 !(phba->fc_topology ==
10443 LPFC_TOPOLOGY_LOOP))
10444 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -050010445 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
10446 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -040010447 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -050010448 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -050010449 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
10450 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
10451 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
10452 }
James Smartc8685952009-11-18 15:39:16 -050010453 }
James Smart6d368e52011-05-24 11:44:12 -040010454 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
10455 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -040010456 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
10457 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
10458 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
10459 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
10460 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
10461 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -040010462 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -040010463 break;
James Smart5ffc2662009-11-18 15:39:44 -050010464 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -040010465 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
10466 iocbq->iocb.un.ulpWord[3]);
10467 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -040010468 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050010469 /* The entire sequence is transmitted for this IOCB */
10470 xmit_len = total_len;
10471 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -050010472 if (phba->link_flag & LS_LOOPBACK_MODE)
10473 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050010474 fallthrough;
James Smart4f774512009-05-22 14:52:35 -040010475 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -040010476 /* word3 iocb=io_tag32 wqe=reserved */
10477 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -040010478 /* word4 relative_offset memcpy */
10479 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -040010480 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
10481 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
10482 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
10483 LPFC_WQE_IOD_WRITE);
10484 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
10485 LPFC_WQE_LENLOC_WORD12);
10486 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -050010487 wqe->xmit_sequence.xmit_len = xmit_len;
10488 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -040010489 break;
James Smart4f774512009-05-22 14:52:35 -040010490 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -040010491 /* word3 iocb=iotag32 wqe=seq_payload_len */
10492 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -040010493 /* word4 iocb=rsvd wqe=rsvd */
10494 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
10495 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -040010496 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -040010497 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -040010498 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
10499 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
10500 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
10501 LPFC_WQE_LENLOC_WORD3);
10502 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -040010503 break;
James Smart4f774512009-05-22 14:52:35 -040010504 case CMD_FCP_IWRITE64_CR:
10505 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -040010506 /* word3 iocb=iotag wqe=payload_offset_len */
10507 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -040010508 bf_set(payload_offset_len, &wqe->fcp_iwrite,
10509 xmit_len + sizeof(struct fcp_rsp));
10510 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
10511 0);
James Smartf0d9bcc2010-10-22 11:07:09 -040010512 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
10513 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
10514 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
10515 iocbq->iocb.ulpFCP2Rcvy);
10516 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
10517 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -040010518 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
10519 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
10520 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -040010521 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -050010522 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -050010523 if (iocbq->iocb_flag & LPFC_IO_OAS) {
10524 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -070010525 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
10526 if (iocbq->priority) {
10527 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
10528 (iocbq->priority << 1));
10529 } else {
James Smart1ba981f2014-02-20 09:56:45 -050010530 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
10531 (phba->cfg_XLanePriority << 1));
10532 }
10533 }
James Smartb5c53952016-03-31 14:12:30 -070010534 /* Note, word 10 is already initialized to 0 */
10535
James Smart414abe02018-06-26 08:24:26 -070010536 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
10537 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -080010538 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
10539 else
10540 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
10541
James Smartb5c53952016-03-31 14:12:30 -070010542 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -080010543 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -070010544 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -070010545 struct fcp_cmnd *fcp_cmnd;
10546 uint32_t *ptr;
10547
10548 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -070010549
10550 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -080010551 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -070010552 fcp_cmnd = lpfc_cmd->fcp_cmnd;
10553
10554 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -080010555 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -070010556 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -080010557 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10558 wqe->generic.bde.addrHigh = 0;
10559 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -070010560
James Smart205e8242018-03-05 12:04:03 -080010561 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10562 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -070010563
10564 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -080010565 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -070010566 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10567 }
James Smart7851fe22011-07-22 18:36:52 -040010568 break;
James Smartf0d9bcc2010-10-22 11:07:09 -040010569 case CMD_FCP_IREAD64_CR:
10570 /* word3 iocb=iotag wqe=payload_offset_len */
10571 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -040010572 bf_set(payload_offset_len, &wqe->fcp_iread,
10573 xmit_len + sizeof(struct fcp_rsp));
10574 bf_set(cmd_buff_len, &wqe->fcp_iread,
10575 0);
James Smartf0d9bcc2010-10-22 11:07:09 -040010576 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
10577 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
10578 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
10579 iocbq->iocb.ulpFCP2Rcvy);
10580 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -040010581 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -040010582 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
10583 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
10584 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -040010585 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -050010586 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -050010587 if (iocbq->iocb_flag & LPFC_IO_OAS) {
10588 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -070010589 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
10590 if (iocbq->priority) {
10591 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
10592 (iocbq->priority << 1));
10593 } else {
James Smart1ba981f2014-02-20 09:56:45 -050010594 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
10595 (phba->cfg_XLanePriority << 1));
10596 }
10597 }
James Smartb5c53952016-03-31 14:12:30 -070010598 /* Note, word 10 is already initialized to 0 */
10599
James Smart414abe02018-06-26 08:24:26 -070010600 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
10601 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -080010602 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
10603 else
10604 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
10605
James Smartb5c53952016-03-31 14:12:30 -070010606 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -080010607 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -070010608 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -070010609 struct fcp_cmnd *fcp_cmnd;
10610 uint32_t *ptr;
10611
10612 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -070010613
10614 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -080010615 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -070010616 fcp_cmnd = lpfc_cmd->fcp_cmnd;
10617
10618 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -080010619 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -070010620 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -080010621 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10622 wqe->generic.bde.addrHigh = 0;
10623 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -070010624
James Smart205e8242018-03-05 12:04:03 -080010625 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
10626 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -070010627
10628 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -080010629 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -070010630 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10631 }
James Smart7851fe22011-07-22 18:36:52 -040010632 break;
James Smartf1126682009-06-10 17:22:44 -040010633 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -040010634 /* word3 iocb=iotag wqe=payload_offset_len */
10635 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
10636 bf_set(payload_offset_len, &wqe->fcp_icmd,
10637 xmit_len + sizeof(struct fcp_rsp));
10638 bf_set(cmd_buff_len, &wqe->fcp_icmd,
10639 0);
James Smartf0d9bcc2010-10-22 11:07:09 -040010640 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -040010641 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -040010642 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -040010643 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
10644 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
10645 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
10646 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
10647 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -040010648 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
10649 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -050010650 if (iocbq->iocb_flag & LPFC_IO_OAS) {
10651 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -070010652 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
10653 if (iocbq->priority) {
10654 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
10655 (iocbq->priority << 1));
10656 } else {
James Smart1ba981f2014-02-20 09:56:45 -050010657 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
10658 (phba->cfg_XLanePriority << 1));
10659 }
10660 }
James Smartb5c53952016-03-31 14:12:30 -070010661 /* Note, word 10 is already initialized to 0 */
10662
10663 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -080010664 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -070010665 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -070010666 struct fcp_cmnd *fcp_cmnd;
10667 uint32_t *ptr;
10668
10669 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -070010670
10671 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -080010672 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -070010673 fcp_cmnd = lpfc_cmd->fcp_cmnd;
10674
10675 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -080010676 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -070010677 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -080010678 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10679 wqe->generic.bde.addrHigh = 0;
10680 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -070010681
James Smart205e8242018-03-05 12:04:03 -080010682 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
10683 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -070010684
10685 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -080010686 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -070010687 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10688 }
James Smart7851fe22011-07-22 18:36:52 -040010689 break;
James Smart4f774512009-05-22 14:52:35 -040010690 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -050010691 /* For this command calculate the xmit length of the
10692 * request bde.
10693 */
10694 xmit_len = 0;
10695 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
10696 sizeof(struct ulp_bde64);
10697 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -050010698 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -050010699 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
10700 break;
James Smart63e801c2010-11-20 23:14:19 -050010701 xmit_len += bde.tus.f.bdeSize;
10702 }
James Smartf0d9bcc2010-10-22 11:07:09 -040010703 /* word3 iocb=IO_TAG wqe=request_payload_len */
10704 wqe->gen_req.request_payload_len = xmit_len;
10705 /* word4 iocb=parameter wqe=relative_offset memcpy */
10706 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -040010707 /* word6 context tag copied in memcpy */
10708 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
10709 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
Dick Kennedy372c1872020-06-30 14:50:00 -070010710 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040010711 "2015 Invalid CT %x command 0x%x\n",
10712 ct, iocbq->iocb.ulpCommand);
10713 return IOCB_ERROR;
10714 }
James Smartf0d9bcc2010-10-22 11:07:09 -040010715 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
10716 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
10717 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
10718 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
10719 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
10720 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
10721 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
10722 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -040010723 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -040010724 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -040010725 break;
James Smart4f774512009-05-22 14:52:35 -040010726 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -040010727 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -040010728 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -040010729 /* word3 iocb=iotag32 wqe=response_payload_len */
10730 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -040010731 /* word4 */
10732 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -040010733 /* word5 iocb=rsvd wge=did */
10734 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -040010735 iocbq->iocb.un.xseq64.xmit_els_remoteID);
10736
10737 if_type = bf_get(lpfc_sli_intf_if_type,
10738 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -080010739 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -040010740 if (iocbq->vport->fc_flag & FC_PT2PT) {
10741 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
10742 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
10743 iocbq->vport->fc_myDID);
10744 if (iocbq->vport->fc_myDID == Fabric_DID) {
10745 bf_set(wqe_els_did,
10746 &wqe->xmit_els_rsp.wqe_dest, 0);
10747 }
10748 }
10749 }
James Smartf0d9bcc2010-10-22 11:07:09 -040010750 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
10751 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
10752 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
10753 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -040010754 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -040010755 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -040010756 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -040010757 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -040010758 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
10759 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
10760 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
10761 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
10762 LPFC_WQE_LENLOC_WORD3);
10763 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -040010764 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
10765 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -050010766 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -040010767 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
10768 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -050010769 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -040010770 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
10771 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -050010772 phba->vpi_ids[phba->pport->vpi]);
10773 }
James Smart4f774512009-05-22 14:52:35 -040010774 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -040010775 break;
James Smart4f774512009-05-22 14:52:35 -040010776 case CMD_CLOSE_XRI_CN:
10777 case CMD_ABORT_XRI_CN:
10778 case CMD_ABORT_XRI_CX:
10779 /* words 0-2 memcpy should be 0 rserved */
10780 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -040010781 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
10782 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
10783 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
10784 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
10785 } else
10786 fip = 0;
10787
10788 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -040010789 /*
James Smartdcf2a4e2010-09-29 11:18:53 -040010790 * The link is down, or the command was ELS_FIP
10791 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -040010792 * on the wire.
10793 */
10794 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
10795 else
10796 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
10797 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -040010798 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
10799 wqe->abort_cmd.rsrvd5 = 0;
10800 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -040010801 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
10802 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -040010803 /*
10804 * The abort handler will send us CMD_ABORT_XRI_CN or
10805 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
10806 */
James Smartf0d9bcc2010-10-22 11:07:09 -040010807 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10808 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
10809 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
10810 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -040010811 cmnd = CMD_ABORT_XRI_CX;
10812 command_type = OTHER_COMMAND;
10813 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -040010814 break;
James Smart6669f9b2009-10-02 15:16:45 -040010815 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -050010816 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -050010817 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -040010818 * we re-construct this WQE here based on information in
10819 * iocbq from scratch.
10820 */
James Smartd9f492a2019-08-14 16:57:04 -070010821 memset(wqe, 0, sizeof(*wqe));
James Smart5ffc2662009-11-18 15:39:44 -050010822 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -040010823 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -050010824 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
10825 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -050010826 LPFC_ABTS_UNSOL_INT) {
10827 /* ABTS sent by initiator to CT exchange, the
10828 * RX_ID field will be filled with the newly
10829 * allocated responder XRI.
10830 */
10831 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10832 iocbq->sli4_xritag);
10833 } else {
10834 /* ABTS sent by responder to CT exchange, the
10835 * RX_ID field will be filled with the responder
10836 * RX_ID from ABTS.
10837 */
10838 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -050010839 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -050010840 }
James Smart6669f9b2009-10-02 15:16:45 -040010841 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
10842 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -050010843
10844 /* Use CT=VPI */
10845 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
10846 ndlp->nlp_DID);
10847 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
10848 iocbq->iocb.ulpContext);
10849 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -040010850 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -050010851 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -040010852 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
10853 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
10854 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -040010855 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
10856 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -050010857 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
10858 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
10859 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
10860 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
10861 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
10862 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
10863 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
10864 }
10865
James Smart7851fe22011-07-22 18:36:52 -040010866 break;
James Smartae9e28f2017-05-15 15:20:51 -070010867 case CMD_SEND_FRAME:
James Smarte62245d2019-08-14 16:57:08 -070010868 bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_SEND_FRAME);
10869 bf_set(wqe_sof, &wqe->generic.wqe_com, 0x2E); /* SOF byte */
10870 bf_set(wqe_eof, &wqe->generic.wqe_com, 0x41); /* EOF byte */
10871 bf_set(wqe_lenloc, &wqe->generic.wqe_com, 1);
10872 bf_set(wqe_xbl, &wqe->generic.wqe_com, 1);
10873 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10874 bf_set(wqe_xc, &wqe->generic.wqe_com, 1);
10875 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, 0xA);
10876 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smartae9e28f2017-05-15 15:20:51 -070010877 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10878 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10879 return 0;
James Smart4f774512009-05-22 14:52:35 -040010880 case CMD_XRI_ABORTED_CX:
10881 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -040010882 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
10883 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
10884 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
10885 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
10886 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070010887 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040010888 "2014 Invalid command 0x%x\n",
10889 iocbq->iocb.ulpCommand);
10890 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -040010891 }
James Smart6d368e52011-05-24 11:44:12 -040010892
James Smart8012cc32012-10-31 14:44:49 -040010893 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
10894 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
10895 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
10896 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
10897 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
10898 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
10899 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
10900 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -040010901 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10902 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10903 wqe->generic.wqe_com.abort_tag = abort_tag;
10904 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
10905 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
10906 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
10907 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -040010908 return 0;
10909}
10910
10911/**
James Smart47ff4c52020-11-15 11:26:41 -080010912 * __lpfc_sli_issue_fcp_io_s3 - SLI3 device for sending fcp io iocb
10913 * @phba: Pointer to HBA context object.
10914 * @ring_number: SLI ring number to issue wqe on.
10915 * @piocb: Pointer to command iocb.
10916 * @flag: Flag indicating if this command can be put into txq.
10917 *
10918 * __lpfc_sli_issue_fcp_io_s3 is wrapper function to invoke lockless func to
10919 * send an iocb command to an HBA with SLI-4 interface spec.
10920 *
10921 * This function takes the hbalock before invoking the lockless version.
10922 * The function will return success after it successfully submit the wqe to
10923 * firmware or after adding to the txq.
10924 **/
10925static int
10926__lpfc_sli_issue_fcp_io_s3(struct lpfc_hba *phba, uint32_t ring_number,
10927 struct lpfc_iocbq *piocb, uint32_t flag)
10928{
10929 unsigned long iflags;
10930 int rc;
10931
10932 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartda255e22020-11-15 11:26:42 -080010933 rc = __lpfc_sli_issue_iocb_s3(phba, ring_number, piocb, flag);
James Smart47ff4c52020-11-15 11:26:41 -080010934 spin_unlock_irqrestore(&phba->hbalock, iflags);
10935
10936 return rc;
10937}
10938
10939/**
10940 * __lpfc_sli_issue_fcp_io_s4 - SLI4 device for sending fcp io wqe
10941 * @phba: Pointer to HBA context object.
10942 * @ring_number: SLI ring number to issue wqe on.
10943 * @piocb: Pointer to command iocb.
10944 * @flag: Flag indicating if this command can be put into txq.
10945 *
10946 * __lpfc_sli_issue_fcp_io_s4 is used by other functions in the driver to issue
10947 * an wqe command to an HBA with SLI-4 interface spec.
10948 *
10949 * This function is a lockless version. The function will return success
10950 * after it successfully submit the wqe to firmware or after adding to the
10951 * txq.
10952 **/
10953static int
10954__lpfc_sli_issue_fcp_io_s4(struct lpfc_hba *phba, uint32_t ring_number,
10955 struct lpfc_iocbq *piocb, uint32_t flag)
10956{
James Smart47ff4c52020-11-15 11:26:41 -080010957 int rc;
James Smartda255e22020-11-15 11:26:42 -080010958 struct lpfc_io_buf *lpfc_cmd =
10959 (struct lpfc_io_buf *)piocb->context1;
10960 union lpfc_wqe128 *wqe = &piocb->wqe;
10961 struct sli4_sge *sgl;
James Smart47ff4c52020-11-15 11:26:41 -080010962
James Smartda255e22020-11-15 11:26:42 -080010963 /* 128 byte wqe support here */
10964 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smart47ff4c52020-11-15 11:26:41 -080010965
James Smartda255e22020-11-15 11:26:42 -080010966 if (phba->fcp_embed_io) {
10967 struct fcp_cmnd *fcp_cmnd;
10968 u32 *ptr;
James Smart47ff4c52020-11-15 11:26:41 -080010969
James Smartda255e22020-11-15 11:26:42 -080010970 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart47ff4c52020-11-15 11:26:41 -080010971
James Smartda255e22020-11-15 11:26:42 -080010972 /* Word 0-2 - FCP_CMND */
10973 wqe->generic.bde.tus.f.bdeFlags =
10974 BUFF_TYPE_BDE_IMMED;
10975 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10976 wqe->generic.bde.addrHigh = 0;
10977 wqe->generic.bde.addrLow = 88; /* Word 22 */
10978
10979 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10980 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
10981
10982 /* Word 22-29 FCP CMND Payload */
10983 ptr = &wqe->words[22];
10984 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10985 } else {
10986 /* Word 0-2 - Inline BDE */
10987 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
10988 wqe->generic.bde.tus.f.bdeSize = sizeof(struct fcp_cmnd);
10989 wqe->generic.bde.addrHigh = sgl->addr_hi;
10990 wqe->generic.bde.addrLow = sgl->addr_lo;
10991
10992 /* Word 10 */
10993 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10994 bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
10995 }
10996
Gaurav Srivastavaf56e86a2021-06-08 10:05:53 +053010997 /* add the VMID tags as per switch response */
10998 if (unlikely(piocb->iocb_flag & LPFC_IO_VMID)) {
10999 if (phba->pport->vmid_priority_tagging) {
11000 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
11001 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
11002 (piocb->vmid_tag.cs_ctl_vmid));
11003 } else {
11004 bf_set(wqe_appid, &wqe->fcp_iwrite.wqe_com, 1);
11005 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
11006 wqe->words[31] = piocb->vmid_tag.app_id;
11007 }
11008 }
James Smartda255e22020-11-15 11:26:42 -080011009 rc = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, piocb);
James Smart47ff4c52020-11-15 11:26:41 -080011010 return rc;
11011}
11012
11013/**
James Smart4f774512009-05-22 14:52:35 -040011014 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
11015 * @phba: Pointer to HBA context object.
11016 * @ring_number: SLI ring number to issue iocb on.
11017 * @piocb: Pointer to command iocb.
11018 * @flag: Flag indicating if this command can be put into txq.
11019 *
11020 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
11021 * an iocb command to an HBA with SLI-4 interface spec.
11022 *
James Smart27f3efd2019-10-18 14:18:19 -070011023 * This function is called with ringlock held. The function will return success
James Smart4f774512009-05-22 14:52:35 -040011024 * after it successfully submit the iocb to firmware or after adding to the
11025 * txq.
11026 **/
11027static int
11028__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
11029 struct lpfc_iocbq *piocb, uint32_t flag)
11030{
11031 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080011032 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -050011033 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -080011034 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -040011035
James Smart895427b2017-02-12 13:52:30 -080011036 /* Get the WQ */
11037 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
11038 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc00f62e2019-08-14 16:57:11 -070011039 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
James Smart895427b2017-02-12 13:52:30 -080011040 } else {
11041 wq = phba->sli4_hba.els_wq;
11042 }
11043
11044 /* Get corresponding ring */
11045 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010011046
James Smartb5c53952016-03-31 14:12:30 -070011047 /*
11048 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -070011049 */
James Smartb5c53952016-03-31 14:12:30 -070011050
James Smartcda7fa182019-03-04 15:15:43 -080011051 lockdep_assert_held(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011052
James Smart4f774512009-05-22 14:52:35 -040011053 if (piocb->sli4_xritag == NO_XRI) {
11054 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -050011055 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -040011056 sglq = NULL;
11057 else {
James Smart0e9bb8d2013-03-01 16:35:12 -050011058 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -040011059 if (!(flag & SLI_IOCB_RET_IOCB)) {
11060 __lpfc_sli_ringtx_put(phba,
11061 pring, piocb);
11062 return IOCB_SUCCESS;
11063 } else {
11064 return IOCB_BUSY;
11065 }
11066 } else {
James Smart895427b2017-02-12 13:52:30 -080011067 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -040011068 if (!sglq) {
11069 if (!(flag & SLI_IOCB_RET_IOCB)) {
11070 __lpfc_sli_ringtx_put(phba,
11071 pring,
11072 piocb);
11073 return IOCB_SUCCESS;
11074 } else
11075 return IOCB_BUSY;
11076 }
11077 }
James Smart4f774512009-05-22 14:52:35 -040011078 }
James Smartda255e22020-11-15 11:26:42 -080011079 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -040011080 /* These IO's already have an XRI and a mapped sgl. */
11081 sglq = NULL;
James Smartda255e22020-11-15 11:26:42 -080011082 }
James Smart2ea259e2017-02-12 13:52:27 -080011083 else {
James Smart6d368e52011-05-24 11:44:12 -040011084 /*
11085 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -040011086 * sglq is on the active list
11087 */
James Smartedccdc12013-01-03 15:43:45 -050011088 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -040011089 if (!sglq)
11090 return IOCB_ERROR;
11091 }
11092
11093 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -040011094 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040011095 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040011096 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -040011097 return IOCB_ERROR;
11098 }
11099
James Smart205e8242018-03-05 12:04:03 -080011100 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -040011101 return IOCB_ERROR;
11102
James Smart205e8242018-03-05 12:04:03 -080011103 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -080011104 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -040011105 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
11106
11107 return 0;
11108}
11109
Lee Jones8514e2f2021-03-03 14:46:18 +000011110/*
James Smart47ff4c52020-11-15 11:26:41 -080011111 * lpfc_sli_issue_fcp_io - Wrapper func for issuing fcp i/o
11112 *
11113 * This routine wraps the actual fcp i/o function for issusing WQE for sli-4
11114 * or IOCB for sli-3 function.
11115 * pointer from the lpfc_hba struct.
11116 *
11117 * Return codes:
11118 * IOCB_ERROR - Error
11119 * IOCB_SUCCESS - Success
11120 * IOCB_BUSY - Busy
11121 **/
11122int
11123lpfc_sli_issue_fcp_io(struct lpfc_hba *phba, uint32_t ring_number,
11124 struct lpfc_iocbq *piocb, uint32_t flag)
11125{
11126 return phba->__lpfc_sli_issue_fcp_io(phba, ring_number, piocb, flag);
11127}
11128
Lee Jones7af29d42020-07-21 17:41:31 +010011129/*
James Smart3772a992009-05-22 14:50:54 -040011130 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
11131 *
11132 * This routine wraps the actual lockless version for issusing IOCB function
11133 * pointer from the lpfc_hba struct.
11134 *
11135 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -070011136 * IOCB_ERROR - Error
11137 * IOCB_SUCCESS - Success
11138 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -040011139 **/
James Smart2a9bf3d2010-06-07 15:24:45 -040011140int
James Smart3772a992009-05-22 14:50:54 -040011141__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
11142 struct lpfc_iocbq *piocb, uint32_t flag)
11143{
11144 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11145}
11146
11147/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011148 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -040011149 * @phba: The hba struct for which this call is being executed.
11150 * @dev_grp: The HBA PCI-Device group number.
11151 *
11152 * This routine sets up the SLI interface API function jump table in @phba
11153 * struct.
11154 * Returns: 0 - success, -ENODEV - failure.
11155 **/
11156int
11157lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
11158{
11159
11160 switch (dev_grp) {
11161 case LPFC_PCI_DEV_LP:
11162 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
11163 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
James Smart47ff4c52020-11-15 11:26:41 -080011164 phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s3;
James Smart3772a992009-05-22 14:50:54 -040011165 break;
James Smart4f774512009-05-22 14:52:35 -040011166 case LPFC_PCI_DEV_OC:
11167 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
11168 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
James Smart47ff4c52020-11-15 11:26:41 -080011169 phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s4;
James Smart4f774512009-05-22 14:52:35 -040011170 break;
James Smart3772a992009-05-22 14:50:54 -040011171 default:
James Smarta5160742021-10-20 14:14:10 -070011172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart3772a992009-05-22 14:50:54 -040011173 "1419 Invalid HBA PCI-device group: 0x%x\n",
11174 dev_grp);
11175 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -040011176 }
11177 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
11178 return 0;
11179}
James Smart92d7f7b2007-06-17 19:56:38 -050011180
James Smarta1efe162015-05-21 13:55:20 -040011181/**
James Smart895427b2017-02-12 13:52:30 -080011182 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -040011183 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -040011184 * @piocb: Pointer to command iocb.
11185 *
James Smart895427b2017-02-12 13:52:30 -080011186 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
11187 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -040011188 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -080011189 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -040011190 */
James Smart895427b2017-02-12 13:52:30 -080011191struct lpfc_sli_ring *
11192lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -040011193{
James Smartc4908502019-01-28 11:14:28 -080011194 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -080011195
James Smart895427b2017-02-12 13:52:30 -080011196 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -080011197 if (unlikely(!phba->sli4_hba.hdwq))
James Smart7370d102019-01-28 11:14:20 -080011198 return NULL;
11199 /*
11200 * for abort iocb hba_wqidx should already
11201 * be setup based on what work queue we used.
11202 */
11203 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc4908502019-01-28 11:14:28 -080011204 lpfc_cmd = (struct lpfc_io_buf *)piocb->context1;
James Smart1fbf9742019-01-28 11:14:26 -080011205 piocb->hba_wqidx = lpfc_cmd->hdwq_no;
James Smart9bd2bff52014-09-03 12:57:30 -040011206 }
James Smartc00f62e2019-08-14 16:57:11 -070011207 return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080011208 } else {
11209 if (unlikely(!phba->sli4_hba.els_wq))
11210 return NULL;
11211 piocb->hba_wqidx = 0;
11212 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -040011213 }
James Smart9bd2bff52014-09-03 12:57:30 -040011214}
11215
James Smarte59058c2008-08-24 21:49:00 -040011216/**
James Smart3621a712009-04-06 18:47:14 -040011217 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -040011218 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010011219 * @ring_number: Ring number
James Smarte59058c2008-08-24 21:49:00 -040011220 * @piocb: Pointer to command iocb.
11221 * @flag: Flag indicating if this command can be put into txq.
11222 *
11223 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
11224 * function. This function gets the hbalock and calls
11225 * __lpfc_sli_issue_iocb function and will return the error returned
11226 * by __lpfc_sli_issue_iocb function. This wrapper is used by
11227 * functions which do not hold hbalock.
11228 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011229int
James Smart3772a992009-05-22 14:50:54 -040011230lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -050011231 struct lpfc_iocbq *piocb, uint32_t flag)
11232{
James Smart2a76a282012-08-03 12:35:54 -040011233 struct lpfc_sli_ring *pring;
James Smart93a4d6f2019-11-04 16:57:05 -080011234 struct lpfc_queue *eq;
James Smart92d7f7b2007-06-17 19:56:38 -050011235 unsigned long iflags;
James Smart6a828b02019-01-28 11:14:31 -080011236 int rc;
James Smart92d7f7b2007-06-17 19:56:38 -050011237
James Smart7e56aa22012-08-03 12:35:34 -040011238 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart93a4d6f2019-11-04 16:57:05 -080011239 eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
11240
James Smart895427b2017-02-12 13:52:30 -080011241 pring = lpfc_sli4_calc_ring(phba, piocb);
11242 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040011243 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -040011244
James Smart9bd2bff52014-09-03 12:57:30 -040011245 spin_lock_irqsave(&pring->ring_lock, iflags);
11246 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11247 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080011248
11249 lpfc_sli4_poll_eq(eq, LPFC_POLL_FASTPATH);
James Smart7e56aa22012-08-03 12:35:34 -040011250 } else {
11251 /* For now, SLI2/3 will still use hbalock */
11252 spin_lock_irqsave(&phba->hbalock, iflags);
11253 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
11254 spin_unlock_irqrestore(&phba->hbalock, iflags);
11255 }
James Smart92d7f7b2007-06-17 19:56:38 -050011256 return rc;
11257}
11258
James Smarte59058c2008-08-24 21:49:00 -040011259/**
James Smart3621a712009-04-06 18:47:14 -040011260 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -040011261 * @phba: Pointer to HBA context object.
11262 *
11263 * This function is called while driver attaches with the
11264 * HBA to setup the extra ring. The extra ring is used
11265 * only when driver needs to support target mode functionality
11266 * or IP over FC functionalities.
11267 *
James Smart895427b2017-02-12 13:52:30 -080011268 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -040011269 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050011270static int
11271lpfc_extra_ring_setup( struct lpfc_hba *phba)
11272{
11273 struct lpfc_sli *psli;
11274 struct lpfc_sli_ring *pring;
11275
11276 psli = &phba->sli;
11277
11278 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -050011279
11280 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -080011281 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -040011282 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11283 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11284 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11285 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050011286
James Smarta4bc3372006-12-02 13:34:16 -050011287 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -080011288 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -050011289
James Smart7e56aa22012-08-03 12:35:34 -040011290 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11291 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11292 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11293 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050011294
11295 /* Setup default profile for this ring */
11296 pring->iotag_max = 4096;
11297 pring->num_mask = 1;
11298 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -050011299 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
11300 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050011301 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
11302 return 0;
11303}
11304
James Smarte7dab162020-10-20 13:27:12 -070011305static void
11306lpfc_sli_post_recovery_event(struct lpfc_hba *phba,
11307 struct lpfc_nodelist *ndlp)
11308{
11309 unsigned long iflags;
11310 struct lpfc_work_evt *evtp = &ndlp->recovery_evt;
11311
11312 spin_lock_irqsave(&phba->hbalock, iflags);
11313 if (!list_empty(&evtp->evt_listp)) {
11314 spin_unlock_irqrestore(&phba->hbalock, iflags);
11315 return;
11316 }
11317
11318 /* Incrementing the reference count until the queued work is done. */
11319 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
11320 if (!evtp->evt_arg1) {
11321 spin_unlock_irqrestore(&phba->hbalock, iflags);
11322 return;
11323 }
11324 evtp->evt = LPFC_EVT_RECOVER_PORT;
11325 list_add_tail(&evtp->evt_listp, &phba->work_list);
11326 spin_unlock_irqrestore(&phba->hbalock, iflags);
11327
11328 lpfc_worker_wake_up(phba);
11329}
11330
James Smartcb69f7d2011-12-13 13:21:57 -050011331/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
11332 * @phba: Pointer to HBA context object.
11333 * @iocbq: Pointer to iocb object.
11334 *
11335 * The async_event handler calls this routine when it receives
11336 * an ASYNC_STATUS_CN event from the port. The port generates
11337 * this event when an Abort Sequence request to an rport fails
11338 * twice in succession. The abort could be originated by the
11339 * driver or by the port. The ABTS could have been for an ELS
11340 * or FCP IO. The port only generates this event when an ABTS
11341 * fails to complete after one retry.
11342 */
11343static void
11344lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
11345 struct lpfc_iocbq *iocbq)
11346{
11347 struct lpfc_nodelist *ndlp = NULL;
11348 uint16_t rpi = 0, vpi = 0;
11349 struct lpfc_vport *vport = NULL;
11350
11351 /* The rpi in the ulpContext is vport-sensitive. */
11352 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
11353 rpi = iocbq->iocb.ulpContext;
11354
11355 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11356 "3092 Port generated ABTS async event "
11357 "on vpi %d rpi %d status 0x%x\n",
11358 vpi, rpi, iocbq->iocb.ulpStatus);
11359
11360 vport = lpfc_find_vport_by_vpid(phba, vpi);
11361 if (!vport)
11362 goto err_exit;
11363 ndlp = lpfc_findnode_rpi(vport, rpi);
James Smart307e3382020-11-15 11:26:30 -080011364 if (!ndlp)
James Smartcb69f7d2011-12-13 13:21:57 -050011365 goto err_exit;
11366
11367 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
11368 lpfc_sli_abts_recover_port(vport, ndlp);
11369 return;
11370
11371 err_exit:
11372 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11373 "3095 Event Context not found, no "
11374 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
11375 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
11376 vpi, rpi);
11377}
11378
11379/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
11380 * @phba: pointer to HBA context object.
11381 * @ndlp: nodelist pointer for the impacted rport.
11382 * @axri: pointer to the wcqe containing the failed exchange.
11383 *
11384 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
11385 * port. The port generates this event when an abort exchange request to an
11386 * rport fails twice in succession with no reply. The abort could be originated
11387 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
11388 */
11389void
11390lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
11391 struct lpfc_nodelist *ndlp,
11392 struct sli4_wcqe_xri_aborted *axri)
11393{
James Smart5c1db2a2012-03-01 22:34:36 -050011394 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -050011395
James Smart307e3382020-11-15 11:26:30 -080011396 if (!ndlp) {
James Smartcb69f7d2011-12-13 13:21:57 -050011397 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11398 "3115 Node Context not found, driver "
11399 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -050011400 return;
11401 }
11402
James Smartcb69f7d2011-12-13 13:21:57 -050011403 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11404 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -050011405 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -040011406 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -050011407 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -050011408 bf_get(lpfc_wcqe_xa_status, axri),
11409 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -050011410
James Smart5c1db2a2012-03-01 22:34:36 -050011411 /*
11412 * Catch the ABTS protocol failure case. Older OCe FW releases returned
11413 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
11414 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
11415 */
James Smarte3d2b802012-08-14 14:25:43 -040011416 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -050011417 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
11418 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smarte7dab162020-10-20 13:27:12 -070011419 lpfc_sli_post_recovery_event(phba, ndlp);
James Smartcb69f7d2011-12-13 13:21:57 -050011420}
11421
James Smarte59058c2008-08-24 21:49:00 -040011422/**
James Smart3621a712009-04-06 18:47:14 -040011423 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -040011424 * @phba: Pointer to HBA context object.
11425 * @pring: Pointer to driver SLI ring object.
11426 * @iocbq: Pointer to iocb object.
11427 *
11428 * This function is called by the slow ring event handler
11429 * function when there is an ASYNC event iocb in the ring.
11430 * This function is called with no lock held.
11431 * Currently this function handles only temperature related
11432 * ASYNC events. The function decodes the temperature sensor
11433 * event message and posts events for the management applications.
11434 **/
James Smart98c9ea52007-10-27 13:37:33 -040011435static void
James Smart57127f12007-10-27 13:37:05 -040011436lpfc_sli_async_event_handler(struct lpfc_hba * phba,
11437 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
11438{
11439 IOCB_t *icmd;
11440 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -040011441 struct temp_event temp_event_data;
11442 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -040011443 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -040011444
11445 icmd = &iocbq->iocb;
11446 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -040011447
James Smartcb69f7d2011-12-13 13:21:57 -050011448 switch (evt_code) {
11449 case ASYNC_TEMP_WARN:
11450 case ASYNC_TEMP_SAFE:
11451 temp_event_data.data = (uint32_t) icmd->ulpContext;
11452 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
11453 if (evt_code == ASYNC_TEMP_WARN) {
11454 temp_event_data.event_code = LPFC_THRESHOLD_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 "0347 Adapter is very hot, please take "
11457 "corrective action. temperature : %d Celsius\n",
11458 (uint32_t) icmd->ulpContext);
11459 } else {
11460 temp_event_data.event_code = LPFC_NORMAL_TEMP;
Dick Kennedy372c1872020-06-30 14:50:00 -070011461 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcb69f7d2011-12-13 13:21:57 -050011462 "0340 Adapter temperature is OK now. "
11463 "temperature : %d Celsius\n",
11464 (uint32_t) icmd->ulpContext);
11465 }
11466
11467 /* Send temperature change event to applications */
11468 shost = lpfc_shost_from_vport(phba->pport);
11469 fc_host_post_vendor_event(shost, fc_get_event_number(),
11470 sizeof(temp_event_data), (char *) &temp_event_data,
11471 LPFC_NL_VENDOR_ID);
11472 break;
11473 case ASYNC_STATUS_CN:
11474 lpfc_sli_abts_err_handler(phba, iocbq);
11475 break;
11476 default:
James Smarta257bf92009-04-06 18:48:10 -040011477 iocb_w = (uint32_t *) icmd;
Dick Kennedy372c1872020-06-30 14:50:00 -070011478 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart76bb24e2007-10-27 13:38:00 -040011479 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -040011480 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -040011481 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
11482 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
11483 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
11484 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -050011485 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -040011486 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
11487 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
11488 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
11489 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
11490
James Smartcb69f7d2011-12-13 13:21:57 -050011491 break;
James Smart57127f12007-10-27 13:37:05 -040011492 }
James Smart57127f12007-10-27 13:37:05 -040011493}
11494
11495
James Smarte59058c2008-08-24 21:49:00 -040011496/**
James Smart895427b2017-02-12 13:52:30 -080011497 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -040011498 * @phba: Pointer to HBA context object.
11499 *
11500 * lpfc_sli_setup sets up rings of the SLI interface with
11501 * number of iocbs per ring and iotags. This function is
11502 * called while driver attach to the HBA and before the
11503 * interrupts are enabled. So there is no need for locking.
11504 *
11505 * This function always returns 0.
11506 **/
dea31012005-04-17 16:05:31 -050011507int
James Smart895427b2017-02-12 13:52:30 -080011508lpfc_sli4_setup(struct lpfc_hba *phba)
11509{
11510 struct lpfc_sli_ring *pring;
11511
11512 pring = phba->sli4_hba.els_wq->pring;
11513 pring->num_mask = LPFC_MAX_RING_MASK;
11514 pring->prt[0].profile = 0; /* Mask 0 */
11515 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11516 pring->prt[0].type = FC_TYPE_ELS;
11517 pring->prt[0].lpfc_sli_rcv_unsol_event =
11518 lpfc_els_unsol_event;
11519 pring->prt[1].profile = 0; /* Mask 1 */
11520 pring->prt[1].rctl = FC_RCTL_ELS_REP;
11521 pring->prt[1].type = FC_TYPE_ELS;
11522 pring->prt[1].lpfc_sli_rcv_unsol_event =
11523 lpfc_els_unsol_event;
11524 pring->prt[2].profile = 0; /* Mask 2 */
11525 /* NameServer Inquiry */
11526 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
11527 /* NameServer */
11528 pring->prt[2].type = FC_TYPE_CT;
11529 pring->prt[2].lpfc_sli_rcv_unsol_event =
11530 lpfc_ct_unsol_event;
11531 pring->prt[3].profile = 0; /* Mask 3 */
11532 /* NameServer response */
11533 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
11534 /* NameServer */
11535 pring->prt[3].type = FC_TYPE_CT;
11536 pring->prt[3].lpfc_sli_rcv_unsol_event =
11537 lpfc_ct_unsol_event;
11538 return 0;
11539}
11540
11541/**
11542 * lpfc_sli_setup - SLI ring setup function
11543 * @phba: Pointer to HBA context object.
11544 *
11545 * lpfc_sli_setup sets up rings of the SLI interface with
11546 * number of iocbs per ring and iotags. This function is
11547 * called while driver attach to the HBA and before the
11548 * interrupts are enabled. So there is no need for locking.
11549 *
11550 * This function always returns 0. SLI3 only.
11551 **/
11552int
dea31012005-04-17 16:05:31 -050011553lpfc_sli_setup(struct lpfc_hba *phba)
11554{
James Smarted957682007-06-17 19:56:37 -050011555 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050011556 struct lpfc_sli *psli = &phba->sli;
11557 struct lpfc_sli_ring *pring;
11558
James Smart2a76a282012-08-03 12:35:54 -040011559 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050011560 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050011561
James Bottomley604a3e32005-10-29 10:28:33 -050011562 psli->iocbq_lookup = NULL;
11563 psli->iocbq_lookup_len = 0;
11564 psli->last_iotag = 0;
11565
dea31012005-04-17 16:05:31 -050011566 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080011567 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050011568 switch (i) {
11569 case LPFC_FCP_RING: /* ring 0 - FCP */
11570 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040011571 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
11572 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
11573 pring->sli.sli3.numCiocb +=
11574 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
11575 pring->sli.sli3.numRiocb +=
11576 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
11577 pring->sli.sli3.numCiocb +=
11578 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
11579 pring->sli.sli3.numRiocb +=
11580 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
11581 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050011582 SLI3_IOCB_CMD_SIZE :
11583 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040011584 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050011585 SLI3_IOCB_RSP_SIZE :
11586 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050011587 pring->iotag_ctr = 0;
11588 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050011589 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050011590 pring->fast_iotag = pring->iotag_max;
11591 pring->num_mask = 0;
11592 break;
James Smarta4bc3372006-12-02 13:34:16 -050011593 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050011594 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040011595 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
11596 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
11597 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050011598 SLI3_IOCB_CMD_SIZE :
11599 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040011600 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050011601 SLI3_IOCB_RSP_SIZE :
11602 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050011603 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050011604 pring->num_mask = 0;
11605 break;
11606 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
11607 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040011608 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
11609 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
11610 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050011611 SLI3_IOCB_CMD_SIZE :
11612 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040011613 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050011614 SLI3_IOCB_RSP_SIZE :
11615 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050011616 pring->fast_iotag = 0;
11617 pring->iotag_ctr = 0;
11618 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040011619 pring->lpfc_sli_rcv_async_status =
11620 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040011621 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050011622 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040011623 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
11624 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050011625 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050011626 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050011627 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040011628 pring->prt[1].rctl = FC_RCTL_ELS_REP;
11629 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050011630 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050011631 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050011632 pring->prt[2].profile = 0; /* Mask 2 */
11633 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040011634 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050011635 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040011636 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050011637 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050011638 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050011639 pring->prt[3].profile = 0; /* Mask 3 */
11640 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040011641 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050011642 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040011643 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050011644 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050011645 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050011646 break;
11647 }
James Smart7e56aa22012-08-03 12:35:34 -040011648 totiocbsize += (pring->sli.sli3.numCiocb *
11649 pring->sli.sli3.sizeCiocb) +
11650 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050011651 }
James Smarted957682007-06-17 19:56:37 -050011652 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050011653 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040011654 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
11655 "SLI2 SLIM Data: x%x x%lx\n",
11656 phba->brd_no, totiocbsize,
11657 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050011658 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050011659 if (phba->cfg_multi_ring_support == 2)
11660 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050011661
11662 return 0;
11663}
11664
James Smarte59058c2008-08-24 21:49:00 -040011665/**
James Smart895427b2017-02-12 13:52:30 -080011666 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040011667 * @phba: Pointer to HBA context object.
11668 *
James Smart895427b2017-02-12 13:52:30 -080011669 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040011670 * ring. This function also initializes ring indices of each ring.
11671 * This function is called during the initialization of the SLI
11672 * interface of an HBA.
11673 * This function is called with no lock held and always returns
11674 * 1.
11675 **/
James Smart895427b2017-02-12 13:52:30 -080011676void
11677lpfc_sli4_queue_init(struct lpfc_hba *phba)
11678{
11679 struct lpfc_sli *psli;
11680 struct lpfc_sli_ring *pring;
11681 int i;
11682
11683 psli = &phba->sli;
11684 spin_lock_irq(&phba->hbalock);
11685 INIT_LIST_HEAD(&psli->mboxq);
11686 INIT_LIST_HEAD(&psli->mboxq_cmpl);
11687 /* Initialize list headers for txq and txcmplq as double linked lists */
James Smartcdb42be2019-01-28 11:14:21 -080011688 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -070011689 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080011690 pring->flag = 0;
11691 pring->ringno = LPFC_FCP_RING;
James Smartc4908502019-01-28 11:14:28 -080011692 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011693 INIT_LIST_HEAD(&pring->txq);
11694 INIT_LIST_HEAD(&pring->txcmplq);
11695 INIT_LIST_HEAD(&pring->iocb_continueq);
11696 spin_lock_init(&pring->ring_lock);
11697 }
11698 pring = phba->sli4_hba.els_wq->pring;
11699 pring->flag = 0;
11700 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080011701 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011702 INIT_LIST_HEAD(&pring->txq);
11703 INIT_LIST_HEAD(&pring->txcmplq);
11704 INIT_LIST_HEAD(&pring->iocb_continueq);
11705 spin_lock_init(&pring->ring_lock);
11706
James Smartcdb42be2019-01-28 11:14:21 -080011707 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080011708 pring = phba->sli4_hba.nvmels_wq->pring;
11709 pring->flag = 0;
11710 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080011711 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011712 INIT_LIST_HEAD(&pring->txq);
11713 INIT_LIST_HEAD(&pring->txcmplq);
11714 INIT_LIST_HEAD(&pring->iocb_continueq);
11715 spin_lock_init(&pring->ring_lock);
11716 }
11717
11718 spin_unlock_irq(&phba->hbalock);
11719}
11720
11721/**
11722 * lpfc_sli_queue_init - Queue initialization function
11723 * @phba: Pointer to HBA context object.
11724 *
11725 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
11726 * ring. This function also initializes ring indices of each ring.
11727 * This function is called during the initialization of the SLI
11728 * interface of an HBA.
11729 * This function is called with no lock held and always returns
11730 * 1.
11731 **/
11732void
11733lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050011734{
11735 struct lpfc_sli *psli;
11736 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050011737 int i;
dea31012005-04-17 16:05:31 -050011738
11739 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050011740 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011741 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050011742 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050011743 /* Initialize list headers for txq and txcmplq as double linked lists */
11744 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080011745 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050011746 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040011747 pring->sli.sli3.next_cmdidx = 0;
11748 pring->sli.sli3.local_getidx = 0;
11749 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050011750 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050011751 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050011752 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080011753 pring->flag = 0;
11754 INIT_LIST_HEAD(&pring->txq);
11755 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040011756 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050011757 }
James Smart2e0fef82007-06-17 19:56:36 -050011758 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011759}
11760
James Smarte59058c2008-08-24 21:49:00 -040011761/**
James Smart04c68492009-05-22 14:52:52 -040011762 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
11763 * @phba: Pointer to HBA context object.
11764 *
11765 * This routine flushes the mailbox command subsystem. It will unconditionally
11766 * flush all the mailbox commands in the three possible stages in the mailbox
11767 * command sub-system: pending mailbox command queue; the outstanding mailbox
11768 * command; and completed mailbox command queue. It is caller's responsibility
11769 * to make sure that the driver is in the proper state to flush the mailbox
11770 * command sub-system. Namely, the posting of mailbox commands into the
11771 * pending mailbox command queue from the various clients must be stopped;
11772 * either the HBA is in a state that it will never works on the outstanding
11773 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
11774 * mailbox command has been completed.
11775 **/
11776static void
11777lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
11778{
11779 LIST_HEAD(completions);
11780 struct lpfc_sli *psli = &phba->sli;
11781 LPFC_MBOXQ_t *pmb;
11782 unsigned long iflag;
11783
James Smart523128e2018-09-10 10:30:46 -070011784 /* Disable softirqs, including timers from obtaining phba->hbalock */
11785 local_bh_disable();
11786
James Smart04c68492009-05-22 14:52:52 -040011787 /* Flush all the mailbox commands in the mbox system */
11788 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart523128e2018-09-10 10:30:46 -070011789
James Smart04c68492009-05-22 14:52:52 -040011790 /* The pending mailbox command queue */
11791 list_splice_init(&phba->sli.mboxq, &completions);
11792 /* The outstanding active mailbox command */
11793 if (psli->mbox_active) {
11794 list_add_tail(&psli->mbox_active->list, &completions);
11795 psli->mbox_active = NULL;
11796 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11797 }
11798 /* The completed mailbox command queue */
11799 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
11800 spin_unlock_irqrestore(&phba->hbalock, iflag);
11801
James Smart523128e2018-09-10 10:30:46 -070011802 /* Enable softirqs again, done with phba->hbalock */
11803 local_bh_enable();
11804
James Smart04c68492009-05-22 14:52:52 -040011805 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
11806 while (!list_empty(&completions)) {
11807 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
11808 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
11809 if (pmb->mbox_cmpl)
11810 pmb->mbox_cmpl(phba, pmb);
11811 }
11812}
11813
11814/**
James Smart3621a712009-04-06 18:47:14 -040011815 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040011816 * @vport: Pointer to virtual port object.
11817 *
11818 * lpfc_sli_host_down is called to clean up the resources
11819 * associated with a vport before destroying virtual
11820 * port data structures.
11821 * This function does following operations:
11822 * - Free discovery resources associated with this virtual
11823 * port.
11824 * - Free iocbs associated with this virtual port in
11825 * the txq.
11826 * - Send abort for all iocb commands associated with this
11827 * vport in txcmplq.
11828 *
11829 * This function is called with no lock held and always returns 1.
11830 **/
dea31012005-04-17 16:05:31 -050011831int
James Smart92d7f7b2007-06-17 19:56:38 -050011832lpfc_sli_host_down(struct lpfc_vport *vport)
11833{
James Smart858c9f62007-06-17 19:56:39 -050011834 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050011835 struct lpfc_hba *phba = vport->phba;
11836 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080011837 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050011838 struct lpfc_sli_ring *pring;
11839 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050011840 int i;
11841 unsigned long flags = 0;
11842 uint16_t prev_pring_flag;
11843
11844 lpfc_cleanup_discovery_resources(vport);
11845
11846 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050011847
James Smart895427b2017-02-12 13:52:30 -080011848 /*
11849 * Error everything on the txq since these iocbs
11850 * have not been given to the FW yet.
11851 * Also issue ABTS for everything on the txcmplq
11852 */
11853 if (phba->sli_rev != LPFC_SLI_REV4) {
11854 for (i = 0; i < psli->num_rings; i++) {
11855 pring = &psli->sli3_ring[i];
11856 prev_pring_flag = pring->flag;
11857 /* Only slow rings */
11858 if (pring->ringno == LPFC_ELS_RING) {
11859 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11860 /* Set the lpfc data pending flag */
11861 set_bit(LPFC_DATA_READY, &phba->data_flags);
11862 }
11863 list_for_each_entry_safe(iocb, next_iocb,
11864 &pring->txq, list) {
11865 if (iocb->vport != vport)
11866 continue;
11867 list_move_tail(&iocb->list, &completions);
11868 }
11869 list_for_each_entry_safe(iocb, next_iocb,
11870 &pring->txcmplq, list) {
11871 if (iocb->vport != vport)
11872 continue;
James Smartdb7531d2020-11-15 11:26:44 -080011873 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11874 NULL);
James Smart895427b2017-02-12 13:52:30 -080011875 }
11876 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050011877 }
James Smart895427b2017-02-12 13:52:30 -080011878 } else {
11879 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11880 pring = qp->pring;
11881 if (!pring)
11882 continue;
11883 if (pring == phba->sli4_hba.els_wq->pring) {
11884 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11885 /* Set the lpfc data pending flag */
11886 set_bit(LPFC_DATA_READY, &phba->data_flags);
11887 }
11888 prev_pring_flag = pring->flag;
James Smart65a3df62019-09-21 20:58:48 -070011889 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011890 list_for_each_entry_safe(iocb, next_iocb,
11891 &pring->txq, list) {
11892 if (iocb->vport != vport)
11893 continue;
11894 list_move_tail(&iocb->list, &completions);
11895 }
James Smart65a3df62019-09-21 20:58:48 -070011896 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011897 list_for_each_entry_safe(iocb, next_iocb,
11898 &pring->txcmplq, list) {
11899 if (iocb->vport != vport)
11900 continue;
James Smartdb7531d2020-11-15 11:26:44 -080011901 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11902 NULL);
James Smart895427b2017-02-12 13:52:30 -080011903 }
11904 pring->flag = prev_pring_flag;
11905 }
James Smart92d7f7b2007-06-17 19:56:38 -050011906 }
James Smart92d7f7b2007-06-17 19:56:38 -050011907 spin_unlock_irqrestore(&phba->hbalock, flags);
11908
James Smarta22d73b2021-01-04 10:02:38 -080011909 /* Make sure HBA is alive */
11910 lpfc_issue_hb_tmo(phba);
11911
James Smarta257bf92009-04-06 18:48:10 -040011912 /* Cancel all the IOCBs from the completions list */
11913 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11914 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050011915 return 1;
11916}
11917
James Smarte59058c2008-08-24 21:49:00 -040011918/**
James Smart3621a712009-04-06 18:47:14 -040011919 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040011920 * @phba: Pointer to HBA context object.
11921 *
11922 * This function cleans up all iocb, buffers, mailbox commands
11923 * while shutting down the HBA. This function is called with no
11924 * lock held and always returns 1.
11925 * This function does the following to cleanup driver resources:
11926 * - Free discovery resources for each virtual port
11927 * - Cleanup any pending fabric iocbs
11928 * - Iterate through the iocb txq and free each entry
11929 * in the list.
11930 * - Free up any buffer posted to the HBA
11931 * - Free mailbox commands in the mailbox queue.
11932 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011933int
James Smart2e0fef82007-06-17 19:56:36 -050011934lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050011935{
James Smart2534ba72007-04-25 09:52:20 -040011936 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050011937 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080011938 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050011939 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050011940 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050011941 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040011942 int i;
11943
11944 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040011945 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050011946
dea31012005-04-17 16:05:31 -050011947 lpfc_hba_down_prep(phba);
11948
James Smart523128e2018-09-10 10:30:46 -070011949 /* Disable softirqs, including timers from obtaining phba->hbalock */
11950 local_bh_disable();
11951
James Smart92d7f7b2007-06-17 19:56:38 -050011952 lpfc_fabric_abort_hba(phba);
11953
James Smart2e0fef82007-06-17 19:56:36 -050011954 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050011955
James Smart895427b2017-02-12 13:52:30 -080011956 /*
11957 * Error everything on the txq since these iocbs
11958 * have not been given to the FW yet.
11959 */
11960 if (phba->sli_rev != LPFC_SLI_REV4) {
11961 for (i = 0; i < psli->num_rings; i++) {
11962 pring = &psli->sli3_ring[i];
11963 /* Only slow rings */
11964 if (pring->ringno == LPFC_ELS_RING) {
11965 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11966 /* Set the lpfc data pending flag */
11967 set_bit(LPFC_DATA_READY, &phba->data_flags);
11968 }
11969 list_splice_init(&pring->txq, &completions);
11970 }
11971 } else {
11972 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11973 pring = qp->pring;
11974 if (!pring)
11975 continue;
James Smart4b0a42b2019-08-14 16:56:42 -070011976 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011977 list_splice_init(&pring->txq, &completions);
James Smart4b0a42b2019-08-14 16:56:42 -070011978 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011979 if (pring == phba->sli4_hba.els_wq->pring) {
11980 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11981 /* Set the lpfc data pending flag */
11982 set_bit(LPFC_DATA_READY, &phba->data_flags);
11983 }
11984 }
dea31012005-04-17 16:05:31 -050011985 }
James Smart2e0fef82007-06-17 19:56:36 -050011986 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050011987
James Smarta257bf92009-04-06 18:48:10 -040011988 /* Cancel all the IOCBs from the completions list */
11989 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11990 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040011991
James Smart0ff10d42008-01-11 01:52:36 -050011992 spin_lock_irqsave(&phba->hbalock, flags);
11993 list_splice_init(&phba->elsbuf, &completions);
11994 phba->elsbuf_cnt = 0;
11995 phba->elsbuf_prev_cnt = 0;
11996 spin_unlock_irqrestore(&phba->hbalock, flags);
11997
11998 while (!list_empty(&completions)) {
11999 list_remove_head(&completions, buf_ptr,
12000 struct lpfc_dmabuf, list);
12001 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
12002 kfree(buf_ptr);
12003 }
12004
James Smart523128e2018-09-10 10:30:46 -070012005 /* Enable softirqs again, done with phba->hbalock */
12006 local_bh_enable();
12007
dea31012005-04-17 16:05:31 -050012008 /* Return any active mbox cmds */
12009 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050012010
James Smartda0436e2009-05-22 14:51:39 -040012011 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050012012 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040012013 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050012014
James Smartda0436e2009-05-22 14:51:39 -040012015 return 1;
12016}
James Smart92d7f7b2007-06-17 19:56:38 -050012017
James Smartda0436e2009-05-22 14:51:39 -040012018/**
James Smart3621a712009-04-06 18:47:14 -040012019 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040012020 * @srcp: Source memory pointer.
12021 * @destp: Destination memory pointer.
12022 * @cnt: Number of words required to be copied.
12023 *
12024 * This function is used for copying data between driver memory
12025 * and the SLI memory. This function also changes the endianness
12026 * of each word if native endianness is different from SLI
12027 * endianness. This function can be called with or without
12028 * lock.
12029 **/
dea31012005-04-17 16:05:31 -050012030void
12031lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
12032{
12033 uint32_t *src = srcp;
12034 uint32_t *dest = destp;
12035 uint32_t ldata;
12036 int i;
12037
12038 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
12039 ldata = *src;
12040 ldata = le32_to_cpu(ldata);
12041 *dest = ldata;
12042 src++;
12043 dest++;
12044 }
12045}
12046
James Smarte59058c2008-08-24 21:49:00 -040012047
12048/**
James Smarta0c87cb2009-07-19 10:01:10 -040012049 * lpfc_sli_bemem_bcopy - SLI memory copy function
12050 * @srcp: Source memory pointer.
12051 * @destp: Destination memory pointer.
12052 * @cnt: Number of words required to be copied.
12053 *
12054 * This function is used for copying data between a data structure
12055 * with big endian representation to local endianness.
12056 * This function can be called with or without lock.
12057 **/
12058void
12059lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
12060{
12061 uint32_t *src = srcp;
12062 uint32_t *dest = destp;
12063 uint32_t ldata;
12064 int i;
12065
12066 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
12067 ldata = *src;
12068 ldata = be32_to_cpu(ldata);
12069 *dest = ldata;
12070 src++;
12071 dest++;
12072 }
12073}
12074
12075/**
James Smart3621a712009-04-06 18:47:14 -040012076 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040012077 * @phba: Pointer to HBA context object.
12078 * @pring: Pointer to driver SLI ring object.
12079 * @mp: Pointer to driver buffer object.
12080 *
12081 * This function is called with no lock held.
12082 * It always return zero after adding the buffer to the postbufq
12083 * buffer list.
12084 **/
dea31012005-04-17 16:05:31 -050012085int
James Smart2e0fef82007-06-17 19:56:36 -050012086lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12087 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050012088{
12089 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
12090 later */
James Smart2e0fef82007-06-17 19:56:36 -050012091 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050012092 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050012093 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050012094 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050012095 return 0;
12096}
12097
James Smarte59058c2008-08-24 21:49:00 -040012098/**
James Smart3621a712009-04-06 18:47:14 -040012099 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040012100 * @phba: Pointer to HBA context object.
12101 *
12102 * When HBQ is enabled, buffers are searched based on tags. This function
12103 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
12104 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
12105 * does not conflict with tags of buffer posted for unsolicited events.
12106 * The function returns the allocated tag. The function is called with
12107 * no locks held.
12108 **/
James Smart76bb24e2007-10-27 13:38:00 -040012109uint32_t
12110lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
12111{
12112 spin_lock_irq(&phba->hbalock);
12113 phba->buffer_tag_count++;
12114 /*
12115 * Always set the QUE_BUFTAG_BIT to distiguish between
12116 * a tag assigned by HBQ.
12117 */
12118 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
12119 spin_unlock_irq(&phba->hbalock);
12120 return phba->buffer_tag_count;
12121}
12122
James Smarte59058c2008-08-24 21:49:00 -040012123/**
James Smart3621a712009-04-06 18:47:14 -040012124 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040012125 * @phba: Pointer to HBA context object.
12126 * @pring: Pointer to driver SLI ring object.
12127 * @tag: Buffer tag.
12128 *
12129 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
12130 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
12131 * iocb is posted to the response ring with the tag of the buffer.
12132 * This function searches the pring->postbufq list using the tag
12133 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
12134 * iocb. If the buffer is found then lpfc_dmabuf object of the
12135 * buffer is returned to the caller else NULL is returned.
12136 * This function is called with no lock held.
12137 **/
James Smart76bb24e2007-10-27 13:38:00 -040012138struct lpfc_dmabuf *
12139lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12140 uint32_t tag)
12141{
12142 struct lpfc_dmabuf *mp, *next_mp;
12143 struct list_head *slp = &pring->postbufq;
12144
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012145 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040012146 spin_lock_irq(&phba->hbalock);
12147 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12148 if (mp->buffer_tag == tag) {
12149 list_del_init(&mp->list);
12150 pring->postbufq_cnt--;
12151 spin_unlock_irq(&phba->hbalock);
12152 return mp;
12153 }
12154 }
12155
12156 spin_unlock_irq(&phba->hbalock);
Dick Kennedy372c1872020-06-30 14:50:00 -070012157 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd7c255b2008-08-24 21:50:00 -040012158 "0402 Cannot find virtual addr for buffer tag on "
James Smart32350662019-08-14 16:57:06 -070012159 "ring %d Data x%lx x%px x%px x%x\n",
James Smart76bb24e2007-10-27 13:38:00 -040012160 pring->ringno, (unsigned long) tag,
12161 slp->next, slp->prev, pring->postbufq_cnt);
12162
12163 return NULL;
12164}
dea31012005-04-17 16:05:31 -050012165
James Smarte59058c2008-08-24 21:49:00 -040012166/**
James Smart3621a712009-04-06 18:47:14 -040012167 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040012168 * @phba: Pointer to HBA context object.
12169 * @pring: Pointer to driver SLI ring object.
12170 * @phys: DMA address of the buffer.
12171 *
12172 * This function searches the buffer list using the dma_address
12173 * of unsolicited event to find the driver's lpfc_dmabuf object
12174 * corresponding to the dma_address. The function returns the
12175 * lpfc_dmabuf object if a buffer is found else it returns NULL.
12176 * This function is called by the ct and els unsolicited event
12177 * handlers to get the buffer associated with the unsolicited
12178 * event.
12179 *
12180 * This function is called with no lock held.
12181 **/
dea31012005-04-17 16:05:31 -050012182struct lpfc_dmabuf *
12183lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12184 dma_addr_t phys)
12185{
12186 struct lpfc_dmabuf *mp, *next_mp;
12187 struct list_head *slp = &pring->postbufq;
12188
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012189 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050012190 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050012191 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
12192 if (mp->phys == phys) {
12193 list_del_init(&mp->list);
12194 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050012195 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050012196 return mp;
12197 }
12198 }
12199
James Smart2e0fef82007-06-17 19:56:36 -050012200 spin_unlock_irq(&phba->hbalock);
Dick Kennedy372c1872020-06-30 14:50:00 -070012201 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040012202 "0410 Cannot find virtual addr for mapped buf on "
James Smart32350662019-08-14 16:57:06 -070012203 "ring %d Data x%llx x%px x%px x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012204 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050012205 slp->next, slp->prev, pring->postbufq_cnt);
12206 return NULL;
12207}
12208
James Smarte59058c2008-08-24 21:49:00 -040012209/**
James Smart3621a712009-04-06 18:47:14 -040012210 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040012211 * @phba: Pointer to HBA context object.
12212 * @cmdiocb: Pointer to driver command iocb object.
12213 * @rspiocb: Pointer to driver response iocb object.
12214 *
12215 * This function is the completion handler for the abort iocbs for
12216 * ELS commands. This function is called from the ELS ring event
12217 * handler with no lock held. This function frees memory resources
12218 * associated with the abort iocb.
12219 **/
dea31012005-04-17 16:05:31 -050012220static void
James Smart2e0fef82007-06-17 19:56:36 -050012221lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12222 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050012223{
James Smart2e0fef82007-06-17 19:56:36 -050012224 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040012225 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050012226 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040012227
12228 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050012229
12230 /*
12231 * Assume that the port already completed and returned, or
12232 * will return the iocb. Just Log the message.
12233 */
James Smart2680eea2007-04-25 09:52:55 -040012234 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
12235 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
12236
James Smart2e0fef82007-06-17 19:56:36 -050012237 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040012238 if (phba->sli_rev < LPFC_SLI_REV4) {
James Smartfaa832e2018-07-31 17:23:18 -070012239 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
12240 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
12241 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
12242 spin_unlock_irq(&phba->hbalock);
12243 goto release_iocb;
12244 }
James Smart45ed1192009-10-02 15:17:02 -040012245 if (abort_iotag != 0 &&
12246 abort_iotag <= phba->sli.last_iotag)
12247 abort_iocb =
12248 phba->sli.iocbq_lookup[abort_iotag];
12249 } else
12250 /* For sli4 the abort_tag is the XRI,
12251 * so the abort routine puts the iotag of the iocb
12252 * being aborted in the context field of the abort
12253 * IOCB.
12254 */
12255 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040012256
James Smart2a9bf3d2010-06-07 15:24:45 -040012257 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
James Smart32350662019-08-14 16:57:06 -070012258 "0327 Cannot abort els iocb x%px "
James Smart2a9bf3d2010-06-07 15:24:45 -040012259 "with tag %x context %x, abort status %x, "
12260 "abort code %x\n",
12261 abort_iocb, abort_iotag, abort_context,
12262 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040012263
James Smartff78d8f2011-12-13 13:21:35 -050012264 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040012265 }
James Smartfaa832e2018-07-31 17:23:18 -070012266release_iocb:
James Bottomley604a3e32005-10-29 10:28:33 -050012267 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050012268 return;
12269}
12270
James Smarte59058c2008-08-24 21:49:00 -040012271/**
James Smart3621a712009-04-06 18:47:14 -040012272 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040012273 * @phba: Pointer to HBA context object.
12274 * @cmdiocb: Pointer to driver command iocb object.
12275 * @rspiocb: Pointer to driver response iocb object.
12276 *
12277 * The function is called from SLI ring event handler with no
12278 * lock held. This function is the completion handler for ELS commands
12279 * which are aborted. The function frees memory resources used for
12280 * the aborted ELS commands.
12281 **/
James Smart9dd83f72021-03-01 09:18:11 -080012282void
James Smart92d7f7b2007-06-17 19:56:38 -050012283lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12284 struct lpfc_iocbq *rspiocb)
12285{
James Smart69a3a7b2021-10-04 16:12:10 -070012286 struct lpfc_nodelist *ndlp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050012287 IOCB_t *irsp = &rspiocb->iocb;
12288
12289 /* ELS cmd tag <ulpIoTag> completes */
12290 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart69a3a7b2021-10-04 16:12:10 -070012291 "0139 Ignoring ELS cmd code x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050012292 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012293 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050012294 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart4e670c8a2021-07-07 11:43:38 -070012295 /*
12296 * Deref the ndlp after free_iocb. sli_release_iocb will access the ndlp
12297 * if exchange is busy.
12298 */
James Smart69a3a7b2021-10-04 16:12:10 -070012299 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
12300 ndlp = cmdiocb->context_un.ndlp;
James Smart858c9f62007-06-17 19:56:39 -050012301 lpfc_ct_free_iocb(phba, cmdiocb);
James Smart69a3a7b2021-10-04 16:12:10 -070012302 } else {
12303 ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
James Smart858c9f62007-06-17 19:56:39 -050012304 lpfc_els_free_iocb(phba, cmdiocb);
James Smart69a3a7b2021-10-04 16:12:10 -070012305 }
James Smart4e670c8a2021-07-07 11:43:38 -070012306
12307 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -050012308}
12309
James Smarte59058c2008-08-24 21:49:00 -040012310/**
James Smartdb7531d2020-11-15 11:26:44 -080012311 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040012312 * @phba: Pointer to HBA context object.
12313 * @pring: Pointer to driver SLI ring object.
12314 * @cmdiocb: Pointer to driver command iocb object.
James Smartdb7531d2020-11-15 11:26:44 -080012315 * @cmpl: completion function.
James Smarte59058c2008-08-24 21:49:00 -040012316 *
James Smartdb7531d2020-11-15 11:26:44 -080012317 * This function issues an abort iocb for the provided command iocb. In case
12318 * of unloading, the abort iocb will not be issued to commands on the ELS
12319 * ring. Instead, the callback function shall be changed to those commands
12320 * so that nothing happens when them finishes. This function is called with
12321 * hbalock held andno ring_lock held (SLI4). The function returns IOCB_SUCCESS
12322 * when the command iocb is an abort request.
12323 *
James Smarte59058c2008-08-24 21:49:00 -040012324 **/
James Smartdb7531d2020-11-15 11:26:44 -080012325int
12326lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
12327 struct lpfc_iocbq *cmdiocb, void *cmpl)
dea31012005-04-17 16:05:31 -050012328{
James Smart2e0fef82007-06-17 19:56:36 -050012329 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012330 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050012331 IOCB_t *icmd = NULL;
12332 IOCB_t *iabt = NULL;
James Smartdb7531d2020-11-15 11:26:44 -080012333 int retval = IOCB_ERROR;
James Smart7e56aa22012-08-03 12:35:34 -040012334 unsigned long iflags;
James Smartfaa832e2018-07-31 17:23:18 -070012335 struct lpfc_nodelist *ndlp;
James Smart07951072007-04-25 09:51:38 -040012336
James Smart92d7f7b2007-06-17 19:56:38 -050012337 /*
12338 * There are certain command types we don't want to abort. And we
12339 * don't want to abort commands that are already in the process of
12340 * being aborted.
James Smart07951072007-04-25 09:51:38 -040012341 */
12342 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050012343 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050012344 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
James Smart078c68b2021-04-11 18:31:12 -070012345 cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED)
James Smartdb7531d2020-11-15 11:26:44 -080012346 return IOCB_ABORTING;
12347
12348 if (!pring) {
12349 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
12350 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
12351 else
12352 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
12353 return retval;
12354 }
12355
12356 /*
12357 * If we're unloading, don't abort iocb on the ELS ring, but change
12358 * the callback so that nothing happens when it finishes.
12359 */
12360 if ((vport->load_flag & FC_UNLOADING) &&
12361 pring->ringno == LPFC_ELS_RING) {
12362 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
12363 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
12364 else
12365 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
12366 return retval;
12367 }
James Smart07951072007-04-25 09:51:38 -040012368
dea31012005-04-17 16:05:31 -050012369 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050012370 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050012371 if (abtsiocbp == NULL)
James Smartdb7531d2020-11-15 11:26:44 -080012372 return IOCB_NORESOURCE;
dea31012005-04-17 16:05:31 -050012373
James Smart07951072007-04-25 09:51:38 -040012374 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050012375 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040012376 */
12377 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
12378
dea31012005-04-17 16:05:31 -050012379 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040012380 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
12381 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040012382 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040012383 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smartdb7531d2020-11-15 11:26:44 -080012384 if (pring->ringno == LPFC_ELS_RING)
12385 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
James Smartfaa832e2018-07-31 17:23:18 -070012386 } else {
James Smartda0436e2009-05-22 14:51:39 -040012387 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
James Smartfaa832e2018-07-31 17:23:18 -070012388 if (pring->ringno == LPFC_ELS_RING) {
12389 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
12390 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
12391 }
12392 }
dea31012005-04-17 16:05:31 -050012393 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040012394 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050012395
James Smart5ffc2662009-11-18 15:39:44 -050012396 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080012397 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart1854f532021-10-20 14:14:15 -070012398 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
12399 abtsiocbp->iocb_flag |= (LPFC_IO_FCP | LPFC_USE_FCPWQIDX);
James Smart9bd2bff52014-09-03 12:57:30 -040012400 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
12401 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050012402
James Smart1854f532021-10-20 14:14:15 -070012403 if (phba->link_state < LPFC_LINK_UP ||
12404 (phba->sli_rev == LPFC_SLI_REV4 &&
12405 phba->sli4_hba.link_state.status == LPFC_FC_LA_TYPE_LINK_DOWN))
James Smart07951072007-04-25 09:51:38 -040012406 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
James Smart1854f532021-10-20 14:14:15 -070012407 else
12408 iabt->ulpCommand = CMD_ABORT_XRI_CN;
James Smart07951072007-04-25 09:51:38 -040012409
James Smartdb7531d2020-11-15 11:26:44 -080012410 if (cmpl)
12411 abtsiocbp->iocb_cmpl = cmpl;
12412 else
12413 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080012414 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040012415
James Smart7e56aa22012-08-03 12:35:34 -040012416 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080012417 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
12418 if (unlikely(pring == NULL))
James Smartdb7531d2020-11-15 11:26:44 -080012419 goto abort_iotag_exit;
James Smart7e56aa22012-08-03 12:35:34 -040012420 /* Note: both hbalock and ring_lock need to be set here */
12421 spin_lock_irqsave(&pring->ring_lock, iflags);
12422 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12423 abtsiocbp, 0);
12424 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12425 } else {
12426 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
12427 abtsiocbp, 0);
12428 }
James Smart07951072007-04-25 09:51:38 -040012429
12430abort_iotag_exit:
James Smartdb7531d2020-11-15 11:26:44 -080012431
12432 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
12433 "0339 Abort xri x%x, original iotag x%x, "
12434 "abort cmd iotag x%x retval x%x\n",
12435 iabt->un.acxri.abortIoTag,
12436 iabt->un.acxri.abortContextTag,
12437 abtsiocbp->iotag, retval);
12438
12439 if (retval) {
12440 cmdiocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12441 __lpfc_sli_release_iocbq(phba, abtsiocbp);
12442 }
12443
James Smart2e0fef82007-06-17 19:56:36 -050012444 /*
12445 * Caller to this routine should check for IOCB_ERROR
12446 * and handle it properly. This routine no longer removes
12447 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040012448 */
James Smart2e0fef82007-06-17 19:56:36 -050012449 return retval;
dea31012005-04-17 16:05:31 -050012450}
12451
James Smarte59058c2008-08-24 21:49:00 -040012452/**
James Smart5af5eee2010-10-22 11:06:38 -040012453 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
12454 * @phba: pointer to lpfc HBA data structure.
12455 *
12456 * This routine will abort all pending and outstanding iocbs to an HBA.
12457 **/
12458void
12459lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
12460{
12461 struct lpfc_sli *psli = &phba->sli;
12462 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080012463 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040012464 int i;
12465
James Smart895427b2017-02-12 13:52:30 -080012466 if (phba->sli_rev != LPFC_SLI_REV4) {
12467 for (i = 0; i < psli->num_rings; i++) {
12468 pring = &psli->sli3_ring[i];
12469 lpfc_sli_abort_iocb_ring(phba, pring);
12470 }
12471 return;
12472 }
12473 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
12474 pring = qp->pring;
12475 if (!pring)
12476 continue;
James Smartdb55fba2014-04-04 13:52:02 -040012477 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040012478 }
12479}
12480
12481/**
James Smartcd8a36a2021-09-10 16:31:53 -070012482 * lpfc_sli_validate_fcp_iocb_for_abort - filter iocbs appropriate for FCP aborts
12483 * @iocbq: Pointer to iocb object.
12484 * @vport: Pointer to driver virtual port object.
12485 *
12486 * This function acts as an iocb filter for functions which abort FCP iocbs.
12487 *
12488 * Return values
12489 * -ENODEV, if a null iocb or vport ptr is encountered
12490 * -EINVAL, if the iocb is not an FCP I/O, not on the TX cmpl queue, premarked as
12491 * driver already started the abort process, or is an abort iocb itself
12492 * 0, passes criteria for aborting the FCP I/O iocb
12493 **/
12494static int
12495lpfc_sli_validate_fcp_iocb_for_abort(struct lpfc_iocbq *iocbq,
12496 struct lpfc_vport *vport)
12497{
12498 IOCB_t *icmd = NULL;
12499
12500 /* No null ptr vports */
12501 if (!iocbq || iocbq->vport != vport)
12502 return -ENODEV;
12503
12504 /* iocb must be for FCP IO, already exists on the TX cmpl queue,
12505 * can't be premarked as driver aborted, nor be an ABORT iocb itself
12506 */
12507 icmd = &iocbq->iocb;
12508 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
12509 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ) ||
12510 (iocbq->iocb_flag & LPFC_DRIVER_ABORTED) ||
12511 (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
12512 icmd->ulpCommand == CMD_CLOSE_XRI_CN))
12513 return -EINVAL;
12514
12515 return 0;
12516}
12517
12518/**
12519 * lpfc_sli_validate_fcp_iocb - validate commands associated with a SCSI target
James Smarte59058c2008-08-24 21:49:00 -040012520 * @iocbq: Pointer to driver iocb object.
12521 * @vport: Pointer to driver virtual port object.
12522 * @tgt_id: SCSI ID of the target.
12523 * @lun_id: LUN ID of the scsi device.
12524 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
12525 *
James Smartcd8a36a2021-09-10 16:31:53 -070012526 * This function acts as an iocb filter for validating a lun/SCSI target/SCSI
12527 * host.
12528 *
12529 * It will return
James Smarte59058c2008-08-24 21:49:00 -040012530 * 0 if the filtering criteria is met for the given iocb and will return
12531 * 1 if the filtering criteria is not met.
12532 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
12533 * given iocb is for the SCSI device specified by vport, tgt_id and
12534 * lun_id parameter.
12535 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
12536 * given iocb is for the SCSI target specified by vport and tgt_id
12537 * parameters.
12538 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
12539 * given iocb is for the SCSI host associated with the given vport.
12540 * This function is called with no locks held.
12541 **/
dea31012005-04-17 16:05:31 -050012542static int
James Smart51ef4c22007-08-02 11:10:31 -040012543lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
12544 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012545 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050012546{
James Smartc4908502019-01-28 11:14:28 -080012547 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050012548 int rc = 1;
12549
James Smartc4908502019-01-28 11:14:28 -080012550 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012551
James Smart495a7142008-06-14 22:52:59 -040012552 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050012553 return rc;
12554
12555 switch (ctx_cmd) {
12556 case LPFC_CTX_LUN:
James Smartb0e83012018-06-26 08:24:29 -070012557 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040012558 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
12559 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050012560 rc = 0;
12561 break;
12562 case LPFC_CTX_TGT:
James Smartb0e83012018-06-26 08:24:29 -070012563 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040012564 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050012565 rc = 0;
12566 break;
dea31012005-04-17 16:05:31 -050012567 case LPFC_CTX_HOST:
12568 rc = 0;
12569 break;
12570 default:
12571 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070012572 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050012573 break;
12574 }
12575
12576 return rc;
12577}
12578
James Smarte59058c2008-08-24 21:49:00 -040012579/**
James Smart3621a712009-04-06 18:47:14 -040012580 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040012581 * @vport: Pointer to virtual port.
12582 * @tgt_id: SCSI ID of the target.
12583 * @lun_id: LUN ID of the scsi device.
12584 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12585 *
12586 * This function returns number of FCP commands pending for the vport.
12587 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
12588 * commands pending on the vport associated with SCSI device specified
12589 * by tgt_id and lun_id parameters.
12590 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
12591 * commands pending on the vport associated with SCSI target specified
12592 * by tgt_id parameter.
12593 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
12594 * commands pending on the vport.
12595 * This function returns the number of iocbs which satisfy the filter.
12596 * This function is called without any lock held.
12597 **/
dea31012005-04-17 16:05:31 -050012598int
James Smart51ef4c22007-08-02 11:10:31 -040012599lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
12600 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050012601{
James Smart51ef4c22007-08-02 11:10:31 -040012602 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012603 struct lpfc_iocbq *iocbq;
James Smartcd8a36a2021-09-10 16:31:53 -070012604 IOCB_t *icmd = NULL;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012605 int sum, i;
James Smartcd8a36a2021-09-10 16:31:53 -070012606 unsigned long iflags;
dea31012005-04-17 16:05:31 -050012607
James Smartcd8a36a2021-09-10 16:31:53 -070012608 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012609 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
12610 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050012611
James Smartcd8a36a2021-09-10 16:31:53 -070012612 if (!iocbq || iocbq->vport != vport)
12613 continue;
12614 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
12615 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
12616 continue;
12617
12618 /* Include counting outstanding aborts */
12619 icmd = &iocbq->iocb;
12620 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
12621 icmd->ulpCommand == CMD_CLOSE_XRI_CN) {
12622 sum++;
12623 continue;
12624 }
12625
12626 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12627 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012628 sum++;
dea31012005-04-17 16:05:31 -050012629 }
James Smartcd8a36a2021-09-10 16:31:53 -070012630 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012631
dea31012005-04-17 16:05:31 -050012632 return sum;
12633}
12634
James Smarte59058c2008-08-24 21:49:00 -040012635/**
James Smartdb7531d2020-11-15 11:26:44 -080012636 * lpfc_sli4_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
12637 * @phba: Pointer to HBA context object
12638 * @cmdiocb: Pointer to command iocb object.
12639 * @wcqe: pointer to the complete wcqe
12640 *
12641 * This function is called when an aborted FCP iocb completes. This
12642 * function is called by the ring event handler with no lock held.
12643 * This function frees the iocb. It is called for sli-4 adapters.
12644 **/
12645void
12646lpfc_sli4_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12647 struct lpfc_wcqe_complete *wcqe)
12648{
12649 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12650 "3017 ABORT_XRI_CN completing on rpi x%x "
12651 "original iotag x%x, abort cmd iotag x%x "
12652 "status 0x%x, reason 0x%x\n",
12653 cmdiocb->iocb.un.acxri.abortContextTag,
12654 cmdiocb->iocb.un.acxri.abortIoTag,
12655 cmdiocb->iotag,
12656 (bf_get(lpfc_wcqe_c_status, wcqe)
12657 & LPFC_IOCB_STATUS_MASK),
12658 wcqe->parameter);
12659 lpfc_sli_release_iocbq(phba, cmdiocb);
12660}
12661
12662/**
James Smart3621a712009-04-06 18:47:14 -040012663 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040012664 * @phba: Pointer to HBA context object
12665 * @cmdiocb: Pointer to command iocb object.
12666 * @rspiocb: Pointer to response iocb object.
12667 *
12668 * This function is called when an aborted FCP iocb completes. This
12669 * function is called by the ring event handler with no lock held.
12670 * This function frees the iocb.
12671 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040012672void
James Smart2e0fef82007-06-17 19:56:36 -050012673lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
12674 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040012675{
James Smartcb69f7d2011-12-13 13:21:57 -050012676 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040012677 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050012678 "original iotag x%x, abort cmd iotag x%x "
12679 "status 0x%x, reason 0x%x\n",
12680 cmdiocb->iocb.un.acxri.abortContextTag,
12681 cmdiocb->iocb.un.acxri.abortIoTag,
12682 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
12683 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050012684 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040012685 return;
12686}
12687
James Smarte59058c2008-08-24 21:49:00 -040012688/**
James Smart3621a712009-04-06 18:47:14 -040012689 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040012690 * @vport: Pointer to virtual port.
James Smarte59058c2008-08-24 21:49:00 -040012691 * @tgt_id: SCSI ID of the target.
12692 * @lun_id: LUN ID of the scsi device.
12693 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
12694 *
12695 * This function sends an abort command for every SCSI command
12696 * associated with the given virtual port pending on the ring
James Smartcd8a36a2021-09-10 16:31:53 -070012697 * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12698 * lpfc_sli_validate_fcp_iocb function. The ordering for validation before
12699 * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12700 * followed by lpfc_sli_validate_fcp_iocb.
12701 *
James Smarte59058c2008-08-24 21:49:00 -040012702 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
12703 * FCP iocbs associated with lun specified by tgt_id and lun_id
12704 * parameters
12705 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
12706 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12707 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
12708 * FCP iocbs associated with virtual port.
James Smart078c68b2021-04-11 18:31:12 -070012709 * The pring used for SLI3 is sli3_ring[LPFC_FCP_RING], for SLI4
12710 * lpfc_sli4_calc_ring is used.
James Smarte59058c2008-08-24 21:49:00 -040012711 * This function returns number of iocbs it failed to abort.
12712 * This function is called with no locks held.
12713 **/
dea31012005-04-17 16:05:31 -050012714int
James Smart078c68b2021-04-11 18:31:12 -070012715lpfc_sli_abort_iocb(struct lpfc_vport *vport, u16 tgt_id, u64 lun_id,
12716 lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050012717{
James Smart51ef4c22007-08-02 11:10:31 -040012718 struct lpfc_hba *phba = vport->phba;
James Smart078c68b2021-04-11 18:31:12 -070012719 struct lpfc_sli_ring *pring = NULL;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012720 struct lpfc_iocbq *iocbq;
dea31012005-04-17 16:05:31 -050012721 int errcnt = 0, ret_val = 0;
James Smartdb7531d2020-11-15 11:26:44 -080012722 unsigned long iflags;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012723 int i;
James Smart078c68b2021-04-11 18:31:12 -070012724 void *fcp_cmpl = NULL;
dea31012005-04-17 16:05:31 -050012725
James Smartb0e83012018-06-26 08:24:29 -070012726 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070012727 if (phba->hba_flag & HBA_IOQ_FLUSH)
James Smartb0e83012018-06-26 08:24:29 -070012728 return errcnt;
12729
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040012730 for (i = 1; i <= phba->sli.last_iotag; i++) {
12731 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050012732
James Smartcd8a36a2021-09-10 16:31:53 -070012733 if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12734 continue;
12735
James Smart51ef4c22007-08-02 11:10:31 -040012736 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050012737 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050012738 continue;
12739
James Smartdb7531d2020-11-15 11:26:44 -080012740 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart078c68b2021-04-11 18:31:12 -070012741 if (phba->sli_rev == LPFC_SLI_REV3) {
12742 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
12743 fcp_cmpl = lpfc_sli_abort_fcp_cmpl;
12744 } else if (phba->sli_rev == LPFC_SLI_REV4) {
12745 pring = lpfc_sli4_calc_ring(phba, iocbq);
12746 fcp_cmpl = lpfc_sli4_abort_fcp_cmpl;
12747 }
James Smartdb7531d2020-11-15 11:26:44 -080012748 ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocbq,
James Smart078c68b2021-04-11 18:31:12 -070012749 fcp_cmpl);
James Smartdb7531d2020-11-15 11:26:44 -080012750 spin_unlock_irqrestore(&phba->hbalock, iflags);
12751 if (ret_val != IOCB_SUCCESS)
dea31012005-04-17 16:05:31 -050012752 errcnt++;
dea31012005-04-17 16:05:31 -050012753 }
12754
12755 return errcnt;
12756}
12757
James Smarte59058c2008-08-24 21:49:00 -040012758/**
James Smart98912dda2014-04-04 13:52:31 -040012759 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
12760 * @vport: Pointer to virtual port.
12761 * @pring: Pointer to driver SLI ring object.
12762 * @tgt_id: SCSI ID of the target.
12763 * @lun_id: LUN ID of the scsi device.
Lee Jones7af29d42020-07-21 17:41:31 +010012764 * @cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
James Smart98912dda2014-04-04 13:52:31 -040012765 *
12766 * This function sends an abort command for every SCSI command
12767 * associated with the given virtual port pending on the ring
James Smartcd8a36a2021-09-10 16:31:53 -070012768 * filtered by lpfc_sli_validate_fcp_iocb_for_abort and then
12769 * lpfc_sli_validate_fcp_iocb function. The ordering for validation before
12770 * submitting abort iocbs must be lpfc_sli_validate_fcp_iocb_for_abort
12771 * followed by lpfc_sli_validate_fcp_iocb.
12772 *
James Smart98912dda2014-04-04 13:52:31 -040012773 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
12774 * FCP iocbs associated with lun specified by tgt_id and lun_id
12775 * parameters
12776 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
12777 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12778 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
12779 * FCP iocbs associated with virtual port.
12780 * This function returns number of iocbs it aborted .
12781 * This function is called with no locks held right after a taskmgmt
12782 * command is sent.
12783 **/
12784int
12785lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
12786 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
12787{
12788 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -080012789 struct lpfc_io_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040012790 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040012791 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040012792 struct lpfc_iocbq *iocbq;
12793 IOCB_t *icmd;
12794 int sum, i, ret_val;
12795 unsigned long iflags;
James Smartc2017262019-01-28 11:14:37 -080012796 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart98912dda2014-04-04 13:52:31 -040012797
James Smart59c68ea2018-04-09 14:24:25 -070012798 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040012799
12800 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070012801 if (phba->hba_flag & HBA_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070012802 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040012803 return 0;
12804 }
12805 sum = 0;
12806
12807 for (i = 1; i <= phba->sli.last_iotag; i++) {
12808 iocbq = phba->sli.iocbq_lookup[i];
12809
James Smartcd8a36a2021-09-10 16:31:53 -070012810 if (lpfc_sli_validate_fcp_iocb_for_abort(iocbq, vport))
12811 continue;
12812
James Smart98912dda2014-04-04 13:52:31 -040012813 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12814 cmd) != 0)
12815 continue;
12816
James Smartc2017262019-01-28 11:14:37 -080012817 /* Guard against IO completion being called at same time */
12818 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12819 spin_lock(&lpfc_cmd->buf_lock);
12820
12821 if (!lpfc_cmd->pCmd) {
12822 spin_unlock(&lpfc_cmd->buf_lock);
12823 continue;
12824 }
12825
12826 if (phba->sli_rev == LPFC_SLI_REV4) {
12827 pring_s4 =
James Smartc00f62e2019-08-14 16:57:11 -070012828 phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
James Smartc2017262019-01-28 11:14:37 -080012829 if (!pring_s4) {
12830 spin_unlock(&lpfc_cmd->buf_lock);
12831 continue;
12832 }
12833 /* Note: both hbalock and ring_lock must be set here */
12834 spin_lock(&pring_s4->ring_lock);
12835 }
12836
James Smart98912dda2014-04-04 13:52:31 -040012837 /*
12838 * If the iocbq is already being aborted, don't take a second
12839 * action, but do count it.
12840 */
James Smartc2017262019-01-28 11:14:37 -080012841 if ((iocbq->iocb_flag & LPFC_DRIVER_ABORTED) ||
12842 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
12843 if (phba->sli_rev == LPFC_SLI_REV4)
12844 spin_unlock(&pring_s4->ring_lock);
12845 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040012846 continue;
James Smartc2017262019-01-28 11:14:37 -080012847 }
James Smart98912dda2014-04-04 13:52:31 -040012848
12849 /* issue ABTS for this IOCB based on iotag */
12850 abtsiocbq = __lpfc_sli_get_iocbq(phba);
James Smartc2017262019-01-28 11:14:37 -080012851 if (!abtsiocbq) {
12852 if (phba->sli_rev == LPFC_SLI_REV4)
12853 spin_unlock(&pring_s4->ring_lock);
12854 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040012855 continue;
James Smartc2017262019-01-28 11:14:37 -080012856 }
James Smart98912dda2014-04-04 13:52:31 -040012857
12858 icmd = &iocbq->iocb;
12859 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
12860 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
12861 if (phba->sli_rev == LPFC_SLI_REV4)
12862 abtsiocbq->iocb.un.acxri.abortIoTag =
12863 iocbq->sli4_xritag;
12864 else
12865 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
12866 abtsiocbq->iocb.ulpLe = 1;
12867 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
12868 abtsiocbq->vport = vport;
12869
12870 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080012871 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040012872 if (iocbq->iocb_flag & LPFC_IO_FCP)
12873 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040012874 if (iocbq->iocb_flag & LPFC_IO_FOF)
12875 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040012876
James Smart8c50d252014-09-03 12:58:16 -040012877 ndlp = lpfc_cmd->rdata->pnode;
12878
12879 if (lpfc_is_link_up(phba) &&
12880 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040012881 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
12882 else
12883 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
12884
12885 /* Setup callback routine and issue the command. */
12886 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
12887
12888 /*
12889 * Indicate the IO is being aborted by the driver and set
12890 * the caller's flag into the aborted IO.
12891 */
12892 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
12893
12894 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -040012895 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
12896 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070012897 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040012898 } else {
12899 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
12900 abtsiocbq, 0);
12901 }
12902
James Smartc2017262019-01-28 11:14:37 -080012903 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040012904
12905 if (ret_val == IOCB_ERROR)
12906 __lpfc_sli_release_iocbq(phba, abtsiocbq);
12907 else
12908 sum++;
12909 }
James Smart59c68ea2018-04-09 14:24:25 -070012910 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040012911 return sum;
12912}
12913
12914/**
James Smart3621a712009-04-06 18:47:14 -040012915 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040012916 * @phba: Pointer to HBA context object.
12917 * @cmdiocbq: Pointer to command iocb.
12918 * @rspiocbq: Pointer to response iocb.
12919 *
12920 * This function is the completion handler for iocbs issued using
12921 * lpfc_sli_issue_iocb_wait function. This function is called by the
12922 * ring event handler function without any lock held. This function
12923 * can be called from both worker thread context and interrupt
12924 * context. This function also can be called from other thread which
12925 * cleans up the SLI layer objects.
12926 * This function copy the contents of the response iocb to the
12927 * response iocb memory object provided by the caller of
12928 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
12929 * sleeps for the iocb completion.
12930 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012931static void
12932lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
12933 struct lpfc_iocbq *cmdiocbq,
12934 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050012935{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012936 wait_queue_head_t *pdone_q;
12937 unsigned long iflags;
James Smartc4908502019-01-28 11:14:28 -080012938 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050012939
James Smart2e0fef82007-06-17 19:56:36 -050012940 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040012941 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
12942
12943 /*
12944 * A time out has occurred for the iocb. If a time out
12945 * completion handler has been supplied, call it. Otherwise,
12946 * just free the iocbq.
12947 */
12948
12949 spin_unlock_irqrestore(&phba->hbalock, iflags);
12950 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
12951 cmdiocbq->wait_iocb_cmpl = NULL;
12952 if (cmdiocbq->iocb_cmpl)
12953 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
12954 else
12955 lpfc_sli_release_iocbq(phba, cmdiocbq);
12956 return;
12957 }
12958
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012959 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
12960 if (cmdiocbq->context2 && rspiocbq)
12961 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
12962 &rspiocbq->iocb, sizeof(IOCB_t));
12963
James Smart0f65ff62010-02-26 14:14:23 -050012964 /* Set the exchange busy flag for task management commands */
12965 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
12966 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smartc4908502019-01-28 11:14:28 -080012967 lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
James Smart0f65ff62010-02-26 14:14:23 -050012968 cur_iocbq);
James Smart324e1c42019-10-18 14:18:21 -070012969 if (rspiocbq && (rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY))
12970 lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
12971 else
12972 lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
James Smart0f65ff62010-02-26 14:14:23 -050012973 }
12974
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012975 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012976 if (pdone_q)
12977 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050012978 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050012979 return;
12980}
12981
James Smarte59058c2008-08-24 21:49:00 -040012982/**
James Smartd11e31d2009-06-10 17:23:06 -040012983 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
12984 * @phba: Pointer to HBA context object..
12985 * @piocbq: Pointer to command iocb.
12986 * @flag: Flag to test.
12987 *
12988 * This routine grabs the hbalock and then test the iocb_flag to
12989 * see if the passed in flag is set.
12990 * Returns:
12991 * 1 if flag is set.
12992 * 0 if flag is not set.
12993 **/
12994static int
12995lpfc_chk_iocb_flg(struct lpfc_hba *phba,
12996 struct lpfc_iocbq *piocbq, uint32_t flag)
12997{
12998 unsigned long iflags;
12999 int ret;
13000
13001 spin_lock_irqsave(&phba->hbalock, iflags);
13002 ret = piocbq->iocb_flag & flag;
13003 spin_unlock_irqrestore(&phba->hbalock, iflags);
13004 return ret;
13005
13006}
13007
13008/**
James Smart3621a712009-04-06 18:47:14 -040013009 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040013010 * @phba: Pointer to HBA context object..
Lee Jones7af29d42020-07-21 17:41:31 +010013011 * @ring_number: Ring number
James Smarte59058c2008-08-24 21:49:00 -040013012 * @piocb: Pointer to command iocb.
13013 * @prspiocbq: Pointer to response iocb.
13014 * @timeout: Timeout in number of seconds.
13015 *
13016 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040013017 * iocb to complete. The iocb_cmpl field of the shall be used
13018 * to handle iocbs which time out. If the field is NULL, the
13019 * function shall free the iocbq structure. If more clean up is
13020 * needed, the caller is expected to provide a completion function
13021 * that will provide the needed clean up. If the iocb command is
13022 * not completed within timeout seconds, the function will either
13023 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
13024 * completion function set in the iocb_cmpl field and then return
13025 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
13026 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040013027 * The function waits for the iocb completion using an
13028 * non-interruptible wait.
13029 * This function will sleep while waiting for iocb completion.
13030 * So, this function should not be called from any context which
13031 * does not allow sleeping. Due to the same reason, this function
13032 * cannot be called with interrupt disabled.
13033 * This function assumes that the iocb completions occur while
13034 * this function sleep. So, this function cannot be called from
13035 * the thread which process iocb completion for this ring.
13036 * This function clears the iocb_flag of the iocb object before
13037 * issuing the iocb and the iocb completion handler sets this
13038 * flag and wakes this thread when the iocb completes.
13039 * The contents of the response iocb will be copied to prspiocbq
13040 * by the completion handler when the command completes.
13041 * This function returns IOCB_SUCCESS when success.
13042 * This function is called with no lock held.
13043 **/
dea31012005-04-17 16:05:31 -050013044int
James Smart2e0fef82007-06-17 19:56:36 -050013045lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040013046 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050013047 struct lpfc_iocbq *piocb,
13048 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013049 uint32_t timeout)
dea31012005-04-17 16:05:31 -050013050{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080013051 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013052 long timeleft, timeout_req = 0;
13053 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050013054 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050013055 struct lpfc_iocbq *iocb;
13056 int txq_cnt = 0;
13057 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080013058 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040013059 unsigned long iflags;
13060 bool iocb_completed = true;
13061
James Smart895427b2017-02-12 13:52:30 -080013062 if (phba->sli_rev >= LPFC_SLI_REV4)
13063 pring = lpfc_sli4_calc_ring(phba, piocb);
13064 else
13065 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050013066 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013067 * If the caller has provided a response iocbq buffer, then context2
13068 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050013069 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013070 if (prspiocbq) {
13071 if (piocb->context2)
13072 return IOCB_ERROR;
13073 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050013074 }
13075
James Smart5a0916b2013-07-15 18:31:42 -040013076 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013077 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
13078 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040013079 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050013080
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050013081 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050013082 if (lpfc_readl(phba->HCregaddr, &creg_val))
13083 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050013084 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
13085 writel(creg_val, phba->HCregaddr);
13086 readl(phba->HCregaddr); /* flush */
13087 }
13088
James Smart2a9bf3d2010-06-07 15:24:45 -040013089 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
13090 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013091 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040013092 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013093 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040013094 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013095 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040013096 spin_lock_irqsave(&phba->hbalock, iflags);
13097 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050013098
James Smart5a0916b2013-07-15 18:31:42 -040013099 /*
13100 * IOCB timed out. Inform the wake iocb wait
13101 * completion function and set local status
13102 */
13103
13104 iocb_completed = false;
13105 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
13106 }
13107 spin_unlock_irqrestore(&phba->hbalock, iflags);
13108 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040013109 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040013110 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040013111 /* Note: we are not indicating if the IOCB has a success
13112 * status or not - that's for the caller to check.
13113 * IOCB_SUCCESS means just that the command was sent and
13114 * completed. Not that it completed successfully.
13115 * */
James Smart7054a602007-04-25 09:52:34 -040013116 } else if (timeleft == 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013117 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040013118 "0338 IOCB wait timeout error - no "
13119 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013120 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040013121 } else {
Dick Kennedy372c1872020-06-30 14:50:00 -070013122 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040013123 "0330 IOCB wake NOT set, "
13124 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013125 timeout, (timeleft / jiffies));
13126 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050013127 }
James Smart2a9bf3d2010-06-07 15:24:45 -040013128 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050013129 if (phba->cfg_log_verbose & LOG_SLI) {
13130 list_for_each_entry(iocb, &pring->txq, list) {
13131 txq_cnt++;
13132 }
13133 list_for_each_entry(iocb, &pring->txcmplq, list) {
13134 txcmplq_cnt++;
13135 }
13136 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13137 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
13138 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
13139 }
James Smart2a9bf3d2010-06-07 15:24:45 -040013140 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013141 } else {
13142 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040013143 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040013144 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013145 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050013146 }
13147
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050013148 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050013149 if (lpfc_readl(phba->HCregaddr, &creg_val))
13150 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050013151 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
13152 writel(creg_val, phba->HCregaddr);
13153 readl(phba->HCregaddr); /* flush */
13154 }
13155
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013156 if (prspiocbq)
13157 piocb->context2 = NULL;
13158
13159 piocb->context_un.wait_queue = NULL;
13160 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050013161 return retval;
13162}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040013163
James Smarte59058c2008-08-24 21:49:00 -040013164/**
James Smart3621a712009-04-06 18:47:14 -040013165 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040013166 * @phba: Pointer to HBA context object.
13167 * @pmboxq: Pointer to driver mailbox object.
13168 * @timeout: Timeout in number of seconds.
13169 *
13170 * This function issues the mailbox to firmware and waits for the
13171 * mailbox command to complete. If the mailbox command is not
13172 * completed within timeout seconds, it returns MBX_TIMEOUT.
13173 * The function waits for the mailbox completion using an
13174 * interruptible wait. If the thread is woken up due to a
13175 * signal, MBX_TIMEOUT error is returned to the caller. Caller
13176 * should not free the mailbox resources, if this function returns
13177 * MBX_TIMEOUT.
13178 * This function will sleep while waiting for mailbox completion.
13179 * So, this function should not be called from any context which
13180 * does not allow sleeping. Due to the same reason, this function
13181 * cannot be called with interrupt disabled.
13182 * This function assumes that the mailbox completion occurs while
13183 * this function sleep. So, this function cannot be called from
13184 * the worker thread which processes mailbox completion.
13185 * This function is called in the context of HBA management
13186 * applications.
13187 * This function returns MBX_SUCCESS when successful.
13188 * This function is called with no lock held.
13189 **/
dea31012005-04-17 16:05:31 -050013190int
James Smart2e0fef82007-06-17 19:56:36 -050013191lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050013192 uint32_t timeout)
13193{
James Smarte29d74f2018-03-05 12:04:07 -080013194 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050013195 int retval;
James Smart858c9f62007-06-17 19:56:39 -050013196 unsigned long flag;
dea31012005-04-17 16:05:31 -050013197
James Smart495a7142008-06-14 22:52:59 -040013198 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050013199 /* setup wake call as IOCB callback */
13200 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050013201
James Smarte29d74f2018-03-05 12:04:07 -080013202 /* setup context3 field to pass wait_queue pointer to wake function */
13203 init_completion(&mbox_done);
13204 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050013205 /* now issue the command */
13206 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050013207 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080013208 wait_for_completion_timeout(&mbox_done,
13209 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040013210
James Smart858c9f62007-06-17 19:56:39 -050013211 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080013212 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040013213 /*
13214 * if LPFC_MBX_WAKE flag is set the mailbox is completed
13215 * else do not free the resources.
13216 */
James Smartd7c47992010-06-08 18:31:54 -040013217 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050013218 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040013219 } else {
James Smart7054a602007-04-25 09:52:34 -040013220 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050013221 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13222 }
13223 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050013224 }
dea31012005-04-17 16:05:31 -050013225 return retval;
13226}
13227
James Smarte59058c2008-08-24 21:49:00 -040013228/**
James Smart3772a992009-05-22 14:50:54 -040013229 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040013230 * @phba: Pointer to HBA context.
Lee Jones7af29d42020-07-21 17:41:31 +010013231 * @mbx_action: Mailbox shutdown options.
James Smarte59058c2008-08-24 21:49:00 -040013232 *
James Smart3772a992009-05-22 14:50:54 -040013233 * This function is called to shutdown the driver's mailbox sub-system.
13234 * It first marks the mailbox sub-system is in a block state to prevent
13235 * the asynchronous mailbox command from issued off the pending mailbox
13236 * command queue. If the mailbox command sub-system shutdown is due to
13237 * HBA error conditions such as EEH or ERATT, this routine shall invoke
13238 * the mailbox sub-system flush routine to forcefully bring down the
13239 * mailbox sub-system. Otherwise, if it is due to normal condition (such
13240 * as with offline or HBA function reset), this routine will wait for the
13241 * outstanding mailbox command to complete before invoking the mailbox
13242 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040013243 **/
James Smart3772a992009-05-22 14:50:54 -040013244void
James Smart618a5232012-06-12 13:54:36 -040013245lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040013246{
James Smart3772a992009-05-22 14:50:54 -040013247 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040013248 unsigned long timeout;
13249
James Smart618a5232012-06-12 13:54:36 -040013250 if (mbx_action == LPFC_MBX_NO_WAIT) {
13251 /* delay 100ms for port state */
13252 msleep(100);
13253 lpfc_sli_mbox_sys_flush(phba);
13254 return;
13255 }
James Smarta183a152011-10-10 21:32:43 -040013256 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050013257
James Smart523128e2018-09-10 10:30:46 -070013258 /* Disable softirqs, including timers from obtaining phba->hbalock */
13259 local_bh_disable();
13260
James Smart3772a992009-05-22 14:50:54 -040013261 spin_lock_irq(&phba->hbalock);
13262 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040013263
13264 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040013265 /* Determine how long we might wait for the active mailbox
13266 * command to be gracefully completed by firmware.
13267 */
James Smarta183a152011-10-10 21:32:43 -040013268 if (phba->sli.mbox_active)
13269 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
13270 phba->sli.mbox_active) *
13271 1000) + jiffies;
13272 spin_unlock_irq(&phba->hbalock);
13273
James Smart523128e2018-09-10 10:30:46 -070013274 /* Enable softirqs again, done with phba->hbalock */
13275 local_bh_enable();
13276
James Smart3772a992009-05-22 14:50:54 -040013277 while (phba->sli.mbox_active) {
13278 /* Check active mailbox complete status every 2ms */
13279 msleep(2);
13280 if (time_after(jiffies, timeout))
13281 /* Timeout, let the mailbox flush routine to
13282 * forcefully release active mailbox command
13283 */
13284 break;
13285 }
James Smart523128e2018-09-10 10:30:46 -070013286 } else {
James Smartd7069f02012-03-01 22:36:29 -050013287 spin_unlock_irq(&phba->hbalock);
13288
James Smart523128e2018-09-10 10:30:46 -070013289 /* Enable softirqs again, done with phba->hbalock */
13290 local_bh_enable();
13291 }
13292
James Smart3772a992009-05-22 14:50:54 -040013293 lpfc_sli_mbox_sys_flush(phba);
13294}
13295
13296/**
13297 * lpfc_sli_eratt_read - read sli-3 error attention events
13298 * @phba: Pointer to HBA context.
13299 *
13300 * This function is called to read the SLI3 device error attention registers
13301 * for possible error attention events. The caller must hold the hostlock
13302 * with spin_lock_irq().
13303 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013304 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040013305 * Register and returns 0 otherwise.
13306 **/
13307static int
13308lpfc_sli_eratt_read(struct lpfc_hba *phba)
13309{
James Smarted957682007-06-17 19:56:37 -050013310 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040013311
James Smart3772a992009-05-22 14:50:54 -040013312 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050013313 if (lpfc_readl(phba->HAregaddr, &ha_copy))
13314 goto unplug_err;
13315
James Smart3772a992009-05-22 14:50:54 -040013316 if (ha_copy & HA_ERATT) {
13317 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050013318 if (lpfc_sli_read_hs(phba))
13319 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040013320
James Smart3772a992009-05-22 14:50:54 -040013321 /* Check if there is a deferred error condition is active */
13322 if ((HS_FFER1 & phba->work_hs) &&
13323 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040013324 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040013325 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040013326 /* Clear all interrupt enable conditions */
13327 writel(0, phba->HCregaddr);
13328 readl(phba->HCregaddr);
13329 }
13330
13331 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040013332 phba->work_ha |= HA_ERATT;
13333 /* Indicate polling handles this ERATT */
13334 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040013335 return 1;
James Smartb4c02652006-07-06 15:50:43 -040013336 }
James Smart3772a992009-05-22 14:50:54 -040013337 return 0;
James Smart9940b972011-03-11 16:06:12 -050013338
13339unplug_err:
13340 /* Set the driver HS work bitmap */
13341 phba->work_hs |= UNPLUG_ERR;
13342 /* Set the driver HA work bitmap */
13343 phba->work_ha |= HA_ERATT;
13344 /* Indicate polling handles this ERATT */
13345 phba->hba_flag |= HBA_ERATT_HANDLED;
13346 return 1;
James Smartb4c02652006-07-06 15:50:43 -040013347}
13348
James Smarte59058c2008-08-24 21:49:00 -040013349/**
James Smartda0436e2009-05-22 14:51:39 -040013350 * lpfc_sli4_eratt_read - read sli-4 error attention events
13351 * @phba: Pointer to HBA context.
13352 *
13353 * This function is called to read the SLI4 device error attention registers
13354 * for possible error attention events. The caller must hold the hostlock
13355 * with spin_lock_irq().
13356 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013357 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040013358 * Register and returns 0 otherwise.
13359 **/
13360static int
13361lpfc_sli4_eratt_read(struct lpfc_hba *phba)
13362{
13363 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050013364 uint32_t if_type, portsmphr;
13365 struct lpfc_register portstat_reg;
James Smart5852ed22022-02-07 10:04:42 -080013366 u32 logmask;
James Smartda0436e2009-05-22 14:51:39 -040013367
James Smart2fcee4b2010-12-15 17:57:46 -050013368 /*
13369 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040013370 * registers for error attention. This can be changed later.
13371 */
James Smart2fcee4b2010-12-15 17:57:46 -050013372 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
13373 switch (if_type) {
13374 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050013375 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
13376 &uerr_sta_lo) ||
13377 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
13378 &uerr_sta_hi)) {
13379 phba->work_hs |= UNPLUG_ERR;
13380 phba->work_ha |= HA_ERATT;
13381 phba->hba_flag |= HBA_ERATT_HANDLED;
13382 return 1;
13383 }
James Smart2fcee4b2010-12-15 17:57:46 -050013384 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
13385 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013386 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050013387 "1423 HBA Unrecoverable error: "
13388 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
13389 "ue_mask_lo_reg=0x%x, "
13390 "ue_mask_hi_reg=0x%x\n",
13391 uerr_sta_lo, uerr_sta_hi,
13392 phba->sli4_hba.ue_mask_lo,
13393 phba->sli4_hba.ue_mask_hi);
13394 phba->work_status[0] = uerr_sta_lo;
13395 phba->work_status[1] = uerr_sta_hi;
13396 phba->work_ha |= HA_ERATT;
13397 phba->hba_flag |= HBA_ERATT_HANDLED;
13398 return 1;
13399 }
13400 break;
13401 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080013402 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050013403 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
13404 &portstat_reg.word0) ||
13405 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
13406 &portsmphr)){
13407 phba->work_hs |= UNPLUG_ERR;
13408 phba->work_ha |= HA_ERATT;
13409 phba->hba_flag |= HBA_ERATT_HANDLED;
13410 return 1;
13411 }
James Smart2fcee4b2010-12-15 17:57:46 -050013412 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
13413 phba->work_status[0] =
13414 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
13415 phba->work_status[1] =
13416 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
James Smart5852ed22022-02-07 10:04:42 -080013417 logmask = LOG_TRACE_EVENT;
13418 if (phba->work_status[0] ==
13419 SLIPORT_ERR1_REG_ERR_CODE_2 &&
13420 phba->work_status[1] == SLIPORT_ERR2_REG_FW_RESTART)
13421 logmask = LOG_SLI;
13422 lpfc_printf_log(phba, KERN_ERR, logmask,
James Smart2e90f4b2011-12-13 13:22:37 -050013423 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050013424 "port status reg 0x%x, "
13425 "port smphr reg 0x%x, "
13426 "error 1=0x%x, error 2=0x%x\n",
13427 portstat_reg.word0,
13428 portsmphr,
13429 phba->work_status[0],
13430 phba->work_status[1]);
13431 phba->work_ha |= HA_ERATT;
13432 phba->hba_flag |= HBA_ERATT_HANDLED;
13433 return 1;
13434 }
13435 break;
13436 case LPFC_SLI_INTF_IF_TYPE_1:
13437 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013438 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050013439 "2886 HBA Error Attention on unsupported "
13440 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050013441 return 1;
James Smartda0436e2009-05-22 14:51:39 -040013442 }
James Smart2fcee4b2010-12-15 17:57:46 -050013443
James Smartda0436e2009-05-22 14:51:39 -040013444 return 0;
13445}
13446
13447/**
James Smart3621a712009-04-06 18:47:14 -040013448 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040013449 * @phba: Pointer to HBA context.
13450 *
James Smart3772a992009-05-22 14:50:54 -040013451 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040013452 * error attention register bit for error attention events.
13453 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013454 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040013455 * Register and returns 0 otherwise.
13456 **/
13457int
13458lpfc_sli_check_eratt(struct lpfc_hba *phba)
13459{
13460 uint32_t ha_copy;
13461
13462 /* If somebody is waiting to handle an eratt, don't process it
13463 * here. The brdkill function will do this.
13464 */
13465 if (phba->link_flag & LS_IGNORE_ERATT)
13466 return 0;
13467
13468 /* Check if interrupt handler handles this ERATT */
13469 spin_lock_irq(&phba->hbalock);
13470 if (phba->hba_flag & HBA_ERATT_HANDLED) {
13471 /* Interrupt handler has handled ERATT */
13472 spin_unlock_irq(&phba->hbalock);
13473 return 0;
13474 }
13475
James Smarta257bf92009-04-06 18:48:10 -040013476 /*
13477 * If there is deferred error attention, do not check for error
13478 * attention
13479 */
13480 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
13481 spin_unlock_irq(&phba->hbalock);
13482 return 0;
13483 }
13484
James Smart3772a992009-05-22 14:50:54 -040013485 /* If PCI channel is offline, don't process it */
13486 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040013487 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040013488 return 0;
13489 }
13490
13491 switch (phba->sli_rev) {
13492 case LPFC_SLI_REV2:
13493 case LPFC_SLI_REV3:
13494 /* Read chip Host Attention (HA) register */
13495 ha_copy = lpfc_sli_eratt_read(phba);
13496 break;
James Smartda0436e2009-05-22 14:51:39 -040013497 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050013498 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040013499 ha_copy = lpfc_sli4_eratt_read(phba);
13500 break;
James Smart3772a992009-05-22 14:50:54 -040013501 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013502 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040013503 "0299 Invalid SLI revision (%d)\n",
13504 phba->sli_rev);
13505 ha_copy = 0;
13506 break;
James Smart93996272008-08-24 21:50:30 -040013507 }
13508 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040013509
13510 return ha_copy;
13511}
13512
13513/**
13514 * lpfc_intr_state_check - Check device state for interrupt handling
13515 * @phba: Pointer to HBA context.
13516 *
13517 * This inline routine checks whether a device or its PCI slot is in a state
13518 * that the interrupt should be handled.
13519 *
13520 * This function returns 0 if the device or the PCI slot is in a state that
13521 * interrupt should be handled, otherwise -EIO.
13522 */
13523static inline int
13524lpfc_intr_state_check(struct lpfc_hba *phba)
13525{
13526 /* If the pci channel is offline, ignore all the interrupts */
13527 if (unlikely(pci_channel_offline(phba->pcidev)))
13528 return -EIO;
13529
13530 /* Update device level interrupt statistics */
13531 phba->sli.slistat.sli_intr++;
13532
13533 /* Ignore all interrupts during initialization. */
13534 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
13535 return -EIO;
13536
James Smart93996272008-08-24 21:50:30 -040013537 return 0;
13538}
13539
13540/**
James Smart3772a992009-05-22 14:50:54 -040013541 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040013542 * @irq: Interrupt number.
13543 * @dev_id: The device context pointer.
13544 *
James Smart93996272008-08-24 21:50:30 -040013545 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040013546 * service routine when device with SLI-3 interface spec is enabled with
13547 * MSI-X multi-message interrupt mode and there are slow-path events in
13548 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
13549 * interrupt mode, this function is called as part of the device-level
13550 * interrupt handler. When the PCI slot is in error recovery or the HBA
13551 * is undergoing initialization, the interrupt handler will not process
13552 * the interrupt. The link attention and ELS ring attention events are
13553 * handled by the worker thread. The interrupt handler signals the worker
13554 * thread and returns for these events. This function is called without
13555 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040013556 * structures.
13557 *
13558 * This function returns IRQ_HANDLED when interrupt is handled else it
13559 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040013560 **/
dea31012005-04-17 16:05:31 -050013561irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040013562lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050013563{
James Smart2e0fef82007-06-17 19:56:36 -050013564 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050013565 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050013566 uint32_t work_ha_copy;
13567 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050013568 unsigned long iflag;
dea31012005-04-17 16:05:31 -050013569 uint32_t control;
13570
James Smart92d7f7b2007-06-17 19:56:38 -050013571 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050013572 struct lpfc_vport *vport;
13573 struct lpfc_nodelist *ndlp;
13574 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050013575 LPFC_MBOXQ_t *pmb;
13576 int rc;
13577
dea31012005-04-17 16:05:31 -050013578 /*
13579 * Get the driver's phba structure from the dev_id and
13580 * assume the HBA is not interrupting.
13581 */
James Smart93996272008-08-24 21:50:30 -040013582 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050013583
13584 if (unlikely(!phba))
13585 return IRQ_NONE;
13586
dea31012005-04-17 16:05:31 -050013587 /*
James Smart93996272008-08-24 21:50:30 -040013588 * Stuff needs to be attented to when this function is invoked as an
13589 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050013590 */
James Smart93996272008-08-24 21:50:30 -040013591 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040013592 /* Check device state for handling interrupt */
13593 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040013594 return IRQ_NONE;
13595 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050013596 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050013597 if (lpfc_readl(phba->HAregaddr, &ha_copy))
13598 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040013599 /* If somebody is waiting to handle an eratt don't process it
13600 * here. The brdkill function will do this.
13601 */
13602 if (phba->link_flag & LS_IGNORE_ERATT)
13603 ha_copy &= ~HA_ERATT;
13604 /* Check the need for handling ERATT in interrupt handler */
13605 if (ha_copy & HA_ERATT) {
13606 if (phba->hba_flag & HBA_ERATT_HANDLED)
13607 /* ERATT polling has handled ERATT */
13608 ha_copy &= ~HA_ERATT;
13609 else
13610 /* Indicate interrupt handler handles ERATT */
13611 phba->hba_flag |= HBA_ERATT_HANDLED;
13612 }
James Smarta257bf92009-04-06 18:48:10 -040013613
13614 /*
13615 * If there is deferred error attention, do not check for any
13616 * interrupt.
13617 */
13618 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040013619 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040013620 return IRQ_NONE;
13621 }
13622
James Smart93996272008-08-24 21:50:30 -040013623 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050013624 if (lpfc_readl(phba->HCregaddr, &hc_copy))
13625 goto unplug_error;
13626
James Smarta747c9c2009-11-18 15:41:10 -050013627 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
13628 HC_LAINT_ENA | HC_ERINT_ENA),
13629 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040013630 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
13631 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050013632 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040013633 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050013634 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040013635 } else
13636 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050013637
dea31012005-04-17 16:05:31 -050013638 work_ha_copy = ha_copy & phba->work_ha_mask;
13639
James Smart93996272008-08-24 21:50:30 -040013640 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050013641 if (work_ha_copy & HA_LATT) {
13642 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
13643 /*
13644 * Turn off Link Attention interrupts
13645 * until CLEAR_LA done
13646 */
James Smart5b75da22008-12-04 22:39:35 -050013647 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050013648 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050013649 if (lpfc_readl(phba->HCregaddr, &control))
13650 goto unplug_error;
dea31012005-04-17 16:05:31 -050013651 control &= ~HC_LAINT_ENA;
13652 writel(control, phba->HCregaddr);
13653 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050013654 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050013655 }
13656 else
13657 work_ha_copy &= ~HA_LATT;
13658 }
13659
James Smart93996272008-08-24 21:50:30 -040013660 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050013661 /*
13662 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
13663 * the only slow ring.
13664 */
13665 status = (work_ha_copy &
13666 (HA_RXMASK << (4*LPFC_ELS_RING)));
13667 status >>= (4*LPFC_ELS_RING);
13668 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050013669 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050013670 if (lpfc_readl(phba->HCregaddr, &control))
13671 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040013672
13673 lpfc_debugfs_slow_ring_trc(phba,
13674 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
13675 control, status,
13676 (uint32_t)phba->sli.slistat.sli_intr);
13677
James Smart858c9f62007-06-17 19:56:39 -050013678 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040013679 lpfc_debugfs_slow_ring_trc(phba,
13680 "ISR Disable ring:"
13681 "pwork:x%x hawork:x%x wait:x%x",
13682 phba->work_ha, work_ha_copy,
13683 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040013684 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040013685
James Smart858c9f62007-06-17 19:56:39 -050013686 control &=
13687 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050013688 writel(control, phba->HCregaddr);
13689 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050013690 }
James Smarta58cbd52007-08-02 11:09:43 -040013691 else {
13692 lpfc_debugfs_slow_ring_trc(phba,
13693 "ISR slow ring: pwork:"
13694 "x%x hawork:x%x wait:x%x",
13695 phba->work_ha, work_ha_copy,
13696 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040013697 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040013698 }
James Smart5b75da22008-12-04 22:39:35 -050013699 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050013700 }
13701 }
James Smart5b75da22008-12-04 22:39:35 -050013702 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040013703 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050013704 if (lpfc_sli_read_hs(phba))
13705 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040013706 /*
13707 * Check if there is a deferred error condition
13708 * is active
13709 */
13710 if ((HS_FFER1 & phba->work_hs) &&
13711 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040013712 HS_FFER6 | HS_FFER7 | HS_FFER8) &
13713 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040013714 phba->hba_flag |= DEFER_ERATT;
13715 /* Clear all interrupt enable conditions */
13716 writel(0, phba->HCregaddr);
13717 readl(phba->HCregaddr);
13718 }
13719 }
13720
James Smart93996272008-08-24 21:50:30 -040013721 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050013722 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040013723 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040013724 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050013725 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050013726
13727 /* First check out the status word */
13728 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
13729 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050013730 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050013731 /*
13732 * Stray Mailbox Interrupt, mbxCommand <cmd>
13733 * mbxStatus <status>
13734 */
Dick Kennedy372c1872020-06-30 14:50:00 -070013735 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040013736 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050013737 "Interrupt mbxCommand x%x "
13738 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040013739 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050013740 pmbox->mbxCommand,
13741 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050013742 /* clear mailbox attention bit */
13743 work_ha_copy &= ~HA_MBATT;
13744 } else {
James Smart97eab632008-04-07 10:16:05 -040013745 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050013746 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050013747 phba->last_completion_time = jiffies;
13748 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050013749 if (pmb->mbox_cmpl) {
13750 lpfc_sli_pcimem_bcopy(mbox, pmbox,
13751 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040013752 if (pmb->out_ext_byte_len &&
James Smart3e1f0712018-11-29 16:09:29 -080013753 pmb->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -040013754 lpfc_sli_pcimem_bcopy(
13755 phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -080013756 pmb->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -040013757 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050013758 }
James Smart09372822008-01-11 01:52:54 -050013759 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13760 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13761
13762 lpfc_debugfs_disc_trc(vport,
13763 LPFC_DISC_TRC_MBOX_VPORT,
13764 "MBOX dflt rpi: : "
13765 "status:x%x rpi:x%x",
13766 (uint32_t)pmbox->mbxStatus,
13767 pmbox->un.varWords[0], 0);
13768
13769 if (!pmbox->mbxStatus) {
13770 mp = (struct lpfc_dmabuf *)
James Smart3e1f0712018-11-29 16:09:29 -080013771 (pmb->ctx_buf);
James Smart09372822008-01-11 01:52:54 -050013772 ndlp = (struct lpfc_nodelist *)
James Smart3e1f0712018-11-29 16:09:29 -080013773 pmb->ctx_ndlp;
James Smart09372822008-01-11 01:52:54 -050013774
13775 /* Reg_LOGIN of dflt RPI was
13776 * successful. new lets get
13777 * rid of the RPI using the
13778 * same mbox buffer.
13779 */
13780 lpfc_unreg_login(phba,
13781 vport->vpi,
13782 pmbox->un.varWords[0],
13783 pmb);
13784 pmb->mbox_cmpl =
13785 lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080013786 pmb->ctx_buf = mp;
13787 pmb->ctx_ndlp = ndlp;
James Smart09372822008-01-11 01:52:54 -050013788 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040013789 rc = lpfc_sli_issue_mbox(phba,
13790 pmb,
13791 MBX_NOWAIT);
13792 if (rc != MBX_BUSY)
13793 lpfc_printf_log(phba,
13794 KERN_ERR,
Dick Kennedy372c1872020-06-30 14:50:00 -070013795 LOG_TRACE_EVENT,
James Smartd7c255b2008-08-24 21:50:00 -040013796 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040013797 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040013798 if (rc != MBX_NOT_FINISHED)
13799 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050013800 }
13801 }
James Smart5b75da22008-12-04 22:39:35 -050013802 spin_lock_irqsave(
13803 &phba->pport->work_port_lock,
13804 iflag);
James Smart09372822008-01-11 01:52:54 -050013805 phba->pport->work_port_events &=
13806 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050013807 spin_unlock_irqrestore(
13808 &phba->pport->work_port_lock,
13809 iflag);
James Smarta22d73b2021-01-04 10:02:38 -080013810
13811 /* Do NOT queue MBX_HEARTBEAT to the worker
13812 * thread for processing.
13813 */
13814 if (pmbox->mbxCommand == MBX_HEARTBEAT) {
13815 /* Process mbox now */
13816 phba->sli.mbox_active = NULL;
13817 phba->sli.sli_flag &=
13818 ~LPFC_SLI_MBOX_ACTIVE;
13819 if (pmb->mbox_cmpl)
13820 pmb->mbox_cmpl(phba, pmb);
13821 } else {
13822 /* Queue to worker thread to process */
13823 lpfc_mbox_cmpl_put(phba, pmb);
13824 }
James Smart858c9f62007-06-17 19:56:39 -050013825 }
James Smart97eab632008-04-07 10:16:05 -040013826 } else
James Smart5b75da22008-12-04 22:39:35 -050013827 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040013828
James Smart92d7f7b2007-06-17 19:56:38 -050013829 if ((work_ha_copy & HA_MBATT) &&
13830 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050013831send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050013832 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040013833 do {
13834 rc = lpfc_sli_issue_mbox(phba, NULL,
13835 MBX_NOWAIT);
13836 } while (rc == MBX_NOT_FINISHED);
13837 if (rc != MBX_SUCCESS)
Dick Kennedy372c1872020-06-30 14:50:00 -070013838 lpfc_printf_log(phba, KERN_ERR,
13839 LOG_TRACE_EVENT,
13840 "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040013841 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050013842 }
13843
James Smart5b75da22008-12-04 22:39:35 -050013844 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050013845 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050013846 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040013847 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050013848 }
James Smart93996272008-08-24 21:50:30 -040013849 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050013850unplug_error:
13851 spin_unlock_irqrestore(&phba->hbalock, iflag);
13852 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050013853
James Smart3772a992009-05-22 14:50:54 -040013854} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040013855
13856/**
James Smart3772a992009-05-22 14:50:54 -040013857 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040013858 * @irq: Interrupt number.
13859 * @dev_id: The device context pointer.
13860 *
13861 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040013862 * service routine when device with SLI-3 interface spec is enabled with
13863 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13864 * ring event in the HBA. However, when the device is enabled with either
13865 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13866 * device-level interrupt handler. When the PCI slot is in error recovery
13867 * or the HBA is undergoing initialization, the interrupt handler will not
13868 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13869 * the intrrupt context. This function is called without any lock held.
13870 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040013871 *
13872 * This function returns IRQ_HANDLED when interrupt is handled else it
13873 * returns IRQ_NONE.
13874 **/
13875irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040013876lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040013877{
13878 struct lpfc_hba *phba;
13879 uint32_t ha_copy;
13880 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050013881 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080013882 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040013883
13884 /* Get the driver's phba structure from the dev_id and
13885 * assume the HBA is not interrupting.
13886 */
13887 phba = (struct lpfc_hba *) dev_id;
13888
13889 if (unlikely(!phba))
13890 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050013891
13892 /*
James Smart93996272008-08-24 21:50:30 -040013893 * Stuff needs to be attented to when this function is invoked as an
13894 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050013895 */
James Smart93996272008-08-24 21:50:30 -040013896 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040013897 /* Check device state for handling interrupt */
13898 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040013899 return IRQ_NONE;
13900 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050013901 if (lpfc_readl(phba->HAregaddr, &ha_copy))
13902 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040013903 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050013904 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040013905 /*
13906 * If there is deferred error attention, do not check for
13907 * any interrupt.
13908 */
13909 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040013910 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040013911 return IRQ_NONE;
13912 }
James Smart93996272008-08-24 21:50:30 -040013913 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
13914 phba->HAregaddr);
13915 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050013916 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040013917 } else
13918 ha_copy = phba->ha_copy;
13919
13920 /*
13921 * Process all events on FCP ring. Take the optimized path for FCP IO.
13922 */
13923 ha_copy &= ~(phba->work_ha_mask);
13924
13925 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050013926 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080013927 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050013928 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080013929 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050013930
13931 if (phba->cfg_multi_ring_support == 2) {
13932 /*
James Smart93996272008-08-24 21:50:30 -040013933 * Process all events on extra ring. Take the optimized path
13934 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050013935 */
James Smart93996272008-08-24 21:50:30 -040013936 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050013937 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050013938 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050013939 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080013940 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050013941 status);
13942 }
13943 }
dea31012005-04-17 16:05:31 -050013944 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040013945} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050013946
James Smart93996272008-08-24 21:50:30 -040013947/**
James Smart3772a992009-05-22 14:50:54 -040013948 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040013949 * @irq: Interrupt number.
13950 * @dev_id: The device context pointer.
13951 *
James Smart3772a992009-05-22 14:50:54 -040013952 * This function is the HBA device-level interrupt handler to device with
13953 * SLI-3 interface spec, called from the PCI layer when either MSI or
13954 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
13955 * requires driver attention. This function invokes the slow-path interrupt
13956 * attention handling function and fast-path interrupt attention handling
13957 * function in turn to process the relevant HBA attention events. This
13958 * function is called without any lock held. It gets the hbalock to access
13959 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040013960 *
13961 * This function returns IRQ_HANDLED when interrupt is handled, else it
13962 * returns IRQ_NONE.
13963 **/
13964irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040013965lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040013966{
13967 struct lpfc_hba *phba;
13968 irqreturn_t sp_irq_rc, fp_irq_rc;
13969 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050013970 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040013971
13972 /*
13973 * Get the driver's phba structure from the dev_id and
13974 * assume the HBA is not interrupting.
13975 */
13976 phba = (struct lpfc_hba *) dev_id;
13977
13978 if (unlikely(!phba))
13979 return IRQ_NONE;
13980
James Smart3772a992009-05-22 14:50:54 -040013981 /* Check device state for handling interrupt */
13982 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040013983 return IRQ_NONE;
13984
13985 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050013986 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
13987 spin_unlock(&phba->hbalock);
13988 return IRQ_HANDLED;
13989 }
13990
James Smart93996272008-08-24 21:50:30 -040013991 if (unlikely(!phba->ha_copy)) {
13992 spin_unlock(&phba->hbalock);
13993 return IRQ_NONE;
13994 } else if (phba->ha_copy & HA_ERATT) {
13995 if (phba->hba_flag & HBA_ERATT_HANDLED)
13996 /* ERATT polling has handled ERATT */
13997 phba->ha_copy &= ~HA_ERATT;
13998 else
13999 /* Indicate interrupt handler handles ERATT */
14000 phba->hba_flag |= HBA_ERATT_HANDLED;
14001 }
14002
James Smarta257bf92009-04-06 18:48:10 -040014003 /*
14004 * If there is deferred error attention, do not check for any interrupt.
14005 */
14006 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020014007 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040014008 return IRQ_NONE;
14009 }
14010
James Smart93996272008-08-24 21:50:30 -040014011 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050014012 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
14013 spin_unlock(&phba->hbalock);
14014 return IRQ_HANDLED;
14015 }
James Smarta747c9c2009-11-18 15:41:10 -050014016 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
14017 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
14018 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040014019 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050014020 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040014021 readl(phba->HAregaddr); /* flush */
14022 spin_unlock(&phba->hbalock);
14023
14024 /*
14025 * Invokes slow-path host attention interrupt handling as appropriate.
14026 */
14027
14028 /* status of events with mailbox and link attention */
14029 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
14030
14031 /* status of events with ELS ring */
14032 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
14033 status2 >>= (4*LPFC_ELS_RING);
14034
14035 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040014036 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040014037 else
14038 sp_irq_rc = IRQ_NONE;
14039
14040 /*
14041 * Invoke fast-path host attention interrupt handling as appropriate.
14042 */
14043
14044 /* status of events with FCP ring */
14045 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
14046 status1 >>= (4*LPFC_FCP_RING);
14047
14048 /* status of events with extra ring */
14049 if (phba->cfg_multi_ring_support == 2) {
14050 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
14051 status2 >>= (4*LPFC_EXTRA_RING);
14052 } else
14053 status2 = 0;
14054
14055 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040014056 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040014057 else
14058 fp_irq_rc = IRQ_NONE;
14059
14060 /* Return device-level interrupt handling status */
14061 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040014062} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040014063
14064/**
James Smart4f774512009-05-22 14:52:35 -040014065 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
14066 * @phba: pointer to lpfc hba data structure.
14067 *
14068 * This routine is invoked by the worker thread to process all the pending
14069 * SLI4 els abort xri events.
14070 **/
14071void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
14072{
14073 struct lpfc_cq_event *cq_event;
James Smarte7dab162020-10-20 13:27:12 -070014074 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -040014075
14076 /* First, declare the els xri abort event has been handled */
James Smarte7dab162020-10-20 13:27:12 -070014077 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040014078 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
James Smarte7dab162020-10-20 13:27:12 -070014079 spin_unlock_irqrestore(&phba->hbalock, iflags);
14080
James Smart4f774512009-05-22 14:52:35 -040014081 /* Now, handle all the els xri abort events */
James Smarte7dab162020-10-20 13:27:12 -070014082 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040014083 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
14084 /* Get the first event from the head of the event queue */
James Smart4f774512009-05-22 14:52:35 -040014085 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
14086 cq_event, struct lpfc_cq_event, list);
James Smarte7dab162020-10-20 13:27:12 -070014087 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14088 iflags);
James Smart4f774512009-05-22 14:52:35 -040014089 /* Notify aborted XRI for ELS work queue */
14090 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
James Smarte7dab162020-10-20 13:27:12 -070014091
James Smart4f774512009-05-22 14:52:35 -040014092 /* Free the event processed back to the free pool */
14093 lpfc_sli4_cq_event_release(phba, cq_event);
James Smarte7dab162020-10-20 13:27:12 -070014094 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14095 iflags);
James Smart4f774512009-05-22 14:52:35 -040014096 }
James Smarte7dab162020-10-20 13:27:12 -070014097 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040014098}
14099
James Smart341af102010-01-26 23:07:37 -050014100/**
14101 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
14102 * @phba: pointer to lpfc hba data structure
14103 * @pIocbIn: pointer to the rspiocbq
14104 * @pIocbOut: pointer to the cmdiocbq
14105 * @wcqe: pointer to the complete wcqe
14106 *
14107 * This routine transfers the fields of a command iocbq to a response iocbq
14108 * by copying all the IOCB fields from command iocbq and transferring the
14109 * completion status information from the complete wcqe.
14110 **/
James Smart4f774512009-05-22 14:52:35 -040014111static void
James Smart341af102010-01-26 23:07:37 -050014112lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
14113 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040014114 struct lpfc_iocbq *pIocbOut,
14115 struct lpfc_wcqe_complete *wcqe)
14116{
James Smartaf227412013-10-10 12:23:10 -040014117 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050014118 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040014119 uint32_t status, max_response;
14120 struct lpfc_dmabuf *dmabuf;
14121 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040014122 size_t offset = offsetof(struct lpfc_iocbq, iocb);
14123
14124 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
14125 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040014126 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050014127 status = bf_get(lpfc_wcqe_c_status, wcqe);
14128 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040014129 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
14130 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
14131 pIocbIn->iocb.un.fcpi.fcpi_parm =
14132 pIocbOut->iocb.un.fcpi.fcpi_parm -
14133 wcqe->total_data_placed;
14134 else
14135 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050014136 else {
James Smart4f774512009-05-22 14:52:35 -040014137 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040014138 switch (pIocbOut->iocb.ulpCommand) {
14139 case CMD_ELS_REQUEST64_CR:
14140 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
14141 bpl = (struct ulp_bde64 *)dmabuf->virt;
14142 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
14143 max_response = bde.tus.f.bdeSize;
14144 break;
14145 case CMD_GEN_REQUEST64_CR:
14146 max_response = 0;
14147 if (!pIocbOut->context3)
14148 break;
14149 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
14150 sizeof(struct ulp_bde64);
14151 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
14152 bpl = (struct ulp_bde64 *)dmabuf->virt;
14153 for (i = 0; i < numBdes; i++) {
14154 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
14155 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
14156 max_response += bde.tus.f.bdeSize;
14157 }
14158 break;
14159 default:
14160 max_response = wcqe->total_data_placed;
14161 break;
14162 }
14163 if (max_response < wcqe->total_data_placed)
14164 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
14165 else
14166 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
14167 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050014168 }
James Smart341af102010-01-26 23:07:37 -050014169
James Smartacd68592012-01-18 16:25:09 -050014170 /* Convert BG errors for completion status */
14171 if (status == CQE_STATUS_DI_ERROR) {
14172 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
14173
14174 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
14175 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
14176 else
14177 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
14178
14179 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
14180 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
14181 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14182 BGS_GUARD_ERR_MASK;
14183 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
14184 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14185 BGS_APPTAG_ERR_MASK;
14186 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
14187 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14188 BGS_REFTAG_ERR_MASK;
14189
14190 /* Check to see if there was any good data before the error */
14191 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
14192 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14193 BGS_HI_WATER_MARK_PRESENT_MASK;
14194 pIocbIn->iocb.unsli3.sli3_bg.bghm =
14195 wcqe->total_data_placed;
14196 }
14197
14198 /*
14199 * Set ALL the error bits to indicate we don't know what
14200 * type of error it is.
14201 */
14202 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
14203 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
14204 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
14205 BGS_GUARD_ERR_MASK);
14206 }
14207
James Smart341af102010-01-26 23:07:37 -050014208 /* Pick up HBA exchange busy condition */
14209 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
14210 spin_lock_irqsave(&phba->hbalock, iflags);
14211 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
14212 spin_unlock_irqrestore(&phba->hbalock, iflags);
14213 }
James Smart4f774512009-05-22 14:52:35 -040014214}
14215
14216/**
James Smart45ed1192009-10-02 15:17:02 -040014217 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
14218 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010014219 * @irspiocbq: Pointer to work-queue completion queue entry.
James Smart45ed1192009-10-02 15:17:02 -040014220 *
14221 * This routine handles an ELS work-queue completion event and construct
14222 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
14223 * discovery engine to handle.
14224 *
14225 * Return: Pointer to the receive IOCBQ, NULL otherwise.
14226 **/
14227static struct lpfc_iocbq *
14228lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
14229 struct lpfc_iocbq *irspiocbq)
14230{
James Smart895427b2017-02-12 13:52:30 -080014231 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040014232 struct lpfc_iocbq *cmdiocbq;
14233 struct lpfc_wcqe_complete *wcqe;
14234 unsigned long iflags;
14235
James Smart895427b2017-02-12 13:52:30 -080014236 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070014237 if (unlikely(!pring))
14238 return NULL;
James Smart895427b2017-02-12 13:52:30 -080014239
James Smart45ed1192009-10-02 15:17:02 -040014240 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart45ed1192009-10-02 15:17:02 -040014241 pring->stats.iocb_event++;
14242 /* Look up the ELS command IOCB and create pseudo response IOCB */
14243 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
14244 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040014245 if (unlikely(!cmdiocbq)) {
14246 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14247 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070014248 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
14249 wcqe->word0, wcqe->total_data_placed,
14250 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040014251 lpfc_sli_release_iocbq(phba, irspiocbq);
14252 return NULL;
14253 }
14254
James Smarte2a8be52019-05-06 17:26:47 -070014255 spin_lock_irqsave(&pring->ring_lock, iflags);
Dick Kennedy401bb412017-09-29 17:34:28 -070014256 /* Put the iocb back on the txcmplq */
14257 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
14258 spin_unlock_irqrestore(&pring->ring_lock, iflags);
14259
James Smart45ed1192009-10-02 15:17:02 -040014260 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050014261 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040014262
14263 return irspiocbq;
14264}
14265
James Smart8a5ca102017-11-20 16:00:30 -080014266inline struct lpfc_cq_event *
14267lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
14268{
14269 struct lpfc_cq_event *cq_event;
14270
14271 /* Allocate a new internal CQ_EVENT entry */
14272 cq_event = lpfc_sli4_cq_event_alloc(phba);
14273 if (!cq_event) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014274 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8a5ca102017-11-20 16:00:30 -080014275 "0602 Failed to alloc CQ_EVENT entry\n");
14276 return NULL;
14277 }
14278
14279 /* Move the CQE into the event */
14280 memcpy(&cq_event->cqe, entry, size);
14281 return cq_event;
14282}
14283
James Smart45ed1192009-10-02 15:17:02 -040014284/**
Colin Ian King291c2542019-12-18 08:43:01 +000014285 * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
James Smart04c68492009-05-22 14:52:52 -040014286 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010014287 * @mcqe: Pointer to mailbox completion queue entry.
James Smart04c68492009-05-22 14:52:52 -040014288 *
Colin Ian King291c2542019-12-18 08:43:01 +000014289 * This routine process a mailbox completion queue entry with asynchronous
James Smart04c68492009-05-22 14:52:52 -040014290 * event.
14291 *
14292 * Return: true if work posted to worker thread, otherwise false.
14293 **/
14294static bool
14295lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14296{
14297 struct lpfc_cq_event *cq_event;
14298 unsigned long iflags;
14299
14300 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14301 "0392 Async Event: word0:x%x, word1:x%x, "
14302 "word2:x%x, word3:x%x\n", mcqe->word0,
14303 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
14304
James Smart8a5ca102017-11-20 16:00:30 -080014305 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
14306 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040014307 return false;
James Smarte7dab162020-10-20 13:27:12 -070014308
14309 spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
James Smart04c68492009-05-22 14:52:52 -040014310 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
James Smarte7dab162020-10-20 13:27:12 -070014311 spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
14312
James Smart04c68492009-05-22 14:52:52 -040014313 /* Set the async event flag */
James Smarte7dab162020-10-20 13:27:12 -070014314 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart04c68492009-05-22 14:52:52 -040014315 phba->hba_flag |= ASYNC_EVENT;
14316 spin_unlock_irqrestore(&phba->hbalock, iflags);
14317
14318 return true;
14319}
14320
14321/**
14322 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
14323 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010014324 * @mcqe: Pointer to mailbox completion queue entry.
James Smart04c68492009-05-22 14:52:52 -040014325 *
14326 * This routine process a mailbox completion queue entry with mailbox
14327 * completion event.
14328 *
14329 * Return: true if work posted to worker thread, otherwise false.
14330 **/
14331static bool
14332lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
14333{
14334 uint32_t mcqe_status;
14335 MAILBOX_t *mbox, *pmbox;
14336 struct lpfc_mqe *mqe;
14337 struct lpfc_vport *vport;
14338 struct lpfc_nodelist *ndlp;
14339 struct lpfc_dmabuf *mp;
14340 unsigned long iflags;
14341 LPFC_MBOXQ_t *pmb;
14342 bool workposted = false;
14343 int rc;
14344
14345 /* If not a mailbox complete MCQE, out by checking mailbox consume */
14346 if (!bf_get(lpfc_trailer_completed, mcqe))
14347 goto out_no_mqe_complete;
14348
14349 /* Get the reference to the active mbox command */
14350 spin_lock_irqsave(&phba->hbalock, iflags);
14351 pmb = phba->sli.mbox_active;
14352 if (unlikely(!pmb)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014353 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart04c68492009-05-22 14:52:52 -040014354 "1832 No pending MBOX command to handle\n");
14355 spin_unlock_irqrestore(&phba->hbalock, iflags);
14356 goto out_no_mqe_complete;
14357 }
14358 spin_unlock_irqrestore(&phba->hbalock, iflags);
14359 mqe = &pmb->u.mqe;
14360 pmbox = (MAILBOX_t *)&pmb->u.mqe;
14361 mbox = phba->mbox;
14362 vport = pmb->vport;
14363
14364 /* Reset heartbeat timer */
14365 phba->last_completion_time = jiffies;
14366 del_timer(&phba->sli.mbox_tmo);
14367
14368 /* Move mbox data to caller's mailbox region, do endian swapping */
14369 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070014370 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040014371
James Smart73d91e52011-10-10 21:32:10 -040014372 /*
14373 * For mcqe errors, conditionally move a modified error code to
14374 * the mbox so that the error will not be missed.
14375 */
14376 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
14377 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
14378 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
14379 bf_set(lpfc_mqe_status, mqe,
14380 (LPFC_MBX_ERROR_RANGE | mcqe_status));
14381 }
James Smart04c68492009-05-22 14:52:52 -040014382 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
14383 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
14384 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
14385 "MBOX dflt rpi: status:x%x rpi:x%x",
14386 mcqe_status,
14387 pmbox->un.varWords[0], 0);
14388 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
James Smart3e1f0712018-11-29 16:09:29 -080014389 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
14390 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart01131e72021-05-14 12:55:49 -070014391
14392 /* Reg_LOGIN of dflt RPI was successful. Mark the
14393 * node as having an UNREG_LOGIN in progress to stop
14394 * an unsolicited PLOGI from the same NPortId from
14395 * starting another mailbox transaction.
James Smart04c68492009-05-22 14:52:52 -040014396 */
James Smart01131e72021-05-14 12:55:49 -070014397 spin_lock_irqsave(&ndlp->lock, iflags);
14398 ndlp->nlp_flag |= NLP_UNREG_INP;
14399 spin_unlock_irqrestore(&ndlp->lock, iflags);
James Smart04c68492009-05-22 14:52:52 -040014400 lpfc_unreg_login(phba, vport->vpi,
14401 pmbox->un.varWords[0], pmb);
14402 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080014403 pmb->ctx_buf = mp;
James Smarte9b11082020-11-15 11:26:33 -080014404
14405 /* No reference taken here. This is a default
14406 * RPI reg/immediate unreg cycle. The reference was
14407 * taken in the reg rpi path and is released when
14408 * this mailbox completes.
14409 */
James Smart3e1f0712018-11-29 16:09:29 -080014410 pmb->ctx_ndlp = ndlp;
James Smart04c68492009-05-22 14:52:52 -040014411 pmb->vport = vport;
14412 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
14413 if (rc != MBX_BUSY)
Dick Kennedy372c1872020-06-30 14:50:00 -070014414 lpfc_printf_log(phba, KERN_ERR,
14415 LOG_TRACE_EVENT,
14416 "0385 rc should "
James Smart04c68492009-05-22 14:52:52 -040014417 "have been MBX_BUSY\n");
14418 if (rc != MBX_NOT_FINISHED)
14419 goto send_current_mbox;
14420 }
14421 }
14422 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
14423 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
14424 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
14425
James Smarta22d73b2021-01-04 10:02:38 -080014426 /* Do NOT queue MBX_HEARTBEAT to the worker thread for processing. */
14427 if (pmbox->mbxCommand == MBX_HEARTBEAT) {
14428 spin_lock_irqsave(&phba->hbalock, iflags);
14429 /* Release the mailbox command posting token */
14430 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14431 phba->sli.mbox_active = NULL;
14432 if (bf_get(lpfc_trailer_consumed, mcqe))
14433 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
14434 spin_unlock_irqrestore(&phba->hbalock, iflags);
14435
14436 /* Post the next mbox command, if there is one */
14437 lpfc_sli4_post_async_mbox(phba);
14438
14439 /* Process cmpl now */
14440 if (pmb->mbox_cmpl)
14441 pmb->mbox_cmpl(phba, pmb);
14442 return false;
14443 }
14444
14445 /* There is mailbox completion work to queue to the worker thread */
James Smart04c68492009-05-22 14:52:52 -040014446 spin_lock_irqsave(&phba->hbalock, iflags);
14447 __lpfc_mbox_cmpl_put(phba, pmb);
14448 phba->work_ha |= HA_MBATT;
14449 spin_unlock_irqrestore(&phba->hbalock, iflags);
14450 workposted = true;
14451
14452send_current_mbox:
14453 spin_lock_irqsave(&phba->hbalock, iflags);
14454 /* Release the mailbox command posting token */
14455 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
14456 /* Setting active mailbox pointer need to be in sync to flag clear */
14457 phba->sli.mbox_active = NULL;
James Smart07b85822019-09-21 20:58:53 -070014458 if (bf_get(lpfc_trailer_consumed, mcqe))
14459 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
James Smart04c68492009-05-22 14:52:52 -040014460 spin_unlock_irqrestore(&phba->hbalock, iflags);
14461 /* Wake up worker thread to post the next pending mailbox command */
14462 lpfc_worker_wake_up(phba);
James Smart07b85822019-09-21 20:58:53 -070014463 return workposted;
14464
James Smart04c68492009-05-22 14:52:52 -040014465out_no_mqe_complete:
James Smart07b85822019-09-21 20:58:53 -070014466 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart04c68492009-05-22 14:52:52 -040014467 if (bf_get(lpfc_trailer_consumed, mcqe))
14468 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
James Smart07b85822019-09-21 20:58:53 -070014469 spin_unlock_irqrestore(&phba->hbalock, iflags);
14470 return false;
James Smart04c68492009-05-22 14:52:52 -040014471}
14472
14473/**
14474 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
14475 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010014476 * @cq: Pointer to associated CQ
James Smart04c68492009-05-22 14:52:52 -040014477 * @cqe: Pointer to mailbox completion queue entry.
14478 *
14479 * This routine process a mailbox completion queue entry, it invokes the
Colin Ian King291c2542019-12-18 08:43:01 +000014480 * proper mailbox complete handling or asynchronous event handling routine
James Smart04c68492009-05-22 14:52:52 -040014481 * according to the MCQE's async bit.
14482 *
14483 * Return: true if work posted to worker thread, otherwise false.
14484 **/
14485static bool
James Smart32517fc2019-01-28 11:14:33 -080014486lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14487 struct lpfc_cqe *cqe)
James Smart04c68492009-05-22 14:52:52 -040014488{
14489 struct lpfc_mcqe mcqe;
14490 bool workposted;
14491
James Smart32517fc2019-01-28 11:14:33 -080014492 cq->CQ_mbox++;
14493
James Smart04c68492009-05-22 14:52:52 -040014494 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070014495 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040014496
14497 /* Invoke the proper event handling routine */
14498 if (!bf_get(lpfc_trailer_async, &mcqe))
14499 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
14500 else
14501 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
14502 return workposted;
14503}
14504
14505/**
James Smart4f774512009-05-22 14:52:35 -040014506 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
14507 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040014508 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040014509 * @wcqe: Pointer to work-queue completion queue entry.
14510 *
14511 * This routine handles an ELS work-queue completion event.
14512 *
14513 * Return: true if work posted to worker thread, otherwise false.
14514 **/
14515static bool
James Smart2a76a282012-08-03 12:35:54 -040014516lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040014517 struct lpfc_wcqe_complete *wcqe)
14518{
James Smart4f774512009-05-22 14:52:35 -040014519 struct lpfc_iocbq *irspiocbq;
14520 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040014521 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050014522 int txq_cnt = 0;
14523 int txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040014524
James Smart11f0e342018-05-04 20:37:57 -070014525 /* Check for response status */
14526 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14527 /* Log the error status */
14528 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14529 "0357 ELS CQE error: status=x%x: "
14530 "CQE: %08x %08x %08x %08x\n",
14531 bf_get(lpfc_wcqe_c_status, wcqe),
14532 wcqe->word0, wcqe->total_data_placed,
14533 wcqe->parameter, wcqe->word3);
14534 }
14535
James Smart45ed1192009-10-02 15:17:02 -040014536 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040014537 irspiocbq = lpfc_sli_get_iocbq(phba);
14538 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050014539 if (!list_empty(&pring->txq))
14540 txq_cnt++;
14541 if (!list_empty(&pring->txcmplq))
14542 txcmplq_cnt++;
Dick Kennedy372c1872020-06-30 14:50:00 -070014543 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2a9bf3d2010-06-07 15:24:45 -040014544 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
James Smartff349bc2019-09-21 20:59:05 -070014545 "els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050014546 txq_cnt, phba->iocb_cnt,
James Smart0e9bb8d2013-03-01 16:35:12 -050014547 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040014548 return false;
James Smart4f774512009-05-22 14:52:35 -040014549 }
James Smart4f774512009-05-22 14:52:35 -040014550
James Smart45ed1192009-10-02 15:17:02 -040014551 /* Save off the slow-path queue event for work thread to process */
14552 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040014553 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040014554 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040014555 &phba->sli4_hba.sp_queue_event);
14556 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040014557 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040014558
James Smart45ed1192009-10-02 15:17:02 -040014559 return true;
James Smart4f774512009-05-22 14:52:35 -040014560}
14561
14562/**
14563 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
14564 * @phba: Pointer to HBA context object.
14565 * @wcqe: Pointer to work-queue completion queue entry.
14566 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080014567 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040014568 * proper WQ release routine to the slow-path WQ.
14569 **/
14570static void
14571lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
14572 struct lpfc_wcqe_release *wcqe)
14573{
James Smart2e90f4b2011-12-13 13:22:37 -050014574 /* sanity check on queue memory */
14575 if (unlikely(!phba->sli4_hba.els_wq))
14576 return;
James Smart4f774512009-05-22 14:52:35 -040014577 /* Check for the slow-path ELS work queue */
14578 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
14579 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
14580 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
14581 else
14582 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14583 "2579 Slow-path wqe consume event carries "
14584 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
14585 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
14586 phba->sli4_hba.els_wq->queue_id);
14587}
14588
14589/**
14590 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
14591 * @phba: Pointer to HBA context object.
14592 * @cq: Pointer to a WQ completion queue.
14593 * @wcqe: Pointer to work-queue completion queue entry.
14594 *
14595 * This routine handles an XRI abort event.
14596 *
14597 * Return: true if work posted to worker thread, otherwise false.
14598 **/
14599static bool
14600lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
14601 struct lpfc_queue *cq,
14602 struct sli4_wcqe_xri_aborted *wcqe)
14603{
14604 bool workposted = false;
14605 struct lpfc_cq_event *cq_event;
14606 unsigned long iflags;
14607
James Smart4f774512009-05-22 14:52:35 -040014608 switch (cq->subtype) {
James Smartc00f62e2019-08-14 16:57:11 -070014609 case LPFC_IO:
14610 lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
14611 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
14612 /* Notify aborted XRI for NVME work queue */
14613 if (phba->nvmet_support)
14614 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
14615 }
James Smart5e5b5112019-01-28 11:14:22 -080014616 workposted = false;
James Smart4f774512009-05-22 14:52:35 -040014617 break;
James Smart422c4cb2017-11-20 16:00:32 -080014618 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040014619 case LPFC_ELS:
James Smarte7dab162020-10-20 13:27:12 -070014620 cq_event = lpfc_cq_event_setup(phba, wcqe, sizeof(*wcqe));
14621 if (!cq_event) {
14622 workposted = false;
14623 break;
14624 }
James Smart5e5b5112019-01-28 11:14:22 -080014625 cq_event->hdwq = cq->hdwq;
James Smarte7dab162020-10-20 13:27:12 -070014626 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
14627 iflags);
James Smart4f774512009-05-22 14:52:35 -040014628 list_add_tail(&cq_event->list,
14629 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
14630 /* Set the els xri abort event flag */
14631 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
James Smarte7dab162020-10-20 13:27:12 -070014632 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
14633 iflags);
James Smart4f774512009-05-22 14:52:35 -040014634 workposted = true;
14635 break;
14636 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070014637 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart318083a2017-03-04 09:30:30 -080014638 "0603 Invalid CQ subtype %d: "
14639 "%08x %08x %08x %08x\n",
14640 cq->subtype, wcqe->word0, wcqe->parameter,
14641 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040014642 workposted = false;
14643 break;
14644 }
14645 return workposted;
14646}
14647
James Smarte817e5d2018-12-13 15:17:53 -080014648#define FC_RCTL_MDS_DIAGS 0xF4
14649
James Smart4f774512009-05-22 14:52:35 -040014650/**
James Smart4d9ab992009-10-02 15:16:39 -040014651 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040014652 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040014653 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040014654 *
James Smart4d9ab992009-10-02 15:16:39 -040014655 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040014656 *
14657 * Return: true if work posted to worker thread, otherwise false.
14658 **/
14659static bool
James Smart4d9ab992009-10-02 15:16:39 -040014660lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
14661{
14662 bool workposted = false;
James Smarte817e5d2018-12-13 15:17:53 -080014663 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040014664 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
14665 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070014666 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040014667 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040014668 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040014669 unsigned long iflags;
14670
James Smart2e90f4b2011-12-13 13:22:37 -050014671 /* sanity check on queue memory */
14672 if (unlikely(!hrq) || unlikely(!drq))
14673 return workposted;
14674
James Smart7851fe22011-07-22 18:36:52 -040014675 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14676 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14677 else
14678 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14679 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040014680 goto out;
14681
14682 status = bf_get(lpfc_rcqe_status, rcqe);
14683 switch (status) {
14684 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
Dick Kennedy372c1872020-06-30 14:50:00 -070014685 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4d9ab992009-10-02 15:16:39 -040014686 "2537 Receive Frame Truncated!!\n");
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050014687 fallthrough;
James Smart4d9ab992009-10-02 15:16:39 -040014688 case FC_STATUS_RQ_SUCCESS:
14689 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080014690 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040014691 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
14692 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040014693 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040014694 spin_unlock_irqrestore(&phba->hbalock, iflags);
14695 goto out;
14696 }
James Smartb84daac2012-08-03 12:35:13 -040014697 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070014698 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040014699 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080014700
James Smarte817e5d2018-12-13 15:17:53 -080014701 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14702
14703 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
14704 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
14705 spin_unlock_irqrestore(&phba->hbalock, iflags);
14706 /* Handle MDS Loopback frames */
Dick Kennedy24411fc2020-08-03 14:02:25 -070014707 if (!(phba->pport->load_flag & FC_UNLOADING))
14708 lpfc_sli4_handle_mds_loopback(phba->pport,
14709 dma_buf);
14710 else
14711 lpfc_in_buf_free(phba, &dma_buf->dbuf);
James Smarte817e5d2018-12-13 15:17:53 -080014712 break;
14713 }
14714
14715 /* save off the frame for the work thread to process */
James Smart4d9ab992009-10-02 15:16:39 -040014716 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040014717 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040014718 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040014719 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040014720 spin_unlock_irqrestore(&phba->hbalock, iflags);
14721 workposted = true;
14722 break;
James Smart4d9ab992009-10-02 15:16:39 -040014723 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070014724 if (phba->nvmet_support) {
14725 tgtp = phba->targetport->private;
Dick Kennedy372c1872020-06-30 14:50:00 -070014726 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart547077a2017-05-15 15:20:40 -070014727 "6402 RQE Error x%x, posted %d err_cnt "
14728 "%d: %x %x %x\n",
14729 status, hrq->RQ_buf_posted,
14730 hrq->RQ_no_posted_buf,
14731 atomic_read(&tgtp->rcv_fcp_cmd_in),
14732 atomic_read(&tgtp->rcv_fcp_cmd_out),
14733 atomic_read(&tgtp->xmt_fcp_release));
14734 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050014735 fallthrough;
James Smart547077a2017-05-15 15:20:40 -070014736
14737 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040014738 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040014739 /* Post more buffers if possible */
14740 spin_lock_irqsave(&phba->hbalock, iflags);
14741 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
14742 spin_unlock_irqrestore(&phba->hbalock, iflags);
14743 workposted = true;
14744 break;
14745 }
14746out:
14747 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040014748}
14749
14750/**
14751 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
14752 * @phba: Pointer to HBA context object.
14753 * @cq: Pointer to the completion queue.
James Smart32517fc2019-01-28 11:14:33 -080014754 * @cqe: Pointer to a completion queue entry.
James Smart4d9ab992009-10-02 15:16:39 -040014755 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030014756 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040014757 * entry.
14758 *
14759 * Return: true if work posted to worker thread, otherwise false.
14760 **/
14761static bool
14762lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040014763 struct lpfc_cqe *cqe)
14764{
James Smart45ed1192009-10-02 15:17:02 -040014765 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040014766 bool workposted = false;
14767
14768 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070014769 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040014770
14771 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040014772 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040014773 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040014774 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040014775 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040014776 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040014777 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040014778 break;
14779 case CQE_CODE_RELEASE_WQE:
14780 /* Process the WQ release event */
14781 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040014782 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040014783 break;
14784 case CQE_CODE_XRI_ABORTED:
14785 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040014786 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014787 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040014788 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040014789 break;
James Smart4d9ab992009-10-02 15:16:39 -040014790 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040014791 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040014792 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040014793 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040014794 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040014795 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040014796 break;
James Smart4f774512009-05-22 14:52:35 -040014797 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070014798 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040014799 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040014800 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040014801 break;
14802 }
14803 return workposted;
14804}
14805
14806/**
James Smart4f774512009-05-22 14:52:35 -040014807 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
14808 * @phba: Pointer to HBA context object.
14809 * @eqe: Pointer to fast-path event queue entry.
Lee Jones7af29d42020-07-21 17:41:31 +010014810 * @speq: Pointer to slow-path event queue.
James Smart4f774512009-05-22 14:52:35 -040014811 *
14812 * This routine process a event queue entry from the slow-path event queue.
14813 * It will check the MajorCode and MinorCode to determine this is for a
14814 * completion event on a completion queue, if not, an error shall be logged
14815 * and just return. Otherwise, it will get to the corresponding completion
14816 * queue and process all the entries on that completion queue, rearm the
14817 * completion queue, and then return.
14818 *
14819 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070014820static void
James Smart67d12732012-08-03 12:36:13 -040014821lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14822 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040014823{
James Smart67d12732012-08-03 12:36:13 -040014824 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040014825 uint16_t cqid;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014826 int ret = 0;
James Smart4f774512009-05-22 14:52:35 -040014827
James Smart4f774512009-05-22 14:52:35 -040014828 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040014829 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040014830
James Smart4f774512009-05-22 14:52:35 -040014831 list_for_each_entry(childq, &speq->child_list, list) {
14832 if (childq->queue_id == cqid) {
14833 cq = childq;
14834 break;
14835 }
14836 }
14837 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040014838 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
Dick Kennedy372c1872020-06-30 14:50:00 -070014839 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040014840 "0365 Slow-path CQ identifier "
14841 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070014842 return;
James Smart4f774512009-05-22 14:52:35 -040014843 }
14844
James Smart895427b2017-02-12 13:52:30 -080014845 /* Save EQ associated with this CQ */
14846 cq->assoc_qp = speq;
14847
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014848 if (is_kdump_kernel())
14849 ret = queue_work(phba->wq, &cq->spwork);
14850 else
14851 ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
14852
14853 if (!ret)
Dick Kennedy372c1872020-06-30 14:50:00 -070014854 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014855 "0390 Cannot schedule queue work "
Dick Kennedyf485c182017-09-29 17:34:34 -070014856 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
Bart Van Assched6d189c2019-03-28 11:06:22 -070014857 cqid, cq->queue_id, raw_smp_processor_id());
Dick Kennedyf485c182017-09-29 17:34:34 -070014858}
14859
14860/**
James Smart32517fc2019-01-28 11:14:33 -080014861 * __lpfc_sli4_process_cq - Process elements of a CQ
Dick Kennedyf485c182017-09-29 17:34:34 -070014862 * @phba: Pointer to HBA context object.
James Smart32517fc2019-01-28 11:14:33 -080014863 * @cq: Pointer to CQ to be processed
14864 * @handler: Routine to process each cqe
14865 * @delay: Pointer to usdelay to set in case of rescheduling of the handler
Dick Kennedy317aeb82020-06-30 14:49:59 -070014866 * @poll_mode: Polling mode we were called from
Dick Kennedyf485c182017-09-29 17:34:34 -070014867 *
James Smart32517fc2019-01-28 11:14:33 -080014868 * This routine processes completion queue entries in a CQ. While a valid
14869 * queue element is found, the handler is called. During processing checks
14870 * are made for periodic doorbell writes to let the hardware know of
14871 * element consumption.
Dick Kennedyf485c182017-09-29 17:34:34 -070014872 *
James Smart32517fc2019-01-28 11:14:33 -080014873 * If the max limit on cqes to process is hit, or there are no more valid
14874 * entries, the loop stops. If we processed a sufficient number of elements,
14875 * meaning there is sufficient load, rather than rearming and generating
14876 * another interrupt, a cq rescheduling delay will be set. A delay of 0
14877 * indicates no rescheduling.
14878 *
14879 * Returns True if work scheduled, False otherwise.
Dick Kennedyf485c182017-09-29 17:34:34 -070014880 **/
James Smart32517fc2019-01-28 11:14:33 -080014881static bool
14882__lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
14883 bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014884 struct lpfc_cqe *), unsigned long *delay,
14885 enum lpfc_poll_mode poll_mode)
Dick Kennedyf485c182017-09-29 17:34:34 -070014886{
Dick Kennedyf485c182017-09-29 17:34:34 -070014887 struct lpfc_cqe *cqe;
14888 bool workposted = false;
James Smart32517fc2019-01-28 11:14:33 -080014889 int count = 0, consumed = 0;
14890 bool arm = true;
14891
14892 /* default - no reschedule */
14893 *delay = 0;
14894
14895 if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
14896 goto rearm_and_exit;
Dick Kennedyf485c182017-09-29 17:34:34 -070014897
James Smart4f774512009-05-22 14:52:35 -040014898 /* Process all the entries to the CQ */
James Smartd74a89a2019-05-21 17:48:55 -070014899 cq->q_flag = 0;
James Smart32517fc2019-01-28 11:14:33 -080014900 cqe = lpfc_sli4_cq_get(cq);
14901 while (cqe) {
James Smart32517fc2019-01-28 11:14:33 -080014902 workposted |= handler(phba, cq, cqe);
14903 __lpfc_sli4_consume_cqe(phba, cq, cqe);
14904
14905 consumed++;
14906 if (!(++count % cq->max_proc_limit))
14907 break;
14908
14909 if (!(count % cq->notify_interval)) {
14910 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14911 LPFC_QUEUE_NOARM);
14912 consumed = 0;
James Smart8156d372019-10-18 14:18:26 -070014913 cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
James Smart4f774512009-05-22 14:52:35 -040014914 }
James Smartb84daac2012-08-03 12:35:13 -040014915
James Smartd74a89a2019-05-21 17:48:55 -070014916 if (count == LPFC_NVMET_CQ_NOTIFY)
14917 cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
14918
James Smart32517fc2019-01-28 11:14:33 -080014919 cqe = lpfc_sli4_cq_get(cq);
14920 }
14921 if (count >= phba->cfg_cq_poll_threshold) {
14922 *delay = 1;
14923 arm = false;
14924 }
14925
Dick Kennedy317aeb82020-06-30 14:49:59 -070014926 /* Note: complete the irq_poll softirq before rearming CQ */
14927 if (poll_mode == LPFC_IRQ_POLL)
14928 irq_poll_complete(&cq->iop);
14929
James Smart32517fc2019-01-28 11:14:33 -080014930 /* Track the max number of CQEs processed in 1 EQ */
14931 if (count > cq->CQ_max_cqe)
14932 cq->CQ_max_cqe = count;
14933
14934 cq->assoc_qp->EQ_cqe_cnt += count;
14935
14936 /* Catch the no cq entry condition */
14937 if (unlikely(count == 0))
14938 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14939 "0369 No entry from completion queue "
14940 "qid=%d\n", cq->queue_id);
14941
Dick Kennedy164ba8d2020-05-01 14:43:03 -070014942 xchg(&cq->queue_claimed, 0);
James Smart32517fc2019-01-28 11:14:33 -080014943
14944rearm_and_exit:
14945 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14946 arm ? LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
14947
14948 return workposted;
14949}
14950
14951/**
Lee Jones8514e2f2021-03-03 14:46:18 +000014952 * __lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
James Smart32517fc2019-01-28 11:14:33 -080014953 * @cq: pointer to CQ to process
14954 *
14955 * This routine calls the cq processing routine with a handler specific
14956 * to the type of queue bound to it.
14957 *
14958 * The CQ routine returns two values: the first is the calling status,
14959 * which indicates whether work was queued to the background discovery
14960 * thread. If true, the routine should wakeup the discovery thread;
14961 * the second is the delay parameter. If non-zero, rather than rearming
14962 * the CQ and yet another interrupt, the CQ handler should be queued so
14963 * that it is processed in a subsequent polling action. The value of
14964 * the delay indicates when to reschedule it.
14965 **/
14966static void
14967__lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
14968{
14969 struct lpfc_hba *phba = cq->phba;
14970 unsigned long delay;
14971 bool workposted = false;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014972 int ret = 0;
James Smart32517fc2019-01-28 11:14:33 -080014973
14974 /* Process and rearm the CQ */
14975 switch (cq->type) {
14976 case LPFC_MCQ:
14977 workposted |= __lpfc_sli4_process_cq(phba, cq,
14978 lpfc_sli4_sp_handle_mcqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014979 &delay, LPFC_QUEUE_WORK);
James Smart32517fc2019-01-28 11:14:33 -080014980 break;
14981 case LPFC_WCQ:
James Smartc00f62e2019-08-14 16:57:11 -070014982 if (cq->subtype == LPFC_IO)
James Smart32517fc2019-01-28 11:14:33 -080014983 workposted |= __lpfc_sli4_process_cq(phba, cq,
14984 lpfc_sli4_fp_handle_cqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014985 &delay, LPFC_QUEUE_WORK);
James Smart32517fc2019-01-28 11:14:33 -080014986 else
14987 workposted |= __lpfc_sli4_process_cq(phba, cq,
14988 lpfc_sli4_sp_handle_cqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014989 &delay, LPFC_QUEUE_WORK);
James Smart4f774512009-05-22 14:52:35 -040014990 break;
14991 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070014992 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040014993 "0370 Invalid completion queue type (%d)\n",
14994 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070014995 return;
James Smart4f774512009-05-22 14:52:35 -040014996 }
14997
James Smart32517fc2019-01-28 11:14:33 -080014998 if (delay) {
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014999 if (is_kdump_kernel())
15000 ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
15001 delay);
15002 else
15003 ret = queue_delayed_work_on(cq->chann, phba->wq,
15004 &cq->sched_spwork, delay);
15005 if (!ret)
Dick Kennedy372c1872020-06-30 14:50:00 -070015006 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -070015007 "0394 Cannot schedule queue work "
James Smart32517fc2019-01-28 11:14:33 -080015008 "for cqid=%d on CPU %d\n",
15009 cq->queue_id, cq->chann);
15010 }
James Smart4f774512009-05-22 14:52:35 -040015011
15012 /* wake up worker thread if there are works to be done */
15013 if (workposted)
15014 lpfc_worker_wake_up(phba);
15015}
15016
15017/**
James Smart32517fc2019-01-28 11:14:33 -080015018 * lpfc_sli4_sp_process_cq - slow-path work handler when started by
15019 * interrupt
15020 * @work: pointer to work element
15021 *
15022 * translates from the work handler and calls the slow-path handler.
15023 **/
15024static void
15025lpfc_sli4_sp_process_cq(struct work_struct *work)
15026{
15027 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
15028
15029 __lpfc_sli4_sp_process_cq(cq);
15030}
15031
15032/**
15033 * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
15034 * @work: pointer to work element
15035 *
15036 * translates from the work handler and calls the slow-path handler.
15037 **/
15038static void
15039lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
15040{
15041 struct lpfc_queue *cq = container_of(to_delayed_work(work),
15042 struct lpfc_queue, sched_spwork);
15043
15044 __lpfc_sli4_sp_process_cq(cq);
15045}
15046
15047/**
James Smart4f774512009-05-22 14:52:35 -040015048 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040015049 * @phba: Pointer to HBA context object.
15050 * @cq: Pointer to associated CQ
15051 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040015052 *
15053 * This routine process a fast-path work queue completion entry from fast-path
15054 * event queue for FCP command response completion.
15055 **/
15056static void
James Smart2a76a282012-08-03 12:35:54 -040015057lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040015058 struct lpfc_wcqe_complete *wcqe)
15059{
James Smart2a76a282012-08-03 12:35:54 -040015060 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040015061 struct lpfc_iocbq *cmdiocbq;
15062 struct lpfc_iocbq irspiocbq;
15063 unsigned long iflags;
15064
James Smart4f774512009-05-22 14:52:35 -040015065 /* Check for response status */
15066 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
15067 /* If resource errors reported from HBA, reduce queue
15068 * depth of the SCSI device.
15069 */
James Smarte3d2b802012-08-14 14:25:43 -040015070 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
15071 IOSTAT_LOCAL_REJECT)) &&
15072 ((wcqe->parameter & IOERR_PARAM_MASK) ==
15073 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040015074 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040015075
Dick Kennedy28ed7372020-06-30 14:49:56 -070015076 /* Log the cmpl status */
James Smart11f0e342018-05-04 20:37:57 -070015077 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
Dick Kennedy28ed7372020-06-30 14:49:56 -070015078 "0373 FCP CQE cmpl: status=x%x: "
James Smart11f0e342018-05-04 20:37:57 -070015079 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040015080 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070015081 wcqe->word0, wcqe->total_data_placed,
15082 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040015083 }
15084
15085 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040015086 spin_lock_irqsave(&pring->ring_lock, iflags);
15087 pring->stats.iocb_event++;
James Smarte2a8be52019-05-06 17:26:47 -070015088 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040015089 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
15090 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart4f774512009-05-22 14:52:35 -040015091 if (unlikely(!cmdiocbq)) {
15092 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15093 "0374 FCP complete with no corresponding "
15094 "cmdiocb: iotag (%d)\n",
15095 bf_get(lpfc_wcqe_c_request_tag, wcqe));
15096 return;
15097 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070015098#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
15099 cmdiocbq->isr_timestamp = cq->isr_timestamp;
15100#endif
James Smart895427b2017-02-12 13:52:30 -080015101 if (cmdiocbq->iocb_cmpl == NULL) {
15102 if (cmdiocbq->wqe_cmpl) {
James Smart96e209b2020-11-15 11:26:43 -080015103 /* For FCP the flag is cleared in wqe_cmpl */
15104 if (!(cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
15105 cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
James Smart895427b2017-02-12 13:52:30 -080015106 spin_lock_irqsave(&phba->hbalock, iflags);
15107 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
15108 spin_unlock_irqrestore(&phba->hbalock, iflags);
15109 }
15110
15111 /* Pass the cmd_iocb and the wcqe to the upper layer */
15112 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
15113 return;
15114 }
James Smart4f774512009-05-22 14:52:35 -040015115 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15116 "0375 FCP cmdiocb not callback function "
15117 "iotag: (%d)\n",
15118 bf_get(lpfc_wcqe_c_request_tag, wcqe));
15119 return;
15120 }
15121
James Smart96e209b2020-11-15 11:26:43 -080015122 /* Only SLI4 non-IO commands stil use IOCB */
James Smart4f774512009-05-22 14:52:35 -040015123 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050015124 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040015125
James Smart0f65ff62010-02-26 14:14:23 -050015126 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
15127 spin_lock_irqsave(&phba->hbalock, iflags);
15128 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
15129 spin_unlock_irqrestore(&phba->hbalock, iflags);
15130 }
15131
James Smart4f774512009-05-22 14:52:35 -040015132 /* Pass the cmd_iocb and the rsp state to the upper layer */
15133 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
15134}
15135
15136/**
15137 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
15138 * @phba: Pointer to HBA context object.
15139 * @cq: Pointer to completion queue.
15140 * @wcqe: Pointer to work-queue completion queue entry.
15141 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080015142 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040015143 * proper WQ release routine to the slow-path WQ.
15144 **/
15145static void
15146lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15147 struct lpfc_wcqe_release *wcqe)
15148{
15149 struct lpfc_queue *childwq;
15150 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080015151 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040015152
15153 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080015154 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040015155 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080015156 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040015157 lpfc_sli4_wq_release(childwq,
15158 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080015159 if (childwq->q_flag & HBA_NVMET_WQFULL)
15160 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040015161 wqid_matched = true;
15162 break;
15163 }
15164 }
15165 /* Report warning log message if no match found */
15166 if (wqid_matched != true)
15167 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15168 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080015169 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040015170}
15171
15172/**
James Smart2d7dbc42017-02-12 13:52:35 -080015173 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
15174 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010015175 * @cq: Pointer to completion queue.
James Smart2d7dbc42017-02-12 13:52:35 -080015176 * @rcqe: Pointer to receive-queue completion queue entry.
15177 *
15178 * This routine process a receive-queue completion queue entry.
15179 *
15180 * Return: true if work posted to worker thread, otherwise false.
15181 **/
15182static bool
15183lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
15184 struct lpfc_rcqe *rcqe)
15185{
15186 bool workposted = false;
15187 struct lpfc_queue *hrq;
15188 struct lpfc_queue *drq;
15189 struct rqb_dmabuf *dma_buf;
15190 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070015191 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080015192 uint32_t status, rq_id;
15193 unsigned long iflags;
15194 uint32_t fctl, idx;
15195
15196 if ((phba->nvmet_support == 0) ||
15197 (phba->sli4_hba.nvmet_cqset == NULL))
15198 return workposted;
15199
15200 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
15201 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
15202 drq = phba->sli4_hba.nvmet_mrq_data[idx];
15203
15204 /* sanity check on queue memory */
15205 if (unlikely(!hrq) || unlikely(!drq))
15206 return workposted;
15207
15208 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
15209 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
15210 else
15211 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
15212
15213 if ((phba->nvmet_support == 0) ||
15214 (rq_id != hrq->queue_id))
15215 return workposted;
15216
15217 status = bf_get(lpfc_rcqe_status, rcqe);
15218 switch (status) {
15219 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
Dick Kennedy372c1872020-06-30 14:50:00 -070015220 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080015221 "6126 Receive Frame Truncated!!\n");
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050015222 fallthrough;
James Smart2d7dbc42017-02-12 13:52:35 -080015223 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080015224 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080015225 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080015226 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
15227 if (!dma_buf) {
15228 hrq->RQ_no_buf_found++;
15229 spin_unlock_irqrestore(&phba->hbalock, iflags);
15230 goto out;
15231 }
15232 spin_unlock_irqrestore(&phba->hbalock, iflags);
15233 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070015234 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080015235 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
15236
15237 /* Just some basic sanity checks on FCP Command frame */
15238 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
James Smart3a8070c2020-03-31 09:50:05 -070015239 fc_hdr->fh_f_ctl[1] << 8 |
15240 fc_hdr->fh_f_ctl[2]);
James Smart2d7dbc42017-02-12 13:52:35 -080015241 if (((fctl &
15242 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
15243 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
15244 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
15245 goto drop;
15246
15247 if (fc_hdr->fh_type == FC_TYPE_FCP) {
James Smartd74a89a2019-05-21 17:48:55 -070015248 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080015249 lpfc_nvmet_unsol_fcp_event(
James Smartd74a89a2019-05-21 17:48:55 -070015250 phba, idx, dma_buf, cq->isr_timestamp,
15251 cq->q_flag & HBA_NVMET_CQ_NOTIFY);
James Smart2d7dbc42017-02-12 13:52:35 -080015252 return false;
15253 }
15254drop:
James Smart22b738a2019-03-12 16:30:11 -070015255 lpfc_rq_buf_free(phba, &dma_buf->hbuf);
James Smart2d7dbc42017-02-12 13:52:35 -080015256 break;
James Smart2d7dbc42017-02-12 13:52:35 -080015257 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070015258 if (phba->nvmet_support) {
15259 tgtp = phba->targetport->private;
Dick Kennedy372c1872020-06-30 14:50:00 -070015260 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart547077a2017-05-15 15:20:40 -070015261 "6401 RQE Error x%x, posted %d err_cnt "
15262 "%d: %x %x %x\n",
15263 status, hrq->RQ_buf_posted,
15264 hrq->RQ_no_posted_buf,
15265 atomic_read(&tgtp->rcv_fcp_cmd_in),
15266 atomic_read(&tgtp->rcv_fcp_cmd_out),
15267 atomic_read(&tgtp->xmt_fcp_release));
15268 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050015269 fallthrough;
James Smart547077a2017-05-15 15:20:40 -070015270
15271 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080015272 hrq->RQ_no_posted_buf++;
15273 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080015274 break;
15275 }
15276out:
15277 return workposted;
15278}
15279
15280/**
James Smart895427b2017-02-12 13:52:30 -080015281 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart32517fc2019-01-28 11:14:33 -080015282 * @phba: adapter with cq
James Smart4f774512009-05-22 14:52:35 -040015283 * @cq: Pointer to the completion queue.
Lee Jones7af29d42020-07-21 17:41:31 +010015284 * @cqe: Pointer to fast-path completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040015285 *
15286 * This routine process a fast-path work queue completion entry from fast-path
15287 * event queue for FCP command response completion.
James Smart32517fc2019-01-28 11:14:33 -080015288 *
15289 * Return: true if work posted to worker thread, otherwise false.
James Smart4f774512009-05-22 14:52:35 -040015290 **/
James Smart32517fc2019-01-28 11:14:33 -080015291static bool
James Smart895427b2017-02-12 13:52:30 -080015292lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040015293 struct lpfc_cqe *cqe)
15294{
15295 struct lpfc_wcqe_release wcqe;
15296 bool workposted = false;
15297
15298 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070015299 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040015300
15301 /* Check and process for different type of WCQE and dispatch */
15302 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
15303 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080015304 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040015305 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040015306 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040015307 phba->last_completion_time = jiffies;
James Smartc00f62e2019-08-14 16:57:11 -070015308 if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
James Smart895427b2017-02-12 13:52:30 -080015309 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040015310 (struct lpfc_wcqe_complete *)&wcqe);
15311 break;
15312 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040015313 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040015314 /* Process the WQ release event */
15315 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
15316 (struct lpfc_wcqe_release *)&wcqe);
15317 break;
15318 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040015319 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040015320 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040015321 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040015322 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
15323 (struct sli4_wcqe_xri_aborted *)&wcqe);
15324 break;
James Smart895427b2017-02-12 13:52:30 -080015325 case CQE_CODE_RECEIVE_V1:
15326 case CQE_CODE_RECEIVE:
15327 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080015328 if (cq->subtype == LPFC_NVMET) {
15329 workposted = lpfc_sli4_nvmet_handle_rcqe(
15330 phba, cq, (struct lpfc_rcqe *)&wcqe);
15331 }
James Smart895427b2017-02-12 13:52:30 -080015332 break;
James Smart4f774512009-05-22 14:52:35 -040015333 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070015334 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -080015335 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040015336 bf_get(lpfc_wcqe_c_code, &wcqe));
15337 break;
15338 }
15339 return workposted;
15340}
15341
15342/**
Dick Kennedy317aeb82020-06-30 14:49:59 -070015343 * lpfc_sli4_sched_cq_work - Schedules cq work
15344 * @phba: Pointer to HBA context object.
15345 * @cq: Pointer to CQ
15346 * @cqid: CQ ID
15347 *
15348 * This routine checks the poll mode of the CQ corresponding to
15349 * cq->chann, then either schedules a softirq or queue_work to complete
15350 * cq work.
15351 *
15352 * queue_work path is taken if in NVMET mode, or if poll_mode is in
15353 * LPFC_QUEUE_WORK mode. Otherwise, softirq path is taken.
15354 *
15355 **/
15356static void lpfc_sli4_sched_cq_work(struct lpfc_hba *phba,
15357 struct lpfc_queue *cq, uint16_t cqid)
15358{
15359 int ret = 0;
15360
15361 switch (cq->poll_mode) {
15362 case LPFC_IRQ_POLL:
James Smart9064aeb2021-08-16 09:28:50 -070015363 /* CGN mgmt is mutually exclusive from softirq processing */
15364 if (phba->cmf_active_mode == LPFC_CFG_OFF) {
15365 irq_poll_sched(&cq->iop);
15366 break;
15367 }
15368 fallthrough;
Dick Kennedy317aeb82020-06-30 14:49:59 -070015369 case LPFC_QUEUE_WORK:
15370 default:
15371 if (is_kdump_kernel())
15372 ret = queue_work(phba->wq, &cq->irqwork);
15373 else
15374 ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
15375 if (!ret)
Dick Kennedy372c1872020-06-30 14:50:00 -070015376 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -070015377 "0383 Cannot schedule queue work "
15378 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
15379 cqid, cq->queue_id,
15380 raw_smp_processor_id());
15381 }
15382}
15383
15384/**
James Smart67d12732012-08-03 12:36:13 -040015385 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040015386 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010015387 * @eq: Pointer to the queue structure.
James Smart4f774512009-05-22 14:52:35 -040015388 * @eqe: Pointer to fast-path event queue entry.
15389 *
15390 * This routine process a event queue entry from the fast-path event queue.
15391 * It will check the MajorCode and MinorCode to determine this is for a
15392 * completion event on a completion queue, if not, an error shall be logged
15393 * and just return. Otherwise, it will get to the corresponding completion
15394 * queue and process all the entries on the completion queue, rearm the
15395 * completion queue, and then return.
15396 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070015397static void
James Smart32517fc2019-01-28 11:14:33 -080015398lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
15399 struct lpfc_eqe *eqe)
James Smart4f774512009-05-22 14:52:35 -040015400{
James Smart895427b2017-02-12 13:52:30 -080015401 struct lpfc_queue *cq = NULL;
James Smart32517fc2019-01-28 11:14:33 -080015402 uint32_t qidx = eq->hdwq;
James Smart2d7dbc42017-02-12 13:52:35 -080015403 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040015404
James Smartcb5172e2010-03-15 11:25:07 -040015405 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070015406 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart67d12732012-08-03 12:36:13 -040015407 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040015408 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040015409 bf_get_le32(lpfc_eqe_major_code, eqe),
15410 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070015411 return;
James Smart4f774512009-05-22 14:52:35 -040015412 }
15413
James Smart67d12732012-08-03 12:36:13 -040015414 /* Get the reference to the corresponding CQ */
15415 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
15416
James Smart6a828b02019-01-28 11:14:31 -080015417 /* Use the fast lookup method first */
15418 if (cqid <= phba->sli4_hba.cq_max) {
15419 cq = phba->sli4_hba.cq_lookup[cqid];
15420 if (cq)
15421 goto work_cq;
James Smartcdb42be2019-01-28 11:14:21 -080015422 }
15423
15424 /* Next check for NVMET completion */
James Smart2d7dbc42017-02-12 13:52:35 -080015425 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
15426 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
15427 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
15428 /* Process NVMET unsol rcv */
15429 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
15430 goto process_cq;
15431 }
15432 }
15433
James Smart895427b2017-02-12 13:52:30 -080015434 if (phba->sli4_hba.nvmels_cq &&
15435 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
15436 /* Process NVME unsol rcv */
15437 cq = phba->sli4_hba.nvmels_cq;
15438 }
15439
15440 /* Otherwise this is a Slow path event */
15441 if (cq == NULL) {
James Smartcdb42be2019-01-28 11:14:21 -080015442 lpfc_sli4_sp_handle_eqe(phba, eqe,
15443 phba->sli4_hba.hdwq[qidx].hba_eq);
Dick Kennedyf485c182017-09-29 17:34:34 -070015444 return;
James Smart67d12732012-08-03 12:36:13 -040015445 }
15446
James Smart895427b2017-02-12 13:52:30 -080015447process_cq:
James Smart4f774512009-05-22 14:52:35 -040015448 if (unlikely(cqid != cq->queue_id)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070015449 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040015450 "0368 Miss-matched fast-path completion "
15451 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
15452 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070015453 return;
James Smart4f774512009-05-22 14:52:35 -040015454 }
15455
James Smart6a828b02019-01-28 11:14:31 -080015456work_cq:
James Smartd74a89a2019-05-21 17:48:55 -070015457#if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
15458 if (phba->ktime_on)
15459 cq->isr_timestamp = ktime_get_ns();
15460 else
15461 cq->isr_timestamp = 0;
15462#endif
Dick Kennedy317aeb82020-06-30 14:49:59 -070015463 lpfc_sli4_sched_cq_work(phba, cq, cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070015464}
15465
15466/**
James Smart32517fc2019-01-28 11:14:33 -080015467 * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
15468 * @cq: Pointer to CQ to be processed
Dick Kennedy317aeb82020-06-30 14:49:59 -070015469 * @poll_mode: Enum lpfc_poll_state to determine poll mode
Dick Kennedyf485c182017-09-29 17:34:34 -070015470 *
James Smart32517fc2019-01-28 11:14:33 -080015471 * This routine calls the cq processing routine with the handler for
15472 * fast path CQEs.
15473 *
15474 * The CQ routine returns two values: the first is the calling status,
15475 * which indicates whether work was queued to the background discovery
15476 * thread. If true, the routine should wakeup the discovery thread;
15477 * the second is the delay parameter. If non-zero, rather than rearming
15478 * the CQ and yet another interrupt, the CQ handler should be queued so
15479 * that it is processed in a subsequent polling action. The value of
15480 * the delay indicates when to reschedule it.
Dick Kennedyf485c182017-09-29 17:34:34 -070015481 **/
15482static void
Dick Kennedy317aeb82020-06-30 14:49:59 -070015483__lpfc_sli4_hba_process_cq(struct lpfc_queue *cq,
15484 enum lpfc_poll_mode poll_mode)
Dick Kennedyf485c182017-09-29 17:34:34 -070015485{
Dick Kennedyf485c182017-09-29 17:34:34 -070015486 struct lpfc_hba *phba = cq->phba;
James Smart32517fc2019-01-28 11:14:33 -080015487 unsigned long delay;
Dick Kennedyf485c182017-09-29 17:34:34 -070015488 bool workposted = false;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070015489 int ret = 0;
Dick Kennedyf485c182017-09-29 17:34:34 -070015490
James Smart32517fc2019-01-28 11:14:33 -080015491 /* process and rearm the CQ */
15492 workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070015493 &delay, poll_mode);
James Smart32517fc2019-01-28 11:14:33 -080015494
15495 if (delay) {
Dick Kennedy86ee57a2020-06-30 14:49:55 -070015496 if (is_kdump_kernel())
15497 ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
15498 delay);
15499 else
15500 ret = queue_delayed_work_on(cq->chann, phba->wq,
15501 &cq->sched_irqwork, delay);
15502 if (!ret)
Dick Kennedy372c1872020-06-30 14:50:00 -070015503 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -070015504 "0367 Cannot schedule queue work "
15505 "for cqid=%d on CPU %d\n",
15506 cq->queue_id, cq->chann);
James Smart4f774512009-05-22 14:52:35 -040015507 }
15508
James Smart4f774512009-05-22 14:52:35 -040015509 /* wake up worker thread if there are works to be done */
15510 if (workposted)
15511 lpfc_worker_wake_up(phba);
15512}
15513
James Smart1ba981f2014-02-20 09:56:45 -050015514/**
James Smart32517fc2019-01-28 11:14:33 -080015515 * lpfc_sli4_hba_process_cq - fast-path work handler when started by
15516 * interrupt
15517 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050015518 *
James Smart32517fc2019-01-28 11:14:33 -080015519 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050015520 **/
15521static void
James Smart32517fc2019-01-28 11:14:33 -080015522lpfc_sli4_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050015523{
James Smart32517fc2019-01-28 11:14:33 -080015524 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050015525
Dick Kennedy317aeb82020-06-30 14:49:59 -070015526 __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
James Smart1ba981f2014-02-20 09:56:45 -050015527}
15528
15529/**
Lee Jones8514e2f2021-03-03 14:46:18 +000015530 * lpfc_sli4_dly_hba_process_cq - fast-path work handler when started by timer
James Smart32517fc2019-01-28 11:14:33 -080015531 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050015532 *
James Smart32517fc2019-01-28 11:14:33 -080015533 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050015534 **/
James Smart32517fc2019-01-28 11:14:33 -080015535static void
15536lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050015537{
James Smart32517fc2019-01-28 11:14:33 -080015538 struct lpfc_queue *cq = container_of(to_delayed_work(work),
15539 struct lpfc_queue, sched_irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050015540
Dick Kennedy317aeb82020-06-30 14:49:59 -070015541 __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
James Smart1ba981f2014-02-20 09:56:45 -050015542}
15543
James Smart4f774512009-05-22 14:52:35 -040015544/**
James Smart67d12732012-08-03 12:36:13 -040015545 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040015546 * @irq: Interrupt number.
15547 * @dev_id: The device context pointer.
15548 *
15549 * This function is directly called from the PCI layer as an interrupt
15550 * service routine when device with SLI-4 interface spec is enabled with
15551 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
15552 * ring event in the HBA. However, when the device is enabled with either
15553 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
15554 * device-level interrupt handler. When the PCI slot is in error recovery
15555 * or the HBA is undergoing initialization, the interrupt handler will not
15556 * process the interrupt. The SCSI FCP fast-path ring event are handled in
15557 * the intrrupt context. This function is called without any lock held.
15558 * It gets the hbalock to access and update SLI data structures. Note that,
15559 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
15560 * equal to that of FCP CQ index.
15561 *
James Smart67d12732012-08-03 12:36:13 -040015562 * The link attention and ELS ring attention events are handled
15563 * by the worker thread. The interrupt handler signals the worker thread
15564 * and returns for these events. This function is called without any lock
15565 * held. It gets the hbalock to access and update SLI data structures.
15566 *
James Smart4f774512009-05-22 14:52:35 -040015567 * This function returns IRQ_HANDLED when interrupt is handled else it
15568 * returns IRQ_NONE.
15569 **/
15570irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040015571lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040015572{
15573 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080015574 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040015575 struct lpfc_queue *fpeq;
James Smart4f774512009-05-22 14:52:35 -040015576 unsigned long iflag;
15577 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080015578 int hba_eqidx;
James Smart32517fc2019-01-28 11:14:33 -080015579 struct lpfc_eq_intr_info *eqi;
James Smart4f774512009-05-22 14:52:35 -040015580
15581 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080015582 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
15583 phba = hba_eq_hdl->phba;
15584 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040015585
15586 if (unlikely(!phba))
15587 return IRQ_NONE;
James Smartcdb42be2019-01-28 11:14:21 -080015588 if (unlikely(!phba->sli4_hba.hdwq))
James Smart5350d872011-10-10 21:33:49 -040015589 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040015590
15591 /* Get to the EQ struct associated with this vector */
James Smart657add42019-05-21 17:49:06 -070015592 fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
James Smart2e90f4b2011-12-13 13:22:37 -050015593 if (unlikely(!fpeq))
15594 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040015595
15596 /* Check device state for handling interrupt */
15597 if (unlikely(lpfc_intr_state_check(phba))) {
15598 /* Check again for link_state with lock held */
15599 spin_lock_irqsave(&phba->hbalock, iflag);
15600 if (phba->link_state < LPFC_LINK_DOWN)
15601 /* Flush, clear interrupt, and rearm the EQ */
James Smart24c7c0a2019-09-21 20:58:58 -070015602 lpfc_sli4_eqcq_flush(phba, fpeq);
James Smart4f774512009-05-22 14:52:35 -040015603 spin_unlock_irqrestore(&phba->hbalock, iflag);
15604 return IRQ_NONE;
15605 }
15606
Dick Kennedya7fc0712020-05-01 14:43:08 -070015607 eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
15608 eqi->icnt++;
15609
Bart Van Assched6d189c2019-03-28 11:06:22 -070015610 fpeq->last_cpu = raw_smp_processor_id();
James Smart4f774512009-05-22 14:52:35 -040015611
Dick Kennedya7fc0712020-05-01 14:43:08 -070015612 if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
James Smart8156d372019-10-18 14:18:26 -070015613 fpeq->q_flag & HBA_EQ_DELAY_CHK &&
James Smart32517fc2019-01-28 11:14:33 -080015614 phba->cfg_auto_imax &&
15615 fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
15616 phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
15617 lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
James Smartb84daac2012-08-03 12:35:13 -040015618
James Smart32517fc2019-01-28 11:14:33 -080015619 /* process and rearm the EQ */
James Smart93a4d6f2019-11-04 16:57:05 -080015620 ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040015621
15622 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040015623 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040015624 if (phba->intr_type == MSIX)
15625 /* MSI-X treated interrupt served as no EQ share INT */
15626 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15627 "0358 MSI-X interrupt with no EQE\n");
15628 else
15629 /* Non MSI-X treated on interrupt as EQ share INT */
15630 return IRQ_NONE;
15631 }
15632
15633 return IRQ_HANDLED;
James Smart3bfab8a2021-04-11 18:31:23 -070015634} /* lpfc_sli4_hba_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040015635
15636/**
15637 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
15638 * @irq: Interrupt number.
15639 * @dev_id: The device context pointer.
15640 *
15641 * This function is the device-level interrupt handler to device with SLI-4
15642 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
15643 * interrupt mode is enabled and there is an event in the HBA which requires
15644 * driver attention. This function invokes the slow-path interrupt attention
15645 * handling function and fast-path interrupt attention handling function in
15646 * turn to process the relevant HBA attention events. This function is called
15647 * without any lock held. It gets the hbalock to access and update SLI data
15648 * structures.
15649 *
15650 * This function returns IRQ_HANDLED when interrupt is handled, else it
15651 * returns IRQ_NONE.
15652 **/
15653irqreturn_t
15654lpfc_sli4_intr_handler(int irq, void *dev_id)
15655{
15656 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040015657 irqreturn_t hba_irq_rc;
15658 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080015659 int qidx;
James Smart4f774512009-05-22 14:52:35 -040015660
15661 /* Get the driver's phba structure from the dev_id */
15662 phba = (struct lpfc_hba *)dev_id;
15663
15664 if (unlikely(!phba))
15665 return IRQ_NONE;
15666
15667 /*
James Smart4f774512009-05-22 14:52:35 -040015668 * Invoke fast-path host attention interrupt handling as appropriate.
15669 */
James Smart6a828b02019-01-28 11:14:31 -080015670 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040015671 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080015672 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040015673 if (hba_irq_rc == IRQ_HANDLED)
15674 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040015675 }
15676
James Smart67d12732012-08-03 12:36:13 -040015677 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040015678} /* lpfc_sli4_intr_handler */
15679
James Smart93a4d6f2019-11-04 16:57:05 -080015680void lpfc_sli4_poll_hbtimer(struct timer_list *t)
15681{
15682 struct lpfc_hba *phba = from_timer(phba, t, cpuhp_poll_timer);
15683 struct lpfc_queue *eq;
15684 int i = 0;
15685
15686 rcu_read_lock();
15687
15688 list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
15689 i += lpfc_sli4_poll_eq(eq, LPFC_POLL_SLOWPATH);
15690 if (!list_empty(&phba->poll_list))
15691 mod_timer(&phba->cpuhp_poll_timer,
15692 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
15693
15694 rcu_read_unlock();
15695}
15696
15697inline int lpfc_sli4_poll_eq(struct lpfc_queue *eq, uint8_t path)
15698{
15699 struct lpfc_hba *phba = eq->phba;
15700 int i = 0;
15701
15702 /*
15703 * Unlocking an irq is one of the entry point to check
15704 * for re-schedule, but we are good for io submission
15705 * path as midlayer does a get_cpu to glue us in. Flush
15706 * out the invalidate queue so we can see the updated
15707 * value for flag.
15708 */
15709 smp_rmb();
15710
15711 if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
15712 /* We will not likely get the completion for the caller
15713 * during this iteration but i guess that's fine.
15714 * Future io's coming on this eq should be able to
15715 * pick it up. As for the case of single io's, they
15716 * will be handled through a sched from polling timer
15717 * function which is currently triggered every 1msec.
15718 */
15719 i = lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM);
15720
15721 return i;
15722}
15723
15724static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
15725{
15726 struct lpfc_hba *phba = eq->phba;
15727
James Smartf861f592020-03-22 11:12:54 -070015728 /* kickstart slowpath processing if needed */
15729 if (list_empty(&phba->poll_list))
James Smart93a4d6f2019-11-04 16:57:05 -080015730 mod_timer(&phba->cpuhp_poll_timer,
15731 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
James Smart93a4d6f2019-11-04 16:57:05 -080015732
15733 list_add_rcu(&eq->_poll_list, &phba->poll_list);
15734 synchronize_rcu();
15735}
15736
15737static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
15738{
15739 struct lpfc_hba *phba = eq->phba;
15740
15741 /* Disable slowpath processing for this eq. Kick start the eq
15742 * by RE-ARMING the eq's ASAP
15743 */
15744 list_del_rcu(&eq->_poll_list);
15745 synchronize_rcu();
15746
15747 if (list_empty(&phba->poll_list))
15748 del_timer_sync(&phba->cpuhp_poll_timer);
15749}
15750
James Smartd480e572019-11-11 15:03:58 -080015751void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
James Smart93a4d6f2019-11-04 16:57:05 -080015752{
15753 struct lpfc_queue *eq, *next;
15754
15755 list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
15756 list_del(&eq->_poll_list);
15757
15758 INIT_LIST_HEAD(&phba->poll_list);
15759 synchronize_rcu();
15760}
15761
15762static inline void
15763__lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
15764{
15765 if (mode == eq->mode)
15766 return;
15767 /*
15768 * currently this function is only called during a hotplug
15769 * event and the cpu on which this function is executing
15770 * is going offline. By now the hotplug has instructed
15771 * the scheduler to remove this cpu from cpu active mask.
15772 * So we don't need to work about being put aside by the
15773 * scheduler for a high priority process. Yes, the inte-
15774 * rrupts could come but they are known to retire ASAP.
15775 */
15776
15777 /* Disable polling in the fastpath */
15778 WRITE_ONCE(eq->mode, mode);
15779 /* flush out the store buffer */
15780 smp_wmb();
15781
15782 /*
15783 * Add this eq to the polling list and start polling. For
15784 * a grace period both interrupt handler and poller will
15785 * try to process the eq _but_ that's fine. We have a
15786 * synchronization mechanism in place (queue_claimed) to
15787 * deal with it. This is just a draining phase for int-
15788 * errupt handler (not eq's) as we have guranteed through
15789 * barrier that all the CPUs have seen the new CQ_POLLED
15790 * state. which will effectively disable the REARMING of
15791 * the EQ. The whole idea is eq's die off eventually as
15792 * we are not rearming EQ's anymore.
15793 */
15794 mode ? lpfc_sli4_add_to_poll_list(eq) :
15795 lpfc_sli4_remove_from_poll_list(eq);
15796}
15797
15798void lpfc_sli4_start_polling(struct lpfc_queue *eq)
15799{
15800 __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
15801}
15802
15803void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
15804{
15805 struct lpfc_hba *phba = eq->phba;
15806
15807 __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
15808
15809 /* Kick start for the pending io's in h/w.
15810 * Once we switch back to interrupt processing on a eq
15811 * the io path completion will only arm eq's when it
15812 * receives a completion. But since eq's are in disa-
15813 * rmed state it doesn't receive a completion. This
15814 * creates a deadlock scenaro.
15815 */
15816 phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
15817}
15818
James Smart4f774512009-05-22 14:52:35 -040015819/**
15820 * lpfc_sli4_queue_free - free a queue structure and associated memory
15821 * @queue: The queue structure to free.
15822 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040015823 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040015824 * the host resident queue. This function must be called after destroying the
15825 * queue on the HBA.
15826 **/
15827void
15828lpfc_sli4_queue_free(struct lpfc_queue *queue)
15829{
15830 struct lpfc_dmabuf *dmabuf;
15831
15832 if (!queue)
15833 return;
15834
James Smart4645f7b2019-03-12 16:30:14 -070015835 if (!list_empty(&queue->wq_list))
15836 list_del(&queue->wq_list);
15837
James Smart4f774512009-05-22 14:52:35 -040015838 while (!list_empty(&queue->page_list)) {
15839 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
15840 list);
James Smart81b96ed2017-11-20 16:00:29 -080015841 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040015842 dmabuf->virt, dmabuf->phys);
15843 kfree(dmabuf);
15844 }
James Smart895427b2017-02-12 13:52:30 -080015845 if (queue->rqbp) {
15846 lpfc_free_rq_buffer(queue->phba, queue);
15847 kfree(queue->rqbp);
15848 }
James Smartd1f525a2017-04-21 16:04:55 -070015849
James Smart32517fc2019-01-28 11:14:33 -080015850 if (!list_empty(&queue->cpu_list))
15851 list_del(&queue->cpu_list);
15852
James Smart4f774512009-05-22 14:52:35 -040015853 kfree(queue);
15854 return;
15855}
15856
15857/**
15858 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
15859 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080015860 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040015861 * @entry_size: The size of each queue entry for this queue.
Lee Jones7af29d42020-07-21 17:41:31 +010015862 * @entry_count: The number of entries that this queue will handle.
James Smartc1a21eb2019-03-12 16:30:29 -070015863 * @cpu: The cpu that will primarily utilize this queue.
James Smart4f774512009-05-22 14:52:35 -040015864 *
15865 * This function allocates a queue structure and the DMAable memory used for
15866 * the host resident queue. This function must be called before creating the
15867 * queue on the HBA.
15868 **/
15869struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080015870lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
James Smartc1a21eb2019-03-12 16:30:29 -070015871 uint32_t entry_size, uint32_t entry_count, int cpu)
James Smart4f774512009-05-22 14:52:35 -040015872{
15873 struct lpfc_queue *queue;
15874 struct lpfc_dmabuf *dmabuf;
James Smartcb5172e2010-03-15 11:25:07 -040015875 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart9afbee32019-03-12 16:30:28 -070015876 uint16_t x, pgcnt;
James Smart4f774512009-05-22 14:52:35 -040015877
James Smartcb5172e2010-03-15 11:25:07 -040015878 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015879 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040015880
James Smart9afbee32019-03-12 16:30:28 -070015881 pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080015882
15883 /* If needed, Adjust page count to match the max the adapter supports */
James Smart9afbee32019-03-12 16:30:28 -070015884 if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
15885 pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
15886
James Smartc1a21eb2019-03-12 16:30:29 -070015887 queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
15888 GFP_KERNEL, cpu_to_node(cpu));
James Smart9afbee32019-03-12 16:30:28 -070015889 if (!queue)
15890 return NULL;
James Smart895427b2017-02-12 13:52:30 -080015891
James Smart4f774512009-05-22 14:52:35 -040015892 INIT_LIST_HEAD(&queue->list);
James Smart93a4d6f2019-11-04 16:57:05 -080015893 INIT_LIST_HEAD(&queue->_poll_list);
James Smart895427b2017-02-12 13:52:30 -080015894 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080015895 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040015896 INIT_LIST_HEAD(&queue->page_list);
15897 INIT_LIST_HEAD(&queue->child_list);
James Smart32517fc2019-01-28 11:14:33 -080015898 INIT_LIST_HEAD(&queue->cpu_list);
James Smart81b96ed2017-11-20 16:00:29 -080015899
15900 /* Set queue parameters now. If the system cannot provide memory
15901 * resources, the free routine needs to know what was allocated.
15902 */
James Smart9afbee32019-03-12 16:30:28 -070015903 queue->page_count = pgcnt;
15904 queue->q_pgs = (void **)&queue[1];
15905 queue->entry_cnt_per_pg = hw_page_size / entry_size;
James Smart81b96ed2017-11-20 16:00:29 -080015906 queue->entry_size = entry_size;
15907 queue->entry_count = entry_count;
15908 queue->page_size = hw_page_size;
15909 queue->phba = phba;
15910
James Smart9afbee32019-03-12 16:30:28 -070015911 for (x = 0; x < queue->page_count; x++) {
James Smartc1a21eb2019-03-12 16:30:29 -070015912 dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
15913 dev_to_node(&phba->pcidev->dev));
James Smart4f774512009-05-22 14:52:35 -040015914 if (!dmabuf)
15915 goto out_fail;
Luis Chamberlain750afb02019-01-04 09:23:09 +010015916 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
15917 hw_page_size, &dmabuf->phys,
15918 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040015919 if (!dmabuf->virt) {
15920 kfree(dmabuf);
15921 goto out_fail;
15922 }
15923 dmabuf->buffer_tag = x;
15924 list_add_tail(&dmabuf->list, &queue->page_list);
James Smart9afbee32019-03-12 16:30:28 -070015925 /* use lpfc_sli4_qe to index a paritcular entry in this page */
15926 queue->q_pgs[x] = dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -040015927 }
Dick Kennedyf485c182017-09-29 17:34:34 -070015928 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
15929 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart32517fc2019-01-28 11:14:33 -080015930 INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
15931 INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040015932
James Smart32517fc2019-01-28 11:14:33 -080015933 /* notify_interval will be set during q creation */
James Smart64eb4dc2017-05-15 15:20:49 -070015934
James Smart4f774512009-05-22 14:52:35 -040015935 return queue;
15936out_fail:
15937 lpfc_sli4_queue_free(queue);
15938 return NULL;
15939}
15940
15941/**
James Smart962bc512013-01-03 15:44:00 -050015942 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
15943 * @phba: HBA structure that indicates port to create a queue on.
15944 * @pci_barset: PCI BAR set flag.
15945 *
15946 * This function shall perform iomap of the specified PCI BAR address to host
15947 * memory address if not already done so and return it. The returned host
15948 * memory address can be NULL.
15949 */
15950static void __iomem *
15951lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
15952{
James Smart962bc512013-01-03 15:44:00 -050015953 if (!phba->pcidev)
15954 return NULL;
James Smart962bc512013-01-03 15:44:00 -050015955
15956 switch (pci_barset) {
15957 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050015958 return phba->pci_bar0_memmap_p;
15959 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050015960 return phba->pci_bar2_memmap_p;
15961 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050015962 return phba->pci_bar4_memmap_p;
15963 default:
15964 break;
15965 }
15966 return NULL;
15967}
15968
15969/**
James Smartcb733e32019-01-28 11:14:32 -080015970 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
15971 * @phba: HBA structure that EQs are on.
15972 * @startq: The starting EQ index to modify
15973 * @numq: The number of EQs (consecutive indexes) to modify
15974 * @usdelay: amount of delay
James Smart173edbb2012-06-12 13:54:50 -040015975 *
James Smartcb733e32019-01-28 11:14:32 -080015976 * This function revises the EQ delay on 1 or more EQs. The EQ delay
15977 * is set either by writing to a register (if supported by the SLI Port)
15978 * or by mailbox command. The mailbox command allows several EQs to be
15979 * updated at once.
James Smart173edbb2012-06-12 13:54:50 -040015980 *
James Smartcb733e32019-01-28 11:14:32 -080015981 * The @phba struct is used to send a mailbox command to HBA. The @startq
15982 * is used to get the starting EQ index to change. The @numq value is
15983 * used to specify how many consecutive EQ indexes, starting at EQ index,
15984 * are to be changed. This function is asynchronous and will wait for any
15985 * mailbox commands to finish before returning.
James Smart173edbb2012-06-12 13:54:50 -040015986 *
James Smartcb733e32019-01-28 11:14:32 -080015987 * On success this function will return a zero. If unable to allocate
15988 * enough memory this function will return -ENOMEM. If a mailbox command
15989 * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
15990 * have had their delay multipler changed.
James Smart173edbb2012-06-12 13:54:50 -040015991 **/
James Smartcb733e32019-01-28 11:14:32 -080015992void
James Smart0cf07f842017-06-01 21:07:10 -070015993lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
James Smartcb733e32019-01-28 11:14:32 -080015994 uint32_t numq, uint32_t usdelay)
James Smart173edbb2012-06-12 13:54:50 -040015995{
15996 struct lpfc_mbx_modify_eq_delay *eq_delay;
15997 LPFC_MBOXQ_t *mbox;
15998 struct lpfc_queue *eq;
James Smartcb733e32019-01-28 11:14:32 -080015999 int cnt = 0, rc, length;
James Smart173edbb2012-06-12 13:54:50 -040016000 uint32_t shdr_status, shdr_add_status;
James Smartcb733e32019-01-28 11:14:32 -080016001 uint32_t dmult;
James Smart895427b2017-02-12 13:52:30 -080016002 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040016003 union lpfc_sli4_cfg_shdr *shdr;
James Smart173edbb2012-06-12 13:54:50 -040016004
James Smart6a828b02019-01-28 11:14:31 -080016005 if (startq >= phba->cfg_irq_chann)
James Smartcb733e32019-01-28 11:14:32 -080016006 return;
16007
16008 if (usdelay > 0xFFFF) {
16009 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
16010 "6429 usdelay %d too large. Scaled down to "
16011 "0xFFFF.\n", usdelay);
16012 usdelay = 0xFFFF;
16013 }
16014
16015 /* set values by EQ_DELAY register if supported */
16016 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
16017 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070016018 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smartcb733e32019-01-28 11:14:32 -080016019 if (!eq)
16020 continue;
16021
James Smart32517fc2019-01-28 11:14:33 -080016022 lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
James Smartcb733e32019-01-28 11:14:32 -080016023
16024 if (++cnt >= numq)
16025 break;
16026 }
James Smartcb733e32019-01-28 11:14:32 -080016027 return;
16028 }
16029
16030 /* Otherwise, set values by mailbox cmd */
James Smart173edbb2012-06-12 13:54:50 -040016031
16032 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smartcb733e32019-01-28 11:14:32 -080016033 if (!mbox) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016034 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcb733e32019-01-28 11:14:32 -080016035 "6428 Failed allocating mailbox cmd buffer."
16036 " EQ delay was not set.\n");
16037 return;
16038 }
James Smart173edbb2012-06-12 13:54:50 -040016039 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
16040 sizeof(struct lpfc_sli4_cfg_mhdr));
16041 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16042 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
16043 length, LPFC_SLI4_MBX_EMBED);
16044 eq_delay = &mbox->u.mqe.un.eq_delay;
16045
16046 /* Calculate delay multiper from maximum interrupt per second */
James Smartcb733e32019-01-28 11:14:32 -080016047 dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
16048 if (dmult)
16049 dmult--;
James Smart0cf07f842017-06-01 21:07:10 -070016050 if (dmult > LPFC_DMULT_MAX)
16051 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040016052
James Smart6a828b02019-01-28 11:14:31 -080016053 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070016054 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart173edbb2012-06-12 13:54:50 -040016055 if (!eq)
16056 continue;
James Smartcb733e32019-01-28 11:14:32 -080016057 eq->q_mode = usdelay;
James Smart173edbb2012-06-12 13:54:50 -040016058 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
16059 eq_delay->u.request.eq[cnt].phase = 0;
16060 eq_delay->u.request.eq[cnt].delay_multi = dmult;
James Smart0cf07f842017-06-01 21:07:10 -070016061
James Smartcb733e32019-01-28 11:14:32 -080016062 if (++cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040016063 break;
16064 }
16065 eq_delay->u.request.num_eq = cnt;
16066
16067 mbox->vport = phba->pport;
16068 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080016069 mbox->ctx_buf = NULL;
16070 mbox->ctx_ndlp = NULL;
James Smart173edbb2012-06-12 13:54:50 -040016071 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16072 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
16073 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16074 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16075 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016076 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart173edbb2012-06-12 13:54:50 -040016077 "2512 MODIFY_EQ_DELAY mailbox failed with "
16078 "status x%x add_status x%x, mbx status x%x\n",
16079 shdr_status, shdr_add_status, rc);
James Smart173edbb2012-06-12 13:54:50 -040016080 }
16081 mempool_free(mbox, phba->mbox_mem_pool);
James Smartcb733e32019-01-28 11:14:32 -080016082 return;
James Smart173edbb2012-06-12 13:54:50 -040016083}
16084
16085/**
James Smart4f774512009-05-22 14:52:35 -040016086 * lpfc_eq_create - Create an Event Queue on the HBA
16087 * @phba: HBA structure that indicates port to create a queue on.
16088 * @eq: The queue structure to use to create the event queue.
16089 * @imax: The maximum interrupt per second limit.
16090 *
16091 * This function creates an event queue, as detailed in @eq, on a port,
16092 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
16093 *
16094 * The @phba struct is used to send mailbox command to HBA. The @eq struct
16095 * is used to get the entry count and entry size that are necessary to
16096 * determine the number of pages to allocate and use for this queue. This
16097 * function will send the EQ_CREATE mailbox command to the HBA to setup the
16098 * event queue. This function is asynchronous and will wait for the mailbox
16099 * command to finish before continuing.
16100 *
16101 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040016102 * memory this function will return -ENOMEM. If the queue create mailbox command
16103 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016104 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016105int
James Smartee020062012-09-29 11:28:52 -040016106lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040016107{
16108 struct lpfc_mbx_eq_create *eq_create;
16109 LPFC_MBOXQ_t *mbox;
16110 int rc, length, status = 0;
16111 struct lpfc_dmabuf *dmabuf;
16112 uint32_t shdr_status, shdr_add_status;
16113 union lpfc_sli4_cfg_shdr *shdr;
16114 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040016115 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16116
James Smart2e90f4b2011-12-13 13:22:37 -050016117 /* sanity check on queue memory */
16118 if (!eq)
16119 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040016120 if (!phba->sli4_hba.pc_sli4_params.supported)
16121 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040016122
16123 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16124 if (!mbox)
16125 return -ENOMEM;
16126 length = (sizeof(struct lpfc_mbx_eq_create) -
16127 sizeof(struct lpfc_sli4_cfg_mhdr));
16128 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16129 LPFC_MBOX_OPCODE_EQ_CREATE,
16130 length, LPFC_SLI4_MBX_EMBED);
16131 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080016132 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040016133 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
16134 eq->page_count);
16135 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
16136 LPFC_EQE_SIZE);
16137 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080016138
16139 /* Use version 2 of CREATE_EQ if eqav is set */
16140 if (phba->sli4_hba.pc_sli4_params.eqav) {
16141 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16142 LPFC_Q_CREATE_VERSION_2);
16143 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
16144 phba->sli4_hba.pc_sli4_params.eqav);
16145 }
16146
James Smart2c9c5a02015-04-07 15:07:15 -040016147 /* don't setup delay multiplier using EQ_CREATE */
16148 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040016149 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
16150 dmult);
16151 switch (eq->entry_count) {
16152 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070016153 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040016154 "0360 Unsupported EQ count. (%d)\n",
16155 eq->entry_count);
James Smart04d210c2019-05-21 17:49:03 -070016156 if (eq->entry_count < 256) {
16157 status = -EINVAL;
16158 goto out;
16159 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016160 fallthrough; /* otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040016161 case 256:
16162 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16163 LPFC_EQ_CNT_256);
16164 break;
16165 case 512:
16166 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16167 LPFC_EQ_CNT_512);
16168 break;
16169 case 1024:
16170 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16171 LPFC_EQ_CNT_1024);
16172 break;
16173 case 2048:
16174 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16175 LPFC_EQ_CNT_2048);
16176 break;
16177 case 4096:
16178 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
16179 LPFC_EQ_CNT_4096);
16180 break;
16181 }
16182 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040016183 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040016184 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16185 putPaddrLow(dmabuf->phys);
16186 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16187 putPaddrHigh(dmabuf->phys);
16188 }
16189 mbox->vport = phba->pport;
16190 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080016191 mbox->ctx_buf = NULL;
16192 mbox->ctx_ndlp = NULL;
James Smart4f774512009-05-22 14:52:35 -040016193 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040016194 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16195 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16196 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016197 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040016198 "2500 EQ_CREATE mailbox failed with "
16199 "status x%x add_status x%x, mbx status x%x\n",
16200 shdr_status, shdr_add_status, rc);
16201 status = -ENXIO;
16202 }
16203 eq->type = LPFC_EQ;
16204 eq->subtype = LPFC_NONE;
16205 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
16206 if (eq->queue_id == 0xFFFF)
16207 status = -ENXIO;
16208 eq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016209 eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
16210 eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
James Smart04d210c2019-05-21 17:49:03 -070016211out:
James Smart8fa38512009-07-19 10:01:03 -040016212 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016213 return status;
16214}
16215
Dick Kennedy317aeb82020-06-30 14:49:59 -070016216static int lpfc_cq_poll_hdler(struct irq_poll *iop, int budget)
16217{
16218 struct lpfc_queue *cq = container_of(iop, struct lpfc_queue, iop);
16219
Colin Ian King26e0b9a2020-07-07 16:00:18 +010016220 __lpfc_sli4_hba_process_cq(cq, LPFC_IRQ_POLL);
Dick Kennedy317aeb82020-06-30 14:49:59 -070016221
16222 return 1;
16223}
16224
James Smart4f774512009-05-22 14:52:35 -040016225/**
16226 * lpfc_cq_create - Create a Completion Queue on the HBA
16227 * @phba: HBA structure that indicates port to create a queue on.
16228 * @cq: The queue structure to use to create the completion queue.
16229 * @eq: The event queue to bind this completion queue to.
Lee Jones7af29d42020-07-21 17:41:31 +010016230 * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16231 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
James Smart4f774512009-05-22 14:52:35 -040016232 *
16233 * This function creates a completion queue, as detailed in @wq, on a port,
16234 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
16235 *
16236 * The @phba struct is used to send mailbox command to HBA. The @cq struct
16237 * is used to get the entry count and entry size that are necessary to
16238 * determine the number of pages to allocate and use for this queue. The @eq
16239 * is used to indicate which event queue to bind this completion queue to. This
16240 * function will send the CQ_CREATE mailbox command to the HBA to setup the
16241 * completion queue. This function is asynchronous and will wait for the mailbox
16242 * command to finish before continuing.
16243 *
16244 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040016245 * memory this function will return -ENOMEM. If the queue create mailbox command
16246 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016247 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016248int
James Smart4f774512009-05-22 14:52:35 -040016249lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
16250 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
16251{
16252 struct lpfc_mbx_cq_create *cq_create;
16253 struct lpfc_dmabuf *dmabuf;
16254 LPFC_MBOXQ_t *mbox;
16255 int rc, length, status = 0;
16256 uint32_t shdr_status, shdr_add_status;
16257 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040016258
James Smart2e90f4b2011-12-13 13:22:37 -050016259 /* sanity check on queue memory */
16260 if (!cq || !eq)
16261 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040016262
James Smart4f774512009-05-22 14:52:35 -040016263 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16264 if (!mbox)
16265 return -ENOMEM;
16266 length = (sizeof(struct lpfc_mbx_cq_create) -
16267 sizeof(struct lpfc_sli4_cfg_mhdr));
16268 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16269 LPFC_MBOX_OPCODE_CQ_CREATE,
16270 length, LPFC_SLI4_MBX_EMBED);
16271 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050016272 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040016273 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
16274 cq->page_count);
16275 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
16276 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050016277 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16278 phba->sli4_hba.pc_sli4_params.cqv);
16279 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080016280 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
16281 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050016282 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
16283 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080016284 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
16285 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050016286 } else {
16287 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
16288 eq->queue_id);
16289 }
James Smart4f774512009-05-22 14:52:35 -040016290 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080016291 case 2048:
16292 case 4096:
16293 if (phba->sli4_hba.pc_sli4_params.cqv ==
16294 LPFC_Q_CREATE_VERSION_2) {
16295 cq_create->u.request.context.lpfc_cq_context_count =
16296 cq->entry_count;
16297 bf_set(lpfc_cq_context_count,
16298 &cq_create->u.request.context,
16299 LPFC_CQ_CNT_WORD7);
16300 break;
16301 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016302 fallthrough;
James Smart4f774512009-05-22 14:52:35 -040016303 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070016304 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2ea259e2017-02-12 13:52:27 -080016305 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070016306 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080016307 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070016308 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040016309 if (cq->entry_count < 256) {
16310 status = -EINVAL;
16311 goto out;
16312 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016313 fallthrough; /* otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040016314 case 256:
16315 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16316 LPFC_CQ_CNT_256);
16317 break;
16318 case 512:
16319 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16320 LPFC_CQ_CNT_512);
16321 break;
16322 case 1024:
16323 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
16324 LPFC_CQ_CNT_1024);
16325 break;
16326 }
16327 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080016328 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040016329 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16330 putPaddrLow(dmabuf->phys);
16331 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16332 putPaddrHigh(dmabuf->phys);
16333 }
16334 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16335
16336 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040016337 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16338 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16339 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016340 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040016341 "2501 CQ_CREATE mailbox failed with "
16342 "status x%x add_status x%x, mbx status x%x\n",
16343 shdr_status, shdr_add_status, rc);
16344 status = -ENXIO;
16345 goto out;
16346 }
16347 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
16348 if (cq->queue_id == 0xFFFF) {
16349 status = -ENXIO;
16350 goto out;
16351 }
16352 /* link the cq onto the parent eq child list */
16353 list_add_tail(&cq->list, &eq->child_list);
16354 /* Set up completion queue's type and subtype */
16355 cq->type = type;
16356 cq->subtype = subtype;
16357 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050016358 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080016359 cq->assoc_qp = eq;
James Smart4f774512009-05-22 14:52:35 -040016360 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016361 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16362 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
James Smart4f774512009-05-22 14:52:35 -040016363
James Smart6a828b02019-01-28 11:14:31 -080016364 if (cq->queue_id > phba->sli4_hba.cq_max)
16365 phba->sli4_hba.cq_max = cq->queue_id;
Dick Kennedy317aeb82020-06-30 14:49:59 -070016366
16367 irq_poll_init(&cq->iop, LPFC_IRQ_POLL_WEIGHT, lpfc_cq_poll_hdler);
James Smart8fa38512009-07-19 10:01:03 -040016368out:
16369 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016370 return status;
16371}
16372
16373/**
James Smart2d7dbc42017-02-12 13:52:35 -080016374 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
16375 * @phba: HBA structure that indicates port to create a queue on.
16376 * @cqp: The queue structure array to use to create the completion queues.
James Smartcdb42be2019-01-28 11:14:21 -080016377 * @hdwq: The hardware queue array with the EQ to bind completion queues to.
Lee Jones7af29d42020-07-21 17:41:31 +010016378 * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
16379 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
James Smart2d7dbc42017-02-12 13:52:35 -080016380 *
16381 * This function creates a set of completion queue, s to support MRQ
16382 * as detailed in @cqp, on a port,
16383 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
16384 *
16385 * The @phba struct is used to send mailbox command to HBA. The @cq struct
16386 * is used to get the entry count and entry size that are necessary to
16387 * determine the number of pages to allocate and use for this queue. The @eq
16388 * is used to indicate which event queue to bind this completion queue to. This
16389 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
16390 * completion queue. This function is asynchronous and will wait for the mailbox
16391 * command to finish before continuing.
16392 *
16393 * On success this function will return a zero. If unable to allocate enough
16394 * memory this function will return -ENOMEM. If the queue create mailbox command
16395 * fails this function will return -ENXIO.
16396 **/
16397int
16398lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
James Smartcdb42be2019-01-28 11:14:21 -080016399 struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
16400 uint32_t subtype)
James Smart2d7dbc42017-02-12 13:52:35 -080016401{
16402 struct lpfc_queue *cq;
16403 struct lpfc_queue *eq;
16404 struct lpfc_mbx_cq_create_set *cq_set;
16405 struct lpfc_dmabuf *dmabuf;
16406 LPFC_MBOXQ_t *mbox;
16407 int rc, length, alloclen, status = 0;
16408 int cnt, idx, numcq, page_idx = 0;
16409 uint32_t shdr_status, shdr_add_status;
16410 union lpfc_sli4_cfg_shdr *shdr;
16411 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16412
16413 /* sanity check on queue memory */
16414 numcq = phba->cfg_nvmet_mrq;
James Smartcdb42be2019-01-28 11:14:21 -080016415 if (!cqp || !hdwq || !numcq)
James Smart2d7dbc42017-02-12 13:52:35 -080016416 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080016417
16418 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16419 if (!mbox)
16420 return -ENOMEM;
16421
16422 length = sizeof(struct lpfc_mbx_cq_create_set);
16423 length += ((numcq * cqp[0]->page_count) *
16424 sizeof(struct dma_address));
16425 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16426 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
16427 LPFC_SLI4_MBX_NEMBED);
16428 if (alloclen < length) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016429 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080016430 "3098 Allocated DMA memory size (%d) is "
16431 "less than the requested DMA memory size "
16432 "(%d)\n", alloclen, length);
16433 status = -ENOMEM;
16434 goto out;
16435 }
16436 cq_set = mbox->sge_array->addr[0];
16437 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
16438 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
16439
16440 for (idx = 0; idx < numcq; idx++) {
16441 cq = cqp[idx];
James Smartcdb42be2019-01-28 11:14:21 -080016442 eq = hdwq[idx].hba_eq;
James Smart2d7dbc42017-02-12 13:52:35 -080016443 if (!cq || !eq) {
16444 status = -ENOMEM;
16445 goto out;
16446 }
James Smart81b96ed2017-11-20 16:00:29 -080016447 if (!phba->sli4_hba.pc_sli4_params.supported)
16448 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080016449
16450 switch (idx) {
16451 case 0:
16452 bf_set(lpfc_mbx_cq_create_set_page_size,
16453 &cq_set->u.request,
16454 (hw_page_size / SLI4_PAGE_SIZE));
16455 bf_set(lpfc_mbx_cq_create_set_num_pages,
16456 &cq_set->u.request, cq->page_count);
16457 bf_set(lpfc_mbx_cq_create_set_evt,
16458 &cq_set->u.request, 1);
16459 bf_set(lpfc_mbx_cq_create_set_valid,
16460 &cq_set->u.request, 1);
16461 bf_set(lpfc_mbx_cq_create_set_cqe_size,
16462 &cq_set->u.request, 0);
16463 bf_set(lpfc_mbx_cq_create_set_num_cq,
16464 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080016465 bf_set(lpfc_mbx_cq_create_set_autovalid,
16466 &cq_set->u.request,
16467 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080016468 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080016469 case 2048:
16470 case 4096:
16471 if (phba->sli4_hba.pc_sli4_params.cqv ==
16472 LPFC_Q_CREATE_VERSION_2) {
16473 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16474 &cq_set->u.request,
16475 cq->entry_count);
16476 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16477 &cq_set->u.request,
16478 LPFC_CQ_CNT_WORD7);
16479 break;
16480 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016481 fallthrough;
James Smart2d7dbc42017-02-12 13:52:35 -080016482 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070016483 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080016484 "3118 Bad CQ count. (%d)\n",
16485 cq->entry_count);
16486 if (cq->entry_count < 256) {
16487 status = -EINVAL;
16488 goto out;
16489 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016490 fallthrough; /* otherwise default to smallest */
James Smart2d7dbc42017-02-12 13:52:35 -080016491 case 256:
16492 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16493 &cq_set->u.request, LPFC_CQ_CNT_256);
16494 break;
16495 case 512:
16496 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16497 &cq_set->u.request, LPFC_CQ_CNT_512);
16498 break;
16499 case 1024:
16500 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
16501 &cq_set->u.request, LPFC_CQ_CNT_1024);
16502 break;
16503 }
16504 bf_set(lpfc_mbx_cq_create_set_eq_id0,
16505 &cq_set->u.request, eq->queue_id);
16506 break;
16507 case 1:
16508 bf_set(lpfc_mbx_cq_create_set_eq_id1,
16509 &cq_set->u.request, eq->queue_id);
16510 break;
16511 case 2:
16512 bf_set(lpfc_mbx_cq_create_set_eq_id2,
16513 &cq_set->u.request, eq->queue_id);
16514 break;
16515 case 3:
16516 bf_set(lpfc_mbx_cq_create_set_eq_id3,
16517 &cq_set->u.request, eq->queue_id);
16518 break;
16519 case 4:
16520 bf_set(lpfc_mbx_cq_create_set_eq_id4,
16521 &cq_set->u.request, eq->queue_id);
16522 break;
16523 case 5:
16524 bf_set(lpfc_mbx_cq_create_set_eq_id5,
16525 &cq_set->u.request, eq->queue_id);
16526 break;
16527 case 6:
16528 bf_set(lpfc_mbx_cq_create_set_eq_id6,
16529 &cq_set->u.request, eq->queue_id);
16530 break;
16531 case 7:
16532 bf_set(lpfc_mbx_cq_create_set_eq_id7,
16533 &cq_set->u.request, eq->queue_id);
16534 break;
16535 case 8:
16536 bf_set(lpfc_mbx_cq_create_set_eq_id8,
16537 &cq_set->u.request, eq->queue_id);
16538 break;
16539 case 9:
16540 bf_set(lpfc_mbx_cq_create_set_eq_id9,
16541 &cq_set->u.request, eq->queue_id);
16542 break;
16543 case 10:
16544 bf_set(lpfc_mbx_cq_create_set_eq_id10,
16545 &cq_set->u.request, eq->queue_id);
16546 break;
16547 case 11:
16548 bf_set(lpfc_mbx_cq_create_set_eq_id11,
16549 &cq_set->u.request, eq->queue_id);
16550 break;
16551 case 12:
16552 bf_set(lpfc_mbx_cq_create_set_eq_id12,
16553 &cq_set->u.request, eq->queue_id);
16554 break;
16555 case 13:
16556 bf_set(lpfc_mbx_cq_create_set_eq_id13,
16557 &cq_set->u.request, eq->queue_id);
16558 break;
16559 case 14:
16560 bf_set(lpfc_mbx_cq_create_set_eq_id14,
16561 &cq_set->u.request, eq->queue_id);
16562 break;
16563 case 15:
16564 bf_set(lpfc_mbx_cq_create_set_eq_id15,
16565 &cq_set->u.request, eq->queue_id);
16566 break;
16567 }
16568
16569 /* link the cq onto the parent eq child list */
16570 list_add_tail(&cq->list, &eq->child_list);
16571 /* Set up completion queue's type and subtype */
16572 cq->type = type;
16573 cq->subtype = subtype;
16574 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080016575 cq->assoc_qp = eq;
James Smart2d7dbc42017-02-12 13:52:35 -080016576 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016577 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
16578 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
16579 cq->entry_count);
James Smart81b96ed2017-11-20 16:00:29 -080016580 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080016581
16582 rc = 0;
16583 list_for_each_entry(dmabuf, &cq->page_list, list) {
16584 memset(dmabuf->virt, 0, hw_page_size);
16585 cnt = page_idx + dmabuf->buffer_tag;
16586 cq_set->u.request.page[cnt].addr_lo =
16587 putPaddrLow(dmabuf->phys);
16588 cq_set->u.request.page[cnt].addr_hi =
16589 putPaddrHigh(dmabuf->phys);
16590 rc++;
16591 }
16592 page_idx += rc;
16593 }
16594
16595 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16596
16597 /* The IOCTL status is embedded in the mailbox subheader. */
16598 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16599 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16600 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016601 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080016602 "3119 CQ_CREATE_SET mailbox failed with "
16603 "status x%x add_status x%x, mbx status x%x\n",
16604 shdr_status, shdr_add_status, rc);
16605 status = -ENXIO;
16606 goto out;
16607 }
16608 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
16609 if (rc == 0xFFFF) {
16610 status = -ENXIO;
16611 goto out;
16612 }
16613
16614 for (idx = 0; idx < numcq; idx++) {
16615 cq = cqp[idx];
16616 cq->queue_id = rc + idx;
James Smart6a828b02019-01-28 11:14:31 -080016617 if (cq->queue_id > phba->sli4_hba.cq_max)
16618 phba->sli4_hba.cq_max = cq->queue_id;
James Smart2d7dbc42017-02-12 13:52:35 -080016619 }
16620
16621out:
16622 lpfc_sli4_mbox_cmd_free(phba, mbox);
16623 return status;
16624}
16625
16626/**
James Smartb19a0612010-04-06 14:48:51 -040016627 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040016628 * @phba: HBA structure that indicates port to create a queue on.
16629 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040016630 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
16631 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040016632 *
James Smartb19a0612010-04-06 14:48:51 -040016633 * This function provides failback (fb) functionality when the
16634 * mq_create_ext fails on older FW generations. It's purpose is identical
16635 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040016636 *
James Smartb19a0612010-04-06 14:48:51 -040016637 * This routine cannot fail as all attributes were previously accessed and
16638 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040016639 **/
James Smartb19a0612010-04-06 14:48:51 -040016640static void
16641lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
16642 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040016643{
16644 struct lpfc_mbx_mq_create *mq_create;
16645 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040016646 int length;
James Smart04c68492009-05-22 14:52:52 -040016647
James Smart04c68492009-05-22 14:52:52 -040016648 length = (sizeof(struct lpfc_mbx_mq_create) -
16649 sizeof(struct lpfc_sli4_cfg_mhdr));
16650 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16651 LPFC_MBOX_OPCODE_MQ_CREATE,
16652 length, LPFC_SLI4_MBX_EMBED);
16653 mq_create = &mbox->u.mqe.un.mq_create;
16654 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040016655 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040016656 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040016657 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040016658 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
16659 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040016660 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050016661 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16662 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040016663 break;
16664 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050016665 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16666 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040016667 break;
16668 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050016669 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16670 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040016671 break;
16672 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050016673 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
16674 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040016675 break;
16676 }
16677 list_for_each_entry(dmabuf, &mq->page_list, list) {
16678 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040016679 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040016680 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040016681 putPaddrHigh(dmabuf->phys);
16682 }
16683}
16684
16685/**
16686 * lpfc_mq_create - Create a mailbox Queue on the HBA
16687 * @phba: HBA structure that indicates port to create a queue on.
16688 * @mq: The queue structure to use to create the mailbox queue.
16689 * @cq: The completion queue to associate with this cq.
16690 * @subtype: The queue's subtype.
16691 *
16692 * This function creates a mailbox queue, as detailed in @mq, on a port,
16693 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
16694 *
16695 * The @phba struct is used to send mailbox command to HBA. The @cq struct
16696 * is used to get the entry count and entry size that are necessary to
16697 * determine the number of pages to allocate and use for this queue. This
16698 * function will send the MQ_CREATE mailbox command to the HBA to setup the
16699 * mailbox queue. This function is asynchronous and will wait for the mailbox
16700 * command to finish before continuing.
16701 *
16702 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040016703 * memory this function will return -ENOMEM. If the queue create mailbox command
16704 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040016705 **/
16706int32_t
16707lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
16708 struct lpfc_queue *cq, uint32_t subtype)
16709{
16710 struct lpfc_mbx_mq_create *mq_create;
16711 struct lpfc_mbx_mq_create_ext *mq_create_ext;
16712 struct lpfc_dmabuf *dmabuf;
16713 LPFC_MBOXQ_t *mbox;
16714 int rc, length, status = 0;
16715 uint32_t shdr_status, shdr_add_status;
16716 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040016717 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040016718
James Smart2e90f4b2011-12-13 13:22:37 -050016719 /* sanity check on queue memory */
16720 if (!mq || !cq)
16721 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040016722 if (!phba->sli4_hba.pc_sli4_params.supported)
16723 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040016724
16725 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16726 if (!mbox)
16727 return -ENOMEM;
16728 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
16729 sizeof(struct lpfc_sli4_cfg_mhdr));
16730 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16731 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
16732 length, LPFC_SLI4_MBX_EMBED);
16733
16734 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050016735 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050016736 bf_set(lpfc_mbx_mq_create_ext_num_pages,
16737 &mq_create_ext->u.request, mq->page_count);
16738 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
16739 &mq_create_ext->u.request, 1);
16740 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040016741 &mq_create_ext->u.request, 1);
16742 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
16743 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050016744 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
16745 &mq_create_ext->u.request, 1);
16746 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
16747 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040016748 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050016749 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16750 phba->sli4_hba.pc_sli4_params.mqv);
16751 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
16752 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
16753 cq->queue_id);
16754 else
16755 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
16756 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040016757 switch (mq->entry_count) {
16758 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070016759 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartb19a0612010-04-06 14:48:51 -040016760 "0362 Unsupported MQ count. (%d)\n",
16761 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040016762 if (mq->entry_count < 16) {
16763 status = -EINVAL;
16764 goto out;
16765 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016766 fallthrough; /* otherwise default to smallest count */
James Smartb19a0612010-04-06 14:48:51 -040016767 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050016768 bf_set(lpfc_mq_context_ring_size,
16769 &mq_create_ext->u.request.context,
16770 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040016771 break;
16772 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050016773 bf_set(lpfc_mq_context_ring_size,
16774 &mq_create_ext->u.request.context,
16775 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040016776 break;
16777 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050016778 bf_set(lpfc_mq_context_ring_size,
16779 &mq_create_ext->u.request.context,
16780 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040016781 break;
16782 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050016783 bf_set(lpfc_mq_context_ring_size,
16784 &mq_create_ext->u.request.context,
16785 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040016786 break;
16787 }
16788 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040016789 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040016790 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
16791 putPaddrLow(dmabuf->phys);
16792 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040016793 putPaddrHigh(dmabuf->phys);
16794 }
16795 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040016796 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16797 &mq_create_ext->u.response);
16798 if (rc != MBX_SUCCESS) {
16799 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16800 "2795 MQ_CREATE_EXT failed with "
16801 "status x%x. Failback to MQ_CREATE.\n",
16802 rc);
16803 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
16804 mq_create = &mbox->u.mqe.un.mq_create;
16805 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16806 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
16807 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16808 &mq_create->u.response);
16809 }
16810
James Smart04c68492009-05-22 14:52:52 -040016811 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040016812 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16813 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16814 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016815 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart04c68492009-05-22 14:52:52 -040016816 "2502 MQ_CREATE mailbox failed with "
16817 "status x%x add_status x%x, mbx status x%x\n",
16818 shdr_status, shdr_add_status, rc);
16819 status = -ENXIO;
16820 goto out;
16821 }
James Smart04c68492009-05-22 14:52:52 -040016822 if (mq->queue_id == 0xFFFF) {
16823 status = -ENXIO;
16824 goto out;
16825 }
16826 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050016827 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040016828 mq->subtype = subtype;
16829 mq->host_index = 0;
16830 mq->hba_index = 0;
16831
16832 /* link the mq onto the parent cq child list */
16833 list_add_tail(&mq->list, &cq->child_list);
16834out:
James Smart8fa38512009-07-19 10:01:03 -040016835 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016836 return status;
16837}
16838
16839/**
James Smart4f774512009-05-22 14:52:35 -040016840 * lpfc_wq_create - Create a Work Queue on the HBA
16841 * @phba: HBA structure that indicates port to create a queue on.
16842 * @wq: The queue structure to use to create the work queue.
16843 * @cq: The completion queue to bind this work queue to.
16844 * @subtype: The subtype of the work queue indicating its functionality.
16845 *
16846 * This function creates a work queue, as detailed in @wq, on a port, described
16847 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
16848 *
16849 * The @phba struct is used to send mailbox command to HBA. The @wq struct
16850 * is used to get the entry count and entry size that are necessary to
16851 * determine the number of pages to allocate and use for this queue. The @cq
16852 * is used to indicate which completion queue to bind this work queue to. This
16853 * function will send the WQ_CREATE mailbox command to the HBA to setup the
16854 * work queue. This function is asynchronous and will wait for the mailbox
16855 * command to finish before continuing.
16856 *
16857 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040016858 * memory this function will return -ENOMEM. If the queue create mailbox command
16859 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016860 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016861int
James Smart4f774512009-05-22 14:52:35 -040016862lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
16863 struct lpfc_queue *cq, uint32_t subtype)
16864{
16865 struct lpfc_mbx_wq_create *wq_create;
16866 struct lpfc_dmabuf *dmabuf;
16867 LPFC_MBOXQ_t *mbox;
16868 int rc, length, status = 0;
16869 uint32_t shdr_status, shdr_add_status;
16870 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040016871 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050016872 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050016873 void __iomem *bar_memmap_p;
16874 uint32_t db_offset;
16875 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080016876 uint8_t dpp_barset;
16877 uint32_t dpp_offset;
James Smart81b96ed2017-11-20 16:00:29 -080016878 uint8_t wq_create_version;
Lee Jones11d8e562020-07-21 17:41:44 +010016879#ifdef CONFIG_X86
16880 unsigned long pg_addr;
16881#endif
James Smart49198b32010-04-06 15:04:33 -040016882
James Smart2e90f4b2011-12-13 13:22:37 -050016883 /* sanity check on queue memory */
16884 if (!wq || !cq)
16885 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040016886 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080016887 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040016888
16889 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16890 if (!mbox)
16891 return -ENOMEM;
16892 length = (sizeof(struct lpfc_mbx_wq_create) -
16893 sizeof(struct lpfc_sli4_cfg_mhdr));
16894 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16895 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
16896 length, LPFC_SLI4_MBX_EMBED);
16897 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050016898 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040016899 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
16900 wq->page_count);
16901 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
16902 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040016903
16904 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050016905 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16906 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050016907
James Smartc176ffa2018-01-30 15:58:46 -080016908 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
16909 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080016910 wq_create_version = LPFC_Q_CREATE_VERSION_1;
16911 else
16912 wq_create_version = LPFC_Q_CREATE_VERSION_0;
16913
James Smart1351e692018-02-22 08:18:43 -080016914 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040016915 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050016916 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
16917 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070016918 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16919 LPFC_Q_CREATE_VERSION_1);
16920
James Smart5a6f1332011-03-11 16:05:35 -050016921 switch (wq->entry_size) {
16922 default:
16923 case 64:
16924 bf_set(lpfc_mbx_wq_create_wqe_size,
16925 &wq_create->u.request_1,
16926 LPFC_WQ_WQE_SIZE_64);
16927 break;
16928 case 128:
16929 bf_set(lpfc_mbx_wq_create_wqe_size,
16930 &wq_create->u.request_1,
16931 LPFC_WQ_WQE_SIZE_128);
16932 break;
16933 }
James Smart1351e692018-02-22 08:18:43 -080016934 /* Request DPP by default */
16935 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080016936 bf_set(lpfc_mbx_wq_create_page_size,
16937 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080016938 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050016939 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040016940 break;
16941 default:
James Smart1351e692018-02-22 08:18:43 -080016942 page = wq_create->u.request.page;
16943 break;
James Smart5a6f1332011-03-11 16:05:35 -050016944 }
James Smart0c651872013-07-15 18:33:23 -040016945
James Smart4f774512009-05-22 14:52:35 -040016946 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040016947 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050016948 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
16949 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040016950 }
James Smart962bc512013-01-03 15:44:00 -050016951
16952 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16953 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
16954
James Smart4f774512009-05-22 14:52:35 -040016955 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16956 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040016957 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16958 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16959 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016960 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040016961 "2503 WQ_CREATE mailbox failed with "
16962 "status x%x add_status x%x, mbx status x%x\n",
16963 shdr_status, shdr_add_status, rc);
16964 status = -ENXIO;
16965 goto out;
16966 }
James Smart1351e692018-02-22 08:18:43 -080016967
16968 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
16969 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
16970 &wq_create->u.response);
16971 else
16972 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
16973 &wq_create->u.response_1);
16974
James Smart4f774512009-05-22 14:52:35 -040016975 if (wq->queue_id == 0xFFFF) {
16976 status = -ENXIO;
16977 goto out;
16978 }
James Smart1351e692018-02-22 08:18:43 -080016979
16980 wq->db_format = LPFC_DB_LIST_FORMAT;
16981 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
16982 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16983 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
16984 &wq_create->u.response);
16985 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
16986 (wq->db_format != LPFC_DB_RING_FORMAT)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016987 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080016988 "3265 WQ[%d] doorbell format "
16989 "not supported: x%x\n",
16990 wq->queue_id, wq->db_format);
16991 status = -EINVAL;
16992 goto out;
16993 }
16994 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
16995 &wq_create->u.response);
16996 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16997 pci_barset);
16998 if (!bar_memmap_p) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016999 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080017000 "3263 WQ[%d] failed to memmap "
17001 "pci barset:x%x\n",
17002 wq->queue_id, pci_barset);
17003 status = -ENOMEM;
17004 goto out;
17005 }
17006 db_offset = wq_create->u.response.doorbell_offset;
17007 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
17008 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017009 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080017010 "3252 WQ[%d] doorbell offset "
17011 "not supported: x%x\n",
17012 wq->queue_id, db_offset);
17013 status = -EINVAL;
17014 goto out;
17015 }
17016 wq->db_regaddr = bar_memmap_p + db_offset;
17017 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17018 "3264 WQ[%d]: barset:x%x, offset:x%x, "
17019 "format:x%x\n", wq->queue_id,
17020 pci_barset, db_offset, wq->db_format);
17021 } else
17022 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050017023 } else {
James Smart1351e692018-02-22 08:18:43 -080017024 /* Check if DPP was honored by the firmware */
17025 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
17026 &wq_create->u.response_1);
17027 if (wq->dpp_enable) {
17028 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
17029 &wq_create->u.response_1);
17030 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17031 pci_barset);
17032 if (!bar_memmap_p) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017033 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080017034 "3267 WQ[%d] failed to memmap "
17035 "pci barset:x%x\n",
17036 wq->queue_id, pci_barset);
17037 status = -ENOMEM;
17038 goto out;
17039 }
17040 db_offset = wq_create->u.response_1.doorbell_offset;
17041 wq->db_regaddr = bar_memmap_p + db_offset;
17042 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
17043 &wq_create->u.response_1);
17044 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
17045 &wq_create->u.response_1);
17046 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
17047 dpp_barset);
17048 if (!bar_memmap_p) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017049 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080017050 "3268 WQ[%d] failed to memmap "
17051 "pci barset:x%x\n",
17052 wq->queue_id, dpp_barset);
17053 status = -ENOMEM;
17054 goto out;
17055 }
17056 dpp_offset = wq_create->u.response_1.dpp_offset;
17057 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
17058 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
17059 "3271 WQ[%d]: barset:x%x, offset:x%x, "
17060 "dpp_id:x%x dpp_barset:x%x "
17061 "dpp_offset:x%x\n",
17062 wq->queue_id, pci_barset, db_offset,
17063 wq->dpp_id, dpp_barset, dpp_offset);
17064
Lee Jones3c1311a2020-07-21 17:41:28 +010017065#ifdef CONFIG_X86
James Smart1351e692018-02-22 08:18:43 -080017066 /* Enable combined writes for DPP aperture */
17067 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
James Smart1351e692018-02-22 08:18:43 -080017068 rc = set_memory_wc(pg_addr, 1);
17069 if (rc) {
17070 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17071 "3272 Cannot setup Combined "
17072 "Write on WQ[%d] - disable DPP\n",
17073 wq->queue_id);
17074 phba->cfg_enable_dpp = 0;
17075 }
17076#else
17077 phba->cfg_enable_dpp = 0;
17078#endif
17079 } else
17080 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050017081 }
James Smart895427b2017-02-12 13:52:30 -080017082 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
17083 if (wq->pring == NULL) {
17084 status = -ENOMEM;
17085 goto out;
17086 }
James Smart4f774512009-05-22 14:52:35 -040017087 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050017088 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040017089 wq->subtype = subtype;
17090 wq->host_index = 0;
17091 wq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080017092 wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040017093
17094 /* link the wq onto the parent cq child list */
17095 list_add_tail(&wq->list, &cq->child_list);
17096out:
James Smart8fa38512009-07-19 10:01:03 -040017097 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017098 return status;
17099}
17100
17101/**
17102 * lpfc_rq_create - Create a Receive Queue on the HBA
17103 * @phba: HBA structure that indicates port to create a queue on.
17104 * @hrq: The queue structure to use to create the header receive queue.
17105 * @drq: The queue structure to use to create the data receive queue.
17106 * @cq: The completion queue to bind this work queue to.
Lee Jones7af29d42020-07-21 17:41:31 +010017107 * @subtype: The subtype of the work queue indicating its functionality.
James Smart4f774512009-05-22 14:52:35 -040017108 *
17109 * This function creates a receive buffer queue pair , as detailed in @hrq and
17110 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17111 * to the HBA.
17112 *
17113 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17114 * struct is used to get the entry count that is necessary to determine the
17115 * number of pages to use for this queue. The @cq is used to indicate which
17116 * completion queue to bind received buffers that are posted to these queues to.
17117 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17118 * receive queue pair. This function is asynchronous and will wait for the
17119 * mailbox command to finish before continuing.
17120 *
17121 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040017122 * memory this function will return -ENOMEM. If the queue create mailbox command
17123 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040017124 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040017125int
James Smart4f774512009-05-22 14:52:35 -040017126lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17127 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
17128{
17129 struct lpfc_mbx_rq_create *rq_create;
17130 struct lpfc_dmabuf *dmabuf;
17131 LPFC_MBOXQ_t *mbox;
17132 int rc, length, status = 0;
17133 uint32_t shdr_status, shdr_add_status;
17134 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040017135 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050017136 void __iomem *bar_memmap_p;
17137 uint32_t db_offset;
17138 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040017139
James Smart2e90f4b2011-12-13 13:22:37 -050017140 /* sanity check on queue memory */
17141 if (!hrq || !drq || !cq)
17142 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040017143 if (!phba->sli4_hba.pc_sli4_params.supported)
17144 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040017145
17146 if (hrq->entry_count != drq->entry_count)
17147 return -EINVAL;
17148 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17149 if (!mbox)
17150 return -ENOMEM;
17151 length = (sizeof(struct lpfc_mbx_rq_create) -
17152 sizeof(struct lpfc_sli4_cfg_mhdr));
17153 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17154 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17155 length, LPFC_SLI4_MBX_EMBED);
17156 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050017157 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17158 bf_set(lpfc_mbox_hdr_version, &shdr->request,
17159 phba->sli4_hba.pc_sli4_params.rqv);
17160 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17161 bf_set(lpfc_rq_context_rqe_count_1,
17162 &rq_create->u.request.context,
17163 hrq->entry_count);
17164 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040017165 bf_set(lpfc_rq_context_rqe_size,
17166 &rq_create->u.request.context,
17167 LPFC_RQE_SIZE_8);
17168 bf_set(lpfc_rq_context_page_size,
17169 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080017170 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050017171 } else {
17172 switch (hrq->entry_count) {
17173 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070017174 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5a6f1332011-03-11 16:05:35 -050017175 "2535 Unsupported RQ count. (%d)\n",
17176 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040017177 if (hrq->entry_count < 512) {
17178 status = -EINVAL;
17179 goto out;
17180 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050017181 fallthrough; /* otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050017182 case 512:
17183 bf_set(lpfc_rq_context_rqe_count,
17184 &rq_create->u.request.context,
17185 LPFC_RQ_RING_SIZE_512);
17186 break;
17187 case 1024:
17188 bf_set(lpfc_rq_context_rqe_count,
17189 &rq_create->u.request.context,
17190 LPFC_RQ_RING_SIZE_1024);
17191 break;
17192 case 2048:
17193 bf_set(lpfc_rq_context_rqe_count,
17194 &rq_create->u.request.context,
17195 LPFC_RQ_RING_SIZE_2048);
17196 break;
17197 case 4096:
17198 bf_set(lpfc_rq_context_rqe_count,
17199 &rq_create->u.request.context,
17200 LPFC_RQ_RING_SIZE_4096);
17201 break;
17202 }
17203 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
17204 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040017205 }
17206 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17207 cq->queue_id);
17208 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17209 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040017210 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040017211 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040017212 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17213 putPaddrLow(dmabuf->phys);
17214 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17215 putPaddrHigh(dmabuf->phys);
17216 }
James Smart962bc512013-01-03 15:44:00 -050017217 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17218 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
17219
James Smart4f774512009-05-22 14:52:35 -040017220 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17221 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040017222 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17223 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17224 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017225 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017226 "2504 RQ_CREATE mailbox failed with "
17227 "status x%x add_status x%x, mbx status x%x\n",
17228 shdr_status, shdr_add_status, rc);
17229 status = -ENXIO;
17230 goto out;
17231 }
17232 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17233 if (hrq->queue_id == 0xFFFF) {
17234 status = -ENXIO;
17235 goto out;
17236 }
James Smart962bc512013-01-03 15:44:00 -050017237
17238 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
17239 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
17240 &rq_create->u.response);
17241 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
17242 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017243 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -050017244 "3262 RQ [%d] doorbell format not "
17245 "supported: x%x\n", hrq->queue_id,
17246 hrq->db_format);
17247 status = -EINVAL;
17248 goto out;
17249 }
17250
17251 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
17252 &rq_create->u.response);
17253 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
17254 if (!bar_memmap_p) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017255 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -050017256 "3269 RQ[%d] failed to memmap pci "
17257 "barset:x%x\n", hrq->queue_id,
17258 pci_barset);
17259 status = -ENOMEM;
17260 goto out;
17261 }
17262
17263 db_offset = rq_create->u.response.doorbell_offset;
17264 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
17265 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017266 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -050017267 "3270 RQ[%d] doorbell offset not "
17268 "supported: x%x\n", hrq->queue_id,
17269 db_offset);
17270 status = -EINVAL;
17271 goto out;
17272 }
17273 hrq->db_regaddr = bar_memmap_p + db_offset;
17274 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040017275 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
17276 "format:x%x\n", hrq->queue_id, pci_barset,
17277 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050017278 } else {
17279 hrq->db_format = LPFC_DB_RING_FORMAT;
17280 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17281 }
James Smart4f774512009-05-22 14:52:35 -040017282 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050017283 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040017284 hrq->subtype = subtype;
17285 hrq->host_index = 0;
17286 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080017287 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040017288
17289 /* now create the data queue */
17290 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17291 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
17292 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050017293 bf_set(lpfc_mbox_hdr_version, &shdr->request,
17294 phba->sli4_hba.pc_sli4_params.rqv);
17295 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
17296 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040017297 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070017298 if (subtype == LPFC_NVMET)
17299 rq_create->u.request.context.buffer_size =
17300 LPFC_NVMET_DATA_BUF_SIZE;
17301 else
17302 rq_create->u.request.context.buffer_size =
17303 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040017304 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
17305 LPFC_RQE_SIZE_8);
17306 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
17307 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050017308 } else {
17309 switch (drq->entry_count) {
17310 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070017311 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5a6f1332011-03-11 16:05:35 -050017312 "2536 Unsupported RQ count. (%d)\n",
17313 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040017314 if (drq->entry_count < 512) {
17315 status = -EINVAL;
17316 goto out;
17317 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050017318 fallthrough; /* otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050017319 case 512:
17320 bf_set(lpfc_rq_context_rqe_count,
17321 &rq_create->u.request.context,
17322 LPFC_RQ_RING_SIZE_512);
17323 break;
17324 case 1024:
17325 bf_set(lpfc_rq_context_rqe_count,
17326 &rq_create->u.request.context,
17327 LPFC_RQ_RING_SIZE_1024);
17328 break;
17329 case 2048:
17330 bf_set(lpfc_rq_context_rqe_count,
17331 &rq_create->u.request.context,
17332 LPFC_RQ_RING_SIZE_2048);
17333 break;
17334 case 4096:
17335 bf_set(lpfc_rq_context_rqe_count,
17336 &rq_create->u.request.context,
17337 LPFC_RQ_RING_SIZE_4096);
17338 break;
17339 }
James Smart3c603be2017-05-15 15:20:44 -070017340 if (subtype == LPFC_NVMET)
17341 bf_set(lpfc_rq_context_buf_size,
17342 &rq_create->u.request.context,
17343 LPFC_NVMET_DATA_BUF_SIZE);
17344 else
17345 bf_set(lpfc_rq_context_buf_size,
17346 &rq_create->u.request.context,
17347 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040017348 }
17349 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
17350 cq->queue_id);
17351 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
17352 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040017353 list_for_each_entry(dmabuf, &drq->page_list, list) {
17354 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
17355 putPaddrLow(dmabuf->phys);
17356 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
17357 putPaddrHigh(dmabuf->phys);
17358 }
James Smart962bc512013-01-03 15:44:00 -050017359 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
17360 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040017361 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17362 /* The IOCTL status is embedded in the mailbox subheader. */
17363 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
17364 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17365 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17366 if (shdr_status || shdr_add_status || rc) {
17367 status = -ENXIO;
17368 goto out;
17369 }
17370 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17371 if (drq->queue_id == 0xFFFF) {
17372 status = -ENXIO;
17373 goto out;
17374 }
17375 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050017376 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040017377 drq->subtype = subtype;
17378 drq->host_index = 0;
17379 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080017380 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040017381
17382 /* link the header and data RQs onto the parent cq child list */
17383 list_add_tail(&hrq->list, &cq->child_list);
17384 list_add_tail(&drq->list, &cq->child_list);
17385
17386out:
James Smart8fa38512009-07-19 10:01:03 -040017387 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017388 return status;
17389}
17390
17391/**
James Smart2d7dbc42017-02-12 13:52:35 -080017392 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
17393 * @phba: HBA structure that indicates port to create a queue on.
17394 * @hrqp: The queue structure array to use to create the header receive queues.
17395 * @drqp: The queue structure array to use to create the data receive queues.
17396 * @cqp: The completion queue array to bind these receive queues to.
Lee Jones7af29d42020-07-21 17:41:31 +010017397 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
James Smart2d7dbc42017-02-12 13:52:35 -080017398 *
17399 * This function creates a receive buffer queue pair , as detailed in @hrq and
17400 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
17401 * to the HBA.
17402 *
17403 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
17404 * struct is used to get the entry count that is necessary to determine the
17405 * number of pages to use for this queue. The @cq is used to indicate which
17406 * completion queue to bind received buffers that are posted to these queues to.
17407 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
17408 * receive queue pair. This function is asynchronous and will wait for the
17409 * mailbox command to finish before continuing.
17410 *
17411 * On success this function will return a zero. If unable to allocate enough
17412 * memory this function will return -ENOMEM. If the queue create mailbox command
17413 * fails this function will return -ENXIO.
17414 **/
17415int
17416lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
17417 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
17418 uint32_t subtype)
17419{
17420 struct lpfc_queue *hrq, *drq, *cq;
17421 struct lpfc_mbx_rq_create_v2 *rq_create;
17422 struct lpfc_dmabuf *dmabuf;
17423 LPFC_MBOXQ_t *mbox;
17424 int rc, length, alloclen, status = 0;
17425 int cnt, idx, numrq, page_idx = 0;
17426 uint32_t shdr_status, shdr_add_status;
17427 union lpfc_sli4_cfg_shdr *shdr;
17428 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
17429
17430 numrq = phba->cfg_nvmet_mrq;
17431 /* sanity check on array memory */
17432 if (!hrqp || !drqp || !cqp || !numrq)
17433 return -ENODEV;
17434 if (!phba->sli4_hba.pc_sli4_params.supported)
17435 hw_page_size = SLI4_PAGE_SIZE;
17436
17437 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17438 if (!mbox)
17439 return -ENOMEM;
17440
17441 length = sizeof(struct lpfc_mbx_rq_create_v2);
17442 length += ((2 * numrq * hrqp[0]->page_count) *
17443 sizeof(struct dma_address));
17444
17445 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17446 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
17447 LPFC_SLI4_MBX_NEMBED);
17448 if (alloclen < length) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017449 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080017450 "3099 Allocated DMA memory size (%d) is "
17451 "less than the requested DMA memory size "
17452 "(%d)\n", alloclen, length);
17453 status = -ENOMEM;
17454 goto out;
17455 }
17456
17457
17458
17459 rq_create = mbox->sge_array->addr[0];
17460 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
17461
17462 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
17463 cnt = 0;
17464
17465 for (idx = 0; idx < numrq; idx++) {
17466 hrq = hrqp[idx];
17467 drq = drqp[idx];
17468 cq = cqp[idx];
17469
James Smart2d7dbc42017-02-12 13:52:35 -080017470 /* sanity check on queue memory */
17471 if (!hrq || !drq || !cq) {
17472 status = -ENODEV;
17473 goto out;
17474 }
17475
James Smart7aabe842017-03-04 09:30:22 -080017476 if (hrq->entry_count != drq->entry_count) {
17477 status = -EINVAL;
17478 goto out;
17479 }
17480
James Smart2d7dbc42017-02-12 13:52:35 -080017481 if (idx == 0) {
17482 bf_set(lpfc_mbx_rq_create_num_pages,
17483 &rq_create->u.request,
17484 hrq->page_count);
17485 bf_set(lpfc_mbx_rq_create_rq_cnt,
17486 &rq_create->u.request, (numrq * 2));
17487 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
17488 1);
17489 bf_set(lpfc_rq_context_base_cq,
17490 &rq_create->u.request.context,
17491 cq->queue_id);
17492 bf_set(lpfc_rq_context_data_size,
17493 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070017494 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080017495 bf_set(lpfc_rq_context_hdr_size,
17496 &rq_create->u.request.context,
17497 LPFC_HDR_BUF_SIZE);
17498 bf_set(lpfc_rq_context_rqe_count_1,
17499 &rq_create->u.request.context,
17500 hrq->entry_count);
17501 bf_set(lpfc_rq_context_rqe_size,
17502 &rq_create->u.request.context,
17503 LPFC_RQE_SIZE_8);
17504 bf_set(lpfc_rq_context_page_size,
17505 &rq_create->u.request.context,
17506 (PAGE_SIZE/SLI4_PAGE_SIZE));
17507 }
17508 rc = 0;
17509 list_for_each_entry(dmabuf, &hrq->page_list, list) {
17510 memset(dmabuf->virt, 0, hw_page_size);
17511 cnt = page_idx + dmabuf->buffer_tag;
17512 rq_create->u.request.page[cnt].addr_lo =
17513 putPaddrLow(dmabuf->phys);
17514 rq_create->u.request.page[cnt].addr_hi =
17515 putPaddrHigh(dmabuf->phys);
17516 rc++;
17517 }
17518 page_idx += rc;
17519
17520 rc = 0;
17521 list_for_each_entry(dmabuf, &drq->page_list, list) {
17522 memset(dmabuf->virt, 0, hw_page_size);
17523 cnt = page_idx + dmabuf->buffer_tag;
17524 rq_create->u.request.page[cnt].addr_lo =
17525 putPaddrLow(dmabuf->phys);
17526 rq_create->u.request.page[cnt].addr_hi =
17527 putPaddrHigh(dmabuf->phys);
17528 rc++;
17529 }
17530 page_idx += rc;
17531
17532 hrq->db_format = LPFC_DB_RING_FORMAT;
17533 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17534 hrq->type = LPFC_HRQ;
17535 hrq->assoc_qid = cq->queue_id;
17536 hrq->subtype = subtype;
17537 hrq->host_index = 0;
17538 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080017539 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080017540
17541 drq->db_format = LPFC_DB_RING_FORMAT;
17542 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
17543 drq->type = LPFC_DRQ;
17544 drq->assoc_qid = cq->queue_id;
17545 drq->subtype = subtype;
17546 drq->host_index = 0;
17547 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080017548 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080017549
17550 list_add_tail(&hrq->list, &cq->child_list);
17551 list_add_tail(&drq->list, &cq->child_list);
17552 }
17553
17554 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17555 /* The IOCTL status is embedded in the mailbox subheader. */
17556 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17557 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17558 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017559 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080017560 "3120 RQ_CREATE mailbox failed with "
17561 "status x%x add_status x%x, mbx status x%x\n",
17562 shdr_status, shdr_add_status, rc);
17563 status = -ENXIO;
17564 goto out;
17565 }
17566 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
17567 if (rc == 0xFFFF) {
17568 status = -ENXIO;
17569 goto out;
17570 }
17571
17572 /* Initialize all RQs with associated queue id */
17573 for (idx = 0; idx < numrq; idx++) {
17574 hrq = hrqp[idx];
17575 hrq->queue_id = rc + (2 * idx);
17576 drq = drqp[idx];
17577 drq->queue_id = rc + (2 * idx) + 1;
17578 }
17579
17580out:
17581 lpfc_sli4_mbox_cmd_free(phba, mbox);
17582 return status;
17583}
17584
17585/**
James Smart4f774512009-05-22 14:52:35 -040017586 * lpfc_eq_destroy - Destroy an event Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010017587 * @phba: HBA structure that indicates port to destroy a queue on.
James Smart4f774512009-05-22 14:52:35 -040017588 * @eq: The queue structure associated with the queue to destroy.
17589 *
17590 * This function destroys a queue, as detailed in @eq by sending an mailbox
17591 * command, specific to the type of queue, to the HBA.
17592 *
17593 * The @eq struct is used to get the queue ID of the queue to destroy.
17594 *
17595 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040017596 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040017597 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040017598int
James Smart4f774512009-05-22 14:52:35 -040017599lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
17600{
17601 LPFC_MBOXQ_t *mbox;
17602 int rc, length, status = 0;
17603 uint32_t shdr_status, shdr_add_status;
17604 union lpfc_sli4_cfg_shdr *shdr;
17605
James Smart2e90f4b2011-12-13 13:22:37 -050017606 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040017607 if (!eq)
17608 return -ENODEV;
James Smart32517fc2019-01-28 11:14:33 -080017609
James Smart4f774512009-05-22 14:52:35 -040017610 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
17611 if (!mbox)
17612 return -ENOMEM;
17613 length = (sizeof(struct lpfc_mbx_eq_destroy) -
17614 sizeof(struct lpfc_sli4_cfg_mhdr));
17615 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17616 LPFC_MBOX_OPCODE_EQ_DESTROY,
17617 length, LPFC_SLI4_MBX_EMBED);
17618 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
17619 eq->queue_id);
17620 mbox->vport = eq->phba->pport;
17621 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17622
17623 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
17624 /* The IOCTL status is embedded in the mailbox subheader. */
17625 shdr = (union lpfc_sli4_cfg_shdr *)
17626 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
17627 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17628 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17629 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017630 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017631 "2505 EQ_DESTROY mailbox failed with "
17632 "status x%x add_status x%x, mbx status x%x\n",
17633 shdr_status, shdr_add_status, rc);
17634 status = -ENXIO;
17635 }
17636
17637 /* Remove eq from any list */
17638 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040017639 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017640 return status;
17641}
17642
17643/**
17644 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010017645 * @phba: HBA structure that indicates port to destroy a queue on.
James Smart4f774512009-05-22 14:52:35 -040017646 * @cq: The queue structure associated with the queue to destroy.
17647 *
17648 * This function destroys a queue, as detailed in @cq by sending an mailbox
17649 * command, specific to the type of queue, to the HBA.
17650 *
17651 * The @cq struct is used to get the queue ID of the queue to destroy.
17652 *
17653 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040017654 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040017655 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040017656int
James Smart4f774512009-05-22 14:52:35 -040017657lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
17658{
17659 LPFC_MBOXQ_t *mbox;
17660 int rc, length, status = 0;
17661 uint32_t shdr_status, shdr_add_status;
17662 union lpfc_sli4_cfg_shdr *shdr;
17663
James Smart2e90f4b2011-12-13 13:22:37 -050017664 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040017665 if (!cq)
17666 return -ENODEV;
17667 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
17668 if (!mbox)
17669 return -ENOMEM;
17670 length = (sizeof(struct lpfc_mbx_cq_destroy) -
17671 sizeof(struct lpfc_sli4_cfg_mhdr));
17672 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17673 LPFC_MBOX_OPCODE_CQ_DESTROY,
17674 length, LPFC_SLI4_MBX_EMBED);
17675 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
17676 cq->queue_id);
17677 mbox->vport = cq->phba->pport;
17678 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17679 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
17680 /* The IOCTL status is embedded in the mailbox subheader. */
17681 shdr = (union lpfc_sli4_cfg_shdr *)
17682 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
17683 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17684 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17685 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017686 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017687 "2506 CQ_DESTROY mailbox failed with "
17688 "status x%x add_status x%x, mbx status x%x\n",
17689 shdr_status, shdr_add_status, rc);
17690 status = -ENXIO;
17691 }
17692 /* Remove cq from any list */
17693 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040017694 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017695 return status;
17696}
17697
17698/**
James Smart04c68492009-05-22 14:52:52 -040017699 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010017700 * @phba: HBA structure that indicates port to destroy a queue on.
17701 * @mq: The queue structure associated with the queue to destroy.
James Smart04c68492009-05-22 14:52:52 -040017702 *
17703 * This function destroys a queue, as detailed in @mq by sending an mailbox
17704 * command, specific to the type of queue, to the HBA.
17705 *
17706 * The @mq struct is used to get the queue ID of the queue to destroy.
17707 *
17708 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040017709 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040017710 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040017711int
James Smart04c68492009-05-22 14:52:52 -040017712lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
17713{
17714 LPFC_MBOXQ_t *mbox;
17715 int rc, length, status = 0;
17716 uint32_t shdr_status, shdr_add_status;
17717 union lpfc_sli4_cfg_shdr *shdr;
17718
James Smart2e90f4b2011-12-13 13:22:37 -050017719 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040017720 if (!mq)
17721 return -ENODEV;
17722 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
17723 if (!mbox)
17724 return -ENOMEM;
17725 length = (sizeof(struct lpfc_mbx_mq_destroy) -
17726 sizeof(struct lpfc_sli4_cfg_mhdr));
17727 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17728 LPFC_MBOX_OPCODE_MQ_DESTROY,
17729 length, LPFC_SLI4_MBX_EMBED);
17730 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
17731 mq->queue_id);
17732 mbox->vport = mq->phba->pport;
17733 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17734 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
17735 /* The IOCTL status is embedded in the mailbox subheader. */
17736 shdr = (union lpfc_sli4_cfg_shdr *)
17737 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
17738 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17739 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17740 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017741 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart04c68492009-05-22 14:52:52 -040017742 "2507 MQ_DESTROY mailbox failed with "
17743 "status x%x add_status x%x, mbx status x%x\n",
17744 shdr_status, shdr_add_status, rc);
17745 status = -ENXIO;
17746 }
17747 /* Remove mq from any list */
17748 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040017749 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040017750 return status;
17751}
17752
17753/**
James Smart4f774512009-05-22 14:52:35 -040017754 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010017755 * @phba: HBA structure that indicates port to destroy a queue on.
James Smart4f774512009-05-22 14:52:35 -040017756 * @wq: The queue structure associated with the queue to destroy.
17757 *
17758 * This function destroys a queue, as detailed in @wq by sending an mailbox
17759 * command, specific to the type of queue, to the HBA.
17760 *
17761 * The @wq struct is used to get the queue ID of the queue to destroy.
17762 *
17763 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040017764 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040017765 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040017766int
James Smart4f774512009-05-22 14:52:35 -040017767lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
17768{
17769 LPFC_MBOXQ_t *mbox;
17770 int rc, length, status = 0;
17771 uint32_t shdr_status, shdr_add_status;
17772 union lpfc_sli4_cfg_shdr *shdr;
17773
James Smart2e90f4b2011-12-13 13:22:37 -050017774 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040017775 if (!wq)
17776 return -ENODEV;
17777 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
17778 if (!mbox)
17779 return -ENOMEM;
17780 length = (sizeof(struct lpfc_mbx_wq_destroy) -
17781 sizeof(struct lpfc_sli4_cfg_mhdr));
17782 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17783 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
17784 length, LPFC_SLI4_MBX_EMBED);
17785 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
17786 wq->queue_id);
17787 mbox->vport = wq->phba->pport;
17788 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17789 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
17790 shdr = (union lpfc_sli4_cfg_shdr *)
17791 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
17792 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17793 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17794 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017795 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017796 "2508 WQ_DESTROY mailbox failed with "
17797 "status x%x add_status x%x, mbx status x%x\n",
17798 shdr_status, shdr_add_status, rc);
17799 status = -ENXIO;
17800 }
17801 /* Remove wq from any list */
17802 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070017803 kfree(wq->pring);
17804 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040017805 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017806 return status;
17807}
17808
17809/**
17810 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010017811 * @phba: HBA structure that indicates port to destroy a queue on.
17812 * @hrq: The queue structure associated with the queue to destroy.
17813 * @drq: The queue structure associated with the queue to destroy.
James Smart4f774512009-05-22 14:52:35 -040017814 *
17815 * This function destroys a queue, as detailed in @rq by sending an mailbox
17816 * command, specific to the type of queue, to the HBA.
17817 *
17818 * The @rq struct is used to get the queue ID of the queue to destroy.
17819 *
17820 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040017821 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040017822 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040017823int
James Smart4f774512009-05-22 14:52:35 -040017824lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17825 struct lpfc_queue *drq)
17826{
17827 LPFC_MBOXQ_t *mbox;
17828 int rc, length, status = 0;
17829 uint32_t shdr_status, shdr_add_status;
17830 union lpfc_sli4_cfg_shdr *shdr;
17831
James Smart2e90f4b2011-12-13 13:22:37 -050017832 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040017833 if (!hrq || !drq)
17834 return -ENODEV;
17835 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
17836 if (!mbox)
17837 return -ENOMEM;
17838 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050017839 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040017840 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17841 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
17842 length, LPFC_SLI4_MBX_EMBED);
17843 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17844 hrq->queue_id);
17845 mbox->vport = hrq->phba->pport;
17846 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17847 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
17848 /* The IOCTL status is embedded in the mailbox subheader. */
17849 shdr = (union lpfc_sli4_cfg_shdr *)
17850 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17851 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17852 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17853 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017854 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017855 "2509 RQ_DESTROY mailbox failed with "
17856 "status x%x add_status x%x, mbx status x%x\n",
17857 shdr_status, shdr_add_status, rc);
James Smart304ee432021-04-11 18:31:17 -070017858 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017859 return -ENXIO;
17860 }
17861 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17862 drq->queue_id);
17863 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
17864 shdr = (union lpfc_sli4_cfg_shdr *)
17865 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17866 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17867 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17868 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017869 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017870 "2510 RQ_DESTROY mailbox failed with "
17871 "status x%x add_status x%x, mbx status x%x\n",
17872 shdr_status, shdr_add_status, rc);
17873 status = -ENXIO;
17874 }
17875 list_del_init(&hrq->list);
17876 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040017877 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017878 return status;
17879}
17880
17881/**
17882 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
17883 * @phba: The virtual port for which this call being executed.
17884 * @pdma_phys_addr0: Physical address of the 1st SGL page.
17885 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
17886 * @xritag: the xritag that ties this io to the SGL pages.
17887 *
17888 * This routine will post the sgl pages for the IO that has the xritag
17889 * that is in the iocbq structure. The xritag is assigned during iocbq
17890 * creation and persists for as long as the driver is loaded.
17891 * if the caller has fewer than 256 scatter gather segments to map then
17892 * pdma_phys_addr1 should be 0.
17893 * If the caller needs to map more than 256 scatter gather segment then
17894 * pdma_phys_addr1 should be a valid physical address.
17895 * physical address for SGLs must be 64 byte aligned.
17896 * If you are going to map 2 SGL's then the first one must have 256 entries
17897 * the second sgl can have between 1 and 256 entries.
17898 *
17899 * Return codes:
17900 * 0 - Success
17901 * -ENXIO, -ENOMEM - Failure
17902 **/
17903int
17904lpfc_sli4_post_sgl(struct lpfc_hba *phba,
17905 dma_addr_t pdma_phys_addr0,
17906 dma_addr_t pdma_phys_addr1,
17907 uint16_t xritag)
17908{
17909 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
17910 LPFC_MBOXQ_t *mbox;
17911 int rc;
17912 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040017913 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040017914 union lpfc_sli4_cfg_shdr *shdr;
17915
17916 if (xritag == NO_XRI) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017917 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017918 "0364 Invalid param:\n");
17919 return -EINVAL;
17920 }
17921
17922 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17923 if (!mbox)
17924 return -ENOMEM;
17925
17926 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17927 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17928 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050017929 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040017930
17931 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
17932 &mbox->u.mqe.un.post_sgl_pages;
17933 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
17934 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
17935
17936 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
17937 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
17938 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
17939 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
17940
17941 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
17942 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
17943 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
17944 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
17945 if (!phba->sli4_hba.intr_enable)
17946 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040017947 else {
James Smarta183a152011-10-10 21:32:43 -040017948 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040017949 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17950 }
James Smart4f774512009-05-22 14:52:35 -040017951 /* The IOCTL status is embedded in the mailbox subheader. */
17952 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
17953 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17954 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart304ee432021-04-11 18:31:17 -070017955 if (!phba->sli4_hba.intr_enable)
17956 mempool_free(mbox, phba->mbox_mem_pool);
17957 else if (rc != MBX_TIMEOUT)
James Smart4f774512009-05-22 14:52:35 -040017958 mempool_free(mbox, phba->mbox_mem_pool);
17959 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017960 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017961 "2511 POST_SGL mailbox failed with "
17962 "status x%x add_status x%x, mbx status x%x\n",
17963 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040017964 }
17965 return 0;
17966}
James Smart4f774512009-05-22 14:52:35 -040017967
17968/**
James Smart88a2cfb2011-07-22 18:36:33 -040017969 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040017970 * @phba: pointer to lpfc hba data structure.
17971 *
17972 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040017973 * HBA consistent with the SLI-4 interface spec. This routine
17974 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17975 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040017976 *
James Smart88a2cfb2011-07-22 18:36:33 -040017977 * Returns
17978 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
17979 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17980 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017981static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017982lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
17983{
17984 unsigned long xri;
17985
17986 /*
17987 * Fetch the next logical xri. Because this index is logical,
17988 * the driver starts at 0 each time.
17989 */
17990 spin_lock_irq(&phba->hbalock);
Yury Norovb5c7e7e2021-08-14 14:17:03 -070017991 xri = find_first_zero_bit(phba->sli4_hba.xri_bmask,
17992 phba->sli4_hba.max_cfg_param.max_xri);
James Smart6d368e52011-05-24 11:44:12 -040017993 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
17994 spin_unlock_irq(&phba->hbalock);
17995 return NO_XRI;
17996 } else {
17997 set_bit(xri, phba->sli4_hba.xri_bmask);
17998 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040017999 }
James Smart6d368e52011-05-24 11:44:12 -040018000 spin_unlock_irq(&phba->hbalock);
18001 return xri;
18002}
18003
18004/**
Lee Jones8514e2f2021-03-03 14:46:18 +000018005 * __lpfc_sli4_free_xri - Release an xri for reuse.
James Smart6d368e52011-05-24 11:44:12 -040018006 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010018007 * @xri: xri to release.
James Smart6d368e52011-05-24 11:44:12 -040018008 *
18009 * This routine is invoked to release an xri to the pool of
18010 * available rpis maintained by the driver.
18011 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018012static void
James Smart6d368e52011-05-24 11:44:12 -040018013__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18014{
18015 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040018016 phba->sli4_hba.max_cfg_param.xri_used--;
18017 }
18018}
18019
18020/**
18021 * lpfc_sli4_free_xri - Release an xri for reuse.
18022 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010018023 * @xri: xri to release.
James Smart6d368e52011-05-24 11:44:12 -040018024 *
18025 * This routine is invoked to release an xri to the pool of
18026 * available rpis maintained by the driver.
18027 **/
18028void
18029lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
18030{
18031 spin_lock_irq(&phba->hbalock);
18032 __lpfc_sli4_free_xri(phba, xri);
18033 spin_unlock_irq(&phba->hbalock);
18034}
18035
18036/**
James Smart4f774512009-05-22 14:52:35 -040018037 * lpfc_sli4_next_xritag - Get an xritag for the io
18038 * @phba: Pointer to HBA context object.
18039 *
18040 * This function gets an xritag for the iocb. If there is no unused xritag
18041 * it will return 0xffff.
18042 * The function returns the allocated xritag if successful, else returns zero.
18043 * Zero is not a valid xritag.
18044 * The caller is not required to hold any lock.
18045 **/
18046uint16_t
18047lpfc_sli4_next_xritag(struct lpfc_hba *phba)
18048{
James Smart6d368e52011-05-24 11:44:12 -040018049 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040018050
James Smart6d368e52011-05-24 11:44:12 -040018051 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040018052 if (xri_index == NO_XRI)
18053 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
18054 "2004 Failed to allocate XRI.last XRITAG is %d"
18055 " Max XRI is %d, Used XRI is %d\n",
18056 xri_index,
18057 phba->sli4_hba.max_cfg_param.max_xri,
18058 phba->sli4_hba.max_cfg_param.xri_used);
18059 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040018060}
18061
18062/**
James Smart895427b2017-02-12 13:52:30 -080018063 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040018064 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040018065 * @post_sgl_list: pointer to els sgl entry list.
Lee Jones7af29d42020-07-21 17:41:31 +010018066 * @post_cnt: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040018067 *
18068 * This routine is invoked to post a block of driver's sgl pages to the
18069 * HBA using non-embedded mailbox command. No Lock is held. This routine
18070 * is only called when the driver is loading and after all IO has been
18071 * stopped.
18072 **/
James Smart8a9d2e82012-05-09 21:16:12 -040018073static int
James Smart895427b2017-02-12 13:52:30 -080018074lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040018075 struct list_head *post_sgl_list,
18076 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040018077{
James Smart8a9d2e82012-05-09 21:16:12 -040018078 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040018079 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18080 struct sgl_page_pairs *sgl_pg_pairs;
18081 void *viraddr;
18082 LPFC_MBOXQ_t *mbox;
18083 uint32_t reqlen, alloclen, pg_pairs;
18084 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040018085 uint16_t xritag_start = 0;
18086 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040018087 uint32_t shdr_status, shdr_add_status;
18088 union lpfc_sli4_cfg_shdr *shdr;
18089
James Smart895427b2017-02-12 13:52:30 -080018090 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040018091 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040018092 if (reqlen > SLI4_PAGE_SIZE) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018093 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040018094 "2559 Block sgl registration required DMA "
18095 "size (%d) great than a page\n", reqlen);
18096 return -ENOMEM;
18097 }
James Smart895427b2017-02-12 13:52:30 -080018098
James Smart4f774512009-05-22 14:52:35 -040018099 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040018100 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040018101 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040018102
18103 /* Allocate DMA memory and set up the non-embedded mailbox command */
18104 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18105 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
18106 LPFC_SLI4_MBX_NEMBED);
18107
18108 if (alloclen < reqlen) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018109 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040018110 "0285 Allocated DMA memory size (%d) is "
18111 "less than the requested DMA memory "
18112 "size (%d)\n", alloclen, reqlen);
18113 lpfc_sli4_mbox_cmd_free(phba, mbox);
18114 return -ENOMEM;
18115 }
James Smart4f774512009-05-22 14:52:35 -040018116 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040018117 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040018118 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18119 sgl_pg_pairs = &sgl->sgl_pg_pairs;
18120
James Smart8a9d2e82012-05-09 21:16:12 -040018121 pg_pairs = 0;
18122 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040018123 /* Set up the sge entry */
18124 sgl_pg_pairs->sgl_pg0_addr_lo =
18125 cpu_to_le32(putPaddrLow(sglq_entry->phys));
18126 sgl_pg_pairs->sgl_pg0_addr_hi =
18127 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
18128 sgl_pg_pairs->sgl_pg1_addr_lo =
18129 cpu_to_le32(putPaddrLow(0));
18130 sgl_pg_pairs->sgl_pg1_addr_hi =
18131 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040018132
James Smart4f774512009-05-22 14:52:35 -040018133 /* Keep the first xritag on the list */
18134 if (pg_pairs == 0)
18135 xritag_start = sglq_entry->sli4_xritag;
18136 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040018137 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040018138 }
James Smart6d368e52011-05-24 11:44:12 -040018139
18140 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040018141 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080018142 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040018143 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080018144
James Smart4f774512009-05-22 14:52:35 -040018145 if (!phba->sli4_hba.intr_enable)
18146 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18147 else {
James Smarta183a152011-10-10 21:32:43 -040018148 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040018149 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18150 }
18151 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
18152 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18153 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart304ee432021-04-11 18:31:17 -070018154 if (!phba->sli4_hba.intr_enable)
18155 lpfc_sli4_mbox_cmd_free(phba, mbox);
18156 else if (rc != MBX_TIMEOUT)
James Smart4f774512009-05-22 14:52:35 -040018157 lpfc_sli4_mbox_cmd_free(phba, mbox);
18158 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018159 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040018160 "2513 POST_SGL_BLOCK mailbox command failed "
18161 "status x%x add_status x%x mbx status x%x\n",
18162 shdr_status, shdr_add_status, rc);
18163 rc = -ENXIO;
18164 }
18165 return rc;
18166}
18167
18168/**
James Smart5e5b5112019-01-28 11:14:22 -080018169 * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
James Smart4f774512009-05-22 14:52:35 -040018170 * @phba: pointer to lpfc hba data structure.
James Smart0794d602019-01-28 11:14:19 -080018171 * @nblist: pointer to nvme buffer list.
James Smart4f774512009-05-22 14:52:35 -040018172 * @count: number of scsi buffers on the list.
18173 *
18174 * This routine is invoked to post a block of @count scsi sgl pages from a
James Smart0794d602019-01-28 11:14:19 -080018175 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
James Smart4f774512009-05-22 14:52:35 -040018176 * No Lock is held.
18177 *
18178 **/
James Smart0794d602019-01-28 11:14:19 -080018179static int
James Smart5e5b5112019-01-28 11:14:22 -080018180lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
18181 int count)
James Smart4f774512009-05-22 14:52:35 -040018182{
James Smartc4908502019-01-28 11:14:28 -080018183 struct lpfc_io_buf *lpfc_ncmd;
James Smart4f774512009-05-22 14:52:35 -040018184 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
18185 struct sgl_page_pairs *sgl_pg_pairs;
18186 void *viraddr;
18187 LPFC_MBOXQ_t *mbox;
18188 uint32_t reqlen, alloclen, pg_pairs;
18189 uint32_t mbox_tmo;
18190 uint16_t xritag_start = 0;
18191 int rc = 0;
18192 uint32_t shdr_status, shdr_add_status;
18193 dma_addr_t pdma_phys_bpl1;
18194 union lpfc_sli4_cfg_shdr *shdr;
18195
18196 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040018197 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040018198 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040018199 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040018200 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080018201 "6118 Block sgl registration required DMA "
James Smart4f774512009-05-22 14:52:35 -040018202 "size (%d) great than a page\n", reqlen);
18203 return -ENOMEM;
18204 }
18205 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18206 if (!mbox) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018207 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -080018208 "6119 Failed to allocate mbox cmd memory\n");
James Smart4f774512009-05-22 14:52:35 -040018209 return -ENOMEM;
18210 }
18211
18212 /* Allocate DMA memory and set up the non-embedded mailbox command */
18213 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
James Smart0794d602019-01-28 11:14:19 -080018214 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
18215 reqlen, LPFC_SLI4_MBX_NEMBED);
James Smart4f774512009-05-22 14:52:35 -040018216
18217 if (alloclen < reqlen) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018218 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -080018219 "6120 Allocated DMA memory size (%d) is "
James Smart4f774512009-05-22 14:52:35 -040018220 "less than the requested DMA memory "
18221 "size (%d)\n", alloclen, reqlen);
18222 lpfc_sli4_mbox_cmd_free(phba, mbox);
18223 return -ENOMEM;
18224 }
James Smart6d368e52011-05-24 11:44:12 -040018225
James Smart4f774512009-05-22 14:52:35 -040018226 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040018227 viraddr = mbox->sge_array->addr[0];
18228
18229 /* Set up the SGL pages in the non-embedded DMA pages */
18230 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
18231 sgl_pg_pairs = &sgl->sgl_pg_pairs;
18232
18233 pg_pairs = 0;
James Smart0794d602019-01-28 11:14:19 -080018234 list_for_each_entry(lpfc_ncmd, nblist, list) {
James Smart4f774512009-05-22 14:52:35 -040018235 /* Set up the sge entry */
18236 sgl_pg_pairs->sgl_pg0_addr_lo =
James Smart0794d602019-01-28 11:14:19 -080018237 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040018238 sgl_pg_pairs->sgl_pg0_addr_hi =
James Smart0794d602019-01-28 11:14:19 -080018239 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040018240 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
James Smart0794d602019-01-28 11:14:19 -080018241 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
18242 SGL_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040018243 else
18244 pdma_phys_bpl1 = 0;
18245 sgl_pg_pairs->sgl_pg1_addr_lo =
18246 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
18247 sgl_pg_pairs->sgl_pg1_addr_hi =
18248 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
18249 /* Keep the first xritag on the list */
18250 if (pg_pairs == 0)
James Smart0794d602019-01-28 11:14:19 -080018251 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040018252 sgl_pg_pairs++;
18253 pg_pairs++;
18254 }
18255 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
18256 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
18257 /* Perform endian conversion if necessary */
18258 sgl->word0 = cpu_to_le32(sgl->word0);
18259
James Smart0794d602019-01-28 11:14:19 -080018260 if (!phba->sli4_hba.intr_enable) {
James Smart4f774512009-05-22 14:52:35 -040018261 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart0794d602019-01-28 11:14:19 -080018262 } else {
James Smarta183a152011-10-10 21:32:43 -040018263 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040018264 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18265 }
James Smart0794d602019-01-28 11:14:19 -080018266 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040018267 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18268 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart304ee432021-04-11 18:31:17 -070018269 if (!phba->sli4_hba.intr_enable)
18270 lpfc_sli4_mbox_cmd_free(phba, mbox);
18271 else if (rc != MBX_TIMEOUT)
James Smart4f774512009-05-22 14:52:35 -040018272 lpfc_sli4_mbox_cmd_free(phba, mbox);
18273 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018274 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -080018275 "6125 POST_SGL_BLOCK mailbox command failed "
James Smart4f774512009-05-22 14:52:35 -040018276 "status x%x add_status x%x mbx status x%x\n",
18277 shdr_status, shdr_add_status, rc);
18278 rc = -ENXIO;
18279 }
18280 return rc;
18281}
18282
18283/**
James Smart5e5b5112019-01-28 11:14:22 -080018284 * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
James Smart0794d602019-01-28 11:14:19 -080018285 * @phba: pointer to lpfc hba data structure.
18286 * @post_nblist: pointer to the nvme buffer list.
Lee Jones7af29d42020-07-21 17:41:31 +010018287 * @sb_count: number of nvme buffers.
James Smart0794d602019-01-28 11:14:19 -080018288 *
18289 * This routine walks a list of nvme buffers that was passed in. It attempts
18290 * to construct blocks of nvme buffer sgls which contains contiguous xris and
18291 * uses the non-embedded SGL block post mailbox commands to post to the port.
18292 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
18293 * embedded SGL post mailbox command for posting. The @post_nblist passed in
18294 * must be local list, thus no lock is needed when manipulate the list.
18295 *
18296 * Returns: 0 = failure, non-zero number of successfully posted buffers.
18297 **/
18298int
James Smart5e5b5112019-01-28 11:14:22 -080018299lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
18300 struct list_head *post_nblist, int sb_count)
James Smart0794d602019-01-28 11:14:19 -080018301{
James Smartc4908502019-01-28 11:14:28 -080018302 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart0794d602019-01-28 11:14:19 -080018303 int status, sgl_size;
18304 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
18305 dma_addr_t pdma_phys_sgl1;
18306 int last_xritag = NO_XRI;
18307 int cur_xritag;
James Smart0794d602019-01-28 11:14:19 -080018308 LIST_HEAD(prep_nblist);
18309 LIST_HEAD(blck_nblist);
18310 LIST_HEAD(nvme_nblist);
18311
18312 /* sanity check */
18313 if (sb_count <= 0)
18314 return -EINVAL;
18315
18316 sgl_size = phba->cfg_sg_dma_buf_size;
18317 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
18318 list_del_init(&lpfc_ncmd->list);
18319 block_cnt++;
18320 if ((last_xritag != NO_XRI) &&
18321 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
18322 /* a hole in xri block, form a sgl posting block */
18323 list_splice_init(&prep_nblist, &blck_nblist);
18324 post_cnt = block_cnt - 1;
18325 /* prepare list for next posting block */
18326 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18327 block_cnt = 1;
18328 } else {
18329 /* prepare list for next posting block */
18330 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
18331 /* enough sgls for non-embed sgl mbox command */
18332 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
18333 list_splice_init(&prep_nblist, &blck_nblist);
18334 post_cnt = block_cnt;
18335 block_cnt = 0;
18336 }
18337 }
18338 num_posting++;
18339 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18340
18341 /* end of repost sgl list condition for NVME buffers */
18342 if (num_posting == sb_count) {
18343 if (post_cnt == 0) {
18344 /* last sgl posting block */
18345 list_splice_init(&prep_nblist, &blck_nblist);
18346 post_cnt = block_cnt;
18347 } else if (block_cnt == 1) {
18348 /* last single sgl with non-contiguous xri */
18349 if (sgl_size > SGL_PAGE_SIZE)
18350 pdma_phys_sgl1 =
18351 lpfc_ncmd->dma_phys_sgl +
18352 SGL_PAGE_SIZE;
18353 else
18354 pdma_phys_sgl1 = 0;
18355 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
18356 status = lpfc_sli4_post_sgl(
18357 phba, lpfc_ncmd->dma_phys_sgl,
18358 pdma_phys_sgl1, cur_xritag);
18359 if (status) {
James Smartc4908502019-01-28 11:14:28 -080018360 /* Post error. Buffer unavailable. */
18361 lpfc_ncmd->flags |=
18362 LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080018363 } else {
James Smartc4908502019-01-28 11:14:28 -080018364 /* Post success. Bffer available. */
18365 lpfc_ncmd->flags &=
18366 ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080018367 lpfc_ncmd->status = IOSTAT_SUCCESS;
18368 num_posted++;
18369 }
18370 /* success, put on NVME buffer sgl list */
18371 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18372 }
18373 }
18374
18375 /* continue until a nembed page worth of sgls */
18376 if (post_cnt == 0)
18377 continue;
18378
18379 /* post block of NVME buffer list sgls */
James Smart5e5b5112019-01-28 11:14:22 -080018380 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
18381 post_cnt);
James Smart0794d602019-01-28 11:14:19 -080018382
18383 /* don't reset xirtag due to hole in xri block */
18384 if (block_cnt == 0)
18385 last_xritag = NO_XRI;
18386
18387 /* reset NVME buffer post count for next round of posting */
18388 post_cnt = 0;
18389
18390 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
18391 while (!list_empty(&blck_nblist)) {
18392 list_remove_head(&blck_nblist, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -080018393 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -080018394 if (status) {
James Smartc4908502019-01-28 11:14:28 -080018395 /* Post error. Mark buffer unavailable. */
18396 lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080018397 } else {
James Smartc4908502019-01-28 11:14:28 -080018398 /* Post success, Mark buffer available. */
18399 lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080018400 lpfc_ncmd->status = IOSTAT_SUCCESS;
18401 num_posted++;
18402 }
18403 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
18404 }
18405 }
18406 /* Push NVME buffers with sgl posted to the available list */
James Smart5e5b5112019-01-28 11:14:22 -080018407 lpfc_io_buf_replenish(phba, &nvme_nblist);
18408
James Smart0794d602019-01-28 11:14:19 -080018409 return num_posted;
18410}
18411
18412/**
James Smart4f774512009-05-22 14:52:35 -040018413 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
18414 * @phba: pointer to lpfc_hba struct that the frame was received on
18415 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18416 *
18417 * This function checks the fields in the @fc_hdr to see if the FC frame is a
18418 * valid type of frame that the LPFC driver will handle. This function will
18419 * return a zero if the frame is a valid frame or a non zero value when the
18420 * frame does not pass the check.
18421 **/
18422static int
18423lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
18424{
Tomas Henzl474ffb72010-12-22 16:52:40 +010018425 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040018426 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050018427 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040018428
James Smarte62245d2019-08-14 16:57:08 -070018429#define FC_RCTL_MDS_DIAGS 0xF4
18430
James Smart4f774512009-05-22 14:52:35 -040018431 switch (fc_hdr->fh_r_ctl) {
18432 case FC_RCTL_DD_UNCAT: /* uncategorized information */
18433 case FC_RCTL_DD_SOL_DATA: /* solicited data */
18434 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
18435 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
18436 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
18437 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
18438 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
18439 case FC_RCTL_DD_CMD_STATUS: /* command status */
18440 case FC_RCTL_ELS_REQ: /* extended link services request */
18441 case FC_RCTL_ELS_REP: /* extended link services reply */
18442 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
18443 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
18444 case FC_RCTL_BA_NOP: /* basic link service NOP */
18445 case FC_RCTL_BA_ABTS: /* basic link service abort */
18446 case FC_RCTL_BA_RMC: /* remove connection */
18447 case FC_RCTL_BA_ACC: /* basic accept */
18448 case FC_RCTL_BA_RJT: /* basic reject */
18449 case FC_RCTL_BA_PRMT:
18450 case FC_RCTL_ACK_1: /* acknowledge_1 */
18451 case FC_RCTL_ACK_0: /* acknowledge_0 */
18452 case FC_RCTL_P_RJT: /* port reject */
18453 case FC_RCTL_F_RJT: /* fabric reject */
18454 case FC_RCTL_P_BSY: /* port busy */
18455 case FC_RCTL_F_BSY: /* fabric busy to data frame */
18456 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
18457 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070018458 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040018459 case FC_RCTL_END: /* end */
18460 break;
18461 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
18462 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18463 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
18464 return lpfc_fc_frame_check(phba, fc_hdr);
18465 default:
18466 goto drop;
18467 }
James Smartae9e28f2017-05-15 15:20:51 -070018468
James Smart4f774512009-05-22 14:52:35 -040018469 switch (fc_hdr->fh_type) {
18470 case FC_TYPE_BLS:
18471 case FC_TYPE_ELS:
18472 case FC_TYPE_FCP:
18473 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080018474 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040018475 break;
18476 case FC_TYPE_IP:
18477 case FC_TYPE_ILS:
18478 default:
18479 goto drop;
18480 }
James Smart546fc852011-03-11 16:06:29 -050018481
James Smart4f774512009-05-22 14:52:35 -040018482 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070018483 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040018484 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070018485 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
18486 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
18487 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
18488 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
18489 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040018490 return 0;
18491drop:
18492 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070018493 "2539 Dropped frame rctl:x%x type:x%x\n",
18494 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040018495 return 1;
18496}
18497
18498/**
18499 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
18500 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18501 *
18502 * This function processes the FC header to retrieve the VFI from the VF
18503 * header, if one exists. This function will return the VFI if one exists
18504 * or 0 if no VSAN Header exists.
18505 **/
18506static uint32_t
18507lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
18508{
18509 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
18510
18511 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
18512 return 0;
18513 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
18514}
18515
18516/**
18517 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
18518 * @phba: Pointer to the HBA structure to search for the vport on
18519 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
18520 * @fcfi: The FC Fabric ID that the frame came from
Lee Jones7af29d42020-07-21 17:41:31 +010018521 * @did: Destination ID to match against
James Smart4f774512009-05-22 14:52:35 -040018522 *
18523 * This function searches the @phba for a vport that matches the content of the
18524 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
18525 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
18526 * returns the matching vport pointer or NULL if unable to match frame to a
18527 * vport.
18528 **/
18529static struct lpfc_vport *
18530lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080018531 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040018532{
18533 struct lpfc_vport **vports;
18534 struct lpfc_vport *vport = NULL;
18535 int i;
James Smart939723a2012-05-09 21:19:03 -040018536
James Smartbf086112011-08-21 21:48:13 -040018537 if (did == Fabric_DID)
18538 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040018539 if ((phba->pport->fc_flag & FC_PT2PT) &&
18540 !(phba->link_state == LPFC_HBA_READY))
18541 return phba->pport;
18542
James Smart4f774512009-05-22 14:52:35 -040018543 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080018544 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040018545 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
18546 if (phba->fcf.fcfi == fcfi &&
18547 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
18548 vports[i]->fc_myDID == did) {
18549 vport = vports[i];
18550 break;
18551 }
18552 }
James Smart895427b2017-02-12 13:52:30 -080018553 }
James Smart4f774512009-05-22 14:52:35 -040018554 lpfc_destroy_vport_work_array(phba, vports);
18555 return vport;
18556}
18557
18558/**
James Smart45ed1192009-10-02 15:17:02 -040018559 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
18560 * @vport: The vport to work on.
18561 *
18562 * This function updates the receive sequence time stamp for this vport. The
18563 * receive sequence time stamp indicates the time that the last frame of the
18564 * the sequence that has been idle for the longest amount of time was received.
18565 * the driver uses this time stamp to indicate if any received sequences have
18566 * timed out.
18567 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018568static void
James Smart45ed1192009-10-02 15:17:02 -040018569lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
18570{
18571 struct lpfc_dmabuf *h_buf;
18572 struct hbq_dmabuf *dmabuf = NULL;
18573
18574 /* get the oldest sequence on the rcv list */
18575 h_buf = list_get_first(&vport->rcv_buffer_list,
18576 struct lpfc_dmabuf, list);
18577 if (!h_buf)
18578 return;
18579 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18580 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
18581}
18582
18583/**
18584 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
18585 * @vport: The vport that the received sequences were sent to.
18586 *
18587 * This function cleans up all outstanding received sequences. This is called
18588 * by the driver when a link event or user action invalidates all the received
18589 * sequences.
18590 **/
18591void
18592lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
18593{
18594 struct lpfc_dmabuf *h_buf, *hnext;
18595 struct lpfc_dmabuf *d_buf, *dnext;
18596 struct hbq_dmabuf *dmabuf = NULL;
18597
18598 /* start with the oldest sequence on the rcv list */
18599 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18600 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18601 list_del_init(&dmabuf->hbuf.list);
18602 list_for_each_entry_safe(d_buf, dnext,
18603 &dmabuf->dbuf.list, list) {
18604 list_del_init(&d_buf->list);
18605 lpfc_in_buf_free(vport->phba, d_buf);
18606 }
18607 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18608 }
18609}
18610
18611/**
18612 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
18613 * @vport: The vport that the received sequences were sent to.
18614 *
18615 * This function determines whether any received sequences have timed out by
18616 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
18617 * indicates that there is at least one timed out sequence this routine will
18618 * go through the received sequences one at a time from most inactive to most
18619 * active to determine which ones need to be cleaned up. Once it has determined
18620 * that a sequence needs to be cleaned up it will simply free up the resources
18621 * without sending an abort.
18622 **/
18623void
18624lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
18625{
18626 struct lpfc_dmabuf *h_buf, *hnext;
18627 struct lpfc_dmabuf *d_buf, *dnext;
18628 struct hbq_dmabuf *dmabuf = NULL;
18629 unsigned long timeout;
18630 int abort_count = 0;
18631
18632 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18633 vport->rcv_buffer_time_stamp);
18634 if (list_empty(&vport->rcv_buffer_list) ||
18635 time_before(jiffies, timeout))
18636 return;
18637 /* start with the oldest sequence on the rcv list */
18638 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
18639 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18640 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
18641 dmabuf->time_stamp);
18642 if (time_before(jiffies, timeout))
18643 break;
18644 abort_count++;
18645 list_del_init(&dmabuf->hbuf.list);
18646 list_for_each_entry_safe(d_buf, dnext,
18647 &dmabuf->dbuf.list, list) {
18648 list_del_init(&d_buf->list);
18649 lpfc_in_buf_free(vport->phba, d_buf);
18650 }
18651 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
18652 }
18653 if (abort_count)
18654 lpfc_update_rcv_time_stamp(vport);
18655}
18656
18657/**
James Smart4f774512009-05-22 14:52:35 -040018658 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
Lee Jones7af29d42020-07-21 17:41:31 +010018659 * @vport: pointer to a vitural port
James Smart4f774512009-05-22 14:52:35 -040018660 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
18661 *
18662 * This function searches through the existing incomplete sequences that have
18663 * been sent to this @vport. If the frame matches one of the incomplete
18664 * sequences then the dbuf in the @dmabuf is added to the list of frames that
18665 * make up that sequence. If no sequence is found that matches this frame then
18666 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
18667 * This function returns a pointer to the first dmabuf in the sequence list that
18668 * the frame was linked to.
18669 **/
18670static struct hbq_dmabuf *
18671lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18672{
18673 struct fc_frame_header *new_hdr;
18674 struct fc_frame_header *temp_hdr;
18675 struct lpfc_dmabuf *d_buf;
18676 struct lpfc_dmabuf *h_buf;
18677 struct hbq_dmabuf *seq_dmabuf = NULL;
18678 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050018679 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040018680
James Smart4d9ab992009-10-02 15:16:39 -040018681 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040018682 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040018683 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050018684
James Smart4f774512009-05-22 14:52:35 -040018685 /* Use the hdr_buf to find the sequence that this frame belongs to */
18686 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18687 temp_hdr = (struct fc_frame_header *)h_buf->virt;
18688 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18689 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18690 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18691 continue;
18692 /* found a pending sequence that matches this frame */
18693 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18694 break;
18695 }
18696 if (!seq_dmabuf) {
18697 /*
18698 * This indicates first frame received for this sequence.
18699 * Queue the buffer on the vport's rcv_buffer_list.
18700 */
18701 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040018702 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040018703 return dmabuf;
18704 }
18705 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050018706 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
18707 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040018708 list_del_init(&seq_dmabuf->hbuf.list);
18709 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
18710 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040018711 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040018712 return dmabuf;
18713 }
James Smart45ed1192009-10-02 15:17:02 -040018714 /* move this sequence to the tail to indicate a young sequence */
18715 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
18716 seq_dmabuf->time_stamp = jiffies;
18717 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050018718 if (list_empty(&seq_dmabuf->dbuf.list)) {
James Smarteeead812009-12-21 17:01:23 -050018719 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
18720 return seq_dmabuf;
18721 }
James Smart4f774512009-05-22 14:52:35 -040018722 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050018723 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
18724 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040018725 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18726 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
18727 /*
18728 * If the frame's sequence count is greater than the frame on
18729 * the list then insert the frame right after this frame
18730 */
James Smarteeead812009-12-21 17:01:23 -050018731 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
18732 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040018733 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050018734 found = 1;
18735 break;
James Smart4f774512009-05-22 14:52:35 -040018736 }
James Smart4360ca92015-12-16 18:12:04 -050018737
18738 if (&d_buf->list == &seq_dmabuf->dbuf.list)
18739 break;
18740 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040018741 }
James Smart4360ca92015-12-16 18:12:04 -050018742
18743 if (found)
18744 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040018745 return NULL;
18746}
18747
18748/**
James Smart6669f9b2009-10-02 15:16:45 -040018749 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
18750 * @vport: pointer to a vitural port
18751 * @dmabuf: pointer to a dmabuf that describes the FC sequence
18752 *
18753 * This function tries to abort from the partially assembed sequence, described
18754 * by the information from basic abbort @dmabuf. It checks to see whether such
18755 * partially assembled sequence held by the driver. If so, it shall free up all
18756 * the frames from the partially assembled sequence.
18757 *
18758 * Return
18759 * true -- if there is matching partially assembled sequence present and all
18760 * the frames freed with the sequence;
18761 * false -- if there is no matching partially assembled sequence present so
18762 * nothing got aborted in the lower layer driver
18763 **/
18764static bool
18765lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
18766 struct hbq_dmabuf *dmabuf)
18767{
18768 struct fc_frame_header *new_hdr;
18769 struct fc_frame_header *temp_hdr;
18770 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
18771 struct hbq_dmabuf *seq_dmabuf = NULL;
18772
18773 /* Use the hdr_buf to find the sequence that matches this frame */
18774 INIT_LIST_HEAD(&dmabuf->dbuf.list);
18775 INIT_LIST_HEAD(&dmabuf->hbuf.list);
18776 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18777 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
18778 temp_hdr = (struct fc_frame_header *)h_buf->virt;
18779 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18780 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18781 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18782 continue;
18783 /* found a pending sequence that matches this frame */
18784 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18785 break;
18786 }
18787
18788 /* Free up all the frames from the partially assembled sequence */
18789 if (seq_dmabuf) {
18790 list_for_each_entry_safe(d_buf, n_buf,
18791 &seq_dmabuf->dbuf.list, list) {
18792 list_del_init(&d_buf->list);
18793 lpfc_in_buf_free(vport->phba, d_buf);
18794 }
18795 return true;
18796 }
18797 return false;
18798}
18799
18800/**
James Smart6dd9e312013-01-03 15:43:37 -050018801 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
18802 * @vport: pointer to a vitural port
18803 * @dmabuf: pointer to a dmabuf that describes the FC sequence
18804 *
18805 * This function tries to abort from the assembed sequence from upper level
18806 * protocol, described by the information from basic abbort @dmabuf. It
18807 * checks to see whether such pending context exists at upper level protocol.
18808 * If so, it shall clean up the pending context.
18809 *
18810 * Return
18811 * true -- if there is matching pending context of the sequence cleaned
18812 * at ulp;
18813 * false -- if there is no matching pending context of the sequence present
18814 * at ulp.
18815 **/
18816static bool
18817lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18818{
18819 struct lpfc_hba *phba = vport->phba;
18820 int handled;
18821
18822 /* Accepting abort at ulp with SLI4 only */
18823 if (phba->sli_rev < LPFC_SLI_REV4)
18824 return false;
18825
18826 /* Register all caring upper level protocols to attend abort */
18827 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
18828 if (handled)
18829 return true;
18830
18831 return false;
18832}
18833
18834/**
James Smart546fc852011-03-11 16:06:29 -050018835 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040018836 * @phba: Pointer to HBA context object.
18837 * @cmd_iocbq: pointer to the command iocbq structure.
18838 * @rsp_iocbq: pointer to the response iocbq structure.
18839 *
James Smart546fc852011-03-11 16:06:29 -050018840 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040018841 * event. It properly releases the memory allocated to the sequence abort
18842 * accept iocb.
18843 **/
18844static void
James Smart546fc852011-03-11 16:06:29 -050018845lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040018846 struct lpfc_iocbq *cmd_iocbq,
18847 struct lpfc_iocbq *rsp_iocbq)
18848{
James Smart6dd9e312013-01-03 15:43:37 -050018849 struct lpfc_nodelist *ndlp;
18850
18851 if (cmd_iocbq) {
18852 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
18853 lpfc_nlp_put(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040018854 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050018855 }
James Smart6b5151f2012-01-18 16:24:06 -050018856
18857 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
18858 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
Dick Kennedy372c1872020-06-30 14:50:00 -070018859 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6b5151f2012-01-18 16:24:06 -050018860 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
18861 rsp_iocbq->iocb.ulpStatus,
18862 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040018863}
18864
18865/**
James Smart6d368e52011-05-24 11:44:12 -040018866 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
18867 * @phba: Pointer to HBA context object.
18868 * @xri: xri id in transaction.
18869 *
18870 * This function validates the xri maps to the known range of XRIs allocated an
18871 * used by the driver.
18872 **/
James Smart7851fe22011-07-22 18:36:52 -040018873uint16_t
James Smart6d368e52011-05-24 11:44:12 -040018874lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
18875 uint16_t xri)
18876{
James Smarta2fc4aef2014-09-03 12:57:55 -040018877 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040018878
18879 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
18880 if (xri == phba->sli4_hba.xri_ids[i])
18881 return i;
18882 }
18883 return NO_XRI;
18884}
18885
James Smart6d368e52011-05-24 11:44:12 -040018886/**
James Smart546fc852011-03-11 16:06:29 -050018887 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart3bfab8a2021-04-11 18:31:23 -070018888 * @vport: pointer to a virtual port.
James Smart6669f9b2009-10-02 15:16:45 -040018889 * @fc_hdr: pointer to a FC frame header.
Lee Jones7af29d42020-07-21 17:41:31 +010018890 * @aborted: was the partially assembled receive sequence successfully aborted
James Smart6669f9b2009-10-02 15:16:45 -040018891 *
James Smart546fc852011-03-11 16:06:29 -050018892 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040018893 * event after aborting the sequence handling.
18894 **/
James Smart86c67372017-04-21 16:05:04 -070018895void
James Smart6dd9e312013-01-03 15:43:37 -050018896lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
18897 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040018898{
James Smart6dd9e312013-01-03 15:43:37 -050018899 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040018900 struct lpfc_iocbq *ctiocb = NULL;
18901 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040018902 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050018903 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040018904 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050018905 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040018906
18907 if (!lpfc_is_link_up(phba))
18908 return;
18909
18910 sid = sli4_sid_from_fc_hdr(fc_hdr);
18911 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050018912 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040018913
James Smart6dd9e312013-01-03 15:43:37 -050018914 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040018915 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070018916 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050018917 if (!ndlp) {
18918 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
18919 "1268 Failed to allocate ndlp for "
18920 "oxid:x%x SID:x%x\n", oxid, sid);
18921 return;
18922 }
James Smart6dd9e312013-01-03 15:43:37 -050018923 /* Put ndlp onto pport node list */
18924 lpfc_enqueue_node(vport, ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040018925 }
18926
James Smart546fc852011-03-11 16:06:29 -050018927 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040018928 ctiocb = lpfc_sli_get_iocbq(phba);
18929 if (!ctiocb)
18930 return;
18931
James Smart5ffc2662009-11-18 15:39:44 -050018932 /* Extract the F_CTL field from FC_HDR */
18933 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
18934
James Smart6669f9b2009-10-02 15:16:45 -040018935 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040018936 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050018937 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040018938 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
18939 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
18940 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
18941
18942 /* Fill in the rest of iocb fields */
18943 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
18944 icmd->ulpBdeCount = 0;
18945 icmd->ulpLe = 1;
18946 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040018947 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050018948 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart4430f7f2020-11-15 11:26:31 -080018949 if (!ctiocb->context1) {
18950 lpfc_sli_release_iocbq(phba, ctiocb);
18951 return;
18952 }
James Smart6669f9b2009-10-02 15:16:45 -040018953
James Smart6669f9b2009-10-02 15:16:45 -040018954 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050018955 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040018956 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050018957 ctiocb->sli4_xritag = NO_XRI;
18958
James Smartee0f4fe2012-05-09 21:19:14 -040018959 if (fctl & FC_FC_EX_CTX)
18960 /* Exchange responder sent the abort so we
18961 * own the oxid.
18962 */
18963 xri = oxid;
18964 else
18965 xri = rxid;
18966 lxri = lpfc_sli4_xri_inrange(phba, xri);
18967 if (lxri != NO_XRI)
18968 lpfc_set_rrq_active(phba, ndlp, lxri,
18969 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050018970 /* For BA_ABTS from exchange responder, if the logical xri with
18971 * the oxid maps to the FCP XRI range, the port no longer has
18972 * that exchange context, send a BLS_RJT. Override the IOCB for
18973 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050018974 */
James Smart6dd9e312013-01-03 15:43:37 -050018975 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080018976 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050018977 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
18978 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
18979 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
18980 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
18981 }
18982
18983 /* If BA_ABTS failed to abort a partially assembled receive sequence,
18984 * the driver no longer has that exchange, send a BLS_RJT. Override
18985 * the IOCB for a BA_RJT.
18986 */
18987 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050018988 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
18989 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
18990 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
18991 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
18992 }
James Smart6669f9b2009-10-02 15:16:45 -040018993
James Smart5ffc2662009-11-18 15:39:44 -050018994 if (fctl & FC_FC_EX_CTX) {
18995 /* ABTS sent by responder to CT exchange, construction
18996 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
18997 * field and RX_ID from ABTS for RX_ID field.
18998 */
James Smart546fc852011-03-11 16:06:29 -050018999 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050019000 } else {
19001 /* ABTS sent by initiator to CT exchange, construction
19002 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050019003 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050019004 */
James Smart546fc852011-03-11 16:06:29 -050019005 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050019006 }
James Smartf09c3ac2012-03-01 22:33:29 -050019007 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050019008 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050019009
James Smart546fc852011-03-11 16:06:29 -050019010 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050019011 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
19012 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
19013 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050019014
19015 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
19016 if (rc == IOCB_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019017 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart6dd9e312013-01-03 15:43:37 -050019018 "2925 Failed to issue CT ABTS RSP x%x on "
19019 "xri x%x, Data x%x\n",
19020 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
19021 phba->link_state);
19022 lpfc_nlp_put(ndlp);
19023 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050019024 lpfc_sli_release_iocbq(phba, ctiocb);
19025 }
James Smart6669f9b2009-10-02 15:16:45 -040019026}
19027
19028/**
19029 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
19030 * @vport: Pointer to the vport on which this sequence was received
19031 * @dmabuf: pointer to a dmabuf that describes the FC sequence
19032 *
19033 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
19034 * receive sequence is only partially assembed by the driver, it shall abort
19035 * the partially assembled frames for the sequence. Otherwise, if the
19036 * unsolicited receive sequence has been completely assembled and passed to
Ferruh Yigit02e3e582020-07-28 15:56:06 +010019037 * the Upper Layer Protocol (ULP), it then mark the per oxid status for the
James Smart6669f9b2009-10-02 15:16:45 -040019038 * unsolicited sequence has been aborted. After that, it will issue a basic
19039 * accept to accept the abort.
19040 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040019041static void
James Smart6669f9b2009-10-02 15:16:45 -040019042lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
19043 struct hbq_dmabuf *dmabuf)
19044{
19045 struct lpfc_hba *phba = vport->phba;
19046 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050019047 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050019048 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040019049
James Smart6669f9b2009-10-02 15:16:45 -040019050 /* Make a copy of fc_hdr before the dmabuf being released */
19051 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050019052 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040019053
James Smart5ffc2662009-11-18 15:39:44 -050019054 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050019055 /* ABTS by responder to exchange, no cleanup needed */
19056 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050019057 } else {
James Smart6dd9e312013-01-03 15:43:37 -050019058 /* ABTS by initiator to exchange, need to do cleanup */
19059 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
19060 if (aborted == false)
19061 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050019062 }
James Smart6dd9e312013-01-03 15:43:37 -050019063 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19064
James Smart86c67372017-04-21 16:05:04 -070019065 if (phba->nvmet_support) {
19066 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
19067 return;
19068 }
19069
James Smart6dd9e312013-01-03 15:43:37 -050019070 /* Respond with BA_ACC or BA_RJT accordingly */
19071 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040019072}
19073
19074/**
James Smart4f774512009-05-22 14:52:35 -040019075 * lpfc_seq_complete - Indicates if a sequence is complete
19076 * @dmabuf: pointer to a dmabuf that describes the FC sequence
19077 *
19078 * This function checks the sequence, starting with the frame described by
19079 * @dmabuf, to see if all the frames associated with this sequence are present.
19080 * the frames associated with this sequence are linked to the @dmabuf using the
19081 * dbuf list. This function looks for two major things. 1) That the first frame
19082 * has a sequence count of zero. 2) There is a frame with last frame of sequence
19083 * set. 3) That there are no holes in the sequence count. The function will
19084 * return 1 when the sequence is complete, otherwise it will return 0.
19085 **/
19086static int
19087lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
19088{
19089 struct fc_frame_header *hdr;
19090 struct lpfc_dmabuf *d_buf;
19091 struct hbq_dmabuf *seq_dmabuf;
19092 uint32_t fctl;
19093 int seq_count = 0;
19094
19095 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19096 /* make sure first fame of sequence has a sequence count of zero */
19097 if (hdr->fh_seq_cnt != seq_count)
19098 return 0;
19099 fctl = (hdr->fh_f_ctl[0] << 16 |
19100 hdr->fh_f_ctl[1] << 8 |
19101 hdr->fh_f_ctl[2]);
19102 /* If last frame of sequence we can return success. */
19103 if (fctl & FC_FC_END_SEQ)
19104 return 1;
19105 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
19106 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19107 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19108 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050019109 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040019110 return 0;
19111 fctl = (hdr->fh_f_ctl[0] << 16 |
19112 hdr->fh_f_ctl[1] << 8 |
19113 hdr->fh_f_ctl[2]);
19114 /* If last frame of sequence we can return success. */
19115 if (fctl & FC_FC_END_SEQ)
19116 return 1;
19117 }
19118 return 0;
19119}
19120
19121/**
19122 * lpfc_prep_seq - Prep sequence for ULP processing
19123 * @vport: Pointer to the vport on which this sequence was received
Lee Jones7af29d42020-07-21 17:41:31 +010019124 * @seq_dmabuf: pointer to a dmabuf that describes the FC sequence
James Smart4f774512009-05-22 14:52:35 -040019125 *
19126 * This function takes a sequence, described by a list of frames, and creates
19127 * a list of iocbq structures to describe the sequence. This iocbq list will be
19128 * used to issue to the generic unsolicited sequence handler. This routine
19129 * returns a pointer to the first iocbq in the list. If the function is unable
19130 * to allocate an iocbq then it throw out the received frames that were not
19131 * able to be described and return a pointer to the first iocbq. If unable to
19132 * allocate any iocbqs (including the first) this function will return NULL.
19133 **/
19134static struct lpfc_iocbq *
19135lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
19136{
James Smart7851fe22011-07-22 18:36:52 -040019137 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040019138 struct lpfc_dmabuf *d_buf, *n_buf;
19139 struct lpfc_iocbq *first_iocbq, *iocbq;
19140 struct fc_frame_header *fc_hdr;
19141 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040019142 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050019143 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040019144
19145 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19146 /* remove from receive buffer list */
19147 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040019148 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040019149 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040019150 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040019151 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040019152 /* Get an iocbq struct to fill in. */
19153 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
19154 if (first_iocbq) {
19155 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040019156 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040019157 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080019158 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040019159
19160 /* Check FC Header to see what TYPE of frame we are rcv'ing */
19161 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
19162 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
19163 first_iocbq->iocb.un.rcvels.parmRo =
19164 sli4_did_from_fc_hdr(fc_hdr);
19165 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
19166 } else
19167 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040019168 first_iocbq->iocb.ulpContext = NO_XRI;
19169 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
19170 be16_to_cpu(fc_hdr->fh_ox_id);
19171 /* iocbq is prepped for internal consumption. Physical vpi. */
19172 first_iocbq->iocb.unsli3.rcvsli3.vpi =
19173 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040019174 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040019175 tot_len = bf_get(lpfc_rcqe_length,
19176 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
19177
James Smart4f774512009-05-22 14:52:35 -040019178 first_iocbq->context2 = &seq_dmabuf->dbuf;
19179 first_iocbq->context3 = NULL;
19180 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040019181 if (tot_len > LPFC_DATA_BUF_SIZE)
19182 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040019183 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040019184 else
19185 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
19186
James Smart4f774512009-05-22 14:52:35 -040019187 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040019188
James Smart7851fe22011-07-22 18:36:52 -040019189 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040019190 }
19191 iocbq = first_iocbq;
19192 /*
19193 * Each IOCBq can have two Buffers assigned, so go through the list
19194 * of buffers for this sequence and save two buffers in each IOCBq
19195 */
19196 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
19197 if (!iocbq) {
19198 lpfc_in_buf_free(vport->phba, d_buf);
19199 continue;
19200 }
19201 if (!iocbq->context3) {
19202 iocbq->context3 = d_buf;
19203 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040019204 /* We need to get the size out of the right CQE */
19205 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19206 len = bf_get(lpfc_rcqe_length,
19207 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040019208 pbde = (struct ulp_bde64 *)
19209 &iocbq->iocb.unsli3.sli3Words[4];
19210 if (len > LPFC_DATA_BUF_SIZE)
19211 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
19212 else
19213 pbde->tus.f.bdeSize = len;
19214
James Smart7851fe22011-07-22 18:36:52 -040019215 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
19216 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040019217 } else {
19218 iocbq = lpfc_sli_get_iocbq(vport->phba);
19219 if (!iocbq) {
19220 if (first_iocbq) {
19221 first_iocbq->iocb.ulpStatus =
19222 IOSTAT_FCP_RSP_ERROR;
19223 first_iocbq->iocb.un.ulpWord[4] =
19224 IOERR_NO_RESOURCES;
19225 }
19226 lpfc_in_buf_free(vport->phba, d_buf);
19227 continue;
19228 }
James Smart7851fe22011-07-22 18:36:52 -040019229 /* We need to get the size out of the right CQE */
19230 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
19231 len = bf_get(lpfc_rcqe_length,
19232 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040019233 iocbq->context2 = d_buf;
19234 iocbq->context3 = NULL;
19235 iocbq->iocb.ulpBdeCount = 1;
19236 if (len > LPFC_DATA_BUF_SIZE)
19237 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
19238 LPFC_DATA_BUF_SIZE;
19239 else
19240 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
19241
James Smart7851fe22011-07-22 18:36:52 -040019242 tot_len += len;
19243 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
19244
James Smart4f774512009-05-22 14:52:35 -040019245 iocbq->iocb.un.rcvels.remoteID = sid;
19246 list_add_tail(&iocbq->list, &first_iocbq->list);
19247 }
19248 }
James Smart39c4f1a2020-01-27 16:23:01 -080019249 /* Free the sequence's header buffer */
19250 if (!first_iocbq)
19251 lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
19252
James Smart4f774512009-05-22 14:52:35 -040019253 return first_iocbq;
19254}
19255
James Smart6669f9b2009-10-02 15:16:45 -040019256static void
19257lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
19258 struct hbq_dmabuf *seq_dmabuf)
19259{
19260 struct fc_frame_header *fc_hdr;
19261 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
19262 struct lpfc_hba *phba = vport->phba;
19263
19264 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
19265 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
19266 if (!iocbq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019267 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6669f9b2009-10-02 15:16:45 -040019268 "2707 Ring %d handler: Failed to allocate "
19269 "iocb Rctl x%x Type x%x received\n",
19270 LPFC_ELS_RING,
19271 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19272 return;
19273 }
19274 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080019275 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040019276 iocbq, fc_hdr->fh_r_ctl,
19277 fc_hdr->fh_type))
Dick Kennedy372c1872020-06-30 14:50:00 -070019278 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6669f9b2009-10-02 15:16:45 -040019279 "2540 Ring %d handler: unexpected Rctl "
19280 "x%x Type x%x received\n",
19281 LPFC_ELS_RING,
19282 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
19283
19284 /* Free iocb created in lpfc_prep_seq */
19285 list_for_each_entry_safe(curr_iocb, next_iocb,
19286 &iocbq->list, list) {
19287 list_del_init(&curr_iocb->list);
19288 lpfc_sli_release_iocbq(phba, curr_iocb);
19289 }
19290 lpfc_sli_release_iocbq(phba, iocbq);
19291}
19292
James Smartae9e28f2017-05-15 15:20:51 -070019293static void
19294lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
19295 struct lpfc_iocbq *rspiocb)
19296{
19297 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
19298
19299 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020019300 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070019301 kfree(pcmd);
19302 lpfc_sli_release_iocbq(phba, cmdiocb);
James Smarte817e5d2018-12-13 15:17:53 -080019303 lpfc_drain_txq(phba);
James Smartae9e28f2017-05-15 15:20:51 -070019304}
19305
19306static void
19307lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
19308 struct hbq_dmabuf *dmabuf)
19309{
19310 struct fc_frame_header *fc_hdr;
19311 struct lpfc_hba *phba = vport->phba;
19312 struct lpfc_iocbq *iocbq = NULL;
19313 union lpfc_wqe *wqe;
19314 struct lpfc_dmabuf *pcmd = NULL;
19315 uint32_t frame_len;
19316 int rc;
James Smarte817e5d2018-12-13 15:17:53 -080019317 unsigned long iflags;
James Smartae9e28f2017-05-15 15:20:51 -070019318
19319 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
19320 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
19321
19322 /* Send the received frame back */
19323 iocbq = lpfc_sli_get_iocbq(phba);
James Smarte817e5d2018-12-13 15:17:53 -080019324 if (!iocbq) {
19325 /* Queue cq event and wakeup worker thread to process it */
19326 spin_lock_irqsave(&phba->hbalock, iflags);
19327 list_add_tail(&dmabuf->cq_event.list,
19328 &phba->sli4_hba.sp_queue_event);
19329 phba->hba_flag |= HBA_SP_QUEUE_EVT;
19330 spin_unlock_irqrestore(&phba->hbalock, iflags);
19331 lpfc_worker_wake_up(phba);
19332 return;
19333 }
James Smartae9e28f2017-05-15 15:20:51 -070019334
19335 /* Allocate buffer for command payload */
19336 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
19337 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020019338 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070019339 &pcmd->phys);
19340 if (!pcmd || !pcmd->virt)
19341 goto exit;
19342
19343 INIT_LIST_HEAD(&pcmd->list);
19344
19345 /* copyin the payload */
19346 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
19347
19348 /* fill in BDE's for command */
19349 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
19350 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
19351 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
19352 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
19353
19354 iocbq->context2 = pcmd;
19355 iocbq->vport = vport;
19356 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
19357 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
19358
19359 /*
19360 * Setup rest of the iocb as though it were a WQE
19361 * Build the SEND_FRAME WQE
19362 */
19363 wqe = (union lpfc_wqe *)&iocbq->iocb;
19364
19365 wqe->send_frame.frame_len = frame_len;
19366 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
19367 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
19368 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
19369 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
19370 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
19371 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
19372
19373 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
19374 iocbq->iocb.ulpLe = 1;
19375 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
19376 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
19377 if (rc == IOCB_ERROR)
19378 goto exit;
19379
19380 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19381 return;
19382
19383exit:
19384 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
19385 "2023 Unable to process MDS loopback frame\n");
19386 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020019387 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070019388 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070019389 if (iocbq)
19390 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070019391 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19392}
19393
James Smart4f774512009-05-22 14:52:35 -040019394/**
19395 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
19396 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010019397 * @dmabuf: Pointer to a dmabuf that describes the FC sequence.
James Smart4f774512009-05-22 14:52:35 -040019398 *
19399 * This function is called with no lock held. This function processes all
19400 * the received buffers and gives it to upper layers when a received buffer
19401 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080019402 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040019403 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
19404 * appropriate receive function when the final frame in a sequence is received.
19405 **/
James Smart4d9ab992009-10-02 15:16:39 -040019406void
19407lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
19408 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040019409{
James Smart4d9ab992009-10-02 15:16:39 -040019410 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040019411 struct fc_frame_header *fc_hdr;
19412 struct lpfc_vport *vport;
19413 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040019414 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040019415
James Smart4f774512009-05-22 14:52:35 -040019416 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040019417 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080019418
James Smarte817e5d2018-12-13 15:17:53 -080019419 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
19420 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
19421 vport = phba->pport;
19422 /* Handle MDS Loopback frames */
Dick Kennedy24411fc2020-08-03 14:02:25 -070019423 if (!(phba->pport->load_flag & FC_UNLOADING))
19424 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19425 else
19426 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smarte817e5d2018-12-13 15:17:53 -080019427 return;
19428 }
19429
James Smart4d9ab992009-10-02 15:16:39 -040019430 /* check to see if this a valid type of frame */
19431 if (lpfc_fc_frame_check(phba, fc_hdr)) {
19432 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19433 return;
19434 }
James Smart2ea259e2017-02-12 13:52:27 -080019435
James Smart7851fe22011-07-22 18:36:52 -040019436 if ((bf_get(lpfc_cqe_code,
19437 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
19438 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
19439 &dmabuf->cq_event.cqe.rcqe_cmpl);
19440 else
19441 fcfi = bf_get(lpfc_rcqe_fcf_id,
19442 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040019443
James Smarte62245d2019-08-14 16:57:08 -070019444 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
19445 vport = phba->pport;
19446 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
19447 "2023 MDS Loopback %d bytes\n",
19448 bf_get(lpfc_rcqe_length,
19449 &dmabuf->cq_event.cqe.rcqe_cmpl));
19450 /* Handle MDS Loopback frames */
19451 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
19452 return;
19453 }
19454
James Smart895427b2017-02-12 13:52:30 -080019455 /* d_id this frame is directed to */
19456 did = sli4_did_from_fc_hdr(fc_hdr);
19457
19458 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040019459 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040019460 /* throw out the frame */
19461 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19462 return;
19463 }
James Smart939723a2012-05-09 21:19:03 -040019464
James Smart939723a2012-05-09 21:19:03 -040019465 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
19466 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
19467 (did != Fabric_DID)) {
19468 /*
19469 * Throw out the frame if we are not pt2pt.
19470 * The pt2pt protocol allows for discovery frames
19471 * to be received without a registered VPI.
19472 */
19473 if (!(vport->fc_flag & FC_PT2PT) ||
19474 (phba->link_state == LPFC_HBA_READY)) {
19475 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19476 return;
19477 }
19478 }
19479
James Smart6669f9b2009-10-02 15:16:45 -040019480 /* Handle the basic abort sequence (BA_ABTS) event */
19481 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
19482 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
19483 return;
19484 }
19485
James Smart4d9ab992009-10-02 15:16:39 -040019486 /* Link this frame */
19487 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
19488 if (!seq_dmabuf) {
19489 /* unable to add frame to vport - throw it out */
19490 lpfc_in_buf_free(phba, &dmabuf->dbuf);
19491 return;
19492 }
19493 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050019494 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040019495 return;
James Smartdef9c7a2009-12-21 17:02:28 -050019496
James Smart6669f9b2009-10-02 15:16:45 -040019497 /* Send the complete sequence to the upper layer protocol */
19498 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040019499}
James Smart6fb120a2009-05-22 14:52:59 -040019500
19501/**
19502 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
19503 * @phba: pointer to lpfc hba data structure.
19504 *
19505 * This routine is invoked to post rpi header templates to the
19506 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040019507 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19508 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040019509 *
19510 * This routine does not require any locks. It's usage is expected
19511 * to be driver load or reset recovery when the driver is
19512 * sequential.
19513 *
19514 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020019515 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040019516 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040019517 * When this error occurs, the driver is not guaranteed
19518 * to have any rpi regions posted to the device and
19519 * must either attempt to repost the regions or take a
19520 * fatal error.
19521 **/
19522int
19523lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
19524{
19525 struct lpfc_rpi_hdr *rpi_page;
19526 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040019527 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040019528
James Smart6d368e52011-05-24 11:44:12 -040019529 /* SLI4 ports that support extents do not require RPI headers. */
19530 if (!phba->sli4_hba.rpi_hdrs_in_use)
19531 goto exit;
19532 if (phba->sli4_hba.extents_in_use)
19533 return -EIO;
19534
James Smart6fb120a2009-05-22 14:52:59 -040019535 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040019536 /*
19537 * Assign the rpi headers a physical rpi only if the driver
19538 * has not initialized those resources. A port reset only
19539 * needs the headers posted.
19540 */
19541 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
19542 LPFC_RPI_RSRC_RDY)
19543 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
19544
James Smart6fb120a2009-05-22 14:52:59 -040019545 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
19546 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019547 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019548 "2008 Error %d posting all rpi "
19549 "headers\n", rc);
19550 rc = -EIO;
19551 break;
19552 }
19553 }
19554
James Smart6d368e52011-05-24 11:44:12 -040019555 exit:
19556 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
19557 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040019558 return rc;
19559}
19560
19561/**
19562 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
19563 * @phba: pointer to lpfc hba data structure.
19564 * @rpi_page: pointer to the rpi memory region.
19565 *
19566 * This routine is invoked to post a single rpi header to the
19567 * HBA consistent with the SLI-4 interface spec. This memory region
19568 * maps up to 64 rpi context regions.
19569 *
19570 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020019571 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040019572 * -ENOMEM - No available memory
19573 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040019574 **/
19575int
19576lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
19577{
19578 LPFC_MBOXQ_t *mboxq;
19579 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
19580 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040019581 uint32_t shdr_status, shdr_add_status;
19582 union lpfc_sli4_cfg_shdr *shdr;
19583
James Smart6d368e52011-05-24 11:44:12 -040019584 /* SLI4 ports that support extents do not require RPI headers. */
19585 if (!phba->sli4_hba.rpi_hdrs_in_use)
19586 return rc;
19587 if (phba->sli4_hba.extents_in_use)
19588 return -EIO;
19589
James Smart6fb120a2009-05-22 14:52:59 -040019590 /* The port is notified of the header region via a mailbox command. */
19591 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19592 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019593 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019594 "2001 Unable to allocate memory for issuing "
19595 "SLI_CONFIG_SPECIAL mailbox command\n");
19596 return -ENOMEM;
19597 }
19598
19599 /* Post all rpi memory regions to the port. */
19600 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040019601 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19602 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
19603 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050019604 sizeof(struct lpfc_sli4_cfg_mhdr),
19605 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040019606
19607
19608 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040019609 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
19610 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040019611 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
19612 hdr_tmpl, rpi_page->page_count);
19613
James Smart6fb120a2009-05-22 14:52:59 -040019614 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
19615 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040019616 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040019617 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
19618 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19619 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart304ee432021-04-11 18:31:17 -070019620 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -040019621 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019622 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019623 "2514 POST_RPI_HDR mailbox failed with "
19624 "status x%x add_status x%x, mbx status x%x\n",
19625 shdr_status, shdr_add_status, rc);
19626 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070019627 } else {
19628 /*
19629 * The next_rpi stores the next logical module-64 rpi value used
19630 * to post physical rpis in subsequent rpi postings.
19631 */
19632 spin_lock_irq(&phba->hbalock);
19633 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
19634 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040019635 }
19636 return rc;
19637}
19638
19639/**
19640 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
19641 * @phba: pointer to lpfc hba data structure.
19642 *
19643 * This routine is invoked to post rpi header templates to the
19644 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040019645 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
19646 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040019647 *
19648 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020019649 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040019650 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
19651 **/
19652int
19653lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
19654{
James Smart6d368e52011-05-24 11:44:12 -040019655 unsigned long rpi;
19656 uint16_t max_rpi, rpi_limit;
19657 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040019658 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040019659 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040019660
James Smart6fb120a2009-05-22 14:52:59 -040019661 /*
James Smart6d368e52011-05-24 11:44:12 -040019662 * Fetch the next logical rpi. Because this index is logical,
19663 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040019664 */
James Smart4902b382013-10-10 12:20:35 -040019665 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040019666 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
19667 rpi_limit = phba->sli4_hba.next_rpi;
19668
Yury Norovb5c7e7e2021-08-14 14:17:03 -070019669 rpi = find_first_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit);
James Smart6d368e52011-05-24 11:44:12 -040019670 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040019671 rpi = LPFC_RPI_ALLOC_ERROR;
19672 else {
19673 set_bit(rpi, phba->sli4_hba.rpi_bmask);
19674 phba->sli4_hba.max_cfg_param.rpi_used++;
19675 phba->sli4_hba.rpi_count++;
19676 }
James Smart0f154222019-09-21 20:58:52 -070019677 lpfc_printf_log(phba, KERN_INFO,
19678 LOG_NODE | LOG_DISCOVERY,
19679 "0001 Allocated rpi:x%x max:x%x lim:x%x\n",
James Smartbe6bb942015-04-07 15:07:22 -040019680 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040019681
19682 /*
19683 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040019684 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040019685 */
19686 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
19687 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040019688 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040019689 return rpi;
19690 }
19691
19692 /*
James Smart6d368e52011-05-24 11:44:12 -040019693 * RPI header postings are not required for SLI4 ports capable of
19694 * extents.
19695 */
19696 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040019697 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040019698 return rpi;
19699 }
19700
19701 /*
James Smart6fb120a2009-05-22 14:52:59 -040019702 * If the driver is running low on rpi resources, allocate another
19703 * page now. Note that the next_rpi value is used because
19704 * it represents how many are actually in use whereas max_rpi notes
19705 * how many are supported max by the device.
19706 */
James Smart6d368e52011-05-24 11:44:12 -040019707 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040019708 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040019709 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
19710 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
19711 if (!rpi_hdr) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019712 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019713 "2002 Error Could not grow rpi "
19714 "count\n");
19715 } else {
James Smart6d368e52011-05-24 11:44:12 -040019716 lrpi = rpi_hdr->start_rpi;
19717 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040019718 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
19719 }
19720 }
19721
19722 return rpi;
19723}
19724
19725/**
Lee Jones8514e2f2021-03-03 14:46:18 +000019726 * __lpfc_sli4_free_rpi - Release an rpi for reuse.
James Smart6fb120a2009-05-22 14:52:59 -040019727 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010019728 * @rpi: rpi to free
James Smart6fb120a2009-05-22 14:52:59 -040019729 *
19730 * This routine is invoked to release an rpi to the pool of
19731 * available rpis maintained by the driver.
19732 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040019733static void
James Smartd7c47992010-06-08 18:31:54 -040019734__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19735{
James Smart7cfd5632019-11-04 16:56:58 -080019736 /*
19737 * if the rpi value indicates a prior unreg has already
19738 * been done, skip the unreg.
19739 */
19740 if (rpi == LPFC_RPI_ALLOC_ERROR)
19741 return;
19742
James Smartd7c47992010-06-08 18:31:54 -040019743 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
19744 phba->sli4_hba.rpi_count--;
19745 phba->sli4_hba.max_cfg_param.rpi_used--;
James Smartb95b2112019-08-14 16:56:47 -070019746 } else {
James Smart0f154222019-09-21 20:58:52 -070019747 lpfc_printf_log(phba, KERN_INFO,
19748 LOG_NODE | LOG_DISCOVERY,
James Smartb95b2112019-08-14 16:56:47 -070019749 "2016 rpi %x not inuse\n",
19750 rpi);
James Smartd7c47992010-06-08 18:31:54 -040019751 }
19752}
19753
19754/**
19755 * lpfc_sli4_free_rpi - Release an rpi for reuse.
19756 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010019757 * @rpi: rpi to free
James Smartd7c47992010-06-08 18:31:54 -040019758 *
19759 * This routine is invoked to release an rpi to the pool of
19760 * available rpis maintained by the driver.
19761 **/
19762void
James Smart6fb120a2009-05-22 14:52:59 -040019763lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
19764{
19765 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040019766 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040019767 spin_unlock_irq(&phba->hbalock);
19768}
19769
19770/**
19771 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
19772 * @phba: pointer to lpfc hba data structure.
19773 *
19774 * This routine is invoked to remove the memory region that
19775 * provided rpi via a bitmask.
19776 **/
19777void
19778lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
19779{
19780 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040019781 kfree(phba->sli4_hba.rpi_ids);
19782 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040019783}
19784
19785/**
19786 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
Lee Jones7af29d42020-07-21 17:41:31 +010019787 * @ndlp: pointer to lpfc nodelist data structure.
19788 * @cmpl: completion call-back.
19789 * @arg: data to load as MBox 'caller buffer information'
James Smart6fb120a2009-05-22 14:52:59 -040019790 *
19791 * This routine is invoked to remove the memory region that
19792 * provided rpi via a bitmask.
19793 **/
19794int
James Smart6b5151f2012-01-18 16:24:06 -050019795lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
19796 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040019797{
19798 LPFC_MBOXQ_t *mboxq;
19799 struct lpfc_hba *phba = ndlp->phba;
19800 int rc;
19801
19802 /* The port is notified of the header region via a mailbox command. */
19803 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19804 if (!mboxq)
19805 return -ENOMEM;
19806
James Smart1037e4b2021-05-14 12:55:52 -070019807 /* If cmpl assigned, then this nlp_get pairs with
19808 * lpfc_mbx_cmpl_resume_rpi.
19809 *
19810 * Else cmpl is NULL, then this nlp_get pairs with
19811 * lpfc_sli_def_mbox_cmpl.
19812 */
19813 if (!lpfc_nlp_get(ndlp)) {
19814 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
19815 "2122 %s: Failed to get nlp ref\n",
19816 __func__);
19817 mempool_free(mboxq, phba->mbox_mem_pool);
19818 return -EIO;
19819 }
19820
James Smart6fb120a2009-05-22 14:52:59 -040019821 /* Post all rpi memory regions to the port. */
19822 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050019823 if (cmpl) {
19824 mboxq->mbox_cmpl = cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080019825 mboxq->ctx_buf = arg;
James Smart72859902012-01-18 16:25:38 -050019826 } else
19827 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart1037e4b2021-05-14 12:55:52 -070019828 mboxq->ctx_ndlp = ndlp;
James Smart6b5151f2012-01-18 16:24:06 -050019829 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040019830 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19831 if (rc == MBX_NOT_FINISHED) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019832 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019833 "2010 Resume RPI Mailbox failed "
19834 "status %d, mbxStatus x%x\n", rc,
19835 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart1037e4b2021-05-14 12:55:52 -070019836 lpfc_nlp_put(ndlp);
James Smart6fb120a2009-05-22 14:52:59 -040019837 mempool_free(mboxq, phba->mbox_mem_pool);
19838 return -EIO;
19839 }
19840 return 0;
19841}
19842
19843/**
19844 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050019845 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040019846 *
James Smart76a95d72010-11-20 23:11:48 -050019847 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040019848 *
19849 * Returns:
19850 * 0 success
19851 * -Evalue otherwise
19852 **/
19853int
James Smart76a95d72010-11-20 23:11:48 -050019854lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040019855{
19856 LPFC_MBOXQ_t *mboxq;
19857 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040019858 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040019859 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050019860 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040019861 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19862 if (!mboxq)
19863 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050019864 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040019865 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040019866 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040019867 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019868 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019869 "2022 INIT VPI Mailbox failed "
19870 "status %d, mbxStatus x%x\n", rc,
19871 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040019872 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040019873 }
James Smart6a9c52c2009-10-02 15:16:51 -040019874 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050019875 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040019876
19877 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040019878}
19879
19880/**
19881 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
19882 * @phba: pointer to lpfc hba data structure.
19883 * @mboxq: Pointer to mailbox object.
19884 *
19885 * This routine is invoked to manually add a single FCF record. The caller
19886 * must pass a completely initialized FCF_Record. This routine takes
19887 * care of the nonembedded mailbox operations.
19888 **/
19889static void
19890lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
19891{
19892 void *virt_addr;
19893 union lpfc_sli4_cfg_shdr *shdr;
19894 uint32_t shdr_status, shdr_add_status;
19895
19896 virt_addr = mboxq->sge_array->addr[0];
19897 /* The IOCTL status is embedded in the mailbox subheader. */
19898 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
19899 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19900 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19901
19902 if ((shdr_status || shdr_add_status) &&
19903 (shdr_status != STATUS_FCF_IN_USE))
Dick Kennedy372c1872020-06-30 14:50:00 -070019904 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019905 "2558 ADD_FCF_RECORD mailbox failed with "
19906 "status x%x add_status x%x\n",
19907 shdr_status, shdr_add_status);
19908
19909 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19910}
19911
19912/**
19913 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
19914 * @phba: pointer to lpfc hba data structure.
19915 * @fcf_record: pointer to the initialized fcf record to add.
19916 *
19917 * This routine is invoked to manually add a single FCF record. The caller
19918 * must pass a completely initialized FCF_Record. This routine takes
19919 * care of the nonembedded mailbox operations.
19920 **/
19921int
19922lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
19923{
19924 int rc = 0;
19925 LPFC_MBOXQ_t *mboxq;
19926 uint8_t *bytep;
19927 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040019928 struct lpfc_mbx_sge sge;
19929 uint32_t alloc_len, req_len;
19930 uint32_t fcfindex;
19931
19932 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19933 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019934 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019935 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
19936 return -ENOMEM;
19937 }
19938
19939 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
19940 sizeof(uint32_t);
19941
19942 /* Allocate DMA memory and set up the non-embedded mailbox command */
19943 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19944 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
19945 req_len, LPFC_SLI4_MBX_NEMBED);
19946 if (alloc_len < req_len) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019947 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019948 "2523 Allocated DMA memory size (x%x) is "
19949 "less than the requested DMA memory "
19950 "size (x%x)\n", alloc_len, req_len);
19951 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19952 return -ENOMEM;
19953 }
19954
19955 /*
19956 * Get the first SGE entry from the non-embedded DMA memory. This
19957 * routine only uses a single SGE.
19958 */
19959 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040019960 virt_addr = mboxq->sge_array->addr[0];
19961 /*
19962 * Configure the FCF record for FCFI 0. This is the driver's
19963 * hardcoded default and gets used in nonFIP mode.
19964 */
19965 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
19966 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
19967 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
19968
19969 /*
19970 * Copy the fcf_index and the FCF Record Data. The data starts after
19971 * the FCoE header plus word10. The data copy needs to be endian
19972 * correct.
19973 */
19974 bytep += sizeof(uint32_t);
19975 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
19976 mboxq->vport = phba->pport;
19977 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
19978 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19979 if (rc == MBX_NOT_FINISHED) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019980 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019981 "2515 ADD_FCF_RECORD mailbox failed with "
19982 "status 0x%x\n", rc);
19983 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19984 rc = -EIO;
19985 } else
19986 rc = 0;
19987
19988 return rc;
19989}
19990
19991/**
19992 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
19993 * @phba: pointer to lpfc hba data structure.
19994 * @fcf_record: pointer to the fcf record to write the default data.
19995 * @fcf_index: FCF table entry index.
19996 *
19997 * This routine is invoked to build the driver's default FCF record. The
19998 * values used are hardcoded. This routine handles memory initialization.
19999 *
20000 **/
20001void
20002lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
20003 struct fcf_record *fcf_record,
20004 uint16_t fcf_index)
20005{
20006 memset(fcf_record, 0, sizeof(struct fcf_record));
20007 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
20008 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
20009 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
20010 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
20011 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
20012 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
20013 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
20014 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
20015 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
20016 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
20017 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
20018 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
20019 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040020020 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040020021 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
20022 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
20023 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
20024 /* Set the VLAN bit map */
20025 if (phba->valid_vlan) {
20026 fcf_record->vlan_bitmap[phba->vlan_id / 8]
20027 = 1 << (phba->vlan_id % 8);
20028 }
20029}
20030
20031/**
James Smart0c9ab6f2010-02-26 14:15:57 -050020032 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040020033 * @phba: pointer to lpfc hba data structure.
20034 * @fcf_index: FCF table entry offset.
20035 *
James Smart0c9ab6f2010-02-26 14:15:57 -050020036 * This routine is invoked to scan the entire FCF table by reading FCF
20037 * record and processing it one at a time starting from the @fcf_index
20038 * for initial FCF discovery or fast FCF failover rediscovery.
20039 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030020040 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050020041 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040020042 **/
20043int
James Smart0c9ab6f2010-02-26 14:15:57 -050020044lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040020045{
20046 int rc = 0, error;
20047 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040020048
James Smart32b97932009-07-19 10:01:21 -040020049 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050020050 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040020051 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20052 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020053 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040020054 "2000 Failed to allocate mbox for "
20055 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040020056 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050020057 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040020058 }
James Smartecfd03c2010-02-12 14:41:27 -050020059 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050020060 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050020061 if (rc) {
20062 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050020063 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040020064 }
James Smartecfd03c2010-02-12 14:41:27 -050020065 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040020066 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050020067 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040020068
20069 spin_lock_irq(&phba->hbalock);
20070 phba->hba_flag |= FCF_TS_INPROG;
20071 spin_unlock_irq(&phba->hbalock);
20072
James Smart6fb120a2009-05-22 14:52:59 -040020073 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050020074 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040020075 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050020076 else {
James Smart38b92ef2010-08-04 16:11:39 -040020077 /* Reset eligible FCF count for new scan */
20078 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040020079 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040020080 error = 0;
James Smart32b97932009-07-19 10:01:21 -040020081 }
James Smart0c9ab6f2010-02-26 14:15:57 -050020082fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040020083 if (error) {
20084 if (mboxq)
20085 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040020086 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040020087 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040020088 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040020089 spin_unlock_irq(&phba->hbalock);
20090 }
James Smart6fb120a2009-05-22 14:52:59 -040020091 return error;
20092}
James Smarta0c87cb2009-07-19 10:01:10 -040020093
20094/**
James Smarta93ff372010-10-22 11:06:08 -040020095 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050020096 * @phba: pointer to lpfc hba data structure.
20097 * @fcf_index: FCF table entry offset.
20098 *
20099 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040020100 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050020101 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030020102 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050020103 * otherwise.
20104 **/
20105int
20106lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20107{
20108 int rc = 0, error;
20109 LPFC_MBOXQ_t *mboxq;
20110
20111 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20112 if (!mboxq) {
20113 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20114 "2763 Failed to allocate mbox for "
20115 "READ_FCF cmd\n");
20116 error = -ENOMEM;
20117 goto fail_fcf_read;
20118 }
20119 /* Construct the read FCF record mailbox command */
20120 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20121 if (rc) {
20122 error = -EINVAL;
20123 goto fail_fcf_read;
20124 }
20125 /* Issue the mailbox command asynchronously */
20126 mboxq->vport = phba->pport;
20127 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
20128 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20129 if (rc == MBX_NOT_FINISHED)
20130 error = -EIO;
20131 else
20132 error = 0;
20133
20134fail_fcf_read:
20135 if (error && mboxq)
20136 lpfc_sli4_mbox_cmd_free(phba, mboxq);
20137 return error;
20138}
20139
20140/**
20141 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
20142 * @phba: pointer to lpfc hba data structure.
20143 * @fcf_index: FCF table entry offset.
20144 *
20145 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040020146 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050020147 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030020148 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050020149 * otherwise.
20150 **/
20151int
20152lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
20153{
20154 int rc = 0, error;
20155 LPFC_MBOXQ_t *mboxq;
20156
20157 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20158 if (!mboxq) {
20159 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
20160 "2758 Failed to allocate mbox for "
20161 "READ_FCF cmd\n");
20162 error = -ENOMEM;
20163 goto fail_fcf_read;
20164 }
20165 /* Construct the read FCF record mailbox command */
20166 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
20167 if (rc) {
20168 error = -EINVAL;
20169 goto fail_fcf_read;
20170 }
20171 /* Issue the mailbox command asynchronously */
20172 mboxq->vport = phba->pport;
20173 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
20174 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
20175 if (rc == MBX_NOT_FINISHED)
20176 error = -EIO;
20177 else
20178 error = 0;
20179
20180fail_fcf_read:
20181 if (error && mboxq)
20182 lpfc_sli4_mbox_cmd_free(phba, mboxq);
20183 return error;
20184}
20185
20186/**
James Smartf5cb5302015-12-16 18:11:52 -050020187 * lpfc_check_next_fcf_pri_level
Lee Jones7af29d42020-07-21 17:41:31 +010020188 * @phba: pointer to the lpfc_hba struct for this port.
James Smart7d791df2011-07-22 18:37:52 -040020189 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
20190 * routine when the rr_bmask is empty. The FCF indecies are put into the
20191 * rr_bmask based on their priority level. Starting from the highest priority
20192 * to the lowest. The most likely FCF candidate will be in the highest
20193 * priority group. When this routine is called it searches the fcf_pri list for
20194 * next lowest priority group and repopulates the rr_bmask with only those
20195 * fcf_indexes.
20196 * returns:
20197 * 1=success 0=failure
20198 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040020199static int
James Smart7d791df2011-07-22 18:37:52 -040020200lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
20201{
20202 uint16_t next_fcf_pri;
20203 uint16_t last_index;
20204 struct lpfc_fcf_pri *fcf_pri;
20205 int rc;
20206 int ret = 0;
20207
20208 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20209 LPFC_SLI4_FCF_TBL_INDX_MAX);
20210 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
20211 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050020212
20213 /* Verify the priority list has 2 or more entries */
20214 spin_lock_irq(&phba->hbalock);
20215 if (list_empty(&phba->fcf.fcf_pri_list) ||
20216 list_is_singular(&phba->fcf.fcf_pri_list)) {
20217 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040020218 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
20219 "3061 Last IDX %d\n", last_index);
20220 return 0; /* Empty rr list */
20221 }
James Smart25626692013-03-01 16:36:54 -050020222 spin_unlock_irq(&phba->hbalock);
20223
James Smart7d791df2011-07-22 18:37:52 -040020224 next_fcf_pri = 0;
20225 /*
20226 * Clear the rr_bmask and set all of the bits that are at this
20227 * priority.
20228 */
20229 memset(phba->fcf.fcf_rr_bmask, 0,
20230 sizeof(*phba->fcf.fcf_rr_bmask));
20231 spin_lock_irq(&phba->hbalock);
20232 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20233 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
20234 continue;
20235 /*
20236 * the 1st priority that has not FLOGI failed
20237 * will be the highest.
20238 */
20239 if (!next_fcf_pri)
20240 next_fcf_pri = fcf_pri->fcf_rec.priority;
20241 spin_unlock_irq(&phba->hbalock);
20242 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20243 rc = lpfc_sli4_fcf_rr_index_set(phba,
20244 fcf_pri->fcf_rec.fcf_index);
20245 if (rc)
20246 return 0;
20247 }
20248 spin_lock_irq(&phba->hbalock);
20249 }
20250 /*
20251 * if next_fcf_pri was not set above and the list is not empty then
20252 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070020253 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040020254 */
20255 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
20256 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
20257 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
20258 /*
20259 * the 1st priority that has not FLOGI failed
20260 * will be the highest.
20261 */
20262 if (!next_fcf_pri)
20263 next_fcf_pri = fcf_pri->fcf_rec.priority;
20264 spin_unlock_irq(&phba->hbalock);
20265 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
20266 rc = lpfc_sli4_fcf_rr_index_set(phba,
20267 fcf_pri->fcf_rec.fcf_index);
20268 if (rc)
20269 return 0;
20270 }
20271 spin_lock_irq(&phba->hbalock);
20272 }
20273 } else
20274 ret = 1;
20275 spin_unlock_irq(&phba->hbalock);
20276
20277 return ret;
20278}
20279/**
James Smart0c9ab6f2010-02-26 14:15:57 -050020280 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
20281 * @phba: pointer to lpfc hba data structure.
20282 *
20283 * This routine is to get the next eligible FCF record index in a round
20284 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040020285 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050020286 * shall be returned, otherwise, the next eligible FCF record's index
20287 * shall be returned.
20288 **/
20289uint16_t
20290lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
20291{
20292 uint16_t next_fcf_index;
20293
James Smart421c6622013-01-03 15:44:16 -050020294initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040020295 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050020296 next_fcf_index = phba->fcf.current_rec.fcf_indx;
20297
James Smart7d791df2011-07-22 18:37:52 -040020298next_priority:
James Smart421c6622013-01-03 15:44:16 -050020299 /* Determine the next fcf index to check */
20300 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050020301 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
20302 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040020303 next_fcf_index);
20304
James Smart0c9ab6f2010-02-26 14:15:57 -050020305 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040020306 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20307 /*
20308 * If we have wrapped then we need to clear the bits that
20309 * have been tested so that we can detect when we should
20310 * change the priority level.
20311 */
Yury Norovb5c7e7e2021-08-14 14:17:03 -070020312 next_fcf_index = find_first_bit(phba->fcf.fcf_rr_bmask,
20313 LPFC_SLI4_FCF_TBL_INDX_MAX);
James Smart7d791df2011-07-22 18:37:52 -040020314 }
20315
James Smart0c9ab6f2010-02-26 14:15:57 -050020316
James Smart3804dc82010-07-14 15:31:37 -040020317 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040020318 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
20319 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
20320 /*
20321 * If next fcf index is not found check if there are lower
20322 * Priority level fcf's in the fcf_priority list.
20323 * Set up the rr_bmask with all of the avaiable fcf bits
20324 * at that level and continue the selection process.
20325 */
20326 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050020327 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040020328 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
20329 "2844 No roundrobin failover FCF available\n");
James Smart036cad12018-10-23 13:41:06 -070020330
20331 return LPFC_FCOE_FCF_NEXT_NONE;
James Smart3804dc82010-07-14 15:31:37 -040020332 }
20333
James Smart7d791df2011-07-22 18:37:52 -040020334 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
20335 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050020336 LPFC_FCF_FLOGI_FAILED) {
20337 if (list_is_singular(&phba->fcf.fcf_pri_list))
20338 return LPFC_FCOE_FCF_NEXT_NONE;
20339
James Smart7d791df2011-07-22 18:37:52 -040020340 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050020341 }
James Smart7d791df2011-07-22 18:37:52 -040020342
James Smart3804dc82010-07-14 15:31:37 -040020343 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040020344 "2845 Get next roundrobin failover FCF (x%x)\n",
20345 next_fcf_index);
20346
James Smart0c9ab6f2010-02-26 14:15:57 -050020347 return next_fcf_index;
20348}
20349
20350/**
20351 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
20352 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010020353 * @fcf_index: index into the FCF table to 'set'
James Smart0c9ab6f2010-02-26 14:15:57 -050020354 *
20355 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040020356 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050020357 * does not go beyond the range of the driver allocated bmask dimension
20358 * before setting the bit.
20359 *
20360 * Returns 0 if the index bit successfully set, otherwise, it returns
20361 * -EINVAL.
20362 **/
20363int
20364lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
20365{
20366 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20367 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040020368 "2610 FCF (x%x) reached driver's book "
20369 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050020370 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20371 return -EINVAL;
20372 }
20373 /* Set the eligible FCF record index bmask */
20374 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
20375
James Smart3804dc82010-07-14 15:31:37 -040020376 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040020377 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040020378 "bmask\n", fcf_index);
20379
James Smart0c9ab6f2010-02-26 14:15:57 -050020380 return 0;
20381}
20382
20383/**
James Smart3804dc82010-07-14 15:31:37 -040020384 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050020385 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010020386 * @fcf_index: index into the FCF table to 'clear'
James Smart0c9ab6f2010-02-26 14:15:57 -050020387 *
20388 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040020389 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050020390 * does not go beyond the range of the driver allocated bmask dimension
20391 * before clearing the bit.
20392 **/
20393void
20394lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
20395{
James Smart9a803a72013-09-06 12:17:56 -040020396 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050020397 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
20398 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040020399 "2762 FCF (x%x) reached driver's book "
20400 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050020401 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
20402 return;
20403 }
20404 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040020405 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040020406 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
20407 list) {
James Smart7d791df2011-07-22 18:37:52 -040020408 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
20409 list_del_init(&fcf_pri->list);
20410 break;
20411 }
20412 }
20413 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050020414 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040020415
20416 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040020417 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040020418 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050020419}
20420
20421/**
James Smartecfd03c2010-02-12 14:41:27 -050020422 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
20423 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010020424 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
James Smartecfd03c2010-02-12 14:41:27 -050020425 *
20426 * This routine is the completion routine for the rediscover FCF table mailbox
20427 * command. If the mailbox command returned failure, it will try to stop the
20428 * FCF rediscover wait timer.
20429 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040020430static void
James Smartecfd03c2010-02-12 14:41:27 -050020431lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
20432{
20433 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20434 uint32_t shdr_status, shdr_add_status;
20435
20436 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20437
20438 shdr_status = bf_get(lpfc_mbox_hdr_status,
20439 &redisc_fcf->header.cfg_shdr.response);
20440 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20441 &redisc_fcf->header.cfg_shdr.response);
20442 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050020443 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050020444 "2746 Requesting for FCF rediscovery failed "
20445 "status x%x add_status x%x\n",
20446 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050020447 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050020448 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050020449 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050020450 spin_unlock_irq(&phba->hbalock);
20451 /*
20452 * CVL event triggered FCF rediscover request failed,
20453 * last resort to re-try current registered FCF entry.
20454 */
20455 lpfc_retry_pport_discovery(phba);
20456 } else {
20457 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050020458 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050020459 spin_unlock_irq(&phba->hbalock);
20460 /*
20461 * DEAD FCF event triggered FCF rediscover request
20462 * failed, last resort to fail over as a link down
20463 * to FCF registration.
20464 */
20465 lpfc_sli4_fcf_dead_failthrough(phba);
20466 }
James Smart0c9ab6f2010-02-26 14:15:57 -050020467 } else {
20468 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040020469 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050020470 /*
20471 * Start FCF rediscovery wait timer for pending FCF
20472 * before rescan FCF record table.
20473 */
20474 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050020475 }
James Smartecfd03c2010-02-12 14:41:27 -050020476
20477 mempool_free(mbox, phba->mbox_mem_pool);
20478}
20479
20480/**
James Smart3804dc82010-07-14 15:31:37 -040020481 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050020482 * @phba: pointer to lpfc hba data structure.
20483 *
20484 * This routine is invoked to request for rediscovery of the entire FCF table
20485 * by the port.
20486 **/
20487int
20488lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
20489{
20490 LPFC_MBOXQ_t *mbox;
20491 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
20492 int rc, length;
20493
James Smart0c9ab6f2010-02-26 14:15:57 -050020494 /* Cancel retry delay timers to all vports before FCF rediscover */
20495 lpfc_cancel_all_vport_retry_delay_timer(phba);
20496
James Smartecfd03c2010-02-12 14:41:27 -050020497 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20498 if (!mbox) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020499 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartecfd03c2010-02-12 14:41:27 -050020500 "2745 Failed to allocate mbox for "
20501 "requesting FCF rediscover.\n");
20502 return -ENOMEM;
20503 }
20504
20505 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
20506 sizeof(struct lpfc_sli4_cfg_mhdr));
20507 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
20508 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
20509 length, LPFC_SLI4_MBX_EMBED);
20510
20511 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
20512 /* Set count to 0 for invalidating the entire FCF database */
20513 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
20514
20515 /* Issue the mailbox command asynchronously */
20516 mbox->vport = phba->pport;
20517 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
20518 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
20519
20520 if (rc == MBX_NOT_FINISHED) {
20521 mempool_free(mbox, phba->mbox_mem_pool);
20522 return -EIO;
20523 }
20524 return 0;
20525}
20526
20527/**
James Smartfc2b9892010-02-26 14:15:29 -050020528 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
20529 * @phba: pointer to lpfc hba data structure.
20530 *
20531 * This function is the failover routine as a last resort to the FCF DEAD
20532 * event when driver failed to perform fast FCF failover.
20533 **/
20534void
20535lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
20536{
20537 uint32_t link_state;
20538
20539 /*
20540 * Last resort as FCF DEAD event failover will treat this as
20541 * a link down, but save the link state because we don't want
20542 * it to be changed to Link Down unless it is already down.
20543 */
20544 link_state = phba->link_state;
20545 lpfc_linkdown(phba);
20546 phba->link_state = link_state;
20547
20548 /* Unregister FCF if no devices connected to it */
20549 lpfc_unregister_unused_fcf(phba);
20550}
20551
20552/**
James Smart026abb82011-12-13 13:20:45 -050020553 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040020554 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050020555 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040020556 *
James Smart026abb82011-12-13 13:20:45 -050020557 * This function gets SLI3 port configure region 23 data through memory dump
20558 * mailbox command. When it successfully retrieves data, the size of the data
20559 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040020560 **/
James Smart026abb82011-12-13 13:20:45 -050020561static uint32_t
20562lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040020563{
20564 LPFC_MBOXQ_t *pmb = NULL;
20565 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050020566 uint32_t offset = 0;
James Smarte4ec1022021-04-21 16:45:11 -070020567 int rc;
James Smarta0c87cb2009-07-19 10:01:10 -040020568
James Smart026abb82011-12-13 13:20:45 -050020569 if (!rgn23_data)
20570 return 0;
20571
James Smarta0c87cb2009-07-19 10:01:10 -040020572 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20573 if (!pmb) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020574 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart026abb82011-12-13 13:20:45 -050020575 "2600 failed to allocate mailbox memory\n");
20576 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040020577 }
20578 mb = &pmb->u.mb;
20579
James Smarta0c87cb2009-07-19 10:01:10 -040020580 do {
20581 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
20582 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
20583
20584 if (rc != MBX_SUCCESS) {
20585 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050020586 "2601 failed to read config "
20587 "region 23, rc 0x%x Status 0x%x\n",
20588 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040020589 mb->un.varDmp.word_cnt = 0;
20590 }
20591 /*
20592 * dump mem may return a zero when finished or we got a
20593 * mailbox error, either way we are done.
20594 */
20595 if (mb->un.varDmp.word_cnt == 0)
20596 break;
James Smarta0c87cb2009-07-19 10:01:10 -040020597
James Smarte4ec1022021-04-21 16:45:11 -070020598 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
20599 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
20600
James Smarta0c87cb2009-07-19 10:01:10 -040020601 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smarte4ec1022021-04-21 16:45:11 -070020602 rgn23_data + offset,
20603 mb->un.varDmp.word_cnt);
20604 offset += mb->un.varDmp.word_cnt;
20605 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
James Smarta0c87cb2009-07-19 10:01:10 -040020606
James Smart026abb82011-12-13 13:20:45 -050020607 mempool_free(pmb, phba->mbox_mem_pool);
20608 return offset;
20609}
20610
20611/**
20612 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
20613 * @phba: pointer to lpfc hba data structure.
20614 * @rgn23_data: pointer to configure region 23 data.
20615 *
20616 * This function gets SLI4 port configure region 23 data through memory dump
20617 * mailbox command. When it successfully retrieves data, the size of the data
20618 * will be returned, otherwise, 0 will be returned.
20619 **/
20620static uint32_t
20621lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
20622{
20623 LPFC_MBOXQ_t *mboxq = NULL;
20624 struct lpfc_dmabuf *mp = NULL;
20625 struct lpfc_mqe *mqe;
20626 uint32_t data_length = 0;
20627 int rc;
20628
20629 if (!rgn23_data)
20630 return 0;
20631
20632 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20633 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020634 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart026abb82011-12-13 13:20:45 -050020635 "3105 failed to allocate mailbox memory\n");
20636 return 0;
20637 }
20638
20639 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
20640 goto out;
20641 mqe = &mboxq->u.mqe;
James Smart3e1f0712018-11-29 16:09:29 -080020642 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smart026abb82011-12-13 13:20:45 -050020643 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
20644 if (rc)
20645 goto out;
20646 data_length = mqe->un.mb_words[5];
20647 if (data_length == 0)
20648 goto out;
20649 if (data_length > DMP_RGN23_SIZE) {
20650 data_length = 0;
20651 goto out;
20652 }
20653 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
20654out:
20655 mempool_free(mboxq, phba->mbox_mem_pool);
20656 if (mp) {
20657 lpfc_mbuf_free(phba, mp->virt, mp->phys);
20658 kfree(mp);
20659 }
20660 return data_length;
20661}
20662
20663/**
20664 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
20665 * @phba: pointer to lpfc hba data structure.
20666 *
20667 * This function read region 23 and parse TLV for port status to
20668 * decide if the user disaled the port. If the TLV indicates the
20669 * port is disabled, the hba_flag is set accordingly.
20670 **/
20671void
20672lpfc_sli_read_link_ste(struct lpfc_hba *phba)
20673{
20674 uint8_t *rgn23_data = NULL;
20675 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
20676 uint32_t offset = 0;
20677
20678 /* Get adapter Region 23 data */
20679 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
20680 if (!rgn23_data)
20681 goto out;
20682
20683 if (phba->sli_rev < LPFC_SLI_REV4)
20684 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
20685 else {
20686 if_type = bf_get(lpfc_sli_intf_if_type,
20687 &phba->sli4_hba.sli_intf);
20688 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
20689 goto out;
20690 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
20691 }
James Smarta0c87cb2009-07-19 10:01:10 -040020692
20693 if (!data_size)
20694 goto out;
20695
20696 /* Check the region signature first */
20697 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020698 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta0c87cb2009-07-19 10:01:10 -040020699 "2619 Config region 23 has bad signature\n");
20700 goto out;
20701 }
20702 offset += 4;
20703
20704 /* Check the data structure version */
20705 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020706 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta0c87cb2009-07-19 10:01:10 -040020707 "2620 Config region 23 has bad version\n");
20708 goto out;
20709 }
20710 offset += 4;
20711
20712 /* Parse TLV entries in the region */
20713 while (offset < data_size) {
20714 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
20715 break;
20716 /*
20717 * If the TLV is not driver specific TLV or driver id is
20718 * not linux driver id, skip the record.
20719 */
20720 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
20721 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
20722 (rgn23_data[offset + 3] != 0)) {
20723 offset += rgn23_data[offset + 1] * 4 + 4;
20724 continue;
20725 }
20726
20727 /* Driver found a driver specific TLV in the config region */
20728 sub_tlv_len = rgn23_data[offset + 1] * 4;
20729 offset += 4;
20730 tlv_offset = 0;
20731
20732 /*
20733 * Search for configured port state sub-TLV.
20734 */
20735 while ((offset < data_size) &&
20736 (tlv_offset < sub_tlv_len)) {
20737 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
20738 offset += 4;
20739 tlv_offset += 4;
20740 break;
20741 }
20742 if (rgn23_data[offset] != PORT_STE_TYPE) {
20743 offset += rgn23_data[offset + 1] * 4 + 4;
20744 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
20745 continue;
20746 }
20747
20748 /* This HBA contains PORT_STE configured */
20749 if (!rgn23_data[offset + 2])
20750 phba->hba_flag |= LINK_DISABLED;
20751
20752 goto out;
20753 }
20754 }
James Smart026abb82011-12-13 13:20:45 -050020755
James Smarta0c87cb2009-07-19 10:01:10 -040020756out:
James Smarta0c87cb2009-07-19 10:01:10 -040020757 kfree(rgn23_data);
20758 return;
20759}
James Smart695a8142010-01-26 23:08:03 -050020760
20761/**
James Smart16a93e82021-07-07 11:43:34 -070020762 * lpfc_log_fw_write_cmpl - logs firmware write completion status
20763 * @phba: pointer to lpfc hba data structure
20764 * @shdr_status: wr_object rsp's status field
20765 * @shdr_add_status: wr_object rsp's add_status field
20766 * @shdr_add_status_2: wr_object rsp's add_status_2 field
20767 * @shdr_change_status: wr_object rsp's change_status field
20768 * @shdr_csf: wr_object rsp's csf bit
20769 *
20770 * This routine is intended to be called after a firmware write completes.
20771 * It will log next action items to be performed by the user to instantiate
20772 * the newly downloaded firmware or reason for incompatibility.
20773 **/
20774static void
20775lpfc_log_fw_write_cmpl(struct lpfc_hba *phba, u32 shdr_status,
20776 u32 shdr_add_status, u32 shdr_add_status_2,
20777 u32 shdr_change_status, u32 shdr_csf)
20778{
20779 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20780 "4198 %s: flash_id x%02x, asic_rev x%02x, "
20781 "status x%02x, add_status x%02x, add_status_2 x%02x, "
20782 "change_status x%02x, csf %01x\n", __func__,
20783 phba->sli4_hba.flash_id, phba->sli4_hba.asic_rev,
20784 shdr_status, shdr_add_status, shdr_add_status_2,
20785 shdr_change_status, shdr_csf);
20786
20787 if (shdr_add_status == LPFC_ADD_STATUS_INCOMPAT_OBJ) {
20788 switch (shdr_add_status_2) {
20789 case LPFC_ADD_STATUS_2_INCOMPAT_FLASH:
20790 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20791 "4199 Firmware write failed: "
20792 "image incompatible with flash x%02x\n",
20793 phba->sli4_hba.flash_id);
20794 break;
20795 case LPFC_ADD_STATUS_2_INCORRECT_ASIC:
20796 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20797 "4200 Firmware write failed: "
20798 "image incompatible with ASIC "
20799 "architecture x%02x\n",
20800 phba->sli4_hba.asic_rev);
20801 break;
20802 default:
20803 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
20804 "4210 Firmware write failed: "
20805 "add_status_2 x%02x\n",
20806 shdr_add_status_2);
20807 break;
20808 }
20809 } else if (!shdr_status && !shdr_add_status) {
20810 if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
20811 shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
20812 if (shdr_csf)
20813 shdr_change_status =
20814 LPFC_CHANGE_STATUS_PCI_RESET;
20815 }
20816
20817 switch (shdr_change_status) {
20818 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
20819 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20820 "3198 Firmware write complete: System "
20821 "reboot required to instantiate\n");
20822 break;
20823 case (LPFC_CHANGE_STATUS_FW_RESET):
20824 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20825 "3199 Firmware write complete: "
20826 "Firmware reset required to "
20827 "instantiate\n");
20828 break;
20829 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
20830 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20831 "3200 Firmware write complete: Port "
20832 "Migration or PCI Reset required to "
20833 "instantiate\n");
20834 break;
20835 case (LPFC_CHANGE_STATUS_PCI_RESET):
20836 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
20837 "3201 Firmware write complete: PCI "
20838 "Reset required to instantiate\n");
20839 break;
20840 default:
20841 break;
20842 }
20843 }
20844}
20845
20846/**
James Smart52d52442011-05-24 11:42:45 -040020847 * lpfc_wr_object - write an object to the firmware
20848 * @phba: HBA structure that indicates port to create a queue on.
20849 * @dmabuf_list: list of dmabufs to write to the port.
20850 * @size: the total byte value of the objects to write to the port.
20851 * @offset: the current offset to be used to start the transfer.
20852 *
20853 * This routine will create a wr_object mailbox command to send to the port.
20854 * the mailbox command will be constructed using the dma buffers described in
20855 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
20856 * BDEs that the imbedded mailbox can support. The @offset variable will be
20857 * used to indicate the starting offset of the transfer and will also return
20858 * the offset after the write object mailbox has completed. @size is used to
20859 * determine the end of the object and whether the eof bit should be set.
20860 *
20861 * Return 0 is successful and offset will contain the the new offset to use
20862 * for the next write.
20863 * Return negative value for error cases.
20864 **/
20865int
20866lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
20867 uint32_t size, uint32_t *offset)
20868{
20869 struct lpfc_mbx_wr_object *wr_object;
20870 LPFC_MBOXQ_t *mbox;
20871 int rc = 0, i = 0;
James Smart16a93e82021-07-07 11:43:34 -070020872 uint32_t shdr_status, shdr_add_status, shdr_add_status_2;
20873 uint32_t shdr_change_status = 0, shdr_csf = 0;
James Smart52d52442011-05-24 11:42:45 -040020874 uint32_t mbox_tmo;
James Smart52d52442011-05-24 11:42:45 -040020875 struct lpfc_dmabuf *dmabuf;
20876 uint32_t written = 0;
James Smart50212672018-12-13 15:17:57 -080020877 bool check_change_status = false;
James Smart52d52442011-05-24 11:42:45 -040020878
20879 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20880 if (!mbox)
20881 return -ENOMEM;
20882
20883 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
20884 LPFC_MBOX_OPCODE_WRITE_OBJECT,
20885 sizeof(struct lpfc_mbx_wr_object) -
20886 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
20887
20888 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
20889 wr_object->u.request.write_offset = *offset;
20890 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
20891 wr_object->u.request.object_name[0] =
20892 cpu_to_le32(wr_object->u.request.object_name[0]);
20893 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
20894 list_for_each_entry(dmabuf, dmabuf_list, list) {
20895 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
20896 break;
20897 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
20898 wr_object->u.request.bde[i].addrHigh =
20899 putPaddrHigh(dmabuf->phys);
20900 if (written + SLI4_PAGE_SIZE >= size) {
20901 wr_object->u.request.bde[i].tus.f.bdeSize =
20902 (size - written);
20903 written += (size - written);
20904 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
James Smart50212672018-12-13 15:17:57 -080020905 bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
20906 check_change_status = true;
James Smart52d52442011-05-24 11:42:45 -040020907 } else {
20908 wr_object->u.request.bde[i].tus.f.bdeSize =
20909 SLI4_PAGE_SIZE;
20910 written += SLI4_PAGE_SIZE;
20911 }
20912 i++;
20913 }
20914 wr_object->u.request.bde_count = i;
20915 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
20916 if (!phba->sli4_hba.intr_enable)
20917 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
20918 else {
James Smarta183a152011-10-10 21:32:43 -040020919 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040020920 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
20921 }
20922 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart50212672018-12-13 15:17:57 -080020923 shdr_status = bf_get(lpfc_mbox_hdr_status,
20924 &wr_object->header.cfg_shdr.response);
20925 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20926 &wr_object->header.cfg_shdr.response);
James Smart16a93e82021-07-07 11:43:34 -070020927 shdr_add_status_2 = bf_get(lpfc_mbox_hdr_add_status_2,
20928 &wr_object->header.cfg_shdr.response);
James Smart50212672018-12-13 15:17:57 -080020929 if (check_change_status) {
20930 shdr_change_status = bf_get(lpfc_wr_object_change_status,
20931 &wr_object->u.response);
James Smart16a93e82021-07-07 11:43:34 -070020932 shdr_csf = bf_get(lpfc_wr_object_csf,
20933 &wr_object->u.response);
James Smart50212672018-12-13 15:17:57 -080020934 }
James Smart16a93e82021-07-07 11:43:34 -070020935
James Smart304ee432021-04-11 18:31:17 -070020936 if (!phba->sli4_hba.intr_enable)
20937 mempool_free(mbox, phba->mbox_mem_pool);
20938 else if (rc != MBX_TIMEOUT)
James Smart52d52442011-05-24 11:42:45 -040020939 mempool_free(mbox, phba->mbox_mem_pool);
James Smart16a93e82021-07-07 11:43:34 -070020940 if (shdr_status || shdr_add_status || shdr_add_status_2 || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020941 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart52d52442011-05-24 11:42:45 -040020942 "3025 Write Object mailbox failed with "
James Smart16a93e82021-07-07 11:43:34 -070020943 "status x%x add_status x%x, add_status_2 x%x, "
20944 "mbx status x%x\n",
20945 shdr_status, shdr_add_status, shdr_add_status_2,
20946 rc);
James Smart52d52442011-05-24 11:42:45 -040020947 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080020948 *offset = shdr_add_status;
James Smart16a93e82021-07-07 11:43:34 -070020949 } else {
James Smart52d52442011-05-24 11:42:45 -040020950 *offset += wr_object->u.response.actual_write_length;
James Smart16a93e82021-07-07 11:43:34 -070020951 }
20952
20953 if (rc || check_change_status)
20954 lpfc_log_fw_write_cmpl(phba, shdr_status, shdr_add_status,
20955 shdr_add_status_2, shdr_change_status,
20956 shdr_csf);
James Smart52d52442011-05-24 11:42:45 -040020957 return rc;
20958}
20959
20960/**
James Smart695a8142010-01-26 23:08:03 -050020961 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
20962 * @vport: pointer to vport data structure.
20963 *
20964 * This function iterate through the mailboxq and clean up all REG_LOGIN
20965 * and REG_VPI mailbox commands associated with the vport. This function
20966 * is called when driver want to restart discovery of the vport due to
20967 * a Clear Virtual Link event.
20968 **/
20969void
20970lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
20971{
20972 struct lpfc_hba *phba = vport->phba;
20973 LPFC_MBOXQ_t *mb, *nextmb;
20974 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040020975 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040020976 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smartd439d282010-09-29 11:18:45 -040020977 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050020978 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050020979
James Smartd439d282010-09-29 11:18:45 -040020980 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050020981 spin_lock_irq(&phba->hbalock);
20982 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
20983 if (mb->vport != vport)
20984 continue;
20985
20986 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
20987 (mb->u.mb.mbxCommand != MBX_REG_VPI))
20988 continue;
20989
Zou Wei47018082021-06-08 08:51:33 +080020990 list_move_tail(&mb->list, &mbox_cmd_list);
James Smartd439d282010-09-29 11:18:45 -040020991 }
20992 /* Clean up active mailbox command with the vport */
20993 mb = phba->sli.mbox_active;
20994 if (mb && (mb->vport == vport)) {
20995 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
20996 (mb->u.mb.mbxCommand == MBX_REG_VPI))
20997 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
20998 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080020999 act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -040021000 /* Put reference count for delayed processing */
21001 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
21002 /* Unregister the RPI when mailbox complete */
21003 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21004 }
21005 }
James Smart63e801c2010-11-20 23:14:19 -050021006 /* Cleanup any mailbox completions which are not yet processed */
21007 do {
21008 restart_loop = 0;
21009 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
21010 /*
21011 * If this mailox is already processed or it is
21012 * for another vport ignore it.
21013 */
21014 if ((mb->vport != vport) ||
21015 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
21016 continue;
21017
21018 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
21019 (mb->u.mb.mbxCommand != MBX_REG_VPI))
21020 continue;
21021
21022 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
21023 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080021024 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smart63e801c2010-11-20 23:14:19 -050021025 /* Unregister the RPI when mailbox complete */
21026 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
21027 restart_loop = 1;
21028 spin_unlock_irq(&phba->hbalock);
James Smartc6adba12020-11-15 11:26:34 -080021029 spin_lock(&ndlp->lock);
James Smart63e801c2010-11-20 23:14:19 -050021030 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
James Smartc6adba12020-11-15 11:26:34 -080021031 spin_unlock(&ndlp->lock);
James Smart63e801c2010-11-20 23:14:19 -050021032 spin_lock_irq(&phba->hbalock);
21033 break;
21034 }
21035 }
21036 } while (restart_loop);
21037
James Smartd439d282010-09-29 11:18:45 -040021038 spin_unlock_irq(&phba->hbalock);
21039
21040 /* Release the cleaned-up mailbox commands */
21041 while (!list_empty(&mbox_cmd_list)) {
21042 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050021043 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080021044 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
James Smart695a8142010-01-26 23:08:03 -050021045 if (mp) {
21046 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
21047 kfree(mp);
21048 }
James Smart3e1f0712018-11-29 16:09:29 -080021049 mb->ctx_buf = NULL;
21050 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
21051 mb->ctx_ndlp = NULL;
James Smart78730cf2010-04-06 15:06:30 -040021052 if (ndlp) {
James Smartc6adba12020-11-15 11:26:34 -080021053 spin_lock(&ndlp->lock);
James Smart589a52d2010-07-14 15:30:54 -040021054 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
James Smartc6adba12020-11-15 11:26:34 -080021055 spin_unlock(&ndlp->lock);
James Smart78730cf2010-04-06 15:06:30 -040021056 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040021057 }
James Smart695a8142010-01-26 23:08:03 -050021058 }
James Smart695a8142010-01-26 23:08:03 -050021059 mempool_free(mb, phba->mbox_mem_pool);
21060 }
James Smartd439d282010-09-29 11:18:45 -040021061
21062 /* Release the ndlp with the cleaned-up active mailbox command */
21063 if (act_mbx_ndlp) {
James Smartc6adba12020-11-15 11:26:34 -080021064 spin_lock(&act_mbx_ndlp->lock);
James Smartd439d282010-09-29 11:18:45 -040021065 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
James Smartc6adba12020-11-15 11:26:34 -080021066 spin_unlock(&act_mbx_ndlp->lock);
James Smartd439d282010-09-29 11:18:45 -040021067 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050021068 }
James Smart695a8142010-01-26 23:08:03 -050021069}
21070
James Smart2a9bf3d2010-06-07 15:24:45 -040021071/**
21072 * lpfc_drain_txq - Drain the txq
21073 * @phba: Pointer to HBA context object.
21074 *
21075 * This function attempt to submit IOCBs on the txq
21076 * to the adapter. For SLI4 adapters, the txq contains
21077 * ELS IOCBs that have been deferred because the there
21078 * are no SGLs. This congestion can occur with large
21079 * vport counts during node discovery.
21080 **/
21081
21082uint32_t
21083lpfc_drain_txq(struct lpfc_hba *phba)
21084{
21085 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080021086 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090021087 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040021088 unsigned long iflags = 0;
21089 char *fail_msg = NULL;
21090 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080021091 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040021092 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070021093 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040021094
James Smartdc19e3b2018-05-24 21:08:57 -070021095 if (phba->link_flag & LS_MDS_LOOPBACK) {
21096 /* MDS WQE are posted only to first WQ*/
James Smartc00f62e2019-08-14 16:57:11 -070021097 wq = phba->sli4_hba.hdwq[0].io_wq;
James Smartdc19e3b2018-05-24 21:08:57 -070021098 if (unlikely(!wq))
21099 return 0;
21100 pring = wq->pring;
21101 } else {
21102 wq = phba->sli4_hba.els_wq;
21103 if (unlikely(!wq))
21104 return 0;
21105 pring = lpfc_phba_elsring(phba);
21106 }
21107
21108 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070021109 return 0;
James Smart895427b2017-02-12 13:52:30 -080021110
James Smart398d81c2013-05-31 17:04:19 -040021111 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050021112 list_for_each_entry(piocbq, &pring->txq, list) {
21113 txq_cnt++;
21114 }
21115
21116 if (txq_cnt > pring->txq_max)
21117 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040021118
James Smart398d81c2013-05-31 17:04:19 -040021119 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040021120
James Smart0e9bb8d2013-03-01 16:35:12 -050021121 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040021122 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040021123
James Smart19ca7602010-11-20 23:11:55 -050021124 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040021125 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040021126 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedy372c1872020-06-30 14:50:00 -070021127 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta6298522012-06-12 13:54:11 -040021128 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050021129 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040021130 break;
21131 }
James Smart895427b2017-02-12 13:52:30 -080021132 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040021133 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050021134 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040021135 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040021136 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040021137 }
James Smart0e9bb8d2013-03-01 16:35:12 -050021138 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040021139
21140 /* The xri and iocb resources secured,
21141 * attempt to issue request
21142 */
James Smart6d368e52011-05-24 11:44:12 -040021143 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040021144 piocbq->sli4_xritag = sglq->sli4_xritag;
21145 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
21146 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080021147 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040021148 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070021149 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040021150 fail_msg = " - Wq is full";
21151 else
21152 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
21153
21154 if (fail_msg) {
21155 /* Failed means we can't issue and need to cancel */
Dick Kennedy372c1872020-06-30 14:50:00 -070021156 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2a9bf3d2010-06-07 15:24:45 -040021157 "2822 IOCB failed %s iotag 0x%x "
21158 "xri 0x%x\n",
21159 fail_msg,
21160 piocbq->iotag, piocbq->sli4_xritag);
21161 list_add_tail(&piocbq->list, &completions);
James Smart99154582021-09-10 16:31:46 -070021162 fail_msg = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040021163 }
James Smart398d81c2013-05-31 17:04:19 -040021164 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040021165 }
21166
James Smart2a9bf3d2010-06-07 15:24:45 -040021167 /* Cancel all the IOCBs that cannot be issued */
21168 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
21169 IOERR_SLI_ABORTED);
21170
James Smart0e9bb8d2013-03-01 16:35:12 -050021171 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040021172}
James Smart895427b2017-02-12 13:52:30 -080021173
21174/**
21175 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
21176 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010021177 * @pwqeq: Pointer to command WQE.
James Smart895427b2017-02-12 13:52:30 -080021178 * @sglq: Pointer to the scatter gather queue object.
21179 *
21180 * This routine converts the bpl or bde that is in the WQE
21181 * to a sgl list for the sli4 hardware. The physical address
21182 * of the bpl/bde is converted back to a virtual address.
21183 * If the WQE contains a BPL then the list of BDE's is
21184 * converted to sli4_sge's. If the WQE contains a single
21185 * BDE then it is converted to a single sli_sge.
21186 * The WQE is still in cpu endianness so the contents of
21187 * the bpl can be used without byte swapping.
21188 *
21189 * Returns valid XRI = Success, NO_XRI = Failure.
21190 */
21191static uint16_t
21192lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
21193 struct lpfc_sglq *sglq)
21194{
21195 uint16_t xritag = NO_XRI;
21196 struct ulp_bde64 *bpl = NULL;
21197 struct ulp_bde64 bde;
21198 struct sli4_sge *sgl = NULL;
21199 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080021200 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080021201 int numBdes = 0;
21202 int i = 0;
21203 uint32_t offset = 0; /* accumulated offset in the sg request list */
21204 int inbound = 0; /* number of sg reply entries inbound from firmware */
21205 uint32_t cmd;
21206
21207 if (!pwqeq || !sglq)
21208 return xritag;
21209
21210 sgl = (struct sli4_sge *)sglq->sgl;
21211 wqe = &pwqeq->wqe;
21212 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
21213
21214 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
21215 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
21216 return sglq->sli4_xritag;
21217 numBdes = pwqeq->rsvd2;
21218 if (numBdes) {
21219 /* The addrHigh and addrLow fields within the WQE
21220 * have not been byteswapped yet so there is no
21221 * need to swap them back.
21222 */
21223 if (pwqeq->context3)
21224 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
21225 else
21226 return xritag;
21227
21228 bpl = (struct ulp_bde64 *)dmabuf->virt;
21229 if (!bpl)
21230 return xritag;
21231
21232 for (i = 0; i < numBdes; i++) {
21233 /* Should already be byte swapped. */
21234 sgl->addr_hi = bpl->addrHigh;
21235 sgl->addr_lo = bpl->addrLow;
21236
21237 sgl->word2 = le32_to_cpu(sgl->word2);
21238 if ((i+1) == numBdes)
21239 bf_set(lpfc_sli4_sge_last, sgl, 1);
21240 else
21241 bf_set(lpfc_sli4_sge_last, sgl, 0);
21242 /* swap the size field back to the cpu so we
21243 * can assign it to the sgl.
21244 */
21245 bde.tus.w = le32_to_cpu(bpl->tus.w);
21246 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
21247 /* The offsets in the sgl need to be accumulated
21248 * separately for the request and reply lists.
21249 * The request is always first, the reply follows.
21250 */
21251 switch (cmd) {
21252 case CMD_GEN_REQUEST64_WQE:
21253 /* add up the reply sg entries */
21254 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
21255 inbound++;
21256 /* first inbound? reset the offset */
21257 if (inbound == 1)
21258 offset = 0;
21259 bf_set(lpfc_sli4_sge_offset, sgl, offset);
21260 bf_set(lpfc_sli4_sge_type, sgl,
21261 LPFC_SGE_TYPE_DATA);
21262 offset += bde.tus.f.bdeSize;
21263 break;
21264 case CMD_FCP_TRSP64_WQE:
21265 bf_set(lpfc_sli4_sge_offset, sgl, 0);
21266 bf_set(lpfc_sli4_sge_type, sgl,
21267 LPFC_SGE_TYPE_DATA);
21268 break;
21269 case CMD_FCP_TSEND64_WQE:
21270 case CMD_FCP_TRECEIVE64_WQE:
21271 bf_set(lpfc_sli4_sge_type, sgl,
21272 bpl->tus.f.bdeFlags);
21273 if (i < 3)
21274 offset = 0;
21275 else
21276 offset += bde.tus.f.bdeSize;
21277 bf_set(lpfc_sli4_sge_offset, sgl, offset);
21278 break;
21279 }
21280 sgl->word2 = cpu_to_le32(sgl->word2);
21281 bpl++;
21282 sgl++;
21283 }
21284 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
21285 /* The addrHigh and addrLow fields of the BDE have not
21286 * been byteswapped yet so they need to be swapped
21287 * before putting them in the sgl.
21288 */
21289 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
21290 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
21291 sgl->word2 = le32_to_cpu(sgl->word2);
21292 bf_set(lpfc_sli4_sge_last, sgl, 1);
21293 sgl->word2 = cpu_to_le32(sgl->word2);
21294 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
21295 }
21296 return sglq->sli4_xritag;
21297}
21298
21299/**
21300 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
21301 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010021302 * @qp: Pointer to HDW queue.
James Smart895427b2017-02-12 13:52:30 -080021303 * @pwqe: Pointer to command WQE.
21304 **/
21305int
James Smart1fbf9742019-01-28 11:14:26 -080021306lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
James Smart895427b2017-02-12 13:52:30 -080021307 struct lpfc_iocbq *pwqe)
21308{
James Smart205e8242018-03-05 12:04:03 -080021309 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smart7cacae22020-03-31 09:50:03 -070021310 struct lpfc_async_xchg_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080021311 struct lpfc_queue *wq;
21312 struct lpfc_sglq *sglq;
21313 struct lpfc_sli_ring *pring;
21314 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070021315 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080021316
21317 /* NVME_LS and NVME_LS ABTS requests. */
21318 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
21319 pring = phba->sli4_hba.nvmels_wq->pring;
James Smart6a828b02019-01-28 11:14:31 -080021320 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21321 qp, wq_access);
James Smart895427b2017-02-12 13:52:30 -080021322 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
21323 if (!sglq) {
21324 spin_unlock_irqrestore(&pring->ring_lock, iflags);
21325 return WQE_BUSY;
21326 }
21327 pwqe->sli4_lxritag = sglq->sli4_lxritag;
21328 pwqe->sli4_xritag = sglq->sli4_xritag;
21329 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
21330 spin_unlock_irqrestore(&pring->ring_lock, iflags);
21331 return WQE_ERROR;
21332 }
21333 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21334 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070021335 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
21336 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080021337 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070021338 return ret;
James Smart895427b2017-02-12 13:52:30 -080021339 }
Dick Kennedycd22d602017-08-23 16:55:35 -070021340
James Smart895427b2017-02-12 13:52:30 -080021341 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21342 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080021343
21344 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smart895427b2017-02-12 13:52:30 -080021345 return 0;
21346 }
21347
21348 /* NVME_FCREQ and NVME_ABTS requests */
James Smart02243832021-08-16 09:28:54 -070021349 if (pwqe->iocb_flag & (LPFC_IO_NVME | LPFC_IO_FCP | LPFC_IO_CMF)) {
James Smart895427b2017-02-12 13:52:30 -080021350 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070021351 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080021352 pring = wq->pring;
James Smart895427b2017-02-12 13:52:30 -080021353
James Smartc00f62e2019-08-14 16:57:11 -070021354 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart895427b2017-02-12 13:52:30 -080021355
James Smart6a828b02019-01-28 11:14:31 -080021356 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21357 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070021358 ret = lpfc_sli4_wq_put(wq, wqe);
21359 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080021360 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070021361 return ret;
James Smart895427b2017-02-12 13:52:30 -080021362 }
21363 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21364 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080021365
21366 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smart895427b2017-02-12 13:52:30 -080021367 return 0;
21368 }
21369
James Smartf358dd02017-02-12 13:52:34 -080021370 /* NVMET requests */
21371 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
21372 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070021373 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080021374 pring = wq->pring;
James Smartf358dd02017-02-12 13:52:34 -080021375
James Smartf358dd02017-02-12 13:52:34 -080021376 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070021377 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080021378 if (pwqe->sli4_xritag == NO_XRI) {
21379 pwqe->sli4_lxritag = sglq->sli4_lxritag;
21380 pwqe->sli4_xritag = sglq->sli4_xritag;
21381 }
21382 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
21383 pwqe->sli4_xritag);
James Smartc00f62e2019-08-14 16:57:11 -070021384 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart1fbf9742019-01-28 11:14:26 -080021385
James Smart6a828b02019-01-28 11:14:31 -080021386 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
21387 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070021388 ret = lpfc_sli4_wq_put(wq, wqe);
21389 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080021390 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070021391 return ret;
James Smartf358dd02017-02-12 13:52:34 -080021392 }
21393 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
21394 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080021395
21396 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smartf358dd02017-02-12 13:52:34 -080021397 return 0;
21398 }
James Smart895427b2017-02-12 13:52:30 -080021399 return WQE_ERROR;
21400}
James Smartc4908502019-01-28 11:14:28 -080021401
James Smartdb7531d2020-11-15 11:26:44 -080021402/**
21403 * lpfc_sli4_issue_abort_iotag - SLI-4 WQE init & issue for the Abort
21404 * @phba: Pointer to HBA context object.
21405 * @cmdiocb: Pointer to driver command iocb object.
21406 * @cmpl: completion function.
21407 *
21408 * Fill the appropriate fields for the abort WQE and call
21409 * internal routine lpfc_sli4_issue_wqe to send the WQE
21410 * This function is called with hbalock held and no ring_lock held.
21411 *
21412 * RETURNS 0 - SUCCESS
21413 **/
21414
21415int
21416lpfc_sli4_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
21417 void *cmpl)
21418{
21419 struct lpfc_vport *vport = cmdiocb->vport;
21420 struct lpfc_iocbq *abtsiocb = NULL;
21421 union lpfc_wqe128 *abtswqe;
21422 struct lpfc_io_buf *lpfc_cmd;
21423 int retval = IOCB_ERROR;
21424 u16 xritag = cmdiocb->sli4_xritag;
21425
21426 /*
21427 * The scsi command can not be in txq and it is in flight because the
21428 * pCmd is still pointing at the SCSI command we have to abort. There
21429 * is no need to search the txcmplq. Just send an abort to the FW.
21430 */
21431
21432 abtsiocb = __lpfc_sli_get_iocbq(phba);
21433 if (!abtsiocb)
21434 return WQE_NORESOURCE;
21435
21436 /* Indicate the IO is being aborted by the driver. */
21437 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
21438
21439 abtswqe = &abtsiocb->wqe;
21440 memset(abtswqe, 0, sizeof(*abtswqe));
21441
James Smart696770e2021-05-28 14:22:40 -070021442 if (!lpfc_is_link_up(phba))
James Smartdb7531d2020-11-15 11:26:44 -080021443 bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 1);
James Smartdb7531d2020-11-15 11:26:44 -080021444 bf_set(abort_cmd_criteria, &abtswqe->abort_cmd, T_XRI_TAG);
21445 abtswqe->abort_cmd.rsrvd5 = 0;
21446 abtswqe->abort_cmd.wqe_com.abort_tag = xritag;
21447 bf_set(wqe_reqtag, &abtswqe->abort_cmd.wqe_com, abtsiocb->iotag);
21448 bf_set(wqe_cmnd, &abtswqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
21449 bf_set(wqe_xri_tag, &abtswqe->generic.wqe_com, 0);
21450 bf_set(wqe_qosd, &abtswqe->abort_cmd.wqe_com, 1);
21451 bf_set(wqe_lenloc, &abtswqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
21452 bf_set(wqe_cmd_type, &abtswqe->abort_cmd.wqe_com, OTHER_COMMAND);
21453
21454 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
21455 abtsiocb->hba_wqidx = cmdiocb->hba_wqidx;
21456 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
21457 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
21458 abtsiocb->iocb_flag |= LPFC_IO_FCP;
21459 if (cmdiocb->iocb_flag & LPFC_IO_NVME)
21460 abtsiocb->iocb_flag |= LPFC_IO_NVME;
21461 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
21462 abtsiocb->iocb_flag |= LPFC_IO_FOF;
21463 abtsiocb->vport = vport;
21464 abtsiocb->wqe_cmpl = cmpl;
21465
21466 lpfc_cmd = container_of(cmdiocb, struct lpfc_io_buf, cur_iocbq);
21467 retval = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, abtsiocb);
21468
21469 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
21470 "0359 Abort xri x%x, original iotag x%x, "
21471 "abort cmd iotag x%x retval x%x\n",
21472 xritag, cmdiocb->iotag, abtsiocb->iotag, retval);
21473
21474 if (retval) {
21475 cmdiocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
21476 __lpfc_sli_release_iocbq(phba, abtsiocb);
21477 }
21478
21479 return retval;
21480}
21481
James Smartc4908502019-01-28 11:14:28 -080021482#ifdef LPFC_MXP_STAT
21483/**
21484 * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
21485 * @phba: pointer to lpfc hba data structure.
21486 * @hwqid: belong to which HWQ.
21487 *
21488 * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
21489 * 15 seconds after a test case is running.
21490 *
21491 * The user should call lpfc_debugfs_multixripools_write before running a test
21492 * case to clear stat_snapshot_taken. Then the user starts a test case. During
21493 * test case is running, stat_snapshot_taken is incremented by 1 every time when
21494 * this routine is called from heartbeat timer. When stat_snapshot_taken is
21495 * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
21496 **/
21497void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
21498{
21499 struct lpfc_sli4_hdw_queue *qp;
21500 struct lpfc_multixri_pool *multixri_pool;
21501 struct lpfc_pvt_pool *pvt_pool;
21502 struct lpfc_pbl_pool *pbl_pool;
21503 u32 txcmplq_cnt;
21504
21505 qp = &phba->sli4_hba.hdwq[hwqid];
21506 multixri_pool = qp->p_multixri_pool;
21507 if (!multixri_pool)
21508 return;
21509
21510 if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
21511 pvt_pool = &qp->p_multixri_pool->pvt_pool;
21512 pbl_pool = &qp->p_multixri_pool->pbl_pool;
James Smartc00f62e2019-08-14 16:57:11 -070021513 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080021514
21515 multixri_pool->stat_pbl_count = pbl_pool->count;
21516 multixri_pool->stat_pvt_count = pvt_pool->count;
21517 multixri_pool->stat_busy_count = txcmplq_cnt;
21518 }
21519
21520 multixri_pool->stat_snapshot_taken++;
21521}
21522#endif
21523
21524/**
21525 * lpfc_adjust_pvt_pool_count - Adjust private pool count
21526 * @phba: pointer to lpfc hba data structure.
21527 * @hwqid: belong to which HWQ.
21528 *
21529 * This routine moves some XRIs from private to public pool when private pool
21530 * is not busy.
21531 **/
21532void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
21533{
21534 struct lpfc_multixri_pool *multixri_pool;
21535 u32 io_req_count;
21536 u32 prev_io_req_count;
21537
21538 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21539 if (!multixri_pool)
21540 return;
21541 io_req_count = multixri_pool->io_req_count;
21542 prev_io_req_count = multixri_pool->prev_io_req_count;
21543
21544 if (prev_io_req_count != io_req_count) {
21545 /* Private pool is busy */
21546 multixri_pool->prev_io_req_count = io_req_count;
21547 } else {
21548 /* Private pool is not busy.
21549 * Move XRIs from private to public pool.
21550 */
21551 lpfc_move_xri_pvt_to_pbl(phba, hwqid);
21552 }
21553}
21554
21555/**
21556 * lpfc_adjust_high_watermark - Adjust high watermark
21557 * @phba: pointer to lpfc hba data structure.
21558 * @hwqid: belong to which HWQ.
21559 *
21560 * This routine sets high watermark as number of outstanding XRIs,
21561 * but make sure the new value is between xri_limit/2 and xri_limit.
21562 **/
21563void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
21564{
21565 u32 new_watermark;
21566 u32 watermark_max;
21567 u32 watermark_min;
21568 u32 xri_limit;
21569 u32 txcmplq_cnt;
21570 u32 abts_io_bufs;
21571 struct lpfc_multixri_pool *multixri_pool;
21572 struct lpfc_sli4_hdw_queue *qp;
21573
21574 qp = &phba->sli4_hba.hdwq[hwqid];
21575 multixri_pool = qp->p_multixri_pool;
21576 if (!multixri_pool)
21577 return;
21578 xri_limit = multixri_pool->xri_limit;
21579
21580 watermark_max = xri_limit;
21581 watermark_min = xri_limit / 2;
21582
James Smartc00f62e2019-08-14 16:57:11 -070021583 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080021584 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070021585 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080021586
21587 new_watermark = txcmplq_cnt + abts_io_bufs;
21588 new_watermark = min(watermark_max, new_watermark);
21589 new_watermark = max(watermark_min, new_watermark);
21590 multixri_pool->pvt_pool.high_watermark = new_watermark;
21591
21592#ifdef LPFC_MXP_STAT
21593 multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
21594 new_watermark);
21595#endif
21596}
21597
21598/**
21599 * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
21600 * @phba: pointer to lpfc hba data structure.
21601 * @hwqid: belong to which HWQ.
21602 *
21603 * This routine is called from hearbeat timer when pvt_pool is idle.
21604 * All free XRIs are moved from private to public pool on hwqid with 2 steps.
21605 * The first step moves (all - low_watermark) amount of XRIs.
21606 * The second step moves the rest of XRIs.
21607 **/
21608void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
21609{
21610 struct lpfc_pbl_pool *pbl_pool;
21611 struct lpfc_pvt_pool *pvt_pool;
James Smart6a828b02019-01-28 11:14:31 -080021612 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080021613 struct lpfc_io_buf *lpfc_ncmd;
21614 struct lpfc_io_buf *lpfc_ncmd_next;
21615 unsigned long iflag;
21616 struct list_head tmp_list;
21617 u32 tmp_count;
21618
James Smart6a828b02019-01-28 11:14:31 -080021619 qp = &phba->sli4_hba.hdwq[hwqid];
21620 pbl_pool = &qp->p_multixri_pool->pbl_pool;
21621 pvt_pool = &qp->p_multixri_pool->pvt_pool;
James Smartc4908502019-01-28 11:14:28 -080021622 tmp_count = 0;
21623
James Smart6a828b02019-01-28 11:14:31 -080021624 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
21625 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080021626
21627 if (pvt_pool->count > pvt_pool->low_watermark) {
21628 /* Step 1: move (all - low_watermark) from pvt_pool
21629 * to pbl_pool
21630 */
21631
21632 /* Move low watermark of bufs from pvt_pool to tmp_list */
21633 INIT_LIST_HEAD(&tmp_list);
21634 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21635 &pvt_pool->list, list) {
21636 list_move_tail(&lpfc_ncmd->list, &tmp_list);
21637 tmp_count++;
21638 if (tmp_count >= pvt_pool->low_watermark)
21639 break;
21640 }
21641
21642 /* Move all bufs from pvt_pool to pbl_pool */
21643 list_splice_init(&pvt_pool->list, &pbl_pool->list);
21644
21645 /* Move all bufs from tmp_list to pvt_pool */
21646 list_splice(&tmp_list, &pvt_pool->list);
21647
21648 pbl_pool->count += (pvt_pool->count - tmp_count);
21649 pvt_pool->count = tmp_count;
21650 } else {
21651 /* Step 2: move the rest from pvt_pool to pbl_pool */
21652 list_splice_init(&pvt_pool->list, &pbl_pool->list);
21653 pbl_pool->count += pvt_pool->count;
21654 pvt_pool->count = 0;
21655 }
21656
21657 spin_unlock(&pvt_pool->lock);
21658 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21659}
21660
21661/**
21662 * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21663 * @phba: pointer to lpfc hba data structure
Lee Jones7af29d42020-07-21 17:41:31 +010021664 * @qp: pointer to HDW queue
James Smartc4908502019-01-28 11:14:28 -080021665 * @pbl_pool: specified public free XRI pool
21666 * @pvt_pool: specified private free XRI pool
21667 * @count: number of XRIs to move
21668 *
21669 * This routine tries to move some free common bufs from the specified pbl_pool
21670 * to the specified pvt_pool. It might move less than count XRIs if there's not
21671 * enough in public pool.
21672 *
21673 * Return:
21674 * true - if XRIs are successfully moved from the specified pbl_pool to the
21675 * specified pvt_pool
21676 * false - if the specified pbl_pool is empty or locked by someone else
21677 **/
21678static bool
James Smart6a828b02019-01-28 11:14:31 -080021679_lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
21680 struct lpfc_pbl_pool *pbl_pool,
James Smartc4908502019-01-28 11:14:28 -080021681 struct lpfc_pvt_pool *pvt_pool, u32 count)
21682{
21683 struct lpfc_io_buf *lpfc_ncmd;
21684 struct lpfc_io_buf *lpfc_ncmd_next;
21685 unsigned long iflag;
21686 int ret;
21687
21688 ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
21689 if (ret) {
21690 if (pbl_pool->count) {
21691 /* Move a batch of XRIs from public to private pool */
James Smart6a828b02019-01-28 11:14:31 -080021692 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080021693 list_for_each_entry_safe(lpfc_ncmd,
21694 lpfc_ncmd_next,
21695 &pbl_pool->list,
21696 list) {
21697 list_move_tail(&lpfc_ncmd->list,
21698 &pvt_pool->list);
21699 pvt_pool->count++;
21700 pbl_pool->count--;
21701 count--;
21702 if (count == 0)
21703 break;
21704 }
21705
21706 spin_unlock(&pvt_pool->lock);
21707 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21708 return true;
21709 }
21710 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21711 }
21712
21713 return false;
21714}
21715
21716/**
21717 * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
21718 * @phba: pointer to lpfc hba data structure.
21719 * @hwqid: belong to which HWQ.
21720 * @count: number of XRIs to move
21721 *
21722 * This routine tries to find some free common bufs in one of public pools with
21723 * Round Robin method. The search always starts from local hwqid, then the next
21724 * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
21725 * a batch of free common bufs are moved to private pool on hwqid.
21726 * It might move less than count XRIs if there's not enough in public pool.
21727 **/
21728void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
21729{
21730 struct lpfc_multixri_pool *multixri_pool;
21731 struct lpfc_multixri_pool *next_multixri_pool;
21732 struct lpfc_pvt_pool *pvt_pool;
21733 struct lpfc_pbl_pool *pbl_pool;
James Smart6a828b02019-01-28 11:14:31 -080021734 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080021735 u32 next_hwqid;
21736 u32 hwq_count;
21737 int ret;
21738
James Smart6a828b02019-01-28 11:14:31 -080021739 qp = &phba->sli4_hba.hdwq[hwqid];
21740 multixri_pool = qp->p_multixri_pool;
James Smartc4908502019-01-28 11:14:28 -080021741 pvt_pool = &multixri_pool->pvt_pool;
21742 pbl_pool = &multixri_pool->pbl_pool;
21743
21744 /* Check if local pbl_pool is available */
James Smart6a828b02019-01-28 11:14:31 -080021745 ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080021746 if (ret) {
21747#ifdef LPFC_MXP_STAT
21748 multixri_pool->local_pbl_hit_count++;
21749#endif
21750 return;
21751 }
21752
21753 hwq_count = phba->cfg_hdw_queue;
21754
21755 /* Get the next hwqid which was found last time */
21756 next_hwqid = multixri_pool->rrb_next_hwqid;
21757
21758 do {
21759 /* Go to next hwq */
21760 next_hwqid = (next_hwqid + 1) % hwq_count;
21761
21762 next_multixri_pool =
21763 phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
21764 pbl_pool = &next_multixri_pool->pbl_pool;
21765
21766 /* Check if the public free xri pool is available */
21767 ret = _lpfc_move_xri_pbl_to_pvt(
James Smart6a828b02019-01-28 11:14:31 -080021768 phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080021769
21770 /* Exit while-loop if success or all hwqid are checked */
21771 } while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
21772
21773 /* Starting point for the next time */
21774 multixri_pool->rrb_next_hwqid = next_hwqid;
21775
21776 if (!ret) {
21777 /* stats: all public pools are empty*/
21778 multixri_pool->pbl_empty_count++;
21779 }
21780
21781#ifdef LPFC_MXP_STAT
21782 if (ret) {
21783 if (next_hwqid == hwqid)
21784 multixri_pool->local_pbl_hit_count++;
21785 else
21786 multixri_pool->other_pbl_hit_count++;
21787 }
21788#endif
21789}
21790
21791/**
21792 * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
21793 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010021794 * @hwqid: belong to which HWQ.
James Smartc4908502019-01-28 11:14:28 -080021795 *
21796 * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
21797 * low watermark.
21798 **/
21799void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
21800{
21801 struct lpfc_multixri_pool *multixri_pool;
21802 struct lpfc_pvt_pool *pvt_pool;
21803
21804 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
21805 pvt_pool = &multixri_pool->pvt_pool;
21806
21807 if (pvt_pool->count < pvt_pool->low_watermark)
21808 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21809}
21810
21811/**
21812 * lpfc_release_io_buf - Return one IO buf back to free pool
21813 * @phba: pointer to lpfc hba data structure.
21814 * @lpfc_ncmd: IO buf to be returned.
21815 * @qp: belong to which HWQ.
21816 *
21817 * This routine returns one IO buf back to free pool. If this is an urgent IO,
21818 * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
21819 * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
21820 * xri_limit. If cfg_xri_rebalancing==0, the IO buf is returned to
21821 * lpfc_io_buf_list_put.
21822 **/
21823void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
21824 struct lpfc_sli4_hdw_queue *qp)
21825{
21826 unsigned long iflag;
21827 struct lpfc_pbl_pool *pbl_pool;
21828 struct lpfc_pvt_pool *pvt_pool;
21829 struct lpfc_epd_pool *epd_pool;
21830 u32 txcmplq_cnt;
21831 u32 xri_owned;
21832 u32 xri_limit;
21833 u32 abts_io_bufs;
21834
21835 /* MUST zero fields if buffer is reused by another protocol */
21836 lpfc_ncmd->nvmeCmd = NULL;
21837 lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
21838 lpfc_ncmd->cur_iocbq.iocb_cmpl = NULL;
21839
James Smart35a635a2019-09-21 20:59:02 -070021840 if (phba->cfg_xpsgl && !phba->nvmet_support &&
21841 !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
21842 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
21843
21844 if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
21845 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
21846
James Smartc4908502019-01-28 11:14:28 -080021847 if (phba->cfg_xri_rebalancing) {
21848 if (lpfc_ncmd->expedite) {
21849 /* Return to expedite pool */
21850 epd_pool = &phba->epd_pool;
21851 spin_lock_irqsave(&epd_pool->lock, iflag);
21852 list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
21853 epd_pool->count++;
21854 spin_unlock_irqrestore(&epd_pool->lock, iflag);
21855 return;
21856 }
21857
21858 /* Avoid invalid access if an IO sneaks in and is being rejected
21859 * just _after_ xri pools are destroyed in lpfc_offline.
21860 * Nothing much can be done at this point.
21861 */
21862 if (!qp->p_multixri_pool)
21863 return;
21864
21865 pbl_pool = &qp->p_multixri_pool->pbl_pool;
21866 pvt_pool = &qp->p_multixri_pool->pvt_pool;
21867
James Smartc00f62e2019-08-14 16:57:11 -070021868 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080021869 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070021870 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080021871
21872 xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
21873 xri_limit = qp->p_multixri_pool->xri_limit;
21874
21875#ifdef LPFC_MXP_STAT
21876 if (xri_owned <= xri_limit)
21877 qp->p_multixri_pool->below_limit_count++;
21878 else
21879 qp->p_multixri_pool->above_limit_count++;
21880#endif
21881
21882 /* XRI goes to either public or private free xri pool
21883 * based on watermark and xri_limit
21884 */
21885 if ((pvt_pool->count < pvt_pool->low_watermark) ||
21886 (xri_owned < xri_limit &&
21887 pvt_pool->count < pvt_pool->high_watermark)) {
James Smart6a828b02019-01-28 11:14:31 -080021888 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
21889 qp, free_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080021890 list_add_tail(&lpfc_ncmd->list,
21891 &pvt_pool->list);
21892 pvt_pool->count++;
21893 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21894 } else {
James Smart6a828b02019-01-28 11:14:31 -080021895 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
21896 qp, free_pub_pool);
James Smartc4908502019-01-28 11:14:28 -080021897 list_add_tail(&lpfc_ncmd->list,
21898 &pbl_pool->list);
21899 pbl_pool->count++;
21900 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21901 }
21902 } else {
James Smart6a828b02019-01-28 11:14:31 -080021903 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
21904 qp, free_xri);
James Smartc4908502019-01-28 11:14:28 -080021905 list_add_tail(&lpfc_ncmd->list,
21906 &qp->lpfc_io_buf_list_put);
21907 qp->put_io_bufs++;
21908 spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
21909 iflag);
21910 }
21911}
21912
21913/**
21914 * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
21915 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010021916 * @qp: pointer to HDW queue
James Smartc4908502019-01-28 11:14:28 -080021917 * @pvt_pool: pointer to private pool data structure.
21918 * @ndlp: pointer to lpfc nodelist data structure.
21919 *
21920 * This routine tries to get one free IO buf from private pool.
21921 *
21922 * Return:
21923 * pointer to one free IO buf - if private pool is not empty
21924 * NULL - if private pool is empty
21925 **/
21926static struct lpfc_io_buf *
21927lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
James Smart6a828b02019-01-28 11:14:31 -080021928 struct lpfc_sli4_hdw_queue *qp,
James Smartc4908502019-01-28 11:14:28 -080021929 struct lpfc_pvt_pool *pvt_pool,
21930 struct lpfc_nodelist *ndlp)
21931{
21932 struct lpfc_io_buf *lpfc_ncmd;
21933 struct lpfc_io_buf *lpfc_ncmd_next;
21934 unsigned long iflag;
21935
James Smart6a828b02019-01-28 11:14:31 -080021936 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080021937 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21938 &pvt_pool->list, list) {
21939 if (lpfc_test_rrq_active(
21940 phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
21941 continue;
21942 list_del(&lpfc_ncmd->list);
21943 pvt_pool->count--;
21944 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21945 return lpfc_ncmd;
21946 }
21947 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21948
21949 return NULL;
21950}
21951
21952/**
21953 * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
21954 * @phba: pointer to lpfc hba data structure.
21955 *
21956 * This routine tries to get one free IO buf from expedite pool.
21957 *
21958 * Return:
21959 * pointer to one free IO buf - if expedite pool is not empty
21960 * NULL - if expedite pool is empty
21961 **/
21962static struct lpfc_io_buf *
21963lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
21964{
21965 struct lpfc_io_buf *lpfc_ncmd;
21966 struct lpfc_io_buf *lpfc_ncmd_next;
21967 unsigned long iflag;
21968 struct lpfc_epd_pool *epd_pool;
21969
21970 epd_pool = &phba->epd_pool;
21971 lpfc_ncmd = NULL;
21972
21973 spin_lock_irqsave(&epd_pool->lock, iflag);
21974 if (epd_pool->count > 0) {
21975 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21976 &epd_pool->list, list) {
21977 list_del(&lpfc_ncmd->list);
21978 epd_pool->count--;
21979 break;
21980 }
21981 }
21982 spin_unlock_irqrestore(&epd_pool->lock, iflag);
21983
21984 return lpfc_ncmd;
21985}
21986
21987/**
21988 * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
21989 * @phba: pointer to lpfc hba data structure.
21990 * @ndlp: pointer to lpfc nodelist data structure.
21991 * @hwqid: belong to which HWQ
21992 * @expedite: 1 means this request is urgent.
21993 *
21994 * This routine will do the following actions and then return a pointer to
21995 * one free IO buf.
21996 *
21997 * 1. If private free xri count is empty, move some XRIs from public to
21998 * private pool.
21999 * 2. Get one XRI from private free xri pool.
22000 * 3. If we fail to get one from pvt_pool and this is an expedite request,
22001 * get one free xri from expedite pool.
22002 *
22003 * Note: ndlp is only used on SCSI side for RRQ testing.
22004 * The caller should pass NULL for ndlp on NVME side.
22005 *
22006 * Return:
22007 * pointer to one free IO buf - if private pool is not empty
22008 * NULL - if private pool is empty
22009 **/
22010static struct lpfc_io_buf *
22011lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
22012 struct lpfc_nodelist *ndlp,
22013 int hwqid, int expedite)
22014{
22015 struct lpfc_sli4_hdw_queue *qp;
22016 struct lpfc_multixri_pool *multixri_pool;
22017 struct lpfc_pvt_pool *pvt_pool;
22018 struct lpfc_io_buf *lpfc_ncmd;
22019
22020 qp = &phba->sli4_hba.hdwq[hwqid];
22021 lpfc_ncmd = NULL;
James Smart25ac2c972021-09-10 16:31:54 -070022022 if (!qp) {
22023 lpfc_printf_log(phba, KERN_INFO,
22024 LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22025 "5556 NULL qp for hwqid x%x\n", hwqid);
22026 return lpfc_ncmd;
22027 }
James Smartc4908502019-01-28 11:14:28 -080022028 multixri_pool = qp->p_multixri_pool;
James Smart25ac2c972021-09-10 16:31:54 -070022029 if (!multixri_pool) {
22030 lpfc_printf_log(phba, KERN_INFO,
22031 LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22032 "5557 NULL multixri for hwqid x%x\n", hwqid);
22033 return lpfc_ncmd;
22034 }
James Smartc4908502019-01-28 11:14:28 -080022035 pvt_pool = &multixri_pool->pvt_pool;
James Smart25ac2c972021-09-10 16:31:54 -070022036 if (!pvt_pool) {
22037 lpfc_printf_log(phba, KERN_INFO,
22038 LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22039 "5558 NULL pvt_pool for hwqid x%x\n", hwqid);
22040 return lpfc_ncmd;
22041 }
James Smartc4908502019-01-28 11:14:28 -080022042 multixri_pool->io_req_count++;
22043
22044 /* If pvt_pool is empty, move some XRIs from public to private pool */
22045 if (pvt_pool->count == 0)
22046 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
22047
22048 /* Get one XRI from private free xri pool */
James Smart6a828b02019-01-28 11:14:31 -080022049 lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
James Smartc4908502019-01-28 11:14:28 -080022050
22051 if (lpfc_ncmd) {
22052 lpfc_ncmd->hdwq = qp;
22053 lpfc_ncmd->hdwq_no = hwqid;
22054 } else if (expedite) {
22055 /* If we fail to get one from pvt_pool and this is an expedite
22056 * request, get one free xri from expedite pool.
22057 */
22058 lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
22059 }
22060
22061 return lpfc_ncmd;
22062}
22063
22064static inline struct lpfc_io_buf *
22065lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
22066{
22067 struct lpfc_sli4_hdw_queue *qp;
22068 struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
22069
22070 qp = &phba->sli4_hba.hdwq[idx];
22071 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
22072 &qp->lpfc_io_buf_list_get, list) {
22073 if (lpfc_test_rrq_active(phba, ndlp,
22074 lpfc_cmd->cur_iocbq.sli4_lxritag))
22075 continue;
22076
22077 if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
22078 continue;
22079
22080 list_del_init(&lpfc_cmd->list);
22081 qp->get_io_bufs--;
22082 lpfc_cmd->hdwq = qp;
22083 lpfc_cmd->hdwq_no = idx;
22084 return lpfc_cmd;
22085 }
22086 return NULL;
22087}
22088
22089/**
22090 * lpfc_get_io_buf - Get one IO buffer from free pool
22091 * @phba: The HBA for which this call is being executed.
22092 * @ndlp: pointer to lpfc nodelist data structure.
22093 * @hwqid: belong to which HWQ
22094 * @expedite: 1 means this request is urgent.
22095 *
22096 * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
22097 * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
22098 * a IO buffer from head of @hdwq io_buf_list and returns to caller.
22099 *
22100 * Note: ndlp is only used on SCSI side for RRQ testing.
22101 * The caller should pass NULL for ndlp on NVME side.
22102 *
22103 * Return codes:
22104 * NULL - Error
22105 * Pointer to lpfc_io_buf - Success
22106 **/
22107struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
22108 struct lpfc_nodelist *ndlp,
22109 u32 hwqid, int expedite)
22110{
22111 struct lpfc_sli4_hdw_queue *qp;
22112 unsigned long iflag;
22113 struct lpfc_io_buf *lpfc_cmd;
22114
22115 qp = &phba->sli4_hba.hdwq[hwqid];
22116 lpfc_cmd = NULL;
James Smart25ac2c972021-09-10 16:31:54 -070022117 if (!qp) {
22118 lpfc_printf_log(phba, KERN_WARNING,
22119 LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
22120 "5555 NULL qp for hwqid x%x\n", hwqid);
22121 return lpfc_cmd;
22122 }
James Smartc4908502019-01-28 11:14:28 -080022123
22124 if (phba->cfg_xri_rebalancing)
22125 lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
22126 phba, ndlp, hwqid, expedite);
22127 else {
James Smart6a828b02019-01-28 11:14:31 -080022128 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
22129 qp, alloc_xri_get);
James Smartc4908502019-01-28 11:14:28 -080022130 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
22131 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22132 if (!lpfc_cmd) {
James Smart6a828b02019-01-28 11:14:31 -080022133 lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
22134 qp, alloc_xri_put);
James Smartc4908502019-01-28 11:14:28 -080022135 list_splice(&qp->lpfc_io_buf_list_put,
22136 &qp->lpfc_io_buf_list_get);
22137 qp->get_io_bufs += qp->put_io_bufs;
22138 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
22139 qp->put_io_bufs = 0;
22140 spin_unlock(&qp->io_buf_list_put_lock);
22141 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
22142 expedite)
22143 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
22144 }
22145 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
22146 }
22147
22148 return lpfc_cmd;
22149}
James Smartd79c9e92019-08-14 16:57:09 -070022150
22151/**
James Smart72df8a452021-08-16 09:28:52 -070022152 * lpfc_read_object - Retrieve object data from HBA
22153 * @phba: The HBA for which this call is being executed.
22154 * @rdobject: Pathname of object data we want to read.
22155 * @datap: Pointer to where data will be copied to.
22156 * @datasz: size of data area
22157 *
22158 * This routine is limited to object sizes of LPFC_BPL_SIZE (1024) or less.
22159 * The data will be truncated if datasz is not large enough.
22160 * Version 1 is not supported with Embedded mbox cmd, so we must use version 0.
22161 * Returns the actual bytes read from the object.
22162 */
22163int
22164lpfc_read_object(struct lpfc_hba *phba, char *rdobject, uint32_t *datap,
22165 uint32_t datasz)
22166{
22167 struct lpfc_mbx_read_object *read_object;
22168 LPFC_MBOXQ_t *mbox;
22169 int rc, length, eof, j, byte_cnt = 0;
22170 uint32_t shdr_status, shdr_add_status;
22171 union lpfc_sli4_cfg_shdr *shdr;
22172 struct lpfc_dmabuf *pcmd;
James Smart59936432021-08-30 16:12:43 -070022173 u32 rd_object_name[LPFC_MBX_OBJECT_NAME_LEN_DW] = {0};
James Smart72df8a452021-08-16 09:28:52 -070022174
22175 /* sanity check on queue memory */
22176 if (!datap)
22177 return -ENODEV;
22178
22179 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
22180 if (!mbox)
22181 return -ENOMEM;
22182 length = (sizeof(struct lpfc_mbx_read_object) -
22183 sizeof(struct lpfc_sli4_cfg_mhdr));
22184 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
22185 LPFC_MBOX_OPCODE_READ_OBJECT,
22186 length, LPFC_SLI4_MBX_EMBED);
22187 read_object = &mbox->u.mqe.un.read_object;
22188 shdr = (union lpfc_sli4_cfg_shdr *)&read_object->header.cfg_shdr;
22189
22190 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_0);
22191 bf_set(lpfc_mbx_rd_object_rlen, &read_object->u.request, datasz);
22192 read_object->u.request.rd_object_offset = 0;
22193 read_object->u.request.rd_object_cnt = 1;
22194
22195 memset((void *)read_object->u.request.rd_object_name, 0,
22196 LPFC_OBJ_NAME_SZ);
James Smart59936432021-08-30 16:12:43 -070022197 scnprintf((char *)rd_object_name, sizeof(rd_object_name), rdobject);
James Smart72df8a452021-08-16 09:28:52 -070022198 for (j = 0; j < strlen(rdobject); j++)
22199 read_object->u.request.rd_object_name[j] =
James Smart59936432021-08-30 16:12:43 -070022200 cpu_to_le32(rd_object_name[j]);
James Smart72df8a452021-08-16 09:28:52 -070022201
22202 pcmd = kmalloc(sizeof(*pcmd), GFP_KERNEL);
22203 if (pcmd)
22204 pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
22205 if (!pcmd || !pcmd->virt) {
22206 kfree(pcmd);
22207 mempool_free(mbox, phba->mbox_mem_pool);
22208 return -ENOMEM;
22209 }
22210 memset((void *)pcmd->virt, 0, LPFC_BPL_SIZE);
22211 read_object->u.request.rd_object_hbuf[0].pa_lo =
22212 putPaddrLow(pcmd->phys);
22213 read_object->u.request.rd_object_hbuf[0].pa_hi =
22214 putPaddrHigh(pcmd->phys);
22215 read_object->u.request.rd_object_hbuf[0].length = LPFC_BPL_SIZE;
22216
22217 mbox->vport = phba->pport;
22218 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
22219 mbox->ctx_buf = NULL;
22220 mbox->ctx_ndlp = NULL;
22221
22222 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
22223 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
22224 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
22225
22226 if (shdr_status == STATUS_FAILED &&
22227 shdr_add_status == ADD_STATUS_INVALID_OBJECT_NAME) {
22228 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22229 "4674 No port cfg file in FW.\n");
22230 byte_cnt = -ENOENT;
22231 } else if (shdr_status || shdr_add_status || rc) {
22232 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_CGN_MGMT,
22233 "2625 READ_OBJECT mailbox failed with "
22234 "status x%x add_status x%x, mbx status x%x\n",
22235 shdr_status, shdr_add_status, rc);
22236 byte_cnt = -ENXIO;
22237 } else {
22238 /* Success */
22239 length = read_object->u.response.rd_object_actual_rlen;
22240 eof = bf_get(lpfc_mbx_rd_object_eof, &read_object->u.response);
22241 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_CGN_MGMT,
22242 "2626 READ_OBJECT Success len %d:%d, EOF %d\n",
22243 length, datasz, eof);
22244
22245 /* Detect the port config file exists but is empty */
22246 if (!length && eof) {
22247 byte_cnt = 0;
22248 goto exit;
22249 }
22250
22251 byte_cnt = length;
22252 lpfc_sli_pcimem_bcopy(pcmd->virt, datap, byte_cnt);
22253 }
22254
22255 exit:
22256 lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
22257 kfree(pcmd);
22258 mempool_free(mbox, phba->mbox_mem_pool);
22259 return byte_cnt;
22260}
22261
22262/**
James Smartd79c9e92019-08-14 16:57:09 -070022263 * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
22264 * @phba: The HBA for which this call is being executed.
22265 * @lpfc_buf: IO buf structure to append the SGL chunk
22266 *
22267 * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
22268 * and will allocate an SGL chunk if the pool is empty.
22269 *
22270 * Return codes:
22271 * NULL - Error
22272 * Pointer to sli4_hybrid_sgl - Success
22273 **/
22274struct sli4_hybrid_sgl *
22275lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22276{
22277 struct sli4_hybrid_sgl *list_entry = NULL;
22278 struct sli4_hybrid_sgl *tmp = NULL;
22279 struct sli4_hybrid_sgl *allocated_sgl = NULL;
22280 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22281 struct list_head *buf_list = &hdwq->sgl_list;
James Smarta4c21ac2019-09-21 20:59:01 -070022282 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070022283
James Smarta4c21ac2019-09-21 20:59:01 -070022284 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022285
22286 if (likely(!list_empty(buf_list))) {
22287 /* break off 1 chunk from the sgl_list */
22288 list_for_each_entry_safe(list_entry, tmp,
22289 buf_list, list_node) {
22290 list_move_tail(&list_entry->list_node,
22291 &lpfc_buf->dma_sgl_xtra_list);
22292 break;
22293 }
22294 } else {
22295 /* allocate more */
James Smarta4c21ac2019-09-21 20:59:01 -070022296 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022297 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
James Smart4583a4f2019-11-15 16:38:47 -080022298 cpu_to_node(hdwq->io_wq->chann));
James Smartd79c9e92019-08-14 16:57:09 -070022299 if (!tmp) {
22300 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22301 "8353 error kmalloc memory for HDWQ "
22302 "%d %s\n",
22303 lpfc_buf->hdwq_no, __func__);
22304 return NULL;
22305 }
22306
22307 tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
22308 GFP_ATOMIC, &tmp->dma_phys_sgl);
22309 if (!tmp->dma_sgl) {
22310 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22311 "8354 error pool_alloc memory for HDWQ "
22312 "%d %s\n",
22313 lpfc_buf->hdwq_no, __func__);
22314 kfree(tmp);
22315 return NULL;
22316 }
22317
James Smarta4c21ac2019-09-21 20:59:01 -070022318 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022319 list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
22320 }
22321
22322 allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
22323 struct sli4_hybrid_sgl,
22324 list_node);
22325
James Smarta4c21ac2019-09-21 20:59:01 -070022326 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022327
22328 return allocated_sgl;
22329}
22330
22331/**
22332 * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
22333 * @phba: The HBA for which this call is being executed.
22334 * @lpfc_buf: IO buf structure with the SGL chunk
22335 *
22336 * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
22337 *
22338 * Return codes:
22339 * 0 - Success
22340 * -EINVAL - Error
22341 **/
22342int
22343lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
22344{
22345 int rc = 0;
22346 struct sli4_hybrid_sgl *list_entry = NULL;
22347 struct sli4_hybrid_sgl *tmp = NULL;
22348 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22349 struct list_head *buf_list = &hdwq->sgl_list;
James Smarta4c21ac2019-09-21 20:59:01 -070022350 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070022351
James Smarta4c21ac2019-09-21 20:59:01 -070022352 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022353
22354 if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
22355 list_for_each_entry_safe(list_entry, tmp,
22356 &lpfc_buf->dma_sgl_xtra_list,
22357 list_node) {
22358 list_move_tail(&list_entry->list_node,
22359 buf_list);
22360 }
22361 } else {
22362 rc = -EINVAL;
22363 }
22364
James Smarta4c21ac2019-09-21 20:59:01 -070022365 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022366 return rc;
22367}
22368
22369/**
22370 * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
22371 * @phba: phba object
22372 * @hdwq: hdwq to cleanup sgl buff resources on
22373 *
22374 * This routine frees all SGL chunks of hdwq SGL chunk pool.
22375 *
22376 * Return codes:
22377 * None
22378 **/
22379void
22380lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
22381 struct lpfc_sli4_hdw_queue *hdwq)
22382{
22383 struct list_head *buf_list = &hdwq->sgl_list;
22384 struct sli4_hybrid_sgl *list_entry = NULL;
22385 struct sli4_hybrid_sgl *tmp = NULL;
James Smarta4c21ac2019-09-21 20:59:01 -070022386 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070022387
James Smarta4c21ac2019-09-21 20:59:01 -070022388 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022389
22390 /* Free sgl pool */
22391 list_for_each_entry_safe(list_entry, tmp,
22392 buf_list, list_node) {
22393 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
22394 list_entry->dma_sgl,
22395 list_entry->dma_phys_sgl);
22396 list_del(&list_entry->list_node);
22397 kfree(list_entry);
22398 }
22399
James Smarta4c21ac2019-09-21 20:59:01 -070022400 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022401}
22402
22403/**
22404 * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
22405 * @phba: The HBA for which this call is being executed.
22406 * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
22407 *
22408 * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
22409 * and will allocate an CMD/RSP buffer if the pool is empty.
22410 *
22411 * Return codes:
22412 * NULL - Error
22413 * Pointer to fcp_cmd_rsp_buf - Success
22414 **/
22415struct fcp_cmd_rsp_buf *
22416lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22417 struct lpfc_io_buf *lpfc_buf)
22418{
22419 struct fcp_cmd_rsp_buf *list_entry = NULL;
22420 struct fcp_cmd_rsp_buf *tmp = NULL;
22421 struct fcp_cmd_rsp_buf *allocated_buf = NULL;
22422 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22423 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
James Smarta4c21ac2019-09-21 20:59:01 -070022424 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070022425
James Smarta4c21ac2019-09-21 20:59:01 -070022426 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022427
22428 if (likely(!list_empty(buf_list))) {
22429 /* break off 1 chunk from the list */
22430 list_for_each_entry_safe(list_entry, tmp,
22431 buf_list,
22432 list_node) {
22433 list_move_tail(&list_entry->list_node,
22434 &lpfc_buf->dma_cmd_rsp_list);
22435 break;
22436 }
22437 } else {
22438 /* allocate more */
James Smarta4c21ac2019-09-21 20:59:01 -070022439 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022440 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
James Smart4583a4f2019-11-15 16:38:47 -080022441 cpu_to_node(hdwq->io_wq->chann));
James Smartd79c9e92019-08-14 16:57:09 -070022442 if (!tmp) {
22443 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22444 "8355 error kmalloc memory for HDWQ "
22445 "%d %s\n",
22446 lpfc_buf->hdwq_no, __func__);
22447 return NULL;
22448 }
22449
22450 tmp->fcp_cmnd = dma_pool_alloc(phba->lpfc_cmd_rsp_buf_pool,
22451 GFP_ATOMIC,
22452 &tmp->fcp_cmd_rsp_dma_handle);
22453
22454 if (!tmp->fcp_cmnd) {
22455 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
22456 "8356 error pool_alloc memory for HDWQ "
22457 "%d %s\n",
22458 lpfc_buf->hdwq_no, __func__);
22459 kfree(tmp);
22460 return NULL;
22461 }
22462
22463 tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
22464 sizeof(struct fcp_cmnd));
22465
James Smarta4c21ac2019-09-21 20:59:01 -070022466 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022467 list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
22468 }
22469
22470 allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
22471 struct fcp_cmd_rsp_buf,
22472 list_node);
22473
James Smarta4c21ac2019-09-21 20:59:01 -070022474 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022475
22476 return allocated_buf;
22477}
22478
22479/**
22480 * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
22481 * @phba: The HBA for which this call is being executed.
22482 * @lpfc_buf: IO buf structure with the CMD/RSP buf
22483 *
22484 * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
22485 *
22486 * Return codes:
22487 * 0 - Success
22488 * -EINVAL - Error
22489 **/
22490int
22491lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22492 struct lpfc_io_buf *lpfc_buf)
22493{
22494 int rc = 0;
22495 struct fcp_cmd_rsp_buf *list_entry = NULL;
22496 struct fcp_cmd_rsp_buf *tmp = NULL;
22497 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
22498 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
James Smarta4c21ac2019-09-21 20:59:01 -070022499 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070022500
James Smarta4c21ac2019-09-21 20:59:01 -070022501 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022502
22503 if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
22504 list_for_each_entry_safe(list_entry, tmp,
22505 &lpfc_buf->dma_cmd_rsp_list,
22506 list_node) {
22507 list_move_tail(&list_entry->list_node,
22508 buf_list);
22509 }
22510 } else {
22511 rc = -EINVAL;
22512 }
22513
James Smarta4c21ac2019-09-21 20:59:01 -070022514 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022515 return rc;
22516}
22517
22518/**
22519 * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
22520 * @phba: phba object
22521 * @hdwq: hdwq to cleanup cmd rsp buff resources on
22522 *
22523 * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
22524 *
22525 * Return codes:
22526 * None
22527 **/
22528void
22529lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
22530 struct lpfc_sli4_hdw_queue *hdwq)
22531{
22532 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
22533 struct fcp_cmd_rsp_buf *list_entry = NULL;
22534 struct fcp_cmd_rsp_buf *tmp = NULL;
James Smarta4c21ac2019-09-21 20:59:01 -070022535 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070022536
James Smarta4c21ac2019-09-21 20:59:01 -070022537 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022538
22539 /* Free cmd_rsp buf pool */
22540 list_for_each_entry_safe(list_entry, tmp,
22541 buf_list,
22542 list_node) {
22543 dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
22544 list_entry->fcp_cmnd,
22545 list_entry->fcp_cmd_rsp_dma_handle);
22546 list_del(&list_entry->list_node);
22547 kfree(list_entry);
22548 }
22549
James Smarta4c21ac2019-09-21 20:59:01 -070022550 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070022551}