blob: 7832f84706679e5584713b839ffa75f7d1ec4a50 [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 *
938 * This function clears the sglq pointer from the array of acive
939 * sglq's. The xritag that is passed in is used to index into the
940 * array. Before the xritag can be used it needs to be adjusted
941 * by subtracting the xribase.
942 *
943 * Returns sglq ponter = success, NULL = Failure.
944 **/
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 *
960 * This function returns the sglq pointer from the array of acive
961 * sglq's. The xritag that is passed in is used to index into the
962 * array. Before the xritag can be used it needs to be adjusted
963 * by subtracting the xribase.
964 *
965 * Returns sglq ponter = success, NULL = Failure.
966 **/
James Smart0f65ff62010-02-26 14:14:23 -0500967struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400968__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
969{
James Smartda0436e2009-05-22 14:51:39 -0400970 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400971
972 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400973 return sglq;
974}
975
976/**
James Smart1151e3e2011-02-16 12:39:35 -0500977 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500978 * @phba: Pointer to HBA context object.
979 * @xritag: xri used in this exchange.
980 * @rrq: The RRQ to be cleared.
981 *
James Smart19ca7602010-11-20 23:11:55 -0500982 **/
James Smart1151e3e2011-02-16 12:39:35 -0500983void
984lpfc_clr_rrq_active(struct lpfc_hba *phba,
985 uint16_t xritag,
986 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500987{
James Smart1151e3e2011-02-16 12:39:35 -0500988 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500989
James Smart2693f5d2021-03-01 09:18:03 -0800990 /* Lookup did to verify if did is still active on this vport */
James Smart307e3382020-11-15 11:26:30 -0800991 if (rrq->vport)
James Smart1151e3e2011-02-16 12:39:35 -0500992 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500993
James Smart1151e3e2011-02-16 12:39:35 -0500994 if (!ndlp)
995 goto out;
996
James Smartcff261f2013-12-17 20:29:47 -0500997 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500998 rrq->send_rrq = 0;
999 rrq->xritag = 0;
1000 rrq->rrq_stop_time = 0;
1001 }
James Smart1151e3e2011-02-16 12:39:35 -05001002out:
James Smart19ca7602010-11-20 23:11:55 -05001003 mempool_free(rrq, phba->rrq_pool);
1004}
1005
1006/**
1007 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
1008 * @phba: Pointer to HBA context object.
1009 *
1010 * This function is called with hbalock held. This function
1011 * Checks if stop_time (ratov from setting rrq active) has
1012 * been reached, if it has and the send_rrq flag is set then
1013 * it will call lpfc_send_rrq. If the send_rrq flag is not set
1014 * then it will just call the routine to clear the rrq and
1015 * free the rrq resource.
1016 * The timer is set to the next rrq that is going to expire before
1017 * leaving the routine.
1018 *
1019 **/
1020void
1021lpfc_handle_rrq_active(struct lpfc_hba *phba)
1022{
1023 struct lpfc_node_rrq *rrq;
1024 struct lpfc_node_rrq *nextrrq;
1025 unsigned long next_time;
1026 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -05001027 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -05001028
1029 spin_lock_irqsave(&phba->hbalock, iflags);
1030 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -04001031 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -05001032 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -05001033 &phba->active_rrq_list, list) {
1034 if (time_after(jiffies, rrq->rrq_stop_time))
1035 list_move(&rrq->list, &send_rrq);
1036 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -05001037 next_time = rrq->rrq_stop_time;
1038 }
1039 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -05001040 if ((!list_empty(&phba->active_rrq_list)) &&
1041 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -05001042 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -05001043 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
1044 list_del(&rrq->list);
Bart Van Asscheffd43812019-03-28 11:06:17 -07001045 if (!rrq->send_rrq) {
James Smart1151e3e2011-02-16 12:39:35 -05001046 /* this call will free the rrq */
Bart Van Asscheffd43812019-03-28 11:06:17 -07001047 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1048 } else if (lpfc_send_rrq(phba, rrq)) {
James Smart1151e3e2011-02-16 12:39:35 -05001049 /* if we send the rrq then the completion handler
1050 * will clear the bit in the xribitmap.
1051 */
1052 lpfc_clr_rrq_active(phba, rrq->xritag,
1053 rrq);
1054 }
1055 }
James Smart19ca7602010-11-20 23:11:55 -05001056}
1057
1058/**
1059 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
1060 * @vport: Pointer to vport context object.
1061 * @xri: The xri used in the exchange.
1062 * @did: The targets DID for this exchange.
1063 *
1064 * returns NULL = rrq not found in the phba->active_rrq_list.
1065 * rrq = rrq for this xri and target.
1066 **/
1067struct lpfc_node_rrq *
1068lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
1069{
1070 struct lpfc_hba *phba = vport->phba;
1071 struct lpfc_node_rrq *rrq;
1072 struct lpfc_node_rrq *nextrrq;
1073 unsigned long iflags;
1074
1075 if (phba->sli_rev != LPFC_SLI_REV4)
1076 return NULL;
1077 spin_lock_irqsave(&phba->hbalock, iflags);
1078 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
1079 if (rrq->vport == vport && rrq->xritag == xri &&
1080 rrq->nlp_DID == did){
1081 list_del(&rrq->list);
1082 spin_unlock_irqrestore(&phba->hbalock, iflags);
1083 return rrq;
1084 }
1085 }
1086 spin_unlock_irqrestore(&phba->hbalock, iflags);
1087 return NULL;
1088}
1089
1090/**
1091 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
1092 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -05001093 * @ndlp: Pointer to the lpfc_node_list structure.
1094 * If ndlp is NULL Remove all active RRQs for this vport from the
1095 * phba->active_rrq_list and clear the rrq.
1096 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -05001097 **/
1098void
James Smart1151e3e2011-02-16 12:39:35 -05001099lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -05001100
1101{
1102 struct lpfc_hba *phba = vport->phba;
1103 struct lpfc_node_rrq *rrq;
1104 struct lpfc_node_rrq *nextrrq;
1105 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -05001106 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -05001107
1108 if (phba->sli_rev != LPFC_SLI_REV4)
1109 return;
James Smart1151e3e2011-02-16 12:39:35 -05001110 if (!ndlp) {
1111 lpfc_sli4_vport_delete_els_xri_aborted(vport);
1112 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -05001113 }
James Smart1151e3e2011-02-16 12:39:35 -05001114 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart2693f5d2021-03-01 09:18:03 -08001115 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
1116 if (rrq->vport != vport)
1117 continue;
1118
1119 if (!ndlp || ndlp == lpfc_findnode_did(vport, rrq->nlp_DID))
James Smart1151e3e2011-02-16 12:39:35 -05001120 list_move(&rrq->list, &rrq_list);
James Smart2693f5d2021-03-01 09:18:03 -08001121
1122 }
James Smart19ca7602010-11-20 23:11:55 -05001123 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -05001124
1125 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
1126 list_del(&rrq->list);
1127 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1128 }
James Smart19ca7602010-11-20 23:11:55 -05001129}
1130
1131/**
James Smart1151e3e2011-02-16 12:39:35 -05001132 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -05001133 * @phba: Pointer to HBA context object.
1134 * @ndlp: Targets nodelist pointer for this exchange.
Lee Jones7af29d42020-07-21 17:41:31 +01001135 * @xritag: the xri in the bitmap to test.
James Smart19ca7602010-11-20 23:11:55 -05001136 *
James Smarte2a8be52019-05-06 17:26:47 -07001137 * This function returns:
1138 * 0 = rrq not active for this xri
1139 * 1 = rrq is valid for this xri.
James Smart19ca7602010-11-20 23:11:55 -05001140 **/
James Smart1151e3e2011-02-16 12:39:35 -05001141int
1142lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -05001143 uint16_t xritag)
1144{
James Smart19ca7602010-11-20 23:11:55 -05001145 if (!ndlp)
1146 return 0;
James Smartcff261f2013-12-17 20:29:47 -05001147 if (!ndlp->active_rrqs_xri_bitmap)
1148 return 0;
1149 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
Colin Ian King258f84f2019-02-12 15:29:45 +00001150 return 1;
James Smart19ca7602010-11-20 23:11:55 -05001151 else
1152 return 0;
1153}
1154
1155/**
1156 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
1157 * @phba: Pointer to HBA context object.
1158 * @ndlp: nodelist pointer for this target.
1159 * @xritag: xri used in this exchange.
1160 * @rxid: Remote Exchange ID.
1161 * @send_rrq: Flag used to determine if we should send rrq els cmd.
1162 *
1163 * This function takes the hbalock.
1164 * The active bit is always set in the active rrq xri_bitmap even
1165 * if there is no slot avaiable for the other rrq information.
1166 *
1167 * returns 0 rrq actived for this xri
1168 * < 0 No memory or invalid ndlp.
1169 **/
1170int
1171lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -05001172 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -05001173{
James Smart19ca7602010-11-20 23:11:55 -05001174 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -05001175 struct lpfc_node_rrq *rrq;
1176 int empty;
1177
1178 if (!ndlp)
1179 return -EINVAL;
1180
1181 if (!phba->cfg_enable_rrq)
1182 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001183
1184 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -05001185 if (phba->pport->load_flag & FC_UNLOADING) {
1186 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
1187 goto out;
1188 }
1189
James Smartb42c07c2012-01-18 16:25:55 -05001190 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
1191 goto out;
1192
James Smartcff261f2013-12-17 20:29:47 -05001193 if (!ndlp->active_rrqs_xri_bitmap)
1194 goto out;
1195
1196 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -05001197 goto out;
1198
James Smart19ca7602010-11-20 23:11:55 -05001199 spin_unlock_irqrestore(&phba->hbalock, iflags);
Dick Kennedy9dace1f2020-06-30 14:49:53 -07001200 rrq = mempool_alloc(phba->rrq_pool, GFP_ATOMIC);
James Smartb42c07c2012-01-18 16:25:55 -05001201 if (!rrq) {
1202 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1203 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1204 " DID:0x%x Send:%d\n",
1205 xritag, rxid, ndlp->nlp_DID, send_rrq);
1206 return -EINVAL;
1207 }
James Smarte5771b42013-03-01 16:37:14 -05001208 if (phba->cfg_enable_rrq == 1)
1209 rrq->send_rrq = send_rrq;
1210 else
1211 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -05001212 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -04001213 rrq->rrq_stop_time = jiffies +
1214 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -05001215 rrq->nlp_DID = ndlp->nlp_DID;
1216 rrq->vport = ndlp->vport;
1217 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -05001218 spin_lock_irqsave(&phba->hbalock, iflags);
1219 empty = list_empty(&phba->active_rrq_list);
1220 list_add_tail(&rrq->list, &phba->active_rrq_list);
1221 phba->hba_flag |= HBA_RRQ_ACTIVE;
1222 if (empty)
1223 lpfc_worker_wake_up(phba);
1224 spin_unlock_irqrestore(&phba->hbalock, iflags);
1225 return 0;
1226out:
1227 spin_unlock_irqrestore(&phba->hbalock, iflags);
1228 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1229 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1230 " DID:0x%x Send:%d\n",
1231 xritag, rxid, ndlp->nlp_DID, send_rrq);
1232 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001233}
1234
1235/**
James Smart895427b2017-02-12 13:52:30 -08001236 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -04001237 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01001238 * @piocbq: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -04001239 *
James Smarte2a8be52019-05-06 17:26:47 -07001240 * The driver calls this function with either the nvme ls ring lock
1241 * or the fc els ring lock held depending on the iocb usage. This function
1242 * gets a new driver sglq object from the sglq list. If the list is not empty
1243 * then it is successful, it returns pointer to the newly allocated sglq
1244 * object else it returns NULL.
James Smartda0436e2009-05-22 14:51:39 -04001245 **/
1246static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -08001247__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -04001248{
James Smart895427b2017-02-12 13:52:30 -08001249 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -04001250 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001251 struct lpfc_sglq *start_sglq = NULL;
James Smartc4908502019-01-28 11:14:28 -08001252 struct lpfc_io_buf *lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001253 struct lpfc_nodelist *ndlp;
James Smarte2a8be52019-05-06 17:26:47 -07001254 struct lpfc_sli_ring *pring = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001255 int found = 0;
1256
James Smarte2a8be52019-05-06 17:26:47 -07001257 if (piocbq->iocb_flag & LPFC_IO_NVME_LS)
1258 pring = phba->sli4_hba.nvmels_wq->pring;
1259 else
1260 pring = lpfc_phba_elsring(phba);
1261
1262 lockdep_assert_held(&pring->ring_lock);
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001263
James Smart19ca7602010-11-20 23:11:55 -05001264 if (piocbq->iocb_flag & LPFC_IO_FCP) {
James Smartc4908502019-01-28 11:14:28 -08001265 lpfc_cmd = (struct lpfc_io_buf *) piocbq->context1;
James Smart19ca7602010-11-20 23:11:55 -05001266 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -05001267 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -04001268 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -05001269 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -04001270 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1271 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1272 ndlp = NULL;
1273 else
1274 ndlp = piocbq->context_un.ndlp;
1275 } else {
James Smart19ca7602010-11-20 23:11:55 -05001276 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -04001277 }
James Smart19ca7602010-11-20 23:11:55 -05001278
James Smart895427b2017-02-12 13:52:30 -08001279 spin_lock(&phba->sli4_hba.sgl_list_lock);
1280 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -05001281 start_sglq = sglq;
1282 while (!found) {
1283 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -08001284 break;
James Smart895427b2017-02-12 13:52:30 -08001285 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1286 test_bit(sglq->sli4_lxritag,
1287 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -05001288 /* This xri has an rrq outstanding for this DID.
1289 * put it back in the list and get another xri.
1290 */
James Smart895427b2017-02-12 13:52:30 -08001291 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001292 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -08001293 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -05001294 struct lpfc_sglq, list);
1295 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -07001296 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001297 sglq = NULL;
1298 break;
1299 } else
1300 continue;
1301 }
1302 sglq->ndlp = ndlp;
1303 found = 1;
James Smart6d368e52011-05-24 11:44:12 -04001304 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -05001305 sglq->state = SGL_ALLOCATED;
1306 }
James Smart895427b2017-02-12 13:52:30 -08001307 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001308 return sglq;
1309}
1310
1311/**
James Smartf358dd02017-02-12 13:52:34 -08001312 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1313 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01001314 * @piocbq: Pointer to the iocbq.
James Smartf358dd02017-02-12 13:52:34 -08001315 *
1316 * This function is called with the sgl_list lock held. This function
1317 * gets a new driver sglq object from the sglq list. If the
1318 * list is not empty then it is successful, it returns pointer to the newly
1319 * allocated sglq object else it returns NULL.
1320 **/
1321struct lpfc_sglq *
1322__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1323{
1324 struct list_head *lpfc_nvmet_sgl_list;
1325 struct lpfc_sglq *sglq = NULL;
1326
1327 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1328
1329 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1330
1331 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1332 if (!sglq)
1333 return NULL;
1334 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1335 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001336 return sglq;
1337}
1338
James Smarte59058c2008-08-24 21:49:00 -04001339/**
James Smart3621a712009-04-06 18:47:14 -04001340 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001341 * @phba: Pointer to HBA context object.
1342 *
1343 * This function is called with no lock held. This function
1344 * allocates a new driver iocb object from the iocb pool. If the
1345 * allocation is successful, it returns pointer to the newly
1346 * allocated iocb object else it returns NULL.
1347 **/
James Smart2e0fef82007-06-17 19:56:36 -05001348struct lpfc_iocbq *
1349lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001350{
James Smart2e0fef82007-06-17 19:56:36 -05001351 struct lpfc_iocbq * iocbq = NULL;
1352 unsigned long iflags;
1353
1354 spin_lock_irqsave(&phba->hbalock, iflags);
1355 iocbq = __lpfc_sli_get_iocbq(phba);
1356 spin_unlock_irqrestore(&phba->hbalock, iflags);
1357 return iocbq;
1358}
1359
James Smarte59058c2008-08-24 21:49:00 -04001360/**
James Smart4f774512009-05-22 14:52:35 -04001361 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1362 * @phba: Pointer to HBA context object.
1363 * @iocbq: Pointer to driver iocb object.
1364 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001365 * This function is called to release the driver iocb object
1366 * to the iocb pool. The iotag in the iocb object
James Smart4f774512009-05-22 14:52:35 -04001367 * does not change for each use of the iocb object. This function
1368 * clears all other fields of the iocb object when it is freed.
1369 * The sqlq structure that holds the xritag and phys and virtual
1370 * mappings for the scatter gather list is retrieved from the
1371 * active array of sglq. The get of the sglq pointer also clears
1372 * the entry in the array. If the status of the IO indiactes that
1373 * this IO was aborted then the sglq entry it put on the
1374 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1375 * IO has good status or fails for any other reason then the sglq
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001376 * entry is added to the free list (lpfc_els_sgl_list). The hbalock is
1377 * asserted held in the code path calling this routine.
James Smart4f774512009-05-22 14:52:35 -04001378 **/
1379static void
1380__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1381{
1382 struct lpfc_sglq *sglq;
1383 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001384 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001385 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001386
1387 if (iocbq->sli4_xritag == NO_XRI)
1388 sglq = NULL;
1389 else
James Smart6d368e52011-05-24 11:44:12 -04001390 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1391
James Smart0e9bb8d2013-03-01 16:35:12 -05001392
James Smart4f774512009-05-22 14:52:35 -04001393 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001394 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1395 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1396 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001397 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001398 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001399 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001400 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1401 spin_unlock_irqrestore(
1402 &phba->sli4_hba.sgl_list_lock, iflag);
1403 goto out;
1404 }
1405
James Smart4f774512009-05-22 14:52:35 -04001406 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1407 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001408 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1409 iflag);
James Smart307e3382020-11-15 11:26:30 -08001410
1411 /* Check if we can get a reference on ndlp */
1412 if (sglq->ndlp && !lpfc_nlp_get(sglq->ndlp))
1413 sglq->ndlp = NULL;
1414
James Smart341af102010-01-26 23:07:37 -05001415 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001416 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001417 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001418 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001419 } else {
James Smart895427b2017-02-12 13:52:30 -08001420 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1421 iflag);
James Smart4f774512009-05-22 14:52:35 -04001422 sglq->state = SGL_FREED;
1423 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001424 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001425 &phba->sli4_hba.lpfc_els_sgl_list);
1426 spin_unlock_irqrestore(
1427 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart9628aac2021-03-01 09:18:16 -08001428 pring = lpfc_phba_elsring(phba);
James Smart2a9bf3d2010-06-07 15:24:45 -04001429 /* Check if TXQ queue needs to be serviced */
James Smart9628aac2021-03-01 09:18:16 -08001430 if (pring && (!list_empty(&pring->txq)))
James Smart2a9bf3d2010-06-07 15:24:45 -04001431 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001432 }
James Smart4f774512009-05-22 14:52:35 -04001433 }
1434
James Smartf358dd02017-02-12 13:52:34 -08001435out:
James Smart4f774512009-05-22 14:52:35 -04001436 /*
1437 * Clean all volatile data fields, preserve iotag and node struct.
1438 */
1439 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001440 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001441 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001442 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1443 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001444 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1445}
1446
James Smart2a9bf3d2010-06-07 15:24:45 -04001447
James Smart4f774512009-05-22 14:52:35 -04001448/**
James Smart3772a992009-05-22 14:50:54 -04001449 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1450 * @phba: Pointer to HBA context object.
1451 * @iocbq: Pointer to driver iocb object.
1452 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001453 * This function is called to release the driver iocb object to the
1454 * iocb pool. The iotag in the iocb object does not change for each
1455 * use of the iocb object. This function clears all other fields of
1456 * the iocb object when it is freed. The hbalock is asserted held in
1457 * the code path calling this routine.
James Smart3772a992009-05-22 14:50:54 -04001458 **/
1459static void
1460__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1461{
1462 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1463
James Smart0e9bb8d2013-03-01 16:35:12 -05001464 /*
James Smart3772a992009-05-22 14:50:54 -04001465 * Clean all volatile data fields, preserve iotag and node struct.
1466 */
1467 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1468 iocbq->sli4_xritag = NO_XRI;
1469 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1470}
1471
1472/**
James Smart3621a712009-04-06 18:47:14 -04001473 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001474 * @phba: Pointer to HBA context object.
1475 * @iocbq: Pointer to driver iocb object.
1476 *
1477 * This function is called with hbalock held to release driver
1478 * iocb object to the iocb pool. The iotag in the iocb object
1479 * does not change for each use of the iocb object. This function
1480 * clears all other fields of the iocb object when it is freed.
1481 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001482static void
James Smart2e0fef82007-06-17 19:56:36 -05001483__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1484{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001485 lockdep_assert_held(&phba->hbalock);
1486
James Smart3772a992009-05-22 14:50:54 -04001487 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001488 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001489}
1490
James Smarte59058c2008-08-24 21:49:00 -04001491/**
James Smart3621a712009-04-06 18:47:14 -04001492 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001493 * @phba: Pointer to HBA context object.
1494 * @iocbq: Pointer to driver iocb object.
1495 *
1496 * This function is called with no lock held to release the iocb to
1497 * iocb pool.
1498 **/
James Smart2e0fef82007-06-17 19:56:36 -05001499void
1500lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1501{
1502 unsigned long iflags;
1503
1504 /*
1505 * Clean all volatile data fields, preserve iotag and node struct.
1506 */
1507 spin_lock_irqsave(&phba->hbalock, iflags);
1508 __lpfc_sli_release_iocbq(phba, iocbq);
1509 spin_unlock_irqrestore(&phba->hbalock, iflags);
1510}
1511
James Smarte59058c2008-08-24 21:49:00 -04001512/**
James Smarta257bf92009-04-06 18:48:10 -04001513 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1514 * @phba: Pointer to HBA context object.
1515 * @iocblist: List of IOCBs.
1516 * @ulpstatus: ULP status in IOCB command field.
1517 * @ulpWord4: ULP word-4 in IOCB command field.
1518 *
1519 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1520 * on the list by invoking the complete callback function associated with the
1521 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1522 * fields.
1523 **/
1524void
1525lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1526 uint32_t ulpstatus, uint32_t ulpWord4)
1527{
1528 struct lpfc_iocbq *piocb;
1529
1530 while (!list_empty(iocblist)) {
1531 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smart9ec58ec2021-01-04 10:02:35 -08001532 if (piocb->wqe_cmpl) {
James Smart84f2ddf2019-08-14 16:56:55 -07001533 if (piocb->iocb_flag & LPFC_IO_NVME)
James Smart9ec58ec2021-01-04 10:02:35 -08001534 lpfc_nvme_cancel_iocb(phba, piocb,
1535 ulpstatus, ulpWord4);
James Smart84f2ddf2019-08-14 16:56:55 -07001536 else
1537 lpfc_sli_release_iocbq(phba, piocb);
James Smart9ec58ec2021-01-04 10:02:35 -08001538
1539 } else if (piocb->iocb_cmpl) {
James Smarta257bf92009-04-06 18:48:10 -04001540 piocb->iocb.ulpStatus = ulpstatus;
1541 piocb->iocb.un.ulpWord[4] = ulpWord4;
1542 (piocb->iocb_cmpl) (phba, piocb, piocb);
James Smart9ec58ec2021-01-04 10:02:35 -08001543 } else {
1544 lpfc_sli_release_iocbq(phba, piocb);
James Smarta257bf92009-04-06 18:48:10 -04001545 }
1546 }
1547 return;
1548}
1549
1550/**
James Smart3621a712009-04-06 18:47:14 -04001551 * lpfc_sli_iocb_cmd_type - Get the iocb type
1552 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001553 *
1554 * This function is called by ring event handler function to get the iocb type.
1555 * This function translates the iocb command to an iocb command type used to
1556 * decide the final disposition of each completed IOCB.
1557 * The function returns
1558 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1559 * LPFC_SOL_IOCB if it is a solicited iocb completion
1560 * LPFC_ABORT_IOCB if it is an abort iocb
1561 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1562 *
1563 * The caller is not required to hold any lock.
1564 **/
dea31012005-04-17 16:05:31 -05001565static lpfc_iocb_type
1566lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1567{
1568 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1569
1570 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1571 return 0;
1572
1573 switch (iocb_cmnd) {
1574 case CMD_XMIT_SEQUENCE_CR:
1575 case CMD_XMIT_SEQUENCE_CX:
1576 case CMD_XMIT_BCAST_CN:
1577 case CMD_XMIT_BCAST_CX:
1578 case CMD_ELS_REQUEST_CR:
1579 case CMD_ELS_REQUEST_CX:
1580 case CMD_CREATE_XRI_CR:
1581 case CMD_CREATE_XRI_CX:
1582 case CMD_GET_RPI_CN:
1583 case CMD_XMIT_ELS_RSP_CX:
1584 case CMD_GET_RPI_CR:
1585 case CMD_FCP_IWRITE_CR:
1586 case CMD_FCP_IWRITE_CX:
1587 case CMD_FCP_IREAD_CR:
1588 case CMD_FCP_IREAD_CX:
1589 case CMD_FCP_ICMND_CR:
1590 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001591 case CMD_FCP_TSEND_CX:
1592 case CMD_FCP_TRSP_CX:
1593 case CMD_FCP_TRECEIVE_CX:
1594 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001595 case CMD_ADAPTER_MSG:
1596 case CMD_ADAPTER_DUMP:
1597 case CMD_XMIT_SEQUENCE64_CR:
1598 case CMD_XMIT_SEQUENCE64_CX:
1599 case CMD_XMIT_BCAST64_CN:
1600 case CMD_XMIT_BCAST64_CX:
1601 case CMD_ELS_REQUEST64_CR:
1602 case CMD_ELS_REQUEST64_CX:
1603 case CMD_FCP_IWRITE64_CR:
1604 case CMD_FCP_IWRITE64_CX:
1605 case CMD_FCP_IREAD64_CR:
1606 case CMD_FCP_IREAD64_CX:
1607 case CMD_FCP_ICMND64_CR:
1608 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001609 case CMD_FCP_TSEND64_CX:
1610 case CMD_FCP_TRSP64_CX:
1611 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001612 case CMD_GEN_REQUEST64_CR:
1613 case CMD_GEN_REQUEST64_CX:
1614 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001615 case DSSCMD_IWRITE64_CR:
1616 case DSSCMD_IWRITE64_CX:
1617 case DSSCMD_IREAD64_CR:
1618 case DSSCMD_IREAD64_CX:
Dick Kennedyc93764a2020-06-30 14:49:49 -07001619 case CMD_SEND_FRAME:
dea31012005-04-17 16:05:31 -05001620 type = LPFC_SOL_IOCB;
1621 break;
1622 case CMD_ABORT_XRI_CN:
1623 case CMD_ABORT_XRI_CX:
1624 case CMD_CLOSE_XRI_CN:
1625 case CMD_CLOSE_XRI_CX:
1626 case CMD_XRI_ABORTED_CX:
1627 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001628 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001629 type = LPFC_ABORT_IOCB;
1630 break;
1631 case CMD_RCV_SEQUENCE_CX:
1632 case CMD_RCV_ELS_REQ_CX:
1633 case CMD_RCV_SEQUENCE64_CX:
1634 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001635 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001636 case CMD_IOCB_RCV_SEQ64_CX:
1637 case CMD_IOCB_RCV_ELS64_CX:
1638 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001639 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001640 type = LPFC_UNSOL_IOCB;
1641 break;
James Smart3163f722008-02-08 18:50:25 -05001642 case CMD_IOCB_XMIT_MSEQ64_CR:
1643 case CMD_IOCB_XMIT_MSEQ64_CX:
1644 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1645 case CMD_IOCB_RCV_ELS_LIST64_CX:
1646 case CMD_IOCB_CLOSE_EXTENDED_CN:
1647 case CMD_IOCB_ABORT_EXTENDED_CN:
1648 case CMD_IOCB_RET_HBQE64_CN:
1649 case CMD_IOCB_FCP_IBIDIR64_CR:
1650 case CMD_IOCB_FCP_IBIDIR64_CX:
1651 case CMD_IOCB_FCP_ITASKMGT64_CX:
1652 case CMD_IOCB_LOGENTRY_CN:
1653 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1654 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001655 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001656 type = LPFC_UNKNOWN_IOCB;
1657 break;
dea31012005-04-17 16:05:31 -05001658 default:
1659 type = LPFC_UNKNOWN_IOCB;
1660 break;
1661 }
1662
1663 return type;
1664}
1665
James Smarte59058c2008-08-24 21:49:00 -04001666/**
James Smart3621a712009-04-06 18:47:14 -04001667 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001668 * @phba: Pointer to HBA context object.
1669 *
1670 * This function is called from SLI initialization code
1671 * to configure every ring of the HBA's SLI interface. The
1672 * caller is not required to hold any lock. This function issues
1673 * a config_ring mailbox command for each ring.
1674 * This function returns zero if successful else returns a negative
1675 * error code.
1676 **/
dea31012005-04-17 16:05:31 -05001677static int
James Smarted957682007-06-17 19:56:37 -05001678lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001679{
1680 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001681 LPFC_MBOXQ_t *pmb;
1682 MAILBOX_t *pmbox;
1683 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001684
James Smarted957682007-06-17 19:56:37 -05001685 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1686 if (!pmb)
1687 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001688 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001689 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001690 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001691 lpfc_config_ring(phba, i, pmb);
1692 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1693 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001694 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001695 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001696 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1697 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001698 rc, pmbox->mbxCommand,
1699 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001700 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001701 ret = -ENXIO;
1702 break;
dea31012005-04-17 16:05:31 -05001703 }
1704 }
James Smarted957682007-06-17 19:56:37 -05001705 mempool_free(pmb, phba->mbox_mem_pool);
1706 return ret;
dea31012005-04-17 16:05:31 -05001707}
1708
James Smarte59058c2008-08-24 21:49:00 -04001709/**
James Smart3621a712009-04-06 18:47:14 -04001710 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001711 * @phba: Pointer to HBA context object.
1712 * @pring: Pointer to driver SLI ring object.
1713 * @piocb: Pointer to the driver iocb object.
1714 *
James Smarte2a8be52019-05-06 17:26:47 -07001715 * The driver calls this function with the hbalock held for SLI3 ports or
1716 * the ring lock held for SLI4 ports. The function adds the
James Smarte59058c2008-08-24 21:49:00 -04001717 * new iocb to txcmplq of the given ring. This function always returns
1718 * 0. If this function is called for ELS ring, this function checks if
1719 * there is a vport associated with the ELS command. This function also
1720 * starts els_tmofunc timer if this is an ELS command.
1721 **/
dea31012005-04-17 16:05:31 -05001722static int
James Smart2e0fef82007-06-17 19:56:36 -05001723lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1724 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001725{
James Smarte2a8be52019-05-06 17:26:47 -07001726 if (phba->sli_rev == LPFC_SLI_REV4)
1727 lockdep_assert_held(&pring->ring_lock);
1728 else
1729 lockdep_assert_held(&phba->hbalock);
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001730
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001731 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001732
dea31012005-04-17 16:05:31 -05001733 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001734 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08001735 pring->txcmplq_cnt++;
James Smart2a9bf3d2010-06-07 15:24:45 -04001736
James Smart92d7f7b2007-06-17 19:56:38 -05001737 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1738 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001739 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1740 BUG_ON(!piocb->vport);
1741 if (!(piocb->vport->load_flag & FC_UNLOADING))
1742 mod_timer(&piocb->vport->els_tmofunc,
1743 jiffies +
1744 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1745 }
dea31012005-04-17 16:05:31 -05001746
James Smart2e0fef82007-06-17 19:56:36 -05001747 return 0;
dea31012005-04-17 16:05:31 -05001748}
1749
James Smarte59058c2008-08-24 21:49:00 -04001750/**
James Smart3621a712009-04-06 18:47:14 -04001751 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001752 * @phba: Pointer to HBA context object.
1753 * @pring: Pointer to driver SLI ring object.
1754 *
1755 * This function is called with hbalock held to get next
1756 * iocb in txq of the given ring. If there is any iocb in
1757 * the txq, the function returns first iocb in the list after
1758 * removing the iocb from the list, else it returns NULL.
1759 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001760struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001761lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001762{
dea31012005-04-17 16:05:31 -05001763 struct lpfc_iocbq *cmd_iocb;
1764
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001765 lockdep_assert_held(&phba->hbalock);
1766
James Smart858c9f62007-06-17 19:56:39 -05001767 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001768 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001769}
1770
James Smarte59058c2008-08-24 21:49:00 -04001771/**
James Smart3621a712009-04-06 18:47:14 -04001772 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001773 * @phba: Pointer to HBA context object.
1774 * @pring: Pointer to driver SLI ring object.
1775 *
1776 * This function is called with hbalock held and the caller must post the
1777 * iocb without releasing the lock. If the caller releases the lock,
1778 * iocb slot returned by the function is not guaranteed to be available.
1779 * The function returns pointer to the next available iocb slot if there
1780 * is available slot in the ring, else it returns NULL.
1781 * If the get index of the ring is ahead of the put index, the function
1782 * will post an error attention event to the worker thread to take the
1783 * HBA to offline state.
1784 **/
dea31012005-04-17 16:05:31 -05001785static IOCB_t *
1786lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1787{
James Smart34b02dc2008-08-24 21:49:55 -04001788 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001789 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001790
1791 lockdep_assert_held(&phba->hbalock);
1792
James Smart7e56aa22012-08-03 12:35:34 -04001793 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1794 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1795 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001796
James Smart7e56aa22012-08-03 12:35:34 -04001797 if (unlikely(pring->sli.sli3.local_getidx ==
1798 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001799
James Smart7e56aa22012-08-03 12:35:34 -04001800 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001801
James Smart7e56aa22012-08-03 12:35:34 -04001802 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07001803 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04001804 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001805 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001806 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001807 pring->sli.sli3.local_getidx,
1808 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001809
James Smart2e0fef82007-06-17 19:56:36 -05001810 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001811 /*
1812 * All error attention handlers are posted to
1813 * worker thread
1814 */
1815 phba->work_ha |= HA_ERATT;
1816 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001817
James Smart5e9d9b82008-06-14 22:52:53 -04001818 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001819
1820 return NULL;
1821 }
1822
James Smart7e56aa22012-08-03 12:35:34 -04001823 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001824 return NULL;
1825 }
1826
James Smarted957682007-06-17 19:56:37 -05001827 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001828}
1829
James Smarte59058c2008-08-24 21:49:00 -04001830/**
James Smart3621a712009-04-06 18:47:14 -04001831 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001832 * @phba: Pointer to HBA context object.
1833 * @iocbq: Pointer to driver iocb object.
1834 *
1835 * This function gets an iotag for the iocb. If there is no unused iotag and
1836 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1837 * array and assigns a new iotag.
1838 * The function returns the allocated iotag if successful, else returns zero.
1839 * Zero is not a valid iotag.
1840 * The caller is not required to hold any lock.
1841 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001842uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001843lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001844{
James Smart2e0fef82007-06-17 19:56:36 -05001845 struct lpfc_iocbq **new_arr;
1846 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001847 size_t new_len;
1848 struct lpfc_sli *psli = &phba->sli;
1849 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001850
James Smart2e0fef82007-06-17 19:56:36 -05001851 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001852 iotag = psli->last_iotag;
1853 if(++iotag < psli->iocbq_lookup_len) {
1854 psli->last_iotag = iotag;
1855 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001856 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001857 iocbq->iotag = iotag;
1858 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001859 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001860 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1861 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001862 spin_unlock_irq(&phba->hbalock);
Kees Cook6396bb22018-06-12 14:03:40 -07001863 new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001864 GFP_KERNEL);
1865 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001866 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001867 old_arr = psli->iocbq_lookup;
1868 if (new_len <= psli->iocbq_lookup_len) {
1869 /* highly unprobable case */
1870 kfree(new_arr);
1871 iotag = psli->last_iotag;
1872 if(++iotag < psli->iocbq_lookup_len) {
1873 psli->last_iotag = iotag;
1874 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001875 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001876 iocbq->iotag = iotag;
1877 return iotag;
1878 }
James Smart2e0fef82007-06-17 19:56:36 -05001879 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001880 return 0;
1881 }
1882 if (psli->iocbq_lookup)
1883 memcpy(new_arr, old_arr,
1884 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001885 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001886 psli->iocbq_lookup = new_arr;
1887 psli->iocbq_lookup_len = new_len;
1888 psli->last_iotag = iotag;
1889 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001890 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001891 iocbq->iotag = iotag;
1892 kfree(old_arr);
1893 return iotag;
1894 }
James Smart8f6d98d2006-08-01 07:34:00 -04001895 } else
James Smart2e0fef82007-06-17 19:56:36 -05001896 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001897
James Smartbc739052010-08-04 16:11:18 -04001898 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001899 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1900 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001901
James Bottomley604a3e32005-10-29 10:28:33 -05001902 return 0;
dea31012005-04-17 16:05:31 -05001903}
1904
James Smarte59058c2008-08-24 21:49:00 -04001905/**
James Smart3621a712009-04-06 18:47:14 -04001906 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001907 * @phba: Pointer to HBA context object.
1908 * @pring: Pointer to driver SLI ring object.
1909 * @iocb: Pointer to iocb slot in the ring.
1910 * @nextiocb: Pointer to driver iocb object which need to be
1911 * posted to firmware.
1912 *
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001913 * This function is called to post a new iocb to the firmware. This
1914 * function copies the new iocb to ring iocb slot and updates the
1915 * ring pointers. It adds the new iocb to txcmplq if there is
James Smarte59058c2008-08-24 21:49:00 -04001916 * a completion call back for this iocb else the function will free the
Dick Kennedy88acb4d2020-05-01 14:43:07 -07001917 * iocb object. The hbalock is asserted held in the code path calling
1918 * this routine.
James Smarte59058c2008-08-24 21:49:00 -04001919 **/
dea31012005-04-17 16:05:31 -05001920static void
1921lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1922 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1923{
1924 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001925 * Set up an iotag
dea31012005-04-17 16:05:31 -05001926 */
James Bottomley604a3e32005-10-29 10:28:33 -05001927 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001928
James Smarte2a0a9d2008-12-04 22:40:02 -05001929
James Smarta58cbd52007-08-02 11:09:43 -04001930 if (pring->ringno == LPFC_ELS_RING) {
1931 lpfc_debugfs_slow_ring_trc(phba,
1932 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1933 *(((uint32_t *) &nextiocb->iocb) + 4),
1934 *(((uint32_t *) &nextiocb->iocb) + 6),
1935 *(((uint32_t *) &nextiocb->iocb) + 7));
1936 }
1937
dea31012005-04-17 16:05:31 -05001938 /*
1939 * Issue iocb command to adapter
1940 */
James Smart92d7f7b2007-06-17 19:56:38 -05001941 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001942 wmb();
1943 pring->stats.iocb_cmd++;
1944
1945 /*
1946 * If there is no completion routine to call, we can release the
1947 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1948 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1949 */
1950 if (nextiocb->iocb_cmpl)
1951 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001952 else
James Smart2e0fef82007-06-17 19:56:36 -05001953 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001954
1955 /*
1956 * Let the HBA know what IOCB slot will be the next one the
1957 * driver will put a command into.
1958 */
James Smart7e56aa22012-08-03 12:35:34 -04001959 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1960 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001961}
1962
James Smarte59058c2008-08-24 21:49:00 -04001963/**
James Smart3621a712009-04-06 18:47:14 -04001964 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001965 * @phba: Pointer to HBA context object.
1966 * @pring: Pointer to driver SLI ring object.
1967 *
1968 * The caller is not required to hold any lock for calling this function.
1969 * This function updates the chip attention bits for the ring to inform firmware
1970 * that there are pending work to be done for this ring and requests an
1971 * interrupt when there is space available in the ring. This function is
1972 * called when the driver is unable to post more iocbs to the ring due
1973 * to unavailability of space in the ring.
1974 **/
dea31012005-04-17 16:05:31 -05001975static void
James Smart2e0fef82007-06-17 19:56:36 -05001976lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001977{
1978 int ringno = pring->ringno;
1979
1980 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1981
1982 wmb();
1983
1984 /*
1985 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1986 * The HBA will tell us when an IOCB entry is available.
1987 */
1988 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1989 readl(phba->CAregaddr); /* flush */
1990
1991 pring->stats.iocb_cmd_full++;
1992}
1993
James Smarte59058c2008-08-24 21:49:00 -04001994/**
James Smart3621a712009-04-06 18:47:14 -04001995 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001996 * @phba: Pointer to HBA context object.
1997 * @pring: Pointer to driver SLI ring object.
1998 *
1999 * This function updates the chip attention register bit for the
2000 * given ring to inform HBA that there is more work to be done
2001 * in this ring. The caller is not required to hold any lock.
2002 **/
dea31012005-04-17 16:05:31 -05002003static void
James Smart2e0fef82007-06-17 19:56:36 -05002004lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05002005{
2006 int ringno = pring->ringno;
2007
2008 /*
2009 * Tell the HBA that there is work to do in this ring.
2010 */
James Smart34b02dc2008-08-24 21:49:55 -04002011 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
2012 wmb();
2013 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
2014 readl(phba->CAregaddr); /* flush */
2015 }
dea31012005-04-17 16:05:31 -05002016}
2017
James Smarte59058c2008-08-24 21:49:00 -04002018/**
James Smart3621a712009-04-06 18:47:14 -04002019 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04002020 * @phba: Pointer to HBA context object.
2021 * @pring: Pointer to driver SLI ring object.
2022 *
2023 * This function is called with hbalock held to post pending iocbs
2024 * in the txq to the firmware. This function is called when driver
2025 * detects space available in the ring.
2026 **/
dea31012005-04-17 16:05:31 -05002027static void
James Smart2e0fef82007-06-17 19:56:36 -05002028lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05002029{
2030 IOCB_t *iocb;
2031 struct lpfc_iocbq *nextiocb;
2032
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002033 lockdep_assert_held(&phba->hbalock);
2034
dea31012005-04-17 16:05:31 -05002035 /*
2036 * Check to see if:
2037 * (a) there is anything on the txq to send
2038 * (b) link is up
2039 * (c) link attention events can be processed (fcp ring only)
2040 * (d) IOCB processing is not blocked by the outstanding mbox command.
2041 */
James Smart0e9bb8d2013-03-01 16:35:12 -05002042
2043 if (lpfc_is_link_up(phba) &&
2044 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08002045 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04002046 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05002047
2048 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2049 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
2050 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2051
2052 if (iocb)
2053 lpfc_sli_update_ring(phba, pring);
2054 else
2055 lpfc_sli_update_full_ring(phba, pring);
2056 }
2057
2058 return;
2059}
2060
James Smarte59058c2008-08-24 21:49:00 -04002061/**
James Smart3621a712009-04-06 18:47:14 -04002062 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002063 * @phba: Pointer to HBA context object.
2064 * @hbqno: HBQ number.
2065 *
2066 * This function is called with hbalock held to get the next
2067 * available slot for the given HBQ. If there is free slot
2068 * available for the HBQ it will return pointer to the next available
2069 * HBQ entry else it will return NULL.
2070 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002071static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05002072lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
2073{
2074 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2075
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002076 lockdep_assert_held(&phba->hbalock);
2077
James Smarted957682007-06-17 19:56:37 -05002078 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
2079 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
2080 hbqp->next_hbqPutIdx = 0;
2081
2082 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002083 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05002084 uint32_t getidx = le32_to_cpu(raw_index);
2085
2086 hbqp->local_hbqGetIdx = getidx;
2087
2088 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002089 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002090 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05002091 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04002092 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05002093 hbqp->entry_count);
2094
2095 phba->link_state = LPFC_HBA_ERROR;
2096 return NULL;
2097 }
2098
2099 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
2100 return NULL;
2101 }
2102
James Smart51ef4c22007-08-02 11:10:31 -04002103 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
2104 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05002105}
2106
James Smarte59058c2008-08-24 21:49:00 -04002107/**
James Smart3621a712009-04-06 18:47:14 -04002108 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04002109 * @phba: Pointer to HBA context object.
2110 *
2111 * This function is called with no lock held to free all the
2112 * hbq buffers while uninitializing the SLI interface. It also
2113 * frees the HBQ buffers returned by the firmware but not yet
2114 * processed by the upper layers.
2115 **/
James Smarted957682007-06-17 19:56:37 -05002116void
2117lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
2118{
James Smart92d7f7b2007-06-17 19:56:38 -05002119 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2120 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05002121 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04002122 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05002123
James Smart51ef4c22007-08-02 11:10:31 -04002124 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05002125 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05002126 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04002127 for (i = 0; i < hbq_count; ++i) {
2128 list_for_each_entry_safe(dmabuf, next_dmabuf,
2129 &phba->hbqs[i].hbq_buffer_list, list) {
2130 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
2131 list_del(&hbq_buf->dbuf.list);
2132 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
2133 }
James Smarta8adb832007-10-27 13:37:53 -04002134 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05002135 }
James Smart3163f722008-02-08 18:50:25 -05002136
2137 /* Mark the HBQs not in use */
2138 phba->hbq_in_use = 0;
2139 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05002140}
2141
James Smarte59058c2008-08-24 21:49:00 -04002142/**
James Smart3621a712009-04-06 18:47:14 -04002143 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002144 * @phba: Pointer to HBA context object.
2145 * @hbqno: HBQ number.
2146 * @hbq_buf: Pointer to HBQ buffer.
2147 *
2148 * This function is called with the hbalock held to post a
2149 * hbq buffer to the firmware. If the function finds an empty
2150 * slot in the HBQ, it will post the buffer. The function will return
2151 * pointer to the hbq entry if it successfully post the buffer
2152 * else it will return NULL.
2153 **/
James Smart3772a992009-05-22 14:50:54 -04002154static int
James Smarted957682007-06-17 19:56:37 -05002155lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05002156 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05002157{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002158 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002159 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
2160}
2161
2162/**
2163 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
2164 * @phba: Pointer to HBA context object.
2165 * @hbqno: HBQ number.
2166 * @hbq_buf: Pointer to HBQ buffer.
2167 *
2168 * This function is called with the hbalock held to post a hbq buffer to the
2169 * firmware. If the function finds an empty slot in the HBQ, it will post the
2170 * buffer and place it on the hbq_buffer_list. The function will return zero if
2171 * it successfully post the buffer else it will return an error.
2172 **/
2173static int
2174lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
2175 struct hbq_dmabuf *hbq_buf)
2176{
James Smarted957682007-06-17 19:56:37 -05002177 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05002178 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05002179
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002180 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05002181 /* Get next HBQ entry slot to use */
2182 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
2183 if (hbqe) {
2184 struct hbq_s *hbqp = &phba->hbqs[hbqno];
2185
James Smart92d7f7b2007-06-17 19:56:38 -05002186 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2187 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08002188 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05002189 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002190 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
2191 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
2192 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05002193 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
2194 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05002195 /* flush */
James Smarted957682007-06-17 19:56:37 -05002196 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04002197 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04002198 return 0;
2199 } else
2200 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05002201}
2202
James Smart4f774512009-05-22 14:52:35 -04002203/**
2204 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
2205 * @phba: Pointer to HBA context object.
2206 * @hbqno: HBQ number.
2207 * @hbq_buf: Pointer to HBQ buffer.
2208 *
2209 * This function is called with the hbalock held to post an RQE to the SLI4
2210 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
2211 * the hbq_buffer_list and return zero, otherwise it will return an error.
2212 **/
2213static int
2214lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
2215 struct hbq_dmabuf *hbq_buf)
2216{
2217 int rc;
2218 struct lpfc_rqe hrqe;
2219 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08002220 struct lpfc_queue *hrq;
2221 struct lpfc_queue *drq;
2222
2223 if (hbqno != LPFC_ELS_HBQ)
2224 return 1;
2225 hrq = phba->sli4_hba.hdr_rq;
2226 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002227
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002228 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002229 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2230 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2231 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2232 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002233 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002234 if (rc < 0)
2235 return rc;
James Smart895427b2017-02-12 13:52:30 -08002236 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002237 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2238 return 0;
2239}
2240
James Smarte59058c2008-08-24 21:49:00 -04002241/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002242static struct lpfc_hbq_init lpfc_els_hbq = {
2243 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002244 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002245 .mask_count = 0,
2246 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002247 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002248 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002249 .init_count = 40,
2250 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002251};
James Smarted957682007-06-17 19:56:37 -05002252
James Smarte59058c2008-08-24 21:49:00 -04002253/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002254struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002255 &lpfc_els_hbq,
2256};
2257
James Smarte59058c2008-08-24 21:49:00 -04002258/**
James Smart3621a712009-04-06 18:47:14 -04002259 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002260 * @phba: Pointer to HBA context object.
2261 * @hbqno: HBQ number.
2262 * @count: Number of HBQ buffers to be posted.
2263 *
James Smartd7c255b2008-08-24 21:50:00 -04002264 * This function is called with no lock held to post more hbq buffers to the
2265 * given HBQ. The function returns the number of HBQ buffers successfully
2266 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002267 **/
James Smart311464e2007-08-02 11:10:37 -04002268static int
James Smart92d7f7b2007-06-17 19:56:38 -05002269lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2270{
James Smartd7c255b2008-08-24 21:50:00 -04002271 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002272 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002273 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002274 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002275 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002276 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002277
James Smartd7c255b2008-08-24 21:50:00 -04002278 if ((phba->hbqs[hbqno].buffer_count + count) >
2279 lpfc_hbq_defs[hbqno]->entry_count)
2280 count = lpfc_hbq_defs[hbqno]->entry_count -
2281 phba->hbqs[hbqno].buffer_count;
2282 if (!count)
2283 return 0;
2284 /* Allocate HBQ entries */
2285 for (i = 0; i < count; i++) {
2286 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2287 if (!hbq_buffer)
2288 break;
2289 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2290 }
James Smart3163f722008-02-08 18:50:25 -05002291 /* Check whether HBQ is still in use */
2292 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002293 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002294 goto err;
2295 while (!list_empty(&hbq_buf_list)) {
2296 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2297 dbuf.list);
2298 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2299 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002300 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002301 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002302 posted++;
2303 } else
James Smart51ef4c22007-08-02 11:10:31 -04002304 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002305 }
James Smart3163f722008-02-08 18:50:25 -05002306 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002307 return posted;
2308err:
2309 spin_unlock_irqrestore(&phba->hbalock, flags);
2310 while (!list_empty(&hbq_buf_list)) {
2311 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2312 dbuf.list);
2313 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2314 }
James Smart92d7f7b2007-06-17 19:56:38 -05002315 return 0;
James Smarted957682007-06-17 19:56:37 -05002316}
2317
James Smarte59058c2008-08-24 21:49:00 -04002318/**
James Smart3621a712009-04-06 18:47:14 -04002319 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002320 * @phba: Pointer to HBA context object.
2321 * @qno: HBQ number.
2322 *
2323 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002324 * is called with no lock held. The function returns the number of HBQ entries
2325 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002326 **/
James Smarted957682007-06-17 19:56:37 -05002327int
James Smart92d7f7b2007-06-17 19:56:38 -05002328lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002329{
James Smartdef9c7a2009-12-21 17:02:28 -05002330 if (phba->sli_rev == LPFC_SLI_REV4)
2331 return 0;
2332 else
2333 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2334 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002335}
2336
James Smarte59058c2008-08-24 21:49:00 -04002337/**
James Smart3621a712009-04-06 18:47:14 -04002338 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002339 * @phba: Pointer to HBA context object.
2340 * @qno: HBQ queue number.
2341 *
2342 * This function is called from SLI initialization code path with
2343 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002344 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002345 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002346static int
James Smart92d7f7b2007-06-17 19:56:38 -05002347lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002348{
James Smartdef9c7a2009-12-21 17:02:28 -05002349 if (phba->sli_rev == LPFC_SLI_REV4)
2350 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002351 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002352 else
2353 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2354 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002355}
2356
Lee Jones7af29d42020-07-21 17:41:31 +01002357/*
James Smart3772a992009-05-22 14:50:54 -04002358 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
James Smart3772a992009-05-22 14:50:54 -04002359 *
2360 * This function removes the first hbq buffer on an hbq list and returns a
2361 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2362 **/
2363static struct hbq_dmabuf *
2364lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2365{
2366 struct lpfc_dmabuf *d_buf;
2367
2368 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2369 if (!d_buf)
2370 return NULL;
2371 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2372}
2373
2374/**
James Smart2d7dbc42017-02-12 13:52:35 -08002375 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2376 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01002377 * @hrq: HBQ number.
James Smart2d7dbc42017-02-12 13:52:35 -08002378 *
2379 * This function removes the first RQ buffer on an RQ buffer list and returns a
2380 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2381 **/
2382static struct rqb_dmabuf *
2383lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2384{
2385 struct lpfc_dmabuf *h_buf;
2386 struct lpfc_rqb *rqbp;
2387
2388 rqbp = hrq->rqbp;
2389 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2390 struct lpfc_dmabuf, list);
2391 if (!h_buf)
2392 return NULL;
2393 rqbp->buffer_count--;
2394 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2395}
2396
2397/**
James Smart3621a712009-04-06 18:47:14 -04002398 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002399 * @phba: Pointer to HBA context object.
2400 * @tag: Tag of the hbq buffer.
2401 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002402 * This function searches for the hbq buffer associated with the given tag in
2403 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2404 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002405 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002406static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002407lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2408{
James Smart92d7f7b2007-06-17 19:56:38 -05002409 struct lpfc_dmabuf *d_buf;
2410 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002411 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002412
James Smart51ef4c22007-08-02 11:10:31 -04002413 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002414 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002415 return NULL;
2416
James Smart3772a992009-05-22 14:50:54 -04002417 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002418 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002419 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002420 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002421 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002422 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002423 }
2424 }
James Smart3772a992009-05-22 14:50:54 -04002425 spin_unlock_irq(&phba->hbalock);
Dick Kennedy372c1872020-06-30 14:50:00 -07002426 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002427 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002428 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002429 return NULL;
James Smarted957682007-06-17 19:56:37 -05002430}
2431
James Smarte59058c2008-08-24 21:49:00 -04002432/**
James Smart3621a712009-04-06 18:47:14 -04002433 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002434 * @phba: Pointer to HBA context object.
2435 * @hbq_buffer: Pointer to HBQ buffer.
2436 *
2437 * This function is called with hbalock. This function gives back
2438 * the hbq buffer to firmware. If the HBQ does not have space to
2439 * post the buffer, it will free the buffer.
2440 **/
James Smarted957682007-06-17 19:56:37 -05002441void
James Smart51ef4c22007-08-02 11:10:31 -04002442lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002443{
2444 uint32_t hbqno;
2445
James Smart51ef4c22007-08-02 11:10:31 -04002446 if (hbq_buffer) {
2447 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002448 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002449 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002450 }
2451}
2452
James Smarte59058c2008-08-24 21:49:00 -04002453/**
James Smart3621a712009-04-06 18:47:14 -04002454 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002455 * @mbxCommand: mailbox command code.
2456 *
2457 * This function is called by the mailbox event handler function to verify
2458 * that the completed mailbox command is a legitimate mailbox command. If the
2459 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2460 * and the mailbox event handler will take the HBA offline.
2461 **/
dea31012005-04-17 16:05:31 -05002462static int
2463lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2464{
2465 uint8_t ret;
2466
2467 switch (mbxCommand) {
2468 case MBX_LOAD_SM:
2469 case MBX_READ_NV:
2470 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002471 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002472 case MBX_RUN_BIU_DIAG:
2473 case MBX_INIT_LINK:
2474 case MBX_DOWN_LINK:
2475 case MBX_CONFIG_LINK:
2476 case MBX_CONFIG_RING:
2477 case MBX_RESET_RING:
2478 case MBX_READ_CONFIG:
2479 case MBX_READ_RCONFIG:
2480 case MBX_READ_SPARM:
2481 case MBX_READ_STATUS:
2482 case MBX_READ_RPI:
2483 case MBX_READ_XRI:
2484 case MBX_READ_REV:
2485 case MBX_READ_LNK_STAT:
2486 case MBX_REG_LOGIN:
2487 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002488 case MBX_CLEAR_LA:
2489 case MBX_DUMP_MEMORY:
2490 case MBX_DUMP_CONTEXT:
2491 case MBX_RUN_DIAGS:
2492 case MBX_RESTART:
2493 case MBX_UPDATE_CFG:
2494 case MBX_DOWN_LOAD:
2495 case MBX_DEL_LD_ENTRY:
2496 case MBX_RUN_PROGRAM:
2497 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002498 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002499 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002500 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002501 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002502 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002503 case MBX_LOAD_AREA:
2504 case MBX_RUN_BIU_DIAG64:
2505 case MBX_CONFIG_PORT:
2506 case MBX_READ_SPARM64:
2507 case MBX_READ_RPI64:
2508 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002509 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002510 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002511 case MBX_SET_DEBUG:
2512 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002513 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002514 case MBX_REG_VPI:
2515 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002516 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002517 case MBX_PORT_CAPABILITIES:
2518 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002519 case MBX_SLI4_CONFIG:
2520 case MBX_SLI4_REQ_FTRS:
2521 case MBX_REG_FCFI:
2522 case MBX_UNREG_FCFI:
2523 case MBX_REG_VFI:
2524 case MBX_UNREG_VFI:
2525 case MBX_INIT_VPI:
2526 case MBX_INIT_VFI:
2527 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002528 case MBX_READ_EVENT_LOG_STATUS:
2529 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002530 case MBX_SECURITY_MGMT:
2531 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002532 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002533 ret = mbxCommand;
2534 break;
2535 default:
2536 ret = MBX_SHUTDOWN;
2537 break;
2538 }
James Smart2e0fef82007-06-17 19:56:36 -05002539 return ret;
dea31012005-04-17 16:05:31 -05002540}
James Smarte59058c2008-08-24 21:49:00 -04002541
2542/**
James Smart3621a712009-04-06 18:47:14 -04002543 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002544 * @phba: Pointer to HBA context object.
2545 * @pmboxq: Pointer to mailbox command.
2546 *
2547 * This is completion handler function for mailbox commands issued from
2548 * lpfc_sli_issue_mbox_wait function. This function is called by the
2549 * mailbox event handler function with no lock held. This function
2550 * will wake up thread waiting on the wait queue pointed by context1
2551 * of the mailbox.
2552 **/
James Smart04c68492009-05-22 14:52:52 -04002553void
James Smart2e0fef82007-06-17 19:56:36 -05002554lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002555{
James Smart858c9f62007-06-17 19:56:39 -05002556 unsigned long drvr_flag;
James Smarte29d74f2018-03-05 12:04:07 -08002557 struct completion *pmbox_done;
dea31012005-04-17 16:05:31 -05002558
2559 /*
James Smarte29d74f2018-03-05 12:04:07 -08002560 * If pmbox_done is empty, the driver thread gave up waiting and
dea31012005-04-17 16:05:31 -05002561 * continued running.
2562 */
James Smart7054a602007-04-25 09:52:34 -04002563 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002564 spin_lock_irqsave(&phba->hbalock, drvr_flag);
James Smarte29d74f2018-03-05 12:04:07 -08002565 pmbox_done = (struct completion *)pmboxq->context3;
2566 if (pmbox_done)
2567 complete(pmbox_done);
James Smart858c9f62007-06-17 19:56:39 -05002568 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002569 return;
2570}
2571
James Smartb95b2112019-08-14 16:56:47 -07002572static void
2573__lpfc_sli_rpi_release(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2574{
2575 unsigned long iflags;
2576
2577 if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
2578 lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
James Smartc6adba12020-11-15 11:26:34 -08002579 spin_lock_irqsave(&ndlp->lock, iflags);
James Smartb95b2112019-08-14 16:56:47 -07002580 ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
2581 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
James Smartc6adba12020-11-15 11:26:34 -08002582 spin_unlock_irqrestore(&ndlp->lock, iflags);
James Smartb95b2112019-08-14 16:56:47 -07002583 }
2584 ndlp->nlp_flag &= ~NLP_UNREG_INP;
2585}
James Smarte59058c2008-08-24 21:49:00 -04002586
2587/**
James Smart3621a712009-04-06 18:47:14 -04002588 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002589 * @phba: Pointer to HBA context object.
2590 * @pmb: Pointer to mailbox object.
2591 *
2592 * This function is the default mailbox completion handler. It
2593 * frees the memory resources associated with the completed mailbox
2594 * command. If the completed command is a REG_LOGIN mailbox command,
2595 * this function will issue a UREG_LOGIN to re-claim the RPI.
2596 **/
dea31012005-04-17 16:05:31 -05002597void
James Smart2e0fef82007-06-17 19:56:36 -05002598lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002599{
James Smartd439d282010-09-29 11:18:45 -04002600 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002601 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002602 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002603 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002604 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002605 int rc;
2606
James Smart3e1f0712018-11-29 16:09:29 -08002607 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smart7054a602007-04-25 09:52:34 -04002608
dea31012005-04-17 16:05:31 -05002609 if (mp) {
2610 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2611 kfree(mp);
2612 }
James Smart7054a602007-04-25 09:52:34 -04002613
2614 /*
2615 * If a REG_LOGIN succeeded after node is destroyed or node
2616 * is in re-discovery driver need to cleanup the RPI.
2617 */
James Smart2e0fef82007-06-17 19:56:36 -05002618 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002619 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2620 !pmb->u.mb.mbxStatus) {
2621 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002622 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart38503942020-03-22 11:12:53 -07002623 if (phba->sli_rev == LPFC_SLI_REV4)
2624 vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
James Smart04c68492009-05-22 14:52:52 -04002625 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002626 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002627 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002628 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2629 if (rc != MBX_NOT_FINISHED)
2630 return;
2631 }
2632
James Smart695a8142010-01-26 23:08:03 -05002633 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2634 !(phba->pport->load_flag & FC_UNLOADING) &&
2635 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002636 shost = lpfc_shost_from_vport(vport);
2637 spin_lock_irq(shost->host_lock);
2638 vport->vpi_state |= LPFC_VPI_REGISTERED;
2639 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2640 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002641 }
2642
James Smartd439d282010-09-29 11:18:45 -04002643 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -08002644 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -04002645 lpfc_nlp_put(ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002646 pmb->ctx_buf = NULL;
2647 pmb->ctx_ndlp = NULL;
2648 }
2649
2650 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2651 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
2652
2653 /* Check to see if there are any deferred events to process */
2654 if (ndlp) {
2655 lpfc_printf_vlog(
2656 vport,
2657 KERN_INFO, LOG_MBOX | LOG_DISCOVERY,
2658 "1438 UNREG cmpl deferred mbox x%x "
James Smarte9b11082020-11-15 11:26:33 -08002659 "on NPort x%x Data: x%x x%x %px x%x x%x\n",
James Smartdea16bd2018-11-29 16:09:30 -08002660 ndlp->nlp_rpi, ndlp->nlp_DID,
James Smarte9b11082020-11-15 11:26:33 -08002661 ndlp->nlp_flag, ndlp->nlp_defer_did,
2662 ndlp, vport->load_flag, kref_read(&ndlp->kref));
James Smartdea16bd2018-11-29 16:09:30 -08002663
2664 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2665 (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
James Smart00292e02018-12-13 15:17:55 -08002666 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002667 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
2668 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002669 } else {
James Smartb95b2112019-08-14 16:56:47 -07002670 __lpfc_sli_rpi_release(vport, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002671 }
James Smarta70e63e2020-11-15 11:26:38 -08002672
2673 /* The unreg_login mailbox is complete and had a
2674 * reference that has to be released. The PLOGI
2675 * got its own ref.
2676 */
2677 lpfc_nlp_put(ndlp);
James Smart9b164062019-03-12 16:30:06 -07002678 pmb->ctx_ndlp = NULL;
James Smartdea16bd2018-11-29 16:09:30 -08002679 }
James Smartd439d282010-09-29 11:18:45 -04002680 }
2681
James Smartdcf2a4e2010-09-29 11:18:53 -04002682 /* Check security permission status on INIT_LINK mailbox command */
2683 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2684 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
Dick Kennedy372c1872020-06-30 14:50:00 -07002685 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartdcf2a4e2010-09-29 11:18:53 -04002686 "2860 SLI authentication is required "
2687 "for INIT_LINK but has not done yet\n");
2688
James Smart04c68492009-05-22 14:52:52 -04002689 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2690 lpfc_sli4_mbox_cmd_free(phba, pmb);
2691 else
2692 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002693}
James Smartbe6bb942015-04-07 15:07:22 -04002694 /**
2695 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2696 * @phba: Pointer to HBA context object.
2697 * @pmb: Pointer to mailbox object.
2698 *
2699 * This function is the unreg rpi mailbox completion handler. It
2700 * frees the memory resources associated with the completed mailbox
James Smarta70e63e2020-11-15 11:26:38 -08002701 * command. An additional reference is put on the ndlp to prevent
James Smartbe6bb942015-04-07 15:07:22 -04002702 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2703 * the unreg mailbox command completes, this routine puts the
2704 * reference back.
2705 *
2706 **/
2707void
2708lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2709{
2710 struct lpfc_vport *vport = pmb->vport;
2711 struct lpfc_nodelist *ndlp;
2712
James Smart3e1f0712018-11-29 16:09:29 -08002713 ndlp = pmb->ctx_ndlp;
James Smartbe6bb942015-04-07 15:07:22 -04002714 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2715 if (phba->sli_rev == LPFC_SLI_REV4 &&
2716 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002717 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002718 LPFC_SLI_INTF_IF_TYPE_2)) {
2719 if (ndlp) {
James Smartdea16bd2018-11-29 16:09:30 -08002720 lpfc_printf_vlog(
James Smarte9b11082020-11-15 11:26:33 -08002721 vport, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smartdea16bd2018-11-29 16:09:30 -08002722 "0010 UNREG_LOGIN vpi:%x "
2723 "rpi:%x DID:%x defer x%x flg x%x "
James Smart307e3382020-11-15 11:26:30 -08002724 "%px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002725 vport->vpi, ndlp->nlp_rpi,
2726 ndlp->nlp_DID, ndlp->nlp_defer_did,
2727 ndlp->nlp_flag,
James Smart307e3382020-11-15 11:26:30 -08002728 ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002729 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartdea16bd2018-11-29 16:09:30 -08002730
2731 /* Check to see if there are any deferred
2732 * events to process
2733 */
2734 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2735 (ndlp->nlp_defer_did !=
2736 NLP_EVT_NOTHING_PENDING)) {
2737 lpfc_printf_vlog(
2738 vport, KERN_INFO, LOG_DISCOVERY,
2739 "4111 UNREG cmpl deferred "
2740 "clr x%x on "
James Smart32350662019-08-14 16:57:06 -07002741 "NPort x%x Data: x%x x%px\n",
James Smartdea16bd2018-11-29 16:09:30 -08002742 ndlp->nlp_rpi, ndlp->nlp_DID,
2743 ndlp->nlp_defer_did, ndlp);
James Smart00292e02018-12-13 15:17:55 -08002744 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08002745 ndlp->nlp_defer_did =
2746 NLP_EVT_NOTHING_PENDING;
2747 lpfc_issue_els_plogi(
2748 vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08002749 } else {
James Smartb95b2112019-08-14 16:56:47 -07002750 __lpfc_sli_rpi_release(vport, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08002751 }
James Smart4430f7f2020-11-15 11:26:31 -08002752
2753 lpfc_nlp_put(ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04002754 }
2755 }
2756 }
2757
2758 mempool_free(pmb, phba->mbox_mem_pool);
2759}
dea31012005-04-17 16:05:31 -05002760
James Smarte59058c2008-08-24 21:49:00 -04002761/**
James Smart3621a712009-04-06 18:47:14 -04002762 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002763 * @phba: Pointer to HBA context object.
2764 *
2765 * This function is called with no lock held. This function processes all
2766 * the completed mailbox commands and gives it to upper layers. The interrupt
2767 * service routine processes mailbox completion interrupt and adds completed
2768 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2769 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2770 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2771 * function returns the mailbox commands to the upper layer by calling the
2772 * completion handler function of each mailbox.
2773 **/
dea31012005-04-17 16:05:31 -05002774int
James Smart2e0fef82007-06-17 19:56:36 -05002775lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002776{
James Smart92d7f7b2007-06-17 19:56:38 -05002777 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002778 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002779 int rc;
2780 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002781
2782 phba->sli.slistat.mbox_event++;
2783
James Smart92d7f7b2007-06-17 19:56:38 -05002784 /* Get all completed mailboxe buffers into the cmplq */
2785 spin_lock_irq(&phba->hbalock);
2786 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2787 spin_unlock_irq(&phba->hbalock);
2788
dea31012005-04-17 16:05:31 -05002789 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002790 do {
2791 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2792 if (pmb == NULL)
2793 break;
2794
James Smart04c68492009-05-22 14:52:52 -04002795 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002796
James Smart858c9f62007-06-17 19:56:39 -05002797 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2798 if (pmb->vport) {
2799 lpfc_debugfs_disc_trc(pmb->vport,
2800 LPFC_DISC_TRC_MBOX_VPORT,
2801 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2802 (uint32_t)pmbox->mbxCommand,
2803 pmbox->un.varWords[0],
2804 pmbox->un.varWords[1]);
2805 }
2806 else {
2807 lpfc_debugfs_disc_trc(phba->pport,
2808 LPFC_DISC_TRC_MBOX,
2809 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2810 (uint32_t)pmbox->mbxCommand,
2811 pmbox->un.varWords[0],
2812 pmbox->un.varWords[1]);
2813 }
2814 }
2815
dea31012005-04-17 16:05:31 -05002816 /*
2817 * It is a fatal error if unknown mbox command completion.
2818 */
2819 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2820 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002821 /* Unknown mailbox command compl */
Dick Kennedy372c1872020-06-30 14:50:00 -07002822 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04002823 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002824 "x%x (x%x/x%x) Cmpl\n",
James Smart43bfea12019-09-21 20:58:57 -07002825 pmb->vport ? pmb->vport->vpi :
2826 LPFC_VPORT_UNKNOWN,
James Smart04c68492009-05-22 14:52:52 -04002827 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002828 lpfc_sli_config_mbox_subsys_get(phba,
2829 pmb),
2830 lpfc_sli_config_mbox_opcode_get(phba,
2831 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002832 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002833 phba->work_hs = HS_FFER3;
2834 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002835 continue;
dea31012005-04-17 16:05:31 -05002836 }
2837
dea31012005-04-17 16:05:31 -05002838 if (pmbox->mbxStatus) {
2839 phba->sli.slistat.mbox_stat_err++;
2840 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2841 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002842 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002843 LOG_MBOX | LOG_SLI,
2844 "(%d):0305 Mbox cmd cmpl "
2845 "error - RETRYing Data: x%x "
2846 "(x%x/x%x) x%x x%x x%x\n",
James Smart43bfea12019-09-21 20:58:57 -07002847 pmb->vport ? pmb->vport->vpi :
2848 LPFC_VPORT_UNKNOWN,
James Smarta183a152011-10-10 21:32:43 -04002849 pmbox->mbxCommand,
2850 lpfc_sli_config_mbox_subsys_get(phba,
2851 pmb),
2852 lpfc_sli_config_mbox_opcode_get(phba,
2853 pmb),
2854 pmbox->mbxStatus,
2855 pmbox->un.varWords[0],
James Smart43bfea12019-09-21 20:58:57 -07002856 pmb->vport ? pmb->vport->port_state :
2857 LPFC_VPORT_UNKNOWN);
dea31012005-04-17 16:05:31 -05002858 pmbox->mbxStatus = 0;
2859 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002860 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002861 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002862 continue;
dea31012005-04-17 16:05:31 -05002863 }
2864 }
2865
2866 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002867 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
Sakari Ailus2d44d162019-09-04 19:04:23 +03002868 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl %ps "
James Smarte74c03c2013-04-17 20:15:19 -04002869 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2870 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002871 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002872 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002873 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2874 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002875 pmb->mbox_cmpl,
2876 *((uint32_t *) pmbox),
2877 pmbox->un.varWords[0],
2878 pmbox->un.varWords[1],
2879 pmbox->un.varWords[2],
2880 pmbox->un.varWords[3],
2881 pmbox->un.varWords[4],
2882 pmbox->un.varWords[5],
2883 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002884 pmbox->un.varWords[7],
2885 pmbox->un.varWords[8],
2886 pmbox->un.varWords[9],
2887 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002888
James Smart92d7f7b2007-06-17 19:56:38 -05002889 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002890 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002891 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002892 return 0;
dea31012005-04-17 16:05:31 -05002893}
James Smart92d7f7b2007-06-17 19:56:38 -05002894
James Smarte59058c2008-08-24 21:49:00 -04002895/**
James Smart3621a712009-04-06 18:47:14 -04002896 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002897 * @phba: Pointer to HBA context object.
2898 * @pring: Pointer to driver SLI ring object.
2899 * @tag: buffer tag.
2900 *
2901 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2902 * is set in the tag the buffer is posted for a particular exchange,
2903 * the function will return the buffer without replacing the buffer.
2904 * If the buffer is for unsolicited ELS or CT traffic, this function
2905 * returns the buffer and also posts another buffer to the firmware.
2906 **/
James Smart76bb24e2007-10-27 13:38:00 -04002907static struct lpfc_dmabuf *
2908lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002909 struct lpfc_sli_ring *pring,
2910 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002911{
James Smart9f1e1b52008-12-04 22:39:40 -05002912 struct hbq_dmabuf *hbq_entry;
2913
James Smart76bb24e2007-10-27 13:38:00 -04002914 if (tag & QUE_BUFTAG_BIT)
2915 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002916 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2917 if (!hbq_entry)
2918 return NULL;
2919 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002920}
James Smart57127f12007-10-27 13:37:05 -04002921
James Smart3772a992009-05-22 14:50:54 -04002922/**
James Smart3a8070c2020-03-31 09:50:05 -07002923 * lpfc_nvme_unsol_ls_handler - Process an unsolicited event data buffer
2924 * containing a NVME LS request.
2925 * @phba: pointer to lpfc hba data structure.
2926 * @piocb: pointer to the iocbq struct representing the sequence starting
2927 * frame.
2928 *
2929 * This routine initially validates the NVME LS, validates there is a login
2930 * with the port that sent the LS, and then calls the appropriate nvme host
2931 * or target LS request handler.
2932 **/
2933static void
2934lpfc_nvme_unsol_ls_handler(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
2935{
2936 struct lpfc_nodelist *ndlp;
2937 struct lpfc_dmabuf *d_buf;
2938 struct hbq_dmabuf *nvmebuf;
2939 struct fc_frame_header *fc_hdr;
2940 struct lpfc_async_xchg_ctx *axchg = NULL;
2941 char *failwhy = NULL;
2942 uint32_t oxid, sid, did, fctl, size;
James Smart4e57e0b2020-05-20 11:59:28 -07002943 int ret = 1;
James Smart3a8070c2020-03-31 09:50:05 -07002944
2945 d_buf = piocb->context2;
2946
2947 nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2948 fc_hdr = nvmebuf->hbuf.virt;
2949 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2950 sid = sli4_sid_from_fc_hdr(fc_hdr);
2951 did = sli4_did_from_fc_hdr(fc_hdr);
2952 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
2953 fc_hdr->fh_f_ctl[1] << 8 |
2954 fc_hdr->fh_f_ctl[2]);
2955 size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
2956
2957 lpfc_nvmeio_data(phba, "NVME LS RCV: xri x%x sz %d from %06x\n",
2958 oxid, size, sid);
2959
2960 if (phba->pport->load_flag & FC_UNLOADING) {
2961 failwhy = "Driver Unloading";
2962 } else if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME)) {
2963 failwhy = "NVME FC4 Disabled";
2964 } else if (!phba->nvmet_support && !phba->pport->localport) {
2965 failwhy = "No Localport";
2966 } else if (phba->nvmet_support && !phba->targetport) {
2967 failwhy = "No Targetport";
2968 } else if (unlikely(fc_hdr->fh_r_ctl != FC_RCTL_ELS4_REQ)) {
2969 failwhy = "Bad NVME LS R_CTL";
2970 } else if (unlikely((fctl & 0x00FF0000) !=
2971 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT))) {
2972 failwhy = "Bad NVME LS F_CTL";
2973 } else {
2974 axchg = kzalloc(sizeof(*axchg), GFP_ATOMIC);
2975 if (!axchg)
2976 failwhy = "No CTX memory";
2977 }
2978
2979 if (unlikely(failwhy)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07002980 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3a8070c2020-03-31 09:50:05 -07002981 "6154 Drop NVME LS: SID %06X OXID x%X: %s\n",
2982 sid, oxid, failwhy);
2983 goto out_fail;
2984 }
2985
2986 /* validate the source of the LS is logged in */
2987 ndlp = lpfc_findnode_did(phba->pport, sid);
James Smart307e3382020-11-15 11:26:30 -08002988 if (!ndlp ||
James Smart3a8070c2020-03-31 09:50:05 -07002989 ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2990 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2991 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2992 "6216 NVME Unsol rcv: No ndlp: "
2993 "NPort_ID x%x oxid x%x\n",
2994 sid, oxid);
2995 goto out_fail;
2996 }
2997
2998 axchg->phba = phba;
2999 axchg->ndlp = ndlp;
3000 axchg->size = size;
3001 axchg->oxid = oxid;
3002 axchg->sid = sid;
3003 axchg->wqeq = NULL;
3004 axchg->state = LPFC_NVME_STE_LS_RCV;
3005 axchg->entry_cnt = 1;
3006 axchg->rqb_buffer = (void *)nvmebuf;
3007 axchg->hdwq = &phba->sli4_hba.hdwq[0];
3008 axchg->payload = nvmebuf->dbuf.virt;
3009 INIT_LIST_HEAD(&axchg->list);
3010
James Smart243156c2021-01-04 10:02:37 -08003011 if (phba->nvmet_support) {
James Smart3a8070c2020-03-31 09:50:05 -07003012 ret = lpfc_nvmet_handle_lsreq(phba, axchg);
James Smart243156c2021-01-04 10:02:37 -08003013 spin_lock_irq(&ndlp->lock);
3014 if (!ret && !(ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH)) {
3015 ndlp->fc4_xpt_flags |= NLP_XPT_HAS_HH;
3016 spin_unlock_irq(&ndlp->lock);
3017
3018 /* This reference is a single occurrence to hold the
3019 * node valid until the nvmet transport calls
3020 * host_release.
3021 */
3022 if (!lpfc_nlp_get(ndlp))
3023 goto out_fail;
3024
3025 lpfc_printf_log(phba, KERN_ERR, LOG_NODE,
3026 "6206 NVMET unsol ls_req ndlp %p "
3027 "DID x%x xflags x%x refcnt %d\n",
3028 ndlp, ndlp->nlp_DID,
3029 ndlp->fc4_xpt_flags,
3030 kref_read(&ndlp->kref));
3031 } else {
3032 spin_unlock_irq(&ndlp->lock);
3033 }
3034 } else {
James Smart3a8070c2020-03-31 09:50:05 -07003035 ret = lpfc_nvme_handle_lsreq(phba, axchg);
James Smart243156c2021-01-04 10:02:37 -08003036 }
James Smart3a8070c2020-03-31 09:50:05 -07003037
3038 /* if zero, LS was successfully handled. If non-zero, LS not handled */
3039 if (!ret)
3040 return;
3041
James Smart243156c2021-01-04 10:02:37 -08003042out_fail:
Dick Kennedy372c1872020-06-30 14:50:00 -07003043 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3a8070c2020-03-31 09:50:05 -07003044 "6155 Drop NVME LS from DID %06X: SID %06X OXID x%X "
3045 "NVMe%s handler failed %d\n",
3046 did, sid, oxid,
3047 (phba->nvmet_support) ? "T" : "I", ret);
3048
James Smart3a8070c2020-03-31 09:50:05 -07003049 /* recycle receive buffer */
3050 lpfc_in_buf_free(phba, &nvmebuf->dbuf);
3051
3052 /* If start of new exchange, abort it */
James Smart4e57e0b2020-05-20 11:59:28 -07003053 if (axchg && (fctl & FC_FC_FIRST_SEQ && !(fctl & FC_FC_EX_CTX)))
3054 ret = lpfc_nvme_unsol_ls_issue_abort(phba, axchg, sid, oxid);
3055
3056 if (ret)
3057 kfree(axchg);
James Smart3a8070c2020-03-31 09:50:05 -07003058}
3059
3060/**
James Smart3772a992009-05-22 14:50:54 -04003061 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
3062 * @phba: Pointer to HBA context object.
3063 * @pring: Pointer to driver SLI ring object.
3064 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
3065 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
3066 * @fch_type: the type for the first frame of the sequence.
3067 *
3068 * This function is called with no lock held. This function uses the r_ctl and
3069 * type of the received sequence to find the correct callback function to call
3070 * to process the sequence.
3071 **/
3072static int
3073lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3074 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
3075 uint32_t fch_type)
3076{
3077 int i;
3078
James Smartf358dd02017-02-12 13:52:34 -08003079 switch (fch_type) {
3080 case FC_TYPE_NVME:
James Smart3a8070c2020-03-31 09:50:05 -07003081 lpfc_nvme_unsol_ls_handler(phba, saveq);
James Smartf358dd02017-02-12 13:52:34 -08003082 return 1;
3083 default:
3084 break;
3085 }
3086
James Smart3772a992009-05-22 14:50:54 -04003087 /* unSolicited Responses */
3088 if (pring->prt[0].profile) {
3089 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
3090 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
3091 saveq);
3092 return 1;
3093 }
3094 /* We must search, based on rctl / type
3095 for the right routine */
3096 for (i = 0; i < pring->num_mask; i++) {
3097 if ((pring->prt[i].rctl == fch_r_ctl) &&
3098 (pring->prt[i].type == fch_type)) {
3099 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
3100 (pring->prt[i].lpfc_sli_rcv_unsol_event)
3101 (phba, pring, saveq);
3102 return 1;
3103 }
3104 }
3105 return 0;
3106}
James Smarte59058c2008-08-24 21:49:00 -04003107
3108/**
James Smart3621a712009-04-06 18:47:14 -04003109 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04003110 * @phba: Pointer to HBA context object.
3111 * @pring: Pointer to driver SLI ring object.
3112 * @saveq: Pointer to the unsolicited iocb.
3113 *
3114 * This function is called with no lock held by the ring event handler
3115 * when there is an unsolicited iocb posted to the response ring by the
3116 * firmware. This function gets the buffer associated with the iocbs
3117 * and calls the event handler for the ring. This function handles both
3118 * qring buffers and hbq buffers.
3119 * When the function returns 1 the caller can free the iocb object otherwise
3120 * upper layer functions will free the iocb objects.
3121 **/
dea31012005-04-17 16:05:31 -05003122static int
3123lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3124 struct lpfc_iocbq *saveq)
3125{
3126 IOCB_t * irsp;
3127 WORD5 * w5p;
3128 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04003129 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05003130 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05003131
dea31012005-04-17 16:05:31 -05003132 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04003133
3134 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
3135 if (pring->lpfc_sli_rcv_async_status)
3136 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
3137 else
3138 lpfc_printf_log(phba,
3139 KERN_WARNING,
3140 LOG_SLI,
3141 "0316 Ring %d handler: unexpected "
3142 "ASYNC_STATUS iocb received evt_code "
3143 "0x%x\n",
3144 pring->ringno,
3145 irsp->un.asyncstat.evt_code);
3146 return 1;
3147 }
3148
James Smart3163f722008-02-08 18:50:25 -05003149 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
3150 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
3151 if (irsp->ulpBdeCount > 0) {
3152 dmzbuf = lpfc_sli_get_buff(phba, pring,
3153 irsp->un.ulpWord[3]);
3154 lpfc_in_buf_free(phba, dmzbuf);
3155 }
3156
3157 if (irsp->ulpBdeCount > 1) {
3158 dmzbuf = lpfc_sli_get_buff(phba, pring,
3159 irsp->unsli3.sli3Words[3]);
3160 lpfc_in_buf_free(phba, dmzbuf);
3161 }
3162
3163 if (irsp->ulpBdeCount > 2) {
3164 dmzbuf = lpfc_sli_get_buff(phba, pring,
3165 irsp->unsli3.sli3Words[7]);
3166 lpfc_in_buf_free(phba, dmzbuf);
3167 }
3168
3169 return 1;
3170 }
3171
James Smart92d7f7b2007-06-17 19:56:38 -05003172 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04003173 if (irsp->ulpBdeCount != 0) {
3174 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05003175 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04003176 if (!saveq->context2)
3177 lpfc_printf_log(phba,
3178 KERN_ERR,
3179 LOG_SLI,
3180 "0341 Ring %d Cannot find buffer for "
3181 "an unsolicited iocb. tag 0x%x\n",
3182 pring->ringno,
3183 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04003184 }
3185 if (irsp->ulpBdeCount == 2) {
3186 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04003187 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04003188 if (!saveq->context3)
3189 lpfc_printf_log(phba,
3190 KERN_ERR,
3191 LOG_SLI,
3192 "0342 Ring %d Cannot find buffer for an"
3193 " unsolicited iocb. tag 0x%x\n",
3194 pring->ringno,
3195 irsp->unsli3.sli3Words[7]);
3196 }
3197 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04003198 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04003199 if (irsp->ulpBdeCount != 0) {
3200 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
3201 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05003202 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04003203 lpfc_printf_log(phba,
3204 KERN_ERR,
3205 LOG_SLI,
3206 "0343 Ring %d Cannot find "
3207 "buffer for an unsolicited iocb"
3208 ". tag 0x%x\n", pring->ringno,
3209 irsp->un.ulpWord[3]);
3210 }
3211 if (irsp->ulpBdeCount == 2) {
3212 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
3213 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05003214 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04003215 lpfc_printf_log(phba,
3216 KERN_ERR,
3217 LOG_SLI,
3218 "0344 Ring %d Cannot find "
3219 "buffer for an unsolicited "
3220 "iocb. tag 0x%x\n",
3221 pring->ringno,
3222 irsp->unsli3.sli3Words[7]);
3223 }
3224 }
James Smart92d7f7b2007-06-17 19:56:38 -05003225 }
James Smart9c2face2008-01-11 01:53:18 -05003226 if (irsp->ulpBdeCount != 0 &&
3227 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
3228 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
3229 int found = 0;
3230
3231 /* search continue save q for same XRI */
3232 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04003233 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
3234 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05003235 list_add_tail(&saveq->list, &iocbq->list);
3236 found = 1;
3237 break;
3238 }
3239 }
3240 if (!found)
3241 list_add_tail(&saveq->clist,
3242 &pring->iocb_continue_saveq);
3243 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
3244 list_del_init(&iocbq->clist);
3245 saveq = iocbq;
3246 irsp = &(saveq->iocb);
3247 } else
3248 return 0;
3249 }
3250 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
3251 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
3252 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04003253 Rctl = FC_RCTL_ELS_REQ;
3254 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05003255 } else {
3256 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
3257 Rctl = w5p->hcsw.Rctl;
3258 Type = w5p->hcsw.Type;
3259
3260 /* Firmware Workaround */
3261 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
3262 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
3263 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04003264 Rctl = FC_RCTL_ELS_REQ;
3265 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05003266 w5p->hcsw.Rctl = Rctl;
3267 w5p->hcsw.Type = Type;
3268 }
3269 }
James Smart92d7f7b2007-06-17 19:56:38 -05003270
James Smart3772a992009-05-22 14:50:54 -04003271 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05003272 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003273 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003274 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04003275 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04003276
James Smart92d7f7b2007-06-17 19:56:38 -05003277 return 1;
dea31012005-04-17 16:05:31 -05003278}
3279
James Smarte59058c2008-08-24 21:49:00 -04003280/**
James Smart3621a712009-04-06 18:47:14 -04003281 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04003282 * @phba: Pointer to HBA context object.
3283 * @pring: Pointer to driver SLI ring object.
3284 * @prspiocb: Pointer to response iocb object.
3285 *
3286 * This function looks up the iocb_lookup table to get the command iocb
3287 * corresponding to the given response iocb using the iotag of the
James Smarte2a8be52019-05-06 17:26:47 -07003288 * response iocb. The driver calls this function with the hbalock held
3289 * for SLI3 ports or the ring lock held for SLI4 ports.
James Smarte59058c2008-08-24 21:49:00 -04003290 * This function returns the command iocb object if it finds the command
3291 * iocb else returns NULL.
3292 **/
dea31012005-04-17 16:05:31 -05003293static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05003294lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
3295 struct lpfc_sli_ring *pring,
3296 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05003297{
dea31012005-04-17 16:05:31 -05003298 struct lpfc_iocbq *cmd_iocb = NULL;
3299 uint16_t iotag;
James Smarte2a8be52019-05-06 17:26:47 -07003300 spinlock_t *temp_lock = NULL;
3301 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05003302
James Smarte2a8be52019-05-06 17:26:47 -07003303 if (phba->sli_rev == LPFC_SLI_REV4)
3304 temp_lock = &pring->ring_lock;
3305 else
3306 temp_lock = &phba->hbalock;
3307
3308 spin_lock_irqsave(temp_lock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003309 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05003310
James Bottomley604a3e32005-10-29 10:28:33 -05003311 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3312 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003313 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07003314 /* remove from txcmpl queue list */
3315 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04003316 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003317 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003318 spin_unlock_irqrestore(temp_lock, iflag);
James Smart89533e92016-10-13 15:06:15 -07003319 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003320 }
dea31012005-04-17 16:05:31 -05003321 }
3322
James Smarte2a8be52019-05-06 17:26:47 -07003323 spin_unlock_irqrestore(temp_lock, iflag);
Dick Kennedy372c1872020-06-30 14:50:00 -07003324 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart89533e92016-10-13 15:06:15 -07003325 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05003326 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003327 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05003328 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05003329 return NULL;
3330}
3331
James Smarte59058c2008-08-24 21:49:00 -04003332/**
James Smart3772a992009-05-22 14:50:54 -04003333 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
3334 * @phba: Pointer to HBA context object.
3335 * @pring: Pointer to driver SLI ring object.
3336 * @iotag: IOCB tag.
3337 *
3338 * This function looks up the iocb_lookup table to get the command iocb
James Smarte2a8be52019-05-06 17:26:47 -07003339 * corresponding to the given iotag. The driver calls this function with
3340 * the ring lock held because this function is an SLI4 port only helper.
James Smart3772a992009-05-22 14:50:54 -04003341 * This function returns the command iocb object if it finds the command
3342 * iocb else returns NULL.
3343 **/
3344static struct lpfc_iocbq *
3345lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
3346 struct lpfc_sli_ring *pring, uint16_t iotag)
3347{
James Smart895427b2017-02-12 13:52:30 -08003348 struct lpfc_iocbq *cmd_iocb = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07003349 spinlock_t *temp_lock = NULL;
3350 unsigned long iflag = 0;
James Smart3772a992009-05-22 14:50:54 -04003351
James Smarte2a8be52019-05-06 17:26:47 -07003352 if (phba->sli_rev == LPFC_SLI_REV4)
3353 temp_lock = &pring->ring_lock;
3354 else
3355 temp_lock = &phba->hbalock;
3356
3357 spin_lock_irqsave(temp_lock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003358 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
3359 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04003360 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
3361 /* remove from txcmpl queue list */
3362 list_del_init(&cmd_iocb->list);
3363 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartc4908502019-01-28 11:14:28 -08003364 pring->txcmplq_cnt--;
James Smarte2a8be52019-05-06 17:26:47 -07003365 spin_unlock_irqrestore(temp_lock, iflag);
James Smart4f2e66c2012-05-09 21:17:07 -04003366 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04003367 }
James Smart3772a992009-05-22 14:50:54 -04003368 }
James Smart89533e92016-10-13 15:06:15 -07003369
James Smarte2a8be52019-05-06 17:26:47 -07003370 spin_unlock_irqrestore(temp_lock, iflag);
Dick Kennedy372c1872020-06-30 14:50:00 -07003371 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08003372 "0372 iotag x%x lookup error: max iotag (x%x) "
3373 "iocb_flag x%x\n",
3374 iotag, phba->sli.last_iotag,
3375 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04003376 return NULL;
3377}
3378
3379/**
James Smart3621a712009-04-06 18:47:14 -04003380 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04003381 * @phba: Pointer to HBA context object.
3382 * @pring: Pointer to driver SLI ring object.
3383 * @saveq: Pointer to the response iocb to be processed.
3384 *
3385 * This function is called by the ring event handler for non-fcp
3386 * rings when there is a new response iocb in the response ring.
3387 * The caller is not required to hold any locks. This function
3388 * gets the command iocb associated with the response iocb and
3389 * calls the completion handler for the command iocb. If there
3390 * is no completion handler, the function will free the resources
3391 * associated with command iocb. If the response iocb is for
3392 * an already aborted command iocb, the status of the completion
3393 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
3394 * This function always returns 1.
3395 **/
dea31012005-04-17 16:05:31 -05003396static int
James Smart2e0fef82007-06-17 19:56:36 -05003397lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05003398 struct lpfc_iocbq *saveq)
3399{
James Smart2e0fef82007-06-17 19:56:36 -05003400 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05003401 int rc = 1;
3402 unsigned long iflag;
3403
James Bottomley604a3e32005-10-29 10:28:33 -05003404 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
dea31012005-04-17 16:05:31 -05003405 if (cmdiocbp) {
3406 if (cmdiocbp->iocb_cmpl) {
3407 /*
James Smartea2151b2008-09-07 11:52:10 -04003408 * If an ELS command failed send an event to mgmt
3409 * application.
3410 */
3411 if (saveq->iocb.ulpStatus &&
3412 (pring->ringno == LPFC_ELS_RING) &&
3413 (cmdiocbp->iocb.ulpCommand ==
3414 CMD_ELS_REQUEST64_CR))
3415 lpfc_send_els_failure_event(phba,
3416 cmdiocbp, saveq);
3417
3418 /*
dea31012005-04-17 16:05:31 -05003419 * Post all ELS completions to the worker thread.
3420 * All other are passed to the completion callback.
3421 */
3422 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05003423 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3424 (cmdiocbp->iocb_flag &
3425 LPFC_DRIVER_ABORTED)) {
3426 spin_lock_irqsave(&phba->hbalock,
3427 iflag);
James Smart07951072007-04-25 09:51:38 -04003428 cmdiocbp->iocb_flag &=
3429 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05003430 spin_unlock_irqrestore(&phba->hbalock,
3431 iflag);
James Smart07951072007-04-25 09:51:38 -04003432 saveq->iocb.ulpStatus =
3433 IOSTAT_LOCAL_REJECT;
3434 saveq->iocb.un.ulpWord[4] =
3435 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05003436
3437 /* Firmware could still be in progress
3438 * of DMAing payload, so don't free data
3439 * buffer till after a hbeat.
3440 */
James Smart341af102010-01-26 23:07:37 -05003441 spin_lock_irqsave(&phba->hbalock,
3442 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05003443 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05003444 spin_unlock_irqrestore(&phba->hbalock,
3445 iflag);
3446 }
James Smart0f65ff62010-02-26 14:14:23 -05003447 if (phba->sli_rev == LPFC_SLI_REV4) {
3448 if (saveq->iocb_flag &
3449 LPFC_EXCHANGE_BUSY) {
3450 /* Set cmdiocb flag for the
3451 * exchange busy so sgl (xri)
3452 * will not be released until
3453 * the abort xri is received
3454 * from hba.
3455 */
3456 spin_lock_irqsave(
3457 &phba->hbalock, iflag);
3458 cmdiocbp->iocb_flag |=
3459 LPFC_EXCHANGE_BUSY;
3460 spin_unlock_irqrestore(
3461 &phba->hbalock, iflag);
3462 }
3463 if (cmdiocbp->iocb_flag &
3464 LPFC_DRIVER_ABORTED) {
3465 /*
3466 * Clear LPFC_DRIVER_ABORTED
3467 * bit in case it was driver
3468 * initiated abort.
3469 */
3470 spin_lock_irqsave(
3471 &phba->hbalock, iflag);
3472 cmdiocbp->iocb_flag &=
3473 ~LPFC_DRIVER_ABORTED;
3474 spin_unlock_irqrestore(
3475 &phba->hbalock, iflag);
3476 cmdiocbp->iocb.ulpStatus =
3477 IOSTAT_LOCAL_REJECT;
3478 cmdiocbp->iocb.un.ulpWord[4] =
3479 IOERR_ABORT_REQUESTED;
3480 /*
3481 * For SLI4, irsiocb contains
3482 * NO_XRI in sli_xritag, it
3483 * shall not affect releasing
3484 * sgl (xri) process.
3485 */
3486 saveq->iocb.ulpStatus =
3487 IOSTAT_LOCAL_REJECT;
3488 saveq->iocb.un.ulpWord[4] =
3489 IOERR_SLI_ABORTED;
3490 spin_lock_irqsave(
3491 &phba->hbalock, iflag);
3492 saveq->iocb_flag |=
3493 LPFC_DELAY_MEM_FREE;
3494 spin_unlock_irqrestore(
3495 &phba->hbalock, iflag);
3496 }
James Smart07951072007-04-25 09:51:38 -04003497 }
dea31012005-04-17 16:05:31 -05003498 }
James Smart2e0fef82007-06-17 19:56:36 -05003499 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003500 } else
3501 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003502 } else {
3503 /*
3504 * Unknown initiating command based on the response iotag.
3505 * This could be the case on the ELS ring because of
3506 * lpfc_els_abort().
3507 */
3508 if (pring->ringno != LPFC_ELS_RING) {
3509 /*
3510 * Ring <ringno> handler: unexpected completion IoTag
3511 * <IoTag>
3512 */
James Smarta257bf92009-04-06 18:48:10 -04003513 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003514 "0322 Ring %d handler: "
3515 "unexpected completion IoTag x%x "
3516 "Data: x%x x%x x%x x%x\n",
3517 pring->ringno,
3518 saveq->iocb.ulpIoTag,
3519 saveq->iocb.ulpStatus,
3520 saveq->iocb.un.ulpWord[4],
3521 saveq->iocb.ulpCommand,
3522 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003523 }
3524 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003525
dea31012005-04-17 16:05:31 -05003526 return rc;
3527}
3528
James Smarte59058c2008-08-24 21:49:00 -04003529/**
James Smart3621a712009-04-06 18:47:14 -04003530 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003531 * @phba: Pointer to HBA context object.
3532 * @pring: Pointer to driver SLI ring object.
3533 *
3534 * This function is called from the iocb ring event handlers when
3535 * put pointer is ahead of the get pointer for a ring. This function signal
3536 * an error attention condition to the worker thread and the worker
3537 * thread will transition the HBA to offline state.
3538 **/
James Smart2e0fef82007-06-17 19:56:36 -05003539static void
3540lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003541{
James Smart34b02dc2008-08-24 21:49:55 -04003542 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003543 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003544 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003545 * rsp ring <portRspMax>
3546 */
Dick Kennedy372c1872020-06-30 14:50:00 -07003547 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003548 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003549 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003550 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003551 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003552
James Smart2e0fef82007-06-17 19:56:36 -05003553 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003554
3555 /*
3556 * All error attention handlers are posted to
3557 * worker thread
3558 */
3559 phba->work_ha |= HA_ERATT;
3560 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003561
James Smart5e9d9b82008-06-14 22:52:53 -04003562 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003563
3564 return;
3565}
3566
James Smarte59058c2008-08-24 21:49:00 -04003567/**
James Smart3621a712009-04-06 18:47:14 -04003568 * lpfc_poll_eratt - Error attention polling timer timeout handler
Lee Jones7af29d42020-07-21 17:41:31 +01003569 * @t: Context to fetch pointer to address of HBA context object from.
James Smart93996272008-08-24 21:50:30 -04003570 *
3571 * This function is invoked by the Error Attention polling timer when the
3572 * timer times out. It will check the SLI Error Attention register for
3573 * possible attention events. If so, it will post an Error Attention event
3574 * and wake up worker thread to process it. Otherwise, it will set up the
3575 * Error Attention polling timer for the next poll.
3576 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003577void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003578{
3579 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003580 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003581 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003582
Kees Cookf22eb4d2017-09-06 20:24:26 -07003583 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003584
James Smartaa6fbb72012-08-03 12:36:03 -04003585 /* Here we will also keep track of interrupts per sec of the hba */
3586 sli_intr = phba->sli.slistat.sli_intr;
3587
3588 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3589 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3590 sli_intr);
3591 else
3592 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3593
James Smart65791f12016-07-06 12:35:56 -07003594 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3595 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003596 phba->sli.slistat.sli_ips = cnt;
3597
3598 phba->sli.slistat.sli_prev_intr = sli_intr;
3599
James Smart93996272008-08-24 21:50:30 -04003600 /* Check chip HA register for error event */
3601 eratt = lpfc_sli_check_eratt(phba);
3602
3603 if (eratt)
3604 /* Tell the worker thread there is work to do */
3605 lpfc_worker_wake_up(phba);
3606 else
3607 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003608 mod_timer(&phba->eratt_poll,
3609 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003610 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003611 return;
3612}
3613
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003614
James Smarte59058c2008-08-24 21:49:00 -04003615/**
James Smart3621a712009-04-06 18:47:14 -04003616 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003617 * @phba: Pointer to HBA context object.
3618 * @pring: Pointer to driver SLI ring object.
3619 * @mask: Host attention register mask for this ring.
3620 *
3621 * This function is called from the interrupt context when there is a ring
3622 * event for the fcp ring. The caller does not hold any lock.
3623 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003624 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003625 * LE bit set. The function will call the completion handler of the command iocb
3626 * if the response iocb indicates a completion for a command iocb or it is
3627 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3628 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003629 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003630 * to check it explicitly.
3631 */
3632int
James Smart2e0fef82007-06-17 19:56:36 -05003633lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3634 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003635{
James Smart34b02dc2008-08-24 21:49:55 -04003636 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003637 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003638 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003639 struct lpfc_iocbq *cmdiocbq = NULL;
3640 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003641 uint32_t status;
3642 uint32_t portRspPut, portRspMax;
3643 int rc = 1;
3644 lpfc_iocb_type type;
3645 unsigned long iflag;
3646 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003647
James Smart2e0fef82007-06-17 19:56:36 -05003648 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003649 pring->stats.iocb_event++;
3650
dea31012005-04-17 16:05:31 -05003651 /*
3652 * The next available response entry should never exceed the maximum
3653 * entries. If it does, treat it as an adapter hardware error.
3654 */
James Smart7e56aa22012-08-03 12:35:34 -04003655 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003656 portRspPut = le32_to_cpu(pgp->rspPutInx);
3657 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003658 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003659 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003660 return 1;
3661 }
James Smart45ed1192009-10-02 15:17:02 -04003662 if (phba->fcp_ring_in_use) {
3663 spin_unlock_irqrestore(&phba->hbalock, iflag);
3664 return 1;
3665 } else
3666 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003667
3668 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003669 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003670 /*
3671 * Fetch an entry off the ring and copy it into a local data
3672 * structure. The copy involves a byte-swap since the
3673 * network byte order and pci byte orders are different.
3674 */
James Smarted957682007-06-17 19:56:37 -05003675 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003676 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003677
James Smart7e56aa22012-08-03 12:35:34 -04003678 if (++pring->sli.sli3.rspidx >= portRspMax)
3679 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003680
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003681 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3682 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003683 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003684 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003685 irsp = &rspiocbq.iocb;
3686
dea31012005-04-17 16:05:31 -05003687 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3688 pring->stats.iocb_rsp++;
3689 rsp_cmpl++;
3690
3691 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003692 /*
3693 * If resource errors reported from HBA, reduce
3694 * queuedepths of the SCSI device.
3695 */
3696 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003697 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3698 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003699 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003700 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003701 spin_lock_irqsave(&phba->hbalock, iflag);
3702 }
3703
dea31012005-04-17 16:05:31 -05003704 /* Rsp ring <ringno> error: IOCB */
3705 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003706 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003707 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003708 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003709 irsp->un.ulpWord[0],
3710 irsp->un.ulpWord[1],
3711 irsp->un.ulpWord[2],
3712 irsp->un.ulpWord[3],
3713 irsp->un.ulpWord[4],
3714 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003715 *(uint32_t *)&irsp->un1,
3716 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003717 }
3718
3719 switch (type) {
3720 case LPFC_ABORT_IOCB:
3721 case LPFC_SOL_IOCB:
3722 /*
3723 * Idle exchange closed via ABTS from port. No iocb
3724 * resources need to be recovered.
3725 */
3726 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003727 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003728 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003729 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003730 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003731 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003732 break;
3733 }
3734
James Smarte2a8be52019-05-06 17:26:47 -07003735 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05003736 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3737 &rspiocbq);
James Smarte2a8be52019-05-06 17:26:47 -07003738 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05003739 if (unlikely(!cmdiocbq))
3740 break;
3741 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3742 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3743 if (cmdiocbq->iocb_cmpl) {
3744 spin_unlock_irqrestore(&phba->hbalock, iflag);
3745 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3746 &rspiocbq);
3747 spin_lock_irqsave(&phba->hbalock, iflag);
3748 }
dea31012005-04-17 16:05:31 -05003749 break;
James Smarta4bc3372006-12-02 13:34:16 -05003750 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003751 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003752 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003753 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003754 break;
dea31012005-04-17 16:05:31 -05003755 default:
3756 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3757 char adaptermsg[LPFC_MAX_ADPTMSG];
3758 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3759 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3760 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003761 dev_warn(&((phba->pcidev)->dev),
3762 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003763 phba->brd_no, adaptermsg);
3764 } else {
3765 /* Unknown IOCB command */
Dick Kennedy372c1872020-06-30 14:50:00 -07003766 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003767 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003768 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003769 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003770 irsp->ulpStatus,
3771 irsp->ulpIoTag,
3772 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003773 }
3774 break;
3775 }
3776
3777 /*
3778 * The response IOCB has been processed. Update the ring
3779 * pointer in SLIM. If the port response put pointer has not
3780 * been updated, sync the pgp->rspPutInx and fetch the new port
3781 * response put pointer.
3782 */
James Smart7e56aa22012-08-03 12:35:34 -04003783 writel(pring->sli.sli3.rspidx,
3784 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003785
James Smart7e56aa22012-08-03 12:35:34 -04003786 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003787 portRspPut = le32_to_cpu(pgp->rspPutInx);
3788 }
3789
3790 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3791 pring->stats.iocb_rsp_full++;
3792 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3793 writel(status, phba->CAregaddr);
3794 readl(phba->CAregaddr);
3795 }
3796 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3797 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3798 pring->stats.iocb_cmd_empty++;
3799
3800 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003801 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003802 lpfc_sli_resume_iocb(phba, pring);
3803
3804 if ((pring->lpfc_sli_cmd_available))
3805 (pring->lpfc_sli_cmd_available) (phba, pring);
3806
3807 }
3808
James Smart45ed1192009-10-02 15:17:02 -04003809 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003810 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003811 return rc;
3812}
3813
James Smarte59058c2008-08-24 21:49:00 -04003814/**
James Smart3772a992009-05-22 14:50:54 -04003815 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3816 * @phba: Pointer to HBA context object.
3817 * @pring: Pointer to driver SLI ring object.
3818 * @rspiocbp: Pointer to driver response IOCB object.
3819 *
3820 * This function is called from the worker thread when there is a slow-path
3821 * response IOCB to process. This function chains all the response iocbs until
3822 * seeing the iocb with the LE bit set. The function will call
3823 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3824 * completion of a command iocb. The function will call the
3825 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3826 * The function frees the resources or calls the completion handler if this
3827 * iocb is an abort completion. The function returns NULL when the response
3828 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3829 * this function shall chain the iocb on to the iocb_continueq and return the
3830 * response iocb passed in.
3831 **/
3832static struct lpfc_iocbq *
3833lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3834 struct lpfc_iocbq *rspiocbp)
3835{
3836 struct lpfc_iocbq *saveq;
3837 struct lpfc_iocbq *cmdiocbp;
3838 struct lpfc_iocbq *next_iocb;
3839 IOCB_t *irsp = NULL;
3840 uint32_t free_saveq;
3841 uint8_t iocb_cmd_type;
3842 lpfc_iocb_type type;
3843 unsigned long iflag;
3844 int rc;
3845
3846 spin_lock_irqsave(&phba->hbalock, iflag);
3847 /* First add the response iocb to the countinueq list */
3848 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3849 pring->iocb_continueq_cnt++;
3850
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003851 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003852 irsp = &rspiocbp->iocb;
3853 if (irsp->ulpLe) {
3854 /*
3855 * By default, the driver expects to free all resources
3856 * associated with this iocb completion.
3857 */
3858 free_saveq = 1;
3859 saveq = list_get_first(&pring->iocb_continueq,
3860 struct lpfc_iocbq, list);
3861 irsp = &(saveq->iocb);
3862 list_del_init(&pring->iocb_continueq);
3863 pring->iocb_continueq_cnt = 0;
3864
3865 pring->stats.iocb_rsp++;
3866
3867 /*
3868 * If resource errors reported from HBA, reduce
3869 * queuedepths of the SCSI device.
3870 */
3871 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003872 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3873 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003874 spin_unlock_irqrestore(&phba->hbalock, iflag);
3875 phba->lpfc_rampdown_queue_depth(phba);
3876 spin_lock_irqsave(&phba->hbalock, iflag);
3877 }
3878
3879 if (irsp->ulpStatus) {
3880 /* Rsp ring <ringno> error: IOCB */
3881 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3882 "0328 Rsp Ring %d error: "
3883 "IOCB Data: "
3884 "x%x x%x x%x x%x "
3885 "x%x x%x x%x x%x "
3886 "x%x x%x x%x x%x "
3887 "x%x x%x x%x x%x\n",
3888 pring->ringno,
3889 irsp->un.ulpWord[0],
3890 irsp->un.ulpWord[1],
3891 irsp->un.ulpWord[2],
3892 irsp->un.ulpWord[3],
3893 irsp->un.ulpWord[4],
3894 irsp->un.ulpWord[5],
3895 *(((uint32_t *) irsp) + 6),
3896 *(((uint32_t *) irsp) + 7),
3897 *(((uint32_t *) irsp) + 8),
3898 *(((uint32_t *) irsp) + 9),
3899 *(((uint32_t *) irsp) + 10),
3900 *(((uint32_t *) irsp) + 11),
3901 *(((uint32_t *) irsp) + 12),
3902 *(((uint32_t *) irsp) + 13),
3903 *(((uint32_t *) irsp) + 14),
3904 *(((uint32_t *) irsp) + 15));
3905 }
3906
3907 /*
3908 * Fetch the IOCB command type and call the correct completion
3909 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3910 * get freed back to the lpfc_iocb_list by the discovery
3911 * kernel thread.
3912 */
3913 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3914 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3915 switch (type) {
3916 case LPFC_SOL_IOCB:
3917 spin_unlock_irqrestore(&phba->hbalock, iflag);
3918 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3919 spin_lock_irqsave(&phba->hbalock, iflag);
3920 break;
3921
3922 case LPFC_UNSOL_IOCB:
3923 spin_unlock_irqrestore(&phba->hbalock, iflag);
3924 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3925 spin_lock_irqsave(&phba->hbalock, iflag);
3926 if (!rc)
3927 free_saveq = 0;
3928 break;
3929
3930 case LPFC_ABORT_IOCB:
3931 cmdiocbp = NULL;
James Smarte2a8be52019-05-06 17:26:47 -07003932 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX) {
3933 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003934 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3935 saveq);
James Smarte2a8be52019-05-06 17:26:47 -07003936 spin_lock_irqsave(&phba->hbalock, iflag);
3937 }
James Smart3772a992009-05-22 14:50:54 -04003938 if (cmdiocbp) {
3939 /* Call the specified completion routine */
3940 if (cmdiocbp->iocb_cmpl) {
3941 spin_unlock_irqrestore(&phba->hbalock,
3942 iflag);
3943 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3944 saveq);
3945 spin_lock_irqsave(&phba->hbalock,
3946 iflag);
3947 } else
3948 __lpfc_sli_release_iocbq(phba,
3949 cmdiocbp);
3950 }
3951 break;
3952
3953 case LPFC_UNKNOWN_IOCB:
3954 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3955 char adaptermsg[LPFC_MAX_ADPTMSG];
3956 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3957 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3958 MAX_MSG_DATA);
3959 dev_warn(&((phba->pcidev)->dev),
3960 "lpfc%d: %s\n",
3961 phba->brd_no, adaptermsg);
3962 } else {
3963 /* Unknown IOCB command */
Dick Kennedy372c1872020-06-30 14:50:00 -07003964 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04003965 "0335 Unknown IOCB "
3966 "command Data: x%x "
3967 "x%x x%x x%x\n",
3968 irsp->ulpCommand,
3969 irsp->ulpStatus,
3970 irsp->ulpIoTag,
3971 irsp->ulpContext);
3972 }
3973 break;
3974 }
3975
3976 if (free_saveq) {
3977 list_for_each_entry_safe(rspiocbp, next_iocb,
3978 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003979 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003980 __lpfc_sli_release_iocbq(phba, rspiocbp);
3981 }
3982 __lpfc_sli_release_iocbq(phba, saveq);
3983 }
3984 rspiocbp = NULL;
3985 }
3986 spin_unlock_irqrestore(&phba->hbalock, iflag);
3987 return rspiocbp;
3988}
3989
3990/**
3991 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003992 * @phba: Pointer to HBA context object.
3993 * @pring: Pointer to driver SLI ring object.
3994 * @mask: Host attention register mask for this ring.
3995 *
James Smart3772a992009-05-22 14:50:54 -04003996 * This routine wraps the actual slow_ring event process routine from the
3997 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003998 **/
James Smart3772a992009-05-22 14:50:54 -04003999void
James Smart2e0fef82007-06-17 19:56:36 -05004000lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
4001 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05004002{
James Smart3772a992009-05-22 14:50:54 -04004003 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
4004}
4005
4006/**
4007 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
4008 * @phba: Pointer to HBA context object.
4009 * @pring: Pointer to driver SLI ring object.
4010 * @mask: Host attention register mask for this ring.
4011 *
4012 * This function is called from the worker thread when there is a ring event
4013 * for non-fcp rings. The caller does not hold any lock. The function will
4014 * remove each response iocb in the response ring and calls the handle
4015 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4016 **/
4017static void
4018lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
4019 struct lpfc_sli_ring *pring, uint32_t mask)
4020{
James Smart34b02dc2008-08-24 21:49:55 -04004021 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05004022 IOCB_t *entry;
4023 IOCB_t *irsp = NULL;
4024 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05004025 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05004026 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04004027 uint32_t status;
dea31012005-04-17 16:05:31 -05004028
James Smart34b02dc2008-08-24 21:49:55 -04004029 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05004030 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05004031 pring->stats.iocb_event++;
4032
dea31012005-04-17 16:05:31 -05004033 /*
4034 * The next available response entry should never exceed the maximum
4035 * entries. If it does, treat it as an adapter hardware error.
4036 */
James Smart7e56aa22012-08-03 12:35:34 -04004037 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05004038 portRspPut = le32_to_cpu(pgp->rspPutInx);
4039 if (portRspPut >= portRspMax) {
4040 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004041 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05004042 * rsp ring <portRspMax>
4043 */
Dick Kennedy372c1872020-06-30 14:50:00 -07004044 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04004045 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004046 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04004047 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05004048
James Smart2e0fef82007-06-17 19:56:36 -05004049 phba->link_state = LPFC_HBA_ERROR;
4050 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05004051
4052 phba->work_hs = HS_FFER3;
4053 lpfc_handle_eratt(phba);
4054
James Smart3772a992009-05-22 14:50:54 -04004055 return;
dea31012005-04-17 16:05:31 -05004056 }
4057
4058 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04004059 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05004060 /*
4061 * Build a completion list and call the appropriate handler.
4062 * The process is to get the next available response iocb, get
4063 * a free iocb from the list, copy the response data into the
4064 * free iocb, insert to the continuation list, and update the
4065 * next response index to slim. This process makes response
4066 * iocb's in the ring available to DMA as fast as possible but
4067 * pays a penalty for a copy operation. Since the iocb is
4068 * only 32 bytes, this penalty is considered small relative to
4069 * the PCI reads for register values and a slim write. When
4070 * the ulpLe field is set, the entire Command has been
4071 * received.
4072 */
James Smarted957682007-06-17 19:56:37 -05004073 entry = lpfc_resp_iocb(phba, pring);
4074
James Smart858c9f62007-06-17 19:56:39 -05004075 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05004076 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05004077 if (rspiocbp == NULL) {
4078 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07004079 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05004080 break;
4081 }
4082
James Smarted957682007-06-17 19:56:37 -05004083 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
4084 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05004085 irsp = &rspiocbp->iocb;
4086
James Smart7e56aa22012-08-03 12:35:34 -04004087 if (++pring->sli.sli3.rspidx >= portRspMax)
4088 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05004089
James Smarta58cbd52007-08-02 11:09:43 -04004090 if (pring->ringno == LPFC_ELS_RING) {
4091 lpfc_debugfs_slow_ring_trc(phba,
4092 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
4093 *(((uint32_t *) irsp) + 4),
4094 *(((uint32_t *) irsp) + 6),
4095 *(((uint32_t *) irsp) + 7));
4096 }
4097
James Smart7e56aa22012-08-03 12:35:34 -04004098 writel(pring->sli.sli3.rspidx,
4099 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05004100
James Smart3772a992009-05-22 14:50:54 -04004101 spin_unlock_irqrestore(&phba->hbalock, iflag);
4102 /* Handle the response IOCB */
4103 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
4104 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05004105
4106 /*
4107 * If the port response put pointer has not been updated, sync
4108 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
4109 * response put pointer.
4110 */
James Smart7e56aa22012-08-03 12:35:34 -04004111 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05004112 portRspPut = le32_to_cpu(pgp->rspPutInx);
4113 }
James Smart7e56aa22012-08-03 12:35:34 -04004114 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05004115
James Smart92d7f7b2007-06-17 19:56:38 -05004116 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05004117 /* At least one response entry has been freed */
4118 pring->stats.iocb_rsp_full++;
4119 /* SET RxRE_RSP in Chip Att register */
4120 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
4121 writel(status, phba->CAregaddr);
4122 readl(phba->CAregaddr); /* flush */
4123 }
4124 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
4125 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
4126 pring->stats.iocb_cmd_empty++;
4127
4128 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04004129 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05004130 lpfc_sli_resume_iocb(phba, pring);
4131
4132 if ((pring->lpfc_sli_cmd_available))
4133 (pring->lpfc_sli_cmd_available) (phba, pring);
4134
4135 }
4136
James Smart2e0fef82007-06-17 19:56:36 -05004137 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04004138 return;
dea31012005-04-17 16:05:31 -05004139}
4140
James Smarte59058c2008-08-24 21:49:00 -04004141/**
James Smart4f774512009-05-22 14:52:35 -04004142 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
4143 * @phba: Pointer to HBA context object.
4144 * @pring: Pointer to driver SLI ring object.
4145 * @mask: Host attention register mask for this ring.
4146 *
4147 * This function is called from the worker thread when there is a pending
4148 * ELS response iocb on the driver internal slow-path response iocb worker
4149 * queue. The caller does not hold any lock. The function will remove each
4150 * response iocb from the response worker queue and calls the handle
4151 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
4152 **/
4153static void
4154lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
4155 struct lpfc_sli_ring *pring, uint32_t mask)
4156{
4157 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04004158 struct hbq_dmabuf *dmabuf;
4159 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04004160 unsigned long iflag;
James Smart0ef01a22018-09-10 10:30:45 -07004161 int count = 0;
James Smart4f774512009-05-22 14:52:35 -04004162
James Smart45ed1192009-10-02 15:17:02 -04004163 spin_lock_irqsave(&phba->hbalock, iflag);
4164 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
4165 spin_unlock_irqrestore(&phba->hbalock, iflag);
4166 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04004167 /* Get the response iocb from the head of work queue */
4168 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04004169 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04004170 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04004171 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04004172
4173 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
4174 case CQE_CODE_COMPL_WQE:
4175 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
4176 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04004177 /* Translate ELS WCQE to response IOCBQ */
4178 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
4179 irspiocbq);
4180 if (irspiocbq)
4181 lpfc_sli_sp_handle_rspiocb(phba, pring,
4182 irspiocbq);
James Smart0ef01a22018-09-10 10:30:45 -07004183 count++;
James Smart4d9ab992009-10-02 15:16:39 -04004184 break;
4185 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04004186 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04004187 dmabuf = container_of(cq_event, struct hbq_dmabuf,
4188 cq_event);
4189 lpfc_sli4_handle_received_buffer(phba, dmabuf);
James Smart0ef01a22018-09-10 10:30:45 -07004190 count++;
James Smart4d9ab992009-10-02 15:16:39 -04004191 break;
4192 default:
4193 break;
4194 }
James Smart0ef01a22018-09-10 10:30:45 -07004195
4196 /* Limit the number of events to 64 to avoid soft lockups */
4197 if (count == 64)
4198 break;
James Smart4f774512009-05-22 14:52:35 -04004199 }
4200}
4201
4202/**
James Smart3621a712009-04-06 18:47:14 -04004203 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04004204 * @phba: Pointer to HBA context object.
4205 * @pring: Pointer to driver SLI ring object.
4206 *
4207 * This function aborts all iocbs in the given ring and frees all the iocb
4208 * objects in txq. This function issues an abort iocb for all the iocb commands
4209 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4210 * the return of this function. The caller is not required to hold any locks.
4211 **/
James Smart2e0fef82007-06-17 19:56:36 -05004212void
dea31012005-04-17 16:05:31 -05004213lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
4214{
James Smart2534ba72007-04-25 09:52:20 -04004215 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004216 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05004217
James Smart92d7f7b2007-06-17 19:56:38 -05004218 if (pring->ringno == LPFC_ELS_RING) {
4219 lpfc_fabric_abort_hba(phba);
4220 }
4221
dea31012005-04-17 16:05:31 -05004222 /* Error everything on txq and txcmplq
4223 * First do the txq.
4224 */
James Smartdb55fba2014-04-04 13:52:02 -04004225 if (phba->sli_rev >= LPFC_SLI_REV4) {
4226 spin_lock_irq(&pring->ring_lock);
4227 list_splice_init(&pring->txq, &completions);
4228 pring->txq_cnt = 0;
4229 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05004230
James Smartdb55fba2014-04-04 13:52:02 -04004231 spin_lock_irq(&phba->hbalock);
4232 /* Next issue ABTS for everything on the txcmplq */
4233 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
James Smartdb7531d2020-11-15 11:26:44 -08004234 lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
James Smartdb55fba2014-04-04 13:52:02 -04004235 spin_unlock_irq(&phba->hbalock);
4236 } else {
4237 spin_lock_irq(&phba->hbalock);
4238 list_splice_init(&pring->txq, &completions);
4239 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04004240
James Smartdb55fba2014-04-04 13:52:02 -04004241 /* Next issue ABTS for everything on the txcmplq */
4242 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
James Smartdb7531d2020-11-15 11:26:44 -08004243 lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
James Smartdb55fba2014-04-04 13:52:02 -04004244 spin_unlock_irq(&phba->hbalock);
4245 }
James Smarta22d73b2021-01-04 10:02:38 -08004246 /* Make sure HBA is alive */
4247 lpfc_issue_hb_tmo(phba);
James Smart2534ba72007-04-25 09:52:20 -04004248
James Smarta257bf92009-04-06 18:48:10 -04004249 /* Cancel all the IOCBs from the completions list */
4250 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4251 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05004252}
4253
James Smarte59058c2008-08-24 21:49:00 -04004254/**
James Smartdb55fba2014-04-04 13:52:02 -04004255 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
4256 * @phba: Pointer to HBA context object.
James Smartdb55fba2014-04-04 13:52:02 -04004257 *
4258 * This function aborts all iocbs in FCP rings and frees all the iocb
4259 * objects in txq. This function issues an abort iocb for all the iocb commands
4260 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
4261 * the return of this function. The caller is not required to hold any locks.
4262 **/
4263void
4264lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
4265{
4266 struct lpfc_sli *psli = &phba->sli;
4267 struct lpfc_sli_ring *pring;
4268 uint32_t i;
4269
4270 /* Look on all the FCP Rings for the iotag */
4271 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004272 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07004273 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smartdb55fba2014-04-04 13:52:02 -04004274 lpfc_sli_abort_iocb_ring(phba, pring);
4275 }
4276 } else {
James Smart895427b2017-02-12 13:52:30 -08004277 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004278 lpfc_sli_abort_iocb_ring(phba, pring);
4279 }
4280}
4281
James Smart895427b2017-02-12 13:52:30 -08004282/**
James Smartc00f62e2019-08-14 16:57:11 -07004283 * lpfc_sli_flush_io_rings - flush all iocbs in the IO ring
James Smarta8e497d2008-08-24 21:50:11 -04004284 * @phba: Pointer to HBA context object.
4285 *
James Smartc00f62e2019-08-14 16:57:11 -07004286 * This function flushes all iocbs in the IO ring and frees all the iocb
James Smarta8e497d2008-08-24 21:50:11 -04004287 * objects in txq and txcmplq. This function will not issue abort iocbs
4288 * for all the iocb commands in txcmplq, they will just be returned with
4289 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
4290 * slot has been permanently disabled.
4291 **/
4292void
James Smartc00f62e2019-08-14 16:57:11 -07004293lpfc_sli_flush_io_rings(struct lpfc_hba *phba)
James Smarta8e497d2008-08-24 21:50:11 -04004294{
4295 LIST_HEAD(txq);
4296 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04004297 struct lpfc_sli *psli = &phba->sli;
4298 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04004299 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08004300 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04004301
4302 spin_lock_irq(&phba->hbalock);
James Smart4cd70892020-03-22 11:12:57 -07004303 if (phba->hba_flag & HBA_IOQ_FLUSH ||
4304 !phba->sli4_hba.hdwq) {
4305 spin_unlock_irq(&phba->hbalock);
4306 return;
4307 }
James Smart4f2e66c2012-05-09 21:17:07 -04004308 /* Indicate the I/O queues are flushed */
James Smartc00f62e2019-08-14 16:57:11 -07004309 phba->hba_flag |= HBA_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04004310 spin_unlock_irq(&phba->hbalock);
4311
James Smartdb55fba2014-04-04 13:52:02 -04004312 /* Look on all the FCP Rings for the iotag */
4313 if (phba->sli_rev >= LPFC_SLI_REV4) {
James Smartcdb42be2019-01-28 11:14:21 -08004314 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -07004315 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smarta8e497d2008-08-24 21:50:11 -04004316
James Smartdb55fba2014-04-04 13:52:02 -04004317 spin_lock_irq(&pring->ring_lock);
4318 /* Retrieve everything on txq */
4319 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004320 list_for_each_entry_safe(piocb, next_iocb,
4321 &pring->txcmplq, list)
4322 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004323 /* Retrieve everything on the txcmplq */
4324 list_splice_init(&pring->txcmplq, &txcmplq);
4325 pring->txq_cnt = 0;
4326 pring->txcmplq_cnt = 0;
4327 spin_unlock_irq(&pring->ring_lock);
4328
4329 /* Flush the txq */
4330 lpfc_sli_cancel_iocbs(phba, &txq,
4331 IOSTAT_LOCAL_REJECT,
4332 IOERR_SLI_DOWN);
4333 /* Flush the txcmpq */
4334 lpfc_sli_cancel_iocbs(phba, &txcmplq,
4335 IOSTAT_LOCAL_REJECT,
4336 IOERR_SLI_DOWN);
4337 }
4338 } else {
James Smart895427b2017-02-12 13:52:30 -08004339 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04004340
4341 spin_lock_irq(&phba->hbalock);
4342 /* Retrieve everything on txq */
4343 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08004344 list_for_each_entry_safe(piocb, next_iocb,
4345 &pring->txcmplq, list)
4346 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04004347 /* Retrieve everything on the txcmplq */
4348 list_splice_init(&pring->txcmplq, &txcmplq);
4349 pring->txq_cnt = 0;
4350 pring->txcmplq_cnt = 0;
4351 spin_unlock_irq(&phba->hbalock);
4352
4353 /* Flush the txq */
4354 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
4355 IOERR_SLI_DOWN);
4356 /* Flush the txcmpq */
4357 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
4358 IOERR_SLI_DOWN);
4359 }
James Smarta8e497d2008-08-24 21:50:11 -04004360}
4361
4362/**
James Smart3772a992009-05-22 14:50:54 -04004363 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004364 * @phba: Pointer to HBA context object.
4365 * @mask: Bit mask to be checked.
4366 *
4367 * This function reads the host status register and compares
4368 * with the provided bit mask to check if HBA completed
4369 * the restart. This function will wait in a loop for the
4370 * HBA to complete restart. If the HBA does not restart within
4371 * 15 iterations, the function will reset the HBA again. The
4372 * function returns 1 when HBA fail to restart otherwise returns
4373 * zero.
4374 **/
James Smart3772a992009-05-22 14:50:54 -04004375static int
4376lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004377{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004378 uint32_t status;
4379 int i = 0;
4380 int retval = 0;
dea31012005-04-17 16:05:31 -05004381
Jamie Wellnitz41415862006-02-28 19:25:27 -05004382 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004383 if (lpfc_readl(phba->HSregaddr, &status))
4384 return 1;
dea31012005-04-17 16:05:31 -05004385
James Smartd2f25472021-01-04 10:02:27 -08004386 phba->hba_flag |= HBA_NEEDS_CFG_PORT;
4387
Jamie Wellnitz41415862006-02-28 19:25:27 -05004388 /*
4389 * Check status register every 100ms for 5 retries, then every
4390 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4391 * every 2.5 sec for 4.
4392 * Break our of the loop if errors occurred during init.
4393 */
4394 while (((status & mask) != mask) &&
4395 !(status & HS_FFERM) &&
4396 i++ < 20) {
dea31012005-04-17 16:05:31 -05004397
Jamie Wellnitz41415862006-02-28 19:25:27 -05004398 if (i <= 5)
4399 msleep(10);
4400 else if (i <= 10)
4401 msleep(500);
4402 else
4403 msleep(2500);
dea31012005-04-17 16:05:31 -05004404
Jamie Wellnitz41415862006-02-28 19:25:27 -05004405 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004406 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004407 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004408 lpfc_sli_brdrestart(phba);
4409 }
4410 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004411 if (lpfc_readl(phba->HSregaddr, &status)) {
4412 retval = 1;
4413 break;
4414 }
dea31012005-04-17 16:05:31 -05004415 }
dea31012005-04-17 16:05:31 -05004416
Jamie Wellnitz41415862006-02-28 19:25:27 -05004417 /* Check to see if any errors occurred during init */
4418 if ((status & HS_FFERM) || (i >= 20)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07004419 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte40a02c2010-02-26 14:13:54 -05004420 "2751 Adapter failed to restart, "
4421 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4422 status,
4423 readl(phba->MBslimaddr + 0xa8),
4424 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004425 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004426 retval = 1;
4427 }
dea31012005-04-17 16:05:31 -05004428
Jamie Wellnitz41415862006-02-28 19:25:27 -05004429 return retval;
dea31012005-04-17 16:05:31 -05004430}
4431
James Smartda0436e2009-05-22 14:51:39 -04004432/**
4433 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4434 * @phba: Pointer to HBA context object.
4435 * @mask: Bit mask to be checked.
4436 *
4437 * This function checks the host status register to check if HBA is
4438 * ready. This function will wait in a loop for the HBA to be ready
4439 * If the HBA is not ready , the function will will reset the HBA PCI
4440 * function again. The function returns 1 when HBA fail to be ready
4441 * otherwise returns zero.
4442 **/
4443static int
4444lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4445{
4446 uint32_t status;
4447 int retval = 0;
4448
4449 /* Read the HBA Host Status Register */
4450 status = lpfc_sli4_post_status_check(phba);
4451
4452 if (status) {
4453 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4454 lpfc_sli_brdrestart(phba);
4455 status = lpfc_sli4_post_status_check(phba);
4456 }
4457
4458 /* Check to see if any errors occurred during init */
4459 if (status) {
4460 phba->link_state = LPFC_HBA_ERROR;
4461 retval = 1;
4462 } else
4463 phba->sli4_hba.intr_enable = 0;
4464
4465 return retval;
4466}
4467
4468/**
4469 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4470 * @phba: Pointer to HBA context object.
4471 * @mask: Bit mask to be checked.
4472 *
4473 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4474 * from the API jump table function pointer from the lpfc_hba struct.
4475 **/
4476int
4477lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4478{
4479 return phba->lpfc_sli_brdready(phba, mask);
4480}
4481
James Smart92908312006-03-07 15:04:13 -05004482#define BARRIER_TEST_PATTERN (0xdeadbeef)
4483
James Smarte59058c2008-08-24 21:49:00 -04004484/**
James Smart3621a712009-04-06 18:47:14 -04004485 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004486 * @phba: Pointer to HBA context object.
4487 *
James Smart1b511972011-12-13 13:23:09 -05004488 * This function is called before resetting an HBA. This function is called
4489 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004490 **/
James Smart2e0fef82007-06-17 19:56:36 -05004491void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004492{
James Smart65a29c12006-07-06 15:50:50 -04004493 uint32_t __iomem *resp_buf;
4494 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004495 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004496 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004497 int i;
4498 uint8_t hdrtype;
4499
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004500 lockdep_assert_held(&phba->hbalock);
4501
James Smart92908312006-03-07 15:04:13 -05004502 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4503 if (hdrtype != 0x80 ||
4504 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4505 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4506 return;
4507
4508 /*
4509 * Tell the other part of the chip to suspend temporarily all
4510 * its DMA activity.
4511 */
James Smart65a29c12006-07-06 15:50:50 -04004512 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004513
4514 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004515 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4516 return;
James Smart92908312006-03-07 15:04:13 -05004517 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4518 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004519 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004520
James Smart9940b972011-03-11 16:06:12 -05004521 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4522 return;
4523 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004524 /* Clear Chip error bit */
4525 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004526 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004527 }
4528
4529 mbox = 0;
4530 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4531 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4532
4533 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004534 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004535 writel(mbox, mbox_buf);
4536
James Smart9940b972011-03-11 16:06:12 -05004537 for (i = 0; i < 50; i++) {
4538 if (lpfc_readl((resp_buf + 1), &resp_data))
4539 return;
4540 if (resp_data != ~(BARRIER_TEST_PATTERN))
4541 mdelay(1);
4542 else
4543 break;
4544 }
4545 resp_data = 0;
4546 if (lpfc_readl((resp_buf + 1), &resp_data))
4547 return;
4548 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004549 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004550 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004551 goto restore_hc;
4552 else
4553 goto clear_errat;
4554 }
4555
4556 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004557 resp_data = 0;
4558 for (i = 0; i < 500; i++) {
4559 if (lpfc_readl(resp_buf, &resp_data))
4560 return;
4561 if (resp_data != mbox)
4562 mdelay(1);
4563 else
4564 break;
4565 }
James Smart92908312006-03-07 15:04:13 -05004566
4567clear_errat:
4568
James Smart9940b972011-03-11 16:06:12 -05004569 while (++i < 500) {
4570 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4571 return;
4572 if (!(ha_copy & HA_ERATT))
4573 mdelay(1);
4574 else
4575 break;
4576 }
James Smart92908312006-03-07 15:04:13 -05004577
4578 if (readl(phba->HAregaddr) & HA_ERATT) {
4579 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004580 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004581 }
4582
4583restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004584 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004585 writel(hc_copy, phba->HCregaddr);
4586 readl(phba->HCregaddr); /* flush */
4587}
4588
James Smarte59058c2008-08-24 21:49:00 -04004589/**
James Smart3621a712009-04-06 18:47:14 -04004590 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004591 * @phba: Pointer to HBA context object.
4592 *
4593 * This function issues a kill_board mailbox command and waits for
4594 * the error attention interrupt. This function is called for stopping
4595 * the firmware processing. The caller is not required to hold any
4596 * locks. This function calls lpfc_hba_down_post function to free
4597 * any pending commands after the kill. The function will return 1 when it
4598 * fails to kill the board else will return 0.
4599 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004600int
James Smart2e0fef82007-06-17 19:56:36 -05004601lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004602{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004603 struct lpfc_sli *psli;
4604 LPFC_MBOXQ_t *pmb;
4605 uint32_t status;
4606 uint32_t ha_copy;
4607 int retval;
4608 int i = 0;
4609
4610 psli = &phba->sli;
4611
4612 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004613 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004614 "0329 Kill HBA Data: x%x x%x\n",
4615 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004616
James Smart98c9ea52007-10-27 13:37:33 -04004617 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4618 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004619 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004620
4621 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004622 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004623 if (lpfc_readl(phba->HCregaddr, &status)) {
4624 spin_unlock_irq(&phba->hbalock);
4625 mempool_free(pmb, phba->mbox_mem_pool);
4626 return 1;
4627 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004628 status &= ~HC_ERINT_ENA;
4629 writel(status, phba->HCregaddr);
4630 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004631 phba->link_flag |= LS_IGNORE_ERATT;
4632 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004633
4634 lpfc_kill_board(phba, pmb);
4635 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4636 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4637
4638 if (retval != MBX_SUCCESS) {
4639 if (retval != MBX_BUSY)
4640 mempool_free(pmb, phba->mbox_mem_pool);
Dick Kennedy372c1872020-06-30 14:50:00 -07004641 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte40a02c2010-02-26 14:13:54 -05004642 "2752 KILL_BOARD command failed retval %d\n",
4643 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004644 spin_lock_irq(&phba->hbalock);
4645 phba->link_flag &= ~LS_IGNORE_ERATT;
4646 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004647 return 1;
4648 }
4649
James Smartf4b4c682009-05-22 14:53:12 -04004650 spin_lock_irq(&phba->hbalock);
4651 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4652 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004653
Jamie Wellnitz41415862006-02-28 19:25:27 -05004654 mempool_free(pmb, phba->mbox_mem_pool);
4655
4656 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4657 * attention every 100ms for 3 seconds. If we don't get ERATT after
4658 * 3 seconds we still set HBA_ERROR state because the status of the
4659 * board is now undefined.
4660 */
James Smart9940b972011-03-11 16:06:12 -05004661 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4662 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004663 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4664 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004665 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4666 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004667 }
4668
4669 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004670 if (ha_copy & HA_ERATT) {
4671 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004672 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004673 }
James Smart2e0fef82007-06-17 19:56:36 -05004674 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004675 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004676 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004677 phba->link_flag &= ~LS_IGNORE_ERATT;
4678 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004679
Jamie Wellnitz41415862006-02-28 19:25:27 -05004680 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004681 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004682
James Smart2e0fef82007-06-17 19:56:36 -05004683 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004684}
4685
James Smarte59058c2008-08-24 21:49:00 -04004686/**
James Smart3772a992009-05-22 14:50:54 -04004687 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004688 * @phba: Pointer to HBA context object.
4689 *
4690 * This function resets the HBA by writing HC_INITFF to the control
4691 * register. After the HBA resets, this function resets all the iocb ring
4692 * indices. This function disables PCI layer parity checking during
4693 * the reset.
4694 * This function returns 0 always.
4695 * The caller is not required to hold any locks.
4696 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004697int
James Smart2e0fef82007-06-17 19:56:36 -05004698lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004699{
4700 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004701 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004702 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004703 int i;
dea31012005-04-17 16:05:31 -05004704
Jamie Wellnitz41415862006-02-28 19:25:27 -05004705 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004706
Jamie Wellnitz41415862006-02-28 19:25:27 -05004707 /* Reset HBA */
4708 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004709 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004710 (phba->pport) ? phba->pport->port_state : 0,
4711 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004712
4713 /* perform board reset */
4714 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004715 phba->link_events = 0;
James Smartd2f25472021-01-04 10:02:27 -08004716 phba->hba_flag |= HBA_NEEDS_CFG_PORT;
James Smart4492b732017-04-27 15:08:26 -07004717 if (phba->pport) {
4718 phba->pport->fc_myDID = 0;
4719 phba->pport->fc_prevDID = 0;
4720 }
dea31012005-04-17 16:05:31 -05004721
Jamie Wellnitz41415862006-02-28 19:25:27 -05004722 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004723 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value))
4724 return -EIO;
4725
Jamie Wellnitz41415862006-02-28 19:25:27 -05004726 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4727 (cfg_value &
4728 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4729
James Smart3772a992009-05-22 14:50:54 -04004730 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4731
Jamie Wellnitz41415862006-02-28 19:25:27 -05004732 /* Now toggle INITFF bit in the Host Control Register */
4733 writel(HC_INITFF, phba->HCregaddr);
4734 mdelay(1);
4735 readl(phba->HCregaddr); /* flush */
4736 writel(0, phba->HCregaddr);
4737 readl(phba->HCregaddr); /* flush */
4738
4739 /* Restore PCI cmd register */
4740 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004741
4742 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004743 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004744 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004745 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004746 pring->sli.sli3.rspidx = 0;
4747 pring->sli.sli3.next_cmdidx = 0;
4748 pring->sli.sli3.local_getidx = 0;
4749 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004750 pring->missbufcnt = 0;
4751 }
dea31012005-04-17 16:05:31 -05004752
James Smart2e0fef82007-06-17 19:56:36 -05004753 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004754 return 0;
4755}
4756
James Smarte59058c2008-08-24 21:49:00 -04004757/**
James Smartda0436e2009-05-22 14:51:39 -04004758 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4759 * @phba: Pointer to HBA context object.
4760 *
4761 * This function resets a SLI4 HBA. This function disables PCI layer parity
4762 * checking during resets the device. The caller is not required to hold
4763 * any locks.
4764 *
James Smart8c24a4f2019-08-14 16:56:53 -07004765 * This function returns 0 on success else returns negative error code.
James Smartda0436e2009-05-22 14:51:39 -04004766 **/
4767int
4768lpfc_sli4_brdreset(struct lpfc_hba *phba)
4769{
4770 struct lpfc_sli *psli = &phba->sli;
4771 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004772 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004773
4774 /* Reset HBA */
4775 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004776 "0295 Reset HBA Data: x%x x%x x%x\n",
4777 phba->pport->port_state, psli->sli_flag,
4778 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004779
4780 /* perform board reset */
4781 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004782 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004783 phba->pport->fc_myDID = 0;
4784 phba->pport->fc_prevDID = 0;
4785
James Smartda0436e2009-05-22 14:51:39 -04004786 spin_lock_irq(&phba->hbalock);
4787 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4788 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004789 spin_unlock_irq(&phba->hbalock);
4790
James Smart02936352014-04-04 13:52:12 -04004791 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4792 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4793 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4794 return rc;
4795 }
4796
James Smartda0436e2009-05-22 14:51:39 -04004797 /* Now physically reset the device */
4798 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4799 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004800
4801 /* Turn off parity checking and serr during the physical reset */
James Smart32a93102019-03-12 16:30:13 -07004802 if (pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value)) {
4803 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4804 "3205 PCI read Config failed\n");
4805 return -EIO;
4806 }
4807
James Smartbe858b62010-12-15 17:57:20 -05004808 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4809 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4810
James Smart88318812012-09-29 11:29:29 -04004811 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004812 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004813
James Smartbe858b62010-12-15 17:57:20 -05004814 /* Restore PCI cmd register */
4815 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4816
James Smart27b01b82012-05-09 21:19:44 -04004817 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004818}
4819
4820/**
4821 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004822 * @phba: Pointer to HBA context object.
4823 *
4824 * This function is called in the SLI initialization code path to
4825 * restart the HBA. The caller is not required to hold any lock.
4826 * This function writes MBX_RESTART mailbox command to the SLIM and
4827 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4828 * function to free any pending commands. The function enables
4829 * POST only during the first initialization. The function returns zero.
4830 * The function does not guarantee completion of MBX_RESTART mailbox
4831 * command before the return of this function.
4832 **/
James Smartda0436e2009-05-22 14:51:39 -04004833static int
4834lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004835{
4836 MAILBOX_t *mb;
4837 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004838 volatile uint32_t word0;
4839 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004840 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004841
James Smart2e0fef82007-06-17 19:56:36 -05004842 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004843
James Smart0d878412009-10-02 15:16:56 -04004844 /* Take PCIe device Advanced Error Reporting (AER) state */
4845 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4846
Jamie Wellnitz41415862006-02-28 19:25:27 -05004847 psli = &phba->sli;
4848
4849 /* Restart HBA */
4850 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004851 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004852 (phba->pport) ? phba->pport->port_state : 0,
4853 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004854
4855 word0 = 0;
4856 mb = (MAILBOX_t *) &word0;
4857 mb->mbxCommand = MBX_RESTART;
4858 mb->mbxHc = 1;
4859
James Smart92908312006-03-07 15:04:13 -05004860 lpfc_reset_barrier(phba);
4861
Jamie Wellnitz41415862006-02-28 19:25:27 -05004862 to_slim = phba->MBslimaddr;
4863 writel(*(uint32_t *) mb, to_slim);
4864 readl(to_slim); /* flush */
4865
4866 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004867 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004868 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004869 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004870 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004871 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004872 writel(*(uint32_t *) mb, to_slim);
4873 readl(to_slim); /* flush */
4874
4875 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004876 if (phba->pport)
4877 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004878 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004879 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004880 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004881
James Smart64ba8812006-08-02 15:24:34 -04004882 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004883 psli->stats_start = ktime_get_seconds();
James Smart64ba8812006-08-02 15:24:34 -04004884
James Smarteaf15d52008-12-04 22:39:29 -05004885 /* Give the INITFF and Post time to settle. */
4886 mdelay(100);
dea31012005-04-17 16:05:31 -05004887
James Smart0d878412009-10-02 15:16:56 -04004888 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4889 if (hba_aer_enabled)
4890 pci_disable_pcie_error_reporting(phba->pcidev);
4891
Jamie Wellnitz41415862006-02-28 19:25:27 -05004892 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004893
4894 return 0;
4895}
4896
James Smarte59058c2008-08-24 21:49:00 -04004897/**
James Smartda0436e2009-05-22 14:51:39 -04004898 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4899 * @phba: Pointer to HBA context object.
4900 *
4901 * This function is called in the SLI initialization code path to restart
4902 * a SLI4 HBA. The caller is not required to hold any lock.
4903 * At the end of the function, it calls lpfc_hba_down_post function to
4904 * free any pending commands.
4905 **/
4906static int
4907lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4908{
4909 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004910 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004911 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004912
4913 /* Restart HBA */
4914 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4915 "0296 Restart HBA Data: x%x x%x\n",
4916 phba->pport->port_state, psli->sli_flag);
4917
James Smart75baf692010-06-08 18:31:21 -04004918 /* Take PCIe device Advanced Error Reporting (AER) state */
4919 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4920
James Smart27b01b82012-05-09 21:19:44 -04004921 rc = lpfc_sli4_brdreset(phba);
James Smart4fb86a62019-09-03 14:54:41 -07004922 if (rc) {
4923 phba->link_state = LPFC_HBA_ERROR;
4924 goto hba_down_queue;
4925 }
James Smartda0436e2009-05-22 14:51:39 -04004926
4927 spin_lock_irq(&phba->hbalock);
4928 phba->pport->stopped = 0;
4929 phba->link_state = LPFC_INIT_START;
4930 phba->hba_flag = 0;
4931 spin_unlock_irq(&phba->hbalock);
4932
4933 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
Arnd Bergmannc4d6204d2018-06-18 17:28:23 +02004934 psli->stats_start = ktime_get_seconds();
James Smartda0436e2009-05-22 14:51:39 -04004935
James Smart75baf692010-06-08 18:31:21 -04004936 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4937 if (hba_aer_enabled)
4938 pci_disable_pcie_error_reporting(phba->pcidev);
4939
James Smart4fb86a62019-09-03 14:54:41 -07004940hba_down_queue:
James Smartda0436e2009-05-22 14:51:39 -04004941 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004942 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004943
James Smart27b01b82012-05-09 21:19:44 -04004944 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004945}
4946
4947/**
4948 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4949 * @phba: Pointer to HBA context object.
4950 *
4951 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4952 * API jump table function pointer from the lpfc_hba struct.
4953**/
4954int
4955lpfc_sli_brdrestart(struct lpfc_hba *phba)
4956{
4957 return phba->lpfc_sli_brdrestart(phba);
4958}
4959
4960/**
James Smart3621a712009-04-06 18:47:14 -04004961 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004962 * @phba: Pointer to HBA context object.
4963 *
4964 * This function is called after a HBA restart to wait for successful
4965 * restart of the HBA. Successful restart of the HBA is indicated by
4966 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4967 * iteration, the function will restart the HBA again. The function returns
4968 * zero if HBA successfully restarted else returns negative error code.
4969 **/
James Smart4492b732017-04-27 15:08:26 -07004970int
dea31012005-04-17 16:05:31 -05004971lpfc_sli_chipset_init(struct lpfc_hba *phba)
4972{
4973 uint32_t status, i = 0;
4974
4975 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004976 if (lpfc_readl(phba->HSregaddr, &status))
4977 return -EIO;
dea31012005-04-17 16:05:31 -05004978
4979 /* Check status register to see what current state is */
4980 i = 0;
4981 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4982
James Smartdcf2a4e2010-09-29 11:18:53 -04004983 /* Check every 10ms for 10 retries, then every 100ms for 90
4984 * retries, then every 1 sec for 50 retires for a total of
4985 * ~60 seconds before reset the board again and check every
4986 * 1 sec for 50 retries. The up to 60 seconds before the
4987 * board ready is required by the Falcon FIPS zeroization
4988 * complete, and any reset the board in between shall cause
4989 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004990 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004991 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004992 /* Adapter failed to init, timeout, status reg
4993 <status> */
Dick Kennedy372c1872020-06-30 14:50:00 -07004994 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04004995 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004996 "timeout, status reg x%x, "
4997 "FW Data: A8 x%x AC x%x\n", status,
4998 readl(phba->MBslimaddr + 0xa8),
4999 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05005000 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005001 return -ETIMEDOUT;
5002 }
5003
5004 /* Check to see if any errors occurred during init */
5005 if (status & HS_FFERM) {
5006 /* ERROR: During chipset initialization */
5007 /* Adapter failed to init, chipset, status reg
5008 <status> */
Dick Kennedy372c1872020-06-30 14:50:00 -07005009 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005010 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05005011 "chipset, status reg x%x, "
5012 "FW Data: A8 x%x AC x%x\n", status,
5013 readl(phba->MBslimaddr + 0xa8),
5014 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05005015 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005016 return -EIO;
5017 }
5018
James Smartdcf2a4e2010-09-29 11:18:53 -04005019 if (i <= 10)
dea31012005-04-17 16:05:31 -05005020 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04005021 else if (i <= 100)
5022 msleep(100);
5023 else
5024 msleep(1000);
dea31012005-04-17 16:05:31 -05005025
James Smartdcf2a4e2010-09-29 11:18:53 -04005026 if (i == 150) {
5027 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05005028 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005029 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05005030 }
5031 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05005032 if (lpfc_readl(phba->HSregaddr, &status))
5033 return -EIO;
dea31012005-04-17 16:05:31 -05005034 }
5035
5036 /* Check to see if any errors occurred during init */
5037 if (status & HS_FFERM) {
5038 /* ERROR: During chipset initialization */
5039 /* Adapter failed to init, chipset, status reg <status> */
Dick Kennedy372c1872020-06-30 14:50:00 -07005040 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005041 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05005042 "status reg x%x, "
5043 "FW Data: A8 x%x AC x%x\n", status,
5044 readl(phba->MBslimaddr + 0xa8),
5045 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05005046 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005047 return -EIO;
5048 }
5049
James Smartd2f25472021-01-04 10:02:27 -08005050 phba->hba_flag |= HBA_NEEDS_CFG_PORT;
5051
dea31012005-04-17 16:05:31 -05005052 /* Clear all interrupt enable conditions */
5053 writel(0, phba->HCregaddr);
5054 readl(phba->HCregaddr); /* flush */
5055
5056 /* setup host attn register */
5057 writel(0xffffffff, phba->HAregaddr);
5058 readl(phba->HAregaddr); /* flush */
5059 return 0;
5060}
5061
James Smarte59058c2008-08-24 21:49:00 -04005062/**
James Smart3621a712009-04-06 18:47:14 -04005063 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04005064 *
5065 * This function calculates and returns the number of HBQs required to be
5066 * configured.
5067 **/
James Smart78b2d852007-08-02 11:10:21 -04005068int
James Smarted957682007-06-17 19:56:37 -05005069lpfc_sli_hbq_count(void)
5070{
James Smart92d7f7b2007-06-17 19:56:38 -05005071 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05005072}
5073
James Smarte59058c2008-08-24 21:49:00 -04005074/**
James Smart3621a712009-04-06 18:47:14 -04005075 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04005076 *
5077 * This function adds the number of hbq entries in every HBQ to get
5078 * the total number of hbq entries required for the HBA and returns
5079 * the total count.
5080 **/
James Smarted957682007-06-17 19:56:37 -05005081static int
5082lpfc_sli_hbq_entry_count(void)
5083{
5084 int hbq_count = lpfc_sli_hbq_count();
5085 int count = 0;
5086 int i;
5087
5088 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05005089 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05005090 return count;
5091}
5092
James Smarte59058c2008-08-24 21:49:00 -04005093/**
James Smart3621a712009-04-06 18:47:14 -04005094 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04005095 *
5096 * This function calculates amount of memory required for all hbq entries
5097 * to be configured and returns the total memory required.
5098 **/
dea31012005-04-17 16:05:31 -05005099int
James Smarted957682007-06-17 19:56:37 -05005100lpfc_sli_hbq_size(void)
5101{
5102 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
5103}
5104
James Smarte59058c2008-08-24 21:49:00 -04005105/**
James Smart3621a712009-04-06 18:47:14 -04005106 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04005107 * @phba: Pointer to HBA context object.
5108 *
5109 * This function is called during the SLI initialization to configure
5110 * all the HBQs and post buffers to the HBQ. The caller is not
5111 * required to hold any locks. This function will return zero if successful
5112 * else it will return negative error code.
5113 **/
James Smarted957682007-06-17 19:56:37 -05005114static int
5115lpfc_sli_hbq_setup(struct lpfc_hba *phba)
5116{
5117 int hbq_count = lpfc_sli_hbq_count();
5118 LPFC_MBOXQ_t *pmb;
5119 MAILBOX_t *pmbox;
5120 uint32_t hbqno;
5121 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05005122
James Smart92d7f7b2007-06-17 19:56:38 -05005123 /* Get a Mailbox buffer to setup mailbox
5124 * commands for HBA initialization
5125 */
James Smarted957682007-06-17 19:56:37 -05005126 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5127
5128 if (!pmb)
5129 return -ENOMEM;
5130
James Smart04c68492009-05-22 14:52:52 -04005131 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05005132
5133 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
5134 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05005135 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05005136
5137 hbq_entry_index = 0;
5138 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
5139 phba->hbqs[hbqno].next_hbqPutIdx = 0;
5140 phba->hbqs[hbqno].hbqPutIdx = 0;
5141 phba->hbqs[hbqno].local_hbqGetIdx = 0;
5142 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05005143 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04005144 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
5145 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05005146 hbq_entry_index += phba->hbqs[hbqno].entry_count;
5147
5148 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
5149 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
5150 mbxStatus <status>, ring <num> */
5151
5152 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05005153 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005154 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05005155 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04005156 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05005157 pmbox->mbxStatus, hbqno);
5158
5159 phba->link_state = LPFC_HBA_ERROR;
5160 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04005161 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05005162 }
5163 }
5164 phba->hbq_count = hbq_count;
5165
James Smarted957682007-06-17 19:56:37 -05005166 mempool_free(pmb, phba->mbox_mem_pool);
5167
James Smart92d7f7b2007-06-17 19:56:38 -05005168 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04005169 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
5170 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05005171 return 0;
5172}
5173
James Smarte59058c2008-08-24 21:49:00 -04005174/**
James Smart4f774512009-05-22 14:52:35 -04005175 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
5176 * @phba: Pointer to HBA context object.
5177 *
5178 * This function is called during the SLI initialization to configure
5179 * all the HBQs and post buffers to the HBQ. The caller is not
5180 * required to hold any locks. This function will return zero if successful
5181 * else it will return negative error code.
5182 **/
5183static int
5184lpfc_sli4_rb_setup(struct lpfc_hba *phba)
5185{
5186 phba->hbq_in_use = 1;
James Smart999fbbc2019-12-18 15:58:06 -08005187 /**
5188 * Specific case when the MDS diagnostics is enabled and supported.
5189 * The receive buffer count is truncated to manage the incoming
5190 * traffic.
5191 **/
5192 if (phba->cfg_enable_mds_diags && phba->mds_diags_support)
5193 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5194 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count >> 1;
5195 else
5196 phba->hbqs[LPFC_ELS_HBQ].entry_count =
5197 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04005198 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08005199 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04005200 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04005201 return 0;
5202}
5203
5204/**
James Smart3621a712009-04-06 18:47:14 -04005205 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04005206 * @phba: Pointer to HBA context object.
5207 * @sli_mode: sli mode - 2/3
5208 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005209 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04005210 * to issue config_port mailbox command. This function restarts the
5211 * HBA firmware and issues a config_port mailbox command to configure
5212 * the SLI interface in the sli mode specified by sli_mode
5213 * variable. The caller is not required to hold any locks.
5214 * The function returns 0 if successful, else returns negative error
5215 * code.
5216 **/
James Smart93996272008-08-24 21:50:30 -04005217int
5218lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05005219{
5220 LPFC_MBOXQ_t *pmb;
5221 uint32_t resetcount = 0, rc = 0, done = 0;
5222
5223 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5224 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05005225 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005226 return -ENOMEM;
5227 }
5228
James Smarted957682007-06-17 19:56:37 -05005229 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05005230 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05005231 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04005232 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005233 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05005234 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05005235 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05005236 rc = lpfc_sli_chipset_init(phba);
5237 if (rc)
5238 break;
5239
James Smart2e0fef82007-06-17 19:56:36 -05005240 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04005241 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005242 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005243 resetcount++;
5244
James Smarted957682007-06-17 19:56:37 -05005245 /* Call pre CONFIG_PORT mailbox command initialization. A
5246 * value of 0 means the call was successful. Any other
5247 * nonzero value is a failure, but if ERESTART is returned,
5248 * the driver may reset the HBA and try again.
5249 */
dea31012005-04-17 16:05:31 -05005250 rc = lpfc_config_port_prep(phba);
5251 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05005252 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05005253 continue;
James Smart34b02dc2008-08-24 21:49:55 -04005254 } else if (rc)
dea31012005-04-17 16:05:31 -05005255 break;
James Smart6d368e52011-05-24 11:44:12 -04005256
James Smart2e0fef82007-06-17 19:56:36 -05005257 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05005258 lpfc_config_port(phba, pmb);
5259 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04005260 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
5261 LPFC_SLI3_HBQ_ENABLED |
5262 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04005263 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05005264 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005265 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005266 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05005267 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04005268 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05005269 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04005270 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05005271 spin_unlock_irq(&phba->hbalock);
5272 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04005273 } else {
5274 /* Allow asynchronous mailbox command to go through */
5275 spin_lock_irq(&phba->hbalock);
5276 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5277 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05005278 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05005279
5280 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
5281 (pmb->u.mb.un.varCfgPort.gasabt == 0))
5282 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
5283 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04005284 }
dea31012005-04-17 16:05:31 -05005285 }
James Smarted957682007-06-17 19:56:37 -05005286 if (!done) {
5287 rc = -EINVAL;
5288 goto do_prep_failed;
5289 }
James Smart04c68492009-05-22 14:52:52 -04005290 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
5291 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04005292 rc = -ENXIO;
5293 goto do_prep_failed;
5294 }
James Smart04c68492009-05-22 14:52:52 -04005295 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04005296 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005297 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
5298 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
5299 phba->max_vpi : phba->max_vports;
5300
James Smart34b02dc2008-08-24 21:49:55 -04005301 } else
5302 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04005303 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04005304 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04005305 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04005306 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04005307
5308 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
5309 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05005310
James Smartf44ac122018-03-05 12:04:08 -08005311 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
5312 if (pmb->u.mb.un.varCfgPort.gbg == 0) {
5313 phba->cfg_enable_bg = 0;
5314 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
Dick Kennedy372c1872020-06-30 14:50:00 -07005315 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte2a0a9d2008-12-04 22:40:02 -05005316 "0443 Adapter did not grant "
5317 "BlockGuard\n");
James Smartf44ac122018-03-05 12:04:08 -08005318 }
James Smarte2a0a9d2008-12-04 22:40:02 -05005319 }
James Smart34b02dc2008-08-24 21:49:55 -04005320 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05005321 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04005322 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04005323 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05005324 }
James Smart92d7f7b2007-06-17 19:56:38 -05005325do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05005326 mempool_free(pmb, phba->mbox_mem_pool);
5327 return rc;
5328}
5329
James Smarte59058c2008-08-24 21:49:00 -04005330
5331/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08005332 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04005333 * @phba: Pointer to HBA context object.
5334 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08005335 * This function is the main SLI initialization function. This function
5336 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04005337 * error attention handler code. Caller is not required to hold any
5338 * locks. This function issues config_port mailbox command to configure
5339 * the SLI, setup iocb rings and HBQ rings. In the end the function
5340 * calls the config_port_post function to issue init_link mailbox
5341 * command and to start the discovery. The function will return zero
5342 * if successful, else it will return negative error code.
5343 **/
James Smarted957682007-06-17 19:56:37 -05005344int
5345lpfc_sli_hba_setup(struct lpfc_hba *phba)
5346{
5347 uint32_t rc;
James Smartd2f25472021-01-04 10:02:27 -08005348 int i;
James Smart6d368e52011-05-24 11:44:12 -04005349 int longs;
James Smarted957682007-06-17 19:56:37 -05005350
James Smartd2f25472021-01-04 10:02:27 -08005351 /* Enable ISR already does config_port because of config_msi mbx */
5352 if (phba->hba_flag & HBA_NEEDS_CFG_PORT) {
5353 rc = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
5354 if (rc)
5355 return -EIO;
5356 phba->hba_flag &= ~HBA_NEEDS_CFG_PORT;
James Smarted957682007-06-17 19:56:37 -05005357 }
James Smartb5c53952016-03-31 14:12:30 -07005358 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005359
James Smart0d878412009-10-02 15:16:56 -04005360 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5361 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5362 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5363 if (!rc) {
5364 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5365 "2709 This device supports "
5366 "Advanced Error Reporting (AER)\n");
5367 spin_lock_irq(&phba->hbalock);
5368 phba->hba_flag |= HBA_AER_ENABLED;
5369 spin_unlock_irq(&phba->hbalock);
5370 } else {
5371 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5372 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005373 "Advanced Error Reporting (AER): %d\n",
5374 rc);
James Smart0d878412009-10-02 15:16:56 -04005375 phba->cfg_aer_support = 0;
5376 }
5377 }
5378
James Smarted957682007-06-17 19:56:37 -05005379 if (phba->sli_rev == 3) {
5380 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5381 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005382 } else {
5383 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5384 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005385 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005386 }
5387
5388 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005389 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5390 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005391 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005392
5393 if (rc)
5394 goto lpfc_sli_hba_setup_error;
5395
James Smart6d368e52011-05-24 11:44:12 -04005396 /* Initialize VPIs. */
5397 if (phba->sli_rev == LPFC_SLI_REV3) {
5398 /*
5399 * The VPI bitmask and physical ID array are allocated
5400 * and initialized once only - at driver load. A port
5401 * reset doesn't need to reinitialize this memory.
5402 */
5403 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5404 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07005405 phba->vpi_bmask = kcalloc(longs,
5406 sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04005407 GFP_KERNEL);
5408 if (!phba->vpi_bmask) {
5409 rc = -ENOMEM;
5410 goto lpfc_sli_hba_setup_error;
5411 }
5412
Kees Cook6396bb22018-06-12 14:03:40 -07005413 phba->vpi_ids = kcalloc(phba->max_vpi + 1,
5414 sizeof(uint16_t),
5415 GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -04005416 if (!phba->vpi_ids) {
5417 kfree(phba->vpi_bmask);
5418 rc = -ENOMEM;
5419 goto lpfc_sli_hba_setup_error;
5420 }
5421 for (i = 0; i < phba->max_vpi; i++)
5422 phba->vpi_ids[i] = i;
5423 }
5424 }
5425
James Smart93996272008-08-24 21:50:30 -04005426 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005427 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5428 rc = lpfc_sli_hbq_setup(phba);
5429 if (rc)
5430 goto lpfc_sli_hba_setup_error;
5431 }
James Smart04c68492009-05-22 14:52:52 -04005432 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005433 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005434 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005435
5436 rc = lpfc_config_port_post(phba);
5437 if (rc)
5438 goto lpfc_sli_hba_setup_error;
5439
James Smarted957682007-06-17 19:56:37 -05005440 return rc;
5441
James Smart92d7f7b2007-06-17 19:56:38 -05005442lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005443 phba->link_state = LPFC_HBA_ERROR;
Dick Kennedy372c1872020-06-30 14:50:00 -07005444 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04005445 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005446 return rc;
5447}
5448
James Smartda0436e2009-05-22 14:51:39 -04005449/**
5450 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5451 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01005452 *
James Smartda0436e2009-05-22 14:51:39 -04005453 * This function issue a dump mailbox command to read config region
5454 * 23 and parse the records in the region and populate driver
5455 * data structure.
5456 **/
5457static int
James Smartff78d8f2011-12-13 13:21:35 -05005458lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005459{
James Smartff78d8f2011-12-13 13:21:35 -05005460 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005461 struct lpfc_dmabuf *mp;
5462 struct lpfc_mqe *mqe;
5463 uint32_t data_length;
5464 int rc;
5465
5466 /* Program the default value of vlan_id and fc_map */
5467 phba->valid_vlan = 0;
5468 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5469 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5470 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5471
James Smartff78d8f2011-12-13 13:21:35 -05005472 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5473 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005474 return -ENOMEM;
5475
James Smartff78d8f2011-12-13 13:21:35 -05005476 mqe = &mboxq->u.mqe;
5477 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5478 rc = -ENOMEM;
5479 goto out_free_mboxq;
5480 }
5481
James Smart3e1f0712018-11-29 16:09:29 -08005482 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04005483 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5484
5485 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5486 "(%d):2571 Mailbox cmd x%x Status x%x "
5487 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5488 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5489 "CQ: x%x x%x x%x x%x\n",
5490 mboxq->vport ? mboxq->vport->vpi : 0,
5491 bf_get(lpfc_mqe_command, mqe),
5492 bf_get(lpfc_mqe_status, mqe),
5493 mqe->un.mb_words[0], mqe->un.mb_words[1],
5494 mqe->un.mb_words[2], mqe->un.mb_words[3],
5495 mqe->un.mb_words[4], mqe->un.mb_words[5],
5496 mqe->un.mb_words[6], mqe->un.mb_words[7],
5497 mqe->un.mb_words[8], mqe->un.mb_words[9],
5498 mqe->un.mb_words[10], mqe->un.mb_words[11],
5499 mqe->un.mb_words[12], mqe->un.mb_words[13],
5500 mqe->un.mb_words[14], mqe->un.mb_words[15],
5501 mqe->un.mb_words[16], mqe->un.mb_words[50],
5502 mboxq->mcqe.word0,
5503 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5504 mboxq->mcqe.trailer);
5505
5506 if (rc) {
5507 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5508 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005509 rc = -EIO;
5510 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005511 }
5512 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005513 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005514 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5515 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005516 rc = -EIO;
5517 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005518 }
James Smartda0436e2009-05-22 14:51:39 -04005519
5520 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5521 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5522 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005523 rc = 0;
5524
5525out_free_mboxq:
5526 mempool_free(mboxq, phba->mbox_mem_pool);
5527 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005528}
5529
5530/**
5531 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5532 * @phba: pointer to lpfc hba data structure.
5533 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5534 * @vpd: pointer to the memory to hold resulting port vpd data.
5535 * @vpd_size: On input, the number of bytes allocated to @vpd.
5536 * On output, the number of data bytes in @vpd.
5537 *
5538 * This routine executes a READ_REV SLI4 mailbox command. In
5539 * addition, this routine gets the port vpd data.
5540 *
5541 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005542 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005543 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005544 **/
5545static int
5546lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5547 uint8_t *vpd, uint32_t *vpd_size)
5548{
5549 int rc = 0;
5550 uint32_t dma_size;
5551 struct lpfc_dmabuf *dmabuf;
5552 struct lpfc_mqe *mqe;
5553
5554 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5555 if (!dmabuf)
5556 return -ENOMEM;
5557
5558 /*
5559 * Get a DMA buffer for the vpd data resulting from the READ_REV
5560 * mailbox command.
5561 */
5562 dma_size = *vpd_size;
Luis Chamberlain750afb02019-01-04 09:23:09 +01005563 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev, dma_size,
5564 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005565 if (!dmabuf->virt) {
5566 kfree(dmabuf);
5567 return -ENOMEM;
5568 }
James Smartda0436e2009-05-22 14:51:39 -04005569
5570 /*
5571 * The SLI4 implementation of READ_REV conflicts at word1,
5572 * bits 31:16 and SLI4 adds vpd functionality not present
5573 * in SLI3. This code corrects the conflicts.
5574 */
5575 lpfc_read_rev(phba, mboxq);
5576 mqe = &mboxq->u.mqe;
5577 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5578 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5579 mqe->un.read_rev.word1 &= 0x0000FFFF;
5580 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5581 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5582
5583 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5584 if (rc) {
5585 dma_free_coherent(&phba->pcidev->dev, dma_size,
5586 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005587 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005588 return -EIO;
5589 }
5590
James Smartda0436e2009-05-22 14:51:39 -04005591 /*
5592 * The available vpd length cannot be bigger than the
5593 * DMA buffer passed to the port. Catch the less than
5594 * case and update the caller's size.
5595 */
5596 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5597 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5598
James Smartd7c47992010-06-08 18:31:54 -04005599 memcpy(vpd, dmabuf->virt, *vpd_size);
5600
James Smartda0436e2009-05-22 14:51:39 -04005601 dma_free_coherent(&phba->pcidev->dev, dma_size,
5602 dmabuf->virt, dmabuf->phys);
5603 kfree(dmabuf);
5604 return 0;
5605}
5606
5607/**
James Smartb3b4f3e2019-03-12 16:30:23 -07005608 * lpfc_sli4_get_ctl_attr - Retrieve SLI4 device controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005609 * @phba: pointer to lpfc hba data structure.
5610 *
5611 * This routine retrieves SLI4 device physical port name this PCI function
5612 * is attached to.
5613 *
5614 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005615 * 0 - successful
James Smartb3b4f3e2019-03-12 16:30:23 -07005616 * otherwise - failed to retrieve controller attributes
James Smartcd1c8302011-10-10 21:33:25 -04005617 **/
5618static int
James Smartb3b4f3e2019-03-12 16:30:23 -07005619lpfc_sli4_get_ctl_attr(struct lpfc_hba *phba)
James Smartcd1c8302011-10-10 21:33:25 -04005620{
5621 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005622 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5623 struct lpfc_controller_attribute *cntl_attr;
James Smartcd1c8302011-10-10 21:33:25 -04005624 void *virtaddr = NULL;
5625 uint32_t alloclen, reqlen;
5626 uint32_t shdr_status, shdr_add_status;
5627 union lpfc_sli4_cfg_shdr *shdr;
James Smartcd1c8302011-10-10 21:33:25 -04005628 int rc;
5629
James Smartcd1c8302011-10-10 21:33:25 -04005630 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5631 if (!mboxq)
5632 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005633
James Smartb3b4f3e2019-03-12 16:30:23 -07005634 /* Send COMMON_GET_CNTL_ATTRIBUTES mbox cmd */
James Smartcd1c8302011-10-10 21:33:25 -04005635 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5636 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5637 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5638 LPFC_SLI4_MBX_NEMBED);
James Smartb3b4f3e2019-03-12 16:30:23 -07005639
James Smartcd1c8302011-10-10 21:33:25 -04005640 if (alloclen < reqlen) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005641 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcd1c8302011-10-10 21:33:25 -04005642 "3084 Allocated DMA memory size (%d) is "
5643 "less than the requested DMA memory size "
5644 "(%d)\n", alloclen, reqlen);
5645 rc = -ENOMEM;
5646 goto out_free_mboxq;
5647 }
5648 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5649 virtaddr = mboxq->sge_array->addr[0];
5650 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5651 shdr = &mbx_cntl_attr->cfg_shdr;
5652 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5653 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5654 if (shdr_status || shdr_add_status || rc) {
5655 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5656 "3085 Mailbox x%x (x%x/x%x) failed, "
5657 "rc:x%x, status:x%x, add_status:x%x\n",
5658 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5659 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5660 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5661 rc, shdr_status, shdr_add_status);
5662 rc = -ENXIO;
5663 goto out_free_mboxq;
5664 }
James Smartb3b4f3e2019-03-12 16:30:23 -07005665
James Smartcd1c8302011-10-10 21:33:25 -04005666 cntl_attr = &mbx_cntl_attr->cntl_attr;
5667 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5668 phba->sli4_hba.lnk_info.lnk_tp =
5669 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5670 phba->sli4_hba.lnk_info.lnk_no =
5671 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
James Smartb3b4f3e2019-03-12 16:30:23 -07005672
5673 memset(phba->BIOSVersion, 0, sizeof(phba->BIOSVersion));
5674 strlcat(phba->BIOSVersion, (char *)cntl_attr->bios_ver_str,
5675 sizeof(phba->BIOSVersion));
5676
James Smartcd1c8302011-10-10 21:33:25 -04005677 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartb3b4f3e2019-03-12 16:30:23 -07005678 "3086 lnk_type:%d, lnk_numb:%d, bios_ver:%s\n",
James Smartcd1c8302011-10-10 21:33:25 -04005679 phba->sli4_hba.lnk_info.lnk_tp,
James Smartb3b4f3e2019-03-12 16:30:23 -07005680 phba->sli4_hba.lnk_info.lnk_no,
5681 phba->BIOSVersion);
5682out_free_mboxq:
5683 if (rc != MBX_TIMEOUT) {
5684 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5685 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5686 else
5687 mempool_free(mboxq, phba->mbox_mem_pool);
5688 }
5689 return rc;
5690}
5691
5692/**
5693 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5694 * @phba: pointer to lpfc hba data structure.
5695 *
5696 * This routine retrieves SLI4 device physical port name this PCI function
5697 * is attached to.
5698 *
5699 * Return codes
5700 * 0 - successful
5701 * otherwise - failed to retrieve physical port name
5702 **/
5703static int
5704lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5705{
5706 LPFC_MBOXQ_t *mboxq;
5707 struct lpfc_mbx_get_port_name *get_port_name;
5708 uint32_t shdr_status, shdr_add_status;
5709 union lpfc_sli4_cfg_shdr *shdr;
5710 char cport_name = 0;
5711 int rc;
5712
5713 /* We assume nothing at this point */
5714 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5715 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5716
5717 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5718 if (!mboxq)
5719 return -ENOMEM;
5720 /* obtain link type and link number via READ_CONFIG */
5721 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5722 lpfc_sli4_read_config(phba);
5723 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5724 goto retrieve_ppname;
5725
5726 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5727 rc = lpfc_sli4_get_ctl_attr(phba);
5728 if (rc)
5729 goto out_free_mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005730
5731retrieve_ppname:
5732 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5733 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5734 sizeof(struct lpfc_mbx_get_port_name) -
5735 sizeof(struct lpfc_sli4_cfg_mhdr),
5736 LPFC_SLI4_MBX_EMBED);
5737 get_port_name = &mboxq->u.mqe.un.get_port_name;
5738 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5739 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5740 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5741 phba->sli4_hba.lnk_info.lnk_tp);
5742 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5743 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5744 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5745 if (shdr_status || shdr_add_status || rc) {
5746 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5747 "3087 Mailbox x%x (x%x/x%x) failed: "
5748 "rc:x%x, status:x%x, add_status:x%x\n",
5749 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5750 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5751 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5752 rc, shdr_status, shdr_add_status);
5753 rc = -ENXIO;
5754 goto out_free_mboxq;
5755 }
5756 switch (phba->sli4_hba.lnk_info.lnk_no) {
5757 case LPFC_LINK_NUMBER_0:
5758 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5759 &get_port_name->u.response);
5760 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5761 break;
5762 case LPFC_LINK_NUMBER_1:
5763 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5764 &get_port_name->u.response);
5765 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5766 break;
5767 case LPFC_LINK_NUMBER_2:
5768 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5769 &get_port_name->u.response);
5770 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5771 break;
5772 case LPFC_LINK_NUMBER_3:
5773 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5774 &get_port_name->u.response);
5775 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5776 break;
5777 default:
5778 break;
5779 }
5780
5781 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5782 phba->Port[0] = cport_name;
5783 phba->Port[1] = '\0';
5784 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5785 "3091 SLI get port name: %s\n", phba->Port);
5786 }
5787
5788out_free_mboxq:
5789 if (rc != MBX_TIMEOUT) {
5790 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5791 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5792 else
5793 mempool_free(mboxq, phba->mbox_mem_pool);
5794 }
5795 return rc;
5796}
5797
5798/**
James Smartda0436e2009-05-22 14:51:39 -04005799 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5800 * @phba: pointer to lpfc hba data structure.
5801 *
5802 * This routine is called to explicitly arm the SLI4 device's completion and
5803 * event queues
5804 **/
5805static void
5806lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5807{
James Smart895427b2017-02-12 13:52:30 -08005808 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005809 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartcdb42be2019-01-28 11:14:21 -08005810 struct lpfc_sli4_hdw_queue *qp;
James Smart657add42019-05-21 17:49:06 -07005811 struct lpfc_queue *eq;
James Smartda0436e2009-05-22 14:51:39 -04005812
James Smart32517fc2019-01-28 11:14:33 -08005813 sli4_hba->sli4_write_cq_db(phba, sli4_hba->mbx_cq, 0, LPFC_QUEUE_REARM);
5814 sli4_hba->sli4_write_cq_db(phba, sli4_hba->els_cq, 0, LPFC_QUEUE_REARM);
James Smartb71413d2018-02-22 08:18:40 -08005815 if (sli4_hba->nvmels_cq)
James Smart32517fc2019-01-28 11:14:33 -08005816 sli4_hba->sli4_write_cq_db(phba, sli4_hba->nvmels_cq, 0,
5817 LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005818
James Smartcdb42be2019-01-28 11:14:21 -08005819 if (sli4_hba->hdwq) {
James Smart657add42019-05-21 17:49:06 -07005820 /* Loop thru all Hardware Queues */
James Smartcdb42be2019-01-28 11:14:21 -08005821 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
James Smart657add42019-05-21 17:49:06 -07005822 qp = &sli4_hba->hdwq[qidx];
5823 /* ARM the corresponding CQ */
James Smart01f2ef62019-08-28 16:19:11 -07005824 sli4_hba->sli4_write_cq_db(phba, qp->io_cq, 0,
James Smartc00f62e2019-08-14 16:57:11 -07005825 LPFC_QUEUE_REARM);
James Smartcdb42be2019-01-28 11:14:21 -08005826 }
James Smart895427b2017-02-12 13:52:30 -08005827
James Smart657add42019-05-21 17:49:06 -07005828 /* Loop thru all IRQ vectors */
5829 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
5830 eq = sli4_hba->hba_eq_hdl[qidx].eq;
5831 /* ARM the corresponding EQ */
5832 sli4_hba->sli4_write_eq_db(phba, eq,
5833 0, LPFC_QUEUE_REARM);
5834 }
James Smartcdb42be2019-01-28 11:14:21 -08005835 }
James Smart1ba981f2014-02-20 09:56:45 -05005836
James Smart2d7dbc42017-02-12 13:52:35 -08005837 if (phba->nvmet_support) {
5838 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smart32517fc2019-01-28 11:14:33 -08005839 sli4_hba->sli4_write_cq_db(phba,
5840 sli4_hba->nvmet_cqset[qidx], 0,
James Smart2d7dbc42017-02-12 13:52:35 -08005841 LPFC_QUEUE_REARM);
5842 }
James Smart2e90f4b2011-12-13 13:22:37 -05005843 }
James Smartda0436e2009-05-22 14:51:39 -04005844}
5845
5846/**
James Smart6d368e52011-05-24 11:44:12 -04005847 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5848 * @phba: Pointer to HBA context object.
5849 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005850 * @extnt_count: buffer to hold port available extent count.
5851 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005852 *
James Smartb76f2dc2011-07-22 18:37:42 -04005853 * This function calls the port and retrievs the number of available
5854 * extents and their size for a particular extent type.
5855 *
5856 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005857 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005858int
James Smart6d368e52011-05-24 11:44:12 -04005859lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5860 uint16_t *extnt_count, uint16_t *extnt_size)
5861{
5862 int rc = 0;
5863 uint32_t length;
5864 uint32_t mbox_tmo;
5865 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5866 LPFC_MBOXQ_t *mbox;
5867
5868 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5869 if (!mbox)
5870 return -ENOMEM;
5871
5872 /* Find out how many extents are available for this resource type */
5873 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5874 sizeof(struct lpfc_sli4_cfg_mhdr));
5875 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5876 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5877 length, LPFC_SLI4_MBX_EMBED);
5878
5879 /* Send an extents count of 0 - the GET doesn't use it. */
5880 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5881 LPFC_SLI4_MBX_EMBED);
5882 if (unlikely(rc)) {
5883 rc = -EIO;
5884 goto err_exit;
5885 }
5886
5887 if (!phba->sli4_hba.intr_enable)
5888 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5889 else {
James Smarta183a152011-10-10 21:32:43 -04005890 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005891 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5892 }
5893 if (unlikely(rc)) {
5894 rc = -EIO;
5895 goto err_exit;
5896 }
5897
5898 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5899 if (bf_get(lpfc_mbox_hdr_status,
5900 &rsrc_info->header.cfg_shdr.response)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07005901 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6d368e52011-05-24 11:44:12 -04005902 "2930 Failed to get resource extents "
5903 "Status 0x%x Add'l Status 0x%x\n",
5904 bf_get(lpfc_mbox_hdr_status,
5905 &rsrc_info->header.cfg_shdr.response),
5906 bf_get(lpfc_mbox_hdr_add_status,
5907 &rsrc_info->header.cfg_shdr.response));
5908 rc = -EIO;
5909 goto err_exit;
5910 }
5911
5912 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5913 &rsrc_info->u.rsp);
5914 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5915 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005916
5917 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5918 "3162 Retrieved extents type-%d from port: count:%d, "
5919 "size:%d\n", type, *extnt_count, *extnt_size);
5920
5921err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005922 mempool_free(mbox, phba->mbox_mem_pool);
5923 return rc;
5924}
5925
5926/**
5927 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5928 * @phba: Pointer to HBA context object.
5929 * @type: The extent type to check.
5930 *
5931 * This function reads the current available extents from the port and checks
5932 * if the extent count or extent size has changed since the last access.
5933 * Callers use this routine post port reset to understand if there is a
5934 * extent reprovisioning requirement.
5935 *
5936 * Returns:
5937 * -Error: error indicates problem.
5938 * 1: Extent count or size has changed.
5939 * 0: No changes.
5940 **/
5941static int
5942lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5943{
5944 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5945 uint16_t size_diff, rsrc_ext_size;
5946 int rc = 0;
5947 struct lpfc_rsrc_blks *rsrc_entry;
5948 struct list_head *rsrc_blk_list = NULL;
5949
5950 size_diff = 0;
5951 curr_ext_cnt = 0;
5952 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5953 &rsrc_ext_cnt,
5954 &rsrc_ext_size);
5955 if (unlikely(rc))
5956 return -EIO;
5957
5958 switch (type) {
5959 case LPFC_RSC_TYPE_FCOE_RPI:
5960 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5961 break;
5962 case LPFC_RSC_TYPE_FCOE_VPI:
5963 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5964 break;
5965 case LPFC_RSC_TYPE_FCOE_XRI:
5966 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5967 break;
5968 case LPFC_RSC_TYPE_FCOE_VFI:
5969 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5970 break;
5971 default:
5972 break;
5973 }
5974
5975 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5976 curr_ext_cnt++;
5977 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5978 size_diff++;
5979 }
5980
5981 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5982 rc = 1;
5983
5984 return rc;
5985}
5986
5987/**
5988 * lpfc_sli4_cfg_post_extnts -
5989 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01005990 * @extnt_cnt: number of available extents.
5991 * @type: the extent type (rpi, xri, vfi, vpi).
5992 * @emb: buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5993 * @mbox: pointer to the caller's allocated mailbox structure.
James Smart6d368e52011-05-24 11:44:12 -04005994 *
5995 * This function executes the extents allocation request. It also
5996 * takes care of the amount of memory needed to allocate or get the
5997 * allocated extents. It is the caller's responsibility to evaluate
5998 * the response.
5999 *
6000 * Returns:
6001 * -Error: Error value describes the condition found.
6002 * 0: if successful
6003 **/
6004static int
James Smart8a9d2e82012-05-09 21:16:12 -04006005lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006006 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
6007{
6008 int rc = 0;
6009 uint32_t req_len;
6010 uint32_t emb_len;
6011 uint32_t alloc_len, mbox_tmo;
6012
6013 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04006014 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04006015
6016 /*
6017 * Calculate the size of an embedded mailbox. The uint32_t
6018 * accounts for extents-specific word.
6019 */
6020 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6021 sizeof(uint32_t);
6022
6023 /*
6024 * Presume the allocation and response will fit into an embedded
6025 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6026 */
6027 *emb = LPFC_SLI4_MBX_EMBED;
6028 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04006029 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04006030 sizeof(union lpfc_sli4_cfg_shdr) +
6031 sizeof(uint32_t);
6032 *emb = LPFC_SLI4_MBX_NEMBED;
6033 }
6034
6035 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6036 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
6037 req_len, *emb);
6038 if (alloc_len < req_len) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006039 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartb76f2dc2011-07-22 18:37:42 -04006040 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04006041 "less than the requested DMA memory "
6042 "size (x%x)\n", alloc_len, req_len);
6043 return -ENOMEM;
6044 }
James Smart8a9d2e82012-05-09 21:16:12 -04006045 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04006046 if (unlikely(rc))
6047 return -EIO;
6048
6049 if (!phba->sli4_hba.intr_enable)
6050 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6051 else {
James Smarta183a152011-10-10 21:32:43 -04006052 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006053 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6054 }
6055
6056 if (unlikely(rc))
6057 rc = -EIO;
6058 return rc;
6059}
6060
6061/**
6062 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
6063 * @phba: Pointer to HBA context object.
6064 * @type: The resource extent type to allocate.
6065 *
6066 * This function allocates the number of elements for the specified
6067 * resource type.
6068 **/
6069static int
6070lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
6071{
6072 bool emb = false;
6073 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
6074 uint16_t rsrc_id, rsrc_start, j, k;
6075 uint16_t *ids;
6076 int i, rc;
6077 unsigned long longs;
6078 unsigned long *bmask;
6079 struct lpfc_rsrc_blks *rsrc_blks;
6080 LPFC_MBOXQ_t *mbox;
6081 uint32_t length;
6082 struct lpfc_id_range *id_array = NULL;
6083 void *virtaddr = NULL;
6084 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6085 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6086 struct list_head *ext_blk_list;
6087
6088 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
6089 &rsrc_cnt,
6090 &rsrc_size);
6091 if (unlikely(rc))
6092 return -EIO;
6093
6094 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006095 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6d368e52011-05-24 11:44:12 -04006096 "3009 No available Resource Extents "
6097 "for resource type 0x%x: Count: 0x%x, "
6098 "Size 0x%x\n", type, rsrc_cnt,
6099 rsrc_size);
6100 return -ENOMEM;
6101 }
6102
James Smart8a9d2e82012-05-09 21:16:12 -04006103 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
6104 "2903 Post resource extents type-0x%x: "
6105 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04006106
6107 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6108 if (!mbox)
6109 return -ENOMEM;
6110
James Smart8a9d2e82012-05-09 21:16:12 -04006111 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006112 if (unlikely(rc)) {
6113 rc = -EIO;
6114 goto err_exit;
6115 }
6116
6117 /*
6118 * Figure out where the response is located. Then get local pointers
6119 * to the response data. The port does not guarantee to respond to
6120 * all extents counts request so update the local variable with the
6121 * allocated count from the port.
6122 */
6123 if (emb == LPFC_SLI4_MBX_EMBED) {
6124 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6125 id_array = &rsrc_ext->u.rsp.id[0];
6126 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6127 } else {
6128 virtaddr = mbox->sge_array->addr[0];
6129 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6130 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6131 id_array = &n_rsrc->id;
6132 }
6133
6134 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
6135 rsrc_id_cnt = rsrc_cnt * rsrc_size;
6136
6137 /*
6138 * Based on the resource size and count, correct the base and max
6139 * resource values.
6140 */
6141 length = sizeof(struct lpfc_rsrc_blks);
6142 switch (type) {
6143 case LPFC_RSC_TYPE_FCOE_RPI:
Kees Cook6396bb22018-06-12 14:03:40 -07006144 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006145 sizeof(unsigned long),
6146 GFP_KERNEL);
6147 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6148 rc = -ENOMEM;
6149 goto err_exit;
6150 }
Kees Cook6396bb22018-06-12 14:03:40 -07006151 phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006152 sizeof(uint16_t),
6153 GFP_KERNEL);
6154 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6155 kfree(phba->sli4_hba.rpi_bmask);
6156 rc = -ENOMEM;
6157 goto err_exit;
6158 }
6159
6160 /*
6161 * The next_rpi was initialized with the maximum available
6162 * count but the port may allocate a smaller number. Catch
6163 * that case and update the next_rpi.
6164 */
6165 phba->sli4_hba.next_rpi = rsrc_id_cnt;
6166
6167 /* Initialize local ptrs for common extent processing later. */
6168 bmask = phba->sli4_hba.rpi_bmask;
6169 ids = phba->sli4_hba.rpi_ids;
6170 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
6171 break;
6172 case LPFC_RSC_TYPE_FCOE_VPI:
Kees Cook6396bb22018-06-12 14:03:40 -07006173 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006174 GFP_KERNEL);
6175 if (unlikely(!phba->vpi_bmask)) {
6176 rc = -ENOMEM;
6177 goto err_exit;
6178 }
Kees Cook6396bb22018-06-12 14:03:40 -07006179 phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006180 GFP_KERNEL);
6181 if (unlikely(!phba->vpi_ids)) {
6182 kfree(phba->vpi_bmask);
6183 rc = -ENOMEM;
6184 goto err_exit;
6185 }
6186
6187 /* Initialize local ptrs for common extent processing later. */
6188 bmask = phba->vpi_bmask;
6189 ids = phba->vpi_ids;
6190 ext_blk_list = &phba->lpfc_vpi_blk_list;
6191 break;
6192 case LPFC_RSC_TYPE_FCOE_XRI:
Kees Cook6396bb22018-06-12 14:03:40 -07006193 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006194 sizeof(unsigned long),
6195 GFP_KERNEL);
6196 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6197 rc = -ENOMEM;
6198 goto err_exit;
6199 }
James Smart8a9d2e82012-05-09 21:16:12 -04006200 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006201 phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006202 sizeof(uint16_t),
6203 GFP_KERNEL);
6204 if (unlikely(!phba->sli4_hba.xri_ids)) {
6205 kfree(phba->sli4_hba.xri_bmask);
6206 rc = -ENOMEM;
6207 goto err_exit;
6208 }
6209
6210 /* Initialize local ptrs for common extent processing later. */
6211 bmask = phba->sli4_hba.xri_bmask;
6212 ids = phba->sli4_hba.xri_ids;
6213 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
6214 break;
6215 case LPFC_RSC_TYPE_FCOE_VFI:
Kees Cook6396bb22018-06-12 14:03:40 -07006216 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006217 sizeof(unsigned long),
6218 GFP_KERNEL);
6219 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6220 rc = -ENOMEM;
6221 goto err_exit;
6222 }
Kees Cook6396bb22018-06-12 14:03:40 -07006223 phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
James Smart6d368e52011-05-24 11:44:12 -04006224 sizeof(uint16_t),
6225 GFP_KERNEL);
6226 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6227 kfree(phba->sli4_hba.vfi_bmask);
6228 rc = -ENOMEM;
6229 goto err_exit;
6230 }
6231
6232 /* Initialize local ptrs for common extent processing later. */
6233 bmask = phba->sli4_hba.vfi_bmask;
6234 ids = phba->sli4_hba.vfi_ids;
6235 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
6236 break;
6237 default:
6238 /* Unsupported Opcode. Fail call. */
6239 id_array = NULL;
6240 bmask = NULL;
6241 ids = NULL;
6242 ext_blk_list = NULL;
6243 goto err_exit;
6244 }
6245
6246 /*
6247 * Complete initializing the extent configuration with the
6248 * allocated ids assigned to this function. The bitmask serves
6249 * as an index into the array and manages the available ids. The
6250 * array just stores the ids communicated to the port via the wqes.
6251 */
6252 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
6253 if ((i % 2) == 0)
6254 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
6255 &id_array[k]);
6256 else
6257 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
6258 &id_array[k]);
6259
6260 rsrc_blks = kzalloc(length, GFP_KERNEL);
6261 if (unlikely(!rsrc_blks)) {
6262 rc = -ENOMEM;
6263 kfree(bmask);
6264 kfree(ids);
6265 goto err_exit;
6266 }
6267 rsrc_blks->rsrc_start = rsrc_id;
6268 rsrc_blks->rsrc_size = rsrc_size;
6269 list_add_tail(&rsrc_blks->list, ext_blk_list);
6270 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08006271 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart5e5b5112019-01-28 11:14:22 -08006272 phba->sli4_hba.io_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08006273 lpfc_sli4_get_iocb_cnt(phba);
James Smart895427b2017-02-12 13:52:30 -08006274 }
James Smart6d368e52011-05-24 11:44:12 -04006275
6276 while (rsrc_id < (rsrc_start + rsrc_size)) {
6277 ids[j] = rsrc_id;
6278 rsrc_id++;
6279 j++;
6280 }
6281 /* Entire word processed. Get next word.*/
6282 if ((i % 2) == 1)
6283 k++;
6284 }
6285 err_exit:
6286 lpfc_sli4_mbox_cmd_free(phba, mbox);
6287 return rc;
6288}
6289
James Smart895427b2017-02-12 13:52:30 -08006290
6291
James Smart6d368e52011-05-24 11:44:12 -04006292/**
6293 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
6294 * @phba: Pointer to HBA context object.
6295 * @type: the extent's type.
6296 *
6297 * This function deallocates all extents of a particular resource type.
6298 * SLI4 does not allow for deallocating a particular extent range. It
6299 * is the caller's responsibility to release all kernel memory resources.
6300 **/
6301static int
6302lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
6303{
6304 int rc;
6305 uint32_t length, mbox_tmo = 0;
6306 LPFC_MBOXQ_t *mbox;
6307 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
6308 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
6309
6310 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6311 if (!mbox)
6312 return -ENOMEM;
6313
6314 /*
6315 * This function sends an embedded mailbox because it only sends the
6316 * the resource type. All extents of this type are released by the
6317 * port.
6318 */
6319 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
6320 sizeof(struct lpfc_sli4_cfg_mhdr));
6321 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6322 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
6323 length, LPFC_SLI4_MBX_EMBED);
6324
6325 /* Send an extents count of 0 - the dealloc doesn't use it. */
6326 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
6327 LPFC_SLI4_MBX_EMBED);
6328 if (unlikely(rc)) {
6329 rc = -EIO;
6330 goto out_free_mbox;
6331 }
6332 if (!phba->sli4_hba.intr_enable)
6333 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6334 else {
James Smarta183a152011-10-10 21:32:43 -04006335 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04006336 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6337 }
6338 if (unlikely(rc)) {
6339 rc = -EIO;
6340 goto out_free_mbox;
6341 }
6342
6343 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
6344 if (bf_get(lpfc_mbox_hdr_status,
6345 &dealloc_rsrc->header.cfg_shdr.response)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006346 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6d368e52011-05-24 11:44:12 -04006347 "2919 Failed to release resource extents "
6348 "for type %d - Status 0x%x Add'l Status 0x%x. "
6349 "Resource memory not released.\n",
6350 type,
6351 bf_get(lpfc_mbox_hdr_status,
6352 &dealloc_rsrc->header.cfg_shdr.response),
6353 bf_get(lpfc_mbox_hdr_add_status,
6354 &dealloc_rsrc->header.cfg_shdr.response));
6355 rc = -EIO;
6356 goto out_free_mbox;
6357 }
6358
6359 /* Release kernel memory resources for the specific type. */
6360 switch (type) {
6361 case LPFC_RSC_TYPE_FCOE_VPI:
6362 kfree(phba->vpi_bmask);
6363 kfree(phba->vpi_ids);
6364 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6365 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6366 &phba->lpfc_vpi_blk_list, list) {
6367 list_del_init(&rsrc_blk->list);
6368 kfree(rsrc_blk);
6369 }
James Smart16a3a202013-04-17 20:14:38 -04006370 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006371 break;
6372 case LPFC_RSC_TYPE_FCOE_XRI:
6373 kfree(phba->sli4_hba.xri_bmask);
6374 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006375 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6376 &phba->sli4_hba.lpfc_xri_blk_list, list) {
6377 list_del_init(&rsrc_blk->list);
6378 kfree(rsrc_blk);
6379 }
6380 break;
6381 case LPFC_RSC_TYPE_FCOE_VFI:
6382 kfree(phba->sli4_hba.vfi_bmask);
6383 kfree(phba->sli4_hba.vfi_ids);
6384 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6385 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6386 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6387 list_del_init(&rsrc_blk->list);
6388 kfree(rsrc_blk);
6389 }
6390 break;
6391 case LPFC_RSC_TYPE_FCOE_RPI:
6392 /* RPI bitmask and physical id array are cleaned up earlier. */
6393 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6394 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6395 list_del_init(&rsrc_blk->list);
6396 kfree(rsrc_blk);
6397 }
6398 break;
6399 default:
6400 break;
6401 }
6402
6403 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6404
6405 out_free_mbox:
6406 mempool_free(mbox, phba->mbox_mem_pool);
6407 return rc;
6408}
6409
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006410static void
James Smart7bdedb32016-07-06 12:36:00 -07006411lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6412 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006413{
James Smart65791f12016-07-06 12:35:56 -07006414 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006415
James Smart65791f12016-07-06 12:35:56 -07006416 len = sizeof(struct lpfc_mbx_set_feature) -
6417 sizeof(struct lpfc_sli4_cfg_mhdr);
6418 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6419 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6420 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006421
James Smart7bdedb32016-07-06 12:36:00 -07006422 switch (feature) {
6423 case LPFC_SET_UE_RECOVERY:
6424 bf_set(lpfc_mbx_set_feature_UER,
6425 &mbox->u.mqe.un.set_feature, 1);
6426 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6427 mbox->u.mqe.un.set_feature.param_len = 8;
6428 break;
6429 case LPFC_SET_MDS_DIAGS:
6430 bf_set(lpfc_mbx_set_feature_mds,
6431 &mbox->u.mqe.un.set_feature, 1);
6432 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006433 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006434 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6435 mbox->u.mqe.un.set_feature.param_len = 8;
6436 break;
James Smart171f6c42019-11-04 16:57:07 -08006437 case LPFC_SET_DUAL_DUMP:
6438 bf_set(lpfc_mbx_set_feature_dd,
6439 &mbox->u.mqe.un.set_feature, LPFC_ENABLE_DUAL_DUMP);
6440 bf_set(lpfc_mbx_set_feature_ddquery,
6441 &mbox->u.mqe.un.set_feature, 0);
6442 mbox->u.mqe.un.set_feature.feature = LPFC_SET_DUAL_DUMP;
6443 mbox->u.mqe.un.set_feature.param_len = 4;
6444 break;
James Smart65791f12016-07-06 12:35:56 -07006445 }
James Smart7bdedb32016-07-06 12:36:00 -07006446
6447 return;
James Smart65791f12016-07-06 12:35:56 -07006448}
6449
James Smart6d368e52011-05-24 11:44:12 -04006450/**
James Smart1165a5c2018-11-29 16:09:39 -08006451 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
6452 * @phba: Pointer to HBA context object.
6453 *
6454 * Disable FW logging into host memory on the adapter. To
6455 * be done before reading logs from the host memory.
6456 **/
6457void
6458lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
6459{
6460 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6461
James Smart95bfc6d2019-10-18 14:18:27 -07006462 spin_lock_irq(&phba->hbalock);
6463 ras_fwlog->state = INACTIVE;
6464 spin_unlock_irq(&phba->hbalock);
James Smart1165a5c2018-11-29 16:09:39 -08006465
6466 /* Disable FW logging to host memory */
6467 writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
6468 phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
James Smart95bfc6d2019-10-18 14:18:27 -07006469
6470 /* Wait 10ms for firmware to stop using DMA buffer */
6471 usleep_range(10 * 1000, 20 * 1000);
James Smart1165a5c2018-11-29 16:09:39 -08006472}
6473
6474/**
James Smartd2cc9bc2018-09-10 10:30:50 -07006475 * lpfc_sli4_ras_dma_free - Free memory allocated for FW logging.
6476 * @phba: Pointer to HBA context object.
6477 *
6478 * This function is called to free memory allocated for RAS FW logging
6479 * support in the driver.
6480 **/
6481void
6482lpfc_sli4_ras_dma_free(struct lpfc_hba *phba)
6483{
6484 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6485 struct lpfc_dmabuf *dmabuf, *next;
6486
6487 if (!list_empty(&ras_fwlog->fwlog_buff_list)) {
6488 list_for_each_entry_safe(dmabuf, next,
6489 &ras_fwlog->fwlog_buff_list,
6490 list) {
6491 list_del(&dmabuf->list);
6492 dma_free_coherent(&phba->pcidev->dev,
6493 LPFC_RAS_MAX_ENTRY_SIZE,
6494 dmabuf->virt, dmabuf->phys);
6495 kfree(dmabuf);
6496 }
6497 }
6498
6499 if (ras_fwlog->lwpd.virt) {
6500 dma_free_coherent(&phba->pcidev->dev,
6501 sizeof(uint32_t) * 2,
6502 ras_fwlog->lwpd.virt,
6503 ras_fwlog->lwpd.phys);
6504 ras_fwlog->lwpd.virt = NULL;
6505 }
6506
James Smart95bfc6d2019-10-18 14:18:27 -07006507 spin_lock_irq(&phba->hbalock);
6508 ras_fwlog->state = INACTIVE;
6509 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07006510}
6511
6512/**
6513 * lpfc_sli4_ras_dma_alloc: Allocate memory for FW support
6514 * @phba: Pointer to HBA context object.
6515 * @fwlog_buff_count: Count of buffers to be created.
6516 *
6517 * This routine DMA memory for Log Write Position Data[LPWD] and buffer
6518 * to update FW log is posted to the adapter.
6519 * Buffer count is calculated based on module param ras_fwlog_buffsize
6520 * Size of each buffer posted to FW is 64K.
6521 **/
6522
6523static int
6524lpfc_sli4_ras_dma_alloc(struct lpfc_hba *phba,
6525 uint32_t fwlog_buff_count)
6526{
6527 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6528 struct lpfc_dmabuf *dmabuf;
6529 int rc = 0, i = 0;
6530
6531 /* Initialize List */
6532 INIT_LIST_HEAD(&ras_fwlog->fwlog_buff_list);
6533
6534 /* Allocate memory for the LWPD */
6535 ras_fwlog->lwpd.virt = dma_alloc_coherent(&phba->pcidev->dev,
6536 sizeof(uint32_t) * 2,
6537 &ras_fwlog->lwpd.phys,
6538 GFP_KERNEL);
6539 if (!ras_fwlog->lwpd.virt) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006540 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006541 "6185 LWPD Memory Alloc Failed\n");
6542
6543 return -ENOMEM;
6544 }
6545
6546 ras_fwlog->fw_buffcount = fwlog_buff_count;
6547 for (i = 0; i < ras_fwlog->fw_buffcount; i++) {
6548 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
6549 GFP_KERNEL);
6550 if (!dmabuf) {
6551 rc = -ENOMEM;
6552 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6553 "6186 Memory Alloc failed FW logging");
6554 goto free_mem;
6555 }
6556
Luis Chamberlain750afb02019-01-04 09:23:09 +01006557 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
James Smartd2cc9bc2018-09-10 10:30:50 -07006558 LPFC_RAS_MAX_ENTRY_SIZE,
Luis Chamberlain750afb02019-01-04 09:23:09 +01006559 &dmabuf->phys, GFP_KERNEL);
James Smartd2cc9bc2018-09-10 10:30:50 -07006560 if (!dmabuf->virt) {
6561 kfree(dmabuf);
6562 rc = -ENOMEM;
6563 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6564 "6187 DMA Alloc Failed FW logging");
6565 goto free_mem;
6566 }
James Smartd2cc9bc2018-09-10 10:30:50 -07006567 dmabuf->buffer_tag = i;
6568 list_add_tail(&dmabuf->list, &ras_fwlog->fwlog_buff_list);
6569 }
6570
6571free_mem:
6572 if (rc)
6573 lpfc_sli4_ras_dma_free(phba);
6574
6575 return rc;
6576}
6577
6578/**
6579 * lpfc_sli4_ras_mbox_cmpl: Completion handler for RAS MBX command
6580 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +01006581 * @pmb: pointer to the driver internal queue element for mailbox command.
James Smartd2cc9bc2018-09-10 10:30:50 -07006582 *
6583 * Completion handler for driver's RAS MBX command to the device.
6584 **/
6585static void
6586lpfc_sli4_ras_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6587{
6588 MAILBOX_t *mb;
6589 union lpfc_sli4_cfg_shdr *shdr;
6590 uint32_t shdr_status, shdr_add_status;
6591 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6592
6593 mb = &pmb->u.mb;
6594
6595 shdr = (union lpfc_sli4_cfg_shdr *)
6596 &pmb->u.mqe.un.ras_fwlog.header.cfg_shdr;
6597 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6598 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6599
6600 if (mb->mbxStatus != MBX_SUCCESS || shdr_status) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006601 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006602 "6188 FW LOG mailbox "
6603 "completed with status x%x add_status x%x,"
6604 " mbx status x%x\n",
6605 shdr_status, shdr_add_status, mb->mbxStatus);
James Smartcb349902018-11-29 16:09:27 -08006606
6607 ras_fwlog->ras_hwsupport = false;
James Smartd2cc9bc2018-09-10 10:30:50 -07006608 goto disable_ras;
6609 }
6610
James Smart95bfc6d2019-10-18 14:18:27 -07006611 spin_lock_irq(&phba->hbalock);
6612 ras_fwlog->state = ACTIVE;
6613 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07006614 mempool_free(pmb, phba->mbox_mem_pool);
6615
6616 return;
6617
6618disable_ras:
6619 /* Free RAS DMA memory */
6620 lpfc_sli4_ras_dma_free(phba);
6621 mempool_free(pmb, phba->mbox_mem_pool);
6622}
6623
6624/**
6625 * lpfc_sli4_ras_fwlog_init: Initialize memory and post RAS MBX command
6626 * @phba: pointer to lpfc hba data structure.
6627 * @fwlog_level: Logging verbosity level.
6628 * @fwlog_enable: Enable/Disable logging.
6629 *
6630 * Initialize memory and post mailbox command to enable FW logging in host
6631 * memory.
6632 **/
6633int
6634lpfc_sli4_ras_fwlog_init(struct lpfc_hba *phba,
6635 uint32_t fwlog_level,
6636 uint32_t fwlog_enable)
6637{
6638 struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
6639 struct lpfc_mbx_set_ras_fwlog *mbx_fwlog = NULL;
6640 struct lpfc_dmabuf *dmabuf;
6641 LPFC_MBOXQ_t *mbox;
6642 uint32_t len = 0, fwlog_buffsize, fwlog_entry_count;
6643 int rc = 0;
6644
James Smart95bfc6d2019-10-18 14:18:27 -07006645 spin_lock_irq(&phba->hbalock);
6646 ras_fwlog->state = INACTIVE;
6647 spin_unlock_irq(&phba->hbalock);
6648
James Smartd2cc9bc2018-09-10 10:30:50 -07006649 fwlog_buffsize = (LPFC_RAS_MIN_BUFF_POST_SIZE *
6650 phba->cfg_ras_fwlog_buffsize);
6651 fwlog_entry_count = (fwlog_buffsize/LPFC_RAS_MAX_ENTRY_SIZE);
6652
6653 /*
6654 * If re-enabling FW logging support use earlier allocated
6655 * DMA buffers while posting MBX command.
6656 **/
6657 if (!ras_fwlog->lwpd.virt) {
6658 rc = lpfc_sli4_ras_dma_alloc(phba, fwlog_entry_count);
6659 if (rc) {
6660 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smartcb349902018-11-29 16:09:27 -08006661 "6189 FW Log Memory Allocation Failed");
James Smartd2cc9bc2018-09-10 10:30:50 -07006662 return rc;
6663 }
6664 }
6665
6666 /* Setup Mailbox command */
6667 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6668 if (!mbox) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006669 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd2cc9bc2018-09-10 10:30:50 -07006670 "6190 RAS MBX Alloc Failed");
6671 rc = -ENOMEM;
6672 goto mem_free;
6673 }
6674
6675 ras_fwlog->fw_loglevel = fwlog_level;
6676 len = (sizeof(struct lpfc_mbx_set_ras_fwlog) -
6677 sizeof(struct lpfc_sli4_cfg_mhdr));
6678
6679 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_LOWLEVEL,
6680 LPFC_MBOX_OPCODE_SET_DIAG_LOG_OPTION,
6681 len, LPFC_SLI4_MBX_EMBED);
6682
6683 mbx_fwlog = (struct lpfc_mbx_set_ras_fwlog *)&mbox->u.mqe.un.ras_fwlog;
6684 bf_set(lpfc_fwlog_enable, &mbx_fwlog->u.request,
6685 fwlog_enable);
6686 bf_set(lpfc_fwlog_loglvl, &mbx_fwlog->u.request,
6687 ras_fwlog->fw_loglevel);
6688 bf_set(lpfc_fwlog_buffcnt, &mbx_fwlog->u.request,
6689 ras_fwlog->fw_buffcount);
6690 bf_set(lpfc_fwlog_buffsz, &mbx_fwlog->u.request,
6691 LPFC_RAS_MAX_ENTRY_SIZE/SLI4_PAGE_SIZE);
6692
6693 /* Update DMA buffer address */
6694 list_for_each_entry(dmabuf, &ras_fwlog->fwlog_buff_list, list) {
6695 memset(dmabuf->virt, 0, LPFC_RAS_MAX_ENTRY_SIZE);
6696
6697 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_lo =
6698 putPaddrLow(dmabuf->phys);
6699
6700 mbx_fwlog->u.request.buff_fwlog[dmabuf->buffer_tag].addr_hi =
6701 putPaddrHigh(dmabuf->phys);
6702 }
6703
6704 /* Update LPWD address */
6705 mbx_fwlog->u.request.lwpd.addr_lo = putPaddrLow(ras_fwlog->lwpd.phys);
6706 mbx_fwlog->u.request.lwpd.addr_hi = putPaddrHigh(ras_fwlog->lwpd.phys);
6707
James Smart95bfc6d2019-10-18 14:18:27 -07006708 spin_lock_irq(&phba->hbalock);
6709 ras_fwlog->state = REG_INPROGRESS;
6710 spin_unlock_irq(&phba->hbalock);
James Smartd2cc9bc2018-09-10 10:30:50 -07006711 mbox->vport = phba->pport;
6712 mbox->mbox_cmpl = lpfc_sli4_ras_mbox_cmpl;
6713
6714 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6715
6716 if (rc == MBX_NOT_FINISHED) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006717 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcb349902018-11-29 16:09:27 -08006718 "6191 FW-Log Mailbox failed. "
James Smartd2cc9bc2018-09-10 10:30:50 -07006719 "status %d mbxStatus : x%x", rc,
6720 bf_get(lpfc_mqe_status, &mbox->u.mqe));
6721 mempool_free(mbox, phba->mbox_mem_pool);
6722 rc = -EIO;
6723 goto mem_free;
6724 } else
6725 rc = 0;
6726mem_free:
6727 if (rc)
6728 lpfc_sli4_ras_dma_free(phba);
6729
6730 return rc;
6731}
6732
6733/**
6734 * lpfc_sli4_ras_setup - Check if RAS supported on the adapter
6735 * @phba: Pointer to HBA context object.
6736 *
6737 * Check if RAS is supported on the adapter and initialize it.
6738 **/
6739void
6740lpfc_sli4_ras_setup(struct lpfc_hba *phba)
6741{
6742 /* Check RAS FW Log needs to be enabled or not */
6743 if (lpfc_check_fwlog_support(phba))
6744 return;
6745
6746 lpfc_sli4_ras_fwlog_init(phba, phba->cfg_ras_fwlog_level,
6747 LPFC_RAS_ENABLE_LOGGING);
6748}
6749
6750/**
James Smart6d368e52011-05-24 11:44:12 -04006751 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6752 * @phba: Pointer to HBA context object.
6753 *
6754 * This function allocates all SLI4 resource identifiers.
6755 **/
6756int
6757lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6758{
6759 int i, rc, error = 0;
6760 uint16_t count, base;
6761 unsigned long longs;
6762
James Smartff78d8f2011-12-13 13:21:35 -05006763 if (!phba->sli4_hba.rpi_hdrs_in_use)
6764 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006765 if (phba->sli4_hba.extents_in_use) {
6766 /*
6767 * The port supports resource extents. The XRI, VPI, VFI, RPI
6768 * resource extent count must be read and allocated before
6769 * provisioning the resource id arrays.
6770 */
6771 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6772 LPFC_IDX_RSRC_RDY) {
6773 /*
6774 * Extent-based resources are set - the driver could
6775 * be in a port reset. Figure out if any corrective
6776 * actions need to be taken.
6777 */
6778 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6779 LPFC_RSC_TYPE_FCOE_VFI);
6780 if (rc != 0)
6781 error++;
6782 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6783 LPFC_RSC_TYPE_FCOE_VPI);
6784 if (rc != 0)
6785 error++;
6786 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6787 LPFC_RSC_TYPE_FCOE_XRI);
6788 if (rc != 0)
6789 error++;
6790 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6791 LPFC_RSC_TYPE_FCOE_RPI);
6792 if (rc != 0)
6793 error++;
6794
6795 /*
6796 * It's possible that the number of resources
6797 * provided to this port instance changed between
6798 * resets. Detect this condition and reallocate
6799 * resources. Otherwise, there is no action.
6800 */
6801 if (error) {
6802 lpfc_printf_log(phba, KERN_INFO,
6803 LOG_MBOX | LOG_INIT,
6804 "2931 Detected extent resource "
6805 "change. Reallocating all "
6806 "extents.\n");
6807 rc = lpfc_sli4_dealloc_extent(phba,
6808 LPFC_RSC_TYPE_FCOE_VFI);
6809 rc = lpfc_sli4_dealloc_extent(phba,
6810 LPFC_RSC_TYPE_FCOE_VPI);
6811 rc = lpfc_sli4_dealloc_extent(phba,
6812 LPFC_RSC_TYPE_FCOE_XRI);
6813 rc = lpfc_sli4_dealloc_extent(phba,
6814 LPFC_RSC_TYPE_FCOE_RPI);
6815 } else
6816 return 0;
6817 }
6818
6819 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6820 if (unlikely(rc))
6821 goto err_exit;
6822
6823 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6824 if (unlikely(rc))
6825 goto err_exit;
6826
6827 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6828 if (unlikely(rc))
6829 goto err_exit;
6830
6831 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6832 if (unlikely(rc))
6833 goto err_exit;
6834 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6835 LPFC_IDX_RSRC_RDY);
6836 return rc;
6837 } else {
6838 /*
6839 * The port does not support resource extents. The XRI, VPI,
6840 * VFI, RPI resource ids were determined from READ_CONFIG.
6841 * Just allocate the bitmasks and provision the resource id
6842 * arrays. If a port reset is active, the resources don't
6843 * need any action - just exit.
6844 */
6845 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006846 LPFC_IDX_RSRC_RDY) {
6847 lpfc_sli4_dealloc_resource_identifiers(phba);
6848 lpfc_sli4_remove_rpis(phba);
6849 }
James Smart6d368e52011-05-24 11:44:12 -04006850 /* RPIs. */
6851 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006852 if (count <= 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006853 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a630c22013-01-03 15:44:09 -05006854 "3279 Invalid provisioning of "
6855 "rpi:%d\n", count);
6856 rc = -EINVAL;
6857 goto err_exit;
6858 }
James Smart6d368e52011-05-24 11:44:12 -04006859 base = phba->sli4_hba.max_cfg_param.rpi_base;
6860 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006861 phba->sli4_hba.rpi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006862 sizeof(unsigned long),
6863 GFP_KERNEL);
6864 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6865 rc = -ENOMEM;
6866 goto err_exit;
6867 }
Kees Cook6396bb22018-06-12 14:03:40 -07006868 phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006869 GFP_KERNEL);
6870 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6871 rc = -ENOMEM;
6872 goto free_rpi_bmask;
6873 }
6874
6875 for (i = 0; i < count; i++)
6876 phba->sli4_hba.rpi_ids[i] = base + i;
6877
6878 /* VPIs. */
6879 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006880 if (count <= 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006881 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a630c22013-01-03 15:44:09 -05006882 "3280 Invalid provisioning of "
6883 "vpi:%d\n", count);
6884 rc = -EINVAL;
6885 goto free_rpi_ids;
6886 }
James Smart6d368e52011-05-24 11:44:12 -04006887 base = phba->sli4_hba.max_cfg_param.vpi_base;
6888 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006889 phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
James Smart6d368e52011-05-24 11:44:12 -04006890 GFP_KERNEL);
6891 if (unlikely(!phba->vpi_bmask)) {
6892 rc = -ENOMEM;
6893 goto free_rpi_ids;
6894 }
Kees Cook6396bb22018-06-12 14:03:40 -07006895 phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006896 GFP_KERNEL);
6897 if (unlikely(!phba->vpi_ids)) {
6898 rc = -ENOMEM;
6899 goto free_vpi_bmask;
6900 }
6901
6902 for (i = 0; i < count; i++)
6903 phba->vpi_ids[i] = base + i;
6904
6905 /* XRIs. */
6906 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006907 if (count <= 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006908 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a630c22013-01-03 15:44:09 -05006909 "3281 Invalid provisioning of "
6910 "xri:%d\n", count);
6911 rc = -EINVAL;
6912 goto free_vpi_ids;
6913 }
James Smart6d368e52011-05-24 11:44:12 -04006914 base = phba->sli4_hba.max_cfg_param.xri_base;
6915 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006916 phba->sli4_hba.xri_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006917 sizeof(unsigned long),
6918 GFP_KERNEL);
6919 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6920 rc = -ENOMEM;
6921 goto free_vpi_ids;
6922 }
James Smart41899be2012-03-01 22:34:19 -05006923 phba->sli4_hba.max_cfg_param.xri_used = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07006924 phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006925 GFP_KERNEL);
6926 if (unlikely(!phba->sli4_hba.xri_ids)) {
6927 rc = -ENOMEM;
6928 goto free_xri_bmask;
6929 }
6930
6931 for (i = 0; i < count; i++)
6932 phba->sli4_hba.xri_ids[i] = base + i;
6933
6934 /* VFIs. */
6935 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006936 if (count <= 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07006937 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0a630c22013-01-03 15:44:09 -05006938 "3282 Invalid provisioning of "
6939 "vfi:%d\n", count);
6940 rc = -EINVAL;
6941 goto free_xri_ids;
6942 }
James Smart6d368e52011-05-24 11:44:12 -04006943 base = phba->sli4_hba.max_cfg_param.vfi_base;
6944 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07006945 phba->sli4_hba.vfi_bmask = kcalloc(longs,
James Smart6d368e52011-05-24 11:44:12 -04006946 sizeof(unsigned long),
6947 GFP_KERNEL);
6948 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6949 rc = -ENOMEM;
6950 goto free_xri_ids;
6951 }
Kees Cook6396bb22018-06-12 14:03:40 -07006952 phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
James Smart6d368e52011-05-24 11:44:12 -04006953 GFP_KERNEL);
6954 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6955 rc = -ENOMEM;
6956 goto free_vfi_bmask;
6957 }
6958
6959 for (i = 0; i < count; i++)
6960 phba->sli4_hba.vfi_ids[i] = base + i;
6961
6962 /*
6963 * Mark all resources ready. An HBA reset doesn't need
6964 * to reset the initialization.
6965 */
6966 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6967 LPFC_IDX_RSRC_RDY);
6968 return 0;
6969 }
6970
6971 free_vfi_bmask:
6972 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006973 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006974 free_xri_ids:
6975 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006976 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006977 free_xri_bmask:
6978 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006979 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006980 free_vpi_ids:
6981 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006982 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006983 free_vpi_bmask:
6984 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006985 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006986 free_rpi_ids:
6987 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006988 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006989 free_rpi_bmask:
6990 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006991 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006992 err_exit:
6993 return rc;
6994}
6995
6996/**
6997 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6998 * @phba: Pointer to HBA context object.
6999 *
7000 * This function allocates the number of elements for the specified
7001 * resource type.
7002 **/
7003int
7004lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
7005{
7006 if (phba->sli4_hba.extents_in_use) {
7007 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
7008 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
7009 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
7010 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
7011 } else {
7012 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04007013 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04007014 kfree(phba->vpi_ids);
7015 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7016 kfree(phba->sli4_hba.xri_bmask);
7017 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04007018 kfree(phba->sli4_hba.vfi_bmask);
7019 kfree(phba->sli4_hba.vfi_ids);
7020 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7021 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
7022 }
7023
7024 return 0;
7025}
7026
7027/**
James Smartb76f2dc2011-07-22 18:37:42 -04007028 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
7029 * @phba: Pointer to HBA context object.
7030 * @type: The resource extent type.
Lee Jones7af29d42020-07-21 17:41:31 +01007031 * @extnt_cnt: buffer to hold port extent count response
James Smartb76f2dc2011-07-22 18:37:42 -04007032 * @extnt_size: buffer to hold port extent size response.
7033 *
7034 * This function calls the port to read the host allocated extents
7035 * for a particular type.
7036 **/
7037int
7038lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
7039 uint16_t *extnt_cnt, uint16_t *extnt_size)
7040{
7041 bool emb;
7042 int rc = 0;
7043 uint16_t curr_blks = 0;
7044 uint32_t req_len, emb_len;
7045 uint32_t alloc_len, mbox_tmo;
7046 struct list_head *blk_list_head;
7047 struct lpfc_rsrc_blks *rsrc_blk;
7048 LPFC_MBOXQ_t *mbox;
7049 void *virtaddr = NULL;
7050 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
7051 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
7052 union lpfc_sli4_cfg_shdr *shdr;
7053
7054 switch (type) {
7055 case LPFC_RSC_TYPE_FCOE_VPI:
7056 blk_list_head = &phba->lpfc_vpi_blk_list;
7057 break;
7058 case LPFC_RSC_TYPE_FCOE_XRI:
7059 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
7060 break;
7061 case LPFC_RSC_TYPE_FCOE_VFI:
7062 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
7063 break;
7064 case LPFC_RSC_TYPE_FCOE_RPI:
7065 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
7066 break;
7067 default:
7068 return -EIO;
7069 }
7070
7071 /* Count the number of extents currently allocatd for this type. */
7072 list_for_each_entry(rsrc_blk, blk_list_head, list) {
7073 if (curr_blks == 0) {
7074 /*
7075 * The GET_ALLOCATED mailbox does not return the size,
7076 * just the count. The size should be just the size
7077 * stored in the current allocated block and all sizes
7078 * for an extent type are the same so set the return
7079 * value now.
7080 */
7081 *extnt_size = rsrc_blk->rsrc_size;
7082 }
7083 curr_blks++;
7084 }
7085
James Smartb76f2dc2011-07-22 18:37:42 -04007086 /*
7087 * Calculate the size of an embedded mailbox. The uint32_t
7088 * accounts for extents-specific word.
7089 */
7090 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
7091 sizeof(uint32_t);
7092
7093 /*
7094 * Presume the allocation and response will fit into an embedded
7095 * mailbox. If not true, reconfigure to a non-embedded mailbox.
7096 */
7097 emb = LPFC_SLI4_MBX_EMBED;
7098 req_len = emb_len;
7099 if (req_len > emb_len) {
7100 req_len = curr_blks * sizeof(uint16_t) +
7101 sizeof(union lpfc_sli4_cfg_shdr) +
7102 sizeof(uint32_t);
7103 emb = LPFC_SLI4_MBX_NEMBED;
7104 }
7105
7106 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7107 if (!mbox)
7108 return -ENOMEM;
7109 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
7110
7111 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7112 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
7113 req_len, emb);
7114 if (alloc_len < req_len) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007115 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartb76f2dc2011-07-22 18:37:42 -04007116 "2983 Allocated DMA memory size (x%x) is "
7117 "less than the requested DMA memory "
7118 "size (x%x)\n", alloc_len, req_len);
7119 rc = -ENOMEM;
7120 goto err_exit;
7121 }
7122 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
7123 if (unlikely(rc)) {
7124 rc = -EIO;
7125 goto err_exit;
7126 }
7127
7128 if (!phba->sli4_hba.intr_enable)
7129 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
7130 else {
James Smarta183a152011-10-10 21:32:43 -04007131 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04007132 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
7133 }
7134
7135 if (unlikely(rc)) {
7136 rc = -EIO;
7137 goto err_exit;
7138 }
7139
7140 /*
7141 * Figure out where the response is located. Then get local pointers
7142 * to the response data. The port does not guarantee to respond to
7143 * all extents counts request so update the local variable with the
7144 * allocated count from the port.
7145 */
7146 if (emb == LPFC_SLI4_MBX_EMBED) {
7147 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
7148 shdr = &rsrc_ext->header.cfg_shdr;
7149 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
7150 } else {
7151 virtaddr = mbox->sge_array->addr[0];
7152 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
7153 shdr = &n_rsrc->cfg_shdr;
7154 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
7155 }
7156
7157 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007158 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartb76f2dc2011-07-22 18:37:42 -04007159 "2984 Failed to read allocated resources "
7160 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
7161 type,
7162 bf_get(lpfc_mbox_hdr_status, &shdr->response),
7163 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
7164 rc = -EIO;
7165 goto err_exit;
7166 }
7167 err_exit:
7168 lpfc_sli4_mbox_cmd_free(phba, mbox);
7169 return rc;
7170}
7171
7172/**
James Smart0ef69962017-04-21 16:04:50 -07007173 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04007174 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08007175 * @sgl_list: linked link of sgl buffers to post
7176 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04007177 *
James Smart895427b2017-02-12 13:52:30 -08007178 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04007179 * repost them to the port by using SGL block post. This is needed after a
7180 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08007181 * of buffer sgls which contains contiguous xris and uses the non-embedded
7182 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04007183 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
7184 * mailbox command for posting.
7185 *
7186 * Returns: 0 = success, non-zero failure.
7187 **/
7188static int
James Smart895427b2017-02-12 13:52:30 -08007189lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
7190 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04007191{
7192 struct lpfc_sglq *sglq_entry = NULL;
7193 struct lpfc_sglq *sglq_entry_next = NULL;
7194 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08007195 int status, total_cnt;
7196 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04007197 int last_xritag = NO_XRI;
7198 LIST_HEAD(prep_sgl_list);
7199 LIST_HEAD(blck_sgl_list);
7200 LIST_HEAD(allc_sgl_list);
7201 LIST_HEAD(post_sgl_list);
7202 LIST_HEAD(free_sgl_list);
7203
James Smart38c20672013-03-01 16:37:44 -05007204 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007205 spin_lock(&phba->sli4_hba.sgl_list_lock);
7206 list_splice_init(sgl_list, &allc_sgl_list);
7207 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007208 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007209
James Smart895427b2017-02-12 13:52:30 -08007210 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007211 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
7212 &allc_sgl_list, list) {
7213 list_del_init(&sglq_entry->list);
7214 block_cnt++;
7215 if ((last_xritag != NO_XRI) &&
7216 (sglq_entry->sli4_xritag != last_xritag + 1)) {
7217 /* a hole in xri block, form a sgl posting block */
7218 list_splice_init(&prep_sgl_list, &blck_sgl_list);
7219 post_cnt = block_cnt - 1;
7220 /* prepare list for next posting block */
7221 list_add_tail(&sglq_entry->list, &prep_sgl_list);
7222 block_cnt = 1;
7223 } else {
7224 /* prepare list for next posting block */
7225 list_add_tail(&sglq_entry->list, &prep_sgl_list);
7226 /* enough sgls for non-embed sgl mbox command */
7227 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
7228 list_splice_init(&prep_sgl_list,
7229 &blck_sgl_list);
7230 post_cnt = block_cnt;
7231 block_cnt = 0;
7232 }
7233 }
7234 num_posted++;
7235
7236 /* keep track of last sgl's xritag */
7237 last_xritag = sglq_entry->sli4_xritag;
7238
James Smart895427b2017-02-12 13:52:30 -08007239 /* end of repost sgl list condition for buffers */
7240 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04007241 if (post_cnt == 0) {
7242 list_splice_init(&prep_sgl_list,
7243 &blck_sgl_list);
7244 post_cnt = block_cnt;
7245 } else if (block_cnt == 1) {
7246 status = lpfc_sli4_post_sgl(phba,
7247 sglq_entry->phys, 0,
7248 sglq_entry->sli4_xritag);
7249 if (!status) {
7250 /* successful, put sgl to posted list */
7251 list_add_tail(&sglq_entry->list,
7252 &post_sgl_list);
7253 } else {
7254 /* Failure, put sgl to free list */
7255 lpfc_printf_log(phba, KERN_WARNING,
7256 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007257 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04007258 "sgl, xritag:x%x\n",
7259 sglq_entry->sli4_xritag);
7260 list_add_tail(&sglq_entry->list,
7261 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007262 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04007263 }
7264 }
7265 }
7266
7267 /* continue until a nembed page worth of sgls */
7268 if (post_cnt == 0)
7269 continue;
7270
James Smart895427b2017-02-12 13:52:30 -08007271 /* post the buffer list sgls as a block */
7272 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
7273 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04007274
7275 if (!status) {
7276 /* success, put sgl list to posted sgl list */
7277 list_splice_init(&blck_sgl_list, &post_sgl_list);
7278 } else {
7279 /* Failure, put sgl list to free sgl list */
7280 sglq_entry_first = list_first_entry(&blck_sgl_list,
7281 struct lpfc_sglq,
7282 list);
7283 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08007284 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04007285 "xritag:x%x-x%x\n",
7286 sglq_entry_first->sli4_xritag,
7287 (sglq_entry_first->sli4_xritag +
7288 post_cnt - 1));
7289 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04007290 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007291 }
7292
7293 /* don't reset xirtag due to hole in xri block */
7294 if (block_cnt == 0)
7295 last_xritag = NO_XRI;
7296
James Smart895427b2017-02-12 13:52:30 -08007297 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04007298 post_cnt = 0;
7299 }
7300
James Smart895427b2017-02-12 13:52:30 -08007301 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04007302 lpfc_free_sgl_list(phba, &free_sgl_list);
7303
James Smart895427b2017-02-12 13:52:30 -08007304 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04007305 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05007306 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08007307 spin_lock(&phba->sli4_hba.sgl_list_lock);
7308 list_splice_init(&post_sgl_list, sgl_list);
7309 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05007310 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04007311 } else {
Dick Kennedy372c1872020-06-30 14:50:00 -07007312 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08007313 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04007314 return -EIO;
7315 }
James Smart895427b2017-02-12 13:52:30 -08007316
7317 /* return the number of XRIs actually posted */
7318 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04007319}
7320
James Smart0794d602019-01-28 11:14:19 -08007321/**
James Smart5e5b5112019-01-28 11:14:22 -08007322 * lpfc_sli4_repost_io_sgl_list - Repost all the allocated nvme buffer sgls
James Smart0794d602019-01-28 11:14:19 -08007323 * @phba: pointer to lpfc hba data structure.
7324 *
7325 * This routine walks the list of nvme buffers that have been allocated and
7326 * repost them to the port by using SGL block post. This is needed after a
7327 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
7328 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
James Smart5e5b5112019-01-28 11:14:22 -08007329 * to the lpfc_io_buf_list. If the repost fails, reject all nvme buffers.
James Smart0794d602019-01-28 11:14:19 -08007330 *
7331 * Returns: 0 = success, non-zero failure.
7332 **/
Bart Van Assche3999df72019-03-28 11:06:16 -07007333static int
James Smart5e5b5112019-01-28 11:14:22 -08007334lpfc_sli4_repost_io_sgl_list(struct lpfc_hba *phba)
James Smart0794d602019-01-28 11:14:19 -08007335{
7336 LIST_HEAD(post_nblist);
7337 int num_posted, rc = 0;
7338
7339 /* get all NVME buffers need to repost to a local list */
James Smart5e5b5112019-01-28 11:14:22 -08007340 lpfc_io_buf_flush(phba, &post_nblist);
James Smart0794d602019-01-28 11:14:19 -08007341
7342 /* post the list of nvme buffer sgls to port if available */
7343 if (!list_empty(&post_nblist)) {
James Smart5e5b5112019-01-28 11:14:22 -08007344 num_posted = lpfc_sli4_post_io_sgl_list(
7345 phba, &post_nblist, phba->sli4_hba.io_xri_cnt);
James Smart0794d602019-01-28 11:14:19 -08007346 /* failed to post any nvme buffer, return error */
7347 if (num_posted == 0)
7348 rc = -EIO;
7349 }
7350 return rc;
7351}
7352
Bart Van Assche3999df72019-03-28 11:06:16 -07007353static void
James Smart61bda8f2016-10-13 15:06:05 -07007354lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
7355{
7356 uint32_t len;
7357
7358 len = sizeof(struct lpfc_mbx_set_host_data) -
7359 sizeof(struct lpfc_sli4_cfg_mhdr);
7360 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
7361 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
7362 LPFC_SLI4_MBX_EMBED);
7363
7364 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08007365 mbox->u.mqe.un.set_host_data.param_len =
7366 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07007367 snprintf(mbox->u.mqe.un.set_host_data.data,
7368 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
7369 "Linux %s v"LPFC_DRIVER_VERSION,
7370 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
7371}
7372
James Smarta8cf5df2017-05-15 15:20:46 -07007373int
James Smart6c621a22017-05-15 15:20:45 -07007374lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07007375 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07007376{
7377 int rc, i;
7378 struct lpfc_rqe hrqe;
7379 struct lpfc_rqe drqe;
7380 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08007381 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07007382 struct rqb_dmabuf *rqb_buffer;
7383 LIST_HEAD(rqb_buf_list);
7384
7385 rqbp = hrq->rqbp;
7386 for (i = 0; i < count; i++) {
James Smart62e3a931d2020-10-20 13:27:11 -07007387 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007388 /* IF RQ is already full, don't bother */
James Smart62e3a931d2020-10-20 13:27:11 -07007389 if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {
7390 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007391 break;
James Smart62e3a931d2020-10-20 13:27:11 -07007392 }
7393 spin_unlock_irqrestore(&phba->hbalock, flags);
7394
James Smart6c621a22017-05-15 15:20:45 -07007395 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
7396 if (!rqb_buffer)
7397 break;
7398 rqb_buffer->hrq = hrq;
7399 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07007400 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07007401 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
7402 }
James Smart62e3a931d2020-10-20 13:27:11 -07007403
7404 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007405 while (!list_empty(&rqb_buf_list)) {
7406 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
7407 hbuf.list);
7408
7409 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
7410 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
7411 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
7412 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
7413 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
7414 if (rc < 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007415 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart411de512018-01-30 15:58:52 -08007416 "6421 Cannot post to HRQ %d: %x %x %x "
7417 "DRQ %x %x\n",
7418 hrq->queue_id,
7419 hrq->host_index,
7420 hrq->hba_index,
7421 hrq->entry_count,
7422 drq->host_index,
7423 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07007424 rqbp->rqb_free_buffer(phba, rqb_buffer);
7425 } else {
7426 list_add_tail(&rqb_buffer->hbuf.list,
7427 &rqbp->rqb_buffer_list);
7428 rqbp->buffer_count++;
7429 }
7430 }
James Smart411de512018-01-30 15:58:52 -08007431 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07007432 return 1;
7433}
7434
Dick Kennedy317aeb82020-06-30 14:49:59 -07007435/**
7436 * lpfc_init_idle_stat_hb - Initialize idle_stat tracking
Lee Jones7af29d42020-07-21 17:41:31 +01007437 * @phba: pointer to lpfc hba data structure.
Dick Kennedy317aeb82020-06-30 14:49:59 -07007438 *
7439 * This routine initializes the per-cq idle_stat to dynamically dictate
7440 * polling decisions.
7441 *
7442 * Return codes:
7443 * None
7444 **/
7445static void lpfc_init_idle_stat_hb(struct lpfc_hba *phba)
7446{
7447 int i;
7448 struct lpfc_sli4_hdw_queue *hdwq;
7449 struct lpfc_queue *cq;
7450 struct lpfc_idle_stat *idle_stat;
7451 u64 wall;
7452
7453 for_each_present_cpu(i) {
7454 hdwq = &phba->sli4_hba.hdwq[phba->sli4_hba.cpu_map[i].hdwq];
7455 cq = hdwq->io_cq;
7456
7457 /* Skip if we've already handled this cq's primary CPU */
7458 if (cq->chann != i)
7459 continue;
7460
7461 idle_stat = &phba->sli4_hba.idle_stat[i];
7462
7463 idle_stat->prev_idle = get_cpu_idle_time(i, &wall, 1);
7464 idle_stat->prev_wall = wall;
7465
7466 if (phba->nvmet_support)
7467 cq->poll_mode = LPFC_QUEUE_WORK;
7468 else
7469 cq->poll_mode = LPFC_IRQ_POLL;
7470 }
7471
7472 if (!phba->nvmet_support)
7473 schedule_delayed_work(&phba->idle_stat_delay_work,
7474 msecs_to_jiffies(LPFC_IDLE_STAT_DELAY));
7475}
7476
Dick Kennedyf0020e42020-06-30 14:49:58 -07007477static void lpfc_sli4_dip(struct lpfc_hba *phba)
7478{
7479 uint32_t if_type;
7480
7481 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7482 if (if_type == LPFC_SLI_INTF_IF_TYPE_2 ||
7483 if_type == LPFC_SLI_INTF_IF_TYPE_6) {
7484 struct lpfc_register reg_data;
7485
7486 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
7487 &reg_data.word0))
7488 return;
7489
7490 if (bf_get(lpfc_sliport_status_dip, &reg_data))
James Smart0b3ad322021-01-04 10:02:39 -08007491 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedyf0020e42020-06-30 14:49:58 -07007492 "2904 Firmware Dump Image Present"
7493 " on Adapter");
7494 }
7495}
7496
James Smart8a9d2e82012-05-09 21:16:12 -04007497/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08007498 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04007499 * @phba: Pointer to HBA context object.
7500 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08007501 * This function is the main SLI4 device initialization PCI function. This
7502 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04007503 * HBA error attention handler code. Caller is not required to hold any
7504 * locks.
7505 **/
7506int
7507lpfc_sli4_hba_setup(struct lpfc_hba *phba)
7508{
James Smart171f6c42019-11-04 16:57:07 -08007509 int rc, i, cnt, len, dd;
James Smartda0436e2009-05-22 14:51:39 -04007510 LPFC_MBOXQ_t *mboxq;
7511 struct lpfc_mqe *mqe;
7512 uint8_t *vpd;
7513 uint32_t vpd_size;
7514 uint32_t ftr_rsp = 0;
7515 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
7516 struct lpfc_vport *vport = phba->pport;
7517 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08007518 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04007519
7520 /* Perform a PCI function reset to start from clean */
7521 rc = lpfc_pci_function_reset(phba);
7522 if (unlikely(rc))
7523 return -ENODEV;
7524
7525 /* Check the HBA Host Status Register for readyness */
7526 rc = lpfc_sli4_post_status_check(phba);
7527 if (unlikely(rc))
7528 return -ENODEV;
7529 else {
7530 spin_lock_irq(&phba->hbalock);
7531 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
7532 spin_unlock_irq(&phba->hbalock);
7533 }
7534
Dick Kennedyf0020e42020-06-30 14:49:58 -07007535 lpfc_sli4_dip(phba);
7536
James Smartda0436e2009-05-22 14:51:39 -04007537 /*
7538 * Allocate a single mailbox container for initializing the
7539 * port.
7540 */
7541 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7542 if (!mboxq)
7543 return -ENOMEM;
7544
James Smartda0436e2009-05-22 14:51:39 -04007545 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04007546 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04007547 vpd = kzalloc(vpd_size, GFP_KERNEL);
7548 if (!vpd) {
7549 rc = -ENOMEM;
7550 goto out_free_mbox;
7551 }
7552
7553 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05007554 if (unlikely(rc)) {
7555 kfree(vpd);
7556 goto out_free_mbox;
7557 }
James Smart572709e2013-07-15 18:32:43 -04007558
James Smartda0436e2009-05-22 14:51:39 -04007559 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04007560 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07007561 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05007562 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007563 phba->fcp_embed_io = 0; /* SLI4 FC support only */
7564 } else {
James Smart76a95d72010-11-20 23:11:48 -05007565 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07007566 }
James Smart45ed1192009-10-02 15:17:02 -04007567
7568 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
7569 LPFC_DCBX_CEE_MODE)
7570 phba->hba_flag |= HBA_FIP_SUPPORT;
7571 else
7572 phba->hba_flag &= ~HBA_FIP_SUPPORT;
7573
James Smartc00f62e2019-08-14 16:57:11 -07007574 phba->hba_flag &= ~HBA_IOQ_FLUSH;
James Smart4f2e66c2012-05-09 21:17:07 -04007575
James Smartc31098c2011-04-16 11:03:33 -04007576 if (phba->sli_rev != LPFC_SLI_REV4) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007577 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04007578 "0376 READ_REV Error. SLI Level %d "
7579 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05007580 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04007581 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007582 kfree(vpd);
7583 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007584 }
James Smartcd1c8302011-10-10 21:33:25 -04007585
7586 /*
James Smartff78d8f2011-12-13 13:21:35 -05007587 * Continue initialization with default values even if driver failed
7588 * to read FCoE param config regions, only read parameters if the
7589 * board is FCoE
7590 */
7591 if (phba->hba_flag & HBA_FCOE_MODE &&
7592 lpfc_sli4_read_fcoe_params(phba))
7593 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
7594 "2570 Failed to read FCoE parameters\n");
7595
7596 /*
James Smartcd1c8302011-10-10 21:33:25 -04007597 * Retrieve sli4 device physical port name, failure of doing it
7598 * is considered as non-fatal.
7599 */
7600 rc = lpfc_sli4_retrieve_pport_name(phba);
7601 if (!rc)
7602 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7603 "3080 Successful retrieving SLI4 device "
7604 "physical port name: %s.\n", phba->Port);
7605
James Smartb3b4f3e2019-03-12 16:30:23 -07007606 rc = lpfc_sli4_get_ctl_attr(phba);
7607 if (!rc)
7608 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7609 "8351 Successful retrieving SLI4 device "
7610 "CTL ATTR\n");
7611
James Smartda0436e2009-05-22 14:51:39 -04007612 /*
7613 * Evaluate the read rev and vpd data. Populate the driver
7614 * state with the results. If this routine fails, the failure
7615 * is not fatal as the driver will use generic values.
7616 */
7617 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
7618 if (unlikely(!rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007619 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04007620 "0377 Error %d parsing vpd. "
7621 "Using defaults.\n", rc);
7622 rc = 0;
7623 }
James Smart76a95d72010-11-20 23:11:48 -05007624 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04007625
James Smartf1126682009-06-10 17:22:44 -04007626 /* Save information as VPD data */
7627 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
7628 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
James Smart4e565cf2018-02-22 08:18:50 -08007629
7630 /*
7631 * This is because first G7 ASIC doesn't support the standard
7632 * 0x5a NVME cmd descriptor type/subtype
7633 */
7634 if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
7635 LPFC_SLI_INTF_IF_TYPE_6) &&
7636 (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
7637 (phba->vpd.rev.smRev == 0) &&
7638 (phba->cfg_nvme_embed_cmd == 1))
7639 phba->cfg_nvme_embed_cmd = 0;
7640
James Smartf1126682009-06-10 17:22:44 -04007641 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
7642 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
7643 &mqe->un.read_rev);
7644 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
7645 &mqe->un.read_rev);
7646 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
7647 &mqe->un.read_rev);
7648 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
7649 &mqe->un.read_rev);
7650 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
7651 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
7652 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
7653 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
7654 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
7655 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
7656 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7657 "(%d):0380 READ_REV Status x%x "
7658 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
7659 mboxq->vport ? mboxq->vport->vpi : 0,
7660 bf_get(lpfc_mqe_status, mqe),
7661 phba->vpd.rev.opFwName,
7662 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
7663 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04007664
James Smart65791f12016-07-06 12:35:56 -07007665 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07007666 LPFC_SLI_INTF_IF_TYPE_0) {
7667 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
7668 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7669 if (rc == MBX_SUCCESS) {
7670 phba->hba_flag |= HBA_RECOVERABLE_UE;
7671 /* Set 1Sec interval to detect UE */
7672 phba->eratt_poll_interval = 1;
7673 phba->sli4_hba.ue_to_sr = bf_get(
7674 lpfc_mbx_set_feature_UESR,
7675 &mboxq->u.mqe.un.set_feature);
7676 phba->sli4_hba.ue_to_rp = bf_get(
7677 lpfc_mbx_set_feature_UERP,
7678 &mboxq->u.mqe.un.set_feature);
7679 }
7680 }
7681
7682 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
7683 /* Enable MDS Diagnostics only if the SLI Port supports it */
7684 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
7685 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7686 if (rc != MBX_SUCCESS)
7687 phba->mds_diags_support = 0;
7688 }
James Smart572709e2013-07-15 18:32:43 -04007689
James Smartda0436e2009-05-22 14:51:39 -04007690 /*
7691 * Discover the port's supported feature set and match it against the
7692 * hosts requests.
7693 */
7694 lpfc_request_features(phba, mboxq);
7695 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7696 if (unlikely(rc)) {
7697 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007698 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007699 }
7700
7701 /*
7702 * The port must support FCP initiator mode as this is the
7703 * only mode running in the host.
7704 */
7705 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
7706 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7707 "0378 No support for fcpi mode.\n");
7708 ftr_rsp++;
7709 }
James Smart0bc2b7c2018-02-22 08:18:48 -08007710
7711 /* Performance Hints are ONLY for FCoE */
7712 if (phba->hba_flag & HBA_FCOE_MODE) {
7713 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
7714 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
7715 else
7716 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
7717 }
7718
James Smartda0436e2009-05-22 14:51:39 -04007719 /*
7720 * If the port cannot support the host's requested features
7721 * then turn off the global config parameters to disable the
7722 * feature in the driver. This is not a fatal error.
7723 */
James Smartf44ac122018-03-05 12:04:08 -08007724 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
7725 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
7726 phba->cfg_enable_bg = 0;
7727 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
James Smartbf086112011-08-21 21:48:13 -04007728 ftr_rsp++;
James Smartf44ac122018-03-05 12:04:08 -08007729 }
James Smartbf086112011-08-21 21:48:13 -04007730 }
James Smartda0436e2009-05-22 14:51:39 -04007731
7732 if (phba->max_vpi && phba->cfg_enable_npiv &&
7733 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7734 ftr_rsp++;
7735
7736 if (ftr_rsp) {
7737 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7738 "0379 Feature Mismatch Data: x%08x %08x "
7739 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
7740 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
7741 phba->cfg_enable_npiv, phba->max_vpi);
7742 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
7743 phba->cfg_enable_bg = 0;
7744 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
7745 phba->cfg_enable_npiv = 0;
7746 }
7747
7748 /* These SLI3 features are assumed in SLI4 */
7749 spin_lock_irq(&phba->hbalock);
7750 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
7751 spin_unlock_irq(&phba->hbalock);
7752
James Smart171f6c42019-11-04 16:57:07 -08007753 /* Always try to enable dual dump feature if we can */
7754 lpfc_set_features(phba, mboxq, LPFC_SET_DUAL_DUMP);
7755 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7756 dd = bf_get(lpfc_mbx_set_feature_dd, &mboxq->u.mqe.un.set_feature);
7757 if ((rc == MBX_SUCCESS) && (dd == LPFC_ENABLE_DUAL_DUMP))
Dick Kennedy372c1872020-06-30 14:50:00 -07007758 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart171f6c42019-11-04 16:57:07 -08007759 "6448 Dual Dump is enabled\n");
7760 else
7761 lpfc_printf_log(phba, KERN_INFO, LOG_SLI | LOG_INIT,
7762 "6447 Dual Dump Mailbox x%x (x%x/x%x) failed, "
7763 "rc:x%x dd:x%x\n",
7764 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7765 lpfc_sli_config_mbox_subsys_get(
7766 phba, mboxq),
7767 lpfc_sli_config_mbox_opcode_get(
7768 phba, mboxq),
7769 rc, dd);
James Smart6d368e52011-05-24 11:44:12 -04007770 /*
7771 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
7772 * calls depends on these resources to complete port setup.
7773 */
7774 rc = lpfc_sli4_alloc_resource_identifiers(phba);
7775 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007776 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6d368e52011-05-24 11:44:12 -04007777 "2920 Failed to alloc Resource IDs "
7778 "rc = x%x\n", rc);
7779 goto out_free_mbox;
7780 }
7781
James Smart61bda8f2016-10-13 15:06:05 -07007782 lpfc_set_host_data(phba, mboxq);
7783
7784 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7785 if (rc) {
7786 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7787 "2134 Failed to set host os driver version %x",
7788 rc);
7789 }
7790
James Smartda0436e2009-05-22 14:51:39 -04007791 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05007792 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
7793 if (rc) {
7794 phba->link_state = LPFC_HBA_ERROR;
7795 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05007796 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05007797 }
7798
James Smartda0436e2009-05-22 14:51:39 -04007799 mboxq->vport = vport;
7800 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart3e1f0712018-11-29 16:09:29 -08007801 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smartda0436e2009-05-22 14:51:39 -04007802 if (rc == MBX_SUCCESS) {
7803 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
7804 rc = 0;
7805 }
7806
7807 /*
7808 * This memory was allocated by the lpfc_read_sparam routine. Release
7809 * it to the mbuf pool.
7810 */
7811 lpfc_mbuf_free(phba, mp->virt, mp->phys);
7812 kfree(mp);
James Smart3e1f0712018-11-29 16:09:29 -08007813 mboxq->ctx_buf = NULL;
James Smartda0436e2009-05-22 14:51:39 -04007814 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007815 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04007816 "0382 READ_SPARAM command failed "
7817 "status %d, mbxStatus x%x\n",
7818 rc, bf_get(lpfc_mqe_status, mqe));
7819 phba->link_state = LPFC_HBA_ERROR;
7820 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007821 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007822 }
7823
James Smart05580562011-05-24 11:40:48 -04007824 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007825
7826 /* Update the fc_host data structures with new wwn. */
7827 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7828 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7829
James Smart895427b2017-02-12 13:52:30 -08007830 /* Create all the SLI4 queues */
7831 rc = lpfc_sli4_queue_create(phba);
7832 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007833 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08007834 "3089 Failed to allocate queues\n");
7835 rc = -ENODEV;
7836 goto out_free_mbox;
7837 }
7838 /* Set up all the queues to the device */
7839 rc = lpfc_sli4_queue_setup(phba);
7840 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007841 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08007842 "0381 Error %d during queue setup.\n ", rc);
7843 goto out_stop_timers;
7844 }
7845 /* Initialize the driver internal SLI layer lists. */
7846 lpfc_sli4_setup(phba);
7847 lpfc_sli4_queue_init(phba);
7848
7849 /* update host els xri-sgl sizes and mappings */
7850 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007851 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007852 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04007853 "1400 Failed to update xri-sgl size and "
7854 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007855 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007856 }
7857
James Smart8a9d2e82012-05-09 21:16:12 -04007858 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007859 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7860 phba->sli4_hba.els_xri_cnt);
7861 if (unlikely(rc < 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007862 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8a9d2e82012-05-09 21:16:12 -04007863 "0582 Error %d during els sgl post "
7864 "operation\n", rc);
7865 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007866 goto out_destroy_queue;
7867 }
7868 phba->sli4_hba.els_xri_cnt = rc;
7869
James Smartf358dd02017-02-12 13:52:34 -08007870 if (phba->nvmet_support) {
7871 /* update host nvmet xri-sgl sizes and mappings */
7872 rc = lpfc_sli4_nvmet_sgl_update(phba);
7873 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007874 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08007875 "6308 Failed to update nvmet-sgl size "
7876 "and mapping: %d\n", rc);
7877 goto out_destroy_queue;
7878 }
7879
7880 /* register the nvmet sgl pool to the port */
7881 rc = lpfc_sli4_repost_sgl_list(
7882 phba,
7883 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7884 phba->sli4_hba.nvmet_xri_cnt);
7885 if (unlikely(rc < 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007886 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartf358dd02017-02-12 13:52:34 -08007887 "3117 Error %d during nvmet "
7888 "sgl post\n", rc);
7889 rc = -ENODEV;
7890 goto out_destroy_queue;
7891 }
7892 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007893
James Smarta5f73372019-09-21 20:58:50 -07007894 /* We allocate an iocbq for every receive context SGL.
7895 * The additional allocation is for abort and ls handling.
7896 */
7897 cnt = phba->sli4_hba.nvmet_xri_cnt +
7898 phba->sli4_hba.max_cfg_param.max_xri;
James Smartf358dd02017-02-12 13:52:34 -08007899 } else {
James Smart0794d602019-01-28 11:14:19 -08007900 /* update host common xri-sgl sizes and mappings */
James Smart5e5b5112019-01-28 11:14:22 -08007901 rc = lpfc_sli4_io_sgl_update(phba);
James Smart895427b2017-02-12 13:52:30 -08007902 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007903 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -08007904 "6082 Failed to update nvme-sgl size "
7905 "and mapping: %d\n", rc);
7906 goto out_destroy_queue;
7907 }
James Smart6c621a22017-05-15 15:20:45 -07007908
James Smart0794d602019-01-28 11:14:19 -08007909 /* register the allocated common sgl pool to the port */
James Smart5e5b5112019-01-28 11:14:22 -08007910 rc = lpfc_sli4_repost_io_sgl_list(phba);
James Smart0794d602019-01-28 11:14:19 -08007911 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007912 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -08007913 "6116 Error %d during nvme sgl post "
7914 "operation\n", rc);
7915 /* Some NVME buffers were moved to abort nvme list */
7916 /* A pci function reset will repost them */
7917 rc = -ENODEV;
7918 goto out_destroy_queue;
7919 }
James Smarta5f73372019-09-21 20:58:50 -07007920 /* Each lpfc_io_buf job structure has an iocbq element.
7921 * This cnt provides for abort, els, ct and ls requests.
7922 */
7923 cnt = phba->sli4_hba.max_cfg_param.max_xri;
James Smart11e644e2017-06-15 22:56:48 -07007924 }
7925
7926 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007927 /* Initialize and populate the iocb list per host */
7928 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta5f73372019-09-21 20:58:50 -07007929 "2821 initialize iocb list with %d entries\n",
7930 cnt);
James Smart6c621a22017-05-15 15:20:45 -07007931 rc = lpfc_init_iocb_list(phba, cnt);
7932 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007933 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart11e644e2017-06-15 22:56:48 -07007934 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007935 goto out_destroy_queue;
7936 }
James Smart8a9d2e82012-05-09 21:16:12 -04007937 }
7938
James Smart11e644e2017-06-15 22:56:48 -07007939 if (phba->nvmet_support)
7940 lpfc_nvmet_create_targetport(phba);
7941
James Smart2d7dbc42017-02-12 13:52:35 -08007942 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007943 /* Post initial buffers to all RQs created */
7944 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7945 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7946 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7947 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7948 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007949 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007950 rqbp->buffer_count = 0;
7951
James Smart2d7dbc42017-02-12 13:52:35 -08007952 lpfc_post_rq_buffer(
7953 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7954 phba->sli4_hba.nvmet_mrq_data[i],
James Smart2448e482018-04-09 14:24:24 -07007955 phba->cfg_nvmet_mrq_post, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007956 }
7957 }
7958
James Smartda0436e2009-05-22 14:51:39 -04007959 /* Post the rpi header region to the device. */
7960 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7961 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07007962 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04007963 "0393 Error %d during rpi post operation\n",
7964 rc);
7965 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007966 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007967 }
James Smart97f2ecf2012-03-01 22:35:23 -05007968 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007969
James Smart895427b2017-02-12 13:52:30 -08007970 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007971 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007972 /*
7973 * The FC Port needs to register FCFI (index 0)
7974 */
7975 lpfc_reg_fcfi(phba, mboxq);
7976 mboxq->vport = phba->pport;
7977 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7978 if (rc != MBX_SUCCESS)
7979 goto out_unset_queue;
7980 rc = 0;
7981 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7982 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007983 } else {
7984 /* We are a NVME Target mode with MRQ > 1 */
7985
7986 /* First register the FCFI */
7987 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7988 mboxq->vport = phba->pport;
7989 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7990 if (rc != MBX_SUCCESS)
7991 goto out_unset_queue;
7992 rc = 0;
7993 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7994 &mboxq->u.mqe.un.reg_fcfi_mrq);
7995
7996 /* Next register the MRQs */
7997 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7998 mboxq->vport = phba->pport;
7999 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8000 if (rc != MBX_SUCCESS)
8001 goto out_unset_queue;
8002 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08008003 }
8004 /* Check if the port is configured to be disabled */
8005 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04008006 }
8007
James Smartc4908502019-01-28 11:14:28 -08008008 /* Don't post more new bufs if repost already recovered
8009 * the nvme sgls.
8010 */
8011 if (phba->nvmet_support == 0) {
8012 if (phba->sli4_hba.io_xri_cnt == 0) {
8013 len = lpfc_new_io_buf(
8014 phba, phba->sli4_hba.io_xri_max);
8015 if (len == 0) {
8016 rc = -ENOMEM;
8017 goto out_unset_queue;
8018 }
8019
8020 if (phba->cfg_xri_rebalancing)
8021 lpfc_create_multixri_pools(phba);
8022 }
8023 } else {
8024 phba->cfg_xri_rebalancing = 0;
8025 }
8026
James Smartda0436e2009-05-22 14:51:39 -04008027 /* Allow asynchronous mailbox command to go through */
8028 spin_lock_irq(&phba->hbalock);
8029 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8030 spin_unlock_irq(&phba->hbalock);
8031
8032 /* Post receive buffers to the device */
8033 lpfc_sli4_rb_setup(phba);
8034
James Smartfc2b9892010-02-26 14:15:29 -05008035 /* Reset HBA FCF states after HBA reset */
8036 phba->fcf.fcf_flag = 0;
8037 phba->fcf.current_rec.flag = 0;
8038
James Smartda0436e2009-05-22 14:51:39 -04008039 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04008040 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04008041 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04008042
8043 /* Start heart beat timer */
8044 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04008045 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smarta22d73b2021-01-04 10:02:38 -08008046 phba->hba_flag &= ~(HBA_HBEAT_INP | HBA_HBEAT_TMO);
James Smartda0436e2009-05-22 14:51:39 -04008047 phba->last_completion_time = jiffies;
8048
James Smart32517fc2019-01-28 11:14:33 -08008049 /* start eq_delay heartbeat */
8050 if (phba->cfg_auto_imax)
8051 queue_delayed_work(phba->wq, &phba->eq_delay_work,
8052 msecs_to_jiffies(LPFC_EQ_DELAY_MSECS));
8053
Dick Kennedy317aeb82020-06-30 14:49:59 -07008054 /* start per phba idle_stat_delay heartbeat */
8055 lpfc_init_idle_stat_hb(phba);
8056
James Smartda0436e2009-05-22 14:51:39 -04008057 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04008058 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07008059 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04008060
James Smart75baf692010-06-08 18:31:21 -04008061 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
8062 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
8063 rc = pci_enable_pcie_error_reporting(phba->pcidev);
8064 if (!rc) {
8065 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8066 "2829 This device supports "
8067 "Advanced Error Reporting (AER)\n");
8068 spin_lock_irq(&phba->hbalock);
8069 phba->hba_flag |= HBA_AER_ENABLED;
8070 spin_unlock_irq(&phba->hbalock);
8071 } else {
8072 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8073 "2830 This device does not support "
8074 "Advanced Error Reporting (AER)\n");
8075 phba->cfg_aer_support = 0;
8076 }
James Smart0a96e972011-07-22 18:37:28 -04008077 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04008078 }
8079
James Smartda0436e2009-05-22 14:51:39 -04008080 /*
8081 * The port is ready, set the host's link state to LINK_DOWN
8082 * in preparation for link interrupts.
8083 */
James Smartda0436e2009-05-22 14:51:39 -04008084 spin_lock_irq(&phba->hbalock);
8085 phba->link_state = LPFC_LINK_DOWN;
James Smart1dc5ec22018-10-23 13:41:11 -07008086
8087 /* Check if physical ports are trunked */
8088 if (bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba))
8089 phba->trunk_link.link0.state = LPFC_LINK_DOWN;
8090 if (bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba))
8091 phba->trunk_link.link1.state = LPFC_LINK_DOWN;
8092 if (bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba))
8093 phba->trunk_link.link2.state = LPFC_LINK_DOWN;
8094 if (bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba))
8095 phba->trunk_link.link3.state = LPFC_LINK_DOWN;
James Smartda0436e2009-05-22 14:51:39 -04008096 spin_unlock_irq(&phba->hbalock);
James Smart1dc5ec22018-10-23 13:41:11 -07008097
James Smarte8869f52019-03-12 16:30:18 -07008098 /* Arm the CQs and then EQs on device */
8099 lpfc_sli4_arm_cqeq_intr(phba);
8100
8101 /* Indicate device interrupt mode */
8102 phba->sli4_hba.intr_enable = 1;
8103
James Smart026abb82011-12-13 13:20:45 -05008104 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
8105 (phba->hba_flag & LINK_DISABLED)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008106 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart026abb82011-12-13 13:20:45 -05008107 "3103 Adapter Link is disabled.\n");
8108 lpfc_down_link(phba, mboxq);
8109 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8110 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07008111 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart026abb82011-12-13 13:20:45 -05008112 "3104 Adapter failed to issue "
8113 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
James Smartc4908502019-01-28 11:14:28 -08008114 goto out_io_buff_free;
James Smart026abb82011-12-13 13:20:45 -05008115 }
8116 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05008117 /* don't perform init_link on SLI4 FC port loopback test */
8118 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
8119 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
8120 if (rc)
James Smartc4908502019-01-28 11:14:28 -08008121 goto out_io_buff_free;
James Smart1b511972011-12-13 13:23:09 -05008122 }
James Smart5350d872011-10-10 21:33:49 -04008123 }
8124 mempool_free(mboxq, phba->mbox_mem_pool);
8125 return rc;
James Smartc4908502019-01-28 11:14:28 -08008126out_io_buff_free:
8127 /* Free allocated IO Buffers */
8128 lpfc_io_free(phba);
James Smart76a95d72010-11-20 23:11:48 -05008129out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04008130 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04008131 lpfc_sli4_queue_unset(phba);
8132out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07008133 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04008134 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04008135out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04008136 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04008137out_free_mbox:
8138 mempool_free(mboxq, phba->mbox_mem_pool);
8139 return rc;
8140}
James Smarte59058c2008-08-24 21:49:00 -04008141
8142/**
James Smart3621a712009-04-06 18:47:14 -04008143 * lpfc_mbox_timeout - Timeout call back function for mbox timer
Lee Jones7af29d42020-07-21 17:41:31 +01008144 * @t: Context to fetch pointer to hba structure from.
dea31012005-04-17 16:05:31 -05008145 *
James Smarte59058c2008-08-24 21:49:00 -04008146 * This is the callback function for mailbox timer. The mailbox
8147 * timer is armed when a new mailbox command is issued and the timer
8148 * is deleted when the mailbox complete. The function is called by
8149 * the kernel timer code when a mailbox does not complete within
8150 * expected time. This function wakes up the worker thread to
8151 * process the mailbox timeout and returns. All the processing is
8152 * done by the worker thread function lpfc_mbox_timeout_handler.
8153 **/
dea31012005-04-17 16:05:31 -05008154void
Kees Cookf22eb4d2017-09-06 20:24:26 -07008155lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05008156{
Kees Cookf22eb4d2017-09-06 20:24:26 -07008157 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05008158 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05008159 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05008160
James Smart2e0fef82007-06-17 19:56:36 -05008161 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008162 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05008163 if (!tmo_posted)
8164 phba->pport->work_port_events |= WORKER_MBOX_TMO;
8165 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
8166
James Smart5e9d9b82008-06-14 22:52:53 -04008167 if (!tmo_posted)
8168 lpfc_worker_wake_up(phba);
8169 return;
dea31012005-04-17 16:05:31 -05008170}
8171
James Smarte8d3c3b2013-10-10 12:21:30 -04008172/**
8173 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
8174 * are pending
8175 * @phba: Pointer to HBA context object.
8176 *
8177 * This function checks if any mailbox completions are present on the mailbox
8178 * completion queue.
8179 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04008180static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04008181lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
8182{
8183
8184 uint32_t idx;
8185 struct lpfc_queue *mcq;
8186 struct lpfc_mcqe *mcqe;
8187 bool pending_completions = false;
James Smart7365f6f2018-02-22 08:18:46 -08008188 uint8_t qe_valid;
James Smarte8d3c3b2013-10-10 12:21:30 -04008189
8190 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8191 return false;
8192
8193 /* Check for completions on mailbox completion queue */
8194
8195 mcq = phba->sli4_hba.mbx_cq;
8196 idx = mcq->hba_index;
James Smart7365f6f2018-02-22 08:18:46 -08008197 qe_valid = mcq->qe_valid;
James Smart9afbee32019-03-12 16:30:28 -07008198 while (bf_get_le32(lpfc_cqe_valid,
8199 (struct lpfc_cqe *)lpfc_sli4_qe(mcq, idx)) == qe_valid) {
8200 mcqe = (struct lpfc_mcqe *)(lpfc_sli4_qe(mcq, idx));
James Smarte8d3c3b2013-10-10 12:21:30 -04008201 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
8202 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
8203 pending_completions = true;
8204 break;
8205 }
8206 idx = (idx + 1) % mcq->entry_count;
8207 if (mcq->hba_index == idx)
8208 break;
James Smart7365f6f2018-02-22 08:18:46 -08008209
8210 /* if the index wrapped around, toggle the valid bit */
8211 if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
8212 qe_valid = (qe_valid) ? 0 : 1;
James Smarte8d3c3b2013-10-10 12:21:30 -04008213 }
8214 return pending_completions;
8215
8216}
8217
8218/**
8219 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
8220 * that were missed.
8221 * @phba: Pointer to HBA context object.
8222 *
8223 * For sli4, it is possible to miss an interrupt. As such mbox completions
8224 * maybe missed causing erroneous mailbox timeouts to occur. This function
8225 * checks to see if mbox completions are on the mailbox completion queue
8226 * and will process all the completions associated with the eq for the
8227 * mailbox completion queue.
8228 **/
YueHaibingd7b761b2019-05-31 23:28:41 +08008229static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04008230lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
8231{
James Smartb71413d2018-02-22 08:18:40 -08008232 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04008233 uint32_t eqidx;
8234 struct lpfc_queue *fpeq = NULL;
James Smart657add42019-05-21 17:49:06 -07008235 struct lpfc_queue *eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04008236 bool mbox_pending;
8237
8238 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
8239 return false;
8240
James Smart657add42019-05-21 17:49:06 -07008241 /* Find the EQ associated with the mbox CQ */
8242 if (sli4_hba->hdwq) {
8243 for (eqidx = 0; eqidx < phba->cfg_irq_chann; eqidx++) {
8244 eq = phba->sli4_hba.hba_eq_hdl[eqidx].eq;
Daniel Wagner535fb492019-10-18 18:21:11 +02008245 if (eq && eq->queue_id == sli4_hba->mbx_cq->assoc_qid) {
James Smart657add42019-05-21 17:49:06 -07008246 fpeq = eq;
James Smarte8d3c3b2013-10-10 12:21:30 -04008247 break;
8248 }
James Smart657add42019-05-21 17:49:06 -07008249 }
8250 }
James Smarte8d3c3b2013-10-10 12:21:30 -04008251 if (!fpeq)
8252 return false;
8253
8254 /* Turn off interrupts from this EQ */
8255
James Smartb71413d2018-02-22 08:18:40 -08008256 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04008257
8258 /* Check to see if a mbox completion is pending */
8259
8260 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
8261
8262 /*
8263 * If a mbox completion is pending, process all the events on EQ
8264 * associated with the mbox completion queue (this could include
8265 * mailbox commands, async events, els commands, receive queue data
8266 * and fcp commands)
8267 */
8268
8269 if (mbox_pending)
James Smart32517fc2019-01-28 11:14:33 -08008270 /* process and rearm the EQ */
James Smart93a4d6f2019-11-04 16:57:05 -08008271 lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
James Smart32517fc2019-01-28 11:14:33 -08008272 else
8273 /* Always clear and re-arm the EQ */
8274 sli4_hba->sli4_write_eq_db(phba, fpeq, 0, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04008275
8276 return mbox_pending;
8277
8278}
James Smarte59058c2008-08-24 21:49:00 -04008279
8280/**
James Smart3621a712009-04-06 18:47:14 -04008281 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04008282 * @phba: Pointer to HBA context object.
8283 *
8284 * This function is called from worker thread when a mailbox command times out.
8285 * The caller is not required to hold any locks. This function will reset the
8286 * HBA and recover all the pending commands.
8287 **/
dea31012005-04-17 16:05:31 -05008288void
8289lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
8290{
James Smart2e0fef82007-06-17 19:56:36 -05008291 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04008292 MAILBOX_t *mb = NULL;
8293
James Smart1dcb58e2007-04-25 09:51:30 -04008294 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008295
James Smart9ec58ec2021-01-04 10:02:35 -08008296 /* If the mailbox completed, process the completion */
8297 lpfc_sli4_process_missed_mbox_completions(phba);
8298
8299 if (!(psli->sli_flag & LPFC_SLI_ACTIVE))
James Smarte8d3c3b2013-10-10 12:21:30 -04008300 return;
8301
James Smarteb016562014-09-03 12:58:06 -04008302 if (pmbox != NULL)
8303 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04008304 /* Check the pmbox pointer first. There is a race condition
8305 * between the mbox timeout handler getting executed in the
8306 * worklist and the mailbox actually completing. When this
8307 * race condition occurs, the mbox_active will be NULL.
8308 */
8309 spin_lock_irq(&phba->hbalock);
8310 if (pmbox == NULL) {
8311 lpfc_printf_log(phba, KERN_WARNING,
8312 LOG_MBOX | LOG_SLI,
8313 "0353 Active Mailbox cleared - mailbox timeout "
8314 "exiting\n");
8315 spin_unlock_irq(&phba->hbalock);
8316 return;
8317 }
8318
dea31012005-04-17 16:05:31 -05008319 /* Mbox cmd <mbxCommand> timeout */
Dick Kennedy372c1872020-06-30 14:50:00 -07008320 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart32350662019-08-14 16:57:06 -07008321 "0310 Mailbox command x%x timeout Data: x%x x%x x%px\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008322 mb->mbxCommand,
8323 phba->pport->port_state,
8324 phba->sli.sli_flag,
8325 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04008326 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008327
James Smart1dcb58e2007-04-25 09:51:30 -04008328 /* Setting state unknown so lpfc_sli_abort_iocb_ring
8329 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008330 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04008331 */
James Smart2e0fef82007-06-17 19:56:36 -05008332 spin_lock_irq(&phba->pport->work_port_lock);
8333 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8334 spin_unlock_irq(&phba->pport->work_port_lock);
8335 spin_lock_irq(&phba->hbalock);
8336 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04008337 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008338 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04008339
Dick Kennedy372c1872020-06-30 14:50:00 -07008340 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart76bb24e2007-10-27 13:38:00 -04008341 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04008342
8343 /* Reset the HBA device */
8344 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05008345}
8346
James Smarte59058c2008-08-24 21:49:00 -04008347/**
James Smart3772a992009-05-22 14:50:54 -04008348 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04008349 * @phba: Pointer to HBA context object.
8350 * @pmbox: Pointer to mailbox object.
8351 * @flag: Flag indicating how the mailbox need to be processed.
8352 *
8353 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04008354 * to submit a mailbox command to firmware with SLI-3 interface spec. This
8355 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04008356 * The mailbox command can be submitted in polling mode, in which case
8357 * this function will wait in a polling loop for the completion of the
8358 * mailbox.
8359 * If the mailbox is submitted in no_wait mode (not polling) the
8360 * function will submit the command and returns immediately without waiting
8361 * for the mailbox completion. The no_wait is supported only when HBA
8362 * is in SLI2/SLI3 mode - interrupts are enabled.
8363 * The SLI interface allows only one mailbox pending at a time. If the
8364 * mailbox is issued in polling mode and there is already a mailbox
8365 * pending, then the function will return an error. If the mailbox is issued
8366 * in NO_WAIT mode and there is a mailbox pending already, the function
8367 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
8368 * The sli layer owns the mailbox object until the completion of mailbox
8369 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
8370 * return codes the caller owns the mailbox command after the return of
8371 * the function.
8372 **/
James Smart3772a992009-05-22 14:50:54 -04008373static int
8374lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
8375 uint32_t flag)
dea31012005-04-17 16:05:31 -05008376{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008377 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05008378 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05008379 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05008380 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05008381 int i;
James Smart09372822008-01-11 01:52:54 -05008382 unsigned long timeout;
dea31012005-04-17 16:05:31 -05008383 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04008384 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05008385 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04008386 int processing_queue = 0;
8387
8388 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8389 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04008390 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04008391 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04008392 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8393 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8394 return MBX_SUCCESS;
8395 }
James Smart58da1ff2008-04-07 10:15:56 -04008396 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04008397 pmbox = lpfc_mbox_get(phba);
8398 if (!pmbox) {
8399 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8400 return MBX_SUCCESS;
8401 }
8402 }
dea31012005-04-17 16:05:31 -05008403
James Smarted957682007-06-17 19:56:37 -05008404 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05008405 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05008406 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04008407 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05008408 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05008409 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008410 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04008411 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05008412 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04008413 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05008414 }
8415 }
8416
Linas Vepstas8d63f372007-02-14 14:28:36 -06008417 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04008418 if (unlikely(pci_channel_offline(phba->pcidev))) {
8419 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8420 goto out_not_finished;
8421 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06008422
James Smarta257bf92009-04-06 18:48:10 -04008423 /* If HBA has a deferred error attention, fail the iocb. */
8424 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8425 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8426 goto out_not_finished;
8427 }
8428
dea31012005-04-17 16:05:31 -05008429 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05008430
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008431 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05008432 status = MBX_SUCCESS;
8433
James Smart2e0fef82007-06-17 19:56:36 -05008434 if (phba->link_state == LPFC_HBA_ERROR) {
8435 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05008436
8437 /* Mbox command <mbxCommand> cannot issue */
Dick Kennedy372c1872020-06-30 14:50:00 -07008438 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04008439 "(%d):0311 Mailbox command x%x cannot "
8440 "issue Data: x%x x%x\n",
8441 pmbox->vport ? pmbox->vport->vpi : 0,
8442 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008443 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05008444 }
8445
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008446 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05008447 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
8448 !(hc_copy & HC_MBINT_ENA)) {
8449 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Dick Kennedy372c1872020-06-30 14:50:00 -07008450 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04008451 "(%d):2528 Mailbox command x%x cannot "
8452 "issue Data: x%x x%x\n",
8453 pmbox->vport ? pmbox->vport->vpi : 0,
8454 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05008455 goto out_not_finished;
8456 }
James Smart92908312006-03-07 15:04:13 -05008457 }
8458
dea31012005-04-17 16:05:31 -05008459 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8460 /* Polling for a mbox command when another one is already active
8461 * is not allowed in SLI. Also, the driver must have established
8462 * SLI2 mode to queue and process multiple mbox commands.
8463 */
8464
8465 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05008466 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008467
8468 /* Mbox command <mbxCommand> cannot issue */
Dick Kennedy372c1872020-06-30 14:50:00 -07008469 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04008470 "(%d):2529 Mailbox command x%x "
8471 "cannot issue Data: x%x x%x\n",
8472 pmbox->vport ? pmbox->vport->vpi : 0,
8473 pmbox->u.mb.mbxCommand,
8474 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008475 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008476 }
8477
James Smart3772a992009-05-22 14:50:54 -04008478 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05008479 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008480 /* Mbox command <mbxCommand> cannot issue */
Dick Kennedy372c1872020-06-30 14:50:00 -07008481 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04008482 "(%d):2530 Mailbox command x%x "
8483 "cannot issue Data: x%x x%x\n",
8484 pmbox->vport ? pmbox->vport->vpi : 0,
8485 pmbox->u.mb.mbxCommand,
8486 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008487 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008488 }
8489
dea31012005-04-17 16:05:31 -05008490 /* Another mailbox command is still being processed, queue this
8491 * command to be processed later.
8492 */
8493 lpfc_mbox_put(phba, pmbox);
8494
8495 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05008496 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008497 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05008498 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008499 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07008500 mbx->mbxCommand,
8501 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008502 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008503
8504 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05008505 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008506
James Smart858c9f62007-06-17 19:56:39 -05008507 if (pmbox->vport) {
8508 lpfc_debugfs_disc_trc(pmbox->vport,
8509 LPFC_DISC_TRC_MBOX_VPORT,
8510 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008511 (uint32_t)mbx->mbxCommand,
8512 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008513 }
8514 else {
8515 lpfc_debugfs_disc_trc(phba->pport,
8516 LPFC_DISC_TRC_MBOX,
8517 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008518 (uint32_t)mbx->mbxCommand,
8519 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008520 }
8521
James Smart2e0fef82007-06-17 19:56:36 -05008522 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05008523 }
8524
dea31012005-04-17 16:05:31 -05008525 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8526
8527 /* If we are not polling, we MUST be in SLI2 mode */
8528 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04008529 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008530 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05008531 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008532 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05008533 /* Mbox command <mbxCommand> cannot issue */
Dick Kennedy372c1872020-06-30 14:50:00 -07008534 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -04008535 "(%d):2531 Mailbox command x%x "
8536 "cannot issue Data: x%x x%x\n",
8537 pmbox->vport ? pmbox->vport->vpi : 0,
8538 pmbox->u.mb.mbxCommand,
8539 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04008540 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008541 }
8542 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04008543 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8544 1000);
8545 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05008546 }
8547
8548 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05008549 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008550 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05008551 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008552 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07008553 mbx->mbxCommand,
8554 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05008555 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05008556
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008557 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05008558 if (pmbox->vport) {
8559 lpfc_debugfs_disc_trc(pmbox->vport,
8560 LPFC_DISC_TRC_MBOX_VPORT,
8561 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008562 (uint32_t)mbx->mbxCommand,
8563 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008564 }
8565 else {
8566 lpfc_debugfs_disc_trc(phba->pport,
8567 LPFC_DISC_TRC_MBOX,
8568 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008569 (uint32_t)mbx->mbxCommand,
8570 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05008571 }
8572 }
8573
dea31012005-04-17 16:05:31 -05008574 psli->slistat.mbox_cmd++;
8575 evtctr = psli->slistat.mbox_event;
8576
8577 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008578 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05008579
James Smart3772a992009-05-22 14:50:54 -04008580 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04008581 /* Populate mbox extension offset word. */
8582 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008583 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008584 = (uint8_t *)phba->mbox_ext
8585 - (uint8_t *)phba->mbox;
8586 }
8587
8588 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008589 if (pmbox->in_ext_byte_len && pmbox->ctx_buf) {
8590 lpfc_sli_pcimem_bcopy(pmbox->ctx_buf,
8591 (uint8_t *)phba->mbox_ext,
8592 pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008593 }
8594 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008595 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008596 } else {
James Smart7a470272010-03-15 11:25:20 -04008597 /* Populate mbox extension offset word. */
8598 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008599 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04008600 = MAILBOX_HBA_EXT_OFFSET;
8601
8602 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008603 if (pmbox->in_ext_byte_len && pmbox->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -04008604 lpfc_memcpy_to_slim(phba->MBslimaddr +
8605 MAILBOX_HBA_EXT_OFFSET,
James Smart3e1f0712018-11-29 16:09:29 -08008606 pmbox->ctx_buf, pmbox->in_ext_byte_len);
James Smart7a470272010-03-15 11:25:20 -04008607
James Smart895427b2017-02-12 13:52:30 -08008608 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008609 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08008610 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
8611 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05008612
8613 /* First copy mbox command data to HBA SLIM, skip past first
8614 word */
8615 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008616 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05008617 MAILBOX_CMD_SIZE - sizeof (uint32_t));
8618
8619 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008620 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05008621 to_slim = phba->MBslimaddr;
8622 writel(ldata, to_slim);
8623 readl(to_slim); /* flush */
8624
James Smart895427b2017-02-12 13:52:30 -08008625 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05008626 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04008627 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008628 }
8629
8630 wmb();
dea31012005-04-17 16:05:31 -05008631
8632 switch (flag) {
8633 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05008634 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05008635 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05008636 /* Interrupt board to do it */
8637 writel(CA_MBATT, phba->CAregaddr);
8638 readl(phba->CAregaddr); /* flush */
8639 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05008640 break;
8641
8642 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05008643 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05008644 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05008645 /* Interrupt board to do it */
8646 writel(CA_MBATT, phba->CAregaddr);
8647 readl(phba->CAregaddr); /* flush */
8648
James Smart3772a992009-05-22 14:50:54 -04008649 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008650 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04008651 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008652 word0 = le32_to_cpu(word0);
8653 } else {
8654 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05008655 if (lpfc_readl(phba->MBslimaddr, &word0)) {
8656 spin_unlock_irqrestore(&phba->hbalock,
8657 drvr_flag);
8658 goto out_not_finished;
8659 }
dea31012005-04-17 16:05:31 -05008660 }
8661
8662 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008663 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8664 spin_unlock_irqrestore(&phba->hbalock,
8665 drvr_flag);
8666 goto out_not_finished;
8667 }
James Smarta183a152011-10-10 21:32:43 -04008668 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
8669 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05008670 i = 0;
dea31012005-04-17 16:05:31 -05008671 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05008672 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
8673 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05008674 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05008675 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05008676 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05008677 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05008678 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04008679 goto out_not_finished;
dea31012005-04-17 16:05:31 -05008680 }
8681
8682 /* Check if we took a mbox interrupt while we were
8683 polling */
8684 if (((word0 & OWN_CHIP) != OWN_CHIP)
8685 && (evtctr != psli->slistat.mbox_event))
8686 break;
8687
James Smart09372822008-01-11 01:52:54 -05008688 if (i++ > 10) {
8689 spin_unlock_irqrestore(&phba->hbalock,
8690 drvr_flag);
8691 msleep(1);
8692 spin_lock_irqsave(&phba->hbalock, drvr_flag);
8693 }
dea31012005-04-17 16:05:31 -05008694
James Smart3772a992009-05-22 14:50:54 -04008695 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008696 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04008697 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05008698 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008699 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05008700 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04008701 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05008702 /* Check real SLIM for any errors */
8703 slimword0 = readl(phba->MBslimaddr);
8704 slimmb = (MAILBOX_t *) & slimword0;
8705 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
8706 && slimmb->mbxStatus) {
8707 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04008708 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05008709 word0 = slimword0;
8710 }
8711 }
8712 } else {
8713 /* First copy command data */
8714 word0 = readl(phba->MBslimaddr);
8715 }
8716 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05008717 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
8718 spin_unlock_irqrestore(&phba->hbalock,
8719 drvr_flag);
8720 goto out_not_finished;
8721 }
dea31012005-04-17 16:05:31 -05008722 }
8723
James Smart3772a992009-05-22 14:50:54 -04008724 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05008725 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08008726 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
8727 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008728 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008729 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
James Smart7a470272010-03-15 11:25:20 -04008730 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -08008731 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008732 pmbox->out_ext_byte_len);
8733 }
dea31012005-04-17 16:05:31 -05008734 } else {
8735 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008736 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08008737 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04008738 /* Copy the mailbox extension data */
James Smart3e1f0712018-11-29 16:09:29 -08008739 if (pmbox->out_ext_byte_len && pmbox->ctx_buf) {
8740 lpfc_memcpy_from_slim(
8741 pmbox->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -04008742 phba->MBslimaddr +
8743 MAILBOX_HBA_EXT_OFFSET,
8744 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05008745 }
8746 }
8747
8748 writel(HA_MBATT, phba->HAregaddr);
8749 readl(phba->HAregaddr); /* flush */
8750
8751 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08008752 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05008753 }
8754
James Smart2e0fef82007-06-17 19:56:36 -05008755 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
8756 return status;
James Smart58da1ff2008-04-07 10:15:56 -04008757
8758out_not_finished:
8759 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04008760 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04008761 lpfc_mbox_cmpl_put(phba, pmbox);
8762 }
8763 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05008764}
8765
James Smarte59058c2008-08-24 21:49:00 -04008766/**
James Smartf1126682009-06-10 17:22:44 -04008767 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
8768 * @phba: Pointer to HBA context object.
8769 *
8770 * The function blocks the posting of SLI4 asynchronous mailbox commands from
8771 * the driver internal pending mailbox queue. It will then try to wait out the
8772 * possible outstanding mailbox command before return.
8773 *
8774 * Returns:
8775 * 0 - the outstanding mailbox command completed; otherwise, the wait for
8776 * the outstanding mailbox command timed out.
8777 **/
8778static int
8779lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
8780{
8781 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04008782 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04008783 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04008784
8785 /* Mark the asynchronous mailbox command posting as blocked */
8786 spin_lock_irq(&phba->hbalock);
8787 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04008788 /* Determine how long we might wait for the active mailbox
8789 * command to be gracefully completed by firmware.
8790 */
James Smarta183a152011-10-10 21:32:43 -04008791 if (phba->sli.mbox_active)
8792 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
8793 phba->sli.mbox_active) *
8794 1000) + jiffies;
8795 spin_unlock_irq(&phba->hbalock);
8796
James Smarte8d3c3b2013-10-10 12:21:30 -04008797 /* Make sure the mailbox is really active */
8798 if (timeout)
8799 lpfc_sli4_process_missed_mbox_completions(phba);
8800
James Smartf1126682009-06-10 17:22:44 -04008801 /* Wait for the outstnading mailbox command to complete */
8802 while (phba->sli.mbox_active) {
8803 /* Check active mailbox complete status every 2ms */
8804 msleep(2);
8805 if (time_after(jiffies, timeout)) {
8806 /* Timeout, marked the outstanding cmd not complete */
8807 rc = 1;
8808 break;
8809 }
8810 }
8811
8812 /* Can not cleanly block async mailbox command, fails it */
8813 if (rc) {
8814 spin_lock_irq(&phba->hbalock);
8815 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8816 spin_unlock_irq(&phba->hbalock);
8817 }
8818 return rc;
8819}
8820
8821/**
8822 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
8823 * @phba: Pointer to HBA context object.
8824 *
8825 * The function unblocks and resume posting of SLI4 asynchronous mailbox
8826 * commands from the driver internal pending mailbox queue. It makes sure
8827 * that there is no outstanding mailbox command before resuming posting
8828 * asynchronous mailbox commands. If, for any reason, there is outstanding
8829 * mailbox command, it will try to wait it out before resuming asynchronous
8830 * mailbox command posting.
8831 **/
8832static void
8833lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
8834{
8835 struct lpfc_sli *psli = &phba->sli;
8836
8837 spin_lock_irq(&phba->hbalock);
8838 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8839 /* Asynchronous mailbox posting is not blocked, do nothing */
8840 spin_unlock_irq(&phba->hbalock);
8841 return;
8842 }
8843
8844 /* Outstanding synchronous mailbox command is guaranteed to be done,
8845 * successful or timeout, after timing-out the outstanding mailbox
8846 * command shall always be removed, so just unblock posting async
8847 * mailbox command and resume
8848 */
8849 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8850 spin_unlock_irq(&phba->hbalock);
8851
Colin Ian King291c2542019-12-18 08:43:01 +00008852 /* wake up worker thread to post asynchronous mailbox command */
James Smartf1126682009-06-10 17:22:44 -04008853 lpfc_worker_wake_up(phba);
8854}
8855
8856/**
James Smart2d843ed2012-09-29 11:29:06 -04008857 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8858 * @phba: Pointer to HBA context object.
8859 * @mboxq: Pointer to mailbox object.
8860 *
8861 * The function waits for the bootstrap mailbox register ready bit from
8862 * port for twice the regular mailbox command timeout value.
8863 *
8864 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8865 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8866 **/
8867static int
8868lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8869{
8870 uint32_t db_ready;
8871 unsigned long timeout;
8872 struct lpfc_register bmbx_reg;
8873
8874 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8875 * 1000) + jiffies;
8876
8877 do {
8878 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8879 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8880 if (!db_ready)
James Smarte2ffe4d2019-03-12 16:30:15 -07008881 mdelay(2);
James Smart2d843ed2012-09-29 11:29:06 -04008882
8883 if (time_after(jiffies, timeout))
8884 return MBXERR_ERROR;
8885 } while (!db_ready);
8886
8887 return 0;
8888}
8889
8890/**
James Smartda0436e2009-05-22 14:51:39 -04008891 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8892 * @phba: Pointer to HBA context object.
8893 * @mboxq: Pointer to mailbox object.
8894 *
8895 * The function posts a mailbox to the port. The mailbox is expected
8896 * to be comletely filled in and ready for the port to operate on it.
8897 * This routine executes a synchronous completion operation on the
8898 * mailbox by polling for its completion.
8899 *
8900 * The caller must not be holding any locks when calling this routine.
8901 *
8902 * Returns:
8903 * MBX_SUCCESS - mailbox posted successfully
8904 * Any of the MBX error values.
8905 **/
8906static int
8907lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8908{
8909 int rc = MBX_SUCCESS;
8910 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008911 uint32_t mcqe_status;
8912 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008913 struct lpfc_sli *psli = &phba->sli;
8914 struct lpfc_mqe *mb = &mboxq->u.mqe;
8915 struct lpfc_bmbx_create *mbox_rgn;
8916 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008917
8918 /*
8919 * Only one mailbox can be active to the bootstrap mailbox region
8920 * at a time and there is no queueing provided.
8921 */
8922 spin_lock_irqsave(&phba->hbalock, iflag);
8923 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8924 spin_unlock_irqrestore(&phba->hbalock, iflag);
Dick Kennedy372c1872020-06-30 14:50:00 -07008925 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta183a152011-10-10 21:32:43 -04008926 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008927 "cannot issue Data: x%x x%x\n",
8928 mboxq->vport ? mboxq->vport->vpi : 0,
8929 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008930 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8931 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008932 psli->sli_flag, MBX_POLL);
8933 return MBXERR_ERROR;
8934 }
8935 /* The server grabs the token and owns it until release */
8936 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8937 phba->sli.mbox_active = mboxq;
8938 spin_unlock_irqrestore(&phba->hbalock, iflag);
8939
James Smart2d843ed2012-09-29 11:29:06 -04008940 /* wait for bootstrap mbox register for readyness */
8941 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8942 if (rc)
8943 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008944 /*
8945 * Initialize the bootstrap memory region to avoid stale data areas
8946 * in the mailbox post. Then copy the caller's mailbox contents to
8947 * the bmbx mailbox region.
8948 */
8949 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8950 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
James Smart48f8fdb2018-05-04 20:37:51 -07008951 lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8952 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008953
8954 /* Post the high mailbox dma address to the port and wait for ready. */
8955 dma_address = &phba->sli4_hba.bmbx.dma_address;
8956 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8957
James Smart2d843ed2012-09-29 11:29:06 -04008958 /* wait for bootstrap mbox register for hi-address write done */
8959 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8960 if (rc)
8961 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008962
8963 /* Post the low mailbox dma address to the port. */
8964 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008965
James Smart2d843ed2012-09-29 11:29:06 -04008966 /* wait for bootstrap mbox register for low address write done */
8967 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8968 if (rc)
8969 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008970
8971 /*
8972 * Read the CQ to ensure the mailbox has completed.
8973 * If so, update the mailbox status so that the upper layers
8974 * can complete the request normally.
8975 */
James Smart48f8fdb2018-05-04 20:37:51 -07008976 lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8977 sizeof(struct lpfc_mqe));
James Smartda0436e2009-05-22 14:51:39 -04008978 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
James Smart48f8fdb2018-05-04 20:37:51 -07008979 lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8980 sizeof(struct lpfc_mcqe));
James Smartda0436e2009-05-22 14:51:39 -04008981 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008982 /*
8983 * When the CQE status indicates a failure and the mailbox status
8984 * indicates success then copy the CQE status into the mailbox status
8985 * (and prefix it with x4000).
8986 */
James Smartda0436e2009-05-22 14:51:39 -04008987 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008988 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8989 bf_set(lpfc_mqe_status, mb,
8990 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008991 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008992 } else
8993 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008994
8995 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008996 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008997 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8998 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008999 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
9000 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9001 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009002 bf_get(lpfc_mqe_status, mb),
9003 mb->un.mb_words[0], mb->un.mb_words[1],
9004 mb->un.mb_words[2], mb->un.mb_words[3],
9005 mb->un.mb_words[4], mb->un.mb_words[5],
9006 mb->un.mb_words[6], mb->un.mb_words[7],
9007 mb->un.mb_words[8], mb->un.mb_words[9],
9008 mb->un.mb_words[10], mb->un.mb_words[11],
9009 mb->un.mb_words[12], mboxq->mcqe.word0,
9010 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
9011 mboxq->mcqe.trailer);
9012exit:
9013 /* We are holding the token, no needed for lock when release */
9014 spin_lock_irqsave(&phba->hbalock, iflag);
9015 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9016 phba->sli.mbox_active = NULL;
9017 spin_unlock_irqrestore(&phba->hbalock, iflag);
9018 return rc;
9019}
9020
9021/**
9022 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
9023 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01009024 * @mboxq: Pointer to mailbox object.
James Smartda0436e2009-05-22 14:51:39 -04009025 * @flag: Flag indicating how the mailbox need to be processed.
9026 *
9027 * This function is called by discovery code and HBA management code to submit
9028 * a mailbox command to firmware with SLI-4 interface spec.
9029 *
9030 * Return codes the caller owns the mailbox command after the return of the
9031 * function.
9032 **/
9033static int
9034lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
9035 uint32_t flag)
9036{
9037 struct lpfc_sli *psli = &phba->sli;
9038 unsigned long iflags;
9039 int rc;
9040
James Smartb76f2dc2011-07-22 18:37:42 -04009041 /* dump from issue mailbox command if setup */
9042 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
9043
James Smart8fa38512009-07-19 10:01:03 -04009044 rc = lpfc_mbox_dev_check(phba);
9045 if (unlikely(rc)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009046 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta183a152011-10-10 21:32:43 -04009047 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04009048 "cannot issue Data: x%x x%x\n",
9049 mboxq->vport ? mboxq->vport->vpi : 0,
9050 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009051 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9052 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04009053 psli->sli_flag, flag);
9054 goto out_not_finished;
9055 }
9056
James Smartda0436e2009-05-22 14:51:39 -04009057 /* Detect polling mode and jump to a handler */
9058 if (!phba->sli4_hba.intr_enable) {
9059 if (flag == MBX_POLL)
9060 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
9061 else
9062 rc = -EIO;
9063 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04009064 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04009065 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04009066 "(x%x/x%x) failure: "
9067 "mqe_sta: x%x mcqe_sta: x%x/x%x "
9068 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04009069 mboxq->vport ? mboxq->vport->vpi : 0,
9070 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009071 lpfc_sli_config_mbox_subsys_get(phba,
9072 mboxq),
9073 lpfc_sli_config_mbox_opcode_get(phba,
9074 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04009075 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
9076 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
9077 bf_get(lpfc_mcqe_ext_status,
9078 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04009079 psli->sli_flag, flag);
9080 return rc;
9081 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04009082 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
9083 "(%d):2542 Try to issue mailbox command "
James Smart7365f6f2018-02-22 08:18:46 -08009084 "x%x (x%x/x%x) synchronously ahead of async "
James Smartf1126682009-06-10 17:22:44 -04009085 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04009086 mboxq->vport ? mboxq->vport->vpi : 0,
9087 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009088 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9089 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009090 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04009091 /* Try to block the asynchronous mailbox posting */
9092 rc = lpfc_sli4_async_mbox_block(phba);
9093 if (!rc) {
9094 /* Successfully blocked, now issue sync mbox cmd */
9095 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
9096 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04009097 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04009098 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04009099 "(%d):2597 Sync Mailbox command "
9100 "x%x (x%x/x%x) failure: "
9101 "mqe_sta: x%x mcqe_sta: x%x/x%x "
9102 "Data: x%x x%x\n,",
9103 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04009104 mboxq->u.mb.mbxCommand,
9105 lpfc_sli_config_mbox_subsys_get(phba,
9106 mboxq),
9107 lpfc_sli_config_mbox_opcode_get(phba,
9108 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04009109 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
9110 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
9111 bf_get(lpfc_mcqe_ext_status,
9112 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04009113 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04009114 /* Unblock the async mailbox posting afterward */
9115 lpfc_sli4_async_mbox_unblock(phba);
9116 }
9117 return rc;
James Smartda0436e2009-05-22 14:51:39 -04009118 }
9119
Colin Ian King291c2542019-12-18 08:43:01 +00009120 /* Now, interrupt mode asynchronous mailbox command */
James Smartda0436e2009-05-22 14:51:39 -04009121 rc = lpfc_mbox_cmd_check(phba, mboxq);
9122 if (rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009123 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta183a152011-10-10 21:32:43 -04009124 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04009125 "cannot issue Data: x%x x%x\n",
9126 mboxq->vport ? mboxq->vport->vpi : 0,
9127 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009128 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9129 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009130 psli->sli_flag, flag);
9131 goto out_not_finished;
9132 }
James Smartda0436e2009-05-22 14:51:39 -04009133
9134 /* Put the mailbox command to the driver internal FIFO */
9135 psli->slistat.mbox_busy++;
9136 spin_lock_irqsave(&phba->hbalock, iflags);
9137 lpfc_mbox_put(phba, mboxq);
9138 spin_unlock_irqrestore(&phba->hbalock, iflags);
9139 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
9140 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04009141 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04009142 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
9143 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04009144 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9145 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009146 phba->pport->port_state,
9147 psli->sli_flag, MBX_NOWAIT);
9148 /* Wake up worker thread to transport mailbox command from head */
9149 lpfc_worker_wake_up(phba);
9150
9151 return MBX_BUSY;
9152
9153out_not_finished:
9154 return MBX_NOT_FINISHED;
9155}
9156
9157/**
9158 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
9159 * @phba: Pointer to HBA context object.
9160 *
9161 * This function is called by worker thread to send a mailbox command to
9162 * SLI4 HBA firmware.
9163 *
9164 **/
9165int
9166lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
9167{
9168 struct lpfc_sli *psli = &phba->sli;
9169 LPFC_MBOXQ_t *mboxq;
9170 int rc = MBX_SUCCESS;
9171 unsigned long iflags;
9172 struct lpfc_mqe *mqe;
9173 uint32_t mbx_cmnd;
9174
9175 /* Check interrupt mode before post async mailbox command */
9176 if (unlikely(!phba->sli4_hba.intr_enable))
9177 return MBX_NOT_FINISHED;
9178
9179 /* Check for mailbox command service token */
9180 spin_lock_irqsave(&phba->hbalock, iflags);
9181 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
9182 spin_unlock_irqrestore(&phba->hbalock, iflags);
9183 return MBX_NOT_FINISHED;
9184 }
9185 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
9186 spin_unlock_irqrestore(&phba->hbalock, iflags);
9187 return MBX_NOT_FINISHED;
9188 }
9189 if (unlikely(phba->sli.mbox_active)) {
9190 spin_unlock_irqrestore(&phba->hbalock, iflags);
Dick Kennedy372c1872020-06-30 14:50:00 -07009191 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009192 "0384 There is pending active mailbox cmd\n");
9193 return MBX_NOT_FINISHED;
9194 }
9195 /* Take the mailbox command service token */
9196 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
9197
9198 /* Get the next mailbox command from head of queue */
9199 mboxq = lpfc_mbox_get(phba);
9200
9201 /* If no more mailbox command waiting for post, we're done */
9202 if (!mboxq) {
9203 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9204 spin_unlock_irqrestore(&phba->hbalock, iflags);
9205 return MBX_SUCCESS;
9206 }
9207 phba->sli.mbox_active = mboxq;
9208 spin_unlock_irqrestore(&phba->hbalock, iflags);
9209
9210 /* Check device readiness for posting mailbox command */
9211 rc = lpfc_mbox_dev_check(phba);
9212 if (unlikely(rc))
9213 /* Driver clean routine will clean up pending mailbox */
9214 goto out_not_finished;
9215
9216 /* Prepare the mbox command to be posted */
9217 mqe = &mboxq->u.mqe;
9218 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
9219
9220 /* Start timer for the mbox_tmo and log some mailbox post messages */
9221 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04009222 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04009223
9224 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04009225 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04009226 "x%x x%x\n",
9227 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04009228 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9229 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009230 phba->pport->port_state, psli->sli_flag);
9231
9232 if (mbx_cmnd != MBX_HEARTBEAT) {
9233 if (mboxq->vport) {
9234 lpfc_debugfs_disc_trc(mboxq->vport,
9235 LPFC_DISC_TRC_MBOX_VPORT,
9236 "MBOX Send vport: cmd:x%x mb:x%x x%x",
9237 mbx_cmnd, mqe->un.mb_words[0],
9238 mqe->un.mb_words[1]);
9239 } else {
9240 lpfc_debugfs_disc_trc(phba->pport,
9241 LPFC_DISC_TRC_MBOX,
9242 "MBOX Send: cmd:x%x mb:x%x x%x",
9243 mbx_cmnd, mqe->un.mb_words[0],
9244 mqe->un.mb_words[1]);
9245 }
9246 }
9247 psli->slistat.mbox_cmd++;
9248
9249 /* Post the mailbox command to the port */
9250 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
9251 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009252 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta183a152011-10-10 21:32:43 -04009253 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04009254 "cannot issue Data: x%x x%x\n",
9255 mboxq->vport ? mboxq->vport->vpi : 0,
9256 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04009257 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
9258 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04009259 psli->sli_flag, MBX_NOWAIT);
9260 goto out_not_finished;
9261 }
9262
9263 return rc;
9264
9265out_not_finished:
9266 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05009267 if (phba->sli.mbox_active) {
9268 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
9269 __lpfc_mbox_cmpl_put(phba, mboxq);
9270 /* Release the token */
9271 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9272 phba->sli.mbox_active = NULL;
9273 }
James Smartda0436e2009-05-22 14:51:39 -04009274 spin_unlock_irqrestore(&phba->hbalock, iflags);
9275
9276 return MBX_NOT_FINISHED;
9277}
9278
9279/**
9280 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
9281 * @phba: Pointer to HBA context object.
9282 * @pmbox: Pointer to mailbox object.
9283 * @flag: Flag indicating how the mailbox need to be processed.
9284 *
9285 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
9286 * the API jump table function pointer from the lpfc_hba struct.
9287 *
9288 * Return codes the caller owns the mailbox command after the return of the
9289 * function.
9290 **/
9291int
9292lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
9293{
9294 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
9295}
9296
9297/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009298 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04009299 * @phba: The hba struct for which this call is being executed.
9300 * @dev_grp: The HBA PCI-Device group number.
9301 *
9302 * This routine sets up the mbox interface API function jump table in @phba
9303 * struct.
9304 * Returns: 0 - success, -ENODEV - failure.
9305 **/
9306int
9307lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9308{
9309
9310 switch (dev_grp) {
9311 case LPFC_PCI_DEV_LP:
9312 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
9313 phba->lpfc_sli_handle_slow_ring_event =
9314 lpfc_sli_handle_slow_ring_event_s3;
9315 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
9316 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
9317 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
9318 break;
9319 case LPFC_PCI_DEV_OC:
9320 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
9321 phba->lpfc_sli_handle_slow_ring_event =
9322 lpfc_sli_handle_slow_ring_event_s4;
9323 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
9324 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
9325 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
9326 break;
9327 default:
Dick Kennedy372c1872020-06-30 14:50:00 -07009328 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartda0436e2009-05-22 14:51:39 -04009329 "1420 Invalid HBA PCI-device group: 0x%x\n",
9330 dev_grp);
9331 return -ENODEV;
James Smartda0436e2009-05-22 14:51:39 -04009332 }
9333 return 0;
9334}
9335
9336/**
James Smart3621a712009-04-06 18:47:14 -04009337 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04009338 * @phba: Pointer to HBA context object.
9339 * @pring: Pointer to driver SLI ring object.
9340 * @piocb: Pointer to address of newly added command iocb.
9341 *
James Smart27f3efd2019-10-18 14:18:19 -07009342 * This function is called with hbalock held for SLI3 ports or
9343 * the ring lock held for SLI4 ports to add a command
James Smarte59058c2008-08-24 21:49:00 -04009344 * iocb to the txq when SLI layer cannot submit the command iocb
9345 * to the ring.
9346 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009347void
James Smart92d7f7b2007-06-17 19:56:38 -05009348__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009349 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05009350{
James Smart27f3efd2019-10-18 14:18:19 -07009351 if (phba->sli_rev == LPFC_SLI_REV4)
9352 lockdep_assert_held(&pring->ring_lock);
9353 else
9354 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009355 /* Insert the caller's iocb in the txq tail for later processing. */
9356 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05009357}
9358
James Smarte59058c2008-08-24 21:49:00 -04009359/**
James Smart3621a712009-04-06 18:47:14 -04009360 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04009361 * @phba: Pointer to HBA context object.
9362 * @pring: Pointer to driver SLI ring object.
9363 * @piocb: Pointer to address of newly added command iocb.
9364 *
9365 * This function is called with hbalock held before a new
9366 * iocb is submitted to the firmware. This function checks
9367 * txq to flush the iocbs in txq to Firmware before
9368 * submitting new iocbs to the Firmware.
9369 * If there are iocbs in the txq which need to be submitted
9370 * to firmware, lpfc_sli_next_iocb returns the first element
9371 * of the txq after dequeuing it from txq.
9372 * If there is no iocb in the txq then the function will return
9373 * *piocb and *piocb is set to NULL. Caller needs to check
9374 * *piocb to find if there are more commands in the txq.
9375 **/
dea31012005-04-17 16:05:31 -05009376static struct lpfc_iocbq *
9377lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009378 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05009379{
9380 struct lpfc_iocbq * nextiocb;
9381
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009382 lockdep_assert_held(&phba->hbalock);
9383
dea31012005-04-17 16:05:31 -05009384 nextiocb = lpfc_sli_ringtx_get(phba, pring);
9385 if (!nextiocb) {
9386 nextiocb = *piocb;
9387 *piocb = NULL;
9388 }
9389
9390 return nextiocb;
9391}
9392
James Smarte59058c2008-08-24 21:49:00 -04009393/**
James Smart3772a992009-05-22 14:50:54 -04009394 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009395 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04009396 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04009397 * @piocb: Pointer to command iocb.
9398 * @flag: Flag indicating if this command can be put into txq.
9399 *
James Smart3772a992009-05-22 14:50:54 -04009400 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
9401 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
9402 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
9403 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
9404 * this function allows only iocbs for posting buffers. This function finds
9405 * next available slot in the command ring and posts the command to the
9406 * available slot and writes the port attention register to request HBA start
9407 * processing new iocb. If there is no slot available in the ring and
9408 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
9409 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04009410 *
James Smart3772a992009-05-22 14:50:54 -04009411 * This function is called with hbalock held. The function will return success
9412 * after it successfully submit the iocb to firmware or after adding to the
9413 * txq.
James Smarte59058c2008-08-24 21:49:00 -04009414 **/
James Smart98c9ea52007-10-27 13:37:33 -04009415static int
James Smart3772a992009-05-22 14:50:54 -04009416__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05009417 struct lpfc_iocbq *piocb, uint32_t flag)
9418{
9419 struct lpfc_iocbq *nextiocb;
9420 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08009421 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05009422
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009423 lockdep_assert_held(&phba->hbalock);
9424
James Smart92d7f7b2007-06-17 19:56:38 -05009425 if (piocb->iocb_cmpl && (!piocb->vport) &&
9426 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
9427 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009428 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04009429 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05009430 piocb->iocb.ulpCommand);
9431 dump_stack();
9432 return IOCB_ERROR;
9433 }
9434
9435
Linas Vepstas8d63f372007-02-14 14:28:36 -06009436 /* If the PCI channel is in offline state, do not post iocbs. */
9437 if (unlikely(pci_channel_offline(phba->pcidev)))
9438 return IOCB_ERROR;
9439
James Smarta257bf92009-04-06 18:48:10 -04009440 /* If HBA has a deferred error attention, fail the iocb. */
9441 if (unlikely(phba->hba_flag & DEFER_ERATT))
9442 return IOCB_ERROR;
9443
dea31012005-04-17 16:05:31 -05009444 /*
9445 * We should never get an IOCB if we are in a < LINK_DOWN state
9446 */
James Smart2e0fef82007-06-17 19:56:36 -05009447 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05009448 return IOCB_ERROR;
9449
9450 /*
9451 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04009452 * outstanding event.
dea31012005-04-17 16:05:31 -05009453 */
James Smart0b727fe2007-10-27 13:37:25 -04009454 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05009455 goto iocb_busy;
9456
James Smart2e0fef82007-06-17 19:56:36 -05009457 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05009458 /*
James Smart2680eea2007-04-25 09:52:55 -04009459 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05009460 * can be issued if the link is not up.
9461 */
9462 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04009463 case CMD_GEN_REQUEST64_CR:
9464 case CMD_GEN_REQUEST64_CX:
9465 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
9466 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04009467 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04009468 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
9469 MENLO_TRANSPORT_TYPE))
9470
9471 goto iocb_busy;
9472 break;
dea31012005-04-17 16:05:31 -05009473 case CMD_QUE_RING_BUF_CN:
9474 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05009475 /*
9476 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
9477 * completion, iocb_cmpl MUST be 0.
9478 */
9479 if (piocb->iocb_cmpl)
9480 piocb->iocb_cmpl = NULL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009481 fallthrough;
dea31012005-04-17 16:05:31 -05009482 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04009483 case CMD_CLOSE_XRI_CN:
9484 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05009485 break;
9486 default:
9487 goto iocb_busy;
9488 }
9489
9490 /*
9491 * For FCP commands, we must be in a state where we can process link
9492 * attention events.
9493 */
James Smart895427b2017-02-12 13:52:30 -08009494 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05009495 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05009496 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05009497 }
dea31012005-04-17 16:05:31 -05009498
dea31012005-04-17 16:05:31 -05009499 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
9500 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
9501 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
9502
9503 if (iocb)
9504 lpfc_sli_update_ring(phba, pring);
9505 else
9506 lpfc_sli_update_full_ring(phba, pring);
9507
9508 if (!piocb)
9509 return IOCB_SUCCESS;
9510
9511 goto out_busy;
9512
9513 iocb_busy:
9514 pring->stats.iocb_cmd_delay++;
9515
9516 out_busy:
9517
9518 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05009519 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05009520 return IOCB_SUCCESS;
9521 }
9522
9523 return IOCB_BUSY;
9524}
9525
James Smart3772a992009-05-22 14:50:54 -04009526/**
James Smart4f774512009-05-22 14:52:35 -04009527 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
9528 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01009529 * @piocbq: Pointer to command iocb.
James Smart4f774512009-05-22 14:52:35 -04009530 * @sglq: Pointer to the scatter gather queue object.
9531 *
9532 * This routine converts the bpl or bde that is in the IOCB
9533 * to a sgl list for the sli4 hardware. The physical address
9534 * of the bpl/bde is converted back to a virtual address.
9535 * If the IOCB contains a BPL then the list of BDE's is
9536 * converted to sli4_sge's. If the IOCB contains a single
9537 * BDE then it is converted to a single sli_sge.
9538 * The IOCB is still in cpu endianess so the contents of
9539 * the bpl can be used without byte swapping.
9540 *
9541 * Returns valid XRI = Success, NO_XRI = Failure.
9542**/
9543static uint16_t
9544lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
9545 struct lpfc_sglq *sglq)
9546{
9547 uint16_t xritag = NO_XRI;
9548 struct ulp_bde64 *bpl = NULL;
9549 struct ulp_bde64 bde;
9550 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05009551 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04009552 IOCB_t *icmd;
9553 int numBdes = 0;
9554 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05009555 uint32_t offset = 0; /* accumulated offset in the sg request list */
9556 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04009557
9558 if (!piocbq || !sglq)
9559 return xritag;
9560
9561 sgl = (struct sli4_sge *)sglq->sgl;
9562 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05009563 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
9564 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009565 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
9566 numBdes = icmd->un.genreq64.bdl.bdeSize /
9567 sizeof(struct ulp_bde64);
9568 /* The addrHigh and addrLow fields within the IOCB
9569 * have not been byteswapped yet so there is no
9570 * need to swap them back.
9571 */
James Smart1b511972011-12-13 13:23:09 -05009572 if (piocbq->context3)
9573 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
9574 else
9575 return xritag;
James Smart4f774512009-05-22 14:52:35 -04009576
James Smart1b511972011-12-13 13:23:09 -05009577 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04009578 if (!bpl)
9579 return xritag;
9580
9581 for (i = 0; i < numBdes; i++) {
9582 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05009583 sgl->addr_hi = bpl->addrHigh;
9584 sgl->addr_lo = bpl->addrLow;
9585
James Smart05580562011-05-24 11:40:48 -04009586 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009587 if ((i+1) == numBdes)
9588 bf_set(lpfc_sli4_sge_last, sgl, 1);
9589 else
9590 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05009591 /* swap the size field back to the cpu so we
9592 * can assign it to the sgl.
9593 */
9594 bde.tus.w = le32_to_cpu(bpl->tus.w);
9595 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05009596 /* The offsets in the sgl need to be accumulated
9597 * separately for the request and reply lists.
9598 * The request is always first, the reply follows.
9599 */
9600 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
9601 /* add up the reply sg entries */
9602 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
9603 inbound++;
9604 /* first inbound? reset the offset */
9605 if (inbound == 1)
9606 offset = 0;
9607 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04009608 bf_set(lpfc_sli4_sge_type, sgl,
9609 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05009610 offset += bde.tus.f.bdeSize;
9611 }
James Smart546fc852011-03-11 16:06:29 -05009612 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009613 bpl++;
9614 sgl++;
9615 }
9616 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
9617 /* The addrHigh and addrLow fields of the BDE have not
9618 * been byteswapped yet so they need to be swapped
9619 * before putting them in the sgl.
9620 */
9621 sgl->addr_hi =
9622 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
9623 sgl->addr_lo =
9624 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04009625 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04009626 bf_set(lpfc_sli4_sge_last, sgl, 1);
9627 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05009628 sgl->sge_len =
9629 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04009630 }
9631 return sglq->sli4_xritag;
9632}
9633
9634/**
Lee Jones8514e2f2021-03-03 14:46:18 +00009635 * lpfc_sli4_iocb2wqe - Convert the IOCB to a work queue entry.
James Smart4f774512009-05-22 14:52:35 -04009636 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +01009637 * @iocbq: Pointer to command iocb.
James Smart4f774512009-05-22 14:52:35 -04009638 * @wqe: Pointer to the work queue entry.
9639 *
9640 * This routine converts the iocb command to its Work Queue Entry
9641 * equivalent. The wqe pointer should not have any fields set when
9642 * this routine is called because it will memcpy over them.
9643 * This routine does not set the CQ_ID or the WQEC bits in the
9644 * wqe.
9645 *
9646 * Returns: 0 = Success, IOCB_ERROR = Failure.
9647 **/
9648static int
9649lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
James Smart205e8242018-03-05 12:04:03 -08009650 union lpfc_wqe128 *wqe)
James Smart4f774512009-05-22 14:52:35 -04009651{
James Smart5ffc2662009-11-18 15:39:44 -05009652 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04009653 uint8_t ct = 0;
9654 uint32_t fip;
9655 uint32_t abort_tag;
9656 uint8_t command_type = ELS_COMMAND_NON_FIP;
9657 uint8_t cmnd;
9658 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04009659 uint16_t abrt_iotag;
9660 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04009661 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04009662 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05009663 int numBdes, i;
9664 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04009665 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05009666 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05009667 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04009668
James Smart45ed1192009-10-02 15:17:02 -04009669 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04009670 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04009671 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04009672 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05009673 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04009674 command_type = ELS_COMMAND_FIP;
9675 else
9676 command_type = ELS_COMMAND_NON_FIP;
9677
James Smartb5c53952016-03-31 14:12:30 -07009678 if (phba->fcp_embed_io)
9679 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04009680 /* Some of the fields are in the right position already */
9681 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smarte62245d2019-08-14 16:57:08 -07009682 /* The ct field has moved so reset */
9683 wqe->generic.wqe_com.word7 = 0;
9684 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07009685
9686 abort_tag = (uint32_t) iocbq->iotag;
9687 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04009688 /* words0-2 bpl convert bde */
9689 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05009690 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9691 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04009692 bpl = (struct ulp_bde64 *)
9693 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
9694 if (!bpl)
9695 return IOCB_ERROR;
9696
9697 /* Should already be byte swapped. */
9698 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
9699 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
9700 /* swap the size field back to the cpu so we
9701 * can assign it to the sgl.
9702 */
9703 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05009704 xmit_len = wqe->generic.bde.tus.f.bdeSize;
9705 total_len = 0;
9706 for (i = 0; i < numBdes; i++) {
9707 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
9708 total_len += bde.tus.f.bdeSize;
9709 }
James Smart4f774512009-05-22 14:52:35 -04009710 } else
James Smart5ffc2662009-11-18 15:39:44 -05009711 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04009712
9713 iocbq->iocb.ulpIoTag = iocbq->iotag;
9714 cmnd = iocbq->iocb.ulpCommand;
9715
9716 switch (iocbq->iocb.ulpCommand) {
9717 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04009718 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
9719 ndlp = iocbq->context_un.ndlp;
9720 else
9721 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009722 if (!iocbq->iocb.ulpLe) {
Dick Kennedy372c1872020-06-30 14:50:00 -07009723 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -04009724 "2007 Only Limited Edition cmd Format"
9725 " supported 0x%x\n",
9726 iocbq->iocb.ulpCommand);
9727 return IOCB_ERROR;
9728 }
James Smartff78d8f2011-12-13 13:21:35 -05009729
James Smart5ffc2662009-11-18 15:39:44 -05009730 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009731 /* Els_reguest64 has a TMO */
9732 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
9733 iocbq->iocb.ulpTimeout);
9734 /* Need a VF for word 4 set the vf bit*/
9735 bf_set(els_req64_vf, &wqe->els_req, 0);
9736 /* And a VFID for word 12 */
9737 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04009738 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04009739 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9740 iocbq->iocb.ulpContext);
9741 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
9742 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04009743 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05009744 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05009745 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
9746 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05009747 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9748 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05009749 if_type = bf_get(lpfc_sli_intf_if_type,
9750 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009751 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05009752 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05009753 *pcmd == ELS_CMD_SCR ||
James Smartdf3fe762020-02-10 09:31:55 -08009754 *pcmd == ELS_CMD_RDF ||
James Smartf60cb932019-05-14 14:58:05 -07009755 *pcmd == ELS_CMD_RSCN_XMT ||
James Smart6b5151f2012-01-18 16:24:06 -05009756 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05009757 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05009758 *pcmd == ELS_CMD_PLOGI)) {
9759 bf_set(els_req64_sp, &wqe->els_req, 1);
9760 bf_set(els_req64_sid, &wqe->els_req,
9761 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009762 if ((*pcmd == ELS_CMD_FLOGI) &&
9763 !(phba->fc_topology ==
9764 LPFC_TOPOLOGY_LOOP))
9765 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05009766 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
9767 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04009768 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05009769 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05009770 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
9771 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
9772 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
9773 }
James Smartc8685952009-11-18 15:39:16 -05009774 }
James Smart6d368e52011-05-24 11:44:12 -04009775 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
9776 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009777 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
9778 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
9779 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
9780 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
9781 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9782 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009783 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04009784 break;
James Smart5ffc2662009-11-18 15:39:44 -05009785 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04009786 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
9787 iocbq->iocb.un.ulpWord[3]);
9788 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009789 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05009790 /* The entire sequence is transmitted for this IOCB */
9791 xmit_len = total_len;
9792 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05009793 if (phba->link_flag & LS_LOOPBACK_MODE)
9794 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009795 fallthrough;
James Smart4f774512009-05-22 14:52:35 -04009796 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04009797 /* word3 iocb=io_tag32 wqe=reserved */
9798 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04009799 /* word4 relative_offset memcpy */
9800 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009801 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
9802 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
9803 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
9804 LPFC_WQE_IOD_WRITE);
9805 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
9806 LPFC_WQE_LENLOC_WORD12);
9807 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05009808 wqe->xmit_sequence.xmit_len = xmit_len;
9809 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009810 break;
James Smart4f774512009-05-22 14:52:35 -04009811 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04009812 /* word3 iocb=iotag32 wqe=seq_payload_len */
9813 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009814 /* word4 iocb=rsvd wqe=rsvd */
9815 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
9816 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04009817 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009818 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04009819 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
9820 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
9821 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
9822 LPFC_WQE_LENLOC_WORD3);
9823 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04009824 break;
James Smart4f774512009-05-22 14:52:35 -04009825 case CMD_FCP_IWRITE64_CR:
9826 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04009827 /* word3 iocb=iotag wqe=payload_offset_len */
9828 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009829 bf_set(payload_offset_len, &wqe->fcp_iwrite,
9830 xmit_len + sizeof(struct fcp_rsp));
9831 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
9832 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009833 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9834 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9835 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
9836 iocbq->iocb.ulpFCP2Rcvy);
9837 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
9838 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009839 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
9840 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
9841 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009842 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009843 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009844 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9845 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009846 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9847 if (iocbq->priority) {
9848 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9849 (iocbq->priority << 1));
9850 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009851 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9852 (phba->cfg_XLanePriority << 1));
9853 }
9854 }
James Smartb5c53952016-03-31 14:12:30 -07009855 /* Note, word 10 is already initialized to 0 */
9856
James Smart414abe02018-06-26 08:24:26 -07009857 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9858 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009859 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
9860 else
9861 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
9862
James Smartb5c53952016-03-31 14:12:30 -07009863 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009864 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009865 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009866 struct fcp_cmnd *fcp_cmnd;
9867 uint32_t *ptr;
9868
9869 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009870
9871 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009872 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009873 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9874
9875 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009876 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009877 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009878 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9879 wqe->generic.bde.addrHigh = 0;
9880 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009881
James Smart205e8242018-03-05 12:04:03 -08009882 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
9883 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009884
9885 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009886 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009887 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9888 }
James Smart7851fe22011-07-22 18:36:52 -04009889 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009890 case CMD_FCP_IREAD64_CR:
9891 /* word3 iocb=iotag wqe=payload_offset_len */
9892 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009893 bf_set(payload_offset_len, &wqe->fcp_iread,
9894 xmit_len + sizeof(struct fcp_rsp));
9895 bf_set(cmd_buff_len, &wqe->fcp_iread,
9896 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009897 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9898 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9899 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9900 iocbq->iocb.ulpFCP2Rcvy);
9901 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009902 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009903 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9904 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9905 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009906 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009907 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009908 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9909 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009910 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9911 if (iocbq->priority) {
9912 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9913 (iocbq->priority << 1));
9914 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009915 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9916 (phba->cfg_XLanePriority << 1));
9917 }
9918 }
James Smartb5c53952016-03-31 14:12:30 -07009919 /* Note, word 10 is already initialized to 0 */
9920
James Smart414abe02018-06-26 08:24:26 -07009921 /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
9922 if (phba->cfg_enable_pbde)
James Smart0bc2b7c2018-02-22 08:18:48 -08009923 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
9924 else
9925 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
9926
James Smartb5c53952016-03-31 14:12:30 -07009927 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009928 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009929 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009930 struct fcp_cmnd *fcp_cmnd;
9931 uint32_t *ptr;
9932
9933 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009934
9935 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009936 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009937 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9938
9939 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009940 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009941 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009942 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
9943 wqe->generic.bde.addrHigh = 0;
9944 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -07009945
James Smart205e8242018-03-05 12:04:03 -08009946 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
9947 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -07009948
9949 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -08009950 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -07009951 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9952 }
James Smart7851fe22011-07-22 18:36:52 -04009953 break;
James Smartf1126682009-06-10 17:22:44 -04009954 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009955 /* word3 iocb=iotag wqe=payload_offset_len */
9956 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9957 bf_set(payload_offset_len, &wqe->fcp_icmd,
9958 xmit_len + sizeof(struct fcp_rsp));
9959 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9960 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009961 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009962 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009963 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009964 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9965 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9966 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9967 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9968 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009969 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9970 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009971 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9972 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009973 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9974 if (iocbq->priority) {
9975 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9976 (iocbq->priority << 1));
9977 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009978 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9979 (phba->cfg_XLanePriority << 1));
9980 }
9981 }
James Smartb5c53952016-03-31 14:12:30 -07009982 /* Note, word 10 is already initialized to 0 */
9983
9984 if (phba->fcp_embed_io) {
James Smartc4908502019-01-28 11:14:28 -08009985 struct lpfc_io_buf *lpfc_cmd;
James Smartb5c53952016-03-31 14:12:30 -07009986 struct sli4_sge *sgl;
James Smartb5c53952016-03-31 14:12:30 -07009987 struct fcp_cmnd *fcp_cmnd;
9988 uint32_t *ptr;
9989
9990 /* 128 byte wqe support here */
James Smartb5c53952016-03-31 14:12:30 -07009991
9992 lpfc_cmd = iocbq->context1;
James Smart0794d602019-01-28 11:14:19 -08009993 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smartb5c53952016-03-31 14:12:30 -07009994 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9995
9996 /* Word 0-2 - FCP_CMND */
James Smart205e8242018-03-05 12:04:03 -08009997 wqe->generic.bde.tus.f.bdeFlags =
James Smartb5c53952016-03-31 14:12:30 -07009998 BUFF_TYPE_BDE_IMMED;
James Smart205e8242018-03-05 12:04:03 -08009999 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10000 wqe->generic.bde.addrHigh = 0;
10001 wqe->generic.bde.addrLow = 88; /* Word 22 */
James Smartb5c53952016-03-31 14:12:30 -070010002
James Smart205e8242018-03-05 12:04:03 -080010003 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
10004 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
James Smartb5c53952016-03-31 14:12:30 -070010005
10006 /* Word 22-29 FCP CMND Payload */
James Smart205e8242018-03-05 12:04:03 -080010007 ptr = &wqe->words[22];
James Smartb5c53952016-03-31 14:12:30 -070010008 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10009 }
James Smart7851fe22011-07-22 18:36:52 -040010010 break;
James Smart4f774512009-05-22 14:52:35 -040010011 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -050010012 /* For this command calculate the xmit length of the
10013 * request bde.
10014 */
10015 xmit_len = 0;
10016 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
10017 sizeof(struct ulp_bde64);
10018 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -050010019 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -050010020 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
10021 break;
James Smart63e801c2010-11-20 23:14:19 -050010022 xmit_len += bde.tus.f.bdeSize;
10023 }
James Smartf0d9bcc2010-10-22 11:07:09 -040010024 /* word3 iocb=IO_TAG wqe=request_payload_len */
10025 wqe->gen_req.request_payload_len = xmit_len;
10026 /* word4 iocb=parameter wqe=relative_offset memcpy */
10027 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -040010028 /* word6 context tag copied in memcpy */
10029 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
10030 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
Dick Kennedy372c1872020-06-30 14:50:00 -070010031 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040010032 "2015 Invalid CT %x command 0x%x\n",
10033 ct, iocbq->iocb.ulpCommand);
10034 return IOCB_ERROR;
10035 }
James Smartf0d9bcc2010-10-22 11:07:09 -040010036 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
10037 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
10038 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
10039 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
10040 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
10041 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
10042 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
10043 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -040010044 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -040010045 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -040010046 break;
James Smart4f774512009-05-22 14:52:35 -040010047 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -040010048 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -040010049 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -040010050 /* word3 iocb=iotag32 wqe=response_payload_len */
10051 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -040010052 /* word4 */
10053 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -040010054 /* word5 iocb=rsvd wge=did */
10055 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -040010056 iocbq->iocb.un.xseq64.xmit_els_remoteID);
10057
10058 if_type = bf_get(lpfc_sli_intf_if_type,
10059 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -080010060 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -040010061 if (iocbq->vport->fc_flag & FC_PT2PT) {
10062 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
10063 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
10064 iocbq->vport->fc_myDID);
10065 if (iocbq->vport->fc_myDID == Fabric_DID) {
10066 bf_set(wqe_els_did,
10067 &wqe->xmit_els_rsp.wqe_dest, 0);
10068 }
10069 }
10070 }
James Smartf0d9bcc2010-10-22 11:07:09 -040010071 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
10072 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
10073 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
10074 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -040010075 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -040010076 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -040010077 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -040010078 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -040010079 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
10080 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
10081 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
10082 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
10083 LPFC_WQE_LENLOC_WORD3);
10084 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -040010085 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
10086 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -050010087 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
10088 iocbq->context2)->virt);
10089 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -040010090 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
10091 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -050010092 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -040010093 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
10094 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -050010095 phba->vpi_ids[phba->pport->vpi]);
10096 }
James Smart4f774512009-05-22 14:52:35 -040010097 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -040010098 break;
James Smart4f774512009-05-22 14:52:35 -040010099 case CMD_CLOSE_XRI_CN:
10100 case CMD_ABORT_XRI_CN:
10101 case CMD_ABORT_XRI_CX:
10102 /* words 0-2 memcpy should be 0 rserved */
10103 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -040010104 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
10105 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
10106 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
10107 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
10108 } else
10109 fip = 0;
10110
10111 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -040010112 /*
James Smartdcf2a4e2010-09-29 11:18:53 -040010113 * The link is down, or the command was ELS_FIP
10114 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -040010115 * on the wire.
10116 */
10117 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
10118 else
10119 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
10120 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -040010121 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
10122 wqe->abort_cmd.rsrvd5 = 0;
10123 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -040010124 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
10125 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -040010126 /*
10127 * The abort handler will send us CMD_ABORT_XRI_CN or
10128 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
10129 */
James Smartf0d9bcc2010-10-22 11:07:09 -040010130 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10131 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
10132 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
10133 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -040010134 cmnd = CMD_ABORT_XRI_CX;
10135 command_type = OTHER_COMMAND;
10136 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -040010137 break;
James Smart6669f9b2009-10-02 15:16:45 -040010138 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -050010139 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -050010140 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -040010141 * we re-construct this WQE here based on information in
10142 * iocbq from scratch.
10143 */
James Smartd9f492a2019-08-14 16:57:04 -070010144 memset(wqe, 0, sizeof(*wqe));
James Smart5ffc2662009-11-18 15:39:44 -050010145 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -040010146 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -050010147 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
10148 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -050010149 LPFC_ABTS_UNSOL_INT) {
10150 /* ABTS sent by initiator to CT exchange, the
10151 * RX_ID field will be filled with the newly
10152 * allocated responder XRI.
10153 */
10154 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
10155 iocbq->sli4_xritag);
10156 } else {
10157 /* ABTS sent by responder to CT exchange, the
10158 * RX_ID field will be filled with the responder
10159 * RX_ID from ABTS.
10160 */
10161 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -050010162 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -050010163 }
James Smart6669f9b2009-10-02 15:16:45 -040010164 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
10165 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -050010166
10167 /* Use CT=VPI */
10168 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
10169 ndlp->nlp_DID);
10170 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
10171 iocbq->iocb.ulpContext);
10172 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -040010173 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -050010174 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -040010175 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
10176 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
10177 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -040010178 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
10179 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -050010180 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
10181 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
10182 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
10183 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
10184 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
10185 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
10186 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
10187 }
10188
James Smart7851fe22011-07-22 18:36:52 -040010189 break;
James Smartae9e28f2017-05-15 15:20:51 -070010190 case CMD_SEND_FRAME:
James Smarte62245d2019-08-14 16:57:08 -070010191 bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_SEND_FRAME);
10192 bf_set(wqe_sof, &wqe->generic.wqe_com, 0x2E); /* SOF byte */
10193 bf_set(wqe_eof, &wqe->generic.wqe_com, 0x41); /* EOF byte */
10194 bf_set(wqe_lenloc, &wqe->generic.wqe_com, 1);
10195 bf_set(wqe_xbl, &wqe->generic.wqe_com, 1);
10196 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10197 bf_set(wqe_xc, &wqe->generic.wqe_com, 1);
10198 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, 0xA);
10199 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smartae9e28f2017-05-15 15:20:51 -070010200 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10201 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10202 return 0;
James Smart4f774512009-05-22 14:52:35 -040010203 case CMD_XRI_ABORTED_CX:
10204 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -040010205 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
10206 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
10207 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
10208 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
10209 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070010210 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040010211 "2014 Invalid command 0x%x\n",
10212 iocbq->iocb.ulpCommand);
10213 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -040010214 }
James Smart6d368e52011-05-24 11:44:12 -040010215
James Smart8012cc32012-10-31 14:44:49 -040010216 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
10217 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
10218 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
10219 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
10220 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
10221 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
10222 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
10223 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -040010224 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
10225 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
10226 wqe->generic.wqe_com.abort_tag = abort_tag;
10227 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
10228 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
10229 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
10230 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -040010231 return 0;
10232}
10233
10234/**
James Smart47ff4c52020-11-15 11:26:41 -080010235 * __lpfc_sli_issue_fcp_io_s3 - SLI3 device for sending fcp io iocb
10236 * @phba: Pointer to HBA context object.
10237 * @ring_number: SLI ring number to issue wqe on.
10238 * @piocb: Pointer to command iocb.
10239 * @flag: Flag indicating if this command can be put into txq.
10240 *
10241 * __lpfc_sli_issue_fcp_io_s3 is wrapper function to invoke lockless func to
10242 * send an iocb command to an HBA with SLI-4 interface spec.
10243 *
10244 * This function takes the hbalock before invoking the lockless version.
10245 * The function will return success after it successfully submit the wqe to
10246 * firmware or after adding to the txq.
10247 **/
10248static int
10249__lpfc_sli_issue_fcp_io_s3(struct lpfc_hba *phba, uint32_t ring_number,
10250 struct lpfc_iocbq *piocb, uint32_t flag)
10251{
10252 unsigned long iflags;
10253 int rc;
10254
10255 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartda255e22020-11-15 11:26:42 -080010256 rc = __lpfc_sli_issue_iocb_s3(phba, ring_number, piocb, flag);
James Smart47ff4c52020-11-15 11:26:41 -080010257 spin_unlock_irqrestore(&phba->hbalock, iflags);
10258
10259 return rc;
10260}
10261
10262/**
10263 * __lpfc_sli_issue_fcp_io_s4 - SLI4 device for sending fcp io wqe
10264 * @phba: Pointer to HBA context object.
10265 * @ring_number: SLI ring number to issue wqe on.
10266 * @piocb: Pointer to command iocb.
10267 * @flag: Flag indicating if this command can be put into txq.
10268 *
10269 * __lpfc_sli_issue_fcp_io_s4 is used by other functions in the driver to issue
10270 * an wqe command to an HBA with SLI-4 interface spec.
10271 *
10272 * This function is a lockless version. The function will return success
10273 * after it successfully submit the wqe to firmware or after adding to the
10274 * txq.
10275 **/
10276static int
10277__lpfc_sli_issue_fcp_io_s4(struct lpfc_hba *phba, uint32_t ring_number,
10278 struct lpfc_iocbq *piocb, uint32_t flag)
10279{
James Smart47ff4c52020-11-15 11:26:41 -080010280 int rc;
James Smartda255e22020-11-15 11:26:42 -080010281 struct lpfc_io_buf *lpfc_cmd =
10282 (struct lpfc_io_buf *)piocb->context1;
10283 union lpfc_wqe128 *wqe = &piocb->wqe;
10284 struct sli4_sge *sgl;
James Smart47ff4c52020-11-15 11:26:41 -080010285
James Smartda255e22020-11-15 11:26:42 -080010286 /* 128 byte wqe support here */
10287 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
James Smart47ff4c52020-11-15 11:26:41 -080010288
James Smartda255e22020-11-15 11:26:42 -080010289 if (phba->fcp_embed_io) {
10290 struct fcp_cmnd *fcp_cmnd;
10291 u32 *ptr;
James Smart47ff4c52020-11-15 11:26:41 -080010292
James Smartda255e22020-11-15 11:26:42 -080010293 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart47ff4c52020-11-15 11:26:41 -080010294
James Smartda255e22020-11-15 11:26:42 -080010295 /* Word 0-2 - FCP_CMND */
10296 wqe->generic.bde.tus.f.bdeFlags =
10297 BUFF_TYPE_BDE_IMMED;
10298 wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
10299 wqe->generic.bde.addrHigh = 0;
10300 wqe->generic.bde.addrLow = 88; /* Word 22 */
10301
10302 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
10303 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
10304
10305 /* Word 22-29 FCP CMND Payload */
10306 ptr = &wqe->words[22];
10307 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
10308 } else {
10309 /* Word 0-2 - Inline BDE */
10310 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
10311 wqe->generic.bde.tus.f.bdeSize = sizeof(struct fcp_cmnd);
10312 wqe->generic.bde.addrHigh = sgl->addr_hi;
10313 wqe->generic.bde.addrLow = sgl->addr_lo;
10314
10315 /* Word 10 */
10316 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
10317 bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
10318 }
10319
10320 rc = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, piocb);
James Smart47ff4c52020-11-15 11:26:41 -080010321 return rc;
10322}
10323
10324/**
James Smart4f774512009-05-22 14:52:35 -040010325 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
10326 * @phba: Pointer to HBA context object.
10327 * @ring_number: SLI ring number to issue iocb on.
10328 * @piocb: Pointer to command iocb.
10329 * @flag: Flag indicating if this command can be put into txq.
10330 *
10331 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
10332 * an iocb command to an HBA with SLI-4 interface spec.
10333 *
James Smart27f3efd2019-10-18 14:18:19 -070010334 * This function is called with ringlock held. The function will return success
James Smart4f774512009-05-22 14:52:35 -040010335 * after it successfully submit the iocb to firmware or after adding to the
10336 * txq.
10337 **/
10338static int
10339__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
10340 struct lpfc_iocbq *piocb, uint32_t flag)
10341{
10342 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080010343 union lpfc_wqe128 wqe;
James Smart1ba981f2014-02-20 09:56:45 -050010344 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -080010345 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -040010346
James Smart895427b2017-02-12 13:52:30 -080010347 /* Get the WQ */
10348 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
10349 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc00f62e2019-08-14 16:57:11 -070010350 wq = phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq;
James Smart895427b2017-02-12 13:52:30 -080010351 } else {
10352 wq = phba->sli4_hba.els_wq;
10353 }
10354
10355 /* Get corresponding ring */
10356 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010357
James Smartb5c53952016-03-31 14:12:30 -070010358 /*
10359 * The WQE can be either 64 or 128 bytes,
James Smartb5c53952016-03-31 14:12:30 -070010360 */
James Smartb5c53952016-03-31 14:12:30 -070010361
James Smartcda7fa182019-03-04 15:15:43 -080010362 lockdep_assert_held(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080010363
James Smart4f774512009-05-22 14:52:35 -040010364 if (piocb->sli4_xritag == NO_XRI) {
10365 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -050010366 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -040010367 sglq = NULL;
10368 else {
James Smart0e9bb8d2013-03-01 16:35:12 -050010369 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -040010370 if (!(flag & SLI_IOCB_RET_IOCB)) {
10371 __lpfc_sli_ringtx_put(phba,
10372 pring, piocb);
10373 return IOCB_SUCCESS;
10374 } else {
10375 return IOCB_BUSY;
10376 }
10377 } else {
James Smart895427b2017-02-12 13:52:30 -080010378 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -040010379 if (!sglq) {
10380 if (!(flag & SLI_IOCB_RET_IOCB)) {
10381 __lpfc_sli_ringtx_put(phba,
10382 pring,
10383 piocb);
10384 return IOCB_SUCCESS;
10385 } else
10386 return IOCB_BUSY;
10387 }
10388 }
James Smart4f774512009-05-22 14:52:35 -040010389 }
James Smartda255e22020-11-15 11:26:42 -080010390 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -040010391 /* These IO's already have an XRI and a mapped sgl. */
10392 sglq = NULL;
James Smartda255e22020-11-15 11:26:42 -080010393 }
James Smart2ea259e2017-02-12 13:52:27 -080010394 else {
James Smart6d368e52011-05-24 11:44:12 -040010395 /*
10396 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -040010397 * sglq is on the active list
10398 */
James Smartedccdc12013-01-03 15:43:45 -050010399 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -040010400 if (!sglq)
10401 return IOCB_ERROR;
10402 }
10403
10404 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -040010405 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040010406 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040010407 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -040010408 return IOCB_ERROR;
10409 }
10410
James Smart205e8242018-03-05 12:04:03 -080010411 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
James Smart4f774512009-05-22 14:52:35 -040010412 return IOCB_ERROR;
10413
James Smart205e8242018-03-05 12:04:03 -080010414 if (lpfc_sli4_wq_put(wq, &wqe))
James Smart895427b2017-02-12 13:52:30 -080010415 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -040010416 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
10417
10418 return 0;
10419}
10420
Lee Jones8514e2f2021-03-03 14:46:18 +000010421/*
James Smart47ff4c52020-11-15 11:26:41 -080010422 * lpfc_sli_issue_fcp_io - Wrapper func for issuing fcp i/o
10423 *
10424 * This routine wraps the actual fcp i/o function for issusing WQE for sli-4
10425 * or IOCB for sli-3 function.
10426 * pointer from the lpfc_hba struct.
10427 *
10428 * Return codes:
10429 * IOCB_ERROR - Error
10430 * IOCB_SUCCESS - Success
10431 * IOCB_BUSY - Busy
10432 **/
10433int
10434lpfc_sli_issue_fcp_io(struct lpfc_hba *phba, uint32_t ring_number,
10435 struct lpfc_iocbq *piocb, uint32_t flag)
10436{
10437 return phba->__lpfc_sli_issue_fcp_io(phba, ring_number, piocb, flag);
10438}
10439
Lee Jones7af29d42020-07-21 17:41:31 +010010440/*
James Smart3772a992009-05-22 14:50:54 -040010441 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
10442 *
10443 * This routine wraps the actual lockless version for issusing IOCB function
10444 * pointer from the lpfc_hba struct.
10445 *
10446 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -070010447 * IOCB_ERROR - Error
10448 * IOCB_SUCCESS - Success
10449 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -040010450 **/
James Smart2a9bf3d2010-06-07 15:24:45 -040010451int
James Smart3772a992009-05-22 14:50:54 -040010452__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
10453 struct lpfc_iocbq *piocb, uint32_t flag)
10454{
10455 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10456}
10457
10458/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010459 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -040010460 * @phba: The hba struct for which this call is being executed.
10461 * @dev_grp: The HBA PCI-Device group number.
10462 *
10463 * This routine sets up the SLI interface API function jump table in @phba
10464 * struct.
10465 * Returns: 0 - success, -ENODEV - failure.
10466 **/
10467int
10468lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
10469{
10470
10471 switch (dev_grp) {
10472 case LPFC_PCI_DEV_LP:
10473 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
10474 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
James Smart47ff4c52020-11-15 11:26:41 -080010475 phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s3;
James Smart3772a992009-05-22 14:50:54 -040010476 break;
James Smart4f774512009-05-22 14:52:35 -040010477 case LPFC_PCI_DEV_OC:
10478 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
10479 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
James Smart47ff4c52020-11-15 11:26:41 -080010480 phba->__lpfc_sli_issue_fcp_io = __lpfc_sli_issue_fcp_io_s4;
James Smart4f774512009-05-22 14:52:35 -040010481 break;
James Smart3772a992009-05-22 14:50:54 -040010482 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070010483 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040010484 "1419 Invalid HBA PCI-device group: 0x%x\n",
10485 dev_grp);
10486 return -ENODEV;
James Smart3772a992009-05-22 14:50:54 -040010487 }
10488 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
10489 return 0;
10490}
James Smart92d7f7b2007-06-17 19:56:38 -050010491
James Smarta1efe162015-05-21 13:55:20 -040010492/**
James Smart895427b2017-02-12 13:52:30 -080010493 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -040010494 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -040010495 * @piocb: Pointer to command iocb.
10496 *
James Smart895427b2017-02-12 13:52:30 -080010497 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
10498 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -040010499 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -080010500 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -040010501 */
James Smart895427b2017-02-12 13:52:30 -080010502struct lpfc_sli_ring *
10503lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -040010504{
James Smartc4908502019-01-28 11:14:28 -080010505 struct lpfc_io_buf *lpfc_cmd;
James Smart5e5b5112019-01-28 11:14:22 -080010506
James Smart895427b2017-02-12 13:52:30 -080010507 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smartcdb42be2019-01-28 11:14:21 -080010508 if (unlikely(!phba->sli4_hba.hdwq))
James Smart7370d102019-01-28 11:14:20 -080010509 return NULL;
10510 /*
10511 * for abort iocb hba_wqidx should already
10512 * be setup based on what work queue we used.
10513 */
10514 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartc4908502019-01-28 11:14:28 -080010515 lpfc_cmd = (struct lpfc_io_buf *)piocb->context1;
James Smart1fbf9742019-01-28 11:14:26 -080010516 piocb->hba_wqidx = lpfc_cmd->hdwq_no;
James Smart9bd2bff52014-09-03 12:57:30 -040010517 }
James Smartc00f62e2019-08-14 16:57:11 -070010518 return phba->sli4_hba.hdwq[piocb->hba_wqidx].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080010519 } else {
10520 if (unlikely(!phba->sli4_hba.els_wq))
10521 return NULL;
10522 piocb->hba_wqidx = 0;
10523 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -040010524 }
James Smart9bd2bff52014-09-03 12:57:30 -040010525}
10526
James Smarte59058c2008-08-24 21:49:00 -040010527/**
James Smart3621a712009-04-06 18:47:14 -040010528 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -040010529 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010010530 * @ring_number: Ring number
James Smarte59058c2008-08-24 21:49:00 -040010531 * @piocb: Pointer to command iocb.
10532 * @flag: Flag indicating if this command can be put into txq.
10533 *
10534 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
10535 * function. This function gets the hbalock and calls
10536 * __lpfc_sli_issue_iocb function and will return the error returned
10537 * by __lpfc_sli_issue_iocb function. This wrapper is used by
10538 * functions which do not hold hbalock.
10539 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010540int
James Smart3772a992009-05-22 14:50:54 -040010541lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -050010542 struct lpfc_iocbq *piocb, uint32_t flag)
10543{
James Smart2a76a282012-08-03 12:35:54 -040010544 struct lpfc_sli_ring *pring;
James Smart93a4d6f2019-11-04 16:57:05 -080010545 struct lpfc_queue *eq;
James Smart92d7f7b2007-06-17 19:56:38 -050010546 unsigned long iflags;
James Smart6a828b02019-01-28 11:14:31 -080010547 int rc;
James Smart92d7f7b2007-06-17 19:56:38 -050010548
James Smart7e56aa22012-08-03 12:35:34 -040010549 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart93a4d6f2019-11-04 16:57:05 -080010550 eq = phba->sli4_hba.hdwq[piocb->hba_wqidx].hba_eq;
10551
James Smart895427b2017-02-12 13:52:30 -080010552 pring = lpfc_sli4_calc_ring(phba, piocb);
10553 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010554 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -040010555
James Smart9bd2bff52014-09-03 12:57:30 -040010556 spin_lock_irqsave(&pring->ring_lock, iflags);
10557 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10558 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080010559
10560 lpfc_sli4_poll_eq(eq, LPFC_POLL_FASTPATH);
James Smart7e56aa22012-08-03 12:35:34 -040010561 } else {
10562 /* For now, SLI2/3 will still use hbalock */
10563 spin_lock_irqsave(&phba->hbalock, iflags);
10564 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
10565 spin_unlock_irqrestore(&phba->hbalock, iflags);
10566 }
James Smart92d7f7b2007-06-17 19:56:38 -050010567 return rc;
10568}
10569
James Smarte59058c2008-08-24 21:49:00 -040010570/**
James Smart3621a712009-04-06 18:47:14 -040010571 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010572 * @phba: Pointer to HBA context object.
10573 *
10574 * This function is called while driver attaches with the
10575 * HBA to setup the extra ring. The extra ring is used
10576 * only when driver needs to support target mode functionality
10577 * or IP over FC functionalities.
10578 *
James Smart895427b2017-02-12 13:52:30 -080010579 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -040010580 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010581static int
10582lpfc_extra_ring_setup( struct lpfc_hba *phba)
10583{
10584 struct lpfc_sli *psli;
10585 struct lpfc_sli_ring *pring;
10586
10587 psli = &phba->sli;
10588
10589 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -050010590
10591 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -080010592 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -040010593 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10594 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10595 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10596 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010597
James Smarta4bc3372006-12-02 13:34:16 -050010598 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -080010599 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -050010600
James Smart7e56aa22012-08-03 12:35:34 -040010601 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10602 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10603 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10604 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010605
10606 /* Setup default profile for this ring */
10607 pring->iotag_max = 4096;
10608 pring->num_mask = 1;
10609 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -050010610 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
10611 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010612 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
10613 return 0;
10614}
10615
James Smarte7dab162020-10-20 13:27:12 -070010616static void
10617lpfc_sli_post_recovery_event(struct lpfc_hba *phba,
10618 struct lpfc_nodelist *ndlp)
10619{
10620 unsigned long iflags;
10621 struct lpfc_work_evt *evtp = &ndlp->recovery_evt;
10622
10623 spin_lock_irqsave(&phba->hbalock, iflags);
10624 if (!list_empty(&evtp->evt_listp)) {
10625 spin_unlock_irqrestore(&phba->hbalock, iflags);
10626 return;
10627 }
10628
10629 /* Incrementing the reference count until the queued work is done. */
10630 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
10631 if (!evtp->evt_arg1) {
10632 spin_unlock_irqrestore(&phba->hbalock, iflags);
10633 return;
10634 }
10635 evtp->evt = LPFC_EVT_RECOVER_PORT;
10636 list_add_tail(&evtp->evt_listp, &phba->work_list);
10637 spin_unlock_irqrestore(&phba->hbalock, iflags);
10638
10639 lpfc_worker_wake_up(phba);
10640}
10641
James Smartcb69f7d2011-12-13 13:21:57 -050010642/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
10643 * @phba: Pointer to HBA context object.
10644 * @iocbq: Pointer to iocb object.
10645 *
10646 * The async_event handler calls this routine when it receives
10647 * an ASYNC_STATUS_CN event from the port. The port generates
10648 * this event when an Abort Sequence request to an rport fails
10649 * twice in succession. The abort could be originated by the
10650 * driver or by the port. The ABTS could have been for an ELS
10651 * or FCP IO. The port only generates this event when an ABTS
10652 * fails to complete after one retry.
10653 */
10654static void
10655lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
10656 struct lpfc_iocbq *iocbq)
10657{
10658 struct lpfc_nodelist *ndlp = NULL;
10659 uint16_t rpi = 0, vpi = 0;
10660 struct lpfc_vport *vport = NULL;
10661
10662 /* The rpi in the ulpContext is vport-sensitive. */
10663 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
10664 rpi = iocbq->iocb.ulpContext;
10665
10666 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10667 "3092 Port generated ABTS async event "
10668 "on vpi %d rpi %d status 0x%x\n",
10669 vpi, rpi, iocbq->iocb.ulpStatus);
10670
10671 vport = lpfc_find_vport_by_vpid(phba, vpi);
10672 if (!vport)
10673 goto err_exit;
10674 ndlp = lpfc_findnode_rpi(vport, rpi);
James Smart307e3382020-11-15 11:26:30 -080010675 if (!ndlp)
James Smartcb69f7d2011-12-13 13:21:57 -050010676 goto err_exit;
10677
10678 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
10679 lpfc_sli_abts_recover_port(vport, ndlp);
10680 return;
10681
10682 err_exit:
10683 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10684 "3095 Event Context not found, no "
10685 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
10686 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
10687 vpi, rpi);
10688}
10689
10690/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
10691 * @phba: pointer to HBA context object.
10692 * @ndlp: nodelist pointer for the impacted rport.
10693 * @axri: pointer to the wcqe containing the failed exchange.
10694 *
10695 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
10696 * port. The port generates this event when an abort exchange request to an
10697 * rport fails twice in succession with no reply. The abort could be originated
10698 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
10699 */
10700void
10701lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
10702 struct lpfc_nodelist *ndlp,
10703 struct sli4_wcqe_xri_aborted *axri)
10704{
James Smart5c1db2a2012-03-01 22:34:36 -050010705 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -050010706
James Smart307e3382020-11-15 11:26:30 -080010707 if (!ndlp) {
James Smartcb69f7d2011-12-13 13:21:57 -050010708 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10709 "3115 Node Context not found, driver "
10710 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -050010711 return;
10712 }
10713
James Smartcb69f7d2011-12-13 13:21:57 -050010714 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10715 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -050010716 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -040010717 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -050010718 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -050010719 bf_get(lpfc_wcqe_xa_status, axri),
10720 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -050010721
James Smart5c1db2a2012-03-01 22:34:36 -050010722 /*
10723 * Catch the ABTS protocol failure case. Older OCe FW releases returned
10724 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
10725 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
10726 */
James Smarte3d2b802012-08-14 14:25:43 -040010727 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -050010728 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
10729 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smarte7dab162020-10-20 13:27:12 -070010730 lpfc_sli_post_recovery_event(phba, ndlp);
James Smartcb69f7d2011-12-13 13:21:57 -050010731}
10732
James Smarte59058c2008-08-24 21:49:00 -040010733/**
James Smart3621a712009-04-06 18:47:14 -040010734 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -040010735 * @phba: Pointer to HBA context object.
10736 * @pring: Pointer to driver SLI ring object.
10737 * @iocbq: Pointer to iocb object.
10738 *
10739 * This function is called by the slow ring event handler
10740 * function when there is an ASYNC event iocb in the ring.
10741 * This function is called with no lock held.
10742 * Currently this function handles only temperature related
10743 * ASYNC events. The function decodes the temperature sensor
10744 * event message and posts events for the management applications.
10745 **/
James Smart98c9ea52007-10-27 13:37:33 -040010746static void
James Smart57127f12007-10-27 13:37:05 -040010747lpfc_sli_async_event_handler(struct lpfc_hba * phba,
10748 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
10749{
10750 IOCB_t *icmd;
10751 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -040010752 struct temp_event temp_event_data;
10753 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -040010754 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -040010755
10756 icmd = &iocbq->iocb;
10757 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -040010758
James Smartcb69f7d2011-12-13 13:21:57 -050010759 switch (evt_code) {
10760 case ASYNC_TEMP_WARN:
10761 case ASYNC_TEMP_SAFE:
10762 temp_event_data.data = (uint32_t) icmd->ulpContext;
10763 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
10764 if (evt_code == ASYNC_TEMP_WARN) {
10765 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
Dick Kennedy372c1872020-06-30 14:50:00 -070010766 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcb69f7d2011-12-13 13:21:57 -050010767 "0347 Adapter is very hot, please take "
10768 "corrective action. temperature : %d Celsius\n",
10769 (uint32_t) icmd->ulpContext);
10770 } else {
10771 temp_event_data.event_code = LPFC_NORMAL_TEMP;
Dick Kennedy372c1872020-06-30 14:50:00 -070010772 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcb69f7d2011-12-13 13:21:57 -050010773 "0340 Adapter temperature is OK now. "
10774 "temperature : %d Celsius\n",
10775 (uint32_t) icmd->ulpContext);
10776 }
10777
10778 /* Send temperature change event to applications */
10779 shost = lpfc_shost_from_vport(phba->pport);
10780 fc_host_post_vendor_event(shost, fc_get_event_number(),
10781 sizeof(temp_event_data), (char *) &temp_event_data,
10782 LPFC_NL_VENDOR_ID);
10783 break;
10784 case ASYNC_STATUS_CN:
10785 lpfc_sli_abts_err_handler(phba, iocbq);
10786 break;
10787 default:
James Smarta257bf92009-04-06 18:48:10 -040010788 iocb_w = (uint32_t *) icmd;
Dick Kennedy372c1872020-06-30 14:50:00 -070010789 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart76bb24e2007-10-27 13:38:00 -040010790 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -040010791 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -040010792 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
10793 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
10794 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
10795 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -050010796 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -040010797 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
10798 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
10799 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
10800 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
10801
James Smartcb69f7d2011-12-13 13:21:57 -050010802 break;
James Smart57127f12007-10-27 13:37:05 -040010803 }
James Smart57127f12007-10-27 13:37:05 -040010804}
10805
10806
James Smarte59058c2008-08-24 21:49:00 -040010807/**
James Smart895427b2017-02-12 13:52:30 -080010808 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -040010809 * @phba: Pointer to HBA context object.
10810 *
10811 * lpfc_sli_setup sets up rings of the SLI interface with
10812 * number of iocbs per ring and iotags. This function is
10813 * called while driver attach to the HBA and before the
10814 * interrupts are enabled. So there is no need for locking.
10815 *
10816 * This function always returns 0.
10817 **/
dea31012005-04-17 16:05:31 -050010818int
James Smart895427b2017-02-12 13:52:30 -080010819lpfc_sli4_setup(struct lpfc_hba *phba)
10820{
10821 struct lpfc_sli_ring *pring;
10822
10823 pring = phba->sli4_hba.els_wq->pring;
10824 pring->num_mask = LPFC_MAX_RING_MASK;
10825 pring->prt[0].profile = 0; /* Mask 0 */
10826 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10827 pring->prt[0].type = FC_TYPE_ELS;
10828 pring->prt[0].lpfc_sli_rcv_unsol_event =
10829 lpfc_els_unsol_event;
10830 pring->prt[1].profile = 0; /* Mask 1 */
10831 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10832 pring->prt[1].type = FC_TYPE_ELS;
10833 pring->prt[1].lpfc_sli_rcv_unsol_event =
10834 lpfc_els_unsol_event;
10835 pring->prt[2].profile = 0; /* Mask 2 */
10836 /* NameServer Inquiry */
10837 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
10838 /* NameServer */
10839 pring->prt[2].type = FC_TYPE_CT;
10840 pring->prt[2].lpfc_sli_rcv_unsol_event =
10841 lpfc_ct_unsol_event;
10842 pring->prt[3].profile = 0; /* Mask 3 */
10843 /* NameServer response */
10844 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
10845 /* NameServer */
10846 pring->prt[3].type = FC_TYPE_CT;
10847 pring->prt[3].lpfc_sli_rcv_unsol_event =
10848 lpfc_ct_unsol_event;
10849 return 0;
10850}
10851
10852/**
10853 * lpfc_sli_setup - SLI ring setup function
10854 * @phba: Pointer to HBA context object.
10855 *
10856 * lpfc_sli_setup sets up rings of the SLI interface with
10857 * number of iocbs per ring and iotags. This function is
10858 * called while driver attach to the HBA and before the
10859 * interrupts are enabled. So there is no need for locking.
10860 *
10861 * This function always returns 0. SLI3 only.
10862 **/
10863int
dea31012005-04-17 16:05:31 -050010864lpfc_sli_setup(struct lpfc_hba *phba)
10865{
James Smarted957682007-06-17 19:56:37 -050010866 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -050010867 struct lpfc_sli *psli = &phba->sli;
10868 struct lpfc_sli_ring *pring;
10869
James Smart2a76a282012-08-03 12:35:54 -040010870 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -050010871 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -050010872
James Bottomley604a3e32005-10-29 10:28:33 -050010873 psli->iocbq_lookup = NULL;
10874 psli->iocbq_lookup_len = 0;
10875 psli->last_iotag = 0;
10876
dea31012005-04-17 16:05:31 -050010877 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010878 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010879 switch (i) {
10880 case LPFC_FCP_RING: /* ring 0 - FCP */
10881 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010882 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
10883 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
10884 pring->sli.sli3.numCiocb +=
10885 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
10886 pring->sli.sli3.numRiocb +=
10887 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
10888 pring->sli.sli3.numCiocb +=
10889 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
10890 pring->sli.sli3.numRiocb +=
10891 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
10892 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010893 SLI3_IOCB_CMD_SIZE :
10894 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010895 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010896 SLI3_IOCB_RSP_SIZE :
10897 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010898 pring->iotag_ctr = 0;
10899 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -050010900 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -050010901 pring->fast_iotag = pring->iotag_max;
10902 pring->num_mask = 0;
10903 break;
James Smarta4bc3372006-12-02 13:34:16 -050010904 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -050010905 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010906 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
10907 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
10908 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010909 SLI3_IOCB_CMD_SIZE :
10910 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010911 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010912 SLI3_IOCB_RSP_SIZE :
10913 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -050010914 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -050010915 pring->num_mask = 0;
10916 break;
10917 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
10918 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -040010919 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
10920 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
10921 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010922 SLI3_IOCB_CMD_SIZE :
10923 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -040010924 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -050010925 SLI3_IOCB_RSP_SIZE :
10926 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -050010927 pring->fast_iotag = 0;
10928 pring->iotag_ctr = 0;
10929 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -040010930 pring->lpfc_sli_rcv_async_status =
10931 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -040010932 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -050010933 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -040010934 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
10935 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010936 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010937 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010938 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -040010939 pring->prt[1].rctl = FC_RCTL_ELS_REP;
10940 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -050010941 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010942 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -050010943 pring->prt[2].profile = 0; /* Mask 2 */
10944 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -040010945 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -050010946 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010947 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010948 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010949 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010950 pring->prt[3].profile = 0; /* Mask 3 */
10951 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -040010952 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -050010953 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -040010954 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -050010955 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -050010956 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010957 break;
10958 }
James Smart7e56aa22012-08-03 12:35:34 -040010959 totiocbsize += (pring->sli.sli3.numCiocb *
10960 pring->sli.sli3.sizeCiocb) +
10961 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010962 }
James Smarted957682007-06-17 19:56:37 -050010963 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010964 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010965 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10966 "SLI2 SLIM Data: x%x x%lx\n",
10967 phba->brd_no, totiocbsize,
10968 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010969 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010970 if (phba->cfg_multi_ring_support == 2)
10971 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010972
10973 return 0;
10974}
10975
James Smarte59058c2008-08-24 21:49:00 -040010976/**
James Smart895427b2017-02-12 13:52:30 -080010977 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010978 * @phba: Pointer to HBA context object.
10979 *
James Smart895427b2017-02-12 13:52:30 -080010980 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010981 * ring. This function also initializes ring indices of each ring.
10982 * This function is called during the initialization of the SLI
10983 * interface of an HBA.
10984 * This function is called with no lock held and always returns
10985 * 1.
10986 **/
James Smart895427b2017-02-12 13:52:30 -080010987void
10988lpfc_sli4_queue_init(struct lpfc_hba *phba)
10989{
10990 struct lpfc_sli *psli;
10991 struct lpfc_sli_ring *pring;
10992 int i;
10993
10994 psli = &phba->sli;
10995 spin_lock_irq(&phba->hbalock);
10996 INIT_LIST_HEAD(&psli->mboxq);
10997 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10998 /* Initialize list headers for txq and txcmplq as double linked lists */
James Smartcdb42be2019-01-28 11:14:21 -080010999 for (i = 0; i < phba->cfg_hdw_queue; i++) {
James Smartc00f62e2019-08-14 16:57:11 -070011000 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
James Smart895427b2017-02-12 13:52:30 -080011001 pring->flag = 0;
11002 pring->ringno = LPFC_FCP_RING;
James Smartc4908502019-01-28 11:14:28 -080011003 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011004 INIT_LIST_HEAD(&pring->txq);
11005 INIT_LIST_HEAD(&pring->txcmplq);
11006 INIT_LIST_HEAD(&pring->iocb_continueq);
11007 spin_lock_init(&pring->ring_lock);
11008 }
11009 pring = phba->sli4_hba.els_wq->pring;
11010 pring->flag = 0;
11011 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080011012 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011013 INIT_LIST_HEAD(&pring->txq);
11014 INIT_LIST_HEAD(&pring->txcmplq);
11015 INIT_LIST_HEAD(&pring->iocb_continueq);
11016 spin_lock_init(&pring->ring_lock);
11017
James Smartcdb42be2019-01-28 11:14:21 -080011018 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
James Smart895427b2017-02-12 13:52:30 -080011019 pring = phba->sli4_hba.nvmels_wq->pring;
11020 pring->flag = 0;
11021 pring->ringno = LPFC_ELS_RING;
James Smartc4908502019-01-28 11:14:28 -080011022 pring->txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011023 INIT_LIST_HEAD(&pring->txq);
11024 INIT_LIST_HEAD(&pring->txcmplq);
11025 INIT_LIST_HEAD(&pring->iocb_continueq);
11026 spin_lock_init(&pring->ring_lock);
11027 }
11028
11029 spin_unlock_irq(&phba->hbalock);
11030}
11031
11032/**
11033 * lpfc_sli_queue_init - Queue initialization function
11034 * @phba: Pointer to HBA context object.
11035 *
11036 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
11037 * ring. This function also initializes ring indices of each ring.
11038 * This function is called during the initialization of the SLI
11039 * interface of an HBA.
11040 * This function is called with no lock held and always returns
11041 * 1.
11042 **/
11043void
11044lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050011045{
11046 struct lpfc_sli *psli;
11047 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050011048 int i;
dea31012005-04-17 16:05:31 -050011049
11050 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050011051 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011052 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050011053 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050011054 /* Initialize list headers for txq and txcmplq as double linked lists */
11055 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080011056 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050011057 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040011058 pring->sli.sli3.next_cmdidx = 0;
11059 pring->sli.sli3.local_getidx = 0;
11060 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050011061 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050011062 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050011063 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080011064 pring->flag = 0;
11065 INIT_LIST_HEAD(&pring->txq);
11066 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040011067 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050011068 }
James Smart2e0fef82007-06-17 19:56:36 -050011069 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011070}
11071
James Smarte59058c2008-08-24 21:49:00 -040011072/**
James Smart04c68492009-05-22 14:52:52 -040011073 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
11074 * @phba: Pointer to HBA context object.
11075 *
11076 * This routine flushes the mailbox command subsystem. It will unconditionally
11077 * flush all the mailbox commands in the three possible stages in the mailbox
11078 * command sub-system: pending mailbox command queue; the outstanding mailbox
11079 * command; and completed mailbox command queue. It is caller's responsibility
11080 * to make sure that the driver is in the proper state to flush the mailbox
11081 * command sub-system. Namely, the posting of mailbox commands into the
11082 * pending mailbox command queue from the various clients must be stopped;
11083 * either the HBA is in a state that it will never works on the outstanding
11084 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
11085 * mailbox command has been completed.
11086 **/
11087static void
11088lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
11089{
11090 LIST_HEAD(completions);
11091 struct lpfc_sli *psli = &phba->sli;
11092 LPFC_MBOXQ_t *pmb;
11093 unsigned long iflag;
11094
James Smart523128e2018-09-10 10:30:46 -070011095 /* Disable softirqs, including timers from obtaining phba->hbalock */
11096 local_bh_disable();
11097
James Smart04c68492009-05-22 14:52:52 -040011098 /* Flush all the mailbox commands in the mbox system */
11099 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart523128e2018-09-10 10:30:46 -070011100
James Smart04c68492009-05-22 14:52:52 -040011101 /* The pending mailbox command queue */
11102 list_splice_init(&phba->sli.mboxq, &completions);
11103 /* The outstanding active mailbox command */
11104 if (psli->mbox_active) {
11105 list_add_tail(&psli->mbox_active->list, &completions);
11106 psli->mbox_active = NULL;
11107 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11108 }
11109 /* The completed mailbox command queue */
11110 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
11111 spin_unlock_irqrestore(&phba->hbalock, iflag);
11112
James Smart523128e2018-09-10 10:30:46 -070011113 /* Enable softirqs again, done with phba->hbalock */
11114 local_bh_enable();
11115
James Smart04c68492009-05-22 14:52:52 -040011116 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
11117 while (!list_empty(&completions)) {
11118 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
11119 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
11120 if (pmb->mbox_cmpl)
11121 pmb->mbox_cmpl(phba, pmb);
11122 }
11123}
11124
11125/**
James Smart3621a712009-04-06 18:47:14 -040011126 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040011127 * @vport: Pointer to virtual port object.
11128 *
11129 * lpfc_sli_host_down is called to clean up the resources
11130 * associated with a vport before destroying virtual
11131 * port data structures.
11132 * This function does following operations:
11133 * - Free discovery resources associated with this virtual
11134 * port.
11135 * - Free iocbs associated with this virtual port in
11136 * the txq.
11137 * - Send abort for all iocb commands associated with this
11138 * vport in txcmplq.
11139 *
11140 * This function is called with no lock held and always returns 1.
11141 **/
dea31012005-04-17 16:05:31 -050011142int
James Smart92d7f7b2007-06-17 19:56:38 -050011143lpfc_sli_host_down(struct lpfc_vport *vport)
11144{
James Smart858c9f62007-06-17 19:56:39 -050011145 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050011146 struct lpfc_hba *phba = vport->phba;
11147 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080011148 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050011149 struct lpfc_sli_ring *pring;
11150 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050011151 int i;
11152 unsigned long flags = 0;
11153 uint16_t prev_pring_flag;
11154
11155 lpfc_cleanup_discovery_resources(vport);
11156
11157 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050011158
James Smart895427b2017-02-12 13:52:30 -080011159 /*
11160 * Error everything on the txq since these iocbs
11161 * have not been given to the FW yet.
11162 * Also issue ABTS for everything on the txcmplq
11163 */
11164 if (phba->sli_rev != LPFC_SLI_REV4) {
11165 for (i = 0; i < psli->num_rings; i++) {
11166 pring = &psli->sli3_ring[i];
11167 prev_pring_flag = pring->flag;
11168 /* Only slow rings */
11169 if (pring->ringno == LPFC_ELS_RING) {
11170 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11171 /* Set the lpfc data pending flag */
11172 set_bit(LPFC_DATA_READY, &phba->data_flags);
11173 }
11174 list_for_each_entry_safe(iocb, next_iocb,
11175 &pring->txq, list) {
11176 if (iocb->vport != vport)
11177 continue;
11178 list_move_tail(&iocb->list, &completions);
11179 }
11180 list_for_each_entry_safe(iocb, next_iocb,
11181 &pring->txcmplq, list) {
11182 if (iocb->vport != vport)
11183 continue;
James Smartdb7531d2020-11-15 11:26:44 -080011184 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11185 NULL);
James Smart895427b2017-02-12 13:52:30 -080011186 }
11187 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050011188 }
James Smart895427b2017-02-12 13:52:30 -080011189 } else {
11190 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11191 pring = qp->pring;
11192 if (!pring)
11193 continue;
11194 if (pring == phba->sli4_hba.els_wq->pring) {
11195 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11196 /* Set the lpfc data pending flag */
11197 set_bit(LPFC_DATA_READY, &phba->data_flags);
11198 }
11199 prev_pring_flag = pring->flag;
James Smart65a3df62019-09-21 20:58:48 -070011200 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011201 list_for_each_entry_safe(iocb, next_iocb,
11202 &pring->txq, list) {
11203 if (iocb->vport != vport)
11204 continue;
11205 list_move_tail(&iocb->list, &completions);
11206 }
James Smart65a3df62019-09-21 20:58:48 -070011207 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011208 list_for_each_entry_safe(iocb, next_iocb,
11209 &pring->txcmplq, list) {
11210 if (iocb->vport != vport)
11211 continue;
James Smartdb7531d2020-11-15 11:26:44 -080011212 lpfc_sli_issue_abort_iotag(phba, pring, iocb,
11213 NULL);
James Smart895427b2017-02-12 13:52:30 -080011214 }
11215 pring->flag = prev_pring_flag;
11216 }
James Smart92d7f7b2007-06-17 19:56:38 -050011217 }
James Smart92d7f7b2007-06-17 19:56:38 -050011218 spin_unlock_irqrestore(&phba->hbalock, flags);
11219
James Smarta22d73b2021-01-04 10:02:38 -080011220 /* Make sure HBA is alive */
11221 lpfc_issue_hb_tmo(phba);
11222
James Smarta257bf92009-04-06 18:48:10 -040011223 /* Cancel all the IOCBs from the completions list */
11224 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11225 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050011226 return 1;
11227}
11228
James Smarte59058c2008-08-24 21:49:00 -040011229/**
James Smart3621a712009-04-06 18:47:14 -040011230 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040011231 * @phba: Pointer to HBA context object.
11232 *
11233 * This function cleans up all iocb, buffers, mailbox commands
11234 * while shutting down the HBA. This function is called with no
11235 * lock held and always returns 1.
11236 * This function does the following to cleanup driver resources:
11237 * - Free discovery resources for each virtual port
11238 * - Cleanup any pending fabric iocbs
11239 * - Iterate through the iocb txq and free each entry
11240 * in the list.
11241 * - Free up any buffer posted to the HBA
11242 * - Free mailbox commands in the mailbox queue.
11243 **/
James Smart92d7f7b2007-06-17 19:56:38 -050011244int
James Smart2e0fef82007-06-17 19:56:36 -050011245lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050011246{
James Smart2534ba72007-04-25 09:52:20 -040011247 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050011248 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080011249 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050011250 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050011251 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050011252 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040011253 int i;
11254
11255 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040011256 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050011257
dea31012005-04-17 16:05:31 -050011258 lpfc_hba_down_prep(phba);
11259
James Smart523128e2018-09-10 10:30:46 -070011260 /* Disable softirqs, including timers from obtaining phba->hbalock */
11261 local_bh_disable();
11262
James Smart92d7f7b2007-06-17 19:56:38 -050011263 lpfc_fabric_abort_hba(phba);
11264
James Smart2e0fef82007-06-17 19:56:36 -050011265 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050011266
James Smart895427b2017-02-12 13:52:30 -080011267 /*
11268 * Error everything on the txq since these iocbs
11269 * have not been given to the FW yet.
11270 */
11271 if (phba->sli_rev != LPFC_SLI_REV4) {
11272 for (i = 0; i < psli->num_rings; i++) {
11273 pring = &psli->sli3_ring[i];
11274 /* Only slow rings */
11275 if (pring->ringno == LPFC_ELS_RING) {
11276 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11277 /* Set the lpfc data pending flag */
11278 set_bit(LPFC_DATA_READY, &phba->data_flags);
11279 }
11280 list_splice_init(&pring->txq, &completions);
11281 }
11282 } else {
11283 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11284 pring = qp->pring;
11285 if (!pring)
11286 continue;
James Smart4b0a42b2019-08-14 16:56:42 -070011287 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011288 list_splice_init(&pring->txq, &completions);
James Smart4b0a42b2019-08-14 16:56:42 -070011289 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -080011290 if (pring == phba->sli4_hba.els_wq->pring) {
11291 pring->flag |= LPFC_DEFERRED_RING_EVENT;
11292 /* Set the lpfc data pending flag */
11293 set_bit(LPFC_DATA_READY, &phba->data_flags);
11294 }
11295 }
dea31012005-04-17 16:05:31 -050011296 }
James Smart2e0fef82007-06-17 19:56:36 -050011297 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050011298
James Smarta257bf92009-04-06 18:48:10 -040011299 /* Cancel all the IOCBs from the completions list */
11300 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
11301 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040011302
James Smart0ff10d42008-01-11 01:52:36 -050011303 spin_lock_irqsave(&phba->hbalock, flags);
11304 list_splice_init(&phba->elsbuf, &completions);
11305 phba->elsbuf_cnt = 0;
11306 phba->elsbuf_prev_cnt = 0;
11307 spin_unlock_irqrestore(&phba->hbalock, flags);
11308
11309 while (!list_empty(&completions)) {
11310 list_remove_head(&completions, buf_ptr,
11311 struct lpfc_dmabuf, list);
11312 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
11313 kfree(buf_ptr);
11314 }
11315
James Smart523128e2018-09-10 10:30:46 -070011316 /* Enable softirqs again, done with phba->hbalock */
11317 local_bh_enable();
11318
dea31012005-04-17 16:05:31 -050011319 /* Return any active mbox cmds */
11320 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050011321
James Smartda0436e2009-05-22 14:51:39 -040011322 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050011323 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040011324 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050011325
James Smartda0436e2009-05-22 14:51:39 -040011326 return 1;
11327}
James Smart92d7f7b2007-06-17 19:56:38 -050011328
James Smartda0436e2009-05-22 14:51:39 -040011329/**
James Smart3621a712009-04-06 18:47:14 -040011330 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040011331 * @srcp: Source memory pointer.
11332 * @destp: Destination memory pointer.
11333 * @cnt: Number of words required to be copied.
11334 *
11335 * This function is used for copying data between driver memory
11336 * and the SLI memory. This function also changes the endianness
11337 * of each word if native endianness is different from SLI
11338 * endianness. This function can be called with or without
11339 * lock.
11340 **/
dea31012005-04-17 16:05:31 -050011341void
11342lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
11343{
11344 uint32_t *src = srcp;
11345 uint32_t *dest = destp;
11346 uint32_t ldata;
11347 int i;
11348
11349 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
11350 ldata = *src;
11351 ldata = le32_to_cpu(ldata);
11352 *dest = ldata;
11353 src++;
11354 dest++;
11355 }
11356}
11357
James Smarte59058c2008-08-24 21:49:00 -040011358
11359/**
James Smarta0c87cb2009-07-19 10:01:10 -040011360 * lpfc_sli_bemem_bcopy - SLI memory copy function
11361 * @srcp: Source memory pointer.
11362 * @destp: Destination memory pointer.
11363 * @cnt: Number of words required to be copied.
11364 *
11365 * This function is used for copying data between a data structure
11366 * with big endian representation to local endianness.
11367 * This function can be called with or without lock.
11368 **/
11369void
11370lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
11371{
11372 uint32_t *src = srcp;
11373 uint32_t *dest = destp;
11374 uint32_t ldata;
11375 int i;
11376
11377 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
11378 ldata = *src;
11379 ldata = be32_to_cpu(ldata);
11380 *dest = ldata;
11381 src++;
11382 dest++;
11383 }
11384}
11385
11386/**
James Smart3621a712009-04-06 18:47:14 -040011387 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040011388 * @phba: Pointer to HBA context object.
11389 * @pring: Pointer to driver SLI ring object.
11390 * @mp: Pointer to driver buffer object.
11391 *
11392 * This function is called with no lock held.
11393 * It always return zero after adding the buffer to the postbufq
11394 * buffer list.
11395 **/
dea31012005-04-17 16:05:31 -050011396int
James Smart2e0fef82007-06-17 19:56:36 -050011397lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11398 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050011399{
11400 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
11401 later */
James Smart2e0fef82007-06-17 19:56:36 -050011402 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011403 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050011404 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050011405 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011406 return 0;
11407}
11408
James Smarte59058c2008-08-24 21:49:00 -040011409/**
James Smart3621a712009-04-06 18:47:14 -040011410 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040011411 * @phba: Pointer to HBA context object.
11412 *
11413 * When HBQ is enabled, buffers are searched based on tags. This function
11414 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
11415 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
11416 * does not conflict with tags of buffer posted for unsolicited events.
11417 * The function returns the allocated tag. The function is called with
11418 * no locks held.
11419 **/
James Smart76bb24e2007-10-27 13:38:00 -040011420uint32_t
11421lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
11422{
11423 spin_lock_irq(&phba->hbalock);
11424 phba->buffer_tag_count++;
11425 /*
11426 * Always set the QUE_BUFTAG_BIT to distiguish between
11427 * a tag assigned by HBQ.
11428 */
11429 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
11430 spin_unlock_irq(&phba->hbalock);
11431 return phba->buffer_tag_count;
11432}
11433
James Smarte59058c2008-08-24 21:49:00 -040011434/**
James Smart3621a712009-04-06 18:47:14 -040011435 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040011436 * @phba: Pointer to HBA context object.
11437 * @pring: Pointer to driver SLI ring object.
11438 * @tag: Buffer tag.
11439 *
11440 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
11441 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
11442 * iocb is posted to the response ring with the tag of the buffer.
11443 * This function searches the pring->postbufq list using the tag
11444 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
11445 * iocb. If the buffer is found then lpfc_dmabuf object of the
11446 * buffer is returned to the caller else NULL is returned.
11447 * This function is called with no lock held.
11448 **/
James Smart76bb24e2007-10-27 13:38:00 -040011449struct lpfc_dmabuf *
11450lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11451 uint32_t tag)
11452{
11453 struct lpfc_dmabuf *mp, *next_mp;
11454 struct list_head *slp = &pring->postbufq;
11455
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011456 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040011457 spin_lock_irq(&phba->hbalock);
11458 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
11459 if (mp->buffer_tag == tag) {
11460 list_del_init(&mp->list);
11461 pring->postbufq_cnt--;
11462 spin_unlock_irq(&phba->hbalock);
11463 return mp;
11464 }
11465 }
11466
11467 spin_unlock_irq(&phba->hbalock);
Dick Kennedy372c1872020-06-30 14:50:00 -070011468 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartd7c255b2008-08-24 21:50:00 -040011469 "0402 Cannot find virtual addr for buffer tag on "
James Smart32350662019-08-14 16:57:06 -070011470 "ring %d Data x%lx x%px x%px x%x\n",
James Smart76bb24e2007-10-27 13:38:00 -040011471 pring->ringno, (unsigned long) tag,
11472 slp->next, slp->prev, pring->postbufq_cnt);
11473
11474 return NULL;
11475}
dea31012005-04-17 16:05:31 -050011476
James Smarte59058c2008-08-24 21:49:00 -040011477/**
James Smart3621a712009-04-06 18:47:14 -040011478 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040011479 * @phba: Pointer to HBA context object.
11480 * @pring: Pointer to driver SLI ring object.
11481 * @phys: DMA address of the buffer.
11482 *
11483 * This function searches the buffer list using the dma_address
11484 * of unsolicited event to find the driver's lpfc_dmabuf object
11485 * corresponding to the dma_address. The function returns the
11486 * lpfc_dmabuf object if a buffer is found else it returns NULL.
11487 * This function is called by the ct and els unsolicited event
11488 * handlers to get the buffer associated with the unsolicited
11489 * event.
11490 *
11491 * This function is called with no lock held.
11492 **/
dea31012005-04-17 16:05:31 -050011493struct lpfc_dmabuf *
11494lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11495 dma_addr_t phys)
11496{
11497 struct lpfc_dmabuf *mp, *next_mp;
11498 struct list_head *slp = &pring->postbufq;
11499
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011500 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050011501 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011502 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
11503 if (mp->phys == phys) {
11504 list_del_init(&mp->list);
11505 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050011506 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050011507 return mp;
11508 }
11509 }
11510
James Smart2e0fef82007-06-17 19:56:36 -050011511 spin_unlock_irq(&phba->hbalock);
Dick Kennedy372c1872020-06-30 14:50:00 -070011512 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040011513 "0410 Cannot find virtual addr for mapped buf on "
James Smart32350662019-08-14 16:57:06 -070011514 "ring %d Data x%llx x%px x%px x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011515 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050011516 slp->next, slp->prev, pring->postbufq_cnt);
11517 return NULL;
11518}
11519
James Smarte59058c2008-08-24 21:49:00 -040011520/**
James Smart3621a712009-04-06 18:47:14 -040011521 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040011522 * @phba: Pointer to HBA context object.
11523 * @cmdiocb: Pointer to driver command iocb object.
11524 * @rspiocb: Pointer to driver response iocb object.
11525 *
11526 * This function is the completion handler for the abort iocbs for
11527 * ELS commands. This function is called from the ELS ring event
11528 * handler with no lock held. This function frees memory resources
11529 * associated with the abort iocb.
11530 **/
dea31012005-04-17 16:05:31 -050011531static void
James Smart2e0fef82007-06-17 19:56:36 -050011532lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11533 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050011534{
James Smart2e0fef82007-06-17 19:56:36 -050011535 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040011536 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050011537 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040011538
11539 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050011540
11541 /*
11542 * Assume that the port already completed and returned, or
11543 * will return the iocb. Just Log the message.
11544 */
James Smart2680eea2007-04-25 09:52:55 -040011545 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
11546 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
11547
James Smart2e0fef82007-06-17 19:56:36 -050011548 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040011549 if (phba->sli_rev < LPFC_SLI_REV4) {
James Smartfaa832e2018-07-31 17:23:18 -070011550 if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
11551 irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
11552 irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
11553 spin_unlock_irq(&phba->hbalock);
11554 goto release_iocb;
11555 }
James Smart45ed1192009-10-02 15:17:02 -040011556 if (abort_iotag != 0 &&
11557 abort_iotag <= phba->sli.last_iotag)
11558 abort_iocb =
11559 phba->sli.iocbq_lookup[abort_iotag];
11560 } else
11561 /* For sli4 the abort_tag is the XRI,
11562 * so the abort routine puts the iotag of the iocb
11563 * being aborted in the context field of the abort
11564 * IOCB.
11565 */
11566 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040011567
James Smart2a9bf3d2010-06-07 15:24:45 -040011568 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
James Smart32350662019-08-14 16:57:06 -070011569 "0327 Cannot abort els iocb x%px "
James Smart2a9bf3d2010-06-07 15:24:45 -040011570 "with tag %x context %x, abort status %x, "
11571 "abort code %x\n",
11572 abort_iocb, abort_iotag, abort_context,
11573 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040011574
James Smartff78d8f2011-12-13 13:21:35 -050011575 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040011576 }
James Smartfaa832e2018-07-31 17:23:18 -070011577release_iocb:
James Bottomley604a3e32005-10-29 10:28:33 -050011578 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050011579 return;
11580}
11581
James Smarte59058c2008-08-24 21:49:00 -040011582/**
James Smart3621a712009-04-06 18:47:14 -040011583 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040011584 * @phba: Pointer to HBA context object.
11585 * @cmdiocb: Pointer to driver command iocb object.
11586 * @rspiocb: Pointer to driver response iocb object.
11587 *
11588 * The function is called from SLI ring event handler with no
11589 * lock held. This function is the completion handler for ELS commands
11590 * which are aborted. The function frees memory resources used for
11591 * the aborted ELS commands.
11592 **/
James Smart9dd83f72021-03-01 09:18:11 -080011593void
James Smart92d7f7b2007-06-17 19:56:38 -050011594lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11595 struct lpfc_iocbq *rspiocb)
11596{
11597 IOCB_t *irsp = &rspiocb->iocb;
11598
11599 /* ELS cmd tag <ulpIoTag> completes */
11600 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040011601 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050011602 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011603 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050011604 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart4430f7f2020-11-15 11:26:31 -080011605 lpfc_nlp_put((struct lpfc_nodelist *)cmdiocb->context1);
James Smart858c9f62007-06-17 19:56:39 -050011606 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
11607 lpfc_ct_free_iocb(phba, cmdiocb);
11608 else
11609 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050011610}
11611
James Smarte59058c2008-08-24 21:49:00 -040011612/**
James Smartdb7531d2020-11-15 11:26:44 -080011613 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040011614 * @phba: Pointer to HBA context object.
11615 * @pring: Pointer to driver SLI ring object.
11616 * @cmdiocb: Pointer to driver command iocb object.
James Smartdb7531d2020-11-15 11:26:44 -080011617 * @cmpl: completion function.
James Smarte59058c2008-08-24 21:49:00 -040011618 *
James Smartdb7531d2020-11-15 11:26:44 -080011619 * This function issues an abort iocb for the provided command iocb. In case
11620 * of unloading, the abort iocb will not be issued to commands on the ELS
11621 * ring. Instead, the callback function shall be changed to those commands
11622 * so that nothing happens when them finishes. This function is called with
11623 * hbalock held andno ring_lock held (SLI4). The function returns IOCB_SUCCESS
11624 * when the command iocb is an abort request.
11625 *
James Smarte59058c2008-08-24 21:49:00 -040011626 **/
James Smartdb7531d2020-11-15 11:26:44 -080011627int
11628lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
11629 struct lpfc_iocbq *cmdiocb, void *cmpl)
dea31012005-04-17 16:05:31 -050011630{
James Smart2e0fef82007-06-17 19:56:36 -050011631 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011632 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050011633 IOCB_t *icmd = NULL;
11634 IOCB_t *iabt = NULL;
James Smartdb7531d2020-11-15 11:26:44 -080011635 int retval = IOCB_ERROR;
James Smart7e56aa22012-08-03 12:35:34 -040011636 unsigned long iflags;
James Smartfaa832e2018-07-31 17:23:18 -070011637 struct lpfc_nodelist *ndlp;
James Smart07951072007-04-25 09:51:38 -040011638
James Smart92d7f7b2007-06-17 19:56:38 -050011639 /*
11640 * There are certain command types we don't want to abort. And we
11641 * don't want to abort commands that are already in the process of
11642 * being aborted.
James Smart07951072007-04-25 09:51:38 -040011643 */
11644 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050011645 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050011646 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
James Smart078c68b2021-04-11 18:31:12 -070011647 cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED)
James Smartdb7531d2020-11-15 11:26:44 -080011648 return IOCB_ABORTING;
11649
11650 if (!pring) {
11651 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11652 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11653 else
11654 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11655 return retval;
11656 }
11657
11658 /*
11659 * If we're unloading, don't abort iocb on the ELS ring, but change
11660 * the callback so that nothing happens when it finishes.
11661 */
11662 if ((vport->load_flag & FC_UNLOADING) &&
11663 pring->ringno == LPFC_ELS_RING) {
11664 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
11665 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
11666 else
11667 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
11668 return retval;
11669 }
James Smart07951072007-04-25 09:51:38 -040011670
dea31012005-04-17 16:05:31 -050011671 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050011672 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011673 if (abtsiocbp == NULL)
James Smartdb7531d2020-11-15 11:26:44 -080011674 return IOCB_NORESOURCE;
dea31012005-04-17 16:05:31 -050011675
James Smart07951072007-04-25 09:51:38 -040011676 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050011677 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040011678 */
11679 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
11680
dea31012005-04-17 16:05:31 -050011681 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040011682 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
11683 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040011684 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040011685 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smartdb7531d2020-11-15 11:26:44 -080011686 if (pring->ringno == LPFC_ELS_RING)
11687 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
James Smartfaa832e2018-07-31 17:23:18 -070011688 } else {
James Smartda0436e2009-05-22 14:51:39 -040011689 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
James Smartfaa832e2018-07-31 17:23:18 -070011690 if (pring->ringno == LPFC_ELS_RING) {
11691 ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
11692 iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
11693 }
11694 }
dea31012005-04-17 16:05:31 -050011695 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040011696 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050011697
James Smart5ffc2662009-11-18 15:39:44 -050011698 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011699 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smartdb7531d2020-11-15 11:26:44 -080011700 if (cmdiocb->iocb_flag & LPFC_IO_FCP) {
11701 abtsiocbp->iocb_flag |= LPFC_IO_FCP;
James Smart341af102010-01-26 23:07:37 -050011702 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smartdb7531d2020-11-15 11:26:44 -080011703 }
James Smart9bd2bff52014-09-03 12:57:30 -040011704 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
11705 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011706
James Smart2e0fef82007-06-17 19:56:36 -050011707 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040011708 iabt->ulpCommand = CMD_ABORT_XRI_CN;
11709 else
11710 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
11711
James Smartdb7531d2020-11-15 11:26:44 -080011712 if (cmpl)
11713 abtsiocbp->iocb_cmpl = cmpl;
11714 else
11715 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080011716 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040011717
James Smart7e56aa22012-08-03 12:35:34 -040011718 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011719 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
11720 if (unlikely(pring == NULL))
James Smartdb7531d2020-11-15 11:26:44 -080011721 goto abort_iotag_exit;
James Smart7e56aa22012-08-03 12:35:34 -040011722 /* Note: both hbalock and ring_lock need to be set here */
11723 spin_lock_irqsave(&pring->ring_lock, iflags);
11724 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11725 abtsiocbp, 0);
11726 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11727 } else {
11728 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
11729 abtsiocbp, 0);
11730 }
James Smart07951072007-04-25 09:51:38 -040011731
11732abort_iotag_exit:
James Smartdb7531d2020-11-15 11:26:44 -080011733
11734 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
11735 "0339 Abort xri x%x, original iotag x%x, "
11736 "abort cmd iotag x%x retval x%x\n",
11737 iabt->un.acxri.abortIoTag,
11738 iabt->un.acxri.abortContextTag,
11739 abtsiocbp->iotag, retval);
11740
11741 if (retval) {
11742 cmdiocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11743 __lpfc_sli_release_iocbq(phba, abtsiocbp);
11744 }
11745
James Smart2e0fef82007-06-17 19:56:36 -050011746 /*
11747 * Caller to this routine should check for IOCB_ERROR
11748 * and handle it properly. This routine no longer removes
11749 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040011750 */
James Smart2e0fef82007-06-17 19:56:36 -050011751 return retval;
dea31012005-04-17 16:05:31 -050011752}
11753
James Smarte59058c2008-08-24 21:49:00 -040011754/**
James Smart5af5eee2010-10-22 11:06:38 -040011755 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
11756 * @phba: pointer to lpfc HBA data structure.
11757 *
11758 * This routine will abort all pending and outstanding iocbs to an HBA.
11759 **/
11760void
11761lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
11762{
11763 struct lpfc_sli *psli = &phba->sli;
11764 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080011765 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040011766 int i;
11767
James Smart895427b2017-02-12 13:52:30 -080011768 if (phba->sli_rev != LPFC_SLI_REV4) {
11769 for (i = 0; i < psli->num_rings; i++) {
11770 pring = &psli->sli3_ring[i];
11771 lpfc_sli_abort_iocb_ring(phba, pring);
11772 }
11773 return;
11774 }
11775 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
11776 pring = qp->pring;
11777 if (!pring)
11778 continue;
James Smartdb55fba2014-04-04 13:52:02 -040011779 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040011780 }
11781}
11782
11783/**
James Smart3621a712009-04-06 18:47:14 -040011784 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040011785 * @iocbq: Pointer to driver iocb object.
11786 * @vport: Pointer to driver virtual port object.
11787 * @tgt_id: SCSI ID of the target.
11788 * @lun_id: LUN ID of the scsi device.
11789 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
11790 *
James Smart3621a712009-04-06 18:47:14 -040011791 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040011792 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
11793 * 0 if the filtering criteria is met for the given iocb and will return
11794 * 1 if the filtering criteria is not met.
11795 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
11796 * given iocb is for the SCSI device specified by vport, tgt_id and
11797 * lun_id parameter.
11798 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
11799 * given iocb is for the SCSI target specified by vport and tgt_id
11800 * parameters.
11801 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
11802 * given iocb is for the SCSI host associated with the given vport.
11803 * This function is called with no locks held.
11804 **/
dea31012005-04-17 16:05:31 -050011805static int
James Smart51ef4c22007-08-02 11:10:31 -040011806lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
11807 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011808 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011809{
James Smartc4908502019-01-28 11:14:28 -080011810 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011811 int rc = 1;
11812
James Smart9ec58ec2021-01-04 10:02:35 -080011813 if (!iocbq || iocbq->vport != vport)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011814 return rc;
11815
James Smartb0e83012018-06-26 08:24:29 -070011816 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
11817 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
James Smart51ef4c22007-08-02 11:10:31 -040011818 return rc;
11819
James Smartc4908502019-01-28 11:14:28 -080011820 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011821
James Smart495a7142008-06-14 22:52:59 -040011822 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050011823 return rc;
11824
11825 switch (ctx_cmd) {
11826 case LPFC_CTX_LUN:
James Smartb0e83012018-06-26 08:24:29 -070011827 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011828 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
11829 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050011830 rc = 0;
11831 break;
11832 case LPFC_CTX_TGT:
James Smartb0e83012018-06-26 08:24:29 -070011833 if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
James Smart495a7142008-06-14 22:52:59 -040011834 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011835 rc = 0;
11836 break;
dea31012005-04-17 16:05:31 -050011837 case LPFC_CTX_HOST:
11838 rc = 0;
11839 break;
11840 default:
11841 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011842 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011843 break;
11844 }
11845
11846 return rc;
11847}
11848
James Smarte59058c2008-08-24 21:49:00 -040011849/**
James Smart3621a712009-04-06 18:47:14 -040011850 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011851 * @vport: Pointer to virtual port.
11852 * @tgt_id: SCSI ID of the target.
11853 * @lun_id: LUN ID of the scsi device.
11854 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11855 *
11856 * This function returns number of FCP commands pending for the vport.
11857 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11858 * commands pending on the vport associated with SCSI device specified
11859 * by tgt_id and lun_id parameters.
11860 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11861 * commands pending on the vport associated with SCSI target specified
11862 * by tgt_id parameter.
11863 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11864 * commands pending on the vport.
11865 * This function returns the number of iocbs which satisfy the filter.
11866 * This function is called without any lock held.
11867 **/
dea31012005-04-17 16:05:31 -050011868int
James Smart51ef4c22007-08-02 11:10:31 -040011869lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11870 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011871{
James Smart51ef4c22007-08-02 11:10:31 -040011872 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011873 struct lpfc_iocbq *iocbq;
11874 int sum, i;
dea31012005-04-17 16:05:31 -050011875
Johannes Thumshirn31979002016-07-18 16:06:03 +020011876 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011877 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11878 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011879
James Smart51ef4c22007-08-02 11:10:31 -040011880 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11881 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011882 sum++;
dea31012005-04-17 16:05:31 -050011883 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011884 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011885
dea31012005-04-17 16:05:31 -050011886 return sum;
11887}
11888
James Smarte59058c2008-08-24 21:49:00 -040011889/**
James Smartdb7531d2020-11-15 11:26:44 -080011890 * lpfc_sli4_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
11891 * @phba: Pointer to HBA context object
11892 * @cmdiocb: Pointer to command iocb object.
11893 * @wcqe: pointer to the complete wcqe
11894 *
11895 * This function is called when an aborted FCP iocb completes. This
11896 * function is called by the ring event handler with no lock held.
11897 * This function frees the iocb. It is called for sli-4 adapters.
11898 **/
11899void
11900lpfc_sli4_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11901 struct lpfc_wcqe_complete *wcqe)
11902{
11903 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11904 "3017 ABORT_XRI_CN completing on rpi x%x "
11905 "original iotag x%x, abort cmd iotag x%x "
11906 "status 0x%x, reason 0x%x\n",
11907 cmdiocb->iocb.un.acxri.abortContextTag,
11908 cmdiocb->iocb.un.acxri.abortIoTag,
11909 cmdiocb->iotag,
11910 (bf_get(lpfc_wcqe_c_status, wcqe)
11911 & LPFC_IOCB_STATUS_MASK),
11912 wcqe->parameter);
11913 lpfc_sli_release_iocbq(phba, cmdiocb);
11914}
11915
11916/**
James Smart3621a712009-04-06 18:47:14 -040011917 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011918 * @phba: Pointer to HBA context object
11919 * @cmdiocb: Pointer to command iocb object.
11920 * @rspiocb: Pointer to response iocb object.
11921 *
11922 * This function is called when an aborted FCP iocb completes. This
11923 * function is called by the ring event handler with no lock held.
11924 * This function frees the iocb.
11925 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011926void
James Smart2e0fef82007-06-17 19:56:36 -050011927lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11928 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011929{
James Smartcb69f7d2011-12-13 13:21:57 -050011930 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011931 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011932 "original iotag x%x, abort cmd iotag x%x "
11933 "status 0x%x, reason 0x%x\n",
11934 cmdiocb->iocb.un.acxri.abortContextTag,
11935 cmdiocb->iocb.un.acxri.abortIoTag,
11936 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11937 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011938 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011939 return;
11940}
11941
James Smarte59058c2008-08-24 21:49:00 -040011942/**
James Smart3621a712009-04-06 18:47:14 -040011943 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011944 * @vport: Pointer to virtual port.
James Smarte59058c2008-08-24 21:49:00 -040011945 * @tgt_id: SCSI ID of the target.
11946 * @lun_id: LUN ID of the scsi device.
11947 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11948 *
11949 * This function sends an abort command for every SCSI command
11950 * associated with the given virtual port pending on the ring
11951 * filtered by lpfc_sli_validate_fcp_iocb function.
11952 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11953 * FCP iocbs associated with lun specified by tgt_id and lun_id
11954 * parameters
11955 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11956 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11957 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11958 * FCP iocbs associated with virtual port.
James Smart078c68b2021-04-11 18:31:12 -070011959 * The pring used for SLI3 is sli3_ring[LPFC_FCP_RING], for SLI4
11960 * lpfc_sli4_calc_ring is used.
James Smarte59058c2008-08-24 21:49:00 -040011961 * This function returns number of iocbs it failed to abort.
11962 * This function is called with no locks held.
11963 **/
dea31012005-04-17 16:05:31 -050011964int
James Smart078c68b2021-04-11 18:31:12 -070011965lpfc_sli_abort_iocb(struct lpfc_vport *vport, u16 tgt_id, u64 lun_id,
11966 lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011967{
James Smart51ef4c22007-08-02 11:10:31 -040011968 struct lpfc_hba *phba = vport->phba;
James Smart078c68b2021-04-11 18:31:12 -070011969 struct lpfc_sli_ring *pring = NULL;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011970 struct lpfc_iocbq *iocbq;
dea31012005-04-17 16:05:31 -050011971 int errcnt = 0, ret_val = 0;
James Smartdb7531d2020-11-15 11:26:44 -080011972 unsigned long iflags;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011973 int i;
James Smart078c68b2021-04-11 18:31:12 -070011974 void *fcp_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011975
James Smartb0e83012018-06-26 08:24:29 -070011976 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070011977 if (phba->hba_flag & HBA_IOQ_FLUSH)
James Smartb0e83012018-06-26 08:24:29 -070011978 return errcnt;
11979
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011980 for (i = 1; i <= phba->sli.last_iotag; i++) {
11981 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011982
James Smart51ef4c22007-08-02 11:10:31 -040011983 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011984 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011985 continue;
11986
James Smartdb7531d2020-11-15 11:26:44 -080011987 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart078c68b2021-04-11 18:31:12 -070011988 if (phba->sli_rev == LPFC_SLI_REV3) {
11989 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
11990 fcp_cmpl = lpfc_sli_abort_fcp_cmpl;
11991 } else if (phba->sli_rev == LPFC_SLI_REV4) {
11992 pring = lpfc_sli4_calc_ring(phba, iocbq);
11993 fcp_cmpl = lpfc_sli4_abort_fcp_cmpl;
11994 }
James Smartdb7531d2020-11-15 11:26:44 -080011995 ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocbq,
James Smart078c68b2021-04-11 18:31:12 -070011996 fcp_cmpl);
James Smartdb7531d2020-11-15 11:26:44 -080011997 spin_unlock_irqrestore(&phba->hbalock, iflags);
11998 if (ret_val != IOCB_SUCCESS)
dea31012005-04-17 16:05:31 -050011999 errcnt++;
dea31012005-04-17 16:05:31 -050012000 }
12001
12002 return errcnt;
12003}
12004
James Smarte59058c2008-08-24 21:49:00 -040012005/**
James Smart98912dda2014-04-04 13:52:31 -040012006 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
12007 * @vport: Pointer to virtual port.
12008 * @pring: Pointer to driver SLI ring object.
12009 * @tgt_id: SCSI ID of the target.
12010 * @lun_id: LUN ID of the scsi device.
Lee Jones7af29d42020-07-21 17:41:31 +010012011 * @cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
James Smart98912dda2014-04-04 13:52:31 -040012012 *
12013 * This function sends an abort command for every SCSI command
12014 * associated with the given virtual port pending on the ring
12015 * filtered by lpfc_sli_validate_fcp_iocb function.
12016 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
12017 * FCP iocbs associated with lun specified by tgt_id and lun_id
12018 * parameters
12019 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
12020 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
12021 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
12022 * FCP iocbs associated with virtual port.
12023 * This function returns number of iocbs it aborted .
12024 * This function is called with no locks held right after a taskmgmt
12025 * command is sent.
12026 **/
12027int
12028lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
12029 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
12030{
12031 struct lpfc_hba *phba = vport->phba;
James Smartc4908502019-01-28 11:14:28 -080012032 struct lpfc_io_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040012033 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040012034 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040012035 struct lpfc_iocbq *iocbq;
12036 IOCB_t *icmd;
12037 int sum, i, ret_val;
12038 unsigned long iflags;
James Smartc2017262019-01-28 11:14:37 -080012039 struct lpfc_sli_ring *pring_s4 = NULL;
James Smart98912dda2014-04-04 13:52:31 -040012040
James Smart59c68ea2018-04-09 14:24:25 -070012041 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040012042
12043 /* all I/Os are in process of being flushed */
James Smartc00f62e2019-08-14 16:57:11 -070012044 if (phba->hba_flag & HBA_IOQ_FLUSH) {
James Smart59c68ea2018-04-09 14:24:25 -070012045 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040012046 return 0;
12047 }
12048 sum = 0;
12049
12050 for (i = 1; i <= phba->sli.last_iotag; i++) {
12051 iocbq = phba->sli.iocbq_lookup[i];
12052
12053 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
12054 cmd) != 0)
12055 continue;
12056
James Smartc2017262019-01-28 11:14:37 -080012057 /* Guard against IO completion being called at same time */
12058 lpfc_cmd = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
12059 spin_lock(&lpfc_cmd->buf_lock);
12060
12061 if (!lpfc_cmd->pCmd) {
12062 spin_unlock(&lpfc_cmd->buf_lock);
12063 continue;
12064 }
12065
12066 if (phba->sli_rev == LPFC_SLI_REV4) {
12067 pring_s4 =
James Smartc00f62e2019-08-14 16:57:11 -070012068 phba->sli4_hba.hdwq[iocbq->hba_wqidx].io_wq->pring;
James Smartc2017262019-01-28 11:14:37 -080012069 if (!pring_s4) {
12070 spin_unlock(&lpfc_cmd->buf_lock);
12071 continue;
12072 }
12073 /* Note: both hbalock and ring_lock must be set here */
12074 spin_lock(&pring_s4->ring_lock);
12075 }
12076
James Smart98912dda2014-04-04 13:52:31 -040012077 /*
12078 * If the iocbq is already being aborted, don't take a second
12079 * action, but do count it.
12080 */
James Smartc2017262019-01-28 11:14:37 -080012081 if ((iocbq->iocb_flag & LPFC_DRIVER_ABORTED) ||
12082 !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
12083 if (phba->sli_rev == LPFC_SLI_REV4)
12084 spin_unlock(&pring_s4->ring_lock);
12085 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040012086 continue;
James Smartc2017262019-01-28 11:14:37 -080012087 }
James Smart98912dda2014-04-04 13:52:31 -040012088
12089 /* issue ABTS for this IOCB based on iotag */
12090 abtsiocbq = __lpfc_sli_get_iocbq(phba);
James Smartc2017262019-01-28 11:14:37 -080012091 if (!abtsiocbq) {
12092 if (phba->sli_rev == LPFC_SLI_REV4)
12093 spin_unlock(&pring_s4->ring_lock);
12094 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040012095 continue;
James Smartc2017262019-01-28 11:14:37 -080012096 }
James Smart98912dda2014-04-04 13:52:31 -040012097
12098 icmd = &iocbq->iocb;
12099 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
12100 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
12101 if (phba->sli_rev == LPFC_SLI_REV4)
12102 abtsiocbq->iocb.un.acxri.abortIoTag =
12103 iocbq->sli4_xritag;
12104 else
12105 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
12106 abtsiocbq->iocb.ulpLe = 1;
12107 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
12108 abtsiocbq->vport = vport;
12109
12110 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080012111 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040012112 if (iocbq->iocb_flag & LPFC_IO_FCP)
12113 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040012114 if (iocbq->iocb_flag & LPFC_IO_FOF)
12115 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040012116
James Smart8c50d252014-09-03 12:58:16 -040012117 ndlp = lpfc_cmd->rdata->pnode;
12118
12119 if (lpfc_is_link_up(phba) &&
12120 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040012121 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
12122 else
12123 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
12124
12125 /* Setup callback routine and issue the command. */
12126 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
12127
12128 /*
12129 * Indicate the IO is being aborted by the driver and set
12130 * the caller's flag into the aborted IO.
12131 */
12132 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
12133
12134 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart98912dda2014-04-04 13:52:31 -040012135 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
12136 abtsiocbq, 0);
James Smart59c68ea2018-04-09 14:24:25 -070012137 spin_unlock(&pring_s4->ring_lock);
James Smart98912dda2014-04-04 13:52:31 -040012138 } else {
12139 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
12140 abtsiocbq, 0);
12141 }
12142
James Smartc2017262019-01-28 11:14:37 -080012143 spin_unlock(&lpfc_cmd->buf_lock);
James Smart98912dda2014-04-04 13:52:31 -040012144
12145 if (ret_val == IOCB_ERROR)
12146 __lpfc_sli_release_iocbq(phba, abtsiocbq);
12147 else
12148 sum++;
12149 }
James Smart59c68ea2018-04-09 14:24:25 -070012150 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart98912dda2014-04-04 13:52:31 -040012151 return sum;
12152}
12153
12154/**
James Smart3621a712009-04-06 18:47:14 -040012155 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040012156 * @phba: Pointer to HBA context object.
12157 * @cmdiocbq: Pointer to command iocb.
12158 * @rspiocbq: Pointer to response iocb.
12159 *
12160 * This function is the completion handler for iocbs issued using
12161 * lpfc_sli_issue_iocb_wait function. This function is called by the
12162 * ring event handler function without any lock held. This function
12163 * can be called from both worker thread context and interrupt
12164 * context. This function also can be called from other thread which
12165 * cleans up the SLI layer objects.
12166 * This function copy the contents of the response iocb to the
12167 * response iocb memory object provided by the caller of
12168 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
12169 * sleeps for the iocb completion.
12170 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012171static void
12172lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
12173 struct lpfc_iocbq *cmdiocbq,
12174 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050012175{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012176 wait_queue_head_t *pdone_q;
12177 unsigned long iflags;
James Smartc4908502019-01-28 11:14:28 -080012178 struct lpfc_io_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050012179
James Smart2e0fef82007-06-17 19:56:36 -050012180 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040012181 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
12182
12183 /*
12184 * A time out has occurred for the iocb. If a time out
12185 * completion handler has been supplied, call it. Otherwise,
12186 * just free the iocbq.
12187 */
12188
12189 spin_unlock_irqrestore(&phba->hbalock, iflags);
12190 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
12191 cmdiocbq->wait_iocb_cmpl = NULL;
12192 if (cmdiocbq->iocb_cmpl)
12193 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
12194 else
12195 lpfc_sli_release_iocbq(phba, cmdiocbq);
12196 return;
12197 }
12198
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012199 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
12200 if (cmdiocbq->context2 && rspiocbq)
12201 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
12202 &rspiocbq->iocb, sizeof(IOCB_t));
12203
James Smart0f65ff62010-02-26 14:14:23 -050012204 /* Set the exchange busy flag for task management commands */
12205 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
12206 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smartc4908502019-01-28 11:14:28 -080012207 lpfc_cmd = container_of(cmdiocbq, struct lpfc_io_buf,
James Smart0f65ff62010-02-26 14:14:23 -050012208 cur_iocbq);
James Smart324e1c42019-10-18 14:18:21 -070012209 if (rspiocbq && (rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY))
12210 lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
12211 else
12212 lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
James Smart0f65ff62010-02-26 14:14:23 -050012213 }
12214
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012215 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012216 if (pdone_q)
12217 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050012218 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050012219 return;
12220}
12221
James Smarte59058c2008-08-24 21:49:00 -040012222/**
James Smartd11e31d2009-06-10 17:23:06 -040012223 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
12224 * @phba: Pointer to HBA context object..
12225 * @piocbq: Pointer to command iocb.
12226 * @flag: Flag to test.
12227 *
12228 * This routine grabs the hbalock and then test the iocb_flag to
12229 * see if the passed in flag is set.
12230 * Returns:
12231 * 1 if flag is set.
12232 * 0 if flag is not set.
12233 **/
12234static int
12235lpfc_chk_iocb_flg(struct lpfc_hba *phba,
12236 struct lpfc_iocbq *piocbq, uint32_t flag)
12237{
12238 unsigned long iflags;
12239 int ret;
12240
12241 spin_lock_irqsave(&phba->hbalock, iflags);
12242 ret = piocbq->iocb_flag & flag;
12243 spin_unlock_irqrestore(&phba->hbalock, iflags);
12244 return ret;
12245
12246}
12247
12248/**
James Smart3621a712009-04-06 18:47:14 -040012249 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040012250 * @phba: Pointer to HBA context object..
Lee Jones7af29d42020-07-21 17:41:31 +010012251 * @ring_number: Ring number
James Smarte59058c2008-08-24 21:49:00 -040012252 * @piocb: Pointer to command iocb.
12253 * @prspiocbq: Pointer to response iocb.
12254 * @timeout: Timeout in number of seconds.
12255 *
12256 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040012257 * iocb to complete. The iocb_cmpl field of the shall be used
12258 * to handle iocbs which time out. If the field is NULL, the
12259 * function shall free the iocbq structure. If more clean up is
12260 * needed, the caller is expected to provide a completion function
12261 * that will provide the needed clean up. If the iocb command is
12262 * not completed within timeout seconds, the function will either
12263 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
12264 * completion function set in the iocb_cmpl field and then return
12265 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
12266 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040012267 * The function waits for the iocb completion using an
12268 * non-interruptible wait.
12269 * This function will sleep while waiting for iocb completion.
12270 * So, this function should not be called from any context which
12271 * does not allow sleeping. Due to the same reason, this function
12272 * cannot be called with interrupt disabled.
12273 * This function assumes that the iocb completions occur while
12274 * this function sleep. So, this function cannot be called from
12275 * the thread which process iocb completion for this ring.
12276 * This function clears the iocb_flag of the iocb object before
12277 * issuing the iocb and the iocb completion handler sets this
12278 * flag and wakes this thread when the iocb completes.
12279 * The contents of the response iocb will be copied to prspiocbq
12280 * by the completion handler when the command completes.
12281 * This function returns IOCB_SUCCESS when success.
12282 * This function is called with no lock held.
12283 **/
dea31012005-04-17 16:05:31 -050012284int
James Smart2e0fef82007-06-17 19:56:36 -050012285lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040012286 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050012287 struct lpfc_iocbq *piocb,
12288 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012289 uint32_t timeout)
dea31012005-04-17 16:05:31 -050012290{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080012291 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012292 long timeleft, timeout_req = 0;
12293 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012294 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050012295 struct lpfc_iocbq *iocb;
12296 int txq_cnt = 0;
12297 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080012298 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040012299 unsigned long iflags;
12300 bool iocb_completed = true;
12301
James Smart895427b2017-02-12 13:52:30 -080012302 if (phba->sli_rev >= LPFC_SLI_REV4)
12303 pring = lpfc_sli4_calc_ring(phba, piocb);
12304 else
12305 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050012306 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012307 * If the caller has provided a response iocbq buffer, then context2
12308 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050012309 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012310 if (prspiocbq) {
12311 if (piocb->context2)
12312 return IOCB_ERROR;
12313 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050012314 }
12315
James Smart5a0916b2013-07-15 18:31:42 -040012316 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012317 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
12318 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040012319 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050012320
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012321 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050012322 if (lpfc_readl(phba->HCregaddr, &creg_val))
12323 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012324 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
12325 writel(creg_val, phba->HCregaddr);
12326 readl(phba->HCregaddr); /* flush */
12327 }
12328
James Smart2a9bf3d2010-06-07 15:24:45 -040012329 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
12330 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012331 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040012332 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012333 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040012334 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012335 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040012336 spin_lock_irqsave(&phba->hbalock, iflags);
12337 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050012338
James Smart5a0916b2013-07-15 18:31:42 -040012339 /*
12340 * IOCB timed out. Inform the wake iocb wait
12341 * completion function and set local status
12342 */
12343
12344 iocb_completed = false;
12345 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
12346 }
12347 spin_unlock_irqrestore(&phba->hbalock, iflags);
12348 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040012349 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012350 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040012351 /* Note: we are not indicating if the IOCB has a success
12352 * status or not - that's for the caller to check.
12353 * IOCB_SUCCESS means just that the command was sent and
12354 * completed. Not that it completed successfully.
12355 * */
James Smart7054a602007-04-25 09:52:34 -040012356 } else if (timeleft == 0) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012357 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040012358 "0338 IOCB wait timeout error - no "
12359 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012360 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040012361 } else {
Dick Kennedy372c1872020-06-30 14:50:00 -070012362 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040012363 "0330 IOCB wake NOT set, "
12364 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012365 timeout, (timeleft / jiffies));
12366 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050012367 }
James Smart2a9bf3d2010-06-07 15:24:45 -040012368 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012369 if (phba->cfg_log_verbose & LOG_SLI) {
12370 list_for_each_entry(iocb, &pring->txq, list) {
12371 txq_cnt++;
12372 }
12373 list_for_each_entry(iocb, &pring->txcmplq, list) {
12374 txcmplq_cnt++;
12375 }
12376 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12377 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
12378 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
12379 }
James Smart2a9bf3d2010-06-07 15:24:45 -040012380 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012381 } else {
12382 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012383 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012384 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012385 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050012386 }
12387
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012388 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050012389 if (lpfc_readl(phba->HCregaddr, &creg_val))
12390 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050012391 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
12392 writel(creg_val, phba->HCregaddr);
12393 readl(phba->HCregaddr); /* flush */
12394 }
12395
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012396 if (prspiocbq)
12397 piocb->context2 = NULL;
12398
12399 piocb->context_un.wait_queue = NULL;
12400 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050012401 return retval;
12402}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040012403
James Smarte59058c2008-08-24 21:49:00 -040012404/**
James Smart3621a712009-04-06 18:47:14 -040012405 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040012406 * @phba: Pointer to HBA context object.
12407 * @pmboxq: Pointer to driver mailbox object.
12408 * @timeout: Timeout in number of seconds.
12409 *
12410 * This function issues the mailbox to firmware and waits for the
12411 * mailbox command to complete. If the mailbox command is not
12412 * completed within timeout seconds, it returns MBX_TIMEOUT.
12413 * The function waits for the mailbox completion using an
12414 * interruptible wait. If the thread is woken up due to a
12415 * signal, MBX_TIMEOUT error is returned to the caller. Caller
12416 * should not free the mailbox resources, if this function returns
12417 * MBX_TIMEOUT.
12418 * This function will sleep while waiting for mailbox completion.
12419 * So, this function should not be called from any context which
12420 * does not allow sleeping. Due to the same reason, this function
12421 * cannot be called with interrupt disabled.
12422 * This function assumes that the mailbox completion occurs while
12423 * this function sleep. So, this function cannot be called from
12424 * the worker thread which processes mailbox completion.
12425 * This function is called in the context of HBA management
12426 * applications.
12427 * This function returns MBX_SUCCESS when successful.
12428 * This function is called with no lock held.
12429 **/
dea31012005-04-17 16:05:31 -050012430int
James Smart2e0fef82007-06-17 19:56:36 -050012431lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050012432 uint32_t timeout)
12433{
James Smarte29d74f2018-03-05 12:04:07 -080012434 struct completion mbox_done;
dea31012005-04-17 16:05:31 -050012435 int retval;
James Smart858c9f62007-06-17 19:56:39 -050012436 unsigned long flag;
dea31012005-04-17 16:05:31 -050012437
James Smart495a7142008-06-14 22:52:59 -040012438 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050012439 /* setup wake call as IOCB callback */
12440 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
dea31012005-04-17 16:05:31 -050012441
James Smarte29d74f2018-03-05 12:04:07 -080012442 /* setup context3 field to pass wait_queue pointer to wake function */
12443 init_completion(&mbox_done);
12444 pmboxq->context3 = &mbox_done;
dea31012005-04-17 16:05:31 -050012445 /* now issue the command */
12446 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050012447 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smarte29d74f2018-03-05 12:04:07 -080012448 wait_for_completion_timeout(&mbox_done,
12449 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040012450
James Smart858c9f62007-06-17 19:56:39 -050012451 spin_lock_irqsave(&phba->hbalock, flag);
James Smarte29d74f2018-03-05 12:04:07 -080012452 pmboxq->context3 = NULL;
James Smart7054a602007-04-25 09:52:34 -040012453 /*
12454 * if LPFC_MBX_WAKE flag is set the mailbox is completed
12455 * else do not free the resources.
12456 */
James Smartd7c47992010-06-08 18:31:54 -040012457 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050012458 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040012459 } else {
James Smart7054a602007-04-25 09:52:34 -040012460 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050012461 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12462 }
12463 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -050012464 }
dea31012005-04-17 16:05:31 -050012465 return retval;
12466}
12467
James Smarte59058c2008-08-24 21:49:00 -040012468/**
James Smart3772a992009-05-22 14:50:54 -040012469 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040012470 * @phba: Pointer to HBA context.
Lee Jones7af29d42020-07-21 17:41:31 +010012471 * @mbx_action: Mailbox shutdown options.
James Smarte59058c2008-08-24 21:49:00 -040012472 *
James Smart3772a992009-05-22 14:50:54 -040012473 * This function is called to shutdown the driver's mailbox sub-system.
12474 * It first marks the mailbox sub-system is in a block state to prevent
12475 * the asynchronous mailbox command from issued off the pending mailbox
12476 * command queue. If the mailbox command sub-system shutdown is due to
12477 * HBA error conditions such as EEH or ERATT, this routine shall invoke
12478 * the mailbox sub-system flush routine to forcefully bring down the
12479 * mailbox sub-system. Otherwise, if it is due to normal condition (such
12480 * as with offline or HBA function reset), this routine will wait for the
12481 * outstanding mailbox command to complete before invoking the mailbox
12482 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040012483 **/
James Smart3772a992009-05-22 14:50:54 -040012484void
James Smart618a5232012-06-12 13:54:36 -040012485lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040012486{
James Smart3772a992009-05-22 14:50:54 -040012487 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040012488 unsigned long timeout;
12489
James Smart618a5232012-06-12 13:54:36 -040012490 if (mbx_action == LPFC_MBX_NO_WAIT) {
12491 /* delay 100ms for port state */
12492 msleep(100);
12493 lpfc_sli_mbox_sys_flush(phba);
12494 return;
12495 }
James Smarta183a152011-10-10 21:32:43 -040012496 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050012497
James Smart523128e2018-09-10 10:30:46 -070012498 /* Disable softirqs, including timers from obtaining phba->hbalock */
12499 local_bh_disable();
12500
James Smart3772a992009-05-22 14:50:54 -040012501 spin_lock_irq(&phba->hbalock);
12502 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040012503
12504 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040012505 /* Determine how long we might wait for the active mailbox
12506 * command to be gracefully completed by firmware.
12507 */
James Smarta183a152011-10-10 21:32:43 -040012508 if (phba->sli.mbox_active)
12509 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
12510 phba->sli.mbox_active) *
12511 1000) + jiffies;
12512 spin_unlock_irq(&phba->hbalock);
12513
James Smart523128e2018-09-10 10:30:46 -070012514 /* Enable softirqs again, done with phba->hbalock */
12515 local_bh_enable();
12516
James Smart3772a992009-05-22 14:50:54 -040012517 while (phba->sli.mbox_active) {
12518 /* Check active mailbox complete status every 2ms */
12519 msleep(2);
12520 if (time_after(jiffies, timeout))
12521 /* Timeout, let the mailbox flush routine to
12522 * forcefully release active mailbox command
12523 */
12524 break;
12525 }
James Smart523128e2018-09-10 10:30:46 -070012526 } else {
James Smartd7069f02012-03-01 22:36:29 -050012527 spin_unlock_irq(&phba->hbalock);
12528
James Smart523128e2018-09-10 10:30:46 -070012529 /* Enable softirqs again, done with phba->hbalock */
12530 local_bh_enable();
12531 }
12532
James Smart3772a992009-05-22 14:50:54 -040012533 lpfc_sli_mbox_sys_flush(phba);
12534}
12535
12536/**
12537 * lpfc_sli_eratt_read - read sli-3 error attention events
12538 * @phba: Pointer to HBA context.
12539 *
12540 * This function is called to read the SLI3 device error attention registers
12541 * for possible error attention events. The caller must hold the hostlock
12542 * with spin_lock_irq().
12543 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012544 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040012545 * Register and returns 0 otherwise.
12546 **/
12547static int
12548lpfc_sli_eratt_read(struct lpfc_hba *phba)
12549{
James Smarted957682007-06-17 19:56:37 -050012550 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040012551
James Smart3772a992009-05-22 14:50:54 -040012552 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050012553 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12554 goto unplug_err;
12555
James Smart3772a992009-05-22 14:50:54 -040012556 if (ha_copy & HA_ERATT) {
12557 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050012558 if (lpfc_sli_read_hs(phba))
12559 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040012560
James Smart3772a992009-05-22 14:50:54 -040012561 /* Check if there is a deferred error condition is active */
12562 if ((HS_FFER1 & phba->work_hs) &&
12563 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012564 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040012565 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040012566 /* Clear all interrupt enable conditions */
12567 writel(0, phba->HCregaddr);
12568 readl(phba->HCregaddr);
12569 }
12570
12571 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040012572 phba->work_ha |= HA_ERATT;
12573 /* Indicate polling handles this ERATT */
12574 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012575 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012576 }
James Smart3772a992009-05-22 14:50:54 -040012577 return 0;
James Smart9940b972011-03-11 16:06:12 -050012578
12579unplug_err:
12580 /* Set the driver HS work bitmap */
12581 phba->work_hs |= UNPLUG_ERR;
12582 /* Set the driver HA work bitmap */
12583 phba->work_ha |= HA_ERATT;
12584 /* Indicate polling handles this ERATT */
12585 phba->hba_flag |= HBA_ERATT_HANDLED;
12586 return 1;
James Smartb4c02652006-07-06 15:50:43 -040012587}
12588
James Smarte59058c2008-08-24 21:49:00 -040012589/**
James Smartda0436e2009-05-22 14:51:39 -040012590 * lpfc_sli4_eratt_read - read sli-4 error attention events
12591 * @phba: Pointer to HBA context.
12592 *
12593 * This function is called to read the SLI4 device error attention registers
12594 * for possible error attention events. The caller must hold the hostlock
12595 * with spin_lock_irq().
12596 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012597 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040012598 * Register and returns 0 otherwise.
12599 **/
12600static int
12601lpfc_sli4_eratt_read(struct lpfc_hba *phba)
12602{
12603 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050012604 uint32_t if_type, portsmphr;
12605 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040012606
James Smart2fcee4b2010-12-15 17:57:46 -050012607 /*
12608 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040012609 * registers for error attention. This can be changed later.
12610 */
James Smart2fcee4b2010-12-15 17:57:46 -050012611 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
12612 switch (if_type) {
12613 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050012614 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
12615 &uerr_sta_lo) ||
12616 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
12617 &uerr_sta_hi)) {
12618 phba->work_hs |= UNPLUG_ERR;
12619 phba->work_ha |= HA_ERATT;
12620 phba->hba_flag |= HBA_ERATT_HANDLED;
12621 return 1;
12622 }
James Smart2fcee4b2010-12-15 17:57:46 -050012623 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
12624 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070012625 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050012626 "1423 HBA Unrecoverable error: "
12627 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
12628 "ue_mask_lo_reg=0x%x, "
12629 "ue_mask_hi_reg=0x%x\n",
12630 uerr_sta_lo, uerr_sta_hi,
12631 phba->sli4_hba.ue_mask_lo,
12632 phba->sli4_hba.ue_mask_hi);
12633 phba->work_status[0] = uerr_sta_lo;
12634 phba->work_status[1] = uerr_sta_hi;
12635 phba->work_ha |= HA_ERATT;
12636 phba->hba_flag |= HBA_ERATT_HANDLED;
12637 return 1;
12638 }
12639 break;
12640 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080012641 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050012642 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
12643 &portstat_reg.word0) ||
12644 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
12645 &portsmphr)){
12646 phba->work_hs |= UNPLUG_ERR;
12647 phba->work_ha |= HA_ERATT;
12648 phba->hba_flag |= HBA_ERATT_HANDLED;
12649 return 1;
12650 }
James Smart2fcee4b2010-12-15 17:57:46 -050012651 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
12652 phba->work_status[0] =
12653 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
12654 phba->work_status[1] =
12655 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
Dick Kennedy372c1872020-06-30 14:50:00 -070012656 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2e90f4b2011-12-13 13:22:37 -050012657 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050012658 "port status reg 0x%x, "
12659 "port smphr reg 0x%x, "
12660 "error 1=0x%x, error 2=0x%x\n",
12661 portstat_reg.word0,
12662 portsmphr,
12663 phba->work_status[0],
12664 phba->work_status[1]);
12665 phba->work_ha |= HA_ERATT;
12666 phba->hba_flag |= HBA_ERATT_HANDLED;
12667 return 1;
12668 }
12669 break;
12670 case LPFC_SLI_INTF_IF_TYPE_1:
12671 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070012672 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2fcee4b2010-12-15 17:57:46 -050012673 "2886 HBA Error Attention on unsupported "
12674 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050012675 return 1;
James Smartda0436e2009-05-22 14:51:39 -040012676 }
James Smart2fcee4b2010-12-15 17:57:46 -050012677
James Smartda0436e2009-05-22 14:51:39 -040012678 return 0;
12679}
12680
12681/**
James Smart3621a712009-04-06 18:47:14 -040012682 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040012683 * @phba: Pointer to HBA context.
12684 *
James Smart3772a992009-05-22 14:50:54 -040012685 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040012686 * error attention register bit for error attention events.
12687 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012688 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040012689 * Register and returns 0 otherwise.
12690 **/
12691int
12692lpfc_sli_check_eratt(struct lpfc_hba *phba)
12693{
12694 uint32_t ha_copy;
12695
12696 /* If somebody is waiting to handle an eratt, don't process it
12697 * here. The brdkill function will do this.
12698 */
12699 if (phba->link_flag & LS_IGNORE_ERATT)
12700 return 0;
12701
12702 /* Check if interrupt handler handles this ERATT */
12703 spin_lock_irq(&phba->hbalock);
12704 if (phba->hba_flag & HBA_ERATT_HANDLED) {
12705 /* Interrupt handler has handled ERATT */
12706 spin_unlock_irq(&phba->hbalock);
12707 return 0;
12708 }
12709
James Smarta257bf92009-04-06 18:48:10 -040012710 /*
12711 * If there is deferred error attention, do not check for error
12712 * attention
12713 */
12714 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
12715 spin_unlock_irq(&phba->hbalock);
12716 return 0;
12717 }
12718
James Smart3772a992009-05-22 14:50:54 -040012719 /* If PCI channel is offline, don't process it */
12720 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040012721 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012722 return 0;
12723 }
12724
12725 switch (phba->sli_rev) {
12726 case LPFC_SLI_REV2:
12727 case LPFC_SLI_REV3:
12728 /* Read chip Host Attention (HA) register */
12729 ha_copy = lpfc_sli_eratt_read(phba);
12730 break;
James Smartda0436e2009-05-22 14:51:39 -040012731 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050012732 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040012733 ha_copy = lpfc_sli4_eratt_read(phba);
12734 break;
James Smart3772a992009-05-22 14:50:54 -040012735 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070012736 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart3772a992009-05-22 14:50:54 -040012737 "0299 Invalid SLI revision (%d)\n",
12738 phba->sli_rev);
12739 ha_copy = 0;
12740 break;
James Smart93996272008-08-24 21:50:30 -040012741 }
12742 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040012743
12744 return ha_copy;
12745}
12746
12747/**
12748 * lpfc_intr_state_check - Check device state for interrupt handling
12749 * @phba: Pointer to HBA context.
12750 *
12751 * This inline routine checks whether a device or its PCI slot is in a state
12752 * that the interrupt should be handled.
12753 *
12754 * This function returns 0 if the device or the PCI slot is in a state that
12755 * interrupt should be handled, otherwise -EIO.
12756 */
12757static inline int
12758lpfc_intr_state_check(struct lpfc_hba *phba)
12759{
12760 /* If the pci channel is offline, ignore all the interrupts */
12761 if (unlikely(pci_channel_offline(phba->pcidev)))
12762 return -EIO;
12763
12764 /* Update device level interrupt statistics */
12765 phba->sli.slistat.sli_intr++;
12766
12767 /* Ignore all interrupts during initialization. */
12768 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
12769 return -EIO;
12770
James Smart93996272008-08-24 21:50:30 -040012771 return 0;
12772}
12773
12774/**
James Smart3772a992009-05-22 14:50:54 -040012775 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040012776 * @irq: Interrupt number.
12777 * @dev_id: The device context pointer.
12778 *
James Smart93996272008-08-24 21:50:30 -040012779 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012780 * service routine when device with SLI-3 interface spec is enabled with
12781 * MSI-X multi-message interrupt mode and there are slow-path events in
12782 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
12783 * interrupt mode, this function is called as part of the device-level
12784 * interrupt handler. When the PCI slot is in error recovery or the HBA
12785 * is undergoing initialization, the interrupt handler will not process
12786 * the interrupt. The link attention and ELS ring attention events are
12787 * handled by the worker thread. The interrupt handler signals the worker
12788 * thread and returns for these events. This function is called without
12789 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040012790 * structures.
12791 *
12792 * This function returns IRQ_HANDLED when interrupt is handled else it
12793 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040012794 **/
dea31012005-04-17 16:05:31 -050012795irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012796lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050012797{
James Smart2e0fef82007-06-17 19:56:36 -050012798 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050012799 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050012800 uint32_t work_ha_copy;
12801 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012802 unsigned long iflag;
dea31012005-04-17 16:05:31 -050012803 uint32_t control;
12804
James Smart92d7f7b2007-06-17 19:56:38 -050012805 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050012806 struct lpfc_vport *vport;
12807 struct lpfc_nodelist *ndlp;
12808 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050012809 LPFC_MBOXQ_t *pmb;
12810 int rc;
12811
dea31012005-04-17 16:05:31 -050012812 /*
12813 * Get the driver's phba structure from the dev_id and
12814 * assume the HBA is not interrupting.
12815 */
James Smart93996272008-08-24 21:50:30 -040012816 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050012817
12818 if (unlikely(!phba))
12819 return IRQ_NONE;
12820
dea31012005-04-17 16:05:31 -050012821 /*
James Smart93996272008-08-24 21:50:30 -040012822 * Stuff needs to be attented to when this function is invoked as an
12823 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012824 */
James Smart93996272008-08-24 21:50:30 -040012825 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012826 /* Check device state for handling interrupt */
12827 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012828 return IRQ_NONE;
12829 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050012830 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012831 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12832 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040012833 /* If somebody is waiting to handle an eratt don't process it
12834 * here. The brdkill function will do this.
12835 */
12836 if (phba->link_flag & LS_IGNORE_ERATT)
12837 ha_copy &= ~HA_ERATT;
12838 /* Check the need for handling ERATT in interrupt handler */
12839 if (ha_copy & HA_ERATT) {
12840 if (phba->hba_flag & HBA_ERATT_HANDLED)
12841 /* ERATT polling has handled ERATT */
12842 ha_copy &= ~HA_ERATT;
12843 else
12844 /* Indicate interrupt handler handles ERATT */
12845 phba->hba_flag |= HBA_ERATT_HANDLED;
12846 }
James Smarta257bf92009-04-06 18:48:10 -040012847
12848 /*
12849 * If there is deferred error attention, do not check for any
12850 * interrupt.
12851 */
12852 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012853 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012854 return IRQ_NONE;
12855 }
12856
James Smart93996272008-08-24 21:50:30 -040012857 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012858 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12859 goto unplug_error;
12860
James Smarta747c9c2009-11-18 15:41:10 -050012861 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12862 HC_LAINT_ENA | HC_ERINT_ENA),
12863 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012864 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12865 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012866 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012867 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012868 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012869 } else
12870 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012871
dea31012005-04-17 16:05:31 -050012872 work_ha_copy = ha_copy & phba->work_ha_mask;
12873
James Smart93996272008-08-24 21:50:30 -040012874 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012875 if (work_ha_copy & HA_LATT) {
12876 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12877 /*
12878 * Turn off Link Attention interrupts
12879 * until CLEAR_LA done
12880 */
James Smart5b75da22008-12-04 22:39:35 -050012881 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012882 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012883 if (lpfc_readl(phba->HCregaddr, &control))
12884 goto unplug_error;
dea31012005-04-17 16:05:31 -050012885 control &= ~HC_LAINT_ENA;
12886 writel(control, phba->HCregaddr);
12887 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012888 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012889 }
12890 else
12891 work_ha_copy &= ~HA_LATT;
12892 }
12893
James Smart93996272008-08-24 21:50:30 -040012894 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012895 /*
12896 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12897 * the only slow ring.
12898 */
12899 status = (work_ha_copy &
12900 (HA_RXMASK << (4*LPFC_ELS_RING)));
12901 status >>= (4*LPFC_ELS_RING);
12902 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012903 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012904 if (lpfc_readl(phba->HCregaddr, &control))
12905 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012906
12907 lpfc_debugfs_slow_ring_trc(phba,
12908 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12909 control, status,
12910 (uint32_t)phba->sli.slistat.sli_intr);
12911
James Smart858c9f62007-06-17 19:56:39 -050012912 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012913 lpfc_debugfs_slow_ring_trc(phba,
12914 "ISR Disable ring:"
12915 "pwork:x%x hawork:x%x wait:x%x",
12916 phba->work_ha, work_ha_copy,
12917 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012918 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012919
James Smart858c9f62007-06-17 19:56:39 -050012920 control &=
12921 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012922 writel(control, phba->HCregaddr);
12923 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012924 }
James Smarta58cbd52007-08-02 11:09:43 -040012925 else {
12926 lpfc_debugfs_slow_ring_trc(phba,
12927 "ISR slow ring: pwork:"
12928 "x%x hawork:x%x wait:x%x",
12929 phba->work_ha, work_ha_copy,
12930 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012931 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012932 }
James Smart5b75da22008-12-04 22:39:35 -050012933 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012934 }
12935 }
James Smart5b75da22008-12-04 22:39:35 -050012936 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012937 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012938 if (lpfc_sli_read_hs(phba))
12939 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012940 /*
12941 * Check if there is a deferred error condition
12942 * is active
12943 */
12944 if ((HS_FFER1 & phba->work_hs) &&
12945 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012946 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12947 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012948 phba->hba_flag |= DEFER_ERATT;
12949 /* Clear all interrupt enable conditions */
12950 writel(0, phba->HCregaddr);
12951 readl(phba->HCregaddr);
12952 }
12953 }
12954
James Smart93996272008-08-24 21:50:30 -040012955 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012956 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012957 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012958 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012959 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012960
12961 /* First check out the status word */
12962 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12963 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012964 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012965 /*
12966 * Stray Mailbox Interrupt, mbxCommand <cmd>
12967 * mbxStatus <status>
12968 */
Dick Kennedy372c1872020-06-30 14:50:00 -070012969 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarte8b62012007-08-02 11:10:09 -040012970 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012971 "Interrupt mbxCommand x%x "
12972 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012973 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012974 pmbox->mbxCommand,
12975 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012976 /* clear mailbox attention bit */
12977 work_ha_copy &= ~HA_MBATT;
12978 } else {
James Smart97eab632008-04-07 10:16:05 -040012979 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012980 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012981 phba->last_completion_time = jiffies;
12982 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012983 if (pmb->mbox_cmpl) {
12984 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12985 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012986 if (pmb->out_ext_byte_len &&
James Smart3e1f0712018-11-29 16:09:29 -080012987 pmb->ctx_buf)
James Smart7a470272010-03-15 11:25:20 -040012988 lpfc_sli_pcimem_bcopy(
12989 phba->mbox_ext,
James Smart3e1f0712018-11-29 16:09:29 -080012990 pmb->ctx_buf,
James Smart7a470272010-03-15 11:25:20 -040012991 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012992 }
James Smart09372822008-01-11 01:52:54 -050012993 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12994 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12995
12996 lpfc_debugfs_disc_trc(vport,
12997 LPFC_DISC_TRC_MBOX_VPORT,
12998 "MBOX dflt rpi: : "
12999 "status:x%x rpi:x%x",
13000 (uint32_t)pmbox->mbxStatus,
13001 pmbox->un.varWords[0], 0);
13002
13003 if (!pmbox->mbxStatus) {
13004 mp = (struct lpfc_dmabuf *)
James Smart3e1f0712018-11-29 16:09:29 -080013005 (pmb->ctx_buf);
James Smart09372822008-01-11 01:52:54 -050013006 ndlp = (struct lpfc_nodelist *)
James Smart3e1f0712018-11-29 16:09:29 -080013007 pmb->ctx_ndlp;
James Smart09372822008-01-11 01:52:54 -050013008
13009 /* Reg_LOGIN of dflt RPI was
13010 * successful. new lets get
13011 * rid of the RPI using the
13012 * same mbox buffer.
13013 */
13014 lpfc_unreg_login(phba,
13015 vport->vpi,
13016 pmbox->un.varWords[0],
13017 pmb);
13018 pmb->mbox_cmpl =
13019 lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080013020 pmb->ctx_buf = mp;
13021 pmb->ctx_ndlp = ndlp;
James Smart09372822008-01-11 01:52:54 -050013022 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040013023 rc = lpfc_sli_issue_mbox(phba,
13024 pmb,
13025 MBX_NOWAIT);
13026 if (rc != MBX_BUSY)
13027 lpfc_printf_log(phba,
13028 KERN_ERR,
Dick Kennedy372c1872020-06-30 14:50:00 -070013029 LOG_TRACE_EVENT,
James Smartd7c255b2008-08-24 21:50:00 -040013030 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040013031 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040013032 if (rc != MBX_NOT_FINISHED)
13033 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050013034 }
13035 }
James Smart5b75da22008-12-04 22:39:35 -050013036 spin_lock_irqsave(
13037 &phba->pport->work_port_lock,
13038 iflag);
James Smart09372822008-01-11 01:52:54 -050013039 phba->pport->work_port_events &=
13040 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050013041 spin_unlock_irqrestore(
13042 &phba->pport->work_port_lock,
13043 iflag);
James Smarta22d73b2021-01-04 10:02:38 -080013044
13045 /* Do NOT queue MBX_HEARTBEAT to the worker
13046 * thread for processing.
13047 */
13048 if (pmbox->mbxCommand == MBX_HEARTBEAT) {
13049 /* Process mbox now */
13050 phba->sli.mbox_active = NULL;
13051 phba->sli.sli_flag &=
13052 ~LPFC_SLI_MBOX_ACTIVE;
13053 if (pmb->mbox_cmpl)
13054 pmb->mbox_cmpl(phba, pmb);
13055 } else {
13056 /* Queue to worker thread to process */
13057 lpfc_mbox_cmpl_put(phba, pmb);
13058 }
James Smart858c9f62007-06-17 19:56:39 -050013059 }
James Smart97eab632008-04-07 10:16:05 -040013060 } else
James Smart5b75da22008-12-04 22:39:35 -050013061 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040013062
James Smart92d7f7b2007-06-17 19:56:38 -050013063 if ((work_ha_copy & HA_MBATT) &&
13064 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050013065send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050013066 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040013067 do {
13068 rc = lpfc_sli_issue_mbox(phba, NULL,
13069 MBX_NOWAIT);
13070 } while (rc == MBX_NOT_FINISHED);
13071 if (rc != MBX_SUCCESS)
Dick Kennedy372c1872020-06-30 14:50:00 -070013072 lpfc_printf_log(phba, KERN_ERR,
13073 LOG_TRACE_EVENT,
13074 "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040013075 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050013076 }
13077
James Smart5b75da22008-12-04 22:39:35 -050013078 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050013079 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050013080 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040013081 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050013082 }
James Smart93996272008-08-24 21:50:30 -040013083 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050013084unplug_error:
13085 spin_unlock_irqrestore(&phba->hbalock, iflag);
13086 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050013087
James Smart3772a992009-05-22 14:50:54 -040013088} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040013089
13090/**
James Smart3772a992009-05-22 14:50:54 -040013091 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040013092 * @irq: Interrupt number.
13093 * @dev_id: The device context pointer.
13094 *
13095 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040013096 * service routine when device with SLI-3 interface spec is enabled with
13097 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13098 * ring event in the HBA. However, when the device is enabled with either
13099 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13100 * device-level interrupt handler. When the PCI slot is in error recovery
13101 * or the HBA is undergoing initialization, the interrupt handler will not
13102 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13103 * the intrrupt context. This function is called without any lock held.
13104 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040013105 *
13106 * This function returns IRQ_HANDLED when interrupt is handled else it
13107 * returns IRQ_NONE.
13108 **/
13109irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040013110lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040013111{
13112 struct lpfc_hba *phba;
13113 uint32_t ha_copy;
13114 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050013115 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080013116 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040013117
13118 /* Get the driver's phba structure from the dev_id and
13119 * assume the HBA is not interrupting.
13120 */
13121 phba = (struct lpfc_hba *) dev_id;
13122
13123 if (unlikely(!phba))
13124 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050013125
13126 /*
James Smart93996272008-08-24 21:50:30 -040013127 * Stuff needs to be attented to when this function is invoked as an
13128 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050013129 */
James Smart93996272008-08-24 21:50:30 -040013130 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040013131 /* Check device state for handling interrupt */
13132 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040013133 return IRQ_NONE;
13134 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050013135 if (lpfc_readl(phba->HAregaddr, &ha_copy))
13136 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040013137 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050013138 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040013139 /*
13140 * If there is deferred error attention, do not check for
13141 * any interrupt.
13142 */
13143 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040013144 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040013145 return IRQ_NONE;
13146 }
James Smart93996272008-08-24 21:50:30 -040013147 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
13148 phba->HAregaddr);
13149 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050013150 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040013151 } else
13152 ha_copy = phba->ha_copy;
13153
13154 /*
13155 * Process all events on FCP ring. Take the optimized path for FCP IO.
13156 */
13157 ha_copy &= ~(phba->work_ha_mask);
13158
13159 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050013160 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080013161 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050013162 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080013163 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050013164
13165 if (phba->cfg_multi_ring_support == 2) {
13166 /*
James Smart93996272008-08-24 21:50:30 -040013167 * Process all events on extra ring. Take the optimized path
13168 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050013169 */
James Smart93996272008-08-24 21:50:30 -040013170 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050013171 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050013172 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050013173 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080013174 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050013175 status);
13176 }
13177 }
dea31012005-04-17 16:05:31 -050013178 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040013179} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050013180
James Smart93996272008-08-24 21:50:30 -040013181/**
James Smart3772a992009-05-22 14:50:54 -040013182 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040013183 * @irq: Interrupt number.
13184 * @dev_id: The device context pointer.
13185 *
James Smart3772a992009-05-22 14:50:54 -040013186 * This function is the HBA device-level interrupt handler to device with
13187 * SLI-3 interface spec, called from the PCI layer when either MSI or
13188 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
13189 * requires driver attention. This function invokes the slow-path interrupt
13190 * attention handling function and fast-path interrupt attention handling
13191 * function in turn to process the relevant HBA attention events. This
13192 * function is called without any lock held. It gets the hbalock to access
13193 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040013194 *
13195 * This function returns IRQ_HANDLED when interrupt is handled, else it
13196 * returns IRQ_NONE.
13197 **/
13198irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040013199lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040013200{
13201 struct lpfc_hba *phba;
13202 irqreturn_t sp_irq_rc, fp_irq_rc;
13203 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050013204 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040013205
13206 /*
13207 * Get the driver's phba structure from the dev_id and
13208 * assume the HBA is not interrupting.
13209 */
13210 phba = (struct lpfc_hba *) dev_id;
13211
13212 if (unlikely(!phba))
13213 return IRQ_NONE;
13214
James Smart3772a992009-05-22 14:50:54 -040013215 /* Check device state for handling interrupt */
13216 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040013217 return IRQ_NONE;
13218
13219 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050013220 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
13221 spin_unlock(&phba->hbalock);
13222 return IRQ_HANDLED;
13223 }
13224
James Smart93996272008-08-24 21:50:30 -040013225 if (unlikely(!phba->ha_copy)) {
13226 spin_unlock(&phba->hbalock);
13227 return IRQ_NONE;
13228 } else if (phba->ha_copy & HA_ERATT) {
13229 if (phba->hba_flag & HBA_ERATT_HANDLED)
13230 /* ERATT polling has handled ERATT */
13231 phba->ha_copy &= ~HA_ERATT;
13232 else
13233 /* Indicate interrupt handler handles ERATT */
13234 phba->hba_flag |= HBA_ERATT_HANDLED;
13235 }
13236
James Smarta257bf92009-04-06 18:48:10 -040013237 /*
13238 * If there is deferred error attention, do not check for any interrupt.
13239 */
13240 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020013241 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040013242 return IRQ_NONE;
13243 }
13244
James Smart93996272008-08-24 21:50:30 -040013245 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050013246 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
13247 spin_unlock(&phba->hbalock);
13248 return IRQ_HANDLED;
13249 }
James Smarta747c9c2009-11-18 15:41:10 -050013250 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
13251 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
13252 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040013253 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050013254 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040013255 readl(phba->HAregaddr); /* flush */
13256 spin_unlock(&phba->hbalock);
13257
13258 /*
13259 * Invokes slow-path host attention interrupt handling as appropriate.
13260 */
13261
13262 /* status of events with mailbox and link attention */
13263 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
13264
13265 /* status of events with ELS ring */
13266 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
13267 status2 >>= (4*LPFC_ELS_RING);
13268
13269 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040013270 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040013271 else
13272 sp_irq_rc = IRQ_NONE;
13273
13274 /*
13275 * Invoke fast-path host attention interrupt handling as appropriate.
13276 */
13277
13278 /* status of events with FCP ring */
13279 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
13280 status1 >>= (4*LPFC_FCP_RING);
13281
13282 /* status of events with extra ring */
13283 if (phba->cfg_multi_ring_support == 2) {
13284 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
13285 status2 >>= (4*LPFC_EXTRA_RING);
13286 } else
13287 status2 = 0;
13288
13289 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040013290 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040013291 else
13292 fp_irq_rc = IRQ_NONE;
13293
13294 /* Return device-level interrupt handling status */
13295 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040013296} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040013297
13298/**
James Smart4f774512009-05-22 14:52:35 -040013299 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
13300 * @phba: pointer to lpfc hba data structure.
13301 *
13302 * This routine is invoked by the worker thread to process all the pending
13303 * SLI4 els abort xri events.
13304 **/
13305void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
13306{
13307 struct lpfc_cq_event *cq_event;
James Smarte7dab162020-10-20 13:27:12 -070013308 unsigned long iflags;
James Smart4f774512009-05-22 14:52:35 -040013309
13310 /* First, declare the els xri abort event has been handled */
James Smarte7dab162020-10-20 13:27:12 -070013311 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013312 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
James Smarte7dab162020-10-20 13:27:12 -070013313 spin_unlock_irqrestore(&phba->hbalock, iflags);
13314
James Smart4f774512009-05-22 14:52:35 -040013315 /* Now, handle all the els xri abort events */
James Smarte7dab162020-10-20 13:27:12 -070013316 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013317 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
13318 /* Get the first event from the head of the event queue */
James Smart4f774512009-05-22 14:52:35 -040013319 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
13320 cq_event, struct lpfc_cq_event, list);
James Smarte7dab162020-10-20 13:27:12 -070013321 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
13322 iflags);
James Smart4f774512009-05-22 14:52:35 -040013323 /* Notify aborted XRI for ELS work queue */
13324 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
James Smarte7dab162020-10-20 13:27:12 -070013325
James Smart4f774512009-05-22 14:52:35 -040013326 /* Free the event processed back to the free pool */
13327 lpfc_sli4_cq_event_release(phba, cq_event);
James Smarte7dab162020-10-20 13:27:12 -070013328 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
13329 iflags);
James Smart4f774512009-05-22 14:52:35 -040013330 }
James Smarte7dab162020-10-20 13:27:12 -070013331 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013332}
13333
James Smart341af102010-01-26 23:07:37 -050013334/**
13335 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
13336 * @phba: pointer to lpfc hba data structure
13337 * @pIocbIn: pointer to the rspiocbq
13338 * @pIocbOut: pointer to the cmdiocbq
13339 * @wcqe: pointer to the complete wcqe
13340 *
13341 * This routine transfers the fields of a command iocbq to a response iocbq
13342 * by copying all the IOCB fields from command iocbq and transferring the
13343 * completion status information from the complete wcqe.
13344 **/
James Smart4f774512009-05-22 14:52:35 -040013345static void
James Smart341af102010-01-26 23:07:37 -050013346lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
13347 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040013348 struct lpfc_iocbq *pIocbOut,
13349 struct lpfc_wcqe_complete *wcqe)
13350{
James Smartaf227412013-10-10 12:23:10 -040013351 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050013352 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040013353 uint32_t status, max_response;
13354 struct lpfc_dmabuf *dmabuf;
13355 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040013356 size_t offset = offsetof(struct lpfc_iocbq, iocb);
13357
13358 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
13359 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040013360 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050013361 status = bf_get(lpfc_wcqe_c_status, wcqe);
13362 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040013363 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
13364 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
13365 pIocbIn->iocb.un.fcpi.fcpi_parm =
13366 pIocbOut->iocb.un.fcpi.fcpi_parm -
13367 wcqe->total_data_placed;
13368 else
13369 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050013370 else {
James Smart4f774512009-05-22 14:52:35 -040013371 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040013372 switch (pIocbOut->iocb.ulpCommand) {
13373 case CMD_ELS_REQUEST64_CR:
13374 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
13375 bpl = (struct ulp_bde64 *)dmabuf->virt;
13376 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
13377 max_response = bde.tus.f.bdeSize;
13378 break;
13379 case CMD_GEN_REQUEST64_CR:
13380 max_response = 0;
13381 if (!pIocbOut->context3)
13382 break;
13383 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
13384 sizeof(struct ulp_bde64);
13385 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
13386 bpl = (struct ulp_bde64 *)dmabuf->virt;
13387 for (i = 0; i < numBdes; i++) {
13388 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
13389 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
13390 max_response += bde.tus.f.bdeSize;
13391 }
13392 break;
13393 default:
13394 max_response = wcqe->total_data_placed;
13395 break;
13396 }
13397 if (max_response < wcqe->total_data_placed)
13398 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
13399 else
13400 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
13401 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050013402 }
James Smart341af102010-01-26 23:07:37 -050013403
James Smartacd68592012-01-18 16:25:09 -050013404 /* Convert BG errors for completion status */
13405 if (status == CQE_STATUS_DI_ERROR) {
13406 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
13407
13408 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
13409 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
13410 else
13411 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
13412
13413 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
13414 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
13415 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13416 BGS_GUARD_ERR_MASK;
13417 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
13418 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13419 BGS_APPTAG_ERR_MASK;
13420 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
13421 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13422 BGS_REFTAG_ERR_MASK;
13423
13424 /* Check to see if there was any good data before the error */
13425 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
13426 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13427 BGS_HI_WATER_MARK_PRESENT_MASK;
13428 pIocbIn->iocb.unsli3.sli3_bg.bghm =
13429 wcqe->total_data_placed;
13430 }
13431
13432 /*
13433 * Set ALL the error bits to indicate we don't know what
13434 * type of error it is.
13435 */
13436 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
13437 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
13438 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
13439 BGS_GUARD_ERR_MASK);
13440 }
13441
James Smart341af102010-01-26 23:07:37 -050013442 /* Pick up HBA exchange busy condition */
13443 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
13444 spin_lock_irqsave(&phba->hbalock, iflags);
13445 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
13446 spin_unlock_irqrestore(&phba->hbalock, iflags);
13447 }
James Smart4f774512009-05-22 14:52:35 -040013448}
13449
13450/**
James Smart45ed1192009-10-02 15:17:02 -040013451 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
13452 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010013453 * @irspiocbq: Pointer to work-queue completion queue entry.
James Smart45ed1192009-10-02 15:17:02 -040013454 *
13455 * This routine handles an ELS work-queue completion event and construct
13456 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
13457 * discovery engine to handle.
13458 *
13459 * Return: Pointer to the receive IOCBQ, NULL otherwise.
13460 **/
13461static struct lpfc_iocbq *
13462lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
13463 struct lpfc_iocbq *irspiocbq)
13464{
James Smart895427b2017-02-12 13:52:30 -080013465 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040013466 struct lpfc_iocbq *cmdiocbq;
13467 struct lpfc_wcqe_complete *wcqe;
13468 unsigned long iflags;
13469
James Smart895427b2017-02-12 13:52:30 -080013470 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070013471 if (unlikely(!pring))
13472 return NULL;
James Smart895427b2017-02-12 13:52:30 -080013473
James Smart45ed1192009-10-02 15:17:02 -040013474 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart45ed1192009-10-02 15:17:02 -040013475 pring->stats.iocb_event++;
13476 /* Look up the ELS command IOCB and create pseudo response IOCB */
13477 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13478 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040013479 if (unlikely(!cmdiocbq)) {
13480 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13481 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070013482 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
13483 wcqe->word0, wcqe->total_data_placed,
13484 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040013485 lpfc_sli_release_iocbq(phba, irspiocbq);
13486 return NULL;
13487 }
13488
James Smarte2a8be52019-05-06 17:26:47 -070013489 spin_lock_irqsave(&pring->ring_lock, iflags);
Dick Kennedy401bb412017-09-29 17:34:28 -070013490 /* Put the iocb back on the txcmplq */
13491 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
13492 spin_unlock_irqrestore(&pring->ring_lock, iflags);
13493
James Smart45ed1192009-10-02 15:17:02 -040013494 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013495 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040013496
13497 return irspiocbq;
13498}
13499
James Smart8a5ca102017-11-20 16:00:30 -080013500inline struct lpfc_cq_event *
13501lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
13502{
13503 struct lpfc_cq_event *cq_event;
13504
13505 /* Allocate a new internal CQ_EVENT entry */
13506 cq_event = lpfc_sli4_cq_event_alloc(phba);
13507 if (!cq_event) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013508 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart8a5ca102017-11-20 16:00:30 -080013509 "0602 Failed to alloc CQ_EVENT entry\n");
13510 return NULL;
13511 }
13512
13513 /* Move the CQE into the event */
13514 memcpy(&cq_event->cqe, entry, size);
13515 return cq_event;
13516}
13517
James Smart45ed1192009-10-02 15:17:02 -040013518/**
Colin Ian King291c2542019-12-18 08:43:01 +000013519 * lpfc_sli4_sp_handle_async_event - Handle an asynchronous event
James Smart04c68492009-05-22 14:52:52 -040013520 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010013521 * @mcqe: Pointer to mailbox completion queue entry.
James Smart04c68492009-05-22 14:52:52 -040013522 *
Colin Ian King291c2542019-12-18 08:43:01 +000013523 * This routine process a mailbox completion queue entry with asynchronous
James Smart04c68492009-05-22 14:52:52 -040013524 * event.
13525 *
13526 * Return: true if work posted to worker thread, otherwise false.
13527 **/
13528static bool
13529lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13530{
13531 struct lpfc_cq_event *cq_event;
13532 unsigned long iflags;
13533
13534 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13535 "0392 Async Event: word0:x%x, word1:x%x, "
13536 "word2:x%x, word3:x%x\n", mcqe->word0,
13537 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
13538
James Smart8a5ca102017-11-20 16:00:30 -080013539 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
13540 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040013541 return false;
James Smarte7dab162020-10-20 13:27:12 -070013542
13543 spin_lock_irqsave(&phba->sli4_hba.asynce_list_lock, iflags);
James Smart04c68492009-05-22 14:52:52 -040013544 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
James Smarte7dab162020-10-20 13:27:12 -070013545 spin_unlock_irqrestore(&phba->sli4_hba.asynce_list_lock, iflags);
13546
James Smart04c68492009-05-22 14:52:52 -040013547 /* Set the async event flag */
James Smarte7dab162020-10-20 13:27:12 -070013548 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart04c68492009-05-22 14:52:52 -040013549 phba->hba_flag |= ASYNC_EVENT;
13550 spin_unlock_irqrestore(&phba->hbalock, iflags);
13551
13552 return true;
13553}
13554
13555/**
13556 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
13557 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010013558 * @mcqe: Pointer to mailbox completion queue entry.
James Smart04c68492009-05-22 14:52:52 -040013559 *
13560 * This routine process a mailbox completion queue entry with mailbox
13561 * completion event.
13562 *
13563 * Return: true if work posted to worker thread, otherwise false.
13564 **/
13565static bool
13566lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
13567{
13568 uint32_t mcqe_status;
13569 MAILBOX_t *mbox, *pmbox;
13570 struct lpfc_mqe *mqe;
13571 struct lpfc_vport *vport;
13572 struct lpfc_nodelist *ndlp;
13573 struct lpfc_dmabuf *mp;
13574 unsigned long iflags;
13575 LPFC_MBOXQ_t *pmb;
13576 bool workposted = false;
13577 int rc;
13578
13579 /* If not a mailbox complete MCQE, out by checking mailbox consume */
13580 if (!bf_get(lpfc_trailer_completed, mcqe))
13581 goto out_no_mqe_complete;
13582
13583 /* Get the reference to the active mbox command */
13584 spin_lock_irqsave(&phba->hbalock, iflags);
13585 pmb = phba->sli.mbox_active;
13586 if (unlikely(!pmb)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070013587 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart04c68492009-05-22 14:52:52 -040013588 "1832 No pending MBOX command to handle\n");
13589 spin_unlock_irqrestore(&phba->hbalock, iflags);
13590 goto out_no_mqe_complete;
13591 }
13592 spin_unlock_irqrestore(&phba->hbalock, iflags);
13593 mqe = &pmb->u.mqe;
13594 pmbox = (MAILBOX_t *)&pmb->u.mqe;
13595 mbox = phba->mbox;
13596 vport = pmb->vport;
13597
13598 /* Reset heartbeat timer */
13599 phba->last_completion_time = jiffies;
13600 del_timer(&phba->sli.mbox_tmo);
13601
13602 /* Move mbox data to caller's mailbox region, do endian swapping */
13603 if (pmb->mbox_cmpl && mbox)
James Smart48f8fdb2018-05-04 20:37:51 -070013604 lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040013605
James Smart73d91e52011-10-10 21:32:10 -040013606 /*
13607 * For mcqe errors, conditionally move a modified error code to
13608 * the mbox so that the error will not be missed.
13609 */
13610 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
13611 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
13612 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
13613 bf_set(lpfc_mqe_status, mqe,
13614 (LPFC_MBX_ERROR_RANGE | mcqe_status));
13615 }
James Smart04c68492009-05-22 14:52:52 -040013616 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
13617 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
13618 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
13619 "MBOX dflt rpi: status:x%x rpi:x%x",
13620 mcqe_status,
13621 pmbox->un.varWords[0], 0);
13622 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
James Smart3e1f0712018-11-29 16:09:29 -080013623 mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
13624 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart04c68492009-05-22 14:52:52 -040013625 /* Reg_LOGIN of dflt RPI was successful. Now lets get
13626 * RID of the PPI using the same mbox buffer.
13627 */
13628 lpfc_unreg_login(phba, vport->vpi,
13629 pmbox->un.varWords[0], pmb);
13630 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
James Smart3e1f0712018-11-29 16:09:29 -080013631 pmb->ctx_buf = mp;
James Smarte9b11082020-11-15 11:26:33 -080013632
13633 /* No reference taken here. This is a default
13634 * RPI reg/immediate unreg cycle. The reference was
13635 * taken in the reg rpi path and is released when
13636 * this mailbox completes.
13637 */
James Smart3e1f0712018-11-29 16:09:29 -080013638 pmb->ctx_ndlp = ndlp;
James Smart04c68492009-05-22 14:52:52 -040013639 pmb->vport = vport;
13640 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
13641 if (rc != MBX_BUSY)
Dick Kennedy372c1872020-06-30 14:50:00 -070013642 lpfc_printf_log(phba, KERN_ERR,
13643 LOG_TRACE_EVENT,
13644 "0385 rc should "
James Smart04c68492009-05-22 14:52:52 -040013645 "have been MBX_BUSY\n");
13646 if (rc != MBX_NOT_FINISHED)
13647 goto send_current_mbox;
13648 }
13649 }
13650 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
13651 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
13652 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
13653
James Smarta22d73b2021-01-04 10:02:38 -080013654 /* Do NOT queue MBX_HEARTBEAT to the worker thread for processing. */
13655 if (pmbox->mbxCommand == MBX_HEARTBEAT) {
13656 spin_lock_irqsave(&phba->hbalock, iflags);
13657 /* Release the mailbox command posting token */
13658 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13659 phba->sli.mbox_active = NULL;
13660 if (bf_get(lpfc_trailer_consumed, mcqe))
13661 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
13662 spin_unlock_irqrestore(&phba->hbalock, iflags);
13663
13664 /* Post the next mbox command, if there is one */
13665 lpfc_sli4_post_async_mbox(phba);
13666
13667 /* Process cmpl now */
13668 if (pmb->mbox_cmpl)
13669 pmb->mbox_cmpl(phba, pmb);
13670 return false;
13671 }
13672
13673 /* There is mailbox completion work to queue to the worker thread */
James Smart04c68492009-05-22 14:52:52 -040013674 spin_lock_irqsave(&phba->hbalock, iflags);
13675 __lpfc_mbox_cmpl_put(phba, pmb);
13676 phba->work_ha |= HA_MBATT;
13677 spin_unlock_irqrestore(&phba->hbalock, iflags);
13678 workposted = true;
13679
13680send_current_mbox:
13681 spin_lock_irqsave(&phba->hbalock, iflags);
13682 /* Release the mailbox command posting token */
13683 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
13684 /* Setting active mailbox pointer need to be in sync to flag clear */
13685 phba->sli.mbox_active = NULL;
James Smart07b85822019-09-21 20:58:53 -070013686 if (bf_get(lpfc_trailer_consumed, mcqe))
13687 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
James Smart04c68492009-05-22 14:52:52 -040013688 spin_unlock_irqrestore(&phba->hbalock, iflags);
13689 /* Wake up worker thread to post the next pending mailbox command */
13690 lpfc_worker_wake_up(phba);
James Smart07b85822019-09-21 20:58:53 -070013691 return workposted;
13692
James Smart04c68492009-05-22 14:52:52 -040013693out_no_mqe_complete:
James Smart07b85822019-09-21 20:58:53 -070013694 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart04c68492009-05-22 14:52:52 -040013695 if (bf_get(lpfc_trailer_consumed, mcqe))
13696 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
James Smart07b85822019-09-21 20:58:53 -070013697 spin_unlock_irqrestore(&phba->hbalock, iflags);
13698 return false;
James Smart04c68492009-05-22 14:52:52 -040013699}
13700
13701/**
13702 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
13703 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010013704 * @cq: Pointer to associated CQ
James Smart04c68492009-05-22 14:52:52 -040013705 * @cqe: Pointer to mailbox completion queue entry.
13706 *
13707 * This routine process a mailbox completion queue entry, it invokes the
Colin Ian King291c2542019-12-18 08:43:01 +000013708 * proper mailbox complete handling or asynchronous event handling routine
James Smart04c68492009-05-22 14:52:52 -040013709 * according to the MCQE's async bit.
13710 *
13711 * Return: true if work posted to worker thread, otherwise false.
13712 **/
13713static bool
James Smart32517fc2019-01-28 11:14:33 -080013714lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13715 struct lpfc_cqe *cqe)
James Smart04c68492009-05-22 14:52:52 -040013716{
13717 struct lpfc_mcqe mcqe;
13718 bool workposted;
13719
James Smart32517fc2019-01-28 11:14:33 -080013720 cq->CQ_mbox++;
13721
James Smart04c68492009-05-22 14:52:52 -040013722 /* Copy the mailbox MCQE and convert endian order as needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013723 lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
James Smart04c68492009-05-22 14:52:52 -040013724
13725 /* Invoke the proper event handling routine */
13726 if (!bf_get(lpfc_trailer_async, &mcqe))
13727 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
13728 else
13729 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
13730 return workposted;
13731}
13732
13733/**
James Smart4f774512009-05-22 14:52:35 -040013734 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
13735 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040013736 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040013737 * @wcqe: Pointer to work-queue completion queue entry.
13738 *
13739 * This routine handles an ELS work-queue completion event.
13740 *
13741 * Return: true if work posted to worker thread, otherwise false.
13742 **/
13743static bool
James Smart2a76a282012-08-03 12:35:54 -040013744lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013745 struct lpfc_wcqe_complete *wcqe)
13746{
James Smart4f774512009-05-22 14:52:35 -040013747 struct lpfc_iocbq *irspiocbq;
13748 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040013749 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050013750 int txq_cnt = 0;
13751 int txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040013752
James Smart11f0e342018-05-04 20:37:57 -070013753 /* Check for response status */
13754 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13755 /* Log the error status */
13756 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
13757 "0357 ELS CQE error: status=x%x: "
13758 "CQE: %08x %08x %08x %08x\n",
13759 bf_get(lpfc_wcqe_c_status, wcqe),
13760 wcqe->word0, wcqe->total_data_placed,
13761 wcqe->parameter, wcqe->word3);
13762 }
13763
James Smart45ed1192009-10-02 15:17:02 -040013764 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040013765 irspiocbq = lpfc_sli_get_iocbq(phba);
13766 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050013767 if (!list_empty(&pring->txq))
13768 txq_cnt++;
13769 if (!list_empty(&pring->txcmplq))
13770 txcmplq_cnt++;
Dick Kennedy372c1872020-06-30 14:50:00 -070013771 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2a9bf3d2010-06-07 15:24:45 -040013772 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
James Smartff349bc2019-09-21 20:59:05 -070013773 "els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050013774 txq_cnt, phba->iocb_cnt,
James Smart0e9bb8d2013-03-01 16:35:12 -050013775 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040013776 return false;
James Smart4f774512009-05-22 14:52:35 -040013777 }
James Smart4f774512009-05-22 14:52:35 -040013778
James Smart45ed1192009-10-02 15:17:02 -040013779 /* Save off the slow-path queue event for work thread to process */
13780 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040013781 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040013782 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013783 &phba->sli4_hba.sp_queue_event);
13784 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040013785 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013786
James Smart45ed1192009-10-02 15:17:02 -040013787 return true;
James Smart4f774512009-05-22 14:52:35 -040013788}
13789
13790/**
13791 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
13792 * @phba: Pointer to HBA context object.
13793 * @wcqe: Pointer to work-queue completion queue entry.
13794 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013795 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013796 * proper WQ release routine to the slow-path WQ.
13797 **/
13798static void
13799lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
13800 struct lpfc_wcqe_release *wcqe)
13801{
James Smart2e90f4b2011-12-13 13:22:37 -050013802 /* sanity check on queue memory */
13803 if (unlikely(!phba->sli4_hba.els_wq))
13804 return;
James Smart4f774512009-05-22 14:52:35 -040013805 /* Check for the slow-path ELS work queue */
13806 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
13807 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
13808 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13809 else
13810 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13811 "2579 Slow-path wqe consume event carries "
13812 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
13813 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
13814 phba->sli4_hba.els_wq->queue_id);
13815}
13816
13817/**
13818 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
13819 * @phba: Pointer to HBA context object.
13820 * @cq: Pointer to a WQ completion queue.
13821 * @wcqe: Pointer to work-queue completion queue entry.
13822 *
13823 * This routine handles an XRI abort event.
13824 *
13825 * Return: true if work posted to worker thread, otherwise false.
13826 **/
13827static bool
13828lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
13829 struct lpfc_queue *cq,
13830 struct sli4_wcqe_xri_aborted *wcqe)
13831{
13832 bool workposted = false;
13833 struct lpfc_cq_event *cq_event;
13834 unsigned long iflags;
13835
James Smart4f774512009-05-22 14:52:35 -040013836 switch (cq->subtype) {
James Smartc00f62e2019-08-14 16:57:11 -070013837 case LPFC_IO:
13838 lpfc_sli4_io_xri_aborted(phba, wcqe, cq->hdwq);
13839 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) {
13840 /* Notify aborted XRI for NVME work queue */
13841 if (phba->nvmet_support)
13842 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
13843 }
James Smart5e5b5112019-01-28 11:14:22 -080013844 workposted = false;
James Smart4f774512009-05-22 14:52:35 -040013845 break;
James Smart422c4cb2017-11-20 16:00:32 -080013846 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040013847 case LPFC_ELS:
James Smarte7dab162020-10-20 13:27:12 -070013848 cq_event = lpfc_cq_event_setup(phba, wcqe, sizeof(*wcqe));
13849 if (!cq_event) {
13850 workposted = false;
13851 break;
13852 }
James Smart5e5b5112019-01-28 11:14:22 -080013853 cq_event->hdwq = cq->hdwq;
James Smarte7dab162020-10-20 13:27:12 -070013854 spin_lock_irqsave(&phba->sli4_hba.els_xri_abrt_list_lock,
13855 iflags);
James Smart4f774512009-05-22 14:52:35 -040013856 list_add_tail(&cq_event->list,
13857 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
13858 /* Set the els xri abort event flag */
13859 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
James Smarte7dab162020-10-20 13:27:12 -070013860 spin_unlock_irqrestore(&phba->sli4_hba.els_xri_abrt_list_lock,
13861 iflags);
James Smart4f774512009-05-22 14:52:35 -040013862 workposted = true;
13863 break;
13864 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070013865 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart318083a2017-03-04 09:30:30 -080013866 "0603 Invalid CQ subtype %d: "
13867 "%08x %08x %08x %08x\n",
13868 cq->subtype, wcqe->word0, wcqe->parameter,
13869 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040013870 workposted = false;
13871 break;
13872 }
13873 return workposted;
13874}
13875
James Smarte817e5d2018-12-13 15:17:53 -080013876#define FC_RCTL_MDS_DIAGS 0xF4
13877
James Smart4f774512009-05-22 14:52:35 -040013878/**
James Smart4d9ab992009-10-02 15:16:39 -040013879 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040013880 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040013881 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013882 *
James Smart4d9ab992009-10-02 15:16:39 -040013883 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013884 *
13885 * Return: true if work posted to worker thread, otherwise false.
13886 **/
13887static bool
James Smart4d9ab992009-10-02 15:16:39 -040013888lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13889{
13890 bool workposted = false;
James Smarte817e5d2018-12-13 15:17:53 -080013891 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013892 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13893 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013894 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013895 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013896 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013897 unsigned long iflags;
13898
James Smart2e90f4b2011-12-13 13:22:37 -050013899 /* sanity check on queue memory */
13900 if (unlikely(!hrq) || unlikely(!drq))
13901 return workposted;
13902
James Smart7851fe22011-07-22 18:36:52 -040013903 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13904 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13905 else
13906 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13907 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013908 goto out;
13909
13910 status = bf_get(lpfc_rcqe_status, rcqe);
13911 switch (status) {
13912 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
Dick Kennedy372c1872020-06-30 14:50:00 -070013913 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4d9ab992009-10-02 15:16:39 -040013914 "2537 Receive Frame Truncated!!\n");
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050013915 fallthrough;
James Smart4d9ab992009-10-02 15:16:39 -040013916 case FC_STATUS_RQ_SUCCESS:
13917 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013918 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013919 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13920 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013921 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013922 spin_unlock_irqrestore(&phba->hbalock, iflags);
13923 goto out;
13924 }
James Smartb84daac2012-08-03 12:35:13 -040013925 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013926 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013927 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013928
James Smarte817e5d2018-12-13 15:17:53 -080013929 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13930
13931 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
13932 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
13933 spin_unlock_irqrestore(&phba->hbalock, iflags);
13934 /* Handle MDS Loopback frames */
Dick Kennedy24411fc2020-08-03 14:02:25 -070013935 if (!(phba->pport->load_flag & FC_UNLOADING))
13936 lpfc_sli4_handle_mds_loopback(phba->pport,
13937 dma_buf);
13938 else
13939 lpfc_in_buf_free(phba, &dma_buf->dbuf);
James Smarte817e5d2018-12-13 15:17:53 -080013940 break;
13941 }
13942
13943 /* save off the frame for the work thread to process */
James Smart4d9ab992009-10-02 15:16:39 -040013944 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013945 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013946 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013947 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013948 spin_unlock_irqrestore(&phba->hbalock, iflags);
13949 workposted = true;
13950 break;
James Smart4d9ab992009-10-02 15:16:39 -040013951 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013952 if (phba->nvmet_support) {
13953 tgtp = phba->targetport->private;
Dick Kennedy372c1872020-06-30 14:50:00 -070013954 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart547077a2017-05-15 15:20:40 -070013955 "6402 RQE Error x%x, posted %d err_cnt "
13956 "%d: %x %x %x\n",
13957 status, hrq->RQ_buf_posted,
13958 hrq->RQ_no_posted_buf,
13959 atomic_read(&tgtp->rcv_fcp_cmd_in),
13960 atomic_read(&tgtp->rcv_fcp_cmd_out),
13961 atomic_read(&tgtp->xmt_fcp_release));
13962 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050013963 fallthrough;
James Smart547077a2017-05-15 15:20:40 -070013964
13965 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013966 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013967 /* Post more buffers if possible */
13968 spin_lock_irqsave(&phba->hbalock, iflags);
13969 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13970 spin_unlock_irqrestore(&phba->hbalock, iflags);
13971 workposted = true;
13972 break;
13973 }
13974out:
13975 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013976}
13977
13978/**
13979 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13980 * @phba: Pointer to HBA context object.
13981 * @cq: Pointer to the completion queue.
James Smart32517fc2019-01-28 11:14:33 -080013982 * @cqe: Pointer to a completion queue entry.
James Smart4d9ab992009-10-02 15:16:39 -040013983 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013984 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013985 * entry.
13986 *
13987 * Return: true if work posted to worker thread, otherwise false.
13988 **/
13989static bool
13990lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013991 struct lpfc_cqe *cqe)
13992{
James Smart45ed1192009-10-02 15:17:02 -040013993 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013994 bool workposted = false;
13995
13996 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070013997 lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013998
13999 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040014000 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040014001 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040014002 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040014003 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040014004 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040014005 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040014006 break;
14007 case CQE_CODE_RELEASE_WQE:
14008 /* Process the WQ release event */
14009 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040014010 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040014011 break;
14012 case CQE_CODE_XRI_ABORTED:
14013 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040014014 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014015 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040014016 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040014017 break;
James Smart4d9ab992009-10-02 15:16:39 -040014018 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040014019 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040014020 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040014021 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040014022 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040014023 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040014024 break;
James Smart4f774512009-05-22 14:52:35 -040014025 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070014026 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040014027 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040014028 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040014029 break;
14030 }
14031 return workposted;
14032}
14033
14034/**
James Smart4f774512009-05-22 14:52:35 -040014035 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
14036 * @phba: Pointer to HBA context object.
14037 * @eqe: Pointer to fast-path event queue entry.
Lee Jones7af29d42020-07-21 17:41:31 +010014038 * @speq: Pointer to slow-path event queue.
James Smart4f774512009-05-22 14:52:35 -040014039 *
14040 * This routine process a event queue entry from the slow-path event queue.
14041 * It will check the MajorCode and MinorCode to determine this is for a
14042 * completion event on a completion queue, if not, an error shall be logged
14043 * and just return. Otherwise, it will get to the corresponding completion
14044 * queue and process all the entries on that completion queue, rearm the
14045 * completion queue, and then return.
14046 *
14047 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070014048static void
James Smart67d12732012-08-03 12:36:13 -040014049lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
14050 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040014051{
James Smart67d12732012-08-03 12:36:13 -040014052 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040014053 uint16_t cqid;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014054 int ret = 0;
James Smart4f774512009-05-22 14:52:35 -040014055
James Smart4f774512009-05-22 14:52:35 -040014056 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040014057 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040014058
James Smart4f774512009-05-22 14:52:35 -040014059 list_for_each_entry(childq, &speq->child_list, list) {
14060 if (childq->queue_id == cqid) {
14061 cq = childq;
14062 break;
14063 }
14064 }
14065 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040014066 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
Dick Kennedy372c1872020-06-30 14:50:00 -070014067 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart75baf692010-06-08 18:31:21 -040014068 "0365 Slow-path CQ identifier "
14069 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070014070 return;
James Smart4f774512009-05-22 14:52:35 -040014071 }
14072
James Smart895427b2017-02-12 13:52:30 -080014073 /* Save EQ associated with this CQ */
14074 cq->assoc_qp = speq;
14075
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014076 if (is_kdump_kernel())
14077 ret = queue_work(phba->wq, &cq->spwork);
14078 else
14079 ret = queue_work_on(cq->chann, phba->wq, &cq->spwork);
14080
14081 if (!ret)
Dick Kennedy372c1872020-06-30 14:50:00 -070014082 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014083 "0390 Cannot schedule queue work "
Dick Kennedyf485c182017-09-29 17:34:34 -070014084 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
Bart Van Assched6d189c2019-03-28 11:06:22 -070014085 cqid, cq->queue_id, raw_smp_processor_id());
Dick Kennedyf485c182017-09-29 17:34:34 -070014086}
14087
14088/**
James Smart32517fc2019-01-28 11:14:33 -080014089 * __lpfc_sli4_process_cq - Process elements of a CQ
Dick Kennedyf485c182017-09-29 17:34:34 -070014090 * @phba: Pointer to HBA context object.
James Smart32517fc2019-01-28 11:14:33 -080014091 * @cq: Pointer to CQ to be processed
14092 * @handler: Routine to process each cqe
14093 * @delay: Pointer to usdelay to set in case of rescheduling of the handler
Dick Kennedy317aeb82020-06-30 14:49:59 -070014094 * @poll_mode: Polling mode we were called from
Dick Kennedyf485c182017-09-29 17:34:34 -070014095 *
James Smart32517fc2019-01-28 11:14:33 -080014096 * This routine processes completion queue entries in a CQ. While a valid
14097 * queue element is found, the handler is called. During processing checks
14098 * are made for periodic doorbell writes to let the hardware know of
14099 * element consumption.
Dick Kennedyf485c182017-09-29 17:34:34 -070014100 *
James Smart32517fc2019-01-28 11:14:33 -080014101 * If the max limit on cqes to process is hit, or there are no more valid
14102 * entries, the loop stops. If we processed a sufficient number of elements,
14103 * meaning there is sufficient load, rather than rearming and generating
14104 * another interrupt, a cq rescheduling delay will be set. A delay of 0
14105 * indicates no rescheduling.
14106 *
14107 * Returns True if work scheduled, False otherwise.
Dick Kennedyf485c182017-09-29 17:34:34 -070014108 **/
James Smart32517fc2019-01-28 11:14:33 -080014109static bool
14110__lpfc_sli4_process_cq(struct lpfc_hba *phba, struct lpfc_queue *cq,
14111 bool (*handler)(struct lpfc_hba *, struct lpfc_queue *,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014112 struct lpfc_cqe *), unsigned long *delay,
14113 enum lpfc_poll_mode poll_mode)
Dick Kennedyf485c182017-09-29 17:34:34 -070014114{
Dick Kennedyf485c182017-09-29 17:34:34 -070014115 struct lpfc_cqe *cqe;
14116 bool workposted = false;
James Smart32517fc2019-01-28 11:14:33 -080014117 int count = 0, consumed = 0;
14118 bool arm = true;
14119
14120 /* default - no reschedule */
14121 *delay = 0;
14122
14123 if (cmpxchg(&cq->queue_claimed, 0, 1) != 0)
14124 goto rearm_and_exit;
Dick Kennedyf485c182017-09-29 17:34:34 -070014125
James Smart4f774512009-05-22 14:52:35 -040014126 /* Process all the entries to the CQ */
James Smartd74a89a2019-05-21 17:48:55 -070014127 cq->q_flag = 0;
James Smart32517fc2019-01-28 11:14:33 -080014128 cqe = lpfc_sli4_cq_get(cq);
14129 while (cqe) {
James Smart32517fc2019-01-28 11:14:33 -080014130 workposted |= handler(phba, cq, cqe);
14131 __lpfc_sli4_consume_cqe(phba, cq, cqe);
14132
14133 consumed++;
14134 if (!(++count % cq->max_proc_limit))
14135 break;
14136
14137 if (!(count % cq->notify_interval)) {
14138 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14139 LPFC_QUEUE_NOARM);
14140 consumed = 0;
James Smart8156d372019-10-18 14:18:26 -070014141 cq->assoc_qp->q_flag |= HBA_EQ_DELAY_CHK;
James Smart4f774512009-05-22 14:52:35 -040014142 }
James Smartb84daac2012-08-03 12:35:13 -040014143
James Smartd74a89a2019-05-21 17:48:55 -070014144 if (count == LPFC_NVMET_CQ_NOTIFY)
14145 cq->q_flag |= HBA_NVMET_CQ_NOTIFY;
14146
James Smart32517fc2019-01-28 11:14:33 -080014147 cqe = lpfc_sli4_cq_get(cq);
14148 }
14149 if (count >= phba->cfg_cq_poll_threshold) {
14150 *delay = 1;
14151 arm = false;
14152 }
14153
Dick Kennedy317aeb82020-06-30 14:49:59 -070014154 /* Note: complete the irq_poll softirq before rearming CQ */
14155 if (poll_mode == LPFC_IRQ_POLL)
14156 irq_poll_complete(&cq->iop);
14157
James Smart32517fc2019-01-28 11:14:33 -080014158 /* Track the max number of CQEs processed in 1 EQ */
14159 if (count > cq->CQ_max_cqe)
14160 cq->CQ_max_cqe = count;
14161
14162 cq->assoc_qp->EQ_cqe_cnt += count;
14163
14164 /* Catch the no cq entry condition */
14165 if (unlikely(count == 0))
14166 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
14167 "0369 No entry from completion queue "
14168 "qid=%d\n", cq->queue_id);
14169
Dick Kennedy164ba8d2020-05-01 14:43:03 -070014170 xchg(&cq->queue_claimed, 0);
James Smart32517fc2019-01-28 11:14:33 -080014171
14172rearm_and_exit:
14173 phba->sli4_hba.sli4_write_cq_db(phba, cq, consumed,
14174 arm ? LPFC_QUEUE_REARM : LPFC_QUEUE_NOARM);
14175
14176 return workposted;
14177}
14178
14179/**
Lee Jones8514e2f2021-03-03 14:46:18 +000014180 * __lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
James Smart32517fc2019-01-28 11:14:33 -080014181 * @cq: pointer to CQ to process
14182 *
14183 * This routine calls the cq processing routine with a handler specific
14184 * to the type of queue bound to it.
14185 *
14186 * The CQ routine returns two values: the first is the calling status,
14187 * which indicates whether work was queued to the background discovery
14188 * thread. If true, the routine should wakeup the discovery thread;
14189 * the second is the delay parameter. If non-zero, rather than rearming
14190 * the CQ and yet another interrupt, the CQ handler should be queued so
14191 * that it is processed in a subsequent polling action. The value of
14192 * the delay indicates when to reschedule it.
14193 **/
14194static void
14195__lpfc_sli4_sp_process_cq(struct lpfc_queue *cq)
14196{
14197 struct lpfc_hba *phba = cq->phba;
14198 unsigned long delay;
14199 bool workposted = false;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014200 int ret = 0;
James Smart32517fc2019-01-28 11:14:33 -080014201
14202 /* Process and rearm the CQ */
14203 switch (cq->type) {
14204 case LPFC_MCQ:
14205 workposted |= __lpfc_sli4_process_cq(phba, cq,
14206 lpfc_sli4_sp_handle_mcqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014207 &delay, LPFC_QUEUE_WORK);
James Smart32517fc2019-01-28 11:14:33 -080014208 break;
14209 case LPFC_WCQ:
James Smartc00f62e2019-08-14 16:57:11 -070014210 if (cq->subtype == LPFC_IO)
James Smart32517fc2019-01-28 11:14:33 -080014211 workposted |= __lpfc_sli4_process_cq(phba, cq,
14212 lpfc_sli4_fp_handle_cqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014213 &delay, LPFC_QUEUE_WORK);
James Smart32517fc2019-01-28 11:14:33 -080014214 else
14215 workposted |= __lpfc_sli4_process_cq(phba, cq,
14216 lpfc_sli4_sp_handle_cqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014217 &delay, LPFC_QUEUE_WORK);
James Smart4f774512009-05-22 14:52:35 -040014218 break;
14219 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070014220 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040014221 "0370 Invalid completion queue type (%d)\n",
14222 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070014223 return;
James Smart4f774512009-05-22 14:52:35 -040014224 }
14225
James Smart32517fc2019-01-28 11:14:33 -080014226 if (delay) {
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014227 if (is_kdump_kernel())
14228 ret = queue_delayed_work(phba->wq, &cq->sched_spwork,
14229 delay);
14230 else
14231 ret = queue_delayed_work_on(cq->chann, phba->wq,
14232 &cq->sched_spwork, delay);
14233 if (!ret)
Dick Kennedy372c1872020-06-30 14:50:00 -070014234 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014235 "0394 Cannot schedule queue work "
James Smart32517fc2019-01-28 11:14:33 -080014236 "for cqid=%d on CPU %d\n",
14237 cq->queue_id, cq->chann);
14238 }
James Smart4f774512009-05-22 14:52:35 -040014239
14240 /* wake up worker thread if there are works to be done */
14241 if (workposted)
14242 lpfc_worker_wake_up(phba);
14243}
14244
14245/**
James Smart32517fc2019-01-28 11:14:33 -080014246 * lpfc_sli4_sp_process_cq - slow-path work handler when started by
14247 * interrupt
14248 * @work: pointer to work element
14249 *
14250 * translates from the work handler and calls the slow-path handler.
14251 **/
14252static void
14253lpfc_sli4_sp_process_cq(struct work_struct *work)
14254{
14255 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, spwork);
14256
14257 __lpfc_sli4_sp_process_cq(cq);
14258}
14259
14260/**
14261 * lpfc_sli4_dly_sp_process_cq - slow-path work handler when started by timer
14262 * @work: pointer to work element
14263 *
14264 * translates from the work handler and calls the slow-path handler.
14265 **/
14266static void
14267lpfc_sli4_dly_sp_process_cq(struct work_struct *work)
14268{
14269 struct lpfc_queue *cq = container_of(to_delayed_work(work),
14270 struct lpfc_queue, sched_spwork);
14271
14272 __lpfc_sli4_sp_process_cq(cq);
14273}
14274
14275/**
James Smart4f774512009-05-22 14:52:35 -040014276 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040014277 * @phba: Pointer to HBA context object.
14278 * @cq: Pointer to associated CQ
14279 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040014280 *
14281 * This routine process a fast-path work queue completion entry from fast-path
14282 * event queue for FCP command response completion.
14283 **/
14284static void
James Smart2a76a282012-08-03 12:35:54 -040014285lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040014286 struct lpfc_wcqe_complete *wcqe)
14287{
James Smart2a76a282012-08-03 12:35:54 -040014288 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040014289 struct lpfc_iocbq *cmdiocbq;
14290 struct lpfc_iocbq irspiocbq;
14291 unsigned long iflags;
14292
James Smart4f774512009-05-22 14:52:35 -040014293 /* Check for response status */
14294 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
14295 /* If resource errors reported from HBA, reduce queue
14296 * depth of the SCSI device.
14297 */
James Smarte3d2b802012-08-14 14:25:43 -040014298 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
14299 IOSTAT_LOCAL_REJECT)) &&
14300 ((wcqe->parameter & IOERR_PARAM_MASK) ==
14301 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040014302 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040014303
Dick Kennedy28ed7372020-06-30 14:49:56 -070014304 /* Log the cmpl status */
James Smart11f0e342018-05-04 20:37:57 -070014305 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
Dick Kennedy28ed7372020-06-30 14:49:56 -070014306 "0373 FCP CQE cmpl: status=x%x: "
James Smart11f0e342018-05-04 20:37:57 -070014307 "CQE: %08x %08x %08x %08x\n",
James Smart4f774512009-05-22 14:52:35 -040014308 bf_get(lpfc_wcqe_c_status, wcqe),
James Smart11f0e342018-05-04 20:37:57 -070014309 wcqe->word0, wcqe->total_data_placed,
14310 wcqe->parameter, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040014311 }
14312
14313 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040014314 spin_lock_irqsave(&pring->ring_lock, iflags);
14315 pring->stats.iocb_event++;
James Smarte2a8be52019-05-06 17:26:47 -070014316 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040014317 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
14318 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart4f774512009-05-22 14:52:35 -040014319 if (unlikely(!cmdiocbq)) {
14320 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14321 "0374 FCP complete with no corresponding "
14322 "cmdiocb: iotag (%d)\n",
14323 bf_get(lpfc_wcqe_c_request_tag, wcqe));
14324 return;
14325 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070014326#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
14327 cmdiocbq->isr_timestamp = cq->isr_timestamp;
14328#endif
James Smart895427b2017-02-12 13:52:30 -080014329 if (cmdiocbq->iocb_cmpl == NULL) {
14330 if (cmdiocbq->wqe_cmpl) {
James Smart96e209b2020-11-15 11:26:43 -080014331 /* For FCP the flag is cleared in wqe_cmpl */
14332 if (!(cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
14333 cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
James Smart895427b2017-02-12 13:52:30 -080014334 spin_lock_irqsave(&phba->hbalock, iflags);
14335 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
14336 spin_unlock_irqrestore(&phba->hbalock, iflags);
14337 }
14338
14339 /* Pass the cmd_iocb and the wcqe to the upper layer */
14340 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
14341 return;
14342 }
James Smart4f774512009-05-22 14:52:35 -040014343 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14344 "0375 FCP cmdiocb not callback function "
14345 "iotag: (%d)\n",
14346 bf_get(lpfc_wcqe_c_request_tag, wcqe));
14347 return;
14348 }
14349
James Smart96e209b2020-11-15 11:26:43 -080014350 /* Only SLI4 non-IO commands stil use IOCB */
James Smart4f774512009-05-22 14:52:35 -040014351 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050014352 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040014353
James Smart0f65ff62010-02-26 14:14:23 -050014354 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
14355 spin_lock_irqsave(&phba->hbalock, iflags);
14356 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
14357 spin_unlock_irqrestore(&phba->hbalock, iflags);
14358 }
14359
James Smart4f774512009-05-22 14:52:35 -040014360 /* Pass the cmd_iocb and the rsp state to the upper layer */
14361 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
14362}
14363
14364/**
14365 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
14366 * @phba: Pointer to HBA context object.
14367 * @cq: Pointer to completion queue.
14368 * @wcqe: Pointer to work-queue completion queue entry.
14369 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080014370 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040014371 * proper WQ release routine to the slow-path WQ.
14372 **/
14373static void
14374lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14375 struct lpfc_wcqe_release *wcqe)
14376{
14377 struct lpfc_queue *childwq;
14378 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080014379 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040014380
14381 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080014382 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040014383 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080014384 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040014385 lpfc_sli4_wq_release(childwq,
14386 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080014387 if (childwq->q_flag & HBA_NVMET_WQFULL)
14388 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040014389 wqid_matched = true;
14390 break;
14391 }
14392 }
14393 /* Report warning log message if no match found */
14394 if (wqid_matched != true)
14395 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14396 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080014397 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040014398}
14399
14400/**
James Smart2d7dbc42017-02-12 13:52:35 -080014401 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
14402 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010014403 * @cq: Pointer to completion queue.
James Smart2d7dbc42017-02-12 13:52:35 -080014404 * @rcqe: Pointer to receive-queue completion queue entry.
14405 *
14406 * This routine process a receive-queue completion queue entry.
14407 *
14408 * Return: true if work posted to worker thread, otherwise false.
14409 **/
14410static bool
14411lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
14412 struct lpfc_rcqe *rcqe)
14413{
14414 bool workposted = false;
14415 struct lpfc_queue *hrq;
14416 struct lpfc_queue *drq;
14417 struct rqb_dmabuf *dma_buf;
14418 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070014419 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080014420 uint32_t status, rq_id;
14421 unsigned long iflags;
14422 uint32_t fctl, idx;
14423
14424 if ((phba->nvmet_support == 0) ||
14425 (phba->sli4_hba.nvmet_cqset == NULL))
14426 return workposted;
14427
14428 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
14429 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
14430 drq = phba->sli4_hba.nvmet_mrq_data[idx];
14431
14432 /* sanity check on queue memory */
14433 if (unlikely(!hrq) || unlikely(!drq))
14434 return workposted;
14435
14436 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
14437 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
14438 else
14439 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
14440
14441 if ((phba->nvmet_support == 0) ||
14442 (rq_id != hrq->queue_id))
14443 return workposted;
14444
14445 status = bf_get(lpfc_rcqe_status, rcqe);
14446 switch (status) {
14447 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
Dick Kennedy372c1872020-06-30 14:50:00 -070014448 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080014449 "6126 Receive Frame Truncated!!\n");
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050014450 fallthrough;
James Smart2d7dbc42017-02-12 13:52:35 -080014451 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080014452 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080014453 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080014454 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
14455 if (!dma_buf) {
14456 hrq->RQ_no_buf_found++;
14457 spin_unlock_irqrestore(&phba->hbalock, iflags);
14458 goto out;
14459 }
14460 spin_unlock_irqrestore(&phba->hbalock, iflags);
14461 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070014462 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080014463 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
14464
14465 /* Just some basic sanity checks on FCP Command frame */
14466 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
James Smart3a8070c2020-03-31 09:50:05 -070014467 fc_hdr->fh_f_ctl[1] << 8 |
14468 fc_hdr->fh_f_ctl[2]);
James Smart2d7dbc42017-02-12 13:52:35 -080014469 if (((fctl &
14470 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
14471 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
14472 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
14473 goto drop;
14474
14475 if (fc_hdr->fh_type == FC_TYPE_FCP) {
James Smartd74a89a2019-05-21 17:48:55 -070014476 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080014477 lpfc_nvmet_unsol_fcp_event(
James Smartd74a89a2019-05-21 17:48:55 -070014478 phba, idx, dma_buf, cq->isr_timestamp,
14479 cq->q_flag & HBA_NVMET_CQ_NOTIFY);
James Smart2d7dbc42017-02-12 13:52:35 -080014480 return false;
14481 }
14482drop:
James Smart22b738a2019-03-12 16:30:11 -070014483 lpfc_rq_buf_free(phba, &dma_buf->hbuf);
James Smart2d7dbc42017-02-12 13:52:35 -080014484 break;
James Smart2d7dbc42017-02-12 13:52:35 -080014485 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070014486 if (phba->nvmet_support) {
14487 tgtp = phba->targetport->private;
Dick Kennedy372c1872020-06-30 14:50:00 -070014488 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart547077a2017-05-15 15:20:40 -070014489 "6401 RQE Error x%x, posted %d err_cnt "
14490 "%d: %x %x %x\n",
14491 status, hrq->RQ_buf_posted,
14492 hrq->RQ_no_posted_buf,
14493 atomic_read(&tgtp->rcv_fcp_cmd_in),
14494 atomic_read(&tgtp->rcv_fcp_cmd_out),
14495 atomic_read(&tgtp->xmt_fcp_release));
14496 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050014497 fallthrough;
James Smart547077a2017-05-15 15:20:40 -070014498
14499 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080014500 hrq->RQ_no_posted_buf++;
14501 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080014502 break;
14503 }
14504out:
14505 return workposted;
14506}
14507
14508/**
James Smart895427b2017-02-12 13:52:30 -080014509 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart32517fc2019-01-28 11:14:33 -080014510 * @phba: adapter with cq
James Smart4f774512009-05-22 14:52:35 -040014511 * @cq: Pointer to the completion queue.
Lee Jones7af29d42020-07-21 17:41:31 +010014512 * @cqe: Pointer to fast-path completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040014513 *
14514 * This routine process a fast-path work queue completion entry from fast-path
14515 * event queue for FCP command response completion.
James Smart32517fc2019-01-28 11:14:33 -080014516 *
14517 * Return: true if work posted to worker thread, otherwise false.
James Smart4f774512009-05-22 14:52:35 -040014518 **/
James Smart32517fc2019-01-28 11:14:33 -080014519static bool
James Smart895427b2017-02-12 13:52:30 -080014520lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040014521 struct lpfc_cqe *cqe)
14522{
14523 struct lpfc_wcqe_release wcqe;
14524 bool workposted = false;
14525
14526 /* Copy the work queue CQE and convert endian order if needed */
James Smart48f8fdb2018-05-04 20:37:51 -070014527 lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040014528
14529 /* Check and process for different type of WCQE and dispatch */
14530 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
14531 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080014532 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040014533 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040014534 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040014535 phba->last_completion_time = jiffies;
James Smartc00f62e2019-08-14 16:57:11 -070014536 if (cq->subtype == LPFC_IO || cq->subtype == LPFC_NVME_LS)
James Smart895427b2017-02-12 13:52:30 -080014537 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040014538 (struct lpfc_wcqe_complete *)&wcqe);
14539 break;
14540 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040014541 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040014542 /* Process the WQ release event */
14543 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
14544 (struct lpfc_wcqe_release *)&wcqe);
14545 break;
14546 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040014547 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040014548 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040014549 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040014550 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
14551 (struct sli4_wcqe_xri_aborted *)&wcqe);
14552 break;
James Smart895427b2017-02-12 13:52:30 -080014553 case CQE_CODE_RECEIVE_V1:
14554 case CQE_CODE_RECEIVE:
14555 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080014556 if (cq->subtype == LPFC_NVMET) {
14557 workposted = lpfc_sli4_nvmet_handle_rcqe(
14558 phba, cq, (struct lpfc_rcqe *)&wcqe);
14559 }
James Smart895427b2017-02-12 13:52:30 -080014560 break;
James Smart4f774512009-05-22 14:52:35 -040014561 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070014562 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart895427b2017-02-12 13:52:30 -080014563 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040014564 bf_get(lpfc_wcqe_c_code, &wcqe));
14565 break;
14566 }
14567 return workposted;
14568}
14569
14570/**
Dick Kennedy317aeb82020-06-30 14:49:59 -070014571 * lpfc_sli4_sched_cq_work - Schedules cq work
14572 * @phba: Pointer to HBA context object.
14573 * @cq: Pointer to CQ
14574 * @cqid: CQ ID
14575 *
14576 * This routine checks the poll mode of the CQ corresponding to
14577 * cq->chann, then either schedules a softirq or queue_work to complete
14578 * cq work.
14579 *
14580 * queue_work path is taken if in NVMET mode, or if poll_mode is in
14581 * LPFC_QUEUE_WORK mode. Otherwise, softirq path is taken.
14582 *
14583 **/
14584static void lpfc_sli4_sched_cq_work(struct lpfc_hba *phba,
14585 struct lpfc_queue *cq, uint16_t cqid)
14586{
14587 int ret = 0;
14588
14589 switch (cq->poll_mode) {
14590 case LPFC_IRQ_POLL:
14591 irq_poll_sched(&cq->iop);
14592 break;
14593 case LPFC_QUEUE_WORK:
14594 default:
14595 if (is_kdump_kernel())
14596 ret = queue_work(phba->wq, &cq->irqwork);
14597 else
14598 ret = queue_work_on(cq->chann, phba->wq, &cq->irqwork);
14599 if (!ret)
Dick Kennedy372c1872020-06-30 14:50:00 -070014600 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014601 "0383 Cannot schedule queue work "
14602 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
14603 cqid, cq->queue_id,
14604 raw_smp_processor_id());
14605 }
14606}
14607
14608/**
James Smart67d12732012-08-03 12:36:13 -040014609 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040014610 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010014611 * @eq: Pointer to the queue structure.
James Smart4f774512009-05-22 14:52:35 -040014612 * @eqe: Pointer to fast-path event queue entry.
14613 *
14614 * This routine process a event queue entry from the fast-path event queue.
14615 * It will check the MajorCode and MinorCode to determine this is for a
14616 * completion event on a completion queue, if not, an error shall be logged
14617 * and just return. Otherwise, it will get to the corresponding completion
14618 * queue and process all the entries on the completion queue, rearm the
14619 * completion queue, and then return.
14620 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070014621static void
James Smart32517fc2019-01-28 11:14:33 -080014622lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_queue *eq,
14623 struct lpfc_eqe *eqe)
James Smart4f774512009-05-22 14:52:35 -040014624{
James Smart895427b2017-02-12 13:52:30 -080014625 struct lpfc_queue *cq = NULL;
James Smart32517fc2019-01-28 11:14:33 -080014626 uint32_t qidx = eq->hdwq;
James Smart2d7dbc42017-02-12 13:52:35 -080014627 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040014628
James Smartcb5172e2010-03-15 11:25:07 -040014629 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014630 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart67d12732012-08-03 12:36:13 -040014631 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040014632 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040014633 bf_get_le32(lpfc_eqe_major_code, eqe),
14634 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070014635 return;
James Smart4f774512009-05-22 14:52:35 -040014636 }
14637
James Smart67d12732012-08-03 12:36:13 -040014638 /* Get the reference to the corresponding CQ */
14639 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
14640
James Smart6a828b02019-01-28 11:14:31 -080014641 /* Use the fast lookup method first */
14642 if (cqid <= phba->sli4_hba.cq_max) {
14643 cq = phba->sli4_hba.cq_lookup[cqid];
14644 if (cq)
14645 goto work_cq;
James Smartcdb42be2019-01-28 11:14:21 -080014646 }
14647
14648 /* Next check for NVMET completion */
James Smart2d7dbc42017-02-12 13:52:35 -080014649 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
14650 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
14651 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
14652 /* Process NVMET unsol rcv */
14653 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
14654 goto process_cq;
14655 }
14656 }
14657
James Smart895427b2017-02-12 13:52:30 -080014658 if (phba->sli4_hba.nvmels_cq &&
14659 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
14660 /* Process NVME unsol rcv */
14661 cq = phba->sli4_hba.nvmels_cq;
14662 }
14663
14664 /* Otherwise this is a Slow path event */
14665 if (cq == NULL) {
James Smartcdb42be2019-01-28 11:14:21 -080014666 lpfc_sli4_sp_handle_eqe(phba, eqe,
14667 phba->sli4_hba.hdwq[qidx].hba_eq);
Dick Kennedyf485c182017-09-29 17:34:34 -070014668 return;
James Smart67d12732012-08-03 12:36:13 -040014669 }
14670
James Smart895427b2017-02-12 13:52:30 -080014671process_cq:
James Smart4f774512009-05-22 14:52:35 -040014672 if (unlikely(cqid != cq->queue_id)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070014673 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040014674 "0368 Miss-matched fast-path completion "
14675 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
14676 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070014677 return;
James Smart4f774512009-05-22 14:52:35 -040014678 }
14679
James Smart6a828b02019-01-28 11:14:31 -080014680work_cq:
James Smartd74a89a2019-05-21 17:48:55 -070014681#if defined(CONFIG_SCSI_LPFC_DEBUG_FS)
14682 if (phba->ktime_on)
14683 cq->isr_timestamp = ktime_get_ns();
14684 else
14685 cq->isr_timestamp = 0;
14686#endif
Dick Kennedy317aeb82020-06-30 14:49:59 -070014687 lpfc_sli4_sched_cq_work(phba, cq, cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070014688}
14689
14690/**
James Smart32517fc2019-01-28 11:14:33 -080014691 * __lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
14692 * @cq: Pointer to CQ to be processed
Dick Kennedy317aeb82020-06-30 14:49:59 -070014693 * @poll_mode: Enum lpfc_poll_state to determine poll mode
Dick Kennedyf485c182017-09-29 17:34:34 -070014694 *
James Smart32517fc2019-01-28 11:14:33 -080014695 * This routine calls the cq processing routine with the handler for
14696 * fast path CQEs.
14697 *
14698 * The CQ routine returns two values: the first is the calling status,
14699 * which indicates whether work was queued to the background discovery
14700 * thread. If true, the routine should wakeup the discovery thread;
14701 * the second is the delay parameter. If non-zero, rather than rearming
14702 * the CQ and yet another interrupt, the CQ handler should be queued so
14703 * that it is processed in a subsequent polling action. The value of
14704 * the delay indicates when to reschedule it.
Dick Kennedyf485c182017-09-29 17:34:34 -070014705 **/
14706static void
Dick Kennedy317aeb82020-06-30 14:49:59 -070014707__lpfc_sli4_hba_process_cq(struct lpfc_queue *cq,
14708 enum lpfc_poll_mode poll_mode)
Dick Kennedyf485c182017-09-29 17:34:34 -070014709{
Dick Kennedyf485c182017-09-29 17:34:34 -070014710 struct lpfc_hba *phba = cq->phba;
James Smart32517fc2019-01-28 11:14:33 -080014711 unsigned long delay;
Dick Kennedyf485c182017-09-29 17:34:34 -070014712 bool workposted = false;
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014713 int ret = 0;
Dick Kennedyf485c182017-09-29 17:34:34 -070014714
James Smart32517fc2019-01-28 11:14:33 -080014715 /* process and rearm the CQ */
14716 workposted |= __lpfc_sli4_process_cq(phba, cq, lpfc_sli4_fp_handle_cqe,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014717 &delay, poll_mode);
James Smart32517fc2019-01-28 11:14:33 -080014718
14719 if (delay) {
Dick Kennedy86ee57a2020-06-30 14:49:55 -070014720 if (is_kdump_kernel())
14721 ret = queue_delayed_work(phba->wq, &cq->sched_irqwork,
14722 delay);
14723 else
14724 ret = queue_delayed_work_on(cq->chann, phba->wq,
14725 &cq->sched_irqwork, delay);
14726 if (!ret)
Dick Kennedy372c1872020-06-30 14:50:00 -070014727 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
Dick Kennedy317aeb82020-06-30 14:49:59 -070014728 "0367 Cannot schedule queue work "
14729 "for cqid=%d on CPU %d\n",
14730 cq->queue_id, cq->chann);
James Smart4f774512009-05-22 14:52:35 -040014731 }
14732
James Smart4f774512009-05-22 14:52:35 -040014733 /* wake up worker thread if there are works to be done */
14734 if (workposted)
14735 lpfc_worker_wake_up(phba);
14736}
14737
James Smart1ba981f2014-02-20 09:56:45 -050014738/**
James Smart32517fc2019-01-28 11:14:33 -080014739 * lpfc_sli4_hba_process_cq - fast-path work handler when started by
14740 * interrupt
14741 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050014742 *
James Smart32517fc2019-01-28 11:14:33 -080014743 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050014744 **/
14745static void
James Smart32517fc2019-01-28 11:14:33 -080014746lpfc_sli4_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050014747{
James Smart32517fc2019-01-28 11:14:33 -080014748 struct lpfc_queue *cq = container_of(work, struct lpfc_queue, irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050014749
Dick Kennedy317aeb82020-06-30 14:49:59 -070014750 __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
James Smart1ba981f2014-02-20 09:56:45 -050014751}
14752
14753/**
Lee Jones8514e2f2021-03-03 14:46:18 +000014754 * lpfc_sli4_dly_hba_process_cq - fast-path work handler when started by timer
James Smart32517fc2019-01-28 11:14:33 -080014755 * @work: pointer to work element
James Smart1ba981f2014-02-20 09:56:45 -050014756 *
James Smart32517fc2019-01-28 11:14:33 -080014757 * translates from the work handler and calls the fast-path handler.
James Smart1ba981f2014-02-20 09:56:45 -050014758 **/
James Smart32517fc2019-01-28 11:14:33 -080014759static void
14760lpfc_sli4_dly_hba_process_cq(struct work_struct *work)
James Smart1ba981f2014-02-20 09:56:45 -050014761{
James Smart32517fc2019-01-28 11:14:33 -080014762 struct lpfc_queue *cq = container_of(to_delayed_work(work),
14763 struct lpfc_queue, sched_irqwork);
James Smart1ba981f2014-02-20 09:56:45 -050014764
Dick Kennedy317aeb82020-06-30 14:49:59 -070014765 __lpfc_sli4_hba_process_cq(cq, LPFC_QUEUE_WORK);
James Smart1ba981f2014-02-20 09:56:45 -050014766}
14767
James Smart4f774512009-05-22 14:52:35 -040014768/**
James Smart67d12732012-08-03 12:36:13 -040014769 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040014770 * @irq: Interrupt number.
14771 * @dev_id: The device context pointer.
14772 *
14773 * This function is directly called from the PCI layer as an interrupt
14774 * service routine when device with SLI-4 interface spec is enabled with
14775 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
14776 * ring event in the HBA. However, when the device is enabled with either
14777 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
14778 * device-level interrupt handler. When the PCI slot is in error recovery
14779 * or the HBA is undergoing initialization, the interrupt handler will not
14780 * process the interrupt. The SCSI FCP fast-path ring event are handled in
14781 * the intrrupt context. This function is called without any lock held.
14782 * It gets the hbalock to access and update SLI data structures. Note that,
14783 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
14784 * equal to that of FCP CQ index.
14785 *
James Smart67d12732012-08-03 12:36:13 -040014786 * The link attention and ELS ring attention events are handled
14787 * by the worker thread. The interrupt handler signals the worker thread
14788 * and returns for these events. This function is called without any lock
14789 * held. It gets the hbalock to access and update SLI data structures.
14790 *
James Smart4f774512009-05-22 14:52:35 -040014791 * This function returns IRQ_HANDLED when interrupt is handled else it
14792 * returns IRQ_NONE.
14793 **/
14794irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040014795lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040014796{
14797 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080014798 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040014799 struct lpfc_queue *fpeq;
James Smart4f774512009-05-22 14:52:35 -040014800 unsigned long iflag;
14801 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080014802 int hba_eqidx;
James Smart32517fc2019-01-28 11:14:33 -080014803 struct lpfc_eq_intr_info *eqi;
James Smart4f774512009-05-22 14:52:35 -040014804
14805 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080014806 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
14807 phba = hba_eq_hdl->phba;
14808 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040014809
14810 if (unlikely(!phba))
14811 return IRQ_NONE;
James Smartcdb42be2019-01-28 11:14:21 -080014812 if (unlikely(!phba->sli4_hba.hdwq))
James Smart5350d872011-10-10 21:33:49 -040014813 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014814
14815 /* Get to the EQ struct associated with this vector */
James Smart657add42019-05-21 17:49:06 -070014816 fpeq = phba->sli4_hba.hba_eq_hdl[hba_eqidx].eq;
James Smart2e90f4b2011-12-13 13:22:37 -050014817 if (unlikely(!fpeq))
14818 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014819
14820 /* Check device state for handling interrupt */
14821 if (unlikely(lpfc_intr_state_check(phba))) {
14822 /* Check again for link_state with lock held */
14823 spin_lock_irqsave(&phba->hbalock, iflag);
14824 if (phba->link_state < LPFC_LINK_DOWN)
14825 /* Flush, clear interrupt, and rearm the EQ */
James Smart24c7c0a2019-09-21 20:58:58 -070014826 lpfc_sli4_eqcq_flush(phba, fpeq);
James Smart4f774512009-05-22 14:52:35 -040014827 spin_unlock_irqrestore(&phba->hbalock, iflag);
14828 return IRQ_NONE;
14829 }
14830
Dick Kennedya7fc0712020-05-01 14:43:08 -070014831 eqi = this_cpu_ptr(phba->sli4_hba.eq_info);
14832 eqi->icnt++;
14833
Bart Van Assched6d189c2019-03-28 11:06:22 -070014834 fpeq->last_cpu = raw_smp_processor_id();
James Smart4f774512009-05-22 14:52:35 -040014835
Dick Kennedya7fc0712020-05-01 14:43:08 -070014836 if (eqi->icnt > LPFC_EQD_ISR_TRIGGER &&
James Smart8156d372019-10-18 14:18:26 -070014837 fpeq->q_flag & HBA_EQ_DELAY_CHK &&
James Smart32517fc2019-01-28 11:14:33 -080014838 phba->cfg_auto_imax &&
14839 fpeq->q_mode != LPFC_MAX_AUTO_EQ_DELAY &&
14840 phba->sli.sli_flag & LPFC_SLI_USE_EQDR)
14841 lpfc_sli4_mod_hba_eq_delay(phba, fpeq, LPFC_MAX_AUTO_EQ_DELAY);
James Smartb84daac2012-08-03 12:35:13 -040014842
James Smart32517fc2019-01-28 11:14:33 -080014843 /* process and rearm the EQ */
James Smart93a4d6f2019-11-04 16:57:05 -080014844 ecount = lpfc_sli4_process_eq(phba, fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040014845
14846 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040014847 fpeq->EQ_no_entry++;
James Smart4f774512009-05-22 14:52:35 -040014848 if (phba->intr_type == MSIX)
14849 /* MSI-X treated interrupt served as no EQ share INT */
14850 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14851 "0358 MSI-X interrupt with no EQE\n");
14852 else
14853 /* Non MSI-X treated on interrupt as EQ share INT */
14854 return IRQ_NONE;
14855 }
14856
14857 return IRQ_HANDLED;
14858} /* lpfc_sli4_fp_intr_handler */
14859
14860/**
14861 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
14862 * @irq: Interrupt number.
14863 * @dev_id: The device context pointer.
14864 *
14865 * This function is the device-level interrupt handler to device with SLI-4
14866 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
14867 * interrupt mode is enabled and there is an event in the HBA which requires
14868 * driver attention. This function invokes the slow-path interrupt attention
14869 * handling function and fast-path interrupt attention handling function in
14870 * turn to process the relevant HBA attention events. This function is called
14871 * without any lock held. It gets the hbalock to access and update SLI data
14872 * structures.
14873 *
14874 * This function returns IRQ_HANDLED when interrupt is handled, else it
14875 * returns IRQ_NONE.
14876 **/
14877irqreturn_t
14878lpfc_sli4_intr_handler(int irq, void *dev_id)
14879{
14880 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040014881 irqreturn_t hba_irq_rc;
14882 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080014883 int qidx;
James Smart4f774512009-05-22 14:52:35 -040014884
14885 /* Get the driver's phba structure from the dev_id */
14886 phba = (struct lpfc_hba *)dev_id;
14887
14888 if (unlikely(!phba))
14889 return IRQ_NONE;
14890
14891 /*
James Smart4f774512009-05-22 14:52:35 -040014892 * Invoke fast-path host attention interrupt handling as appropriate.
14893 */
James Smart6a828b02019-01-28 11:14:31 -080014894 for (qidx = 0; qidx < phba->cfg_irq_chann; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014895 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014896 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014897 if (hba_irq_rc == IRQ_HANDLED)
14898 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014899 }
14900
James Smart67d12732012-08-03 12:36:13 -040014901 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014902} /* lpfc_sli4_intr_handler */
14903
James Smart93a4d6f2019-11-04 16:57:05 -080014904void lpfc_sli4_poll_hbtimer(struct timer_list *t)
14905{
14906 struct lpfc_hba *phba = from_timer(phba, t, cpuhp_poll_timer);
14907 struct lpfc_queue *eq;
14908 int i = 0;
14909
14910 rcu_read_lock();
14911
14912 list_for_each_entry_rcu(eq, &phba->poll_list, _poll_list)
14913 i += lpfc_sli4_poll_eq(eq, LPFC_POLL_SLOWPATH);
14914 if (!list_empty(&phba->poll_list))
14915 mod_timer(&phba->cpuhp_poll_timer,
14916 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
14917
14918 rcu_read_unlock();
14919}
14920
14921inline int lpfc_sli4_poll_eq(struct lpfc_queue *eq, uint8_t path)
14922{
14923 struct lpfc_hba *phba = eq->phba;
14924 int i = 0;
14925
14926 /*
14927 * Unlocking an irq is one of the entry point to check
14928 * for re-schedule, but we are good for io submission
14929 * path as midlayer does a get_cpu to glue us in. Flush
14930 * out the invalidate queue so we can see the updated
14931 * value for flag.
14932 */
14933 smp_rmb();
14934
14935 if (READ_ONCE(eq->mode) == LPFC_EQ_POLL)
14936 /* We will not likely get the completion for the caller
14937 * during this iteration but i guess that's fine.
14938 * Future io's coming on this eq should be able to
14939 * pick it up. As for the case of single io's, they
14940 * will be handled through a sched from polling timer
14941 * function which is currently triggered every 1msec.
14942 */
14943 i = lpfc_sli4_process_eq(phba, eq, LPFC_QUEUE_NOARM);
14944
14945 return i;
14946}
14947
14948static inline void lpfc_sli4_add_to_poll_list(struct lpfc_queue *eq)
14949{
14950 struct lpfc_hba *phba = eq->phba;
14951
James Smartf861f592020-03-22 11:12:54 -070014952 /* kickstart slowpath processing if needed */
14953 if (list_empty(&phba->poll_list))
James Smart93a4d6f2019-11-04 16:57:05 -080014954 mod_timer(&phba->cpuhp_poll_timer,
14955 jiffies + msecs_to_jiffies(LPFC_POLL_HB));
James Smart93a4d6f2019-11-04 16:57:05 -080014956
14957 list_add_rcu(&eq->_poll_list, &phba->poll_list);
14958 synchronize_rcu();
14959}
14960
14961static inline void lpfc_sli4_remove_from_poll_list(struct lpfc_queue *eq)
14962{
14963 struct lpfc_hba *phba = eq->phba;
14964
14965 /* Disable slowpath processing for this eq. Kick start the eq
14966 * by RE-ARMING the eq's ASAP
14967 */
14968 list_del_rcu(&eq->_poll_list);
14969 synchronize_rcu();
14970
14971 if (list_empty(&phba->poll_list))
14972 del_timer_sync(&phba->cpuhp_poll_timer);
14973}
14974
James Smartd480e572019-11-11 15:03:58 -080014975void lpfc_sli4_cleanup_poll_list(struct lpfc_hba *phba)
James Smart93a4d6f2019-11-04 16:57:05 -080014976{
14977 struct lpfc_queue *eq, *next;
14978
14979 list_for_each_entry_safe(eq, next, &phba->poll_list, _poll_list)
14980 list_del(&eq->_poll_list);
14981
14982 INIT_LIST_HEAD(&phba->poll_list);
14983 synchronize_rcu();
14984}
14985
14986static inline void
14987__lpfc_sli4_switch_eqmode(struct lpfc_queue *eq, uint8_t mode)
14988{
14989 if (mode == eq->mode)
14990 return;
14991 /*
14992 * currently this function is only called during a hotplug
14993 * event and the cpu on which this function is executing
14994 * is going offline. By now the hotplug has instructed
14995 * the scheduler to remove this cpu from cpu active mask.
14996 * So we don't need to work about being put aside by the
14997 * scheduler for a high priority process. Yes, the inte-
14998 * rrupts could come but they are known to retire ASAP.
14999 */
15000
15001 /* Disable polling in the fastpath */
15002 WRITE_ONCE(eq->mode, mode);
15003 /* flush out the store buffer */
15004 smp_wmb();
15005
15006 /*
15007 * Add this eq to the polling list and start polling. For
15008 * a grace period both interrupt handler and poller will
15009 * try to process the eq _but_ that's fine. We have a
15010 * synchronization mechanism in place (queue_claimed) to
15011 * deal with it. This is just a draining phase for int-
15012 * errupt handler (not eq's) as we have guranteed through
15013 * barrier that all the CPUs have seen the new CQ_POLLED
15014 * state. which will effectively disable the REARMING of
15015 * the EQ. The whole idea is eq's die off eventually as
15016 * we are not rearming EQ's anymore.
15017 */
15018 mode ? lpfc_sli4_add_to_poll_list(eq) :
15019 lpfc_sli4_remove_from_poll_list(eq);
15020}
15021
15022void lpfc_sli4_start_polling(struct lpfc_queue *eq)
15023{
15024 __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_POLL);
15025}
15026
15027void lpfc_sli4_stop_polling(struct lpfc_queue *eq)
15028{
15029 struct lpfc_hba *phba = eq->phba;
15030
15031 __lpfc_sli4_switch_eqmode(eq, LPFC_EQ_INTERRUPT);
15032
15033 /* Kick start for the pending io's in h/w.
15034 * Once we switch back to interrupt processing on a eq
15035 * the io path completion will only arm eq's when it
15036 * receives a completion. But since eq's are in disa-
15037 * rmed state it doesn't receive a completion. This
15038 * creates a deadlock scenaro.
15039 */
15040 phba->sli4_hba.sli4_write_eq_db(phba, eq, 0, LPFC_QUEUE_REARM);
15041}
15042
James Smart4f774512009-05-22 14:52:35 -040015043/**
15044 * lpfc_sli4_queue_free - free a queue structure and associated memory
15045 * @queue: The queue structure to free.
15046 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040015047 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040015048 * the host resident queue. This function must be called after destroying the
15049 * queue on the HBA.
15050 **/
15051void
15052lpfc_sli4_queue_free(struct lpfc_queue *queue)
15053{
15054 struct lpfc_dmabuf *dmabuf;
15055
15056 if (!queue)
15057 return;
15058
James Smart4645f7b2019-03-12 16:30:14 -070015059 if (!list_empty(&queue->wq_list))
15060 list_del(&queue->wq_list);
15061
James Smart4f774512009-05-22 14:52:35 -040015062 while (!list_empty(&queue->page_list)) {
15063 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
15064 list);
James Smart81b96ed2017-11-20 16:00:29 -080015065 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040015066 dmabuf->virt, dmabuf->phys);
15067 kfree(dmabuf);
15068 }
James Smart895427b2017-02-12 13:52:30 -080015069 if (queue->rqbp) {
15070 lpfc_free_rq_buffer(queue->phba, queue);
15071 kfree(queue->rqbp);
15072 }
James Smartd1f525a2017-04-21 16:04:55 -070015073
James Smart32517fc2019-01-28 11:14:33 -080015074 if (!list_empty(&queue->cpu_list))
15075 list_del(&queue->cpu_list);
15076
James Smart4f774512009-05-22 14:52:35 -040015077 kfree(queue);
15078 return;
15079}
15080
15081/**
15082 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
15083 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080015084 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040015085 * @entry_size: The size of each queue entry for this queue.
Lee Jones7af29d42020-07-21 17:41:31 +010015086 * @entry_count: The number of entries that this queue will handle.
James Smartc1a21eb2019-03-12 16:30:29 -070015087 * @cpu: The cpu that will primarily utilize this queue.
James Smart4f774512009-05-22 14:52:35 -040015088 *
15089 * This function allocates a queue structure and the DMAable memory used for
15090 * the host resident queue. This function must be called before creating the
15091 * queue on the HBA.
15092 **/
15093struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080015094lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
James Smartc1a21eb2019-03-12 16:30:29 -070015095 uint32_t entry_size, uint32_t entry_count, int cpu)
James Smart4f774512009-05-22 14:52:35 -040015096{
15097 struct lpfc_queue *queue;
15098 struct lpfc_dmabuf *dmabuf;
James Smartcb5172e2010-03-15 11:25:07 -040015099 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart9afbee32019-03-12 16:30:28 -070015100 uint16_t x, pgcnt;
James Smart4f774512009-05-22 14:52:35 -040015101
James Smartcb5172e2010-03-15 11:25:07 -040015102 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015103 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040015104
James Smart9afbee32019-03-12 16:30:28 -070015105 pgcnt = ALIGN(entry_size * entry_count, hw_page_size) / hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080015106
15107 /* If needed, Adjust page count to match the max the adapter supports */
James Smart9afbee32019-03-12 16:30:28 -070015108 if (pgcnt > phba->sli4_hba.pc_sli4_params.wqpcnt)
15109 pgcnt = phba->sli4_hba.pc_sli4_params.wqpcnt;
15110
James Smartc1a21eb2019-03-12 16:30:29 -070015111 queue = kzalloc_node(sizeof(*queue) + (sizeof(void *) * pgcnt),
15112 GFP_KERNEL, cpu_to_node(cpu));
James Smart9afbee32019-03-12 16:30:28 -070015113 if (!queue)
15114 return NULL;
James Smart895427b2017-02-12 13:52:30 -080015115
James Smart4f774512009-05-22 14:52:35 -040015116 INIT_LIST_HEAD(&queue->list);
James Smart93a4d6f2019-11-04 16:57:05 -080015117 INIT_LIST_HEAD(&queue->_poll_list);
James Smart895427b2017-02-12 13:52:30 -080015118 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080015119 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040015120 INIT_LIST_HEAD(&queue->page_list);
15121 INIT_LIST_HEAD(&queue->child_list);
James Smart32517fc2019-01-28 11:14:33 -080015122 INIT_LIST_HEAD(&queue->cpu_list);
James Smart81b96ed2017-11-20 16:00:29 -080015123
15124 /* Set queue parameters now. If the system cannot provide memory
15125 * resources, the free routine needs to know what was allocated.
15126 */
James Smart9afbee32019-03-12 16:30:28 -070015127 queue->page_count = pgcnt;
15128 queue->q_pgs = (void **)&queue[1];
15129 queue->entry_cnt_per_pg = hw_page_size / entry_size;
James Smart81b96ed2017-11-20 16:00:29 -080015130 queue->entry_size = entry_size;
15131 queue->entry_count = entry_count;
15132 queue->page_size = hw_page_size;
15133 queue->phba = phba;
15134
James Smart9afbee32019-03-12 16:30:28 -070015135 for (x = 0; x < queue->page_count; x++) {
James Smartc1a21eb2019-03-12 16:30:29 -070015136 dmabuf = kzalloc_node(sizeof(*dmabuf), GFP_KERNEL,
15137 dev_to_node(&phba->pcidev->dev));
James Smart4f774512009-05-22 14:52:35 -040015138 if (!dmabuf)
15139 goto out_fail;
Luis Chamberlain750afb02019-01-04 09:23:09 +010015140 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
15141 hw_page_size, &dmabuf->phys,
15142 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040015143 if (!dmabuf->virt) {
15144 kfree(dmabuf);
15145 goto out_fail;
15146 }
15147 dmabuf->buffer_tag = x;
15148 list_add_tail(&dmabuf->list, &queue->page_list);
James Smart9afbee32019-03-12 16:30:28 -070015149 /* use lpfc_sli4_qe to index a paritcular entry in this page */
15150 queue->q_pgs[x] = dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -040015151 }
Dick Kennedyf485c182017-09-29 17:34:34 -070015152 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
15153 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart32517fc2019-01-28 11:14:33 -080015154 INIT_DELAYED_WORK(&queue->sched_irqwork, lpfc_sli4_dly_hba_process_cq);
15155 INIT_DELAYED_WORK(&queue->sched_spwork, lpfc_sli4_dly_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040015156
James Smart32517fc2019-01-28 11:14:33 -080015157 /* notify_interval will be set during q creation */
James Smart64eb4dc2017-05-15 15:20:49 -070015158
James Smart4f774512009-05-22 14:52:35 -040015159 return queue;
15160out_fail:
15161 lpfc_sli4_queue_free(queue);
15162 return NULL;
15163}
15164
15165/**
James Smart962bc512013-01-03 15:44:00 -050015166 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
15167 * @phba: HBA structure that indicates port to create a queue on.
15168 * @pci_barset: PCI BAR set flag.
15169 *
15170 * This function shall perform iomap of the specified PCI BAR address to host
15171 * memory address if not already done so and return it. The returned host
15172 * memory address can be NULL.
15173 */
15174static void __iomem *
15175lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
15176{
James Smart962bc512013-01-03 15:44:00 -050015177 if (!phba->pcidev)
15178 return NULL;
James Smart962bc512013-01-03 15:44:00 -050015179
15180 switch (pci_barset) {
15181 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050015182 return phba->pci_bar0_memmap_p;
15183 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050015184 return phba->pci_bar2_memmap_p;
15185 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050015186 return phba->pci_bar4_memmap_p;
15187 default:
15188 break;
15189 }
15190 return NULL;
15191}
15192
15193/**
James Smartcb733e32019-01-28 11:14:32 -080015194 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on EQs
15195 * @phba: HBA structure that EQs are on.
15196 * @startq: The starting EQ index to modify
15197 * @numq: The number of EQs (consecutive indexes) to modify
15198 * @usdelay: amount of delay
James Smart173edbb2012-06-12 13:54:50 -040015199 *
James Smartcb733e32019-01-28 11:14:32 -080015200 * This function revises the EQ delay on 1 or more EQs. The EQ delay
15201 * is set either by writing to a register (if supported by the SLI Port)
15202 * or by mailbox command. The mailbox command allows several EQs to be
15203 * updated at once.
James Smart173edbb2012-06-12 13:54:50 -040015204 *
James Smartcb733e32019-01-28 11:14:32 -080015205 * The @phba struct is used to send a mailbox command to HBA. The @startq
15206 * is used to get the starting EQ index to change. The @numq value is
15207 * used to specify how many consecutive EQ indexes, starting at EQ index,
15208 * are to be changed. This function is asynchronous and will wait for any
15209 * mailbox commands to finish before returning.
James Smart173edbb2012-06-12 13:54:50 -040015210 *
James Smartcb733e32019-01-28 11:14:32 -080015211 * On success this function will return a zero. If unable to allocate
15212 * enough memory this function will return -ENOMEM. If a mailbox command
15213 * fails this function will return -ENXIO. Note: on ENXIO, some EQs may
15214 * have had their delay multipler changed.
James Smart173edbb2012-06-12 13:54:50 -040015215 **/
James Smartcb733e32019-01-28 11:14:32 -080015216void
James Smart0cf07f842017-06-01 21:07:10 -070015217lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
James Smartcb733e32019-01-28 11:14:32 -080015218 uint32_t numq, uint32_t usdelay)
James Smart173edbb2012-06-12 13:54:50 -040015219{
15220 struct lpfc_mbx_modify_eq_delay *eq_delay;
15221 LPFC_MBOXQ_t *mbox;
15222 struct lpfc_queue *eq;
James Smartcb733e32019-01-28 11:14:32 -080015223 int cnt = 0, rc, length;
James Smart173edbb2012-06-12 13:54:50 -040015224 uint32_t shdr_status, shdr_add_status;
James Smartcb733e32019-01-28 11:14:32 -080015225 uint32_t dmult;
James Smart895427b2017-02-12 13:52:30 -080015226 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040015227 union lpfc_sli4_cfg_shdr *shdr;
James Smart173edbb2012-06-12 13:54:50 -040015228
James Smart6a828b02019-01-28 11:14:31 -080015229 if (startq >= phba->cfg_irq_chann)
James Smartcb733e32019-01-28 11:14:32 -080015230 return;
15231
15232 if (usdelay > 0xFFFF) {
15233 lpfc_printf_log(phba, KERN_INFO, LOG_INIT | LOG_FCP | LOG_NVME,
15234 "6429 usdelay %d too large. Scaled down to "
15235 "0xFFFF.\n", usdelay);
15236 usdelay = 0xFFFF;
15237 }
15238
15239 /* set values by EQ_DELAY register if supported */
15240 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
15241 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070015242 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smartcb733e32019-01-28 11:14:32 -080015243 if (!eq)
15244 continue;
15245
James Smart32517fc2019-01-28 11:14:33 -080015246 lpfc_sli4_mod_hba_eq_delay(phba, eq, usdelay);
James Smartcb733e32019-01-28 11:14:32 -080015247
15248 if (++cnt >= numq)
15249 break;
15250 }
James Smartcb733e32019-01-28 11:14:32 -080015251 return;
15252 }
15253
15254 /* Otherwise, set values by mailbox cmd */
James Smart173edbb2012-06-12 13:54:50 -040015255
15256 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smartcb733e32019-01-28 11:14:32 -080015257 if (!mbox) {
Dick Kennedy372c1872020-06-30 14:50:00 -070015258 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartcb733e32019-01-28 11:14:32 -080015259 "6428 Failed allocating mailbox cmd buffer."
15260 " EQ delay was not set.\n");
15261 return;
15262 }
James Smart173edbb2012-06-12 13:54:50 -040015263 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
15264 sizeof(struct lpfc_sli4_cfg_mhdr));
15265 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15266 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
15267 length, LPFC_SLI4_MBX_EMBED);
15268 eq_delay = &mbox->u.mqe.un.eq_delay;
15269
15270 /* Calculate delay multiper from maximum interrupt per second */
James Smartcb733e32019-01-28 11:14:32 -080015271 dmult = (usdelay * LPFC_DMULT_CONST) / LPFC_SEC_TO_USEC;
15272 if (dmult)
15273 dmult--;
James Smart0cf07f842017-06-01 21:07:10 -070015274 if (dmult > LPFC_DMULT_MAX)
15275 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040015276
James Smart6a828b02019-01-28 11:14:31 -080015277 for (qidx = startq; qidx < phba->cfg_irq_chann; qidx++) {
James Smart657add42019-05-21 17:49:06 -070015278 eq = phba->sli4_hba.hba_eq_hdl[qidx].eq;
James Smart173edbb2012-06-12 13:54:50 -040015279 if (!eq)
15280 continue;
James Smartcb733e32019-01-28 11:14:32 -080015281 eq->q_mode = usdelay;
James Smart173edbb2012-06-12 13:54:50 -040015282 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
15283 eq_delay->u.request.eq[cnt].phase = 0;
15284 eq_delay->u.request.eq[cnt].delay_multi = dmult;
James Smart0cf07f842017-06-01 21:07:10 -070015285
James Smartcb733e32019-01-28 11:14:32 -080015286 if (++cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040015287 break;
15288 }
15289 eq_delay->u.request.num_eq = cnt;
15290
15291 mbox->vport = phba->pport;
15292 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080015293 mbox->ctx_buf = NULL;
15294 mbox->ctx_ndlp = NULL;
James Smart173edbb2012-06-12 13:54:50 -040015295 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15296 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
15297 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15298 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15299 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070015300 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart173edbb2012-06-12 13:54:50 -040015301 "2512 MODIFY_EQ_DELAY mailbox failed with "
15302 "status x%x add_status x%x, mbx status x%x\n",
15303 shdr_status, shdr_add_status, rc);
James Smart173edbb2012-06-12 13:54:50 -040015304 }
15305 mempool_free(mbox, phba->mbox_mem_pool);
James Smartcb733e32019-01-28 11:14:32 -080015306 return;
James Smart173edbb2012-06-12 13:54:50 -040015307}
15308
15309/**
James Smart4f774512009-05-22 14:52:35 -040015310 * lpfc_eq_create - Create an Event Queue on the HBA
15311 * @phba: HBA structure that indicates port to create a queue on.
15312 * @eq: The queue structure to use to create the event queue.
15313 * @imax: The maximum interrupt per second limit.
15314 *
15315 * This function creates an event queue, as detailed in @eq, on a port,
15316 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
15317 *
15318 * The @phba struct is used to send mailbox command to HBA. The @eq struct
15319 * is used to get the entry count and entry size that are necessary to
15320 * determine the number of pages to allocate and use for this queue. This
15321 * function will send the EQ_CREATE mailbox command to the HBA to setup the
15322 * event queue. This function is asynchronous and will wait for the mailbox
15323 * command to finish before continuing.
15324 *
15325 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015326 * memory this function will return -ENOMEM. If the queue create mailbox command
15327 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015328 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015329int
James Smartee020062012-09-29 11:28:52 -040015330lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040015331{
15332 struct lpfc_mbx_eq_create *eq_create;
15333 LPFC_MBOXQ_t *mbox;
15334 int rc, length, status = 0;
15335 struct lpfc_dmabuf *dmabuf;
15336 uint32_t shdr_status, shdr_add_status;
15337 union lpfc_sli4_cfg_shdr *shdr;
15338 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040015339 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15340
James Smart2e90f4b2011-12-13 13:22:37 -050015341 /* sanity check on queue memory */
15342 if (!eq)
15343 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015344 if (!phba->sli4_hba.pc_sli4_params.supported)
15345 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015346
15347 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15348 if (!mbox)
15349 return -ENOMEM;
15350 length = (sizeof(struct lpfc_mbx_eq_create) -
15351 sizeof(struct lpfc_sli4_cfg_mhdr));
15352 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15353 LPFC_MBOX_OPCODE_EQ_CREATE,
15354 length, LPFC_SLI4_MBX_EMBED);
15355 eq_create = &mbox->u.mqe.un.eq_create;
James Smart7365f6f2018-02-22 08:18:46 -080015356 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015357 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
15358 eq->page_count);
15359 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
15360 LPFC_EQE_SIZE);
15361 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart7365f6f2018-02-22 08:18:46 -080015362
15363 /* Use version 2 of CREATE_EQ if eqav is set */
15364 if (phba->sli4_hba.pc_sli4_params.eqav) {
15365 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15366 LPFC_Q_CREATE_VERSION_2);
15367 bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
15368 phba->sli4_hba.pc_sli4_params.eqav);
15369 }
15370
James Smart2c9c5a02015-04-07 15:07:15 -040015371 /* don't setup delay multiplier using EQ_CREATE */
15372 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040015373 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
15374 dmult);
15375 switch (eq->entry_count) {
15376 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070015377 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040015378 "0360 Unsupported EQ count. (%d)\n",
15379 eq->entry_count);
James Smart04d210c2019-05-21 17:49:03 -070015380 if (eq->entry_count < 256) {
15381 status = -EINVAL;
15382 goto out;
15383 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050015384 fallthrough; /* otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040015385 case 256:
15386 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15387 LPFC_EQ_CNT_256);
15388 break;
15389 case 512:
15390 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15391 LPFC_EQ_CNT_512);
15392 break;
15393 case 1024:
15394 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15395 LPFC_EQ_CNT_1024);
15396 break;
15397 case 2048:
15398 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15399 LPFC_EQ_CNT_2048);
15400 break;
15401 case 4096:
15402 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
15403 LPFC_EQ_CNT_4096);
15404 break;
15405 }
15406 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015407 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015408 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15409 putPaddrLow(dmabuf->phys);
15410 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15411 putPaddrHigh(dmabuf->phys);
15412 }
15413 mbox->vport = phba->pport;
15414 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080015415 mbox->ctx_buf = NULL;
15416 mbox->ctx_ndlp = NULL;
James Smart4f774512009-05-22 14:52:35 -040015417 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart4f774512009-05-22 14:52:35 -040015418 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15419 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15420 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070015421 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040015422 "2500 EQ_CREATE mailbox failed with "
15423 "status x%x add_status x%x, mbx status x%x\n",
15424 shdr_status, shdr_add_status, rc);
15425 status = -ENXIO;
15426 }
15427 eq->type = LPFC_EQ;
15428 eq->subtype = LPFC_NONE;
15429 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
15430 if (eq->queue_id == 0xFFFF)
15431 status = -ENXIO;
15432 eq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015433 eq->notify_interval = LPFC_EQ_NOTIFY_INTRVL;
15434 eq->max_proc_limit = LPFC_EQ_MAX_PROC_LIMIT;
James Smart04d210c2019-05-21 17:49:03 -070015435out:
James Smart8fa38512009-07-19 10:01:03 -040015436 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015437 return status;
15438}
15439
Dick Kennedy317aeb82020-06-30 14:49:59 -070015440static int lpfc_cq_poll_hdler(struct irq_poll *iop, int budget)
15441{
15442 struct lpfc_queue *cq = container_of(iop, struct lpfc_queue, iop);
15443
Colin Ian King26e0b9a2020-07-07 16:00:18 +010015444 __lpfc_sli4_hba_process_cq(cq, LPFC_IRQ_POLL);
Dick Kennedy317aeb82020-06-30 14:49:59 -070015445
15446 return 1;
15447}
15448
James Smart4f774512009-05-22 14:52:35 -040015449/**
15450 * lpfc_cq_create - Create a Completion Queue on the HBA
15451 * @phba: HBA structure that indicates port to create a queue on.
15452 * @cq: The queue structure to use to create the completion queue.
15453 * @eq: The event queue to bind this completion queue to.
Lee Jones7af29d42020-07-21 17:41:31 +010015454 * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
15455 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
James Smart4f774512009-05-22 14:52:35 -040015456 *
15457 * This function creates a completion queue, as detailed in @wq, on a port,
15458 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
15459 *
15460 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15461 * is used to get the entry count and entry size that are necessary to
15462 * determine the number of pages to allocate and use for this queue. The @eq
15463 * is used to indicate which event queue to bind this completion queue to. This
15464 * function will send the CQ_CREATE mailbox command to the HBA to setup the
15465 * completion queue. This function is asynchronous and will wait for the mailbox
15466 * command to finish before continuing.
15467 *
15468 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015469 * memory this function will return -ENOMEM. If the queue create mailbox command
15470 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015471 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015472int
James Smart4f774512009-05-22 14:52:35 -040015473lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
15474 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
15475{
15476 struct lpfc_mbx_cq_create *cq_create;
15477 struct lpfc_dmabuf *dmabuf;
15478 LPFC_MBOXQ_t *mbox;
15479 int rc, length, status = 0;
15480 uint32_t shdr_status, shdr_add_status;
15481 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015482
James Smart2e90f4b2011-12-13 13:22:37 -050015483 /* sanity check on queue memory */
15484 if (!cq || !eq)
15485 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015486
James Smart4f774512009-05-22 14:52:35 -040015487 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15488 if (!mbox)
15489 return -ENOMEM;
15490 length = (sizeof(struct lpfc_mbx_cq_create) -
15491 sizeof(struct lpfc_sli4_cfg_mhdr));
15492 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15493 LPFC_MBOX_OPCODE_CQ_CREATE,
15494 length, LPFC_SLI4_MBX_EMBED);
15495 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015496 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015497 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
15498 cq->page_count);
15499 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
15500 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015501 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15502 phba->sli4_hba.pc_sli4_params.cqv);
15503 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080015504 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
15505 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015506 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
15507 eq->queue_id);
James Smart7365f6f2018-02-22 08:18:46 -080015508 bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
15509 phba->sli4_hba.pc_sli4_params.cqav);
James Smart5a6f1332011-03-11 16:05:35 -050015510 } else {
15511 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
15512 eq->queue_id);
15513 }
James Smart4f774512009-05-22 14:52:35 -040015514 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080015515 case 2048:
15516 case 4096:
15517 if (phba->sli4_hba.pc_sli4_params.cqv ==
15518 LPFC_Q_CREATE_VERSION_2) {
15519 cq_create->u.request.context.lpfc_cq_context_count =
15520 cq->entry_count;
15521 bf_set(lpfc_cq_context_count,
15522 &cq_create->u.request.context,
15523 LPFC_CQ_CNT_WORD7);
15524 break;
15525 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050015526 fallthrough;
James Smart4f774512009-05-22 14:52:35 -040015527 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070015528 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2ea259e2017-02-12 13:52:27 -080015529 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070015530 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080015531 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070015532 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040015533 if (cq->entry_count < 256) {
15534 status = -EINVAL;
15535 goto out;
15536 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050015537 fallthrough; /* otherwise default to smallest count */
James Smart4f774512009-05-22 14:52:35 -040015538 case 256:
15539 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15540 LPFC_CQ_CNT_256);
15541 break;
15542 case 512:
15543 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15544 LPFC_CQ_CNT_512);
15545 break;
15546 case 1024:
15547 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
15548 LPFC_CQ_CNT_1024);
15549 break;
15550 }
15551 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080015552 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040015553 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15554 putPaddrLow(dmabuf->phys);
15555 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15556 putPaddrHigh(dmabuf->phys);
15557 }
15558 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15559
15560 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015561 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15562 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15563 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070015564 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040015565 "2501 CQ_CREATE mailbox failed with "
15566 "status x%x add_status x%x, mbx status x%x\n",
15567 shdr_status, shdr_add_status, rc);
15568 status = -ENXIO;
15569 goto out;
15570 }
15571 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
15572 if (cq->queue_id == 0xFFFF) {
15573 status = -ENXIO;
15574 goto out;
15575 }
15576 /* link the cq onto the parent eq child list */
15577 list_add_tail(&cq->list, &eq->child_list);
15578 /* Set up completion queue's type and subtype */
15579 cq->type = type;
15580 cq->subtype = subtype;
15581 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050015582 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080015583 cq->assoc_qp = eq;
James Smart4f774512009-05-22 14:52:35 -040015584 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015585 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15586 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit, cq->entry_count);
James Smart4f774512009-05-22 14:52:35 -040015587
James Smart6a828b02019-01-28 11:14:31 -080015588 if (cq->queue_id > phba->sli4_hba.cq_max)
15589 phba->sli4_hba.cq_max = cq->queue_id;
Dick Kennedy317aeb82020-06-30 14:49:59 -070015590
15591 irq_poll_init(&cq->iop, LPFC_IRQ_POLL_WEIGHT, lpfc_cq_poll_hdler);
James Smart8fa38512009-07-19 10:01:03 -040015592out:
15593 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015594 return status;
15595}
15596
15597/**
James Smart2d7dbc42017-02-12 13:52:35 -080015598 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
15599 * @phba: HBA structure that indicates port to create a queue on.
15600 * @cqp: The queue structure array to use to create the completion queues.
James Smartcdb42be2019-01-28 11:14:21 -080015601 * @hdwq: The hardware queue array with the EQ to bind completion queues to.
Lee Jones7af29d42020-07-21 17:41:31 +010015602 * @type: Type of queue (EQ, GCQ, MCQ, WCQ, etc).
15603 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
James Smart2d7dbc42017-02-12 13:52:35 -080015604 *
15605 * This function creates a set of completion queue, s to support MRQ
15606 * as detailed in @cqp, on a port,
15607 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
15608 *
15609 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15610 * is used to get the entry count and entry size that are necessary to
15611 * determine the number of pages to allocate and use for this queue. The @eq
15612 * is used to indicate which event queue to bind this completion queue to. This
15613 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
15614 * completion queue. This function is asynchronous and will wait for the mailbox
15615 * command to finish before continuing.
15616 *
15617 * On success this function will return a zero. If unable to allocate enough
15618 * memory this function will return -ENOMEM. If the queue create mailbox command
15619 * fails this function will return -ENXIO.
15620 **/
15621int
15622lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
James Smartcdb42be2019-01-28 11:14:21 -080015623 struct lpfc_sli4_hdw_queue *hdwq, uint32_t type,
15624 uint32_t subtype)
James Smart2d7dbc42017-02-12 13:52:35 -080015625{
15626 struct lpfc_queue *cq;
15627 struct lpfc_queue *eq;
15628 struct lpfc_mbx_cq_create_set *cq_set;
15629 struct lpfc_dmabuf *dmabuf;
15630 LPFC_MBOXQ_t *mbox;
15631 int rc, length, alloclen, status = 0;
15632 int cnt, idx, numcq, page_idx = 0;
15633 uint32_t shdr_status, shdr_add_status;
15634 union lpfc_sli4_cfg_shdr *shdr;
15635 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15636
15637 /* sanity check on queue memory */
15638 numcq = phba->cfg_nvmet_mrq;
James Smartcdb42be2019-01-28 11:14:21 -080015639 if (!cqp || !hdwq || !numcq)
James Smart2d7dbc42017-02-12 13:52:35 -080015640 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080015641
15642 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15643 if (!mbox)
15644 return -ENOMEM;
15645
15646 length = sizeof(struct lpfc_mbx_cq_create_set);
15647 length += ((numcq * cqp[0]->page_count) *
15648 sizeof(struct dma_address));
15649 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15650 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
15651 LPFC_SLI4_MBX_NEMBED);
15652 if (alloclen < length) {
Dick Kennedy372c1872020-06-30 14:50:00 -070015653 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080015654 "3098 Allocated DMA memory size (%d) is "
15655 "less than the requested DMA memory size "
15656 "(%d)\n", alloclen, length);
15657 status = -ENOMEM;
15658 goto out;
15659 }
15660 cq_set = mbox->sge_array->addr[0];
15661 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
15662 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
15663
15664 for (idx = 0; idx < numcq; idx++) {
15665 cq = cqp[idx];
James Smartcdb42be2019-01-28 11:14:21 -080015666 eq = hdwq[idx].hba_eq;
James Smart2d7dbc42017-02-12 13:52:35 -080015667 if (!cq || !eq) {
15668 status = -ENOMEM;
15669 goto out;
15670 }
James Smart81b96ed2017-11-20 16:00:29 -080015671 if (!phba->sli4_hba.pc_sli4_params.supported)
15672 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080015673
15674 switch (idx) {
15675 case 0:
15676 bf_set(lpfc_mbx_cq_create_set_page_size,
15677 &cq_set->u.request,
15678 (hw_page_size / SLI4_PAGE_SIZE));
15679 bf_set(lpfc_mbx_cq_create_set_num_pages,
15680 &cq_set->u.request, cq->page_count);
15681 bf_set(lpfc_mbx_cq_create_set_evt,
15682 &cq_set->u.request, 1);
15683 bf_set(lpfc_mbx_cq_create_set_valid,
15684 &cq_set->u.request, 1);
15685 bf_set(lpfc_mbx_cq_create_set_cqe_size,
15686 &cq_set->u.request, 0);
15687 bf_set(lpfc_mbx_cq_create_set_num_cq,
15688 &cq_set->u.request, numcq);
James Smart7365f6f2018-02-22 08:18:46 -080015689 bf_set(lpfc_mbx_cq_create_set_autovalid,
15690 &cq_set->u.request,
15691 phba->sli4_hba.pc_sli4_params.cqav);
James Smart2d7dbc42017-02-12 13:52:35 -080015692 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080015693 case 2048:
15694 case 4096:
15695 if (phba->sli4_hba.pc_sli4_params.cqv ==
15696 LPFC_Q_CREATE_VERSION_2) {
15697 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15698 &cq_set->u.request,
15699 cq->entry_count);
15700 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15701 &cq_set->u.request,
15702 LPFC_CQ_CNT_WORD7);
15703 break;
15704 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050015705 fallthrough;
James Smart2d7dbc42017-02-12 13:52:35 -080015706 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070015707 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080015708 "3118 Bad CQ count. (%d)\n",
15709 cq->entry_count);
15710 if (cq->entry_count < 256) {
15711 status = -EINVAL;
15712 goto out;
15713 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050015714 fallthrough; /* otherwise default to smallest */
James Smart2d7dbc42017-02-12 13:52:35 -080015715 case 256:
15716 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15717 &cq_set->u.request, LPFC_CQ_CNT_256);
15718 break;
15719 case 512:
15720 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15721 &cq_set->u.request, LPFC_CQ_CNT_512);
15722 break;
15723 case 1024:
15724 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
15725 &cq_set->u.request, LPFC_CQ_CNT_1024);
15726 break;
15727 }
15728 bf_set(lpfc_mbx_cq_create_set_eq_id0,
15729 &cq_set->u.request, eq->queue_id);
15730 break;
15731 case 1:
15732 bf_set(lpfc_mbx_cq_create_set_eq_id1,
15733 &cq_set->u.request, eq->queue_id);
15734 break;
15735 case 2:
15736 bf_set(lpfc_mbx_cq_create_set_eq_id2,
15737 &cq_set->u.request, eq->queue_id);
15738 break;
15739 case 3:
15740 bf_set(lpfc_mbx_cq_create_set_eq_id3,
15741 &cq_set->u.request, eq->queue_id);
15742 break;
15743 case 4:
15744 bf_set(lpfc_mbx_cq_create_set_eq_id4,
15745 &cq_set->u.request, eq->queue_id);
15746 break;
15747 case 5:
15748 bf_set(lpfc_mbx_cq_create_set_eq_id5,
15749 &cq_set->u.request, eq->queue_id);
15750 break;
15751 case 6:
15752 bf_set(lpfc_mbx_cq_create_set_eq_id6,
15753 &cq_set->u.request, eq->queue_id);
15754 break;
15755 case 7:
15756 bf_set(lpfc_mbx_cq_create_set_eq_id7,
15757 &cq_set->u.request, eq->queue_id);
15758 break;
15759 case 8:
15760 bf_set(lpfc_mbx_cq_create_set_eq_id8,
15761 &cq_set->u.request, eq->queue_id);
15762 break;
15763 case 9:
15764 bf_set(lpfc_mbx_cq_create_set_eq_id9,
15765 &cq_set->u.request, eq->queue_id);
15766 break;
15767 case 10:
15768 bf_set(lpfc_mbx_cq_create_set_eq_id10,
15769 &cq_set->u.request, eq->queue_id);
15770 break;
15771 case 11:
15772 bf_set(lpfc_mbx_cq_create_set_eq_id11,
15773 &cq_set->u.request, eq->queue_id);
15774 break;
15775 case 12:
15776 bf_set(lpfc_mbx_cq_create_set_eq_id12,
15777 &cq_set->u.request, eq->queue_id);
15778 break;
15779 case 13:
15780 bf_set(lpfc_mbx_cq_create_set_eq_id13,
15781 &cq_set->u.request, eq->queue_id);
15782 break;
15783 case 14:
15784 bf_set(lpfc_mbx_cq_create_set_eq_id14,
15785 &cq_set->u.request, eq->queue_id);
15786 break;
15787 case 15:
15788 bf_set(lpfc_mbx_cq_create_set_eq_id15,
15789 &cq_set->u.request, eq->queue_id);
15790 break;
15791 }
15792
15793 /* link the cq onto the parent eq child list */
15794 list_add_tail(&cq->list, &eq->child_list);
15795 /* Set up completion queue's type and subtype */
15796 cq->type = type;
15797 cq->subtype = subtype;
15798 cq->assoc_qid = eq->queue_id;
James Smart6a828b02019-01-28 11:14:31 -080015799 cq->assoc_qp = eq;
James Smart2d7dbc42017-02-12 13:52:35 -080015800 cq->host_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080015801 cq->notify_interval = LPFC_CQ_NOTIFY_INTRVL;
15802 cq->max_proc_limit = min(phba->cfg_cq_max_proc_limit,
15803 cq->entry_count);
James Smart81b96ed2017-11-20 16:00:29 -080015804 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080015805
15806 rc = 0;
15807 list_for_each_entry(dmabuf, &cq->page_list, list) {
15808 memset(dmabuf->virt, 0, hw_page_size);
15809 cnt = page_idx + dmabuf->buffer_tag;
15810 cq_set->u.request.page[cnt].addr_lo =
15811 putPaddrLow(dmabuf->phys);
15812 cq_set->u.request.page[cnt].addr_hi =
15813 putPaddrHigh(dmabuf->phys);
15814 rc++;
15815 }
15816 page_idx += rc;
15817 }
15818
15819 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15820
15821 /* The IOCTL status is embedded in the mailbox subheader. */
15822 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15823 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15824 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070015825 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080015826 "3119 CQ_CREATE_SET mailbox failed with "
15827 "status x%x add_status x%x, mbx status x%x\n",
15828 shdr_status, shdr_add_status, rc);
15829 status = -ENXIO;
15830 goto out;
15831 }
15832 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
15833 if (rc == 0xFFFF) {
15834 status = -ENXIO;
15835 goto out;
15836 }
15837
15838 for (idx = 0; idx < numcq; idx++) {
15839 cq = cqp[idx];
15840 cq->queue_id = rc + idx;
James Smart6a828b02019-01-28 11:14:31 -080015841 if (cq->queue_id > phba->sli4_hba.cq_max)
15842 phba->sli4_hba.cq_max = cq->queue_id;
James Smart2d7dbc42017-02-12 13:52:35 -080015843 }
15844
15845out:
15846 lpfc_sli4_mbox_cmd_free(phba, mbox);
15847 return status;
15848}
15849
15850/**
James Smartb19a0612010-04-06 14:48:51 -040015851 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040015852 * @phba: HBA structure that indicates port to create a queue on.
15853 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040015854 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
15855 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040015856 *
James Smartb19a0612010-04-06 14:48:51 -040015857 * This function provides failback (fb) functionality when the
15858 * mq_create_ext fails on older FW generations. It's purpose is identical
15859 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040015860 *
James Smartb19a0612010-04-06 14:48:51 -040015861 * This routine cannot fail as all attributes were previously accessed and
15862 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040015863 **/
James Smartb19a0612010-04-06 14:48:51 -040015864static void
15865lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
15866 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040015867{
15868 struct lpfc_mbx_mq_create *mq_create;
15869 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040015870 int length;
James Smart04c68492009-05-22 14:52:52 -040015871
James Smart04c68492009-05-22 14:52:52 -040015872 length = (sizeof(struct lpfc_mbx_mq_create) -
15873 sizeof(struct lpfc_sli4_cfg_mhdr));
15874 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15875 LPFC_MBOX_OPCODE_MQ_CREATE,
15876 length, LPFC_SLI4_MBX_EMBED);
15877 mq_create = &mbox->u.mqe.un.mq_create;
15878 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040015879 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040015880 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040015881 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040015882 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
15883 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040015884 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015885 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15886 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040015887 break;
15888 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015889 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15890 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040015891 break;
15892 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050015893 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15894 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040015895 break;
15896 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050015897 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
15898 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040015899 break;
15900 }
15901 list_for_each_entry(dmabuf, &mq->page_list, list) {
15902 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040015903 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040015904 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040015905 putPaddrHigh(dmabuf->phys);
15906 }
15907}
15908
15909/**
15910 * lpfc_mq_create - Create a mailbox Queue on the HBA
15911 * @phba: HBA structure that indicates port to create a queue on.
15912 * @mq: The queue structure to use to create the mailbox queue.
15913 * @cq: The completion queue to associate with this cq.
15914 * @subtype: The queue's subtype.
15915 *
15916 * This function creates a mailbox queue, as detailed in @mq, on a port,
15917 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
15918 *
15919 * The @phba struct is used to send mailbox command to HBA. The @cq struct
15920 * is used to get the entry count and entry size that are necessary to
15921 * determine the number of pages to allocate and use for this queue. This
15922 * function will send the MQ_CREATE mailbox command to the HBA to setup the
15923 * mailbox queue. This function is asynchronous and will wait for the mailbox
15924 * command to finish before continuing.
15925 *
15926 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015927 * memory this function will return -ENOMEM. If the queue create mailbox command
15928 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040015929 **/
15930int32_t
15931lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
15932 struct lpfc_queue *cq, uint32_t subtype)
15933{
15934 struct lpfc_mbx_mq_create *mq_create;
15935 struct lpfc_mbx_mq_create_ext *mq_create_ext;
15936 struct lpfc_dmabuf *dmabuf;
15937 LPFC_MBOXQ_t *mbox;
15938 int rc, length, status = 0;
15939 uint32_t shdr_status, shdr_add_status;
15940 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015941 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040015942
James Smart2e90f4b2011-12-13 13:22:37 -050015943 /* sanity check on queue memory */
15944 if (!mq || !cq)
15945 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015946 if (!phba->sli4_hba.pc_sli4_params.supported)
15947 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040015948
15949 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15950 if (!mbox)
15951 return -ENOMEM;
15952 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
15953 sizeof(struct lpfc_sli4_cfg_mhdr));
15954 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15955 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
15956 length, LPFC_SLI4_MBX_EMBED);
15957
15958 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050015959 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050015960 bf_set(lpfc_mbx_mq_create_ext_num_pages,
15961 &mq_create_ext->u.request, mq->page_count);
15962 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
15963 &mq_create_ext->u.request, 1);
15964 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040015965 &mq_create_ext->u.request, 1);
15966 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
15967 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050015968 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
15969 &mq_create_ext->u.request, 1);
15970 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
15971 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040015972 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050015973 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15974 phba->sli4_hba.pc_sli4_params.mqv);
15975 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
15976 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
15977 cq->queue_id);
15978 else
15979 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
15980 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040015981 switch (mq->entry_count) {
15982 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070015983 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartb19a0612010-04-06 14:48:51 -040015984 "0362 Unsupported MQ count. (%d)\n",
15985 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015986 if (mq->entry_count < 16) {
15987 status = -EINVAL;
15988 goto out;
15989 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050015990 fallthrough; /* otherwise default to smallest count */
James Smartb19a0612010-04-06 14:48:51 -040015991 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050015992 bf_set(lpfc_mq_context_ring_size,
15993 &mq_create_ext->u.request.context,
15994 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040015995 break;
15996 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050015997 bf_set(lpfc_mq_context_ring_size,
15998 &mq_create_ext->u.request.context,
15999 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040016000 break;
16001 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050016002 bf_set(lpfc_mq_context_ring_size,
16003 &mq_create_ext->u.request.context,
16004 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040016005 break;
16006 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050016007 bf_set(lpfc_mq_context_ring_size,
16008 &mq_create_ext->u.request.context,
16009 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040016010 break;
16011 }
16012 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040016013 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040016014 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
16015 putPaddrLow(dmabuf->phys);
16016 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040016017 putPaddrHigh(dmabuf->phys);
16018 }
16019 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040016020 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16021 &mq_create_ext->u.response);
16022 if (rc != MBX_SUCCESS) {
16023 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16024 "2795 MQ_CREATE_EXT failed with "
16025 "status x%x. Failback to MQ_CREATE.\n",
16026 rc);
16027 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
16028 mq_create = &mbox->u.mqe.un.mq_create;
16029 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16030 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
16031 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
16032 &mq_create->u.response);
16033 }
16034
James Smart04c68492009-05-22 14:52:52 -040016035 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040016036 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16037 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16038 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016039 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart04c68492009-05-22 14:52:52 -040016040 "2502 MQ_CREATE mailbox failed with "
16041 "status x%x add_status x%x, mbx status x%x\n",
16042 shdr_status, shdr_add_status, rc);
16043 status = -ENXIO;
16044 goto out;
16045 }
James Smart04c68492009-05-22 14:52:52 -040016046 if (mq->queue_id == 0xFFFF) {
16047 status = -ENXIO;
16048 goto out;
16049 }
16050 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050016051 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040016052 mq->subtype = subtype;
16053 mq->host_index = 0;
16054 mq->hba_index = 0;
16055
16056 /* link the mq onto the parent cq child list */
16057 list_add_tail(&mq->list, &cq->child_list);
16058out:
James Smart8fa38512009-07-19 10:01:03 -040016059 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016060 return status;
16061}
16062
16063/**
James Smart4f774512009-05-22 14:52:35 -040016064 * lpfc_wq_create - Create a Work Queue on the HBA
16065 * @phba: HBA structure that indicates port to create a queue on.
16066 * @wq: The queue structure to use to create the work queue.
16067 * @cq: The completion queue to bind this work queue to.
16068 * @subtype: The subtype of the work queue indicating its functionality.
16069 *
16070 * This function creates a work queue, as detailed in @wq, on a port, described
16071 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
16072 *
16073 * The @phba struct is used to send mailbox command to HBA. The @wq struct
16074 * is used to get the entry count and entry size that are necessary to
16075 * determine the number of pages to allocate and use for this queue. The @cq
16076 * is used to indicate which completion queue to bind this work queue to. This
16077 * function will send the WQ_CREATE mailbox command to the HBA to setup the
16078 * work queue. This function is asynchronous and will wait for the mailbox
16079 * command to finish before continuing.
16080 *
16081 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040016082 * memory this function will return -ENOMEM. If the queue create mailbox command
16083 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016084 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016085int
James Smart4f774512009-05-22 14:52:35 -040016086lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
16087 struct lpfc_queue *cq, uint32_t subtype)
16088{
16089 struct lpfc_mbx_wq_create *wq_create;
16090 struct lpfc_dmabuf *dmabuf;
16091 LPFC_MBOXQ_t *mbox;
16092 int rc, length, status = 0;
16093 uint32_t shdr_status, shdr_add_status;
16094 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040016095 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050016096 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050016097 void __iomem *bar_memmap_p;
16098 uint32_t db_offset;
16099 uint16_t pci_barset;
James Smart1351e692018-02-22 08:18:43 -080016100 uint8_t dpp_barset;
16101 uint32_t dpp_offset;
James Smart81b96ed2017-11-20 16:00:29 -080016102 uint8_t wq_create_version;
Lee Jones11d8e562020-07-21 17:41:44 +010016103#ifdef CONFIG_X86
16104 unsigned long pg_addr;
16105#endif
James Smart49198b32010-04-06 15:04:33 -040016106
James Smart2e90f4b2011-12-13 13:22:37 -050016107 /* sanity check on queue memory */
16108 if (!wq || !cq)
16109 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040016110 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080016111 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040016112
16113 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16114 if (!mbox)
16115 return -ENOMEM;
16116 length = (sizeof(struct lpfc_mbx_wq_create) -
16117 sizeof(struct lpfc_sli4_cfg_mhdr));
16118 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16119 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
16120 length, LPFC_SLI4_MBX_EMBED);
16121 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050016122 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040016123 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
16124 wq->page_count);
16125 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
16126 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040016127
16128 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050016129 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16130 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050016131
James Smartc176ffa2018-01-30 15:58:46 -080016132 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
16133 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080016134 wq_create_version = LPFC_Q_CREATE_VERSION_1;
16135 else
16136 wq_create_version = LPFC_Q_CREATE_VERSION_0;
16137
James Smart1351e692018-02-22 08:18:43 -080016138 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040016139 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050016140 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
16141 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070016142 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16143 LPFC_Q_CREATE_VERSION_1);
16144
James Smart5a6f1332011-03-11 16:05:35 -050016145 switch (wq->entry_size) {
16146 default:
16147 case 64:
16148 bf_set(lpfc_mbx_wq_create_wqe_size,
16149 &wq_create->u.request_1,
16150 LPFC_WQ_WQE_SIZE_64);
16151 break;
16152 case 128:
16153 bf_set(lpfc_mbx_wq_create_wqe_size,
16154 &wq_create->u.request_1,
16155 LPFC_WQ_WQE_SIZE_128);
16156 break;
16157 }
James Smart1351e692018-02-22 08:18:43 -080016158 /* Request DPP by default */
16159 bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
James Smart8ea73db2017-02-12 13:52:25 -080016160 bf_set(lpfc_mbx_wq_create_page_size,
16161 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080016162 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050016163 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040016164 break;
16165 default:
James Smart1351e692018-02-22 08:18:43 -080016166 page = wq_create->u.request.page;
16167 break;
James Smart5a6f1332011-03-11 16:05:35 -050016168 }
James Smart0c651872013-07-15 18:33:23 -040016169
James Smart4f774512009-05-22 14:52:35 -040016170 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040016171 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050016172 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
16173 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040016174 }
James Smart962bc512013-01-03 15:44:00 -050016175
16176 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16177 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
16178
James Smart4f774512009-05-22 14:52:35 -040016179 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16180 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040016181 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16182 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16183 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016184 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040016185 "2503 WQ_CREATE mailbox failed with "
16186 "status x%x add_status x%x, mbx status x%x\n",
16187 shdr_status, shdr_add_status, rc);
16188 status = -ENXIO;
16189 goto out;
16190 }
James Smart1351e692018-02-22 08:18:43 -080016191
16192 if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
16193 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
16194 &wq_create->u.response);
16195 else
16196 wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
16197 &wq_create->u.response_1);
16198
James Smart4f774512009-05-22 14:52:35 -040016199 if (wq->queue_id == 0xFFFF) {
16200 status = -ENXIO;
16201 goto out;
16202 }
James Smart1351e692018-02-22 08:18:43 -080016203
16204 wq->db_format = LPFC_DB_LIST_FORMAT;
16205 if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
16206 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16207 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
16208 &wq_create->u.response);
16209 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
16210 (wq->db_format != LPFC_DB_RING_FORMAT)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016211 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080016212 "3265 WQ[%d] doorbell format "
16213 "not supported: x%x\n",
16214 wq->queue_id, wq->db_format);
16215 status = -EINVAL;
16216 goto out;
16217 }
16218 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
16219 &wq_create->u.response);
16220 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16221 pci_barset);
16222 if (!bar_memmap_p) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016223 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080016224 "3263 WQ[%d] failed to memmap "
16225 "pci barset:x%x\n",
16226 wq->queue_id, pci_barset);
16227 status = -ENOMEM;
16228 goto out;
16229 }
16230 db_offset = wq_create->u.response.doorbell_offset;
16231 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
16232 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016233 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080016234 "3252 WQ[%d] doorbell offset "
16235 "not supported: x%x\n",
16236 wq->queue_id, db_offset);
16237 status = -EINVAL;
16238 goto out;
16239 }
16240 wq->db_regaddr = bar_memmap_p + db_offset;
16241 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16242 "3264 WQ[%d]: barset:x%x, offset:x%x, "
16243 "format:x%x\n", wq->queue_id,
16244 pci_barset, db_offset, wq->db_format);
16245 } else
16246 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050016247 } else {
James Smart1351e692018-02-22 08:18:43 -080016248 /* Check if DPP was honored by the firmware */
16249 wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
16250 &wq_create->u.response_1);
16251 if (wq->dpp_enable) {
16252 pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
16253 &wq_create->u.response_1);
16254 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16255 pci_barset);
16256 if (!bar_memmap_p) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016257 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080016258 "3267 WQ[%d] failed to memmap "
16259 "pci barset:x%x\n",
16260 wq->queue_id, pci_barset);
16261 status = -ENOMEM;
16262 goto out;
16263 }
16264 db_offset = wq_create->u.response_1.doorbell_offset;
16265 wq->db_regaddr = bar_memmap_p + db_offset;
16266 wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
16267 &wq_create->u.response_1);
16268 dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
16269 &wq_create->u.response_1);
16270 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
16271 dpp_barset);
16272 if (!bar_memmap_p) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016273 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart1351e692018-02-22 08:18:43 -080016274 "3268 WQ[%d] failed to memmap "
16275 "pci barset:x%x\n",
16276 wq->queue_id, dpp_barset);
16277 status = -ENOMEM;
16278 goto out;
16279 }
16280 dpp_offset = wq_create->u.response_1.dpp_offset;
16281 wq->dpp_regaddr = bar_memmap_p + dpp_offset;
16282 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16283 "3271 WQ[%d]: barset:x%x, offset:x%x, "
16284 "dpp_id:x%x dpp_barset:x%x "
16285 "dpp_offset:x%x\n",
16286 wq->queue_id, pci_barset, db_offset,
16287 wq->dpp_id, dpp_barset, dpp_offset);
16288
Lee Jones3c1311a2020-07-21 17:41:28 +010016289#ifdef CONFIG_X86
James Smart1351e692018-02-22 08:18:43 -080016290 /* Enable combined writes for DPP aperture */
16291 pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
James Smart1351e692018-02-22 08:18:43 -080016292 rc = set_memory_wc(pg_addr, 1);
16293 if (rc) {
16294 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16295 "3272 Cannot setup Combined "
16296 "Write on WQ[%d] - disable DPP\n",
16297 wq->queue_id);
16298 phba->cfg_enable_dpp = 0;
16299 }
16300#else
16301 phba->cfg_enable_dpp = 0;
16302#endif
16303 } else
16304 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
James Smart962bc512013-01-03 15:44:00 -050016305 }
James Smart895427b2017-02-12 13:52:30 -080016306 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
16307 if (wq->pring == NULL) {
16308 status = -ENOMEM;
16309 goto out;
16310 }
James Smart4f774512009-05-22 14:52:35 -040016311 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050016312 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040016313 wq->subtype = subtype;
16314 wq->host_index = 0;
16315 wq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016316 wq->notify_interval = LPFC_WQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040016317
16318 /* link the wq onto the parent cq child list */
16319 list_add_tail(&wq->list, &cq->child_list);
16320out:
James Smart8fa38512009-07-19 10:01:03 -040016321 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016322 return status;
16323}
16324
16325/**
16326 * lpfc_rq_create - Create a Receive Queue on the HBA
16327 * @phba: HBA structure that indicates port to create a queue on.
16328 * @hrq: The queue structure to use to create the header receive queue.
16329 * @drq: The queue structure to use to create the data receive queue.
16330 * @cq: The completion queue to bind this work queue to.
Lee Jones7af29d42020-07-21 17:41:31 +010016331 * @subtype: The subtype of the work queue indicating its functionality.
James Smart4f774512009-05-22 14:52:35 -040016332 *
16333 * This function creates a receive buffer queue pair , as detailed in @hrq and
16334 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
16335 * to the HBA.
16336 *
16337 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
16338 * struct is used to get the entry count that is necessary to determine the
16339 * number of pages to use for this queue. The @cq is used to indicate which
16340 * completion queue to bind received buffers that are posted to these queues to.
16341 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
16342 * receive queue pair. This function is asynchronous and will wait for the
16343 * mailbox command to finish before continuing.
16344 *
16345 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040016346 * memory this function will return -ENOMEM. If the queue create mailbox command
16347 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016348 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016349int
James Smart4f774512009-05-22 14:52:35 -040016350lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
16351 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
16352{
16353 struct lpfc_mbx_rq_create *rq_create;
16354 struct lpfc_dmabuf *dmabuf;
16355 LPFC_MBOXQ_t *mbox;
16356 int rc, length, status = 0;
16357 uint32_t shdr_status, shdr_add_status;
16358 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040016359 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050016360 void __iomem *bar_memmap_p;
16361 uint32_t db_offset;
16362 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040016363
James Smart2e90f4b2011-12-13 13:22:37 -050016364 /* sanity check on queue memory */
16365 if (!hrq || !drq || !cq)
16366 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040016367 if (!phba->sli4_hba.pc_sli4_params.supported)
16368 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040016369
16370 if (hrq->entry_count != drq->entry_count)
16371 return -EINVAL;
16372 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16373 if (!mbox)
16374 return -ENOMEM;
16375 length = (sizeof(struct lpfc_mbx_rq_create) -
16376 sizeof(struct lpfc_sli4_cfg_mhdr));
16377 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16378 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
16379 length, LPFC_SLI4_MBX_EMBED);
16380 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050016381 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
16382 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16383 phba->sli4_hba.pc_sli4_params.rqv);
16384 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
16385 bf_set(lpfc_rq_context_rqe_count_1,
16386 &rq_create->u.request.context,
16387 hrq->entry_count);
16388 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040016389 bf_set(lpfc_rq_context_rqe_size,
16390 &rq_create->u.request.context,
16391 LPFC_RQE_SIZE_8);
16392 bf_set(lpfc_rq_context_page_size,
16393 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080016394 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050016395 } else {
16396 switch (hrq->entry_count) {
16397 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070016398 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5a6f1332011-03-11 16:05:35 -050016399 "2535 Unsupported RQ count. (%d)\n",
16400 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040016401 if (hrq->entry_count < 512) {
16402 status = -EINVAL;
16403 goto out;
16404 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016405 fallthrough; /* otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050016406 case 512:
16407 bf_set(lpfc_rq_context_rqe_count,
16408 &rq_create->u.request.context,
16409 LPFC_RQ_RING_SIZE_512);
16410 break;
16411 case 1024:
16412 bf_set(lpfc_rq_context_rqe_count,
16413 &rq_create->u.request.context,
16414 LPFC_RQ_RING_SIZE_1024);
16415 break;
16416 case 2048:
16417 bf_set(lpfc_rq_context_rqe_count,
16418 &rq_create->u.request.context,
16419 LPFC_RQ_RING_SIZE_2048);
16420 break;
16421 case 4096:
16422 bf_set(lpfc_rq_context_rqe_count,
16423 &rq_create->u.request.context,
16424 LPFC_RQ_RING_SIZE_4096);
16425 break;
16426 }
16427 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
16428 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040016429 }
16430 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
16431 cq->queue_id);
16432 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
16433 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040016434 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040016435 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040016436 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16437 putPaddrLow(dmabuf->phys);
16438 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16439 putPaddrHigh(dmabuf->phys);
16440 }
James Smart962bc512013-01-03 15:44:00 -050016441 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16442 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
16443
James Smart4f774512009-05-22 14:52:35 -040016444 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16445 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040016446 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16447 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16448 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016449 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040016450 "2504 RQ_CREATE mailbox failed with "
16451 "status x%x add_status x%x, mbx status x%x\n",
16452 shdr_status, shdr_add_status, rc);
16453 status = -ENXIO;
16454 goto out;
16455 }
16456 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16457 if (hrq->queue_id == 0xFFFF) {
16458 status = -ENXIO;
16459 goto out;
16460 }
James Smart962bc512013-01-03 15:44:00 -050016461
16462 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
16463 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
16464 &rq_create->u.response);
16465 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
16466 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016467 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -050016468 "3262 RQ [%d] doorbell format not "
16469 "supported: x%x\n", hrq->queue_id,
16470 hrq->db_format);
16471 status = -EINVAL;
16472 goto out;
16473 }
16474
16475 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
16476 &rq_create->u.response);
16477 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
16478 if (!bar_memmap_p) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016479 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -050016480 "3269 RQ[%d] failed to memmap pci "
16481 "barset:x%x\n", hrq->queue_id,
16482 pci_barset);
16483 status = -ENOMEM;
16484 goto out;
16485 }
16486
16487 db_offset = rq_create->u.response.doorbell_offset;
16488 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
16489 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016490 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart962bc512013-01-03 15:44:00 -050016491 "3270 RQ[%d] doorbell offset not "
16492 "supported: x%x\n", hrq->queue_id,
16493 db_offset);
16494 status = -EINVAL;
16495 goto out;
16496 }
16497 hrq->db_regaddr = bar_memmap_p + db_offset;
16498 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040016499 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
16500 "format:x%x\n", hrq->queue_id, pci_barset,
16501 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050016502 } else {
16503 hrq->db_format = LPFC_DB_RING_FORMAT;
16504 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16505 }
James Smart4f774512009-05-22 14:52:35 -040016506 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050016507 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040016508 hrq->subtype = subtype;
16509 hrq->host_index = 0;
16510 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016511 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040016512
16513 /* now create the data queue */
16514 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16515 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
16516 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050016517 bf_set(lpfc_mbox_hdr_version, &shdr->request,
16518 phba->sli4_hba.pc_sli4_params.rqv);
16519 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
16520 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040016521 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070016522 if (subtype == LPFC_NVMET)
16523 rq_create->u.request.context.buffer_size =
16524 LPFC_NVMET_DATA_BUF_SIZE;
16525 else
16526 rq_create->u.request.context.buffer_size =
16527 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040016528 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
16529 LPFC_RQE_SIZE_8);
16530 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
16531 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050016532 } else {
16533 switch (drq->entry_count) {
16534 default:
Dick Kennedy372c1872020-06-30 14:50:00 -070016535 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart5a6f1332011-03-11 16:05:35 -050016536 "2536 Unsupported RQ count. (%d)\n",
16537 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040016538 if (drq->entry_count < 512) {
16539 status = -EINVAL;
16540 goto out;
16541 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -050016542 fallthrough; /* otherwise default to smallest count */
James Smart5a6f1332011-03-11 16:05:35 -050016543 case 512:
16544 bf_set(lpfc_rq_context_rqe_count,
16545 &rq_create->u.request.context,
16546 LPFC_RQ_RING_SIZE_512);
16547 break;
16548 case 1024:
16549 bf_set(lpfc_rq_context_rqe_count,
16550 &rq_create->u.request.context,
16551 LPFC_RQ_RING_SIZE_1024);
16552 break;
16553 case 2048:
16554 bf_set(lpfc_rq_context_rqe_count,
16555 &rq_create->u.request.context,
16556 LPFC_RQ_RING_SIZE_2048);
16557 break;
16558 case 4096:
16559 bf_set(lpfc_rq_context_rqe_count,
16560 &rq_create->u.request.context,
16561 LPFC_RQ_RING_SIZE_4096);
16562 break;
16563 }
James Smart3c603be2017-05-15 15:20:44 -070016564 if (subtype == LPFC_NVMET)
16565 bf_set(lpfc_rq_context_buf_size,
16566 &rq_create->u.request.context,
16567 LPFC_NVMET_DATA_BUF_SIZE);
16568 else
16569 bf_set(lpfc_rq_context_buf_size,
16570 &rq_create->u.request.context,
16571 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040016572 }
16573 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
16574 cq->queue_id);
16575 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
16576 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040016577 list_for_each_entry(dmabuf, &drq->page_list, list) {
16578 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
16579 putPaddrLow(dmabuf->phys);
16580 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
16581 putPaddrHigh(dmabuf->phys);
16582 }
James Smart962bc512013-01-03 15:44:00 -050016583 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
16584 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040016585 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16586 /* The IOCTL status is embedded in the mailbox subheader. */
16587 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
16588 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16589 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16590 if (shdr_status || shdr_add_status || rc) {
16591 status = -ENXIO;
16592 goto out;
16593 }
16594 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16595 if (drq->queue_id == 0xFFFF) {
16596 status = -ENXIO;
16597 goto out;
16598 }
16599 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050016600 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040016601 drq->subtype = subtype;
16602 drq->host_index = 0;
16603 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016604 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart4f774512009-05-22 14:52:35 -040016605
16606 /* link the header and data RQs onto the parent cq child list */
16607 list_add_tail(&hrq->list, &cq->child_list);
16608 list_add_tail(&drq->list, &cq->child_list);
16609
16610out:
James Smart8fa38512009-07-19 10:01:03 -040016611 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016612 return status;
16613}
16614
16615/**
James Smart2d7dbc42017-02-12 13:52:35 -080016616 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
16617 * @phba: HBA structure that indicates port to create a queue on.
16618 * @hrqp: The queue structure array to use to create the header receive queues.
16619 * @drqp: The queue structure array to use to create the data receive queues.
16620 * @cqp: The completion queue array to bind these receive queues to.
Lee Jones7af29d42020-07-21 17:41:31 +010016621 * @subtype: Functional purpose of the queue (MBOX, IO, ELS, NVMET, etc).
James Smart2d7dbc42017-02-12 13:52:35 -080016622 *
16623 * This function creates a receive buffer queue pair , as detailed in @hrq and
16624 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
16625 * to the HBA.
16626 *
16627 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
16628 * struct is used to get the entry count that is necessary to determine the
16629 * number of pages to use for this queue. The @cq is used to indicate which
16630 * completion queue to bind received buffers that are posted to these queues to.
16631 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
16632 * receive queue pair. This function is asynchronous and will wait for the
16633 * mailbox command to finish before continuing.
16634 *
16635 * On success this function will return a zero. If unable to allocate enough
16636 * memory this function will return -ENOMEM. If the queue create mailbox command
16637 * fails this function will return -ENXIO.
16638 **/
16639int
16640lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
16641 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
16642 uint32_t subtype)
16643{
16644 struct lpfc_queue *hrq, *drq, *cq;
16645 struct lpfc_mbx_rq_create_v2 *rq_create;
16646 struct lpfc_dmabuf *dmabuf;
16647 LPFC_MBOXQ_t *mbox;
16648 int rc, length, alloclen, status = 0;
16649 int cnt, idx, numrq, page_idx = 0;
16650 uint32_t shdr_status, shdr_add_status;
16651 union lpfc_sli4_cfg_shdr *shdr;
16652 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
16653
16654 numrq = phba->cfg_nvmet_mrq;
16655 /* sanity check on array memory */
16656 if (!hrqp || !drqp || !cqp || !numrq)
16657 return -ENODEV;
16658 if (!phba->sli4_hba.pc_sli4_params.supported)
16659 hw_page_size = SLI4_PAGE_SIZE;
16660
16661 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16662 if (!mbox)
16663 return -ENOMEM;
16664
16665 length = sizeof(struct lpfc_mbx_rq_create_v2);
16666 length += ((2 * numrq * hrqp[0]->page_count) *
16667 sizeof(struct dma_address));
16668
16669 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16670 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
16671 LPFC_SLI4_MBX_NEMBED);
16672 if (alloclen < length) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016673 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080016674 "3099 Allocated DMA memory size (%d) is "
16675 "less than the requested DMA memory size "
16676 "(%d)\n", alloclen, length);
16677 status = -ENOMEM;
16678 goto out;
16679 }
16680
16681
16682
16683 rq_create = mbox->sge_array->addr[0];
16684 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
16685
16686 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
16687 cnt = 0;
16688
16689 for (idx = 0; idx < numrq; idx++) {
16690 hrq = hrqp[idx];
16691 drq = drqp[idx];
16692 cq = cqp[idx];
16693
James Smart2d7dbc42017-02-12 13:52:35 -080016694 /* sanity check on queue memory */
16695 if (!hrq || !drq || !cq) {
16696 status = -ENODEV;
16697 goto out;
16698 }
16699
James Smart7aabe842017-03-04 09:30:22 -080016700 if (hrq->entry_count != drq->entry_count) {
16701 status = -EINVAL;
16702 goto out;
16703 }
16704
James Smart2d7dbc42017-02-12 13:52:35 -080016705 if (idx == 0) {
16706 bf_set(lpfc_mbx_rq_create_num_pages,
16707 &rq_create->u.request,
16708 hrq->page_count);
16709 bf_set(lpfc_mbx_rq_create_rq_cnt,
16710 &rq_create->u.request, (numrq * 2));
16711 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
16712 1);
16713 bf_set(lpfc_rq_context_base_cq,
16714 &rq_create->u.request.context,
16715 cq->queue_id);
16716 bf_set(lpfc_rq_context_data_size,
16717 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070016718 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080016719 bf_set(lpfc_rq_context_hdr_size,
16720 &rq_create->u.request.context,
16721 LPFC_HDR_BUF_SIZE);
16722 bf_set(lpfc_rq_context_rqe_count_1,
16723 &rq_create->u.request.context,
16724 hrq->entry_count);
16725 bf_set(lpfc_rq_context_rqe_size,
16726 &rq_create->u.request.context,
16727 LPFC_RQE_SIZE_8);
16728 bf_set(lpfc_rq_context_page_size,
16729 &rq_create->u.request.context,
16730 (PAGE_SIZE/SLI4_PAGE_SIZE));
16731 }
16732 rc = 0;
16733 list_for_each_entry(dmabuf, &hrq->page_list, list) {
16734 memset(dmabuf->virt, 0, hw_page_size);
16735 cnt = page_idx + dmabuf->buffer_tag;
16736 rq_create->u.request.page[cnt].addr_lo =
16737 putPaddrLow(dmabuf->phys);
16738 rq_create->u.request.page[cnt].addr_hi =
16739 putPaddrHigh(dmabuf->phys);
16740 rc++;
16741 }
16742 page_idx += rc;
16743
16744 rc = 0;
16745 list_for_each_entry(dmabuf, &drq->page_list, list) {
16746 memset(dmabuf->virt, 0, hw_page_size);
16747 cnt = page_idx + dmabuf->buffer_tag;
16748 rq_create->u.request.page[cnt].addr_lo =
16749 putPaddrLow(dmabuf->phys);
16750 rq_create->u.request.page[cnt].addr_hi =
16751 putPaddrHigh(dmabuf->phys);
16752 rc++;
16753 }
16754 page_idx += rc;
16755
16756 hrq->db_format = LPFC_DB_RING_FORMAT;
16757 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16758 hrq->type = LPFC_HRQ;
16759 hrq->assoc_qid = cq->queue_id;
16760 hrq->subtype = subtype;
16761 hrq->host_index = 0;
16762 hrq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016763 hrq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016764
16765 drq->db_format = LPFC_DB_RING_FORMAT;
16766 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
16767 drq->type = LPFC_DRQ;
16768 drq->assoc_qid = cq->queue_id;
16769 drq->subtype = subtype;
16770 drq->host_index = 0;
16771 drq->hba_index = 0;
James Smart32517fc2019-01-28 11:14:33 -080016772 drq->notify_interval = LPFC_RQ_NOTIFY_INTRVL;
James Smart2d7dbc42017-02-12 13:52:35 -080016773
16774 list_add_tail(&hrq->list, &cq->child_list);
16775 list_add_tail(&drq->list, &cq->child_list);
16776 }
16777
16778 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16779 /* The IOCTL status is embedded in the mailbox subheader. */
16780 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16781 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16782 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016783 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2d7dbc42017-02-12 13:52:35 -080016784 "3120 RQ_CREATE mailbox failed with "
16785 "status x%x add_status x%x, mbx status x%x\n",
16786 shdr_status, shdr_add_status, rc);
16787 status = -ENXIO;
16788 goto out;
16789 }
16790 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
16791 if (rc == 0xFFFF) {
16792 status = -ENXIO;
16793 goto out;
16794 }
16795
16796 /* Initialize all RQs with associated queue id */
16797 for (idx = 0; idx < numrq; idx++) {
16798 hrq = hrqp[idx];
16799 hrq->queue_id = rc + (2 * idx);
16800 drq = drqp[idx];
16801 drq->queue_id = rc + (2 * idx) + 1;
16802 }
16803
16804out:
16805 lpfc_sli4_mbox_cmd_free(phba, mbox);
16806 return status;
16807}
16808
16809/**
James Smart4f774512009-05-22 14:52:35 -040016810 * lpfc_eq_destroy - Destroy an event Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010016811 * @phba: HBA structure that indicates port to destroy a queue on.
James Smart4f774512009-05-22 14:52:35 -040016812 * @eq: The queue structure associated with the queue to destroy.
16813 *
16814 * This function destroys a queue, as detailed in @eq by sending an mailbox
16815 * command, specific to the type of queue, to the HBA.
16816 *
16817 * The @eq struct is used to get the queue ID of the queue to destroy.
16818 *
16819 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016820 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016821 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016822int
James Smart4f774512009-05-22 14:52:35 -040016823lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
16824{
16825 LPFC_MBOXQ_t *mbox;
16826 int rc, length, status = 0;
16827 uint32_t shdr_status, shdr_add_status;
16828 union lpfc_sli4_cfg_shdr *shdr;
16829
James Smart2e90f4b2011-12-13 13:22:37 -050016830 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016831 if (!eq)
16832 return -ENODEV;
James Smart32517fc2019-01-28 11:14:33 -080016833
James Smart4f774512009-05-22 14:52:35 -040016834 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
16835 if (!mbox)
16836 return -ENOMEM;
16837 length = (sizeof(struct lpfc_mbx_eq_destroy) -
16838 sizeof(struct lpfc_sli4_cfg_mhdr));
16839 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16840 LPFC_MBOX_OPCODE_EQ_DESTROY,
16841 length, LPFC_SLI4_MBX_EMBED);
16842 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
16843 eq->queue_id);
16844 mbox->vport = eq->phba->pport;
16845 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16846
16847 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
16848 /* The IOCTL status is embedded in the mailbox subheader. */
16849 shdr = (union lpfc_sli4_cfg_shdr *)
16850 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
16851 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16852 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16853 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016854 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040016855 "2505 EQ_DESTROY mailbox failed with "
16856 "status x%x add_status x%x, mbx status x%x\n",
16857 shdr_status, shdr_add_status, rc);
16858 status = -ENXIO;
16859 }
16860
16861 /* Remove eq from any list */
16862 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040016863 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016864 return status;
16865}
16866
16867/**
16868 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010016869 * @phba: HBA structure that indicates port to destroy a queue on.
James Smart4f774512009-05-22 14:52:35 -040016870 * @cq: The queue structure associated with the queue to destroy.
16871 *
16872 * This function destroys a queue, as detailed in @cq by sending an mailbox
16873 * command, specific to the type of queue, to the HBA.
16874 *
16875 * The @cq struct is used to get the queue ID of the queue to destroy.
16876 *
16877 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016878 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016879 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016880int
James Smart4f774512009-05-22 14:52:35 -040016881lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
16882{
16883 LPFC_MBOXQ_t *mbox;
16884 int rc, length, status = 0;
16885 uint32_t shdr_status, shdr_add_status;
16886 union lpfc_sli4_cfg_shdr *shdr;
16887
James Smart2e90f4b2011-12-13 13:22:37 -050016888 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016889 if (!cq)
16890 return -ENODEV;
16891 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
16892 if (!mbox)
16893 return -ENOMEM;
16894 length = (sizeof(struct lpfc_mbx_cq_destroy) -
16895 sizeof(struct lpfc_sli4_cfg_mhdr));
16896 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16897 LPFC_MBOX_OPCODE_CQ_DESTROY,
16898 length, LPFC_SLI4_MBX_EMBED);
16899 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
16900 cq->queue_id);
16901 mbox->vport = cq->phba->pport;
16902 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16903 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
16904 /* The IOCTL status is embedded in the mailbox subheader. */
16905 shdr = (union lpfc_sli4_cfg_shdr *)
16906 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
16907 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16908 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16909 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016910 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040016911 "2506 CQ_DESTROY mailbox failed with "
16912 "status x%x add_status x%x, mbx status x%x\n",
16913 shdr_status, shdr_add_status, rc);
16914 status = -ENXIO;
16915 }
16916 /* Remove cq from any list */
16917 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040016918 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040016919 return status;
16920}
16921
16922/**
James Smart04c68492009-05-22 14:52:52 -040016923 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010016924 * @phba: HBA structure that indicates port to destroy a queue on.
16925 * @mq: The queue structure associated with the queue to destroy.
James Smart04c68492009-05-22 14:52:52 -040016926 *
16927 * This function destroys a queue, as detailed in @mq by sending an mailbox
16928 * command, specific to the type of queue, to the HBA.
16929 *
16930 * The @mq struct is used to get the queue ID of the queue to destroy.
16931 *
16932 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016933 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040016934 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016935int
James Smart04c68492009-05-22 14:52:52 -040016936lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
16937{
16938 LPFC_MBOXQ_t *mbox;
16939 int rc, length, status = 0;
16940 uint32_t shdr_status, shdr_add_status;
16941 union lpfc_sli4_cfg_shdr *shdr;
16942
James Smart2e90f4b2011-12-13 13:22:37 -050016943 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040016944 if (!mq)
16945 return -ENODEV;
16946 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
16947 if (!mbox)
16948 return -ENOMEM;
16949 length = (sizeof(struct lpfc_mbx_mq_destroy) -
16950 sizeof(struct lpfc_sli4_cfg_mhdr));
16951 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16952 LPFC_MBOX_OPCODE_MQ_DESTROY,
16953 length, LPFC_SLI4_MBX_EMBED);
16954 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
16955 mq->queue_id);
16956 mbox->vport = mq->phba->pport;
16957 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16958 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
16959 /* The IOCTL status is embedded in the mailbox subheader. */
16960 shdr = (union lpfc_sli4_cfg_shdr *)
16961 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
16962 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16963 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16964 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070016965 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart04c68492009-05-22 14:52:52 -040016966 "2507 MQ_DESTROY mailbox failed with "
16967 "status x%x add_status x%x, mbx status x%x\n",
16968 shdr_status, shdr_add_status, rc);
16969 status = -ENXIO;
16970 }
16971 /* Remove mq from any list */
16972 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040016973 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040016974 return status;
16975}
16976
16977/**
James Smart4f774512009-05-22 14:52:35 -040016978 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010016979 * @phba: HBA structure that indicates port to destroy a queue on.
James Smart4f774512009-05-22 14:52:35 -040016980 * @wq: The queue structure associated with the queue to destroy.
16981 *
16982 * This function destroys a queue, as detailed in @wq by sending an mailbox
16983 * command, specific to the type of queue, to the HBA.
16984 *
16985 * The @wq struct is used to get the queue ID of the queue to destroy.
16986 *
16987 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040016988 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040016989 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040016990int
James Smart4f774512009-05-22 14:52:35 -040016991lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
16992{
16993 LPFC_MBOXQ_t *mbox;
16994 int rc, length, status = 0;
16995 uint32_t shdr_status, shdr_add_status;
16996 union lpfc_sli4_cfg_shdr *shdr;
16997
James Smart2e90f4b2011-12-13 13:22:37 -050016998 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040016999 if (!wq)
17000 return -ENODEV;
17001 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
17002 if (!mbox)
17003 return -ENOMEM;
17004 length = (sizeof(struct lpfc_mbx_wq_destroy) -
17005 sizeof(struct lpfc_sli4_cfg_mhdr));
17006 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17007 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
17008 length, LPFC_SLI4_MBX_EMBED);
17009 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
17010 wq->queue_id);
17011 mbox->vport = wq->phba->pport;
17012 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17013 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
17014 shdr = (union lpfc_sli4_cfg_shdr *)
17015 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
17016 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17017 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17018 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017019 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017020 "2508 WQ_DESTROY mailbox failed with "
17021 "status x%x add_status x%x, mbx status x%x\n",
17022 shdr_status, shdr_add_status, rc);
17023 status = -ENXIO;
17024 }
17025 /* Remove wq from any list */
17026 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070017027 kfree(wq->pring);
17028 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040017029 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017030 return status;
17031}
17032
17033/**
17034 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
Lee Jones7af29d42020-07-21 17:41:31 +010017035 * @phba: HBA structure that indicates port to destroy a queue on.
17036 * @hrq: The queue structure associated with the queue to destroy.
17037 * @drq: The queue structure associated with the queue to destroy.
James Smart4f774512009-05-22 14:52:35 -040017038 *
17039 * This function destroys a queue, as detailed in @rq by sending an mailbox
17040 * command, specific to the type of queue, to the HBA.
17041 *
17042 * The @rq struct is used to get the queue ID of the queue to destroy.
17043 *
17044 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040017045 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040017046 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040017047int
James Smart4f774512009-05-22 14:52:35 -040017048lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
17049 struct lpfc_queue *drq)
17050{
17051 LPFC_MBOXQ_t *mbox;
17052 int rc, length, status = 0;
17053 uint32_t shdr_status, shdr_add_status;
17054 union lpfc_sli4_cfg_shdr *shdr;
17055
James Smart2e90f4b2011-12-13 13:22:37 -050017056 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040017057 if (!hrq || !drq)
17058 return -ENODEV;
17059 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
17060 if (!mbox)
17061 return -ENOMEM;
17062 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050017063 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040017064 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17065 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
17066 length, LPFC_SLI4_MBX_EMBED);
17067 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17068 hrq->queue_id);
17069 mbox->vport = hrq->phba->pport;
17070 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17071 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
17072 /* The IOCTL status is embedded in the mailbox subheader. */
17073 shdr = (union lpfc_sli4_cfg_shdr *)
17074 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17075 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17076 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17077 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017078 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017079 "2509 RQ_DESTROY mailbox failed with "
17080 "status x%x add_status x%x, mbx status x%x\n",
17081 shdr_status, shdr_add_status, rc);
17082 if (rc != MBX_TIMEOUT)
17083 mempool_free(mbox, hrq->phba->mbox_mem_pool);
17084 return -ENXIO;
17085 }
17086 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
17087 drq->queue_id);
17088 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
17089 shdr = (union lpfc_sli4_cfg_shdr *)
17090 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
17091 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17092 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17093 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017094 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017095 "2510 RQ_DESTROY mailbox failed with "
17096 "status x%x add_status x%x, mbx status x%x\n",
17097 shdr_status, shdr_add_status, rc);
17098 status = -ENXIO;
17099 }
17100 list_del_init(&hrq->list);
17101 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040017102 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040017103 return status;
17104}
17105
17106/**
17107 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
17108 * @phba: The virtual port for which this call being executed.
17109 * @pdma_phys_addr0: Physical address of the 1st SGL page.
17110 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
17111 * @xritag: the xritag that ties this io to the SGL pages.
17112 *
17113 * This routine will post the sgl pages for the IO that has the xritag
17114 * that is in the iocbq structure. The xritag is assigned during iocbq
17115 * creation and persists for as long as the driver is loaded.
17116 * if the caller has fewer than 256 scatter gather segments to map then
17117 * pdma_phys_addr1 should be 0.
17118 * If the caller needs to map more than 256 scatter gather segment then
17119 * pdma_phys_addr1 should be a valid physical address.
17120 * physical address for SGLs must be 64 byte aligned.
17121 * If you are going to map 2 SGL's then the first one must have 256 entries
17122 * the second sgl can have between 1 and 256 entries.
17123 *
17124 * Return codes:
17125 * 0 - Success
17126 * -ENXIO, -ENOMEM - Failure
17127 **/
17128int
17129lpfc_sli4_post_sgl(struct lpfc_hba *phba,
17130 dma_addr_t pdma_phys_addr0,
17131 dma_addr_t pdma_phys_addr1,
17132 uint16_t xritag)
17133{
17134 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
17135 LPFC_MBOXQ_t *mbox;
17136 int rc;
17137 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040017138 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040017139 union lpfc_sli4_cfg_shdr *shdr;
17140
17141 if (xritag == NO_XRI) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017142 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017143 "0364 Invalid param:\n");
17144 return -EINVAL;
17145 }
17146
17147 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17148 if (!mbox)
17149 return -ENOMEM;
17150
17151 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17152 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17153 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050017154 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040017155
17156 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
17157 &mbox->u.mqe.un.post_sgl_pages;
17158 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
17159 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
17160
17161 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
17162 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
17163 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
17164 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
17165
17166 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
17167 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
17168 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
17169 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
17170 if (!phba->sli4_hba.intr_enable)
17171 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040017172 else {
James Smarta183a152011-10-10 21:32:43 -040017173 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040017174 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17175 }
James Smart4f774512009-05-22 14:52:35 -040017176 /* The IOCTL status is embedded in the mailbox subheader. */
17177 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
17178 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17179 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17180 if (rc != MBX_TIMEOUT)
17181 mempool_free(mbox, phba->mbox_mem_pool);
17182 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017183 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017184 "2511 POST_SGL mailbox failed with "
17185 "status x%x add_status x%x, mbx status x%x\n",
17186 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040017187 }
17188 return 0;
17189}
James Smart4f774512009-05-22 14:52:35 -040017190
17191/**
James Smart88a2cfb2011-07-22 18:36:33 -040017192 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040017193 * @phba: pointer to lpfc hba data structure.
17194 *
17195 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040017196 * HBA consistent with the SLI-4 interface spec. This routine
17197 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17198 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040017199 *
James Smart88a2cfb2011-07-22 18:36:33 -040017200 * Returns
17201 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
17202 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17203 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017204static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040017205lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
17206{
17207 unsigned long xri;
17208
17209 /*
17210 * Fetch the next logical xri. Because this index is logical,
17211 * the driver starts at 0 each time.
17212 */
17213 spin_lock_irq(&phba->hbalock);
17214 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
17215 phba->sli4_hba.max_cfg_param.max_xri, 0);
17216 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
17217 spin_unlock_irq(&phba->hbalock);
17218 return NO_XRI;
17219 } else {
17220 set_bit(xri, phba->sli4_hba.xri_bmask);
17221 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040017222 }
James Smart6d368e52011-05-24 11:44:12 -040017223 spin_unlock_irq(&phba->hbalock);
17224 return xri;
17225}
17226
17227/**
Lee Jones8514e2f2021-03-03 14:46:18 +000017228 * __lpfc_sli4_free_xri - Release an xri for reuse.
James Smart6d368e52011-05-24 11:44:12 -040017229 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010017230 * @xri: xri to release.
James Smart6d368e52011-05-24 11:44:12 -040017231 *
17232 * This routine is invoked to release an xri to the pool of
17233 * available rpis maintained by the driver.
17234 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017235static void
James Smart6d368e52011-05-24 11:44:12 -040017236__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
17237{
17238 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040017239 phba->sli4_hba.max_cfg_param.xri_used--;
17240 }
17241}
17242
17243/**
17244 * lpfc_sli4_free_xri - Release an xri for reuse.
17245 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010017246 * @xri: xri to release.
James Smart6d368e52011-05-24 11:44:12 -040017247 *
17248 * This routine is invoked to release an xri to the pool of
17249 * available rpis maintained by the driver.
17250 **/
17251void
17252lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
17253{
17254 spin_lock_irq(&phba->hbalock);
17255 __lpfc_sli4_free_xri(phba, xri);
17256 spin_unlock_irq(&phba->hbalock);
17257}
17258
17259/**
James Smart4f774512009-05-22 14:52:35 -040017260 * lpfc_sli4_next_xritag - Get an xritag for the io
17261 * @phba: Pointer to HBA context object.
17262 *
17263 * This function gets an xritag for the iocb. If there is no unused xritag
17264 * it will return 0xffff.
17265 * The function returns the allocated xritag if successful, else returns zero.
17266 * Zero is not a valid xritag.
17267 * The caller is not required to hold any lock.
17268 **/
17269uint16_t
17270lpfc_sli4_next_xritag(struct lpfc_hba *phba)
17271{
James Smart6d368e52011-05-24 11:44:12 -040017272 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040017273
James Smart6d368e52011-05-24 11:44:12 -040017274 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040017275 if (xri_index == NO_XRI)
17276 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17277 "2004 Failed to allocate XRI.last XRITAG is %d"
17278 " Max XRI is %d, Used XRI is %d\n",
17279 xri_index,
17280 phba->sli4_hba.max_cfg_param.max_xri,
17281 phba->sli4_hba.max_cfg_param.xri_used);
17282 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040017283}
17284
17285/**
James Smart895427b2017-02-12 13:52:30 -080017286 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040017287 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040017288 * @post_sgl_list: pointer to els sgl entry list.
Lee Jones7af29d42020-07-21 17:41:31 +010017289 * @post_cnt: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040017290 *
17291 * This routine is invoked to post a block of driver's sgl pages to the
17292 * HBA using non-embedded mailbox command. No Lock is held. This routine
17293 * is only called when the driver is loading and after all IO has been
17294 * stopped.
17295 **/
James Smart8a9d2e82012-05-09 21:16:12 -040017296static int
James Smart895427b2017-02-12 13:52:30 -080017297lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040017298 struct list_head *post_sgl_list,
17299 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040017300{
James Smart8a9d2e82012-05-09 21:16:12 -040017301 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040017302 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
17303 struct sgl_page_pairs *sgl_pg_pairs;
17304 void *viraddr;
17305 LPFC_MBOXQ_t *mbox;
17306 uint32_t reqlen, alloclen, pg_pairs;
17307 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040017308 uint16_t xritag_start = 0;
17309 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040017310 uint32_t shdr_status, shdr_add_status;
17311 union lpfc_sli4_cfg_shdr *shdr;
17312
James Smart895427b2017-02-12 13:52:30 -080017313 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040017314 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040017315 if (reqlen > SLI4_PAGE_SIZE) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017316 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017317 "2559 Block sgl registration required DMA "
17318 "size (%d) great than a page\n", reqlen);
17319 return -ENOMEM;
17320 }
James Smart895427b2017-02-12 13:52:30 -080017321
James Smart4f774512009-05-22 14:52:35 -040017322 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040017323 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040017324 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040017325
17326 /* Allocate DMA memory and set up the non-embedded mailbox command */
17327 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17328 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
17329 LPFC_SLI4_MBX_NEMBED);
17330
17331 if (alloclen < reqlen) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017332 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017333 "0285 Allocated DMA memory size (%d) is "
17334 "less than the requested DMA memory "
17335 "size (%d)\n", alloclen, reqlen);
17336 lpfc_sli4_mbox_cmd_free(phba, mbox);
17337 return -ENOMEM;
17338 }
James Smart4f774512009-05-22 14:52:35 -040017339 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040017340 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040017341 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
17342 sgl_pg_pairs = &sgl->sgl_pg_pairs;
17343
James Smart8a9d2e82012-05-09 21:16:12 -040017344 pg_pairs = 0;
17345 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040017346 /* Set up the sge entry */
17347 sgl_pg_pairs->sgl_pg0_addr_lo =
17348 cpu_to_le32(putPaddrLow(sglq_entry->phys));
17349 sgl_pg_pairs->sgl_pg0_addr_hi =
17350 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
17351 sgl_pg_pairs->sgl_pg1_addr_lo =
17352 cpu_to_le32(putPaddrLow(0));
17353 sgl_pg_pairs->sgl_pg1_addr_hi =
17354 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040017355
James Smart4f774512009-05-22 14:52:35 -040017356 /* Keep the first xritag on the list */
17357 if (pg_pairs == 0)
17358 xritag_start = sglq_entry->sli4_xritag;
17359 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040017360 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040017361 }
James Smart6d368e52011-05-24 11:44:12 -040017362
17363 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040017364 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080017365 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040017366 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080017367
James Smart4f774512009-05-22 14:52:35 -040017368 if (!phba->sli4_hba.intr_enable)
17369 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17370 else {
James Smarta183a152011-10-10 21:32:43 -040017371 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040017372 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17373 }
17374 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
17375 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17376 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17377 if (rc != MBX_TIMEOUT)
17378 lpfc_sli4_mbox_cmd_free(phba, mbox);
17379 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017380 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart4f774512009-05-22 14:52:35 -040017381 "2513 POST_SGL_BLOCK mailbox command failed "
17382 "status x%x add_status x%x mbx status x%x\n",
17383 shdr_status, shdr_add_status, rc);
17384 rc = -ENXIO;
17385 }
17386 return rc;
17387}
17388
17389/**
James Smart5e5b5112019-01-28 11:14:22 -080017390 * lpfc_sli4_post_io_sgl_block - post a block of nvme sgl list to firmware
James Smart4f774512009-05-22 14:52:35 -040017391 * @phba: pointer to lpfc hba data structure.
James Smart0794d602019-01-28 11:14:19 -080017392 * @nblist: pointer to nvme buffer list.
James Smart4f774512009-05-22 14:52:35 -040017393 * @count: number of scsi buffers on the list.
17394 *
17395 * This routine is invoked to post a block of @count scsi sgl pages from a
James Smart0794d602019-01-28 11:14:19 -080017396 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
James Smart4f774512009-05-22 14:52:35 -040017397 * No Lock is held.
17398 *
17399 **/
James Smart0794d602019-01-28 11:14:19 -080017400static int
James Smart5e5b5112019-01-28 11:14:22 -080017401lpfc_sli4_post_io_sgl_block(struct lpfc_hba *phba, struct list_head *nblist,
17402 int count)
James Smart4f774512009-05-22 14:52:35 -040017403{
James Smartc4908502019-01-28 11:14:28 -080017404 struct lpfc_io_buf *lpfc_ncmd;
James Smart4f774512009-05-22 14:52:35 -040017405 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
17406 struct sgl_page_pairs *sgl_pg_pairs;
17407 void *viraddr;
17408 LPFC_MBOXQ_t *mbox;
17409 uint32_t reqlen, alloclen, pg_pairs;
17410 uint32_t mbox_tmo;
17411 uint16_t xritag_start = 0;
17412 int rc = 0;
17413 uint32_t shdr_status, shdr_add_status;
17414 dma_addr_t pdma_phys_bpl1;
17415 union lpfc_sli4_cfg_shdr *shdr;
17416
17417 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040017418 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040017419 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040017420 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040017421 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
James Smart0794d602019-01-28 11:14:19 -080017422 "6118 Block sgl registration required DMA "
James Smart4f774512009-05-22 14:52:35 -040017423 "size (%d) great than a page\n", reqlen);
17424 return -ENOMEM;
17425 }
17426 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17427 if (!mbox) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017428 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -080017429 "6119 Failed to allocate mbox cmd memory\n");
James Smart4f774512009-05-22 14:52:35 -040017430 return -ENOMEM;
17431 }
17432
17433 /* Allocate DMA memory and set up the non-embedded mailbox command */
17434 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
James Smart0794d602019-01-28 11:14:19 -080017435 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
17436 reqlen, LPFC_SLI4_MBX_NEMBED);
James Smart4f774512009-05-22 14:52:35 -040017437
17438 if (alloclen < reqlen) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017439 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -080017440 "6120 Allocated DMA memory size (%d) is "
James Smart4f774512009-05-22 14:52:35 -040017441 "less than the requested DMA memory "
17442 "size (%d)\n", alloclen, reqlen);
17443 lpfc_sli4_mbox_cmd_free(phba, mbox);
17444 return -ENOMEM;
17445 }
James Smart6d368e52011-05-24 11:44:12 -040017446
James Smart4f774512009-05-22 14:52:35 -040017447 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040017448 viraddr = mbox->sge_array->addr[0];
17449
17450 /* Set up the SGL pages in the non-embedded DMA pages */
17451 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
17452 sgl_pg_pairs = &sgl->sgl_pg_pairs;
17453
17454 pg_pairs = 0;
James Smart0794d602019-01-28 11:14:19 -080017455 list_for_each_entry(lpfc_ncmd, nblist, list) {
James Smart4f774512009-05-22 14:52:35 -040017456 /* Set up the sge entry */
17457 sgl_pg_pairs->sgl_pg0_addr_lo =
James Smart0794d602019-01-28 11:14:19 -080017458 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040017459 sgl_pg_pairs->sgl_pg0_addr_hi =
James Smart0794d602019-01-28 11:14:19 -080017460 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
James Smart4f774512009-05-22 14:52:35 -040017461 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
James Smart0794d602019-01-28 11:14:19 -080017462 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
17463 SGL_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040017464 else
17465 pdma_phys_bpl1 = 0;
17466 sgl_pg_pairs->sgl_pg1_addr_lo =
17467 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
17468 sgl_pg_pairs->sgl_pg1_addr_hi =
17469 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
17470 /* Keep the first xritag on the list */
17471 if (pg_pairs == 0)
James Smart0794d602019-01-28 11:14:19 -080017472 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -040017473 sgl_pg_pairs++;
17474 pg_pairs++;
17475 }
17476 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
17477 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
17478 /* Perform endian conversion if necessary */
17479 sgl->word0 = cpu_to_le32(sgl->word0);
17480
James Smart0794d602019-01-28 11:14:19 -080017481 if (!phba->sli4_hba.intr_enable) {
James Smart4f774512009-05-22 14:52:35 -040017482 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart0794d602019-01-28 11:14:19 -080017483 } else {
James Smarta183a152011-10-10 21:32:43 -040017484 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040017485 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17486 }
James Smart0794d602019-01-28 11:14:19 -080017487 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040017488 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17489 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17490 if (rc != MBX_TIMEOUT)
17491 lpfc_sli4_mbox_cmd_free(phba, mbox);
17492 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070017493 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart0794d602019-01-28 11:14:19 -080017494 "6125 POST_SGL_BLOCK mailbox command failed "
James Smart4f774512009-05-22 14:52:35 -040017495 "status x%x add_status x%x mbx status x%x\n",
17496 shdr_status, shdr_add_status, rc);
17497 rc = -ENXIO;
17498 }
17499 return rc;
17500}
17501
17502/**
James Smart5e5b5112019-01-28 11:14:22 -080017503 * lpfc_sli4_post_io_sgl_list - Post blocks of nvme buffer sgls from a list
James Smart0794d602019-01-28 11:14:19 -080017504 * @phba: pointer to lpfc hba data structure.
17505 * @post_nblist: pointer to the nvme buffer list.
Lee Jones7af29d42020-07-21 17:41:31 +010017506 * @sb_count: number of nvme buffers.
James Smart0794d602019-01-28 11:14:19 -080017507 *
17508 * This routine walks a list of nvme buffers that was passed in. It attempts
17509 * to construct blocks of nvme buffer sgls which contains contiguous xris and
17510 * uses the non-embedded SGL block post mailbox commands to post to the port.
17511 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
17512 * embedded SGL post mailbox command for posting. The @post_nblist passed in
17513 * must be local list, thus no lock is needed when manipulate the list.
17514 *
17515 * Returns: 0 = failure, non-zero number of successfully posted buffers.
17516 **/
17517int
James Smart5e5b5112019-01-28 11:14:22 -080017518lpfc_sli4_post_io_sgl_list(struct lpfc_hba *phba,
17519 struct list_head *post_nblist, int sb_count)
James Smart0794d602019-01-28 11:14:19 -080017520{
James Smartc4908502019-01-28 11:14:28 -080017521 struct lpfc_io_buf *lpfc_ncmd, *lpfc_ncmd_next;
James Smart0794d602019-01-28 11:14:19 -080017522 int status, sgl_size;
17523 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
17524 dma_addr_t pdma_phys_sgl1;
17525 int last_xritag = NO_XRI;
17526 int cur_xritag;
James Smart0794d602019-01-28 11:14:19 -080017527 LIST_HEAD(prep_nblist);
17528 LIST_HEAD(blck_nblist);
17529 LIST_HEAD(nvme_nblist);
17530
17531 /* sanity check */
17532 if (sb_count <= 0)
17533 return -EINVAL;
17534
17535 sgl_size = phba->cfg_sg_dma_buf_size;
17536 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
17537 list_del_init(&lpfc_ncmd->list);
17538 block_cnt++;
17539 if ((last_xritag != NO_XRI) &&
17540 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
17541 /* a hole in xri block, form a sgl posting block */
17542 list_splice_init(&prep_nblist, &blck_nblist);
17543 post_cnt = block_cnt - 1;
17544 /* prepare list for next posting block */
17545 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
17546 block_cnt = 1;
17547 } else {
17548 /* prepare list for next posting block */
17549 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
17550 /* enough sgls for non-embed sgl mbox command */
17551 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
17552 list_splice_init(&prep_nblist, &blck_nblist);
17553 post_cnt = block_cnt;
17554 block_cnt = 0;
17555 }
17556 }
17557 num_posting++;
17558 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
17559
17560 /* end of repost sgl list condition for NVME buffers */
17561 if (num_posting == sb_count) {
17562 if (post_cnt == 0) {
17563 /* last sgl posting block */
17564 list_splice_init(&prep_nblist, &blck_nblist);
17565 post_cnt = block_cnt;
17566 } else if (block_cnt == 1) {
17567 /* last single sgl with non-contiguous xri */
17568 if (sgl_size > SGL_PAGE_SIZE)
17569 pdma_phys_sgl1 =
17570 lpfc_ncmd->dma_phys_sgl +
17571 SGL_PAGE_SIZE;
17572 else
17573 pdma_phys_sgl1 = 0;
17574 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
17575 status = lpfc_sli4_post_sgl(
17576 phba, lpfc_ncmd->dma_phys_sgl,
17577 pdma_phys_sgl1, cur_xritag);
17578 if (status) {
James Smartc4908502019-01-28 11:14:28 -080017579 /* Post error. Buffer unavailable. */
17580 lpfc_ncmd->flags |=
17581 LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080017582 } else {
James Smartc4908502019-01-28 11:14:28 -080017583 /* Post success. Bffer available. */
17584 lpfc_ncmd->flags &=
17585 ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080017586 lpfc_ncmd->status = IOSTAT_SUCCESS;
17587 num_posted++;
17588 }
17589 /* success, put on NVME buffer sgl list */
17590 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
17591 }
17592 }
17593
17594 /* continue until a nembed page worth of sgls */
17595 if (post_cnt == 0)
17596 continue;
17597
17598 /* post block of NVME buffer list sgls */
James Smart5e5b5112019-01-28 11:14:22 -080017599 status = lpfc_sli4_post_io_sgl_block(phba, &blck_nblist,
17600 post_cnt);
James Smart0794d602019-01-28 11:14:19 -080017601
17602 /* don't reset xirtag due to hole in xri block */
17603 if (block_cnt == 0)
17604 last_xritag = NO_XRI;
17605
17606 /* reset NVME buffer post count for next round of posting */
17607 post_cnt = 0;
17608
17609 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
17610 while (!list_empty(&blck_nblist)) {
17611 list_remove_head(&blck_nblist, lpfc_ncmd,
James Smartc4908502019-01-28 11:14:28 -080017612 struct lpfc_io_buf, list);
James Smart0794d602019-01-28 11:14:19 -080017613 if (status) {
James Smartc4908502019-01-28 11:14:28 -080017614 /* Post error. Mark buffer unavailable. */
17615 lpfc_ncmd->flags |= LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080017616 } else {
James Smartc4908502019-01-28 11:14:28 -080017617 /* Post success, Mark buffer available. */
17618 lpfc_ncmd->flags &= ~LPFC_SBUF_NOT_POSTED;
James Smart0794d602019-01-28 11:14:19 -080017619 lpfc_ncmd->status = IOSTAT_SUCCESS;
17620 num_posted++;
17621 }
17622 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
17623 }
17624 }
17625 /* Push NVME buffers with sgl posted to the available list */
James Smart5e5b5112019-01-28 11:14:22 -080017626 lpfc_io_buf_replenish(phba, &nvme_nblist);
17627
James Smart0794d602019-01-28 11:14:19 -080017628 return num_posted;
17629}
17630
17631/**
James Smart4f774512009-05-22 14:52:35 -040017632 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
17633 * @phba: pointer to lpfc_hba struct that the frame was received on
17634 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17635 *
17636 * This function checks the fields in the @fc_hdr to see if the FC frame is a
17637 * valid type of frame that the LPFC driver will handle. This function will
17638 * return a zero if the frame is a valid frame or a non zero value when the
17639 * frame does not pass the check.
17640 **/
17641static int
17642lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
17643{
Tomas Henzl474ffb72010-12-22 16:52:40 +010017644 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040017645 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050017646 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040017647
James Smarte62245d2019-08-14 16:57:08 -070017648#define FC_RCTL_MDS_DIAGS 0xF4
17649
James Smart4f774512009-05-22 14:52:35 -040017650 switch (fc_hdr->fh_r_ctl) {
17651 case FC_RCTL_DD_UNCAT: /* uncategorized information */
17652 case FC_RCTL_DD_SOL_DATA: /* solicited data */
17653 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
17654 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
17655 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
17656 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
17657 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
17658 case FC_RCTL_DD_CMD_STATUS: /* command status */
17659 case FC_RCTL_ELS_REQ: /* extended link services request */
17660 case FC_RCTL_ELS_REP: /* extended link services reply */
17661 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
17662 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
17663 case FC_RCTL_BA_NOP: /* basic link service NOP */
17664 case FC_RCTL_BA_ABTS: /* basic link service abort */
17665 case FC_RCTL_BA_RMC: /* remove connection */
17666 case FC_RCTL_BA_ACC: /* basic accept */
17667 case FC_RCTL_BA_RJT: /* basic reject */
17668 case FC_RCTL_BA_PRMT:
17669 case FC_RCTL_ACK_1: /* acknowledge_1 */
17670 case FC_RCTL_ACK_0: /* acknowledge_0 */
17671 case FC_RCTL_P_RJT: /* port reject */
17672 case FC_RCTL_F_RJT: /* fabric reject */
17673 case FC_RCTL_P_BSY: /* port busy */
17674 case FC_RCTL_F_BSY: /* fabric busy to data frame */
17675 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
17676 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070017677 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040017678 case FC_RCTL_END: /* end */
17679 break;
17680 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
17681 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
17682 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
17683 return lpfc_fc_frame_check(phba, fc_hdr);
17684 default:
17685 goto drop;
17686 }
James Smartae9e28f2017-05-15 15:20:51 -070017687
James Smart4f774512009-05-22 14:52:35 -040017688 switch (fc_hdr->fh_type) {
17689 case FC_TYPE_BLS:
17690 case FC_TYPE_ELS:
17691 case FC_TYPE_FCP:
17692 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080017693 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040017694 break;
17695 case FC_TYPE_IP:
17696 case FC_TYPE_ILS:
17697 default:
17698 goto drop;
17699 }
James Smart546fc852011-03-11 16:06:29 -050017700
James Smart4f774512009-05-22 14:52:35 -040017701 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070017702 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040017703 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070017704 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
17705 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
17706 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
17707 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
17708 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040017709 return 0;
17710drop:
17711 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070017712 "2539 Dropped frame rctl:x%x type:x%x\n",
17713 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040017714 return 1;
17715}
17716
17717/**
17718 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
17719 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17720 *
17721 * This function processes the FC header to retrieve the VFI from the VF
17722 * header, if one exists. This function will return the VFI if one exists
17723 * or 0 if no VSAN Header exists.
17724 **/
17725static uint32_t
17726lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
17727{
17728 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
17729
17730 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
17731 return 0;
17732 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
17733}
17734
17735/**
17736 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
17737 * @phba: Pointer to the HBA structure to search for the vport on
17738 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
17739 * @fcfi: The FC Fabric ID that the frame came from
Lee Jones7af29d42020-07-21 17:41:31 +010017740 * @did: Destination ID to match against
James Smart4f774512009-05-22 14:52:35 -040017741 *
17742 * This function searches the @phba for a vport that matches the content of the
17743 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
17744 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
17745 * returns the matching vport pointer or NULL if unable to match frame to a
17746 * vport.
17747 **/
17748static struct lpfc_vport *
17749lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080017750 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040017751{
17752 struct lpfc_vport **vports;
17753 struct lpfc_vport *vport = NULL;
17754 int i;
James Smart939723a2012-05-09 21:19:03 -040017755
James Smartbf086112011-08-21 21:48:13 -040017756 if (did == Fabric_DID)
17757 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040017758 if ((phba->pport->fc_flag & FC_PT2PT) &&
17759 !(phba->link_state == LPFC_HBA_READY))
17760 return phba->pport;
17761
James Smart4f774512009-05-22 14:52:35 -040017762 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080017763 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040017764 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
17765 if (phba->fcf.fcfi == fcfi &&
17766 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
17767 vports[i]->fc_myDID == did) {
17768 vport = vports[i];
17769 break;
17770 }
17771 }
James Smart895427b2017-02-12 13:52:30 -080017772 }
James Smart4f774512009-05-22 14:52:35 -040017773 lpfc_destroy_vport_work_array(phba, vports);
17774 return vport;
17775}
17776
17777/**
James Smart45ed1192009-10-02 15:17:02 -040017778 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
17779 * @vport: The vport to work on.
17780 *
17781 * This function updates the receive sequence time stamp for this vport. The
17782 * receive sequence time stamp indicates the time that the last frame of the
17783 * the sequence that has been idle for the longest amount of time was received.
17784 * the driver uses this time stamp to indicate if any received sequences have
17785 * timed out.
17786 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017787static void
James Smart45ed1192009-10-02 15:17:02 -040017788lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
17789{
17790 struct lpfc_dmabuf *h_buf;
17791 struct hbq_dmabuf *dmabuf = NULL;
17792
17793 /* get the oldest sequence on the rcv list */
17794 h_buf = list_get_first(&vport->rcv_buffer_list,
17795 struct lpfc_dmabuf, list);
17796 if (!h_buf)
17797 return;
17798 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17799 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
17800}
17801
17802/**
17803 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
17804 * @vport: The vport that the received sequences were sent to.
17805 *
17806 * This function cleans up all outstanding received sequences. This is called
17807 * by the driver when a link event or user action invalidates all the received
17808 * sequences.
17809 **/
17810void
17811lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
17812{
17813 struct lpfc_dmabuf *h_buf, *hnext;
17814 struct lpfc_dmabuf *d_buf, *dnext;
17815 struct hbq_dmabuf *dmabuf = NULL;
17816
17817 /* start with the oldest sequence on the rcv list */
17818 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17819 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17820 list_del_init(&dmabuf->hbuf.list);
17821 list_for_each_entry_safe(d_buf, dnext,
17822 &dmabuf->dbuf.list, list) {
17823 list_del_init(&d_buf->list);
17824 lpfc_in_buf_free(vport->phba, d_buf);
17825 }
17826 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17827 }
17828}
17829
17830/**
17831 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
17832 * @vport: The vport that the received sequences were sent to.
17833 *
17834 * This function determines whether any received sequences have timed out by
17835 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
17836 * indicates that there is at least one timed out sequence this routine will
17837 * go through the received sequences one at a time from most inactive to most
17838 * active to determine which ones need to be cleaned up. Once it has determined
17839 * that a sequence needs to be cleaned up it will simply free up the resources
17840 * without sending an abort.
17841 **/
17842void
17843lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
17844{
17845 struct lpfc_dmabuf *h_buf, *hnext;
17846 struct lpfc_dmabuf *d_buf, *dnext;
17847 struct hbq_dmabuf *dmabuf = NULL;
17848 unsigned long timeout;
17849 int abort_count = 0;
17850
17851 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17852 vport->rcv_buffer_time_stamp);
17853 if (list_empty(&vport->rcv_buffer_list) ||
17854 time_before(jiffies, timeout))
17855 return;
17856 /* start with the oldest sequence on the rcv list */
17857 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
17858 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17859 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
17860 dmabuf->time_stamp);
17861 if (time_before(jiffies, timeout))
17862 break;
17863 abort_count++;
17864 list_del_init(&dmabuf->hbuf.list);
17865 list_for_each_entry_safe(d_buf, dnext,
17866 &dmabuf->dbuf.list, list) {
17867 list_del_init(&d_buf->list);
17868 lpfc_in_buf_free(vport->phba, d_buf);
17869 }
17870 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
17871 }
17872 if (abort_count)
17873 lpfc_update_rcv_time_stamp(vport);
17874}
17875
17876/**
James Smart4f774512009-05-22 14:52:35 -040017877 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
Lee Jones7af29d42020-07-21 17:41:31 +010017878 * @vport: pointer to a vitural port
James Smart4f774512009-05-22 14:52:35 -040017879 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
17880 *
17881 * This function searches through the existing incomplete sequences that have
17882 * been sent to this @vport. If the frame matches one of the incomplete
17883 * sequences then the dbuf in the @dmabuf is added to the list of frames that
17884 * make up that sequence. If no sequence is found that matches this frame then
17885 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
17886 * This function returns a pointer to the first dmabuf in the sequence list that
17887 * the frame was linked to.
17888 **/
17889static struct hbq_dmabuf *
17890lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
17891{
17892 struct fc_frame_header *new_hdr;
17893 struct fc_frame_header *temp_hdr;
17894 struct lpfc_dmabuf *d_buf;
17895 struct lpfc_dmabuf *h_buf;
17896 struct hbq_dmabuf *seq_dmabuf = NULL;
17897 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050017898 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040017899
James Smart4d9ab992009-10-02 15:16:39 -040017900 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017901 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040017902 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050017903
James Smart4f774512009-05-22 14:52:35 -040017904 /* Use the hdr_buf to find the sequence that this frame belongs to */
17905 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17906 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17907 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
17908 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
17909 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
17910 continue;
17911 /* found a pending sequence that matches this frame */
17912 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
17913 break;
17914 }
17915 if (!seq_dmabuf) {
17916 /*
17917 * This indicates first frame received for this sequence.
17918 * Queue the buffer on the vport's rcv_buffer_list.
17919 */
17920 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040017921 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017922 return dmabuf;
17923 }
17924 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050017925 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
17926 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040017927 list_del_init(&seq_dmabuf->hbuf.list);
17928 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
17929 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017930 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017931 return dmabuf;
17932 }
James Smart45ed1192009-10-02 15:17:02 -040017933 /* move this sequence to the tail to indicate a young sequence */
17934 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
17935 seq_dmabuf->time_stamp = jiffies;
17936 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050017937 if (list_empty(&seq_dmabuf->dbuf.list)) {
17938 temp_hdr = dmabuf->hbuf.virt;
17939 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
17940 return seq_dmabuf;
17941 }
James Smart4f774512009-05-22 14:52:35 -040017942 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050017943 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
17944 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040017945 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17946 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
17947 /*
17948 * If the frame's sequence count is greater than the frame on
17949 * the list then insert the frame right after this frame
17950 */
James Smarteeead812009-12-21 17:01:23 -050017951 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
17952 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040017953 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050017954 found = 1;
17955 break;
James Smart4f774512009-05-22 14:52:35 -040017956 }
James Smart4360ca92015-12-16 18:12:04 -050017957
17958 if (&d_buf->list == &seq_dmabuf->dbuf.list)
17959 break;
17960 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040017961 }
James Smart4360ca92015-12-16 18:12:04 -050017962
17963 if (found)
17964 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017965 return NULL;
17966}
17967
17968/**
James Smart6669f9b2009-10-02 15:16:45 -040017969 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
17970 * @vport: pointer to a vitural port
17971 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17972 *
17973 * This function tries to abort from the partially assembed sequence, described
17974 * by the information from basic abbort @dmabuf. It checks to see whether such
17975 * partially assembled sequence held by the driver. If so, it shall free up all
17976 * the frames from the partially assembled sequence.
17977 *
17978 * Return
17979 * true -- if there is matching partially assembled sequence present and all
17980 * the frames freed with the sequence;
17981 * false -- if there is no matching partially assembled sequence present so
17982 * nothing got aborted in the lower layer driver
17983 **/
17984static bool
17985lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
17986 struct hbq_dmabuf *dmabuf)
17987{
17988 struct fc_frame_header *new_hdr;
17989 struct fc_frame_header *temp_hdr;
17990 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
17991 struct hbq_dmabuf *seq_dmabuf = NULL;
17992
17993 /* Use the hdr_buf to find the sequence that matches this frame */
17994 INIT_LIST_HEAD(&dmabuf->dbuf.list);
17995 INIT_LIST_HEAD(&dmabuf->hbuf.list);
17996 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17997 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
17998 temp_hdr = (struct fc_frame_header *)h_buf->virt;
17999 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
18000 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
18001 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
18002 continue;
18003 /* found a pending sequence that matches this frame */
18004 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
18005 break;
18006 }
18007
18008 /* Free up all the frames from the partially assembled sequence */
18009 if (seq_dmabuf) {
18010 list_for_each_entry_safe(d_buf, n_buf,
18011 &seq_dmabuf->dbuf.list, list) {
18012 list_del_init(&d_buf->list);
18013 lpfc_in_buf_free(vport->phba, d_buf);
18014 }
18015 return true;
18016 }
18017 return false;
18018}
18019
18020/**
James Smart6dd9e312013-01-03 15:43:37 -050018021 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
18022 * @vport: pointer to a vitural port
18023 * @dmabuf: pointer to a dmabuf that describes the FC sequence
18024 *
18025 * This function tries to abort from the assembed sequence from upper level
18026 * protocol, described by the information from basic abbort @dmabuf. It
18027 * checks to see whether such pending context exists at upper level protocol.
18028 * If so, it shall clean up the pending context.
18029 *
18030 * Return
18031 * true -- if there is matching pending context of the sequence cleaned
18032 * at ulp;
18033 * false -- if there is no matching pending context of the sequence present
18034 * at ulp.
18035 **/
18036static bool
18037lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
18038{
18039 struct lpfc_hba *phba = vport->phba;
18040 int handled;
18041
18042 /* Accepting abort at ulp with SLI4 only */
18043 if (phba->sli_rev < LPFC_SLI_REV4)
18044 return false;
18045
18046 /* Register all caring upper level protocols to attend abort */
18047 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
18048 if (handled)
18049 return true;
18050
18051 return false;
18052}
18053
18054/**
James Smart546fc852011-03-11 16:06:29 -050018055 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040018056 * @phba: Pointer to HBA context object.
18057 * @cmd_iocbq: pointer to the command iocbq structure.
18058 * @rsp_iocbq: pointer to the response iocbq structure.
18059 *
James Smart546fc852011-03-11 16:06:29 -050018060 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040018061 * event. It properly releases the memory allocated to the sequence abort
18062 * accept iocb.
18063 **/
18064static void
James Smart546fc852011-03-11 16:06:29 -050018065lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040018066 struct lpfc_iocbq *cmd_iocbq,
18067 struct lpfc_iocbq *rsp_iocbq)
18068{
James Smart6dd9e312013-01-03 15:43:37 -050018069 struct lpfc_nodelist *ndlp;
18070
18071 if (cmd_iocbq) {
18072 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
18073 lpfc_nlp_put(ndlp);
18074 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040018075 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050018076 }
James Smart6b5151f2012-01-18 16:24:06 -050018077
18078 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
18079 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
Dick Kennedy372c1872020-06-30 14:50:00 -070018080 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6b5151f2012-01-18 16:24:06 -050018081 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
18082 rsp_iocbq->iocb.ulpStatus,
18083 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040018084}
18085
18086/**
James Smart6d368e52011-05-24 11:44:12 -040018087 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
18088 * @phba: Pointer to HBA context object.
18089 * @xri: xri id in transaction.
18090 *
18091 * This function validates the xri maps to the known range of XRIs allocated an
18092 * used by the driver.
18093 **/
James Smart7851fe22011-07-22 18:36:52 -040018094uint16_t
James Smart6d368e52011-05-24 11:44:12 -040018095lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
18096 uint16_t xri)
18097{
James Smarta2fc4aef2014-09-03 12:57:55 -040018098 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040018099
18100 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
18101 if (xri == phba->sli4_hba.xri_ids[i])
18102 return i;
18103 }
18104 return NO_XRI;
18105}
18106
James Smart6d368e52011-05-24 11:44:12 -040018107/**
James Smart546fc852011-03-11 16:06:29 -050018108 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
Lee Jones7af29d42020-07-21 17:41:31 +010018109 * @vport: pointer to a vitural port.
James Smart6669f9b2009-10-02 15:16:45 -040018110 * @fc_hdr: pointer to a FC frame header.
Lee Jones7af29d42020-07-21 17:41:31 +010018111 * @aborted: was the partially assembled receive sequence successfully aborted
James Smart6669f9b2009-10-02 15:16:45 -040018112 *
James Smart546fc852011-03-11 16:06:29 -050018113 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040018114 * event after aborting the sequence handling.
18115 **/
James Smart86c67372017-04-21 16:05:04 -070018116void
James Smart6dd9e312013-01-03 15:43:37 -050018117lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
18118 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040018119{
James Smart6dd9e312013-01-03 15:43:37 -050018120 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040018121 struct lpfc_iocbq *ctiocb = NULL;
18122 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040018123 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050018124 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040018125 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050018126 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040018127
18128 if (!lpfc_is_link_up(phba))
18129 return;
18130
18131 sid = sli4_sid_from_fc_hdr(fc_hdr);
18132 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050018133 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040018134
James Smart6dd9e312013-01-03 15:43:37 -050018135 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040018136 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070018137 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050018138 if (!ndlp) {
18139 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
18140 "1268 Failed to allocate ndlp for "
18141 "oxid:x%x SID:x%x\n", oxid, sid);
18142 return;
18143 }
James Smart6dd9e312013-01-03 15:43:37 -050018144 /* Put ndlp onto pport node list */
18145 lpfc_enqueue_node(vport, ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040018146 }
18147
James Smart546fc852011-03-11 16:06:29 -050018148 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040018149 ctiocb = lpfc_sli_get_iocbq(phba);
18150 if (!ctiocb)
18151 return;
18152
James Smart5ffc2662009-11-18 15:39:44 -050018153 /* Extract the F_CTL field from FC_HDR */
18154 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
18155
James Smart6669f9b2009-10-02 15:16:45 -040018156 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040018157 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050018158 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040018159 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
18160 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
18161 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
18162
18163 /* Fill in the rest of iocb fields */
18164 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
18165 icmd->ulpBdeCount = 0;
18166 icmd->ulpLe = 1;
18167 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040018168 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050018169 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart4430f7f2020-11-15 11:26:31 -080018170 if (!ctiocb->context1) {
18171 lpfc_sli_release_iocbq(phba, ctiocb);
18172 return;
18173 }
James Smart6669f9b2009-10-02 15:16:45 -040018174
James Smart6669f9b2009-10-02 15:16:45 -040018175 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050018176 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040018177 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050018178 ctiocb->sli4_xritag = NO_XRI;
18179
James Smartee0f4fe2012-05-09 21:19:14 -040018180 if (fctl & FC_FC_EX_CTX)
18181 /* Exchange responder sent the abort so we
18182 * own the oxid.
18183 */
18184 xri = oxid;
18185 else
18186 xri = rxid;
18187 lxri = lpfc_sli4_xri_inrange(phba, xri);
18188 if (lxri != NO_XRI)
18189 lpfc_set_rrq_active(phba, ndlp, lxri,
18190 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050018191 /* For BA_ABTS from exchange responder, if the logical xri with
18192 * the oxid maps to the FCP XRI range, the port no longer has
18193 * that exchange context, send a BLS_RJT. Override the IOCB for
18194 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050018195 */
James Smart6dd9e312013-01-03 15:43:37 -050018196 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080018197 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050018198 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
18199 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
18200 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
18201 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
18202 }
18203
18204 /* If BA_ABTS failed to abort a partially assembled receive sequence,
18205 * the driver no longer has that exchange, send a BLS_RJT. Override
18206 * the IOCB for a BA_RJT.
18207 */
18208 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050018209 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
18210 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
18211 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
18212 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
18213 }
James Smart6669f9b2009-10-02 15:16:45 -040018214
James Smart5ffc2662009-11-18 15:39:44 -050018215 if (fctl & FC_FC_EX_CTX) {
18216 /* ABTS sent by responder to CT exchange, construction
18217 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
18218 * field and RX_ID from ABTS for RX_ID field.
18219 */
James Smart546fc852011-03-11 16:06:29 -050018220 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050018221 } else {
18222 /* ABTS sent by initiator to CT exchange, construction
18223 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050018224 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050018225 */
James Smart546fc852011-03-11 16:06:29 -050018226 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050018227 }
James Smartf09c3ac2012-03-01 22:33:29 -050018228 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050018229 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050018230
James Smart546fc852011-03-11 16:06:29 -050018231 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050018232 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
18233 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
18234 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050018235
18236 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
18237 if (rc == IOCB_ERROR) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018238 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart6dd9e312013-01-03 15:43:37 -050018239 "2925 Failed to issue CT ABTS RSP x%x on "
18240 "xri x%x, Data x%x\n",
18241 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
18242 phba->link_state);
18243 lpfc_nlp_put(ndlp);
18244 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050018245 lpfc_sli_release_iocbq(phba, ctiocb);
18246 }
James Smart6669f9b2009-10-02 15:16:45 -040018247}
18248
18249/**
18250 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
18251 * @vport: Pointer to the vport on which this sequence was received
18252 * @dmabuf: pointer to a dmabuf that describes the FC sequence
18253 *
18254 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
18255 * receive sequence is only partially assembed by the driver, it shall abort
18256 * the partially assembled frames for the sequence. Otherwise, if the
18257 * unsolicited receive sequence has been completely assembled and passed to
Ferruh Yigit02e3e582020-07-28 15:56:06 +010018258 * the Upper Layer Protocol (ULP), it then mark the per oxid status for the
James Smart6669f9b2009-10-02 15:16:45 -040018259 * unsolicited sequence has been aborted. After that, it will issue a basic
18260 * accept to accept the abort.
18261 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018262static void
James Smart6669f9b2009-10-02 15:16:45 -040018263lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
18264 struct hbq_dmabuf *dmabuf)
18265{
18266 struct lpfc_hba *phba = vport->phba;
18267 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050018268 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050018269 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040018270
James Smart6669f9b2009-10-02 15:16:45 -040018271 /* Make a copy of fc_hdr before the dmabuf being released */
18272 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050018273 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040018274
James Smart5ffc2662009-11-18 15:39:44 -050018275 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050018276 /* ABTS by responder to exchange, no cleanup needed */
18277 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050018278 } else {
James Smart6dd9e312013-01-03 15:43:37 -050018279 /* ABTS by initiator to exchange, need to do cleanup */
18280 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
18281 if (aborted == false)
18282 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050018283 }
James Smart6dd9e312013-01-03 15:43:37 -050018284 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18285
James Smart86c67372017-04-21 16:05:04 -070018286 if (phba->nvmet_support) {
18287 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
18288 return;
18289 }
18290
James Smart6dd9e312013-01-03 15:43:37 -050018291 /* Respond with BA_ACC or BA_RJT accordingly */
18292 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040018293}
18294
18295/**
James Smart4f774512009-05-22 14:52:35 -040018296 * lpfc_seq_complete - Indicates if a sequence is complete
18297 * @dmabuf: pointer to a dmabuf that describes the FC sequence
18298 *
18299 * This function checks the sequence, starting with the frame described by
18300 * @dmabuf, to see if all the frames associated with this sequence are present.
18301 * the frames associated with this sequence are linked to the @dmabuf using the
18302 * dbuf list. This function looks for two major things. 1) That the first frame
18303 * has a sequence count of zero. 2) There is a frame with last frame of sequence
18304 * set. 3) That there are no holes in the sequence count. The function will
18305 * return 1 when the sequence is complete, otherwise it will return 0.
18306 **/
18307static int
18308lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
18309{
18310 struct fc_frame_header *hdr;
18311 struct lpfc_dmabuf *d_buf;
18312 struct hbq_dmabuf *seq_dmabuf;
18313 uint32_t fctl;
18314 int seq_count = 0;
18315
18316 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18317 /* make sure first fame of sequence has a sequence count of zero */
18318 if (hdr->fh_seq_cnt != seq_count)
18319 return 0;
18320 fctl = (hdr->fh_f_ctl[0] << 16 |
18321 hdr->fh_f_ctl[1] << 8 |
18322 hdr->fh_f_ctl[2]);
18323 /* If last frame of sequence we can return success. */
18324 if (fctl & FC_FC_END_SEQ)
18325 return 1;
18326 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
18327 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18328 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
18329 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050018330 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040018331 return 0;
18332 fctl = (hdr->fh_f_ctl[0] << 16 |
18333 hdr->fh_f_ctl[1] << 8 |
18334 hdr->fh_f_ctl[2]);
18335 /* If last frame of sequence we can return success. */
18336 if (fctl & FC_FC_END_SEQ)
18337 return 1;
18338 }
18339 return 0;
18340}
18341
18342/**
18343 * lpfc_prep_seq - Prep sequence for ULP processing
18344 * @vport: Pointer to the vport on which this sequence was received
Lee Jones7af29d42020-07-21 17:41:31 +010018345 * @seq_dmabuf: pointer to a dmabuf that describes the FC sequence
James Smart4f774512009-05-22 14:52:35 -040018346 *
18347 * This function takes a sequence, described by a list of frames, and creates
18348 * a list of iocbq structures to describe the sequence. This iocbq list will be
18349 * used to issue to the generic unsolicited sequence handler. This routine
18350 * returns a pointer to the first iocbq in the list. If the function is unable
18351 * to allocate an iocbq then it throw out the received frames that were not
18352 * able to be described and return a pointer to the first iocbq. If unable to
18353 * allocate any iocbqs (including the first) this function will return NULL.
18354 **/
18355static struct lpfc_iocbq *
18356lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
18357{
James Smart7851fe22011-07-22 18:36:52 -040018358 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040018359 struct lpfc_dmabuf *d_buf, *n_buf;
18360 struct lpfc_iocbq *first_iocbq, *iocbq;
18361 struct fc_frame_header *fc_hdr;
18362 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040018363 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050018364 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040018365
18366 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
18367 /* remove from receive buffer list */
18368 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040018369 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040018370 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040018371 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040018372 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040018373 /* Get an iocbq struct to fill in. */
18374 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
18375 if (first_iocbq) {
18376 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040018377 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040018378 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080018379 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040018380
18381 /* Check FC Header to see what TYPE of frame we are rcv'ing */
18382 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
18383 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
18384 first_iocbq->iocb.un.rcvels.parmRo =
18385 sli4_did_from_fc_hdr(fc_hdr);
18386 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
18387 } else
18388 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040018389 first_iocbq->iocb.ulpContext = NO_XRI;
18390 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
18391 be16_to_cpu(fc_hdr->fh_ox_id);
18392 /* iocbq is prepped for internal consumption. Physical vpi. */
18393 first_iocbq->iocb.unsli3.rcvsli3.vpi =
18394 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040018395 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040018396 tot_len = bf_get(lpfc_rcqe_length,
18397 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
18398
James Smart4f774512009-05-22 14:52:35 -040018399 first_iocbq->context2 = &seq_dmabuf->dbuf;
18400 first_iocbq->context3 = NULL;
18401 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040018402 if (tot_len > LPFC_DATA_BUF_SIZE)
18403 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040018404 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040018405 else
18406 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
18407
James Smart4f774512009-05-22 14:52:35 -040018408 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040018409
James Smart7851fe22011-07-22 18:36:52 -040018410 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040018411 }
18412 iocbq = first_iocbq;
18413 /*
18414 * Each IOCBq can have two Buffers assigned, so go through the list
18415 * of buffers for this sequence and save two buffers in each IOCBq
18416 */
18417 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
18418 if (!iocbq) {
18419 lpfc_in_buf_free(vport->phba, d_buf);
18420 continue;
18421 }
18422 if (!iocbq->context3) {
18423 iocbq->context3 = d_buf;
18424 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040018425 /* We need to get the size out of the right CQE */
18426 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18427 len = bf_get(lpfc_rcqe_length,
18428 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040018429 pbde = (struct ulp_bde64 *)
18430 &iocbq->iocb.unsli3.sli3Words[4];
18431 if (len > LPFC_DATA_BUF_SIZE)
18432 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
18433 else
18434 pbde->tus.f.bdeSize = len;
18435
James Smart7851fe22011-07-22 18:36:52 -040018436 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
18437 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040018438 } else {
18439 iocbq = lpfc_sli_get_iocbq(vport->phba);
18440 if (!iocbq) {
18441 if (first_iocbq) {
18442 first_iocbq->iocb.ulpStatus =
18443 IOSTAT_FCP_RSP_ERROR;
18444 first_iocbq->iocb.un.ulpWord[4] =
18445 IOERR_NO_RESOURCES;
18446 }
18447 lpfc_in_buf_free(vport->phba, d_buf);
18448 continue;
18449 }
James Smart7851fe22011-07-22 18:36:52 -040018450 /* We need to get the size out of the right CQE */
18451 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
18452 len = bf_get(lpfc_rcqe_length,
18453 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040018454 iocbq->context2 = d_buf;
18455 iocbq->context3 = NULL;
18456 iocbq->iocb.ulpBdeCount = 1;
18457 if (len > LPFC_DATA_BUF_SIZE)
18458 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
18459 LPFC_DATA_BUF_SIZE;
18460 else
18461 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
18462
James Smart7851fe22011-07-22 18:36:52 -040018463 tot_len += len;
18464 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
18465
James Smart4f774512009-05-22 14:52:35 -040018466 iocbq->iocb.un.rcvels.remoteID = sid;
18467 list_add_tail(&iocbq->list, &first_iocbq->list);
18468 }
18469 }
James Smart39c4f1a2020-01-27 16:23:01 -080018470 /* Free the sequence's header buffer */
18471 if (!first_iocbq)
18472 lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
18473
James Smart4f774512009-05-22 14:52:35 -040018474 return first_iocbq;
18475}
18476
James Smart6669f9b2009-10-02 15:16:45 -040018477static void
18478lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
18479 struct hbq_dmabuf *seq_dmabuf)
18480{
18481 struct fc_frame_header *fc_hdr;
18482 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
18483 struct lpfc_hba *phba = vport->phba;
18484
18485 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
18486 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
18487 if (!iocbq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018488 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6669f9b2009-10-02 15:16:45 -040018489 "2707 Ring %d handler: Failed to allocate "
18490 "iocb Rctl x%x Type x%x received\n",
18491 LPFC_ELS_RING,
18492 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18493 return;
18494 }
18495 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080018496 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040018497 iocbq, fc_hdr->fh_r_ctl,
18498 fc_hdr->fh_type))
Dick Kennedy372c1872020-06-30 14:50:00 -070018499 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6669f9b2009-10-02 15:16:45 -040018500 "2540 Ring %d handler: unexpected Rctl "
18501 "x%x Type x%x received\n",
18502 LPFC_ELS_RING,
18503 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
18504
18505 /* Free iocb created in lpfc_prep_seq */
18506 list_for_each_entry_safe(curr_iocb, next_iocb,
18507 &iocbq->list, list) {
18508 list_del_init(&curr_iocb->list);
18509 lpfc_sli_release_iocbq(phba, curr_iocb);
18510 }
18511 lpfc_sli_release_iocbq(phba, iocbq);
18512}
18513
James Smartae9e28f2017-05-15 15:20:51 -070018514static void
18515lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
18516 struct lpfc_iocbq *rspiocb)
18517{
18518 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
18519
18520 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020018521 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070018522 kfree(pcmd);
18523 lpfc_sli_release_iocbq(phba, cmdiocb);
James Smarte817e5d2018-12-13 15:17:53 -080018524 lpfc_drain_txq(phba);
James Smartae9e28f2017-05-15 15:20:51 -070018525}
18526
18527static void
18528lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
18529 struct hbq_dmabuf *dmabuf)
18530{
18531 struct fc_frame_header *fc_hdr;
18532 struct lpfc_hba *phba = vport->phba;
18533 struct lpfc_iocbq *iocbq = NULL;
18534 union lpfc_wqe *wqe;
18535 struct lpfc_dmabuf *pcmd = NULL;
18536 uint32_t frame_len;
18537 int rc;
James Smarte817e5d2018-12-13 15:17:53 -080018538 unsigned long iflags;
James Smartae9e28f2017-05-15 15:20:51 -070018539
18540 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
18541 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
18542
18543 /* Send the received frame back */
18544 iocbq = lpfc_sli_get_iocbq(phba);
James Smarte817e5d2018-12-13 15:17:53 -080018545 if (!iocbq) {
18546 /* Queue cq event and wakeup worker thread to process it */
18547 spin_lock_irqsave(&phba->hbalock, iflags);
18548 list_add_tail(&dmabuf->cq_event.list,
18549 &phba->sli4_hba.sp_queue_event);
18550 phba->hba_flag |= HBA_SP_QUEUE_EVT;
18551 spin_unlock_irqrestore(&phba->hbalock, iflags);
18552 lpfc_worker_wake_up(phba);
18553 return;
18554 }
James Smartae9e28f2017-05-15 15:20:51 -070018555
18556 /* Allocate buffer for command payload */
18557 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
18558 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020018559 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070018560 &pcmd->phys);
18561 if (!pcmd || !pcmd->virt)
18562 goto exit;
18563
18564 INIT_LIST_HEAD(&pcmd->list);
18565
18566 /* copyin the payload */
18567 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
18568
18569 /* fill in BDE's for command */
18570 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
18571 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
18572 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
18573 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
18574
18575 iocbq->context2 = pcmd;
18576 iocbq->vport = vport;
18577 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
18578 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
18579
18580 /*
18581 * Setup rest of the iocb as though it were a WQE
18582 * Build the SEND_FRAME WQE
18583 */
18584 wqe = (union lpfc_wqe *)&iocbq->iocb;
18585
18586 wqe->send_frame.frame_len = frame_len;
18587 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
18588 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
18589 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
18590 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
18591 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
18592 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
18593
18594 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
18595 iocbq->iocb.ulpLe = 1;
18596 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
18597 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
18598 if (rc == IOCB_ERROR)
18599 goto exit;
18600
18601 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18602 return;
18603
18604exit:
18605 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
18606 "2023 Unable to process MDS loopback frame\n");
18607 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020018608 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070018609 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070018610 if (iocbq)
18611 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070018612 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18613}
18614
James Smart4f774512009-05-22 14:52:35 -040018615/**
18616 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
18617 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010018618 * @dmabuf: Pointer to a dmabuf that describes the FC sequence.
James Smart4f774512009-05-22 14:52:35 -040018619 *
18620 * This function is called with no lock held. This function processes all
18621 * the received buffers and gives it to upper layers when a received buffer
18622 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080018623 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040018624 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
18625 * appropriate receive function when the final frame in a sequence is received.
18626 **/
James Smart4d9ab992009-10-02 15:16:39 -040018627void
18628lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
18629 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040018630{
James Smart4d9ab992009-10-02 15:16:39 -040018631 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040018632 struct fc_frame_header *fc_hdr;
18633 struct lpfc_vport *vport;
18634 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040018635 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040018636
James Smart4f774512009-05-22 14:52:35 -040018637 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040018638 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080018639
James Smarte817e5d2018-12-13 15:17:53 -080018640 if (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS ||
18641 fc_hdr->fh_r_ctl == FC_RCTL_DD_UNSOL_DATA) {
18642 vport = phba->pport;
18643 /* Handle MDS Loopback frames */
Dick Kennedy24411fc2020-08-03 14:02:25 -070018644 if (!(phba->pport->load_flag & FC_UNLOADING))
18645 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
18646 else
18647 lpfc_in_buf_free(phba, &dmabuf->dbuf);
James Smarte817e5d2018-12-13 15:17:53 -080018648 return;
18649 }
18650
James Smart4d9ab992009-10-02 15:16:39 -040018651 /* check to see if this a valid type of frame */
18652 if (lpfc_fc_frame_check(phba, fc_hdr)) {
18653 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18654 return;
18655 }
James Smart2ea259e2017-02-12 13:52:27 -080018656
James Smart7851fe22011-07-22 18:36:52 -040018657 if ((bf_get(lpfc_cqe_code,
18658 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
18659 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
18660 &dmabuf->cq_event.cqe.rcqe_cmpl);
18661 else
18662 fcfi = bf_get(lpfc_rcqe_fcf_id,
18663 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040018664
James Smarte62245d2019-08-14 16:57:08 -070018665 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
18666 vport = phba->pport;
18667 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
18668 "2023 MDS Loopback %d bytes\n",
18669 bf_get(lpfc_rcqe_length,
18670 &dmabuf->cq_event.cqe.rcqe_cmpl));
18671 /* Handle MDS Loopback frames */
18672 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
18673 return;
18674 }
18675
James Smart895427b2017-02-12 13:52:30 -080018676 /* d_id this frame is directed to */
18677 did = sli4_did_from_fc_hdr(fc_hdr);
18678
18679 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040018680 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040018681 /* throw out the frame */
18682 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18683 return;
18684 }
James Smart939723a2012-05-09 21:19:03 -040018685
James Smart939723a2012-05-09 21:19:03 -040018686 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
18687 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
18688 (did != Fabric_DID)) {
18689 /*
18690 * Throw out the frame if we are not pt2pt.
18691 * The pt2pt protocol allows for discovery frames
18692 * to be received without a registered VPI.
18693 */
18694 if (!(vport->fc_flag & FC_PT2PT) ||
18695 (phba->link_state == LPFC_HBA_READY)) {
18696 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18697 return;
18698 }
18699 }
18700
James Smart6669f9b2009-10-02 15:16:45 -040018701 /* Handle the basic abort sequence (BA_ABTS) event */
18702 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
18703 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
18704 return;
18705 }
18706
James Smart4d9ab992009-10-02 15:16:39 -040018707 /* Link this frame */
18708 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
18709 if (!seq_dmabuf) {
18710 /* unable to add frame to vport - throw it out */
18711 lpfc_in_buf_free(phba, &dmabuf->dbuf);
18712 return;
18713 }
18714 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050018715 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040018716 return;
James Smartdef9c7a2009-12-21 17:02:28 -050018717
James Smart6669f9b2009-10-02 15:16:45 -040018718 /* Send the complete sequence to the upper layer protocol */
18719 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040018720}
James Smart6fb120a2009-05-22 14:52:59 -040018721
18722/**
18723 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
18724 * @phba: pointer to lpfc hba data structure.
18725 *
18726 * This routine is invoked to post rpi header templates to the
18727 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018728 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18729 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018730 *
18731 * This routine does not require any locks. It's usage is expected
18732 * to be driver load or reset recovery when the driver is
18733 * sequential.
18734 *
18735 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018736 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040018737 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040018738 * When this error occurs, the driver is not guaranteed
18739 * to have any rpi regions posted to the device and
18740 * must either attempt to repost the regions or take a
18741 * fatal error.
18742 **/
18743int
18744lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
18745{
18746 struct lpfc_rpi_hdr *rpi_page;
18747 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040018748 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018749
James Smart6d368e52011-05-24 11:44:12 -040018750 /* SLI4 ports that support extents do not require RPI headers. */
18751 if (!phba->sli4_hba.rpi_hdrs_in_use)
18752 goto exit;
18753 if (phba->sli4_hba.extents_in_use)
18754 return -EIO;
18755
James Smart6fb120a2009-05-22 14:52:59 -040018756 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040018757 /*
18758 * Assign the rpi headers a physical rpi only if the driver
18759 * has not initialized those resources. A port reset only
18760 * needs the headers posted.
18761 */
18762 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
18763 LPFC_RPI_RSRC_RDY)
18764 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
18765
James Smart6fb120a2009-05-22 14:52:59 -040018766 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
18767 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018768 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040018769 "2008 Error %d posting all rpi "
18770 "headers\n", rc);
18771 rc = -EIO;
18772 break;
18773 }
18774 }
18775
James Smart6d368e52011-05-24 11:44:12 -040018776 exit:
18777 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
18778 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040018779 return rc;
18780}
18781
18782/**
18783 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
18784 * @phba: pointer to lpfc hba data structure.
18785 * @rpi_page: pointer to the rpi memory region.
18786 *
18787 * This routine is invoked to post a single rpi header to the
18788 * HBA consistent with the SLI-4 interface spec. This memory region
18789 * maps up to 64 rpi context regions.
18790 *
18791 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018792 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040018793 * -ENOMEM - No available memory
18794 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040018795 **/
18796int
18797lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
18798{
18799 LPFC_MBOXQ_t *mboxq;
18800 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
18801 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018802 uint32_t shdr_status, shdr_add_status;
18803 union lpfc_sli4_cfg_shdr *shdr;
18804
James Smart6d368e52011-05-24 11:44:12 -040018805 /* SLI4 ports that support extents do not require RPI headers. */
18806 if (!phba->sli4_hba.rpi_hdrs_in_use)
18807 return rc;
18808 if (phba->sli4_hba.extents_in_use)
18809 return -EIO;
18810
James Smart6fb120a2009-05-22 14:52:59 -040018811 /* The port is notified of the header region via a mailbox command. */
18812 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18813 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018814 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040018815 "2001 Unable to allocate memory for issuing "
18816 "SLI_CONFIG_SPECIAL mailbox command\n");
18817 return -ENOMEM;
18818 }
18819
18820 /* Post all rpi memory regions to the port. */
18821 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040018822 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18823 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
18824 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050018825 sizeof(struct lpfc_sli4_cfg_mhdr),
18826 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040018827
18828
18829 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040018830 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
18831 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040018832 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
18833 hdr_tmpl, rpi_page->page_count);
18834
James Smart6fb120a2009-05-22 14:52:59 -040018835 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
18836 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040018837 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040018838 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
18839 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18840 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18841 if (rc != MBX_TIMEOUT)
18842 mempool_free(mboxq, phba->mbox_mem_pool);
18843 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018844 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040018845 "2514 POST_RPI_HDR mailbox failed with "
18846 "status x%x add_status x%x, mbx status x%x\n",
18847 shdr_status, shdr_add_status, rc);
18848 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070018849 } else {
18850 /*
18851 * The next_rpi stores the next logical module-64 rpi value used
18852 * to post physical rpis in subsequent rpi postings.
18853 */
18854 spin_lock_irq(&phba->hbalock);
18855 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
18856 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040018857 }
18858 return rc;
18859}
18860
18861/**
18862 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
18863 * @phba: pointer to lpfc hba data structure.
18864 *
18865 * This routine is invoked to post rpi header templates to the
18866 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040018867 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
18868 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040018869 *
18870 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020018871 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040018872 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
18873 **/
18874int
18875lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
18876{
James Smart6d368e52011-05-24 11:44:12 -040018877 unsigned long rpi;
18878 uint16_t max_rpi, rpi_limit;
18879 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040018880 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040018881 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040018882
James Smart6fb120a2009-05-22 14:52:59 -040018883 /*
James Smart6d368e52011-05-24 11:44:12 -040018884 * Fetch the next logical rpi. Because this index is logical,
18885 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040018886 */
James Smart4902b382013-10-10 12:20:35 -040018887 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040018888 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
18889 rpi_limit = phba->sli4_hba.next_rpi;
18890
James Smart6d368e52011-05-24 11:44:12 -040018891 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
18892 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040018893 rpi = LPFC_RPI_ALLOC_ERROR;
18894 else {
18895 set_bit(rpi, phba->sli4_hba.rpi_bmask);
18896 phba->sli4_hba.max_cfg_param.rpi_used++;
18897 phba->sli4_hba.rpi_count++;
18898 }
James Smart0f154222019-09-21 20:58:52 -070018899 lpfc_printf_log(phba, KERN_INFO,
18900 LOG_NODE | LOG_DISCOVERY,
18901 "0001 Allocated rpi:x%x max:x%x lim:x%x\n",
James Smartbe6bb942015-04-07 15:07:22 -040018902 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040018903
18904 /*
18905 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040018906 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040018907 */
18908 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
18909 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040018910 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018911 return rpi;
18912 }
18913
18914 /*
James Smart6d368e52011-05-24 11:44:12 -040018915 * RPI header postings are not required for SLI4 ports capable of
18916 * extents.
18917 */
18918 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040018919 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040018920 return rpi;
18921 }
18922
18923 /*
James Smart6fb120a2009-05-22 14:52:59 -040018924 * If the driver is running low on rpi resources, allocate another
18925 * page now. Note that the next_rpi value is used because
18926 * it represents how many are actually in use whereas max_rpi notes
18927 * how many are supported max by the device.
18928 */
James Smart6d368e52011-05-24 11:44:12 -040018929 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040018930 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040018931 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
18932 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
18933 if (!rpi_hdr) {
Dick Kennedy372c1872020-06-30 14:50:00 -070018934 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040018935 "2002 Error Could not grow rpi "
18936 "count\n");
18937 } else {
James Smart6d368e52011-05-24 11:44:12 -040018938 lrpi = rpi_hdr->start_rpi;
18939 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040018940 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
18941 }
18942 }
18943
18944 return rpi;
18945}
18946
18947/**
Lee Jones8514e2f2021-03-03 14:46:18 +000018948 * __lpfc_sli4_free_rpi - Release an rpi for reuse.
James Smart6fb120a2009-05-22 14:52:59 -040018949 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010018950 * @rpi: rpi to free
James Smart6fb120a2009-05-22 14:52:59 -040018951 *
18952 * This routine is invoked to release an rpi to the pool of
18953 * available rpis maintained by the driver.
18954 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018955static void
James Smartd7c47992010-06-08 18:31:54 -040018956__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18957{
James Smart7cfd5632019-11-04 16:56:58 -080018958 /*
18959 * if the rpi value indicates a prior unreg has already
18960 * been done, skip the unreg.
18961 */
18962 if (rpi == LPFC_RPI_ALLOC_ERROR)
18963 return;
18964
James Smartd7c47992010-06-08 18:31:54 -040018965 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
18966 phba->sli4_hba.rpi_count--;
18967 phba->sli4_hba.max_cfg_param.rpi_used--;
James Smartb95b2112019-08-14 16:56:47 -070018968 } else {
James Smart0f154222019-09-21 20:58:52 -070018969 lpfc_printf_log(phba, KERN_INFO,
18970 LOG_NODE | LOG_DISCOVERY,
James Smartb95b2112019-08-14 16:56:47 -070018971 "2016 rpi %x not inuse\n",
18972 rpi);
James Smartd7c47992010-06-08 18:31:54 -040018973 }
18974}
18975
18976/**
18977 * lpfc_sli4_free_rpi - Release an rpi for reuse.
18978 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010018979 * @rpi: rpi to free
James Smartd7c47992010-06-08 18:31:54 -040018980 *
18981 * This routine is invoked to release an rpi to the pool of
18982 * available rpis maintained by the driver.
18983 **/
18984void
James Smart6fb120a2009-05-22 14:52:59 -040018985lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
18986{
18987 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040018988 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040018989 spin_unlock_irq(&phba->hbalock);
18990}
18991
18992/**
18993 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
18994 * @phba: pointer to lpfc hba data structure.
18995 *
18996 * This routine is invoked to remove the memory region that
18997 * provided rpi via a bitmask.
18998 **/
18999void
19000lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
19001{
19002 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040019003 kfree(phba->sli4_hba.rpi_ids);
19004 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040019005}
19006
19007/**
19008 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
Lee Jones7af29d42020-07-21 17:41:31 +010019009 * @ndlp: pointer to lpfc nodelist data structure.
19010 * @cmpl: completion call-back.
19011 * @arg: data to load as MBox 'caller buffer information'
James Smart6fb120a2009-05-22 14:52:59 -040019012 *
19013 * This routine is invoked to remove the memory region that
19014 * provided rpi via a bitmask.
19015 **/
19016int
James Smart6b5151f2012-01-18 16:24:06 -050019017lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
19018 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040019019{
19020 LPFC_MBOXQ_t *mboxq;
19021 struct lpfc_hba *phba = ndlp->phba;
19022 int rc;
19023
19024 /* The port is notified of the header region via a mailbox command. */
19025 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19026 if (!mboxq)
19027 return -ENOMEM;
19028
19029 /* Post all rpi memory regions to the port. */
19030 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050019031 if (cmpl) {
19032 mboxq->mbox_cmpl = cmpl;
James Smart3e1f0712018-11-29 16:09:29 -080019033 mboxq->ctx_buf = arg;
19034 mboxq->ctx_ndlp = ndlp;
James Smart72859902012-01-18 16:25:38 -050019035 } else
19036 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050019037 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040019038 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19039 if (rc == MBX_NOT_FINISHED) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019040 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019041 "2010 Resume RPI Mailbox failed "
19042 "status %d, mbxStatus x%x\n", rc,
19043 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
19044 mempool_free(mboxq, phba->mbox_mem_pool);
19045 return -EIO;
19046 }
19047 return 0;
19048}
19049
19050/**
19051 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050019052 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040019053 *
James Smart76a95d72010-11-20 23:11:48 -050019054 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040019055 *
19056 * Returns:
19057 * 0 success
19058 * -Evalue otherwise
19059 **/
19060int
James Smart76a95d72010-11-20 23:11:48 -050019061lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040019062{
19063 LPFC_MBOXQ_t *mboxq;
19064 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040019065 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040019066 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050019067 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040019068 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19069 if (!mboxq)
19070 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050019071 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040019072 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040019073 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040019074 if (rc != MBX_SUCCESS) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019075 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019076 "2022 INIT VPI Mailbox failed "
19077 "status %d, mbxStatus x%x\n", rc,
19078 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040019079 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040019080 }
James Smart6a9c52c2009-10-02 15:16:51 -040019081 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050019082 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040019083
19084 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040019085}
19086
19087/**
19088 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
19089 * @phba: pointer to lpfc hba data structure.
19090 * @mboxq: Pointer to mailbox object.
19091 *
19092 * This routine is invoked to manually add a single FCF record. The caller
19093 * must pass a completely initialized FCF_Record. This routine takes
19094 * care of the nonembedded mailbox operations.
19095 **/
19096static void
19097lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
19098{
19099 void *virt_addr;
19100 union lpfc_sli4_cfg_shdr *shdr;
19101 uint32_t shdr_status, shdr_add_status;
19102
19103 virt_addr = mboxq->sge_array->addr[0];
19104 /* The IOCTL status is embedded in the mailbox subheader. */
19105 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
19106 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
19107 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
19108
19109 if ((shdr_status || shdr_add_status) &&
19110 (shdr_status != STATUS_FCF_IN_USE))
Dick Kennedy372c1872020-06-30 14:50:00 -070019111 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019112 "2558 ADD_FCF_RECORD mailbox failed with "
19113 "status x%x add_status x%x\n",
19114 shdr_status, shdr_add_status);
19115
19116 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19117}
19118
19119/**
19120 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
19121 * @phba: pointer to lpfc hba data structure.
19122 * @fcf_record: pointer to the initialized fcf record to add.
19123 *
19124 * This routine is invoked to manually add a single FCF record. The caller
19125 * must pass a completely initialized FCF_Record. This routine takes
19126 * care of the nonembedded mailbox operations.
19127 **/
19128int
19129lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
19130{
19131 int rc = 0;
19132 LPFC_MBOXQ_t *mboxq;
19133 uint8_t *bytep;
19134 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040019135 struct lpfc_mbx_sge sge;
19136 uint32_t alloc_len, req_len;
19137 uint32_t fcfindex;
19138
19139 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19140 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019141 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019142 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
19143 return -ENOMEM;
19144 }
19145
19146 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
19147 sizeof(uint32_t);
19148
19149 /* Allocate DMA memory and set up the non-embedded mailbox command */
19150 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19151 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
19152 req_len, LPFC_SLI4_MBX_NEMBED);
19153 if (alloc_len < req_len) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019154 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019155 "2523 Allocated DMA memory size (x%x) is "
19156 "less than the requested DMA memory "
19157 "size (x%x)\n", alloc_len, req_len);
19158 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19159 return -ENOMEM;
19160 }
19161
19162 /*
19163 * Get the first SGE entry from the non-embedded DMA memory. This
19164 * routine only uses a single SGE.
19165 */
19166 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040019167 virt_addr = mboxq->sge_array->addr[0];
19168 /*
19169 * Configure the FCF record for FCFI 0. This is the driver's
19170 * hardcoded default and gets used in nonFIP mode.
19171 */
19172 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
19173 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
19174 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
19175
19176 /*
19177 * Copy the fcf_index and the FCF Record Data. The data starts after
19178 * the FCoE header plus word10. The data copy needs to be endian
19179 * correct.
19180 */
19181 bytep += sizeof(uint32_t);
19182 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
19183 mboxq->vport = phba->pport;
19184 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
19185 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19186 if (rc == MBX_NOT_FINISHED) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019187 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019188 "2515 ADD_FCF_RECORD mailbox failed with "
19189 "status 0x%x\n", rc);
19190 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19191 rc = -EIO;
19192 } else
19193 rc = 0;
19194
19195 return rc;
19196}
19197
19198/**
19199 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
19200 * @phba: pointer to lpfc hba data structure.
19201 * @fcf_record: pointer to the fcf record to write the default data.
19202 * @fcf_index: FCF table entry index.
19203 *
19204 * This routine is invoked to build the driver's default FCF record. The
19205 * values used are hardcoded. This routine handles memory initialization.
19206 *
19207 **/
19208void
19209lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
19210 struct fcf_record *fcf_record,
19211 uint16_t fcf_index)
19212{
19213 memset(fcf_record, 0, sizeof(struct fcf_record));
19214 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
19215 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
19216 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
19217 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
19218 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
19219 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
19220 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
19221 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
19222 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
19223 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
19224 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
19225 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
19226 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040019227 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040019228 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
19229 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
19230 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
19231 /* Set the VLAN bit map */
19232 if (phba->valid_vlan) {
19233 fcf_record->vlan_bitmap[phba->vlan_id / 8]
19234 = 1 << (phba->vlan_id % 8);
19235 }
19236}
19237
19238/**
James Smart0c9ab6f2010-02-26 14:15:57 -050019239 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040019240 * @phba: pointer to lpfc hba data structure.
19241 * @fcf_index: FCF table entry offset.
19242 *
James Smart0c9ab6f2010-02-26 14:15:57 -050019243 * This routine is invoked to scan the entire FCF table by reading FCF
19244 * record and processing it one at a time starting from the @fcf_index
19245 * for initial FCF discovery or fast FCF failover rediscovery.
19246 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030019247 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050019248 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040019249 **/
19250int
James Smart0c9ab6f2010-02-26 14:15:57 -050019251lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040019252{
19253 int rc = 0, error;
19254 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040019255
James Smart32b97932009-07-19 10:01:21 -040019256 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050019257 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040019258 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19259 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019260 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart6fb120a2009-05-22 14:52:59 -040019261 "2000 Failed to allocate mbox for "
19262 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040019263 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050019264 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040019265 }
James Smartecfd03c2010-02-12 14:41:27 -050019266 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050019267 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050019268 if (rc) {
19269 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050019270 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040019271 }
James Smartecfd03c2010-02-12 14:41:27 -050019272 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040019273 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050019274 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040019275
19276 spin_lock_irq(&phba->hbalock);
19277 phba->hba_flag |= FCF_TS_INPROG;
19278 spin_unlock_irq(&phba->hbalock);
19279
James Smart6fb120a2009-05-22 14:52:59 -040019280 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050019281 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040019282 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050019283 else {
James Smart38b92ef2010-08-04 16:11:39 -040019284 /* Reset eligible FCF count for new scan */
19285 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040019286 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040019287 error = 0;
James Smart32b97932009-07-19 10:01:21 -040019288 }
James Smart0c9ab6f2010-02-26 14:15:57 -050019289fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040019290 if (error) {
19291 if (mboxq)
19292 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040019293 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040019294 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040019295 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040019296 spin_unlock_irq(&phba->hbalock);
19297 }
James Smart6fb120a2009-05-22 14:52:59 -040019298 return error;
19299}
James Smarta0c87cb2009-07-19 10:01:10 -040019300
19301/**
James Smarta93ff372010-10-22 11:06:08 -040019302 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050019303 * @phba: pointer to lpfc hba data structure.
19304 * @fcf_index: FCF table entry offset.
19305 *
19306 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040019307 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050019308 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030019309 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050019310 * otherwise.
19311 **/
19312int
19313lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
19314{
19315 int rc = 0, error;
19316 LPFC_MBOXQ_t *mboxq;
19317
19318 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19319 if (!mboxq) {
19320 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
19321 "2763 Failed to allocate mbox for "
19322 "READ_FCF cmd\n");
19323 error = -ENOMEM;
19324 goto fail_fcf_read;
19325 }
19326 /* Construct the read FCF record mailbox command */
19327 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
19328 if (rc) {
19329 error = -EINVAL;
19330 goto fail_fcf_read;
19331 }
19332 /* Issue the mailbox command asynchronously */
19333 mboxq->vport = phba->pport;
19334 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
19335 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19336 if (rc == MBX_NOT_FINISHED)
19337 error = -EIO;
19338 else
19339 error = 0;
19340
19341fail_fcf_read:
19342 if (error && mboxq)
19343 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19344 return error;
19345}
19346
19347/**
19348 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
19349 * @phba: pointer to lpfc hba data structure.
19350 * @fcf_index: FCF table entry offset.
19351 *
19352 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040019353 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050019354 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030019355 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050019356 * otherwise.
19357 **/
19358int
19359lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
19360{
19361 int rc = 0, error;
19362 LPFC_MBOXQ_t *mboxq;
19363
19364 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19365 if (!mboxq) {
19366 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
19367 "2758 Failed to allocate mbox for "
19368 "READ_FCF cmd\n");
19369 error = -ENOMEM;
19370 goto fail_fcf_read;
19371 }
19372 /* Construct the read FCF record mailbox command */
19373 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
19374 if (rc) {
19375 error = -EINVAL;
19376 goto fail_fcf_read;
19377 }
19378 /* Issue the mailbox command asynchronously */
19379 mboxq->vport = phba->pport;
19380 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
19381 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
19382 if (rc == MBX_NOT_FINISHED)
19383 error = -EIO;
19384 else
19385 error = 0;
19386
19387fail_fcf_read:
19388 if (error && mboxq)
19389 lpfc_sli4_mbox_cmd_free(phba, mboxq);
19390 return error;
19391}
19392
19393/**
James Smartf5cb5302015-12-16 18:11:52 -050019394 * lpfc_check_next_fcf_pri_level
Lee Jones7af29d42020-07-21 17:41:31 +010019395 * @phba: pointer to the lpfc_hba struct for this port.
James Smart7d791df2011-07-22 18:37:52 -040019396 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
19397 * routine when the rr_bmask is empty. The FCF indecies are put into the
19398 * rr_bmask based on their priority level. Starting from the highest priority
19399 * to the lowest. The most likely FCF candidate will be in the highest
19400 * priority group. When this routine is called it searches the fcf_pri list for
19401 * next lowest priority group and repopulates the rr_bmask with only those
19402 * fcf_indexes.
19403 * returns:
19404 * 1=success 0=failure
19405 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040019406static int
James Smart7d791df2011-07-22 18:37:52 -040019407lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
19408{
19409 uint16_t next_fcf_pri;
19410 uint16_t last_index;
19411 struct lpfc_fcf_pri *fcf_pri;
19412 int rc;
19413 int ret = 0;
19414
19415 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
19416 LPFC_SLI4_FCF_TBL_INDX_MAX);
19417 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
19418 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050019419
19420 /* Verify the priority list has 2 or more entries */
19421 spin_lock_irq(&phba->hbalock);
19422 if (list_empty(&phba->fcf.fcf_pri_list) ||
19423 list_is_singular(&phba->fcf.fcf_pri_list)) {
19424 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040019425 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
19426 "3061 Last IDX %d\n", last_index);
19427 return 0; /* Empty rr list */
19428 }
James Smart25626692013-03-01 16:36:54 -050019429 spin_unlock_irq(&phba->hbalock);
19430
James Smart7d791df2011-07-22 18:37:52 -040019431 next_fcf_pri = 0;
19432 /*
19433 * Clear the rr_bmask and set all of the bits that are at this
19434 * priority.
19435 */
19436 memset(phba->fcf.fcf_rr_bmask, 0,
19437 sizeof(*phba->fcf.fcf_rr_bmask));
19438 spin_lock_irq(&phba->hbalock);
19439 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
19440 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
19441 continue;
19442 /*
19443 * the 1st priority that has not FLOGI failed
19444 * will be the highest.
19445 */
19446 if (!next_fcf_pri)
19447 next_fcf_pri = fcf_pri->fcf_rec.priority;
19448 spin_unlock_irq(&phba->hbalock);
19449 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
19450 rc = lpfc_sli4_fcf_rr_index_set(phba,
19451 fcf_pri->fcf_rec.fcf_index);
19452 if (rc)
19453 return 0;
19454 }
19455 spin_lock_irq(&phba->hbalock);
19456 }
19457 /*
19458 * if next_fcf_pri was not set above and the list is not empty then
19459 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070019460 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040019461 */
19462 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
19463 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
19464 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
19465 /*
19466 * the 1st priority that has not FLOGI failed
19467 * will be the highest.
19468 */
19469 if (!next_fcf_pri)
19470 next_fcf_pri = fcf_pri->fcf_rec.priority;
19471 spin_unlock_irq(&phba->hbalock);
19472 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
19473 rc = lpfc_sli4_fcf_rr_index_set(phba,
19474 fcf_pri->fcf_rec.fcf_index);
19475 if (rc)
19476 return 0;
19477 }
19478 spin_lock_irq(&phba->hbalock);
19479 }
19480 } else
19481 ret = 1;
19482 spin_unlock_irq(&phba->hbalock);
19483
19484 return ret;
19485}
19486/**
James Smart0c9ab6f2010-02-26 14:15:57 -050019487 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
19488 * @phba: pointer to lpfc hba data structure.
19489 *
19490 * This routine is to get the next eligible FCF record index in a round
19491 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040019492 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050019493 * shall be returned, otherwise, the next eligible FCF record's index
19494 * shall be returned.
19495 **/
19496uint16_t
19497lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
19498{
19499 uint16_t next_fcf_index;
19500
James Smart421c6622013-01-03 15:44:16 -050019501initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040019502 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050019503 next_fcf_index = phba->fcf.current_rec.fcf_indx;
19504
James Smart7d791df2011-07-22 18:37:52 -040019505next_priority:
James Smart421c6622013-01-03 15:44:16 -050019506 /* Determine the next fcf index to check */
19507 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050019508 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
19509 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040019510 next_fcf_index);
19511
James Smart0c9ab6f2010-02-26 14:15:57 -050019512 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040019513 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19514 /*
19515 * If we have wrapped then we need to clear the bits that
19516 * have been tested so that we can detect when we should
19517 * change the priority level.
19518 */
James Smart0c9ab6f2010-02-26 14:15:57 -050019519 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
19520 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040019521 }
19522
James Smart0c9ab6f2010-02-26 14:15:57 -050019523
James Smart3804dc82010-07-14 15:31:37 -040019524 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040019525 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
19526 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
19527 /*
19528 * If next fcf index is not found check if there are lower
19529 * Priority level fcf's in the fcf_priority list.
19530 * Set up the rr_bmask with all of the avaiable fcf bits
19531 * at that level and continue the selection process.
19532 */
19533 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050019534 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040019535 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
19536 "2844 No roundrobin failover FCF available\n");
James Smart036cad12018-10-23 13:41:06 -070019537
19538 return LPFC_FCOE_FCF_NEXT_NONE;
James Smart3804dc82010-07-14 15:31:37 -040019539 }
19540
James Smart7d791df2011-07-22 18:37:52 -040019541 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
19542 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050019543 LPFC_FCF_FLOGI_FAILED) {
19544 if (list_is_singular(&phba->fcf.fcf_pri_list))
19545 return LPFC_FCOE_FCF_NEXT_NONE;
19546
James Smart7d791df2011-07-22 18:37:52 -040019547 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050019548 }
James Smart7d791df2011-07-22 18:37:52 -040019549
James Smart3804dc82010-07-14 15:31:37 -040019550 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019551 "2845 Get next roundrobin failover FCF (x%x)\n",
19552 next_fcf_index);
19553
James Smart0c9ab6f2010-02-26 14:15:57 -050019554 return next_fcf_index;
19555}
19556
19557/**
19558 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
19559 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010019560 * @fcf_index: index into the FCF table to 'set'
James Smart0c9ab6f2010-02-26 14:15:57 -050019561 *
19562 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040019563 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050019564 * does not go beyond the range of the driver allocated bmask dimension
19565 * before setting the bit.
19566 *
19567 * Returns 0 if the index bit successfully set, otherwise, it returns
19568 * -EINVAL.
19569 **/
19570int
19571lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
19572{
19573 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19574 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019575 "2610 FCF (x%x) reached driver's book "
19576 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050019577 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
19578 return -EINVAL;
19579 }
19580 /* Set the eligible FCF record index bmask */
19581 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
19582
James Smart3804dc82010-07-14 15:31:37 -040019583 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019584 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040019585 "bmask\n", fcf_index);
19586
James Smart0c9ab6f2010-02-26 14:15:57 -050019587 return 0;
19588}
19589
19590/**
James Smart3804dc82010-07-14 15:31:37 -040019591 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050019592 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010019593 * @fcf_index: index into the FCF table to 'clear'
James Smart0c9ab6f2010-02-26 14:15:57 -050019594 *
19595 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040019596 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050019597 * does not go beyond the range of the driver allocated bmask dimension
19598 * before clearing the bit.
19599 **/
19600void
19601lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
19602{
James Smart9a803a72013-09-06 12:17:56 -040019603 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050019604 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
19605 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019606 "2762 FCF (x%x) reached driver's book "
19607 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050019608 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
19609 return;
19610 }
19611 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040019612 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040019613 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
19614 list) {
James Smart7d791df2011-07-22 18:37:52 -040019615 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
19616 list_del_init(&fcf_pri->list);
19617 break;
19618 }
19619 }
19620 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050019621 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040019622
19623 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019624 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040019625 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050019626}
19627
19628/**
James Smartecfd03c2010-02-12 14:41:27 -050019629 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
19630 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010019631 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
James Smartecfd03c2010-02-12 14:41:27 -050019632 *
19633 * This routine is the completion routine for the rediscover FCF table mailbox
19634 * command. If the mailbox command returned failure, it will try to stop the
19635 * FCF rediscover wait timer.
19636 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040019637static void
James Smartecfd03c2010-02-12 14:41:27 -050019638lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
19639{
19640 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
19641 uint32_t shdr_status, shdr_add_status;
19642
19643 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
19644
19645 shdr_status = bf_get(lpfc_mbox_hdr_status,
19646 &redisc_fcf->header.cfg_shdr.response);
19647 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
19648 &redisc_fcf->header.cfg_shdr.response);
19649 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050019650 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050019651 "2746 Requesting for FCF rediscovery failed "
19652 "status x%x add_status x%x\n",
19653 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050019654 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050019655 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050019656 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050019657 spin_unlock_irq(&phba->hbalock);
19658 /*
19659 * CVL event triggered FCF rediscover request failed,
19660 * last resort to re-try current registered FCF entry.
19661 */
19662 lpfc_retry_pport_discovery(phba);
19663 } else {
19664 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050019665 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050019666 spin_unlock_irq(&phba->hbalock);
19667 /*
19668 * DEAD FCF event triggered FCF rediscover request
19669 * failed, last resort to fail over as a link down
19670 * to FCF registration.
19671 */
19672 lpfc_sli4_fcf_dead_failthrough(phba);
19673 }
James Smart0c9ab6f2010-02-26 14:15:57 -050019674 } else {
19675 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040019676 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050019677 /*
19678 * Start FCF rediscovery wait timer for pending FCF
19679 * before rescan FCF record table.
19680 */
19681 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050019682 }
James Smartecfd03c2010-02-12 14:41:27 -050019683
19684 mempool_free(mbox, phba->mbox_mem_pool);
19685}
19686
19687/**
James Smart3804dc82010-07-14 15:31:37 -040019688 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050019689 * @phba: pointer to lpfc hba data structure.
19690 *
19691 * This routine is invoked to request for rediscovery of the entire FCF table
19692 * by the port.
19693 **/
19694int
19695lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
19696{
19697 LPFC_MBOXQ_t *mbox;
19698 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
19699 int rc, length;
19700
James Smart0c9ab6f2010-02-26 14:15:57 -050019701 /* Cancel retry delay timers to all vports before FCF rediscover */
19702 lpfc_cancel_all_vport_retry_delay_timer(phba);
19703
James Smartecfd03c2010-02-12 14:41:27 -050019704 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19705 if (!mbox) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019706 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smartecfd03c2010-02-12 14:41:27 -050019707 "2745 Failed to allocate mbox for "
19708 "requesting FCF rediscover.\n");
19709 return -ENOMEM;
19710 }
19711
19712 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
19713 sizeof(struct lpfc_sli4_cfg_mhdr));
19714 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
19715 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
19716 length, LPFC_SLI4_MBX_EMBED);
19717
19718 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
19719 /* Set count to 0 for invalidating the entire FCF database */
19720 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
19721
19722 /* Issue the mailbox command asynchronously */
19723 mbox->vport = phba->pport;
19724 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
19725 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
19726
19727 if (rc == MBX_NOT_FINISHED) {
19728 mempool_free(mbox, phba->mbox_mem_pool);
19729 return -EIO;
19730 }
19731 return 0;
19732}
19733
19734/**
James Smartfc2b9892010-02-26 14:15:29 -050019735 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
19736 * @phba: pointer to lpfc hba data structure.
19737 *
19738 * This function is the failover routine as a last resort to the FCF DEAD
19739 * event when driver failed to perform fast FCF failover.
19740 **/
19741void
19742lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
19743{
19744 uint32_t link_state;
19745
19746 /*
19747 * Last resort as FCF DEAD event failover will treat this as
19748 * a link down, but save the link state because we don't want
19749 * it to be changed to Link Down unless it is already down.
19750 */
19751 link_state = phba->link_state;
19752 lpfc_linkdown(phba);
19753 phba->link_state = link_state;
19754
19755 /* Unregister FCF if no devices connected to it */
19756 lpfc_unregister_unused_fcf(phba);
19757}
19758
19759/**
James Smart026abb82011-12-13 13:20:45 -050019760 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040019761 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050019762 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040019763 *
James Smart026abb82011-12-13 13:20:45 -050019764 * This function gets SLI3 port configure region 23 data through memory dump
19765 * mailbox command. When it successfully retrieves data, the size of the data
19766 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040019767 **/
James Smart026abb82011-12-13 13:20:45 -050019768static uint32_t
19769lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040019770{
19771 LPFC_MBOXQ_t *pmb = NULL;
19772 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050019773 uint32_t offset = 0;
Dick Kennedyd91e3ab2020-06-30 14:49:52 -070019774 int i, rc;
James Smarta0c87cb2009-07-19 10:01:10 -040019775
James Smart026abb82011-12-13 13:20:45 -050019776 if (!rgn23_data)
19777 return 0;
19778
James Smarta0c87cb2009-07-19 10:01:10 -040019779 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19780 if (!pmb) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019781 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart026abb82011-12-13 13:20:45 -050019782 "2600 failed to allocate mailbox memory\n");
19783 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040019784 }
19785 mb = &pmb->u.mb;
19786
James Smarta0c87cb2009-07-19 10:01:10 -040019787 do {
19788 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
19789 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
19790
19791 if (rc != MBX_SUCCESS) {
19792 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050019793 "2601 failed to read config "
19794 "region 23, rc 0x%x Status 0x%x\n",
19795 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040019796 mb->un.varDmp.word_cnt = 0;
19797 }
19798 /*
19799 * dump mem may return a zero when finished or we got a
19800 * mailbox error, either way we are done.
19801 */
19802 if (mb->un.varDmp.word_cnt == 0)
19803 break;
James Smarta0c87cb2009-07-19 10:01:10 -040019804
Dick Kennedyd91e3ab2020-06-30 14:49:52 -070019805 i = mb->un.varDmp.word_cnt * sizeof(uint32_t);
19806 if (offset + i > DMP_RGN23_SIZE)
19807 i = DMP_RGN23_SIZE - offset;
James Smarta0c87cb2009-07-19 10:01:10 -040019808 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
Dick Kennedyd91e3ab2020-06-30 14:49:52 -070019809 rgn23_data + offset, i);
19810 offset += i;
19811 } while (offset < DMP_RGN23_SIZE);
James Smarta0c87cb2009-07-19 10:01:10 -040019812
James Smart026abb82011-12-13 13:20:45 -050019813 mempool_free(pmb, phba->mbox_mem_pool);
19814 return offset;
19815}
19816
19817/**
19818 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
19819 * @phba: pointer to lpfc hba data structure.
19820 * @rgn23_data: pointer to configure region 23 data.
19821 *
19822 * This function gets SLI4 port configure region 23 data through memory dump
19823 * mailbox command. When it successfully retrieves data, the size of the data
19824 * will be returned, otherwise, 0 will be returned.
19825 **/
19826static uint32_t
19827lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
19828{
19829 LPFC_MBOXQ_t *mboxq = NULL;
19830 struct lpfc_dmabuf *mp = NULL;
19831 struct lpfc_mqe *mqe;
19832 uint32_t data_length = 0;
19833 int rc;
19834
19835 if (!rgn23_data)
19836 return 0;
19837
19838 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19839 if (!mboxq) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019840 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart026abb82011-12-13 13:20:45 -050019841 "3105 failed to allocate mailbox memory\n");
19842 return 0;
19843 }
19844
19845 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
19846 goto out;
19847 mqe = &mboxq->u.mqe;
James Smart3e1f0712018-11-29 16:09:29 -080019848 mp = (struct lpfc_dmabuf *)mboxq->ctx_buf;
James Smart026abb82011-12-13 13:20:45 -050019849 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
19850 if (rc)
19851 goto out;
19852 data_length = mqe->un.mb_words[5];
19853 if (data_length == 0)
19854 goto out;
19855 if (data_length > DMP_RGN23_SIZE) {
19856 data_length = 0;
19857 goto out;
19858 }
19859 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
19860out:
19861 mempool_free(mboxq, phba->mbox_mem_pool);
19862 if (mp) {
19863 lpfc_mbuf_free(phba, mp->virt, mp->phys);
19864 kfree(mp);
19865 }
19866 return data_length;
19867}
19868
19869/**
19870 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
19871 * @phba: pointer to lpfc hba data structure.
19872 *
19873 * This function read region 23 and parse TLV for port status to
19874 * decide if the user disaled the port. If the TLV indicates the
19875 * port is disabled, the hba_flag is set accordingly.
19876 **/
19877void
19878lpfc_sli_read_link_ste(struct lpfc_hba *phba)
19879{
19880 uint8_t *rgn23_data = NULL;
19881 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
19882 uint32_t offset = 0;
19883
19884 /* Get adapter Region 23 data */
19885 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
19886 if (!rgn23_data)
19887 goto out;
19888
19889 if (phba->sli_rev < LPFC_SLI_REV4)
19890 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
19891 else {
19892 if_type = bf_get(lpfc_sli_intf_if_type,
19893 &phba->sli4_hba.sli_intf);
19894 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
19895 goto out;
19896 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
19897 }
James Smarta0c87cb2009-07-19 10:01:10 -040019898
19899 if (!data_size)
19900 goto out;
19901
19902 /* Check the region signature first */
19903 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019904 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta0c87cb2009-07-19 10:01:10 -040019905 "2619 Config region 23 has bad signature\n");
19906 goto out;
19907 }
19908 offset += 4;
19909
19910 /* Check the data structure version */
19911 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
Dick Kennedy372c1872020-06-30 14:50:00 -070019912 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta0c87cb2009-07-19 10:01:10 -040019913 "2620 Config region 23 has bad version\n");
19914 goto out;
19915 }
19916 offset += 4;
19917
19918 /* Parse TLV entries in the region */
19919 while (offset < data_size) {
19920 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
19921 break;
19922 /*
19923 * If the TLV is not driver specific TLV or driver id is
19924 * not linux driver id, skip the record.
19925 */
19926 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
19927 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
19928 (rgn23_data[offset + 3] != 0)) {
19929 offset += rgn23_data[offset + 1] * 4 + 4;
19930 continue;
19931 }
19932
19933 /* Driver found a driver specific TLV in the config region */
19934 sub_tlv_len = rgn23_data[offset + 1] * 4;
19935 offset += 4;
19936 tlv_offset = 0;
19937
19938 /*
19939 * Search for configured port state sub-TLV.
19940 */
19941 while ((offset < data_size) &&
19942 (tlv_offset < sub_tlv_len)) {
19943 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
19944 offset += 4;
19945 tlv_offset += 4;
19946 break;
19947 }
19948 if (rgn23_data[offset] != PORT_STE_TYPE) {
19949 offset += rgn23_data[offset + 1] * 4 + 4;
19950 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
19951 continue;
19952 }
19953
19954 /* This HBA contains PORT_STE configured */
19955 if (!rgn23_data[offset + 2])
19956 phba->hba_flag |= LINK_DISABLED;
19957
19958 goto out;
19959 }
19960 }
James Smart026abb82011-12-13 13:20:45 -050019961
James Smarta0c87cb2009-07-19 10:01:10 -040019962out:
James Smarta0c87cb2009-07-19 10:01:10 -040019963 kfree(rgn23_data);
19964 return;
19965}
James Smart695a8142010-01-26 23:08:03 -050019966
19967/**
James Smart52d52442011-05-24 11:42:45 -040019968 * lpfc_wr_object - write an object to the firmware
19969 * @phba: HBA structure that indicates port to create a queue on.
19970 * @dmabuf_list: list of dmabufs to write to the port.
19971 * @size: the total byte value of the objects to write to the port.
19972 * @offset: the current offset to be used to start the transfer.
19973 *
19974 * This routine will create a wr_object mailbox command to send to the port.
19975 * the mailbox command will be constructed using the dma buffers described in
19976 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
19977 * BDEs that the imbedded mailbox can support. The @offset variable will be
19978 * used to indicate the starting offset of the transfer and will also return
19979 * the offset after the write object mailbox has completed. @size is used to
19980 * determine the end of the object and whether the eof bit should be set.
19981 *
19982 * Return 0 is successful and offset will contain the the new offset to use
19983 * for the next write.
19984 * Return negative value for error cases.
19985 **/
19986int
19987lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
19988 uint32_t size, uint32_t *offset)
19989{
19990 struct lpfc_mbx_wr_object *wr_object;
19991 LPFC_MBOXQ_t *mbox;
19992 int rc = 0, i = 0;
James Smartf3d0a8a2019-12-18 15:58:01 -080019993 uint32_t shdr_status, shdr_add_status, shdr_change_status, shdr_csf;
James Smart52d52442011-05-24 11:42:45 -040019994 uint32_t mbox_tmo;
James Smart52d52442011-05-24 11:42:45 -040019995 struct lpfc_dmabuf *dmabuf;
19996 uint32_t written = 0;
James Smart50212672018-12-13 15:17:57 -080019997 bool check_change_status = false;
James Smart52d52442011-05-24 11:42:45 -040019998
19999 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
20000 if (!mbox)
20001 return -ENOMEM;
20002
20003 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
20004 LPFC_MBOX_OPCODE_WRITE_OBJECT,
20005 sizeof(struct lpfc_mbx_wr_object) -
20006 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
20007
20008 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
20009 wr_object->u.request.write_offset = *offset;
20010 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
20011 wr_object->u.request.object_name[0] =
20012 cpu_to_le32(wr_object->u.request.object_name[0]);
20013 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
20014 list_for_each_entry(dmabuf, dmabuf_list, list) {
20015 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
20016 break;
20017 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
20018 wr_object->u.request.bde[i].addrHigh =
20019 putPaddrHigh(dmabuf->phys);
20020 if (written + SLI4_PAGE_SIZE >= size) {
20021 wr_object->u.request.bde[i].tus.f.bdeSize =
20022 (size - written);
20023 written += (size - written);
20024 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
James Smart50212672018-12-13 15:17:57 -080020025 bf_set(lpfc_wr_object_eas, &wr_object->u.request, 1);
20026 check_change_status = true;
James Smart52d52442011-05-24 11:42:45 -040020027 } else {
20028 wr_object->u.request.bde[i].tus.f.bdeSize =
20029 SLI4_PAGE_SIZE;
20030 written += SLI4_PAGE_SIZE;
20031 }
20032 i++;
20033 }
20034 wr_object->u.request.bde_count = i;
20035 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
20036 if (!phba->sli4_hba.intr_enable)
20037 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
20038 else {
James Smarta183a152011-10-10 21:32:43 -040020039 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040020040 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
20041 }
20042 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart50212672018-12-13 15:17:57 -080020043 shdr_status = bf_get(lpfc_mbox_hdr_status,
20044 &wr_object->header.cfg_shdr.response);
20045 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
20046 &wr_object->header.cfg_shdr.response);
20047 if (check_change_status) {
20048 shdr_change_status = bf_get(lpfc_wr_object_change_status,
20049 &wr_object->u.response);
James Smartf3d0a8a2019-12-18 15:58:01 -080020050
20051 if (shdr_change_status == LPFC_CHANGE_STATUS_FW_RESET ||
20052 shdr_change_status == LPFC_CHANGE_STATUS_PORT_MIGRATION) {
20053 shdr_csf = bf_get(lpfc_wr_object_csf,
20054 &wr_object->u.response);
20055 if (shdr_csf)
20056 shdr_change_status =
20057 LPFC_CHANGE_STATUS_PCI_RESET;
20058 }
20059
James Smart50212672018-12-13 15:17:57 -080020060 switch (shdr_change_status) {
20061 case (LPFC_CHANGE_STATUS_PHYS_DEV_RESET):
20062 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20063 "3198 Firmware write complete: System "
20064 "reboot required to instantiate\n");
20065 break;
20066 case (LPFC_CHANGE_STATUS_FW_RESET):
20067 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20068 "3199 Firmware write complete: Firmware"
20069 " reset required to instantiate\n");
20070 break;
20071 case (LPFC_CHANGE_STATUS_PORT_MIGRATION):
20072 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20073 "3200 Firmware write complete: Port "
20074 "Migration or PCI Reset required to "
20075 "instantiate\n");
20076 break;
20077 case (LPFC_CHANGE_STATUS_PCI_RESET):
20078 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
20079 "3201 Firmware write complete: PCI "
20080 "Reset required to instantiate\n");
20081 break;
20082 default:
20083 break;
20084 }
20085 }
James Smart52d52442011-05-24 11:42:45 -040020086 if (rc != MBX_TIMEOUT)
20087 mempool_free(mbox, phba->mbox_mem_pool);
20088 if (shdr_status || shdr_add_status || rc) {
Dick Kennedy372c1872020-06-30 14:50:00 -070020089 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart52d52442011-05-24 11:42:45 -040020090 "3025 Write Object mailbox failed with "
20091 "status x%x add_status x%x, mbx status x%x\n",
20092 shdr_status, shdr_add_status, rc);
20093 rc = -ENXIO;
James Smart1feb8202018-02-22 08:18:47 -080020094 *offset = shdr_add_status;
James Smart52d52442011-05-24 11:42:45 -040020095 } else
20096 *offset += wr_object->u.response.actual_write_length;
20097 return rc;
20098}
20099
20100/**
James Smart695a8142010-01-26 23:08:03 -050020101 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
20102 * @vport: pointer to vport data structure.
20103 *
20104 * This function iterate through the mailboxq and clean up all REG_LOGIN
20105 * and REG_VPI mailbox commands associated with the vport. This function
20106 * is called when driver want to restart discovery of the vport due to
20107 * a Clear Virtual Link event.
20108 **/
20109void
20110lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
20111{
20112 struct lpfc_hba *phba = vport->phba;
20113 LPFC_MBOXQ_t *mb, *nextmb;
20114 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040020115 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040020116 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smartd439d282010-09-29 11:18:45 -040020117 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050020118 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050020119
James Smartd439d282010-09-29 11:18:45 -040020120 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050020121 spin_lock_irq(&phba->hbalock);
20122 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
20123 if (mb->vport != vport)
20124 continue;
20125
20126 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
20127 (mb->u.mb.mbxCommand != MBX_REG_VPI))
20128 continue;
20129
James Smartd439d282010-09-29 11:18:45 -040020130 list_del(&mb->list);
20131 list_add_tail(&mb->list, &mbox_cmd_list);
20132 }
20133 /* Clean up active mailbox command with the vport */
20134 mb = phba->sli.mbox_active;
20135 if (mb && (mb->vport == vport)) {
20136 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
20137 (mb->u.mb.mbxCommand == MBX_REG_VPI))
20138 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
20139 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080020140 act_mbx_ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smartd439d282010-09-29 11:18:45 -040020141 /* Put reference count for delayed processing */
20142 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
20143 /* Unregister the RPI when mailbox complete */
20144 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
20145 }
20146 }
James Smart63e801c2010-11-20 23:14:19 -050020147 /* Cleanup any mailbox completions which are not yet processed */
20148 do {
20149 restart_loop = 0;
20150 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
20151 /*
20152 * If this mailox is already processed or it is
20153 * for another vport ignore it.
20154 */
20155 if ((mb->vport != vport) ||
20156 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
20157 continue;
20158
20159 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
20160 (mb->u.mb.mbxCommand != MBX_REG_VPI))
20161 continue;
20162
20163 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
20164 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080020165 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
James Smart63e801c2010-11-20 23:14:19 -050020166 /* Unregister the RPI when mailbox complete */
20167 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
20168 restart_loop = 1;
20169 spin_unlock_irq(&phba->hbalock);
James Smartc6adba12020-11-15 11:26:34 -080020170 spin_lock(&ndlp->lock);
James Smart63e801c2010-11-20 23:14:19 -050020171 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
James Smartc6adba12020-11-15 11:26:34 -080020172 spin_unlock(&ndlp->lock);
James Smart63e801c2010-11-20 23:14:19 -050020173 spin_lock_irq(&phba->hbalock);
20174 break;
20175 }
20176 }
20177 } while (restart_loop);
20178
James Smartd439d282010-09-29 11:18:45 -040020179 spin_unlock_irq(&phba->hbalock);
20180
20181 /* Release the cleaned-up mailbox commands */
20182 while (!list_empty(&mbox_cmd_list)) {
20183 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050020184 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
James Smart3e1f0712018-11-29 16:09:29 -080020185 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
James Smart695a8142010-01-26 23:08:03 -050020186 if (mp) {
20187 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
20188 kfree(mp);
20189 }
James Smart3e1f0712018-11-29 16:09:29 -080020190 mb->ctx_buf = NULL;
20191 ndlp = (struct lpfc_nodelist *)mb->ctx_ndlp;
20192 mb->ctx_ndlp = NULL;
James Smart78730cf2010-04-06 15:06:30 -040020193 if (ndlp) {
James Smartc6adba12020-11-15 11:26:34 -080020194 spin_lock(&ndlp->lock);
James Smart589a52d2010-07-14 15:30:54 -040020195 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
James Smartc6adba12020-11-15 11:26:34 -080020196 spin_unlock(&ndlp->lock);
James Smart78730cf2010-04-06 15:06:30 -040020197 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040020198 }
James Smart695a8142010-01-26 23:08:03 -050020199 }
James Smart695a8142010-01-26 23:08:03 -050020200 mempool_free(mb, phba->mbox_mem_pool);
20201 }
James Smartd439d282010-09-29 11:18:45 -040020202
20203 /* Release the ndlp with the cleaned-up active mailbox command */
20204 if (act_mbx_ndlp) {
James Smartc6adba12020-11-15 11:26:34 -080020205 spin_lock(&act_mbx_ndlp->lock);
James Smartd439d282010-09-29 11:18:45 -040020206 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
James Smartc6adba12020-11-15 11:26:34 -080020207 spin_unlock(&act_mbx_ndlp->lock);
James Smartd439d282010-09-29 11:18:45 -040020208 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050020209 }
James Smart695a8142010-01-26 23:08:03 -050020210}
20211
James Smart2a9bf3d2010-06-07 15:24:45 -040020212/**
20213 * lpfc_drain_txq - Drain the txq
20214 * @phba: Pointer to HBA context object.
20215 *
20216 * This function attempt to submit IOCBs on the txq
20217 * to the adapter. For SLI4 adapters, the txq contains
20218 * ELS IOCBs that have been deferred because the there
20219 * are no SGLs. This congestion can occur with large
20220 * vport counts during node discovery.
20221 **/
20222
20223uint32_t
20224lpfc_drain_txq(struct lpfc_hba *phba)
20225{
20226 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080020227 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090020228 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040020229 unsigned long iflags = 0;
20230 char *fail_msg = NULL;
20231 struct lpfc_sglq *sglq;
James Smart205e8242018-03-05 12:04:03 -080020232 union lpfc_wqe128 wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040020233 uint32_t txq_cnt = 0;
James Smartdc19e3b2018-05-24 21:08:57 -070020234 struct lpfc_queue *wq;
James Smart2a9bf3d2010-06-07 15:24:45 -040020235
James Smartdc19e3b2018-05-24 21:08:57 -070020236 if (phba->link_flag & LS_MDS_LOOPBACK) {
20237 /* MDS WQE are posted only to first WQ*/
James Smartc00f62e2019-08-14 16:57:11 -070020238 wq = phba->sli4_hba.hdwq[0].io_wq;
James Smartdc19e3b2018-05-24 21:08:57 -070020239 if (unlikely(!wq))
20240 return 0;
20241 pring = wq->pring;
20242 } else {
20243 wq = phba->sli4_hba.els_wq;
20244 if (unlikely(!wq))
20245 return 0;
20246 pring = lpfc_phba_elsring(phba);
20247 }
20248
20249 if (unlikely(!pring) || list_empty(&pring->txq))
Dick Kennedy1234a6d2017-09-29 17:34:29 -070020250 return 0;
James Smart895427b2017-02-12 13:52:30 -080020251
James Smart398d81c2013-05-31 17:04:19 -040020252 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050020253 list_for_each_entry(piocbq, &pring->txq, list) {
20254 txq_cnt++;
20255 }
20256
20257 if (txq_cnt > pring->txq_max)
20258 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040020259
James Smart398d81c2013-05-31 17:04:19 -040020260 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040020261
James Smart0e9bb8d2013-03-01 16:35:12 -050020262 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040020263 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040020264
James Smart19ca7602010-11-20 23:11:55 -050020265 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040020266 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040020267 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedy372c1872020-06-30 14:50:00 -070020268 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smarta6298522012-06-12 13:54:11 -040020269 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050020270 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040020271 break;
20272 }
James Smart895427b2017-02-12 13:52:30 -080020273 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040020274 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050020275 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040020276 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040020277 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040020278 }
James Smart0e9bb8d2013-03-01 16:35:12 -050020279 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040020280
20281 /* The xri and iocb resources secured,
20282 * attempt to issue request
20283 */
James Smart6d368e52011-05-24 11:44:12 -040020284 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040020285 piocbq->sli4_xritag = sglq->sli4_xritag;
20286 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
20287 fail_msg = "to convert bpl to sgl";
James Smart205e8242018-03-05 12:04:03 -080020288 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040020289 fail_msg = "to convert iocb to wqe";
James Smartdc19e3b2018-05-24 21:08:57 -070020290 else if (lpfc_sli4_wq_put(wq, &wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040020291 fail_msg = " - Wq is full";
20292 else
20293 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
20294
20295 if (fail_msg) {
20296 /* Failed means we can't issue and need to cancel */
Dick Kennedy372c1872020-06-30 14:50:00 -070020297 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
James Smart2a9bf3d2010-06-07 15:24:45 -040020298 "2822 IOCB failed %s iotag 0x%x "
20299 "xri 0x%x\n",
20300 fail_msg,
20301 piocbq->iotag, piocbq->sli4_xritag);
20302 list_add_tail(&piocbq->list, &completions);
20303 }
James Smart398d81c2013-05-31 17:04:19 -040020304 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040020305 }
20306
James Smart2a9bf3d2010-06-07 15:24:45 -040020307 /* Cancel all the IOCBs that cannot be issued */
20308 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
20309 IOERR_SLI_ABORTED);
20310
James Smart0e9bb8d2013-03-01 16:35:12 -050020311 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040020312}
James Smart895427b2017-02-12 13:52:30 -080020313
20314/**
20315 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
20316 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010020317 * @pwqeq: Pointer to command WQE.
James Smart895427b2017-02-12 13:52:30 -080020318 * @sglq: Pointer to the scatter gather queue object.
20319 *
20320 * This routine converts the bpl or bde that is in the WQE
20321 * to a sgl list for the sli4 hardware. The physical address
20322 * of the bpl/bde is converted back to a virtual address.
20323 * If the WQE contains a BPL then the list of BDE's is
20324 * converted to sli4_sge's. If the WQE contains a single
20325 * BDE then it is converted to a single sli_sge.
20326 * The WQE is still in cpu endianness so the contents of
20327 * the bpl can be used without byte swapping.
20328 *
20329 * Returns valid XRI = Success, NO_XRI = Failure.
20330 */
20331static uint16_t
20332lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
20333 struct lpfc_sglq *sglq)
20334{
20335 uint16_t xritag = NO_XRI;
20336 struct ulp_bde64 *bpl = NULL;
20337 struct ulp_bde64 bde;
20338 struct sli4_sge *sgl = NULL;
20339 struct lpfc_dmabuf *dmabuf;
James Smart205e8242018-03-05 12:04:03 -080020340 union lpfc_wqe128 *wqe;
James Smart895427b2017-02-12 13:52:30 -080020341 int numBdes = 0;
20342 int i = 0;
20343 uint32_t offset = 0; /* accumulated offset in the sg request list */
20344 int inbound = 0; /* number of sg reply entries inbound from firmware */
20345 uint32_t cmd;
20346
20347 if (!pwqeq || !sglq)
20348 return xritag;
20349
20350 sgl = (struct sli4_sge *)sglq->sgl;
20351 wqe = &pwqeq->wqe;
20352 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
20353
20354 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
20355 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
20356 return sglq->sli4_xritag;
20357 numBdes = pwqeq->rsvd2;
20358 if (numBdes) {
20359 /* The addrHigh and addrLow fields within the WQE
20360 * have not been byteswapped yet so there is no
20361 * need to swap them back.
20362 */
20363 if (pwqeq->context3)
20364 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
20365 else
20366 return xritag;
20367
20368 bpl = (struct ulp_bde64 *)dmabuf->virt;
20369 if (!bpl)
20370 return xritag;
20371
20372 for (i = 0; i < numBdes; i++) {
20373 /* Should already be byte swapped. */
20374 sgl->addr_hi = bpl->addrHigh;
20375 sgl->addr_lo = bpl->addrLow;
20376
20377 sgl->word2 = le32_to_cpu(sgl->word2);
20378 if ((i+1) == numBdes)
20379 bf_set(lpfc_sli4_sge_last, sgl, 1);
20380 else
20381 bf_set(lpfc_sli4_sge_last, sgl, 0);
20382 /* swap the size field back to the cpu so we
20383 * can assign it to the sgl.
20384 */
20385 bde.tus.w = le32_to_cpu(bpl->tus.w);
20386 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
20387 /* The offsets in the sgl need to be accumulated
20388 * separately for the request and reply lists.
20389 * The request is always first, the reply follows.
20390 */
20391 switch (cmd) {
20392 case CMD_GEN_REQUEST64_WQE:
20393 /* add up the reply sg entries */
20394 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
20395 inbound++;
20396 /* first inbound? reset the offset */
20397 if (inbound == 1)
20398 offset = 0;
20399 bf_set(lpfc_sli4_sge_offset, sgl, offset);
20400 bf_set(lpfc_sli4_sge_type, sgl,
20401 LPFC_SGE_TYPE_DATA);
20402 offset += bde.tus.f.bdeSize;
20403 break;
20404 case CMD_FCP_TRSP64_WQE:
20405 bf_set(lpfc_sli4_sge_offset, sgl, 0);
20406 bf_set(lpfc_sli4_sge_type, sgl,
20407 LPFC_SGE_TYPE_DATA);
20408 break;
20409 case CMD_FCP_TSEND64_WQE:
20410 case CMD_FCP_TRECEIVE64_WQE:
20411 bf_set(lpfc_sli4_sge_type, sgl,
20412 bpl->tus.f.bdeFlags);
20413 if (i < 3)
20414 offset = 0;
20415 else
20416 offset += bde.tus.f.bdeSize;
20417 bf_set(lpfc_sli4_sge_offset, sgl, offset);
20418 break;
20419 }
20420 sgl->word2 = cpu_to_le32(sgl->word2);
20421 bpl++;
20422 sgl++;
20423 }
20424 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
20425 /* The addrHigh and addrLow fields of the BDE have not
20426 * been byteswapped yet so they need to be swapped
20427 * before putting them in the sgl.
20428 */
20429 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
20430 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
20431 sgl->word2 = le32_to_cpu(sgl->word2);
20432 bf_set(lpfc_sli4_sge_last, sgl, 1);
20433 sgl->word2 = cpu_to_le32(sgl->word2);
20434 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
20435 }
20436 return sglq->sli4_xritag;
20437}
20438
20439/**
20440 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
20441 * @phba: Pointer to HBA context object.
Lee Jones7af29d42020-07-21 17:41:31 +010020442 * @qp: Pointer to HDW queue.
James Smart895427b2017-02-12 13:52:30 -080020443 * @pwqe: Pointer to command WQE.
20444 **/
20445int
James Smart1fbf9742019-01-28 11:14:26 -080020446lpfc_sli4_issue_wqe(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
James Smart895427b2017-02-12 13:52:30 -080020447 struct lpfc_iocbq *pwqe)
20448{
James Smart205e8242018-03-05 12:04:03 -080020449 union lpfc_wqe128 *wqe = &pwqe->wqe;
James Smart7cacae22020-03-31 09:50:03 -070020450 struct lpfc_async_xchg_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080020451 struct lpfc_queue *wq;
20452 struct lpfc_sglq *sglq;
20453 struct lpfc_sli_ring *pring;
20454 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070020455 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080020456
20457 /* NVME_LS and NVME_LS ABTS requests. */
20458 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
20459 pring = phba->sli4_hba.nvmels_wq->pring;
James Smart6a828b02019-01-28 11:14:31 -080020460 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20461 qp, wq_access);
James Smart895427b2017-02-12 13:52:30 -080020462 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
20463 if (!sglq) {
20464 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20465 return WQE_BUSY;
20466 }
20467 pwqe->sli4_lxritag = sglq->sli4_lxritag;
20468 pwqe->sli4_xritag = sglq->sli4_xritag;
20469 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
20470 spin_unlock_irqrestore(&pring->ring_lock, iflags);
20471 return WQE_ERROR;
20472 }
20473 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
20474 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070020475 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
20476 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080020477 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070020478 return ret;
James Smart895427b2017-02-12 13:52:30 -080020479 }
Dick Kennedycd22d602017-08-23 16:55:35 -070020480
James Smart895427b2017-02-12 13:52:30 -080020481 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20482 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080020483
20484 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smart895427b2017-02-12 13:52:30 -080020485 return 0;
20486 }
20487
20488 /* NVME_FCREQ and NVME_ABTS requests */
James Smartda255e22020-11-15 11:26:42 -080020489 if (pwqe->iocb_flag & LPFC_IO_NVME ||
20490 pwqe->iocb_flag & LPFC_IO_FCP) {
James Smart895427b2017-02-12 13:52:30 -080020491 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070020492 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080020493 pring = wq->pring;
James Smart895427b2017-02-12 13:52:30 -080020494
James Smartc00f62e2019-08-14 16:57:11 -070020495 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart895427b2017-02-12 13:52:30 -080020496
James Smart6a828b02019-01-28 11:14:31 -080020497 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20498 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070020499 ret = lpfc_sli4_wq_put(wq, wqe);
20500 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080020501 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070020502 return ret;
James Smart895427b2017-02-12 13:52:30 -080020503 }
20504 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20505 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080020506
20507 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smart895427b2017-02-12 13:52:30 -080020508 return 0;
20509 }
20510
James Smartf358dd02017-02-12 13:52:34 -080020511 /* NVMET requests */
20512 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
20513 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
James Smartc00f62e2019-08-14 16:57:11 -070020514 wq = qp->io_wq;
James Smart1fbf9742019-01-28 11:14:26 -080020515 pring = wq->pring;
James Smartf358dd02017-02-12 13:52:34 -080020516
James Smartf358dd02017-02-12 13:52:34 -080020517 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070020518 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080020519 if (pwqe->sli4_xritag == NO_XRI) {
20520 pwqe->sli4_lxritag = sglq->sli4_lxritag;
20521 pwqe->sli4_xritag = sglq->sli4_xritag;
20522 }
20523 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
20524 pwqe->sli4_xritag);
James Smartc00f62e2019-08-14 16:57:11 -070020525 bf_set(wqe_cqid, &wqe->generic.wqe_com, qp->io_cq_map);
James Smart1fbf9742019-01-28 11:14:26 -080020526
James Smart6a828b02019-01-28 11:14:31 -080020527 lpfc_qp_spin_lock_irqsave(&pring->ring_lock, iflags,
20528 qp, wq_access);
Dick Kennedycd22d602017-08-23 16:55:35 -070020529 ret = lpfc_sli4_wq_put(wq, wqe);
20530 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080020531 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070020532 return ret;
James Smartf358dd02017-02-12 13:52:34 -080020533 }
20534 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
20535 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart93a4d6f2019-11-04 16:57:05 -080020536
20537 lpfc_sli4_poll_eq(qp->hba_eq, LPFC_POLL_FASTPATH);
James Smartf358dd02017-02-12 13:52:34 -080020538 return 0;
20539 }
James Smart895427b2017-02-12 13:52:30 -080020540 return WQE_ERROR;
20541}
James Smartc4908502019-01-28 11:14:28 -080020542
James Smartdb7531d2020-11-15 11:26:44 -080020543/**
20544 * lpfc_sli4_issue_abort_iotag - SLI-4 WQE init & issue for the Abort
20545 * @phba: Pointer to HBA context object.
20546 * @cmdiocb: Pointer to driver command iocb object.
20547 * @cmpl: completion function.
20548 *
20549 * Fill the appropriate fields for the abort WQE and call
20550 * internal routine lpfc_sli4_issue_wqe to send the WQE
20551 * This function is called with hbalock held and no ring_lock held.
20552 *
20553 * RETURNS 0 - SUCCESS
20554 **/
20555
20556int
20557lpfc_sli4_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
20558 void *cmpl)
20559{
20560 struct lpfc_vport *vport = cmdiocb->vport;
20561 struct lpfc_iocbq *abtsiocb = NULL;
20562 union lpfc_wqe128 *abtswqe;
20563 struct lpfc_io_buf *lpfc_cmd;
20564 int retval = IOCB_ERROR;
20565 u16 xritag = cmdiocb->sli4_xritag;
20566
20567 /*
20568 * The scsi command can not be in txq and it is in flight because the
20569 * pCmd is still pointing at the SCSI command we have to abort. There
20570 * is no need to search the txcmplq. Just send an abort to the FW.
20571 */
20572
20573 abtsiocb = __lpfc_sli_get_iocbq(phba);
20574 if (!abtsiocb)
20575 return WQE_NORESOURCE;
20576
20577 /* Indicate the IO is being aborted by the driver. */
20578 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
20579
20580 abtswqe = &abtsiocb->wqe;
20581 memset(abtswqe, 0, sizeof(*abtswqe));
20582
20583 if (lpfc_is_link_up(phba))
20584 bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 1);
20585 else
20586 bf_set(abort_cmd_ia, &abtswqe->abort_cmd, 0);
20587 bf_set(abort_cmd_criteria, &abtswqe->abort_cmd, T_XRI_TAG);
20588 abtswqe->abort_cmd.rsrvd5 = 0;
20589 abtswqe->abort_cmd.wqe_com.abort_tag = xritag;
20590 bf_set(wqe_reqtag, &abtswqe->abort_cmd.wqe_com, abtsiocb->iotag);
20591 bf_set(wqe_cmnd, &abtswqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
20592 bf_set(wqe_xri_tag, &abtswqe->generic.wqe_com, 0);
20593 bf_set(wqe_qosd, &abtswqe->abort_cmd.wqe_com, 1);
20594 bf_set(wqe_lenloc, &abtswqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
20595 bf_set(wqe_cmd_type, &abtswqe->abort_cmd.wqe_com, OTHER_COMMAND);
20596
20597 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
20598 abtsiocb->hba_wqidx = cmdiocb->hba_wqidx;
20599 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
20600 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
20601 abtsiocb->iocb_flag |= LPFC_IO_FCP;
20602 if (cmdiocb->iocb_flag & LPFC_IO_NVME)
20603 abtsiocb->iocb_flag |= LPFC_IO_NVME;
20604 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
20605 abtsiocb->iocb_flag |= LPFC_IO_FOF;
20606 abtsiocb->vport = vport;
20607 abtsiocb->wqe_cmpl = cmpl;
20608
20609 lpfc_cmd = container_of(cmdiocb, struct lpfc_io_buf, cur_iocbq);
20610 retval = lpfc_sli4_issue_wqe(phba, lpfc_cmd->hdwq, abtsiocb);
20611
20612 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NVME_ABTS | LOG_FCP,
20613 "0359 Abort xri x%x, original iotag x%x, "
20614 "abort cmd iotag x%x retval x%x\n",
20615 xritag, cmdiocb->iotag, abtsiocb->iotag, retval);
20616
20617 if (retval) {
20618 cmdiocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
20619 __lpfc_sli_release_iocbq(phba, abtsiocb);
20620 }
20621
20622 return retval;
20623}
20624
James Smartc4908502019-01-28 11:14:28 -080020625#ifdef LPFC_MXP_STAT
20626/**
20627 * lpfc_snapshot_mxp - Snapshot pbl, pvt and busy count
20628 * @phba: pointer to lpfc hba data structure.
20629 * @hwqid: belong to which HWQ.
20630 *
20631 * The purpose of this routine is to take a snapshot of pbl, pvt and busy count
20632 * 15 seconds after a test case is running.
20633 *
20634 * The user should call lpfc_debugfs_multixripools_write before running a test
20635 * case to clear stat_snapshot_taken. Then the user starts a test case. During
20636 * test case is running, stat_snapshot_taken is incremented by 1 every time when
20637 * this routine is called from heartbeat timer. When stat_snapshot_taken is
20638 * equal to LPFC_MXP_SNAPSHOT_TAKEN, a snapshot is taken.
20639 **/
20640void lpfc_snapshot_mxp(struct lpfc_hba *phba, u32 hwqid)
20641{
20642 struct lpfc_sli4_hdw_queue *qp;
20643 struct lpfc_multixri_pool *multixri_pool;
20644 struct lpfc_pvt_pool *pvt_pool;
20645 struct lpfc_pbl_pool *pbl_pool;
20646 u32 txcmplq_cnt;
20647
20648 qp = &phba->sli4_hba.hdwq[hwqid];
20649 multixri_pool = qp->p_multixri_pool;
20650 if (!multixri_pool)
20651 return;
20652
20653 if (multixri_pool->stat_snapshot_taken == LPFC_MXP_SNAPSHOT_TAKEN) {
20654 pvt_pool = &qp->p_multixri_pool->pvt_pool;
20655 pbl_pool = &qp->p_multixri_pool->pbl_pool;
James Smartc00f62e2019-08-14 16:57:11 -070020656 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080020657
20658 multixri_pool->stat_pbl_count = pbl_pool->count;
20659 multixri_pool->stat_pvt_count = pvt_pool->count;
20660 multixri_pool->stat_busy_count = txcmplq_cnt;
20661 }
20662
20663 multixri_pool->stat_snapshot_taken++;
20664}
20665#endif
20666
20667/**
20668 * lpfc_adjust_pvt_pool_count - Adjust private pool count
20669 * @phba: pointer to lpfc hba data structure.
20670 * @hwqid: belong to which HWQ.
20671 *
20672 * This routine moves some XRIs from private to public pool when private pool
20673 * is not busy.
20674 **/
20675void lpfc_adjust_pvt_pool_count(struct lpfc_hba *phba, u32 hwqid)
20676{
20677 struct lpfc_multixri_pool *multixri_pool;
20678 u32 io_req_count;
20679 u32 prev_io_req_count;
20680
20681 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20682 if (!multixri_pool)
20683 return;
20684 io_req_count = multixri_pool->io_req_count;
20685 prev_io_req_count = multixri_pool->prev_io_req_count;
20686
20687 if (prev_io_req_count != io_req_count) {
20688 /* Private pool is busy */
20689 multixri_pool->prev_io_req_count = io_req_count;
20690 } else {
20691 /* Private pool is not busy.
20692 * Move XRIs from private to public pool.
20693 */
20694 lpfc_move_xri_pvt_to_pbl(phba, hwqid);
20695 }
20696}
20697
20698/**
20699 * lpfc_adjust_high_watermark - Adjust high watermark
20700 * @phba: pointer to lpfc hba data structure.
20701 * @hwqid: belong to which HWQ.
20702 *
20703 * This routine sets high watermark as number of outstanding XRIs,
20704 * but make sure the new value is between xri_limit/2 and xri_limit.
20705 **/
20706void lpfc_adjust_high_watermark(struct lpfc_hba *phba, u32 hwqid)
20707{
20708 u32 new_watermark;
20709 u32 watermark_max;
20710 u32 watermark_min;
20711 u32 xri_limit;
20712 u32 txcmplq_cnt;
20713 u32 abts_io_bufs;
20714 struct lpfc_multixri_pool *multixri_pool;
20715 struct lpfc_sli4_hdw_queue *qp;
20716
20717 qp = &phba->sli4_hba.hdwq[hwqid];
20718 multixri_pool = qp->p_multixri_pool;
20719 if (!multixri_pool)
20720 return;
20721 xri_limit = multixri_pool->xri_limit;
20722
20723 watermark_max = xri_limit;
20724 watermark_min = xri_limit / 2;
20725
James Smartc00f62e2019-08-14 16:57:11 -070020726 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080020727 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070020728 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080020729
20730 new_watermark = txcmplq_cnt + abts_io_bufs;
20731 new_watermark = min(watermark_max, new_watermark);
20732 new_watermark = max(watermark_min, new_watermark);
20733 multixri_pool->pvt_pool.high_watermark = new_watermark;
20734
20735#ifdef LPFC_MXP_STAT
20736 multixri_pool->stat_max_hwm = max(multixri_pool->stat_max_hwm,
20737 new_watermark);
20738#endif
20739}
20740
20741/**
20742 * lpfc_move_xri_pvt_to_pbl - Move some XRIs from private to public pool
20743 * @phba: pointer to lpfc hba data structure.
20744 * @hwqid: belong to which HWQ.
20745 *
20746 * This routine is called from hearbeat timer when pvt_pool is idle.
20747 * All free XRIs are moved from private to public pool on hwqid with 2 steps.
20748 * The first step moves (all - low_watermark) amount of XRIs.
20749 * The second step moves the rest of XRIs.
20750 **/
20751void lpfc_move_xri_pvt_to_pbl(struct lpfc_hba *phba, u32 hwqid)
20752{
20753 struct lpfc_pbl_pool *pbl_pool;
20754 struct lpfc_pvt_pool *pvt_pool;
James Smart6a828b02019-01-28 11:14:31 -080020755 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080020756 struct lpfc_io_buf *lpfc_ncmd;
20757 struct lpfc_io_buf *lpfc_ncmd_next;
20758 unsigned long iflag;
20759 struct list_head tmp_list;
20760 u32 tmp_count;
20761
James Smart6a828b02019-01-28 11:14:31 -080020762 qp = &phba->sli4_hba.hdwq[hwqid];
20763 pbl_pool = &qp->p_multixri_pool->pbl_pool;
20764 pvt_pool = &qp->p_multixri_pool->pvt_pool;
James Smartc4908502019-01-28 11:14:28 -080020765 tmp_count = 0;
20766
James Smart6a828b02019-01-28 11:14:31 -080020767 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag, qp, mv_to_pub_pool);
20768 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_from_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020769
20770 if (pvt_pool->count > pvt_pool->low_watermark) {
20771 /* Step 1: move (all - low_watermark) from pvt_pool
20772 * to pbl_pool
20773 */
20774
20775 /* Move low watermark of bufs from pvt_pool to tmp_list */
20776 INIT_LIST_HEAD(&tmp_list);
20777 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
20778 &pvt_pool->list, list) {
20779 list_move_tail(&lpfc_ncmd->list, &tmp_list);
20780 tmp_count++;
20781 if (tmp_count >= pvt_pool->low_watermark)
20782 break;
20783 }
20784
20785 /* Move all bufs from pvt_pool to pbl_pool */
20786 list_splice_init(&pvt_pool->list, &pbl_pool->list);
20787
20788 /* Move all bufs from tmp_list to pvt_pool */
20789 list_splice(&tmp_list, &pvt_pool->list);
20790
20791 pbl_pool->count += (pvt_pool->count - tmp_count);
20792 pvt_pool->count = tmp_count;
20793 } else {
20794 /* Step 2: move the rest from pvt_pool to pbl_pool */
20795 list_splice_init(&pvt_pool->list, &pbl_pool->list);
20796 pbl_pool->count += pvt_pool->count;
20797 pvt_pool->count = 0;
20798 }
20799
20800 spin_unlock(&pvt_pool->lock);
20801 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20802}
20803
20804/**
20805 * _lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
20806 * @phba: pointer to lpfc hba data structure
Lee Jones7af29d42020-07-21 17:41:31 +010020807 * @qp: pointer to HDW queue
James Smartc4908502019-01-28 11:14:28 -080020808 * @pbl_pool: specified public free XRI pool
20809 * @pvt_pool: specified private free XRI pool
20810 * @count: number of XRIs to move
20811 *
20812 * This routine tries to move some free common bufs from the specified pbl_pool
20813 * to the specified pvt_pool. It might move less than count XRIs if there's not
20814 * enough in public pool.
20815 *
20816 * Return:
20817 * true - if XRIs are successfully moved from the specified pbl_pool to the
20818 * specified pvt_pool
20819 * false - if the specified pbl_pool is empty or locked by someone else
20820 **/
20821static bool
James Smart6a828b02019-01-28 11:14:31 -080020822_lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, struct lpfc_sli4_hdw_queue *qp,
20823 struct lpfc_pbl_pool *pbl_pool,
James Smartc4908502019-01-28 11:14:28 -080020824 struct lpfc_pvt_pool *pvt_pool, u32 count)
20825{
20826 struct lpfc_io_buf *lpfc_ncmd;
20827 struct lpfc_io_buf *lpfc_ncmd_next;
20828 unsigned long iflag;
20829 int ret;
20830
20831 ret = spin_trylock_irqsave(&pbl_pool->lock, iflag);
20832 if (ret) {
20833 if (pbl_pool->count) {
20834 /* Move a batch of XRIs from public to private pool */
James Smart6a828b02019-01-28 11:14:31 -080020835 lpfc_qp_spin_lock(&pvt_pool->lock, qp, mv_to_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080020836 list_for_each_entry_safe(lpfc_ncmd,
20837 lpfc_ncmd_next,
20838 &pbl_pool->list,
20839 list) {
20840 list_move_tail(&lpfc_ncmd->list,
20841 &pvt_pool->list);
20842 pvt_pool->count++;
20843 pbl_pool->count--;
20844 count--;
20845 if (count == 0)
20846 break;
20847 }
20848
20849 spin_unlock(&pvt_pool->lock);
20850 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20851 return true;
20852 }
20853 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
20854 }
20855
20856 return false;
20857}
20858
20859/**
20860 * lpfc_move_xri_pbl_to_pvt - Move some XRIs from public to private pool
20861 * @phba: pointer to lpfc hba data structure.
20862 * @hwqid: belong to which HWQ.
20863 * @count: number of XRIs to move
20864 *
20865 * This routine tries to find some free common bufs in one of public pools with
20866 * Round Robin method. The search always starts from local hwqid, then the next
20867 * HWQ which was found last time (rrb_next_hwqid). Once a public pool is found,
20868 * a batch of free common bufs are moved to private pool on hwqid.
20869 * It might move less than count XRIs if there's not enough in public pool.
20870 **/
20871void lpfc_move_xri_pbl_to_pvt(struct lpfc_hba *phba, u32 hwqid, u32 count)
20872{
20873 struct lpfc_multixri_pool *multixri_pool;
20874 struct lpfc_multixri_pool *next_multixri_pool;
20875 struct lpfc_pvt_pool *pvt_pool;
20876 struct lpfc_pbl_pool *pbl_pool;
James Smart6a828b02019-01-28 11:14:31 -080020877 struct lpfc_sli4_hdw_queue *qp;
James Smartc4908502019-01-28 11:14:28 -080020878 u32 next_hwqid;
20879 u32 hwq_count;
20880 int ret;
20881
James Smart6a828b02019-01-28 11:14:31 -080020882 qp = &phba->sli4_hba.hdwq[hwqid];
20883 multixri_pool = qp->p_multixri_pool;
James Smartc4908502019-01-28 11:14:28 -080020884 pvt_pool = &multixri_pool->pvt_pool;
20885 pbl_pool = &multixri_pool->pbl_pool;
20886
20887 /* Check if local pbl_pool is available */
James Smart6a828b02019-01-28 11:14:31 -080020888 ret = _lpfc_move_xri_pbl_to_pvt(phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020889 if (ret) {
20890#ifdef LPFC_MXP_STAT
20891 multixri_pool->local_pbl_hit_count++;
20892#endif
20893 return;
20894 }
20895
20896 hwq_count = phba->cfg_hdw_queue;
20897
20898 /* Get the next hwqid which was found last time */
20899 next_hwqid = multixri_pool->rrb_next_hwqid;
20900
20901 do {
20902 /* Go to next hwq */
20903 next_hwqid = (next_hwqid + 1) % hwq_count;
20904
20905 next_multixri_pool =
20906 phba->sli4_hba.hdwq[next_hwqid].p_multixri_pool;
20907 pbl_pool = &next_multixri_pool->pbl_pool;
20908
20909 /* Check if the public free xri pool is available */
20910 ret = _lpfc_move_xri_pbl_to_pvt(
James Smart6a828b02019-01-28 11:14:31 -080020911 phba, qp, pbl_pool, pvt_pool, count);
James Smartc4908502019-01-28 11:14:28 -080020912
20913 /* Exit while-loop if success or all hwqid are checked */
20914 } while (!ret && next_hwqid != multixri_pool->rrb_next_hwqid);
20915
20916 /* Starting point for the next time */
20917 multixri_pool->rrb_next_hwqid = next_hwqid;
20918
20919 if (!ret) {
20920 /* stats: all public pools are empty*/
20921 multixri_pool->pbl_empty_count++;
20922 }
20923
20924#ifdef LPFC_MXP_STAT
20925 if (ret) {
20926 if (next_hwqid == hwqid)
20927 multixri_pool->local_pbl_hit_count++;
20928 else
20929 multixri_pool->other_pbl_hit_count++;
20930 }
20931#endif
20932}
20933
20934/**
20935 * lpfc_keep_pvt_pool_above_lowwm - Keep pvt_pool above low watermark
20936 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010020937 * @hwqid: belong to which HWQ.
James Smartc4908502019-01-28 11:14:28 -080020938 *
20939 * This routine get a batch of XRIs from pbl_pool if pvt_pool is less than
20940 * low watermark.
20941 **/
20942void lpfc_keep_pvt_pool_above_lowwm(struct lpfc_hba *phba, u32 hwqid)
20943{
20944 struct lpfc_multixri_pool *multixri_pool;
20945 struct lpfc_pvt_pool *pvt_pool;
20946
20947 multixri_pool = phba->sli4_hba.hdwq[hwqid].p_multixri_pool;
20948 pvt_pool = &multixri_pool->pvt_pool;
20949
20950 if (pvt_pool->count < pvt_pool->low_watermark)
20951 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
20952}
20953
20954/**
20955 * lpfc_release_io_buf - Return one IO buf back to free pool
20956 * @phba: pointer to lpfc hba data structure.
20957 * @lpfc_ncmd: IO buf to be returned.
20958 * @qp: belong to which HWQ.
20959 *
20960 * This routine returns one IO buf back to free pool. If this is an urgent IO,
20961 * the IO buf is returned to expedite pool. If cfg_xri_rebalancing==1,
20962 * the IO buf is returned to pbl_pool or pvt_pool based on watermark and
20963 * xri_limit. If cfg_xri_rebalancing==0, the IO buf is returned to
20964 * lpfc_io_buf_list_put.
20965 **/
20966void lpfc_release_io_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd,
20967 struct lpfc_sli4_hdw_queue *qp)
20968{
20969 unsigned long iflag;
20970 struct lpfc_pbl_pool *pbl_pool;
20971 struct lpfc_pvt_pool *pvt_pool;
20972 struct lpfc_epd_pool *epd_pool;
20973 u32 txcmplq_cnt;
20974 u32 xri_owned;
20975 u32 xri_limit;
20976 u32 abts_io_bufs;
20977
20978 /* MUST zero fields if buffer is reused by another protocol */
20979 lpfc_ncmd->nvmeCmd = NULL;
20980 lpfc_ncmd->cur_iocbq.wqe_cmpl = NULL;
20981 lpfc_ncmd->cur_iocbq.iocb_cmpl = NULL;
20982
James Smart35a635a2019-09-21 20:59:02 -070020983 if (phba->cfg_xpsgl && !phba->nvmet_support &&
20984 !list_empty(&lpfc_ncmd->dma_sgl_xtra_list))
20985 lpfc_put_sgl_per_hdwq(phba, lpfc_ncmd);
20986
20987 if (!list_empty(&lpfc_ncmd->dma_cmd_rsp_list))
20988 lpfc_put_cmd_rsp_buf_per_hdwq(phba, lpfc_ncmd);
20989
James Smartc4908502019-01-28 11:14:28 -080020990 if (phba->cfg_xri_rebalancing) {
20991 if (lpfc_ncmd->expedite) {
20992 /* Return to expedite pool */
20993 epd_pool = &phba->epd_pool;
20994 spin_lock_irqsave(&epd_pool->lock, iflag);
20995 list_add_tail(&lpfc_ncmd->list, &epd_pool->list);
20996 epd_pool->count++;
20997 spin_unlock_irqrestore(&epd_pool->lock, iflag);
20998 return;
20999 }
21000
21001 /* Avoid invalid access if an IO sneaks in and is being rejected
21002 * just _after_ xri pools are destroyed in lpfc_offline.
21003 * Nothing much can be done at this point.
21004 */
21005 if (!qp->p_multixri_pool)
21006 return;
21007
21008 pbl_pool = &qp->p_multixri_pool->pbl_pool;
21009 pvt_pool = &qp->p_multixri_pool->pvt_pool;
21010
James Smartc00f62e2019-08-14 16:57:11 -070021011 txcmplq_cnt = qp->io_wq->pring->txcmplq_cnt;
James Smartc4908502019-01-28 11:14:28 -080021012 abts_io_bufs = qp->abts_scsi_io_bufs;
James Smartc00f62e2019-08-14 16:57:11 -070021013 abts_io_bufs += qp->abts_nvme_io_bufs;
James Smartc4908502019-01-28 11:14:28 -080021014
21015 xri_owned = pvt_pool->count + txcmplq_cnt + abts_io_bufs;
21016 xri_limit = qp->p_multixri_pool->xri_limit;
21017
21018#ifdef LPFC_MXP_STAT
21019 if (xri_owned <= xri_limit)
21020 qp->p_multixri_pool->below_limit_count++;
21021 else
21022 qp->p_multixri_pool->above_limit_count++;
21023#endif
21024
21025 /* XRI goes to either public or private free xri pool
21026 * based on watermark and xri_limit
21027 */
21028 if ((pvt_pool->count < pvt_pool->low_watermark) ||
21029 (xri_owned < xri_limit &&
21030 pvt_pool->count < pvt_pool->high_watermark)) {
James Smart6a828b02019-01-28 11:14:31 -080021031 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag,
21032 qp, free_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080021033 list_add_tail(&lpfc_ncmd->list,
21034 &pvt_pool->list);
21035 pvt_pool->count++;
21036 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21037 } else {
James Smart6a828b02019-01-28 11:14:31 -080021038 lpfc_qp_spin_lock_irqsave(&pbl_pool->lock, iflag,
21039 qp, free_pub_pool);
James Smartc4908502019-01-28 11:14:28 -080021040 list_add_tail(&lpfc_ncmd->list,
21041 &pbl_pool->list);
21042 pbl_pool->count++;
21043 spin_unlock_irqrestore(&pbl_pool->lock, iflag);
21044 }
21045 } else {
James Smart6a828b02019-01-28 11:14:31 -080021046 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_put_lock, iflag,
21047 qp, free_xri);
James Smartc4908502019-01-28 11:14:28 -080021048 list_add_tail(&lpfc_ncmd->list,
21049 &qp->lpfc_io_buf_list_put);
21050 qp->put_io_bufs++;
21051 spin_unlock_irqrestore(&qp->io_buf_list_put_lock,
21052 iflag);
21053 }
21054}
21055
21056/**
21057 * lpfc_get_io_buf_from_private_pool - Get one free IO buf from private pool
21058 * @phba: pointer to lpfc hba data structure.
Lee Jones7af29d42020-07-21 17:41:31 +010021059 * @qp: pointer to HDW queue
James Smartc4908502019-01-28 11:14:28 -080021060 * @pvt_pool: pointer to private pool data structure.
21061 * @ndlp: pointer to lpfc nodelist data structure.
21062 *
21063 * This routine tries to get one free IO buf from private pool.
21064 *
21065 * Return:
21066 * pointer to one free IO buf - if private pool is not empty
21067 * NULL - if private pool is empty
21068 **/
21069static struct lpfc_io_buf *
21070lpfc_get_io_buf_from_private_pool(struct lpfc_hba *phba,
James Smart6a828b02019-01-28 11:14:31 -080021071 struct lpfc_sli4_hdw_queue *qp,
James Smartc4908502019-01-28 11:14:28 -080021072 struct lpfc_pvt_pool *pvt_pool,
21073 struct lpfc_nodelist *ndlp)
21074{
21075 struct lpfc_io_buf *lpfc_ncmd;
21076 struct lpfc_io_buf *lpfc_ncmd_next;
21077 unsigned long iflag;
21078
James Smart6a828b02019-01-28 11:14:31 -080021079 lpfc_qp_spin_lock_irqsave(&pvt_pool->lock, iflag, qp, alloc_pvt_pool);
James Smartc4908502019-01-28 11:14:28 -080021080 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21081 &pvt_pool->list, list) {
21082 if (lpfc_test_rrq_active(
21083 phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
21084 continue;
21085 list_del(&lpfc_ncmd->list);
21086 pvt_pool->count--;
21087 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21088 return lpfc_ncmd;
21089 }
21090 spin_unlock_irqrestore(&pvt_pool->lock, iflag);
21091
21092 return NULL;
21093}
21094
21095/**
21096 * lpfc_get_io_buf_from_expedite_pool - Get one free IO buf from expedite pool
21097 * @phba: pointer to lpfc hba data structure.
21098 *
21099 * This routine tries to get one free IO buf from expedite pool.
21100 *
21101 * Return:
21102 * pointer to one free IO buf - if expedite pool is not empty
21103 * NULL - if expedite pool is empty
21104 **/
21105static struct lpfc_io_buf *
21106lpfc_get_io_buf_from_expedite_pool(struct lpfc_hba *phba)
21107{
21108 struct lpfc_io_buf *lpfc_ncmd;
21109 struct lpfc_io_buf *lpfc_ncmd_next;
21110 unsigned long iflag;
21111 struct lpfc_epd_pool *epd_pool;
21112
21113 epd_pool = &phba->epd_pool;
21114 lpfc_ncmd = NULL;
21115
21116 spin_lock_irqsave(&epd_pool->lock, iflag);
21117 if (epd_pool->count > 0) {
21118 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
21119 &epd_pool->list, list) {
21120 list_del(&lpfc_ncmd->list);
21121 epd_pool->count--;
21122 break;
21123 }
21124 }
21125 spin_unlock_irqrestore(&epd_pool->lock, iflag);
21126
21127 return lpfc_ncmd;
21128}
21129
21130/**
21131 * lpfc_get_io_buf_from_multixri_pools - Get one free IO bufs
21132 * @phba: pointer to lpfc hba data structure.
21133 * @ndlp: pointer to lpfc nodelist data structure.
21134 * @hwqid: belong to which HWQ
21135 * @expedite: 1 means this request is urgent.
21136 *
21137 * This routine will do the following actions and then return a pointer to
21138 * one free IO buf.
21139 *
21140 * 1. If private free xri count is empty, move some XRIs from public to
21141 * private pool.
21142 * 2. Get one XRI from private free xri pool.
21143 * 3. If we fail to get one from pvt_pool and this is an expedite request,
21144 * get one free xri from expedite pool.
21145 *
21146 * Note: ndlp is only used on SCSI side for RRQ testing.
21147 * The caller should pass NULL for ndlp on NVME side.
21148 *
21149 * Return:
21150 * pointer to one free IO buf - if private pool is not empty
21151 * NULL - if private pool is empty
21152 **/
21153static struct lpfc_io_buf *
21154lpfc_get_io_buf_from_multixri_pools(struct lpfc_hba *phba,
21155 struct lpfc_nodelist *ndlp,
21156 int hwqid, int expedite)
21157{
21158 struct lpfc_sli4_hdw_queue *qp;
21159 struct lpfc_multixri_pool *multixri_pool;
21160 struct lpfc_pvt_pool *pvt_pool;
21161 struct lpfc_io_buf *lpfc_ncmd;
21162
21163 qp = &phba->sli4_hba.hdwq[hwqid];
21164 lpfc_ncmd = NULL;
21165 multixri_pool = qp->p_multixri_pool;
21166 pvt_pool = &multixri_pool->pvt_pool;
21167 multixri_pool->io_req_count++;
21168
21169 /* If pvt_pool is empty, move some XRIs from public to private pool */
21170 if (pvt_pool->count == 0)
21171 lpfc_move_xri_pbl_to_pvt(phba, hwqid, XRI_BATCH);
21172
21173 /* Get one XRI from private free xri pool */
James Smart6a828b02019-01-28 11:14:31 -080021174 lpfc_ncmd = lpfc_get_io_buf_from_private_pool(phba, qp, pvt_pool, ndlp);
James Smartc4908502019-01-28 11:14:28 -080021175
21176 if (lpfc_ncmd) {
21177 lpfc_ncmd->hdwq = qp;
21178 lpfc_ncmd->hdwq_no = hwqid;
21179 } else if (expedite) {
21180 /* If we fail to get one from pvt_pool and this is an expedite
21181 * request, get one free xri from expedite pool.
21182 */
21183 lpfc_ncmd = lpfc_get_io_buf_from_expedite_pool(phba);
21184 }
21185
21186 return lpfc_ncmd;
21187}
21188
21189static inline struct lpfc_io_buf *
21190lpfc_io_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp, int idx)
21191{
21192 struct lpfc_sli4_hdw_queue *qp;
21193 struct lpfc_io_buf *lpfc_cmd, *lpfc_cmd_next;
21194
21195 qp = &phba->sli4_hba.hdwq[idx];
21196 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
21197 &qp->lpfc_io_buf_list_get, list) {
21198 if (lpfc_test_rrq_active(phba, ndlp,
21199 lpfc_cmd->cur_iocbq.sli4_lxritag))
21200 continue;
21201
21202 if (lpfc_cmd->flags & LPFC_SBUF_NOT_POSTED)
21203 continue;
21204
21205 list_del_init(&lpfc_cmd->list);
21206 qp->get_io_bufs--;
21207 lpfc_cmd->hdwq = qp;
21208 lpfc_cmd->hdwq_no = idx;
21209 return lpfc_cmd;
21210 }
21211 return NULL;
21212}
21213
21214/**
21215 * lpfc_get_io_buf - Get one IO buffer from free pool
21216 * @phba: The HBA for which this call is being executed.
21217 * @ndlp: pointer to lpfc nodelist data structure.
21218 * @hwqid: belong to which HWQ
21219 * @expedite: 1 means this request is urgent.
21220 *
21221 * This routine gets one IO buffer from free pool. If cfg_xri_rebalancing==1,
21222 * removes a IO buffer from multiXRI pools. If cfg_xri_rebalancing==0, removes
21223 * a IO buffer from head of @hdwq io_buf_list and returns to caller.
21224 *
21225 * Note: ndlp is only used on SCSI side for RRQ testing.
21226 * The caller should pass NULL for ndlp on NVME side.
21227 *
21228 * Return codes:
21229 * NULL - Error
21230 * Pointer to lpfc_io_buf - Success
21231 **/
21232struct lpfc_io_buf *lpfc_get_io_buf(struct lpfc_hba *phba,
21233 struct lpfc_nodelist *ndlp,
21234 u32 hwqid, int expedite)
21235{
21236 struct lpfc_sli4_hdw_queue *qp;
21237 unsigned long iflag;
21238 struct lpfc_io_buf *lpfc_cmd;
21239
21240 qp = &phba->sli4_hba.hdwq[hwqid];
21241 lpfc_cmd = NULL;
21242
21243 if (phba->cfg_xri_rebalancing)
21244 lpfc_cmd = lpfc_get_io_buf_from_multixri_pools(
21245 phba, ndlp, hwqid, expedite);
21246 else {
James Smart6a828b02019-01-28 11:14:31 -080021247 lpfc_qp_spin_lock_irqsave(&qp->io_buf_list_get_lock, iflag,
21248 qp, alloc_xri_get);
James Smartc4908502019-01-28 11:14:28 -080021249 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
21250 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
21251 if (!lpfc_cmd) {
James Smart6a828b02019-01-28 11:14:31 -080021252 lpfc_qp_spin_lock(&qp->io_buf_list_put_lock,
21253 qp, alloc_xri_put);
James Smartc4908502019-01-28 11:14:28 -080021254 list_splice(&qp->lpfc_io_buf_list_put,
21255 &qp->lpfc_io_buf_list_get);
21256 qp->get_io_bufs += qp->put_io_bufs;
21257 INIT_LIST_HEAD(&qp->lpfc_io_buf_list_put);
21258 qp->put_io_bufs = 0;
21259 spin_unlock(&qp->io_buf_list_put_lock);
21260 if (qp->get_io_bufs > LPFC_NVME_EXPEDITE_XRICNT ||
21261 expedite)
21262 lpfc_cmd = lpfc_io_buf(phba, ndlp, hwqid);
21263 }
21264 spin_unlock_irqrestore(&qp->io_buf_list_get_lock, iflag);
21265 }
21266
21267 return lpfc_cmd;
21268}
James Smartd79c9e92019-08-14 16:57:09 -070021269
21270/**
21271 * lpfc_get_sgl_per_hdwq - Get one SGL chunk from hdwq's pool
21272 * @phba: The HBA for which this call is being executed.
21273 * @lpfc_buf: IO buf structure to append the SGL chunk
21274 *
21275 * This routine gets one SGL chunk buffer from hdwq's SGL chunk pool,
21276 * and will allocate an SGL chunk if the pool is empty.
21277 *
21278 * Return codes:
21279 * NULL - Error
21280 * Pointer to sli4_hybrid_sgl - Success
21281 **/
21282struct sli4_hybrid_sgl *
21283lpfc_get_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
21284{
21285 struct sli4_hybrid_sgl *list_entry = NULL;
21286 struct sli4_hybrid_sgl *tmp = NULL;
21287 struct sli4_hybrid_sgl *allocated_sgl = NULL;
21288 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21289 struct list_head *buf_list = &hdwq->sgl_list;
James Smarta4c21ac2019-09-21 20:59:01 -070021290 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070021291
James Smarta4c21ac2019-09-21 20:59:01 -070021292 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021293
21294 if (likely(!list_empty(buf_list))) {
21295 /* break off 1 chunk from the sgl_list */
21296 list_for_each_entry_safe(list_entry, tmp,
21297 buf_list, list_node) {
21298 list_move_tail(&list_entry->list_node,
21299 &lpfc_buf->dma_sgl_xtra_list);
21300 break;
21301 }
21302 } else {
21303 /* allocate more */
James Smarta4c21ac2019-09-21 20:59:01 -070021304 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021305 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
James Smart4583a4f2019-11-15 16:38:47 -080021306 cpu_to_node(hdwq->io_wq->chann));
James Smartd79c9e92019-08-14 16:57:09 -070021307 if (!tmp) {
21308 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21309 "8353 error kmalloc memory for HDWQ "
21310 "%d %s\n",
21311 lpfc_buf->hdwq_no, __func__);
21312 return NULL;
21313 }
21314
21315 tmp->dma_sgl = dma_pool_alloc(phba->lpfc_sg_dma_buf_pool,
21316 GFP_ATOMIC, &tmp->dma_phys_sgl);
21317 if (!tmp->dma_sgl) {
21318 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21319 "8354 error pool_alloc memory for HDWQ "
21320 "%d %s\n",
21321 lpfc_buf->hdwq_no, __func__);
21322 kfree(tmp);
21323 return NULL;
21324 }
21325
James Smarta4c21ac2019-09-21 20:59:01 -070021326 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021327 list_add_tail(&tmp->list_node, &lpfc_buf->dma_sgl_xtra_list);
21328 }
21329
21330 allocated_sgl = list_last_entry(&lpfc_buf->dma_sgl_xtra_list,
21331 struct sli4_hybrid_sgl,
21332 list_node);
21333
James Smarta4c21ac2019-09-21 20:59:01 -070021334 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021335
21336 return allocated_sgl;
21337}
21338
21339/**
21340 * lpfc_put_sgl_per_hdwq - Put one SGL chunk into hdwq pool
21341 * @phba: The HBA for which this call is being executed.
21342 * @lpfc_buf: IO buf structure with the SGL chunk
21343 *
21344 * This routine puts one SGL chunk buffer into hdwq's SGL chunk pool.
21345 *
21346 * Return codes:
21347 * 0 - Success
21348 * -EINVAL - Error
21349 **/
21350int
21351lpfc_put_sgl_per_hdwq(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_buf)
21352{
21353 int rc = 0;
21354 struct sli4_hybrid_sgl *list_entry = NULL;
21355 struct sli4_hybrid_sgl *tmp = NULL;
21356 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21357 struct list_head *buf_list = &hdwq->sgl_list;
James Smarta4c21ac2019-09-21 20:59:01 -070021358 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070021359
James Smarta4c21ac2019-09-21 20:59:01 -070021360 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021361
21362 if (likely(!list_empty(&lpfc_buf->dma_sgl_xtra_list))) {
21363 list_for_each_entry_safe(list_entry, tmp,
21364 &lpfc_buf->dma_sgl_xtra_list,
21365 list_node) {
21366 list_move_tail(&list_entry->list_node,
21367 buf_list);
21368 }
21369 } else {
21370 rc = -EINVAL;
21371 }
21372
James Smarta4c21ac2019-09-21 20:59:01 -070021373 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021374 return rc;
21375}
21376
21377/**
21378 * lpfc_free_sgl_per_hdwq - Free all SGL chunks of hdwq pool
21379 * @phba: phba object
21380 * @hdwq: hdwq to cleanup sgl buff resources on
21381 *
21382 * This routine frees all SGL chunks of hdwq SGL chunk pool.
21383 *
21384 * Return codes:
21385 * None
21386 **/
21387void
21388lpfc_free_sgl_per_hdwq(struct lpfc_hba *phba,
21389 struct lpfc_sli4_hdw_queue *hdwq)
21390{
21391 struct list_head *buf_list = &hdwq->sgl_list;
21392 struct sli4_hybrid_sgl *list_entry = NULL;
21393 struct sli4_hybrid_sgl *tmp = NULL;
James Smarta4c21ac2019-09-21 20:59:01 -070021394 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070021395
James Smarta4c21ac2019-09-21 20:59:01 -070021396 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021397
21398 /* Free sgl pool */
21399 list_for_each_entry_safe(list_entry, tmp,
21400 buf_list, list_node) {
21401 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
21402 list_entry->dma_sgl,
21403 list_entry->dma_phys_sgl);
21404 list_del(&list_entry->list_node);
21405 kfree(list_entry);
21406 }
21407
James Smarta4c21ac2019-09-21 20:59:01 -070021408 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021409}
21410
21411/**
21412 * lpfc_get_cmd_rsp_buf_per_hdwq - Get one CMD/RSP buffer from hdwq
21413 * @phba: The HBA for which this call is being executed.
21414 * @lpfc_buf: IO buf structure to attach the CMD/RSP buffer
21415 *
21416 * This routine gets one CMD/RSP buffer from hdwq's CMD/RSP pool,
21417 * and will allocate an CMD/RSP buffer if the pool is empty.
21418 *
21419 * Return codes:
21420 * NULL - Error
21421 * Pointer to fcp_cmd_rsp_buf - Success
21422 **/
21423struct fcp_cmd_rsp_buf *
21424lpfc_get_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21425 struct lpfc_io_buf *lpfc_buf)
21426{
21427 struct fcp_cmd_rsp_buf *list_entry = NULL;
21428 struct fcp_cmd_rsp_buf *tmp = NULL;
21429 struct fcp_cmd_rsp_buf *allocated_buf = NULL;
21430 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21431 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
James Smarta4c21ac2019-09-21 20:59:01 -070021432 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070021433
James Smarta4c21ac2019-09-21 20:59:01 -070021434 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021435
21436 if (likely(!list_empty(buf_list))) {
21437 /* break off 1 chunk from the list */
21438 list_for_each_entry_safe(list_entry, tmp,
21439 buf_list,
21440 list_node) {
21441 list_move_tail(&list_entry->list_node,
21442 &lpfc_buf->dma_cmd_rsp_list);
21443 break;
21444 }
21445 } else {
21446 /* allocate more */
James Smarta4c21ac2019-09-21 20:59:01 -070021447 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021448 tmp = kmalloc_node(sizeof(*tmp), GFP_ATOMIC,
James Smart4583a4f2019-11-15 16:38:47 -080021449 cpu_to_node(hdwq->io_wq->chann));
James Smartd79c9e92019-08-14 16:57:09 -070021450 if (!tmp) {
21451 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21452 "8355 error kmalloc memory for HDWQ "
21453 "%d %s\n",
21454 lpfc_buf->hdwq_no, __func__);
21455 return NULL;
21456 }
21457
21458 tmp->fcp_cmnd = dma_pool_alloc(phba->lpfc_cmd_rsp_buf_pool,
21459 GFP_ATOMIC,
21460 &tmp->fcp_cmd_rsp_dma_handle);
21461
21462 if (!tmp->fcp_cmnd) {
21463 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
21464 "8356 error pool_alloc memory for HDWQ "
21465 "%d %s\n",
21466 lpfc_buf->hdwq_no, __func__);
21467 kfree(tmp);
21468 return NULL;
21469 }
21470
21471 tmp->fcp_rsp = (struct fcp_rsp *)((uint8_t *)tmp->fcp_cmnd +
21472 sizeof(struct fcp_cmnd));
21473
James Smarta4c21ac2019-09-21 20:59:01 -070021474 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021475 list_add_tail(&tmp->list_node, &lpfc_buf->dma_cmd_rsp_list);
21476 }
21477
21478 allocated_buf = list_last_entry(&lpfc_buf->dma_cmd_rsp_list,
21479 struct fcp_cmd_rsp_buf,
21480 list_node);
21481
James Smarta4c21ac2019-09-21 20:59:01 -070021482 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021483
21484 return allocated_buf;
21485}
21486
21487/**
21488 * lpfc_put_cmd_rsp_buf_per_hdwq - Put one CMD/RSP buffer into hdwq pool
21489 * @phba: The HBA for which this call is being executed.
21490 * @lpfc_buf: IO buf structure with the CMD/RSP buf
21491 *
21492 * This routine puts one CMD/RSP buffer into executing CPU's CMD/RSP pool.
21493 *
21494 * Return codes:
21495 * 0 - Success
21496 * -EINVAL - Error
21497 **/
21498int
21499lpfc_put_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21500 struct lpfc_io_buf *lpfc_buf)
21501{
21502 int rc = 0;
21503 struct fcp_cmd_rsp_buf *list_entry = NULL;
21504 struct fcp_cmd_rsp_buf *tmp = NULL;
21505 struct lpfc_sli4_hdw_queue *hdwq = lpfc_buf->hdwq;
21506 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
James Smarta4c21ac2019-09-21 20:59:01 -070021507 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070021508
James Smarta4c21ac2019-09-21 20:59:01 -070021509 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021510
21511 if (likely(!list_empty(&lpfc_buf->dma_cmd_rsp_list))) {
21512 list_for_each_entry_safe(list_entry, tmp,
21513 &lpfc_buf->dma_cmd_rsp_list,
21514 list_node) {
21515 list_move_tail(&list_entry->list_node,
21516 buf_list);
21517 }
21518 } else {
21519 rc = -EINVAL;
21520 }
21521
James Smarta4c21ac2019-09-21 20:59:01 -070021522 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021523 return rc;
21524}
21525
21526/**
21527 * lpfc_free_cmd_rsp_buf_per_hdwq - Free all CMD/RSP chunks of hdwq pool
21528 * @phba: phba object
21529 * @hdwq: hdwq to cleanup cmd rsp buff resources on
21530 *
21531 * This routine frees all CMD/RSP buffers of hdwq's CMD/RSP buf pool.
21532 *
21533 * Return codes:
21534 * None
21535 **/
21536void
21537lpfc_free_cmd_rsp_buf_per_hdwq(struct lpfc_hba *phba,
21538 struct lpfc_sli4_hdw_queue *hdwq)
21539{
21540 struct list_head *buf_list = &hdwq->cmd_rsp_buf_list;
21541 struct fcp_cmd_rsp_buf *list_entry = NULL;
21542 struct fcp_cmd_rsp_buf *tmp = NULL;
James Smarta4c21ac2019-09-21 20:59:01 -070021543 unsigned long iflags;
James Smartd79c9e92019-08-14 16:57:09 -070021544
James Smarta4c21ac2019-09-21 20:59:01 -070021545 spin_lock_irqsave(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021546
21547 /* Free cmd_rsp buf pool */
21548 list_for_each_entry_safe(list_entry, tmp,
21549 buf_list,
21550 list_node) {
21551 dma_pool_free(phba->lpfc_cmd_rsp_buf_pool,
21552 list_entry->fcp_cmnd,
21553 list_entry->fcp_cmd_rsp_dma_handle);
21554 list_del(&list_entry->list_node);
21555 kfree(list_entry);
21556 }
21557
James Smarta4c21ac2019-09-21 20:59:01 -070021558 spin_unlock_irqrestore(&hdwq->hdwq_lock, iflags);
James Smartd79c9e92019-08-14 16:57:09 -070021559}